@venizia/ignis-worker 0.2.0-10
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/dist/cjs/applications/index.d.ts +2 -0
- package/dist/cjs/applications/index.d.ts.map +1 -0
- package/dist/cjs/applications/index.js +18 -0
- package/dist/cjs/applications/index.js.map +1 -0
- package/dist/cjs/applications/worker.d.ts +67 -0
- package/dist/cjs/applications/worker.d.ts.map +1 -0
- package/dist/cjs/applications/worker.js +175 -0
- package/dist/cjs/applications/worker.js.map +1 -0
- package/dist/cjs/envelope/encode.d.ts +61 -0
- package/dist/cjs/envelope/encode.d.ts.map +1 -0
- package/dist/cjs/envelope/encode.js +116 -0
- package/dist/cjs/envelope/encode.js.map +1 -0
- package/dist/cjs/envelope/index.d.ts +3 -0
- package/dist/cjs/envelope/index.d.ts.map +1 -0
- package/dist/cjs/envelope/index.js +19 -0
- package/dist/cjs/envelope/index.js.map +1 -0
- package/dist/cjs/envelope/types.d.ts +37 -0
- package/dist/cjs/envelope/types.d.ts.map +1 -0
- package/dist/cjs/envelope/types.js +3 -0
- package/dist/cjs/envelope/types.js.map +1 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/transport/common/index.d.ts +2 -0
- package/dist/cjs/transport/common/index.d.ts.map +1 -0
- package/dist/cjs/transport/common/index.js +18 -0
- package/dist/cjs/transport/common/index.js.map +1 -0
- package/dist/cjs/transport/common/types.d.ts +6 -0
- package/dist/cjs/transport/common/types.d.ts.map +1 -0
- package/dist/cjs/transport/common/types.js +3 -0
- package/dist/cjs/transport/common/types.js.map +1 -0
- package/dist/cjs/transport/fetch-bridge.d.ts +35 -0
- package/dist/cjs/transport/fetch-bridge.d.ts.map +1 -0
- package/dist/cjs/transport/fetch-bridge.js +95 -0
- package/dist/cjs/transport/fetch-bridge.js.map +1 -0
- package/dist/cjs/transport/in-process.d.ts +23 -0
- package/dist/cjs/transport/in-process.d.ts.map +1 -0
- package/dist/cjs/transport/in-process.js +47 -0
- package/dist/cjs/transport/in-process.js.map +1 -0
- package/dist/cjs/transport/index.d.ts +6 -0
- package/dist/cjs/transport/index.d.ts.map +1 -0
- package/dist/cjs/transport/index.js +22 -0
- package/dist/cjs/transport/index.js.map +1 -0
- package/dist/cjs/transport/shared.d.ts +102 -0
- package/dist/cjs/transport/shared.d.ts.map +1 -0
- package/dist/cjs/transport/shared.js +396 -0
- package/dist/cjs/transport/shared.js.map +1 -0
- package/dist/cjs/transport/worker.d.ts +62 -0
- package/dist/cjs/transport/worker.d.ts.map +1 -0
- package/dist/cjs/transport/worker.js +223 -0
- package/dist/cjs/transport/worker.js.map +1 -0
- package/dist/esm/applications/index.d.ts +2 -0
- package/dist/esm/applications/index.d.ts.map +1 -0
- package/dist/esm/applications/index.js +2 -0
- package/dist/esm/applications/index.js.map +1 -0
- package/dist/esm/applications/worker.d.ts +67 -0
- package/dist/esm/applications/worker.d.ts.map +1 -0
- package/dist/esm/applications/worker.js +171 -0
- package/dist/esm/applications/worker.js.map +1 -0
- package/dist/esm/envelope/encode.d.ts +61 -0
- package/dist/esm/envelope/encode.d.ts.map +1 -0
- package/dist/esm/envelope/encode.js +112 -0
- package/dist/esm/envelope/encode.js.map +1 -0
- package/dist/esm/envelope/index.d.ts +3 -0
- package/dist/esm/envelope/index.d.ts.map +1 -0
- package/dist/esm/envelope/index.js +3 -0
- package/dist/esm/envelope/index.js.map +1 -0
- package/dist/esm/envelope/types.d.ts +37 -0
- package/dist/esm/envelope/types.d.ts.map +1 -0
- package/dist/esm/envelope/types.js +2 -0
- package/dist/esm/envelope/types.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/transport/common/index.d.ts +2 -0
- package/dist/esm/transport/common/index.d.ts.map +1 -0
- package/dist/esm/transport/common/index.js +2 -0
- package/dist/esm/transport/common/index.js.map +1 -0
- package/dist/esm/transport/common/types.d.ts +6 -0
- package/dist/esm/transport/common/types.d.ts.map +1 -0
- package/dist/esm/transport/common/types.js +2 -0
- package/dist/esm/transport/common/types.js.map +1 -0
- package/dist/esm/transport/fetch-bridge.d.ts +35 -0
- package/dist/esm/transport/fetch-bridge.d.ts.map +1 -0
- package/dist/esm/transport/fetch-bridge.js +91 -0
- package/dist/esm/transport/fetch-bridge.js.map +1 -0
- package/dist/esm/transport/in-process.d.ts +23 -0
- package/dist/esm/transport/in-process.d.ts.map +1 -0
- package/dist/esm/transport/in-process.js +43 -0
- package/dist/esm/transport/in-process.js.map +1 -0
- package/dist/esm/transport/index.d.ts +6 -0
- package/dist/esm/transport/index.d.ts.map +1 -0
- package/dist/esm/transport/index.js +6 -0
- package/dist/esm/transport/index.js.map +1 -0
- package/dist/esm/transport/shared.d.ts +102 -0
- package/dist/esm/transport/shared.d.ts.map +1 -0
- package/dist/esm/transport/shared.js +391 -0
- package/dist/esm/transport/shared.js.map +1 -0
- package/dist/esm/transport/worker.d.ts +62 -0
- package/dist/esm/transport/worker.d.ts.map +1 -0
- package/dist/esm/transport/worker.js +219 -0
- package/dist/esm/transport/worker.js.map +1 -0
- package/package.json +94 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/applications/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./worker"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/applications/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { RestApplication } from '@venizia/ignis-kernel';
|
|
2
|
+
import type { Env, Schema } from 'hono';
|
|
3
|
+
/**
|
|
4
|
+
* Structural shape of the two globals `listen()` can attach to. Never `DedicatedWorkerGlobalScope`:
|
|
5
|
+
* naming it emits it into the published `.d.ts` and forces every consumer to compile with
|
|
6
|
+
* `lib.webworker`. `start` is optional because a `MessagePort`s queue starts disabled and only
|
|
7
|
+
* `start()` enables it, while a worker global scope has no such method at all.
|
|
8
|
+
*/
|
|
9
|
+
export interface IWorkerMessageScope {
|
|
10
|
+
postMessage(message: unknown, transfer?: Array<ArrayBuffer | MessagePort>): void;
|
|
11
|
+
addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
|
|
12
|
+
removeEventListener?(type: 'message', listener: (event: MessageEvent) => void): void;
|
|
13
|
+
start?(): void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Runs an IGNIS application inside a browser Web Worker: `listen()` replaces what
|
|
17
|
+
* `ServerApplication.start()` does with a socket, attaching `onmessage` instead. Gets REST
|
|
18
|
+
* controller registration by inheritance - `RestApplication.registerControllers()` - the whole
|
|
19
|
+
* reason the DI helpers and `RestComponent` sit in the kernel.
|
|
20
|
+
*/
|
|
21
|
+
export declare abstract class WorkerApplication<AppEnv extends Env = Env, AppSchema extends Schema = {}, BasePath extends string = '/'> extends RestApplication<AppEnv, AppSchema, BasePath> {
|
|
22
|
+
/** Resolves once the router tree is mounted - what an envelope arriving during boot waits on. */
|
|
23
|
+
private servingPromise?;
|
|
24
|
+
/** The whole `listen()` call, kept so a second one is a no-op instead of a second listener. */
|
|
25
|
+
private listenPromise?;
|
|
26
|
+
private detachFromScope?;
|
|
27
|
+
private isStopped;
|
|
28
|
+
/**
|
|
29
|
+
* Every failure here is posted back as an `IBffErrorEnvelope`, the failed `postMessage` included:
|
|
30
|
+
* an exception nobody posts leaves the UI waiting out the transport timeout.
|
|
31
|
+
*/
|
|
32
|
+
private handleMessage;
|
|
33
|
+
/** No transfer list: an error envelope carries no `ArrayBuffer` body, and this is already the fallback path - nothing left to hand off if this post fails too. */
|
|
34
|
+
private postErrorEnvelope;
|
|
35
|
+
/** Everything `ServerApplication.start()` does short of binding a socket. Once this resolves the application answers requests. */
|
|
36
|
+
private startServing;
|
|
37
|
+
/**
|
|
38
|
+
* Holds an envelope until the application is serving. A dedicated worker's queue is enabled when
|
|
39
|
+
* its module script is evaluated, so anything posted during boot - PGlite takes ~350ms - reaches
|
|
40
|
+
* this listener and would otherwise be dropped. One shared promise also hands them on in arrival
|
|
41
|
+
* order.
|
|
42
|
+
*/
|
|
43
|
+
private dispatchMessage;
|
|
44
|
+
private attachTo;
|
|
45
|
+
/**
|
|
46
|
+
* `ServerApplication.start()` minus the socket. `opts.scope` defaults to `globalThis`, never a
|
|
47
|
+
* bare `self`, which would tie the published types to `lib.webworker`. Idempotent: without the
|
|
48
|
+
* guard one envelope runs its handler once per call - a POST inserting two rows - and Vite HMR
|
|
49
|
+
* re-evaluating the worker module is the everyday trigger. A boot failure keeps the listener
|
|
50
|
+
* attached so every request hears the failure instead of waiting out the transport timeout.
|
|
51
|
+
*/
|
|
52
|
+
listen(opts?: {
|
|
53
|
+
scope?: MessagePort | IWorkerMessageScope;
|
|
54
|
+
}): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* The counterpart to `listen()`, and to `ServerApplication.stop()`: without it nothing drains the
|
|
57
|
+
* post-stop hooks, so a datasource's `end()` never runs and the OPFS exclusive lock a PGlite
|
|
58
|
+
* holds is never released - the next Worker then fails with `NoModificationAllowedError` until a
|
|
59
|
+
* full page reload. The Vite recipe is `import.meta.hot?.dispose(() => application.stop())`.
|
|
60
|
+
*
|
|
61
|
+
* Detaches BEFORE draining, unlike the server: here the listener IS the socket, and a request
|
|
62
|
+
* admitted while the hooks are closing a datasource has nothing left to run against. A stopped
|
|
63
|
+
* application is not restartable - build a new one.
|
|
64
|
+
*/
|
|
65
|
+
stop(): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=worker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../../src/applications/worker.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAIxC;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB;IAClC,WAAW,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,KAAK,CAAC,WAAW,GAAG,WAAW,CAAC,GAAG,IAAI,CAAC;IACjF,gBAAgB,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,GAAG,IAAI,CAAC;IACjF,mBAAmB,CAAC,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,GAAG,IAAI,CAAC;IACrF,KAAK,CAAC,IAAI,IAAI,CAAC;CAChB;AAED;;;;;GAKG;AACH,8BAAsB,iBAAiB,CACrC,MAAM,SAAS,GAAG,GAAG,GAAG,EACxB,SAAS,SAAS,MAAM,GAAG,EAAE,EAC7B,QAAQ,SAAS,MAAM,GAAG,GAAG,CAC7B,SAAQ,eAAe,CAAC,MAAM,EAAE,SAAS,EAAE,QAAQ,CAAC;IACpD,iGAAiG;IACjG,OAAO,CAAC,cAAc,CAAC,CAAgB;IACvC,+FAA+F;IAC/F,OAAO,CAAC,aAAa,CAAC,CAAgB;IACtC,OAAO,CAAC,eAAe,CAAC,CAAa;IACrC,OAAO,CAAC,SAAS,CAAS;IAE1B;;;OAGG;YACW,aAAa;IAoC3B,kKAAkK;IAClK,OAAO,CAAC,iBAAiB;IAgBzB,kIAAkI;YACpH,YAAY;IAW1B;;;;;OAKG;YACW,eAAe;IAqC7B,OAAO,CAAC,QAAQ;IAsChB;;;;;;OAMG;IACG,MAAM,CAAC,IAAI,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,WAAW,GAAG,mBAAmB,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBjF;;;;;;;;;OASG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAU5B"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkerApplication = void 0;
|
|
4
|
+
const core_1 = require("@venizia/ignis-helpers/core");
|
|
5
|
+
const common_1 = require("@venizia/ignis-helpers/common");
|
|
6
|
+
const ignis_kernel_1 = require("@venizia/ignis-kernel");
|
|
7
|
+
const encode_1 = require("../envelope/encode");
|
|
8
|
+
/**
|
|
9
|
+
* Runs an IGNIS application inside a browser Web Worker: `listen()` replaces what
|
|
10
|
+
* `ServerApplication.start()` does with a socket, attaching `onmessage` instead. Gets REST
|
|
11
|
+
* controller registration by inheritance - `RestApplication.registerControllers()` - the whole
|
|
12
|
+
* reason the DI helpers and `RestComponent` sit in the kernel.
|
|
13
|
+
*/
|
|
14
|
+
class WorkerApplication extends ignis_kernel_1.RestApplication {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
this.isStopped = false;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Every failure here is posted back as an `IBffErrorEnvelope`, the failed `postMessage` included:
|
|
21
|
+
* an exception nobody posts leaves the UI waiting out the transport timeout.
|
|
22
|
+
*/
|
|
23
|
+
async handleMessage(opts) {
|
|
24
|
+
const { scope, envelope } = opts;
|
|
25
|
+
const logger = this.logger.for(this.handleMessage.name);
|
|
26
|
+
let responseEnvelope;
|
|
27
|
+
let failure;
|
|
28
|
+
try {
|
|
29
|
+
const request = encode_1.BffEnvelope.decodeRequest({ envelope });
|
|
30
|
+
const response = await this.getServer().fetch(request);
|
|
31
|
+
responseEnvelope = await encode_1.BffEnvelope.encodeResponse({ response, id: envelope.id });
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
logger.error('Failed to handle worker message | id: %s | error: %s', envelope.id, error);
|
|
35
|
+
failure = error;
|
|
36
|
+
}
|
|
37
|
+
if (responseEnvelope) {
|
|
38
|
+
try {
|
|
39
|
+
scope.postMessage(responseEnvelope, responseEnvelope.body ? [responseEnvelope.body] : []);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
catch (postError) {
|
|
43
|
+
logger.error('Failed to post response back to scope, falling back to an error envelope | id: %s | error: %s', envelope.id, postError);
|
|
44
|
+
failure = postError;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
this.postErrorEnvelope({ scope, id: envelope.id, error: failure });
|
|
48
|
+
}
|
|
49
|
+
/** No transfer list: an error envelope carries no `ArrayBuffer` body, and this is already the fallback path - nothing left to hand off if this post fails too. */
|
|
50
|
+
postErrorEnvelope(opts) {
|
|
51
|
+
const { scope, id, error } = opts;
|
|
52
|
+
try {
|
|
53
|
+
scope.postMessage(encode_1.BffEnvelope.encodeError({ id, error }));
|
|
54
|
+
}
|
|
55
|
+
catch (postError) {
|
|
56
|
+
this.logger
|
|
57
|
+
.for(this.postErrorEnvelope.name)
|
|
58
|
+
.error('Failed to post error envelope back to scope | id: %s | error: %s', id, postError);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Everything `ServerApplication.start()` does short of binding a socket. Once this resolves the application answers requests. */
|
|
62
|
+
async startServing() {
|
|
63
|
+
// The stack itself is `RestApplication.registerDefaultMiddlewares()` - a BFF must answer with
|
|
64
|
+
// the framework's envelope exactly as the server does, so it adds nothing and overrides nothing.
|
|
65
|
+
await this.registerDefaultMiddlewares();
|
|
66
|
+
await this.initialize();
|
|
67
|
+
await this.setupMiddlewares();
|
|
68
|
+
this.getServer().route(this.configs.path.base, this.rootRouter);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Holds an envelope until the application is serving. A dedicated worker's queue is enabled when
|
|
72
|
+
* its module script is evaluated, so anything posted during boot - PGlite takes ~350ms - reaches
|
|
73
|
+
* this listener and would otherwise be dropped. One shared promise also hands them on in arrival
|
|
74
|
+
* order.
|
|
75
|
+
*/
|
|
76
|
+
async dispatchMessage(opts) {
|
|
77
|
+
const { scope, envelope } = opts;
|
|
78
|
+
const logger = this.logger.for(this.dispatchMessage.name);
|
|
79
|
+
try {
|
|
80
|
+
await this.servingPromise;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
logger.error('The application failed to start, answering with an error envelope | id: %s | error: %s', envelope.id, error);
|
|
84
|
+
this.postErrorEnvelope({ scope, id: envelope.id, error });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// Answered, not dropped: a request admitted before `stop()` and still waiting on the boot has
|
|
88
|
+
// a caller holding a promise, and silence there is the timeout this whole path exists to avoid.
|
|
89
|
+
if (this.isStopped) {
|
|
90
|
+
logger.warn('Refusing a request that arrived after stop() | id: %s', envelope.id);
|
|
91
|
+
this.postErrorEnvelope({
|
|
92
|
+
scope,
|
|
93
|
+
id: envelope.id,
|
|
94
|
+
error: (0, core_1.getError)({
|
|
95
|
+
statusCode: common_1.HTTP.ResultCodes.RS_5.ServiceUnavailable,
|
|
96
|
+
message: `[WorkerApplication] The application was stopped before this request could run | id: ${envelope.id}`,
|
|
97
|
+
}),
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
await this.handleMessage({ scope, envelope });
|
|
102
|
+
}
|
|
103
|
+
attachTo(opts) {
|
|
104
|
+
const { scope } = opts;
|
|
105
|
+
const logger = this.logger.for(this.attachTo.name);
|
|
106
|
+
// Typed as the broader `Event`, not `MessageEvent`: Node's ambient `worker_threads.MessagePort`
|
|
107
|
+
// (the fallback this package's DOM-less tsconfig resolves `MessagePort` to) declares
|
|
108
|
+
// `addEventListener` over `Event | MessageEvent`, and a handler narrower than that is not
|
|
109
|
+
// assignable to it.
|
|
110
|
+
const listener = (event) => {
|
|
111
|
+
const envelope = event.data;
|
|
112
|
+
// A worker scope receives every `postMessage` sent to it, not only ours - a dev-server ping,
|
|
113
|
+
// a devtools/extension probe, a library's own init handshake. None of those carry an `id`;
|
|
114
|
+
// silently dropping them (rather than dereferencing `envelope.id` inside `handleMessage`'s
|
|
115
|
+
// own catch block) is what keeps a foreign message from throwing a bare `TypeError` that
|
|
116
|
+
// would otherwise escape this listener entirely, unlogged and unposted.
|
|
117
|
+
if (!envelope?.id) {
|
|
118
|
+
logger.debug('Ignoring worker message with no request id | data: %j', envelope);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// `dispatchMessage` never rejects - every failure inside it, including a failed post, is
|
|
122
|
+
// caught and handled internally. This `.catch` only guards the sliver of code before its own
|
|
123
|
+
// try block ever runs.
|
|
124
|
+
this.dispatchMessage({ scope, envelope }).catch(error => {
|
|
125
|
+
logger.error('Unexpected failure handling worker message | error: %s', error);
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
scope.addEventListener('message', listener);
|
|
129
|
+
scope.start?.();
|
|
130
|
+
this.detachFromScope = () => {
|
|
131
|
+
scope.removeEventListener?.('message', listener);
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* `ServerApplication.start()` minus the socket. `opts.scope` defaults to `globalThis`, never a
|
|
136
|
+
* bare `self`, which would tie the published types to `lib.webworker`. Idempotent: without the
|
|
137
|
+
* guard one envelope runs its handler once per call - a POST inserting two rows - and Vite HMR
|
|
138
|
+
* re-evaluating the worker module is the everyday trigger. A boot failure keeps the listener
|
|
139
|
+
* attached so every request hears the failure instead of waiting out the transport timeout.
|
|
140
|
+
*/
|
|
141
|
+
async listen(opts) {
|
|
142
|
+
if (this.listenPromise) {
|
|
143
|
+
this.logger
|
|
144
|
+
.for(this.listen.name)
|
|
145
|
+
.warn('Already listening - ignoring this call | one scope, one listener');
|
|
146
|
+
return this.listenPromise;
|
|
147
|
+
}
|
|
148
|
+
// Everything down to the first `await` runs synchronously with the caller, which is what makes
|
|
149
|
+
// the listener observable to anything posted in the same turn.
|
|
150
|
+
const scope = opts?.scope ?? globalThis;
|
|
151
|
+
this.attachTo({ scope });
|
|
152
|
+
this.servingPromise = this.startServing();
|
|
153
|
+
this.listenPromise = this.servingPromise.then(() => this.executePostStartHooks());
|
|
154
|
+
return this.listenPromise;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The counterpart to `listen()`, and to `ServerApplication.stop()`: without it nothing drains the
|
|
158
|
+
* post-stop hooks, so a datasource's `end()` never runs and the OPFS exclusive lock a PGlite
|
|
159
|
+
* holds is never released - the next Worker then fails with `NoModificationAllowedError` until a
|
|
160
|
+
* full page reload. The Vite recipe is `import.meta.hot?.dispose(() => application.stop())`.
|
|
161
|
+
*
|
|
162
|
+
* Detaches BEFORE draining, unlike the server: here the listener IS the socket, and a request
|
|
163
|
+
* admitted while the hooks are closing a datasource has nothing left to run against. A stopped
|
|
164
|
+
* application is not restartable - build a new one.
|
|
165
|
+
*/
|
|
166
|
+
async stop() {
|
|
167
|
+
this.isStopped = true;
|
|
168
|
+
this.detachFromScope?.();
|
|
169
|
+
this.detachFromScope = undefined;
|
|
170
|
+
await this.executePostStopHooks();
|
|
171
|
+
this.logger.for(this.stop.name).info('Worker application STOPPED');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
exports.WorkerApplication = WorkerApplication;
|
|
175
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../src/applications/worker.ts"],"names":[],"mappings":";;;AAAA,sDAAuD;AACvD,0DAAqD;AACrD,wDAAwD;AAExD,8CAAgD;AAgBhD;;;;;GAKG;AACH,MAAsB,iBAIpB,SAAQ,8BAA4C;IAJtD;;QAUU,cAAS,GAAG,KAAK,CAAC;IAwM5B,CAAC;IAtMC;;;OAGG;IACK,KAAK,CAAC,aAAa,CAAC,IAG3B;QACC,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;QACjC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAExD,IAAI,gBAAkD,CAAC;QACvD,IAAI,OAAgB,CAAC;QAErB,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,oBAAW,CAAC,aAAa,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;YACxD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACvD,gBAAgB,GAAG,MAAM,oBAAW,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,CAAC,EAAE,EAAE,CAAC,CAAC;QACrF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,sDAAsD,EAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;YACz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AAI,CAAC,aAAa,CAAC;QAC5B,CAAC;QAED,+FAA+F;QAC/F,+DAA+D;QAC/D,MAAM,KAAK,GACT,IAAI,EAAE,KAAK,IAAK,UAAkC,CAAC;QAErD,IAAI,CAAC,QAAQ,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QAEzB,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAC1C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,qBAAqB,EAAE,CAAC,CAAC;QAElF,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QAEtB,IAAI,CAAC,eAAe,EAAE,EAAE,CAAC;QACzB,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QAEjC,MAAM,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAElC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;IACrE,CAAC;CACF;AAlND,8CAkNC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ApplicationError } from '@venizia/ignis-helpers/core';
|
|
2
|
+
import type { IBffErrorEnvelope, IBffRequestEnvelope, IBffResponseEnvelope } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Every request built inside the Worker uses this origin. Never `self.location.href`: a `blob:`
|
|
5
|
+
* worker origin is opaque and `new URL(path, blobUrl)` throws, and Hono's `getPath()` mis-parses
|
|
6
|
+
* `file://`. Hono never resolves the host - it string-scans from `indexOf(':') + 4`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const BFF_SYNTHETIC_ORIGIN = "http://ignis.internal";
|
|
9
|
+
/** Turns `Request`/`Response`/`Error` into plain structured-cloneable shapes and back - none of the three survives `postMessage` as itself. */
|
|
10
|
+
export declare class BffEnvelope {
|
|
11
|
+
/**
|
|
12
|
+
* Statuses the Fetch spec forbids a body on - `new Response(body, { status })` throws if `body`
|
|
13
|
+
* is anything but `null` for one of these, even an empty `ArrayBuffer`.
|
|
14
|
+
*/
|
|
15
|
+
private static readonly NULL_BODY_STATUSES;
|
|
16
|
+
/**
|
|
17
|
+
* Moves a URL onto the synthetic origin, keeping everything below it. A caller builds a request
|
|
18
|
+
* against the page's real origin (or a relative path resolved against it); `decodeRequest` on the
|
|
19
|
+
* far side always reconstructs on `BFF_SYNTHETIC_ORIGIN`.
|
|
20
|
+
*/
|
|
21
|
+
static toSyntheticUrl(opts: {
|
|
22
|
+
url: string;
|
|
23
|
+
}): string;
|
|
24
|
+
/**
|
|
25
|
+
* `url` overrides the request's own, so the origin rewrite costs nothing: rebuilding the `Request`
|
|
26
|
+
* to change its URL would read the body a second time, and a 10 MB upload would hold 20 MB on the
|
|
27
|
+
* UI thread. Reading the body here disturbs the caller's request exactly as `globalThis.fetch`
|
|
28
|
+
* does with the one it is handed.
|
|
29
|
+
*/
|
|
30
|
+
static encodeRequest(opts: {
|
|
31
|
+
request: Request;
|
|
32
|
+
id: string;
|
|
33
|
+
url?: string;
|
|
34
|
+
}): Promise<IBffRequestEnvelope>;
|
|
35
|
+
static decodeRequest(opts: {
|
|
36
|
+
envelope: IBffRequestEnvelope;
|
|
37
|
+
}): Request;
|
|
38
|
+
static encodeResponse(opts: {
|
|
39
|
+
response: Response;
|
|
40
|
+
id: string;
|
|
41
|
+
}): Promise<IBffResponseEnvelope>;
|
|
42
|
+
static decodeResponse(opts: {
|
|
43
|
+
envelope: IBffResponseEnvelope;
|
|
44
|
+
}): Response;
|
|
45
|
+
/**
|
|
46
|
+
* Builds the shape an error crosses `postMessage` as. An `ApplicationError` cannot cross as itself
|
|
47
|
+
* - a custom `Error` subclass loses its name, its own properties and its prototype through
|
|
48
|
+
* structured clone - so it crosses normalised and `decodeError` rebuilds it on the far side.
|
|
49
|
+
*
|
|
50
|
+
* A foreign error gets `500`, matching what the framework's error middleware gives an UNEXPECTED
|
|
51
|
+
* one on the server. Anything reaching here already escaped the whole HTTP layer.
|
|
52
|
+
*/
|
|
53
|
+
static encodeError(opts: {
|
|
54
|
+
id: string;
|
|
55
|
+
error: unknown;
|
|
56
|
+
}): IBffErrorEnvelope;
|
|
57
|
+
static decodeError(opts: {
|
|
58
|
+
envelope: IBffErrorEnvelope;
|
|
59
|
+
}): ApplicationError;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../../src/envelope/encode.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAGpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAE5F;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,0BAA0B,CAAC;AAE5D,+IAA+I;AAC/I,qBAAa,WAAW;IACtB;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAA4B;IAEtE;;;;OAIG;IACH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM;IAKpD;;;;;OAKG;WACU,aAAa,CAAC,IAAI,EAAE;QAC/B,OAAO,EAAE,OAAO,CAAC;QACjB,EAAE,EAAE,MAAM,CAAC;QACX,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAahC,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE;QAAE,QAAQ,EAAE,mBAAmB,CAAA;KAAE,GAAG,OAAO;WAUzD,cAAc,CAAC,IAAI,EAAE;QAChC,QAAQ,EAAE,QAAQ,CAAC;QACnB,EAAE,EAAE,MAAM,CAAC;KACZ,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAajC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE;QAAE,QAAQ,EAAE,oBAAoB,CAAA;KAAE,GAAG,QAAQ;IAazE;;;;;;;OAOG;IACH,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,GAAG,iBAAiB;IAoB3E,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE;QAAE,QAAQ,EAAE,iBAAiB,CAAA;KAAE,GAAG,gBAAgB;CAc5E"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BffEnvelope = exports.BFF_SYNTHETIC_ORIGIN = void 0;
|
|
4
|
+
const core_1 = require("@venizia/ignis-helpers/core");
|
|
5
|
+
const common_1 = require("@venizia/ignis-helpers/common");
|
|
6
|
+
/**
|
|
7
|
+
* Every request built inside the Worker uses this origin. Never `self.location.href`: a `blob:`
|
|
8
|
+
* worker origin is opaque and `new URL(path, blobUrl)` throws, and Hono's `getPath()` mis-parses
|
|
9
|
+
* `file://`. Hono never resolves the host - it string-scans from `indexOf(':') + 4`.
|
|
10
|
+
*/
|
|
11
|
+
exports.BFF_SYNTHETIC_ORIGIN = 'http://ignis.internal';
|
|
12
|
+
/** Turns `Request`/`Response`/`Error` into plain structured-cloneable shapes and back - none of the three survives `postMessage` as itself. */
|
|
13
|
+
class BffEnvelope {
|
|
14
|
+
/**
|
|
15
|
+
* Statuses the Fetch spec forbids a body on - `new Response(body, { status })` throws if `body`
|
|
16
|
+
* is anything but `null` for one of these, even an empty `ArrayBuffer`.
|
|
17
|
+
*/
|
|
18
|
+
static { this.NULL_BODY_STATUSES = new Set([204, 205, 304]); }
|
|
19
|
+
/**
|
|
20
|
+
* Moves a URL onto the synthetic origin, keeping everything below it. A caller builds a request
|
|
21
|
+
* against the page's real origin (or a relative path resolved against it); `decodeRequest` on the
|
|
22
|
+
* far side always reconstructs on `BFF_SYNTHETIC_ORIGIN`.
|
|
23
|
+
*/
|
|
24
|
+
static toSyntheticUrl(opts) {
|
|
25
|
+
const url = new URL(opts.url);
|
|
26
|
+
return `${exports.BFF_SYNTHETIC_ORIGIN}${url.pathname}${url.search}${url.hash}`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `url` overrides the request's own, so the origin rewrite costs nothing: rebuilding the `Request`
|
|
30
|
+
* to change its URL would read the body a second time, and a 10 MB upload would hold 20 MB on the
|
|
31
|
+
* UI thread. Reading the body here disturbs the caller's request exactly as `globalThis.fetch`
|
|
32
|
+
* does with the one it is handed.
|
|
33
|
+
*/
|
|
34
|
+
static async encodeRequest(opts) {
|
|
35
|
+
const { request, id, url } = opts;
|
|
36
|
+
const hasBody = request.method !== 'GET' && request.method !== 'HEAD';
|
|
37
|
+
return {
|
|
38
|
+
id,
|
|
39
|
+
method: request.method,
|
|
40
|
+
url: url ?? request.url,
|
|
41
|
+
headers: [...request.headers.entries()],
|
|
42
|
+
body: hasBody ? await request.arrayBuffer() : undefined,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
static decodeRequest(opts) {
|
|
46
|
+
const { envelope } = opts;
|
|
47
|
+
return new Request(envelope.url, {
|
|
48
|
+
method: envelope.method,
|
|
49
|
+
headers: envelope.headers,
|
|
50
|
+
body: envelope.body,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
static async encodeResponse(opts) {
|
|
54
|
+
const { response, id } = opts;
|
|
55
|
+
const hasBody = !BffEnvelope.NULL_BODY_STATUSES.has(response.status);
|
|
56
|
+
return {
|
|
57
|
+
id,
|
|
58
|
+
status: response.status,
|
|
59
|
+
statusText: response.statusText,
|
|
60
|
+
headers: [...response.headers.entries()],
|
|
61
|
+
body: hasBody ? await response.arrayBuffer() : undefined,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
static decodeResponse(opts) {
|
|
65
|
+
const { envelope } = opts;
|
|
66
|
+
const body = BffEnvelope.NULL_BODY_STATUSES.has(envelope.status)
|
|
67
|
+
? null
|
|
68
|
+
: (envelope.body ?? null);
|
|
69
|
+
return new Response(body, {
|
|
70
|
+
status: envelope.status,
|
|
71
|
+
statusText: envelope.statusText,
|
|
72
|
+
headers: envelope.headers,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Builds the shape an error crosses `postMessage` as. An `ApplicationError` cannot cross as itself
|
|
77
|
+
* - a custom `Error` subclass loses its name, its own properties and its prototype through
|
|
78
|
+
* structured clone - so it crosses normalised and `decodeError` rebuilds it on the far side.
|
|
79
|
+
*
|
|
80
|
+
* A foreign error gets `500`, matching what the framework's error middleware gives an UNEXPECTED
|
|
81
|
+
* one on the server. Anything reaching here already escaped the whole HTTP layer.
|
|
82
|
+
*/
|
|
83
|
+
static encodeError(opts) {
|
|
84
|
+
const { id, error } = opts;
|
|
85
|
+
const applicationError = (0, core_1.isApplicationError)(error)
|
|
86
|
+
? error
|
|
87
|
+
: (0, core_1.getError)({
|
|
88
|
+
statusCode: common_1.HTTP.ResultCodes.RS_5.InternalServerError,
|
|
89
|
+
message: error instanceof Error ? error.message : String(error),
|
|
90
|
+
});
|
|
91
|
+
return {
|
|
92
|
+
id,
|
|
93
|
+
statusCode: applicationError.statusCode,
|
|
94
|
+
error: {
|
|
95
|
+
text: applicationError.normalized.text,
|
|
96
|
+
code: applicationError.normalized.code,
|
|
97
|
+
args: applicationError.normalized.args,
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
static decodeError(opts) {
|
|
102
|
+
const { envelope } = opts;
|
|
103
|
+
return (0, core_1.fromError)({
|
|
104
|
+
error: {
|
|
105
|
+
statusCode: envelope.statusCode,
|
|
106
|
+
normalized: {
|
|
107
|
+
text: envelope.error.text,
|
|
108
|
+
code: envelope.error.code,
|
|
109
|
+
args: envelope.error.args,
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.BffEnvelope = BffEnvelope;
|
|
116
|
+
//# sourceMappingURL=encode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.js","sourceRoot":"","sources":["../../../src/envelope/encode.ts"],"names":[],"mappings":";;;AACA,sDAAsF;AACtF,0DAAqD;AAGrD;;;;GAIG;AACU,QAAA,oBAAoB,GAAG,uBAAuB,CAAC;AAE5D,+IAA+I;AAC/I,MAAa,WAAW;IACtB;;;OAGG;aACqB,uBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAEtE;;;;OAIG;IACH,MAAM,CAAC,cAAc,CAAC,IAAqB;QACzC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC9B,OAAO,GAAG,4BAAoB,GAAG,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;IAC1E,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,IAI1B;QACC,MAAM,EAAE,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;QAClC,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,KAAK,KAAK,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC;QAEtE,OAAO;YACL,EAAE;YACF,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,GAAG,EAAE,GAAG,IAAI,OAAO,CAAC,GAAG;YACvB,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACvC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,OAAO,CAAC,W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ources":["../../../src/envelope/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./encode"), exports);
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/envelope/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,0CAAwB"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Headers are carried as tuples: a `Headers` instance is not structured-cloneable. */
|
|
2
|
+
export type TEnvelopeHeaders = Array<[string, string]>;
|
|
3
|
+
export interface IBffRequestEnvelope {
|
|
4
|
+
id: string;
|
|
5
|
+
method: string;
|
|
6
|
+
url: string;
|
|
7
|
+
headers: TEnvelopeHeaders;
|
|
8
|
+
/** Buffered, never a stream - `ReadableStream` does not transfer in Safari. */
|
|
9
|
+
body?: ArrayBuffer;
|
|
10
|
+
}
|
|
11
|
+
export interface IBffResponseEnvelope {
|
|
12
|
+
id: string;
|
|
13
|
+
status: number;
|
|
14
|
+
statusText?: string;
|
|
15
|
+
headers: TEnvelopeHeaders;
|
|
16
|
+
body?: ArrayBuffer;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A custom `Error` subclass loses its name, its own properties and its prototype through structured
|
|
20
|
+
* clone, so an `ApplicationError` cannot cross as itself. It crosses as its normalised shape and is
|
|
21
|
+
* rebuilt with `fromError` on the far side.
|
|
22
|
+
*/
|
|
23
|
+
export interface IBffErrorEnvelope {
|
|
24
|
+
id: string;
|
|
25
|
+
/**
|
|
26
|
+
* Carried explicitly, because `fromError` defaults a missing one to 400. This envelope is only
|
|
27
|
+
* ever produced when something escaped Hono's `onError` - the 500-class failures a UI must not
|
|
28
|
+
* classify as its own bad request.
|
|
29
|
+
*/
|
|
30
|
+
statusCode: number;
|
|
31
|
+
error: {
|
|
32
|
+
text: string;
|
|
33
|
+
code?: string;
|
|
34
|
+
args?: unknown;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/envelope/types.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,MAAM,MAAM,gBAAgB,GAAG,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAEvD,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,gBAAgB,CAAC;IAC1B,+EAA+E;IAC/E,IAAI,CAAC,EAAE,WAAW,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,gBAAgB,CAAC;IAC1B,IAAI,CAAC,EAAE,WAAW,CAAC;CACpB;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX;;;;OAIG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;CACxD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/envelope/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
require("reflect-metadata");
|
|
18
|
+
__exportStar(require("./applications"), exports);
|
|
19
|
+
__exportStar(require("./envelope"), exports);
|
|
20
|
+
__exportStar(require("./transport"), exports);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4BAA0B;AAE1B,iDAA+B;AAC/B,6CAA2B;AAC3B,8CAA4B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/transport/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/transport/common/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/transport/common/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,KAAK,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CACtD"}
|