@kitionstage/sdk 0.0.0-stage → 0.5.2-stage.2

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 (259) hide show
  1. package/README.md +73 -2
  2. package/dist/LICENSE +177 -0
  3. package/dist/THIRD_PARTY_NOTICES +28 -0
  4. package/dist/lib/assets/kitEngine.worker-3iMvwtlI.js +12732 -0
  5. package/dist/lib/assets/loro_wasm_bg-Gulzks_0.wasm +0 -0
  6. package/dist/lib/chunks/batch-BWpAyHvP.js +789 -0
  7. package/dist/lib/chunks/canonical-BIWfVCL-.js +87 -0
  8. package/dist/lib/chunks/envelope-D6MGY-ZN.js +61 -0
  9. package/dist/lib/chunks/replica-D8w3I4sn.js +2472 -0
  10. package/dist/lib/chunks/runtime-BVJHE1Pg.js +997 -0
  11. package/dist/lib/chunks/session-BqXC9a1-.js +281 -0
  12. package/dist/lib/chunks/toolIds-BCeSGHpV.js +821 -0
  13. package/dist/lib/chunks/workspaceTree-Destqupf.js +23 -0
  14. package/dist/lib/convergence/index.js +1035 -0
  15. package/dist/lib/convergence/node.js +8 -0
  16. package/dist/lib/embed-server/callbacks.js +37 -0
  17. package/dist/lib/embed-server/cli.js +19 -0
  18. package/dist/lib/embed-server/index.js +272 -0
  19. package/dist/lib/extensions/index.js +439 -0
  20. package/dist/lib/extensions/panel.html +208 -0
  21. package/dist/lib/extensions/runtime.js +198 -0
  22. package/dist/lib/extensions/sandbox.html +35 -0
  23. package/dist/lib/kdm/index.js +437 -0
  24. package/dist/lib/kit-dev-host/checkUiIds.mjs +44 -0
  25. package/dist/lib/kit-dev-host/index.js +308 -0
  26. package/dist/lib/kit-dev-host/lineRow.mjs +47 -0
  27. package/dist/lib/kit-dev-host/modeSmoke.mjs +178 -0
  28. package/dist/lib/kit-editor/index.js +6481 -0
  29. package/dist/lib/kit-editor/styles.css +807 -0
  30. package/dist/lib/kit-editor/workspaceTree.js +1 -0
  31. package/dist/lib/kit-engine/index.js +7 -0
  32. package/dist/lib/kit-host/index.js +530 -0
  33. package/dist/lib/kit-ui-protocol/index.js +336 -0
  34. package/dist/lib/node-only/embed-server-callbacks.js +2 -0
  35. package/dist/lib/node-only/embed-server.js +2 -0
  36. package/dist/lib/sync/index.js +1008 -0
  37. package/dist/lib/sync/openToken.js +201 -0
  38. package/dist/lib/sync/replicaVersions.js +51 -0
  39. package/dist/standalone/chunks/batch-CAGYk6m4.js +789 -0
  40. package/dist/standalone/chunks/client-B9xkOcvf.js +772 -0
  41. package/dist/standalone/chunks/collab-CTQZ9h7J.js +1022 -0
  42. package/dist/standalone/chunks/comment-Ds9t8xSE.js +94 -0
  43. package/dist/standalone/chunks/comments-uVDS8Wxp.js +41 -0
  44. package/dist/standalone/chunks/document-B5MMuYyN.js +324 -0
  45. package/dist/standalone/chunks/envelope-GX4Cynkx.js +7612 -0
  46. package/dist/standalone/chunks/index-BEgEEnhU.js +287 -0
  47. package/dist/standalone/chunks/index-EEl8hGbP.js +1967 -0
  48. package/dist/standalone/chunks/kits-Cp51mDlw.js +9 -0
  49. package/dist/standalone/chunks/replica-Di2iCPiD.js +3281 -0
  50. package/dist/standalone/chunks/replicaVersions-C3rJxnk9.js +50 -0
  51. package/dist/standalone/chunks/touched-T9jKJjxn.js +78 -0
  52. package/dist/standalone/index.js +12 -0
  53. package/dist/standalone/script/index.js +339 -0
  54. package/dist/standalone/script/kits/board.js +5 -0
  55. package/dist/standalone/script/kits/cad.js +5 -0
  56. package/dist/standalone/script/kits/design.js +5 -0
  57. package/dist/standalone/script/kits/doc.js +5 -0
  58. package/dist/standalone/script/kits/film.js +5 -0
  59. package/dist/standalone/script/kits/html.js +5 -0
  60. package/dist/standalone/script/kits/index.js +38 -0
  61. package/dist/standalone/script/kits/layout.js +5 -0
  62. package/dist/standalone/script/kits/motion.js +5 -0
  63. package/dist/standalone/script/kits/note.js +5 -0
  64. package/dist/standalone/script/kits/pdf.js +5 -0
  65. package/dist/standalone/script/kits/photo.js +5 -0
  66. package/dist/standalone/script/kits/scene.js +5 -0
  67. package/dist/standalone/script/kits/sheet.js +5 -0
  68. package/dist/standalone/script/kits/slide.js +5 -0
  69. package/dist/standalone/script/kits/table.js +5 -0
  70. package/dist/standalone/script/kits/vector.js +5 -0
  71. package/dist/standalone/script/node.js +401 -0
  72. package/dist/standalone/script/proxy.js +254 -0
  73. package/dist/types/convergence/src/adapter.d.ts +131 -0
  74. package/dist/types/convergence/src/engine.d.ts +66 -0
  75. package/dist/types/convergence/src/index.d.ts +21 -0
  76. package/dist/types/convergence/src/net.d.ts +52 -0
  77. package/dist/types/convergence/src/node.d.ts +6 -0
  78. package/dist/types/convergence/src/report.d.ts +56 -0
  79. package/dist/types/convergence/src/rng.d.ts +12 -0
  80. package/dist/types/convergence/src/scenarios.d.ts +15 -0
  81. package/dist/types/convergence/src/state.d.ts +67 -0
  82. package/dist/types/embed/src/agent.d.ts +88 -0
  83. package/dist/types/embed/src/blobs.d.ts +2 -0
  84. package/dist/types/embed/src/collab.d.ts +250 -0
  85. package/dist/types/embed/src/comments.d.ts +30 -0
  86. package/dist/types/embed/src/errors.d.ts +24 -0
  87. package/dist/types/embed/src/handlers.d.ts +92 -0
  88. package/dist/types/embed/src/index.d.ts +20 -0
  89. package/dist/types/embed/src/mount.d.ts +39 -0
  90. package/dist/types/embed/src/replicaStore.d.ts +3 -0
  91. package/dist/types/embed/src/rooms.d.ts +40 -0
  92. package/dist/types/embed/src/schemas.d.ts +7 -0
  93. package/dist/types/embed/src/syncClients.d.ts +28 -0
  94. package/dist/types/embed/src/theme.d.ts +7 -0
  95. package/dist/types/embed/src/types.d.ts +596 -0
  96. package/dist/types/embed/src/ui.d.ts +33 -0
  97. package/dist/types/embed/src/version.d.ts +2 -0
  98. package/dist/types/embed-server/src/callbacks.d.ts +30 -0
  99. package/dist/types/embed-server/src/cli.d.ts +1 -0
  100. package/dist/types/embed-server/src/index.d.ts +19 -0
  101. package/dist/types/embed-server/src/page.d.ts +15 -0
  102. package/dist/types/embed-server/src/server.d.ts +39 -0
  103. package/dist/types/embed-server/src/session.d.ts +22 -0
  104. package/dist/types/extensions/src/gate.d.ts +17 -0
  105. package/dist/types/extensions/src/generated/runtime.d.ts +8 -0
  106. package/dist/types/extensions/src/host.d.ts +135 -0
  107. package/dist/types/extensions/src/index.d.ts +13 -0
  108. package/dist/types/extensions/src/manifest.d.ts +82 -0
  109. package/dist/types/extensions/src/protocol.d.ts +159 -0
  110. package/dist/types/extensions/src/rpc.d.ts +52 -0
  111. package/dist/types/extensions/src/runtime/document.d.ts +40 -0
  112. package/dist/types/extensions/src/runtime/index.d.ts +10 -0
  113. package/dist/types/extensions/src/runtime/panel.d.ts +19 -0
  114. package/dist/types/extensions/src/runtime/runtime.d.ts +54 -0
  115. package/dist/types/extensions/src/runtime/worker.d.ts +1 -0
  116. package/dist/types/extensions/src/sandbox.d.ts +41 -0
  117. package/dist/types/kdm/src/args.d.ts +12 -0
  118. package/dist/types/kdm/src/batch.d.ts +118 -0
  119. package/dist/types/kdm/src/canonical.d.ts +25 -0
  120. package/dist/types/kdm/src/comment.d.ts +114 -0
  121. package/dist/types/kdm/src/diff.d.ts +125 -0
  122. package/dist/types/kdm/src/envelope.d.ts +27 -0
  123. package/dist/types/kdm/src/errors.d.ts +28 -0
  124. package/dist/types/kdm/src/footprint.d.ts +21 -0
  125. package/dist/types/kdm/src/ids.d.ts +57 -0
  126. package/dist/types/kdm/src/index.d.ts +18 -0
  127. package/dist/types/kdm/src/marks.d.ts +32 -0
  128. package/dist/types/kdm/src/op.d.ts +124 -0
  129. package/dist/types/kdm/src/origin.d.ts +41 -0
  130. package/dist/types/kdm/src/overlay.d.ts +92 -0
  131. package/dist/types/kdm/src/projection.d.ts +29 -0
  132. package/dist/types/kdm/src/read.d.ts +90 -0
  133. package/dist/types/kdm/src/replica.d.ts +403 -0
  134. package/dist/types/kdm/src/schema.d.ts +185 -0
  135. package/dist/types/kdm/src/testing.d.ts +28 -0
  136. package/dist/types/kdm/src/toolIds.d.ts +61 -0
  137. package/dist/types/kdm/src/touched.d.ts +24 -0
  138. package/dist/types/kdm/src/tx.d.ts +215 -0
  139. package/dist/types/kdm/src/value.d.ts +23 -0
  140. package/dist/types/kit-dev-host/src/checkNoPaths.d.ts +19 -0
  141. package/dist/types/kit-dev-host/src/checkUiIds.d.mts +2 -0
  142. package/dist/types/kit-dev-host/src/index.d.ts +179 -0
  143. package/dist/types/kit-dev-host/src/lineRow.d.mts +14 -0
  144. package/dist/types/kit-dev-host/src/memoryFiles.d.ts +15 -0
  145. package/dist/types/kit-dev-host/src/modeSmoke.d.mts +8 -0
  146. package/dist/types/kit-editor/src/CommandDialog.d.ts +22 -0
  147. package/dist/types/kit-editor/src/CommentsPanel.d.ts +45 -0
  148. package/dist/types/kit-editor/src/CustomizeDialog.d.ts +32 -0
  149. package/dist/types/kit-editor/src/HostButtons.d.ts +11 -0
  150. package/dist/types/kit-editor/src/RenderFailure.d.ts +7 -0
  151. package/dist/types/kit-editor/src/TextSink.d.ts +6 -0
  152. package/dist/types/kit-editor/src/brand.d.ts +2 -0
  153. package/dist/types/kit-editor/src/catalogMenus.d.ts +27 -0
  154. package/dist/types/kit-editor/src/chart.d.ts +138 -0
  155. package/dist/types/kit-editor/src/conflicts.d.ts +12 -0
  156. package/dist/types/kit-editor/src/drawing.d.ts +83 -0
  157. package/dist/types/kit-editor/src/editor.d.ts +544 -0
  158. package/dist/types/kit-editor/src/engine/KitCanvas.d.ts +70 -0
  159. package/dist/types/kit-editor/src/engine/backdrop.d.ts +32 -0
  160. package/dist/types/kit-editor/src/engine/createKitFile.d.ts +12 -0
  161. package/dist/types/kit-editor/src/engine/decorations.d.ts +14 -0
  162. package/dist/types/kit-editor/src/engine/engineOverlays.d.ts +197 -0
  163. package/dist/types/kit-editor/src/engine/engineTools.d.ts +116 -0
  164. package/dist/types/kit-editor/src/engine/kitDraft.d.ts +70 -0
  165. package/dist/types/kit-editor/src/engine/kitEngine.d.ts +390 -0
  166. package/dist/types/kit-editor/src/engine/kitEngine.worker.d.ts +30 -0
  167. package/dist/types/kit-editor/src/engine/kitHost.d.ts +41 -0
  168. package/dist/types/kit-editor/src/engine/kitModel.d.ts +32 -0
  169. package/dist/types/kit-editor/src/engine/kitModelRunner.d.ts +99 -0
  170. package/dist/types/kit-editor/src/engine/kitModule.d.ts +185 -0
  171. package/dist/types/kit-editor/src/engine/kitShortcuts.d.ts +32 -0
  172. package/dist/types/kit-editor/src/engine/kitTools.d.ts +79 -0
  173. package/dist/types/kit-editor/src/engine/lazyFonts.d.ts +34 -0
  174. package/dist/types/kit-editor/src/engine/lentFiles.d.ts +29 -0
  175. package/dist/types/kit-editor/src/engine/rasterViewport.d.ts +178 -0
  176. package/dist/types/kit-editor/src/engine/readOnly.d.ts +78 -0
  177. package/dist/types/kit-editor/src/engine/refusal.d.ts +39 -0
  178. package/dist/types/kit-editor/src/engine/replica.d.ts +23 -0
  179. package/dist/types/kit-editor/src/engine/useKitDocument.d.ts +154 -0
  180. package/dist/types/kit-editor/src/engine/workerRelay.d.ts +62 -0
  181. package/dist/types/kit-editor/src/files.d.ts +74 -0
  182. package/dist/types/kit-editor/src/frame/EditorFrame.d.ts +74 -0
  183. package/dist/types/kit-editor/src/frame/FocusBar.d.ts +10 -0
  184. package/dist/types/kit-editor/src/frame/MenuNodes.d.ts +20 -0
  185. package/dist/types/kit-editor/src/frame/MenuRibbon.d.ts +8 -0
  186. package/dist/types/kit-editor/src/frame/ModeButton.d.ts +7 -0
  187. package/dist/types/kit-editor/src/frame/menus.d.ts +64 -0
  188. package/dist/types/kit-editor/src/frame/modes.d.ts +51 -0
  189. package/dist/types/kit-editor/src/frame/useFrameLayout.d.ts +34 -0
  190. package/dist/types/kit-editor/src/host.d.ts +127 -0
  191. package/dist/types/kit-editor/src/index.d.ts +85 -0
  192. package/dist/types/kit-editor/src/ink.d.ts +26 -0
  193. package/dist/types/kit-editor/src/lease.d.ts +53 -0
  194. package/dist/types/kit-editor/src/oneInFlight.d.ts +7 -0
  195. package/dist/types/kit-editor/src/phrases.d.ts +25 -0
  196. package/dist/types/kit-editor/src/presence.d.ts +79 -0
  197. package/dist/types/kit-editor/src/strings.d.ts +6 -0
  198. package/dist/types/kit-editor/src/toolIcons.d.ts +32 -0
  199. package/dist/types/kit-editor/src/uiCatalog.d.ts +59 -0
  200. package/dist/types/kit-editor/src/uiConfig.d.ts +53 -0
  201. package/dist/types/kit-editor/src/uiContext.d.ts +65 -0
  202. package/dist/types/kit-editor/src/uiStore.d.ts +20 -0
  203. package/dist/types/kit-editor/src/useCommentAnchors.d.ts +29 -0
  204. package/dist/types/kit-editor/src/useKitSession.d.ts +115 -0
  205. package/dist/types/kit-editor/src/workspaceTree.d.ts +5 -0
  206. package/dist/types/kit-engine/src/contract.d.ts +154 -0
  207. package/dist/types/kit-engine/src/index.d.ts +2 -0
  208. package/dist/types/kit-engine/src/session.d.ts +133 -0
  209. package/dist/types/kit-host/src/files.d.ts +163 -0
  210. package/dist/types/kit-host/src/index.d.ts +315 -0
  211. package/dist/types/kit-ui-protocol/src/index.d.ts +1145 -0
  212. package/dist/types/loro-crdt.d.ts +9 -0
  213. package/dist/types/script/kits/board.d.ts +534 -0
  214. package/dist/types/script/kits/cad.d.ts +2937 -0
  215. package/dist/types/script/kits/design.d.ts +3213 -0
  216. package/dist/types/script/kits/doc.d.ts +2575 -0
  217. package/dist/types/script/kits/film.d.ts +4067 -0
  218. package/dist/types/script/kits/html.d.ts +38 -0
  219. package/dist/types/script/kits/index.d.ts +20 -0
  220. package/dist/types/script/kits/layout.d.ts +3483 -0
  221. package/dist/types/script/kits/motion.d.ts +4826 -0
  222. package/dist/types/script/kits/note.d.ts +631 -0
  223. package/dist/types/script/kits/pdf.d.ts +829 -0
  224. package/dist/types/script/kits/photo.d.ts +7574 -0
  225. package/dist/types/script/kits/scene.d.ts +2962 -0
  226. package/dist/types/script/kits/sheet.d.ts +2419 -0
  227. package/dist/types/script/kits/slide.d.ts +1894 -0
  228. package/dist/types/script/kits/table.d.ts +654 -0
  229. package/dist/types/script/kits/vector.d.ts +5167 -0
  230. package/dist/types/script/src/agentRelay.d.ts +48 -0
  231. package/dist/types/script/src/capabilities.d.ts +21 -0
  232. package/dist/types/script/src/document.d.ts +87 -0
  233. package/dist/types/script/src/drafts.d.ts +141 -0
  234. package/dist/types/script/src/exchange.d.ts +30 -0
  235. package/dist/types/script/src/headless.d.ts +190 -0
  236. package/dist/types/script/src/index.d.ts +15 -0
  237. package/dist/types/script/src/kitModel.d.ts +17 -0
  238. package/dist/types/script/src/kits.d.ts +8 -0
  239. package/dist/types/script/src/node.d.ts +42 -0
  240. package/dist/types/script/src/port.d.ts +89 -0
  241. package/dist/types/script/src/proxy.d.ts +132 -0
  242. package/dist/types/script/src/room.d.ts +162 -0
  243. package/dist/types/script/src/sources.d.ts +73 -0
  244. package/dist/types/sync/src/backoff.d.ts +9 -0
  245. package/dist/types/sync/src/blobSection.d.ts +23 -0
  246. package/dist/types/sync/src/capabilities.d.ts +20 -0
  247. package/dist/types/sync/src/client.d.ts +253 -0
  248. package/dist/types/sync/src/framing.d.ts +35 -0
  249. package/dist/types/sync/src/hash.d.ts +2 -0
  250. package/dist/types/sync/src/index.d.ts +19 -0
  251. package/dist/types/sync/src/leases.d.ts +82 -0
  252. package/dist/types/sync/src/openToken.d.ts +104 -0
  253. package/dist/types/sync/src/protocol.d.ts +484 -0
  254. package/dist/types/sync/src/relay.d.ts +52 -0
  255. package/dist/types/sync/src/replicaVersions.d.ts +17 -0
  256. package/dist/types/sync/src/saveMachine.d.ts +83 -0
  257. package/dist/types/sync/src/saveRun.d.ts +52 -0
  258. package/dist/types/sync/src/transport.d.ts +19 -0
  259. package/package.json +229 -4
@@ -0,0 +1,8 @@
1
+ import { writeFileSync as n } from "node:fs";
2
+ function s(t, o = process.env) {
3
+ const e = o.KITION_CONVERGENCE_REPORT;
4
+ e && n(e, t.toJsonText());
5
+ }
6
+ export {
7
+ s as saveReportIfRequested
8
+ };
@@ -0,0 +1,37 @@
1
+ import { createHmac as h, createHash as A, timingSafeEqual as d } from "node:crypto";
2
+ const E = "x-kition-signature", f = "x-kition-timestamp", l = "x-kition-subject", g = "x-kition-doc-key", x = "x-kition-version", H = "x-kition-name", b = 300, k = (n) => A("sha256").update(n ?? new Uint8Array()).digest("hex");
3
+ function u(n, t, r, a, e) {
4
+ return `v1=${h("sha256", n).update(`${t.toUpperCase()}
5
+ ${r}
6
+ ${a}
7
+ ${k(e)}`).digest("hex")}`;
8
+ }
9
+ function S(n, t, r, a, e, o = Math.floor(Date.now() / 1e3)) {
10
+ return {
11
+ [f]: String(o),
12
+ [E]: u(n, t, r, o, a),
13
+ [l]: e.subject,
14
+ [g]: e.docKey
15
+ };
16
+ }
17
+ function D(n, t, r = Math.floor(Date.now() / 1e3)) {
18
+ const a = (m) => {
19
+ const i = t.headers[m];
20
+ return Array.isArray(i) ? i[0] : i;
21
+ }, e = Number(a(f)), o = a(E);
22
+ if (!Number.isSafeInteger(e) || Math.abs(r - e) > b || !o) return !1;
23
+ const s = Buffer.from(u(n, t.method, t.path, e, t.body)), c = Buffer.from(o);
24
+ return s.length === c.length && d(s, c);
25
+ }
26
+ export {
27
+ b as CALLBACK_SKEW_SECS,
28
+ g as DOC_KEY_HEADER,
29
+ H as NAME_HEADER,
30
+ E as SIGNATURE_HEADER,
31
+ l as SUBJECT_HEADER,
32
+ f as TIMESTAMP_HEADER,
33
+ x as VERSION_HEADER,
34
+ S as callbackHeaders,
35
+ u as signCallback,
36
+ D as verifyCallback
37
+ };
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ import { readFile as c } from "node:fs/promises";
3
+ import { resolve as a } from "node:path";
4
+ import { pathToFileURL as l } from "node:url";
5
+ import { startEmbedServer as d } from "./index.js";
6
+ const i = `Usage: kition-embed-server <config.json | config.mjs>
7
+
8
+ Runs the embed server for URL embedding (https://developer.kition.ai/url).
9
+ The config holds startEmbedServer's options: id, secret, integrators, kits,
10
+ kitsUrl (or kitsDir and kitsPort), and optionally port, host, embedDir.`, e = process.argv[2];
11
+ (!e || e === "--help" || e === "-h") && (console.log(i), process.exit(e ? 0 : 1));
12
+ const s = a(e), n = s.endsWith(".json") ? JSON.parse(await c(s, "utf8")) : (await import(l(s).href)).default, t = ["id", "secret", "integrators", "kits"].filter((o) => !n?.[o]);
13
+ t.length && (console.error(`${e} names no ${t.join(", ")}
14
+
15
+ ${i}`), process.exit(1));
16
+ const r = await d(n);
17
+ console.log(`embed server: ${r.url}`);
18
+ console.log(`kit packages: ${r.kitsUrl}`);
19
+ for (const o of ["SIGINT", "SIGTERM"]) process.once(o, () => void r.close().then(() => process.exit(0)));
@@ -0,0 +1,272 @@
1
+ import { createServer as H } from "node:http";
2
+ import { readFile as O } from "node:fs/promises";
3
+ import { dirname as W, extname as B, normalize as I, sep as S, join as T } from "node:path";
4
+ import { timingSafeEqual as G, createHmac as K, randomBytes as N } from "node:crypto";
5
+ import { fileURLToPath as J } from "node:url";
6
+ import { decodeOpenToken as M, OpenTokenError as A, verifyOpenToken as V } from "../sync/openToken.js";
7
+ import { NAME_HEADER as _, VERSION_HEADER as E, callbackHeaders as z } from "./callbacks.js";
8
+ import { CALLBACK_SKEW_SECS as he, signCallback as pe, verifyCallback as me } from "./callbacks.js";
9
+ const R = (e) => JSON.stringify(e).replace(/</g, "\\u003c"), F = (e) => e.replace(/[&<>"']/g, (n) => `&#${n.charCodeAt(0)};`);
10
+ function X({ session: e, claims: n, kitUrl: c, embedModule: l, nonce: f, title: b }) {
11
+ const { perms: d } = n, p = {
12
+ session: e,
13
+ kit: { id: n.kit, url: c },
14
+ file: { id: n.file, name: b },
15
+ user: { id: n.sub, name: n.name || n.sub },
16
+ mode: d.edit ? "edit" : d.comment ? "comment" : "view",
17
+ display: { permissions: { download: d.download, export: d.export, share: d.share }, watermark: n.watermark }
18
+ };
19
+ return `<!doctype html>
20
+ <html><head><meta charset="utf-8"><meta name="referrer" content="no-referrer"><title>${F(b)}</title>
21
+ <style nonce="${f}">html,body,#editor{margin:0;height:100%;overflow:hidden}</style></head>
22
+ <body><div id="editor"></div>
23
+ <script type="application/json" id="kition-config">${R(p)}<\/script>
24
+ <script type="module" nonce="${f}">
25
+ import { KitionEmbed, EmbedConflictError } from ${R(l)}
26
+ const config = JSON.parse(document.getElementById('kition-config').textContent)
27
+ // The open token stays out of the page's address, history and referrers.
28
+ history.replaceState(null, '', location.pathname)
29
+ const auth = { authorization: 'Bearer ' + config.session }
30
+ async function failed(response) {
31
+ throw new Error('the embed server answered ' + response.status)
32
+ }
33
+ const replica = {
34
+ read: async () => {
35
+ const response = await fetch('/v1/replica', { headers: auth })
36
+ if (response.status === 404 || response.status === 501) return null
37
+ if (!response.ok) return failed(response)
38
+ const body = new Uint8Array(await response.arrayBuffer())
39
+ const length = new DataView(body.buffer, body.byteOffset).getUint32(0)
40
+ return { snapshot: body.slice(4, 4 + length).buffer, blobs: body.slice(4 + length).buffer, fileStamp: response.headers.get('x-kition-file-stamp') }
41
+ },
42
+ write: async (_id, stored) => {
43
+ const snapshot = new Uint8Array(stored.snapshot), blobs = new Uint8Array(stored.blobs)
44
+ const body = new Uint8Array(4 + snapshot.length + blobs.length)
45
+ new DataView(body.buffer).setUint32(0, snapshot.length)
46
+ body.set(snapshot, 4)
47
+ body.set(blobs, 4 + snapshot.length)
48
+ const response = await fetch('/v1/replica', { method: 'PUT', headers: { ...auth, 'x-kition-file-stamp': stored.fileStamp }, body })
49
+ if (!response.ok && response.status !== 501) return failed(response)
50
+ },
51
+ }
52
+ const storage = {
53
+ read: async () => {
54
+ const response = await fetch('/v1/file', { headers: auth })
55
+ if (!response.ok) return failed(response)
56
+ return { bytes: new Uint8Array(await response.arrayBuffer()), version: response.headers.get('x-kition-version') }
57
+ },
58
+ stamp: async () => {
59
+ const response = await fetch('/v1/file/stamp', { headers: auth })
60
+ if (!response.ok) return failed(response)
61
+ return (await response.json()).version
62
+ },
63
+ write: async (fileId, bytes, { expectedVersion }) => {
64
+ const response = await fetch('/v1/file', { method: 'PUT', headers: { ...auth, ...(expectedVersion ? { 'if-match': expectedVersion } : {}) }, body: bytes })
65
+ if (response.status === 409) throw new EmbedConflictError(fileId, response.headers.get('x-kition-version'))
66
+ if (!response.ok) return failed(response)
67
+ return await response.json()
68
+ },
69
+ replica,
70
+ }
71
+ const editor = await KitionEmbed.mount(document.getElementById('editor'), {
72
+ kit: config.kit,
73
+ file: config.file,
74
+ storage,
75
+ identity: { user: () => config.user },
76
+ permissions: { mode: config.mode },
77
+ display: config.display,
78
+ })
79
+ editor.on('saved', (saved) => parent.postMessage({ kitionDocument: 1, event: 'saved', version: saved.version }, '*'))
80
+ await editor.ready()
81
+ parent.postMessage({ kitionDocument: 1, event: 'ready' }, '*')
82
+ <\/script></body></html>`;
83
+ }
84
+ const D = "kds1.", C = (e) => e.toString("base64url"), L = (e, n) => K("sha256", e).update(`kition-doc-session
85
+ ${n}`).digest();
86
+ function Y(e, n) {
87
+ const c = C(Buffer.from(JSON.stringify(n)));
88
+ return `${D}${c}.${C(L(e, c))}`;
89
+ }
90
+ function Q(e, n, c = Math.floor(Date.now() / 1e3)) {
91
+ if (!n?.startsWith(D)) return null;
92
+ const [l, f, b] = n.slice(D.length).split(".");
93
+ if (!l || !f || b !== void 0) return null;
94
+ const d = L(e, l), p = Buffer.from(f, "base64url");
95
+ if (p.length !== d.length || !G(p, d)) return null;
96
+ try {
97
+ const g = JSON.parse(Buffer.from(l, "base64url").toString("utf8"));
98
+ return typeof g.exp == "number" && c < g.exp ? g : null;
99
+ } catch {
100
+ return null;
101
+ }
102
+ }
103
+ const Z = { ".js": "text/javascript; charset=utf-8", ".mjs": "text/javascript; charset=utf-8", ".map": "application/json", ".css": "text/css", ".wasm": "application/wasm", ".json": "application/json" };
104
+ function q() {
105
+ try {
106
+ return W(J(import.meta.resolve("@kition/sdk/embed")));
107
+ } catch {
108
+ throw new Error("@kition/sdk/embed is not installed next to @kition/sdk/embed/server: install it, or set embedDir");
109
+ }
110
+ }
111
+ class a extends Error {
112
+ constructor(n, c) {
113
+ super(c), this.status = n;
114
+ }
115
+ }
116
+ async function ee(e, n) {
117
+ const c = [];
118
+ let l = 0;
119
+ for await (const f of e) {
120
+ if (l += f.length, l > n) throw new a(413, "too large");
121
+ c.push(f);
122
+ }
123
+ return new Uint8Array(Buffer.concat(c));
124
+ }
125
+ function te(e) {
126
+ if (e.secret.length < 32) throw new Error("the session secret needs at least 32 characters");
127
+ const n = new URL(e.kitsUrl).origin, c = e.fetch ?? fetch, l = e.now ?? (() => Math.floor(Date.now() / 1e3)), f = e.maxBytes ?? 256 * 1024 * 1024, b = e.embedDir ?? q(), d = (o) => e.integrators.find((s) => s.iss === o);
128
+ async function p(o, s, t, i, h = {}) {
129
+ const u = d(o.iss);
130
+ if (!u) throw new a(401, "unknown integrator");
131
+ const r = new URL(`${u.callbackUrl.replace(/\/$/, "")}/files/${encodeURIComponent(o.file)}${t}`), m = z(u.secret, s, r.pathname + r.search, i, { subject: o.sub, docKey: o.docKey }, l());
132
+ return c(r, { method: s, headers: { ...h, ...m }, ...i ? { body: i } : {} });
133
+ }
134
+ async function g(o, s) {
135
+ if (!o) throw new a(400, "no token");
136
+ let t;
137
+ try {
138
+ const w = d(M(o).iss);
139
+ if (!w) throw new A("unknown-key");
140
+ t = await V(o, w.keys, e.id, l());
141
+ } catch (w) {
142
+ throw new a(401, w instanceof A ? `open token refused: ${w.code}` : "open token refused");
143
+ }
144
+ const i = d(t.iss), h = { iss: t.iss, tenant: t.tenant, sub: t.sub, name: t.name, file: t.file, version: t.version, docKey: t.doc_key, perms: t.perms, watermark: t.watermark, kit: "", exp: 0 }, u = await p(h, "HEAD", "", null);
145
+ if (u.status === 404) throw new a(404, "no such file");
146
+ if (!u.ok) throw new a(502, `the integrator answered ${u.status}`);
147
+ const r = u.headers.get(_) || t.file, m = B(r).toLowerCase(), v = e.kits.find((w) => w.extensions.includes(m));
148
+ if (!v) throw new a(415, `no kit opens ${m || "this file"}`);
149
+ const U = { ...h, version: u.headers.get(E) ?? t.version, kit: v.id, exp: l() + (e.sessionTtl ?? 8 * 3600) }, x = N(16).toString("base64"), k = X({ session: Y(e.secret, U), claims: U, kitUrl: new URL(v.path, e.kitsUrl).href, embedModule: "/assets/embed/index.js", nonce: x, title: r });
150
+ s.writeHead(200, {
151
+ "content-type": "text/html; charset=utf-8",
152
+ "cache-control": "no-store",
153
+ "referrer-policy": "no-referrer",
154
+ "x-content-type-options": "nosniff",
155
+ "content-security-policy": [
156
+ "default-src 'none'",
157
+ `script-src 'self' 'nonce-${x}' 'wasm-unsafe-eval'`,
158
+ `style-src 'nonce-${x}'`,
159
+ "connect-src 'self'",
160
+ `frame-src ${n}`,
161
+ "img-src 'self' data: blob:",
162
+ "base-uri 'none'",
163
+ "form-action 'none'",
164
+ `frame-ancestors ${i.origins.length ? i.origins.join(" ") : "'none'"}`
165
+ ].join("; ")
166
+ }), s.end(k);
167
+ }
168
+ async function $(o, s, t) {
169
+ const i = /^Bearer (\S+)$/.exec(o.headers.authorization ?? "")?.[1], h = Q(e.secret, i, l());
170
+ if (!h) throw new a(401, "no session");
171
+ const u = h.perms.edit || h.perms.comment;
172
+ if (o.method === "GET" && t === "/v1/file") {
173
+ const r = await p(h, "GET", "", null);
174
+ if (!r.ok) throw new a(r.status === 404 ? 404 : 502, `the integrator answered ${r.status}`);
175
+ s.writeHead(200, { "content-type": "application/octet-stream", "cache-control": "no-store", [E]: r.headers.get(E) ?? "" }), s.end(Buffer.from(await r.arrayBuffer()));
176
+ return;
177
+ }
178
+ if (o.method === "GET" && t === "/v1/file/stamp") {
179
+ const r = await p(h, "HEAD", "", null);
180
+ if (r.status === 404) return j(s, 200, { version: null });
181
+ if (!r.ok) throw new a(502, `the integrator answered ${r.status}`);
182
+ return j(s, 200, { version: r.headers.get(E) });
183
+ }
184
+ if (o.method === "PUT" && (t === "/v1/file" || t === "/v1/replica")) {
185
+ if (!u) throw new a(403, "this session is read-only");
186
+ const r = await ee(o, f), m = t === "/v1/replica", v = {}, U = o.headers["if-match"];
187
+ !m && typeof U == "string" && (v["if-match"] = U);
188
+ const x = o.headers["x-kition-file-stamp"];
189
+ m && typeof x == "string" && (v["x-kition-file-stamp"] = x);
190
+ const k = await p(h, "PUT", m ? "/replica" : "", r, v);
191
+ if (k.status === 409) {
192
+ s.writeHead(409, { [E]: k.headers.get(E) ?? "" }), s.end();
193
+ return;
194
+ }
195
+ if (m && k.status === 501) throw new a(501, "the integrator keeps no replicas");
196
+ if (!k.ok) throw new a(502, `the integrator answered ${k.status}`);
197
+ const w = m ? {} : await k.json().catch(() => ({}));
198
+ if (!m && typeof w.version != "string") throw new a(502, "the integrator answered no version");
199
+ return j(s, 200, m ? {} : { version: w.version });
200
+ }
201
+ if (o.method === "GET" && t === "/v1/replica") {
202
+ const r = await p(h, "GET", "/replica", null);
203
+ if (r.status === 404 || r.status === 501) throw new a(r.status, "no replica");
204
+ if (!r.ok) throw new a(502, `the integrator answered ${r.status}`);
205
+ s.writeHead(200, { "content-type": "application/octet-stream", "cache-control": "no-store", "x-kition-file-stamp": r.headers.get("x-kition-file-stamp") ?? "" }), s.end(Buffer.from(await r.arrayBuffer()));
206
+ return;
207
+ }
208
+ throw new a(404, "not found");
209
+ }
210
+ async function y(o, s) {
211
+ const t = I(decodeURIComponent(o.slice(14)));
212
+ if (t.startsWith("..") || t.includes(`${S}..`) || t.startsWith(S)) throw new a(404, "not found");
213
+ const i = await O(T(b, t)).catch(() => null);
214
+ if (!i) throw new a(404, "not found");
215
+ s.writeHead(200, { "content-type": Z[B(t)] ?? "application/octet-stream", "x-content-type-options": "nosniff", "cache-control": "public, max-age=300" }), s.end(i);
216
+ }
217
+ return async function(s, t) {
218
+ try {
219
+ const i = new URL(s.url ?? "/", "http://service");
220
+ if (s.method === "GET" && i.pathname === "/v1/open") return await g(i.searchParams.get("token"), t);
221
+ if (i.pathname.startsWith("/v1/")) return await $(s, t, i.pathname);
222
+ if (s.method === "GET" && i.pathname.startsWith("/assets/embed/")) return await y(i.pathname, t);
223
+ throw new a(404, "not found");
224
+ } catch (i) {
225
+ const h = i instanceof a ? i.status : 500;
226
+ i instanceof a || console.error("[kition-embed-server]", i), t.headersSent ? t.end() : j(t, h, { error: i instanceof a ? i.message : "failed" });
227
+ }
228
+ };
229
+ }
230
+ function j(e, n, c) {
231
+ e.writeHead(n, { "content-type": "application/json", "cache-control": "no-store" }), e.end(JSON.stringify(c));
232
+ }
233
+ const P = (e, n, c) => new Promise((l, f) => {
234
+ e.once("error", f), e.listen(n, c, () => l(e.address().port));
235
+ });
236
+ async function le(e) {
237
+ const n = e.host ?? "127.0.0.1";
238
+ let c = null, l = e.kitsUrl;
239
+ if (e.kitsDir) {
240
+ const d = e.kitsDir;
241
+ c = H(async (g, $) => {
242
+ const y = I(decodeURIComponent(new URL(g.url ?? "/", "http://kits").pathname)).replace(/^[/\\]+/, ""), o = y.startsWith("..") || y.includes(`${S}..`) ? null : await O(T(d, y.endsWith(S) || !y ? T(y, "index.html") : y)).catch(() => null);
243
+ if (!o) {
244
+ $.writeHead(404).end();
245
+ return;
246
+ }
247
+ const s = { ".html": "text/html; charset=utf-8", ".js": "text/javascript", ".mjs": "text/javascript", ".css": "text/css", ".wasm": "application/wasm", ".json": "application/json" }[B(y) || ".html"];
248
+ $.writeHead(200, { "content-type": s ?? "application/octet-stream", "x-content-type-options": "nosniff" }), $.end(o);
249
+ });
250
+ const p = await P(c, e.kitsPort ?? 0, n);
251
+ l ??= `http://${n}:${p}/`;
252
+ }
253
+ if (!l) throw new Error("kitsUrl or kitsDir is needed");
254
+ const f = H(te({ ...e, kitsUrl: l })), b = await P(f, e.port ?? 0, n);
255
+ return {
256
+ url: `http://${n}:${b}`,
257
+ kitsUrl: l,
258
+ close: async () => {
259
+ await new Promise((d) => f.close(d)), c && await new Promise((d) => c.close(d));
260
+ }
261
+ };
262
+ }
263
+ export {
264
+ he as CALLBACK_SKEW_SECS,
265
+ z as callbackHeaders,
266
+ te as createEmbedServer,
267
+ Y as makeSession,
268
+ Q as readSession,
269
+ pe as signCallback,
270
+ le as startEmbedServer,
271
+ me as verifyCallback
272
+ };