zeno.id 0.4.0 → 0.6.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.
@@ -1,88 +1 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region src/server/node.ts
3
- const MAX_BODY = 131073;
4
- const readBody = async (request, parsedBody) => {
5
- if (request.method !== "POST") return;
6
- if (request.readableEnded) {
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
- }
11
- const chunks = [];
12
- let size = 0;
13
- for await (const chunk of request) {
14
- const bytes = typeof chunk === "string" ? new TextEncoder().encode(chunk) : chunk;
15
- if (size < MAX_BODY) chunks.push(bytes);
16
- size += bytes.byteLength;
17
- }
18
- const body = new Uint8Array(Math.min(size, MAX_BODY));
19
- let offset = 0;
20
- for (const chunk of chunks) {
21
- const part = chunk.subarray(0, body.length - offset);
22
- body.set(part, offset);
23
- offset += part.byteLength;
24
- }
25
- return new TextDecoder().decode(body);
26
- };
27
- const toWebRequest = async (request, parsedBody) => {
28
- const headers = new Headers();
29
- for (let index = 0; index + 1 < request.rawHeaders.length; index += 2) {
30
- const name = request.rawHeaders[index];
31
- if (!name.startsWith(":")) headers.append(name, request.rawHeaders[index + 1]);
32
- }
33
- const protocol = request.socket.encrypted ? "https" : "http";
34
- const path = request.originalUrl ?? request.url ?? "/";
35
- const host = request.headers.host ?? request.headers[":authority"] ?? "localhost";
36
- const body = await readBody(request, parsedBody);
37
- return new Request(new URL(path, `${protocol}://${String(host)}`), {
38
- method: request.method,
39
- headers,
40
- ...body === void 0 ? {} : { body }
41
- });
42
- };
43
- const writeResponse = async (response, nodeResponse) => {
44
- nodeResponse.statusCode = response.status;
45
- response.headers.forEach((value, name) => {
46
- if (name !== "set-cookie") nodeResponse.setHeader(name, value);
47
- });
48
- const cookies = response.headers.getSetCookie();
49
- if (cookies.length > 0) nodeResponse.setHeader("set-cookie", cookies);
50
- nodeResponse.end(response.body ? new Uint8Array(await response.arrayBuffer()) : void 0);
51
- };
52
- /**
53
- * Mounts the proxy route on Node's `(req, res)`: Express, Fastify or `http.createServer`. `app.all('/zeno',
54
- * nodeHandler(zeno))`, with `zeno` from `createProxy`. It passes the IP address the request came from: Express's and
55
- * Fastify's `req.ip` (behind a proxy of your own, set their trust proxy so it's the visitor's), else the socket's
56
- * address. A body parser in front (`express.json()`) is fine.
57
- */
58
- const nodeHandler = (handler) => {
59
- const handlerWithRawHeaders = handler;
60
- return async (request, response) => {
61
- const req = "raw" in request ? request.raw : request;
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
- let res;
66
- if ("hijack" in response) {
67
- response.hijack();
68
- res = response.raw;
69
- } else res = response;
70
- try {
71
- const webRequest = await toWebRequest(req, parsedBody);
72
- const answer = await handlerWithRawHeaders(webRequest, ip, { rawHeaders: req.rawHeaders });
73
- await writeResponse(answer, res);
74
- } catch (error) {
75
- console.error(`The proxy route failed: ${error instanceof Error ? error.message : String(error)}`);
76
- if (!res.headersSent) {
77
- res.statusCode = 500;
78
- res.setHeader("content-type", "application/json; charset=utf-8");
79
- res.end(JSON.stringify({
80
- error: "server_error",
81
- message: "The proxy route failed; its server log says why"
82
- }));
83
- }
84
- }
85
- };
86
- };
87
- //#endregion
88
- exports.nodeHandler = nodeHandler;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=131073,t=async(t,n)=>{if(t.method!==`POST`)return;if(t.readableEnded)return typeof n==`string`?n:n instanceof Uint8Array?new TextDecoder().decode(n):n===void 0?``:JSON.stringify(n);let r=[],i=0;for await(let n of t){let t=typeof n==`string`?new TextEncoder().encode(n):n;i<e&&r.push(t),i+=t.byteLength}let a=new Uint8Array(Math.min(i,e)),o=0;for(let e of r){let t=e.subarray(0,a.length-o);a.set(t,o),o+=t.byteLength}return new TextDecoder().decode(a)},n=async(e,n)=>{let r=new Headers;for(let t=0;t+1<e.rawHeaders.length;t+=2){let n=e.rawHeaders[t];n.startsWith(`:`)||r.append(n,e.rawHeaders[t+1])}let i=e.socket.encrypted?`https`:`http`,a=e.originalUrl??e.url??`/`,o=e.headers.host??e.headers[`:authority`]??`localhost`,s=await t(e,n);return new Request(new URL(a,`${i}://${String(o)}`),{method:e.method,headers:r,...s===void 0?{}:{body:s}})},r=async(e,t)=>{t.statusCode=e.status,e.headers.forEach((e,n)=>{n!==`set-cookie`&&t.setHeader(n,e)});let n=e.headers.getSetCookie();n.length>0&&t.setHeader(`set-cookie`,n),t.end(e.body?new Uint8Array(await e.arrayBuffer()):void 0)},i=e=>{let t=e;return async(e,i)=>{let a=`raw`in e?e.raw:e,o=e.ip,s=typeof o==`string`?o:a.socket.remoteAddress,c=(`raw`in e,e.body),l;`hijack`in i?(i.hijack(),l=i.raw):l=i;try{let e=await n(a,c),i=await t(e,s,{rawHeaders:a.rawHeaders});await r(i,l)}catch(e){console.error(`The proxy route failed: ${e instanceof Error?e.message:String(e)}`),l.headersSent||(l.statusCode=500,l.setHeader(`content-type`,`application/json; charset=utf-8`),l.end(JSON.stringify({error:`server_error`,message:`The proxy route failed; its server log says why`})))}}};exports.nodeHandler=i;
@@ -1,87 +1 @@
1
- //#region src/server/node.ts
2
- const MAX_BODY = 131073;
3
- const readBody = async (request, parsedBody) => {
4
- if (request.method !== "POST") return;
5
- if (request.readableEnded) {
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
- }
10
- const chunks = [];
11
- let size = 0;
12
- for await (const chunk of request) {
13
- const bytes = typeof chunk === "string" ? new TextEncoder().encode(chunk) : chunk;
14
- if (size < MAX_BODY) chunks.push(bytes);
15
- size += bytes.byteLength;
16
- }
17
- const body = new Uint8Array(Math.min(size, MAX_BODY));
18
- let offset = 0;
19
- for (const chunk of chunks) {
20
- const part = chunk.subarray(0, body.length - offset);
21
- body.set(part, offset);
22
- offset += part.byteLength;
23
- }
24
- return new TextDecoder().decode(body);
25
- };
26
- const toWebRequest = async (request, parsedBody) => {
27
- const headers = new Headers();
28
- for (let index = 0; index + 1 < request.rawHeaders.length; index += 2) {
29
- const name = request.rawHeaders[index];
30
- if (!name.startsWith(":")) headers.append(name, request.rawHeaders[index + 1]);
31
- }
32
- const protocol = request.socket.encrypted ? "https" : "http";
33
- const path = request.originalUrl ?? request.url ?? "/";
34
- const host = request.headers.host ?? request.headers[":authority"] ?? "localhost";
35
- const body = await readBody(request, parsedBody);
36
- return new Request(new URL(path, `${protocol}://${String(host)}`), {
37
- method: request.method,
38
- headers,
39
- ...body === void 0 ? {} : { body }
40
- });
41
- };
42
- const writeResponse = async (response, nodeResponse) => {
43
- nodeResponse.statusCode = response.status;
44
- response.headers.forEach((value, name) => {
45
- if (name !== "set-cookie") nodeResponse.setHeader(name, value);
46
- });
47
- const cookies = response.headers.getSetCookie();
48
- if (cookies.length > 0) nodeResponse.setHeader("set-cookie", cookies);
49
- nodeResponse.end(response.body ? new Uint8Array(await response.arrayBuffer()) : void 0);
50
- };
51
- /**
52
- * Mounts the proxy route on Node's `(req, res)`: Express, Fastify or `http.createServer`. `app.all('/zeno',
53
- * nodeHandler(zeno))`, with `zeno` from `createProxy`. It passes the IP address the request came from: Express's and
54
- * Fastify's `req.ip` (behind a proxy of your own, set their trust proxy so it's the visitor's), else the socket's
55
- * address. A body parser in front (`express.json()`) is fine.
56
- */
57
- const nodeHandler = (handler) => {
58
- const handlerWithRawHeaders = handler;
59
- return async (request, response) => {
60
- const req = "raw" in request ? request.raw : request;
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
- let res;
65
- if ("hijack" in response) {
66
- response.hijack();
67
- res = response.raw;
68
- } else res = response;
69
- try {
70
- const webRequest = await toWebRequest(req, parsedBody);
71
- const answer = await handlerWithRawHeaders(webRequest, ip, { rawHeaders: req.rawHeaders });
72
- await writeResponse(answer, res);
73
- } catch (error) {
74
- console.error(`The proxy route failed: ${error instanceof Error ? error.message : String(error)}`);
75
- if (!res.headersSent) {
76
- res.statusCode = 500;
77
- res.setHeader("content-type", "application/json; charset=utf-8");
78
- res.end(JSON.stringify({
79
- error: "server_error",
80
- message: "The proxy route failed; its server log says why"
81
- }));
82
- }
83
- }
84
- };
85
- };
86
- //#endregion
87
- export { nodeHandler };
1
+ const e=131073,t=async(t,n)=>{if(t.method!==`POST`)return;if(t.readableEnded)return typeof n==`string`?n:n instanceof Uint8Array?new TextDecoder().decode(n):n===void 0?``:JSON.stringify(n);let r=[],i=0;for await(let n of t){let t=typeof n==`string`?new TextEncoder().encode(n):n;i<e&&r.push(t),i+=t.byteLength}let a=new Uint8Array(Math.min(i,e)),o=0;for(let e of r){let t=e.subarray(0,a.length-o);a.set(t,o),o+=t.byteLength}return new TextDecoder().decode(a)},n=async(e,n)=>{let r=new Headers;for(let t=0;t+1<e.rawHeaders.length;t+=2){let n=e.rawHeaders[t];n.startsWith(`:`)||r.append(n,e.rawHeaders[t+1])}let i=e.socket.encrypted?`https`:`http`,a=e.originalUrl??e.url??`/`,o=e.headers.host??e.headers[`:authority`]??`localhost`,s=await t(e,n);return new Request(new URL(a,`${i}://${String(o)}`),{method:e.method,headers:r,...s===void 0?{}:{body:s}})},r=async(e,t)=>{t.statusCode=e.status,e.headers.forEach((e,n)=>{n!==`set-cookie`&&t.setHeader(n,e)});let n=e.headers.getSetCookie();n.length>0&&t.setHeader(`set-cookie`,n),t.end(e.body?new Uint8Array(await e.arrayBuffer()):void 0)},i=e=>{let t=e;return async(e,i)=>{let a=`raw`in e?e.raw:e,o=e.ip,s=typeof o==`string`?o:a.socket.remoteAddress,c=(`raw`in e,e.body),l;`hijack`in i?(i.hijack(),l=i.raw):l=i;try{let e=await n(a,c),i=await t(e,s,{rawHeaders:a.rawHeaders});await r(i,l)}catch(e){console.error(`The proxy route failed: ${e instanceof Error?e.message:String(e)}`),l.headersSent||(l.statusCode=500,l.setHeader(`content-type`,`application/json; charset=utf-8`),l.end(JSON.stringify({error:`server_error`,message:`The proxy route failed; its server log says why`})))}}};export{i as nodeHandler};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zeno.id",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Zeno's SDK for device identification: the browser SDK, and the proxy route and API client for your server",
5
5
  "license": "MIT",
6
6
  "files": [