Skip to content

Commit

Permalink
[WIP] use cloudflare native stream take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Nov 29, 2024
1 parent af92110 commit 8e13661
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 291 deletions.
52 changes: 27 additions & 25 deletions src/runtime/node/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
// https://nodejs.org/api/http.html
import type http from "node:http";
import type nodeHttp from "node:http";
import { notImplemented, notImplementedClass } from "../../_internal/utils";
import mock from "../../mock/proxy";
import * as consts from "./internal/consts";
import { IncomingMessage } from "./internal/request";
import { ServerResponse } from "./internal/response";

// Relative stream import required, see https://github.com/unjs/unenv/issues/353
import { Readable, Writable } from "../stream";
import { IncomingMessageFactory } from "./internal/request";
import { ServerResponseFactory } from "./internal/response";
export * from "./internal/consts";
export * from "./internal/request";
export * from "./internal/response";

export const createServer =
notImplemented<typeof http.createServer>("http.createServer");
export const request = notImplemented<typeof http.request>("http.request");
export const get = notImplemented<typeof http.get>("http.get");
notImplemented<typeof nodeHttp.createServer>("http.createServer");
export const request = notImplemented<typeof nodeHttp.request>("http.request");
export const get = notImplemented<typeof nodeHttp.get>("http.get");

export const Server: typeof nodeHttp.Server =
mock.__createMock__("http.Server");

export const Server: typeof http.Server = mock.__createMock__("http.Server");
export const IncomingMessage = IncomingMessageFactory(Readable);
export const ServerResponse = ServerResponseFactory(Writable);

export const OutgoingMessage: typeof http.OutgoingMessage = mock.__createMock__(
"http.OutgoingMessage",
);
export const OutgoingMessage: typeof nodeHttp.OutgoingMessage =
mock.__createMock__("http.OutgoingMessage");

export const ClientRequest: typeof http.ClientRequest =
export const ClientRequest: typeof nodeHttp.ClientRequest =
mock.__createMock__("http.ClientRequest");

export const Agent: typeof http.Agent = mock.__createMock__("http.Agent");
export const Agent: typeof nodeHttp.Agent = mock.__createMock__("http.Agent");

export const globalAgent: typeof http.globalAgent = new Agent();
export const globalAgent: typeof nodeHttp.globalAgent = new Agent();

export const validateHeaderName = notImplemented<
typeof http.validateHeaderName
typeof nodeHttp.validateHeaderName
>("http.validateHeaderName");

export const validateHeaderValue = notImplemented<
typeof http.validateHeaderValue
typeof nodeHttp.validateHeaderValue
>("http.validateHeaderValue");

export const setMaxIdleHTTPParsers = notImplemented<
typeof http.setMaxIdleHTTPParsers
typeof nodeHttp.setMaxIdleHTTPParsers
>("http.setMaxIdleHTTPParsers");

export const _connectionListener = notImplemented("http._connectionListener");
Expand All @@ -51,13 +53,13 @@ export const CloseEvent =
export const MessageEvent =
globalThis.MessageEvent || notImplementedClass<MessageEvent>("MessageEvent");

export default <typeof http>{
export default <typeof nodeHttp>{
...consts,
IncomingMessage: IncomingMessage as any as typeof http.IncomingMessage,
ServerResponse: ServerResponse as any as typeof http.ServerResponse,
WebSocket: WebSocket as any as typeof http.WebSocket,
CloseEvent: CloseEvent as any as typeof http.CloseEvent,
MessageEvent: MessageEvent as any as typeof http.MessageEvent,
IncomingMessage,
ServerResponse,
WebSocket: WebSocket as any as typeof nodeHttp.WebSocket,
CloseEvent: CloseEvent as any as typeof nodeHttp.CloseEvent,
MessageEvent: MessageEvent as any as typeof nodeHttp.MessageEvent,
createServer,
request,
get,
Expand Down
84 changes: 43 additions & 41 deletions src/runtime/node/http/internal/request.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
import type http from "node:http";
import type nodeHttp from "node:http";
import type nodeStream from "node:stream";
import { Socket } from "node:net";
// Relative stream import required, see https://github.com/unjs/unenv/issues/353
import { Readable } from "../../stream/internal/readable";
import { rawHeaders } from "../../../_internal/utils";

// Docs: https://nodejs.org/api/http.html#http_class_http_incomingmessage
// Implementation: https://github.com/nodejs/node/blob/master/lib/_http_incoming.js

export class IncomingMessage extends Readable implements http.IncomingMessage {
public __unenv__ = {};
export const IncomingMessageFactory = (
Readable: typeof nodeStream.Readable,
): typeof nodeHttp.IncomingMessage =>
class extends Readable implements nodeHttp.IncomingMessage {
public __unenv__ = {};

public aborted: boolean = false;
public httpVersion: string = "1.1";
public httpVersionMajor: number = 1;
public httpVersionMinor: number = 1;
public complete: boolean = true;
public connection: Socket;
public socket: Socket;
public headers: http.IncomingHttpHeaders = {};
public trailers = {};
public method: string = "GET";
public url: string = "/";
public statusCode: number = 200;
public statusMessage: string = "";
public closed: boolean = false;
public errored: Error | null = null;
public aborted: boolean = false;
public httpVersion: string = "1.1";
public httpVersionMajor: number = 1;
public httpVersionMinor: number = 1;
public complete: boolean = true;
public connection: Socket;
public socket: Socket;
public headers: nodeHttp.IncomingHttpHeaders = {};
public trailers = {};
public method: string = "GET";
public url: string = "/";
public statusCode: number = 200;
public statusMessage: string = "";
public closed: boolean = false;
public errored: Error | null = null;

readable: boolean = false;
readable: boolean = false;

constructor(socket?: Socket) {
super();
this.socket = this.connection = socket || new Socket();
}
constructor(socket?: Socket) {
super();
this.socket = this.connection = socket || new Socket();
}

get rawHeaders() {
return rawHeaders(this.headers);
}
get rawHeaders() {
return rawHeaders(this.headers);
}

get rawTrailers() {
return [];
}
get rawTrailers() {
return [];
}

setTimeout(_msecs: number, _callback?: () => void) {
return this;
}
setTimeout(_msecs: number, _callback?: () => void) {
return this;
}

get headersDistinct() {
return _distinct(this.headers);
}
get headersDistinct() {
return _distinct(this.headers);
}

get trailersDistinct() {
return _distinct(this.trailers);
}
}
get trailersDistinct() {
return _distinct(this.trailers);
}
};

function _distinct(obj: Record<string, any>) {
const d: Record<string, string[]> = {};
Expand Down
Loading

0 comments on commit 8e13661

Please sign in to comment.