@kontyra/runtime-node 0.0.0-stage → 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/package.json +25 -4
- package/src/adapter.ts +92 -0
- package/src/index.ts +1 -0
- package/src/test/adapter.test.ts +28 -0
- package/tsconfig.json +18 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kontyra/runtime-node",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node.js HTTP/HTTPS server adapter for Zenor",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -b",
|
|
16
|
+
"test": "node --test dist/test/*.test.js"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@kontyra/core": "*",
|
|
20
|
+
"@kontyra/runtime": "*"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"license": "Proprietary",
|
|
26
|
+
"author": "Kontyra Technologies"
|
|
27
|
+
}
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { ExecutionPipeline } from '@kontyra/runtime';
|
|
3
|
+
|
|
4
|
+
export interface NodeServerOptions {
|
|
5
|
+
port?: number;
|
|
6
|
+
host?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class NodeRuntimeAdapter {
|
|
10
|
+
private pipeline: ExecutionPipeline;
|
|
11
|
+
|
|
12
|
+
constructor(pipeline: ExecutionPipeline) {
|
|
13
|
+
this.pipeline = pipeline;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public createServer(): http.Server {
|
|
17
|
+
return http.createServer(async (nodeReq, nodeRes) => {
|
|
18
|
+
try {
|
|
19
|
+
const webReq = this.toWebRequest(nodeReq);
|
|
20
|
+
const webRes = await this.pipeline.handle(webReq);
|
|
21
|
+
await this.sendWebResponse(webRes, nodeRes);
|
|
22
|
+
} catch (err: any) {
|
|
23
|
+
nodeRes.statusCode = 500;
|
|
24
|
+
nodeRes.setHeader('Content-Type', 'text/plain');
|
|
25
|
+
nodeRes.end('Internal Server Error: ' + err.message);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public toWebRequest(req: http.IncomingMessage): Request {
|
|
31
|
+
const protocol = req.headers['x-forwarded-proto'] || 'http';
|
|
32
|
+
const host = req.headers.host || 'localhost';
|
|
33
|
+
const fullUrl = protocol + '://' + host + (req.url || '/');
|
|
34
|
+
|
|
35
|
+
const headers = new Headers();
|
|
36
|
+
for (const [key, val] of Object.entries(req.headers)) {
|
|
37
|
+
if (val !== undefined) {
|
|
38
|
+
if (Array.isArray(val)) {
|
|
39
|
+
val.forEach((v) => headers.append(key, v));
|
|
40
|
+
} else {
|
|
41
|
+
headers.set(key, val);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const hasBody = req.method !== 'GET' && req.method !== 'HEAD';
|
|
47
|
+
const body = hasBody
|
|
48
|
+
? new ReadableStream({
|
|
49
|
+
start(controller) {
|
|
50
|
+
req.on('data', (chunk) => controller.enqueue(chunk));
|
|
51
|
+
req.on('end', () => controller.close());
|
|
52
|
+
req.on('error', (err) => controller.error(err));
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
: null;
|
|
56
|
+
|
|
57
|
+
return new Request(fullUrl, {
|
|
58
|
+
method: req.method,
|
|
59
|
+
headers,
|
|
60
|
+
body,
|
|
61
|
+
// @ts-ignore Node Request body duplex
|
|
62
|
+
duplex: hasBody ? 'half' : undefined,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public async sendWebResponse(webRes: Response, nodeRes: http.ServerResponse): Promise<void> {
|
|
67
|
+
nodeRes.statusCode = webRes.status;
|
|
68
|
+
nodeRes.statusMessage = webRes.statusText;
|
|
69
|
+
|
|
70
|
+
webRes.headers.forEach((val, key) => {
|
|
71
|
+
nodeRes.setHeader(key, val);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (!webRes.body) {
|
|
75
|
+
nodeRes.end();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const reader = webRes.body.getReader();
|
|
80
|
+
const pump = async () => {
|
|
81
|
+
const { done, value } = await reader.read();
|
|
82
|
+
if (done) {
|
|
83
|
+
nodeRes.end();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
nodeRes.write(value);
|
|
87
|
+
await pump();
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
await pump();
|
|
91
|
+
}
|
|
92
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './adapter.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { ExecutionPipeline } from '@kontyra/runtime';
|
|
4
|
+
import { NodeRuntimeAdapter } from '../adapter.js';
|
|
5
|
+
|
|
6
|
+
test('NodeRuntimeAdapter translates HTTP requests into standard Web Requests', async () => {
|
|
7
|
+
const pipeline = new ExecutionPipeline();
|
|
8
|
+
pipeline.router.insert('/ping', {
|
|
9
|
+
pattern: '/ping',
|
|
10
|
+
handler: async () => new Response('pong', { status: 200 }),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const adapter = new NodeRuntimeAdapter(pipeline);
|
|
14
|
+
const server = adapter.createServer();
|
|
15
|
+
|
|
16
|
+
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', () => resolve()));
|
|
17
|
+
const address = server.address() as any;
|
|
18
|
+
const port = address.port;
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const res = await fetch('http://127.0.0.1:' + port + '/ping');
|
|
22
|
+
assert.equal(res.status, 200);
|
|
23
|
+
const text = await res.text();
|
|
24
|
+
assert.equal(text, 'pong');
|
|
25
|
+
} finally {
|
|
26
|
+
server.close();
|
|
27
|
+
}
|
|
28
|
+
});
|
package/tsconfig.json
ADDED
package/README.md
DELETED