@vampgg/utils 1.0.0-beta.3 → 1.0.0-beta.5
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.
|
@@ -148,5 +148,17 @@ declare class TempoExtensionRouter<Ctx extends {
|
|
|
148
148
|
private send;
|
|
149
149
|
handle(_req: Message, _ctx: Ctx): Promise<Message>;
|
|
150
150
|
}
|
|
151
|
+
type ExtensionSenderContext = {
|
|
152
|
+
sender: Runtime.MessageSender;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Inbound guard + dispatch for the background/service-worker `runtime.onMessage`
|
|
156
|
+
* hook, which the router does not install itself. Only frames from this
|
|
157
|
+
* extension that decode to a Message with a registered methodId are processed;
|
|
158
|
+
* everything else (other extensions, reply frames, malformed traffic) is
|
|
159
|
+
* ignored. Replies travel back through the router's send path, so this never
|
|
160
|
+
* uses `sendResponse`.
|
|
161
|
+
*/
|
|
162
|
+
declare function createExtensionListener(router: Pick<TempoExtensionRouter<ExtensionSenderContext>, "process">, registry: ServiceRegistry): (raw: unknown, sender: Runtime.MessageSender) => undefined;
|
|
151
163
|
//#endregion
|
|
152
|
-
export { ExtensionBaseRouter, TempoExtensionRouter, TempoExtensionRouterConfiguration };
|
|
164
|
+
export { ExtensionBaseRouter, ExtensionSenderContext, TempoExtensionRouter, TempoExtensionRouterConfiguration, createExtensionListener };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Message } from "./bebop.mjs";
|
|
2
2
|
import { RouterCore } from "./router-core.mjs";
|
|
3
3
|
import { Metadata, TempoError, TempoStatusCode, TempoUtil, TempoVersion } from "@tempojs/common";
|
|
4
|
-
import { tabs } from "webextension-polyfill";
|
|
4
|
+
import { runtime, tabs } from "webextension-polyfill";
|
|
5
5
|
import { TempoRouterConfiguration } from "@tempojs/server";
|
|
6
6
|
//#region src/extension-router.ts
|
|
7
7
|
/**
|
|
@@ -190,19 +190,39 @@ var TempoExtensionRouter = class extends ExtensionBaseRouter {
|
|
|
190
190
|
});
|
|
191
191
|
}
|
|
192
192
|
async send(sender, message) {
|
|
193
|
-
|
|
194
|
-
this.logger.warn(`ignoring message to discarded tab`, { tab: sender.tab });
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
193
|
+
const payload = Array.from(Message.encode(message));
|
|
197
194
|
try {
|
|
198
|
-
await tabs.sendMessage(sender.tab.id,
|
|
195
|
+
if (sender.tab?.id !== void 0 && sender.tab.discarded !== true) await tabs.sendMessage(sender.tab.id, payload);
|
|
196
|
+
else await runtime.sendMessage(payload);
|
|
199
197
|
} catch (e) {
|
|
200
|
-
this.logger.
|
|
198
|
+
this.logger.debug("extension reply not delivered; receiver likely gone", { tab: sender.tab }, e);
|
|
201
199
|
}
|
|
202
200
|
}
|
|
203
201
|
async handle(_req, _ctx) {
|
|
204
202
|
return Message({});
|
|
205
203
|
}
|
|
206
204
|
};
|
|
205
|
+
/**
|
|
206
|
+
* Inbound guard + dispatch for the background/service-worker `runtime.onMessage`
|
|
207
|
+
* hook, which the router does not install itself. Only frames from this
|
|
208
|
+
* extension that decode to a Message with a registered methodId are processed;
|
|
209
|
+
* everything else (other extensions, reply frames, malformed traffic) is
|
|
210
|
+
* ignored. Replies travel back through the router's send path, so this never
|
|
211
|
+
* uses `sendResponse`.
|
|
212
|
+
*/
|
|
213
|
+
function createExtensionListener(router, registry) {
|
|
214
|
+
return (raw, sender) => {
|
|
215
|
+
if (sender.id !== runtime.id) return void 0;
|
|
216
|
+
if (!Array.isArray(raw)) return void 0;
|
|
217
|
+
let message;
|
|
218
|
+
try {
|
|
219
|
+
message = Message(Message.decode(new Uint8Array(raw)));
|
|
220
|
+
} catch {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (message.methodId === void 0 || !registry.getMethod(message.methodId)) return;
|
|
224
|
+
router.process(message, Message({}), { sender });
|
|
225
|
+
};
|
|
226
|
+
}
|
|
207
227
|
//#endregion
|
|
208
|
-
export { ExtensionBaseRouter, TempoExtensionRouter, TempoExtensionRouterConfiguration };
|
|
228
|
+
export { ExtensionBaseRouter, TempoExtensionRouter, TempoExtensionRouterConfiguration, createExtensionListener };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { TempoLogger } from "@tempojs/common";
|
|
2
|
+
import { BebopMethodAny, ServiceRegistry } from "@tempojs/server";
|
|
3
|
+
|
|
4
|
+
//#region src/service-registry.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* One router takes one registry, but a host can generate services across
|
|
7
|
+
* several packages (each with its own registry class). Method ids are
|
|
8
|
+
* service+method hashes (collision-free across services), so first-hit-wins
|
|
9
|
+
* lookup is safe. Generated `init()` is destructive (moves the
|
|
10
|
+
* decorator-registered singleton out of a static map), so it must run exactly
|
|
11
|
+
* once — the router constructor also calls `init()`.
|
|
12
|
+
*/
|
|
13
|
+
declare class CompositeServiceRegistry extends ServiceRegistry {
|
|
14
|
+
#private;
|
|
15
|
+
private readonly registries;
|
|
16
|
+
constructor(logger: TempoLogger, registries: readonly ServiceRegistry[]);
|
|
17
|
+
init(): void;
|
|
18
|
+
getMethod(methodId: number): BebopMethodAny | undefined;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { CompositeServiceRegistry };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ServiceRegistry } from "@tempojs/server";
|
|
2
|
+
//#region src/service-registry.ts
|
|
3
|
+
/**
|
|
4
|
+
* One router takes one registry, but a host can generate services across
|
|
5
|
+
* several packages (each with its own registry class). Method ids are
|
|
6
|
+
* service+method hashes (collision-free across services), so first-hit-wins
|
|
7
|
+
* lookup is safe. Generated `init()` is destructive (moves the
|
|
8
|
+
* decorator-registered singleton out of a static map), so it must run exactly
|
|
9
|
+
* once — the router constructor also calls `init()`.
|
|
10
|
+
*/
|
|
11
|
+
var CompositeServiceRegistry = class extends ServiceRegistry {
|
|
12
|
+
registries;
|
|
13
|
+
#initialized = false;
|
|
14
|
+
constructor(logger, registries) {
|
|
15
|
+
super(logger);
|
|
16
|
+
this.registries = registries;
|
|
17
|
+
}
|
|
18
|
+
init() {
|
|
19
|
+
if (this.#initialized) return;
|
|
20
|
+
this.#initialized = true;
|
|
21
|
+
for (const registry of this.registries) registry.init();
|
|
22
|
+
}
|
|
23
|
+
getMethod(methodId) {
|
|
24
|
+
for (const registry of this.registries) {
|
|
25
|
+
const method = registry.getMethod(methodId);
|
|
26
|
+
if (method) return method;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { CompositeServiceRegistry };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vampgg/utils",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.5",
|
|
4
4
|
"description": "Tempo RPC, Bebop serialization, transports, and async primitives for @vampgg.",
|
|
5
5
|
"homepage": "https://github.com/sammccord/vamp/tree/main/packages/utils#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"./extension-router": "./dist/extension-router.mjs",
|
|
30
30
|
"./pino-logger": "./dist/pino-logger.mjs",
|
|
31
31
|
"./router-core": "./dist/router-core.mjs",
|
|
32
|
+
"./service-registry": "./dist/service-registry.mjs",
|
|
32
33
|
"./worker-channel": "./dist/worker-channel.mjs",
|
|
33
34
|
"./worker-router": "./dist/worker-router.mjs",
|
|
34
35
|
"./ws-channel": "./dist/ws-channel.mjs",
|