-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
236 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict' | ||
|
||
const { parseHeaders } = require('../core/util') | ||
const { InvalidArgumentError } = require('../core/errors') | ||
|
||
const kResume = Symbol('resume') | ||
|
||
class UnwrapController { | ||
#paused = false | ||
#reason = null | ||
#aborted = false | ||
#abort | ||
|
||
[kResume] = null | ||
|
||
constructor (abort) { | ||
this.#abort = abort | ||
} | ||
|
||
pause () { | ||
this.#paused = true | ||
} | ||
|
||
resume () { | ||
if (this.#paused) { | ||
this.#paused = false | ||
this[kResume]?.() | ||
} | ||
} | ||
|
||
abort (reason) { | ||
if (!this.#aborted) { | ||
this.#aborted = true | ||
this.#reason = reason | ||
this.#abort(reason) | ||
} | ||
} | ||
|
||
get aborted () { | ||
return this.#aborted | ||
} | ||
|
||
get reason () { | ||
return this.#reason | ||
} | ||
|
||
get paused () { | ||
return this.#paused | ||
} | ||
} | ||
|
||
module.exports = class UnwrapHandler { | ||
#handler | ||
#controller | ||
|
||
constructor (handler) { | ||
this.#handler = handler | ||
} | ||
|
||
static unwrap (handler) { | ||
// TODO (fix): More checks... | ||
return handler.onConnect ? handler : new UnwrapHandler(handler) | ||
} | ||
|
||
onConnect (abort, context) { | ||
this.#controller = new UnwrapController(abort) | ||
this.#handler.onRequestStart?.(this.#controller, context) | ||
} | ||
|
||
onUpgrade (statusCode, rawHeaders, socket) { | ||
this.#handler.onRequestUpgrade?.(statusCode, parseHeaders(rawHeaders), socket) | ||
} | ||
|
||
onHeaders (statusCode, rawHeaders, resume, statusMessage) { | ||
this.#controller[kResume] = resume | ||
this.#handler.onResponseStart?.(this.#controller, statusCode, statusMessage) | ||
this.#handler.onResponseHeaders?.(this.#controller, parseHeaders(rawHeaders)) | ||
return !this.#controller.paused | ||
} | ||
|
||
onData (data) { | ||
this.#handler.onResponseData?.(this.#controller, data) | ||
return !this.#controller.paused | ||
} | ||
|
||
onComplete (rawTrailers) { | ||
this.#handler.onResponseTrailer?.(this.#controller, parseHeaders(rawTrailers)) | ||
this.#handler.onResponseEnd?.(this.#controller) | ||
} | ||
|
||
onError (err) { | ||
if (!this.#handler.onError) { | ||
throw new InvalidArgumentError('invalid onError method') | ||
} | ||
|
||
this.#handler.onResponseError?.(this.#controller, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
'use strict' | ||
|
||
const { InvalidArgumentError } = require('../core/errors') | ||
|
||
module.exports = class WrapHandler { | ||
#handler | ||
#statusCode = 0 | ||
#statusMessage = '' | ||
#trailers = {} | ||
|
||
constructor (handler) { | ||
this.#handler = handler | ||
} | ||
|
||
static wrap (handler) { | ||
// TODO (fix): More checks... | ||
return handler.onRequestStart ? handler : new WrapHandler(handler) | ||
} | ||
|
||
// Unwrap Interface | ||
|
||
onConnect (abort, context) { | ||
return this.#handler.onConnect?.(abort, context) | ||
} | ||
|
||
onHeaders (statusCode, rawHeaders, resume, statusMessage) { | ||
return this.#handler.onHeaders?.(statusCode, rawHeaders, resume, statusMessage) | ||
} | ||
|
||
onUpgrade (statusCode, rawHeaders, socket) { | ||
return this.#handler.onUpgrade?.(statusCode, rawHeaders, socket) | ||
} | ||
|
||
onData (data) { | ||
return this.#handler.onData?.(data) | ||
} | ||
|
||
onComplete (trailers) { | ||
return this.#handler.onComplete?.(trailers) | ||
} | ||
|
||
onError (err) { | ||
if (!this.#handler.onError) { | ||
throw new InvalidArgumentError('invalid onError method') | ||
} | ||
|
||
return this.#handler.onError?.(err) | ||
} | ||
|
||
// Wrap Interface | ||
|
||
onRequestStart (controller, context) { | ||
this.#handler.onConnect?.((reason) => controller.abort(reason), context) | ||
this.#statusCode = 0 | ||
this.#statusMessage = '' | ||
this.#trailers = {} | ||
} | ||
|
||
onRequestError (controller, error) { | ||
if (!this.#handler.onError) { | ||
throw new InvalidArgumentError('invalid onError method') | ||
} | ||
|
||
this.#handler.onError?.(error) | ||
} | ||
|
||
onRequestUpgrade (statusCode, headers, socket) { | ||
const rawHeaders = [] | ||
for (const [key, val] of Object.entries(headers)) { | ||
// TODO (fix): What if val is Array | ||
rawHeaders.push(Buffer.from(key), Buffer.from(val)) | ||
} | ||
|
||
this.#handler.onUpgrade?.(statusCode, rawHeaders, socket) | ||
} | ||
|
||
onResponseStart (controller, statusCode, statusMessage) { | ||
this.#statusCode = statusCode | ||
this.#statusMessage = statusMessage | ||
} | ||
|
||
onResponseHeaders (controller, headers) { | ||
const rawHeaders = [] | ||
for (const [key, val] of Object.entries(headers)) { | ||
// TODO (fix): What if val is Array | ||
rawHeaders.push(Buffer.from(key), Buffer.from(val)) | ||
} | ||
|
||
if (this.#handler.onHeaders?.( | ||
this.#statusCode, | ||
rawHeaders, | ||
() => controller.resume(), | ||
this.#statusMessage | ||
) === false) { | ||
controller.pause() | ||
} | ||
} | ||
|
||
onResponseData (controller, data) { | ||
if (this.#handler.onData?.(data) === false) { | ||
controller.pause() | ||
} | ||
} | ||
|
||
onResponseTrailer (controller, trailers) { | ||
this.#trailers = trailers | ||
} | ||
|
||
onResponseEnd (controller) { | ||
const rawTrailers = [] | ||
for (const [key, val] of Object.entries(this.#trailers)) { | ||
// TODO (fix): What if val is Array | ||
rawTrailers.push(Buffer.from(key), Buffer.from(val)) | ||
} | ||
|
||
this.#handler.onComplete?.(rawTrailers) | ||
} | ||
|
||
onResponseError (controller, error) { | ||
if (!this.#handler.onError) { | ||
throw new InvalidArgumentError('invalid onError method') | ||
} | ||
|
||
this.#handler.onError?.(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.