zeno.id 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -6
- package/dist/index.cjs +62 -62
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +62 -62
- package/dist/server/index.cjs +187 -173
- package/dist/server/index.d.cts +17 -5
- package/dist/server/index.d.ts +17 -5
- package/dist/server/index.js +187 -173
- package/dist/server/node.cjs +18 -18
- package/dist/server/node.js +18 -18
- package/package.json +1 -1
package/dist/server/node.cjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/server/node.ts
|
|
3
3
|
const MAX_BODY = 131073;
|
|
4
|
-
const
|
|
4
|
+
const readBody = async (request, parsedBody) => {
|
|
5
5
|
if (request.method !== "POST") return;
|
|
6
6
|
if (request.readableEnded) {
|
|
7
|
-
if (typeof
|
|
8
|
-
if (
|
|
9
|
-
return
|
|
7
|
+
if (typeof parsedBody === "string") return parsedBody;
|
|
8
|
+
if (parsedBody instanceof Uint8Array) return new TextDecoder().decode(parsedBody);
|
|
9
|
+
return parsedBody === void 0 ? "" : JSON.stringify(parsedBody);
|
|
10
10
|
}
|
|
11
11
|
const chunks = [];
|
|
12
12
|
let size = 0;
|
|
@@ -24,7 +24,7 @@ const bodyOf = async (request, parsed) => {
|
|
|
24
24
|
}
|
|
25
25
|
return new TextDecoder().decode(body);
|
|
26
26
|
};
|
|
27
|
-
const
|
|
27
|
+
const toWebRequest = async (request, parsedBody) => {
|
|
28
28
|
const headers = new Headers();
|
|
29
29
|
for (let index = 0; index + 1 < request.rawHeaders.length; index += 2) {
|
|
30
30
|
const name = request.rawHeaders[index];
|
|
@@ -33,21 +33,21 @@ const requestOf = async (request, parsed) => {
|
|
|
33
33
|
const protocol = request.socket.encrypted ? "https" : "http";
|
|
34
34
|
const path = request.originalUrl ?? request.url ?? "/";
|
|
35
35
|
const host = request.headers.host ?? request.headers[":authority"] ?? "localhost";
|
|
36
|
-
const body = await
|
|
36
|
+
const body = await readBody(request, parsedBody);
|
|
37
37
|
return new Request(new URL(path, `${protocol}://${String(host)}`), {
|
|
38
38
|
method: request.method,
|
|
39
39
|
headers,
|
|
40
40
|
...body === void 0 ? {} : { body }
|
|
41
41
|
});
|
|
42
42
|
};
|
|
43
|
-
const
|
|
44
|
-
|
|
43
|
+
const writeResponse = async (response, nodeResponse) => {
|
|
44
|
+
nodeResponse.statusCode = response.status;
|
|
45
45
|
response.headers.forEach((value, name) => {
|
|
46
|
-
if (name !== "set-cookie")
|
|
46
|
+
if (name !== "set-cookie") nodeResponse.setHeader(name, value);
|
|
47
47
|
});
|
|
48
48
|
const cookies = response.headers.getSetCookie();
|
|
49
|
-
if (cookies.length > 0)
|
|
50
|
-
|
|
49
|
+
if (cookies.length > 0) nodeResponse.setHeader("set-cookie", cookies);
|
|
50
|
+
nodeResponse.end(response.body ? new Uint8Array(await response.arrayBuffer()) : void 0);
|
|
51
51
|
};
|
|
52
52
|
/**
|
|
53
53
|
* Mounts the proxy route on Node's `(req, res)`: Express, Fastify or `http.createServer`. `app.all('/zeno',
|
|
@@ -56,21 +56,21 @@ const send = async (response, to) => {
|
|
|
56
56
|
* address. A body parser in front (`express.json()`) is fine.
|
|
57
57
|
*/
|
|
58
58
|
const nodeHandler = (handler) => {
|
|
59
|
-
const
|
|
59
|
+
const handlerWithRawHeaders = handler;
|
|
60
60
|
return async (request, response) => {
|
|
61
61
|
const req = "raw" in request ? request.raw : request;
|
|
62
|
-
const
|
|
63
|
-
const ip = typeof
|
|
64
|
-
const
|
|
62
|
+
const frameworkIp = request.ip;
|
|
63
|
+
const ip = typeof frameworkIp === "string" ? frameworkIp : req.socket.remoteAddress;
|
|
64
|
+
const parsedBody = "raw" in request ? request.body : request.body;
|
|
65
65
|
let res;
|
|
66
66
|
if ("hijack" in response) {
|
|
67
67
|
response.hijack();
|
|
68
68
|
res = response.raw;
|
|
69
69
|
} else res = response;
|
|
70
70
|
try {
|
|
71
|
-
const
|
|
72
|
-
const answer = await
|
|
73
|
-
await
|
|
71
|
+
const webRequest = await toWebRequest(req, parsedBody);
|
|
72
|
+
const answer = await handlerWithRawHeaders(webRequest, ip, { rawHeaders: req.rawHeaders });
|
|
73
|
+
await writeResponse(answer, res);
|
|
74
74
|
} catch (error) {
|
|
75
75
|
console.error(`The proxy route failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
76
76
|
if (!res.headersSent) {
|
package/dist/server/node.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
//#region src/server/node.ts
|
|
2
2
|
const MAX_BODY = 131073;
|
|
3
|
-
const
|
|
3
|
+
const readBody = async (request, parsedBody) => {
|
|
4
4
|
if (request.method !== "POST") return;
|
|
5
5
|
if (request.readableEnded) {
|
|
6
|
-
if (typeof
|
|
7
|
-
if (
|
|
8
|
-
return
|
|
6
|
+
if (typeof parsedBody === "string") return parsedBody;
|
|
7
|
+
if (parsedBody instanceof Uint8Array) return new TextDecoder().decode(parsedBody);
|
|
8
|
+
return parsedBody === void 0 ? "" : JSON.stringify(parsedBody);
|
|
9
9
|
}
|
|
10
10
|
const chunks = [];
|
|
11
11
|
let size = 0;
|
|
@@ -23,7 +23,7 @@ const bodyOf = async (request, parsed) => {
|
|
|
23
23
|
}
|
|
24
24
|
return new TextDecoder().decode(body);
|
|
25
25
|
};
|
|
26
|
-
const
|
|
26
|
+
const toWebRequest = async (request, parsedBody) => {
|
|
27
27
|
const headers = new Headers();
|
|
28
28
|
for (let index = 0; index + 1 < request.rawHeaders.length; index += 2) {
|
|
29
29
|
const name = request.rawHeaders[index];
|
|
@@ -32,21 +32,21 @@ const requestOf = async (request, parsed) => {
|
|
|
32
32
|
const protocol = request.socket.encrypted ? "https" : "http";
|
|
33
33
|
const path = request.originalUrl ?? request.url ?? "/";
|
|
34
34
|
const host = request.headers.host ?? request.headers[":authority"] ?? "localhost";
|
|
35
|
-
const body = await
|
|
35
|
+
const body = await readBody(request, parsedBody);
|
|
36
36
|
return new Request(new URL(path, `${protocol}://${String(host)}`), {
|
|
37
37
|
method: request.method,
|
|
38
38
|
headers,
|
|
39
39
|
...body === void 0 ? {} : { body }
|
|
40
40
|
});
|
|
41
41
|
};
|
|
42
|
-
const
|
|
43
|
-
|
|
42
|
+
const writeResponse = async (response, nodeResponse) => {
|
|
43
|
+
nodeResponse.statusCode = response.status;
|
|
44
44
|
response.headers.forEach((value, name) => {
|
|
45
|
-
if (name !== "set-cookie")
|
|
45
|
+
if (name !== "set-cookie") nodeResponse.setHeader(name, value);
|
|
46
46
|
});
|
|
47
47
|
const cookies = response.headers.getSetCookie();
|
|
48
|
-
if (cookies.length > 0)
|
|
49
|
-
|
|
48
|
+
if (cookies.length > 0) nodeResponse.setHeader("set-cookie", cookies);
|
|
49
|
+
nodeResponse.end(response.body ? new Uint8Array(await response.arrayBuffer()) : void 0);
|
|
50
50
|
};
|
|
51
51
|
/**
|
|
52
52
|
* Mounts the proxy route on Node's `(req, res)`: Express, Fastify or `http.createServer`. `app.all('/zeno',
|
|
@@ -55,21 +55,21 @@ const send = async (response, to) => {
|
|
|
55
55
|
* address. A body parser in front (`express.json()`) is fine.
|
|
56
56
|
*/
|
|
57
57
|
const nodeHandler = (handler) => {
|
|
58
|
-
const
|
|
58
|
+
const handlerWithRawHeaders = handler;
|
|
59
59
|
return async (request, response) => {
|
|
60
60
|
const req = "raw" in request ? request.raw : request;
|
|
61
|
-
const
|
|
62
|
-
const ip = typeof
|
|
63
|
-
const
|
|
61
|
+
const frameworkIp = request.ip;
|
|
62
|
+
const ip = typeof frameworkIp === "string" ? frameworkIp : req.socket.remoteAddress;
|
|
63
|
+
const parsedBody = "raw" in request ? request.body : request.body;
|
|
64
64
|
let res;
|
|
65
65
|
if ("hijack" in response) {
|
|
66
66
|
response.hijack();
|
|
67
67
|
res = response.raw;
|
|
68
68
|
} else res = response;
|
|
69
69
|
try {
|
|
70
|
-
const
|
|
71
|
-
const answer = await
|
|
72
|
-
await
|
|
70
|
+
const webRequest = await toWebRequest(req, parsedBody);
|
|
71
|
+
const answer = await handlerWithRawHeaders(webRequest, ip, { rawHeaders: req.rawHeaders });
|
|
72
|
+
await writeResponse(answer, res);
|
|
73
73
|
} catch (error) {
|
|
74
74
|
console.error(`The proxy route failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
75
75
|
if (!res.headersSent) {
|