@kiosko/sdk 0.2.11 → 0.2.12

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/dist/http.d.ts CHANGED
@@ -31,6 +31,12 @@ export interface HttpRequest {
31
31
  body?: string;
32
32
  /** Don't use or keep the conditional-request cache for this call. */
33
33
  noCache?: boolean;
34
+ /**
35
+ * The largest body to read, in bytes: a longer one (by its Content-Length,
36
+ * or as it arrives) throws an `HttpError` and is not kept. For URLs the
37
+ * user types, which may answer with anything. Kiosko 0.2.12 and later.
38
+ */
39
+ maxBytes?: number;
34
40
  }
35
41
  export interface HttpResponse<T> {
36
42
  data: T;
package/dist/http.js CHANGED
@@ -51,6 +51,38 @@ const hostOf = (url) => {
51
51
  return url;
52
52
  }
53
53
  };
54
+ /** The body of `res` as text, or an `HttpError` as soon as it passes `max` bytes (without reading the rest). */
55
+ async function limitedText(res, max, url) {
56
+ const tooBig = () => new HttpError(`response larger than ${max} bytes from ${hostOf(url)}`, { status: res.status, url });
57
+ const declared = Number(res.headers.get('content-length'));
58
+ if (Number.isFinite(declared) && declared > max) {
59
+ await res.body?.cancel().catch(() => undefined);
60
+ throw tooBig();
61
+ }
62
+ if (!res.body)
63
+ return '';
64
+ const reader = res.body.getReader();
65
+ const chunks = [];
66
+ let size = 0;
67
+ for (;;) {
68
+ const { done, value } = await reader.read();
69
+ if (done)
70
+ break;
71
+ size += value.byteLength;
72
+ if (size > max) {
73
+ await reader.cancel().catch(() => undefined);
74
+ throw tooBig();
75
+ }
76
+ chunks.push(value);
77
+ }
78
+ const all = new Uint8Array(size);
79
+ let at = 0;
80
+ for (const c of chunks) {
81
+ all.set(c, at);
82
+ at += c.byteLength;
83
+ }
84
+ return new TextDecoder().decode(all);
85
+ }
54
86
  export function createHttp(o = {}) {
55
87
  const doFetch = o.fetch ?? fetch;
56
88
  const now = o.now ?? Date.now;
@@ -85,7 +117,7 @@ export function createHttp(o = {}) {
85
117
  }
86
118
  if (!res.ok)
87
119
  throw new HttpError(`HTTP ${res.status} from ${host}`, { status: res.status, url });
88
- const body = await res.text();
120
+ const body = req.maxBytes === undefined ? await res.text() : await limitedText(res, req.maxBytes, url);
89
121
  if (cacheable) {
90
122
  const etag = res.headers.get('etag') ?? undefined;
91
123
  const modified = res.headers.get('last-modified') ?? undefined;
package/dist/plugin.d.ts CHANGED
@@ -427,6 +427,13 @@ export interface SourceContext<O> extends BaseContext {
427
427
  intervalMs: number;
428
428
  /** How long one `read` may take (the user's `timeoutMs`), for a per-request budget of your own. */
429
429
  timeoutMs: number;
430
+ /**
431
+ * The source profile being read (see `SourceDefinition.profiles`): its id,
432
+ * or undefined for the default one. Each profile is read on its own, with
433
+ * its own options; use it to keep files of each apart in `cacheDir`.
434
+ * Kiosko 0.2.12 and later.
435
+ */
436
+ profile?: string;
430
437
  }
431
438
  export interface SourceDefinition<OF extends ObjectField<any> = ObjectField<any>, D = unknown> {
432
439
  /** Unique inside its plugin. Widgets ask for `plugin/source`. */
@@ -486,6 +493,20 @@ export interface SourceDefinition<OF extends ObjectField<any> = ObjectField<any>
486
493
  locale: string;
487
494
  t: (text: Localized) => string;
488
495
  }) => Choice[]>;
496
+ /**
497
+ * The user may keep several named configurations of it, **profiles**
498
+ * (news «General» and «Technology», two GitHub accounts), each with its
499
+ * own options and read on its own, as if it were another source. A widget
500
+ * picks one with its `profile` (Kiosko adds the picker to the widgets that
501
+ * read the source; the plugin adds nothing) and finds that profile's data
502
+ * in `ctx.sources["plugin/source"]` as usual. Without profiles in the
503
+ * config, nothing changes: the source's options are
504
+ * `sources["plugin/source"]`, the default profile. Off unless declared:
505
+ * a source that reads hardware or the local machine (a battery, a
506
+ * Bluetooth sensor) has one thing to read. Kiosko 0.2.12 and later; older
507
+ * ones ignore it (one configuration).
508
+ */
509
+ profiles?: boolean;
489
510
  }
490
511
  /** One value the editor offers for an option (see `SourceDefinition.choices`). */
491
512
  export interface Choice {
@@ -571,7 +592,7 @@ export interface InterludeDefinition<OF extends ObjectField<any> = ObjectField<a
571
592
  * package.json, `"kiosko": { "sdk": "^0.2.0" }`, so it can be refused before
572
593
  * it runs); the registry compares them with `satisfies`.
573
594
  */
574
- export declare const SDK_VERSION = "0.2.11";
595
+ export declare const SDK_VERSION = "0.2.12";
575
596
  /**
576
597
  * What a plugin does on the machine, declared for the person who installs
577
598
  * it: the Plugins page and the plugin store show it next to the plugin. It
package/dist/plugin.js CHANGED
@@ -18,7 +18,7 @@ export const PAD_KEYS = ['up', 'down', 'left', 'right', 'a', 'b', 'x', 'y', 'l',
18
18
  * package.json, `"kiosko": { "sdk": "^0.2.0" }`, so it can be refused before
19
19
  * it runs); the registry compares them with `satisfies`.
20
20
  */
21
- export const SDK_VERSION = '0.2.11';
21
+ export const SDK_VERSION = '0.2.12';
22
22
  /* Identity helpers: they only exist so TypeScript infers option and data types. */
23
23
  export function defineWidget(w) {
24
24
  return w;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiosko/sdk",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "Plugin SDK for Kiosko: define widgets, data sources and interludes for small always-on LCD panels.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/http.ts CHANGED
@@ -35,6 +35,12 @@ export interface HttpRequest {
35
35
  body?: string;
36
36
  /** Don't use or keep the conditional-request cache for this call. */
37
37
  noCache?: boolean;
38
+ /**
39
+ * The largest body to read, in bytes: a longer one (by its Content-Length,
40
+ * or as it arrives) throws an `HttpError` and is not kept. For URLs the
41
+ * user types, which may answer with anything. Kiosko 0.2.12 and later.
42
+ */
43
+ maxBytes?: number;
38
44
  }
39
45
 
40
46
  export interface HttpResponse<T> {
@@ -74,6 +80,37 @@ const hostOf = (url: string) => {
74
80
  }
75
81
  };
76
82
 
83
+ /** The body of `res` as text, or an `HttpError` as soon as it passes `max` bytes (without reading the rest). */
84
+ async function limitedText(res: Response, max: number, url: string): Promise<string> {
85
+ const tooBig = () => new HttpError(`response larger than ${max} bytes from ${hostOf(url)}`, { status: res.status, url });
86
+ const declared = Number(res.headers.get('content-length'));
87
+ if (Number.isFinite(declared) && declared > max) {
88
+ await res.body?.cancel().catch(() => undefined);
89
+ throw tooBig();
90
+ }
91
+ if (!res.body) return '';
92
+ const reader = res.body.getReader();
93
+ const chunks: Uint8Array[] = [];
94
+ let size = 0;
95
+ for (;;) {
96
+ const { done, value } = await reader.read();
97
+ if (done) break;
98
+ size += value.byteLength;
99
+ if (size > max) {
100
+ await reader.cancel().catch(() => undefined);
101
+ throw tooBig();
102
+ }
103
+ chunks.push(value);
104
+ }
105
+ const all = new Uint8Array(size);
106
+ let at = 0;
107
+ for (const c of chunks) {
108
+ all.set(c, at);
109
+ at += c.byteLength;
110
+ }
111
+ return new TextDecoder().decode(all);
112
+ }
113
+
77
114
  export function createHttp(o: { agent?: string; fetch?: typeof fetch; now?: () => number; maxCached?: number } = {}): HttpClient {
78
115
  const doFetch = o.fetch ?? fetch;
79
116
  const now = o.now ?? Date.now;
@@ -104,7 +141,7 @@ export function createHttp(o: { agent?: string; fetch?: typeof fetch; now?: () =
104
141
  throw new HttpError(`HTTP ${res.status} from ${host}: rate-limited until ${new Date(at).toISOString()}`, { status: res.status, url, retryAt: at });
105
142
  }
106
143
  if (!res.ok) throw new HttpError(`HTTP ${res.status} from ${host}`, { status: res.status, url });
107
- const body = await res.text();
144
+ const body = req.maxBytes === undefined ? await res.text() : await limitedText(res, req.maxBytes, url);
108
145
  if (cacheable) {
109
146
  const etag = res.headers.get('etag') ?? undefined;
110
147
  const modified = res.headers.get('last-modified') ?? undefined;
package/src/plugin.ts CHANGED
@@ -426,6 +426,13 @@ export interface SourceContext<O> extends BaseContext {
426
426
  intervalMs: number;
427
427
  /** How long one `read` may take (the user's `timeoutMs`), for a per-request budget of your own. */
428
428
  timeoutMs: number;
429
+ /**
430
+ * The source profile being read (see `SourceDefinition.profiles`): its id,
431
+ * or undefined for the default one. Each profile is read on its own, with
432
+ * its own options; use it to keep files of each apart in `cacheDir`.
433
+ * Kiosko 0.2.12 and later.
434
+ */
435
+ profile?: string;
429
436
  }
430
437
 
431
438
  export interface SourceDefinition<OF extends ObjectField<any> = ObjectField<any>, D = unknown> {
@@ -480,6 +487,20 @@ export interface SourceDefinition<OF extends ObjectField<any> = ObjectField<any>
480
487
  * Plain and quick: they run when the editor opens a form.
481
488
  */
482
489
  choices?: Record<string, (data: D, ctx: { locale: string; t: (text: Localized) => string }) => Choice[]>;
490
+ /**
491
+ * The user may keep several named configurations of it, **profiles**
492
+ * (news «General» and «Technology», two GitHub accounts), each with its
493
+ * own options and read on its own, as if it were another source. A widget
494
+ * picks one with its `profile` (Kiosko adds the picker to the widgets that
495
+ * read the source; the plugin adds nothing) and finds that profile's data
496
+ * in `ctx.sources["plugin/source"]` as usual. Without profiles in the
497
+ * config, nothing changes: the source's options are
498
+ * `sources["plugin/source"]`, the default profile. Off unless declared:
499
+ * a source that reads hardware or the local machine (a battery, a
500
+ * Bluetooth sensor) has one thing to read. Kiosko 0.2.12 and later; older
501
+ * ones ignore it (one configuration).
502
+ */
503
+ profiles?: boolean;
483
504
  }
484
505
 
485
506
  /** One value the editor offers for an option (see `SourceDefinition.choices`). */
@@ -566,7 +587,7 @@ export interface InterludeDefinition<OF extends ObjectField<any> = ObjectField<a
566
587
  * package.json, `"kiosko": { "sdk": "^0.2.0" }`, so it can be refused before
567
588
  * it runs); the registry compares them with `satisfies`.
568
589
  */
569
- export const SDK_VERSION = '0.2.11';
590
+ export const SDK_VERSION = '0.2.12';
570
591
 
571
592
  /**
572
593
  * What a plugin does on the machine, declared for the person who installs