@impro.social/impro-plugin 0.0.22 → 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.
Files changed (3) hide show
  1. package/main.d.ts +187 -13
  2. package/main.js +212 -15
  3. package/package.json +1 -1
package/main.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
- * Proxied fetch through the host. Requires the `"networkRequest"` permission
3
- * scope and the target URL must be covered by the plugin's manifest allowlist.
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,17 @@ 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.
63
+ * @typedef {{ did: string, collection: string, rkey: string }} BacklinkRecord
64
+ * A record that links to a queried subject.
29
65
  * @typedef {Record<string, unknown>} FeedItem
30
66
  * A `app.bsky.feed.defs#feedViewPost` (post + reply/repost context).
31
67
  * @typedef {{ $type: string } & Record<string, unknown>} RichTextFacetFeature
@@ -194,6 +230,102 @@ export class PluginData {
194
230
  * @returns {Promise<RepoRecord>}
195
231
  */
196
232
  getRecord(repo: string, collection: string, rkey: string): Promise<RepoRecord>;
233
+ /**
234
+ * Get records that link to `subject`, from a backlink
235
+ * index of public records.
236
+ *
237
+ * `subject` is an AT-URI or a DID; `source` names the linking field as
238
+ * `<collection>:<dot.path.to.field>` (e.g.
239
+ * `"app.bsky.graph.listitem:list"`). The host paginates for you, up to
240
+ * `limit` records (max 1000 per call — page by making further calls
241
+ * with a narrower subject).
242
+ *
243
+ * @param {{ subject: string, source: string, limit?: number }} params
244
+ * @returns {Promise<BacklinkRecord[]>}
245
+ */
246
+ getBacklinks({ subject, source, limit }: {
247
+ subject: string;
248
+ source: string;
249
+ limit?: number;
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>;
197
329
  }
198
330
  /**
199
331
  * The plugin's handle to the running impro app. Exposed as `this.app` on a
@@ -209,6 +341,8 @@ export class App {
209
341
  currentUser: ProfileView | null;
210
342
  /** Read-only appview accessors — see {@link PluginData}. */
211
343
  data: PluginData;
344
+ /** Host-mediated binary storage — see {@link BinaryCache}. */
345
+ binaryCache: BinaryCache;
212
346
  /**
213
347
  * Register an event listener. Supported events:
214
348
  *
@@ -279,9 +413,10 @@ export class App {
279
413
  showMoreLikeThis(postUri: string, feedUri: string): Promise<void>;
280
414
  }
281
415
  /**
282
- * Response returned from {@link fetch}. Body is buffered by the host and
283
- * exposed as text or parsed JSON. `status`, `ok`, and `headers` (a `Map`)
284
- * mirror the underlying HTTP response.
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.
285
420
  */
286
421
  export class PluginResponse {
287
422
  /**
@@ -296,7 +431,12 @@ export class PluginResponse {
296
431
  /** @type {Map<string, string>} */
297
432
  headers: Map<string, string>;
298
433
  /**
299
- * Resolves with the response body as a string.
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.
300
440
  * @returns {Promise<string>}
301
441
  */
302
442
  text(): Promise<string>;
@@ -1140,6 +1280,35 @@ export type KnownFollowersResponse = Record<string, unknown>;
1140
1280
  * A raw repo record: `{ uri, cid, value }`.
1141
1281
  */
1142
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
+ };
1304
+ /**
1305
+ * A record that links to a queried subject.
1306
+ */
1307
+ export type BacklinkRecord = {
1308
+ did: string;
1309
+ collection: string;
1310
+ rkey: string;
1311
+ };
1143
1312
  /**
1144
1313
  * A `app.bsky.feed.defs#feedViewPost` (post + reply/repost context).
1145
1314
  */
@@ -1261,10 +1430,11 @@ export type CloneableObject = {
1261
1430
  */
1262
1431
  export type CloneableArray = Cloneable[];
1263
1432
  /**
1264
- * JSON-shaped data — the only thing that can cross between a plugin and the
1265
- * host. Functions, class instances, `Date`, `Map` and friends cannot.
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.
1266
1436
  */
1267
- export type Cloneable = null | undefined | boolean | number | string | CloneableArray | CloneableObject;
1437
+ export type Cloneable = null | undefined | boolean | number | string | ArrayBuffer | CloneableArray | CloneableObject;
1268
1438
  /**
1269
1439
  * {@internal}
1270
1440
  */
@@ -1374,12 +1544,15 @@ export type HostCallMessage = {
1374
1544
  args: Cloneable[];
1375
1545
  };
1376
1546
  /**
1377
- * {@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.
1378
1551
  */
1379
1552
  export type HostResultMessage = {
1380
1553
  type: "hostResult";
1381
1554
  hostCallId: number;
1382
- value?: Cloneable;
1555
+ value?: Cloneable | SerializedFetchResponse;
1383
1556
  error?: string;
1384
1557
  };
1385
1558
  /**
@@ -1397,13 +1570,14 @@ export type HostEventMessage = {
1397
1570
  */
1398
1571
  export type HostMessage = HostCallMessage | HostResultMessage | HostEventMessage;
1399
1572
  /**
1400
- * {@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}).
1401
1575
  */
1402
1576
  export type SerializedFetchResponse = {
1403
1577
  status: number;
1404
1578
  ok: boolean;
1405
1579
  headers: Record<string, string>;
1406
- body: string;
1580
+ body: ArrayBuffer;
1407
1581
  };
1408
1582
  /**
1409
1583
  * {@internal} A {@link PluginFetchInit} with its headers flattened for transfer.
package/main.js CHANGED
@@ -9,6 +9,17 @@
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.
21
+ * @typedef {{ did: string, collection: string, rkey: string }} BacklinkRecord
22
+ * A record that links to a queried subject.
12
23
  * @typedef {Record<string, unknown>} FeedItem
13
24
  * A `app.bsky.feed.defs#feedViewPost` (post + reply/repost context).
14
25
  * @typedef {{ $type: string } & Record<string, unknown>} RichTextFacetFeature
@@ -337,6 +348,141 @@ export class PluginData {
337
348
  hostCall("getRecord", { repo, collection, rkey })
338
349
  );
339
350
  }
351
+ /**
352
+ * Get records that link to `subject`, from a backlink
353
+ * index of public records.
354
+ *
355
+ * `subject` is an AT-URI or a DID; `source` names the linking field as
356
+ * `<collection>:<dot.path.to.field>` (e.g.
357
+ * `"app.bsky.graph.listitem:list"`). The host paginates for you, up to
358
+ * `limit` records (max 1000 per call — page by making further calls
359
+ * with a narrower subject).
360
+ *
361
+ * @param {{ subject: string, source: string, limit?: number }} params
362
+ * @returns {Promise<BacklinkRecord[]>}
363
+ */
364
+ getBacklinks({ subject, source, limit = 100 }) {
365
+ return /** @type {Promise<BacklinkRecord[]>} */ (
366
+ hostCall("getBacklinks", { subject, source, limit })
367
+ );
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
+ }
340
486
  }
341
487
 
342
488
  /**
@@ -355,6 +501,8 @@ export class App {
355
501
  this.currentUser = null;
356
502
  /** Read-only appview accessors — see {@link PluginData}. */
357
503
  this.data = new PluginData();
504
+ /** Host-mediated binary storage — see {@link BinaryCache}. */
505
+ this.binaryCache = new BinaryCache();
358
506
  }
359
507
  /**
360
508
  * Register an event listener. Supported events:
@@ -459,8 +607,9 @@ export class App {
459
607
  }
460
608
 
461
609
  /**
462
- * Proxied fetch through the host. Requires the `"networkRequest"` permission
463
- * scope and the target URL must be covered by the plugin's manifest allowlist.
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.
464
613
  * `init` accepts `method`, `headers` (plain object, `Headers`, `Map`, or
465
614
  * `[name, value]` iterable), and a string `body`. Resolves to a
466
615
  * {@link PluginResponse}.
@@ -475,6 +624,40 @@ export async function fetch(url, init = {}) {
475
624
  return new PluginResponse(result);
476
625
  }
477
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
+
478
661
  /**
479
662
  * @param {unknown} value
480
663
  * @returns {value is { forEach: (callback: (value: string, name: string) => void) => void }}
@@ -533,11 +716,13 @@ function serializeFetchInit(init) {
533
716
  }
534
717
 
535
718
  /**
536
- * Response returned from {@link fetch}. Body is buffered by the host and
537
- * exposed as text or parsed JSON. `status`, `ok`, and `headers` (a `Map`)
538
- * mirror the underlying HTTP response.
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.
539
723
  */
540
724
  export class PluginResponse {
725
+ /** @type {ArrayBuffer} raw response bytes */
541
726
  #body;
542
727
  /**
543
728
  * @internal
@@ -553,18 +738,25 @@ export class PluginResponse {
553
738
  this.#body = body;
554
739
  }
555
740
  /**
556
- * Resolves with the response body as a string.
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.
557
749
  * @returns {Promise<string>}
558
750
  */
559
751
  async text() {
560
- return this.#body;
752
+ return new TextDecoder().decode(await this.arrayBuffer());
561
753
  }
562
754
  /**
563
755
  * Resolves with the response body parsed as JSON.
564
756
  * @returns {Promise<unknown>}
565
757
  */
566
758
  async json() {
567
- return JSON.parse(this.#body);
759
+ return JSON.parse(await this.text());
568
760
  }
569
761
  }
570
762
 
@@ -1962,9 +2154,10 @@ export class VirtualEl {
1962
2154
  * An object whose values are all {@link Cloneable}.
1963
2155
  * @typedef {Cloneable[]} CloneableArray
1964
2156
  * An array of {@link Cloneable} values.
1965
- * @typedef {null | undefined | boolean | number | string | CloneableArray | CloneableObject} Cloneable
1966
- * JSON-shaped data — the only thing that can cross between a plugin and the
1967
- * host. Functions, class instances, `Date`, `Map` and friends cannot.
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.
1968
2161
  * @typedef {{ type: "register", target: "eventListener", event: string, handlerId: number }} RegisterEventListenerMessage
1969
2162
  * {@internal}
1970
2163
  * @typedef {{ type: "register", target: "sidebarItem", icon: string | SerializedElement, title: string, handlerId: number }} RegisterSidebarItemMessage
@@ -1991,14 +2184,18 @@ export class VirtualEl {
1991
2184
  * {@internal} Anything this worker may post to the host.
1992
2185
  * @typedef {{ type: "call", callId: number, handlerId: number, args: Cloneable[] }} HostCallMessage
1993
2186
  * {@internal} The host invoking a handler this worker registered.
1994
- * @typedef {{ type: "hostResult", hostCallId: number, value?: Cloneable, error?: string }} HostResultMessage
1995
- * {@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.
1996
2192
  * @typedef {{ type: "event", event: "modalDismissed", data: { modalId: number } }} HostEventMessage
1997
2193
  * {@internal} An out-of-band notification from the host.
1998
2194
  * @typedef {HostCallMessage | HostResultMessage | HostEventMessage} HostMessage
1999
2195
  * {@internal} Anything the host may post to this worker.
2000
- * @typedef {{ status: number, ok: boolean, headers: Record<string, string>, body: string }} SerializedFetchResponse
2001
- * {@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}).
2002
2199
  * @typedef {{ method?: string, headers?: Record<string, string>, body?: string }} SerializedFetchInit
2003
2200
  * {@internal} A {@link PluginFetchInit} with its headers flattened for transfer.
2004
2201
  * @typedef {Record<string, unknown>} SerializedRichTextToken
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@impro.social/impro-plugin",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "type": "module",
5
5
  "main": "main.js",
6
6
  "types": "./main.d.ts",