diff options
Diffstat (limited to 'src/js/node/http.ts')
-rw-r--r-- | src/js/node/http.ts | 80 |
1 files changed, 36 insertions, 44 deletions
diff --git a/src/js/node/http.ts b/src/js/node/http.ts index 2eb5231d5..4f7549c17 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -1,7 +1,7 @@ // Hardcoded module "node:http" -import { EventEmitter } from "node:events"; -import { Readable, Writable, Duplex } from "node:stream"; -import { isTypedArray } from "util/types"; +const EventEmitter = require("node:events"); +const { isTypedArray } = require("node:util/types"); +const { Duplex, Readable, Writable } = require("node:stream"); const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/; /** @@ -14,14 +14,14 @@ function checkInvalidHeaderChar(val: string) { return RegExpPrototypeExec.call(headerCharRegex, val) !== null; } -export const validateHeaderName = (name, label) => { +const validateHeaderName = (name, label) => { if (typeof name !== "string" || !name || !checkIsHttpToken(name)) { // throw new ERR_INVALID_HTTP_TOKEN(label || "Header name", name); throw new Error("ERR_INVALID_HTTP_TOKEN"); } }; -export const validateHeaderValue = (name, value) => { +const validateHeaderValue = (name, value) => { if (value === undefined) { // throw new ERR_HTTP_INVALID_HEADER_VALUE(value, name); throw new Error("ERR_HTTP_INVALID_HEADER_VALUE"); @@ -57,16 +57,11 @@ function isIPv6(input) { // Importing from node:url is unnecessary const { URL } = globalThis; -const { newArrayWithSize, String, Object, Array } = $lazy("primordials"); - const globalReportError = globalThis.reportError; const setTimeout = globalThis.setTimeout; const fetch = Bun.fetch; const nop = () => {}; -const __DEBUG__ = process.env.__DEBUG__; -const debug = __DEBUG__ ? (...args) => console.log("node:http", ...args) : nop; - const kEmptyObject = Object.freeze(Object.create(null)); const kOutHeaders = Symbol.for("kOutHeaders"); const kEndCalled = Symbol.for("kEndCalled"); @@ -218,11 +213,11 @@ var FakeSocket = class Socket extends Duplex { _write(chunk, encoding, callback) {} }; -export function createServer(options, callback) { +function createServer(options, callback) { return new Server(options, callback); } -export class Agent extends EventEmitter { +class Agent extends EventEmitter { defaultPort = 80; protocol = "http:"; options; @@ -270,7 +265,7 @@ export class Agent extends EventEmitter { } createConnection() { - debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.createConnection is a no-op, returns fake socket"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.createConnection is a no-op, returns fake socket"); return (this.#fakeSocket ??= new FakeSocket()); } @@ -287,30 +282,30 @@ export class Agent extends EventEmitter { } addRequest() { - debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.addRequest is a no-op"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.addRequest is a no-op"); } createSocket(req, options, cb) { - debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.createSocket returns fake socket"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.createSocket returns fake socket"); cb(null, (this.#fakeSocket ??= new FakeSocket())); } removeSocket() { - debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.removeSocket is a no-op"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.removeSocket is a no-op"); } keepSocketAlive() { - debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.keepSocketAlive is a no-op"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.keepSocketAlive is a no-op"); return true; } reuseSocket() { - debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.reuseSocket is a no-op"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.reuseSocket is a no-op"); } destroy() { - debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.destroy is a no-op"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: Agent.destroy is a no-op"); } } function emitListeningNextTick(self, onListen, err, hostname, port) { @@ -331,7 +326,7 @@ function emitListeningNextTick(self, onListen, err, hostname, port) { } } -export class Server extends EventEmitter { +class Server extends EventEmitter { #server; #options; #tls; @@ -558,7 +553,7 @@ export class Server extends EventEmitter { function assignHeaders(object, req) { var headers = req.headers.toJSON(); - const rawHeaders = newArrayWithSize(req.headers.count * 2); + const rawHeaders = $newArrayWithSize(req.headers.count * 2); var i = 0; for (const key in headers) { rawHeaders[i++] = key; @@ -577,7 +572,7 @@ function getDefaultHTTPSAgent() { return (_defaultHTTPSAgent ??= new Agent({ defaultPort: 443, protocol: "https:" })); } -export class IncomingMessage extends Readable { +class IncomingMessage extends Readable { method: string; complete: boolean; @@ -796,7 +791,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) { } if (!msg._hasBody) { - debug("This type of response MUST NOT have a body. " + "Ignoring write() calls."); + $debug("This type of response MUST NOT have a body. " + "Ignoring write() calls."); process.nextTick(callback); return true; } @@ -809,7 +804,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) { return true; } -export class OutgoingMessage extends Writable { +class OutgoingMessage extends Writable { #headers; headersSent = false; sendDate = true; @@ -958,7 +953,7 @@ export class OutgoingMessage extends Writable { } let OriginalWriteHeadFn, OriginalImplicitHeadFn; -export class ServerResponse extends Writable { +class ServerResponse extends Writable { declare _writableState: any; constructor({ req, reply }) { @@ -1196,7 +1191,7 @@ export class ServerResponse extends Writable { OriginalWriteHeadFn = ServerResponse.prototype.writeHead; OriginalImplicitHeadFn = ServerResponse.prototype._implicitHeader; -export class ClientRequest extends OutgoingMessage { +class ClientRequest extends OutgoingMessage { #timeout; #res: IncomingMessage | null = null; #upgradeOrConnect = false; @@ -1285,7 +1280,7 @@ export class ClientRequest extends OutgoingMessage { headers: this.getHeaders(), body: body && method !== "GET" && method !== "HEAD" && method !== "OPTIONS" ? body : undefined, redirect: "manual", - verbose: Boolean(__DEBUG__), + verbose: !!$debug, signal: this[kAbortController].signal, // Timeouts are handled via this.setTimeout. @@ -1300,7 +1295,7 @@ export class ClientRequest extends OutgoingMessage { this.emit("response", res); }) .catch(err => { - if (__DEBUG__) globalReportError(err); + if (!!$debug) globalReportError(err); this.emit("error", err); }) .finally(() => { @@ -1308,7 +1303,7 @@ export class ClientRequest extends OutgoingMessage { this[kClearTimeout](); }); } catch (err) { - if (__DEBUG__) globalReportError(err); + if (!!$debug) globalReportError(err); this.emit("error", err); } finally { callback(); @@ -1388,7 +1383,7 @@ export class ClientRequest extends OutgoingMessage { if (options.path) { const path = String(options.path); if (RegExpPrototypeExec.call(INVALID_PATH_REGEX, path) !== null) { - debug('Path contains unescaped characters: "%s"', path); + $debug('Path contains unescaped characters: "%s"', path); throw new Error("Path contains unescaped characters"); // throw new ERR_UNESCAPED_CHARACTERS("Request path"); } @@ -1467,8 +1462,7 @@ export class ClientRequest extends OutgoingMessage { this.once("response", cb); } - __DEBUG__ && - debug(`new ClientRequest: ${this.#method} ${this.#protocol}//${this.#host}:${this.#port}${this.#path}`); + $debug(`new ClientRequest: ${this.#method} ${this.#protocol}//${this.#host}:${this.#port}${this.#path}`); // if ( // method === "GET" || @@ -1556,12 +1550,13 @@ export class ClientRequest extends OutgoingMessage { } setSocketKeepAlive(enable = true, initialDelay = 0) { - __DEBUG__ && debug(`${NODE_HTTP_WARNING}\n`, "WARN: ClientRequest.setSocketKeepAlive is a no-op"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: ClientRequest.setSocketKeepAlive is a no-op"); } setNoDelay(noDelay = true) { - __DEBUG__ && debug(`${NODE_HTTP_WARNING}\n`, "WARN: ClientRequest.setNoDelay is a no-op"); + $debug(`${NODE_HTTP_WARNING}\n`, "WARN: ClientRequest.setNoDelay is a no-op"); } + [kClearTimeout]() { if (this.#timeoutTimer) { clearTimeout(this.#timeoutTimer); @@ -1666,7 +1661,7 @@ function checkIsHttpToken(val) { // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -export const METHODS = [ +const METHODS = [ "ACL", "BIND", "CHECKOUT", @@ -1703,7 +1698,7 @@ export const METHODS = [ "UNSUBSCRIBE", ]; -export const STATUS_CODES = { +const STATUS_CODES = { 100: "Continue", 101: "Switching Protocols", 102: "Processing", @@ -1863,7 +1858,7 @@ function _writeHead(statusCode, reason, obj, response) { * @param {Function} [cb] * @returns {ClientRequest} */ -export function request(url, options, cb) { +function request(url, options, cb) { return new ClientRequest(url, options, cb); } @@ -1874,14 +1869,14 @@ export function request(url, options, cb) { * @param {Function} [cb] * @returns {ClientRequest} */ -export function get(url, options, cb) { +function get(url, options, cb) { const req = request(url, options, cb); req.end(); return req; } -export var globalAgent = new Agent(); -var defaultObject = { +var globalAgent = new Agent(); +export default { Agent, Server, METHODS, @@ -1895,12 +1890,9 @@ var defaultObject = { validateHeaderName, validateHeaderValue, setMaxIdleHTTPParsers(max) { - debug(`${NODE_HTTP_WARNING}\n`, "setMaxIdleHTTPParsers() is a no-op"); + $debug(`${NODE_HTTP_WARNING}\n`, "setMaxIdleHTTPParsers() is a no-op"); }, globalAgent, ClientRequest, OutgoingMessage, - [Symbol.for("CommonJS")]: 0, }; - -export default defaultObject; |