Skip to content

Commit e118b00

Browse files
committed
v0.3.21-debug.1
1 parent 59f24a6 commit e118b00

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

client.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { request } from "undici";
21
import { makeWebSocket } from "./channel.js";
32
import pkg from "./package.json" with { type: "json" };
43
import { handleServerMessage } from "./handlers.client.js";
@@ -44,7 +43,6 @@ export const connectMainThread = async (
4443
): Promise<Connected> => {
4544
const closed = Promise.withResolvers<Error | undefined>();
4645
const registered = Promise.withResolvers<void>();
47-
const client = request; // Use undici.request for Node.js to allow host header override
4846

4947
const socket = new WebSocket(
5048
`${opts.server}/_connect?${CLIENT_VERSION_QUERY_STRING}=${pkg.version}`,
@@ -64,7 +62,6 @@ export const connectMainThread = async (
6462
(async () => {
6563
let reason: undefined | Error;
6664
const state: ClientState = {
67-
client,
6865
localAddr: opts.localAddr,
6966
live: false,
7067
requests: {},

handlers.client.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ import type {
2121
WSMessage,
2222
} from "./messages.js";
2323
import { arrayBufferSerializer } from "./serializers.js";
24+
import { request as undiciRequest } from "undici";
25+
import { Readable } from 'stream';
26+
27+
function nodeReadableToWebReadable(nodeStream: Readable): ReadableStream<Uint8Array> {
28+
return new ReadableStream({
29+
start(controller) {
30+
nodeStream.on('data', chunk => controller.enqueue(chunk));
31+
nodeStream.on('end', () => controller.close());
32+
nodeStream.on('error', err => controller.error(err));
33+
},
34+
cancel() {
35+
nodeStream.destroy();
36+
}
37+
});
38+
}
39+
2440

2541
/**
2642
* Handler for the 'registered' server message.
@@ -239,38 +255,37 @@ async function doFetch(
239255
// Read from the stream
240256
const signal = link(clientCh.signal, reqSignal);
241257
try {
242-
const response = await fetch(
258+
const response = await undiciRequest(
243259
new URL(request.url, state.localAddr),
244260
{
245-
...state.client ? { client: state.client } : {},
246-
redirect: "manual",
247-
method: request.method,
248-
headers: state.client
249-
? { ...request.headers, host: request.domain }
250-
: request.headers,
251-
body: request.body,
261+
method: request.method as any,
262+
headers: { ...request.headers, host: request.domain },
263+
body: request.body as any,
252264
...(request.body ? { duplex: "half" } : {}),
253265
signal,
254266
},
255267
);
256268

257269
const headers: Record<string, Array<string>> = {};
258270

259-
response.headers.forEach((value, key) => {
260-
headers[key] ??= [];
261-
headers[key].push(value);
262-
});
271+
for (const [key, value] of Object.entries(response.headers)) {
272+
if (typeof value === 'string') {
273+
headers[key] = [value];
274+
} else if (Array.isArray(value)) {
275+
headers[key] = value;
276+
}
277+
}
263278

264279
await clientCh.send({
265280
type: "response-start",
266281
id: request.id,
267-
statusCode: response.status,
268-
statusMessage: response.statusText,
282+
statusCode: response.statusCode,
283+
statusMessage: "OK",
269284
headers,
270285
});
271286

272287
const body = response?.body;
273-
const stream = body ? makeChanStream(body) : undefined;
288+
const stream = body ? makeChanStream(nodeReadableToWebReadable(body)) : undefined;
274289
for await (const chunk of stream?.recv(signal) ?? []) {
275290
await clientCh.send({
276291
type: "data",

messages.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export interface ClientState {
106106
wsSockets: Record<string, WebSocket>;
107107
live: boolean;
108108
localAddr: string;
109-
client?: typeof import("undici").request; // Use undici.request for Node.js
110109
}
111110

112111
export interface ClientHostController {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@deco-cx/warp-node",
3-
"version": "0.3.16",
3+
"version": "0.3.21-debug.1",
44
"description": "WebSocket tunneling library for Node.js and other runtimes",
55
"type": "module",
66
"main": "dist/mod.js",

0 commit comments

Comments
 (0)