Skip to content

Commit

Permalink
refactor(http, cloudflare): use unenv/ imports inside node:http (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored Dec 3, 2024
1 parent fe9f8b2 commit 380be9f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/presets/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const cloudflarePreset: Preset = {
[`node:${m}`, `unenv/runtime/node/${m}/$cloudflare`],
]),
),

// TODO: this is a hotfix and breaks unenv/fetch
// https://github.com/unjs/unenv/issues/364
"unenv/runtime/node/stream/index": "node:stream",
},
inject: {
// workerd already defines `global` and `Buffer`
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/node/http/internal/request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type http from "node:http";
import { Socket } from "node:net";
// Relative stream import required, see https://github.com/unjs/unenv/issues/353
import { Readable } from "../../stream/internal/readable";
// TODO: https://github.com/unjs/unenv/issues/365
import { Readable } from "unenv/runtime/node/stream/index";
import { rawHeaders } from "../../../_internal/utils";

// Docs: https://nodejs.org/api/http.html#http_class_http_incomingmessage
Expand Down Expand Up @@ -52,6 +52,8 @@ export class IncomingMessage extends Readable implements http.IncomingMessage {
get trailersDistinct() {
return _distinct(this.trailers);
}

_read() {}
}

function _distinct(obj: Record<string, any>) {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/node/http/internal/response.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type http from "node:http";
import type { Socket } from "node:net";
import { Callback } from "../../../_internal/types";
// Relative stream import required, see https://github.com/unjs/unenv/issues/353
import { Writable } from "../../stream";
// TODO: https://github.com/unjs/unenv/issues/365
import { Writable } from "unenv/runtime/node/stream/index";

// Docs: https://nodejs.org/api/http.html#http_class_http_serverresponse
// Implementation: https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js
Expand Down
42 changes: 22 additions & 20 deletions test/workerd/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,25 @@ export const workerd_path = {

// --- unenv:fetch

export const unenv_fetch = {
async test() {
// https://srvx.unjs.io/guide/node#reverse-compatibility
// This method is used in h3 v1 and Nitro v2 for server fetch without network roundtrip + internal caching system.
const { createFetch, createCall } = await import("unenv/runtime/fetch");
const serverFetch = createFetch(
createCall(async (req, res) => {
res.end(
JSON.stringify({ "req.url": req.url, "req.headers": req.headers }),
);
}),
);
const res = await serverFetch("/test", { headers: { foo: "bar" } });
const resBody = await res.json();
assert.deepEqual(resBody, {
"req.url": "/test",
"req.headers": { foo: "bar", host: "localhost" },
});
},
};
// https://github.com/unjs/unenv/issues/364
//
// export const unenv_fetch = {
// async test() {
// // https://srvx.unjs.io/guide/node#reverse-compatibility
// // This method is used in h3 v1 and Nitro v2 for server fetch without network roundtrip + internal caching system.
// const { createFetch, createCall } = await import("unenv/runtime/fetch");
// const serverFetch = createFetch(
// createCall(async (req, res) => {
// res.end(
// JSON.stringify({ "req.url": req.url, "req.headers": req.headers }),
// );
// }),
// );
// const res = await serverFetch("/test", { headers: { foo: "bar" } });
// const resBody = await res.json();
// assert.deepEqual(resBody, {
// "req.url": "/test",
// "req.headers": { foo: "bar", host: "localhost" },
// });
// },
// };
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"declaration": true,
"types": [
"node"
]
],
"paths": {
"unenv/*": [
"./src/*"
]
}
},
"include": [
"src/**/*"
Expand Down

0 comments on commit 380be9f

Please sign in to comment.