File
Blob: src/node/http.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 | import { |
| 7 | validateHeaderName, |
| 8 | validateHeaderValue, |
| 9 | } from 'node-internal:internal_http'; |
| 10 | import { METHODS, STATUS_CODES } from 'node-internal:internal_http_constants'; |
| 11 | import { ClientRequest } from 'node-internal:internal_http_client'; |
| 12 | import { OutgoingMessage } from 'node-internal:internal_http_outgoing'; |
| 13 | import { IncomingMessage } from 'node-internal:internal_http_incoming'; |
| 14 | import { Agent, globalAgent } from 'node-internal:internal_http_agent'; |
| 15 | import { |
| 16 | Server, |
| 17 | ServerResponse, |
| 18 | _connectionListener, |
| 19 | } from 'node-internal:internal_http_server'; |
| 20 | import { validateInteger } from 'node-internal:validators'; |
| 21 | import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors'; |
| 22 | import type { IncomingMessageCallback } from 'node-internal:internal_http_util'; |
| 23 | import type { RequestOptions, ServerOptions, RequestListener } from 'node:http'; |
| 24 | |
| 25 | const enableNodejsHttpServerModules = |
| 26 | !!Cloudflare.compatibilityFlags['enable_nodejs_http_server_modules']; |
| 27 | |
| 28 | // TODO(soon): Our global implementation of WebSocket does not match |
| 29 | // Node.js' implementation which is compliant to the spec. However, |
| 30 | // We were previously relying on the unenv polyfills for this, and |
| 31 | // it just re-exported the global also, so this shouldn't be a breaking |
| 32 | // change. Later, however, we'll need to reconcile the Node.js and |
| 33 | // standard spec conformance here. |
| 34 | export const WebSocket = globalThis.WebSocket; |
| 35 | export const CloseEvent = globalThis.CloseEvent; |
| 36 | export const MessageEvent = globalThis.MessageEvent; |
| 37 | |
| 38 | export function request( |
| 39 | url: string | URL | RequestOptions, |
| 40 | options?: RequestOptions | IncomingMessageCallback, |
| 41 | cb?: IncomingMessageCallback |
| 42 | ): ClientRequest { |
| 43 | return new ClientRequest(url, options, cb); |
| 44 | } |
| 45 | |
| 46 | export function get( |
| 47 | url: string | URL | RequestOptions, |
| 48 | options?: RequestOptions | IncomingMessageCallback, |
| 49 | cb?: IncomingMessageCallback |
| 50 | ): ClientRequest { |
| 51 | const req = request(url, options, cb); |
| 52 | req.end(); |
| 53 | return req; |
| 54 | } |
| 55 | |
| 56 | export function createServer( |
| 57 | options: ServerOptions, |
| 58 | handler: RequestListener |
| 59 | ): Server { |
| 60 | if (!enableNodejsHttpServerModules) { |
| 61 | throw new ERR_METHOD_NOT_IMPLEMENTED('createServer'); |
| 62 | } |
| 63 | |
| 64 | return new Server(options, handler); |
| 65 | } |
| 66 | |
| 67 | // the maximum size of HTTP headers (default: 16384 (16KB)) |
| 68 | // ref: https://github.com/nodejs/node/blob/3b715d35440d509c6242ea61dec9d2802f219c83/src/node_options.cc#L787 |
| 69 | export const maxHeaderSize = 16384; |
| 70 | |
| 71 | export function setMaxIdleHTTPParsers(max: unknown): void { |
| 72 | validateInteger(max, 'max', 1); |
| 73 | throw new ERR_METHOD_NOT_IMPLEMENTED('setMaxIdleHTTPParsers'); |
| 74 | } |
| 75 | |
| 76 | export { |
| 77 | validateHeaderName, |
| 78 | validateHeaderValue, |
| 79 | METHODS, |
| 80 | STATUS_CODES, |
| 81 | ClientRequest, |
| 82 | OutgoingMessage, |
| 83 | Agent, |
| 84 | globalAgent, |
| 85 | IncomingMessage, |
| 86 | Server, |
| 87 | ServerResponse, |
| 88 | _connectionListener, |
| 89 | }; |
| 90 | export default { |
| 91 | validateHeaderName, |
| 92 | validateHeaderValue, |
| 93 | METHODS, |
| 94 | STATUS_CODES, |
| 95 | ClientRequest, |
| 96 | request, |
| 97 | get, |
| 98 | OutgoingMessage, |
| 99 | Agent, |
| 100 | globalAgent, |
| 101 | IncomingMessage, |
| 102 | Server, |
| 103 | ServerResponse, |
| 104 | createServer, |
| 105 | maxHeaderSize, |
| 106 | setMaxIdleHTTPParsers, |
| 107 | _connectionListener, |
| 108 | WebSocket, |
| 109 | CloseEvent, |
| 110 | MessageEvent, |
| 111 | }; |