Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Apr 26, 2021
1 parent 6e8df73 commit ebf932c
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 55 deletions.
4 changes: 4 additions & 0 deletions lib/internal/quic/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ function onStreamHeaders() {}

function onStreamBlocked() {}

let initialized = false;

module.exports = {
initializeBinding() {
if (initialized) return;
initialized = true;
initializeCallbacks({
onEndpointClose,
onEndpointDone,
Expand Down
151 changes: 136 additions & 15 deletions lib/internal/quic/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ const kRandomConnectionIdStrategy = new RandomConnectionIDStrategy();
* @property {string} [ccAlgorithm]
* @property {UDPOptions} [udp]
* @property {ArrayBuffer|TypedArray|DataView} [resetTokenSecret]
* @property {AbortSignal} [signal]
*
* @typedef {Object} PreferredAddress
* @property {SocketAddressOrOptions} [ipv4]
Expand Down Expand Up @@ -176,6 +175,22 @@ const kRandomConnectionIdStrategy = new RandomConnectionIDStrategy();
* @property {AbortSignal} [signal]
* @typedef {EndpointConfig|EndpointConfigOptions} EndpointConfigOrOptions
* @typedef {SessionConfig|SessionConfigOptions} SessionConfigOrOptions
*
* @typedef {import('../blob.js').Blob} Blob
* @typedef {import('stream').Readable} Readable
* @typedef {ArrayBuffer|TypedArray|DataView|Blob|Readable|string} StreamPayload
* @typedef {Object} StreamOptionsInit
* @property {boolean} [unidirectional]
* @property {Object|Map<string,string>} [headers]
* @property {Object|Map<string,string>} [trailers]
* @property {StreamPayload|Promise<StreamPayload>} [body]
* @property {string} [encoding]
*
* @typedef {Object} ResponseOptionsInit
* @property {Object|Map<string,string>} [headers]
* @property {Object|Map<string,string>} [trailers]
* @property {StreamPayload|Promise<StreamPayload>} [body]
* @property {string} [encoding]
*/
class EndpointConfig {
[kType] = 'endpoint-config';
Expand Down Expand Up @@ -214,7 +229,6 @@ class EndpointConfig {
ccAlgorithm,
udp,
resetTokenSecret,
signal,
} = options;

if (!SocketAddress.isSocketAddress(address)) {
Expand Down Expand Up @@ -377,13 +391,6 @@ class EndpointConfig {
}
}

if (signal !== undefined) {
validateAbortSignal(signal, 'options.signal');
if (signal.aborted)
throw new AbortError();
}


this[kOptions] = {
address,
retryTokenExpiration,
Expand All @@ -408,7 +415,6 @@ class EndpointConfig {
sendBufferSize,
ttl,
resetTokenSecret: resetTokenSecret || '(generated)',
signal,
};

this[kHandle] = new ConfigObject(
Expand Down Expand Up @@ -455,11 +461,6 @@ class EndpointConfig {

return `EndpointConfig ${inspect(this[kOptions], opts)}`;
}

/** @type {AbortSignal} */
get signal() {
return this[kOptions].signal;
}
}

class SessionConfig {
Expand Down Expand Up @@ -863,6 +864,124 @@ class SessionConfig {
}
}

class StreamOptions {
[kType] = 'stream-options';

/**
* @param {*} val
* @returns
*/
static isStreamOptions(val) {
return val?.[kType] === 'stream-options';
}

/**
* @param {StreamOptionsInit} [options]
*/
constructor(options = {}) {
validateObject(options, 'options');
const {
unidirectional = false,
headers,
trailers,
body,
encoding,
} = options;

this[kOptions] = {
unidirectional,
headers,
trailers,
body,
encoding,
};
}

get unidirectional() {
return this[kOptions].unidirectional;
}

get headers() {
return this[kOptions].headers;
}

get body() {
return this[kOptions].body;
}

get encoding() {
return this[kOptions].encoding;
}

[kInspect](depth, options) {
if (depth < 0)
return this;

const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1
};

return `StreamOptions ${inspect(this[kOptions], opts)}`;
}
}

class ResponseOptions {
[kType] = 'response-options';

/**
* @param {*} val
* @returns
*/
static isResponseOptions(val) {
return val?.[kType] === 'response-options';
}

/**
* @param {ResponseOptionsInit} [options]
*/
constructor(options = {}) {
validateObject(options, 'options');
const {
headers,
trailers,
body,
encoding,
} = options;

this[kOptions] = {
headers,
trailers,
body,
encoding,
};
}

get headers() {
return this[kOptions].headers;
}

get body() {
return this[kOptions].body;
}

get encoding() {
return this[kOptions].encoding;
}

[kInspect](depth, options) {
if (depth < 0)
return this;

const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1
};

return `ResponseOptions ${inspect(this[kOptions], opts)}`;
}
}

/**
* @param {ArrayBuffer|TypedArray|DataView} sessionTicket
* @param {ArrayBuffer|TypedArray|DataView} transportParams
Expand Down Expand Up @@ -900,6 +1019,8 @@ function validateResumeOptions(sessionTicket, transportParams) {
module.exports = {
EndpointConfig,
SessionConfig,
StreamOptions,
ResponseOptions,
validateResumeOptions,
kSecureContext,
kHandle,
Expand Down
Loading

0 comments on commit ebf932c

Please sign in to comment.