@plitzi/sdk-server 0.38.23 → 0.39.0

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 (233) hide show
  1. package/CHANGELOG.md +172 -1
  2. package/README.md +55 -9
  3. package/dist/actions.d.ts +4 -1
  4. package/dist/actions.js +3 -2
  5. package/dist/auth.d.ts +2 -2
  6. package/dist/auth.js +5 -4
  7. package/dist/core/auth/api/accounts.d.ts +11 -0
  8. package/dist/core/auth/api/accounts.js +175 -0
  9. package/dist/core/auth/api/admin.d.ts +41 -0
  10. package/dist/core/auth/api/admin.js +146 -0
  11. package/dist/core/auth/api/context.d.ts +119 -0
  12. package/dist/core/auth/api/context.js +262 -0
  13. package/dist/core/auth/api/factors.d.ts +31 -0
  14. package/dist/core/auth/api/factors.js +209 -0
  15. package/dist/core/auth/api/index.d.ts +94 -0
  16. package/dist/core/auth/api/index.js +43 -0
  17. package/dist/core/auth/api/profile.d.ts +43 -0
  18. package/dist/core/auth/api/profile.js +243 -0
  19. package/dist/core/auth/api/session.d.ts +62 -0
  20. package/dist/core/auth/api/session.js +312 -0
  21. package/dist/core/auth/{api.d.ts → api/types.d.ts} +41 -195
  22. package/dist/core/auth/api/values.d.ts +67 -0
  23. package/dist/core/auth/api/values.js +90 -0
  24. package/dist/core/auth/createAuth.d.ts +49 -39
  25. package/dist/core/auth/createAuth.js +21 -1
  26. package/dist/core/auth/oauth/client.d.ts +2 -2
  27. package/dist/core/auth/oauth/client.js +6 -1
  28. package/dist/core/auth/oauth/index.d.ts +30 -7
  29. package/dist/core/auth/oauth/index.js +33 -14
  30. package/dist/core/auth/oauth/providers/github.js +1 -1
  31. package/dist/core/auth/oauth/providers/google.js +1 -1
  32. package/dist/core/auth/oauth/providers/oidc.d.ts +40 -0
  33. package/dist/core/auth/oauth/providers/oidc.js +197 -0
  34. package/dist/core/auth/oauth/state.d.ts +2 -0
  35. package/dist/core/auth/oauth/state.js +2 -0
  36. package/dist/core/auth/oauth/types.d.ts +21 -3
  37. package/dist/core/auth/oauth/types.js +4 -1
  38. package/dist/core/auth/routes.js +1 -1
  39. package/dist/core/auth/tokens.d.ts +3 -1
  40. package/dist/core/auth/tokens.js +2 -1
  41. package/dist/core/configSeam.d.ts +0 -4
  42. package/dist/core/configSeam.js +12 -1
  43. package/dist/core/createServer.js +16 -4
  44. package/dist/core/freePort.d.ts +10 -0
  45. package/dist/core/freePort.js +39 -1
  46. package/dist/core/handlers/socialRouteHandlers.d.ts +9 -1
  47. package/dist/core/handlers/socialRouteHandlers.js +46 -12
  48. package/dist/core/health.d.ts +1 -0
  49. package/dist/core/health.js +2 -1
  50. package/dist/core/http/stages/authRoutes.d.ts +6 -0
  51. package/dist/core/http/stages/authRoutes.js +42 -1
  52. package/dist/core/http/stages/images.js +5 -3
  53. package/dist/core/http/stages/livePages.js +1 -1
  54. package/dist/core/server/fleet/relay.js +9 -1
  55. package/dist/core/server/fleet/stores.d.ts +7 -0
  56. package/dist/core/server/fleet/stores.js +15 -2
  57. package/dist/core/server/pageServer.js +5 -1
  58. package/dist/core/services/realtime.js +4 -2
  59. package/dist/core/services/registry.js +7 -1
  60. package/dist/core/services/robotsTxt.d.ts +16 -0
  61. package/dist/core/services/robotsTxt.js +50 -0
  62. package/dist/core/services/servedSpace.d.ts +8 -0
  63. package/dist/core/services/servedSpace.js +16 -0
  64. package/dist/core/services/sitemapXml.d.ts +7 -0
  65. package/dist/core/services/sitemapXml.js +37 -0
  66. package/dist/functions-api.d.ts +75 -2
  67. package/dist/helpers/fileStore.d.ts +60 -0
  68. package/dist/helpers/fileStore.js +127 -0
  69. package/dist/helpers/outboundGuard.d.ts +32 -3
  70. package/dist/helpers/outboundGuard.js +50 -5
  71. package/dist/hosts/project/agentRuns.d.ts +24 -0
  72. package/dist/hosts/project/agentRuns.js +107 -0
  73. package/dist/hosts/project/laterAsks.d.ts +41 -0
  74. package/dist/hosts/project/laterAsks.js +117 -0
  75. package/dist/hosts/project/plugins.js +11 -4
  76. package/dist/hosts/project/serveProject.d.ts +11 -3
  77. package/dist/hosts/project/serveProject.js +94 -36
  78. package/dist/hosts/project/spaceFailure.d.ts +4 -0
  79. package/dist/hosts/project/spaceFailure.js +10 -0
  80. package/dist/hosts/project/watchSpace.d.ts +5 -5
  81. package/dist/hosts/project/watchSpace.js +15 -6
  82. package/dist/index.js +2 -2
  83. package/dist/kernel.js +1 -1
  84. package/dist/modules/actions/connectorWrite.js +3 -1
  85. package/dist/modules/actions/index.js +1 -1
  86. package/dist/modules/actions/jobs/fileQueue.d.ts +34 -0
  87. package/dist/modules/actions/jobs/fileQueue.js +72 -0
  88. package/dist/modules/actions/jobs/index.js +3 -3
  89. package/dist/modules/actions/jobs/memoryQueue.js +6 -215
  90. package/dist/modules/actions/jobs/queueDocument.d.ts +14 -0
  91. package/dist/modules/actions/jobs/queueDocument.js +22 -0
  92. package/dist/modules/actions/jobs/queueRules.d.ts +39 -0
  93. package/dist/modules/actions/jobs/queueRules.js +270 -0
  94. package/dist/modules/actions/moduleFor.js +2 -0
  95. package/dist/modules/actions/runtime/fileKv.d.ts +3 -1
  96. package/dist/modules/actions/runtime/fileKv.js +12 -98
  97. package/dist/modules/actions/runtime/guards.d.ts +19 -1
  98. package/dist/modules/actions/runtime/guards.js +10 -1
  99. package/dist/modules/actions/runtime/kvChange.d.ts +40 -0
  100. package/dist/modules/actions/runtime/kvChange.js +23 -1
  101. package/dist/modules/actions/runtime/kvList.d.ts +4 -0
  102. package/dist/modules/actions/runtime/kvList.js +5 -1
  103. package/dist/modules/actions/runtime/kvStore.js +15 -1
  104. package/dist/modules/actions/runtime/namespaceKv.js +2 -0
  105. package/dist/modules/actions/runtime/renderResolver.js +31 -5
  106. package/dist/modules/actions/runtime/runAction.js +16 -6
  107. package/dist/modules/actions/runtime/scope.js +11 -0
  108. package/dist/modules/actions/runtime/taskContext.js +11 -32
  109. package/dist/modules/actions/tasks/auth.js +2 -0
  110. package/dist/modules/actions/tasks/connector.js +6 -2
  111. package/dist/modules/actions/tasks/flow.js +5 -0
  112. package/dist/modules/actions/tasks/http.js +2 -1
  113. package/dist/modules/actions/tasks/kv.js +5 -0
  114. package/dist/modules/actions/tasks/list.js +11 -2
  115. package/dist/modules/actions/tasks/transform.js +2 -0
  116. package/dist/modules/actions/transport/callHandler.js +25 -4
  117. package/dist/modules/actions/types.d.ts +46 -1
  118. package/dist/modules/connectors/engine.d.ts +7 -2
  119. package/dist/modules/connectors/engine.js +4 -4
  120. package/dist/modules/connectors/resolver.d.ts +2 -1
  121. package/dist/modules/connectors/resolver.js +3 -2
  122. package/dist/modules/functions/build.d.ts +7 -2
  123. package/dist/modules/functions/build.js +90 -6
  124. package/dist/modules/functions/capabilities.js +21 -0
  125. package/dist/modules/functions/context.js +6 -2
  126. package/dist/modules/functions/contract.d.ts +45 -1
  127. package/dist/modules/functions/driver.d.ts +2 -1
  128. package/dist/modules/functions/driver.js +39 -0
  129. package/dist/modules/functions/load.d.ts +1 -1
  130. package/dist/modules/functions/load.js +2 -2
  131. package/dist/modules/functions/manifest.js +7 -1
  132. package/dist/modules/functions/oauth.d.ts +3 -1
  133. package/dist/modules/functions/oauth.js +20 -7
  134. package/dist/modules/functions/protocol.d.ts +9 -0
  135. package/dist/modules/functions/sandbox/guest/bytes.d.ts +27 -0
  136. package/dist/modules/functions/sandbox/guest/bytes.js +108 -0
  137. package/dist/modules/functions/sandbox/guest/crypto.d.ts +47 -0
  138. package/dist/modules/functions/sandbox/guest/crypto.js +144 -0
  139. package/dist/modules/functions/sandbox/guest/events.d.ts +152 -0
  140. package/dist/modules/functions/sandbox/guest/events.js +143 -0
  141. package/dist/modules/functions/sandbox/guest/fetch.d.ts +305 -0
  142. package/dist/modules/functions/sandbox/guest/fetch.js +237 -0
  143. package/dist/modules/functions/sandbox/guest/index.d.ts +28 -0
  144. package/dist/modules/functions/sandbox/guest/index.js +130 -0
  145. package/dist/modules/functions/sandbox/{guest.d.ts → guest/types.d.ts} +5 -12
  146. package/dist/modules/functions/sandbox/isolate.js +20 -6
  147. package/dist/modules/functions/space.js +2 -8
  148. package/dist/modules/images/imageProxy.d.ts +3 -2
  149. package/dist/modules/images/imageProxy.js +2 -2
  150. package/dist/modules/mongo/jobQueue.js +0 -19
  151. package/dist/modules/mysql/accounts.js +12 -5
  152. package/dist/modules/mysql/jobs/index.d.ts +1 -1
  153. package/dist/modules/mysql/jobs/schema.d.ts +0 -6
  154. package/dist/modules/mysql/jobs/schema.js +2 -46
  155. package/dist/modules/mysql/schema.js +26 -1
  156. package/dist/modules/mysql/sessions.js +2 -1
  157. package/dist/modules/realtime/handlers.d.ts +9 -4
  158. package/dist/modules/realtime/handlers.js +20 -3
  159. package/dist/modules/realtime/hub.d.ts +4 -2
  160. package/dist/modules/realtime/hub.js +36 -4
  161. package/dist/modules/realtime/index.d.ts +14 -0
  162. package/dist/modules/realtime/index.js +14 -1
  163. package/dist/modules/realtime/presence.d.ts +33 -0
  164. package/dist/modules/realtime/presence.js +56 -0
  165. package/dist/modules/realtime/socket.d.ts +3 -1
  166. package/dist/modules/realtime/socket.js +2 -2
  167. package/dist/modules/rsc/connectorRscData.d.ts +6 -1
  168. package/dist/modules/rsc/connectorRscData.js +27 -6
  169. package/dist/modules/rsc/externalQueryReader.d.ts +28 -0
  170. package/dist/modules/rsc/externalQueryReader.js +43 -0
  171. package/dist/modules/rsc/handler.js +1 -1
  172. package/dist/modules/rsc/resolveRscData.d.ts +11 -1
  173. package/dist/modules/rsc/resolveRscData.js +86 -16
  174. package/dist/modules/runtime/inside.js +1 -1
  175. package/dist/modules/runtime/runner.js +2 -1
  176. package/dist/modules/sqlite/jobQueue.d.ts +35 -0
  177. package/dist/modules/sqlite/jobQueue.js +56 -0
  178. package/dist/modules/sqlite/kv.d.ts +1 -1
  179. package/dist/modules/sqlite/kv.js +2 -10
  180. package/dist/modules/sqlite/open.d.ts +3 -0
  181. package/dist/modules/sqlite/open.js +13 -0
  182. package/dist/modules/ssr/Component.d.ts +5 -1
  183. package/dist/modules/ssr/Component.js +3 -1
  184. package/dist/modules/ssr/applySSRResult.d.ts +8 -1
  185. package/dist/modules/ssr/applySSRResult.js +23 -3
  186. package/dist/modules/ssr/handler.js +30 -5
  187. package/dist/modules/ssr/prepareRender.js +27 -5
  188. package/dist/modules/ssr/preview.d.ts +2 -0
  189. package/dist/modules/ssr/preview.js +6 -3
  190. package/dist/modules/ssr/registerExternalPlugins.d.ts +2 -6
  191. package/dist/modules/ssr/registerExternalPlugins.js +2 -2
  192. package/dist/modules/ssr/resolvePageSeo.d.ts +11 -0
  193. package/dist/modules/ssr/resolvePageSeo.js +13 -2
  194. package/dist/modules/ssr/streamBody.js +2 -2
  195. package/dist/modules/ssr/views/template.ejs +21 -17
  196. package/dist/mysql.d.ts +1 -1
  197. package/dist/mysql.js +2 -2
  198. package/dist/plugins/manager.d.ts +5 -0
  199. package/dist/plugins/manager.js +9 -0
  200. package/dist/sqlite.d.ts +8 -1
  201. package/dist/sqlite.js +3 -1
  202. package/package.json +5 -5
  203. package/skills/plitzi-authoring/CHEATSHEET.md +6 -6
  204. package/skills/plitzi-authoring/SKILL.md +4 -2
  205. package/skills/plitzi-authoring/recipes/accordion.ts +2 -1
  206. package/skills/plitzi-authoring/recipes/carousel.ts +24 -3
  207. package/skills/plitzi-authoring/recipes/detail-page.ts +0 -1
  208. package/skills/plitzi-authoring/recipes/embed-and-svg.ts +3 -1
  209. package/skills/plitzi-authoring/recipes/filter-a-list.ts +0 -1
  210. package/skills/plitzi-authoring/recipes/show-data.ts +0 -1
  211. package/skills/plitzi-authoring/recipes/typed-data.ts +0 -1
  212. package/skills/plitzi-authoring/reference/accessibility.md +7 -2
  213. package/skills/plitzi-authoring/reference/auth.md +15 -4
  214. package/skills/plitzi-authoring/reference/authoring-errors.md +51 -10
  215. package/skills/plitzi-authoring/reference/colours-and-motion.md +7 -1
  216. package/skills/plitzi-authoring/reference/components.md +4 -1
  217. package/skills/plitzi-authoring/reference/data-and-visibility.md +31 -22
  218. package/skills/plitzi-authoring/reference/efficiency.md +2 -1
  219. package/skills/plitzi-authoring/reference/elements-and-styles.md +16 -15
  220. package/skills/plitzi-authoring/reference/flows.md +20 -8
  221. package/skills/plitzi-authoring/reference/kept-state.md +8 -0
  222. package/skills/plitzi-authoring/reference/layouts.md +18 -2
  223. package/skills/plitzi-authoring/reference/lists.md +20 -4
  224. package/skills/plitzi-authoring/reference/plugin-reach.md +12 -0
  225. package/skills/plitzi-authoring/reference/plugins.md +13 -2
  226. package/skills/plitzi-authoring/reference/realtime.md +8 -5
  227. package/skills/plitzi-authoring/reference/structure.md +17 -1
  228. package/skills/plitzi-authoring/reference/templates.md +11 -3
  229. package/skills/plitzi-authoring/reference/testing.md +82 -5
  230. package/skills/plitzi-authoring/reference/typed-sources.md +6 -0
  231. package/skills/plitzi-authoring/reference/validation.md +1 -1
  232. package/dist/core/auth/api.js +0 -1323
  233. package/dist/modules/functions/sandbox/guest.js +0 -662
@@ -0,0 +1,144 @@
1
+ //#region src/modules/functions/sandbox/guest/crypto.ts
2
+ /**
3
+ * `crypto`: random values from the runner's CSPRNG, and a `crypto.subtle` of digests, HMAC and PBKDF2 the runner computes.
4
+ *
5
+ * Printed into the isolate on its own, as the guest is (`isolate.ts`): it may use nothing but its parameters.
6
+ */
7
+ var guestCrypto = (host, { isRecord, bytesToBase64, base64ToBytes, copyBytes }) => {
8
+ const getRandomValues = (array) => {
9
+ if (array.byteLength > 65536) throw new DOMException("At most 65536 bytes at a time", "QuotaExceededError");
10
+ new Uint8Array(array.buffer, array.byteOffset, array.byteLength).set(host.random(array.byteLength));
11
+ return array;
12
+ };
13
+ const randomUUID = () => {
14
+ const bytes = getRandomValues(/* @__PURE__ */ new Uint8Array(16));
15
+ bytes[6] = bytes[6] & 15 | 64;
16
+ bytes[8] = bytes[8] & 63 | 128;
17
+ const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");
18
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
19
+ };
20
+ const HASHES = [
21
+ "SHA-1",
22
+ "SHA-256",
23
+ "SHA-384",
24
+ "SHA-512"
25
+ ];
26
+ const nameOf = (algorithm) => {
27
+ const name = typeof algorithm === "string" ? algorithm : isRecord(algorithm) ? algorithm.name : void 0;
28
+ return typeof name === "string" ? name.toUpperCase() : "";
29
+ };
30
+ const hashOf = (algorithm) => {
31
+ const upper = nameOf(algorithm);
32
+ if (!HASHES.includes(upper)) throw new DOMException(`${upper || String(algorithm)} is not a supported hash (${HASHES.join(", ")})`, "NotSupportedError");
33
+ return upper;
34
+ };
35
+ /** An HMAC key's length when its algorithm names none: the hash's block, as Web Crypto has it. */
36
+ const blockBitsOf = (hash) => hash === "SHA-384" || hash === "SHA-512" ? 1024 : 512;
37
+ /**
38
+ * The two kinds of key this runtime's `crypto.subtle` makes: HMAC, what signing a webhook needs, and PBKDF2, a
39
+ * password to derive from — what keeping a password needs.
40
+ */
41
+ class GuestCryptoKey {
42
+ type = "secret";
43
+ algorithm;
44
+ extractable;
45
+ usages;
46
+ raw;
47
+ constructor(raw, algorithm, extractable, usages) {
48
+ this.raw = raw;
49
+ this.algorithm = algorithm;
50
+ this.extractable = extractable;
51
+ this.usages = usages;
52
+ }
53
+ }
54
+ const bytesOf = (data) => {
55
+ if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) return copyBytes(data);
56
+ throw new TypeError("crypto.subtle takes bytes: an ArrayBuffer or a typed array");
57
+ };
58
+ const cryptoCall = async (message) => {
59
+ const answer = JSON.parse(await host.crypto(JSON.stringify(message)));
60
+ if (!isRecord(answer) || answer.ok !== true || typeof answer.value !== "string") throw new DOMException(isRecord(answer) && typeof answer.error === "string" ? answer.error : "The operation failed", "OperationError");
61
+ return base64ToBytes(answer.value);
62
+ };
63
+ const isKeyOf = (key, name) => key instanceof GuestCryptoKey && key.algorithm.name === name;
64
+ /** The key an operation was handed, when it is of the kind the operation takes and was imported for it. */
65
+ const keyFor = (key, name, usage) => {
66
+ if (!isKeyOf(key, name)) throw new DOMException(`${usage} takes a ${name} key, from crypto.subtle.importKey`, "InvalidAccessError");
67
+ if (!key.usages.includes(usage)) throw new DOMException(`The key was not imported for ${usage}`, "InvalidAccessError");
68
+ return key;
69
+ };
70
+ const hmacOf = (key, usage, data) => {
71
+ const { algorithm, raw } = keyFor(key, "HMAC", usage);
72
+ return cryptoCall({
73
+ op: "hmac",
74
+ hash: algorithm.hash.name,
75
+ key: bytesToBase64(raw),
76
+ data: bytesToBase64(bytesOf(data))
77
+ });
78
+ };
79
+ /** How many rounds and bits a derivation may take is the runner's to say: it is the one doing the work. */
80
+ const pbkdf2Of = (algorithm, baseKey, bits, usage) => {
81
+ if (!isRecord(algorithm) || nameOf(algorithm) !== "PBKDF2") throw new DOMException("This runtime derives with PBKDF2 only", "NotSupportedError");
82
+ const { raw } = keyFor(baseKey, "PBKDF2", usage);
83
+ if (typeof algorithm.iterations !== "number" || typeof bits !== "number") throw new TypeError("PBKDF2 takes { name, hash, salt, iterations } and a length in bits");
84
+ return cryptoCall({
85
+ op: "pbkdf2",
86
+ hash: hashOf(algorithm.hash),
87
+ key: bytesToBase64(raw),
88
+ salt: bytesToBase64(bytesOf(algorithm.salt)),
89
+ iterations: String(algorithm.iterations),
90
+ bits: String(bits)
91
+ });
92
+ };
93
+ /** The key `importKey` makes — thrown, not answered, when it is none this runtime has. */
94
+ const importedKey = (format, keyData, algorithm, extractable, usages) => {
95
+ const name = nameOf(algorithm);
96
+ if (format === "raw" && name === "HMAC" && isRecord(algorithm)) return new GuestCryptoKey(bytesOf(keyData), {
97
+ name,
98
+ hash: { name: hashOf(algorithm.hash) }
99
+ }, extractable, usages);
100
+ if (format === "raw" && name === "PBKDF2") {
101
+ if (extractable) throw new DOMException("A PBKDF2 key is never extractable", "SyntaxError");
102
+ return new GuestCryptoKey(bytesOf(keyData), { name }, false, usages);
103
+ }
104
+ throw new DOMException("This runtime imports raw keys for HMAC and PBKDF2 only (format \"raw\", { name: \"HMAC\", hash } or \"PBKDF2\")", "NotSupportedError");
105
+ };
106
+ return {
107
+ getRandomValues,
108
+ randomUUID,
109
+ subtle: {
110
+ digest: async (algorithm, data) => (await cryptoCall({
111
+ op: "digest",
112
+ hash: hashOf(algorithm),
113
+ data: bytesToBase64(bytesOf(data))
114
+ })).buffer,
115
+ importKey: (format, keyData, algorithm, extractable, usages) => new Promise((resolve) => {
116
+ resolve(importedKey(format, keyData, algorithm, extractable, usages));
117
+ }),
118
+ sign: async (_algorithm, key, data) => (await hmacOf(key, "sign", data)).buffer,
119
+ verify: async (_algorithm, key, signature, data) => {
120
+ const expected = await hmacOf(key, "verify", data);
121
+ const given = bytesOf(signature);
122
+ let difference = expected.length ^ given.length;
123
+ expected.forEach((byte, index) => {
124
+ difference |= byte ^ (given[index] ?? 0);
125
+ });
126
+ return difference === 0;
127
+ },
128
+ deriveBits: async (algorithm, baseKey, length) => (await pbkdf2Of(algorithm, baseKey, length, "deriveBits")).buffer,
129
+ /** Derives an HMAC key — the one other kind this runtime has. */
130
+ deriveKey: async (algorithm, baseKey, derivedKeyAlgorithm, extractable, usages) => {
131
+ if (!isRecord(derivedKeyAlgorithm) || nameOf(derivedKeyAlgorithm) !== "HMAC") throw new DOMException("This runtime derives HMAC keys only", "NotSupportedError");
132
+ const hash = hashOf(derivedKeyAlgorithm.hash);
133
+ const length = derivedKeyAlgorithm.length ?? blockBitsOf(hash);
134
+ const raw = await pbkdf2Of(algorithm, baseKey, length, "deriveKey");
135
+ return new GuestCryptoKey(raw, {
136
+ name: "HMAC",
137
+ hash: { name: hash }
138
+ }, extractable, usages);
139
+ }
140
+ }
141
+ };
142
+ };
143
+ //#endregion
144
+ export { guestCrypto };
@@ -0,0 +1,152 @@
1
+ import { GuestHost } from './types';
2
+ /**
3
+ * Events, abort and timers: `Event`, `EventTarget`, `AbortSignal`, `AbortController` and the timers, on the runner's clock.
4
+ *
5
+ * Printed into the isolate on its own, as the guest is (`isolate.ts`): it may use nothing but its parameters.
6
+ */
7
+ export declare const guestEvents: (host: Pick<GuestHost, "sleep">) => {
8
+ GuestEvent: {
9
+ new (type: string): {
10
+ readonly type: string;
11
+ };
12
+ };
13
+ GuestEventTarget: {
14
+ new (): {
15
+ addEventListener(type: string, listener: ((event: {
16
+ readonly type: string;
17
+ }) => void) | null, options?: boolean | {
18
+ once?: boolean;
19
+ }): void;
20
+ removeEventListener(type: string, listener: ((event: {
21
+ readonly type: string;
22
+ }) => void) | null): void;
23
+ dispatchEvent(event: {
24
+ readonly type: string;
25
+ }): boolean;
26
+ };
27
+ };
28
+ GuestAbortSignal: {
29
+ new (token?: symbol): {
30
+ onabort: ((event: {
31
+ readonly type: string;
32
+ }) => void) | null;
33
+ get aborted(): boolean;
34
+ get reason(): unknown;
35
+ throwIfAborted(): void;
36
+ addEventListener(type: string, listener: ((event: {
37
+ readonly type: string;
38
+ }) => void) | null, options?: boolean | {
39
+ once?: boolean;
40
+ }): void;
41
+ removeEventListener(type: string, listener: ((event: {
42
+ readonly type: string;
43
+ }) => void) | null): void;
44
+ dispatchEvent(event: {
45
+ readonly type: string;
46
+ }): boolean;
47
+ };
48
+ abort(reason?: unknown): {
49
+ onabort: ((event: {
50
+ readonly type: string;
51
+ }) => void) | null;
52
+ get aborted(): boolean;
53
+ get reason(): unknown;
54
+ throwIfAborted(): void;
55
+ addEventListener(type: string, listener: ((event: {
56
+ readonly type: string;
57
+ }) => void) | null, options?: boolean | {
58
+ once?: boolean;
59
+ }): void;
60
+ removeEventListener(type: string, listener: ((event: {
61
+ readonly type: string;
62
+ }) => void) | null): void;
63
+ dispatchEvent(event: {
64
+ readonly type: string;
65
+ }): boolean;
66
+ };
67
+ timeout(ms: number): {
68
+ onabort: ((event: {
69
+ readonly type: string;
70
+ }) => void) | null;
71
+ get aborted(): boolean;
72
+ get reason(): unknown;
73
+ throwIfAborted(): void;
74
+ addEventListener(type: string, listener: ((event: {
75
+ readonly type: string;
76
+ }) => void) | null, options?: boolean | {
77
+ once?: boolean;
78
+ }): void;
79
+ removeEventListener(type: string, listener: ((event: {
80
+ readonly type: string;
81
+ }) => void) | null): void;
82
+ dispatchEvent(event: {
83
+ readonly type: string;
84
+ }): boolean;
85
+ };
86
+ any(sources: {
87
+ onabort: ((event: {
88
+ readonly type: string;
89
+ }) => void) | null;
90
+ get aborted(): boolean;
91
+ get reason(): unknown;
92
+ throwIfAborted(): void;
93
+ addEventListener(type: string, listener: ((event: {
94
+ readonly type: string;
95
+ }) => void) | null, options?: boolean | {
96
+ once?: boolean;
97
+ }): void;
98
+ removeEventListener(type: string, listener: ((event: {
99
+ readonly type: string;
100
+ }) => void) | null): void;
101
+ dispatchEvent(event: {
102
+ readonly type: string;
103
+ }): boolean;
104
+ }[]): {
105
+ onabort: ((event: {
106
+ readonly type: string;
107
+ }) => void) | null;
108
+ get aborted(): boolean;
109
+ get reason(): unknown;
110
+ throwIfAborted(): void;
111
+ addEventListener(type: string, listener: ((event: {
112
+ readonly type: string;
113
+ }) => void) | null, options?: boolean | {
114
+ once?: boolean;
115
+ }): void;
116
+ removeEventListener(type: string, listener: ((event: {
117
+ readonly type: string;
118
+ }) => void) | null): void;
119
+ dispatchEvent(event: {
120
+ readonly type: string;
121
+ }): boolean;
122
+ };
123
+ };
124
+ GuestAbortController: {
125
+ new (): {
126
+ readonly signal: {
127
+ onabort: ((event: {
128
+ readonly type: string;
129
+ }) => void) | null;
130
+ get aborted(): boolean;
131
+ get reason(): unknown;
132
+ throwIfAborted(): void;
133
+ addEventListener(type: string, listener: ((event: {
134
+ readonly type: string;
135
+ }) => void) | null, options?: boolean | {
136
+ once?: boolean;
137
+ }): void;
138
+ removeEventListener(type: string, listener: ((event: {
139
+ readonly type: string;
140
+ }) => void) | null): void;
141
+ dispatchEvent(event: {
142
+ readonly type: string;
143
+ }): boolean;
144
+ };
145
+ abort(reason?: unknown): void;
146
+ };
147
+ };
148
+ guestSetTimeout: (callback: (...args: unknown[]) => void, ms?: number, ...args: unknown[]) => number;
149
+ guestSetInterval: (callback: (...args: unknown[]) => void, ms?: number, ...args: unknown[]) => number;
150
+ clearTimer: (id?: number) => void;
151
+ };
152
+ export type GuestEvents = ReturnType<typeof guestEvents>;
@@ -0,0 +1,143 @@
1
+ //#region src/modules/functions/sandbox/guest/events.ts
2
+ /**
3
+ * Events, abort and timers: `Event`, `EventTarget`, `AbortSignal`, `AbortController` and the timers, on the runner's clock.
4
+ *
5
+ * Printed into the isolate on its own, as the guest is (`isolate.ts`): it may use nothing but its parameters.
6
+ */
7
+ var guestEvents = (host) => {
8
+ class GuestEvent {
9
+ type;
10
+ constructor(type) {
11
+ this.type = type;
12
+ }
13
+ }
14
+ /**
15
+ * Each target's listeners, kept out of the object the code holds — a WeakMap rather than a private field, so the
16
+ * part's type can be named outside it (`GuestEvents`).
17
+ */
18
+ const listenersOf = /* @__PURE__ */ new WeakMap();
19
+ const listenersFor = (target) => {
20
+ const listeners = listenersOf.get(target) ?? /* @__PURE__ */ new Map();
21
+ listenersOf.set(target, listeners);
22
+ return listeners;
23
+ };
24
+ class GuestEventTarget {
25
+ addEventListener(type, listener, options) {
26
+ if (!listener) return;
27
+ const listeners = listenersFor(this).get(type) ?? /* @__PURE__ */ new Map();
28
+ listeners.set(listener, { once: typeof options === "object" && options.once === true });
29
+ listenersFor(this).set(type, listeners);
30
+ }
31
+ removeEventListener(type, listener) {
32
+ if (listener) listenersFor(this).get(type)?.delete(listener);
33
+ }
34
+ dispatchEvent(event) {
35
+ const listeners = listenersFor(this).get(event.type);
36
+ [...listeners ?? /* @__PURE__ */ new Map()].forEach(([listener, { once }]) => {
37
+ if (once) listeners?.delete(listener);
38
+ listener.call(this, event);
39
+ });
40
+ return true;
41
+ }
42
+ }
43
+ const signals = /* @__PURE__ */ new WeakMap();
44
+ const CONSTRUCT = Symbol("AbortSignal");
45
+ class GuestAbortSignal extends GuestEventTarget {
46
+ onabort = null;
47
+ constructor(token) {
48
+ super();
49
+ if (token !== CONSTRUCT) throw new TypeError("Illegal constructor: an AbortSignal comes from an AbortController");
50
+ signals.set(this, {
51
+ aborted: false,
52
+ reason: void 0
53
+ });
54
+ }
55
+ get aborted() {
56
+ return signals.get(this)?.aborted ?? false;
57
+ }
58
+ get reason() {
59
+ return signals.get(this)?.reason;
60
+ }
61
+ throwIfAborted() {
62
+ if (this.aborted) throw this.reason;
63
+ }
64
+ static abort(reason) {
65
+ const signal = new GuestAbortSignal(CONSTRUCT);
66
+ abortSignal(signal, reason);
67
+ return signal;
68
+ }
69
+ static timeout(ms) {
70
+ const signal = new GuestAbortSignal(CONSTRUCT);
71
+ guestSetTimeout(() => {
72
+ abortSignal(signal, new DOMException("The operation timed out.", "TimeoutError"));
73
+ }, ms);
74
+ return signal;
75
+ }
76
+ static any(sources) {
77
+ const signal = new GuestAbortSignal(CONSTRUCT);
78
+ const aborted = sources.find((source) => source.aborted);
79
+ if (aborted) {
80
+ abortSignal(signal, aborted.reason);
81
+ return signal;
82
+ }
83
+ sources.forEach((source) => {
84
+ source.addEventListener("abort", () => abortSignal(signal, source.reason), { once: true });
85
+ });
86
+ return signal;
87
+ }
88
+ }
89
+ const abortSignal = (signal, reason) => {
90
+ const state = signals.get(signal);
91
+ if (!state || state.aborted) return;
92
+ state.aborted = true;
93
+ state.reason = reason === void 0 ? new DOMException("This operation was aborted", "AbortError") : reason;
94
+ const event = new GuestEvent("abort");
95
+ signal.onabort?.call(signal, event);
96
+ signal.dispatchEvent(event);
97
+ };
98
+ class GuestAbortController {
99
+ signal = new GuestAbortSignal(CONSTRUCT);
100
+ abort(reason) {
101
+ abortSignal(this.signal, reason);
102
+ }
103
+ }
104
+ const timers = /* @__PURE__ */ new Set();
105
+ let nextTimer = 1;
106
+ const delayOf = (ms) => Math.max(0, Number(ms) || 0);
107
+ const guestSetTimeout = (callback, ms, ...args) => {
108
+ const id = nextTimer++;
109
+ timers.add(id);
110
+ host.sleep(delayOf(ms)).then(() => {
111
+ if (timers.delete(id)) callback(...args);
112
+ });
113
+ return id;
114
+ };
115
+ const guestSetInterval = (callback, ms, ...args) => {
116
+ const id = nextTimer++;
117
+ timers.add(id);
118
+ const tick = () => {
119
+ host.sleep(Math.max(1, delayOf(ms))).then(() => {
120
+ if (timers.has(id)) {
121
+ callback(...args);
122
+ tick();
123
+ }
124
+ });
125
+ };
126
+ tick();
127
+ return id;
128
+ };
129
+ const clearTimer = (id) => {
130
+ if (id !== void 0) timers.delete(id);
131
+ };
132
+ return {
133
+ GuestEvent,
134
+ GuestEventTarget,
135
+ GuestAbortSignal,
136
+ GuestAbortController,
137
+ guestSetTimeout,
138
+ guestSetInterval,
139
+ clearTimer
140
+ };
141
+ };
142
+ //#endregion
143
+ export { guestEvents };