@impro.social/impro-plugin 0.0.23 → 0.0.24
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/main.d.ts +159 -13
- package/main.js +192 -15
- package/package.json +1 -1
package/main.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Proxied fetch through the host.
|
|
3
|
-
*
|
|
2
|
+
* Proxied fetch through the host. The target URL must match one of the
|
|
3
|
+
* `permissions.fetch` URL patterns declared in the plugin's manifest, which
|
|
4
|
+
* the user grants at install.
|
|
4
5
|
* `init` accepts `method`, `headers` (plain object, `Headers`, `Map`, or
|
|
5
6
|
* `[name, value]` iterable), and a string `body`. Resolves to a
|
|
6
7
|
* {@link PluginResponse}.
|
|
@@ -9,6 +10,30 @@
|
|
|
9
10
|
* @returns {Promise<PluginResponse>}
|
|
10
11
|
*/
|
|
11
12
|
export function fetch(url: string, init?: PluginFetchInit): Promise<PluginResponse>;
|
|
13
|
+
/**
|
|
14
|
+
* Asks the user to grant this plugin network access to `url`'s origin — for
|
|
15
|
+
* endpoints the plugin can't know in advance, such as a user-supplied API
|
|
16
|
+
* provider. Requires `permissions.userFetch` in the manifest.
|
|
17
|
+
*
|
|
18
|
+
* Grants are origin-scoped (scheme, host, and port; the path is ignored) and
|
|
19
|
+
* persist until the user revokes them in the app's plugin settings. Resolves
|
|
20
|
+
* `true` without prompting if the origin is already permitted, so it is safe
|
|
21
|
+
* to call before every request. Resolves `false` if the user declines, if
|
|
22
|
+
* `url` can't be an origin, or if a prompt for this plugin is already open.
|
|
23
|
+
*
|
|
24
|
+
* Any credential for the endpoint is the plugin's to hold — use
|
|
25
|
+
* {@link Plugin.saveLocalData} rather than {@link Plugin.saveData}, which
|
|
26
|
+
* syncs through the user's account preferences.
|
|
27
|
+
* @param {string} url
|
|
28
|
+
* @returns {Promise<boolean>}
|
|
29
|
+
*/
|
|
30
|
+
export function requestFetchPermission(url: string): Promise<boolean>;
|
|
31
|
+
/**
|
|
32
|
+
* The origins the user has granted this plugin, as fetch patterns
|
|
33
|
+
* (`https://example.com/*`). Manifest-declared permissions are not included.
|
|
34
|
+
* @returns {Promise<string[]>}
|
|
35
|
+
*/
|
|
36
|
+
export function getUserGrantedFetchOrigins(): Promise<string[]>;
|
|
12
37
|
/**
|
|
13
38
|
* Convenience wrapper that returns a new {@link FlattenedTokens} for `tokens`.
|
|
14
39
|
* @param {RichTextToken[]} tokens
|
|
@@ -26,6 +51,15 @@ export function flattenForScan(tokens: RichTextToken[]): FlattenedTokens;
|
|
|
26
51
|
* Paginated response from `getKnownFollowers`: `{ followers, cursor }`.
|
|
27
52
|
* @typedef {Record<string, unknown>} RepoRecord
|
|
28
53
|
* A raw repo record: `{ uri, cid, value }`.
|
|
54
|
+
* @typedef {Record<string, unknown>} PostThreadView
|
|
55
|
+
* Hydrated thread for a post: the post plus `parent`/`replies`.
|
|
56
|
+
* @typedef {Record<string, unknown>} ListView
|
|
57
|
+
* An `app.bsky.graph.defs#listView` shape.
|
|
58
|
+
* @typedef {Record<string, unknown>} FeedGeneratorView
|
|
59
|
+
* An `app.bsky.feed.defs#generatorView` shape.
|
|
60
|
+
* @typedef {{ ok: boolean, status: number, data: unknown }} XrpcQueryResponse
|
|
61
|
+
* Raw XRPC response: on failure `data` is the error body
|
|
62
|
+
* (`{ error, message }`) when the service provided one.
|
|
29
63
|
* @typedef {{ did: string, collection: string, rkey: string }} BacklinkRecord
|
|
30
64
|
* A record that links to a queried subject.
|
|
31
65
|
* @typedef {Record<string, unknown>} FeedItem
|
|
@@ -214,6 +248,84 @@ export class PluginData {
|
|
|
214
248
|
source: string;
|
|
215
249
|
limit?: number;
|
|
216
250
|
}): Promise<BacklinkRecord[]>;
|
|
251
|
+
/**
|
|
252
|
+
* Fetch the hydrated thread around a post (the post, its parents, and
|
|
253
|
+
* replies), as the host renders it.
|
|
254
|
+
* @param {string} uri
|
|
255
|
+
* @returns {Promise<PostThreadView | null>}
|
|
256
|
+
*/
|
|
257
|
+
getPostThread(uri: string): Promise<PostThreadView | null>;
|
|
258
|
+
/**
|
|
259
|
+
* Fetch a list's metadata view by AT-URI.
|
|
260
|
+
* @param {string} uri
|
|
261
|
+
* @returns {Promise<ListView | null>}
|
|
262
|
+
*/
|
|
263
|
+
getList(uri: string): Promise<ListView | null>;
|
|
264
|
+
/**
|
|
265
|
+
* Fetch a feed generator view by AT-URI.
|
|
266
|
+
* @param {string} uri
|
|
267
|
+
* @returns {Promise<FeedGeneratorView | null>}
|
|
268
|
+
*/
|
|
269
|
+
getFeedGenerator(uri: string): Promise<FeedGeneratorView | null>;
|
|
270
|
+
/**
|
|
271
|
+
* The current user's full hydrated profile (unlike `app.currentUser`,
|
|
272
|
+
* which carries only `did` and `handle`). `null` when signed out.
|
|
273
|
+
* @returns {Promise<DetailedProfileView | null>}
|
|
274
|
+
*/
|
|
275
|
+
getCurrentUserProfile(): Promise<DetailedProfileView | null>;
|
|
276
|
+
/**
|
|
277
|
+
* Call a read-only AppView query endpoint through the current user's
|
|
278
|
+
* session and get the raw XRPC response back.
|
|
279
|
+
*
|
|
280
|
+
* Only an allowlisted set of `app.bsky.*` query NSIDs is accepted;
|
|
281
|
+
* viewer-private queries (mutes, bookmarks, notifications, preferences,
|
|
282
|
+
* timeline, ...) additionally require the `"privateData"` action
|
|
283
|
+
* permission. Unlike the curated methods above, responses are canonical
|
|
284
|
+
* server state — they may briefly disagree with what the host UI shows
|
|
285
|
+
* while an optimistic update is in flight, and results are not cached.
|
|
286
|
+
*
|
|
287
|
+
* @param {string} nsid e.g. `"app.bsky.feed.getQuotes"`
|
|
288
|
+
* @param {Record<string, string | number | boolean | string[]>} [params]
|
|
289
|
+
* @returns {Promise<XrpcQueryResponse>}
|
|
290
|
+
*/
|
|
291
|
+
xrpcQuery(nsid: string, params?: Record<string, string | number | boolean | string[]>): Promise<XrpcQueryResponse>;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Host-mediated persistent storage for binary data too large for
|
|
295
|
+
* {@link Plugin.loadLocalData}/{@link Plugin.saveLocalData}.
|
|
296
|
+
* Namespaced per plugin; survives reloads but is cleared on
|
|
297
|
+
* uninstall.
|
|
298
|
+
*/
|
|
299
|
+
export class BinaryCache {
|
|
300
|
+
/**
|
|
301
|
+
* @param {string} key
|
|
302
|
+
* @returns {Promise<ArrayBuffer | null>}
|
|
303
|
+
*/
|
|
304
|
+
get(key: string): Promise<ArrayBuffer | null>;
|
|
305
|
+
/**
|
|
306
|
+
* Whether an entry is stored under `key`, without transferring its bytes.
|
|
307
|
+
*
|
|
308
|
+
* @param {string} key
|
|
309
|
+
* @returns {Promise<boolean>}
|
|
310
|
+
*/
|
|
311
|
+
has(key: string): Promise<boolean>;
|
|
312
|
+
/**
|
|
313
|
+
* Every key this plugin currently has stored, in no particular order.
|
|
314
|
+
*
|
|
315
|
+
* @returns {Promise<string[]>}
|
|
316
|
+
*/
|
|
317
|
+
keys(): Promise<string[]>;
|
|
318
|
+
/**
|
|
319
|
+
* @param {string} key
|
|
320
|
+
* @param {ArrayBuffer | ArrayBufferView} data
|
|
321
|
+
* @returns {Promise<void>}
|
|
322
|
+
*/
|
|
323
|
+
put(key: string, data: ArrayBuffer | ArrayBufferView): Promise<void>;
|
|
324
|
+
/**
|
|
325
|
+
* @param {string} key
|
|
326
|
+
* @returns {Promise<void>}
|
|
327
|
+
*/
|
|
328
|
+
delete(key: string): Promise<void>;
|
|
217
329
|
}
|
|
218
330
|
/**
|
|
219
331
|
* The plugin's handle to the running impro app. Exposed as `this.app` on a
|
|
@@ -229,6 +341,8 @@ export class App {
|
|
|
229
341
|
currentUser: ProfileView | null;
|
|
230
342
|
/** Read-only appview accessors — see {@link PluginData}. */
|
|
231
343
|
data: PluginData;
|
|
344
|
+
/** Host-mediated binary storage — see {@link BinaryCache}. */
|
|
345
|
+
binaryCache: BinaryCache;
|
|
232
346
|
/**
|
|
233
347
|
* Register an event listener. Supported events:
|
|
234
348
|
*
|
|
@@ -299,9 +413,10 @@ export class App {
|
|
|
299
413
|
showMoreLikeThis(postUri: string, feedUri: string): Promise<void>;
|
|
300
414
|
}
|
|
301
415
|
/**
|
|
302
|
-
* Response returned from {@link fetch}.
|
|
303
|
-
*
|
|
304
|
-
*
|
|
416
|
+
* Response returned from {@link fetch}. The host buffers the raw response
|
|
417
|
+
* bytes and sends them as an `ArrayBuffer`, and this class decodes them on
|
|
418
|
+
* demand depending on which accessor is called. `status`, `ok`, and `headers`
|
|
419
|
+
* (a `Map`) mirror the underlying HTTP response.
|
|
305
420
|
*/
|
|
306
421
|
export class PluginResponse {
|
|
307
422
|
/**
|
|
@@ -316,7 +431,12 @@ export class PluginResponse {
|
|
|
316
431
|
/** @type {Map<string, string>} */
|
|
317
432
|
headers: Map<string, string>;
|
|
318
433
|
/**
|
|
319
|
-
* Resolves with the response
|
|
434
|
+
* Resolves with the raw response bytes.
|
|
435
|
+
* @returns {Promise<ArrayBuffer>}
|
|
436
|
+
*/
|
|
437
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
438
|
+
/**
|
|
439
|
+
* Resolves with the response body decoded as UTF-8 text.
|
|
320
440
|
* @returns {Promise<string>}
|
|
321
441
|
*/
|
|
322
442
|
text(): Promise<string>;
|
|
@@ -1160,6 +1280,27 @@ export type KnownFollowersResponse = Record<string, unknown>;
|
|
|
1160
1280
|
* A raw repo record: `{ uri, cid, value }`.
|
|
1161
1281
|
*/
|
|
1162
1282
|
export type RepoRecord = Record<string, unknown>;
|
|
1283
|
+
/**
|
|
1284
|
+
* Hydrated thread for a post: the post plus `parent`/`replies`.
|
|
1285
|
+
*/
|
|
1286
|
+
export type PostThreadView = Record<string, unknown>;
|
|
1287
|
+
/**
|
|
1288
|
+
* An `app.bsky.graph.defs#listView` shape.
|
|
1289
|
+
*/
|
|
1290
|
+
export type ListView = Record<string, unknown>;
|
|
1291
|
+
/**
|
|
1292
|
+
* An `app.bsky.feed.defs#generatorView` shape.
|
|
1293
|
+
*/
|
|
1294
|
+
export type FeedGeneratorView = Record<string, unknown>;
|
|
1295
|
+
/**
|
|
1296
|
+
* Raw XRPC response: on failure `data` is the error body
|
|
1297
|
+
* (`{ error, message }`) when the service provided one.
|
|
1298
|
+
*/
|
|
1299
|
+
export type XrpcQueryResponse = {
|
|
1300
|
+
ok: boolean;
|
|
1301
|
+
status: number;
|
|
1302
|
+
data: unknown;
|
|
1303
|
+
};
|
|
1163
1304
|
/**
|
|
1164
1305
|
* A record that links to a queried subject.
|
|
1165
1306
|
*/
|
|
@@ -1289,10 +1430,11 @@ export type CloneableObject = {
|
|
|
1289
1430
|
*/
|
|
1290
1431
|
export type CloneableArray = Cloneable[];
|
|
1291
1432
|
/**
|
|
1292
|
-
* JSON-shaped data
|
|
1293
|
-
* host. Functions, class instances,
|
|
1433
|
+
* JSON-shaped data, plus `ArrayBuffer` for binary payloads — the only thing
|
|
1434
|
+
* that can cross between a plugin and the host. Functions, class instances,
|
|
1435
|
+
* `Date`, `Map` and friends cannot.
|
|
1294
1436
|
*/
|
|
1295
|
-
export type Cloneable = null | undefined | boolean | number | string | CloneableArray | CloneableObject;
|
|
1437
|
+
export type Cloneable = null | undefined | boolean | number | string | ArrayBuffer | CloneableArray | CloneableObject;
|
|
1296
1438
|
/**
|
|
1297
1439
|
* {@internal}
|
|
1298
1440
|
*/
|
|
@@ -1402,12 +1544,15 @@ export type HostCallMessage = {
|
|
|
1402
1544
|
args: Cloneable[];
|
|
1403
1545
|
};
|
|
1404
1546
|
/**
|
|
1405
|
-
* {@internal} The host answering a {@link hostCall}.
|
|
1547
|
+
* {@internal} The host answering a {@link hostCall}. Wider than
|
|
1548
|
+
* {@link Cloneable} because {@link SerializedFetchResponse} carries its
|
|
1549
|
+
* body as an `ArrayBuffer`, which structured clone handles but JSON
|
|
1550
|
+
* cannot.
|
|
1406
1551
|
*/
|
|
1407
1552
|
export type HostResultMessage = {
|
|
1408
1553
|
type: "hostResult";
|
|
1409
1554
|
hostCallId: number;
|
|
1410
|
-
value?: Cloneable;
|
|
1555
|
+
value?: Cloneable | SerializedFetchResponse;
|
|
1411
1556
|
error?: string;
|
|
1412
1557
|
};
|
|
1413
1558
|
/**
|
|
@@ -1425,13 +1570,14 @@ export type HostEventMessage = {
|
|
|
1425
1570
|
*/
|
|
1426
1571
|
export type HostMessage = HostCallMessage | HostResultMessage | HostEventMessage;
|
|
1427
1572
|
/**
|
|
1428
|
-
* {@internal} The host's reply to a proxied {@link fetch}.
|
|
1573
|
+
* {@internal} The host's reply to a proxied {@link fetch}. `body` is
|
|
1574
|
+
* always the raw response bytes (see {@link PluginResponse}).
|
|
1429
1575
|
*/
|
|
1430
1576
|
export type SerializedFetchResponse = {
|
|
1431
1577
|
status: number;
|
|
1432
1578
|
ok: boolean;
|
|
1433
1579
|
headers: Record<string, string>;
|
|
1434
|
-
body:
|
|
1580
|
+
body: ArrayBuffer;
|
|
1435
1581
|
};
|
|
1436
1582
|
/**
|
|
1437
1583
|
* {@internal} A {@link PluginFetchInit} with its headers flattened for transfer.
|
package/main.js
CHANGED
|
@@ -9,6 +9,15 @@
|
|
|
9
9
|
* Paginated response from `getKnownFollowers`: `{ followers, cursor }`.
|
|
10
10
|
* @typedef {Record<string, unknown>} RepoRecord
|
|
11
11
|
* A raw repo record: `{ uri, cid, value }`.
|
|
12
|
+
* @typedef {Record<string, unknown>} PostThreadView
|
|
13
|
+
* Hydrated thread for a post: the post plus `parent`/`replies`.
|
|
14
|
+
* @typedef {Record<string, unknown>} ListView
|
|
15
|
+
* An `app.bsky.graph.defs#listView` shape.
|
|
16
|
+
* @typedef {Record<string, unknown>} FeedGeneratorView
|
|
17
|
+
* An `app.bsky.feed.defs#generatorView` shape.
|
|
18
|
+
* @typedef {{ ok: boolean, status: number, data: unknown }} XrpcQueryResponse
|
|
19
|
+
* Raw XRPC response: on failure `data` is the error body
|
|
20
|
+
* (`{ error, message }`) when the service provided one.
|
|
12
21
|
* @typedef {{ did: string, collection: string, rkey: string }} BacklinkRecord
|
|
13
22
|
* A record that links to a queried subject.
|
|
14
23
|
* @typedef {Record<string, unknown>} FeedItem
|
|
@@ -357,6 +366,123 @@ export class PluginData {
|
|
|
357
366
|
hostCall("getBacklinks", { subject, source, limit })
|
|
358
367
|
);
|
|
359
368
|
}
|
|
369
|
+
/**
|
|
370
|
+
* Fetch the hydrated thread around a post (the post, its parents, and
|
|
371
|
+
* replies), as the host renders it.
|
|
372
|
+
* @param {string} uri
|
|
373
|
+
* @returns {Promise<PostThreadView | null>}
|
|
374
|
+
*/
|
|
375
|
+
getPostThread(uri) {
|
|
376
|
+
return /** @type {Promise<PostThreadView | null>} */ (
|
|
377
|
+
hostCall("getPostThread", { uri })
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Fetch a list's metadata view by AT-URI.
|
|
382
|
+
* @param {string} uri
|
|
383
|
+
* @returns {Promise<ListView | null>}
|
|
384
|
+
*/
|
|
385
|
+
getList(uri) {
|
|
386
|
+
return /** @type {Promise<ListView | null>} */ (hostCall("getList", { uri }));
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Fetch a feed generator view by AT-URI.
|
|
390
|
+
* @param {string} uri
|
|
391
|
+
* @returns {Promise<FeedGeneratorView | null>}
|
|
392
|
+
*/
|
|
393
|
+
getFeedGenerator(uri) {
|
|
394
|
+
return /** @type {Promise<FeedGeneratorView | null>} */ (
|
|
395
|
+
hostCall("getFeedGenerator", { uri })
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* The current user's full hydrated profile (unlike `app.currentUser`,
|
|
400
|
+
* which carries only `did` and `handle`). `null` when signed out.
|
|
401
|
+
* @returns {Promise<DetailedProfileView | null>}
|
|
402
|
+
*/
|
|
403
|
+
getCurrentUserProfile() {
|
|
404
|
+
return /** @type {Promise<DetailedProfileView | null>} */ (
|
|
405
|
+
hostCall("getCurrentUserProfile", {})
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Call a read-only AppView query endpoint through the current user's
|
|
410
|
+
* session and get the raw XRPC response back.
|
|
411
|
+
*
|
|
412
|
+
* Only an allowlisted set of `app.bsky.*` query NSIDs is accepted;
|
|
413
|
+
* viewer-private queries (mutes, bookmarks, notifications, preferences,
|
|
414
|
+
* timeline, ...) additionally require the `"privateData"` action
|
|
415
|
+
* permission. Unlike the curated methods above, responses are canonical
|
|
416
|
+
* server state — they may briefly disagree with what the host UI shows
|
|
417
|
+
* while an optimistic update is in flight, and results are not cached.
|
|
418
|
+
*
|
|
419
|
+
* @param {string} nsid e.g. `"app.bsky.feed.getQuotes"`
|
|
420
|
+
* @param {Record<string, string | number | boolean | string[]>} [params]
|
|
421
|
+
* @returns {Promise<XrpcQueryResponse>}
|
|
422
|
+
*/
|
|
423
|
+
xrpcQuery(nsid, params = {}) {
|
|
424
|
+
return /** @type {Promise<XrpcQueryResponse>} */ (
|
|
425
|
+
hostCall("xrpcQuery", { nsid, params })
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Host-mediated persistent storage for binary data too large for
|
|
432
|
+
* {@link Plugin.loadLocalData}/{@link Plugin.saveLocalData}.
|
|
433
|
+
* Namespaced per plugin; survives reloads but is cleared on
|
|
434
|
+
* uninstall.
|
|
435
|
+
*/
|
|
436
|
+
export class BinaryCache {
|
|
437
|
+
/**
|
|
438
|
+
* @param {string} key
|
|
439
|
+
* @returns {Promise<ArrayBuffer | null>}
|
|
440
|
+
*/
|
|
441
|
+
async get(key) {
|
|
442
|
+
return /** @type {Promise<ArrayBuffer | null>} */ (
|
|
443
|
+
hostCall("getBinaryCacheEntry", { key: String(key) })
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Whether an entry is stored under `key`, without transferring its bytes.
|
|
448
|
+
*
|
|
449
|
+
* @param {string} key
|
|
450
|
+
* @returns {Promise<boolean>}
|
|
451
|
+
*/
|
|
452
|
+
async has(key) {
|
|
453
|
+
return /** @type {boolean} */ (
|
|
454
|
+
await hostCall("hasBinaryCacheEntry", { key: String(key) })
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Every key this plugin currently has stored, in no particular order.
|
|
459
|
+
*
|
|
460
|
+
* @returns {Promise<string[]>}
|
|
461
|
+
*/
|
|
462
|
+
async keys() {
|
|
463
|
+
return /** @type {string[]} */ (await hostCall("listBinaryCacheEntries"));
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* @param {string} key
|
|
467
|
+
* @param {ArrayBuffer | ArrayBufferView} data
|
|
468
|
+
* @returns {Promise<void>}
|
|
469
|
+
*/
|
|
470
|
+
async put(key, data) {
|
|
471
|
+
// A view may be a window onto a larger buffer, so send just its bytes.
|
|
472
|
+
const buffer = /** @type {ArrayBuffer} */ (
|
|
473
|
+
data instanceof ArrayBuffer
|
|
474
|
+
? data
|
|
475
|
+
: data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength)
|
|
476
|
+
);
|
|
477
|
+
await hostCall("putBinaryCacheEntry", { key: String(key), data: buffer });
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* @param {string} key
|
|
481
|
+
* @returns {Promise<void>}
|
|
482
|
+
*/
|
|
483
|
+
async delete(key) {
|
|
484
|
+
await hostCall("deleteBinaryCacheEntry", { key: String(key) });
|
|
485
|
+
}
|
|
360
486
|
}
|
|
361
487
|
|
|
362
488
|
/**
|
|
@@ -375,6 +501,8 @@ export class App {
|
|
|
375
501
|
this.currentUser = null;
|
|
376
502
|
/** Read-only appview accessors — see {@link PluginData}. */
|
|
377
503
|
this.data = new PluginData();
|
|
504
|
+
/** Host-mediated binary storage — see {@link BinaryCache}. */
|
|
505
|
+
this.binaryCache = new BinaryCache();
|
|
378
506
|
}
|
|
379
507
|
/**
|
|
380
508
|
* Register an event listener. Supported events:
|
|
@@ -479,8 +607,9 @@ export class App {
|
|
|
479
607
|
}
|
|
480
608
|
|
|
481
609
|
/**
|
|
482
|
-
* Proxied fetch through the host.
|
|
483
|
-
*
|
|
610
|
+
* Proxied fetch through the host. The target URL must match one of the
|
|
611
|
+
* `permissions.fetch` URL patterns declared in the plugin's manifest, which
|
|
612
|
+
* the user grants at install.
|
|
484
613
|
* `init` accepts `method`, `headers` (plain object, `Headers`, `Map`, or
|
|
485
614
|
* `[name, value]` iterable), and a string `body`. Resolves to a
|
|
486
615
|
* {@link PluginResponse}.
|
|
@@ -495,6 +624,40 @@ export async function fetch(url, init = {}) {
|
|
|
495
624
|
return new PluginResponse(result);
|
|
496
625
|
}
|
|
497
626
|
|
|
627
|
+
/**
|
|
628
|
+
* Asks the user to grant this plugin network access to `url`'s origin — for
|
|
629
|
+
* endpoints the plugin can't know in advance, such as a user-supplied API
|
|
630
|
+
* provider. Requires `permissions.userFetch` in the manifest.
|
|
631
|
+
*
|
|
632
|
+
* Grants are origin-scoped (scheme, host, and port; the path is ignored) and
|
|
633
|
+
* persist until the user revokes them in the app's plugin settings. Resolves
|
|
634
|
+
* `true` without prompting if the origin is already permitted, so it is safe
|
|
635
|
+
* to call before every request. Resolves `false` if the user declines, if
|
|
636
|
+
* `url` can't be an origin, or if a prompt for this plugin is already open.
|
|
637
|
+
*
|
|
638
|
+
* Any credential for the endpoint is the plugin's to hold — use
|
|
639
|
+
* {@link Plugin.saveLocalData} rather than {@link Plugin.saveData}, which
|
|
640
|
+
* syncs through the user's account preferences.
|
|
641
|
+
* @param {string} url
|
|
642
|
+
* @returns {Promise<boolean>}
|
|
643
|
+
*/
|
|
644
|
+
export async function requestFetchPermission(url) {
|
|
645
|
+
return /** @type {boolean} */ (
|
|
646
|
+
await hostCall("requestFetchPermission", { url })
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* The origins the user has granted this plugin, as fetch patterns
|
|
652
|
+
* (`https://example.com/*`). Manifest-declared permissions are not included.
|
|
653
|
+
* @returns {Promise<string[]>}
|
|
654
|
+
*/
|
|
655
|
+
export async function getUserGrantedFetchOrigins() {
|
|
656
|
+
return /** @type {string[]} */ (
|
|
657
|
+
await hostCall("getUserGrantedFetchOrigins", {})
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
|
|
498
661
|
/**
|
|
499
662
|
* @param {unknown} value
|
|
500
663
|
* @returns {value is { forEach: (callback: (value: string, name: string) => void) => void }}
|
|
@@ -553,11 +716,13 @@ function serializeFetchInit(init) {
|
|
|
553
716
|
}
|
|
554
717
|
|
|
555
718
|
/**
|
|
556
|
-
* Response returned from {@link fetch}.
|
|
557
|
-
*
|
|
558
|
-
*
|
|
719
|
+
* Response returned from {@link fetch}. The host buffers the raw response
|
|
720
|
+
* bytes and sends them as an `ArrayBuffer`, and this class decodes them on
|
|
721
|
+
* demand depending on which accessor is called. `status`, `ok`, and `headers`
|
|
722
|
+
* (a `Map`) mirror the underlying HTTP response.
|
|
559
723
|
*/
|
|
560
724
|
export class PluginResponse {
|
|
725
|
+
/** @type {ArrayBuffer} raw response bytes */
|
|
561
726
|
#body;
|
|
562
727
|
/**
|
|
563
728
|
* @internal
|
|
@@ -573,18 +738,25 @@ export class PluginResponse {
|
|
|
573
738
|
this.#body = body;
|
|
574
739
|
}
|
|
575
740
|
/**
|
|
576
|
-
* Resolves with the response
|
|
741
|
+
* Resolves with the raw response bytes.
|
|
742
|
+
* @returns {Promise<ArrayBuffer>}
|
|
743
|
+
*/
|
|
744
|
+
async arrayBuffer() {
|
|
745
|
+
return this.#body;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Resolves with the response body decoded as UTF-8 text.
|
|
577
749
|
* @returns {Promise<string>}
|
|
578
750
|
*/
|
|
579
751
|
async text() {
|
|
580
|
-
return this
|
|
752
|
+
return new TextDecoder().decode(await this.arrayBuffer());
|
|
581
753
|
}
|
|
582
754
|
/**
|
|
583
755
|
* Resolves with the response body parsed as JSON.
|
|
584
756
|
* @returns {Promise<unknown>}
|
|
585
757
|
*/
|
|
586
758
|
async json() {
|
|
587
|
-
return JSON.parse(this
|
|
759
|
+
return JSON.parse(await this.text());
|
|
588
760
|
}
|
|
589
761
|
}
|
|
590
762
|
|
|
@@ -1982,9 +2154,10 @@ export class VirtualEl {
|
|
|
1982
2154
|
* An object whose values are all {@link Cloneable}.
|
|
1983
2155
|
* @typedef {Cloneable[]} CloneableArray
|
|
1984
2156
|
* An array of {@link Cloneable} values.
|
|
1985
|
-
* @typedef {null | undefined | boolean | number | string | CloneableArray | CloneableObject} Cloneable
|
|
1986
|
-
* JSON-shaped data
|
|
1987
|
-
* host. Functions, class instances,
|
|
2157
|
+
* @typedef {null | undefined | boolean | number | string | ArrayBuffer | CloneableArray | CloneableObject} Cloneable
|
|
2158
|
+
* JSON-shaped data, plus `ArrayBuffer` for binary payloads — the only thing
|
|
2159
|
+
* that can cross between a plugin and the host. Functions, class instances,
|
|
2160
|
+
* `Date`, `Map` and friends cannot.
|
|
1988
2161
|
* @typedef {{ type: "register", target: "eventListener", event: string, handlerId: number }} RegisterEventListenerMessage
|
|
1989
2162
|
* {@internal}
|
|
1990
2163
|
* @typedef {{ type: "register", target: "sidebarItem", icon: string | SerializedElement, title: string, handlerId: number }} RegisterSidebarItemMessage
|
|
@@ -2011,14 +2184,18 @@ export class VirtualEl {
|
|
|
2011
2184
|
* {@internal} Anything this worker may post to the host.
|
|
2012
2185
|
* @typedef {{ type: "call", callId: number, handlerId: number, args: Cloneable[] }} HostCallMessage
|
|
2013
2186
|
* {@internal} The host invoking a handler this worker registered.
|
|
2014
|
-
* @typedef {{ type: "hostResult", hostCallId: number, value?: Cloneable, error?: string }} HostResultMessage
|
|
2015
|
-
* {@internal} The host answering a {@link hostCall}.
|
|
2187
|
+
* @typedef {{ type: "hostResult", hostCallId: number, value?: Cloneable | SerializedFetchResponse, error?: string }} HostResultMessage
|
|
2188
|
+
* {@internal} The host answering a {@link hostCall}. Wider than
|
|
2189
|
+
* {@link Cloneable} because {@link SerializedFetchResponse} carries its
|
|
2190
|
+
* body as an `ArrayBuffer`, which structured clone handles but JSON
|
|
2191
|
+
* cannot.
|
|
2016
2192
|
* @typedef {{ type: "event", event: "modalDismissed", data: { modalId: number } }} HostEventMessage
|
|
2017
2193
|
* {@internal} An out-of-band notification from the host.
|
|
2018
2194
|
* @typedef {HostCallMessage | HostResultMessage | HostEventMessage} HostMessage
|
|
2019
2195
|
* {@internal} Anything the host may post to this worker.
|
|
2020
|
-
* @typedef {{ status: number, ok: boolean, headers: Record<string, string>, body:
|
|
2021
|
-
* {@internal} The host's reply to a proxied {@link fetch}.
|
|
2196
|
+
* @typedef {{ status: number, ok: boolean, headers: Record<string, string>, body: ArrayBuffer }} SerializedFetchResponse
|
|
2197
|
+
* {@internal} The host's reply to a proxied {@link fetch}. `body` is
|
|
2198
|
+
* always the raw response bytes (see {@link PluginResponse}).
|
|
2022
2199
|
* @typedef {{ method?: string, headers?: Record<string, string>, body?: string }} SerializedFetchInit
|
|
2023
2200
|
* {@internal} A {@link PluginFetchInit} with its headers flattened for transfer.
|
|
2024
2201
|
* @typedef {Record<string, unknown>} SerializedRichTextToken
|