@edhspace/api 0.1.0 → 0.2.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.
- package/README.md +7 -0
- package/dist/chunk-4STXB3BK.js +160 -0
- package/dist/chunk-F72S63D3.cjs +160 -0
- package/dist/index.cjs +2 -151
- package/dist/index.js +4 -153
- package/dist/sideload.cjs +213 -0
- package/dist/sideload.d.cts +290 -0
- package/dist/sideload.d.ts +290 -0
- package/dist/sideload.js +213 -0
- package/package.json +7 -2
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { D as DeckOperation, o as NetworkRequest, a as ExtensionArguments } from './contracts-r8d4RACz.js';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
declare const sideloadProtocol: "edhspace-sideload";
|
|
5
|
+
declare const sideloadVersion: 1;
|
|
6
|
+
declare const maxSideloadBytes: number;
|
|
7
|
+
declare const sideloadPanelSchema: z.ZodObject<{
|
|
8
|
+
id: z.ZodString;
|
|
9
|
+
javascript: z.ZodString;
|
|
10
|
+
css: z.ZodString;
|
|
11
|
+
}, z.core.$strict>;
|
|
12
|
+
declare const sideloadBundleSchema: z.ZodObject<{
|
|
13
|
+
protocol: z.ZodLiteral<"edhspace-sideload">;
|
|
14
|
+
version: z.ZodLiteral<1>;
|
|
15
|
+
manifest: z.ZodObject<{
|
|
16
|
+
id: z.ZodString;
|
|
17
|
+
apiVersion: z.ZodLiteral<1>;
|
|
18
|
+
version: z.ZodString;
|
|
19
|
+
title: z.ZodString;
|
|
20
|
+
description: z.ZodString;
|
|
21
|
+
author: z.ZodString;
|
|
22
|
+
permissions: z.ZodArray<z.ZodEnum<{
|
|
23
|
+
"deck.read": "deck.read";
|
|
24
|
+
"selection.read": "selection.read";
|
|
25
|
+
"deck.propose": "deck.propose";
|
|
26
|
+
"cards.inspect": "cards.inspect";
|
|
27
|
+
"cards.search": "cards.search";
|
|
28
|
+
"workspace.open": "workspace.open";
|
|
29
|
+
"notifications.show": "notifications.show";
|
|
30
|
+
"storage.read": "storage.read";
|
|
31
|
+
"storage.write": "storage.write";
|
|
32
|
+
"preferences.read": "preferences.read";
|
|
33
|
+
"preferences.write": "preferences.write";
|
|
34
|
+
"network.request": "network.request";
|
|
35
|
+
}>>;
|
|
36
|
+
panels: z.ZodArray<z.ZodObject<{
|
|
37
|
+
id: z.ZodString;
|
|
38
|
+
title: z.ZodString;
|
|
39
|
+
location: z.ZodOptional<z.ZodEnum<{
|
|
40
|
+
workspace: "workspace";
|
|
41
|
+
sidebar: "sidebar";
|
|
42
|
+
}>>;
|
|
43
|
+
}, z.core.$strict>>;
|
|
44
|
+
commands: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
45
|
+
id: z.ZodString;
|
|
46
|
+
title: z.ZodString;
|
|
47
|
+
}, z.core.$strict>>>;
|
|
48
|
+
preferences: z.ZodDefault<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
49
|
+
id: z.ZodString;
|
|
50
|
+
title: z.ZodString;
|
|
51
|
+
description: z.ZodOptional<z.ZodString>;
|
|
52
|
+
type: z.ZodLiteral<"text">;
|
|
53
|
+
default: z.ZodString;
|
|
54
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
55
|
+
id: z.ZodString;
|
|
56
|
+
title: z.ZodString;
|
|
57
|
+
description: z.ZodOptional<z.ZodString>;
|
|
58
|
+
type: z.ZodLiteral<"boolean">;
|
|
59
|
+
default: z.ZodBoolean;
|
|
60
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
61
|
+
id: z.ZodString;
|
|
62
|
+
title: z.ZodString;
|
|
63
|
+
description: z.ZodOptional<z.ZodString>;
|
|
64
|
+
type: z.ZodLiteral<"number">;
|
|
65
|
+
default: z.ZodNumber;
|
|
66
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
67
|
+
id: z.ZodString;
|
|
68
|
+
title: z.ZodString;
|
|
69
|
+
description: z.ZodOptional<z.ZodString>;
|
|
70
|
+
type: z.ZodLiteral<"select">;
|
|
71
|
+
default: z.ZodString;
|
|
72
|
+
options: z.ZodArray<z.ZodObject<{
|
|
73
|
+
value: z.ZodString;
|
|
74
|
+
label: z.ZodString;
|
|
75
|
+
}, z.core.$strict>>;
|
|
76
|
+
}, z.core.$strict>], "type">>>;
|
|
77
|
+
network: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
78
|
+
id: z.ZodString;
|
|
79
|
+
title: z.ZodString;
|
|
80
|
+
url: z.ZodString;
|
|
81
|
+
methods: z.ZodArray<z.ZodEnum<{
|
|
82
|
+
GET: "GET";
|
|
83
|
+
POST: "POST";
|
|
84
|
+
}>>;
|
|
85
|
+
}, z.core.$strict>>>;
|
|
86
|
+
}, z.core.$strict>;
|
|
87
|
+
panels: z.ZodArray<z.ZodObject<{
|
|
88
|
+
id: z.ZodString;
|
|
89
|
+
javascript: z.ZodString;
|
|
90
|
+
css: z.ZodString;
|
|
91
|
+
}, z.core.$strict>>;
|
|
92
|
+
}, z.core.$strict>;
|
|
93
|
+
type SideloadBundle = z.infer<typeof sideloadBundleSchema>;
|
|
94
|
+
type SideloadPanel = z.infer<typeof sideloadPanelSchema>;
|
|
95
|
+
declare const sideloadReadySchema: z.ZodObject<{
|
|
96
|
+
type: z.ZodLiteral<"ready">;
|
|
97
|
+
panelId: z.ZodString;
|
|
98
|
+
protocol: z.ZodLiteral<"edhspace-sideload">;
|
|
99
|
+
version: z.ZodLiteral<1>;
|
|
100
|
+
}, z.core.$strict>;
|
|
101
|
+
declare const sideloadInitSchema: z.ZodObject<{
|
|
102
|
+
type: z.ZodLiteral<"init">;
|
|
103
|
+
sessionId: z.ZodUUID;
|
|
104
|
+
permissions: z.ZodArray<z.ZodEnum<{
|
|
105
|
+
"deck.read": "deck.read";
|
|
106
|
+
"selection.read": "selection.read";
|
|
107
|
+
"deck.propose": "deck.propose";
|
|
108
|
+
"cards.inspect": "cards.inspect";
|
|
109
|
+
"cards.search": "cards.search";
|
|
110
|
+
"workspace.open": "workspace.open";
|
|
111
|
+
"notifications.show": "notifications.show";
|
|
112
|
+
"storage.read": "storage.read";
|
|
113
|
+
"storage.write": "storage.write";
|
|
114
|
+
"preferences.read": "preferences.read";
|
|
115
|
+
"preferences.write": "preferences.write";
|
|
116
|
+
"network.request": "network.request";
|
|
117
|
+
}>>;
|
|
118
|
+
revision: z.ZodString;
|
|
119
|
+
theme: z.ZodEnum<{
|
|
120
|
+
light: "light";
|
|
121
|
+
dark: "dark";
|
|
122
|
+
}>;
|
|
123
|
+
protocol: z.ZodLiteral<"edhspace-sideload">;
|
|
124
|
+
version: z.ZodLiteral<1>;
|
|
125
|
+
}, z.core.$strict>;
|
|
126
|
+
declare const sideloadStateSchema: z.ZodObject<{
|
|
127
|
+
type: z.ZodLiteral<"state">;
|
|
128
|
+
revision: z.ZodString;
|
|
129
|
+
theme: z.ZodEnum<{
|
|
130
|
+
light: "light";
|
|
131
|
+
dark: "dark";
|
|
132
|
+
}>;
|
|
133
|
+
protocol: z.ZodLiteral<"edhspace-sideload">;
|
|
134
|
+
version: z.ZodLiteral<1>;
|
|
135
|
+
}, z.core.$strict>;
|
|
136
|
+
declare const sideloadRevokeSchema: z.ZodObject<{
|
|
137
|
+
type: z.ZodLiteral<"revoke">;
|
|
138
|
+
protocol: z.ZodLiteral<"edhspace-sideload">;
|
|
139
|
+
version: z.ZodLiteral<1>;
|
|
140
|
+
}, z.core.$strict>;
|
|
141
|
+
declare const sideloadErrorSchema: z.ZodObject<{
|
|
142
|
+
type: z.ZodLiteral<"error">;
|
|
143
|
+
message: z.ZodString;
|
|
144
|
+
protocol: z.ZodLiteral<"edhspace-sideload">;
|
|
145
|
+
version: z.ZodLiteral<1>;
|
|
146
|
+
}, z.core.$strict>;
|
|
147
|
+
type SideloadInit = z.infer<typeof sideloadInitSchema>;
|
|
148
|
+
type SideloadState = Pick<SideloadInit, "revision" | "theme"> & {
|
|
149
|
+
error?: Error;
|
|
150
|
+
};
|
|
151
|
+
declare function createSideloadConnection(port: MessagePort, raw: unknown): {
|
|
152
|
+
api: {
|
|
153
|
+
permissions: readonly ("deck.read" | "selection.read" | "deck.propose" | "cards.inspect" | "cards.search" | "workspace.open" | "notifications.show" | "storage.read" | "storage.write" | "preferences.read" | "preferences.write" | "network.request")[];
|
|
154
|
+
deck: {
|
|
155
|
+
read: () => Promise<{
|
|
156
|
+
id: string;
|
|
157
|
+
name: string;
|
|
158
|
+
format: string;
|
|
159
|
+
revision: string;
|
|
160
|
+
cards: {
|
|
161
|
+
id: string;
|
|
162
|
+
name: string;
|
|
163
|
+
quantity: number;
|
|
164
|
+
manaCost?: string | undefined;
|
|
165
|
+
manaValue?: number | undefined;
|
|
166
|
+
typeLine?: string | undefined;
|
|
167
|
+
commander?: boolean | undefined;
|
|
168
|
+
}[];
|
|
169
|
+
}>;
|
|
170
|
+
propose: (operations: DeckOperation[]) => Promise<{
|
|
171
|
+
id: string;
|
|
172
|
+
status: "pending" | "dismissed";
|
|
173
|
+
revision?: string | undefined;
|
|
174
|
+
undoId?: string | undefined;
|
|
175
|
+
} | {
|
|
176
|
+
id: string;
|
|
177
|
+
status: "applied";
|
|
178
|
+
revision: string;
|
|
179
|
+
undoId: string;
|
|
180
|
+
}>;
|
|
181
|
+
addCard: (cardId: string, category: string, quantity?: number) => Promise<{
|
|
182
|
+
id: string;
|
|
183
|
+
status: "pending" | "dismissed";
|
|
184
|
+
revision?: string | undefined;
|
|
185
|
+
undoId?: string | undefined;
|
|
186
|
+
} | {
|
|
187
|
+
id: string;
|
|
188
|
+
status: "applied";
|
|
189
|
+
revision: string;
|
|
190
|
+
undoId: string;
|
|
191
|
+
}>;
|
|
192
|
+
removeCard: (cardId: string, quantity?: number) => Promise<{
|
|
193
|
+
id: string;
|
|
194
|
+
status: "pending" | "dismissed";
|
|
195
|
+
revision?: string | undefined;
|
|
196
|
+
undoId?: string | undefined;
|
|
197
|
+
} | {
|
|
198
|
+
id: string;
|
|
199
|
+
status: "applied";
|
|
200
|
+
revision: string;
|
|
201
|
+
undoId: string;
|
|
202
|
+
}>;
|
|
203
|
+
applyTags: (cardIds: string[], tags: string[]) => Promise<{
|
|
204
|
+
id: string;
|
|
205
|
+
status: "pending" | "dismissed";
|
|
206
|
+
revision?: string | undefined;
|
|
207
|
+
undoId?: string | undefined;
|
|
208
|
+
} | {
|
|
209
|
+
id: string;
|
|
210
|
+
status: "applied";
|
|
211
|
+
revision: string;
|
|
212
|
+
undoId: string;
|
|
213
|
+
}>;
|
|
214
|
+
};
|
|
215
|
+
selection: {
|
|
216
|
+
read: () => Promise<{
|
|
217
|
+
id: string;
|
|
218
|
+
name: string;
|
|
219
|
+
quantity: number;
|
|
220
|
+
manaCost?: string | undefined;
|
|
221
|
+
manaValue?: number | undefined;
|
|
222
|
+
typeLine?: string | undefined;
|
|
223
|
+
commander?: boolean | undefined;
|
|
224
|
+
}[]>;
|
|
225
|
+
};
|
|
226
|
+
network: {
|
|
227
|
+
request: (request: NetworkRequest) => Promise<{
|
|
228
|
+
status: number;
|
|
229
|
+
data: z.core.util.JSONType;
|
|
230
|
+
}>;
|
|
231
|
+
};
|
|
232
|
+
cards: {
|
|
233
|
+
inspect: (cardId: string) => Promise<{
|
|
234
|
+
opened: true;
|
|
235
|
+
}>;
|
|
236
|
+
search: (query: string, page?: number) => Promise<{
|
|
237
|
+
cards: {
|
|
238
|
+
id: string;
|
|
239
|
+
name: string;
|
|
240
|
+
quantity: number;
|
|
241
|
+
manaCost?: string | undefined;
|
|
242
|
+
manaValue?: number | undefined;
|
|
243
|
+
typeLine?: string | undefined;
|
|
244
|
+
commander?: boolean | undefined;
|
|
245
|
+
}[];
|
|
246
|
+
nextPage?: number | undefined;
|
|
247
|
+
}>;
|
|
248
|
+
getByName: (name: string) => Promise<{
|
|
249
|
+
id: string;
|
|
250
|
+
name: string;
|
|
251
|
+
quantity: number;
|
|
252
|
+
manaCost?: string | undefined;
|
|
253
|
+
manaValue?: number | undefined;
|
|
254
|
+
typeLine?: string | undefined;
|
|
255
|
+
commander?: boolean | undefined;
|
|
256
|
+
} | null>;
|
|
257
|
+
};
|
|
258
|
+
workspace: {
|
|
259
|
+
openPanel: (panelId: string, location?: "workspace" | "sidebar") => Promise<{
|
|
260
|
+
panelId: string;
|
|
261
|
+
}>;
|
|
262
|
+
};
|
|
263
|
+
notifications: {
|
|
264
|
+
show: (message: string, kind?: "info" | "success" | "error") => Promise<{
|
|
265
|
+
shown: true;
|
|
266
|
+
}>;
|
|
267
|
+
};
|
|
268
|
+
storage: {
|
|
269
|
+
get: (key: string) => Promise<z.core.util.JSONType>;
|
|
270
|
+
set: (key: string, value: ExtensionArguments["storage.set"]["value"]) => Promise<{
|
|
271
|
+
saved: true;
|
|
272
|
+
}>;
|
|
273
|
+
};
|
|
274
|
+
preferences: {
|
|
275
|
+
get: () => Promise<Record<string, z.core.util.JSONType>>;
|
|
276
|
+
set: (key: string, value: ExtensionArguments["preferences.set"]["value"]) => Promise<{
|
|
277
|
+
saved: true;
|
|
278
|
+
}>;
|
|
279
|
+
};
|
|
280
|
+
dispose: () => void;
|
|
281
|
+
};
|
|
282
|
+
getSnapshot: () => SideloadState;
|
|
283
|
+
subscribe: (listener: () => void) => () => void;
|
|
284
|
+
dispose: () => void;
|
|
285
|
+
};
|
|
286
|
+
type SideloadConnection = ReturnType<typeof createSideloadConnection>;
|
|
287
|
+
declare function connectSideload(panel: string, timeoutMs?: number): Promise<SideloadConnection>;
|
|
288
|
+
declare function createSideloadDocument(raw: SideloadPanel): string;
|
|
289
|
+
|
|
290
|
+
export { type SideloadBundle, type SideloadConnection, type SideloadInit, type SideloadPanel, type SideloadState, connectSideload, createSideloadConnection, createSideloadDocument, maxSideloadBytes, sideloadBundleSchema, sideloadErrorSchema, sideloadInitSchema, sideloadPanelSchema, sideloadProtocol, sideloadReadySchema, sideloadRevokeSchema, sideloadStateSchema, sideloadVersion };
|
package/dist/sideload.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createExtensionClient
|
|
3
|
+
} from "./chunk-4STXB3BK.js";
|
|
4
|
+
import {
|
|
5
|
+
ExtensionError,
|
|
6
|
+
extensionManifestSchema,
|
|
7
|
+
maxResponseBytes,
|
|
8
|
+
messageBytes,
|
|
9
|
+
permissionSchema,
|
|
10
|
+
responseSchema
|
|
11
|
+
} from "./chunk-LBLXK5J2.js";
|
|
12
|
+
|
|
13
|
+
// src/sideload.ts
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
var sideloadProtocol = "edhspace-sideload";
|
|
16
|
+
var sideloadVersion = 1;
|
|
17
|
+
var maxSideloadBytes = 8 * 1024 * 1024;
|
|
18
|
+
var bytes = (value) => new TextEncoder().encode(value).byteLength;
|
|
19
|
+
var panelId = z.string().regex(/^[a-z0-9][a-z0-9.-]{0,99}$/);
|
|
20
|
+
var sideloadPanelSchema = z.strictObject({
|
|
21
|
+
id: panelId,
|
|
22
|
+
javascript: z.string().min(1).max(2 * 1024 * 1024).refine(
|
|
23
|
+
(value) => bytes(value) <= 2 * 1024 * 1024,
|
|
24
|
+
"Sideload JavaScript exceeds 2 MiB."
|
|
25
|
+
),
|
|
26
|
+
css: z.string().max(262144).refine(
|
|
27
|
+
(value) => bytes(value) <= 262144,
|
|
28
|
+
"Sideload CSS exceeds 256 KiB."
|
|
29
|
+
)
|
|
30
|
+
});
|
|
31
|
+
var sideloadBundleSchema = z.strictObject({
|
|
32
|
+
protocol: z.literal(sideloadProtocol),
|
|
33
|
+
version: z.literal(sideloadVersion),
|
|
34
|
+
manifest: extensionManifestSchema,
|
|
35
|
+
panels: z.array(sideloadPanelSchema).min(1).max(16)
|
|
36
|
+
}).superRefine((bundle, context) => {
|
|
37
|
+
const ids = new Set(bundle.panels.map((panel) => panel.id));
|
|
38
|
+
if (ids.size !== bundle.panels.length || ids.size !== bundle.manifest.panels.length || bundle.manifest.panels.some((panel) => !ids.has(panel.id)))
|
|
39
|
+
context.addIssue({
|
|
40
|
+
code: "custom",
|
|
41
|
+
message: "The bundle must contain exactly one entry for each declared panel."
|
|
42
|
+
});
|
|
43
|
+
if (bytes(JSON.stringify(bundle)) > maxSideloadBytes)
|
|
44
|
+
context.addIssue({ code: "custom", message: "Sideload bundle exceeds 8 MiB." });
|
|
45
|
+
});
|
|
46
|
+
var envelope = {
|
|
47
|
+
protocol: z.literal(sideloadProtocol),
|
|
48
|
+
version: z.literal(sideloadVersion)
|
|
49
|
+
};
|
|
50
|
+
var sideloadReadySchema = z.strictObject({
|
|
51
|
+
...envelope,
|
|
52
|
+
type: z.literal("ready"),
|
|
53
|
+
panelId
|
|
54
|
+
});
|
|
55
|
+
var sideloadInitSchema = z.strictObject({
|
|
56
|
+
...envelope,
|
|
57
|
+
type: z.literal("init"),
|
|
58
|
+
sessionId: z.uuid(),
|
|
59
|
+
permissions: z.array(permissionSchema).max(12).refine(
|
|
60
|
+
(values) => new Set(values).size === values.length
|
|
61
|
+
),
|
|
62
|
+
revision: z.string().max(300),
|
|
63
|
+
theme: z.enum(["light", "dark"])
|
|
64
|
+
});
|
|
65
|
+
var sideloadStateSchema = z.strictObject({
|
|
66
|
+
...envelope,
|
|
67
|
+
type: z.literal("state"),
|
|
68
|
+
revision: z.string().max(300),
|
|
69
|
+
theme: z.enum(["light", "dark"])
|
|
70
|
+
});
|
|
71
|
+
var sideloadRevokeSchema = z.strictObject({
|
|
72
|
+
...envelope,
|
|
73
|
+
type: z.literal("revoke")
|
|
74
|
+
});
|
|
75
|
+
var sideloadErrorSchema = z.strictObject({
|
|
76
|
+
...envelope,
|
|
77
|
+
type: z.literal("error"),
|
|
78
|
+
message: z.string().min(1).max(300)
|
|
79
|
+
});
|
|
80
|
+
function createSideloadConnection(port, raw) {
|
|
81
|
+
const init = sideloadInitSchema.parse(raw);
|
|
82
|
+
let state = { revision: init.revision, theme: init.theme };
|
|
83
|
+
let closed = false;
|
|
84
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
85
|
+
const pending = /* @__PURE__ */ new Map();
|
|
86
|
+
const publish = (next) => {
|
|
87
|
+
state = next;
|
|
88
|
+
for (const listener of listeners) listener();
|
|
89
|
+
};
|
|
90
|
+
const close = (error) => {
|
|
91
|
+
if (closed) return;
|
|
92
|
+
closed = true;
|
|
93
|
+
for (const call of pending.values()) {
|
|
94
|
+
clearTimeout(call.timer);
|
|
95
|
+
call.reject(error);
|
|
96
|
+
}
|
|
97
|
+
pending.clear();
|
|
98
|
+
api.dispose();
|
|
99
|
+
port.removeEventListener("message", receive);
|
|
100
|
+
port.removeEventListener("messageerror", invalid);
|
|
101
|
+
port.close();
|
|
102
|
+
publish({ ...state, error });
|
|
103
|
+
listeners.clear();
|
|
104
|
+
};
|
|
105
|
+
const invalid = () => close(new ExtensionError("The host sent an invalid message.", "invalid"));
|
|
106
|
+
const receive = (event) => {
|
|
107
|
+
if (closed) return;
|
|
108
|
+
if (sideloadRevokeSchema.safeParse(event.data).success) {
|
|
109
|
+
close(new ExtensionError("Extension access has been revoked.", "revoked"));
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const update = sideloadStateSchema.safeParse(event.data);
|
|
113
|
+
if (update.success) {
|
|
114
|
+
publish({ revision: update.data.revision, theme: update.data.theme });
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
if (messageBytes(event.data) > maxResponseBytes) return invalid();
|
|
119
|
+
} catch {
|
|
120
|
+
return invalid();
|
|
121
|
+
}
|
|
122
|
+
const response = responseSchema.safeParse(event.data);
|
|
123
|
+
if (!response.success || response.data.sessionId !== init.sessionId) return invalid();
|
|
124
|
+
const call = pending.get(response.data.id);
|
|
125
|
+
if (!call) return;
|
|
126
|
+
pending.delete(response.data.id);
|
|
127
|
+
clearTimeout(call.timer);
|
|
128
|
+
call.resolve(response.data);
|
|
129
|
+
};
|
|
130
|
+
const api = createExtensionClient((request) => new Promise((resolve, reject) => {
|
|
131
|
+
if (closed) {
|
|
132
|
+
reject(new ExtensionError("Extension access has been revoked.", "revoked"));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const timer = setTimeout(() => {
|
|
136
|
+
pending.delete(request.id);
|
|
137
|
+
reject(new ExtensionError("The extension operation timed out.", "timeout"));
|
|
138
|
+
}, request.method === "network.request" ? 9e4 : 1e4);
|
|
139
|
+
pending.set(request.id, { resolve, reject, timer });
|
|
140
|
+
try {
|
|
141
|
+
port.postMessage(request);
|
|
142
|
+
} catch {
|
|
143
|
+
close(new ExtensionError("The extension transport failed.", "unavailable"));
|
|
144
|
+
}
|
|
145
|
+
}), init.sessionId, init.permissions);
|
|
146
|
+
port.addEventListener("message", receive);
|
|
147
|
+
port.addEventListener("messageerror", invalid);
|
|
148
|
+
port.start();
|
|
149
|
+
return {
|
|
150
|
+
api,
|
|
151
|
+
getSnapshot: () => state,
|
|
152
|
+
subscribe: (listener) => {
|
|
153
|
+
listeners.add(listener);
|
|
154
|
+
return () => {
|
|
155
|
+
listeners.delete(listener);
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
dispose: () => close(new ExtensionError("Extension access has been revoked.", "revoked"))
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function connectSideload(panel, timeoutMs = 1e4) {
|
|
162
|
+
panelId.parse(panel);
|
|
163
|
+
if (window.parent === window)
|
|
164
|
+
return Promise.reject(new ExtensionError("Open this bundle through EDHspace.", "unavailable"));
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
const timer = setTimeout(() => {
|
|
167
|
+
window.removeEventListener("message", initialize);
|
|
168
|
+
reject(new ExtensionError("EDHspace did not connect to the extension.", "timeout"));
|
|
169
|
+
}, timeoutMs);
|
|
170
|
+
function initialize(event) {
|
|
171
|
+
if (event.source !== window.parent) return;
|
|
172
|
+
const init = sideloadInitSchema.safeParse(event.data);
|
|
173
|
+
if (!init.success || event.ports.length !== 1) return;
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
window.removeEventListener("message", initialize);
|
|
176
|
+
resolve(createSideloadConnection(event.ports[0], init.data));
|
|
177
|
+
}
|
|
178
|
+
window.addEventListener("message", initialize);
|
|
179
|
+
window.parent.postMessage({
|
|
180
|
+
protocol: sideloadProtocol,
|
|
181
|
+
version: sideloadVersion,
|
|
182
|
+
type: "ready",
|
|
183
|
+
panelId: panel
|
|
184
|
+
}, "*");
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
var base64 = (value) => {
|
|
188
|
+
const encoded = new TextEncoder().encode(value);
|
|
189
|
+
let binary = "";
|
|
190
|
+
for (let offset = 0; offset < encoded.length; offset += 8192)
|
|
191
|
+
binary += String.fromCharCode(...encoded.subarray(offset, offset + 8192));
|
|
192
|
+
return btoa(binary);
|
|
193
|
+
};
|
|
194
|
+
function createSideloadDocument(raw) {
|
|
195
|
+
const panel = sideloadPanelSchema.parse(raw);
|
|
196
|
+
const csp = "default-src 'none'; script-src data:; style-src 'unsafe-inline' data:; img-src data: blob:; font-src data:; connect-src 'none'; frame-src 'none'; worker-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'";
|
|
197
|
+
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="${csp}"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="stylesheet" href="data:text/css;base64,${base64(panel.css)}"><style>body{margin:0;font:16px system-ui}html[data-theme=dark]{color-scheme:dark;--set-panel:#202620;--set-text:#f3f6f3;--set-muted:#bfcbbf;--set-accent:#8fca9b;--set-border:#81968b}</style></head><body><div id="root"></div><script src="data:text/javascript;base64,${base64(panel.javascript)}"></script></body></html>`;
|
|
198
|
+
}
|
|
199
|
+
export {
|
|
200
|
+
connectSideload,
|
|
201
|
+
createSideloadConnection,
|
|
202
|
+
createSideloadDocument,
|
|
203
|
+
maxSideloadBytes,
|
|
204
|
+
sideloadBundleSchema,
|
|
205
|
+
sideloadErrorSchema,
|
|
206
|
+
sideloadInitSchema,
|
|
207
|
+
sideloadPanelSchema,
|
|
208
|
+
sideloadProtocol,
|
|
209
|
+
sideloadReadySchema,
|
|
210
|
+
sideloadRevokeSchema,
|
|
211
|
+
sideloadStateSchema,
|
|
212
|
+
sideloadVersion
|
|
213
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edhspace/api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "Typed capability API for curated native EDHspace extensions",
|
|
@@ -23,6 +23,11 @@
|
|
|
23
23
|
"README.md"
|
|
24
24
|
],
|
|
25
25
|
"exports": {
|
|
26
|
+
"./sideload": {
|
|
27
|
+
"types": "./dist/sideload.d.ts",
|
|
28
|
+
"import": "./dist/sideload.js",
|
|
29
|
+
"require": "./dist/sideload.cjs"
|
|
30
|
+
},
|
|
26
31
|
"./math": {
|
|
27
32
|
"types": "./dist/math.d.ts",
|
|
28
33
|
"import": "./dist/math.js",
|
|
@@ -61,7 +66,7 @@
|
|
|
61
66
|
}
|
|
62
67
|
},
|
|
63
68
|
"devDependencies": {
|
|
64
|
-
"@edhspace/ui": "^0.
|
|
69
|
+
"@edhspace/ui": "^0.2.0",
|
|
65
70
|
"@types/node": "^20.10.0",
|
|
66
71
|
"@types/react": "^19.2.18",
|
|
67
72
|
"@types/react-dom": "^19.2.4",
|