@goodfellow-cms/admin 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,119 +0,0 @@
1
- //#region src/frame/channel.ts
2
- /**
3
- * Requests and events between the admin panel and a frame running the site's
4
- * code, over one `MessagePort`. The admin panel's side treats everything a
5
- * frame sends as untrusted: the site's blocks run there, and could send anything.
6
- */
7
- /** The message the admin panel's page sends a frame once it has loaded, with the port to talk on. */
8
- const CONNECT_MESSAGE = "goodfellow:connect";
9
- /** A request the other side refused or couldn't answer. */
10
- var ChannelError = class extends Error {
11
- name = "ChannelError";
12
- };
13
- function isMessage(data) {
14
- if (typeof data !== "object" || data === null) return false;
15
- const type = data.type;
16
- return type === "request" || type === "response" || type === "event";
17
- }
18
- /** What an error says, as text: errors can't cross the port as they are. */
19
- function errorText(error) {
20
- return error instanceof Error ? error.message : String(error);
21
- }
22
- function createChannel(port, handlers = {}) {
23
- const own = { ...handlers };
24
- const pending = /* @__PURE__ */ new Map();
25
- const listeners = /* @__PURE__ */ new Map();
26
- let next = 1;
27
- let closed = false;
28
- port.onmessage = async (event) => {
29
- const message = event.data;
30
- if (!isMessage(message)) return;
31
- if (message.type === "response") {
32
- const waiting = pending.get(message.id);
33
- if (!waiting) return;
34
- pending.delete(message.id);
35
- if (message.ok) waiting.resolve(message.result);
36
- else waiting.reject(new ChannelError(message.error));
37
- return;
38
- }
39
- if (message.type === "event") {
40
- for (const listener of listeners.get(message.name) ?? []) listener(message.payload);
41
- return;
42
- }
43
- const handler = typeof message.method === "string" ? Object.hasOwn(own, message.method) && own[message.method] : void 0;
44
- if (!handler) {
45
- port.postMessage({
46
- type: "response",
47
- id: message.id,
48
- ok: false,
49
- error: `No such request: ${message.method}`
50
- });
51
- return;
52
- }
53
- try {
54
- const result = await handler(message.params);
55
- if (!closed) port.postMessage({
56
- type: "response",
57
- id: message.id,
58
- ok: true,
59
- result
60
- });
61
- } catch (error) {
62
- if (!closed) port.postMessage({
63
- type: "response",
64
- id: message.id,
65
- ok: false,
66
- error: errorText(error)
67
- });
68
- }
69
- };
70
- return {
71
- call(method, params, transfer = []) {
72
- if (closed) return Promise.reject(new ChannelError("The frame has closed."));
73
- const id = next++;
74
- return new Promise((resolve, reject) => {
75
- pending.set(id, {
76
- resolve,
77
- reject
78
- });
79
- port.postMessage({
80
- type: "request",
81
- id,
82
- method,
83
- params
84
- }, transfer);
85
- });
86
- },
87
- emit(name, payload) {
88
- if (!closed) port.postMessage({
89
- type: "event",
90
- name,
91
- payload
92
- });
93
- },
94
- on(name, listener) {
95
- const set = listeners.get(name) ?? /* @__PURE__ */ new Set();
96
- listeners.set(name, set);
97
- set.add(listener);
98
- return () => set.delete(listener);
99
- },
100
- handle(method, handler) {
101
- own[method] = handler;
102
- return () => {
103
- if (own[method] === handler) delete own[method];
104
- };
105
- },
106
- close() {
107
- closed = true;
108
- port.close();
109
- for (const waiting of pending.values()) waiting.reject(new ChannelError("The frame has closed."));
110
- pending.clear();
111
- }
112
- };
113
- }
114
- //#endregion
115
- //#region src/frame/plugin-protocol.ts
116
- /** A plugin's frame sends this to the admin panel's page once it's listening, and the page answers with a port. */
117
- const PLUGIN_READY_MESSAGE = "goodfellow:plugin-ready";
118
- //#endregion
119
- export { CONNECT_MESSAGE as n, createChannel as r, PLUGIN_READY_MESSAGE as t };