kiosko 0.2.10 → 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/sdk.js CHANGED
@@ -14,7 +14,7 @@ var CATEGORIES = [
14
14
  ];
15
15
  var ALERT_LEVELS = ["info", "warn", "critical"];
16
16
  var PAD_KEYS = ["up", "down", "left", "right", "a", "b", "x", "y", "l", "r", "start", "select"];
17
- var SDK_VERSION = "0.2.10";
17
+ var SDK_VERSION = "0.2.12";
18
18
  function defineWidget(w) {
19
19
  return w;
20
20
  }
@@ -244,7 +244,7 @@ function toJsonSchema(f) {
244
244
  case "list":
245
245
  return { ...base2, type: "array", items: toJsonSchema(f.item), maxItems: f.max, ...f.filter ? { "x-ui": { ...ui, widget: "filter", choices: f.item.kind === "string" ? f.item.choices : void 0 }, "x-filter": true } : {} };
246
246
  case "map":
247
- return { ...base2, type: "object", additionalProperties: { type: "string" } };
247
+ return { ...base2, type: "object", additionalProperties: { type: "string" }, ...f.keyChoices ? { "x-ui": { ...ui, keyChoices: f.keyChoices } } : {} };
248
248
  case "object":
249
249
  return { ...base2, type: "object", properties: Object.fromEntries(Object.entries(f.props).map(([k, sub]) => [k, toJsonSchema(sub)])) };
250
250
  }
@@ -805,6 +805,35 @@ var hostOf = (url) => {
805
805
  return url;
806
806
  }
807
807
  };
808
+ async function limitedText(res, max, url) {
809
+ const tooBig = () => new HttpError(`response larger than ${max} bytes from ${hostOf(url)}`, { status: res.status, url });
810
+ const declared = Number(res.headers.get("content-length"));
811
+ if (Number.isFinite(declared) && declared > max) {
812
+ await res.body?.cancel().catch(() => void 0);
813
+ throw tooBig();
814
+ }
815
+ if (!res.body) return "";
816
+ const reader = res.body.getReader();
817
+ const chunks = [];
818
+ let size = 0;
819
+ for (; ; ) {
820
+ const { done, value } = await reader.read();
821
+ if (done) break;
822
+ size += value.byteLength;
823
+ if (size > max) {
824
+ await reader.cancel().catch(() => void 0);
825
+ throw tooBig();
826
+ }
827
+ chunks.push(value);
828
+ }
829
+ const all = new Uint8Array(size);
830
+ let at = 0;
831
+ for (const c of chunks) {
832
+ all.set(c, at);
833
+ at += c.byteLength;
834
+ }
835
+ return new TextDecoder().decode(all);
836
+ }
808
837
  function createHttp(o = {}) {
809
838
  const doFetch = o.fetch ?? fetch;
810
839
  const now = o.now ?? Date.now;
@@ -834,7 +863,7 @@ function createHttp(o = {}) {
834
863
  throw new HttpError(`HTTP ${res.status} from ${host}: rate-limited until ${new Date(at).toISOString()}`, { status: res.status, url, retryAt: at });
835
864
  }
836
865
  if (!res.ok) throw new HttpError(`HTTP ${res.status} from ${host}`, { status: res.status, url });
837
- const body = await res.text();
866
+ const body = req.maxBytes === void 0 ? await res.text() : await limitedText(res, req.maxBytes, url);
838
867
  if (cacheable) {
839
868
  const etag = res.headers.get("etag") ?? void 0;
840
869
  const modified = res.headers.get("last-modified") ?? void 0;