|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { InvalidArgumentError } = require('../core/errors') |
| 4 | + |
| 5 | +module.exports = class WrapHandler { |
| 6 | + #handler |
| 7 | + #statusCode = 0 |
| 8 | + #statusMessage = '' |
| 9 | + #trailers = {} |
| 10 | + |
| 11 | + constructor (handler) { |
| 12 | + this.#handler = handler |
| 13 | + } |
| 14 | + |
| 15 | + static wrap (handler) { |
| 16 | + // TODO (fix): More checks... |
| 17 | + return handler.onRequestStart ? handler : new WrapHandler(handler) |
| 18 | + } |
| 19 | + |
| 20 | + // Unwrap Interface |
| 21 | + |
| 22 | + onConnect (abort, context) { |
| 23 | + return this.#handler.onConnect?.(abort, context) |
| 24 | + } |
| 25 | + |
| 26 | + onHeaders (statusCode, rawHeaders, resume, statusMessage) { |
| 27 | + return this.#handler.onHeaders?.(statusCode, rawHeaders, resume, statusMessage) |
| 28 | + } |
| 29 | + |
| 30 | + onUpgrade (statusCode, rawHeaders, socket) { |
| 31 | + return this.#handler.onUpgrade?.(statusCode, rawHeaders, socket) |
| 32 | + } |
| 33 | + |
| 34 | + onData (data) { |
| 35 | + return this.#handler.onData?.(data) |
| 36 | + } |
| 37 | + |
| 38 | + onComplete (trailers) { |
| 39 | + return this.#handler.onComplete?.(trailers) |
| 40 | + } |
| 41 | + |
| 42 | + onError (err) { |
| 43 | + if (!this.#handler.onError) { |
| 44 | + throw new InvalidArgumentError('invalid onError method') |
| 45 | + } |
| 46 | + |
| 47 | + return this.#handler.onError?.(err) |
| 48 | + } |
| 49 | + |
| 50 | + // Wrap Interface |
| 51 | + |
| 52 | + onRequestStart (controller, context) { |
| 53 | + this.#handler.onConnect?.((reason) => controller.abort(reason), context) |
| 54 | + this.#statusCode = 0 |
| 55 | + this.#statusMessage = '' |
| 56 | + this.#trailers = {} |
| 57 | + } |
| 58 | + |
| 59 | + onRequestError (controller, error) { |
| 60 | + if (!this.#handler.onError) { |
| 61 | + throw new InvalidArgumentError('invalid onError method') |
| 62 | + } |
| 63 | + |
| 64 | + this.#handler.onError?.(error) |
| 65 | + } |
| 66 | + |
| 67 | + onRequestUpgrade (statusCode, headers, socket) { |
| 68 | + const rawHeaders = [] |
| 69 | + for (const [key, val] of Object.entries(headers)) { |
| 70 | + // TODO (fix): What if val is Array |
| 71 | + rawHeaders.push(Buffer.from(key), Buffer.from(val)) |
| 72 | + } |
| 73 | + |
| 74 | + this.#handler.onUpgrade?.(statusCode, rawHeaders, socket) |
| 75 | + } |
| 76 | + |
| 77 | + onResponseStart (controller, statusCode, statusMessage) { |
| 78 | + this.#statusCode = statusCode |
| 79 | + this.#statusMessage = statusMessage |
| 80 | + } |
| 81 | + |
| 82 | + onResponseHeaders (controller, headers) { |
| 83 | + const rawHeaders = [] |
| 84 | + for (const [key, val] of Object.entries(headers)) { |
| 85 | + // TODO (fix): What if val is Array |
| 86 | + rawHeaders.push(Buffer.from(key), Buffer.from(val)) |
| 87 | + } |
| 88 | + |
| 89 | + if (this.#handler.onHeaders?.( |
| 90 | + this.#statusCode, |
| 91 | + rawHeaders, |
| 92 | + () => controller.resume(), |
| 93 | + this.#statusMessage |
| 94 | + ) === false) { |
| 95 | + controller.pause() |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + onResponseData (controller, data) { |
| 100 | + if (this.#handler.onData?.(data) === false) { |
| 101 | + controller.pause() |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + onResponseTrailer (controller, trailers) { |
| 106 | + this.#trailers = trailers |
| 107 | + } |
| 108 | + |
| 109 | + onResponseEnd (controller) { |
| 110 | + const rawTrailers = [] |
| 111 | + for (const [key, val] of Object.entries(this.#trailers)) { |
| 112 | + // TODO (fix): What if val is Array |
| 113 | + rawTrailers.push(Buffer.from(key), Buffer.from(val)) |
| 114 | + } |
| 115 | + |
| 116 | + this.#handler.onComplete?.(rawTrailers) |
| 117 | + } |
| 118 | + |
| 119 | + onResponseError (controller, error) { |
| 120 | + if (!this.#handler.onError) { |
| 121 | + throw new InvalidArgumentError('invalid onError method') |
| 122 | + } |
| 123 | + |
| 124 | + this.#handler.onError?.(error) |
| 125 | + } |
| 126 | +} |
0 commit comments