Skip to content
File

Blob: src/node/http.ts

typescript112 lines
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 
6import {
7 validateHeaderName,
8 validateHeaderValue,
9} from 'node-internal:internal_http';
10import { METHODS, STATUS_CODES } from 'node-internal:internal_http_constants';
11import { ClientRequest } from 'node-internal:internal_http_client';
12import { OutgoingMessage } from 'node-internal:internal_http_outgoing';
13import { IncomingMessage } from 'node-internal:internal_http_incoming';
14import { Agent, globalAgent } from 'node-internal:internal_http_agent';
15import {
16 Server,
17 ServerResponse,
18 _connectionListener,
19} from 'node-internal:internal_http_server';
20import { validateInteger } from 'node-internal:validators';
21import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors';
22import type { IncomingMessageCallback } from 'node-internal:internal_http_util';
23import type { RequestOptions, ServerOptions, RequestListener } from 'node:http';
24 
25const 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.
34export const WebSocket = globalThis.WebSocket;
35export const CloseEvent = globalThis.CloseEvent;
36export const MessageEvent = globalThis.MessageEvent;
37 
38export 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 
46export 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 
56export 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
69export const maxHeaderSize = 16384;
70 
71export function setMaxIdleHTTPParsers(max: unknown): void {
72 validateInteger(max, 'max', 1);
73 throw new ERR_METHOD_NOT_IMPLEMENTED('setMaxIdleHTTPParsers');
74}
75 
76export {
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};
90export 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};