pipeway-adapter-node 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +43 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pierre-Philippe Prévost
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var toWebRequest = (req, body) => {
|
|
5
|
+
const host = req.headers.host ?? "localhost";
|
|
6
|
+
const url = `http://${host}${req.url ?? "/"}`;
|
|
7
|
+
const headers = new Headers();
|
|
8
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
9
|
+
if (Array.isArray(value)) {
|
|
10
|
+
for (const v of value) headers.append(key, v);
|
|
11
|
+
} else if (value !== void 0) {
|
|
12
|
+
headers.set(key, value);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const method = req.method ?? "GET";
|
|
16
|
+
const init = { method, headers };
|
|
17
|
+
if (method !== "GET" && method !== "HEAD" && body.length > 0) {
|
|
18
|
+
init.body = new Uint8Array(body);
|
|
19
|
+
}
|
|
20
|
+
return new Request(url, init);
|
|
21
|
+
};
|
|
22
|
+
var readBody = (req) => new Promise((resolve, reject) => {
|
|
23
|
+
const chunks = [];
|
|
24
|
+
req.on("data", (c) => chunks.push(c));
|
|
25
|
+
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
26
|
+
req.on("error", reject);
|
|
27
|
+
});
|
|
28
|
+
var sendWebResponse = async (res, web) => {
|
|
29
|
+
res.statusCode = web.status;
|
|
30
|
+
web.headers.forEach((value, key) => res.setHeader(key, value));
|
|
31
|
+
const buf = Buffer.from(await web.arrayBuffer());
|
|
32
|
+
res.end(buf);
|
|
33
|
+
};
|
|
34
|
+
var toNode = (handler) => async (req, res) => {
|
|
35
|
+
const body = await readBody(req);
|
|
36
|
+
const webReq = toWebRequest(req, body);
|
|
37
|
+
const webRes = await handler(webReq, req.params ?? {});
|
|
38
|
+
await sendWebResponse(res, webRes);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
exports.toNode = toNode;
|
|
42
|
+
//# sourceMappingURL=index.cjs.map
|
|
43
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts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AAK,CAAC,CAAC,CAAA;AAC5C,EAAA,GAAA,CAAI,EAAA,CAAG,OAAO,MAAM,OAAA,CAAQ,OAAO,MAAA,CAAO,MAAM,CAAC,CAAC,CAAA;AAClD,EAAA,GAAA,CAAI,EAAA,CAAG,SAAS,MAAM,CAAA;AACxB,CAAC,CAAA;AAEH,IAAM,eAAA,GAAkB,OAAO,GAAA,EAAqB,GAAA,KAAiC;AACnF,EAAA,GAAA,CAAI,aAAa,GAAA,CAAI,MAAA;AACrB,EAAA,GAAA,CAAI,OAAA,CAAQ,QAAQ,CAAC,KAAA,EAAO,QAAQ,GAAA,CAAI,SAAA,CAAU,GAAA,EAAK,KAAK,CAAC,CAAA;AAC7D,EAAA,MAAM,MAAM,MAAA,CAAO,IAAA,CAAK,MAAM,GAAA,CAAI,aAAa,CAAA;AAC/C,EAAA,GAAA,CAAI,IAAI,GAAG,CAAA;AACb,CAAA;AAIO,IAAM,MAAA,GACX,CAAS,OAAA,KACT,OAAO,KAA4C,GAAA,KAAuC;AACxF,EAAA,MAAM,IAAA,GAAO,MAAM,QAAA,CAAS,GAAG,CAAA;AAC/B,EAAA,MAAM,MAAA,GAAS,YAAA,CAAa,GAAA,EAAK,IAAI,CAAA;AACrC,EAAA,MAAM,SAAS,MAAM,OAAA,CAAQ,QAAS,GAAA,CAAI,MAAA,IAAW,EAAc,CAAA;AACnE,EAAA,MAAM,eAAA,CAAgB,KAAK,MAAM,CAAA;AACnC","file":"index.cjs","sourcesContent":["import type { IncomingMessage, ServerResponse } from 'node:http'\n\nimport type { CompiledHandler } from 'pipeway'\n\n// Bridges the Node `req/res` world (Express, Fastify, http.Server) to the\n// Web-standard `Request`/`Response` a pipeway handler speaks. Every OTHER runtime\n// (Bun, Deno, Workers, Next, Hono) needs NO adapter — mount the handler directly.\n\nconst toWebRequest = (req: IncomingMessage, body: Buffer): Request => {\n const host = req.headers.host ?? 'localhost'\n const url = `http://${host}${req.url ?? '/'}`\n const headers = new Headers()\n for (const [key, value] of Object.entries(req.headers)) {\n if (Array.isArray(value)) {\n for (const v of value) headers.append(key, v)\n } else if (value !== undefined) {\n headers.set(key, value)\n }\n }\n const method = req.method ?? 'GET'\n const init: RequestInit = { method, headers }\n if (method !== 'GET' && method !== 'HEAD' && body.length > 0) {\n // Buffer is an ArrayBufferView; wrap as Uint8Array so it satisfies BodyInit\n // under the DOM lib types while staying zero-copy.\n init.body = new Uint8Array(body)\n }\n return new Request(url, init)\n}\n\nconst readBody = (req: IncomingMessage): Promise<Buffer> =>\n new Promise((resolve, reject) => {\n const chunks: Buffer[] = []\n req.on('data', (c: Buffer) => chunks.push(c))\n req.on('end', () => resolve(Buffer.concat(chunks)))\n req.on('error', reject)\n })\n\nconst sendWebResponse = async (res: ServerResponse, web: Response): Promise<void> => {\n res.statusCode = web.status\n web.headers.forEach((value, key) => res.setHeader(key, value))\n const buf = Buffer.from(await web.arrayBuffer())\n res.end(buf)\n}\n\n// Wraps a pipeway handler as an Express/Connect/Node middleware.\n// `params` are read from `req.params` (Express) when present.\nexport const toNode =\n <Params>(handler: CompiledHandler<Params>) =>\n async (req: IncomingMessage & { params?: Params }, res: ServerResponse): Promise<void> => {\n const body = await readBody(req)\n const webReq = toWebRequest(req, body)\n const webRes = await handler(webReq, (req.params ?? ({} as Params)))\n await sendWebResponse(res, webRes)\n }\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { CompiledHandler } from 'pipeway';
|
|
3
|
+
|
|
4
|
+
declare const toNode: <Params>(handler: CompiledHandler<Params>) => (req: IncomingMessage & {
|
|
5
|
+
params?: Params;
|
|
6
|
+
}, res: ServerResponse) => Promise<void>;
|
|
7
|
+
|
|
8
|
+
export { toNode };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { CompiledHandler } from 'pipeway';
|
|
3
|
+
|
|
4
|
+
declare const toNode: <Params>(handler: CompiledHandler<Params>) => (req: IncomingMessage & {
|
|
5
|
+
params?: Params;
|
|
6
|
+
}, res: ServerResponse) => Promise<void>;
|
|
7
|
+
|
|
8
|
+
export { toNode };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var toWebRequest = (req, body) => {
|
|
3
|
+
const host = req.headers.host ?? "localhost";
|
|
4
|
+
const url = `http://${host}${req.url ?? "/"}`;
|
|
5
|
+
const headers = new Headers();
|
|
6
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
7
|
+
if (Array.isArray(value)) {
|
|
8
|
+
for (const v of value) headers.append(key, v);
|
|
9
|
+
} else if (value !== void 0) {
|
|
10
|
+
headers.set(key, value);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const method = req.method ?? "GET";
|
|
14
|
+
const init = { method, headers };
|
|
15
|
+
if (method !== "GET" && method !== "HEAD" && body.length > 0) {
|
|
16
|
+
init.body = new Uint8Array(body);
|
|
17
|
+
}
|
|
18
|
+
return new Request(url, init);
|
|
19
|
+
};
|
|
20
|
+
var readBody = (req) => new Promise((resolve, reject) => {
|
|
21
|
+
const chunks = [];
|
|
22
|
+
req.on("data", (c) => chunks.push(c));
|
|
23
|
+
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
24
|
+
req.on("error", reject);
|
|
25
|
+
});
|
|
26
|
+
var sendWebResponse = async (res, web) => {
|
|
27
|
+
res.statusCode = web.status;
|
|
28
|
+
web.headers.forEach((value, key) => res.setHeader(key, value));
|
|
29
|
+
const buf = Buffer.from(await web.arrayBuffer());
|
|
30
|
+
res.end(buf);
|
|
31
|
+
};
|
|
32
|
+
var toNode = (handler) => async (req, res) => {
|
|
33
|
+
const body = await readBody(req);
|
|
34
|
+
const webReq = toWebRequest(req, body);
|
|
35
|
+
const webRes = await handler(webReq, req.params ?? {});
|
|
36
|
+
await sendWebRespon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M,WAAW,CAAC,GAAA,KAChB,IAAI,OAAA,CAAQ,CAAC,SAAS,MAAA,KAAW;AAC/B,EAAA,MAAM,SAAmB,EAAC;AAC1B,EAAA,GAAA,CAAI,GAAG,MAAA,EAAQ,CAAC,MAAc,MAAA,CAAO,IAAA,CAAK,CAAC,CAAC,CAAA;AAC5C,EAAA,GAAA,CAAI,EAAA,CAAG,OAAO,MAAM,OAAA,CAAQ,OAAO,MAAA,CAAO,MAAM,CAAC,CAAC,CAAA;AAClD,EAAA,GAAA,CAAI,EAAA,CAAG,SAAS,MAAM,CAAA;AACxB,CAAC,CAAA;AAEH,IAAM,eAAA,GAAkB,OAAO,GAAA,EAAqB,GAAA,KAAiC;AACnF,EAAA,GAAA,CAAI,aAAa,GAAA,CAAI,MAAA;AACrB,EAAA,GAAA,CAAI,OAAA,CAAQ,QAAQ,CAAC,KAAA,EAAO,QAAQ,GAAA,CAAI,SAAA,CAAU,GAAA,EAAK,KAAK,CAAC,CAAA;AAC7D,EAAA,MAAM,MAAM,MAAA,CAAO,IAAA,CAAK,MAAM,GAAA,CAAI,aAAa,CAAA;AAC/C,EAAA,GAAA,CAAI,IAAI,GAAG,CAAA;AACb,CAAA;AAIO,IAAM,MAAA,GACX,CAAS,OAAA,KACT,OAAO,KAA4C,GAAA,KAAuC;AACxF,EAAA,MAAM,IAAA,GAAO,MAAM,QAAA,CAAS,GAAG,CAAA;AAC/B,EAAA,MAAM,MAAA,GAAS,YAAA,CAAa,GAAA,EAAK,IAAI,CAAA;AACrC,EAAA,MAAM,SAAS,MAAM,OAAA,CAAQ,QAAS,GAAA,CAAI,MAAA,IAAW,EAAc,CAAA;AACnE,EAAA,MAAM,eAAA,CAAgB,KAAK,MAAM,CAAA;AACnC","file":"index.js","sourcesContent":["import type { IncomingMessage, ServerResponse } from 'node:http'\n\nimport type { CompiledHandler } from 'pipeway'\n\n// Bridges the Node `req/res` world (Express, Fastify, http.Server) to the\n// Web-standard `Request`/`Response` a pipeway handler speaks. Every OTHER runtime\n// (Bun, Deno, Workers, Next, Hono) needs NO adapter — mount the handler directly.\n\nconst toWebRequest = (req: IncomingMessage, body: Buffer): Request => {\n const host = req.headers.host ?? 'localhost'\n const url = `http://${host}${req.url ?? '/'}`\n const headers = new Headers()\n for (const [key, value] of Object.entries(req.headers)) {\n if (Array.isArray(value)) {\n for (const v of value) headers.append(key, v)\n } else if (value !== undefined) {\n headers.set(key, value)\n }\n }\n const method = req.method ?? 'GET'\n const init: RequestInit = { method, headers }\n if (method !== 'GET' && method !== 'HEAD' && body.length > 0) {\n // Buffer is an ArrayBufferView; wrap as Uint8Array so it satisfies BodyInit\n // under the DOM lib types while staying zero-copy.\n init.body = new Uint8Array(body)\n }\n return new Request(url, init)\n}\n\nconst readBody = (req: IncomingMessage): Promise<Buffer> =>\n new Promise((resolve, reject) => {\n const chunks: Buffer[] = []\n req.on('data', (c: Buffer) => chunks.push(c))\n req.on('end', () => resolve(Buffer.concat(chunks)))\n req.on('error', reject)\n })\n\nconst sendWebResponse = async (res: ServerResponse, web: Response): Promise<void> => {\n res.statusCode = web.status\n web.headers.forEach((value, key) => res.setHeader(key, value))\n const buf = Buffer.from(await web.arrayBuffer())\n res.end(buf)\n}\n\n// Wraps a pipeway handler as an Express/Connect/Node middleware.\n// `params` are read from `req.params` (Express) when present.\nexport const toNode =\n <Params>(handler: CompiledHandler<Params>) =>\n async (req: IncomingMessage & { params?: Params }, res: ServerResponse): Promise<void> => {\n const body = await readBody(req)\n const webReq = toWebRequest(req, body)\n const webRes = await handler(webReq, (req.params ?? ({} as Params)))\n await sendWebResponse(res, webRes)\n }\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pipeway-adapter-node",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node (Express/Fastify/http) adapter for pipeway. Every other runtime needs no adapter.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pipeway",
|
|
7
|
+
"express",
|
|
8
|
+
"fastify",
|
|
9
|
+
"node",
|
|
10
|
+
"adapter"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Pierre-Philippe Prévost",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/ppprevost/pipeway.git",
|
|
17
|
+
"directory": "packages/adapter-node"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://ppprevost.github.io/pipeway",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"require": "./dist/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"pipeway": "0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^22.7.0",
|
|
40
|
+
"tsup": "^8.3.0",
|
|
41
|
+
"typescript": "^5.6.0",
|
|
42
|
+
"pipeway": "0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup",
|
|
49
|
+
"typecheck": "tsc --noEmit"
|
|
50
|
+
}
|
|
51
|
+
}
|