@kontyra/runtime-node 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,14 @@
1
+ import http from 'node:http';
2
+ import { ExecutionPipeline } from '@kontyra/runtime';
3
+ export interface NodeServerOptions {
4
+ port?: number;
5
+ host?: string;
6
+ }
7
+ export declare class NodeRuntimeAdapter {
8
+ private pipeline;
9
+ constructor(pipeline: ExecutionPipeline);
10
+ createServer(): http.Server;
11
+ toWebRequest(req: http.IncomingMessage): Request;
12
+ sendWebResponse(webRes: Response, nodeRes: http.ServerResponse): Promise<void>;
13
+ }
14
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,MAAM,WAAW,iBAAiB;IAChC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,QAAQ,CAAoB;gBAExB,QAAQ,EAAE,iBAAiB;IAIhC,YAAY,IAAI,IAAI,CAAC,MAAM;IAc3B,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,GAAG,OAAO;IAoC1C,eAAe,CAAC,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CA0B5F"}
@@ -0,0 +1,77 @@
1
+ import http from 'node:http';
2
+ export class NodeRuntimeAdapter {
3
+ pipeline;
4
+ constructor(pipeline) {
5
+ this.pipeline = pipeline;
6
+ }
7
+ createServer() {
8
+ return http.createServer(async (nodeReq, nodeRes) => {
9
+ try {
10
+ const webReq = this.toWebRequest(nodeReq);
11
+ const webRes = await this.pipeline.handle(webReq);
12
+ await this.sendWebResponse(webRes, nodeRes);
13
+ }
14
+ catch (err) {
15
+ nodeRes.statusCode = 500;
16
+ nodeRes.setHeader('Content-Type', 'text/plain');
17
+ nodeRes.end('Internal Server Error: ' + err.message);
18
+ }
19
+ });
20
+ }
21
+ toWebRequest(req) {
22
+ const protocol = req.headers['x-forwarded-proto'] || 'http';
23
+ const host = req.headers.host || 'localhost';
24
+ const fullUrl = protocol + '://' + host + (req.url || '/');
25
+ const headers = new Headers();
26
+ for (const [key, val] of Object.entries(req.headers)) {
27
+ if (val !== undefined) {
28
+ if (Array.isArray(val)) {
29
+ val.forEach((v) => headers.append(key, v));
30
+ }
31
+ else {
32
+ headers.set(key, val);
33
+ }
34
+ }
35
+ }
36
+ const hasBody = req.method !== 'GET' && req.method !== 'HEAD';
37
+ const body = hasBody
38
+ ? new ReadableStream({
39
+ start(controller) {
40
+ req.on('data', (chunk) => controller.enqueue(chunk));
41
+ req.on('end', () => controller.close());
42
+ req.on('error', (err) => controller.error(err));
43
+ },
44
+ })
45
+ : null;
46
+ return new Request(fullUrl, {
47
+ method: req.method,
48
+ headers,
49
+ body,
50
+ // @ts-ignore Node Request body duplex
51
+ duplex: hasBody ? 'half' : undefined,
52
+ });
53
+ }
54
+ async sendWebResponse(webRes, nodeRes) {
55
+ nodeRes.statusCode = webRes.status;
56
+ nodeRes.statusMessage = webRes.statusText;
57
+ webRes.headers.forEach((val, key) => {
58
+ nodeRes.setHeader(key, val);
59
+ });
60
+ if (!webRes.body) {
61
+ nodeRes.end();
62
+ return;
63
+ }
64
+ const reader = webRes.body.getReader();
65
+ const pump = async () => {
66
+ const { done, value } = await reader.read();
67
+ if (done) {
68
+ nodeRes.end();
69
+ return;
70
+ }
71
+ nodeRes.write(value);
72
+ await pump();
73
+ };
74
+ await pump();
75
+ }
76
+ }
77
+ //# sourceMappingURL=adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.js","sourceRoot":"","sources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UAAU,GAAG,MAAM,CAAC,MAAM,CAAC;QACnC,OAAO,CAAC,aAAa,GAAG,MAAM,CAAC,UAAU,CAAC;QAE1C,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;YAClC,OAAO,CAAC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAC9B,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACjB,OAAO,CAAC,GAAG,EAAE,CAAC;YACd,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;QACvC,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE;YACtB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YAC5C,IAAI,IAAI,EAAE,CAAC;gBACT,OAAO,CAAC,GAAG,EAAE,CAAC;gBACd,OAAO;YACT,CAAC;YACD,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACrB,MAAM,IAAI,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CACF"}
@@ -0,0 +1,2 @@
1
+ export * from './adapter.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './adapter.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=adapter.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.test.d.ts","sourceRoot":"","sources":["../../src/test/adapter.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,26 @@
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
+ test('NodeRuntimeAdapter translates HTTP requests into standard Web Requests', async () => {
6
+ const pipeline = new ExecutionPipeline();
7
+ pipeline.router.insert('/ping', {
8
+ pattern: '/ping',
9
+ handler: async () => new Response('pong', { status: 200 }),
10
+ });
11
+ const adapter = new NodeRuntimeAdapter(pipeline);
12
+ const server = adapter.createServer();
13
+ await new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve()));
14
+ const address = server.address();
15
+ const port = address.port;
16
+ try {
17
+ const res = await fetch('http://127.0.0.1:' + port + '/ping');
18
+ assert.equal(res.status, 200);
19
+ const text = await res.text();
20
+ assert.equal(text, 'pong');
21
+ }
22
+ finally {
23
+ server.close();
24
+ }
25
+ });
26
+ //# sourceMappingURL=adapter.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.test.js","sourceRoot":"","sources":["../../src/test/adapter.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEnD,IAAI,CAAC,wEAAwE,EAAE,KAAK,IAAI,EAAE;IACxF,MAAM,QAAQ,GAAG,IAAI,iBAAiB,EAAE,CAAC;IACzC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE;QAC9B,OAAO,EAAE,OAAO;QAChB,OAAO,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI,QAAQ,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;KAC3D,CAAC,CAAC;IAEH,MAAM,OAAO,GAAG,IAAI,kBAAkB,CAAC,QAAQ,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;IAEtC,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;IACrF,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAS,CAAC;IACxC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE1B,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,mBAAmB,GAAG,IAAI,GAAG,OAAO,CAAC,CAAC;QAC9D,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAC9B,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;QAC9B,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC7B,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,KAAK,EAAE,CAAC;IACjB,CAAC;AACH,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "@kontyra/runtime-node",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
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": "^0.1.1",
20
+ "@kontyra/runtime": "^0.1.1"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "license": "Proprietary",
26
+ "author": "Kontyra Technologies",
27
+ "files": [
28
+ "dist",
29
+ "src"
30
+ ]
31
+ }
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/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.