@toa.io/extensions.exposition 1.0.0-alpha.3 → 1.0.0-alpha.4
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/components/identity.bans/manifest.toa.yaml +1 -0
- package/components/identity.basic/manifest.toa.yaml +1 -0
- package/components/identity.basic/operations/tsconfig.tsbuildinfo +1 -1
- package/components/identity.federation/manifest.toa.yaml +1 -0
- package/components/identity.federation/operations/tsconfig.tsbuildinfo +1 -1
- package/components/identity.roles/manifest.toa.yaml +1 -0
- package/components/identity.roles/operations/tsconfig.tsbuildinfo +1 -1
- package/components/identity.tokens/manifest.toa.yaml +1 -0
- package/components/identity.tokens/operations/tsconfig.tsbuildinfo +1 -1
- package/components/octets.storage/manifest.toa.yaml +1 -0
- package/components/octets.storage/operations/store.js +1 -1
- package/documentation/components.md +5 -5
- package/documentation/query.md +45 -2
- package/features/body.feature +1 -1
- package/features/errors.feature +1 -1
- package/features/etag.feature +86 -0
- package/features/octets.entries.feature +1 -1
- package/features/steps/Gateway.ts +3 -0
- package/features/steps/components/echo/manifest.toa.yaml +1 -0
- package/features/steps/components/greeter/manifest.toa.yaml +1 -0
- package/features/steps/components/octets.tester/manifest.toa.yaml +1 -0
- package/features/steps/components/pots/manifest.toa.yaml +10 -3
- package/features/steps/components/sequences/manifest.toa.yaml +1 -0
- package/features/timing.feature +43 -0
- package/package.json +7 -10
- package/readme.md +7 -6
- package/schemas/annotation.cos.yaml +1 -0
- package/schemas/querystring.cos.yaml +1 -0
- package/source/Annotation.ts +1 -0
- package/source/Directive.test.ts +3 -3
- package/source/Directive.ts +11 -11
- package/source/Endpoint.ts +18 -4
- package/source/Factory.ts +8 -4
- package/source/Gateway.ts +55 -42
- package/source/HTTP/Context.ts +67 -0
- package/source/HTTP/Server.test.ts +1 -1
- package/source/HTTP/Server.ts +60 -95
- package/source/HTTP/Timing.ts +40 -0
- package/source/HTTP/index.ts +1 -0
- package/source/HTTP/messages.test.ts +27 -8
- package/source/HTTP/messages.ts +32 -48
- package/source/Mapping.ts +7 -8
- package/source/deployment.ts +6 -0
- package/source/directives/auth/Anonymous.ts +3 -2
- package/source/directives/auth/Authorization.ts +5 -3
- package/source/directives/auth/Incept.ts +11 -6
- package/source/directives/auth/Role.ts +5 -3
- package/source/directives/auth/Scheme.ts +2 -2
- package/source/directives/cache/Cache.ts +2 -2
- package/source/directives/cache/Control.ts +5 -5
- package/source/directives/cache/types.ts +1 -1
- package/source/directives/cors/CORS.ts +5 -3
- package/source/directives/octets/Context.ts +1 -1
- package/source/directives/octets/Delete.ts +19 -9
- package/source/directives/octets/Fetch.ts +29 -14
- package/source/directives/octets/List.ts +14 -6
- package/source/directives/octets/Octets.ts +4 -2
- package/source/directives/octets/Permute.ts +12 -6
- package/source/directives/octets/Store.ts +18 -16
- package/source/directives/octets/Workflow.ts +3 -3
- package/source/directives/octets/workflows/Workflow.ts +2 -2
- package/source/directives/vary/Vary.ts +1 -1
- package/source/directives/vary/embeddings/Header.ts +1 -1
- package/source/directives/vary/embeddings/Language.ts +1 -1
- package/source/io.ts +2 -2
- package/transpiled/Annotation.d.ts +1 -0
- package/transpiled/Directive.d.ts +6 -6
- package/transpiled/Directive.js +8 -8
- package/transpiled/Directive.js.map +1 -1
- package/transpiled/Endpoint.d.ts +3 -3
- package/transpiled/Endpoint.js +34 -1
- package/transpiled/Endpoint.js.map +1 -1
- package/transpiled/Factory.js +4 -2
- package/transpiled/Factory.js.map +1 -1
- package/transpiled/Gateway.d.ts +5 -6
- package/transpiled/Gateway.js +38 -32
- package/transpiled/Gateway.js.map +1 -1
- package/transpiled/HTTP/Context.d.ts +24 -0
- package/transpiled/HTTP/Context.js +47 -0
- package/transpiled/HTTP/Context.js.map +1 -0
- package/transpiled/HTTP/Server.d.ts +8 -7
- package/transpiled/HTTP/Server.js +68 -76
- package/transpiled/HTTP/Server.js.map +1 -1
- package/transpiled/HTTP/Timing.d.ts +10 -0
- package/transpiled/HTTP/Timing.js +29 -0
- package/transpiled/HTTP/Timing.js.map +1 -0
- package/transpiled/HTTP/index.d.ts +1 -0
- package/transpiled/HTTP/index.js +1 -0
- package/transpiled/HTTP/index.js.map +1 -1
- package/transpiled/HTTP/messages.d.ts +7 -21
- package/transpiled/HTTP/messages.js +24 -26
- package/transpiled/HTTP/messages.js.map +1 -1
- package/transpiled/Mapping.js +7 -7
- package/transpiled/Mapping.js.map +1 -1
- package/transpiled/deployment.js +5 -0
- package/transpiled/deployment.js.map +1 -1
- package/transpiled/directives/auth/Anonymous.js +3 -4
- package/transpiled/directives/auth/Anonymous.js.map +1 -1
- package/transpiled/directives/auth/Authorization.js +1 -1
- package/transpiled/directives/auth/Authorization.js.map +1 -1
- package/transpiled/directives/auth/Incept.d.ts +1 -1
- package/transpiled/directives/auth/Incept.js +11 -6
- package/transpiled/directives/auth/Incept.js.map +1 -1
- package/transpiled/directives/auth/Role.js +5 -3
- package/transpiled/directives/auth/Role.js.map +1 -1
- package/transpiled/directives/auth/Scheme.js +2 -2
- package/transpiled/directives/auth/Scheme.js.map +1 -1
- package/transpiled/directives/cache/Cache.d.ts +1 -1
- package/transpiled/directives/cache/Cache.js +2 -2
- package/transpiled/directives/cache/Cache.js.map +1 -1
- package/transpiled/directives/cache/Control.d.ts +3 -3
- package/transpiled/directives/cache/Control.js +3 -3
- package/transpiled/directives/cache/Control.js.map +1 -1
- package/transpiled/directives/cache/types.d.ts +1 -1
- package/transpiled/directives/cors/CORS.js +4 -3
- package/transpiled/directives/cors/CORS.js.map +1 -1
- package/transpiled/directives/octets/Context.d.ts +1 -1
- package/transpiled/directives/octets/Context.js.map +1 -1
- package/transpiled/directives/octets/Delete.d.ts +1 -1
- package/transpiled/directives/octets/Delete.js +19 -9
- package/transpiled/directives/octets/Delete.js.map +1 -1
- package/transpiled/directives/octets/Fetch.d.ts +1 -1
- package/transpiled/directives/octets/Fetch.js +28 -14
- package/transpiled/directives/octets/Fetch.js.map +1 -1
- package/transpiled/directives/octets/List.d.ts +1 -1
- package/transpiled/directives/octets/List.js +13 -6
- package/transpiled/directives/octets/List.js.map +1 -1
- package/transpiled/directives/octets/Octets.js +4 -2
- package/transpiled/directives/octets/Octets.js.map +1 -1
- package/transpiled/directives/octets/Permute.d.ts +1 -1
- package/transpiled/directives/octets/Permute.js +11 -6
- package/transpiled/directives/octets/Permute.js.map +1 -1
- package/transpiled/directives/octets/Store.d.ts +1 -1
- package/transpiled/directives/octets/Store.js +11 -11
- package/transpiled/directives/octets/Store.js.map +1 -1
- package/transpiled/directives/octets/Workflow.d.ts +1 -1
- package/transpiled/directives/octets/Workflow.js +3 -3
- package/transpiled/directives/octets/Workflow.js.map +1 -1
- package/transpiled/directives/octets/workflows/Workflow.d.ts +1 -1
- package/transpiled/directives/octets/workflows/Workflow.js +2 -2
- package/transpiled/directives/octets/workflows/Workflow.js.map +1 -1
- package/transpiled/directives/vary/Vary.js +1 -1
- package/transpiled/directives/vary/embeddings/Header.js +1 -1
- package/transpiled/directives/vary/embeddings/Header.js.map +1 -1
- package/transpiled/directives/vary/embeddings/Language.js +1 -1
- package/transpiled/directives/vary/embeddings/Language.js.map +1 -1
- package/transpiled/io.d.ts +2 -2
- package/transpiled/tsconfig.tsbuildinfo +1 -1
- package/source/HTTP/Server.fixtures.ts +0 -40
- package/transpiled/HTTP/Server.fixtures.d.ts +0 -10
- package/transpiled/HTTP/Server.fixtures.js +0 -31
- package/transpiled/HTTP/Server.fixtures.js.map +0 -1
package/transpiled/Endpoint.js
CHANGED
|
@@ -1,7 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.EndpointsFactory = exports.Endpoint = void 0;
|
|
4
27
|
const Mapping_1 = require("./Mapping");
|
|
28
|
+
const http = __importStar(require("./HTTP"));
|
|
5
29
|
class Endpoint {
|
|
6
30
|
endpoint;
|
|
7
31
|
mapping;
|
|
@@ -15,7 +39,16 @@ class Endpoint {
|
|
|
15
39
|
async call(body, query, parameters) {
|
|
16
40
|
const request = this.mapping.fit(body, query, parameters);
|
|
17
41
|
this.remote ??= await this.discovery;
|
|
18
|
-
|
|
42
|
+
const reply = await this.remote.invoke(this.endpoint, request);
|
|
43
|
+
if (reply instanceof Error)
|
|
44
|
+
throw new http.Conflict(reply);
|
|
45
|
+
const message = { body: reply };
|
|
46
|
+
if (typeof reply === 'object' && reply !== null && '_version' in reply) {
|
|
47
|
+
message.headers ??= new Headers();
|
|
48
|
+
message.headers.set('etag', `"${reply._version.toString()}"`);
|
|
49
|
+
delete reply._version;
|
|
50
|
+
}
|
|
51
|
+
return message;
|
|
19
52
|
}
|
|
20
53
|
async close() {
|
|
21
54
|
this.remote ??= await this.discovery;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Endpoint.js","sourceRoot":"","sources":["../source/Endpoint.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Endpoint.js","sourceRoot":"","sources":["../source/Endpoint.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,uCAAmC;AAEnC,6CAA8B;AAG9B,MAAa,QAAQ;IACF,QAAQ,CAAQ;IAChB,OAAO,CAAS;IAChB,SAAS,CAAoB;IACtC,MAAM,GAAqB,IAAI,CAAA;IAEvC,YAAoB,QAAgB,EAAE,OAAgB,EAAE,SAA6B;QACnF,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;IAC5B,CAAC;IAEM,KAAK,CAAC,IAAI,CAChB,IAAS,EAAE,KAAiB,EAAE,UAA2B;QACxD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE,UAAU,CAAC,CAAA;QAEzD,IAAI,CAAC,MAAM,KAAK,MAAM,IAAI,CAAC,SAAS,CAAA;QAEpC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;QAE9D,IAAI,KAAK,YAAY,KAAK;YACxB,MAAM,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QAEhC,MAAM,OAAO,GAAyB,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;QAErD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,UAAU,IAAI,KAAK,EAAE,CAAC;YACvE,OAAO,CAAC,OAAO,KAAK,IAAI,OAAO,EAAE,CAAA;YACjC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,M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CAAA"}
|
package/transpiled/Factory.js
CHANGED
|
@@ -46,8 +46,9 @@ class Factory {
|
|
|
46
46
|
}
|
|
47
47
|
service() {
|
|
48
48
|
const debug = process.env.TOA_EXPOSITION_DEBUG === '1';
|
|
49
|
+
const trace = process.env.TOA_EXPOSITION_TRACE === '1';
|
|
49
50
|
const broadcast = this.boot.bindings.broadcast(CHANNEL);
|
|
50
|
-
const server = HTTP_1.Server.create({ methods: RTD_1.syntax.verbs, debug });
|
|
51
|
+
const server = HTTP_1.Server.create({ methods: RTD_1.syntax.verbs, debug, trace });
|
|
51
52
|
const remotes = new Remotes_1.Remotes(this.boot);
|
|
52
53
|
const node = root.resolve();
|
|
53
54
|
const methods = new Endpoint_1.EndpointsFactory(remotes);
|
|
@@ -55,9 +56,10 @@ class Factory {
|
|
|
55
56
|
const interception = new Interception_1.Interception(directives_1.interceptors);
|
|
56
57
|
const tree = new RTD_1.Tree(node, methods, directives);
|
|
57
58
|
const composition = new Composition_1.Composition(this.boot);
|
|
58
|
-
const gateway = new Gateway_1.Gateway(broadcast,
|
|
59
|
+
const gateway = new Gateway_1.Gateway(broadcast, tree, interception);
|
|
59
60
|
gateway.depends(remotes);
|
|
60
61
|
gateway.depends(composition);
|
|
62
|
+
server.attach(gateway.process.bind(gateway));
|
|
61
63
|
server.depends(gateway);
|
|
62
64
|
return server;
|
|
63
65
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Factory.js","sourceRoot":"","sources":["../source/Factory.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAAiC;AACjC,uCAAmC;AACnC,uCAAmC;AACnC,+BAAoC;AACpC,iCAA+B;AAC/B,yCAA4D;AAC5D,6CAAqD;AACrD,2CAAgE;AAChE,+CAA2C;AAC3C,6CAA8B;AAC9B,iDAA6C;
|
|
1
|
+
{"version":3,"file":"Factory.js","sourceRoot":"","sources":["../source/Factory.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;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,CAAA;QACnD,MAAM,IAAI,GAAG,IAAI,UAAI,CAAuB,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC,CAAA;QAEtE,MAAM,WAAW,GAAG,IAAI,yBAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAC9C,MAAM,OAAO,GAAG,IAAI,iBAAO,CAAC,SAAS,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;QAE1D,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;QACxB,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,CAAA;QAE5B,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;QAC5C,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;QAEvB,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AApCD,0BAoCC;AAED,MAAM,OAAO,GAAG,YAAY,CAAA"}
|
package/transpiled/Gateway.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type bindings, Connector } from '@toa.io/core';
|
|
2
2
|
import * as http from './HTTP';
|
|
3
|
-
import type {
|
|
3
|
+
import type { Interception } from './Interception';
|
|
4
4
|
import type { Tree } from './RTD';
|
|
5
5
|
import type { Label } from './discovery';
|
|
6
6
|
import type { Endpoint } from './Endpoint';
|
|
@@ -9,14 +9,13 @@ export declare class Gateway extends Connector {
|
|
|
9
9
|
private readonly broadcast;
|
|
10
10
|
private readonly tree;
|
|
11
11
|
private readonly interceptor;
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
constructor(broadcast: Broadcast, tree: Tree<Endpoint, Directives>, interception: Interception);
|
|
13
|
+
process(context: http.Context): Promise<http.OutgoingMessage>;
|
|
14
14
|
protected open(): Promise<void>;
|
|
15
15
|
protected dispose(): void;
|
|
16
|
-
private process;
|
|
17
16
|
private call;
|
|
17
|
+
private query;
|
|
18
18
|
private discover;
|
|
19
19
|
private merge;
|
|
20
20
|
}
|
|
21
|
-
type Broadcast = bindings.Broadcast<Label>;
|
|
22
|
-
export {};
|
|
21
|
+
export type Broadcast = bindings.Broadcast<Label>;
|
package/transpiled/Gateway.js
CHANGED
|
@@ -31,57 +31,62 @@ class Gateway extends core_1.Connector {
|
|
|
31
31
|
broadcast;
|
|
32
32
|
tree;
|
|
33
33
|
interceptor;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
constructor(broadcast, server, tree, interception) {
|
|
34
|
+
// eslint-disable-next-line max-len
|
|
35
|
+
constructor(broadcast, tree, interception) {
|
|
37
36
|
super();
|
|
38
37
|
this.broadcast = broadcast;
|
|
39
38
|
this.tree = tree;
|
|
40
39
|
this.interceptor = interception;
|
|
41
|
-
this.server = server;
|
|
42
40
|
this.depends(broadcast);
|
|
43
|
-
// this.depends(server)
|
|
44
|
-
server.attach(this.process.bind(this));
|
|
45
41
|
}
|
|
46
|
-
async
|
|
47
|
-
await this.
|
|
48
|
-
console.info('Gateway has started and is awaiting resource branches.');
|
|
49
|
-
}
|
|
50
|
-
dispose() {
|
|
51
|
-
console.info('Gateway is closed.');
|
|
52
|
-
}
|
|
53
|
-
async process(request) {
|
|
54
|
-
const interception = await this.interceptor.intercept(request);
|
|
42
|
+
async process(context) {
|
|
43
|
+
const interception = await context.timing.capture('gate:intercept', this.interceptor.intercept(context));
|
|
55
44
|
if (interception !== null)
|
|
56
45
|
return interception;
|
|
57
|
-
const match = this.tree.match(
|
|
46
|
+
const match = this.tree.match(context.url.pathname);
|
|
58
47
|
if (match === null)
|
|
59
48
|
throw new http.NotFound();
|
|
60
49
|
const { node, parameters } = match;
|
|
61
|
-
if (!(request.method in node.methods))
|
|
50
|
+
if (!(context.request.method in node.methods))
|
|
62
51
|
throw new http.MethodNotAllowed();
|
|
63
|
-
const method = node.methods[request.method];
|
|
64
|
-
const interruption = await method.directives.preflight(
|
|
65
|
-
const response = interruption ??
|
|
66
|
-
|
|
52
|
+
const method = node.methods[context.request.method];
|
|
53
|
+
const interruption = await context.timing.capture('gate:preflight', method.directives.preflight(context, parameters));
|
|
54
|
+
const response = interruption ??
|
|
55
|
+
await context.timing.capture('gate:call', this.call(method, context, parameters));
|
|
56
|
+
await context.timing.capture('gate:settle', method.directives.settle(context, response));
|
|
67
57
|
return response;
|
|
68
58
|
}
|
|
69
|
-
async
|
|
70
|
-
|
|
59
|
+
async open() {
|
|
60
|
+
await this.discover();
|
|
61
|
+
console.info('Gateway has started and is awaiting resource branches.');
|
|
62
|
+
}
|
|
63
|
+
dispose() {
|
|
64
|
+
console.info('Gateway is closed.');
|
|
65
|
+
}
|
|
66
|
+
async call(method, context, parameters) {
|
|
67
|
+
if (context.url.pathname[context.url.pathname.length - 1] !== '/')
|
|
71
68
|
throw new http.NotFound('Trailing slash is required.');
|
|
72
|
-
if (
|
|
69
|
+
if (context.encoder === null)
|
|
73
70
|
throw new http.NotAcceptable();
|
|
74
71
|
if (method.endpoint === null)
|
|
75
72
|
throw new http.MethodNotAllowed();
|
|
76
|
-
const body = await
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
.call(body, request.query, parameters)
|
|
73
|
+
const body = await context.body();
|
|
74
|
+
const query = this.query(context);
|
|
75
|
+
return await method.endpoint
|
|
76
|
+
.call(body, query, parameters)
|
|
81
77
|
.catch(exceptions_1.rethrow);
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
78
|
+
}
|
|
79
|
+
query(context) {
|
|
80
|
+
const query = Object.fromEntries(context.url.searchParams);
|
|
81
|
+
const etag = context.request.headers['if-match'];
|
|
82
|
+
if (etag !== undefined) {
|
|
83
|
+
const match = etag.match(ETAG);
|
|
84
|
+
if (match === null)
|
|
85
|
+
throw new http.BadRequest('Invalid ETag.');
|
|
86
|
+
else
|
|
87
|
+
query.version = parseInt(match.groups.version);
|
|
88
|
+
}
|
|
89
|
+
return query;
|
|
85
90
|
}
|
|
86
91
|
async discover() {
|
|
87
92
|
await this.broadcast.receive('expose', this.merge.bind(this));
|
|
@@ -99,4 +104,5 @@ class Gateway extends core_1.Connector {
|
|
|
99
104
|
}
|
|
100
105
|
}
|
|
101
106
|
exports.Gateway = Gateway;
|
|
107
|
+
const ETAG = /^"(?<version>\d{1,32})"$/;
|
|
102
108
|
//# sourceMappingURL=Gateway.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Gateway.js","sourceRoot":"","sources":["../source/Gateway.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAuD;AACvD,6CAA8B;AAC9B,6CAAsC;
|
|
1
|
+
{"version":3,"file":"Gateway.js","sourceRoot":"","sources":["../source/Gateway.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAuD;AACvD,6CAA8B;AAC9B,6CAAsC;AAQtC,MAAa,OAAQ,SAAQ,gBAAS;IACnB,SAAS,CAAW;IACpB,IAAI,CAA4B;IAChC,WAAW,CAAc;IAE1C,mCAAmC;IACnC,YAAoB,SAAoB,EAAE,IAAgC,EAAE,YAA0B;QACpG,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;QAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,WAAW,GAAG,YAAY,CAAA;QAE/B,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IACzB,CAAC;IAEM,KAAK,CAAC,OAAO,CAAE,OAAqB;QACzC,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,gBAAgB,EAChE,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAA;QAEtC,IAAI,YAAY,KAAK,IAAI;YACvB,OAAO,YAAY,CAAA;QAErB,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QAEnD,IAAI,KAAK,KAAK,IAAI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QAAQ,CAAO,MAAM,EAAE,IAAI,CAAC,CAAA;IACnD,CAAC;IAEO,KAAK,CAAE,MAAc;QAC3B,IAAI,CAAC;YACH,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;YAEpC,OAAO,CAAC,IAAI,CAAC,qBAAqB;gBAChC,IAAI,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS,oBAAoB,CAAC,CAAA;QACjE,CAAC;QAAC,OAAO,SAAS,EAAE,CAAC;YACnB,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;QAC1B,CAAC;IACH,CAAC;CACF;AA9GD,0BA8GC;AAED,MAAM,IAAI,GAAG,0BAA0B,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Timing } from './Timing';
|
|
3
|
+
import { type Format } from './formats';
|
|
4
|
+
import type { OutgoingMessage } from './messages';
|
|
5
|
+
import type * as http from 'node:http';
|
|
6
|
+
export declare class Context {
|
|
7
|
+
readonly request: IncomingMessage;
|
|
8
|
+
readonly url: URL;
|
|
9
|
+
readonly subtype: string | null;
|
|
10
|
+
readonly encoder: Format | null;
|
|
11
|
+
readonly timing: Timing;
|
|
12
|
+
readonly pipelines: Pipelines;
|
|
13
|
+
constructor(request: IncomingMessage, trace?: boolean);
|
|
14
|
+
body<T>(): Promise<T>;
|
|
15
|
+
}
|
|
16
|
+
export interface IncomingMessage extends http.IncomingMessage {
|
|
17
|
+
url: string;
|
|
18
|
+
method: string;
|
|
19
|
+
}
|
|
20
|
+
interface Pipelines {
|
|
21
|
+
body: Array<(input: unknown) => unknown>;
|
|
22
|
+
response: Array<(output: OutgoingMessage) => void>;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Context = void 0;
|
|
7
|
+
const negotiator_1 = __importDefault(require("negotiator"));
|
|
8
|
+
const Timing_1 = require("./Timing");
|
|
9
|
+
const formats_1 = require("./formats");
|
|
10
|
+
const messages_1 = require("./messages");
|
|
11
|
+
class Context {
|
|
12
|
+
request;
|
|
13
|
+
url;
|
|
14
|
+
subtype = null;
|
|
15
|
+
encoder = null;
|
|
16
|
+
timing;
|
|
17
|
+
pipelines = {
|
|
18
|
+
body: [],
|
|
19
|
+
response: []
|
|
20
|
+
};
|
|
21
|
+
constructor(request, trace = false) {
|
|
22
|
+
this.request = request;
|
|
23
|
+
this.url = new URL(request.url, `https://${request.headers.host}`);
|
|
24
|
+
this.timing = new Timing_1.Timing(trace);
|
|
25
|
+
if (this.request.headers.accept !== undefined) {
|
|
26
|
+
const match = SUBTYPE.exec(this.request.headers.accept);
|
|
27
|
+
if (match !== null) {
|
|
28
|
+
const { type, subtype, suffix } = match.groups;
|
|
29
|
+
this.request.headers.accept = `${type}/${suffix}`;
|
|
30
|
+
this.subtype = subtype;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const negotiator = new negotiator_1.default(this.request);
|
|
34
|
+
const mediaType = negotiator.mediaType(formats_1.types);
|
|
35
|
+
if (mediaType !== undefined)
|
|
36
|
+
this.encoder = formats_1.formats[mediaType];
|
|
37
|
+
}
|
|
38
|
+
async body() {
|
|
39
|
+
const value = await (0, messages_1.read)(this);
|
|
40
|
+
return this.pipelines.body.length === 0
|
|
41
|
+
? value
|
|
42
|
+
: this.pipelines.body.reduce((value, transform) => transform(value), value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.Context = Context;
|
|
46
|
+
const SUBTYPE = /^(?<type>\w{1,32})\/(vnd\.toa\.(?<subtype>\S{1,32})\+)(?<suffix>\S{1,32})$/;
|
|
47
|
+
//# sour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AAS,GAAG,UAAU,CAAC,SAAS,CAAC,eAAK,CAAC,CAAA;QAE7C,IAAI,SAAS,KAAK,SAAS;YACzB,IAAI,CAAC,OAAO,GAAG,iBAAO,CAAC,SAAS,CAAC,CAAA;IACrC,CAAC;IAEM,KAAK,CAAC,IAAI;QACf,MAAM,KAAK,GAAG,MAAM,IAAA,eAAI,EAAC,IAAI,CAAC,CAAA;QAE9B,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC;YACrC,CAAC,CAAC,KAAK;YACP,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,CAAA;IAC/E,CAAC;CACF;AA/CD,0BA+CC;AAYD,MAAM,OAAO,GAAG,4EAA4E,CAAA"}
|
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
import { Connector } from '@toa.io/core';
|
|
2
|
-
import { type
|
|
2
|
+
import { type OutgoingMessage } from './messages';
|
|
3
|
+
import { Context } from './Context';
|
|
3
4
|
export declare class Server extends Connector {
|
|
4
|
-
private server
|
|
5
|
-
private readonly
|
|
6
|
-
private
|
|
7
|
-
private readonly requestedPort;
|
|
5
|
+
private readonly server;
|
|
6
|
+
private readonly properties;
|
|
7
|
+
private process?;
|
|
8
8
|
private constructor();
|
|
9
9
|
get port(): number;
|
|
10
10
|
static create(options?: Partial<Properties>): Server;
|
|
11
11
|
attach(process: Processing): void;
|
|
12
12
|
protected open(): Promise<void>;
|
|
13
13
|
protected close(): Promise<void>;
|
|
14
|
-
private
|
|
14
|
+
private listener;
|
|
15
15
|
private success;
|
|
16
16
|
private fail;
|
|
17
17
|
}
|
|
18
18
|
interface Properties {
|
|
19
19
|
methods: Set<string>;
|
|
20
20
|
debug: boolean;
|
|
21
|
+
trace: boolean;
|
|
21
22
|
port: number;
|
|
22
23
|
}
|
|
23
|
-
export type Processing = (input:
|
|
24
|
+
export type Processing = (input: Context) => Promise<OutgoingMessage>;
|
|
24
25
|
export {};
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
27
|
};
|
|
@@ -6,27 +29,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
29
|
exports.Server = void 0;
|
|
7
30
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
31
|
const node_os_1 = __importDefault(require("node:os"));
|
|
9
|
-
const
|
|
32
|
+
const http = __importStar(require("node:http"));
|
|
33
|
+
const node_assert_1 = __importDefault(require("node:assert"));
|
|
34
|
+
const node_events_1 = require("node:events");
|
|
10
35
|
const core_1 = require("@toa.io/core");
|
|
11
36
|
const generic_1 = require("@toa.io/generic");
|
|
12
|
-
const negotiator_1 = __importDefault(require("negotiator"));
|
|
13
37
|
const messages_1 = require("./messages");
|
|
14
38
|
const exceptions_1 = require("./exceptions");
|
|
15
|
-
const
|
|
39
|
+
const Context_1 = require("./Context");
|
|
16
40
|
class Server extends core_1.Connector {
|
|
17
|
-
server;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
constructor(app, debug, port) {
|
|
41
|
+
server = http.createServer();
|
|
42
|
+
properties;
|
|
43
|
+
process;
|
|
44
|
+
constructor(properties) {
|
|
22
45
|
super();
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
this.requestedPort = port;
|
|
46
|
+
this.properties = properties;
|
|
47
|
+
this.server.on('request', (req, res) => this.listener(req, res));
|
|
26
48
|
}
|
|
27
49
|
get port() {
|
|
28
|
-
if (this.
|
|
29
|
-
return this.
|
|
50
|
+
if (this.properties.port !== 0)
|
|
51
|
+
return this.properties.port;
|
|
30
52
|
const address = this.server.address();
|
|
31
53
|
if (address === null || typeof address === 'string')
|
|
32
54
|
throw new Error('Server is not listening on a port.');
|
|
@@ -36,101 +58,71 @@ class Server extends core_1.Connector {
|
|
|
36
58
|
const properties = options === undefined
|
|
37
59
|
? DEFAULTS
|
|
38
60
|
: { ...DEFAULTS, ...options };
|
|
39
|
-
|
|
40
|
-
app.disable('x-powered-by');
|
|
41
|
-
app.use(supportedMethods(properties.methods));
|
|
42
|
-
return new Server(app, properties.debug, properties.port);
|
|
61
|
+
return new Server(properties);
|
|
43
62
|
}
|
|
44
63
|
attach(process) {
|
|
45
|
-
this.
|
|
46
|
-
const message = this.extend(request);
|
|
47
|
-
process(message)
|
|
48
|
-
.then(this.success(message, response))
|
|
49
|
-
.catch(this.fail(message, response));
|
|
50
|
-
});
|
|
64
|
+
this.process = process;
|
|
51
65
|
}
|
|
52
66
|
async open() {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
await listening;
|
|
67
|
+
this.server.listen(this.properties.port);
|
|
68
|
+
await (0, node_events_1.once)(this.server, 'listening');
|
|
56
69
|
console.info('HTTP Server is listening.');
|
|
57
70
|
}
|
|
58
71
|
async close() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
await
|
|
62
|
-
this.server = undefined;
|
|
72
|
+
this.server.close();
|
|
73
|
+
console.info('HTTP Server stopped accepting new connections.');
|
|
74
|
+
await (0, node_events_1.once)(this.server, 'close');
|
|
63
75
|
console.info('HTTP Server has been stopped.');
|
|
64
76
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
listener(request, response) {
|
|
78
|
+
if (request.method === undefined || !this.properties.methods.has(request.method)) {
|
|
79
|
+
response.writeHead(501).end();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (request.url === undefined) {
|
|
83
|
+
response.writeHead(400).end();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
node_assert_1.default.ok(this.process !== undefined, 'No processing function has been attached to the server.');
|
|
87
|
+
const context = new Context_1.Context(request, this.properties.trace);
|
|
88
|
+
this.process(context)
|
|
89
|
+
.then(this.success(context, response))
|
|
90
|
+
.catch(this.fail(context, response));
|
|
76
91
|
}
|
|
77
|
-
success(
|
|
92
|
+
success(context, response) {
|
|
78
93
|
return (message) => {
|
|
79
94
|
let status = message.status;
|
|
80
95
|
if (status === undefined)
|
|
81
96
|
if (message.body === null)
|
|
82
97
|
status = 404;
|
|
83
|
-
else if (request.method === 'POST')
|
|
98
|
+
else if (context.request.method === 'POST')
|
|
84
99
|
status = 201;
|
|
85
100
|
else if (message.body === undefined)
|
|
86
101
|
status = 204;
|
|
87
102
|
else
|
|
88
103
|
status = 200;
|
|
89
|
-
response.status
|
|
90
|
-
(0, messages_1.write)(
|
|
104
|
+
response.statusCode = status;
|
|
105
|
+
(0, messages_1.write)(context, response, message);
|
|
91
106
|
};
|
|
92
107
|
}
|
|
93
|
-
fail(
|
|
108
|
+
fail(context, response) {
|
|
94
109
|
return async (exception) => {
|
|
95
|
-
if (!request.complete)
|
|
96
|
-
await adam(request);
|
|
97
|
-
|
|
110
|
+
if (!context.request.complete)
|
|
111
|
+
await adam(context.request);
|
|
112
|
+
response.statusCode = exception instanceof exceptions_1.Exception
|
|
98
113
|
? exception.status
|
|
99
114
|
: 500;
|
|
100
|
-
response.status(status);
|
|
101
115
|
const message = {};
|
|
102
|
-
const verbose = exception instanceof exceptions_1.ClientError || this.debug;
|
|
116
|
+
const verbose = exception instanceof exceptions_1.ClientError || this.properties.debug;
|
|
103
117
|
if (verbose)
|
|
104
118
|
message.body = exception instanceof exceptions_1.Exception
|
|
105
119
|
? exception.body
|
|
106
120
|
: (exception.stack ?? exception.message);
|
|
107
|
-
(0, messages_1.write)(
|
|
121
|
+
(0, messages_1.write)(context, response, message);
|
|
108
122
|
};
|
|
109
123
|
}
|
|
110
124
|
}
|
|
111
125
|
exports.Server = Server;
|
|
112
|
-
function supportedMethods(methods) {
|
|
113
|
-
return (req, res, next) => {
|
|
114
|
-
if (methods.has(req.method))
|
|
115
|
-
next();
|
|
116
|
-
else
|
|
117
|
-
res.sendStatus(501);
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
function negotiate(request, message) {
|
|
121
|
-
if (request.headers.accept !== undefined) {
|
|
122
|
-
const match = SUBTYPE.exec(request.headers.accept);
|
|
123
|
-
if (match !== null) {
|
|
124
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
125
|
-
const { type, subtype, suffix } = match.groups;
|
|
126
|
-
request.headers.accept = `${type}/${suffix}`;
|
|
127
|
-
message.subtype = subtype;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
const negotiator = new negotiator_1.default(request);
|
|
131
|
-
const mediaType = negotiator.mediaType(formats_1.types);
|
|
132
|
-
message.encoder = mediaType === undefined ? null : formats_1.formats[mediaType];
|
|
133
|
-
}
|
|
134
126
|
// https://github.com/whatwg/fetch/issues/1254
|
|
135
127
|
async function adam(request) {
|
|
136
128
|
const completed = (0, generic_1.promex)();
|
|
@@ -138,12 +130,12 @@ async function adam(request) {
|
|
|
138
130
|
request
|
|
139
131
|
.on('end', completed.callback)
|
|
140
132
|
.pipe(devnull);
|
|
141
|
-
return
|
|
133
|
+
return completed;
|
|
142
134
|
}
|
|
143
135
|
const DEFAULTS = {
|
|
144
136
|
methods: new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']),
|
|
145
137
|
debug: false,
|
|
138
|
+
trace: false,
|
|
146
139
|
port: 8000
|
|
147
140
|
};
|
|
148
|
-
const SUBTYPE = /^(?<type>\w{1,32})\/(vnd\.toa\.(?<subtype>\S{1,32})\+)(?<suffix>\S{1,32})$/;
|
|
149
141
|
//# sourceMappingURL=Server.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Server.js","sourceRoot":"","sources":["../../source/HTTP/Server.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Server.js","sourceRoot":"","sources":["../../source/HTTP/Server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAAwB;AACxB,sDAAwB;AACxB,gDAAiC;AACjC,8DAAgC;AAChC,6CAAkC;AAClC,uCAAwC;AACxC,6CAAwC;AACxC,yCAAwD;AACxD,6CAAqD;AACrD,uCAAmC;AAGnC,MAAa,MAAO,SAAQ,gBAAS;IAClB,MAAM,GAAgB,IAAI,CAAC,YAAY,EAAE,CAAA;IACzC,UAAU,CAAY;IAC/B,OAAO,CAAa;IAE5B,YAAqB,UAAsB;QACzC,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,UAAU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KAAK,EAAE,SAAS,CAAC,QAAQ,CAAC;SAC7B,IAAI,CAAC,OAAO,CAAC,CAAA;IAEhB,OAAO,SAAS,CAAA;AAClB,CAAC;AAED,MAAM,QAAQ,GAAe;IAC3B,OAAO,EAAE,IAAI,GAAG,CAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;IAC9E,KAAK,EAAE,KAAK;IACZ,KAAK,EAAE,KAAK;IACZ,IAAI,EAAE,IAAI;CACX,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { ServerResponse } from 'node:http';
|
|
3
|
+
export declare class Timing {
|
|
4
|
+
private readonly skip;
|
|
5
|
+
private readonly start;
|
|
6
|
+
private readonly breakpoints;
|
|
7
|
+
constructor(enabled: boolean);
|
|
8
|
+
capture<T>(id: string, promise: Promise<T>): Promise<T>;
|
|
9
|
+
append(response: ServerResponse): void;
|
|
10
|
+
}
|