File
Blob: src/node/http2.ts
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | // Copyright Joyent and Node contributors. All rights reserved. MIT license. |
| 5 | // |
| 6 | // Portions of this code are based on unenv https://github.com/unjs/unenv/blob/main/LICENSE |
| 7 | // MIT License, Copyright (c) Pooya Parsa <pooya@pi0.io> |
| 8 | |
| 9 | // We currently have no intention of implementing HTTP/2 in workerd but we want to |
| 10 | // provide the non-op stubs so that polyfills are not necessary for modules that |
| 11 | // unconditionally import 'http2'. |
| 12 | |
| 13 | import type H2 from 'node:http2'; |
| 14 | import type H1 from 'node:http'; |
| 15 | |
| 16 | import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors'; |
| 17 | import { Buffer } from 'node-internal:internal_buffer'; |
| 18 | |
| 19 | import { constants } from 'node-internal:internal_http2_constants'; |
| 20 | |
| 21 | export { constants }; |
| 22 | |
| 23 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters |
| 24 | function notImplementedClass<T = unknown>(name: string): T { |
| 25 | return class { |
| 26 | constructor() { |
| 27 | throw new ERR_METHOD_NOT_IMPLEMENTED(name); |
| 28 | } |
| 29 | } as T; |
| 30 | } |
| 31 | |
| 32 | export function createSecureServer(..._args: unknown[]): H2.Http2SecureServer { |
| 33 | throw new ERR_METHOD_NOT_IMPLEMENTED('http2.createSecureServer'); |
| 34 | } |
| 35 | |
| 36 | export function createServer(..._args: unknown[]): H2.Http2Server { |
| 37 | throw new ERR_METHOD_NOT_IMPLEMENTED('http2.createServer'); |
| 38 | } |
| 39 | |
| 40 | export function connect(..._args: unknown[]): H2.ClientHttp2Session { |
| 41 | throw new ERR_METHOD_NOT_IMPLEMENTED('http2.connect'); |
| 42 | } |
| 43 | |
| 44 | export function performServerHandshake< |
| 45 | Http1Request extends typeof H1.IncomingMessage = typeof H1.IncomingMessage, |
| 46 | Http1Response extends typeof H1.ServerResponse<InstanceType<Http1Request>> = |
| 47 | typeof H1.ServerResponse, |
| 48 | Http2Request extends typeof H2.Http2ServerRequest = |
| 49 | typeof H2.Http2ServerRequest, |
| 50 | Http2Response extends typeof H2.Http2ServerResponse< |
| 51 | InstanceType<Http2Request> |
| 52 | > = typeof H2.Http2ServerResponse, |
| 53 | >( |
| 54 | ..._args: unknown[] |
| 55 | ): H2.ServerHttp2Session< |
| 56 | Http1Request, |
| 57 | Http1Response, |
| 58 | Http2Request, |
| 59 | Http2Response |
| 60 | > { |
| 61 | throw new ERR_METHOD_NOT_IMPLEMENTED('http2.performServerHandshake'); |
| 62 | } |
| 63 | |
| 64 | export const Http2ServerRequest: H2.Http2ServerRequest = notImplementedClass( |
| 65 | 'http2.Http2ServerRequest' |
| 66 | ); |
| 67 | |
| 68 | export const Http2ServerResponse: H2.Http2ServerResponse = notImplementedClass( |
| 69 | 'http2.Http2ServerResponse' |
| 70 | ); |
| 71 | |
| 72 | export function getDefaultSettings(..._args: unknown[]): H2.Settings { |
| 73 | // We really ought to throw here but the unenv polyfill returns an object |
| 74 | // so let's do so also. |
| 75 | return Object.create({ |
| 76 | headerTableSize: 4096, |
| 77 | enablePush: true, |
| 78 | initialWindowSize: 65_535, |
| 79 | maxFrameSize: 16_384, |
| 80 | maxConcurrentStreams: 4_294_967_295, |
| 81 | maxHeaderSize: 65_535, |
| 82 | maxHeaderListSize: 65_535, |
| 83 | enableConnectProtocol: false, |
| 84 | }) as H2.Settings; |
| 85 | } |
| 86 | |
| 87 | export function getPackedSettings(..._args: unknown[]): Buffer { |
| 88 | return Buffer.alloc(0); |
| 89 | } |
| 90 | |
| 91 | export function getUnpackedSettings(..._args: unknown[]): H2.Settings { |
| 92 | return {}; |
| 93 | } |
| 94 | |
| 95 | export const sensitiveHeaders = Symbol('nodejs.http2.sensitiveHeaders'); |
| 96 | |
| 97 | export default { |
| 98 | constants, |
| 99 | createSecureServer, |
| 100 | createServer, |
| 101 | Http2ServerRequest, |
| 102 | Http2ServerResponse, |
| 103 | connect, |
| 104 | getDefaultSettings, |
| 105 | getPackedSettings, |
| 106 | getUnpackedSettings, |
| 107 | performServerHandshake, |
| 108 | sensitiveHeaders, |
| 109 | }; |