diff --git a/benchmark/_test-double-benchmarker.js b/benchmark/_test-double-benchmarker.js index 2d05f9fd92be..de24e2e27657 100644 --- a/benchmark/_test-double-benchmarker.js +++ b/benchmark/_test-double-benchmarker.js @@ -5,15 +5,17 @@ if (!['http', 'https', 'http2'].includes(myModule)) { throw new Error(`Invalid module for benchmark test double: ${myModule}`); } +const url = process.env.test_url; + let options; -if (myModule === 'https') { +if (myModule === 'https' || + (myModule === 'http2' && url.startsWith('https:'))) { options = { rejectUnauthorized: false }; } const http = require(myModule); const duration = +process.env.duration; -const url = process.env.test_url; const start = process.hrtime(); let throughput = 0; @@ -45,7 +47,7 @@ function run() { http.get(url, request); } } else { // HTTP/2 - const client = http.connect(url); + const client = http.connect(url, options); client.on('error', () => {}); request(client.request(), client); } diff --git a/benchmark/http/fetch-server.js b/benchmark/http/fetch-server.js new file mode 100644 index 000000000000..633d785defa7 --- /dev/null +++ b/benchmark/http/fetch-server.js @@ -0,0 +1,51 @@ +'use strict'; + +// Benchmarks the fetch() handler of the experimental Fetch Server API +// (http.fetchServer) over HTTP/1.1, with and without TLS. + +const common = require('../common.js'); +const { default_http_benchmarker } = require('../_http-benchmarkers.js'); + +const bench = common.createBenchmark(main, { + tls: [0, 1], + len: [4, 1024, 102400], + c: [50, 500], + duration: 5, +}, { flags: ['--experimental-http-serve', '--no-warnings'] }); + +function main({ tls, len, c, duration }) { + const { serve, ServerResponse } = require('http').fetchServer; + const body = Buffer.alloc(len, 'x'); + const options = { port: 0, hostname: '127.0.0.1' }; + if (tls) { + const fixtures = require('../../test/common/fixtures'); + options.tls = { + key: fixtures.readKey('rsa_private.pem'), + cert: fixtures.readKey('rsa_cert.crt'), + }; + } + const server = serve({ + fetch() { + return new ServerResponse(body, { + headers: { 'content-type': 'text/plain' }, + }); + }, + }, options); + + server.ready.then(() => { + const [listener] = server; + // The test double for plain HTTP cannot speak TLS. + const benchmarker = tls && default_http_benchmarker === 'test-double-http' ? + 'test-double-https' : undefined; + bench.http({ + path: '/', + port: listener.address.port, + scheme: tls ? 'https' : 'http', + connections: c, + duration, + benchmarker, + }, () => { + server.destroy(); + }); + }); +} diff --git a/benchmark/http2/fetch-server.js b/benchmark/http2/fetch-server.js new file mode 100644 index 000000000000..33a8edf7830e --- /dev/null +++ b/benchmark/http2/fetch-server.js @@ -0,0 +1,53 @@ +'use strict'; + +// Benchmarks the fetch() handler of the experimental Fetch Server API +// (http.fetchServer) over HTTP/2, with prior knowledge (h2c) and over TLS. + +const common = require('../common.js'); + +const bench = common.createBenchmark(main, { + tls: [0, 1], + len: [4, 1024, 102400], + requests: [100, 1000, 5000], + streams: [1, 10, 100], + clients: [2], + benchmarker: ['test-double-http2'], + duration: 5, +}, { flags: ['--experimental-http-serve', '--no-warnings'] }); + +function main({ tls, len, requests, streams, clients, duration }) { + const { serve, ServerResponse } = require('http').fetchServer; + const body = Buffer.alloc(len, 'x'); + const options = { port: 0, hostname: '127.0.0.1' }; + if (tls) { + const fixtures = require('../../test/common/fixtures'); + options.tls = { + key: fixtures.readKey('rsa_private.pem'), + cert: fixtures.readKey('rsa_cert.crt'), + alpn: ['h2'], + }; + } + const server = serve({ + fetch() { + return new ServerResponse(body, { + headers: { 'content-type': 'text/plain' }, + }); + }, + }, options); + + server.ready.then(() => { + const [listener] = server; + bench.http({ + path: '/', + port: listener.address.port, + scheme: tls ? 'https' : 'http', + requests, + maxConcurrentStreams: streams, + clients, + threads: clients, + duration, + }, () => { + server.destroy(); + }); + }); +} diff --git a/doc/api/cli.md b/doc/api/cli.md index 5afcc6edaa5b..cd328dfe584a 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1470,6 +1470,16 @@ added: Enable exposition of [EventSource Web API][] on the global scope. +### `--experimental-http-serve` + + + +> Stability: 1.0 - Early development + +Enable the experimental [Fetch Server API][] exposed as `http.fetchServer`. + ### `--experimental-import-meta-resolve` + +> Stability: 1.0 - Early development. Enable this API with the +> [`--experimental-http-serve`][] CLI flag. + +`http.fetchServer` is a prototype of the WinterTC [Fetch Server API][] +proposal. It is a server model built on request and response objects: a +handler receives a context for each exchange and returns a response. The API +is independent of the [`http.Server`][] and [`http.ServerResponse`][] classes +documented above; it is available only when the +[`--experimental-http-serve`][] flag is set, and accessing `http.fetchServer` +emits an `ExperimentalWarning` the first time. + +```mjs +import http from 'node:http'; +const { serve, ServerResponse } = http.fetchServer; + +const server = serve({ + async fetch(ctx) { + const url = new URL(ctx.request.url); + if (url.pathname === '/hello') { + return ServerResponse.json({ hello: 'world' }); + } + return new ServerResponse('Not found', { status: 404 }); + }, +}, { port: 8080 }); + +await server.ready; +``` + +```cjs +const http = require('node:http'); +const { serve, ServerResponse } = http.fetchServer; + +const server = serve({ + async fetch(ctx) { + const url = new URL(ctx.request.url); + if (url.pathname === '/hello') { + return ServerResponse.json({ hello: 'world' }); + } + return new ServerResponse('Not found', { status: 404 }); + }, +}, { port: 8080 }); + +server.ready.then(() => console.log('listening')); +``` + +The implementation is built on the `node:http` and `node:http2` modules. A +listener without TLS accepts HTTP/1.1 and HTTP/2 with prior knowledge (h2c), +distinguished by the HTTP/2 connection preface. A listener with TLS negotiates +`h2` or `http/1.1` using ALPN. HTTP/3, QUIC, and WebTransport are currently not +supported; see [Fetch Server API limitations][]. + +The objects that the API supplies to a handler (contexts, requests, and +headers) are protocol objects: they can be wrapped or substituted by +middleware. For example, a context can be extended with `Object.create(ctx)` +and still be used with every method of the original context. + +### Handlers + +A handler is an object whose methods the server calls. The following methods +are currently defined; at least one of `fetch` and `connect` must be present. +Other properties of the handler are ignored. + +* `fetch(ctx)`: Handles a request/response exchange. `ctx` is a + {ServerContext}. It returns, or resolves to, a + {fetchServer.ServerResponse} or a WHATWG {Response}, or `undefined` if it + called [`ctx.deny()`][]. +* `connect(ctx)`: Handles `CONNECT` requests, HTTP/2 extended `CONNECT` + requests, and HTTP/1.1 requests that carry an `Upgrade` header field. `ctx` + is a {ConnectContext}. It establishes a tunnel with + [`ctx.accept()`][], [`ctx.upgradeWebSocket()`][], or + [`ctx.upgradeWebSocketStream()`][], or refuses it with a response or + [`ctx.deny()`][]. +* `error(ctx, error)`: Called when `fetch()` or `connect()` throws, rejects, + or returns an invalid value before a response has been committed. It can + return a response to send instead. If it is absent, throws, or returns + nothing usable, a fallback response with status 500 is sent (502 for + `CONNECT`, and 413 when the request content exceeds + `limits.maxRequestBodySize`). + +The methods are called with the handler object as `this`. A handler can +declare the protocol version it implements by setting the +`Symbol.for('wintertc.server.protocol')` property to `1`; any other value is +rejected. + +When a request arrives for a method that has no handler, the server responds +with status 501. An HTTP/1.1 upgrade request is routed to `connect()` only if +it is present; otherwise the `Upgrade` header field is ignored and the request +is routed to `fetch()`. A `connect()` handler can also decline only the upgrade +offer, so that the request is handled by `fetch()`, by calling +`ctx.deny(Symbol.for('wintertc.server.fallthrough'))`. + +Errors that are not handled by `error()`, such as rejections of +[`ctx.waitUntil()`][] promises, are reported as process warnings. + +### `http.fetchServer.serve(handler[, options])` + + + +* `handler` {Object} A handler object. See [Fetch Server handlers][]. +* `options` {Object} + * `autoContinue` {boolean} Whether a `100 (Continue)` response is sent + automatically when the handler starts reading the content of a request + that carries `Expect: 100-continue`. **Default:** `true`. + * `signal` {AbortSignal} Destroys the server when aborted, with the abort + reason. + * `hostname`, `port`, `tls`, and the other members of + [Fetch Server listen options][]. If `port` is given, the server binds a + first listener with these options. +* Returns: {fetchServer.Server} + +Creates a server for `handler`. If `options.port` is absent, the server has no +listeners until [`server.listen()`][fetchServer.server.listen] is called. + +```mjs +import http from 'node:http'; +const { serve, ServerResponse } = http.fetchServer; + +const server = serve({ + fetch: () => new ServerResponse('ok'), +}); +const listener = await server.listen({ port: 0, hostname: '127.0.0.1' }); +console.log(listener.address.port); +``` + +#### Fetch Server listen options + +The following members are accepted by both `serve()` and +[`server.listen()`][fetchServer.server.listen]: + +* `hostname` {string} The address to bind. **Default:** all interfaces. +* `port` {integer} The port to bind. `0` selects an available port. +* `backlog` {integer} The listen backlog. +* `ipv6Only` {boolean} Whether to bind only IPv6 when `hostname` is `::`. + **Default:** `false`. +* `reusePort` {boolean} See the `reusePort` option of [`server.listen()`][]. + **Default:** `false`. +* `tls` {Object} Enables TLS. + * `cert` {string|ArrayBuffer|TypedArray|DataView} The certificate chain in + PEM format. + * `key` {string|ArrayBuffer|TypedArray|DataView} The private key in PEM + format. + * `alpn` {string\[]} The ALPN protocols to offer, in order of preference. + Only `'h2'` and `'http/1.1'` are supported. **Default:** + `['h2', 'http/1.1']`. + * `sni` {Object|Function} Either an object that maps host names to + `{ cert, key }` objects, or a function that is called with the host name + and returns such an object, or a promise for one. +* `altSvc` {string|boolean} An `Alt-Svc` header field value to add to every + response sent over TLS that does not already have one. A boolean, which the + proposal uses to advertise HTTP/3, is accepted and has no effect. +* `extendedConnect` {boolean} Whether HTTP/2 extended `CONNECT` ([RFC 8441][]) + is enabled. **Default:** `true` if the handler has a `connect()` method. +* `limits` {Object} + * `maxHeaderSize` {integer} The maximum size of a request head, in bytes. + * `maxRequestBodySize` {integer} The maximum size of request content, in + bytes. A request that declares a larger size is answered with status 413 + before the handler runs; a request that exceeds the limit while its + content is read fails with `ERR_HTTP_CONTENT_TOO_LARGE`. `0` disables the + limit. **Default:** `0`. + * `maxRequestsPerConnection` {integer} + * `maxConcurrentStreams` {integer} For HTTP/2. + * `maxConnections` {integer} +* `timeouts` {Object} Activity timeouts, in milliseconds. `0` disables a + timeout. + * `handshake` {integer} For the TLS handshake. + * `headers` {integer} For receiving the request head. + * `idle` {integer} For idle keep-alive connections. + * `closeHandshake` {integer} For a WebSocket peer to complete the closing + handshake. **Default:** `30000`. + * `tunnelIdle` {integer} For a WebSocket connection with no frames flowing + in either direction. **Default:** `0`. + * `waitUntil` {integer} How long an exchange waits for its + [`ctx.waitUntil()`][] promises after the response has completed. + **Default:** `30000`. + * `receive` {integer} + * `send` {integer} +* `quic`, `datagrams`, `webTransport` {boolean} Not supported. Setting `quic` + or `webTransport` to `true` throws a `NotSupportedError` {DOMException}. + +A member given to `listen()` overrides the same member given to `serve()`. The +`limits` and `timeouts` objects are merged member by member. + +### Class: `fetchServer.Server` + + + +A server created by [`http.fetchServer.serve()`][]. It is iterable: iterating +it yields its active {fetchServer.Listener} objects. It supports +`await using`, which closes the server and waits for it to close. + +#### `server.ready` + +* Type: {Promise} + +Fulfilled with `undefined` once the listener requested by `serve()`, if any, is +bound. Rejected if binding fails. + +#### `server.listen([options])` + +* `options` {Object} [Fetch Server listen options][]. +* Returns: {Promise} Fulfills with a {fetchServer.Listener}. + +Binds a new listener. Rejects with an `InvalidStateError` {DOMException} once +the server is closing or closed. + +#### `server.busy` + +* Type: {boolean} + +While `true`, new exchanges are refused without invoking the handler: HTTP/1.1 +requests receive a `503 (Service Unavailable)` response and HTTP/2 streams are +reset with `REFUSED_STREAM`. Setting it sets `listener.busy` on every active +listener. **Default:** `false`. + +#### `server.close()` + +* Returns: {Promise} + +Starts a graceful shutdown of every listener: no new connections are accepted, +HTTP/2 connections receive `GOAWAY`, new exchanges on existing connections are +refused as `busy` refuses them, and WebSocket connections are sent a Close +frame with code 1001 (Going Away). In-flight exchanges, including tunnels and +outstanding [`ctx.waitUntil()`][] promises, are allowed to complete. The +returned promise settles as `server.closed` does. + +#### `server.destroy([error])` + +* `error` {any} + +Terminates every listener and in-flight exchange immediately. `server.closed` +is rejected with `error`, or with an `AbortError` {DOMException}. + +#### `server.closed` + +* Type: {Promise} + +Fulfilled once the server has closed gracefully. Rejected if it was destroyed, +or if the initial bind failed. + +### Class: `fetchServer.Listener` + + + +A bound address of a {fetchServer.Server}. Listeners are obtained from +[`server.listen()`][fetchServer.server.listen] or by iterating the server. +A listener has the same `busy`, `close()`, `destroy([error])`, and `closed` +members as a server, which apply to that listener only, and it supports +`await using`. + +#### `listener.address` + +* Type: {Object} + * `address` {string} + * `port` {integer} + * `family` {string} `'IPv4'` or `'IPv6'`. + +### Class: `fetchServer.ServerResponse` + + + +A response created by a handler. It is similar to the WHATWG {Response}, but +its body is consumed as an async iterable of `Uint8Array` batches rather than +as a {ReadableStream}, and it can carry trailer fields. + +#### `new ServerResponse([body[, init]])` + +* `body` {any} The content: a {string}, an {ArrayBuffer}, a {TypedArray}, a + {DataView}, a {Blob}, a {URLSearchParams}, a {FormData}, a + {ReadableStream}, an async or sync iterable that yields arrays of + `Uint8Array` chunks, another {fetchServer.ServerResponse}, or `null`. An + iterable that yields anything else fails the response when it is sent. + **Default:** `null`. +* `init` {Object} + * `status` {integer} In the range 200 to 599. **Default:** `200`. + * `statusText` {string} **Default:** `''`. + * `headers` {Object|Array|Headers|ServerHeaders} The header fields. + * `trailers` {Promise} A promise for the trailer fields, as any value + accepted by `headers`. Only the fields named in the `Trailer` header + field are sent, and fields that are not permitted in trailers are + dropped. + +As for {Response}, a `Content-Type` header field is added for string, +{URLSearchParams}, and {FormData} bodies, and for a {Blob} that has a type. A body cannot be given with +status 204, 205, or 304. + +#### Static method: `ServerResponse.error()` + +* Returns: {fetchServer.ServerResponse} + +Returns a network error response. Returning it from a handler resets the +stream, or closes the HTTP/1.1 connection, instead of sending a response. + +#### Static method: `ServerResponse.json(data[, init])` + +* `data` {any} +* `init` {Object} As for the constructor. +* Returns: {fetchServer.ServerResponse} + +Returns a response whose body is `JSON.stringify(data)`, with +`Content-Type: application/json`. + +#### Static method: `ServerResponse.redirect(url[, status])` + +* `url` {string} The `Location` header field value. It can be relative. +* `status` {integer} One of 301, 302, 303, 307, or 308. **Default:** `302`. +* Returns: {fetchServer.ServerResponse} + +#### Static method: `ServerResponse.from(response)` + +* `response` {Response} +* Returns: {fetchServer.ServerResponse} + +Converts a WHATWG {Response}, for example one obtained from `fetch()`. The +body of `response` is transferred, so it must not be locked or used. + +#### `serverResponse.status`, `serverResponse.statusText`, `serverResponse.ok`, `serverResponse.type`, `serverResponse.url`, `serverResponse.redirected` + +These have the same meaning as for {Response}. `url` is always `''` and +`redirected` is always `false`. + +#### `serverResponse.headers` + +* Type: {ServerHeaders} + +The header fields. They can be modified until the response is sent. + +#### `serverResponse.trailers` + +* Type: {Promise} Fulfills with a {ServerHeaders}. + +The trailer fields, after the fields that are not declared or not permitted +have been removed. + +#### `serverResponse[Symbol.asyncIterator]()` + +Returns an iterator over the body, which yields arrays of `Uint8Array` chunks. +The body can be read only once. + +#### `serverResponse.clone()` + +* Returns: {fetchServer.ServerResponse} + +Returns a copy whose body yields the same data. Throws if the body has been +read. + +### Class: `ServerHeaders` + +A set of header fields, with the methods of the WHATWG {Headers} class: +`append()`, `delete()`, `get()`, `getSetCookie()`, `has()`, `set()`, +`forEach()`, `entries()`, `keys()`, `values()`, and `[Symbol.iterator]()`. +Unlike {Headers}, it has no guards that forbid particular field names. The +headers of a request and the trailers of a response or request are immutable; +modifying them throws an `ERR_HTTP_HEADERS_SENT` error. + +### Class: `ServerRequest` + +The request of an exchange, available as [`ctx.request`][]. + +* `request.method` {string} +* `request.url` {string} The absolute URL, using the scheme of the connection + and the host given by the request. +* `request.headers` {ServerHeaders} Immutable. +* `request.signal` {AbortSignal} Aborted, with a protocol error, if the client + cancels the request or the connection is lost before the exchange + completes. See [Fetch Server errors][]. +* `request.trailers` {Promise} Fulfills with a {ServerHeaders} of the request + trailer fields once the content has been read. It is rejected for a + `CONNECT` request, which has no content. +* `request[Symbol.asyncIterator]()` Returns an iterator over the content, + which yields arrays of `Uint8Array` chunks. The content can be read only + once. Ending the iteration early refuses the rest of the content. + +```mjs +import http from 'node:http'; +const { serve, ServerResponse } = http.fetchServer; + +serve({ + async fetch({ request }) { + let size = 0; + for await (const chunks of request) { + for (const chunk of chunks) size += chunk.byteLength; + } + return new ServerResponse(`received ${size} bytes`); + }, +}, { port: 8080 }); +``` + +### Class: `ServerContext` + +The context of a request/response exchange, passed to `fetch()`. + +#### `ctx.request` + +* Type: {ServerRequest} + +#### `ctx.remoteAddress`, `ctx.localAddress` + +* Type: {Object} + * `address` {string} + * `port` {integer} + * `family` {string} `'IPv4'` or `'IPv6'`. + +#### `ctx.alpnProtocol` + +* Type: {string} `'http/1.1'`, `'h2'`, or, without TLS, `'h2c'`. + +#### `ctx.encrypted` + +* Type: {boolean} Whether the connection uses TLS. + +#### `ctx.serverName` + +* Type: {string|null} The TLS server name indication sent by the client. + +#### `ctx.clientPriority` + +* Type: {Object} The priority requested by the client with the `Priority` + header field ([RFC 9218][]). + * `urgency` {integer} From 0 to 7. **Default:** `3`. + * `incremental` {boolean} **Default:** `false`. + +#### `ctx.serverPriority` + +* Type: {Object|null} A priority, as for `ctx.clientPriority`, that overrides + the client's. It is recorded but does not yet affect scheduling. + +#### `ctx.onpriority` + +* Type: {Function|null} Called when the client changes the priority of the + request. It is not yet called by this implementation. + +#### `ctx.autoContinue` + +* Type: {boolean} The `autoContinue` option of the server. + +#### `ctx.completed` + +* Type: {Promise} + +Fulfilled when the response has been handed in full to the transport, or the +exchange has been denied. Rejected with the abort reason if the exchange is +aborted. + +#### `ctx.sendInformational(status[, headers])` + +* `status` {integer} From 100 to 199, except 101. +* `headers` {Object|Array|Headers|ServerHeaders} + +Sends an informational response, such as `103 (Early Hints)`, before the final +response. Status 100 can be sent only when the request expects it and +`autoContinue` has not sent it already. Throws an `InvalidStateError` +{DOMException} once a final response has been committed. Informational +responses are not sent to HTTP/1.0 clients. + +#### `ctx.deny([error])` + +* `error` {any} + +Refuses the exchange without a response: the HTTP/2 stream is reset, or the +HTTP/1.1 connection is closed. If `error` has a `code` property of +`'ERR_HTTP_REQUEST_REJECTED'`, `'ERR_HTTP_REQUEST_CANCELLED'`, +`'ERR_HTTP_INTERNAL_ERROR'`, or `'ERR_HTTP_GOAWAY'` (or, in a `connect()` +handler for a `CONNECT` request, `'ERR_HTTP_CONNECT_ERROR'`), the stream is +reset with the corresponding HTTP/2 error code; `'ERR_HTTP_GOAWAY'` also sends `GOAWAY` +for the connection. Otherwise `REFUSED_STREAM` is used, which tells the client +the request was not processed and can be retried. The handler then returns +`undefined`. + +#### `ctx.waitUntil(promise)` + +* `promise` {Promise} + +Extends the lifetime of the exchange until `promise` settles, so that work can +continue after the response has been sent, for example logging. Graceful +shutdown waits for these promises, up to the `timeouts.waitUntil` limit. A +rejection is reported as a process warning. + +`sendInformational`, `deny`, and `waitUntil` are bound to the context, so they +can be destructured. + +### Class: `ConnectContext` + +* Extends: {ServerContext} + +The context passed to `connect()`. + +#### `ctx.connectProtocol` + +* Type: {string|null} + +`null` for a plain `CONNECT` request; the `:protocol` pseudo-header field of an +HTTP/2 extended `CONNECT` request, such as `'websocket'` or `'connect-udp'`; +or, for an HTTP/1.1 upgrade, the first protocol named in the `Upgrade` header +field, lowercased and without its version. + +#### `ctx.accept([init])` + +* `init` {Object} + * `headers` {Object|Array|Headers|ServerHeaders} Header fields for the + response that establishes the tunnel. + * `protocol` {string} For an HTTP/1.1 upgrade, the offered protocol to + select. **Default:** the first protocol offered. +* Returns: {Promise} Fulfills with a {Tunnel}. + +Establishes a tunnel by sending a `200` response (for `CONNECT`) or a +`101 (Switching Protocols)` response (for an HTTP/1.1 upgrade). For the +`connect-udp` and `connect-ip` protocols, a `Capsule-Protocol: ?1` header field +is added. + +A `connect()` handler can instead refuse the tunnel by returning a +{fetchServer.ServerResponse}. For a `CONNECT` request, it must not have a 2xx +status; for an HTTP/1.1 upgrade, any status other than 101 is allowed. + +```mjs +import http from 'node:http'; +import net from 'node:net'; +const { serve, ServerResponse } = http.fetchServer; + +// A minimal CONNECT proxy. +serve({ + async connect(ctx) { + if (ctx.connectProtocol !== null) { + return new ServerResponse(null, { status: 501 }); + } + const { hostname, port } = new URL(ctx.request.url); + const upstream = net.connect(port, hostname); + const tunnel = await ctx.accept(); + (async () => { + for await (const chunk of upstream) await tunnel.writer.write(chunk); + await tunnel.writer.end(); + })(); + for await (const chunks of tunnel.readable) { + for (const chunk of chunks) upstream.write(chunk); + } + upstream.end(); + }, +}, { port: 8080 }); +``` + +#### `ctx.upgradeWebSocket([options])` + +* `options` {Object} + * `protocol` {string\[]} The subprotocols the server supports, in order of + preference. The first that the client offered in + `Sec-WebSocket-Protocol` is selected. If none was offered, no subprotocol + is selected. + * `compression` {boolean} Whether to accept the `permessage-deflate` + extension ([RFC 7692][]) if the client offers it. **Default:** `false`. + * `pingInterval` {integer} If not `0`, a Ping frame is sent whenever this + many milliseconds pass without a frame from the client. **Default:** `0`. +* Returns: {WebSocket} + +Completes a WebSocket opening handshake, over an HTTP/1.1 upgrade or an HTTP/2 +extended `CONNECT` request ([RFC 8441][]), and returns an object with the +interface of the WHATWG {WebSocket}, already open. Messages are dispatched as +`message` events as they arrive, without backpressure. Ping frames are +answered automatically. The object is not an instance of the global +`WebSocket` class. + +Its `close([code[, reason]])` method accepts the close codes that a server can +send: 1000 to 1003, 1007 to 1009, 1011 to 1014, and 3000 to 4999. Messages +passed to `send()` before `close()` are still sent. + +Throws an `InvalidStateError` {DOMException} if `ctx.connectProtocol` is not +`'websocket'`, or the exchange has already been accepted, answered, or denied. +Throws a `TypeError` if the request is not a valid opening handshake; the +handler can then still respond, for example with status 426 and +`Sec-WebSocket-Version: 13`. If the client has already gone away, the returned +object fires `error` and `close` events, with code 1006. + +```mjs +import http from 'node:http'; +const { serve, ServerResponse } = http.fetchServer; + +serve({ + connect(ctx) { + if (ctx.connectProtocol !== 'websocket') { + return new ServerResponse(null, { status: 501 }); + } + const ws = ctx.upgradeWebSocket({ protocol: ['chat'] }); + ws.onmessage = (event) => ws.send(`echo: ${event.data}`); + }, +}, { port: 8080 }); +``` + +#### `ctx.upgradeWebSocketStream([options])` + +* `options` {Object} As for [`ctx.upgradeWebSocket()`][]. +* Returns: {ServerWebSocketStream} + +Completes a WebSocket opening handshake, as `ctx.upgradeWebSocket()` does, and +returns a {ServerWebSocketStream}, whose messages are read and written as +streams with backpressure. + +#### `ctx.upgradeWebTransport()` + +* Returns: {Promise} + +WebTransport is not supported. The promise is rejected with a +`NotSupportedError` {DOMException}. + +### Class: `ServerWebSocketStream` + + + +An established WebSocket connection whose messages are read and written as +streams. It is the server-side counterpart of the proposed `WebSocketStream` +interface. It supports `await using`, which closes the connection and waits +for the closing handshake. + +```mjs +import http from 'node:http'; +const { serve } = http.fetchServer; + +serve({ + async connect(ctx) { + await using wss = ctx.upgradeWebSocketStream(); + const { readable, writable } = await wss.opened; + const writer = writable.getWriter(); + // The next message is not read until this one has been echoed, and each + // write waits while the client is not keeping up. + for await (const message of readable) { + await writer.write(message); + } + }, +}, { port: 8080 }); +``` + +#### `wss.opened` + +* Type: {Promise} Fulfills with an {Object}: + * `readable` {ReadableStream} Each chunk is one received message: a + {string} for a text message, or a {Uint8Array} for a binary message. It + closes once the client has started the closing handshake and every + message has been read. Cancelling it starts the closing handshake. + * `writable` {WritableStream} Each chunk written is sent as one message: + an {ArrayBuffer}, {TypedArray}, or {DataView} as a binary message, and + any other value, converted to a string, as a text message. Closing it + starts the closing handshake without a close code. + * `protocol` {string} The selected subprotocol, or `''`. + * `extensions` {string} The negotiated extensions, or `''`. + +The promise is already settled when the object is returned. It is rejected if +the client had gone away before the upgrade. + +#### `wss.closed` + +* Type: {Promise} + +Fulfills with an {Object} with `closeCode` and `reason` properties, taken from +the client's Close frame (with `closeCode` 1005 if it had none), once the +closing handshake has completed. It is rejected with a protocol error if the +connection is lost, the client violates the protocol, or the closing handshake +times out. See [Fetch Server errors][]. + +#### `wss.close([closeInfo])` + +* `closeInfo` {Object} + * `closeCode` {integer} A close code that a server can send, as for + [`ctx.upgradeWebSocket()`][]. + * `reason` {string} At most 123 bytes when encoded as UTF-8. + **Default:** `''`. + +Starts the closing handshake. Messages written to the writable stream that have +not been sent yet are discarded, and their writes are rejected with an +`InvalidStateError` {DOMException}. To close after every written message has +been sent, close the writable stream instead. + +#### `wss.destroy([error])` + +* `error` {any} + +Terminates the connection immediately, without a closing handshake: the +HTTP/2 stream is reset with `CANCEL`, or the HTTP/1.1 connection is closed. +The streams are errored and `wss.closed` is rejected with `error`, or with a +protocol error with code `ERR_HTTP_REQUEST_CANCELLED`. + +### Class: `Tunnel` + +An established tunnel, returned by [`ctx.accept()`][]. A tunnel is part of its +exchange: the exchange remains in flight until the tunnel closes, even after +`connect()` has returned. + +* `tunnel.readable` {AsyncIterable} The bytes received from the client, as + arrays of `Uint8Array` chunks. +* `tunnel.writer` {Object} A writer for the bytes sent to the client, with + `write(chunk)`, `writev(chunks)`, and `end()` methods that return promises, + and synchronous `writeSync()`, `writevSync()`, and `endSync()` variants. +* `tunnel.capsules()` Returns a {CapsuleStream}, which reads and sends the + data stream as capsules of the Capsule Protocol ([RFC 9297][]). It can be + used instead of `readable` and `writer`, but not for a plain `CONNECT` + tunnel or a WebSocket. +* `tunnel.datagrams()` Returns an object with `readable` and `writer` + members for HTTP Datagrams, which are sent as `DATAGRAM` capsules. + `unreliable` is always `false`. It is available only for protocols that use + the Capsule Protocol, such as `connect-udp`. +* `tunnel.close()` Ends the send direction. Returns a promise that settles as + `tunnel.closed` does. +* `tunnel.destroy([error])` Terminates the tunnel immediately. The HTTP/2 + stream is reset with `CONNECT_ERROR`, or the HTTP/1.1 connection is closed. +* `tunnel.closed` {Promise} Fulfilled once both directions have ended. + Rejected with a protocol error if the tunnel terminates abnormally. + +`Tunnel` supports `await using`. + +### Class: `CapsuleStream` + +The capsules of a tunnel, returned by `tunnel.capsules()`. + +* `capsuleStream.readable` {AsyncIterable} The capsules received from the + client, as objects with `type` {integer} and `data` {Uint8Array} + properties. `DATAGRAM` capsules are delivered by `tunnel.datagrams()` + instead, if it has been called, and are otherwise discarded. +* `capsuleStream.send(capsule)` Sends a capsule, an object with `type` and + `data` properties. Returns a {Promise}. + +### Fetch Server errors + +When an exchange is aborted, for example because the client reset the stream +or the connection was lost, `request.signal` is aborted and pending +operations are rejected with a protocol error: a `TypeError` with one of the +following `code` values. When the error comes from an HTTP/2 `RST_STREAM` +frame, it also has a `detail` property, an object with `protocol`, +`errorCode`, and `errorName` properties that describe the received error code. + +* `ERR_HTTP_REQUEST_REJECTED`: The stream was refused (`REFUSED_STREAM`). +* `ERR_HTTP_REQUEST_CANCELLED`: The client cancelled the request (`CANCEL`). +* `ERR_HTTP_INTERNAL_ERROR`: An internal error (`INTERNAL_ERROR`). +* `ERR_HTTP_STREAM_RESET`: The stream was reset with another error code. +* `ERR_HTTP_GOAWAY`: The connection is going away. +* `ERR_HTTP_PROTOCOL_ERROR`: The client violated the HTTP protocol. +* `ERR_HTTP_CONNECT_ERROR`: A tunnel failed (`CONNECT_ERROR`), the client + violated the WebSocket protocol, or the connection closed before the + WebSocket closing handshake completed. +* `ERR_HTTP_REQUEST_BODY_REJECTED`: The rest of the request content was + refused. +* `ERR_HTTP_CONNECTION_RESET`: The connection was lost. +* `ERR_HTTP_TIMEOUT`: A timeout expired. +* `ERR_HTTP_CONTENT_TOO_LARGE`: The request content exceeded + `limits.maxRequestBodySize`. + +These codes are part of the Fetch Server API and are not listed in +[Errors][]. + +### Fetch Server API limitations + +This prototype differs from the proposal in the following ways: + +* HTTP/3, QUIC, WebTransport, and unreliable datagrams are not supported. +* HTTP/2 stream priorities are not applied: `ctx.serverPriority` is recorded, + and `ctx.onpriority` is never called. +* The object returned by `ctx.upgradeWebSocket()` implements the + {WebSocket} interface but is not an instance of the global `WebSocket` + class. +* WebSocket messages are limited to 64 MiB. A larger message fails the + connection with close code 1009 (Message Too Big). +* The `receive` and `send` timeouts are not enforced, and `tunnelIdle` + applies only to WebSocket connections. + [Built-in Proxy Support]: #built-in-proxy-support +[Errors]: errors.md +[Fetch Server API]: https://fetch-server.proposal.wintertc.org/ +[Fetch Server API limitations]: #fetch-server-api-limitations +[Fetch Server errors]: #fetch-server-errors +[Fetch Server handlers]: #handlers +[Fetch Server listen options]: #fetch-server-listen-options [RFC 6455, Section 5.3]: https://datatracker.ietf.org/doc/html/rfc6455#section-5.3 +[RFC 7692]: https://www.rfc-editor.org/rfc/rfc7692 [RFC 8187]: https://www.rfc-editor.org/rfc/rfc8187.txt +[RFC 8441]: https://www.rfc-editor.org/rfc/rfc8441 [RFC 9110 Section 6.6.1]: https://www.rfc-editor.org/rfc/rfc9110#section-6.6.1 +[RFC 9218]: https://www.rfc-editor.org/rfc/rfc9218 +[RFC 9297]: https://www.rfc-editor.org/rfc/rfc9297 [`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`]: errors.md#err_http_content_length_mismatch [`'checkContinue'`]: #event-checkcontinue [`'finish'`]: #event-finish [`'request'`]: #event-request [`'response'`]: #event-response [`'upgrade'`]: #event-upgrade +[`--experimental-http-serve`]: cli.md#--experimental-http-serve [`--insecure-http-parser`]: cli.md#--insecure-http-parser [`--max-http-header-size`]: cli.md#--max-http-header-sizesize [`Agent`]: #class-httpagent @@ -4958,6 +5744,12 @@ const agent2 = new http.Agent({ proxyEnv: process.env }); [`URL`]: url.md#the-whatwg-url-api [`agent.createConnection()`]: #agentcreateconnectionoptions-callback [`agent.getName()`]: #agentgetnameoptions +[`ctx.accept()`]: #ctxacceptinit +[`ctx.deny()`]: #ctxdenyerror +[`ctx.request`]: #ctxrequest +[`ctx.upgradeWebSocket()`]: #ctxupgradewebsocketoptions +[`ctx.upgradeWebSocketStream()`]: #ctxupgradewebsocketstreamoptions +[`ctx.waitUntil()`]: #ctxwaituntilpromise [`destroy()`]: #agentdestroy [`dns.lookup()`]: dns.md#dnslookuphostname-options-callback [`dns.lookup()` hints]: dns.md#supported-getaddrinfo-flags @@ -4968,6 +5760,7 @@ const agent2 = new http.Agent({ proxyEnv: process.env }); [`http.ServerResponse`]: #class-httpserverresponse [`http.Server`]: #class-httpserver [`http.createServer()`]: #httpcreateserveroptions-requestlistener +[`http.fetchServer.serve()`]: #httpfetchserverservehandler-options [`http.get()`]: #httpgetoptions-callback [`http.globalAgent`]: #httpglobalagent [`http.request()`]: #httprequestoptions-callback @@ -5034,6 +5827,7 @@ const agent2 = new http.Agent({ proxyEnv: process.env }); [`writable.uncork()`]: stream.md#writableuncork [`writable.write()`]: stream.md#writablewritechunk-encoding-callback [absolute form]: https://datatracker.ietf.org/doc/html/rfc9112#section-3.2.2 +[fetchServer.server.listen]: #serverlistenoptions [information event]: #event-information [initial delay]: net.md#socketsetkeepaliveenable-initialdelay-interval-count [request target]: https://datatracker.ietf.org/doc/html/rfc9112#section-3.2 diff --git a/doc/node-config-schema.json b/doc/node-config-schema.json index 0d2cb83b517d..851d5073aef5 100644 --- a/doc/node-config-schema.json +++ b/doc/node-config-schema.json @@ -351,6 +351,10 @@ "type": "boolean", "description": "expose experimental Navigator API on the global scope" }, + "experimental-http-serve": { + "type": "boolean", + "description": "experimental Fetch Server API (http.fetchServer)" + }, "experimental-import-meta-resolve": { "type": "boolean", "description": "experimental ES Module import.meta.resolve() parentURL support" diff --git a/doc/node.1 b/doc/node.1 index 32fd0290582d..5c574e07b908 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -862,6 +862,9 @@ dtls documentation for details. .It Fl -experimental-eventsource Enable exposition of EventSource Web API on the global scope. . +.It Fl -experimental-http-serve +Enable the experimental Fetch Server API exposed as \fBhttp.fetchServer\fR. +. .It Fl -experimental-import-meta-resolve Enable experimental \fBimport.meta.resolve()\fR parent URL support, which allows passing a second \fBparentURL\fR argument for contextual resolution. @@ -2271,6 +2274,8 @@ one is included in the list below. .It \fB--experimental-eventsource\fR .It +\fB--experimental-http-serve\fR +.It \fB--experimental-import-meta-resolve\fR .It \fB--experimental-import-text\fR diff --git a/doc/type-map.json b/doc/type-map.json index c603e059a7ee..3499c23be5af 100644 --- a/doc/type-map.json +++ b/doc/type-map.json @@ -22,12 +22,14 @@ "brotli options": "zlib.html#class-brotlioptions", "Buffer": "buffer.html#class-buffer", "ByteLengthQueuingStrategy": "webstreams.html#class-bytelengthqueuingstrategy", + "CapsuleStream": "http.html#class-capsulestream", "Channel": "diagnostics_channel.html#class-channel", "ChildProcess": "child_process.html#class-childprocess", "Cipher": "crypto.html#class-cipher", "Cipheriv": "crypto.html#class-cipheriv", "ClientHttp2Session": "http2.html#class-clienthttp2session", "ClientHttp2Stream": "http2.html#class-clienthttp2stream", + "ConnectContext": "http.html#class-connectcontext", "ContextParams": "webcrypto.html#class-contextparams", "CountQueuingStrategy": "webstreams.html#class-countqueuingstrategy", "Crypto": "webcrypto.html#class-crypto", @@ -58,6 +60,9 @@ "EventEmitter": "events.html#class-eventemitter", "EventListener": "events.html#event-listener", "EventTarget": "events.html#class-eventtarget", + "fetchServer.Listener": "http.html#class-fetchserverlistener", + "fetchServer.Server": "http.html#class-fetchserverserver", + "fetchServer.ServerResponse": "http.html#class-fetchserverserverresponse", "File": "buffer.html#class-file", "FileHandle": "fs.html#class-filehandle", "Handle": "net.html#serverlistenhandle-backlog-callback", @@ -118,8 +123,12 @@ "RsaPssParams": "webcrypto.html#class-rsapssparams", "RunScope": "async_context.html#class-runscope", "RunStoresScope": "diagnostics_channel.html#class-runstoresscope", + "ServerContext": "http.html#class-servercontext", + "ServerHeaders": "http.html#class-serverheaders", "ServerHttp2Session": "http2.html#class-serverhttp2session", "ServerHttp2Stream": "http2.html#class-serverhttp2stream", + "ServerRequest": "http.html#class-serverrequest", + "ServerWebSocketStream": "http.html#class-serverwebsocketstream", "Session": "sqlite.html#class-session", "Share": "stream_iter.html#class-share", "Shareable": "stream_iter.html#interface-shareable", @@ -144,6 +153,7 @@ "Transform": "stream.html#class-streamtransform", "TransformStream": "webstreams.html#class-transformstream", "TransformStreamDefaultController": "webstreams.html#class-transformstreamdefaultcontroller", + "Tunnel": "http.html#class-tunnel", "TurboShakeParams": "webcrypto.html#class-turboshakeparams", "URL": "url.html#the-whatwg-url-api", "URLSearchParams": "url.html#class-urlsearchparams", diff --git a/lib/http.js b/lib/http.js index 142d8b98fcfa..d5c943d2625c 100644 --- a/lib/http.js +++ b/lib/http.js @@ -61,6 +61,8 @@ const { redactProxyUrl, } = require('internal/http'); const { URL } = require('internal/url'); +const { getOptionValue } = require('internal/options'); +const { emitExperimentalWarning } = require('internal/util'); let maxHeaderSize; let undici; @@ -292,7 +294,6 @@ ObjectDefineProperty(module.exports, 'maxHeaderSize', { enumerable: true, get() { if (maxHeaderSize === undefined) { - const { getOptionValue } = require('internal/options'); maxHeaderSize = getOptionValue('--max-http-header-size'); } @@ -312,6 +313,22 @@ ObjectDefineProperty(module.exports, 'globalAgent', { }, }); +if (getOptionValue('--experimental-http-serve')) { + let fetchServer; + ObjectDefineProperty(module.exports, 'fetchServer', { + __proto__: null, + configurable: true, + enumerable: true, + get() { + if (fetchServer === undefined) { + emitExperimentalWarning('http.fetchServer'); + fetchServer = require('internal/http/fetch_server/index'); + } + return fetchServer; + }, + }); +} + ObjectDefineProperty(module.exports, 'WebSocket', { __proto__: null, configurable: true, diff --git a/lib/internal/http/fetch_server/body.js b/lib/internal/http/fetch_server/body.js new file mode 100644 index 000000000000..5b00ceba4143 --- /dev/null +++ b/lib/internal/http/fetch_server/body.js @@ -0,0 +1,452 @@ +'use strict'; + +// Body sources and async iteration shared by ServerRequest and +// ServerResponse. +// https://fetch-server.proposal.wintertc.org/#serverresponse-body + +const { + ArrayBufferIsView, + ArrayBufferPrototypeSlice, + ArrayPrototypePush, + ArrayPrototypeShift, + DataViewPrototypeGetBuffer, + DataViewPrototypeGetByteLength, + DataViewPrototypeGetByteOffset, + FunctionPrototypeCall, + ObjectDefineProperty, + ObjectGetPrototypeOf, + ObjectSetPrototypeOf, + PromisePrototypeThen, + PromiseReject, + PromiseResolve, + Symbol, + SymbolAsyncIterator, + SymbolIterator, + SymbolToStringTag, + TypedArrayPrototypeGetBuffer, + TypedArrayPrototypeGetByteLength, + TypedArrayPrototypeGetByteOffset, + TypedArrayPrototypeSlice, + Uint8Array, +} = primordials; + +const { + codes: { + ERR_INVALID_ARG_TYPE, + ERR_INVALID_STATE, + }, +} = require('internal/errors'); + +const { + isAnyArrayBuffer, + isDataView, + isUint8Array, +} = require('internal/util/types'); + +const { + kEnumerableProperty, +} = require('internal/util'); + +const kAsyncIteratorPrototype = ObjectGetPrototypeOf( + ObjectGetPrototypeOf(async function* () {}).prototype); + +function noop() {} + +function markHandled(promise) { + PromisePrototypeThen(promise, undefined, noop); + return promise; +} + +// ============================================================================= +// Body sources +// ============================================================================= +// A body source implements: +// read(): Promise (null signals end of the body) +// close(reason): void + +class BytesSource { + #bytes; + constructor(bytes) { + this.#bytes = bytes; + } + read() { + const bytes = this.#bytes; + this.#bytes = null; + if (bytes === null || TypedArrayPrototypeGetByteLength(bytes) === 0) { + return PromiseResolve(null); + } + return PromiseResolve([bytes]); + } + close() { + this.#bytes = null; + } + + /** + * Takes the bytes synchronously, for sending them in a single write. + * @returns {Uint8Array} The bytes, which can be empty. + */ + take() { + const bytes = this.#bytes ?? new Uint8Array(0); + this.#bytes = null; + return bytes; + } +} + +class ReadableStreamSource { + #stream; + #reader = null; + #ended = false; + constructor(stream) { + this.#stream = stream; + } + async read() { + if (this.#ended) return null; + this.#reader ??= this.#stream.getReader(); + const { done, value } = await this.#reader.read(); + if (done) { + this.#ended = true; + return null; + } + if (!isUint8Array(value)) { + this.#ended = true; + const err = new ERR_INVALID_ARG_TYPE('chunk', 'Uint8Array', value); + markHandled(this.#reader.cancel(err)); + throw err; + } + return [value]; + } + close(reason) { + if (this.#ended) return; + this.#ended = true; + if (this.#reader !== null) { + markHandled(this.#reader.cancel(reason)); + } else { + markHandled(this.#stream.cancel(reason)); + } + } +} + +function getAsyncIterator(object) { + const method = object[SymbolAsyncIterator]; + if (method !== undefined && method !== null) { + return FunctionPrototypeCall(method, object); + } + const syncMethod = object[SymbolIterator]; + const syncIterator = FunctionPrototypeCall(syncMethod, object); + return { + __proto__: null, + async next() { + const { done, value } = syncIterator.next(); + return { __proto__: null, value: await value, done }; + }, + async return(value) { + if (typeof syncIterator.return === 'function') { + const result = syncIterator.return(value); + return { __proto__: null, value: await result?.value, done: true }; + } + return { __proto__: null, value, done: true }; + }, + }; +} + +/** + * Converts a value produced by an async sequence to a batch of Uint8Array. + * @param {any} value + * @returns {Uint8Array[]} + */ +function toBatch(value) { + if (value === null || typeof value !== 'object' || + typeof value[SymbolIterator] !== 'function') { + throw new ERR_INVALID_ARG_TYPE('batch', 'Iterable', value); + } + const batch = []; + for (const chunk of value) { + if (!isUint8Array(chunk)) { + throw new ERR_INVALID_ARG_TYPE('chunk', 'Uint8Array', chunk); + } + ArrayPrototypePush(batch, chunk); + } + return batch; +} + +class AsyncSequenceSource { + #object; + #iterator = null; + #ended = false; + constructor(object) { + this.#object = object; + } + async read() { + if (this.#ended) return null; + this.#iterator ??= getAsyncIterator(this.#object); + for (;;) { + let result; + try { + result = await this.#iterator.next(); + } catch (err) { + this.#ended = true; + throw err; + } + if (result === null || typeof result !== 'object') { + this.#ended = true; + throw new ERR_INVALID_ARG_TYPE('result', 'Object', result); + } + if (result.done) { + this.#ended = true; + return null; + } + let batch; + try { + batch = toBatch(result.value); + } catch (err) { + this.close(err); + throw err; + } + if (batch.length > 0) return batch; + } + } + close(reason) { + if (this.#ended) return; + this.#ended = true; + const iterator = this.#iterator; + if (iterator !== null && typeof iterator.return === 'function') { + try { + const result = FunctionPrototypeCall(iterator.return, iterator, reason); + if (result != null && typeof result.then === 'function') { + markHandled(PromiseResolve(result)); + } + } catch { + // Errors closing the iterator are ignored. + } + } + } +} + +class LazySource { + // Wraps a source that has to be created asynchronously (e.g. from a Blob). + #factory; + #source = null; + #closed = false; + constructor(factory) { + this.#factory = factory; + } + async read() { + if (this.#closed) return null; + this.#source ??= await this.#factory(); + return this.#source.read(); + } + close(reason) { + if (this.#closed) return; + this.#closed = true; + this.#source?.close(reason); + } +} + +class SplitSide { + #splitter; + #queue = []; + #closed = false; + constructor(splitter) { + this.#splitter = splitter; + } + get closed() { + return this.#closed; + } + enqueue(entry) { + ArrayPrototypePush(this.#queue, entry); + } + async read() { + if (this.#closed) return null; + while (this.#queue.length === 0) { + await this.#splitter.pull(); + } + const entry = this.#queue[0]; + if (entry.end) return null; + if (entry.error !== undefined) throw entry.error; + ArrayPrototypeShift(this.#queue); + return entry.batch; + } + close(reason) { + if (this.#closed) return; + this.#closed = true; + this.#queue = []; + this.#splitter.sideClosed(reason); + } +} + +// Splitting a body for clone() (Section 9.5). +class Splitter { + #source; + #sides = [null, null]; + #reading = null; + #closedCount = 0; + constructor(source) { + this.#source = source; + this.#sides[0] = new SplitSide(this, 0); + this.#sides[1] = new SplitSide(this, 1); + } + get sides() { + return this.#sides; + } + pull() { + if (this.#reading !== null) return this.#reading; + this.#reading = PromisePrototypeThen( + this.#source.read(), + (batch) => { + this.#reading = null; + for (let i = 0; i < 2; i++) { + const side = this.#sides[i]; + if (side.closed) continue; + if (batch === null) { + side.enqueue({ __proto__: null, end: true }); + } else if (i === 0) { + side.enqueue({ __proto__: null, batch }); + } else { + const copy = []; + for (let j = 0; j < batch.length; j++) { + ArrayPrototypePush(copy, TypedArrayPrototypeSlice(batch[j])); + } + side.enqueue({ __proto__: null, batch: copy }); + } + } + }, + (error) => { + this.#reading = null; + for (let i = 0; i < 2; i++) { + const side = this.#sides[i]; + if (!side.closed) side.enqueue({ __proto__: null, error }); + } + }); + return this.#reading; + } + sideClosed(reason) { + if (++this.#closedCount === 2) { + this.#source.close(reason); + } + } +} + +function splitSource(source) { + return new Splitter(source).sides; +} + +function copyBufferSource(object) { + if (isAnyArrayBuffer(object)) { + return new Uint8Array(ArrayBufferPrototypeSlice(object, 0)); + } + if (isDataView(object)) { + return TypedArrayPrototypeSlice(new Uint8Array( + DataViewPrototypeGetBuffer(object), + DataViewPrototypeGetByteOffset(object), + DataViewPrototypeGetByteLength(object))); + } + if (ArrayBufferIsView(object)) { + return TypedArrayPrototypeSlice(new Uint8Array( + TypedArrayPrototypeGetBuffer(object), + TypedArrayPrototypeGetByteOffset(object), + TypedArrayPrototypeGetByteLength(object))); + } + throw new ERR_INVALID_ARG_TYPE('body', 'BufferSource', object); +} + +// ============================================================================= +// Web IDL-style asynchronous iterators +// ============================================================================= + +/** + * Creates a prototype for async iterator objects, as for a Web IDL + * `async_iterable` declaration. + * @param {string} name The interface name. + * @returns {object} + */ +function createAsyncIteratorPrototype(name) { + const proto = { + __proto__: kAsyncIteratorPrototype, + next() { + const state = this?.[kIteratorState]; + if (state === undefined) { + return PromiseReject(new ERR_INVALID_STATE.TypeError( + `Value of "this" must be a ${name} AsyncIterator`)); + } + const nextSteps = () => { + if (state.finished) return { value: undefined, done: true }; + return PromisePrototypeThen(state.next(), (batch) => { + if (batch === null) { + state.finished = true; + return { value: undefined, done: true }; + } + return { value: batch, done: false }; + }, (err) => { + state.finished = true; + throw err; + }); + }; + state.ongoing = state.ongoing !== null ? + PromisePrototypeThen(state.ongoing, nextSteps, nextSteps) : + PromiseResolve(nextSteps()); + return state.ongoing; + }, + return(value) { + const state = this?.[kIteratorState]; + if (state === undefined) { + return PromiseReject(new ERR_INVALID_STATE.TypeError( + `Value of "this" must be a ${name} AsyncIterator`)); + } + const returnSteps = () => { + if (state.finished) return { value, done: true }; + state.finished = true; + return PromisePrototypeThen( + PromiseResolve(state.return(value)), + () => ({ value, done: true })); + }; + state.ongoing = state.ongoing !== null ? + PromisePrototypeThen(state.ongoing, returnSteps, returnSteps) : + PromiseResolve(returnSteps()); + return state.ongoing; + }, + }; + ObjectDefineProperty(proto, 'next', kEnumerableProperty); + ObjectDefineProperty(proto, 'return', kEnumerableProperty); + ObjectDefineProperty(proto, SymbolToStringTag, { + __proto__: null, + configurable: true, + value: `${name} AsyncIterator`, + }); + return proto; +} + +const kIteratorState = Symbol('kIteratorState'); + +/** + * @param {object} proto An async iterator prototype. + * @param {() => Promise} next + * @param {(value: any) => any} returnSteps + * @returns {object} + */ +function createAsyncIterator(proto, next, returnSteps) { + const iterator = { + [kIteratorState]: { + __proto__: null, + next, + return: returnSteps, + ongoing: null, + finished: false, + }, + }; + ObjectSetPrototypeOf(iterator, proto); + return iterator; +} + +module.exports = { + AsyncSequenceSource, + BytesSource, + LazySource, + ReadableStreamSource, + copyBufferSource, + createAsyncIterator, + createAsyncIteratorPrototype, + markHandled, + noop, + splitSource, + toBatch, +}; diff --git a/lib/internal/http/fetch_server/context.js b/lib/internal/http/fetch_server/context.js new file mode 100644 index 000000000000..804a6b1cb5a2 --- /dev/null +++ b/lib/internal/http/fetch_server/context.js @@ -0,0 +1,522 @@ +'use strict'; + +// The ServerContext and ConnectContext protocols of the Fetch Server API. +// https://fetch-server.proposal.wintertc.org/#servercontext + +const { + ObjectDefineProperties, + ObjectGetOwnPropertyDescriptors, + ObjectGetPrototypeOf, + PromiseReject, + PromiseResolve, + ReflectOwnKeys, + RegExpPrototypeExec, + SafeSet, + StringPrototypeIndexOf, + StringPrototypeSlice, + StringPrototypeSplit, + StringPrototypeToLowerCase, + StringPrototypeTrim, + SymbolFor, + SymbolToStringTag, +} = primordials; + +const { + codes: { + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_OUT_OF_RANGE, + }, +} = require('internal/errors'); + +const { + converters, +} = require('internal/webidl'); + +const { + getErrorCode, + invalidState, + kDenyConnectSendable, + kDenySendable, + kRequestRejected, + notSupported, +} = require('internal/http/fetch_server/errors'); + +const { + convertHeadersInit, + listGet, + toByteString, +} = require('internal/http/fetch_server/headers'); + +const kFallThrough = SymbolFor('wintertc.server.fallthrough'); + +// The state of a context is held in a private field, which is stamped onto +// the plain context object by returning it from the base constructor. A +// private field is cheaper to add than a non-enumerable property, and is not +// observable. getState() follows the prototype chain, so that a context that +// is wrapped with Object.create(ctx) can be used as the original. +class ContextBase { + constructor(object) { + // eslint-disable-next-line no-constructor-return + return object; + } +} + +class ContextStamp extends ContextBase { + #state; + constructor(object, state) { + super(object); + this.#state = state; + } + + static get(self) { + for (let current = self; + current !== null && (typeof current === 'object' || + typeof current === 'function'); + current = ObjectGetPrototypeOf(current)) { + if (#state in current) return current.#state; + } + return undefined; + } +} + +// Protocols known to use the Capsule Protocol (Section 11.3). +const kCapsuleProtocols = new SafeSet(['connect-udp', 'connect-ip']); + +function getState(self) { + const state = ContextStamp.get(self); + if (state === undefined) { + throw new ERR_INVALID_ARG_TYPE('this', 'ServerContext', self); + } + return state; +} + +function getOwnAccessors(obj) { + const descriptors = ObjectGetOwnPropertyDescriptors(obj); + const keys = ReflectOwnKeys(descriptors); + for (let i = 0; i < keys.length; i++) { + descriptors[keys[i]].enumerable = true; + } + return descriptors; +} + +function convertAddress(address) { + // Returns the SocketAddress converted to a JavaScript value. + const result = { address: address?.address ?? '', port: address?.port ?? 0 }; + if (address?.family !== undefined) result.family = address.family; + if (address?.path !== undefined) result.path = address.path; + return result; +} + +// Parses an RFC 9218 Priority field value (a Structured Field Dictionary). +const kKeyRegExp = /^[a-z*][a-z0-9_\-.*]*$/; +function parsePriority(value) { + const priority = { urgency: 3, incremental: false }; + if (value === null) return priority; + const members = StringPrototypeSplit(value, ','); + let urgency = 3; + let incremental = false; + for (let i = 0; i < members.length; i++) { + const member = StringPrototypeTrim(members[i]); + if (member === '') return priority; + const eq = StringPrototypeIndexOf(member, '='); + const key = eq === -1 ? member : StringPrototypeSlice(member, 0, eq); + // Parameters on members are ignored. + let val = eq === -1 ? '?1' : StringPrototypeSlice(member, eq + 1); + const semi = StringPrototypeIndexOf(val, ';'); + if (semi !== -1) val = StringPrototypeSlice(val, 0, semi); + if (RegExpPrototypeExec(kKeyRegExp, key) === null) return priority; + if (key === 'u') { + if (RegExpPrototypeExec(/^-?[0-9]{1,15}$/, val) !== null) { + const n = +val; + if (n >= 0 && n <= 7) urgency = n; + } + } else if (key === 'i') { + if (val === '?1') incremental = true; + else if (val === '?0') incremental = false; + } + } + priority.urgency = urgency; + priority.incremental = incremental; + return priority; +} + +function convertPriority(value) { + if (value === null || value === undefined) { + return { urgency: 3, incremental: false }; + } + if (typeof value !== 'object' && typeof value !== 'function') { + throw new ERR_INVALID_ARG_TYPE('serverPriority', 'Object', value); + } + const incremental = value.incremental; + const urgency = value.urgency; + return { + urgency: urgency === undefined ? 3 : + converters['unsigned short'](urgency, { + __proto__: null, + enforceRange: true, + context: 'urgency', + }), + incremental: incremental === undefined ? false : !!incremental, + }; +} + +// ============================================================================= +// ServerContext (Section 5.8, Section 7) +// ============================================================================= + +function sendInformationalSteps(state, status, headers) { + const exchange = state.exchange; + status = converters['unsigned short'](status, { + __proto__: null, + enforceRange: true, + prefix: 'Failed to execute \'sendInformational\'', + context: 'status', + }); + const fields = headers === undefined ? [] : convertHeadersInit(headers); + if (status === 101 || status < 100 || status > 199) { + throw new ERR_OUT_OF_RANGE('status', '>= 100 and <= 199, not 101', status); + } + if (state.inert || exchange.state === 'committed' || + exchange.state === 'complete' || exchange.state === 'denied') { + throw invalidState( + `Cannot send an informational response in the ${exchange.state} state`); + } + if (exchange.state === 'aborted') return; + if (exchange.backend.httpVersion === '1.0') return; + if (status === 100) { + if (!exchange.pending100) { + throw invalidState('No 100 (Continue) response is pending'); + } + exchange.pending100 = false; + exchange.backend.sendContinue(fields); + exchange.state = 'informational'; + return; + } + if (exchange.autoContinue && exchange.pending100) { + exchange.pending100 = false; + exchange.backend.sendContinue([]); + } + exchange.backend.sendInformational(status, fields); + exchange.state = 'informational'; +} + +function denySteps(state, error) { + const exchange = state.exchange; + if (exchange.state === 'aborted') return; + if (state.inert || exchange.state !== 'open') { + throw invalidState(`Cannot deny in the ${exchange.state} state`); + } + if (error === kFallThrough) { + if (!state.isConnect || exchange.kind !== 'upgrade') { + throw invalidState( + 'Only an upgrade-originated exchange can fall through'); + } + exchange.fallThrough = true; + state.inert = true; + return; + } + const sendable = state.isConnect && exchange.kind === 'connect' ? + kDenyConnectSendable : kDenySendable; + const code = getErrorCode(error, sendable) ?? kRequestRejected; + exchange.deny(); + exchange.backend.deny(code); +} + +function waitUntilSteps(state, promise) { + state.exchange.waitUntil(PromiseResolve(promise)); +} + +const contextAccessors = { + __proto__: null, + get request() { + return getState(this).exchange.request; + }, + get remoteAddress() { + return convertAddress(getState(this).exchange.backend.remoteAddress()); + }, + get localAddress() { + return convertAddress(getState(this).exchange.backend.localAddress()); + }, + get alpnProtocol() { + return getState(this).exchange.backend.alpnProtocol; + }, + get encrypted() { + return getState(this).exchange.backend.encrypted; + }, + get serverName() { + return getState(this).exchange.backend.serverName; + }, + get clientPriority() { + const state = getState(this); + state.clientPriority ??= parsePriority( + listGet(state.exchange.backend.headerList, 'priority')); + return { urgency: state.clientPriority.urgency, + incremental: state.clientPriority.incremental }; + }, + get serverPriority() { + const state = getState(this); + const value = state.exchange.serverPriority; + return value == null ? null : + { urgency: value.urgency, incremental: value.incremental }; + }, + set serverPriority(value) { + const state = getState(this); + if (value === null || value === undefined) { + if (!state.inert) state.exchange.serverPriority = null; + return; + } + const priority = convertPriority(value); + if (priority.urgency > 7) { + throw new ERR_OUT_OF_RANGE('urgency', '>= 0 and <= 7', priority.urgency); + } + if (!state.inert) state.exchange.serverPriority = priority; + }, + get onpriority() { + return getState(this).exchange.onpriority ?? null; + }, + set onpriority(value) { + const state = getState(this); + const callback = typeof value === 'function' ? value : null; + if (!state.inert) state.exchange.onpriority = callback; + }, + get autoContinue() { + return getState(this).exchange.autoContinue; + }, + get completed() { + return getState(this).exchange.completed; + }, + get sendInformational() { + const state = getState(this); + state.sendInformational ??= function sendInformational(status, headers) { + return sendInformationalSteps(state, status, headers); + }; + return state.sendInformational; + }, + get deny() { + const state = getState(this); + state.deny ??= function deny(error) { + return denySteps(state, error); + }; + return state.deny; + }, + get waitUntil() { + const state = getState(this); + state.waitUntil ??= function waitUntil(promise) { + if (arguments.length === 0) { + throw new ERR_INVALID_ARG_TYPE('promise', 'Promise', promise); + } + return waitUntilSteps(state, promise); + }; + return state.waitUntil; + }, +}; + +const ServerContextPrototype = { __proto__: null }; +ObjectDefineProperties(ServerContextPrototype, + getOwnAccessors(contextAccessors)); +ObjectDefineProperties(ServerContextPrototype, { + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'ServerContext', + }, +}); + + +// ============================================================================= +// ConnectContext (Section 11) +// ============================================================================= + +function checkEstablish(state) { + const exchange = state.exchange; + if (state.inert || exchange.state === 'committed' || + exchange.state === 'complete' || exchange.state === 'denied') { + throw invalidState( + `Cannot establish a tunnel in the ${exchange.state} state`); + } +} + +function getOfferedProtocols(exchange) { + const upgrade = listGet(exchange.backend.headerList, 'upgrade'); + if (upgrade === null) return []; + const offered = StringPrototypeSplit(upgrade, ','); + const result = []; + for (let i = 0; i < offered.length; i++) { + const token = StringPrototypeTrim(offered[i]); + if (token !== '') result[result.length] = token; + } + return result; +} + +function getConnectProtocol(exchange) { + if (exchange.kind === 'upgrade') { + const offered = getOfferedProtocols(exchange); + if (offered.length === 0) return null; + const first = offered[0]; + const slash = StringPrototypeIndexOf(first, '/'); + return StringPrototypeToLowerCase(slash === -1 ? first : StringPrototypeSlice(first, 0, slash)); + } + return exchange.backend.connectProtocol ?? null; +} + +async function acceptSteps(state, init) { + const exchange = state.exchange; + checkEstablish(state); + let fields = []; + let protocol; + if (init !== undefined && init !== null) { + if (typeof init !== 'object' && typeof init !== 'function') { + throw new ERR_INVALID_ARG_TYPE('init', 'Object', init); + } + if (init.headers !== undefined) fields = convertHeadersInit(init.headers); + if (init.protocol !== undefined) { + protocol = toByteString(init.protocol, 'init.protocol'); + } + } + const offered = getOfferedProtocols(exchange); + if (protocol !== undefined) { + if (exchange.kind !== 'upgrade') { + throw new ERR_INVALID_ARG_VALUE( + 'init.protocol', protocol, + 'can only be given on an upgrade-originated exchange'); + } + const lower = StringPrototypeToLowerCase(protocol); + let matched = false; + for (let i = 0; i < offered.length; i++) { + const candidate = StringPrototypeToLowerCase(offered[i]); + const slash = StringPrototypeIndexOf(candidate, '/'); + if (candidate === lower || + (slash !== -1 && StringPrototypeSlice(candidate, 0, slash) === lower)) { + matched = true; + break; + } + } + if (!matched) { + throw new ERR_INVALID_ARG_VALUE('init.protocol', protocol, + 'was not offered by the client'); + } + } + if (exchange.state === 'aborted') throw exchange.abortReason; + const connectProtocol = getConnectProtocol(exchange); + if (kCapsuleProtocols.has(connectProtocol)) { + let hasCapsuleProtocol = false; + for (let i = 0; i < fields.length; i++) { + if (StringPrototypeToLowerCase(fields[i][0]) === 'capsule-protocol') { + hasCapsuleProtocol = true; + } + } + if (!hasCapsuleProtocol) fields[fields.length] = ['Capsule-Protocol', '?1']; + } + exchange.commit(); + const { createTunnel } = require('internal/http/fetch_server/tunnel'); + const selected = protocol ?? offered[0]; + const transport = await exchange.backend.acceptTunnel(exchange, fields, + selected); + if (exchange.state === 'aborted') throw exchange.abortReason; + return createTunnel(exchange, transport, connectProtocol); +} + +function upgradeWebSocketSteps(state, options, stream) { + const exchange = state.exchange; + const ws = require('internal/http/fetch_server/websocket'); + const converted = ws.convertUpgradeOptions(options); + if (getConnectProtocol(exchange) !== 'websocket') { + throw invalidState('The connect protocol is not "websocket"'); + } + checkEstablish(state); + return stream ? + ws.upgradeWebSocketStream(exchange, converted) : + ws.upgradeWebSocket(exchange, converted); +} + +const connectAccessors = { + __proto__: null, + get connectProtocol() { + return getConnectProtocol(getState(this).exchange); + }, + get accept() { + const state = getState(this); + state.accept ??= function accept(init = undefined) { + try { + return acceptSteps(state, init); + } catch (err) { + return PromiseReject(err); + } + }; + return state.accept; + }, + get upgradeWebSocket() { + const state = getState(this); + state.upgradeWebSocket ??= function upgradeWebSocket(options = undefined) { + return upgradeWebSocketSteps(state, options, false); + }; + return state.upgradeWebSocket; + }, + get upgradeWebSocketStream() { + const state = getState(this); + state.upgradeWebSocketStream ??= + function upgradeWebSocketStream(options = undefined) { + return upgradeWebSocketSteps(state, options, true); + }; + return state.upgradeWebSocketStream; + }, + get upgradeWebTransport() { + const state = getState(this); + state.upgradeWebTransport ??= function upgradeWebTransport() { + const exchange = state.exchange; + if (getConnectProtocol(exchange) !== 'webtransport') { + return PromiseReject(invalidState( + 'The connect protocol is not "webtransport"')); + } + try { + checkEstablish(state); + } catch (err) { + return PromiseReject(err); + } + return PromiseReject(notSupported('WebTransport is not supported')); + }; + return state.upgradeWebTransport; + }, +}; + +const ConnectContextPrototype = { __proto__: ServerContextPrototype }; +ObjectDefineProperties(ConnectContextPrototype, + getOwnAccessors(connectAccessors)); +ObjectDefineProperties(ConnectContextPrototype, { + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'ConnectContext', + }, +}); + +/** + * Creates a ServerContext or ConnectContext for an exchange. + * @param {import('./exchange').Exchange} exchange + * @param {boolean} isConnect + * @returns {object} + */ +function createContext(exchange, isConnect) { + const ctx = { + __proto__: isConnect ? ConnectContextPrototype : ServerContextPrototype, + }; + new ContextStamp(ctx, { + __proto__: null, + exchange, + isConnect, + inert: false, + }); + return ctx; +} + +function isContextInert(ctx) { + return ContextStamp.get(ctx).inert; +} + +module.exports = { + createContext, + getConnectProtocol, + isContextInert, + kFallThrough, +}; diff --git a/lib/internal/http/fetch_server/dispatch.js b/lib/internal/http/fetch_server/dispatch.js new file mode 100644 index 000000000000..521b703e5d19 --- /dev/null +++ b/lib/internal/http/fetch_server/dispatch.js @@ -0,0 +1,335 @@ +'use strict'; + +// Handler invocation (Section 16) and response sending (Section 9.6) for the Fetch +// Server API. +// https://fetch-server.proposal.wintertc.org/#handler-model + +const { + FunctionPrototypeCall, + TypedArrayPrototypeGetByteLength, +} = primordials; + +const { + codes: { + ERR_INVALID_RETURN_VALUE, + }, +} = require('internal/errors'); + +const { + createProtocolError, + getErrorCode, + kCommittedSendable, + kConnectError, + kContentTooLarge, + kInternalError, + kRequestCancelled, +} = require('internal/http/fetch_server/errors'); + +const { + filterTrailers, + getHeaderList, + setHeadersImmutable, +} = require('internal/http/fetch_server/headers'); + +const { + ServerResponse, + acquireBodyReader, + getDeclaredTrailers, + getResponseState, + isResponseUnusable, + isServerResponse, +} = require('internal/http/fetch_server/response'); + +const { + createContext, +} = require('internal/http/fetch_server/context'); + +const { + report, +} = require('internal/http/fetch_server/exchange'); + +let undiciResponse; +function isFetchResponse(value) { + undiciResponse ??= require('internal/deps/undici/undici').Response; + return value instanceof undiciResponse; +} + +function invalidReturn(value) { + return new ERR_INVALID_RETURN_VALUE('a ServerResponse', 'handler', value); +} + +/** + * Sends a ServerResponse for an exchange (Section 9.6). + * @param {import('./exchange').Exchange} exchange + * @param {ServerResponse} response + * @returns {Promise} + */ +async function sendResponse(exchange, response) { + const backend = exchange.backend; + const state = getResponseState(response); + exchange.commit(); + // A pending 100 obligation is omitted (Section 7.4.1). + exchange.pending100 = false; + setHeadersImmutable(state.headers); + + if (state.type === 'error') { + backend.reset(kInternalError); + exchange.complete(); + return; + } + + const headerList = getHeaderList(state.headers); + const isHead = backend.method === 'HEAD'; + const reader = acquireBodyReader(response); + const wantTrailers = !isHead && + getDeclaredTrailers(headerList).length > 0 && + backend.canSendTrailers(headerList); + // A body held as bytes, such as one from a string or a Uint8Array, is sent + // with its length in a single write. + const bytes = isHead || wantTrailers ? undefined : reader.takeBytes(); + const hasBody = state.body !== null && !isHead && + (bytes === undefined || TypedArrayPrototypeGetByteLength(bytes) > 0); + const contentLength = bytes === undefined ? undefined : + TypedArrayPrototypeGetByteLength(bytes); + + try { + backend.respond(state.status, state.statusText, headerList, + { hasBody, wantTrailers, contentLength }); + } catch (err) { + reader.close(err); + report(err); + backend.reset(kInternalError); + exchange.complete(); + return; + } + + if (isHead) reader.close(); + + try { + if (bytes !== undefined) { + await exchange.raceAbort(backend.end(null, hasBody ? bytes : null)); + exchange.complete(); + return; + } + if (hasBody) { + for (;;) { + const batch = await exchange.raceAbort(reader.read()); + if (batch === null) break; + await exchange.raceAbort(backend.write(batch)); + } + } + if (wantTrailers) { + const trailerState = state.trailerState; + await exchange.raceAbort(trailerState.wait()); + if (trailerState.state === 'errored') { + backend.reset(getErrorCode(trailerState.error, kCommittedSendable) ?? + kInternalError); + exchange.complete(); + return; + } + const trailers = filterTrailers(trailerState.list); + await exchange.raceAbort(backend.end(trailers)); + } else { + await exchange.raceAbort(backend.end(null)); + } + } catch (err) { + if (exchange.state === 'aborted') { + reader.close(exchange.abortReason); + return; + } + reader.close(err); + backend.reset(getErrorCode(err, kCommittedSendable) ?? kInternalError); + exchange.complete(); + return; + } + exchange.complete(); +} + +function fallbackStatus(exchange, error) { + if (error?.code === kContentTooLarge) return 413; + return exchange.kind === 'connect' ? 502 : 500; +} + +function sendFallback(exchange, status) { + return sendResponse(exchange, new ServerResponse(null, { status })); +} + +/** + * Checks a value returned by a handler; returns a ServerResponse to send, or + * throws a TypeError for an invalid return. + * @param {import('./exchange').Exchange} exchange + * @param {any} value + * @returns {ServerResponse} + */ +function toResponse(exchange, value) { + if (isFetchResponse(value)) { + value = ServerResponse.from(value); + } + if (!isServerResponse(value) || isResponseUnusable(value)) { + throw invalidReturn(value); + } + if (exchange.kind === 'connect') { + const { status } = getResponseState(value); + if (status >= 200 && status <= 299) { + throw new ERR_INVALID_RETURN_VALUE( + 'a non-2xx ServerResponse', 'connect', value); + } + } + return value; +} + +async function invoke(fn, thisArg, ...args) { + return FunctionPrototypeCall(fn, thisArg, ...args); +} + +/** + * Invokes the error() handler, or sends the fallback response (Section 16.3). + * @param {object} handlers + * @param {import('./exchange').Exchange} exchange + * @param {object} ctx + * @param {any} error + * @returns {Promise} + */ +async function handleFault(handlers, exchange, ctx, error) { + const fallback = fallbackStatus(exchange, error); + if (handlers.error === undefined) { + report(error); + return sendFallback(exchange, fallback); + } + let result; + try { + result = await invoke(handlers.error, handlers.object, ctx, error); + } catch (err) { + report(err); + if (exchange.responseCapable) return sendFallback(exchange, fallback); + return; + } + if (exchange.state === 'aborted') return; + if (exchange.state === 'denied') { + if (result !== undefined) report(invalidReturn(result)); + return; + } + if (!exchange.responseCapable) return; + if (result === undefined) return sendFallback(exchange, fallback); + let response; + try { + response = toResponse(exchange, result); + } catch (err) { + report(err); + return sendFallback(exchange, fallback); + } + return sendResponse(exchange, response); +} + +/** + * Processes the settlement of a fetch() or connect() invocation (Section 16.4). + * @param {object} handlers + * @param {import('./exchange').Exchange} exchange + * @param {object} ctx + * @param {boolean} threw + * @param {any} error + * @param {any} result + * @returns {Promise} true if fetch() should now be invoked for a + * fall-through. + */ +async function settle(handlers, exchange, ctx, threw, error, result) { + if (exchange.state === 'aborted') { + if (threw) report(error); + return false; + } + if (exchange.state === 'denied') { + if (threw) report(error); + else if (result !== undefined) report(invalidReturn(result)); + return false; + } + if (exchange.state === 'committed' || exchange.state === 'complete') { + // A tunnel has been established. + if (threw) { + report(error); + const tunnel = exchange.tunnel; + if (tunnel !== null) { + tunnel.destroy(createProtocolError( + exchange.isWebSocket ? kRequestCancelled : kConnectError, + 'The connect() handler failed after accepting the tunnel')); + } + } else if (result !== undefined) { + report(invalidReturn(result)); + } + return false; + } + if (threw) { + await handleFault(handlers, exchange, ctx, error); + return false; + } + if (result === undefined) { + if (exchange.fallThrough) return true; + await handleFault(handlers, exchange, ctx, + invalidReturn(result)); + return false; + } + let response; + try { + response = toResponse(exchange, result); + } catch (err) { + await handleFault(handlers, exchange, ctx, err); + return false; + } + await sendResponse(exchange, response); + return false; +} + +async function runHandler(handlers, fn, exchange, ctx) { + let threw = false; + let error; + let result; + try { + result = await invoke(fn, handlers.object, ctx); + } catch (err) { + threw = true; + error = err; + } + return settle(handlers, exchange, ctx, threw, error, result); +} + +/** + * Dispatches an exchange to the application's handlers. + * @param {object} handlers The converted HandlerObject. + * @param {import('./exchange').Exchange} exchange + */ +async function dispatch(handlers, exchange) { + try { + if (exchange.kind === 'fetch') { + if (handlers.fetch === undefined) { + await sendFallback(exchange, 501); + return; + } + await runHandler(handlers, handlers.fetch, exchange, + createContext(exchange, false)); + return; + } + const fallThrough = await runHandler(handlers, handlers.connect, exchange, + createContext(exchange, true)); + if (!fallThrough || exchange.state === 'aborted') return; + if (handlers.fetch === undefined) { + await sendFallback(exchange, 501); + return; + } + exchange.fallThrough = false; + await runHandler(handlers, handlers.fetch, exchange, + createContext(exchange, false)); + } catch (err) { + // Errors here are implementation errors; make sure the exchange ends. + report(err); + if (!exchange.terminal) { + exchange.backend.reset(kInternalError); + exchange.complete(); + } + } finally { + exchange.handlerDone(); + } +} + +module.exports = { + dispatch, + sendResponse, +}; diff --git a/lib/internal/http/fetch_server/errors.js b/lib/internal/http/fetch_server/errors.js new file mode 100644 index 000000000000..24daf96777a9 --- /dev/null +++ b/lib/internal/http/fetch_server/errors.js @@ -0,0 +1,185 @@ +'use strict'; + +// Protocol errors and the error code registry of the Fetch Server API. +// https://fetch-server.proposal.wintertc.org/#errors + +const { + ObjectDefineProperty, + ReflectGet, + SafeSet, + TypeError, +} = primordials; + +const { + lazyDOMException, +} = require('internal/util'); + +const kRequestRejected = 'ERR_HTTP_REQUEST_REJECTED'; +const kRequestCancelled = 'ERR_HTTP_REQUEST_CANCELLED'; +const kInternalError = 'ERR_HTTP_INTERNAL_ERROR'; +const kStreamReset = 'ERR_HTTP_STREAM_RESET'; +const kGoaway = 'ERR_HTTP_GOAWAY'; +const kProtocolError = 'ERR_HTTP_PROTOCOL_ERROR'; +const kConnectError = 'ERR_HTTP_CONNECT_ERROR'; +const kRequestBodyRejected = 'ERR_HTTP_REQUEST_BODY_REJECTED'; +const kConnectionReset = 'ERR_HTTP_CONNECTION_RESET'; +const kTimeout = 'ERR_HTTP_TIMEOUT'; +const kContentTooLarge = 'ERR_HTTP_CONTENT_TOO_LARGE'; + +// Sets of error codes that may be sent at particular points (Section 15.1). +const kDenySendable = new SafeSet([ + kRequestRejected, + kRequestCancelled, + kInternalError, + kGoaway, +]); +const kDenyConnectSendable = new SafeSet([ + kRequestRejected, + kRequestCancelled, + kInternalError, + kGoaway, + kConnectError, +]); +const kCommittedSendable = new SafeSet([ + kRequestCancelled, + kInternalError, +]); +const kTunnelSendable = new SafeSet([ + kRequestCancelled, + kInternalError, + kConnectError, +]); + +// HTTP/2 error codes (RFC 9113 Section 7). +const kH2Codes = { + __proto__: null, + [kRequestRejected]: 0x7, // REFUSED_STREAM + [kRequestCancelled]: 0x8, // CANCEL + [kInternalError]: 0x2, // INTERNAL_ERROR + [kProtocolError]: 0x1, // PROTOCOL_ERROR + [kConnectError]: 0xa, // CONNECT_ERROR + [kRequestBodyRejected]: 0x0, // NO_ERROR +}; + +const kH2Names = [ + 'NO_ERROR', + 'PROTOCOL_ERROR', + 'INTERNAL_ERROR', + 'FLOW_CONTROL_ERROR', + 'SETTINGS_TIMEOUT', + 'STREAM_CLOSED', + 'FRAME_SIZE_ERROR', + 'REFUSED_STREAM', + 'CANCEL', + 'COMPRESSION_ERROR', + 'CONNECT_ERROR', + 'ENHANCE_YOUR_CALM', + 'INADEQUATE_SECURITY', + 'HTTP_1_1_REQUIRED', +]; + +/** + * Creates a protocol error (Section 15.2): a TypeError with a `code` own property + * and, optionally, a `detail` own property describing the wire-level signal. + * @param {string} code + * @param {string} [message] + * @param {{ protocol: string, errorCode: number, errorName: string }} [detail] + * @returns {TypeError} + */ +function createProtocolError(code, message = code, detail) { + // eslint-disable-next-line no-restricted-syntax + const err = new TypeError(message); + ObjectDefineProperty(err, 'code', { + __proto__: null, + value: code, + writable: true, + enumerable: true, + configurable: true, + }); + if (detail !== undefined) { + ObjectDefineProperty(err, 'detail', { + __proto__: null, + value: detail, + writable: true, + enumerable: true, + configurable: true, + }); + } + return err; +} + +/** + * Maps an inbound HTTP/2 RST_STREAM code to a protocol error (Section 15.4). + * @param {number} rstCode + * @param {string} protocol The ALPN identifier of the connection. + * @returns {TypeError} + */ +function protocolErrorFromH2(rstCode, protocol) { + let code; + switch (rstCode) { + case 0x1: code = kProtocolError; break; + case 0x8: code = kRequestCancelled; break; + case 0xa: code = kConnectError; break; + default: code = kStreamReset; + } + return createProtocolError(code, `Stream reset by peer (${code})`, { + __proto__: null, + protocol, + errorCode: rstCode, + errorName: kH2Names[rstCode] ?? 'UNKNOWN', + }); +} + +/** + * Gets the error code carried by value, given a set of sendable codes + * (Section 15.2), or null. + * @param {any} value + * @param {SafeSet} sendable + * @returns {string|null} + */ +function getErrorCode(value, sendable) { + if (value === null || + (typeof value !== 'object' && typeof value !== 'function')) { + return null; + } + let code; + try { + code = ReflectGet(value, 'code'); + } catch { + return null; + } + if (typeof code === 'string' && sendable.has(code)) return code; + return null; +} + +function invalidState(message) { + return lazyDOMException(message, 'InvalidStateError'); +} + +function notSupported(message) { + return lazyDOMException(message, 'NotSupportedError'); +} + +module.exports = { + createProtocolError, + getErrorCode, + invalidState, + kCommittedSendable, + kConnectError, + kConnectionReset, + kContentTooLarge, + kDenyConnectSendable, + kDenySendable, + kGoaway, + kH2Codes, + kInternalError, + kProtocolError, + kRequestBodyRejected, + kRequestCancelled, + kRequestRejected, + kStreamReset, + kTimeout, + kTunnelSendable, + notSupported, + protocolErrorFromH2, +}; diff --git a/lib/internal/http/fetch_server/exchange.js b/lib/internal/http/fetch_server/exchange.js new file mode 100644 index 000000000000..ecef7ea804d6 --- /dev/null +++ b/lib/internal/http/fetch_server/exchange.js @@ -0,0 +1,359 @@ +'use strict'; + +// Exchange state model of the Fetch Server API (Section 6), shared by the contexts, +// the request, and the protocol backends. +// https://fetch-server.proposal.wintertc.org/#exchange-states + +const { + ArrayPrototypePush, + PromisePrototypeThen, + PromiseReject, + PromiseResolve, + PromiseWithResolvers, + SafeSet, +} = primordials; + +const { + createProtocolError, + invalidState, + kRequestBodyRejected, +} = require('internal/http/fetch_server/errors'); + +const { + markHandled, +} = require('internal/http/fetch_server/body'); + +const { + setUnrefTimeout, +} = require('internal/timers'); +const { isError } = require('internal/util'); +const { clearTimeout } = require('timers'); + +let AbortController; +function lazyAbortController() { + AbortController ??= require('internal/abort_controller').AbortController; + return AbortController; +} + +let reportFn; +/** + * Reports an error in an implementation-defined manner (Section 7.6, Section 16.4). + * @param {any} error + */ +function report(error) { + reportFn ??= (err) => { + process.emitWarning(isError(err) ? err : `${err}`, + 'FetchServerWarning'); + }; + reportFn(error); +} + +const kTerminalStates = new SafeSet(['complete', 'denied', 'aborted']); + +class Exchange { + // 'open' | 'informational' | 'committed' | 'complete' | 'denied' | + // 'aborted' + state = 'open'; + abortReason = undefined; + + /** @type {'fetch'|'connect'|'upgrade'} */ + kind; + backend; + listener; + autoContinue; + + pending100 = false; + fallThrough = false; + handlerSettled = false; + isWebSocket = false; + // The established tunnel, WebSocket connection, or null. + tunnel = null; + + #abortController = null; + #abortWaiters = []; + #racers = null; + #completed; + #waitUntilCount = 0; + #waitUntilTimer = null; + #ended = false; + #onEnded; + #request = null; + #requestFactory; + #bodyRefused = false; + + // Request trailers (Section 8.2). + trailersState = 'pending'; + trailersList = null; + trailersError = undefined; + #trailersPromise = null; + #trailersSettlers = null; + + constructor(kind, backend, listener, options) { + this.kind = kind; + this.backend = backend; + this.listener = listener; + this.autoContinue = options.autoContinue; + this.#onEnded = options.onEnded; + this.#requestFactory = options.requestFactory; + this.waitUntilTimeout = options.waitUntilTimeout; + this.maxRequestBodySize = options.maxRequestBodySize; + const completed = PromiseWithResolvers(); + markHandled(completed.promise); + this.#completed = completed; + if (kind === 'connect') { + this.failTrailers(invalidState( + 'The request of a CONNECT exchange has no content')); + } + } + + get terminal() { + return kTerminalStates.has(this.state); + } + + get responseCapable() { + return this.state === 'open' || this.state === 'informational'; + } + + get completed() { + return this.#completed.promise; + } + + get ended() { + return this.#ended; + } + + get bodyRefused() { + return this.#bodyRefused; + } + + get request() { + this.#request ??= this.#requestFactory(this); + return this.#request; + } + + get signal() { + if (this.#abortController === null) { + this.#abortController = new (lazyAbortController())(); + if (this.state === 'aborted') { + this.#abortController.abort(this.abortReason); + } + } + return this.#abortController.signal; + } + + /** + * Registers a callback invoked when the exchange is aborted. + * @param {(reason: any) => void} fn + */ + onAbort(fn) { + if (this.state === 'aborted') { + fn(this.abortReason); + return; + } + if (this.terminal) return; + ArrayPrototypePush(this.#abortWaiters, fn); + } + + /** + * Races promise against the abort of the exchange: the result settles as + * promise does, or rejects with the abort reason once the exchange is + * aborted. A rejection of promise after the abort is not observed. + * + * This is cheaper than a promise race, and unlike one it does not add a + * reaction to a long-lived promise for every call. + * @param {Promise} promise + * @returns {Promise} + */ + raceAbort(promise) { + if (this.state === 'aborted') { + markHandled(promise); + return PromiseReject(this.abortReason); + } + const { promise: result, resolve, reject } = PromiseWithResolvers(); + const racers = this.#racers ??= new SafeSet(); + racers.add(reject); + PromisePrototypeThen(promise, (value) => { + racers.delete(reject); + resolve(value); + }, (error) => { + racers.delete(reject); + reject(error); + }); + return result; + } + + #clearAbortWaiters() { + this.#abortWaiters = []; + } + + /** + * Transitions to "aborted" (Section 6), propagating the reason to every surface + * (Section 15.4). + * @param {any} reason A protocol error. + */ + abort(reason) { + if (this.terminal) return; + this.state = 'aborted'; + this.abortReason = reason; + this.pending100 = false; + this.#abortController?.abort(reason); + this.failTrailers(reason); + this.#completed.reject(reason); + const racers = this.#racers; + if (racers !== null) { + this.#racers = null; + for (const reject of racers) reject(reason); + } + const waiters = this.#abortWaiters; + this.#abortWaiters = []; + for (let i = 0; i < waiters.length; i++) { + try { + waiters[i](reason); + } catch (err) { + report(err); + } + } + this.#maybeEnd(); + } + + /** Transitions to "complete" (Section 6). */ + complete() { + if (this.terminal) return; + this.state = 'complete'; + this.pending100 = false; + this.#clearAbortWaiters(); + this.#refuseUnreadBody(); + this.#completed.resolve(); + this.#maybeEnd(); + } + + /** Transitions to "denied" (Section 6). */ + deny() { + this.state = 'denied'; + this.pending100 = false; + this.#clearAbortWaiters(); + this.#refuseUnreadBody(); + this.#completed.resolve(); + this.#maybeEnd(); + } + + commit() { + this.state = 'committed'; + } + + #refuseUnreadBody() { + if (this.trailersState === 'pending') { + this.refuseBody(); + } + } + + /** + * Refuses the remainder of the request body (Section 15.3). + */ + refuseBody() { + if (this.#bodyRefused) return; + this.#bodyRefused = true; + this.failTrailers(createProtocolError( + kRequestBodyRejected, 'The request body was not read to its end')); + this.backend.refuseBody?.(); + } + + // Request trailers (Section 8.2). + + get trailers() { + if (this.#trailersPromise === null) { + const { promise, resolve, reject } = PromiseWithResolvers(); + markHandled(promise); + this.#trailersPromise = promise; + this.#trailersSettlers = { resolve, reject }; + this.#settleTrailersPromise(); + } + return this.#trailersPromise; + } + + #settleTrailersPromise() { + if (this.#trailersSettlers === null) return; + if (this.trailersState === 'complete') { + const { createServerHeaders } = + require('internal/http/fetch_server/headers'); + this.#trailersSettlers.resolve( + createServerHeaders(this.trailersList, 'immutable')); + } else if (this.trailersState === 'errored') { + this.#trailersSettlers.reject(this.trailersError); + } + } + + completeTrailers(list) { + if (this.trailersState !== 'pending') return; + this.trailersState = 'complete'; + this.trailersList = list; + this.#settleTrailersPromise(); + } + + failTrailers(error) { + if (this.trailersState !== 'pending') return; + this.trailersState = 'errored'; + this.trailersError = error; + this.#settleTrailersPromise(); + } + + // Lifecycle (Section 7.6). + + waitUntil(promise) { + if (this.#ended) return; + this.#waitUntilCount++; + PromisePrototypeThen(PromiseResolve(promise), () => { + this.#waitUntilSettled(); + }, (err) => { + if (!this.#ended) report(err); + this.#waitUntilSettled(); + }); + } + + #waitUntilSettled() { + if (this.#ended) return; + this.#waitUntilCount--; + this.#maybeEnd(); + } + + handlerDone() { + this.handlerSettled = true; + this.#maybeEnd(); + } + + #maybeEnd() { + if (this.#ended || !this.terminal || !this.handlerSettled) return; + if (this.#waitUntilCount > 0) { + if (this.#waitUntilTimer === null && this.waitUntilTimeout > 0) { + this.#waitUntilTimer = setUnrefTimeout(() => { + this.#end(); + }, this.waitUntilTimeout); + } + return; + } + this.#end(); + } + + #end() { + if (this.#ended) return; + this.#ended = true; + if (this.#waitUntilTimer !== null) { + clearTimeout(this.#waitUntilTimer); + this.#waitUntilTimer = null; + } + this.#onEnded?.(this); + } + + /** + * Ends the exchange without waiting for waitUntil() promises, used by + * destroy() (Section 18.2.3). + */ + forceEnd() { + this.#end(); + } +} + +module.exports = { + Exchange, + report, +}; diff --git a/lib/internal/http/fetch_server/h1.js b/lib/internal/http/fetch_server/h1.js new file mode 100644 index 000000000000..1fa2dd69947f --- /dev/null +++ b/lib/internal/http/fetch_server/h1.js @@ -0,0 +1,371 @@ +'use strict'; + +// HTTP/1.x backend for the Fetch Server API, built on node:http. + +const { + ArrayPrototypePush, + PromiseResolve, + PromiseWithResolvers, + RegExpPrototypeExec, + SafeSet, + StringPrototypeSplit, + StringPrototypeToLowerCase, + StringPrototypeTrim, +} = primordials; + +const { URL } = require('internal/url'); + +const { + createProtocolError, + kConnectionReset, + kGoaway, +} = require('internal/http/fetch_server/errors'); + +const { + NodeReadableReader, +} = require('internal/http/fetch_server/node_reader'); + +const { + isProhibitedTrailerName, + listContains, +} = require('internal/http/fetch_server/headers'); + +const kHopByHop = new SafeSet([ + 'connection', 'keep-alive', 'proxy-connection', 'transfer-encoding', +]); +const kAbsoluteForm = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//; +const kCloseOption = /(?:^|,)\s*close\s*(?:,|$)/i; + +function filterRequestTrailers(rawTrailers) { + const list = []; + for (let i = 0; i + 1 < rawTrailers.length; i += 2) { + if (!isProhibitedTrailerName(rawTrailers[i])) { + ArrayPrototypePush(list, [rawTrailers[i], rawTrailers[i + 1]]); + } + } + return list; +} + +/** + * Builds the request header list from raw headers (Section 8.1). + * @param {string[]} rawHeaders + * @returns {Array<[string, string]>} + */ +function buildRequestHeaderList(rawHeaders) { + // The connection options of the Connection header field, which are + // removed as hop-by-hop fields, are rare enough to collect lazily. + let removed = null; + for (let i = 0; i + 1 < rawHeaders.length; i += 2) { + if (StringPrototypeToLowerCase(rawHeaders[i]) === 'connection') { + const options = StringPrototypeSplit(rawHeaders[i + 1], ','); + for (let j = 0; j < options.length; j++) { + const option = StringPrototypeToLowerCase( + StringPrototypeTrim(options[j])); + if (option !== 'upgrade' && option !== 'te' && option !== '' && + !kHopByHop.has(option)) { + removed ??= new SafeSet(); + removed.add(option); + } + } + } + } + const list = []; + for (let i = 0; i + 1 < rawHeaders.length; i += 2) { + const lower = StringPrototypeToLowerCase(rawHeaders[i]); + if (!kHopByHop.has(lower) && (removed === null || !removed.has(lower))) { + ArrayPrototypePush(list, [rawHeaders[i], rawHeaders[i + 1]]); + } + } + return list; +} + +function flattenFields(fields) { + const flat = []; + for (let i = 0; i < fields.length; i++) { + ArrayPrototypePush(flat, fields[i][0], fields[i][1]); + } + return flat; +} + +function socketAddress(address, port, family) { + if (address === undefined) return undefined; + return { address, port, family: family === 'IPv6' ? 'IPv6' : 'IPv4' }; +} + +class H1Backend { + req; + res; + socket; + listener; + kind; + method; + url; + connectProtocol = null; + // The stream that carries tunnel data for CONNECT and upgrades. + #tunnelStream; + #head; + #reader = null; + #headerList = null; + + /** + * @param {object} options + * @param {import('http').IncomingMessage} options.req + * @param {import('http').ServerResponse} options.res + * @param {import('net').Socket} options.socket + * @param {object} options.listener + * @param {'fetch'|'connect'|'upgrade'} options.kind + * @param {import('stream').Duplex} [options.tunnelStream] + * @param {Buffer} [options.head] + */ + constructor({ req, res, socket, listener, kind, tunnelStream, head }) { + this.req = req; + this.res = res; + this.socket = socket; + this.listener = listener; + this.kind = kind; + this.#tunnelStream = tunnelStream; + this.#head = head; + this.method = req.method; + this.url = this.#computeUrl(); + res.strictContentLength = true; + } + + get headerList() { + this.#headerList ??= buildRequestHeaderList(this.req.rawHeaders); + return this.#headerList; + } + + get httpVersion() { + return this.req.httpVersion === '1.0' ? '1.0' : '1.1'; + } + + get alpnProtocol() { + const alpn = this.socket.alpnProtocol; + return typeof alpn === 'string' && alpn !== '' ? alpn : 'http/1.1'; + } + + get encrypted() { + return !!this.socket.encrypted; + } + + get serverName() { + const name = this.socket.servername; + return typeof name === 'string' && name !== '' ? name : null; + } + + remoteAddress() { + const s = this.socket; + return socketAddress(s.remoteAddress, s.remotePort, s.remoteFamily); + } + + localAddress() { + const s = this.socket; + return socketAddress(s.localAddress, s.localPort, s.localFamily); + } + + get hasBody() { + const req = this.req; + if (this.kind === 'connect') return false; + return req.headers['transfer-encoding'] !== undefined || + (req.headers['content-length'] !== undefined && + req.headers['content-length'] !== '0'); + } + + #computeUrl() { + const scheme = this.encrypted ? 'https' : 'http'; + const target = this.req.url; + if (this.kind === 'connect') { + return new URL(`${scheme}://${target}/`).href; + } + if (RegExpPrototypeExec(kAbsoluteForm, target) !== null) { + return new URL(target).href; + } + let host = this.req.headers.host; + if (host === undefined) { + // HTTP/1.0 requests can lack a Host header field. + const local = this.socket.localAddress; + const port = this.socket.localPort; + host = this.socket.localFamily === 'IPv6' ? + `[${local}]:${port}` : `${local}:${port}`; + } + return new URL(`${scheme}://${host}${target}`).href; + } + + /** + * Associates the backend with its exchange and wires abort detection. + * @param {import('./exchange').Exchange} exchange + */ + attach(exchange) { + const onClose = () => { + if (exchange.terminal || exchange.tunnel !== null) return; + if (exchange.state === 'committed' && this.res.writableFinished) { + // The response was handed to the transport in full. + exchange.complete(); + return; + } + exchange.abort(createProtocolError( + this.listener.destroying ? kGoaway : kConnectionReset, + 'The connection was lost')); + }; + this.res.on('error', () => {}); + this.res.on('close', onClose); + this.socket.on('close', onClose); + this.socket.on('error', () => {}); + if (this.kind === 'connect' || !this.hasBody) { + if (this.kind !== 'connect') exchange.completeTrailers([]); + } + } + + readBody(exchange) { + this.#reader ??= new NodeReadableReader(this.req, () => { + exchange.completeTrailers(filterRequestTrailers(this.req.rawTrailers)); + }); + return this.#reader.read(); + } + + refuseBody() { + // Node.js reads and discards the remainder of the body after the + // response has been sent. + } + + sendContinue(fields) { + this.res.writeInformation(100, flattenFields(fields)); + this.res._sent100 = true; + } + + sendInformational(status, fields) { + this.res.writeInformation(status, flattenFields(fields)); + } + + deny() { + // HTTP/1.1 has no stream reset: close the connection (Section 7.5). + this.socket.destroy(); + } + + reset() { + this.res.destroy(); + this.socket.destroy(); + } + + canSendTrailers(headerList) { + return this.httpVersion === '1.1' && + !listContains(headerList, 'content-length'); + } + + respond(status, statusText, headerList, + { hasBody, wantTrailers, contentLength }) { + const res = this.res; + const flat = []; + let close = false; + let upgrade = false; + let hasAltSvc = false; + let hasLength = false; + for (let i = 0; i < headerList.length; i++) { + const { 0: name, 1: value } = headerList[i]; + const lower = StringPrototypeToLowerCase(name); + if (lower === 'connection') { + if (RegExpPrototypeExec(kCloseOption, value) !== null) close = true; + continue; + } + if (kHopByHop.has(lower)) continue; + if (lower === 'upgrade') upgrade = true; + if (lower === 'trailer' && !wantTrailers) continue; + if (lower === 'alt-svc') hasAltSvc = true; + if (lower === 'content-length') { + if (status === 204) continue; + hasLength = true; + } + ArrayPrototypePush(flat, name, value); + } + if (!hasLength && !wantTrailers && status !== 204 && status !== 304 && + this.req.method !== 'HEAD') { + const length = hasBody ? contentLength : 0; + if (length !== undefined) { + ArrayPrototypePush(flat, 'Content-Length', `${length}`); + } + } + if (upgrade) ArrayPrototypePush(flat, 'Connection', 'upgrade'); + const altSvc = this.listener.altSvc; + if (typeof altSvc === 'string' && this.encrypted && !hasAltSvc) { + ArrayPrototypePush(flat, 'Alt-Svc', altSvc); + } + if (close || this.listener.draining || this.kind !== 'fetch') { + res.shouldKeepAlive = false; + } + res.writeHead(status, statusText === '' ? undefined : statusText, flat); + } + + write(batch) { + const res = this.res; + let ok = true; + for (let i = 0; i < batch.length; i++) { + ok = res.write(batch[i]); + } + if (ok) return PromiseResolve(); + const { promise, resolve } = PromiseWithResolvers(); + res.once('drain', resolve); + return promise; + } + + /** + * Ends the response. + * @param {Array<[string, string]>|null} trailers + * @param {Uint8Array|null} [data] The last data of the body. + * @returns {Promise} + */ + end(trailers, data = null) { + const res = this.res; + if (trailers !== null && trailers.length > 0) { + res.addTrailers(trailers); + } + const { promise, resolve } = PromiseWithResolvers(); + if (data === null) res.end(resolve); + else res.end(data, resolve); + return promise; + } + + /** + * Performs the success response for a CONNECT or the upgrade handshake, + * and returns the transport stream of the tunnel. + * @returns {Promise<{ stream: import('stream').Duplex }>} + */ + acceptTunnel(exchange, fields, protocol, status = 0) { + const socket = this.socket; + let head; + if (this.kind === 'upgrade') { + if (exchange.pending100) { + exchange.pending100 = false; + this.sendContinue([]); + } + head = 'HTTP/1.1 101 Switching Protocols\r\n' + + `Upgrade: ${protocol}\r\nConnection: Upgrade\r\n`; + } else { + head = `HTTP/1.1 ${status || 200} Connection Established\r\n`; + } + for (let i = 0; i < fields.length; i++) { + const lower = StringPrototypeToLowerCase(fields[i][0]); + if (kHopByHop.has(lower) || lower === 'upgrade' || + lower === 'content-length') { + continue; + } + head += `${fields[i][0]}: ${fields[i][1]}\r\n`; + } + head += '\r\n'; + // The legacy response object is no longer needed. + this.res.detachSocket?.(socket); + socket.write(head); + const stream = this.#tunnelStream ?? socket; + if (this.#head !== undefined && this.#head.length > 0) { + stream.unshift(this.#head); + } + return { __proto__: null, stream, http2: false }; + } + + goaway() {} +} + +module.exports = { + H1Backend, + buildRequestHeaderList, + flattenFields, +}; diff --git a/lib/internal/http/fetch_server/h2.js b/lib/internal/http/fetch_server/h2.js new file mode 100644 index 000000000000..f5957fdfe77f --- /dev/null +++ b/lib/internal/http/fetch_server/h2.js @@ -0,0 +1,352 @@ +'use strict'; + +// HTTP/2 backend for the Fetch Server API, built on node:http2. + +const { + ArrayIsArray, + ArrayPrototypeJoin, + ArrayPrototypePush, + PromiseResolve, + PromiseWithResolvers, + SafeSet, + SafeWeakMap, + StringPrototypeCharCodeAt, + StringPrototypeToLowerCase, +} = primordials; + +const { URL } = require('internal/url'); + +const { + createProtocolError, + kConnectionReset, + kGoaway, + kH2Codes, + kRequestRejected, + protocolErrorFromH2, +} = require('internal/http/fetch_server/errors'); + +const { setImmediate } = require('timers'); + +const { + NodeReadableReader, +} = require('internal/http/fetch_server/node_reader'); + +const { + isProhibitedTrailerName, + listContains, +} = require('internal/http/fetch_server/headers'); + +const kConnectionSpecific = new SafeSet([ + 'connection', 'keep-alive', 'proxy-connection', 'transfer-encoding', + 'upgrade', +]); + +/** + * Builds the request header list from HTTP/2 raw headers (Section 8.1). + * @param {string[]} rawHeaders A flat [name, value, ...] array. + * @param {string|undefined} authority + * @returns {Array<[string, string]>} + */ +function buildRequestHeaderList(rawHeaders, authority) { + const list = []; + let cookies = null; + let hasHost = false; + for (let i = 0; i + 1 < rawHeaders.length; i += 2) { + const name = rawHeaders[i]; + if (StringPrototypeCharCodeAt(name, 0) === 58 /* ':' */) continue; + const lower = StringPrototypeToLowerCase(name); + if (kConnectionSpecific.has(lower)) continue; + if (lower === 'cookie') { + cookies ??= []; + ArrayPrototypePush(cookies, rawHeaders[i + 1]); + continue; + } + if (lower === 'host') hasHost = true; + ArrayPrototypePush(list, [name, rawHeaders[i + 1]]); + } + if (!hasHost && authority !== undefined) { + ArrayPrototypePush(list, ['host', authority]); + } + if (cookies !== null) { + ArrayPrototypePush(list, ['cookie', ArrayPrototypeJoin(cookies, '; ')]); + } + return list; +} + +function toHeaderObject(fields, base) { + const headers = base ?? { __proto__: null }; + for (let i = 0; i < fields.length; i++) { + const name = StringPrototypeToLowerCase(fields[i][0]); + if (kConnectionSpecific.has(name)) continue; + const value = fields[i][1]; + const prev = headers[name]; + if (prev === undefined) { + headers[name] = value; + } else if (ArrayIsArray(prev)) { + ArrayPrototypePush(prev, value); + } else { + headers[name] = [prev, value]; + } + } + return headers; +} + +function socketAddress(address, port, family) { + if (address === undefined) return undefined; + return { address, port, family: family === 'IPv6' ? 'IPv6' : 'IPv4' }; +} + +// Tracks the streams of each session being processed, to select the last +// stream identifier of a GOAWAY frame sent by deny() (Section 7.5). +const sessionStreams = new SafeWeakMap(); + +class H2Backend { + stream; + session; + listener; + kind; + headerList; + method; + url; + connectProtocol; + #reader = null; + + constructor({ stream, headers, flags, rawHeaders, listener }) { + this.stream = stream; + this.session = stream.session; + this.listener = listener; + this.method = headers[':method']; + const authority = headers[':authority']; + this.connectProtocol = headers[':protocol']; + this.kind = this.method === 'CONNECT' ? 'connect' : 'fetch'; + this.headerList = buildRequestHeaderList(rawHeaders, authority); + const scheme = this.encrypted ? 'https' : 'http'; + if (this.kind === 'connect' && this.connectProtocol === undefined) { + this.url = new URL(`${scheme}://${authority}/`).href; + } else { + const host = authority ?? headers.host; + this.url = new URL( + `${headers[':scheme'] ?? scheme}://${host}${headers[':path']}`).href; + } + let streams = sessionStreams.get(this.session); + if (streams === undefined) { + streams = new SafeSet(); + sessionStreams.set(this.session, streams); + } + streams.add(stream.id); + stream.once('close', () => streams.delete(stream.id)); + } + + get httpVersion() { + return '2'; + } + + get alpnProtocol() { + return this.encrypted ? 'h2' : 'h2c'; + } + + get encrypted() { + return !!this.session.encrypted; + } + + get serverName() { + const name = this.session.socket?.servername; + return typeof name === 'string' && name !== '' ? name : null; + } + + remoteAddress() { + const s = this.session.socket; + return socketAddress(s?.remoteAddress, s?.remotePort, s?.remoteFamily); + } + + localAddress() { + const s = this.session.socket; + return socketAddress(s?.localAddress, s?.localPort, s?.localFamily); + } + + get hasBody() { + return !this.stream.endAfterHeaders && this.kind !== 'connect'; + } + + attach(exchange) { + const stream = this.stream; + const trailers = []; + stream.on('error', () => {}); + stream.on('trailers', (headers, flags, rawHeaders) => { + for (let i = 0; i + 1 < rawHeaders.length; i += 2) { + if (StringPrototypeCharCodeAt(rawHeaders[i], 0) === 58) continue; + if (!isProhibitedTrailerName(rawHeaders[i])) { + ArrayPrototypePush(trailers, [rawHeaders[i], rawHeaders[i + 1]]); + } + } + }); + this.trailersList = trailers; + stream.on('close', () => { + if (exchange.terminal || exchange.tunnel !== null) return; + if (exchange.state === 'committed' && stream.writableFinished && + !stream.rstCode) { + exchange.complete(); + return; + } + exchange.abort(this.closeReason()); + }); + if (!this.hasBody && this.kind !== 'connect') { + exchange.completeTrailers([]); + } + } + + closeReason() { + const stream = this.stream; + const rstCode = stream.rstCode; + if (rstCode !== undefined && rstCode !== 0 && + !this.session.destroyed) { + return protocolErrorFromH2(rstCode, this.alpnProtocol); + } + if (this.session.closed && !this.session.destroyed) { + return createProtocolError(kGoaway, 'The connection is going away'); + } + return createProtocolError( + this.listener.destroying ? kGoaway : kConnectionReset, + 'The connection was lost'); + } + + readBody(exchange) { + this.#reader ??= new NodeReadableReader(this.stream, () => { + exchange.completeTrailers(this.trailersList); + }); + return this.#reader.read(); + } + + refuseBody() { + const stream = this.stream; + if (stream.destroyed || stream._readableState.ended) return; + if (stream.headersSent && !stream.writableFinished) { + // Refuse after the response is complete (RFC 9113 Section 8.1). + stream.once('finish', () => this.refuseBody()); + return; + } + if (stream.headersSent) { + setImmediate(() => { + if (!stream.destroyed && !stream._readableState.ended) { + stream.close(kH2Codes.ERR_HTTP_REQUEST_BODY_REJECTED); + } + }); + } + } + + sendContinue(fields) { + this.stream.additionalHeaders(toHeaderObject(fields, + { '__proto__': null, ':status': 100 })); + } + + sendInformational(status, fields) { + this.stream.additionalHeaders(toHeaderObject(fields, + { '__proto__': null, ':status': status })); + } + + deny(code) { + const stream = this.stream; + if (code === kGoaway) { + const session = this.session; + let last = 0; + for (const id of sessionStreams.get(session) ?? []) { + if (id !== stream.id && id > last) last = id; + } + stream.close(kH2Codes[kRequestRejected]); + try { + session.goaway(0, last); + } catch { + // The session might already be closing. + } + return; + } + stream.close(kH2Codes[code]); + } + + reset(code) { + const stream = this.stream; + if (!stream.destroyed) stream.close(kH2Codes[code] ?? kH2Codes.ERR_HTTP_INTERNAL_ERROR); + } + + canSendTrailers() { + return true; + } + + respond(status, statusText, headerList, + { hasBody, wantTrailers, contentLength }) { + const headers = toHeaderObject(headerList, + { '__proto__': null, ':status': status }); + const altSvc = this.listener.altSvc; + if (typeof altSvc === 'string' && this.encrypted && + !listContains(headerList, 'alt-svc')) { + headers['alt-svc'] = altSvc; + } + if (status === 204 || status === 304) { + delete headers['content-length']; + } else if (contentLength !== undefined && hasBody && !wantTrailers && + headers['content-length'] === undefined) { + headers['content-length'] = `${contentLength}`; + } + this.stream.respond(headers, { + endStream: !hasBody && !wantTrailers, + waitForTrailers: wantTrailers, + }); + } + + write(batch) { + const stream = this.stream; + let ok = true; + for (let i = 0; i < batch.length; i++) { + ok = stream.write(batch[i]); + } + if (ok) return PromiseResolve(); + const { promise, resolve } = PromiseWithResolvers(); + stream.once('drain', resolve); + return promise; + } + + end(trailers, data = null) { + const stream = this.stream; + const { promise, resolve: resolveEnd } = PromiseWithResolvers(); + const resolve = () => { + // Once the response is complete, refuse whatever the client has not + // sent yet (RFC 9113 Section 8.1). Like http2 core, defer the reset so + // that the queued response frames are sent before RST_STREAM. + if (!stream.destroyed && !stream._readableState.ended) { + setImmediate(() => { + if (!stream.destroyed && !stream._readableState.ended) { + stream.close(kH2Codes.ERR_HTTP_REQUEST_BODY_REJECTED); + } + }); + } + resolveEnd(); + }; + if (trailers !== null) { + stream.once('wantTrailers', () => { + stream.sendTrailers(toHeaderObject(trailers)); + }); + } + if (stream.writableEnded) { + if (stream.writableFinished) resolve(); + else stream.once('finish', resolve); + } else if (data === null) { + stream.end(resolve); + } else { + stream.end(data, resolve); + } + return promise; + } + + acceptTunnel(exchange, fields) { + const stream = this.stream; + const headers = toHeaderObject(fields, { '__proto__': null, ':status': 200 }); + delete headers['content-length']; + stream.respond(headers, { endStream: false }); + return { __proto__: null, stream, http2: true }; + } +} + +module.exports = { + H2Backend, + toHeaderObject, +}; diff --git a/lib/internal/http/fetch_server/headers.js b/lib/internal/http/fetch_server/headers.js new file mode 100644 index 000000000000..0edfea93ad23 --- /dev/null +++ b/lib/internal/http/fetch_server/headers.js @@ -0,0 +1,521 @@ +'use strict'; + +// The ServerHeaders protocol of the Fetch Server API. +// https://fetch-server.proposal.wintertc.org/#serverheaders + +const { + ArrayIsArray, + ArrayPrototypeJoin, + ArrayPrototypePush, + ArrayPrototypeSort, + ArrayPrototypeSplice, + FunctionPrototypeCall, + ObjectDefineProperties, + ObjectGetOwnPropertyDescriptor, + ObjectGetPrototypeOf, + ObjectSetPrototypeOf, + ReflectGet, + ReflectOwnKeys, + RegExpPrototypeExec, + RegExpPrototypeSymbolReplace, + SafeSet, + StringPrototypeCharCodeAt, + StringPrototypeToLowerCase, + Symbol, + SymbolIterator, + SymbolToStringTag, +} = primordials; + +const { + codes: { + ERR_HTTP_HEADERS_SENT, + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_INVALID_HTTP_TOKEN, + ERR_INVALID_THIS, + }, +} = require('internal/errors'); + +const { + _checkIsHttpToken: checkIsHttpToken, +} = require('internal/http/common'); + +const { + converters, +} = require('internal/webidl'); + +const { + customInspectSymbol: kInspect, + kEnumerableProperty, +} = require('internal/util'); + +const { inspect } = require('internal/util/inspect'); + +// eslint-disable-next-line no-control-regex +const kInvalidValueRegExp = /[\x00\x0a\x0d]/; +const kLeadingTrailingWhitespace = /^[\t\n\r ]+|[\t\n\r ]+$/g; + +/** + * Converts V to a Web IDL ByteString. + * @param {any} V + * @param {string} name + * @returns {string} + */ +function toByteString(V, name) { + const str = converters.DOMString(V, { __proto__: null, prefix: name }); + for (let i = 0; i < str.length; i++) { + if (StringPrototypeCharCodeAt(str, i) > 0xff) { + throw new ERR_INVALID_ARG_VALUE(name, str, + 'must only contain characters <= U+00FF'); + } + } + return str; +} + +function normalizeValue(value) { + return RegExpPrototypeSymbolReplace(kLeadingTrailingWhitespace, value, ''); +} + +function isHeaderName(name) { + return name.length > 0 && checkIsHttpToken(name); +} + +function isHeaderValue(value) { + if (value.length === 0) return true; + const first = StringPrototypeCharCodeAt(value, 0); + const last = StringPrototypeCharCodeAt(value, value.length - 1); + if (first === 0x20 || first === 0x09 || last === 0x20 || last === 0x09) { + return false; + } + return RegExpPrototypeExec(kInvalidValueRegExp, value) === null; +} + +function validateName(name) { + if (!isHeaderName(name)) { + throw new ERR_INVALID_HTTP_TOKEN('Header name', name); + } +} + +function validateValue(name, value) { + if (!isHeaderValue(value)) { + throw new ERR_INVALID_ARG_VALUE('value', value, + `is not a valid value for header "${name}"`); + } +} + +// A prohibited trailer name (Section 10.3). +const kProhibitedTrailers = new SafeSet([ + 'content-length', 'transfer-encoding', 'trailer', + 'connection', 'keep-alive', 'proxy-connection', 'te', 'upgrade', + 'host', + 'authorization', 'proxy-authorization', 'www-authenticate', + 'proxy-authenticate', 'cookie', 'set-cookie', + 'cache-control', 'expect', 'max-forwards', 'pragma', 'range', 'if-match', + 'if-none-match', 'if-modified-since', 'if-unmodified-since', 'if-range', + 'age', 'expires', 'date', 'location', 'retry-after', 'vary', + 'content-encoding', 'content-type', 'content-range', +]); + +function isProhibitedTrailerName(name) { + return kProhibitedTrailers.has(StringPrototypeToLowerCase(name)); +} + +/** + * Removes fields with a prohibited trailer name from a header list. + * @param {Array<[string, string]>} list + * @returns {Array<[string, string]>} + */ +function filterTrailers(list) { + const result = []; + for (let i = 0; i < list.length; i++) { + if (!isProhibitedTrailerName(list[i][0])) { + ArrayPrototypePush(result, list[i]); + } + } + return result; +} + +// ============================================================================= +// Header list operations (Fetch Standard) +// ============================================================================= +// A header list is an Array of [name, value] pairs. + +function listIndexOf(list, lowerName, from = 0) { + for (let i = from; i < list.length; i++) { + if (StringPrototypeToLowerCase(list[i][0]) === lowerName) return i; + } + return -1; +} + +function listContains(list, name) { + return listIndexOf(list, StringPrototypeToLowerCase(name)) !== -1; +} + +function listGet(list, name) { + const lower = StringPrototypeToLowerCase(name); + let values; + for (let i = 0; i < list.length; i++) { + if (StringPrototypeToLowerCase(list[i][0]) === lower) { + values ??= []; + ArrayPrototypePush(values, list[i][1]); + } + } + return values === undefined ? null : ArrayPrototypeJoin(values, ', '); +} + +function listGetAll(list, name) { + const lower = StringPrototypeToLowerCase(name); + const values = []; + for (let i = 0; i < list.length; i++) { + if (StringPrototypeToLowerCase(list[i][0]) === lower) { + ArrayPrototypePush(values, list[i][1]); + } + } + return values; +} + +function listDelete(list, name) { + const lower = StringPrototypeToLowerCase(name); + for (let i = list.length - 1; i >= 0; i--) { + if (StringPrototypeToLowerCase(list[i][0]) === lower) { + ArrayPrototypeSplice(list, i, 1); + } + } +} + +function listSet(list, name, value) { + const lower = StringPrototypeToLowerCase(name); + const index = listIndexOf(list, lower); + if (index === -1) { + ArrayPrototypePush(list, [name, value]); + return; + } + list[index] = [list[index][0], value]; + for (let i = list.length - 1; i > index; i--) { + if (StringPrototypeToLowerCase(list[i][0]) === lower) { + ArrayPrototypeSplice(list, i, 1); + } + } +} + +function compareNames(a, b) { + return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; +} + +function sortAndCombine(list) { + const names = []; + const seen = { __proto__: null }; + for (let i = 0; i < list.length; i++) { + const lower = StringPrototypeToLowerCase(list[i][0]); + if (seen[lower] === undefined) { + seen[lower] = true; + ArrayPrototypePush(names, [lower]); + } + } + ArrayPrototypeSort(names, compareNames); + const result = []; + for (let i = 0; i < names.length; i++) { + const name = names[i][0]; + if (name === 'set-cookie') { + const values = listGetAll(list, name); + for (let j = 0; j < values.length; j++) { + ArrayPrototypePush(result, [name, values[j]]); + } + } else { + ArrayPrototypePush(result, [name, listGet(list, name)]); + } + } + return result; +} + +function copyList(list) { + const copy = []; + for (let i = 0; i < list.length; i++) { + ArrayPrototypePush(copy, [list[i][0], list[i][1]]); + } + return copy; +} + +/** + * Converts a ServerHeadersInit to a header list (Section 10.1). + * @param {any} object + * @param {string} [argName] + * @returns {Array<[string, string]>} + */ +function convertHeadersInit(object, argName = 'headers') { + if (object === null || + (typeof object !== 'object' && typeof object !== 'function')) { + throw new ERR_INVALID_ARG_TYPE(argName, ['Array', 'Object'], object); + } + const list = []; + const iteratorMethod = object[SymbolIterator]; + if (iteratorMethod !== undefined && iteratorMethod !== null) { + if (typeof iteratorMethod !== 'function') { + throw new ERR_INVALID_ARG_TYPE(argName, ['Array', 'Object'], object); + } + for (const pair of { [SymbolIterator]: () => FunctionPrototypeCall(iteratorMethod, object) }) { + if (pair === null || + (typeof pair !== 'object' && typeof pair !== 'function') || + typeof pair[SymbolIterator] !== 'function') { + throw new ERR_INVALID_ARG_VALUE(argName, pair, + 'must contain name/value pairs'); + } + const items = []; + for (const item of pair) { + ArrayPrototypePush(items, toByteString(item, argName)); + } + if (items.length !== 2) { + throw new ERR_INVALID_ARG_VALUE(argName, pair, + 'must contain name/value pairs'); + } + const name = items[0]; + const value = normalizeValue(items[1]); + validateName(name); + validateValue(name, value); + ArrayPrototypePush(list, [name, value]); + } + return list; + } + const keys = ReflectOwnKeys(object); + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + const desc = ObjectGetOwnPropertyDescriptor(object, key); + if (desc === undefined || !desc.enumerable) continue; + const name = toByteString(key, argName); + const value = normalizeValue(toByteString(ReflectGet(object, key), argName)); + validateName(name); + validateValue(name, value); + ArrayPrototypePush(list, [name, value]); + } + return list; +} + +// ============================================================================= +// ServerHeaders +// ============================================================================= + +const kConstruct = Symbol('kConstruct'); + +let getHeaderList; +let setHeadersImmutable; +let isServerHeaders; + +class ServerHeaders { + #list; + #immutable; + + static { + getHeaderList = (headers) => headers.#list; + setHeadersImmutable = (headers) => { headers.#immutable = true; }; + isServerHeaders = (value) => { + return value != null && typeof value === 'object' && #list in value; + }; + } + + constructor(key, list, immutable) { + if (key !== kConstruct) { + throw new ERR_INVALID_THIS('ServerHeaders cannot be constructed'); + } + this.#list = list; + this.#immutable = immutable; + } + + #validate(name, value) { + validateName(name); + validateValue(name, value); + if (this.#immutable) { + throw new ERR_HTTP_HEADERS_SENT('modify'); + } + } + + append(name, value) { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + name = toByteString(name, 'name'); + value = normalizeValue(toByteString(value, 'value')); + this.#validate(name, value); + ArrayPrototypePush(this.#list, [name, value]); + } + + delete(name) { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + name = toByteString(name, 'name'); + this.#validate(name, ''); + listDelete(this.#list, name); + } + + get(name) { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + name = toByteString(name, 'name'); + validateName(name); + return listGet(this.#list, name); + } + + getSetCookie() { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + return listGetAll(this.#list, 'set-cookie'); + } + + has(name) { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + name = toByteString(name, 'name'); + validateName(name); + return listContains(this.#list, name); + } + + set(name, value) { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + name = toByteString(name, 'name'); + value = normalizeValue(toByteString(value, 'value')); + this.#validate(name, value); + listSet(this.#list, name, value); + } + + entries() { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + return createHeadersIterator(this, 'key+value'); + } + + keys() { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + return createHeadersIterator(this, 'key'); + } + + values() { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + return createHeadersIterator(this, 'value'); + } + + forEach(callback, thisArg = undefined) { + if (!isServerHeaders(this)) throw new ERR_INVALID_THIS('ServerHeaders'); + if (typeof callback !== 'function') { + throw new ERR_INVALID_ARG_TYPE('callback', 'Function', callback); + } + let pairs = sortAndCombine(this.#list); + for (let i = 0; i < pairs.length; i++) { + FunctionPrototypeCall(callback, thisArg, pairs[i][1], pairs[i][0], this); + // The list may have been modified by the callback. + pairs = sortAndCombine(this.#list); + } + } + + [kInspect](depth, options) { + if (depth < 0) return 'ServerHeaders {}'; + const obj = { __proto__: null }; + const pairs = sortAndCombine(this.#list); + for (let i = 0; i < pairs.length; i++) { + const name = pairs[i][0]; + const prev = obj[name]; + if (prev === undefined) { + obj[name] = pairs[i][1]; + } else if (ArrayIsArray(prev)) { + ArrayPrototypePush(prev, pairs[i][1]); + } else { + obj[name] = [prev, pairs[i][1]]; + } + } + const opts = { + __proto__: null, + ...options, + depth: options.depth == null ? null : options.depth - 1, + }; + return `ServerHeaders ${inspect(obj, opts)}`; + } +} + +ObjectDefineProperties(ServerHeaders.prototype, { + append: kEnumerableProperty, + delete: kEnumerableProperty, + get: kEnumerableProperty, + getSetCookie: kEnumerableProperty, + has: kEnumerableProperty, + set: kEnumerableProperty, + entries: kEnumerableProperty, + keys: kEnumerableProperty, + values: kEnumerableProperty, + forEach: kEnumerableProperty, + [SymbolIterator]: { + __proto__: null, + configurable: true, + writable: true, + value: ServerHeaders.prototype.entries, + }, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'ServerHeaders', + }, +}); + +// Pair iterator, as for a Web IDL `iterable`. +const kIteratorPrototype = + ObjectGetPrototypeOf(ObjectGetPrototypeOf([][SymbolIterator]())); + +const kHeadersIteratorPrototype = { + __proto__: kIteratorPrototype, + next() { + const state = this?.[kIteratorState]; + if (state === undefined) { + throw new ERR_INVALID_THIS('ServerHeaders Iterator'); + } + const pairs = sortAndCombine(getHeaderList(state.target)); + if (state.index >= pairs.length) { + return { value: undefined, done: true }; + } + const pair = pairs[state.index++]; + let value; + switch (state.kind) { + case 'key': value = pair[0]; break; + case 'value': value = pair[1]; break; + default: value = [pair[0], pair[1]]; + } + return { value, done: false }; + }, +}; +ObjectDefineProperties(kHeadersIteratorPrototype, { + next: kEnumerableProperty, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'ServerHeaders Iterator', + }, +}); + +const kIteratorState = Symbol('kIteratorState'); + +function createHeadersIterator(target, kind) { + const iterator = { [kIteratorState]: { target, kind, index: 0 } }; + ObjectSetPrototypeOf(iterator, kHeadersIteratorPrototype); + return iterator; +} + +/** + * Creates a server headers object (Section 10.1). + * @param {Array<[string, string]>} [list] + * @param {'mutable'|'immutable'} [mutability] + * @returns {ServerHeaders} + */ +function createServerHeaders(list = [], mutability = 'mutable') { + return new ServerHeaders(kConstruct, list, mutability === 'immutable'); +} + +module.exports = { + ServerHeaders, + convertHeadersInit, + copyList, + createServerHeaders, + filterTrailers, + getHeaderList, + isHeaderName, + isHeaderValue, + isProhibitedTrailerName, + isServerHeaders, + listContains, + listDelete, + listGet, + listGetAll, + listSet, + normalizeValue, + setHeadersImmutable, + sortAndCombine, + toByteString, +}; diff --git a/lib/internal/http/fetch_server/index.js b/lib/internal/http/fetch_server/index.js new file mode 100644 index 000000000000..af2449b27203 --- /dev/null +++ b/lib/internal/http/fetch_server/index.js @@ -0,0 +1,17 @@ +'use strict'; + +// Experimental implementation of the Fetch Server API. +// https://fetch-server.proposal.wintertc.org/ + +const { + ObjectFreeze, +} = primordials; + +const { ServerResponse } = require('internal/http/fetch_server/response'); +const { serve } = require('internal/http/fetch_server/server'); + +module.exports = ObjectFreeze({ + __proto__: null, + ServerResponse, + serve, +}); diff --git a/lib/internal/http/fetch_server/node_reader.js b/lib/internal/http/fetch_server/node_reader.js new file mode 100644 index 000000000000..2673c03c7d1a --- /dev/null +++ b/lib/internal/http/fetch_server/node_reader.js @@ -0,0 +1,122 @@ +'use strict'; + +// Reads batches of Uint8Array chunks from a Node.js Readable. + +const { + ArrayPrototypePush, + PromiseReject, + PromiseResolve, + PromiseWithResolvers, + TypedArrayPrototypeGetBuffer, + TypedArrayPrototypeGetByteLength, + TypedArrayPrototypeGetByteOffset, + Uint8Array, +} = primordials; + +const { + codes: { + ERR_STREAM_PREMATURE_CLOSE, + }, +} = require('internal/errors'); + +function toUint8Array(chunk) { + if (typeof chunk === 'string') { + chunk = require('buffer').Buffer.from(chunk); + } + return new Uint8Array(TypedArrayPrototypeGetBuffer(chunk), + TypedArrayPrototypeGetByteOffset(chunk), + TypedArrayPrototypeGetByteLength(chunk)); +} + +class NodeReadableReader { + #stream; + #attached = false; + #ended = false; + #error = null; + #waiter = null; + #onEnd; + + /** + * @param {import('stream').Readable} stream + * @param {() => void} [onEnd] Invoked once the stream has ended. + */ + constructor(stream, onEnd) { + this.#stream = stream; + this.#onEnd = onEnd; + } + + get ended() { + return this.#ended; + } + + #attach() { + if (this.#attached) return; + this.#attached = true; + const stream = this.#stream; + stream.on('readable', () => this.#wake()); + stream.on('end', () => { + this.#ended = true; + this.#onEnd?.(); + this.#wake(); + }); + stream.on('error', (err) => { + this.#error ??= err; + this.#wake(); + }); + stream.on('close', () => { + if (!this.#ended) this.#error ??= new ERR_STREAM_PREMATURE_CLOSE(); + this.#wake(); + }); + if (stream.readableEnded) { + this.#ended = true; + this.#onEnd?.(); + } + } + + #wake() { + const waiter = this.#waiter; + if (waiter === null) return; + this.#waiter = null; + waiter.resolve(); + } + + #drain() { + const stream = this.#stream; + let batch = null; + let chunk; + while ((chunk = stream.read()) !== null) { + batch ??= []; + ArrayPrototypePush(batch, toUint8Array(chunk)); + } + return batch; + } + + /** + * @returns {Promise} + */ + read() { + this.#attach(); + const batch = this.#drain(); + if (batch !== null) return PromiseResolve(batch); + if (this.#ended) return PromiseResolve(null); + if (this.#error !== null) return PromiseReject(this.#error); + return this.#readAsync(); + } + + async #readAsync() { + for (;;) { + const waiter = PromiseWithResolvers(); + this.#waiter = waiter; + await waiter.promise; + const batch = this.#drain(); + if (batch !== null) return batch; + if (this.#ended) return null; + if (this.#error !== null) throw this.#error; + } + } +} + +module.exports = { + NodeReadableReader, + toUint8Array, +}; diff --git a/lib/internal/http/fetch_server/request.js b/lib/internal/http/fetch_server/request.js new file mode 100644 index 000000000000..8f4af5a16fa2 --- /dev/null +++ b/lib/internal/http/fetch_server/request.js @@ -0,0 +1,217 @@ +'use strict'; + +// The ServerRequest protocol of the Fetch Server API. +// https://fetch-server.proposal.wintertc.org/#serverrequest + +const { + ObjectDefineProperties, + PromisePrototypeThen, + PromiseReject, + PromiseResolve, + SymbolAsyncIterator, + SymbolToStringTag, +} = primordials; + +const { + codes: { + ERR_INVALID_STATE, + ERR_INVALID_THIS, + }, +} = require('internal/errors'); + +const { + customInspectSymbol: kInspect, + kEnumerableProperty, +} = require('internal/util'); + +const { inspect } = require('internal/util/inspect'); + +const { + createAsyncIterator, + createAsyncIteratorPrototype, +} = require('internal/http/fetch_server/body'); + +const { + createProtocolError, + invalidState, + kContentTooLarge, +} = require('internal/http/fetch_server/errors'); + +const { + createServerHeaders, +} = require('internal/http/fetch_server/headers'); + +const kAsyncIteratorPrototype = createAsyncIteratorPrototype('ServerRequest'); + +const kConstruct = {}; + +let getRequestExchange; +let isServerRequest; + +class ServerRequest { + #exchange; + #method; + #url; + #headers; + #bodyReader = null; + #bodyEnded = false; + #bytesRead = 0; + #started = false; + + static { + getRequestExchange = (request) => request.#exchange; + isServerRequest = (value) => { + return value != null && typeof value === 'object' && #exchange in value; + }; + } + + constructor(key, exchange, method, url, headerList) { + if (key !== kConstruct) { + throw new ERR_INVALID_THIS('ServerRequest cannot be constructed'); + } + this.#exchange = exchange; + this.#method = method; + this.#url = url; + this.#headers = createServerHeaders(headerList, 'immutable'); + } + + get method() { + return checkThis(this).#method; + } + + get url() { + return checkThis(this).#url; + } + + get headers() { + return checkThis(this).#headers; + } + + get signal() { + return checkThis(this).#exchange.signal; + } + + get trailers() { + return checkThis(this).#exchange.trailers; + } + + [SymbolAsyncIterator]() { + checkThis(this); + let iterator; + const next = () => this.#next(iterator); + const returnSteps = () => { + if (iterator === this.#bodyReader && !this.#bodyEnded) { + this.#bodyEnded = true; + this.#exchange.refuseBody(); + } + }; + iterator = createAsyncIterator(kAsyncIteratorPrototype, next, returnSteps); + this.#bodyReader ??= iterator; + return iterator; + } + + #next(iterator) { + const exchange = this.#exchange; + if (iterator !== this.#bodyReader) { + return PromiseReject(new ERR_INVALID_STATE.TypeError( + 'The ServerRequest body has already been read')); + } + if (exchange.kind === 'connect') { + return PromiseReject(invalidState( + 'The request of a CONNECT exchange has no content')); + } + if (exchange.state === 'aborted') { + return PromiseReject(exchange.abortReason); + } + if (this.#bodyEnded) return PromiseResolve(null); + if (!this.#started) { + this.#started = true; + // Beginning to read the body resolves a pending 100 obligation + // (Section 7.4.1). + if (exchange.pending100 && exchange.autoContinue && + exchange.responseCapable) { + exchange.pending100 = false; + exchange.state = 'informational'; + exchange.backend.sendContinue([]); + } + } + if (exchange.bodyRefused) { + // The body is no longer being received, e.g. because the exchange + // became complete or denied first. + this.#bodyEnded = true; + return PromiseResolve(null); + } + const read = PromisePrototypeThen( + exchange.backend.readBody(exchange), + (batch) => { + if (batch === null) { + this.#bodyEnded = true; + return null; + } + const limit = exchange.maxRequestBodySize; + if (limit > 0) { + for (let i = 0; i < batch.length; i++) { + this.#bytesRead += batch[i].byteLength; + } + if (this.#bytesRead > limit) { + this.#bodyEnded = true; + const err = createProtocolError( + kContentTooLarge, 'The request body exceeds the configured limit'); + exchange.failTrailers(err); + exchange.refuseBody(); + throw err; + } + } + return batch; + }, + (err) => { + this.#bodyEnded = true; + throw exchange.state === 'aborted' ? exchange.abortReason : err; + }); + return exchange.raceAbort(read); + } + + [kInspect](depth, options) { + if (depth < 0) return 'ServerRequest {}'; + const opts = { + __proto__: null, + ...options, + depth: options.depth == null ? null : options.depth - 1, + }; + return `ServerRequest ${inspect({ + method: this.#method, + url: this.#url, + headers: this.#headers, + }, opts)}`; + } +} + +function checkThis(self) { + if (!isServerRequest(self)) { + throw new ERR_INVALID_THIS('ServerRequest'); + } + return self; +} + +ObjectDefineProperties(ServerRequest.prototype, { + method: kEnumerableProperty, + url: kEnumerableProperty, + headers: kEnumerableProperty, + signal: kEnumerableProperty, + trailers: kEnumerableProperty, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'ServerRequest', + }, +}); + +function createServerRequest(exchange, method, url, headerList) { + return new ServerRequest(kConstruct, exchange, method, url, headerList); +} + +module.exports = { + createServerRequest, + getRequestExchange, + isServerRequest, +}; diff --git a/lib/internal/http/fetch_server/response.js b/lib/internal/http/fetch_server/response.js new file mode 100644 index 000000000000..c8f1b6560612 --- /dev/null +++ b/lib/internal/http/fetch_server/response.js @@ -0,0 +1,709 @@ +'use strict'; + +// The ServerResponse interface of the Fetch Server API. +// https://fetch-server.proposal.wintertc.org/#serverresponse + +const { + ArrayPrototypeIncludes, + ArrayPrototypePush, + JSONStringify, + ObjectDefineProperties, + PromisePrototypeThen, + PromiseReject, + PromiseResolve, + PromiseWithResolvers, + RegExpPrototypeExec, + SafeSet, + StringPrototypeSplit, + StringPrototypeToLowerCase, + StringPrototypeTrim, + SymbolAsyncIterator, + SymbolIterator, + SymbolToStringTag, +} = primordials; + +const { + codes: { + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_INVALID_STATE, + ERR_INVALID_THIS, + ERR_OUT_OF_RANGE, + }, +} = require('internal/errors'); + +const { + customInspectSymbol: kInspect, + kEnumerableProperty, +} = require('internal/util'); + +const { inspect } = require('internal/util/inspect'); + +const { + isAnyArrayBuffer, + isArrayBufferView, +} = require('internal/util/types'); + +const { + converters, +} = require('internal/webidl'); + +const { + AsyncSequenceSource, + BytesSource, + LazySource, + ReadableStreamSource, + copyBufferSource, + createAsyncIterator, + createAsyncIteratorPrototype, + markHandled, + splitSource, +} = require('internal/http/fetch_server/body'); + +const { + convertHeadersInit, + copyList, + createServerHeaders, + getHeaderList, + listContains, + listDelete, + listGet, + toByteString, +} = require('internal/http/fetch_server/headers'); + +let encoder; +function encodeUtf8(string) { + encoder ??= new (require('internal/encoding').TextEncoder)(); + return encoder.encode(string); +} + +let undici; +function lazyUndici() { + undici ??= require('internal/deps/undici/undici'); + return undici; +} + +let readableStreamHelpers; +function lazyReadableStream() { + readableStreamHelpers ??= require('internal/webstreams/readablestream'); + return readableStreamHelpers; +} + +let isBlobFn; +function isBlob(value) { + isBlobFn ??= require('internal/blob').isBlob; + return isBlobFn(value); +} + +let URLSearchParamsCtor; +function isURLSearchParams(value) { + URLSearchParamsCtor ??= require('internal/url').URLSearchParams; + return value instanceof URLSearchParamsCtor; +} + +function isFormData(value) { + return value instanceof lazyUndici().FormData; +} + +function isReadableStream(value) { + return lazyReadableStream().isReadableStream(value); +} + +const kNullBodyStatuses = [101, 103, 204, 205, 304]; +const kDecodedEncodings = new SafeSet(['gzip', 'x-gzip', 'deflate', 'br', 'zstd']); +const kRedirectStatuses = [301, 302, 303, 307, 308]; +// reason-phrase = 1*( HTAB / SP / VCHAR / obs-text ) +const kReasonPhraseRegExp = /^[\t\x20-\x7e\x80-\xff]+$/; +// The characters of a URI-reference (RFC 3986 Section 4.1). +const kUriReferenceRegExp = + /^(?:[A-Za-z0-9\-._~:/?#[\]@!$&'()*+,;=]|%[0-9A-Fa-f]{2})*$/; + +// ============================================================================= +// Trailer state (Section 9.4) +// ============================================================================= + +class TrailerState { + list = []; + state = 'pending'; + error = undefined; + waiters = []; + + settle() { + const waiters = this.waiters; + this.waiters = []; + for (let i = 0; i < waiters.length; i++) waiters[i](); + } + + // Returns a promise that settles once the state leaves "pending". + wait() { + if (this.state !== 'pending') return PromiseResolve(); + const { promise, resolve } = PromiseWithResolvers(); + ArrayPrototypePush(this.waiters, resolve); + return promise; + } + + complete(list) { + if (this.state !== 'pending') return; + this.list = list; + this.state = 'complete'; + this.settle(); + } + + fail(error) { + if (this.state !== 'pending') return; + this.error = error; + this.state = 'errored'; + this.settle(); + } +} + +function completeTrailerState() { + const state = new TrailerState(); + state.state = 'complete'; + return state; +} + +function getDeclaredTrailers(headerList) { + const value = listGet(headerList, 'trailer'); + if (value === null) return []; + const names = StringPrototypeSplit(value, ','); + const declared = []; + for (let i = 0; i < names.length; i++) { + const name = StringPrototypeToLowerCase(StringPrototypeTrim(names[i])); + if (name !== '') ArrayPrototypePush(declared, name); + } + return declared; +} + +/** + * Processes a trailers init (Section 9.4). + * @param {TrailerState} state + * @param {Array} headerList The response's header list. + * @param {Promise} trailersPromise + */ +function processTrailersInit(state, headerList, trailersPromise) { + PromisePrototypeThen(trailersPromise, (init) => { + let trailers; + try { + trailers = convertHeadersInit(init, 'trailers'); + } catch (err) { + state.fail(err); + return; + } + const declared = getDeclaredTrailers(headerList); + const filtered = []; + for (let i = 0; i < trailers.length; i++) { + if (ArrayPrototypeIncludes(declared, + StringPrototypeToLowerCase(trailers[i][0]))) { + ArrayPrototypePush(filtered, trailers[i]); + } + } + state.complete(filtered); + }, (reason) => { + state.fail(reason); + }); +} + +// ============================================================================= +// Extracting a body (Section 9.3) +// ============================================================================= + +function isAsyncSequence(value) { + if (value === null || + (typeof value !== 'object' && typeof value !== 'function')) { + return false; + } + return typeof value[SymbolAsyncIterator] === 'function' || + typeof value[SymbolIterator] === 'function'; +} + +/** + * Extracts a response body from a ServerBodyInit (Section 9.3). + * @param {any} object + * @returns {{ source: object, type: string|null }} + */ +function extractBody(object) { + if (isReadableStream(object)) { + const { isReadableStreamLocked } = lazyReadableStream(); + if (isReadableStreamLocked(object) || + require('internal/streams/utils').isDisturbed(object)) { + throw new ERR_INVALID_STATE.TypeError( + 'The ReadableStream is locked or disturbed'); + } + return { source: new ReadableStreamSource(object), type: null }; + } + if (isBlob(object)) { + const type = object.type; + return { + source: new LazySource(async () => { + const buffer = await object.arrayBuffer(); + return new BytesSource(copyBufferSource(buffer)); + }), + type: type === '' ? null : type, + }; + } + if (isAnyArrayBuffer(object) || isArrayBufferView(object)) { + return { source: new BytesSource(copyBufferSource(object)), type: null }; + } + if (isFormData(object)) { + const response = new (lazyUndici().Response)(object); + return { + source: new ReadableStreamSource(response.body), + type: response.headers.get('content-type'), + }; + } + if (isURLSearchParams(object)) { + return { + source: new BytesSource(encodeUtf8(`${object}`)), + type: 'application/x-www-form-urlencoded;charset=UTF-8', + }; + } + if (typeof object !== 'string' && isAsyncSequence(object)) { + return { source: new AsyncSequenceSource(object), type: null }; + } + return { + source: new BytesSource(encodeUtf8(converters.USVString(object))), + type: 'text/plain;charset=UTF-8', + }; +} + +// ============================================================================= +// ServerResponse +// ============================================================================= + +const kAsyncIteratorPrototype = createAsyncIteratorPrototype('ServerResponse'); + +let getResponseState; +let isServerResponse; + +const kCreate = {}; + +function convertInit(init) { + if (init === undefined || init === null) { + return { __proto__: null, status: 200, statusText: '' }; + } + if (typeof init !== 'object' && typeof init !== 'function') { + throw new ERR_INVALID_ARG_TYPE('init', 'Object', init); + } + // Web IDL dictionaries are converted in lexicographic order of members. + const result = { __proto__: null }; + const headers = init.headers; + if (headers !== undefined) result.headers = convertHeadersInit(headers); + const status = init.status; + result.status = status === undefined ? 200 : + converters['unsigned short'](status, { + __proto__: null, + enforceRange: true, + prefix: 'Failed to construct \'ServerResponse\'', + context: 'status', + }); + const statusText = init.statusText; + result.statusText = statusText === undefined ? '' : + toByteString(statusText, 'statusText'); + const trailers = init.trailers; + if (trailers !== undefined) result.trailers = PromiseResolve(trailers); + return result; +} + +/** + * Initializes a server response (Section 9.1). + * @param {object} state + * @param {object} init A converted ServerResponseInit. + * @param {{ source: object, type: string|null }|null} bodyWithType + */ +function initializeResponse(state, init, bodyWithType) { + if (init.status < 200 || init.status > 599) { + throw new ERR_OUT_OF_RANGE('init.status', '>= 200 and <= 599', init.status); + } + if (init.statusText !== '' && + RegExpPrototypeExec(kReasonPhraseRegExp, init.statusText) === null) { + throw new ERR_INVALID_ARG_VALUE('init.statusText', init.statusText, + 'must match the reason-phrase production'); + } + state.status = init.status; + state.statusText = init.statusText; + const list = getHeaderList(state.headers); + if (init.headers !== undefined) { + for (let i = 0; i < init.headers.length; i++) { + ArrayPrototypePush(list, init.headers[i]); + } + } + if (bodyWithType !== null) { + if (ArrayPrototypeIncludes(kNullBodyStatuses, state.status)) { + throw new ERR_INVALID_ARG_VALUE('init.status', state.status, + 'cannot have a body'); + } + state.body = bodyWithType.source; + if (bodyWithType.type !== null && !listContains(list, 'content-type')) { + ArrayPrototypePush(list, ['Content-Type', bodyWithType.type]); + } + } + if (init.trailers !== undefined) { + processTrailersInit(state.trailerState, list, init.trailers); + } else { + state.trailerState = completeTrailerState(); + } +} + +class ServerResponse { + #state; + + static { + getResponseState = (response) => response.#state; + isServerResponse = (value) => { + return value != null && typeof value === 'object' && #state in value; + }; + } + + constructor(body = null, init = undefined) { + if (body === kCreate) { + // Create a server response (Section 9). + this.#state = createState(); + return; + } + // Web IDL converts the arguments before running the constructor steps. + const convertedInit = convertInit(init); + this.#state = createState(); + let bodyWithType = null; + if (body !== null && body !== undefined) { + bodyWithType = extractBody(body); + } + initializeResponse(this.#state, convertedInit, bodyWithType); + } + + static error() { + const response = new ServerResponse(kCreate); + const state = response.#state; + state.type = 'error'; + state.status = 0; + state.trailerState = completeTrailerState(); + return response; + } + + static redirect(url, status = 302) { + url = converters.USVString(url, { + __proto__: null, + prefix: 'Failed to execute \'redirect\' on \'ServerResponse\'', + context: 'url', + }); + status = converters['unsigned short'](status, { + __proto__: null, + enforceRange: true, + prefix: 'Failed to execute \'redirect\' on \'ServerResponse\'', + context: 'status', + }); + if (RegExpPrototypeExec(kUriReferenceRegExp, url) === null) { + throw new ERR_INVALID_ARG_VALUE('url', url, + 'must be a valid URI-reference'); + } + if (!ArrayPrototypeIncludes(kRedirectStatuses, status)) { + throw new ERR_OUT_OF_RANGE('status', 'a redirect status', status); + } + const response = new ServerResponse(kCreate); + const state = response.#state; + state.status = status; + ArrayPrototypePush(getHeaderList(state.headers), ['Location', url]); + state.trailerState = completeTrailerState(); + return response; + } + + static json(data, init = undefined) { + const convertedInit = convertInit(init); + const json = JSONStringify(data); + if (json === undefined) { + throw new ERR_INVALID_ARG_VALUE('data', data, + 'cannot be serialized to JSON'); + } + const response = new ServerResponse(kCreate); + initializeResponse(response.#state, convertedInit, { + source: new BytesSource(encodeUtf8(json)), + type: 'application/json', + }); + return response; + } + + static from(response) { + const { Response } = lazyUndici(); + if (!(response instanceof Response)) { + throw new ERR_INVALID_ARG_TYPE('response', 'Response', response); + } + const type = response.type; + if (type === 'opaque' || type === 'opaqueredirect') { + throw new ERR_INVALID_ARG_VALUE('response', response, + 'must not be an opaque response'); + } + if (response.bodyUsed || response.body?.locked) { + throw new ERR_INVALID_STATE.TypeError('Response body is unusable'); + } + const result = new ServerResponse(kCreate); + const state = result.#state; + if (type === 'error') { + state.type = 'error'; + state.status = 0; + state.trailerState = completeTrailerState(); + return result; + } + state.status = response.status; + state.statusText = response.statusText; + const list = getHeaderList(state.headers); + for (const { 0: name, 1: value } of response.headers) { + ArrayPrototypePush(list, [name, value]); + } + // Bodies obtained from fetch() have been decoded according to their + // Content-Encoding. + if (response.url !== '' && listContains(list, 'content-encoding')) { + const encoding = StringPrototypeToLowerCase( + listGet(list, 'content-encoding')); + if (kDecodedEncodings.has(encoding)) { + listDelete(list, 'content-encoding'); + listDelete(list, 'content-length'); + } + } + if (response.body !== null) { + state.body = new ReadableStreamSource(response.body); + } + const trailers = response.trailers; + if (trailers !== undefined && trailers !== null) { + processTrailersInit(state.trailerState, list, PromiseResolve(trailers)); + } else { + state.trailerState = completeTrailerState(); + } + return result; + } + + get type() { + return checkThis(this).type; + } + + get url() { + checkThis(this); + return ''; + } + + get redirected() { + checkThis(this); + return false; + } + + get status() { + return checkThis(this).status; + } + + get ok() { + const { status } = checkThis(this); + return status >= 200 && status <= 299; + } + + get statusText() { + return checkThis(this).statusText; + } + + get headers() { + return checkThis(this).headers; + } + + get trailers() { + const state = checkThis(this); + if (state.trailersPromise === null) { + const { promise, resolve, reject } = PromiseWithResolvers(); + state.trailersPromise = promise; + markHandled(promise); + const trailerState = state.trailerState; + PromisePrototypeThen(trailerState.wait(), () => { + if (trailerState.state === 'complete') { + resolve(createServerHeaders(copyList(trailerState.list), 'immutable')); + } else { + reject(trailerState.error); + } + }); + } + return state.trailersPromise; + } + + clone() { + const state = checkThis(this); + if (isUnusable(state)) { + throw new ERR_INVALID_STATE.TypeError('ServerResponse body is unusable'); + } + const clone = new ServerResponse(kCreate); + const cloneState = clone.#state; + cloneState.type = state.type; + cloneState.status = state.status; + cloneState.statusText = state.statusText; + const cloneList = getHeaderList(cloneState.headers); + const list = getHeaderList(state.headers); + for (let i = 0; i < list.length; i++) { + ArrayPrototypePush(cloneList, [list[i][0], list[i][1]]); + } + cloneState.trailerState = state.trailerState; + if (state.body !== null) { + const { 0: first, 1: second } = splitSource(state.body); + state.body = first; + cloneState.body = second; + } + return clone; + } + + values() { + return createBodyIterator(checkThis(this)); + } + + [kInspect](depth, options) { + if (depth < 0) return 'ServerResponse {}'; + const state = this.#state; + const opts = { + __proto__: null, + ...options, + depth: options.depth == null ? null : options.depth - 1, + }; + return `ServerResponse ${inspect({ + type: state.type, + status: state.status, + statusText: state.statusText, + headers: state.headers, + bodyUsed: state.bodyReader !== null, + }, opts)}`; + } +} + +function createState() { + return { + __proto__: null, + type: 'default', + status: 200, + statusText: '', + headers: createServerHeaders(), + body: null, + bodyReader: null, + bodyEnded: false, + trailerState: new TrailerState(), + trailersPromise: null, + }; +} + +function checkThis(self) { + if (!isServerResponse(self)) { + throw new ERR_INVALID_THIS('ServerResponse'); + } + return getResponseState(self); +} + +function isUnusable(state) { + return state.body !== null && state.bodyReader !== null; +} + +/** + * Acquires an iterator over the body of a ServerResponse (Section 9.3). The first + * iterator obtained becomes the body reader; any later one rejects on next(). + * @param {object} state + * @returns {object} + */ +function createBodyIterator(state) { + let iterator; + const next = () => { + if (iterator !== state.bodyReader) { + return PromiseReject(new ERR_INVALID_STATE.TypeError( + 'The ServerResponse body has already been read')); + } + if (state.body === null) return PromiseResolve(null); + return PromisePrototypeThen(state.body.read(), (batch) => { + if (batch === null) state.bodyEnded = true; + return batch; + }, (err) => { + state.bodyEnded = true; + throw err; + }); + }; + const returnSteps = (reason) => { + if (iterator === state.bodyReader && state.body !== null && + !state.bodyEnded) { + state.bodyEnded = true; + state.body.close(reason); + } + }; + iterator = createAsyncIterator(kAsyncIteratorPrototype, next, returnSteps); + state.bodyReader ??= iterator; + return iterator; +} + +/** + * Internal reader used by the implementation to send a response body. + * @param {ServerResponse} response + * @returns {object} An object with read() and close(reason) methods. + */ +function acquireBodyReader(response) { + const state = getResponseState(response); + const reader = {}; + state.bodyReader = reader; + return { + __proto__: null, + read() { + if (state.body === null || state.bodyEnded) return PromiseResolve(null); + return PromisePrototypeThen(state.body.read(), (batch) => { + if (batch === null) state.bodyEnded = true; + return batch; + }, (err) => { + state.bodyEnded = true; + throw err; + }); + }, + close(reason) { + if (state.body === null || state.bodyEnded) return; + state.bodyEnded = true; + state.body.close(reason); + }, + // Returns the whole body if it is held as bytes, and undefined otherwise. + takeBytes() { + const body = state.body; + if (body === null || state.bodyEnded || !(body instanceof BytesSource)) { + return undefined; + } + state.bodyEnded = true; + return body.take(); + }, + }; +} + +function isResponseUnusable(response) { + return isUnusable(getResponseState(response)); +} + +ObjectDefineProperties(ServerResponse.prototype, { + type: kEnumerableProperty, + url: kEnumerableProperty, + redirected: kEnumerableProperty, + status: kEnumerableProperty, + ok: kEnumerableProperty, + statusText: kEnumerableProperty, + headers: kEnumerableProperty, + trailers: kEnumerableProperty, + clone: kEnumerableProperty, + values: kEnumerableProperty, + [SymbolAsyncIterator]: { + __proto__: null, + configurable: true, + writable: true, + value: ServerResponse.prototype.values, + }, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'ServerResponse', + }, +}); + +ObjectDefineProperties(ServerResponse, { + error: kEnumerableProperty, + redirect: kEnumerableProperty, + json: kEnumerableProperty, + from: kEnumerableProperty, +}); + +module.exports = { + ServerResponse, + acquireBodyReader, + getDeclaredTrailers, + getResponseState, + isResponseUnusable, + isServerResponse, +}; diff --git a/lib/internal/http/fetch_server/server.js b/lib/internal/http/fetch_server/server.js new file mode 100644 index 000000000000..483e2e2a52ab --- /dev/null +++ b/lib/internal/http/fetch_server/server.js @@ -0,0 +1,1153 @@ +'use strict'; + +// The infrastructure layer of the Fetch Server API: serve(), Server, and +// Listener (Section 17, Section 18). +// https://fetch-server.proposal.wintertc.org/#server-configuration + +const { + ArrayPrototypeIndexOf, + ArrayPrototypePush, + ArrayPrototypeSplice, + FunctionPrototypeCall, + ObjectDefineProperties, + ObjectIs, + PromisePrototypeThen, + PromiseReject, + PromiseResolve, + PromiseWithResolvers, + RegExpPrototypeExec, + SafeArrayIterator, + SafeSet, + StringPrototypeIndexOf, + StringPrototypeSlice, + StringPrototypeStartsWith, + StringPrototypeToLowerCase, + SymbolAsyncDispose, + SymbolFor, + SymbolIterator, + SymbolToStringTag, + TypedArrayPrototypeGetByteLength, +} = primordials; + +const { + codes: { + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_INVALID_THIS, + }, +} = require('internal/errors'); + +const { + customInspectSymbol: kInspect, + kEnumerableProperty, + lazyDOMException, +} = require('internal/util'); + +const { inspect } = require('internal/util/inspect'); + +const { + converters, +} = require('internal/webidl'); + +const { + invalidState, + notSupported, +} = require('internal/http/fetch_server/errors'); + +const { + markHandled, + noop, +} = require('internal/http/fetch_server/body'); + +const { Exchange, report } = require('internal/http/fetch_server/exchange'); +const { dispatch } = require('internal/http/fetch_server/dispatch'); +const { createServerRequest } = require('internal/http/fetch_server/request'); +const { H1Backend } = require('internal/http/fetch_server/h1'); +const { H2Backend } = require('internal/http/fetch_server/h2'); + +const kProtocolMarker = SymbolFor('wintertc.server.protocol'); +const kH2Preface = 'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'; +const kUpgradeConnection = /(?:^|,)\s*upgrade\s*(?:,|$)/i; + +// ============================================================================= +// Option conversion +// ============================================================================= + +/** + * Converts a handler object (Section 16.5). + * @param {any} handler + * @returns {{ fetch?: Function, connect?: Function, error?: Function, + * object: object }} + */ +function convertHandler(handler) { + if (handler === null || + (typeof handler !== 'object' && typeof handler !== 'function')) { + throw new ERR_INVALID_ARG_TYPE('handler', 'Object', handler); + } + const marker = handler[kProtocolMarker]; + if (marker !== undefined && !ObjectIs(marker, 1)) { + throw new ERR_INVALID_ARG_VALUE( + 'handler[Symbol.for(\'wintertc.server.protocol\')]', marker, + 'is not a supported protocol version'); + } + const dict = { __proto__: null, object: handler }; + for (const name of new SafeArrayIterator(['connect', 'error', 'fetch'])) { + const value = handler[name]; + if (value === undefined) continue; + if (typeof value !== 'function') { + throw new ERR_INVALID_ARG_TYPE(`handler.${name}`, 'Function', value); + } + dict[name] = value; + } + if (dict.fetch === undefined && dict.connect === undefined) { + throw new ERR_INVALID_ARG_VALUE('handler', handler, + 'must have a fetch or connect method'); + } + return dict; +} + +function convertInteger(value, name, type = 'unsigned long long') { + if (value === undefined) return undefined; + return converters[type === 'unsigned long long' ? 'long long' : type]( + value, { __proto__: null, enforceRange: true, context: name }); +} + +function convertNonNegative(value, name) { + const n = convertInteger(value, name); + if (n !== undefined && n < 0) { + throw new ERR_INVALID_ARG_VALUE(name, value, 'must be non-negative'); + } + return n; +} + +function convertDictionary(value, name) { + if (value === undefined || value === null) return undefined; + if (typeof value !== 'object' && typeof value !== 'function') { + throw new ERR_INVALID_ARG_TYPE(name, 'Object', value); + } + return value; +} + +const kTimeoutMembers = [ + 'closeHandshake', 'handshake', 'headers', 'idle', 'receive', 'send', + 'tunnelIdle', 'waitUntil', +]; +const kLimitMembers = [ + 'maxConcurrentStreams', 'maxConnections', 'maxHeaderSize', + 'maxRequestBodySize', 'maxRequestsPerConnection', +]; + +function convertMembers(value, name, members) { + const dict = convertDictionary(value, name); + if (dict === undefined) return undefined; + const result = { __proto__: null }; + for (let i = 0; i < members.length; i++) { + const v = convertNonNegative(dict[members[i]], `${name}.${members[i]}`); + if (v !== undefined) result[members[i]] = v; + } + return result; +} + +function convertTLS(value) { + const dict = convertDictionary(value, 'tls'); + if (dict === undefined) return undefined; + const result = { __proto__: null }; + const alpn = dict.alpn; + if (alpn !== undefined) { + result.alpn = converters['sequence'](alpn, { + __proto__: null, context: 'tls.alpn', + }); + } + if (dict.cert !== undefined) result.cert = dict.cert; + if (dict.key !== undefined) result.key = dict.key; + const sni = dict.sni; + if (sni !== undefined) { + if (typeof sni !== 'function' && + (sni === null || typeof sni !== 'object')) { + throw new ERR_INVALID_ARG_TYPE('tls.sni', ['Function', 'Object'], sni); + } + result.sni = sni; + } + return result; +} + +function convertBooleanOrUndefined(value) { + return value === undefined ? undefined : !!value; +} + +/** + * Converts the members shared by ServerOptions and ListenOptions. + * @param {object} options + * @returns {object} + */ +function convertListenMembers(options) { + const result = { __proto__: null }; + const altSvc = options.altSvc; + if (altSvc !== undefined) { + result.altSvc = typeof altSvc === 'boolean' ? altSvc : + converters.DOMString(altSvc, { __proto__: null, context: 'altSvc' }); + } + result.backlog = convertInteger(options.backlog, 'backlog', 'unsigned long'); + result.datagrams = convertBooleanOrUndefined(options.datagrams); + result.extendedConnect = convertBooleanOrUndefined(options.extendedConnect); + if (options.hostname !== undefined) { + result.hostname = converters.DOMString(options.hostname, { + __proto__: null, context: 'hostname', + }); + } + result.ipv6Only = convertBooleanOrUndefined(options.ipv6Only); + result.limits = convertMembers(options.limits, 'limits', kLimitMembers); + result.port = convertInteger(options.port, 'port', 'unsigned short'); + const quic = options.quic; + if (quic !== undefined) { + result.quic = typeof quic === 'boolean' ? quic : + (convertDictionary(quic, 'quic') !== undefined); + } + result.reusePort = convertBooleanOrUndefined(options.reusePort); + result.timeouts = convertMembers(options.timeouts, 'timeouts', + kTimeoutMembers); + result.tls = convertTLS(options.tls); + result.webTransport = convertBooleanOrUndefined(options.webTransport); + return result; +} + +function convertServerOptions(options) { + options ??= {}; + if (typeof options !== 'object' && typeof options !== 'function') { + throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); + } + const result = convertListenMembers(options); + result.autoContinue = options.autoContinue === undefined ? true : + !!options.autoContinue; + const signal = options.signal; + if (signal !== undefined) { + const { validateAbortSignal } = require('internal/validators'); + validateAbortSignal(signal, 'options.signal'); + result.signal = signal; + } + return result; +} + +/** + * Determines the configuration of a binding member by member (Section 17.2). + * @returns {object} + */ +function resolveListenOptions(serverOptions, listenOptions) { + const result = { __proto__: null }; + const keys = ['altSvc', 'backlog', 'datagrams', 'extendedConnect', 'hostname', + 'ipv6Only', 'port', 'quic', 'reusePort', 'tls', 'webTransport']; + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + result[key] = listenOptions[key] !== undefined ? listenOptions[key] : + serverOptions[key]; + } + result.timeouts = { + __proto__: null, ...serverOptions.timeouts, ...listenOptions.timeouts, + }; + result.limits = { + __proto__: null, ...serverOptions.limits, ...listenOptions.limits, + }; + result.port ??= 0; + result.reusePort ??= false; + result.ipv6Only ??= false; + result.quic ??= false; + return result; +} + +// ============================================================================= +// Closeable (Section 18.2) +// ============================================================================= + +class Closeable { + #closed = PromiseWithResolvers(); + #closing = false; + #destroyed = false; + #done = false; + + constructor() { + markHandled(this.#closed.promise); + } + + get closedPromise() { + return this.#closed.promise; + } + + get closing() { + return this.#closing; + } + + get destroyed() { + return this.#destroyed; + } + + get done() { + return this.#done; + } + + startClose() { + if (this.#closing || this.#destroyed) return false; + this.#closing = true; + return true; + } + + startDestroy() { + if (this.#destroyed || this.#done) return false; + this.#destroyed = true; + return true; + } + + resolveClosed() { + if (this.#done) return; + this.#done = true; + this.#closed.resolve(); + } + + rejectClosed(error) { + if (this.#done) return; + this.#done = true; + this.#closed.reject(error); + } +} + +function abortError() { + return lazyDOMException('The server was destroyed', 'AbortError'); +} + +// ============================================================================= +// Listener (Section 5.16) +// ============================================================================= + +let getListenerInternals; +let isListener; + +class ListenerInternals extends Closeable { + server; + options; + netServer = null; + http1 = null; + http2 = null; + busy = false; + sockets = new SafeSet(); + sessions = new SafeSet(); + exchanges = new SafeSet(); + altSvc; + maxRequestBodySize; + waitUntilTimeout; + + constructor(server, options) { + super(); + this.server = server; + this.options = options; + this.busy = server.busy; + this.altSvc = typeof options.altSvc === 'string' ? options.altSvc : undefined; + this.maxRequestBodySize = options.limits.maxRequestBodySize ?? 0; + this.waitUntilTimeout = options.timeouts.waitUntil ?? 30_000; + } + + get draining() { + return this.closing; + } + + get destroying() { + return this.destroyed; + } +} + +class Listener { + #internals; + + static { + getListenerInternals = (listener) => listener.#internals; + isListener = (value) => { + return value != null && typeof value === 'object' && #internals in value; + }; + } + + constructor(key, internals) { + if (key !== kConstruct) { + throw new ERR_INVALID_THIS('Listener cannot be constructed'); + } + this.#internals = internals; + } + + get address() { + const internals = checkListener(this); + const address = internals.netServer?.address(); + if (address === null || address === undefined) { + return { address: '', port: 0 }; + } + if (typeof address === 'string') { + return { address: '', port: 0, family: 'unix', path: address }; + } + return { + address: address.address, + port: address.port, + family: address.family === 'IPv6' ? 'IPv6' : 'IPv4', + }; + } + + get busy() { + return checkListener(this).busy; + } + + set busy(value) { + checkListener(this).busy = !!value; + } + + close() { + const internals = checkListener(this); + closeListener(internals); + return internals.closedPromise; + } + + destroy(error = undefined) { + destroyListener(checkListener(this), error); + } + + get closed() { + return checkListener(this).closedPromise; + } + + [SymbolAsyncDispose]() { + return this.close(); + } + + [kInspect](depth, options) { + if (depth < 0) return 'Listener {}'; + return `Listener ${inspect({ address: this.address, busy: this.busy }, + options)}`; + } +} + +function checkListener(self) { + if (!isListener(self)) throw new ERR_INVALID_THIS('Listener'); + return getListenerInternals(self); +} + +ObjectDefineProperties(Listener.prototype, { + address: kEnumerableProperty, + busy: kEnumerableProperty, + close: kEnumerableProperty, + destroy: kEnumerableProperty, + closed: kEnumerableProperty, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'Listener', + }, +}); + +const kConstruct = {}; + +function maybeFinishClose(internals) { + if (!internals.closing || internals.done) return; + if (internals.netServer?.listening) return; + if (internals.sockets.size > 0 || internals.exchanges.size > 0) return; + internals.http1?.close(); + internals.resolveClosed(); + internals.server.listenerClosed(internals); +} + +function closeListener(internals) { + if (!internals.startClose()) return; + const { netServer } = internals; + if (netServer?.listening) { + netServer.close(() => maybeFinishClose(internals)); + } + for (const session of internals.sessions) { + try { + session.close(); + } catch { + // Ignore sessions that are already closing. + } + } + internals.http1?.closeIdleConnections(); + for (const exchange of internals.exchanges) { + exchange.tunnel?.windDown?.(); + } + maybeFinishClose(internals); +} + +function destroyListener(internals, error) { + if (!internals.startDestroy()) return; + internals.netServer?.close(); + for (const exchange of internals.exchanges) { + exchange.forceEnd(); + } + for (const session of internals.sessions) { + session.destroy(); + } + for (const socket of internals.sockets) { + socket.destroy(); + } + internals.http1?.close(); + internals.rejectClosed(error === undefined ? abortError() : error); + internals.server.listenerClosed(internals); +} + +// ============================================================================= +// Exchange creation +// ============================================================================= + +function createExchange(internals, kind, backend) { + const server = internals.server; + const exchange = new Exchange(kind, backend, internals, { + autoContinue: server.options.autoContinue, + maxRequestBodySize: internals.maxRequestBodySize, + waitUntilTimeout: internals.waitUntilTimeout, + requestFactory: (ex) => createServerRequest( + ex, backend.method, backend.url, backend.headerList), + onEnded: (ex) => { + internals.exchanges.delete(ex); + maybeFinishClose(internals); + }, + }); + internals.exchanges.add(exchange); + backend.attach(exchange); + return exchange; +} + +function refusedByBusy(internals) { + return internals.busy || internals.closing; +} + +function writeSimpleResponse(res, status, extraHeaders = []) { + res.shouldKeepAlive = false; + res.writeHead(status, extraHeaders); + res.end(); +} + +function onHttp1Request(internals, isContinue, req, res) { + if (internals.destroyed) { + req.socket.destroy(); + return; + } + if (refusedByBusy(internals)) { + res.writeHead(503, internals.closing ? ['Connection', 'close'] : []); + res.end(); + return; + } + if (req.url === '*') { + res.writeHead(204, ['Allow', 'GET, HEAD, POST, PUT, DELETE, OPTIONS']); + res.end(); + return; + } + const limit = internals.maxRequestBodySize; + if (limit > 0 && +(req.headers['content-length'] ?? 0) > limit) { + writeSimpleResponse(res, 413); + return; + } + let backend; + try { + backend = new H1Backend({ + req, res, socket: req.socket, listener: internals, kind: 'fetch', + }); + } catch { + writeSimpleResponse(res, 400); + return; + } + const exchange = createExchange(internals, 'fetch', backend); + exchange.pending100 = isContinue; + dispatch(internals.server.handlers, exchange); +} + +function createLegacyResponse(internals, req, socket) { + const { ServerResponse } = require('internal/http/server'); + const res = new ServerResponse(req); + res.shouldKeepAlive = false; + res.assignSocket(socket); + res.on('finish', () => { + res.detachSocket(socket); + socket.end(); + }); + return res; +} + +function onHttp1Tunnel(internals, kind, req, stream, head) { + const socket = req.socket; + internals.sockets.add(socket); + socket.on('error', noop); + if (internals.destroyed) { + socket.destroy(); + return; + } + if (refusedByBusy(internals)) { + socket.end('HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n' + + 'Content-Length: 0\r\n\r\n'); + return; + } + const handlers = internals.server.handlers; + if (kind === 'connect' && handlers.connect === undefined) { + socket.end('HTTP/1.1 501 Not Implemented\r\nConnection: close\r\n' + + 'Content-Length: 0\r\n\r\n'); + return; + } + const res = createLegacyResponse(internals, req, socket); + let backend; + try { + backend = new H1Backend({ + req, res, socket, listener: internals, kind, tunnelStream: stream, head, + }); + } catch { + writeSimpleResponse(res, 400); + return; + } + const exchange = createExchange(internals, kind, backend); + const expect = req.headers.expect; + exchange.pending100 = typeof expect === 'string' && + StringPrototypeToLowerCase(expect) === '100-continue'; + dispatch(handlers, exchange); +} + +function onHttp2Stream(internals, stream, headers, flags, rawHeaders) { + if (internals.destroyed) { + stream.close(0x8); + return; + } + stream.on('error', noop); + if (refusedByBusy(internals)) { + stream.close(0x7); // REFUSED_STREAM + return; + } + const handlers = internals.server.handlers; + const method = headers[':method']; + if (method === 'CONNECT' && handlers.connect === undefined) { + stream.respond({ ':status': 501 }, { endStream: true }); + return; + } + if (method !== 'CONNECT' && headers[':path'] === '*') { + stream.respond({ ':status': 204 }, { endStream: true }); + return; + } + const limit = internals.maxRequestBodySize; + if (limit > 0 && +(headers['content-length'] ?? 0) > limit) { + stream.respond({ ':status': 413 }, { endStream: true }); + return; + } + let backend; + try { + backend = new H2Backend({ stream, headers, flags, rawHeaders, + listener: internals }); + } catch { + stream.respond({ ':status': 400 }, { endStream: true }); + return; + } + const exchange = createExchange(internals, backend.kind, backend); + exchange.pending100 = headers.expect !== undefined && + StringPrototypeToLowerCase(`${headers.expect}`) === '100-continue'; + dispatch(handlers, exchange); +} + +// ============================================================================= +// Binding +// ============================================================================= + +function createHttp1Server(internals) { + const http = require('http'); + const { options } = internals; + const hasConnect = internals.server.handlers.connect !== undefined; + const server = new http.Server({ + maxHeaderSize: options.limits.maxHeaderSize || undefined, + requireHostHeader: true, + shouldUpgradeCallback(req) { + if (!hasConnect || req.httpVersion === '1.0') return false; + const connection = req.headers.connection; + return typeof connection === 'string' && + RegExpPrototypeExec(kUpgradeConnection, connection) !== null; + }, + }); + const timeouts = options.timeouts; + if (timeouts.headers !== undefined) server.headersTimeout = timeouts.headers; + if (timeouts.idle !== undefined) server.keepAliveTimeout = timeouts.idle; + server.requestTimeout = 0; + if (options.limits.maxRequestsPerConnection !== undefined) { + server.maxRequestsPerSocket = options.limits.maxRequestsPerConnection; + } + server.on('request', (req, res) => onHttp1Request(internals, false, req, res)); + server.on('checkContinue', + (req, res) => onHttp1Request(internals, true, req, res)); + server.on('checkExpectation', (req, res) => { + res.writeHead(417); + res.end(); + }); + server.on('connect', (req, socket, head) => + onHttp1Tunnel(internals, 'connect', req, socket, head)); + server.on('upgrade', (req, socket, head) => + onHttp1Tunnel(internals, 'upgrade', req, socket, head)); + server.on('clientError', (err, socket) => { + if (socket.writable && !socket.destroyed) { + socket.end('HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'); + } + socket.destroy(); + }); + // Starts the tracking of headers and request timeouts. + server.emit('listening'); + return server; +} + +function createHttp2Server(internals) { + const http2 = require('http2'); + const { options } = internals; + const hasConnect = internals.server.handlers.connect !== undefined; + const settings = { + enableConnectProtocol: options.extendedConnect ?? hasConnect, + }; + if (options.limits.maxConcurrentStreams !== undefined) { + settings.maxConcurrentStreams = options.limits.maxConcurrentStreams; + } + if (options.limits.maxHeaderSize !== undefined) { + settings.maxHeaderListSize = options.limits.maxHeaderSize; + } + const server = http2.createServer({ settings }); + server.on('session', (session) => { + internals.sessions.add(session); + session.on('error', noop); + session.once('close', () => { + internals.sessions.delete(session); + maybeFinishClose(internals); + }); + if (internals.closing) session.close(); + const maxRequests = options.limits.maxRequestsPerConnection; + if (maxRequests > 0) { + let count = 0; + session.on('stream', () => { + if (++count === maxRequests) session.close(); + }); + } + }); + server.on('stream', (stream, headers, flags, rawHeaders) => + onHttp2Stream(internals, stream, headers, flags, rawHeaders)); + return server; +} + +function trackSocket(internals, socket) { + internals.sockets.add(socket); + socket.once('close', () => { + internals.sockets.delete(socket); + maybeFinishClose(internals); + }); +} + +// Detects the HTTP/2 connection preface on a cleartext connection, and hands +// the connection to the HTTP/1.1 or the HTTP/2 server. +function sniffCleartext(internals, socket) { + let buffered = null; + const onData = (chunk) => { + buffered = buffered === null ? chunk : + require('buffer').Buffer.concat([buffered, chunk]); + const prefix = buffered.latin1Slice(0, kH2Preface.length); + const isPreface = prefix === kH2Preface; + if (!isPreface && StringPrototypeStartsWith(kH2Preface, prefix) && + TypedArrayPrototypeGetByteLength(buffered) < kH2Preface.length) { + return; // Wait for more data. + } + socket.removeListener('data', onData); + socket.pause(); + if (isPreface) { + socket.unshift(buffered); + internals.http2 ??= createHttp2Server(internals); + internals.http2.emit('connection', socket); + } else { + internals.http1 ??= createHttp1Server(internals); + internals.http1.emit('connection', socket); + socket.emit('data', buffered); + socket.resume(); + } + }; + socket.on('data', onData); +} + +function onConnection(internals, socket) { + trackSocket(internals, socket); + socket.on('error', noop); + if (internals.destroyed) { + socket.destroy(); + return; + } + if (internals.options.tls === undefined) { + sniffCleartext(internals, socket); + return; + } + if (socket.alpnProtocol === 'h2') { + internals.http2 ??= createHttp2Server(internals); + internals.http2.emit('connection', socket); + } else { + internals.http1 ??= createHttp1Server(internals); + internals.http1.emit('connection', socket); + } +} + +function toCertificateOptions(certificate, name) { + if (certificate === null || typeof certificate !== 'object') { + throw new ERR_INVALID_ARG_TYPE(name, 'Object', certificate); + } + const { cert, key } = certificate; + return { cert, key }; +} + +function matchSni(map, hostname) { + const keys = []; + for (const key in map) ArrayPrototypePush(keys, key); + const lower = StringPrototypeToLowerCase(hostname); + for (let i = 0; i < keys.length; i++) { + if (StringPrototypeToLowerCase(keys[i]) === lower) return map[keys[i]]; + } + const dot = StringPrototypeIndexOf(lower, '.'); + if (dot > 0) { + const wildcard = `*${StringPrototypeSlice(lower, dot)}`; + for (let i = 0; i < keys.length; i++) { + if (StringPrototypeToLowerCase(keys[i]) === wildcard) return map[keys[i]]; + } + } + return null; +} + +function createTLSOptions(internals, tlsOptions) { + const tls = require('tls'); + const alpn = tlsOptions.alpn ?? ['h2', 'http/1.1']; + for (let i = 0; i < alpn.length; i++) { + if (alpn[i] !== 'h2' && alpn[i] !== 'http/1.1') { + throw new ERR_INVALID_ARG_VALUE('tls.alpn', alpn, + 'must only contain "h2" and "http/1.1"'); + } + } + const result = { + ALPNProtocols: alpn, + }; + if (tlsOptions.cert !== undefined) result.cert = tlsOptions.cert; + if (tlsOptions.key !== undefined) result.key = tlsOptions.key; + const handshake = internals.options.timeouts.handshake; + if (handshake !== undefined) result.handshakeTimeout = handshake; + const sni = tlsOptions.sni; + if (sni !== undefined) { + const contexts = { __proto__: null }; + const getContext = (certificate, name) => { + if (certificate === null || certificate === undefined) return undefined; + return tls.createSecureContext(toCertificateOptions(certificate, name)); + }; + result.SNICallback = (servername, callback) => { + if (typeof sni === 'function') { + PromisePrototypeThen( + PromiseResolve(FunctionPrototypeCall(sni, undefined, + StringPrototypeToLowerCase(servername))), + (certificate) => { + let context; + try { + context = getContext(certificate, 'sni()'); + } catch (err) { + report(err); + callback(err); + return; + } + callback(null, context); + }, + (err) => { + report(err); + callback(err); + }); + return; + } + const certificate = matchSni(sni, servername); + if (certificate === null) { + callback(null, undefined); + return; + } + const key = StringPrototypeToLowerCase(servername); + try { + contexts[key] ??= getContext(certificate, 'tls.sni'); + } catch (err) { + callback(err); + return; + } + callback(null, contexts[key]); + }; + } + return result; +} + +function bind(internals) { + const { options } = internals; + const { promise, resolve, reject } = PromiseWithResolvers(); + let netServer; + try { + if (options.tls !== undefined) { + const tls = require('tls'); + netServer = tls.createServer({ + ...createTLSOptions(internals, options.tls), + allowHalfOpen: true, + }); + netServer.on('secureConnection', + (socket) => onConnection(internals, socket)); + netServer.on('tlsClientError', noop); + } else { + const net = require('net'); + netServer = net.createServer({ allowHalfOpen: true }); + netServer.on('connection', (socket) => onConnection(internals, socket)); + } + } catch (err) { + return PromiseReject(err); + } + if (options.limits.maxConnections !== undefined) { + netServer.maxConnections = options.limits.maxConnections; + } + internals.netServer = netServer; + const onError = (err) => { + netServer.removeListener('listening', onListening); + reject(err); + }; + const onListening = () => { + netServer.removeListener('error', onError); + netServer.on('error', report); + resolve(); + }; + netServer.once('error', onError); + netServer.once('listening', onListening); + const listenOptions = { + port: options.port, + host: options.hostname, + ipv6Only: options.ipv6Only, + reusePort: options.reusePort, + }; + if (options.backlog !== undefined) listenOptions.backlog = options.backlog; + try { + netServer.listen(listenOptions); + } catch (err) { + netServer.removeListener('error', onError); + netServer.removeListener('listening', onListening); + return PromiseReject(err); + } + return promise; +} + +function validateListenOptions(options) { + if (options.quic) { + if (options.tls === undefined) { + throw new ERR_INVALID_ARG_VALUE('options.quic', options.quic, + 'requires options.tls'); + } + throw notSupported('QUIC is not supported'); + } + if (options.webTransport === true) { + throw notSupported('WebTransport is not supported'); + } + if (options.datagrams === true) { + throw notSupported('HTTP/3 datagrams are not supported'); + } +} + +// ============================================================================= +// Server (Section 5.17) +// ============================================================================= + +let getServerInternals; +let isServer; + +class ServerInternals extends Closeable { + handlers; + options; + listeners = []; + busy = false; + ready; + + constructor(handlers, options) { + super(); + this.handlers = handlers; + this.options = options; + } + + listenerClosed(listener) { + const index = ArrayPrototypeIndexOf(this.listeners, listener); + if (index !== -1) ArrayPrototypeSplice(this.listeners, index, 1); + this.maybeFinish(); + } + + maybeFinish() { + if (this.done) return; + if (!this.closing && !this.destroyed) return; + if (this.listeners.length > 0) return; + if (this.destroyed) return; + this.resolveClosed(); + } +} + +class Server { + #internals; + + static { + getServerInternals = (server) => server.#internals; + isServer = (value) => { + return value != null && typeof value === 'object' && #internals in value; + }; + } + + constructor(key, internals) { + if (key !== kConstruct) { + throw new ERR_INVALID_THIS('Server cannot be constructed'); + } + this.#internals = internals; + } + + get ready() { + return checkServer(this).ready; + } + + listen(options = undefined) { + const internals = checkServer(this); + let resolved; + try { + if (internals.closing || internals.destroyed) { + throw invalidState('The server has been closed'); + } + if (options !== undefined && options !== null && + typeof options !== 'object' && typeof options !== 'function') { + throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); + } + resolved = resolveListenOptions(internals.options, + convertListenMembers(options ?? {})); + validateListenOptions(resolved); + } catch (err) { + return PromiseReject(err); + } + return listen(internals, resolved); + } + + get busy() { + return checkServer(this).busy; + } + + set busy(value) { + const internals = checkServer(this); + internals.busy = !!value; + for (let i = 0; i < internals.listeners.length; i++) { + internals.listeners[i].busy = internals.busy; + } + } + + close() { + const internals = checkServer(this); + closeServer(internals); + return internals.closedPromise; + } + + destroy(error = undefined) { + destroyServer(checkServer(this), error); + } + + get closed() { + return checkServer(this).closedPromise; + } + + [SymbolIterator]() { + const internals = checkServer(this); + const listeners = []; + for (let i = 0; i < internals.listeners.length; i++) { + ArrayPrototypePush(listeners, internals.listeners[i].listener); + } + return new SafeArrayIterator(listeners); + } + + [SymbolAsyncDispose]() { + return this.close(); + } + + [kInspect](depth, options) { + if (depth < 0) return 'Server {}'; + return `Server ${inspect({ listeners: [...this], busy: this.busy }, + options)}`; + } +} + +function checkServer(self) { + if (!isServer(self)) throw new ERR_INVALID_THIS('Server'); + return getServerInternals(self); +} + +ObjectDefineProperties(Server.prototype, { + ready: kEnumerableProperty, + listen: kEnumerableProperty, + busy: kEnumerableProperty, + close: kEnumerableProperty, + destroy: kEnumerableProperty, + closed: kEnumerableProperty, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'Server', + }, +}); + +function listen(serverInternals, options) { + const internals = new ListenerInternals(serverInternals, options); + internals.listener = new Listener(kConstruct, internals); + ArrayPrototypePush(serverInternals.listeners, internals); + return PromisePrototypeThen(bind(internals), () => { + if (serverInternals.destroyed) destroyListener(internals); + return internals.listener; + }, (err) => { + const index = ArrayPrototypeIndexOf(serverInternals.listeners, internals); + if (index !== -1) ArrayPrototypeSplice(serverInternals.listeners, index, 1); + throw err; + }); +} + +function closeServer(internals) { + if (!internals.startClose()) return; + const listeners = [...new SafeArrayIterator(internals.listeners)]; + for (let i = 0; i < listeners.length; i++) { + closeListener(listeners[i]); + } + internals.maybeFinish(); +} + +function destroyServer(internals, error) { + if (!internals.startDestroy()) return; + const listeners = [...new SafeArrayIterator(internals.listeners)]; + for (let i = 0; i < listeners.length; i++) { + destroyListener(listeners[i], error); + } + internals.rejectClosed(error === undefined ? abortError() : error); +} + +/** + * The serve() function (Section 17.1). + * @param {object} handler + * @param {object} [options] + * @returns {Server} + */ +function serve(handler, options = undefined) { + const handlers = convertHandler(handler); + const converted = convertServerOptions(options); + const internals = new ServerInternals(handlers, converted); + const server = new Server(kConstruct, internals); + + if (converted.signal !== undefined) { + const signal = converted.signal; + if (signal.aborted) { + destroyServer(internals, signal.reason); + } else { + signal.addEventListener('abort', () => { + destroyServer(internals, signal.reason); + }, { __proto__: null, once: true }); + } + } + + if (converted.port !== undefined && !internals.destroyed) { + const resolved = resolveListenOptions(converted, { __proto__: null }); + validateListenOptions(resolved); + internals.ready = PromisePrototypeThen(listen(internals, resolved), + () => undefined, (err) => { + destroyServer(internals, err); + throw err; + }); + } else { + internals.ready = PromiseResolve(); + } + markHandled(internals.ready); + return server; +} + +module.exports = { + Listener, + Server, + serve, +}; diff --git a/lib/internal/http/fetch_server/tunnel.js b/lib/internal/http/fetch_server/tunnel.js new file mode 100644 index 000000000000..f35f52551d2e --- /dev/null +++ b/lib/internal/http/fetch_server/tunnel.js @@ -0,0 +1,815 @@ +'use strict'; + +// The Tunnel, CapsuleStream, and DatagramStream of the Fetch Server API. +// https://fetch-server.proposal.wintertc.org/#tunnel + +const { + ArrayPrototypePush, + ArrayPrototypeShift, + MathFloor, + NumberIsSafeInteger, + ObjectDefineProperties, + PromisePrototypeThen, + PromiseReject, + PromiseResolve, + PromiseWithResolvers, + SymbolAsyncDispose, + SymbolAsyncIterator, + SymbolToStringTag, + TypedArrayPrototypeGetByteLength, + TypedArrayPrototypeSet, + TypedArrayPrototypeSubarray, + Uint8Array, +} = primordials; + +const { + codes: { + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_INVALID_STATE, + ERR_INVALID_THIS, + }, +} = require('internal/errors'); + +const { + kEnumerableProperty, +} = require('internal/util'); + +const { + isUint8Array, +} = require('internal/util/types'); + +const { + converters, +} = require('internal/webidl'); + +const { + createProtocolError, + getErrorCode, + invalidState, + kConnectError, + kConnectionReset, + kH2Codes, + kTunnelSendable, + protocolErrorFromH2, +} = require('internal/http/fetch_server/errors'); + +const { + markHandled, +} = require('internal/http/fetch_server/body'); + +const { + NodeReadableReader, +} = require('internal/http/fetch_server/node_reader'); + +const { + drainableProtocol, +} = require('internal/streams/iter/types'); + +const { + convertChunks, + toWriterUint8Array, +} = require('internal/streams/iter/utils'); + +const kMaxDatagramSize = 65527; +const kDatagramCapsule = 0; + +// ============================================================================= +// Capsule Protocol codec (RFC 9297 Section 3.2) +// ============================================================================= + +function varintLength(value) { + if (value < 64) return 1; + if (value < 16384) return 2; + if (value < 1073741824) return 4; + return 8; +} + +function writeVarint(target, offset, value) { + const length = varintLength(value); + switch (length) { + case 1: + target[offset] = value; + break; + case 2: + target[offset] = 0x40 | (value >>> 8); + target[offset + 1] = value & 0xff; + break; + case 4: + target[offset] = 0x80 | (value >>> 24); + target[offset + 1] = (value >>> 16) & 0xff; + target[offset + 2] = (value >>> 8) & 0xff; + target[offset + 3] = value & 0xff; + break; + default: { + const high = MathFloor(value / 0x100000000); + const low = value >>> 0; + target[offset] = 0xc0 | (high >>> 24); + target[offset + 1] = (high >>> 16) & 0xff; + target[offset + 2] = (high >>> 8) & 0xff; + target[offset + 3] = high & 0xff; + target[offset + 4] = low >>> 24; + target[offset + 5] = (low >>> 16) & 0xff; + target[offset + 6] = (low >>> 8) & 0xff; + target[offset + 7] = low & 0xff; + } + } + return offset + length; +} + +function encodeCapsule(type, data) { + const dataLength = TypedArrayPrototypeGetByteLength(data); + const out = new Uint8Array(varintLength(type) + varintLength(dataLength) + + dataLength); + let offset = writeVarint(out, 0, type); + offset = writeVarint(out, offset, dataLength); + TypedArrayPrototypeSet(out, data, offset); + return out; +} + +// Reads a varint at offset; returns [value, nextOffset] or null. +function readVarint(buffer, offset) { + if (offset >= buffer.length) return null; + const first = buffer[offset]; + const length = 1 << (first >> 6); + if (offset + length > buffer.length) return null; + let value = first & 0x3f; + for (let i = 1; i < length; i++) { + value = value * 256 + buffer[offset + i]; + } + return [value, offset + length]; +} + +class CapsuleParser { + #buffer = new Uint8Array(0); + + push(chunk) { + if (this.#buffer.length === 0) { + this.#buffer = chunk; + } else { + const combined = new Uint8Array(this.#buffer.length + chunk.length); + TypedArrayPrototypeSet(combined, this.#buffer, 0); + TypedArrayPrototypeSet(combined, chunk, this.#buffer.length); + this.#buffer = combined; + } + const capsules = []; + for (;;) { + const type = readVarint(this.#buffer, 0); + if (type === null) break; + const length = readVarint(this.#buffer, type[1]); + if (length === null) break; + const end = length[1] + length[0]; + if (end > this.#buffer.length) break; + ArrayPrototypePush(capsules, { + type: type[0], + data: TypedArrayPrototypeSubarray(this.#buffer, length[1], end), + }); + this.#buffer = TypedArrayPrototypeSubarray(this.#buffer, end); + } + return capsules; + } + + get hasPartial() { + return this.#buffer.length > 0; + } +} + +// ============================================================================= +// Tunnel internals +// ============================================================================= + +class TunnelInternals { + exchange; + stream; + http2; + connectProtocol; + mode = 'unclaimed'; + reader; + readerIterator = null; + sendDone = false; + recvDone = false; + terminated = false; + closedClean = false; + error = undefined; + bytesWritten = 0; + closed = PromiseWithResolvers(); + endPromise = null; + pendingDrains = []; + // Capsule mode. + capsuleStream = null; + capsuleParser = null; + capsuleQueue = []; + capsuleWaiters = []; + datagramQueue = []; + datagramWaiters = []; + datagramStream = null; + datagramsEnabled = false; + capsulesRequested = false; + + constructor(exchange, transport, connectProtocol) { + this.exchange = exchange; + this.stream = transport.stream; + this.http2 = transport.http2; + this.connectProtocol = connectProtocol; + this.reader = new NodeReadableReader(this.stream, () => { + this.recvDone = true; + this.maybeClose(); + }); + markHandled(this.closed.promise); + const stream = this.stream; + stream.on('error', () => {}); + stream.on('drain', () => this.#drained()); + stream.on('close', () => { + if (this.terminated) return; + if (this.sendDone && this.recvDone) return; + this.terminate(this.#closeReason(), false); + }); + } + + get isWebSocket() { + return this.connectProtocol === 'websocket'; + } + + #closeReason() { + if (this.http2) { + const rstCode = this.stream.rstCode; + if (rstCode !== undefined && rstCode !== 0) { + return protocolErrorFromH2(rstCode, 'h2'); + } + } + return createProtocolError(kConnectionReset, 'The connection was lost'); + } + + #drained() { + const drains = this.pendingDrains; + this.pendingDrains = []; + for (let i = 0; i < drains.length; i++) drains[i].resolve(true); + } + + maybeClose() { + if (this.terminated || !this.sendDone || !this.recvDone) return; + this.terminated = true; + this.closedClean = true; + this.closed.resolve(); + this.exchange.complete(); + if (!this.http2) this.stream.destroy(); + } + + /** + * Terminates the tunnel abnormally. + * @param {any} error The error with which the surfaces fail. + * @param {boolean} local Whether the server terminated the tunnel. + * @param {string} [code] The error code to send. + */ + terminate(error, local, code = kConnectError) { + if (this.terminated) return; + this.terminated = true; + this.error = error; + if (local) { + if (this.http2) { + this.stream.close(kH2Codes[code] ?? kH2Codes[kConnectError]); + } else { + this.stream.destroy(); + } + } + this.#drained(); + this.#wakeCapsules(); + this.#wakeDatagrams(); + this.closed.reject(error); + if (local) { + this.exchange.complete(); + } else { + this.exchange.abort(error); + } + } + + // Raw data stream (Section 12.1). + + claimRaw() { + if (this.mode === 'capsules') { + throw invalidState('The tunnel data stream carries capsules'); + } + this.mode = 'raw'; + } + + readRaw() { + if (this.terminated && !this.closedClean) { + return PromiseReject(this.error); + } + return PromisePrototypeThen(this.reader.read(), undefined, (err) => { + if (!this.terminated) this.terminate(this.#closeReason(), false); + throw this.error ?? err; + }); + } + + write(chunks) { + if (this.terminated) { + return PromiseReject(this.error ?? new ERR_INVALID_STATE.TypeError( + 'The tunnel is closed')); + } + if (this.endPromise !== null) { + return PromiseReject(new ERR_INVALID_STATE.TypeError( + 'The tunnel send direction has ended')); + } + let ok = true; + for (let i = 0; i < chunks.length; i++) { + this.bytesWritten += TypedArrayPrototypeGetByteLength(chunks[i]); + ok = this.stream.write(chunks[i]); + } + if (ok) return PromiseResolve(); + const drain = PromiseWithResolvers(); + ArrayPrototypePush(this.pendingDrains, drain); + return PromisePrototypeThen(drain.promise, () => { + if (this.terminated && !this.closedClean) throw this.error; + }); + } + + end() { + if (this.terminated && !this.closedClean) { + return PromiseReject(this.error); + } + if (this.endPromise === null) { + const { promise, resolve, reject } = PromiseWithResolvers(); + this.endPromise = promise; + markHandled(promise); + this.stream.end(() => { + this.sendDone = true; + resolve(this.bytesWritten); + this.maybeClose(); + }); + PromisePrototypeThen(this.closed.promise, undefined, reject); + } + return this.endPromise; + } + + // Capsule Protocol (Section 12.2). + + claimCapsules() { + if (this.mode === 'raw') { + throw invalidState('The tunnel data stream has been read as bytes'); + } + if (this.mode === 'capsules') return; + this.mode = 'capsules'; + this.capsuleParser = new CapsuleParser(); + this.#pumpCapsules(); + } + + async #pumpCapsules() { + try { + for (;;) { + const batch = await this.readRaw(); + if (batch === null) break; + for (let i = 0; i < batch.length; i++) { + const capsules = this.capsuleParser.push(batch[i]); + for (let j = 0; j < capsules.length; j++) { + const capsule = capsules[j]; + if (capsule.type === kDatagramCapsule) { + if (this.datagramsEnabled) { + ArrayPrototypePush(this.datagramQueue, capsule.data); + this.#wakeDatagrams(); + } + } else { + ArrayPrototypePush(this.capsuleQueue, capsule); + this.#wakeCapsules(); + } + } + } + } + this.capsuleEnded = true; + } catch { + // The tunnel terminated; the error is surfaced by terminate(). + } + this.#wakeCapsules(); + this.#wakeDatagrams(); + } + + #wakeCapsules() { + const waiters = this.capsuleWaiters; + this.capsuleWaiters = []; + for (let i = 0; i < waiters.length; i++) waiters[i](); + } + + #wakeDatagrams() { + const waiters = this.datagramWaiters; + this.datagramWaiters = []; + for (let i = 0; i < waiters.length; i++) waiters[i](); + } + + async nextCapsule() { + for (;;) { + if (this.capsuleQueue.length > 0) { + return ArrayPrototypeShift(this.capsuleQueue); + } + if (this.terminated && !this.closedClean) throw this.error; + if (this.capsuleEnded || this.closedClean) return null; + const { promise, resolve } = PromiseWithResolvers(); + ArrayPrototypePush(this.capsuleWaiters, resolve); + await promise; + } + } + + async nextDatagrams() { + for (;;) { + if (this.datagramQueue.length > 0) { + const batch = this.datagramQueue; + this.datagramQueue = []; + return batch; + } + if (this.terminated && !this.closedClean) throw this.error; + if (this.capsuleEnded || this.closedClean) return null; + const { promise, resolve } = PromiseWithResolvers(); + ArrayPrototypePush(this.datagramWaiters, resolve); + await promise; + } + } + + sendCapsule(type, data) { + return this.write([encodeCapsule(type, data)]); + } + + close() { + if (this.terminated) { + return this.closedClean ? PromiseResolve() : PromiseReject(this.error); + } + markHandled(this.end()); + return this.closed.promise; + } + + destroy(error) { + if (this.terminated) return; + const code = getErrorCode(error, kTunnelSendable) ?? kConnectError; + this.terminate(error ?? createProtocolError(kConnectError, + 'The tunnel was destroyed'), + true, code); + } +} + +// ============================================================================= +// Readable and Writer objects +// ============================================================================= + +function createReadableObject(internals, readStep, onClaim) { + let reader = null; + const readable = { + __proto__: null, + [SymbolAsyncIterator]() { + onClaim(); + let done = false; + const iterator = { + __proto__: null, + next() { + if (iterator !== reader) { + return PromiseReject(new ERR_INVALID_STATE.TypeError( + 'The tunnel data can be read only once')); + } + if (done) return PromiseResolve({ value: undefined, done: true }); + return PromisePrototypeThen(readStep(), (value) => { + if (value === null) { + done = true; + return { value: undefined, done: true }; + } + return { value, done: false }; + }, (err) => { + done = true; + throw err; + }); + }, + return(value) { + if (iterator === reader && !done) { + done = true; + // Ending iteration early terminates the tunnel (Section 12.1). + internals.terminate(createProtocolError( + kConnectError, 'Tunnel iteration ended early'), true); + } + return PromiseResolve({ value, done: true }); + }, + [SymbolAsyncIterator]() { + return this; + }, + }; + reader ??= iterator; + return iterator; + }, + }; + return readable; +} + +function createWriter(internals, onClaim, sendStep = (chunks) => internals.write(chunks)) { + const writer = { + __proto__: null, + get canWrite() { + if (internals.terminated || internals.endPromise !== null) return null; + return !internals.stream.writableNeedDrain; + }, + write(chunk, options = undefined) { + onClaim(); + try { + chunk = toWriterUint8Array(chunk); + } catch (err) { + return PromiseReject(err); + } + if (options?.signal?.aborted) return PromiseReject(options.signal.reason); + return sendStep([chunk]); + }, + writev(chunks, options = undefined) { + onClaim(); + try { + chunks = convertChunks(chunks); + } catch (err) { + return PromiseReject(err); + } + if (options?.signal?.aborted) return PromiseReject(options.signal.reason); + return sendStep(chunks); + }, + writeSync(chunk) { + onClaim(); + if (writer.canWrite !== true) return false; + markHandled(sendStep([toWriterUint8Array(chunk)])); + return true; + }, + writevSync(chunks) { + onClaim(); + if (writer.canWrite !== true) return false; + markHandled(sendStep(convertChunks(chunks))); + return true; + }, + end(options = undefined) { + onClaim(); + if (options?.signal?.aborted) return PromiseReject(options.signal.reason); + return internals.end(); + }, + endSync() { + onClaim(); + if (internals.terminated) return -1; + markHandled(internals.end()); + return -1; + }, + fail(reason = undefined) { + onClaim(); + if (internals.terminated) return; + const code = getErrorCode(reason, kTunnelSendable) ?? kConnectError; + internals.terminate(reason ?? createProtocolError( + kConnectError, 'The tunnel writer failed'), true, code); + }, + [drainableProtocol]() { + const canWrite = writer.canWrite; + if (canWrite === null) return null; + if (canWrite) return PromiseResolve(true); + const drain = PromiseWithResolvers(); + ArrayPrototypePush(internals.pendingDrains, drain); + return drain.promise; + }, + [SymbolAsyncDispose]() { + if (internals.endPromise !== null || internals.terminated) { + return PromiseResolve(); + } + writer.fail(); + return PromiseResolve(); + }, + }; + return writer; +} + +// ============================================================================= +// CapsuleStream (Section 5.10) +// ============================================================================= + +class CapsuleStream { + #internals; + #readable; + + constructor(key, internals) { + if (key !== kConstruct) { + throw new ERR_INVALID_THIS('CapsuleStream cannot be constructed'); + } + this.#internals = internals; + this.#readable = createReadableObject(internals, + () => internals.nextCapsule(), + () => {}); + } + + get readable() { + if (this.#internals === undefined) throw new ERR_INVALID_THIS('CapsuleStream'); + return this.#readable; + } + + send(capsule) { + const internals = this.#internals; + if (internals === undefined) { + return PromiseReject(new ERR_INVALID_THIS('CapsuleStream')); + } + if (capsule === null || typeof capsule !== 'object') { + return PromiseReject(new ERR_INVALID_ARG_TYPE('capsule', 'Object', capsule)); + } + let type; + try { + type = converters['long long'](capsule.type, { + __proto__: null, enforceRange: true, context: 'capsule.type', + }); + } catch (err) { + return PromiseReject(err); + } + const data = capsule.data; + if (!isUint8Array(data)) { + return PromiseReject(new ERR_INVALID_ARG_TYPE('capsule.data', 'Uint8Array', + data)); + } + if (type < 0 || !NumberIsSafeInteger(type)) { + return PromiseReject(new ERR_INVALID_ARG_VALUE('capsule.type', type)); + } + if (type === kDatagramCapsule) { + return PromiseReject(new ERR_INVALID_ARG_VALUE( + 'capsule.type', type, 'DATAGRAM capsules are sent with datagrams()')); + } + return internals.sendCapsule(type, data); + } +} + +ObjectDefineProperties(CapsuleStream.prototype, { + readable: kEnumerableProperty, + send: kEnumerableProperty, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'CapsuleStream', + }, +}); + +// ============================================================================= +// Tunnel (Section 5.9) +// ============================================================================= + +const kConstruct = {}; +let getTunnelInternals; +let isTunnel; + +class Tunnel { + #internals; + #readable = null; + #writer = null; + + static { + getTunnelInternals = (tunnel) => tunnel.#internals; + isTunnel = (value) => { + return value != null && typeof value === 'object' && #internals in value; + }; + } + + constructor(key, internals) { + if (key !== kConstruct) { + throw new ERR_INVALID_THIS('Tunnel cannot be constructed'); + } + this.#internals = internals; + } + + get readable() { + const internals = checkTunnel(this); + if (internals.mode === 'capsules') { + throw invalidState('The tunnel data stream carries capsules'); + } + this.#readable ??= createReadableObject( + internals, () => internals.readRaw(), () => internals.claimRaw()); + return this.#readable; + } + + get writer() { + const internals = checkTunnel(this); + if (internals.mode === 'capsules') { + throw invalidState('The tunnel data stream carries capsules'); + } + this.#writer ??= createWriter(internals, () => internals.claimRaw()); + return this.#writer; + } + + capsules() { + const internals = checkTunnel(this); + if (internals.connectProtocol === null || internals.isWebSocket) { + throw invalidState( + 'The Capsule Protocol is not available on this tunnel'); + } + if (internals.mode === 'raw') { + throw invalidState('The tunnel data stream has been read as bytes'); + } + if (internals.capsulesRequested) { + throw invalidState('capsules() has already been called'); + } + internals.capsulesRequested = true; + internals.claimCapsules(); + internals.capsuleStream = new CapsuleStream(kConstruct, internals); + return internals.capsuleStream; + } + + datagrams() { + const internals = checkTunnel(this); + if (internals.connectProtocol === null || internals.isWebSocket) { + throw invalidState('HTTP Datagrams are not available on this tunnel'); + } + if (internals.mode === 'raw') { + throw invalidState('The tunnel data stream has been read as bytes'); + } + internals.claimCapsules(); + if (internals.datagramStream === null) { + internals.datagramsEnabled = true; + const readable = createReadableObject( + internals, () => internals.nextDatagrams(), () => {}); + // Ending iteration of datagrams early does not affect the tunnel. + const iteratorFactory = readable[SymbolAsyncIterator]; + readable[SymbolAsyncIterator] = function() { + const iterator = iteratorFactory(); + iterator.return = (value) => { + internals.datagramsEnabled = false; + internals.datagramQueue = []; + return PromiseResolve({ value, done: true }); + }; + return iterator; + }; + let ended = false; + const writer = createWriter(internals, () => {}, (chunks) => { + if (ended) { + return PromiseReject(new ERR_INVALID_STATE.TypeError( + 'The datagram writer has ended')); + } + const capsules = []; + for (let i = 0; i < chunks.length; i++) { + const size = TypedArrayPrototypeGetByteLength(chunks[i]); + if (size <= kMaxDatagramSize) { + ArrayPrototypePush(capsules, encodeCapsule(kDatagramCapsule, + chunks[i])); + } + } + return internals.write(capsules); + }); + writer.end = () => { + ended = true; + return PromiseResolve(0); + }; + writer.fail = () => { + ended = true; + }; + internals.datagramStream = { readable, writer }; + } + return { + readable: internals.datagramStream.readable, + writer: internals.datagramStream.writer, + unreliable: false, + maxDatagramSize: kMaxDatagramSize, + }; + } + + close() { + try { + return checkTunnel(this).close(); + } catch (err) { + return PromiseReject(err); + } + } + + destroy(error = undefined) { + checkTunnel(this).destroy(error); + } + + get closed() { + return checkTunnel(this).closed.promise; + } + + [SymbolAsyncDispose]() { + const internals = checkTunnel(this); + if (internals.terminated) return PromiseResolve(); + return internals.close(); + } +} + +function checkTunnel(self) { + if (!isTunnel(self)) throw new ERR_INVALID_THIS('Tunnel'); + return getTunnelInternals(self); +} + +ObjectDefineProperties(Tunnel.prototype, { + readable: kEnumerableProperty, + writer: kEnumerableProperty, + capsules: kEnumerableProperty, + datagrams: kEnumerableProperty, + close: kEnumerableProperty, + destroy: kEnumerableProperty, + closed: kEnumerableProperty, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'Tunnel', + }, +}); + +/** + * Creates a Tunnel for an accepted exchange. + * @returns {Tunnel} + */ +function createTunnel(exchange, transport, connectProtocol) { + const internals = new TunnelInternals(exchange, transport, connectProtocol); + exchange.tunnel = internals; + return new Tunnel(kConstruct, internals); +} + +module.exports = { + Tunnel, + createTunnel, +}; diff --git a/lib/internal/http/fetch_server/websocket.js b/lib/internal/http/fetch_server/websocket.js new file mode 100644 index 000000000000..8d668e55a27e --- /dev/null +++ b/lib/internal/http/fetch_server/websocket.js @@ -0,0 +1,1595 @@ +'use strict'; + +// WebSocket upgrades of the Fetch Server API (Section 11.4, Section 11.5, +// Section 13), with the RFC 6455 framing and the RFC 7692 permessage-deflate +// extension. + +const { + ArrayPrototypePush, + ArrayPrototypeShift, + MathFloor, + ObjectDefineProperties, + Promise, + PromisePrototypeThen, + PromiseReject, + PromiseResolve, + PromiseWithResolvers, + RegExpPrototypeExec, + SafeSet, + StringPrototypeIndexOf, + StringPrototypeSlice, + StringPrototypeSplit, + StringPrototypeToLowerCase, + StringPrototypeToWellFormed, + StringPrototypeTrim, + Symbol, + SymbolAsyncDispose, + SymbolToStringTag, + TypedArrayPrototypeGetByteLength, + TypedArrayPrototypeSet, + TypedArrayPrototypeSlice, + TypedArrayPrototypeSubarray, + Uint8Array, +} = primordials; + +const { Buffer } = require('buffer'); +const { URL } = require('internal/url'); +const { setTimeout, clearTimeout, setImmediate } = require('timers'); + +const { + codes: { + ERR_ILLEGAL_CONSTRUCTOR, + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_INVALID_THIS, + }, +} = require('internal/errors'); + +const { + kEnumerableProperty, + lazyDOMException, +} = require('internal/util'); + +const { + isAnyArrayBuffer, + isArrayBuffer, + isArrayBufferView, +} = require('internal/util/types'); + +const { + converters, + convertToInt, + createSequenceConverter, +} = require('internal/webidl'); + +const { + Event, + EventTarget, + defineEventHandler, +} = require('internal/event_target'); + +const { + createProtocolError, + getErrorCode, + invalidState, + kConnectError, + kConnectionReset, + kH2Codes, + kRequestCancelled, + kTimeout, + kTunnelSendable, + notSupported, + protocolErrorFromH2, +} = require('internal/http/fetch_server/errors'); + +const { + listGet, + listGetAll, +} = require('internal/http/fetch_server/headers'); + +const { + markHandled, +} = require('internal/http/fetch_server/body'); + +const kConstruct = Symbol('kConstruct'); +const kWebSocketGuid = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; + +// Implementation-defined limits (Section 13.2). +const kMaxMessageSize = 64 * 1024 * 1024; +const kReadHighWaterMark = 1024 * 1024; +const kCompressionThreshold = 1024; +const kDefaultCloseHandshakeTimeout = 30_000; + +const kOpContinuation = 0x0; +const kOpText = 0x1; +const kOpBinary = 0x2; +const kOpClose = 0x8; +const kOpPing = 0x9; +const kOpPong = 0xa; + +const kDeflateTrailer = new Uint8Array([0x00, 0x00, 0xff, 0xff]); +const kEmpty = new Uint8Array(0); + +let lazyTextDecoder; +function decodeUtf8(bytes) { + if (lazyTextDecoder === undefined) { + const { TextDecoder } = require('internal/encoding'); + lazyTextDecoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }); + } + return lazyTextDecoder.decode(bytes); +} + +function encodeUtf8(string) { + const buffer = Buffer.from(string, 'utf8'); + const out = new Uint8Array(buffer.length); + TypedArrayPrototypeSet(out, buffer, 0); + return out; +} + +function concatBytes(list, length) { + if (list.length === 1 && TypedArrayPrototypeGetByteLength(list[0]) === length) { + return list[0]; + } + const out = new Uint8Array(length); + let offset = 0; + for (let i = 0; i < list.length; i++) { + TypedArrayPrototypeSet(out, list[i], offset); + offset += TypedArrayPrototypeGetByteLength(list[i]); + } + return out; +} + +// ============================================================================= +// Options (WebSocketUpgradeInit) and close codes (Section 13.4) +// ============================================================================= + +const convertStringSequence = createSequenceConverter(converters.DOMString); + +function convertUpgradeOptions(options) { + const result = { __proto__: null, protocol: undefined, + compression: undefined, pingInterval: undefined }; + if (options === undefined || options === null) return result; + if (typeof options !== 'object' && typeof options !== 'function') { + throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); + } + // Dictionary members are converted in lexicographical order. + const compression = options.compression; + if (compression !== undefined) result.compression = !!compression; + const pingInterval = options.pingInterval; + if (pingInterval !== undefined) { + result.pingInterval = convertToInt(pingInterval, 64, 'unsigned', { + __proto__: null, enforceRange: true, context: 'options.pingInterval', + }); + } + const protocol = options.protocol; + if (protocol !== undefined) { + result.protocol = convertStringSequence(protocol, { + __proto__: null, context: 'options.protocol', + }); + } + return result; +} + +function convertCloseInfo(info) { + const result = { __proto__: null, closeCode: null, reason: '' }; + if (info === undefined || info === null) return result; + if (typeof info !== 'object' && typeof info !== 'function') { + throw new ERR_INVALID_ARG_TYPE('closeInfo', 'Object', info); + } + const closeCode = info.closeCode; + if (closeCode !== undefined) { + result.closeCode = converters['unsigned short'](closeCode, { + __proto__: null, enforceRange: true, context: 'closeInfo.closeCode', + }); + } + const reason = info.reason; + if (reason !== undefined) { + result.reason = converters.USVString(reason, { + __proto__: null, context: 'closeInfo.reason', + }); + } + return result; +} + +function isSendableCloseCode(code) { + return (code >= 1000 && code <= 1003) || (code >= 1007 && code <= 1009) || + (code >= 1011 && code <= 1014) || (code >= 3000 && code <= 4999); +} + +// The codes a peer may send in a Close frame (RFC 6455 Section 7.4). +function isReceivableCloseCode(code) { + return isSendableCloseCode(code) || code === 1010; +} + +/** + * Validates a server close code and reason (Section 13.4). + * @param {number|null} code + * @param {string} reason + */ +function validateServerClose(code, reason) { + if (code !== null && !isSendableCloseCode(code)) { + throw lazyDOMException(`The close code ${code} cannot be sent by a server`, + 'InvalidAccessError'); + } + if (Buffer.byteLength(reason, 'utf8') > 123) { + throw lazyDOMException('The close reason is longer than 123 bytes', + 'SyntaxError'); + } +} + +// ============================================================================= +// Opening handshake (Section 11.4) +// ============================================================================= + +const kTokenRegExp = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; +const kKeyRegExp = /^[A-Za-z0-9+/]{21}[AQgw]==$/; + +function splitList(value) { + const result = []; + const parts = StringPrototypeSplit(value, ','); + for (let i = 0; i < parts.length; i++) { + const part = StringPrototypeTrim(parts[i]); + if (part !== '') ArrayPrototypePush(result, part); + } + return result; +} + +/** + * Checks a WebSocket handshake request (Section 11.4). + * @param {import('./exchange').Exchange} exchange + * @returns {boolean} + */ +function checkHandshake(exchange) { + const list = exchange.backend.headerList; + const version = listGet(list, 'sec-websocket-version'); + if (version === null || StringPrototypeTrim(version) !== '13') return false; + if (exchange.kind !== 'upgrade') return true; + if (exchange.backend.method !== 'GET') return false; + const upgrade = listGet(list, 'upgrade'); + if (upgrade === null) return false; + const offered = splitList(upgrade); + let found = false; + for (let i = 0; i < offered.length; i++) { + if (StringPrototypeToLowerCase(offered[i]) === 'websocket') found = true; + } + if (!found) return false; + const key = listGet(list, 'sec-websocket-key'); + return key !== null && + RegExpPrototypeExec(kKeyRegExp, StringPrototypeTrim(key)) !== null; +} + +function computeAccept(key) { + let createHash; + try { + ({ createHash } = require('crypto')); + } catch { + throw notSupported('WebSocket upgrades over HTTP/1.1 require crypto'); + } + return createHash('sha1').update(`${key}${kWebSocketGuid}`) + .digest('base64'); +} + +// Splits on sep outside of quoted strings. +function splitOutsideQuotes(value, sep) { + const result = []; + let start = 0; + let quoted = false; + for (let i = 0; i < value.length; i++) { + const c = value[i]; + if (quoted) { + if (c === '\\') i++; + else if (c === '"') quoted = false; + } else if (c === '"') { + quoted = true; + } else if (c === sep) { + ArrayPrototypePush(result, StringPrototypeSlice(value, start, i)); + start = i + 1; + } + } + if (quoted) return null; + ArrayPrototypePush(result, StringPrototypeSlice(value, start)); + return result; +} + +function unquote(value) { + if (value[0] !== '"') return value; + let result = ''; + for (let i = 1; i < value.length - 1; i++) { + if (value[i] === '\\') i++; + result += value[i]; + } + return result; +} + +/** + * Parses Sec-WebSocket-Extensions offers (RFC 6455 Section 9.1). + * @param {string} value + * @returns {Array<{ name: string, params: Array<[string, string|true]> }>} + */ +function parseExtensions(value) { + const offers = []; + const extensions = splitOutsideQuotes(value, ','); + if (extensions === null) return offers; + for (let i = 0; i < extensions.length; i++) { + const parts = splitOutsideQuotes(extensions[i], ';'); + const name = StringPrototypeToLowerCase(StringPrototypeTrim(parts[0])); + if (name === '') continue; + if (RegExpPrototypeExec(kTokenRegExp, name) === null) continue; + const params = []; + let valid = true; + for (let j = 1; j < parts.length; j++) { + const param = StringPrototypeTrim(parts[j]); + const eq = StringPrototypeIndexOf(param, '='); + const key = StringPrototypeToLowerCase(StringPrototypeTrim( + eq === -1 ? param : StringPrototypeSlice(param, 0, eq))); + if (RegExpPrototypeExec(kTokenRegExp, key) === null) { + valid = false; + break; + } + const paramValue = eq === -1 ? true : + unquote(StringPrototypeTrim(StringPrototypeSlice(param, eq + 1))); + ArrayPrototypePush(params, [key, paramValue]); + } + if (valid) ArrayPrototypePush(offers, { name, params }); + } + return offers; +} + +const kWindowBitsRegExp = /^(?:[89]|1[0-5])$/; + +/** + * Selects the first acceptable permessage-deflate offer (RFC 7692 Section 7). + * @param {string|null} header + * @returns {object|null} + */ +function negotiateDeflate(header) { + if (header === null) return null; + const offers = parseExtensions(header); + for (let i = 0; i < offers.length; i++) { + const offer = offers[i]; + if (offer.name !== 'permessage-deflate') continue; + const seen = new SafeSet(); + let ok = true; + let serverNoContextTakeover = false; + let clientNoContextTakeover = false; + let serverMaxWindowBits; + for (let j = 0; j < offer.params.length && ok; j++) { + const { 0: key, 1: value } = offer.params[j]; + if (seen.has(key)) { + ok = false; + break; + } + seen.add(key); + switch (key) { + case 'server_no_context_takeover': + if (value !== true) ok = false; + serverNoContextTakeover = true; + break; + case 'client_no_context_takeover': + if (value !== true) ok = false; + clientNoContextTakeover = true; + break; + case 'server_max_window_bits': + // The zlib library cannot produce a raw deflate stream with an 8-bit + // window. + if (value === true || + RegExpPrototypeExec(kWindowBitsRegExp, value) === null || + value === '8') { + ok = false; + } else { + serverMaxWindowBits = +value; + } + break; + case 'client_max_window_bits': + if (value !== true && + RegExpPrototypeExec(kWindowBitsRegExp, value) === null) { + ok = false; + } + break; + default: + ok = false; + } + } + if (!ok) continue; + let response = 'permessage-deflate'; + if (serverNoContextTakeover) response += '; server_no_context_takeover'; + if (clientNoContextTakeover) response += '; client_no_context_takeover'; + if (serverMaxWindowBits !== undefined) { + response += `; server_max_window_bits=${serverMaxWindowBits}`; + } + return { + __proto__: null, + response, + serverNoContextTakeover, + windowBits: serverMaxWindowBits ?? 15, + }; + } + return null; +} + +// ============================================================================= +// Framing (RFC 6455 Section 5) +// ============================================================================= + +function encodeFrameHeader(opcode, length, rsv1) { + let header; + if (length <= 125) { + header = new Uint8Array(2); + header[1] = length; + } else if (length <= 0xffff) { + header = new Uint8Array(4); + header[1] = 126; + header[2] = length >>> 8; + header[3] = length & 0xff; + } else { + header = new Uint8Array(10); + header[1] = 127; + const high = MathFloor(length / 0x100000000); + const low = length >>> 0; + header[2] = high >>> 24; + header[3] = (high >>> 16) & 0xff; + header[4] = (high >>> 8) & 0xff; + header[5] = high & 0xff; + header[6] = low >>> 24; + header[7] = (low >>> 16) & 0xff; + header[8] = (low >>> 8) & 0xff; + header[9] = low & 0xff; + } + header[0] = 0x80 | (rsv1 ? 0x40 : 0) | opcode; + return header; +} + +function encodeClosePayload(code, reason) { + if (code === null) return kEmpty; + const reasonBytes = encodeUtf8(reason); + const payload = new Uint8Array(2 + reasonBytes.length); + payload[0] = code >>> 8; + payload[1] = code & 0xff; + TypedArrayPrototypeSet(payload, reasonBytes, 2); + return payload; +} + +class WebSocketFailure { + constructor(code, message) { + this.code = code; + this.message = message; + } +} + +// ============================================================================= +// Connection internals +// ============================================================================= + +class WebSocketConnection { + exchange; + stream; + http2; + // Push mode: messages are passed to onMessage as they arrive. Pull mode: + // they are queued for nextMessage(), with transport backpressure. + pull; + onMessage = null; + onClosing = null; + onClean = null; + onAbnormal = null; + + closeSent = false; + closeReceived = false; + terminated = false; + clean = false; + error = undefined; + peerCode = 1005; + peerReason = ''; + bufferedAmount = 0; + closed = PromiseWithResolvers(); + + #chunks = []; + #buffered = 0; + #frame = null; + #message = null; + #suspended = false; + #queue = []; + #queuedBytes = 0; + #paused = false; + #waiters = []; + #drains = []; + #sendChain = PromiseResolve(); + #deflate; + #deflater = null; + #inflater = null; + #pingInterval; + #pingTimer = null; + #idleTimeout; + #idleTimer = null; + #closeTimeout; + #closeTimer = null; + + constructor(exchange, transport, { pull, deflate, pingInterval }) { + this.exchange = exchange; + this.stream = transport.stream; + this.http2 = transport.http2; + this.pull = pull; + this.#deflate = deflate; + const timeouts = exchange.listener?.options?.timeouts ?? {}; + this.#pingInterval = pingInterval ?? 0; + this.#idleTimeout = timeouts.tunnelIdle ?? 0; + this.#closeTimeout = timeouts.closeHandshake ?? + kDefaultCloseHandshakeTimeout; + markHandled(this.closed.promise); + + const stream = this.stream; + stream.on('error', () => {}); + stream.on('drain', () => this.#wakeDrains()); + stream.on('data', (chunk) => this.#onData(chunk)); + stream.on('end', () => this.#onEnd()); + stream.on('close', () => this.#onClose()); + this.#armPing(); + this.#armIdle(); + } + + // Timers. + + #armPing() { + if (this.#pingInterval === 0 || this.closeSent || this.terminated) return; + if (this.#pingTimer === null) { + this.#pingTimer = setTimeout(() => { + this.#pingTimer = null; + if (this.closeSent || this.terminated) return; + this.#writeFrame(kOpPing, kEmpty, false); + this.#armPing(); + }, this.#pingInterval); + this.#pingTimer.unref(); + } else { + this.#pingTimer.refresh(); + } + } + + #armIdle() { + if (this.#idleTimeout === 0 || this.terminated) return; + if (this.#idleTimer === null) { + this.#idleTimer = setTimeout(() => { + this.#idleTimer = null; + this.#timeout('The WebSocket connection was idle for too long'); + }, this.#idleTimeout); + this.#idleTimer.unref(); + } else { + this.#idleTimer.refresh(); + } + } + + #clearTimers() { + if (this.#pingTimer !== null) clearTimeout(this.#pingTimer); + if (this.#idleTimer !== null) clearTimeout(this.#idleTimer); + if (this.#closeTimer !== null) clearTimeout(this.#closeTimer); + this.#pingTimer = this.#idleTimer = this.#closeTimer = null; + } + + #timeout(message) { + if (this.terminated) return; + this.#resetTransport(kRequestCancelled); + this.#terminate(createProtocolError(kTimeout, message), true); + } + + // Inbound. + + #onData(chunk) { + if (this.terminated || this.closeReceived) return; + this.#armPing(); + this.#armIdle(); + ArrayPrototypePush(this.#chunks, chunk); + this.#buffered += chunk.length; + this.#process(); + } + + #onEnd() { + if (this.terminated || this.closeReceived) return; + // The peer closed its direction without a closing handshake. + this.#closeTransport(); + this.#terminate(createProtocolError( + kConnectError, + 'The connection was closed before the closing handshake'), true); + } + + #onClose() { + if (this.terminated) return; + if (this.closeSent && this.closeReceived) { + this.#finishClean(); + return; + } + let reason; + const rstCode = this.http2 ? this.stream.rstCode : undefined; + if (rstCode !== undefined && rstCode !== 0) { + reason = protocolErrorFromH2(rstCode, 'h2'); + } else { + reason = createProtocolError(kConnectionReset, 'The connection was lost'); + } + this.#terminate(reason, true); + } + + #consume(length) { + const out = new Uint8Array(length); + let offset = 0; + while (offset < length) { + const chunk = this.#chunks[0]; + const take = length - offset; + if (chunk.length <= take) { + TypedArrayPrototypeSet(out, chunk, offset); + offset += chunk.length; + ArrayPrototypeShift(this.#chunks); + } else { + TypedArrayPrototypeSet(out, TypedArrayPrototypeSubarray(chunk, 0, take), + offset); + this.#chunks[0] = TypedArrayPrototypeSubarray(chunk, take); + offset = length; + } + } + this.#buffered -= length; + return out; + } + + #peek(length) { + const first = this.#chunks[0]; + if (first.length >= length) return first; + return concatBytes(this.#chunks, this.#buffered); + } + + #parseHeader() { + if (this.#buffered < 2) return null; + const start = this.#peek(2); + const b0 = start[0]; + const b1 = start[1]; + const lengthCode = b1 & 0x7f; + const masked = (b1 & 0x80) !== 0; + const needed = 2 + (lengthCode === 126 ? 2 : lengthCode === 127 ? 8 : 0) + + (masked ? 4 : 0); + if (this.#buffered < needed) return null; + const header = this.#consume(needed); + const fin = (b0 & 0x80) !== 0; + const rsv1 = (b0 & 0x40) !== 0; + const opcode = b0 & 0x0f; + let length = lengthCode; + let offset = 2; + if (lengthCode === 126) { + length = (header[2] << 8) | header[3]; + offset = 4; + } else if (lengthCode === 127) { + const high = ((header[2] << 24) | (header[3] << 16) | + (header[4] << 8) | header[5]) >>> 0; + const low = ((header[6] << 24) | (header[7] << 16) | + (header[8] << 8) | header[9]) >>> 0; + if (high > 0x1fffff) { + throw new WebSocketFailure(1009, 'The frame is too large'); + } + length = high * 0x100000000 + low; + offset = 10; + } + if ((b0 & 0x30) !== 0) { + throw new WebSocketFailure(1002, 'RSV2 and RSV3 must be clear'); + } + if (!masked) { + throw new WebSocketFailure(1002, 'Client frames must be masked'); + } + if (opcode >= 0x8) { + if (opcode !== kOpClose && opcode !== kOpPing && opcode !== kOpPong) { + throw new WebSocketFailure(1002, `Unknown opcode ${opcode}`); + } + if (!fin || rsv1 || length > 125) { + throw new WebSocketFailure(1002, 'Invalid control frame'); + } + } else if (opcode === kOpContinuation) { + if (this.#message === null) { + throw new WebSocketFailure(1002, 'Unexpected continuation frame'); + } + if (rsv1) throw new WebSocketFailure(1002, 'RSV1 must be clear'); + } else if (opcode === kOpText || opcode === kOpBinary) { + if (this.#message !== null) { + throw new WebSocketFailure(1002, 'Expected a continuation frame'); + } + if (rsv1 && this.#deflate === null) { + throw new WebSocketFailure(1002, 'RSV1 must be clear'); + } + } else { + throw new WebSocketFailure(1002, `Unknown opcode ${opcode}`); + } + if (opcode < 0x8) { + const total = (this.#message?.length ?? 0) + length; + if (total > kMaxMessageSize) { + throw new WebSocketFailure(1009, 'The message is too large'); + } + } + return { + __proto__: null, + fin, + rsv1, + opcode, + length, + mask: TypedArrayPrototypeSubarray(header, offset, offset + 4), + }; + } + + #process() { + try { + while (!this.#suspended && !this.terminated && !this.closeReceived) { + if (this.#frame === null) { + this.#frame = this.#parseHeader(); + if (this.#frame === null) break; + } + const frame = this.#frame; + if (this.#buffered < frame.length) break; + this.#frame = null; + const payload = frame.length === 0 ? kEmpty : this.#consume(frame.length); + const mask = frame.mask; + for (let i = 0; i < payload.length; i++) payload[i] ^= mask[i & 3]; + this.#handleFrame(frame, payload); + } + } catch (err) { + if (err instanceof WebSocketFailure) { + this.fail(err.code, err.message); + return; + } + throw err; + } + } + + #handleFrame(frame, payload) { + switch (frame.opcode) { + case kOpPing: + if (!this.closeSent) this.#writeFrame(kOpPong, payload, false); + return; + case kOpPong: + return; + case kOpClose: + this.#handlePeerClose(payload); + return; + default: + break; + } + if (frame.opcode !== kOpContinuation) { + this.#message = { + __proto__: null, + text: frame.opcode === kOpText, + compressed: frame.rsv1, + fragments: [], + length: 0, + }; + } + const message = this.#message; + ArrayPrototypePush(message.fragments, payload); + message.length += payload.length; + if (!frame.fin) return; + this.#message = null; + const data = concatBytes(message.fragments, message.length); + if (!message.compressed) { + this.#finishMessage(message.text, data); + return; + } + this.#suspended = true; + PromisePrototypeThen(this.#inflate(data), (inflated) => { + this.#suspended = false; + if (this.terminated) return; + try { + this.#finishMessage(message.text, inflated); + } catch (err) { + this.fail(err.code, err.message); + return; + } + this.#process(); + }, (err) => { + this.#suspended = false; + if (err instanceof WebSocketFailure) this.fail(err.code, err.message); + else this.fail(1007, 'Invalid compressed data'); + }); + } + + #finishMessage(text, bytes) { + let data = bytes; + if (text) { + try { + data = decodeUtf8(bytes); + } catch { + throw new WebSocketFailure(1007, 'Invalid UTF-8 in a text message'); + } + } + if (!this.pull) { + this.onMessage(data); + return; + } + ArrayPrototypePush(this.#queue, data); + this.#queuedBytes += bytes.length; + if (this.#queuedBytes > kReadHighWaterMark && !this.#paused) { + this.#paused = true; + this.stream.pause(); + } + this.#wakeWaiters(); + } + + #handlePeerClose(payload) { + if (payload.length === 1) { + throw new WebSocketFailure(1002, 'Invalid Close frame'); + } + let code = 1005; + let reason = ''; + if (payload.length >= 2) { + code = (payload[0] << 8) | payload[1]; + if (!isReceivableCloseCode(code)) { + throw new WebSocketFailure(1002, `Invalid close code ${code}`); + } + try { + reason = decodeUtf8(TypedArrayPrototypeSubarray(payload, 2)); + } catch { + throw new WebSocketFailure(1007, 'Invalid UTF-8 in a close reason'); + } + } + this.closeReceived = true; + this.peerCode = code; + this.peerReason = reason; + this.#message = null; + this.#chunks = []; + this.#buffered = 0; + if (this.#paused) { + this.#paused = false; + this.stream.resume(); + } + this.onClosing?.(); + this.#wakeWaiters(); + if (!this.closeSent) { + // Echo the status code (RFC 6455 Section 5.5.1). + this.#sendClose(code === 1005 ? null : code, ''); + } else { + this.#finishHandshake(); + } + } + + /** + * Returns the next message for pull mode: a string, a Uint8Array, or null + * once the peer has started the closing handshake and every message has + * been read. + * @returns {Promise} + */ + async nextMessage() { + for (;;) { + if (this.terminated && !this.clean) throw this.error; + if (this.#queue.length > 0) { + const message = ArrayPrototypeShift(this.#queue); + this.#queuedBytes -= typeof message === 'string' ? + Buffer.byteLength(message) : message.length; + if (this.#paused && this.#queuedBytes <= kReadHighWaterMark / 2) { + this.#paused = false; + this.stream.resume(); + } + return message; + } + if (this.closeReceived || this.terminated) return null; + const { promise, resolve } = PromiseWithResolvers(); + ArrayPrototypePush(this.#waiters, resolve); + await promise; + } + } + + #wakeWaiters() { + const waiters = this.#waiters; + this.#waiters = []; + for (let i = 0; i < waiters.length; i++) waiters[i](); + } + + #inflate(data) { + this.#inflater ??= require('zlib').createInflateRaw({ + maxOutputLength: kMaxMessageSize, + }); + const inflater = this.#inflater; + return new Promise((resolve, reject) => { + const chunks = []; + let length = 0; + const onData = (chunk) => { + length += chunk.length; + if (length > kMaxMessageSize) { + cleanup(); + reject(new WebSocketFailure(1009, 'The message is too large')); + return; + } + ArrayPrototypePush(chunks, chunk); + }; + const onError = (err) => { + cleanup(); + reject(err); + }; + const cleanup = () => { + inflater.removeListener('data', onData); + inflater.removeListener('error', onError); + }; + inflater.on('data', onData); + inflater.on('error', onError); + inflater.write(data); + inflater.write(kDeflateTrailer); + inflater.flush(() => { + cleanup(); + if (length <= kMaxMessageSize) resolve(concatBytes(chunks, length)); + }); + }); + } + + #deflateMessage(data) { + const options = this.#deflate; + this.#deflater ??= require('zlib').createDeflateRaw({ + windowBits: options.windowBits, + }); + const deflater = this.#deflater; + const { Z_SYNC_FLUSH } = require('zlib').constants; + return new Promise((resolve, reject) => { + const chunks = []; + let length = 0; + const onData = (chunk) => { + ArrayPrototypePush(chunks, chunk); + length += chunk.length; + }; + const onError = (err) => { + deflater.removeListener('data', onData); + reject(err); + }; + deflater.on('data', onData); + deflater.once('error', onError); + deflater.write(data); + deflater.flush(Z_SYNC_FLUSH, () => { + deflater.removeListener('data', onData); + deflater.removeListener('error', onError); + let out = concatBytes(chunks, length); + if (length >= 4) out = TypedArrayPrototypeSubarray(out, 0, length - 4); + if (out.length === 0) out = new Uint8Array(1); + if (options.serverNoContextTakeover) deflater.reset(); + resolve(out); + }); + }); + } + + // Outbound. + + #writeFrame(opcode, payload, rsv1, callback) { + const header = encodeFrameHeader(opcode, payload.length, rsv1); + this.#armIdle(); + if (payload.length === 0) { + return this.stream.write(header, callback); + } + if (payload.length <= 16384) { + const frame = new Uint8Array(header.length + payload.length); + TypedArrayPrototypeSet(frame, header, 0); + TypedArrayPrototypeSet(frame, payload, header.length); + return this.stream.write(frame, callback); + } + this.stream.write(header); + return this.stream.write(payload, callback); + } + + #wakeDrains() { + const drains = this.#drains; + this.#drains = []; + for (let i = 0; i < drains.length; i++) drains[i](); + } + + #checkSendable() { + if (this.terminated && !this.clean) throw this.error; + if (this.closeSent || this.terminated) { + throw invalidState('The WebSocket closing handshake has started'); + } + } + + /** + * Sends a message. The returned promise resolves once the message has been + * handed to the transport, waiting while the transport is not accepting + * data. + * @param {boolean} text + * @param {Uint8Array|Promise} bytes + * @param {Function} [onWritten] + * @returns {Promise} + */ + sendMessage(text, bytes, onWritten) { + try { + this.#checkSendable(); + } catch (err) { + return PromiseReject(err); + } + const send = async () => { + let data = await bytes; + this.#checkSendable(); + while (this.stream.writableNeedDrain) { + const { promise, resolve } = PromiseWithResolvers(); + ArrayPrototypePush(this.#drains, resolve); + await promise; + this.#checkSendable(); + } + let rsv1 = false; + if (this.#deflate !== null && data.length >= kCompressionThreshold) { + data = await this.#deflateMessage(data); + rsv1 = true; + this.#checkSendable(); + } + this.#writeFrame(text ? kOpText : kOpBinary, data, rsv1, onWritten); + }; + const result = PromisePrototypeThen(this.#sendChain, send); + this.#sendChain = PromisePrototypeThen(result, undefined, () => {}); + return result; + } + + #sendClose(code, reason) { + if (this.closeSent || this.terminated) return; + this.closeSent = true; + if (this.#pingTimer !== null) { + clearTimeout(this.#pingTimer); + this.#pingTimer = null; + } + this.#writeFrame(kOpClose, encodeClosePayload(code, reason), false); + // Messages that are not yet sent are discarded (Section 13.3). + this.#wakeDrains(); + if (this.closeReceived) { + this.#finishHandshake(); + } else if (this.#closeTimeout !== 0) { + this.#closeTimer = setTimeout(() => { + this.#closeTimer = null; + this.#timeout('The peer did not complete the closing handshake'); + }, this.#closeTimeout); + this.#closeTimer.unref(); + } + } + + /** + * The close() steps of Section 13.3, after validation. + * @param {number|null} code + * @param {string} reason + */ + close(code, reason) { + validateServerClose(code, reason); + if (this.closeSent || this.terminated) return; + if (reason !== '' && code === null) code = 1000; + this.#sendClose(code, reason); + } + + /** + * The close() steps of a WebSocket: messages queued by send() before the + * call are still sent (WebSockets Standard). + * @param {number|null} code + * @param {string} reason + */ + closeAfterQueued(code, reason) { + validateServerClose(code, reason); + if (this.closeSent || this.terminated) return; + if (reason !== '' && code === null) code = 1000; + PromisePrototypeThen(this.#sendChain, () => this.#sendClose(code, reason)); + } + + // Starts the closing handshake for a cancel or abort reason (Section 13.3). + closeWithReason(reason) { + let info; + try { + info = convertCloseInfo(reason); + validateServerClose(info.closeCode, info.reason); + } catch { + info = { __proto__: null, closeCode: null, reason: '' }; + } + if (this.closeSent || this.terminated) return; + let code = info.closeCode; + if (info.reason !== '' && code === null) code = 1000; + this.#sendClose(code, info.reason); + } + + windDown() { + if (this.closeSent || this.terminated) return; + this.#sendClose(1001, ''); + } + + #finishHandshake() { + if (this.#closeTimer !== null) { + clearTimeout(this.#closeTimer); + this.#closeTimer = null; + } + this.#closeTransport(); + } + + // Closes the transport in an orderly way: the server closes the TCP + // connection first (RFC 6455 Section 7.1.1); on HTTP/2 it ends the stream. + #closeTransport() { + const stream = this.stream; + if (stream.destroyed) return; + if (!this.http2) { + stream.destroySoon(); + return; + } + stream.end(() => { + if (!stream.destroyed && !stream._readableState.ended) { + setImmediate(() => { + if (!stream.destroyed) stream.close(kH2Codes.ERR_HTTP_REQUEST_BODY_REJECTED); + }); + } + }); + } + + #resetTransport(code) { + const stream = this.stream; + if (stream.destroyed) return; + if (this.http2) { + stream.close(kH2Codes[code] ?? kH2Codes[kRequestCancelled]); + } else { + stream.destroy(); + } + } + + /** + * Fails the WebSocket connection (RFC 6455 Section 7.1.7). + * @param {number} code + * @param {string} message + */ + fail(code, message) { + if (this.terminated) return; + if (!this.closeSent) { + this.closeSent = true; + this.#writeFrame(kOpClose, encodeClosePayload(code, ''), false); + } + this.#closeTransport(); + this.#terminate(createProtocolError(kConnectError, message), true); + } + + destroy(error) { + if (this.terminated) return; + const code = getErrorCode(error, kTunnelSendable) ?? kRequestCancelled; + this.#resetTransport(code); + this.#terminate(error ?? createProtocolError( + kRequestCancelled, 'The WebSocket connection was destroyed'), false); + } + + #finishClean() { + if (this.terminated) return; + this.terminated = true; + this.clean = true; + this.#clearTimers(); + this.#wakeWaiters(); + this.#wakeDrains(); + const info = { __proto__: null, closeCode: this.peerCode, + reason: this.peerReason }; + this.closed.resolve(info); + this.onClean?.(info); + this.exchange.complete(); + } + + /** + * Terminates the connection abnormally. + * @param {any} error + * @param {boolean} aborted Whether the exchange is aborted, rather than + * terminated by the server. + */ + #terminate(error, aborted) { + if (this.terminated) return; + this.terminated = true; + this.error = error; + this.#clearTimers(); + this.#wakeWaiters(); + this.#wakeDrains(); + this.closed.reject(error); + this.onAbnormal?.(error); + if (aborted) this.exchange.abort(error); + else this.exchange.complete(); + } +} + +// ============================================================================= +// Establishing the connection (Section 11.4) +// ============================================================================= + +/** + * Performs the WebSocket server handshake. + * @param {import('./exchange').Exchange} exchange + * @param {object} options Converted WebSocketUpgradeInit. + * @param {boolean} pull + * @returns {{ connection: WebSocketConnection, protocol: string, + * extensions: string }} + */ +function performHandshake(exchange, options, pull) { + const list = exchange.backend.headerList; + const offered = []; + const protocolFields = listGetAll(list, 'sec-websocket-protocol'); + for (let i = 0; i < protocolFields.length; i++) { + const items = splitList(protocolFields[i]); + for (let j = 0; j < items.length; j++) ArrayPrototypePush(offered, items[j]); + } + let protocol = ''; + if (options.protocol !== undefined) { + for (let i = 0; i < options.protocol.length && protocol === ''; i++) { + for (let j = 0; j < offered.length; j++) { + if (offered[j] === options.protocol[i]) { + protocol = offered[j]; + break; + } + } + } + } + let deflate = null; + if (options.compression === true) { + deflate = negotiateDeflate(listGet(list, 'sec-websocket-extensions')); + } + const extensions = deflate?.response ?? ''; + + const fields = []; + if (exchange.kind === 'upgrade') { + const key = StringPrototypeTrim(listGet(list, 'sec-websocket-key')); + ArrayPrototypePush(fields, ['Sec-WebSocket-Accept', computeAccept(key)]); + } + if (protocol !== '') { + ArrayPrototypePush(fields, ['Sec-WebSocket-Protocol', protocol]); + } + if (extensions !== '') { + ArrayPrototypePush(fields, ['Sec-WebSocket-Extensions', extensions]); + } + + exchange.commit(); + exchange.isWebSocket = true; + const transport = exchange.backend.acceptTunnel(exchange, fields, 'websocket'); + const connection = new WebSocketConnection(exchange, transport, { + pull, deflate, pingInterval: options.pingInterval, + }); + exchange.tunnel = connection; + return { connection, protocol, extensions }; +} + +function toBinary(chunk) { + if (isArrayBuffer(chunk)) { + return TypedArrayPrototypeSlice(new Uint8Array(chunk)); + } + if (isArrayBufferView(chunk)) { + return TypedArrayPrototypeSlice(new Uint8Array( + chunk.buffer, chunk.byteOffset, chunk.byteLength)); + } + return null; +} + +// ============================================================================= +// ServerWebSocketStream (Section 13) +// ============================================================================= + +class ServerWebSocketStream { + #connection = null; + #opened; + #closed; + #readable = null; + #writable = null; + + constructor(key = undefined, exchange = undefined, options = undefined) { + if (key !== kConstruct) throw new ERR_ILLEGAL_CONSTRUCTOR(); + if (exchange.state === 'aborted') { + this.#opened = PromiseReject(exchange.abortReason); + markHandled(this.#opened); + this.#closed = PromiseReject(exchange.abortReason); + markHandled(this.#closed); + return; + } + const { connection, protocol, extensions } = + performHandshake(exchange, options, true); + this.#connection = connection; + this.#closed = connection.closed.promise; + + const { ReadableStream } = require('internal/webstreams/readablestream'); + const { WritableStream } = require('internal/webstreams/writablestream'); + let readableController; + let writableController; + this.#readable = new ReadableStream({ + start(controller) { + readableController = controller; + }, + async pull(controller) { + const message = await connection.nextMessage(); + if (message === null) controller.close(); + else controller.enqueue(message); + }, + cancel(reason) { + connection.closeWithReason(reason); + }, + }, { highWaterMark: 0 }); + this.#writable = new WritableStream({ + start(controller) { + writableController = controller; + }, + write(chunk) { + const binary = toBinary(chunk); + if (binary !== null) return connection.sendMessage(false, binary); + if (typeof chunk === 'symbol') { + throw new ERR_INVALID_ARG_TYPE('chunk', 'string', chunk); + } + return connection.sendMessage( + true, encodeUtf8(StringPrototypeToWellFormed(`${chunk}`))); + }, + close() { + connection.close(null, ''); + }, + abort(reason) { + connection.closeWithReason(reason); + }, + }); + connection.onClean = () => { + writableController.error(invalidState('The WebSocket connection is closed')); + }; + connection.onAbnormal = (error) => { + readableController.error(error); + writableController.error(error); + }; + this.#opened = PromiseResolve({ + __proto__: null, + readable: this.#readable, + writable: this.#writable, + extensions, + protocol, + }); + } + + #check() { + if (!(#opened in this)) { + throw new ERR_INVALID_THIS('ServerWebSocketStream'); + } + } + + get opened() { + this.#check(); + return this.#opened; + } + + get closed() { + this.#check(); + return this.#closed; + } + + close(closeInfo = undefined) { + this.#check(); + const { closeCode, reason } = convertCloseInfo(closeInfo); + validateServerClose(closeCode, reason); + this.#connection?.close(closeCode, reason); + } + + destroy(error = undefined) { + this.#check(); + this.#connection?.destroy(error); + } + + [SymbolAsyncDispose]() { + this.#check(); + const connection = this.#connection; + if (connection === null || connection.terminated) return PromiseResolve(); + connection.close(null, ''); + return PromisePrototypeThen(connection.closed.promise, () => {}); + } +} + +ObjectDefineProperties(ServerWebSocketStream.prototype, { + opened: kEnumerableProperty, + closed: kEnumerableProperty, + close: kEnumerableProperty, + destroy: kEnumerableProperty, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'ServerWebSocketStream', + }, +}); + +// ============================================================================= +// WebSocket (Section 11.4) +// ============================================================================= + +const kConnecting = 0; +const kOpen = 1; +const kClosing = 2; +const kClosed = 3; + +let lazyUndici; +function undici() { + lazyUndici ??= require('internal/deps/undici/undici'); + return lazyUndici; +} + +class WebSocket extends EventTarget { + #connection = null; + #url; + #protocol = ''; + #extensions = ''; + #readyState = kOpen; + #binaryType = 'blob'; + #bufferedAmount = 0; + #origin; + + constructor(key = undefined, exchange = undefined, options = undefined) { + if (key !== kConstruct) throw new ERR_ILLEGAL_CONSTRUCTOR(); + super(); + const url = new URL(exchange.backend.url); + url.protocol = exchange.backend.encrypted ? 'wss:' : 'ws:'; + this.#url = url.href; + this.#origin = url.origin; + if (exchange.state === 'aborted') { + this.#readyState = kClosed; + setImmediate(() => this.#fireAbnormal()); + return; + } + const { connection, protocol, extensions } = + performHandshake(exchange, options, false); + this.#connection = connection; + this.#protocol = protocol; + this.#extensions = extensions; + connection.onMessage = (data) => { + if (typeof data !== 'string') { + data = this.#binaryType === 'arraybuffer' ? data.buffer : + new (require('internal/blob').Blob)([data]); + } + const { MessageEvent } = undici(); + this.dispatchEvent(new MessageEvent('message', { + data, origin: this.#origin, + })); + }; + connection.onClosing = () => { + if (this.#readyState === kOpen) this.#readyState = kClosing; + }; + connection.onClean = (info) => { + this.#readyState = kClosed; + const { CloseEvent } = undici(); + this.dispatchEvent(new CloseEvent('close', { + wasClean: true, code: info.closeCode, reason: info.reason, + })); + }; + connection.onAbnormal = () => { + this.#readyState = kClosed; + // Dispatch after the current operation, as a queued task would. + setImmediate(() => this.#fireAbnormal()); + }; + } + + #fireAbnormal() { + const { CloseEvent } = undici(); + this.dispatchEvent(new Event('error')); + this.dispatchEvent(new CloseEvent('close', { + wasClean: false, code: 1006, reason: '', + })); + } + + #check() { + if (!(#url in this)) throw new ERR_INVALID_THIS('WebSocket'); + } + + get url() { + this.#check(); + return this.#url; + } + + get readyState() { + this.#check(); + return this.#readyState; + } + + get bufferedAmount() { + this.#check(); + return this.#bufferedAmount; + } + + get extensions() { + this.#check(); + return this.#extensions; + } + + get protocol() { + this.#check(); + return this.#protocol; + } + + get binaryType() { + this.#check(); + return this.#binaryType; + } + + set binaryType(value) { + this.#check(); + if (value === 'blob' || value === 'arraybuffer') this.#binaryType = value; + } + + close(code = undefined, reason = undefined) { + this.#check(); + const closeCode = code === undefined ? null : + converters['unsigned short'](code, { __proto__: null, clamp: true }); + const closeReason = reason === undefined ? '' : converters.USVString(reason); + validateServerClose(closeCode, closeReason); + if (this.#readyState === kClosing || this.#readyState === kClosed) return; + this.#readyState = kClosing; + this.#connection.closeAfterQueued(closeCode, closeReason); + } + + send(data) { + this.#check(); + if (arguments.length === 0) { + throw new ERR_INVALID_ARG_TYPE('data', 'string', data); + } + let text = false; + let bytes; + let size; + const { Blob } = require('internal/blob'); + if (data instanceof Blob) { + size = data.size; + bytes = PromisePrototypeThen(data.arrayBuffer(), + (buffer) => new Uint8Array(buffer)); + markHandled(bytes); + } else { + bytes = toBinary(data); + if (bytes === null) { + if (isAnyArrayBuffer(data)) bytes = TypedArrayPrototypeSlice(new Uint8Array(data)); + } + if (bytes === null) { + text = true; + bytes = encodeUtf8(converters.USVString(data)); + } + size = bytes.length; + } + this.#bufferedAmount += size; + if (this.#readyState !== kOpen) return; + const connection = this.#connection; + if (connection.closeSent || connection.terminated) return; + markHandled(connection.sendMessage(text, bytes, () => { + this.#bufferedAmount -= size; + })); + } +} + +const kStateConstants = { + CONNECTING: { __proto__: null, value: kConnecting, enumerable: true }, + OPEN: { __proto__: null, value: kOpen, enumerable: true }, + CLOSING: { __proto__: null, value: kClosing, enumerable: true }, + CLOSED: { __proto__: null, value: kClosed, enumerable: true }, +}; +ObjectDefineProperties(WebSocket, kStateConstants); +ObjectDefineProperties(WebSocket.prototype, { + ...kStateConstants, + url: kEnumerableProperty, + readyState: kEnumerableProperty, + bufferedAmount: kEnumerableProperty, + extensions: kEnumerableProperty, + protocol: kEnumerableProperty, + binaryType: kEnumerableProperty, + close: kEnumerableProperty, + send: kEnumerableProperty, + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'WebSocket', + }, +}); +defineEventHandler(WebSocket.prototype, 'open'); +defineEventHandler(WebSocket.prototype, 'error'); +defineEventHandler(WebSocket.prototype, 'close'); +defineEventHandler(WebSocket.prototype, 'message'); + +// ============================================================================= +// Entry points +// ============================================================================= + +/** + * The upgradeWebSocket() steps after the state checks (Section 11.4). + * @param {import('./exchange').Exchange} exchange + * @param {object} options Converted WebSocketUpgradeInit. + * @returns {WebSocket} + */ +function upgradeWebSocket(exchange, options) { + if (exchange.state !== 'aborted' && !checkHandshake(exchange)) { + throw new ERR_INVALID_ARG_VALUE( + 'request', 'not a valid WebSocket opening handshake'); + } + return new WebSocket(kConstruct, exchange, options); +} + +/** + * The upgradeWebSocketStream() steps after the state checks (Section 11.5). + * @param {import('./exchange').Exchange} exchange + * @param {object} options Converted WebSocketUpgradeInit. + * @returns {ServerWebSocketStream} + */ +function upgradeWebSocketStream(exchange, options) { + if (exchange.state !== 'aborted' && !checkHandshake(exchange)) { + throw new ERR_INVALID_ARG_VALUE( + 'request', 'not a valid WebSocket opening handshake'); + } + return new ServerWebSocketStream(kConstruct, exchange, options); +} + +module.exports = { + ServerWebSocketStream, + WebSocket, + convertUpgradeOptions, + upgradeWebSocket, + upgradeWebSocketStream, +}; diff --git a/src/node_options.cc b/src/node_options.cc index 8c82c0a9f650..94cace7095a1 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -797,6 +797,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { BOOL_FIELD(experimental_sqlite), kAllowedInEnvvar, HAVE_SQLITE); + AddOption("--experimental-http-serve", + "experimental Fetch Server API (http.fetchServer)", + BOOL_FIELD(experimental_http_serve), + kAllowedInEnvvar); AddOption("--experimental-stream-iter", "experimental iterable streams API (node:stream/iter)", BOOL_FIELD(experimental_stream_iter), diff --git a/src/node_options.h b/src/node_options.h index d4d4dfc97a55..ae95da57e9bf 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -223,6 +223,7 @@ class EnvironmentOptions : public Options { DEFINE_BOOL_FIELD(experimental_websocket) = true; DEFINE_BOOL_FIELD(experimental_sqlite) = HAVE_SQLITE; DEFINE_BOOL_FIELD(experimental_stream_iter) = EXPERIMENTALS_DEFAULT_VALUE; + DEFINE_BOOL_FIELD(experimental_http_serve) = false; DEFINE_BOOL_FIELD(experimental_vfs) = true; DEFINE_BOOL_FIELD(vfs_load) = false; DEFINE_BOOL_FIELD(sea_vfs) = false; diff --git a/test/parallel/test-http-fetch-server-connect.js b/test/parallel/test-http-fetch-server-connect.js new file mode 100644 index 000000000000..ace6411b193a --- /dev/null +++ b/test/parallel/test-http-fetch-server-connect.js @@ -0,0 +1,260 @@ +// Flags: --experimental-http-serve --no-warnings +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) common.skip('missing crypto'); + +const assert = require('assert'); +const http = require('http'); +const http2 = require('http2'); +const net = require('net'); +const { once } = require('events'); +const { test } = require('node:test'); +const { serve, ServerResponse } = http.fetchServer; + +const kFallThrough = Symbol.for('wintertc.server.fallthrough'); + +async function echoTunnel(tunnel) { + for await (const batch of tunnel.readable) { + await tunnel.writer.writev(batch); + } + await tunnel.writer.end(); +} + +const handler = { + fetch: common.mustCallAtLeast((ctx) => { + return new ServerResponse(`fetch ${ctx.request.headers.get('upgrade')}`); + }, 0), + connect: common.mustCallAtLeast(async (ctx) => { + const { request } = ctx; + const proto = ctx.connectProtocol; + if (proto === 'fall') { + ctx.deny(kFallThrough); + assert.throws(() => ctx.deny(), { name: 'InvalidStateError' }); + return; + } + if (proto === 'refuse') { + return new ServerResponse('refused', { status: 403 }); + } + if (proto === 'ok200') { + // A 2xx response is valid on an upgrade-originated exchange. + return new ServerResponse('declined'); + } + if (proto === 'connect-udp') { + const tunnel = await ctx.accept(); + const capsules = tunnel.capsules(); + assert.throws(() => tunnel.readable, { name: 'InvalidStateError' }); + assert.throws(() => tunnel.capsules(), { name: 'InvalidStateError' }); + const { readable, writer, unreliable } = tunnel.datagrams(); + assert.strictEqual(unreliable, false); + (async () => { + for await (const batch of readable) { + for (const datagram of batch) await writer.write(datagram); + } + })().then(common.mustCall()); + for await (const capsule of capsules.readable) { + await capsules.send({ type: capsule.type + 1, data: capsule.data }); + } + await tunnel.close(); + return; + } + if (proto === null) { + // Plain CONNECT. + assert.match(request.url, /^http:\/\/(example\.com|denied):\d+\/$/); + await assert.rejects(request.trailers, { name: 'InvalidStateError' }); + await assert.rejects(request[Symbol.asyncIterator]().next(), + { name: 'InvalidStateError' }); + if (request.headers.get('host') === 'denied') { + ctx.deny(); + return; + } + if (request.url.startsWith('http://example.com:8443')) { + return new ServerResponse(null, { status: 200 }); + } + } + const tunnel = await ctx.accept({ headers: { 'x-tunnel': 'yes' } }); + if (proto === null) { + assert.throws(() => tunnel.capsules(), { name: 'InvalidStateError' }); + assert.throws(() => tunnel.datagrams(), { name: 'InvalidStateError' }); + } + await assert.rejects(ctx.accept(), { name: 'InvalidStateError' }); + await echoTunnel(tunnel); + await tunnel.closed; + }), +}; + +async function start() { + const server = serve(handler, { port: 0, hostname: '127.0.0.1' }); + await server.ready; + return { server, port: [...server][0].address.port }; +} + +function readUntil(socket, pattern) { + return new Promise((resolve) => { + let data = ''; + const onData = (chunk) => { + data += chunk.toString('latin1'); + if (pattern.test(data)) { + socket.removeListener('data', onData); + resolve(data); + } + }; + socket.on('data', onData); + }); +} + +function encodeCapsule(type, data) { + return Buffer.concat([Buffer.from([type, data.length]), data]); +} + +test('HTTP/1.1 CONNECT', async () => { + const { server, port } = await start(); + try { + const socket = net.connect(port, '127.0.0.1'); + socket.write('CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n'); + const head = await readUntil(socket, /\r\n\r\n/); + assert.match(head, /^HTTP\/1\.1 200 /); + assert.match(head, /x-tunnel: yes/i); + socket.write('ping'); + assert.strictEqual(await readUntil(socket, /ping/), 'ping'); + socket.end(); + await once(socket, 'close'); + + const denied = net.connect(port, '127.0.0.1'); + denied.write('CONNECT example.com:443 HTTP/1.1\r\nHost: denied\r\n\r\n'); + denied.on('data', common.mustNotCall()); + await once(denied, 'close'); + } finally { + await server.close(); + } +}); + +test('HTTP/1.1 upgrade normalization and fall-through', async () => { + const { server, port } = await start(); + try { + const upgrade = (protocol) => new Promise((resolve, reject) => { + const req = http.request({ + host: '127.0.0.1', port, agent: false, + headers: { 'Connection': 'Upgrade', 'Upgrade': protocol }, + }); + req.on('upgrade', (res, socket, head) => resolve({ res, socket, head })); + req.on('response', (res) => { + let body = ''; + res.setEncoding('utf8'); + res.on('data', (c) => body += c); + res.on('end', () => resolve({ res, body })); + }); + req.on('error', reject); + req.end(); + }); + + const fall = await upgrade('fall'); + assert.strictEqual(fall.res.statusCode, 200); + assert.strictEqual(fall.body, 'fetch fall'); + + const refused = await upgrade('refuse'); + assert.strictEqual(refused.res.statusCode, 403); + + const declined = await upgrade('ok200'); + assert.strictEqual(declined.body, 'declined'); + + const echo = await upgrade('Echo/1, other'); + assert.strictEqual(echo.res.statusCode, 101); + assert.strictEqual(echo.res.headers.upgrade, 'Echo/1'); + echo.socket.write('hello'); + assert.strictEqual(await readUntil(echo.socket, /hello/), 'hello'); + echo.socket.end(); + } finally { + await server.close(); + } +}); + +test('HTTP/2 CONNECT and extended CONNECT', async () => { + const { server, port } = await start(); + const client = http2.connect(`http://127.0.0.1:${port}`); + try { + await once(client, 'remoteSettings'); + assert.strictEqual(client.remoteSettings.enableConnectProtocol, true); + + const plain = client.request({ + ':method': 'CONNECT', ':authority': 'example.com:443', + }); + const [headers] = await once(plain, 'response'); + assert.strictEqual(headers[':status'], 200); + assert.strictEqual(headers['x-tunnel'], 'yes'); + plain.write('abc'); + plain.end(); + let data = ''; + plain.setEncoding('utf8'); + for await (const chunk of plain) data += chunk; + assert.strictEqual(data, 'abc'); + + // A 2xx ServerResponse cannot establish a tunnel: 502. + const invalid = client.request({ + ':method': 'CONNECT', ':authority': 'example.com:8443', + }); + // The server refuses the rest of the request with RST_STREAM(NO_ERROR), + // which can arrive together with the response headers. + invalid.on('error', () => {}); + const invalidHeaders = await new Promise((resolve) => { + invalid.on('response', resolve); + }); + assert.strictEqual(invalidHeaders[':status'], 502); + invalid.resume(); + if (!invalid.closed) await once(invalid, 'close'); + + const udp = client.request({ + ':method': 'CONNECT', ':protocol': 'connect-udp', ':scheme': 'https', + ':path': '/.well-known/masque/udp/192.0.2.1/443/', + ':authority': 'example.com', + }); + const [udpHeaders] = await once(udp, 'response'); + assert.strictEqual(udpHeaders[':status'], 200); + assert.strictEqual(udpHeaders['capsule-protocol'], '?1'); + udp.write(encodeCapsule(0x20, Buffer.from('cap'))); + udp.write(encodeCapsule(0x00, Buffer.from('\x00dgram'))); + udp.end(); + const received = []; + for await (const chunk of udp) received.push(chunk); + const all = Buffer.concat(received); + // The capsule is echoed with type + 1, and the datagram as a datagram. + assert.ok(all.includes(encodeCapsule(0x21, Buffer.from('cap')))); + assert.ok(all.includes(encodeCapsule(0x00, Buffer.from('\x00dgram')))); + } finally { + client.close(); + await server.close(); + } +}); + +test('CONNECT without a connect handler', async () => { + const server = serve({ + fetch: common.mustCall((ctx) => { + return new ServerResponse(`fetched ${ctx.request.headers.get('upgrade')}`); + }), + }, { port: 0, hostname: '127.0.0.1' }); + await server.ready; + const port = [...server][0].address.port; + const socket = net.connect(port, '127.0.0.1'); + socket.write('CONNECT example.com:443 HTTP/1.1\r\nHost: example.com\r\n\r\n'); + assert.match(await readUntil(socket, /\r\n\r\n/), /^HTTP\/1\.1 501 /); + socket.destroy(); + + // Without connect(), an upgrade offer is ignored and routed to fetch(). + const body = await new Promise((resolve, reject) => { + const req = http.request({ + host: '127.0.0.1', port, agent: false, + headers: { 'Connection': 'Upgrade', 'Upgrade': 'websocket' }, + }, common.mustCall((res) => { + assert.strictEqual(res.statusCode, 200); + let data = ''; + res.setEncoding('utf8'); + res.on('data', (c) => data += c); + res.on('end', () => resolve(data)); + })); + req.on('upgrade', common.mustNotCall()); + req.on('error', reject); + req.end(); + }); + assert.strictEqual(body, 'fetched websocket'); + await server.close(); +}); diff --git a/test/parallel/test-http-fetch-server-response.js b/test/parallel/test-http-fetch-server-response.js new file mode 100644 index 000000000000..eb7a4946f6c8 --- /dev/null +++ b/test/parallel/test-http-fetch-server-response.js @@ -0,0 +1,221 @@ +// Flags: --experimental-http-serve --no-warnings +'use strict'; + +require('../common'); +const assert = require('assert'); +const { test } = require('node:test'); +const { ServerResponse } = require('http').fetchServer; + +async function text(iterable) { + const decoder = new TextDecoder(); + let result = ''; + for await (const chunks of iterable) { + for (const chunk of chunks) { + assert.ok(chunk instanceof Uint8Array); + result += decoder.decode(chunk, { stream: true }); + } + } + return result + decoder.decode(); +} + +test('fetchServer requires the flag', () => { + const { spawnSync } = require('child_process'); + const child = spawnSync(process.execPath, [ + '-p', 'require("http").fetchServer === undefined', + ]); + assert.strictEqual(child.stdout.toString().trim(), 'true'); +}); + +test('defaults', async () => { + const response = new ServerResponse(); + assert.strictEqual(response.type, 'default'); + assert.strictEqual(response.status, 200); + assert.strictEqual(response.ok, true); + assert.strictEqual(response.statusText, ''); + assert.strictEqual(response.url, ''); + assert.strictEqual(response.redirected, false); + assert.strictEqual(response.headers, response.headers); + assert.strictEqual(await text(response), ''); + const trailers = await response.trailers; + assert.deepStrictEqual([...trailers], []); + assert.strictEqual(response.trailers, response.trailers); + assert.strictEqual(Object.prototype.toString.call(response), + '[object ServerResponse]'); +}); + +test('string body and content type', async () => { + const response = new ServerResponse('hello', { + status: 201, + statusText: 'Created', + headers: [['x-a', '1'], ['X-A', '2']], + }); + assert.strictEqual(response.status, 201); + assert.strictEqual(response.statusText, 'Created'); + assert.strictEqual(response.headers.get('x-a'), '1, 2'); + assert.strictEqual(response.headers.get('content-type'), + 'text/plain;charset=UTF-8'); + assert.strictEqual(await text(response), 'hello'); +}); + +test('body types', async () => { + assert.strictEqual(await text(new ServerResponse(new Uint8Array([104, 105]))), 'hi'); + assert.strictEqual(await text(new ServerResponse(new Uint8Array([104, 105]).buffer)), 'hi'); + assert.strictEqual(await text(new ServerResponse(new Blob(['blob']))), 'blob'); + const params = new ServerResponse(new URLSearchParams({ a: 'b c' })); + assert.strictEqual(params.headers.get('content-type'), + 'application/x-www-form-urlencoded;charset=UTF-8'); + assert.strictEqual(await text(params), 'a=b+c'); + const rs = new ReadableStream({ + start(c) { + c.enqueue(new TextEncoder().encode('rs')); + c.close(); + }, + }); + assert.strictEqual(await text(new ServerResponse(rs)), 'rs'); + async function* gen() { + yield [new TextEncoder().encode('a'), new TextEncoder().encode('b')]; + yield []; + yield [new TextEncoder().encode('c')]; + } + assert.strictEqual(await text(new ServerResponse(gen())), 'abc'); + // A sync iterable of batches is accepted as an async sequence. + assert.strictEqual(await text(new ServerResponse([[new Uint8Array([120])]])), 'x'); + // A ServerResponse can be the body of another. + assert.strictEqual(await text(new ServerResponse(new ServerResponse('nested'))), + 'nested'); + const form = new FormData(); + form.append('a', 'b'); + const formResponse = new ServerResponse(form); + assert.match(formResponse.headers.get('content-type'), + /^multipart\/form-data; boundary=/); + assert.match(await text(formResponse), /name="a"/); +}); + +test('yielding a bare chunk fails when read', async () => { + async function* gen() { + yield new Uint8Array([1]); + } + await assert.rejects(text(new ServerResponse(gen())), { name: 'TypeError' }); +}); + +test('validation', () => { + assert.throws(() => new ServerResponse(null, { status: 199 }), RangeError); + assert.throws(() => new ServerResponse(null, { status: 600 }), RangeError); + assert.throws(() => new ServerResponse(null, { status: 70000 }), TypeError); + assert.throws(() => new ServerResponse('x', { status: 204 }), TypeError); + assert.throws(() => new ServerResponse(null, { statusText: 'a\nb' }), TypeError); + assert.throws(() => new ServerResponse(null, { headers: { 'a b': 'c' } }), + TypeError); + assert.throws(() => new ServerResponse(null, { headers: { a: 'b\r\nc' } }), + TypeError); + assert.throws(() => new ServerResponse(null, { headers: [['a']] }), TypeError); + const locked = new ReadableStream(); + locked.getReader(); + assert.throws(() => new ServerResponse(locked), TypeError); +}); + +test('static methods', async () => { + const error = ServerResponse.error(); + assert.strictEqual(error.type, 'error'); + assert.strictEqual(error.status, 0); + error.headers.set('x', 'y'); + + const redirect = ServerResponse.redirect('/relative?a=b', 307); + assert.strictEqual(redirect.status, 307); + assert.strictEqual(redirect.headers.get('location'), '/relative?a=b'); + assert.strictEqual(ServerResponse.redirect('/x').status, 302); + assert.throws(() => ServerResponse.redirect('/a b'), TypeError); + assert.throws(() => ServerResponse.redirect('/', 200), RangeError); + + const json = ServerResponse.json({ a: 1 }, { status: 202 }); + assert.strictEqual(json.status, 202); + assert.strictEqual(json.headers.get('content-type'), 'application/json'); + assert.strictEqual(await text(json), '{"a":1}'); + assert.throws(() => ServerResponse.json(undefined), TypeError); + + const from = ServerResponse.from(new Response('upstream', { + status: 404, + statusText: 'Nope', + headers: { 'x-up': '1' }, + })); + assert.strictEqual(from.status, 404); + assert.strictEqual(from.statusText, 'Nope'); + assert.strictEqual(from.headers.get('x-up'), '1'); + assert.strictEqual(await text(from), 'upstream'); + assert.strictEqual(ServerResponse.from(Response.error()).type, 'error'); + const used = new Response('x'); + await used.text(); + assert.throws(() => ServerResponse.from(used), TypeError); +}); + +test('body is read once', async () => { + const response = new ServerResponse('once'); + const first = response[Symbol.asyncIterator](); + const second = response[Symbol.asyncIterator](); + await assert.rejects(second.next(), TypeError); + assert.strictEqual((await first.next()).done, false); + assert.throws(() => response.clone(), TypeError); +}); + +test('clone splits the body', async () => { + const response = new ServerResponse('clone me', { headers: { a: 'b' } }); + const clone = response.clone(); + assert.strictEqual(clone.headers.get('a'), 'b'); + assert.notStrictEqual(clone.headers, response.headers); + const [a, b] = await Promise.all([text(response), text(clone)]); + assert.strictEqual(a, 'clone me'); + assert.strictEqual(b, 'clone me'); +}); + +test('trailers', async () => { + const response = new ServerResponse('x', { + headers: { Trailer: 'x-count, x-other' }, + trailers: Promise.resolve({ 'x-count': '1', 'x-undeclared': '2' }), + }); + const trailers = await response.trailers; + assert.deepStrictEqual([...trailers], [['x-count', '1']]); + assert.throws(() => trailers.set('a', 'b'), { code: 'ERR_HTTP_HEADERS_SENT' }); + const clone = response.clone(); + assert.notStrictEqual(clone.trailers, response.trailers); + assert.deepStrictEqual([...await clone.trailers], [['x-count', '1']]); + + const failing = new ServerResponse(null, { + trailers: Promise.reject(new Error('nope')), + }); + await assert.rejects(failing.trailers, /nope/); +}); + +test('headers behave like Headers without guards', () => { + const { headers } = new ServerResponse(); + headers.append('Set-Cookie', 'a=1'); + headers.append('set-cookie', 'b=2'); + headers.set('X-B', ' padded '); + headers.append('x-a', '1'); + assert.deepStrictEqual(headers.getSetCookie(), ['a=1', 'b=2']); + assert.strictEqual(headers.get('x-b'), 'padded'); + assert.deepStrictEqual([...headers], [ + ['set-cookie', 'a=1'], + ['set-cookie', 'b=2'], + ['x-a', '1'], + ['x-b', 'padded'], + ]); + assert.deepStrictEqual([...headers.keys()], + ['set-cookie', 'set-cookie', 'x-a', 'x-b']); + assert.strictEqual(headers[Symbol.iterator], headers.entries); + headers.delete('X-A'); + assert.strictEqual(headers.has('x-a'), false); + const seen = []; + headers.forEach((value, name, h) => { + assert.strictEqual(h, headers); + seen.push(name); + }); + assert.deepStrictEqual(seen, ['set-cookie', 'set-cookie', 'x-b']); + assert.throws(() => headers.get('bad name'), TypeError); + assert.throws(() => headers.get.call({}, 'a'), TypeError); + assert.throws(() => headers.set('x', '\u0100'), TypeError); + // A ServerHeaders object is a valid ServerHeadersInit. + const copy = new ServerResponse(null, { headers }); + assert.deepStrictEqual(copy.headers.getSetCookie(), ['a=1', 'b=2']); + const fromHeaders = new ServerResponse(null, { headers: new Headers({ q: 'r' }) }); + assert.strictEqual(fromHeaders.headers.get('q'), 'r'); +}); diff --git a/test/parallel/test-http-fetch-server-serve.js b/test/parallel/test-http-fetch-server-serve.js new file mode 100644 index 000000000000..d92b883ce2b7 --- /dev/null +++ b/test/parallel/test-http-fetch-server-serve.js @@ -0,0 +1,421 @@ +// Flags: --experimental-http-serve --no-warnings +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) common.skip('missing crypto'); + +const assert = require('assert'); +const http = require('http'); +const http2 = require('http2'); +const net = require('net'); +const fixtures = require('../common/fixtures'); +const { test } = require('node:test'); +const { serve, ServerResponse } = http.fetchServer; + +const key = fixtures.readKey('agent1-key.pem'); +const cert = fixtures.readKey('agent1-cert.pem'); +const ca = fixtures.readKey('ca1-cert.pem'); + +const handler = { + [Symbol.for('wintertc.server.protocol')]: 1, + fetch: common.mustCallAtLeast(async function fetch(ctx) { + const { request } = ctx; + const url = new URL(request.url); + switch (url.pathname) { + case '/info': + return ServerResponse.json({ + method: request.method, + url: request.url, + alpn: ctx.alpnProtocol, + encrypted: ctx.encrypted, + family: ctx.remoteAddress.family, + localPort: ctx.localAddress.port, + host: request.headers.get('host'), + priority: ctx.clientPriority, + thisIsHandler: this === handler, + }); + case '/echo': { + const chunks = []; + for await (const batch of request) chunks.push(...batch); + const trailers = await request.trailers; + return new ServerResponse(Buffer.concat(chunks), { + headers: { 'x-trailer': trailers.get('x-t') ?? '' }, + }); + } + case '/trailers': { + const done = Promise.withResolvers(); + async function* body() { + yield [Buffer.from('data')]; + done.resolve({ 'x-sum': '4', 'content-type': 'dropped' }); + } + return new ServerResponse(body(), { + headers: { 'Trailer': 'x-sum, content-type' }, + trailers: done.promise, + }); + } + case '/informational': + ctx.sendInformational(103, { link: '; rel=preload' }); + assert.throws(() => ctx.sendInformational(101), RangeError); + assert.throws(() => ctx.sendInformational(100), + { name: 'InvalidStateError' }); + assert.throws(() => ctx.deny(), { name: 'InvalidStateError' }); + return new ServerResponse('after hints'); + case '/deny': + ctx.deny(); + assert.throws(() => ctx.deny(), { name: 'InvalidStateError' }); + return; + case '/throw': + throw new Error('boom'); + case '/invalid': + return 'not a response'; + case '/error-response': + return ServerResponse.error(); + case '/wait-until': { + const { waitUntil } = ctx; + waitUntil(new Promise((resolve) => setTimeout(resolve, 10))); + return new ServerResponse('ok'); + } + case '/completed': + ctx.completed.then(common.mustCall()); + return new ServerResponse('ok'); + case '/middleware': { + const inner = Object.create(ctx, { + request: { value: { url: 'http://rewritten/' } }, + }); + assert.strictEqual(inner.request.url, 'http://rewritten/'); + assert.strictEqual(typeof inner.waitUntil, 'function'); + return new ServerResponse(inner.alpnProtocol); + } + case '/fetch-response': + return new Response('from Response', { status: 202 }); + case '/head': + return new ServerResponse('body', { headers: { 'content-length': '4' } }); + default: + return new ServerResponse('not found', { status: 404 }); + } + }), + error(ctx, err) { + if (err.message === 'boom') { + return new ServerResponse('handled', { status: 599 }); + } + }, +}; + +async function startServer(options = {}) { + const server = serve(handler, { port: 0, hostname: '127.0.0.1', ...options }); + await server.ready; + const [listener] = server; + return { server, port: listener.address.port }; +} + +function h1Request(port, path, { method = 'GET', body, headers, tls } = {}) { + return new Promise((resolve, reject) => { + const mod = tls ? require('https') : http; + const req = mod.request({ + host: '127.0.0.1', port, path, method, headers, ca, servername: 'agent1', + agent: false, + }, (res) => { + const chunks = []; + res.on('data', (c) => chunks.push(c)); + res.on('end', () => resolve({ + status: res.statusCode, + headers: res.headers, + trailers: res.trailers, + body: Buffer.concat(chunks).toString(), + })); + }); + req.on('error', reject); + req.on('information', (info) => { + (req.info ??= []).push(info.statusCode); + }); + if (body !== undefined) { + req.addTrailers?.({}); + req.end(body); + } else { + req.end(); + } + }); +} + +function h2Request(client, path, { method = 'GET', body } = {}) { + return new Promise((resolve, reject) => { + const stream = client.request({ ':path': path, ':method': method }, + { waitForTrailers: false }); + const result = { info: [], trailers: null }; + stream.on('headers', (headers) => result.info.push(headers[':status'])); + stream.on('response', (headers) => { result.headers = headers; }); + stream.on('trailers', (trailers) => { result.trailers = trailers; }); + const chunks = []; + stream.on('data', (c) => chunks.push(c)); + stream.on('end', () => { + result.status = result.headers[':status']; + result.body = Buffer.concat(chunks).toString(); + resolve(result); + }); + stream.on('error', () => {}); + stream.on('close', () => { + if (stream.rstCode) { + reject(Object.assign(new Error('reset'), { rstCode: stream.rstCode })); + } + }); + stream.end(body); + }); +} + +test('handler conversion', () => { + assert.throws(() => serve({}), TypeError); + assert.throws(() => serve(null), TypeError); + assert.throws(() => serve({ fetch: 1 }), TypeError); + assert.throws(() => serve({ + [Symbol.for('wintertc.server.protocol')]: '1', fetch() {}, + }), TypeError); + assert.throws(() => serve({ fetch() {} }, { port: 0, quic: true }), TypeError); + assert.throws(() => serve({ fetch() {} }, { + port: 0, quic: true, tls: { key, cert }, + }), { name: 'NotSupportedError' }); +}); + +test('HTTP/1.1', async () => { + const { server, port } = await startServer(); + try { + const info = JSON.parse((await h1Request(port, '/info?a=1', { + headers: { priority: 'u=1, i' }, + })).body); + assert.deepStrictEqual(info, { + method: 'GET', + url: `http://127.0.0.1:${port}/info?a=1`, + alpn: 'http/1.1', + encrypted: false, + family: 'IPv4', + localPort: port, + host: `127.0.0.1:${port}`, + priority: { urgency: 1, incremental: true }, + thisIsHandler: true, + }); + + const echo = await h1Request(port, '/echo', { + method: 'POST', body: 'x'.repeat(65536 * 3), + }); + assert.strictEqual(echo.body.length, 65536 * 3); + + const trailers = await h1Request(port, '/trailers'); + assert.strictEqual(trailers.body, 'data'); + assert.deepStrictEqual({ ...trailers.trailers }, { 'x-sum': '4' }); + + const informational = await h1Request(port, '/informational'); + assert.strictEqual(informational.body, 'after hints'); + + assert.strictEqual((await h1Request(port, '/throw')).status, 599); + assert.strictEqual((await h1Request(port, '/invalid')).status, 500); + await assert.rejects(h1Request(port, '/deny'), { code: 'ECONNRESET' }); + await assert.rejects(h1Request(port, '/error-response'), + { code: 'ECONNRESET' }); + assert.strictEqual((await h1Request(port, '/wait-until')).body, 'ok'); + assert.strictEqual((await h1Request(port, '/completed')).body, 'ok'); + assert.strictEqual((await h1Request(port, '/middleware')).body, 'http/1.1'); + const fromResponse = await h1Request(port, '/fetch-response'); + assert.strictEqual(fromResponse.status, 202); + assert.strictEqual(fromResponse.body, 'from Response'); + const head = await h1Request(port, '/head', { method: 'HEAD' }); + assert.strictEqual(head.headers['content-length'], '4'); + assert.strictEqual(head.body, ''); + assert.strictEqual((await fetch(`http://127.0.0.1:${port}/nope`)).status, + 404); + } finally { + await server.close(); + } +}); + +test('Expect: 100-continue', async () => { + const { server, port } = await startServer(); + try { + const result = await new Promise((resolve, reject) => { + const req = http.request({ + host: '127.0.0.1', port, path: '/echo', method: 'POST', + headers: { 'expect': '100-continue', 'content-length': 5 }, + }); + req.on('continue', () => req.end('hello')); + req.on('response', (res) => { + let body = ''; + res.setEncoding('utf8'); + res.on('data', (c) => body += c); + res.on('end', () => resolve(body)); + }); + req.on('error', reject); + }); + assert.strictEqual(result, 'hello'); + } finally { + await server.close(); + } +}); + +test('h2c with prior knowledge', async () => { + const { server, port } = await startServer(); + const client = http2.connect(`http://127.0.0.1:${port}`); + try { + const info = JSON.parse((await h2Request(client, '/info')).body); + assert.strictEqual(info.alpn, 'h2c'); + assert.strictEqual(info.host, `127.0.0.1:${port}`); + assert.strictEqual(info.url, `http://127.0.0.1:${port}/info`); + + const echo = await h2Request(client, '/echo', { + method: 'POST', body: 'y'.repeat(100000), + }); + assert.strictEqual(echo.body.length, 100000); + + const trailers = await h2Request(client, '/trailers'); + assert.strictEqual(trailers.body, 'data'); + assert.strictEqual(trailers.trailers['x-sum'], '4'); + assert.strictEqual(trailers.trailers['content-type'], undefined); + + const informational = await h2Request(client, '/informational'); + assert.deepStrictEqual(informational.info, [103]); + + await assert.rejects(h2Request(client, '/deny'), { + rstCode: http2.constants.NGHTTP2_REFUSED_STREAM, + }); + await assert.rejects(h2Request(client, '/error-response'), { + rstCode: http2.constants.NGHTTP2_INTERNAL_ERROR, + }); + assert.strictEqual((await h2Request(client, '/throw')).status, 599); + } finally { + client.close(); + await server.close(); + } +}); + +test('TLS with ALPN', async () => { + const { server, port } = await startServer({ tls: { key, cert } }); + try { + const h1 = JSON.parse((await h1Request(port, '/info', { tls: true })).body); + assert.strictEqual(h1.alpn, 'http/1.1'); + assert.strictEqual(h1.encrypted, true); + assert.strictEqual(h1.url, `https://127.0.0.1:${port}/info`); + + const client = http2.connect(`https://127.0.0.1:${port}`, { + ca, servername: 'agent1', + }); + const h2 = JSON.parse((await h2Request(client, '/info')).body); + assert.strictEqual(h2.alpn, 'h2'); + assert.strictEqual(h2.encrypted, true); + client.close(); + } finally { + await server.close(); + } +}); + +test('two-step listen and multiple listeners', async () => { + const server = serve(handler); + await server.ready; + assert.deepStrictEqual([...server], []); + const a = await server.listen({ port: 0, hostname: '127.0.0.1' }); + const b = await server.listen({ port: 0, hostname: '127.0.0.1' }); + assert.deepStrictEqual([...server], [a, b]); + assert.strictEqual(a.address.family, 'IPv4'); + assert.strictEqual((await h1Request(b.address.port, '/middleware')).body, + 'http/1.1'); + await a.close(); + assert.deepStrictEqual([...server], [b]); + await server.close(); + await assert.rejects(server.listen({ port: 0 }), { name: 'InvalidStateError' }); +}); + +test('binding failure rejects ready', async () => { + const blocker = net.createServer().listen(0, '127.0.0.1'); + await new Promise((resolve) => blocker.once('listening', resolve)); + const server = serve(handler, { + port: blocker.address().port, hostname: '127.0.0.1', + }); + await assert.rejects(server.ready, { code: 'EADDRINUSE' }); + await assert.rejects(server.closed, { code: 'EADDRINUSE' }); + blocker.close(); +}); + +test('busy refuses new exchanges', async () => { + const { server, port } = await startServer(); + try { + server.busy = true; + assert.strictEqual([...server][0].busy, true); + assert.strictEqual((await h1Request(port, '/info')).status, 503); + server.busy = false; + assert.strictEqual((await h1Request(port, '/info')).status, 200); + } finally { + await server.close(); + } +}); + +test('destroy and signal', async () => { + const ac = new AbortController(); + const { server } = await startServer({ signal: ac.signal }); + const reason = new Error('shutting down'); + ac.abort(reason); + await assert.rejects(server.closed, reason); + + const { server: other } = await startServer(); + other.destroy(); + await assert.rejects(other.closed, { name: 'AbortError' }); +}); + +test('await using closes the server', async () => { + let port; + { + await using server = serve(handler, { port: 0, hostname: '127.0.0.1' }); + await server.ready; + port = [...server][0].address.port; + } + await assert.rejects(h1Request(port, '/info'), { code: 'ECONNREFUSED' }); +}); + +test('request signal is aborted when the client disconnects', async () => { + const aborted = Promise.withResolvers(); + const server = serve({ + fetch(ctx) { + ctx.request.signal.addEventListener('abort', () => { + aborted.resolve(ctx.request.signal.reason); + }); + ctx.completed.catch(() => {}); + return new Promise(() => {}); + }, + }, { port: 0, hostname: '127.0.0.1' }); + await server.ready; + const socket = net.connect([...server][0].address.port, '127.0.0.1'); + socket.write('GET / HTTP/1.1\r\nHost: x\r\n\r\n'); + setTimeout(() => socket.destroy(), 50); + const reason = await aborted.promise; + assert.strictEqual(reason.name, 'TypeError'); + assert.strictEqual(reason.code, 'ERR_HTTP_CONNECTION_RESET'); + server.destroy(); +}); + +test('content limits', async () => { + const { server, port } = await startServer({ + limits: { maxRequestBodySize: 10 }, + }); + try { + const declared = await h1Request(port, '/echo', { + method: 'POST', body: 'x'.repeat(20), + }); + assert.strictEqual(declared.status, 413); + const streamed = await new Promise((resolve) => { + const req = http.request({ + host: '127.0.0.1', port, path: '/echo', method: 'POST', + }, (res) => { + res.resume(); + resolve(res.statusCode); + }); + req.write('x'.repeat(8)); + setTimeout(() => req.end('y'.repeat(8)), 20); + }); + assert.strictEqual(streamed, 413); + } finally { + await server.close(); + } +}); + +test('no fetch handler answers 501', async () => { + const server = serve({ connect() {} }, { port: 0, hostname: '127.0.0.1' }); + await server.ready; + assert.strictEqual((await h1Request([...server][0].address.port, '/')).status, + 501); + await server.close(); +}); diff --git a/test/parallel/test-http-fetch-server-websocket.js b/test/parallel/test-http-fetch-server-websocket.js new file mode 100644 index 000000000000..f518d19d64b4 --- /dev/null +++ b/test/parallel/test-http-fetch-server-websocket.js @@ -0,0 +1,441 @@ +// Flags: --experimental-http-serve --no-warnings +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) common.skip('missing crypto'); + +const assert = require('assert'); +const http = require('http'); +const http2 = require('http2'); +const net = require('net'); +const { once } = require('events'); +const { test } = require('node:test'); +const { serve, ServerResponse } = http.fetchServer; + +// Raw RFC 6455 client frames, masked as a client must send them. +function frame(opcode, payload = Buffer.alloc(0), { fin = true, mask = true, + rsv1 = false } = {}) { + payload = Buffer.from(payload); + const length = payload.length; + const header = [(fin ? 0x80 : 0) | (rsv1 ? 0x40 : 0) | opcode]; + if (length < 126) { + header.push((mask ? 0x80 : 0) | length); + } else { + header.push((mask ? 0x80 : 0) | 126, length >> 8, length & 0xff); + } + if (!mask) return Buffer.concat([Buffer.from(header), payload]); + const key = Buffer.from([1, 2, 3, 4]); + const masked = Buffer.from(payload); + for (let i = 0; i < masked.length; i++) masked[i] ^= key[i & 3]; + return Buffer.concat([Buffer.from(header), key, masked]); +} + +function closePayload(code, reason = '') { + const buffer = Buffer.alloc(2); + buffer.writeUInt16BE(code); + return Buffer.concat([buffer, Buffer.from(reason)]); +} + +// Parses unmasked server frames. +function parseFrames(buffer) { + const frames = []; + let offset = 0; + while (offset + 2 <= buffer.length) { + let length = buffer[offset + 1] & 0x7f; + let start = offset + 2; + if (length === 126) { + length = buffer.readUInt16BE(offset + 2); + start += 2; + } else if (length === 127) { + length = Number(buffer.readBigUInt64BE(offset + 2)); + start += 8; + } + if (start + length > buffer.length) break; + frames.push({ + fin: (buffer[offset] & 0x80) !== 0, + opcode: buffer[offset] & 0x0f, + payload: buffer.subarray(start, start + length), + }); + offset = start + length; + } + return { frames, rest: buffer.subarray(offset) }; +} + +class RawClient { + constructor(socket, head) { + this.socket = socket; + this.buffer = head ?? Buffer.alloc(0); + this.frames = []; + this.waiters = []; + socket.on('data', (chunk) => { + this.buffer = Buffer.concat([this.buffer, chunk]); + const { frames, rest } = parseFrames(this.buffer); + this.buffer = rest; + this.frames.push(...frames); + this.waiters.splice(0).forEach((fn) => fn()); + }); + socket.on('end', () => this.waiters.splice(0).forEach((fn) => fn())); + } + + async next() { + while (this.frames.length === 0) { + await new Promise((resolve) => this.waiters.push(resolve)); + } + return this.frames.shift(); + } + + send(data) { + this.socket.write(data); + } +} + +async function rawUpgrade(port, path, headers = {}) { + const socket = net.connect(port, '127.0.0.1'); + const lines = [ + `GET ${path} HTTP/1.1`, + 'Host: 127.0.0.1', + 'Connection: Upgrade', + 'Upgrade: websocket', + 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==', + 'Sec-WebSocket-Version: 13', + ]; + for (const [name, value] of Object.entries(headers)) { + lines.push(`${name}: ${value}`); + } + socket.write(`${lines.join('\r\n')}\r\n\r\n`); + return new Promise((resolve, reject) => { + let data = Buffer.alloc(0); + const onData = (chunk) => { + data = Buffer.concat([data, chunk]); + const end = data.indexOf('\r\n\r\n'); + if (end === -1) return; + socket.removeListener('data', onData); + const head = data.subarray(0, end).toString('latin1'); + resolve({ head, client: new RawClient(socket, data.subarray(end + 4)) }); + }; + socket.on('data', onData); + socket.on('error', reject); + }); +} + +async function start(connect, options = {}) { + const server = serve({ + fetch() { + return new ServerResponse('fetch'); + }, + connect, + }, { port: 0, hostname: '127.0.0.1', ...options }); + await server.ready; + return { server, port: [...server][0].address.port }; +} + +test('upgradeWebSocket() with the WHATWG WebSocket client', async () => { + const serverClose = Promise.withResolvers(); + const { server, port } = await start(common.mustCall((ctx) => { + const ws = ctx.upgradeWebSocket({ protocol: ['chat.v2', 'chat.v1'], + compression: true }); + assert.strictEqual(ws.readyState, ws.OPEN); + assert.strictEqual(ws.url, `ws://127.0.0.1:${port}/chat`); + assert.strictEqual(ws.protocol, 'chat.v2'); + assert.strictEqual(ws.extensions, 'permessage-deflate'); + assert.strictEqual(ws.binaryType, 'blob'); + assert.strictEqual(Object.prototype.toString.call(ws), '[object WebSocket]'); + assert.throws(() => ctx.upgradeWebSocket(), { name: 'InvalidStateError' }); + assert.throws(() => ws.close(1005), { name: 'InvalidAccessError' }); + assert.throws(() => ws.close(1000, 'x'.repeat(124)), { name: 'SyntaxError' }); + ws.binaryType = 'arraybuffer'; + ws.onmessage = common.mustCall(async (event) => { + if (event.data === 'bye') { + ws.close(1001, 'going'); + assert.strictEqual(ws.readyState, ws.CLOSING); + return; + } + ws.send(typeof event.data === 'string' ? event.data.toUpperCase() : + new Uint8Array(event.data).reverse()); + ws.send(new Blob(['blob'])); + }, 4); + ws.addEventListener('close', common.mustCall((event) => { + assert.strictEqual(event.wasClean, true); + assert.strictEqual(event.code, 1001); + serverClose.resolve(); + })); + })); + try { + const client = new WebSocket(`ws://127.0.0.1:${port}/chat`, + ['chat.v1', 'chat.v2']); + client.binaryType = 'arraybuffer'; + await once(client, 'open'); + assert.strictEqual(client.protocol, 'chat.v2'); + assert.strictEqual(client.extensions, 'permessage-deflate'); + const received = []; + client.onmessage = (event) => { + received.push(typeof event.data === 'string' ? event.data : + Buffer.from(event.data).toString('hex')); + }; + client.send('hello'); + client.send('z'.repeat(4096)); + client.send(new Uint8Array([1, 2, 3])); + client.send('bye'); + const [event] = await once(client, 'close'); + assert.strictEqual(event.code, 1001); + assert.strictEqual(event.reason, 'going'); + assert.strictEqual(event.wasClean, true); + assert.deepStrictEqual(received, [ + 'HELLO', '626c6f62', 'Z'.repeat(4096), '626c6f62', '030201', '626c6f62', + ]); + await serverClose.promise; + } finally { + await server.close(); + } +}); + +test('upgradeWebSocketStream() echo with backpressure', async () => { + const closed = Promise.withResolvers(); + const { server, port } = await start(common.mustCall(async (ctx) => { + await using wss = ctx.upgradeWebSocketStream(); + const { readable, writable, protocol, extensions } = await wss.opened; + assert.strictEqual(protocol, ''); + assert.strictEqual(extensions, ''); + const writer = writable.getWriter(); + for await (const message of readable) { + if (typeof message === 'string') { + await writer.write(`echo:${message}`); + } else { + assert.ok(message instanceof Uint8Array); + await writer.write(message); + } + } + closed.resolve(await wss.closed); + })); + try { + const client = new WebSocket(`ws://127.0.0.1:${port}/`); + client.binaryType = 'arraybuffer'; + await once(client, 'open'); + const received = []; + client.onmessage = (event) => { + received.push(typeof event.data === 'string' ? event.data : + event.data.byteLength); + if (received.length === 3) client.close(4001, 'client done'); + }; + client.send('one'); + client.send(new Uint8Array(200000)); + client.send('two'); + const [event] = await once(client, 'close'); + assert.strictEqual(event.code, 4001); + assert.deepStrictEqual(received, ['echo:one', 200000, 'echo:two']); + assert.deepStrictEqual({ ...await closed.promise }, + { closeCode: 4001, reason: 'client done' }); + } finally { + await server.close(); + } +}); + +test('handshake validation', async () => { + const { server, port } = await start(common.mustCall((ctx) => { + if (ctx.connectProtocol !== 'websocket') { + assert.throws(() => ctx.upgradeWebSocket(), { name: 'InvalidStateError' }); + assert.throws(() => ctx.upgradeWebSocketStream(), + { name: 'InvalidStateError' }); + return new ServerResponse(null, { status: 501 }); + } + assert.throws(() => ctx.upgradeWebSocket({ pingInterval: -1 }), TypeError); + try { + ctx.upgradeWebSocketStream(); + } catch (err) { + assert.strictEqual(err.name, 'TypeError'); + return new ServerResponse(null, { + status: 426, headers: { 'sec-websocket-version': '13' }, + }); + } + }, 2)); + try { + const invalid = await rawUpgrade(port, '/', { + 'Sec-WebSocket-Version': '8', + }).catch(() => null); + // The second Sec-WebSocket-Version makes the field value "13, 8". + assert.match(invalid.head, /^HTTP\/1\.1 426 /); + invalid.client.socket.destroy(); + + const other = net.connect(port, '127.0.0.1'); + other.write('GET / HTTP/1.1\r\nHost: x\r\nConnection: Upgrade\r\n' + + 'Upgrade: other\r\n\r\n'); + const [chunk] = await once(other, 'data'); + assert.match(chunk.toString(), /^HTTP\/1\.1 501 /); + other.destroy(); + } finally { + await server.close(); + } +}); + +test('raw protocol: fragmentation, ping, and close', async () => { + const done = Promise.withResolvers(); + const { server, port } = await start(common.mustCall(async (ctx) => { + const wss = ctx.upgradeWebSocketStream(); + const { readable, writable } = await wss.opened; + const reader = readable.getReader(); + const writer = writable.getWriter(); + const first = await reader.read(); + assert.strictEqual(first.value, 'hello world'); + await writer.write(new Uint8Array([9, 8])); + const end = await reader.read(); + assert.strictEqual(end.done, true); + done.resolve(await wss.closed); + await assert.rejects(writer.write('late'), { name: 'InvalidStateError' }); + })); + try { + const { head, client } = await rawUpgrade(port, '/'); + assert.match(head, /^HTTP\/1\.1 101 /); + assert.match(head, /Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK\+xOo=/i); + // A fragmented text message with a ping between the fragments. + client.send(frame(0x1, 'hello ', { fin: false })); + client.send(frame(0x9, 'p')); + client.send(frame(0x0, 'world')); + const pong = await client.next(); + assert.strictEqual(pong.opcode, 0xa); + assert.strictEqual(pong.payload.toString(), 'p'); + const binary = await client.next(); + assert.strictEqual(binary.opcode, 0x2); + assert.deepStrictEqual([...binary.payload], [9, 8]); + client.send(frame(0x8, closePayload(4002, 'bye'))); + const close = await client.next(); + assert.strictEqual(close.opcode, 0x8); + assert.strictEqual(close.payload.readUInt16BE(), 4002); + await once(client.socket, 'end'); + assert.deepStrictEqual({ ...await done.promise }, + { closeCode: 4002, reason: 'bye' }); + client.socket.destroy(); + } finally { + await server.close(); + } +}); + +test('protocol violations fail the connection', async () => { + const cases = [ + [frame(0x1, 'x', { mask: false }), 1002], + [frame(0x3, 'x'), 1002], + [frame(0x0, 'x'), 1002], + [frame(0x1, Buffer.from([0xff, 0xfe])), 1007], + [frame(0x1, 'x', { rsv1: true }), 1002], + [frame(0x8, Buffer.from([0x03, 0xed])), 1002], + ]; + let index = 0; + const { server, port } = await start(common.mustCall(async (ctx) => { + const wss = ctx.upgradeWebSocketStream(); + const { readable } = await wss.opened; + ctx.completed.catch(() => {}); + await assert.rejects(readable.getReader().read(), + { code: 'ERR_HTTP_CONNECT_ERROR' }); + await assert.rejects(wss.closed, { code: 'ERR_HTTP_CONNECT_ERROR' }); + }, cases.length)); + try { + for (; index < cases.length; index++) { + const [data, code] = cases[index]; + const { client } = await rawUpgrade(port, '/'); + client.send(data); + const close = await client.next(); + assert.strictEqual(close.opcode, 0x8); + assert.strictEqual(close.payload.readUInt16BE(), code); + client.socket.destroy(); + } + } finally { + await server.close(); + } +}); + +test('pingInterval keeps the connection alive', async () => { + const { server, port } = await start(common.mustCall(async (ctx) => { + const wss = ctx.upgradeWebSocketStream({ pingInterval: 20 }); + await wss.closed; + })); + try { + const { client } = await rawUpgrade(port, '/'); + const ping = await client.next(); + assert.strictEqual(ping.opcode, 0x9); + client.send(frame(0x8)); + const close = await client.next(); + assert.strictEqual(close.opcode, 0x8); + assert.strictEqual(close.payload.length, 0); + client.socket.destroy(); + } finally { + await server.close(); + } +}); + +test('destroy() and peer loss', async () => { + const lost = Promise.withResolvers(); + const { server, port } = await start(common.mustCall(async (ctx) => { + const wss = ctx.upgradeWebSocketStream(); + await wss.opened; + if (new URL(ctx.request.url).pathname === '/destroy') { + wss.destroy(); + await assert.rejects(wss.closed, { code: 'ERR_HTTP_REQUEST_CANCELLED' }); + wss.destroy(); + return; + } + await assert.rejects(ctx.completed, { code: 'ERR_HTTP_CONNECTION_RESET' }); + lost.resolve(await wss.closed.catch((err) => err.code)); + }, 2)); + try { + const client = new WebSocket(`ws://127.0.0.1:${port}/destroy`); + const [event] = await once(client, 'close'); + assert.strictEqual(event.code, 1006); + assert.strictEqual(event.wasClean, false); + + const raw = await rawUpgrade(port, '/lost'); + // A TCP reset is a lost connection. + raw.client.socket.resetAndDestroy(); + assert.strictEqual(await lost.promise, 'ERR_HTTP_CONNECTION_RESET'); + } finally { + await server.close(); + } +}); + +test('server close() sends 1001 Going Away', async () => { + const { server, port } = await start(common.mustCall((ctx) => { + ctx.upgradeWebSocket(); + })); + const client = new WebSocket(`ws://127.0.0.1:${port}/`); + await once(client, 'open'); + const closing = server.close(); + const [event] = await once(client, 'close'); + assert.strictEqual(event.code, 1001); + assert.strictEqual(event.wasClean, true); + await closing; +}); + +test('WebSocket over HTTP/2 extended CONNECT', async () => { + const { server, port } = await start(common.mustCall(async (ctx) => { + assert.strictEqual(ctx.connectProtocol, 'websocket'); + const wss = ctx.upgradeWebSocketStream({ protocol: ['p1'] }); + const { readable, writable, protocol } = await wss.opened; + assert.strictEqual(protocol, 'p1'); + const writer = writable.getWriter(); + for await (const message of readable) await writer.write(message); + await wss.closed; + })); + const session = http2.connect(`http://127.0.0.1:${port}`); + try { + await once(session, 'remoteSettings'); + const stream = session.request({ + ':method': 'CONNECT', ':protocol': 'websocket', ':scheme': 'http', + ':path': '/', ':authority': `127.0.0.1:${port}`, + 'sec-websocket-version': '13', 'sec-websocket-protocol': 'p0, p1', + }); + const [headers] = await once(stream, 'response'); + assert.strictEqual(headers[':status'], 200); + assert.strictEqual(headers['sec-websocket-protocol'], 'p1'); + assert.strictEqual(headers['sec-websocket-accept'], undefined); + const client = new RawClient(stream); + client.send(frame(0x1, 'over h2')); + const echo = await client.next(); + assert.strictEqual(echo.payload.toString(), 'over h2'); + client.send(frame(0x8, closePayload(1000))); + const close = await client.next(); + assert.strictEqual(close.opcode, 0x8); + stream.end(); + await once(stream, 'close'); + } finally { + session.close(); + await server.close(); + } +});