@lotics/app-sdk 0.133.1 → 0.133.3

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.
@@ -348,6 +348,104 @@ function telemetryErrorMessage(status, parsed) {
348
348
  return code ?? gatewayErrorMessage(status);
349
349
  }
350
350
 
351
+ // ../shared/src/schemas/loaded_shape.ts
352
+ var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
353
+
354
+ // ../shared/src/answer_lines.ts
355
+ var ANSWER_LINES_MEDIA_TYPE = "application/x-ndjson";
356
+ var AnswerLinesError = class extends Error {
357
+ name = "AnswerLinesError";
358
+ };
359
+ var mediaTypeOf = (stated) => stated.split(";")[0]?.trim().toLowerCase();
360
+ function isAnswerLines(headers) {
361
+ const type = headers.get("content-type");
362
+ return type !== null && mediaTypeOf(type) === ANSWER_LINES_MEDIA_TYPE;
363
+ }
364
+ async function readAnswerLines(body, count, onLine, signal) {
365
+ signal?.throwIfAborted();
366
+ const reader = body.getReader();
367
+ let onAbort;
368
+ const aborted = new Promise((_, reject) => {
369
+ if (signal === void 0) return;
370
+ onAbort = () => reject(signal.reason);
371
+ signal.addEventListener("abort", onAbort, { once: true });
372
+ });
373
+ const decoder = new TextDecoder();
374
+ const answered = /* @__PURE__ */ new Set();
375
+ let pending2 = "";
376
+ const take = (text) => {
377
+ if (text.trim() === "") return;
378
+ let line;
379
+ try {
380
+ line = JSON.parse(text);
381
+ } catch (cause) {
382
+ throw new AnswerLinesError(`An answer line is not JSON (${text.length} characters).`, { cause });
383
+ }
384
+ if (!isObject(line)) throw new AnswerLinesError("An answer line is not an object.");
385
+ const { at } = line;
386
+ if (typeof at !== "number" || !Number.isInteger(at) || at < 0 || at >= count || answered.has(at)) {
387
+ throw new AnswerLinesError(`An answer line names read ${String(at)}, which is not one of the ${count} left to answer.`);
388
+ }
389
+ answered.add(at);
390
+ onLine(at, line);
391
+ };
392
+ try {
393
+ for (; ; ) {
394
+ const { done, value } = await (signal === void 0 ? reader.read() : Promise.race([reader.read(), aborted]));
395
+ if (done) break;
396
+ pending2 += decoder.decode(value, { stream: true });
397
+ const lines = pending2.split("\n");
398
+ pending2 = lines.pop() ?? "";
399
+ for (const text of lines) take(text);
400
+ }
401
+ pending2 += decoder.decode();
402
+ if (pending2.trim() !== "") throw new AnswerLinesError(`The answer lines ended inside a line (${pending2.length} characters).`);
403
+ } catch (error) {
404
+ await reader.cancel(error);
405
+ throw error;
406
+ } finally {
407
+ if (onAbort !== void 0) signal?.removeEventListener("abort", onAbort);
408
+ }
409
+ }
410
+
411
+ // ../shared/src/query_batch_answer.ts
412
+ var QueryBatchAnswerError = class extends Error {
413
+ name = "QueryBatchAnswerError";
414
+ };
415
+ function queryBatchAnswerOf(value) {
416
+ if (!isObject(value)) return void 0;
417
+ if ("result" in value) return isObject(value.result) ? { result: value.result } : void 0;
418
+ const { error } = value;
419
+ if (!isObject(error) || typeof error.status !== "number" || typeof error.message !== "string") return void 0;
420
+ const { code, hint, message_source: source } = error;
421
+ return {
422
+ error: {
423
+ status: error.status,
424
+ message: error.message,
425
+ ...typeof code === "string" ? { code } : {},
426
+ ...typeof hint === "string" ? { hint } : {},
427
+ ...source === "system" || source === "customer" ? { message_source: source } : {}
428
+ }
429
+ };
430
+ }
431
+ async function readQueryBatch(response, count, onAnswer, signal) {
432
+ const answered = /* @__PURE__ */ new Set();
433
+ const take = (at, value) => {
434
+ const answer = queryBatchAnswerOf(value);
435
+ if (answer === void 0) throw new QueryBatchAnswerError(`The answer to read ${at} is neither a result nor a refusal.`);
436
+ answered.add(at);
437
+ onAnswer(at, answer);
438
+ };
439
+ if (response.body !== null && isAnswerLines(response.headers)) {
440
+ await readAnswerLines(response.body, count, take, signal);
441
+ return answered;
442
+ }
443
+ const body = await response.json();
444
+ if (!isObject(body) || !Array.isArray(body.results)) throw new QueryBatchAnswerError("The batch's answer carries no results.");
445
+ body.results.slice(0, count).forEach((value, at) => take(at, value));
446
+ return answered;
447
+ }
448
+
351
449
  // ../shared/src/diacritics.ts
352
450
  var COMBINING_MARKS = /[̀-ͯ]/g;
353
451
  function foldDiacritics(value) {
@@ -1866,7 +1964,7 @@ async function streamStartError(res) {
1866
1964
  return new ApiRefusal(res.status, parsed);
1867
1965
  }
1868
1966
  var API_TIMEOUT_MS = 3e4;
1869
- async function apiCall(method, path, body, opts) {
1967
+ function apiHeaders(body, opts) {
1870
1968
  const headers = {};
1871
1969
  if (body) headers["content-type"] = "application/json";
1872
1970
  if (sessionToken && !opts?.skipAuth) {
@@ -1875,47 +1973,70 @@ async function apiCall(method, path, body, opts) {
1875
1973
  if (opts?.runToken) {
1876
1974
  headers[APP_AGENT_RUN_TOKEN_HEADER] = opts.runToken;
1877
1975
  }
1976
+ return headers;
1977
+ }
1978
+ function parsedJson(text) {
1979
+ if (!text) return null;
1980
+ try {
1981
+ return JSON.parse(text);
1982
+ } catch {
1983
+ return null;
1984
+ }
1985
+ }
1986
+ async function refused(res, opts) {
1987
+ const parsed = parsedJson(await res.text());
1988
+ const errorBody = parsed;
1989
+ if (res.status === 401 && errorBody?.error_code === PASSWORD_REQUIRED_CODE && !opts?.skipAuth) {
1990
+ sessionToken = null;
1991
+ window.location.assign("/__gate");
1992
+ return new Promise(() => {
1993
+ });
1994
+ }
1995
+ throw new ApiRefusal(res.status, parsed, readFieldErrors(parsed));
1996
+ }
1997
+ async function readResponse(res, opts) {
1998
+ if (!res.ok) return refused(res, opts);
1999
+ const text = await res.text();
2000
+ return parsedJson(text) ?? (text ? text : {});
2001
+ }
2002
+ async function withinTimeout(request) {
1878
2003
  const controller = new AbortController();
1879
2004
  let didTimeout = false;
1880
2005
  const timeoutId = setTimeout(() => {
1881
2006
  didTimeout = true;
1882
2007
  controller.abort();
1883
2008
  }, API_TIMEOUT_MS);
1884
- let res;
1885
2009
  try {
1886
- res = await fetch(`${apiBase()}${path}`, {
1887
- method,
1888
- headers,
1889
- ...method === "POST" && body ? { body: JSON.stringify(body) } : {},
1890
- signal: controller.signal
1891
- });
2010
+ return await request(controller.signal);
1892
2011
  } catch (err) {
1893
- if (didTimeout && err instanceof DOMException && err.name === "AbortError") {
1894
- throw new Error(gatewayErrorMessage(524));
1895
- }
2012
+ if (didTimeout) throw new Error(gatewayErrorMessage(524), { cause: err });
1896
2013
  throw err;
1897
2014
  } finally {
1898
2015
  clearTimeout(timeoutId);
1899
2016
  }
1900
- const text = await res.text();
1901
- let parsed = null;
1902
- if (text) {
1903
- try {
1904
- parsed = JSON.parse(text);
1905
- } catch {
1906
- }
1907
- }
1908
- if (!res.ok) {
1909
- const errorBody = parsed;
1910
- if (res.status === 401 && errorBody?.error_code === PASSWORD_REQUIRED_CODE && !opts?.skipAuth) {
1911
- sessionToken = null;
1912
- window.location.assign("/__gate");
1913
- return new Promise(() => {
1914
- });
1915
- }
1916
- throw new ApiRefusal(res.status, parsed, readFieldErrors(parsed));
1917
- }
1918
- return parsed ?? (text ? text : {});
2017
+ }
2018
+ async function apiCall(method, path, body, opts) {
2019
+ const res = await withinTimeout(
2020
+ (signal) => fetch(`${apiBase()}${path}`, {
2021
+ method,
2022
+ headers: apiHeaders(body, opts),
2023
+ ...method === "POST" && body ? { body: JSON.stringify(body) } : {},
2024
+ signal
2025
+ })
2026
+ );
2027
+ return readResponse(res, opts);
2028
+ }
2029
+ async function apiQueryBatch(path, body, count, onAnswer) {
2030
+ return withinTimeout(async (signal) => {
2031
+ const res = await fetch(`${apiBase()}${path}`, {
2032
+ method: "POST",
2033
+ headers: { ...apiHeaders(body), accept: ANSWER_LINES_MEDIA_TYPE },
2034
+ body: JSON.stringify(body),
2035
+ signal
2036
+ });
2037
+ if (!res.ok) return refused(res);
2038
+ return readQueryBatch(res, count, onAnswer, signal);
2039
+ });
1919
2040
  }
1920
2041
  function rpcStandalone(op, payload) {
1921
2042
  switch (op) {
@@ -2071,37 +2192,19 @@ async function sendAskedReads() {
2071
2192
  if (booted === void 0) return;
2072
2193
  for (let at = 0; at < asked.length; at += APP_QUERY_BATCH_MAX) {
2073
2194
  const chunk = asked.slice(at, at + APP_QUERY_BATCH_MAX);
2074
- apiCall("POST", `/v1/apps/${booted.app_id}/query_batch`, { reads: chunk.map((read) => read.payload) }, { appId: booted.app_id }).then((answered) => {
2075
- const results = batchResults(answered);
2195
+ const answerRead = (k, answer) => {
2196
+ const read = chunk[k];
2197
+ if (read === void 0) return;
2198
+ if ("result" in answer) read.resolve(answer.result);
2199
+ else read.reject(new ApiRefusal(answer.error.status, answer.error));
2200
+ };
2201
+ apiQueryBatch(`/v1/apps/${booted.app_id}/query_batch`, { reads: chunk.map((read) => read.payload) }, chunk.length, answerRead).then((answered) => {
2076
2202
  chunk.forEach((read, k) => {
2077
- const answer = results[k];
2078
- if (answer === void 0) read.reject(new Error(`The server answered ${results.length} of ${chunk.length} reads.`));
2079
- else if ("result" in answer) read.resolve(answer.result);
2080
- else read.reject(new ApiRefusal(answer.error.status, answer.error));
2203
+ if (!answered.has(k)) read.reject(new Error(`The server answered ${answered.size} of ${chunk.length} reads.`));
2081
2204
  });
2082
2205
  }).catch((error) => chunk.forEach((read) => read.reject(error)));
2083
2206
  }
2084
2207
  }
2085
- function batchResults(answered) {
2086
- if (answered === null || typeof answered !== "object" || !("results" in answered) || !Array.isArray(answered.results)) {
2087
- throw new Error("The server's batch answer carried no results.");
2088
- }
2089
- return answered.results.map((answer, at) => {
2090
- if (answer !== null && typeof answer === "object" && "result" in answer) return { result: answer.result };
2091
- const error = answer !== null && typeof answer === "object" && "error" in answer ? answer.error : void 0;
2092
- if (error !== null && typeof error === "object" && "status" in error && typeof error.status === "number" && "message" in error && typeof error.message === "string") {
2093
- return {
2094
- error: {
2095
- status: error.status,
2096
- message: error.message,
2097
- code: "code" in error ? error.code : void 0,
2098
- message_source: "message_source" in error ? error.message_source : void 0
2099
- }
2100
- };
2101
- }
2102
- throw new Error(`The server's batch answer ${at} is neither a result nor a refusal.`);
2103
- });
2104
- }
2105
2208
  async function standaloneExport(payload) {
2106
2209
  const { app_id } = await boot();
2107
2210
  return apiCall("POST", `/v1/apps/${app_id}/export`, payload, { appId: app_id });
package/dist/index.js CHANGED
@@ -61,7 +61,7 @@ import {
61
61
  subtractDecimal,
62
62
  sumDecimal,
63
63
  urlParam
64
- } from "./chunk-J3UN4AJW.js";
64
+ } from "./chunk-FAT4EYLS.js";
65
65
 
66
66
  // src/mount.tsx
67
67
  import { createRoot } from "react-dom/client";
@@ -11681,6 +11681,7 @@ function readFiles(v) {
11681
11681
  const filename = f.filename;
11682
11682
  const mime = f.mime_type;
11683
11683
  const thumb = f.thumbnail_url;
11684
+ const placeholder = Reflect.get(f, "placeholder");
11684
11685
  const size2 = f.size;
11685
11686
  const created_at = f.created_at;
11686
11687
  const template = f.document_template_id;
@@ -11691,6 +11692,7 @@ function readFiles(v) {
11691
11692
  mime_type: typeof mime === "string" ? mime : "",
11692
11693
  url,
11693
11694
  thumbnail_url: typeof thumb === "string" ? thumb : void 0,
11695
+ ...typeof placeholder === "string" ? { placeholder } : {},
11694
11696
  size: typeof size2 === "number" ? size2 : void 0,
11695
11697
  created_at: typeof created_at === "string" ? created_at : void 0,
11696
11698
  document_template_id: typeof template === "string" ? template : void 0,
package/dist/router.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  setUrlParams,
7
7
  settleOpen,
8
8
  subscribeOpen
9
- } from "./chunk-J3UN4AJW.js";
9
+ } from "./chunk-FAT4EYLS.js";
10
10
 
11
11
  // src/router.tsx
12
12
  import { useEffect, useLayoutEffect, useRef } from "react";
@@ -12,6 +12,8 @@ export interface AppFile {
12
12
  mime_type: string;
13
13
  url: string;
14
14
  thumbnail_url?: string;
15
+ /** An image's blurred preview (a base64 ThumbHash), drawn while `url` loads; absent where none was kept. */
16
+ placeholder?: string;
15
17
  /** Bytes; absent on older files — sort by the raw number, format at display. */
16
18
  size?: number | null;
17
19
  /** ISO upload timestamp. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lotics/app-sdk",
3
- "version": "0.133.1",
3
+ "version": "0.133.3",
4
4
  "description": "The SDK a Lotics custom-code app reads and writes through — typed hooks over the host bridge, cell readers, mount() and AppRouter",
5
5
  "type": "module",
6
6
  "exports": {