@elaraai/e3-web 0.0.0-stage → 1.0.85

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 (303) hide show
  1. package/LICENSE.md +54 -0
  2. package/README.md +395 -2
  3. package/dist/src/app.d.ts +91 -0
  4. package/dist/src/app.d.ts.map +1 -0
  5. package/dist/src/app.js +112 -0
  6. package/dist/src/app.js.map +1 -0
  7. package/dist/src/bridge/bridge.spec.d.ts +6 -0
  8. package/dist/src/bridge/bridge.spec.d.ts.map +1 -0
  9. package/dist/src/bridge/bridge.spec.js +56 -0
  10. package/dist/src/bridge/bridge.spec.js.map +1 -0
  11. package/dist/src/bridge/page.d.ts +149 -0
  12. package/dist/src/bridge/page.d.ts.map +1 -0
  13. package/dist/src/bridge/page.js +328 -0
  14. package/dist/src/bridge/page.js.map +1 -0
  15. package/dist/src/bridge/protocol.d.ts +181 -0
  16. package/dist/src/bridge/protocol.d.ts.map +1 -0
  17. package/dist/src/bridge/protocol.js +113 -0
  18. package/dist/src/bridge/protocol.js.map +1 -0
  19. package/dist/src/bridge/worker.d.ts +124 -0
  20. package/dist/src/bridge/worker.d.ts.map +1 -0
  21. package/dist/src/bridge/worker.js +185 -0
  22. package/dist/src/bridge/worker.js.map +1 -0
  23. package/dist/src/browser/bridge.page.d.ts +12 -0
  24. package/dist/src/browser/bridge.page.d.ts.map +1 -0
  25. package/dist/src/browser/bridge.page.js +79 -0
  26. package/dist/src/browser/bridge.page.js.map +1 -0
  27. package/dist/src/browser/bridge.spec.d.ts +6 -0
  28. package/dist/src/browser/bridge.spec.d.ts.map +1 -0
  29. package/dist/src/browser/bridge.spec.js +105 -0
  30. package/dist/src/browser/bridge.spec.js.map +1 -0
  31. package/dist/src/browser/bridge.worker.d.ts +6 -0
  32. package/dist/src/browser/bridge.worker.d.ts.map +1 -0
  33. package/dist/src/browser/bridge.worker.js +35 -0
  34. package/dist/src/browser/bridge.worker.js.map +1 -0
  35. package/dist/src/browser/compliance.d.ts +68 -0
  36. package/dist/src/browser/compliance.d.ts.map +1 -0
  37. package/dist/src/browser/compliance.js +30 -0
  38. package/dist/src/browser/compliance.js.map +1 -0
  39. package/dist/src/browser/compliance.page.d.ts +6 -0
  40. package/dist/src/browser/compliance.page.d.ts.map +1 -0
  41. package/dist/src/browser/compliance.page.js +204 -0
  42. package/dist/src/browser/compliance.page.js.map +1 -0
  43. package/dist/src/browser/compliance.spec.d.ts +6 -0
  44. package/dist/src/browser/compliance.spec.d.ts.map +1 -0
  45. package/dist/src/browser/compliance.spec.js +147 -0
  46. package/dist/src/browser/compliance.spec.js.map +1 -0
  47. package/dist/src/browser/e3-api-1.spec.d.ts +6 -0
  48. package/dist/src/browser/e3-api-1.spec.d.ts.map +1 -0
  49. package/dist/src/browser/e3-api-1.spec.js +12 -0
  50. package/dist/src/browser/e3-api-1.spec.js.map +1 -0
  51. package/dist/src/browser/e3-api-2.spec.d.ts +6 -0
  52. package/dist/src/browser/e3-api-2.spec.d.ts.map +1 -0
  53. package/dist/src/browser/e3-api-2.spec.js +12 -0
  54. package/dist/src/browser/e3-api-2.spec.js.map +1 -0
  55. package/dist/src/browser/e3-api-3.spec.d.ts +6 -0
  56. package/dist/src/browser/e3-api-3.spec.d.ts.map +1 -0
  57. package/dist/src/browser/e3-api-3.spec.js +12 -0
  58. package/dist/src/browser/e3-api-3.spec.js.map +1 -0
  59. package/dist/src/browser/e3-api-parts.spec.d.ts +6 -0
  60. package/dist/src/browser/e3-api-parts.spec.d.ts.map +1 -0
  61. package/dist/src/browser/e3-api-parts.spec.js +45 -0
  62. package/dist/src/browser/e3-api-parts.spec.js.map +1 -0
  63. package/dist/src/browser/e3-api.d.ts +28 -0
  64. package/dist/src/browser/e3-api.d.ts.map +1 -0
  65. package/dist/src/browser/e3-api.js +144 -0
  66. package/dist/src/browser/e3-api.js.map +1 -0
  67. package/dist/src/browser/e3-client.worker.d.ts +6 -0
  68. package/dist/src/browser/e3-client.worker.d.ts.map +1 -0
  69. package/dist/src/browser/e3-client.worker.js +19 -0
  70. package/dist/src/browser/e3-client.worker.js.map +1 -0
  71. package/dist/src/browser/e3-forward.d.ts +27 -0
  72. package/dist/src/browser/e3-forward.d.ts.map +1 -0
  73. package/dist/src/browser/e3-forward.js +81 -0
  74. package/dist/src/browser/e3-forward.js.map +1 -0
  75. package/dist/src/browser/e3-forward.spec.d.ts +6 -0
  76. package/dist/src/browser/e3-forward.spec.d.ts.map +1 -0
  77. package/dist/src/browser/e3-forward.spec.js +84 -0
  78. package/dist/src/browser/e3-forward.spec.js.map +1 -0
  79. package/dist/src/browser/e3-port.worker.d.ts +6 -0
  80. package/dist/src/browser/e3-port.worker.d.ts.map +1 -0
  81. package/dist/src/browser/e3-port.worker.js +21 -0
  82. package/dist/src/browser/e3-port.worker.js.map +1 -0
  83. package/dist/src/browser/e3-unit.worker.d.ts +6 -0
  84. package/dist/src/browser/e3-unit.worker.d.ts.map +1 -0
  85. package/dist/src/browser/e3-unit.worker.js +18 -0
  86. package/dist/src/browser/e3-unit.worker.js.map +1 -0
  87. package/dist/src/browser/e3-wire.d.ts +43 -0
  88. package/dist/src/browser/e3-wire.d.ts.map +1 -0
  89. package/dist/src/browser/e3-wire.js +32 -0
  90. package/dist/src/browser/e3-wire.js.map +1 -0
  91. package/dist/src/browser/e3.page.d.ts +21 -0
  92. package/dist/src/browser/e3.page.d.ts.map +1 -0
  93. package/dist/src/browser/e3.page.js +119 -0
  94. package/dist/src/browser/e3.page.js.map +1 -0
  95. package/dist/src/browser/e3.spec.d.ts +6 -0
  96. package/dist/src/browser/e3.spec.d.ts.map +1 -0
  97. package/dist/src/browser/e3.spec.js +358 -0
  98. package/dist/src/browser/e3.spec.js.map +1 -0
  99. package/dist/src/browser/e3.worker.d.ts +6 -0
  100. package/dist/src/browser/e3.worker.d.ts.map +1 -0
  101. package/dist/src/browser/e3.worker.js +38 -0
  102. package/dist/src/browser/e3.worker.js.map +1 -0
  103. package/dist/src/browser/harness.d.ts +175 -0
  104. package/dist/src/browser/harness.d.ts.map +1 -0
  105. package/dist/src/browser/harness.js +375 -0
  106. package/dist/src/browser/harness.js.map +1 -0
  107. package/dist/src/browser/page.d.ts +47 -0
  108. package/dist/src/browser/page.d.ts.map +1 -0
  109. package/dist/src/browser/page.js +47 -0
  110. package/dist/src/browser/page.js.map +1 -0
  111. package/dist/src/browser/repository.page.d.ts +85 -0
  112. package/dist/src/browser/repository.page.d.ts.map +1 -0
  113. package/dist/src/browser/repository.page.js +271 -0
  114. package/dist/src/browser/repository.page.js.map +1 -0
  115. package/dist/src/browser/runner.page.d.ts +6 -0
  116. package/dist/src/browser/runner.page.d.ts.map +1 -0
  117. package/dist/src/browser/runner.page.js +62 -0
  118. package/dist/src/browser/runner.page.js.map +1 -0
  119. package/dist/src/browser/runner.spec.d.ts +6 -0
  120. package/dist/src/browser/runner.spec.d.ts.map +1 -0
  121. package/dist/src/browser/runner.spec.js +46 -0
  122. package/dist/src/browser/runner.spec.js.map +1 -0
  123. package/dist/src/browser/storage.page.d.ts +6 -0
  124. package/dist/src/browser/storage.page.d.ts.map +1 -0
  125. package/dist/src/browser/storage.page.js +289 -0
  126. package/dist/src/browser/storage.page.js.map +1 -0
  127. package/dist/src/browser/storage.spec.d.ts +6 -0
  128. package/dist/src/browser/storage.spec.d.ts.map +1 -0
  129. package/dist/src/browser/storage.spec.js +211 -0
  130. package/dist/src/browser/storage.spec.js.map +1 -0
  131. package/dist/src/browser/unit.worker.d.ts +6 -0
  132. package/dist/src/browser/unit.worker.d.ts.map +1 -0
  133. package/dist/src/browser/unit.worker.js +17 -0
  134. package/dist/src/browser/unit.worker.js.map +1 -0
  135. package/dist/src/execution/WebTaskRunner.d.ts +159 -0
  136. package/dist/src/execution/WebTaskRunner.d.ts.map +1 -0
  137. package/dist/src/execution/WebTaskRunner.js +581 -0
  138. package/dist/src/execution/WebTaskRunner.js.map +1 -0
  139. package/dist/src/execution/WebTaskRunner.spec.d.ts +6 -0
  140. package/dist/src/execution/WebTaskRunner.spec.d.ts.map +1 -0
  141. package/dist/src/execution/WebTaskRunner.spec.js +91 -0
  142. package/dist/src/execution/WebTaskRunner.spec.js.map +1 -0
  143. package/dist/src/execution/e3-platform.d.ts +41 -0
  144. package/dist/src/execution/e3-platform.d.ts.map +1 -0
  145. package/dist/src/execution/e3-platform.js +52 -0
  146. package/dist/src/execution/e3-platform.js.map +1 -0
  147. package/dist/src/execution/in-process.d.ts +32 -0
  148. package/dist/src/execution/in-process.d.ts.map +1 -0
  149. package/dist/src/execution/in-process.js +89 -0
  150. package/dist/src/execution/in-process.js.map +1 -0
  151. package/dist/src/execution/pool.d.ts +172 -0
  152. package/dist/src/execution/pool.d.ts.map +1 -0
  153. package/dist/src/execution/pool.js +352 -0
  154. package/dist/src/execution/pool.js.map +1 -0
  155. package/dist/src/execution/pool.spec.d.ts +6 -0
  156. package/dist/src/execution/pool.spec.d.ts.map +1 -0
  157. package/dist/src/execution/pool.spec.js +252 -0
  158. package/dist/src/execution/pool.spec.js.map +1 -0
  159. package/dist/src/execution/protocol.d.ts +108 -0
  160. package/dist/src/execution/protocol.d.ts.map +1 -0
  161. package/dist/src/execution/protocol.js +27 -0
  162. package/dist/src/execution/protocol.js.map +1 -0
  163. package/dist/src/execution/unit-server.d.ts +131 -0
  164. package/dist/src/execution/unit-server.d.ts.map +1 -0
  165. package/dist/src/execution/unit-server.js +142 -0
  166. package/dist/src/execution/unit-server.js.map +1 -0
  167. package/dist/src/index.d.ts +39 -0
  168. package/dist/src/index.d.ts.map +1 -0
  169. package/dist/src/index.js +37 -0
  170. package/dist/src/index.js.map +1 -0
  171. package/dist/src/index.spec.d.ts +6 -0
  172. package/dist/src/index.spec.d.ts.map +1 -0
  173. package/dist/src/index.spec.js +212 -0
  174. package/dist/src/index.spec.js.map +1 -0
  175. package/dist/src/node.d.ts +12 -0
  176. package/dist/src/node.d.ts.map +1 -0
  177. package/dist/src/node.js +12 -0
  178. package/dist/src/node.js.map +1 -0
  179. package/dist/src/storage/WebStateStore.d.ts +64 -0
  180. package/dist/src/storage/WebStateStore.d.ts.map +1 -0
  181. package/dist/src/storage/WebStateStore.js +237 -0
  182. package/dist/src/storage/WebStateStore.js.map +1 -0
  183. package/dist/src/storage/WebStorage.d.ts +225 -0
  184. package/dist/src/storage/WebStorage.d.ts.map +1 -0
  185. package/dist/src/storage/WebStorage.js +1462 -0
  186. package/dist/src/storage/WebStorage.js.map +1 -0
  187. package/dist/src/storage/adapters.d.ts +610 -0
  188. package/dist/src/storage/adapters.d.ts.map +1 -0
  189. package/dist/src/storage/adapters.js +246 -0
  190. package/dist/src/storage/adapters.js.map +1 -0
  191. package/dist/src/storage/indexeddb.d.ts +93 -0
  192. package/dist/src/storage/indexeddb.d.ts.map +1 -0
  193. package/dist/src/storage/indexeddb.js +384 -0
  194. package/dist/src/storage/indexeddb.js.map +1 -0
  195. package/dist/src/storage/indexeddb.spec.d.ts +6 -0
  196. package/dist/src/storage/indexeddb.spec.d.ts.map +1 -0
  197. package/dist/src/storage/indexeddb.spec.js +77 -0
  198. package/dist/src/storage/indexeddb.spec.js.map +1 -0
  199. package/dist/src/storage/memory.d.ts +275 -0
  200. package/dist/src/storage/memory.d.ts.map +1 -0
  201. package/dist/src/storage/memory.js +724 -0
  202. package/dist/src/storage/memory.js.map +1 -0
  203. package/dist/src/storage/memory.spec.d.ts +6 -0
  204. package/dist/src/storage/memory.spec.d.ts.map +1 -0
  205. package/dist/src/storage/memory.spec.js +71 -0
  206. package/dist/src/storage/memory.spec.js.map +1 -0
  207. package/dist/src/storage/node-files.d.ts +38 -0
  208. package/dist/src/storage/node-files.d.ts.map +1 -0
  209. package/dist/src/storage/node-files.js +148 -0
  210. package/dist/src/storage/node-files.js.map +1 -0
  211. package/dist/src/storage/node-files.spec.d.ts +6 -0
  212. package/dist/src/storage/node-files.spec.d.ts.map +1 -0
  213. package/dist/src/storage/node-files.spec.js +40 -0
  214. package/dist/src/storage/node-files.spec.js.map +1 -0
  215. package/dist/src/storage/opfs.d.ts +145 -0
  216. package/dist/src/storage/opfs.d.ts.map +1 -0
  217. package/dist/src/storage/opfs.js +514 -0
  218. package/dist/src/storage/opfs.js.map +1 -0
  219. package/dist/src/storage/opfs.spec.d.ts +6 -0
  220. package/dist/src/storage/opfs.spec.d.ts.map +1 -0
  221. package/dist/src/storage/opfs.spec.js +51 -0
  222. package/dist/src/storage/opfs.spec.js.map +1 -0
  223. package/dist/src/storage/record-keys.d.ts +78 -0
  224. package/dist/src/storage/record-keys.d.ts.map +1 -0
  225. package/dist/src/storage/record-keys.js +71 -0
  226. package/dist/src/storage/record-keys.js.map +1 -0
  227. package/dist/src/storage/stores.spec.d.ts +6 -0
  228. package/dist/src/storage/stores.spec.d.ts.map +1 -0
  229. package/dist/src/storage/stores.spec.js +326 -0
  230. package/dist/src/storage/stores.spec.js.map +1 -0
  231. package/dist/src/storage/web-locks.d.ts +103 -0
  232. package/dist/src/storage/web-locks.d.ts.map +1 -0
  233. package/dist/src/storage/web-locks.js +228 -0
  234. package/dist/src/storage/web-locks.js.map +1 -0
  235. package/dist/src/testing/adapter-contract.d.ts +116 -0
  236. package/dist/src/testing/adapter-contract.d.ts.map +1 -0
  237. package/dist/src/testing/adapter-contract.js +769 -0
  238. package/dist/src/testing/adapter-contract.js.map +1 -0
  239. package/dist/src/testing/assert.d.ts +75 -0
  240. package/dist/src/testing/assert.d.ts.map +1 -0
  241. package/dist/src/testing/assert.js +146 -0
  242. package/dist/src/testing/assert.js.map +1 -0
  243. package/dist/src/testing/assert.spec.d.ts +6 -0
  244. package/dist/src/testing/assert.spec.d.ts.map +1 -0
  245. package/dist/src/testing/assert.spec.js +62 -0
  246. package/dist/src/testing/assert.spec.js.map +1 -0
  247. package/dist/src/testing/bridge-contract.d.ts +55 -0
  248. package/dist/src/testing/bridge-contract.d.ts.map +1 -0
  249. package/dist/src/testing/bridge-contract.js +376 -0
  250. package/dist/src/testing/bridge-contract.js.map +1 -0
  251. package/dist/src/testing/bridge-handler.d.ts +35 -0
  252. package/dist/src/testing/bridge-handler.d.ts.map +1 -0
  253. package/dist/src/testing/bridge-handler.js +108 -0
  254. package/dist/src/testing/bridge-handler.js.map +1 -0
  255. package/dist/src/testing/callers.d.ts +29 -0
  256. package/dist/src/testing/callers.d.ts.map +1 -0
  257. package/dist/src/testing/callers.js +24 -0
  258. package/dist/src/testing/callers.js.map +1 -0
  259. package/dist/src/testing/e3-fixtures.d.ts +38 -0
  260. package/dist/src/testing/e3-fixtures.d.ts.map +1 -0
  261. package/dist/src/testing/e3-fixtures.js +126 -0
  262. package/dist/src/testing/e3-fixtures.js.map +1 -0
  263. package/dist/src/testing/hold-platform.d.ts +33 -0
  264. package/dist/src/testing/hold-platform.d.ts.map +1 -0
  265. package/dist/src/testing/hold-platform.js +41 -0
  266. package/dist/src/testing/hold-platform.js.map +1 -0
  267. package/dist/src/testing/runner-cases.d.ts +112 -0
  268. package/dist/src/testing/runner-cases.d.ts.map +1 -0
  269. package/dist/src/testing/runner-cases.js +959 -0
  270. package/dist/src/testing/runner-cases.js.map +1 -0
  271. package/dist/src/testing/runner-fixtures.d.ts +13 -0
  272. package/dist/src/testing/runner-fixtures.d.ts.map +1 -0
  273. package/dist/src/testing/runner-fixtures.js +173 -0
  274. package/dist/src/testing/runner-fixtures.js.map +1 -0
  275. package/dist/src/testing/test-platform.d.ts +73 -0
  276. package/dist/src/testing/test-platform.d.ts.map +1 -0
  277. package/dist/src/testing/test-platform.js +143 -0
  278. package/dist/src/testing/test-platform.js.map +1 -0
  279. package/dist/src/transfer/WebTransferBackend.d.ts +780 -0
  280. package/dist/src/transfer/WebTransferBackend.d.ts.map +1 -0
  281. package/dist/src/transfer/WebTransferBackend.js +1140 -0
  282. package/dist/src/transfer/WebTransferBackend.js.map +1 -0
  283. package/dist/src/transfer/WebTransferBackend.spec.d.ts +6 -0
  284. package/dist/src/transfer/WebTransferBackend.spec.d.ts.map +1 -0
  285. package/dist/src/transfer/WebTransferBackend.spec.js +582 -0
  286. package/dist/src/transfer/WebTransferBackend.spec.js.map +1 -0
  287. package/dist/src/transfer/endpoints.d.ts +46 -0
  288. package/dist/src/transfer/endpoints.d.ts.map +1 -0
  289. package/dist/src/transfer/endpoints.js +157 -0
  290. package/dist/src/transfer/endpoints.js.map +1 -0
  291. package/dist/src/units.d.ts +51 -0
  292. package/dist/src/units.d.ts.map +1 -0
  293. package/dist/src/units.js +74 -0
  294. package/dist/src/units.js.map +1 -0
  295. package/dist/src/worker.d.ts +126 -0
  296. package/dist/src/worker.d.ts.map +1 -0
  297. package/dist/src/worker.js +186 -0
  298. package/dist/src/worker.js.map +1 -0
  299. package/dist/src/worker.spec.d.ts +6 -0
  300. package/dist/src/worker.spec.d.ts.map +1 -0
  301. package/dist/src/worker.spec.js +195 -0
  302. package/dist/src/worker.spec.js.map +1 -0
  303. package/package.json +76 -3
@@ -0,0 +1,769 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Licensed under BSL 1.1. See LICENSE for details.
4
+ */
5
+ /**
6
+ * The adapters' contract: what every implementation of the four adapters
7
+ * does, whether it keeps what it holds in memory, in IndexedDB, in OPFS, with
8
+ * Web Locks or on the machine.
9
+ *
10
+ * A case is a plain function over a setup, asserting with `./assert.js`, so
11
+ * it runs where its adapters are: in Node, registered as a `node:test` test
12
+ * over the in-memory adapters and the machine's files, and in a page over
13
+ * IndexedDB, OPFS and Web Locks, where the page runs it by name and the
14
+ * Node-side spec reports it as a test of its own.
15
+ *
16
+ * @packageDocumentation
17
+ */
18
+ import { compareKeys, } from '../storage/adapters.js';
19
+ import { deepEqual, equal, ok, rejects, render, throws } from './assert.js';
20
+ /**
21
+ * Runs a case over a fresh setup, and undoes the setup after.
22
+ *
23
+ * @param adapterCase - The case
24
+ * @param setup - Makes its setup
25
+ * @throws What the case throws; or, when it passed, what undoing its setup
26
+ * threw
27
+ */
28
+ export async function runAdapterCase(adapterCase, setup) {
29
+ const cleanups = [];
30
+ let failure = null;
31
+ try {
32
+ await adapterCase.run(await setup((undo) => cleanups.push(undo)));
33
+ }
34
+ catch (error) {
35
+ failure = { error };
36
+ }
37
+ for (const undo of cleanups.reverse()) {
38
+ try {
39
+ await undo();
40
+ }
41
+ catch (error) {
42
+ failure ??= { error };
43
+ }
44
+ }
45
+ if (failure !== null)
46
+ throw failure.error;
47
+ }
48
+ /**
49
+ * Finds a case of a contract by its name.
50
+ *
51
+ * @param contract - The contract
52
+ * @param name - The case's name
53
+ * @returns The case
54
+ * @throws {Error} When the contract has no case of that name
55
+ */
56
+ export function caseNamed(contract, name) {
57
+ const found = contract.find((each) => each.name === name);
58
+ if (found === undefined)
59
+ throw new Error(`the contract has no case '${name}'`);
60
+ return found;
61
+ }
62
+ /**
63
+ * The read size every files setup makes its adapter with: 100,000 bytes.
64
+ *
65
+ * @remarks
66
+ * No browser's stream of a file chunks by it. Chromium hands an OPFS file's
67
+ * stream over in multiples of 64 KiB, or whole, as its reads find it, so the
68
+ * 300,000 bytes of the contract's large file come back in three chunks of no
69
+ * more than 100,000 bytes from an adapter that slices them, and never from one
70
+ * that yields what the stream yields.
71
+ */
72
+ export const CONTRACT_READ_CHUNK = 100_000;
73
+ // =============================================================================
74
+ // Helpers
75
+ // =============================================================================
76
+ const encoder = new TextEncoder();
77
+ const decoder = new TextDecoder();
78
+ /** The UTF-8 bytes of a text. */
79
+ function bytes(text) {
80
+ return encoder.encode(text);
81
+ }
82
+ /** The text UTF-8 bytes hold. */
83
+ function text(data) {
84
+ return decoder.decode(data);
85
+ }
86
+ /** A source that yields the texts' bytes, a chunk each. */
87
+ async function* chunks(...texts) {
88
+ for (const each of texts)
89
+ yield bytes(each);
90
+ }
91
+ /** A source that yields the texts' bytes, and then fails. */
92
+ async function* failing(...texts) {
93
+ for (const each of texts)
94
+ yield bytes(each);
95
+ throw new Error('the source failed');
96
+ }
97
+ /**
98
+ * A source that yields its first chunk, and the rest only once it is let
99
+ * through: a write in flight for as long as a case needs one.
100
+ */
101
+ function gated(first, ...rest) {
102
+ let open;
103
+ const opened = new Promise((resolve) => {
104
+ open = resolve;
105
+ });
106
+ let reached;
107
+ const waiting = new Promise((resolve) => {
108
+ reached = resolve;
109
+ });
110
+ async function* source() {
111
+ yield bytes(first);
112
+ reached();
113
+ await opened;
114
+ for (const each of rest)
115
+ yield bytes(each);
116
+ }
117
+ return { source: source(), waiting, open };
118
+ }
119
+ /** Writes records whose values are their keys' parts, joined by `/`. */
120
+ async function putAll(records, keys) {
121
+ await records.transact(async (tx) => {
122
+ for (const key of keys)
123
+ tx.put(key, bytes(key.join('/')));
124
+ });
125
+ }
126
+ /** Reads a file whole, and the size of each chunk it came in. */
127
+ async function readAll(files, path) {
128
+ const parts = [];
129
+ for await (const chunk of files.read(path))
130
+ parts.push(chunk);
131
+ const data = new Uint8Array(parts.reduce((size, part) => size + part.length, 0));
132
+ let offset = 0;
133
+ for (const part of parts) {
134
+ data.set(part, offset);
135
+ offset += part.length;
136
+ }
137
+ return { data, chunks: parts.map((part) => part.length) };
138
+ }
139
+ // =============================================================================
140
+ // Records
141
+ // =============================================================================
142
+ /**
143
+ * The records adapter's contract.
144
+ */
145
+ export const recordsContract = [
146
+ {
147
+ name: 'reads a record a transaction put, and nothing of a key never written',
148
+ run: async ({ open }) => {
149
+ const records = await open();
150
+ equal(await records.get(['a']), null);
151
+ deepEqual(await records.scan([]), []);
152
+ const answer = await records.transact(async (tx) => {
153
+ tx.put(['a'], bytes('one'));
154
+ return 'done';
155
+ });
156
+ equal(answer, 'done', 'the transaction answers what its work does');
157
+ deepEqual(await records.get(['a']), bytes('one'));
158
+ deepEqual(await records.scan([]), [{ key: ['a'], value: bytes('one') }]);
159
+ },
160
+ },
161
+ {
162
+ name: 'orders keys part by part, by UTF-16 code unit, and a key before the keys it is a prefix of',
163
+ run: async ({ open }) => {
164
+ const records = await open();
165
+ const ordered = [
166
+ ['A'], ['a'], ['a', ''], ['a', '\u0000'], ['a', 'Z'], ['a', 'b'], ['a', 'b', 'c'], ['ab'], ['b'], ['é'], ['😀'], ['￿'],
167
+ ];
168
+ const written = [5, 0, 11, 3, 8, 1, 10, 2, 7, 4, 9, 6].map((i) => ordered[i]);
169
+ await putAll(records, written);
170
+ deepEqual(await records.keys([]), ordered);
171
+ deepEqual((await records.scan([])).map(({ key }) => key), ordered);
172
+ deepEqual([...written].sort(compareKeys), ordered, 'compareKeys orders keys as the store does');
173
+ },
174
+ },
175
+ {
176
+ name: 'scans the records under a prefix, the prefix\'s own among them, and no others',
177
+ run: async ({ open }) => {
178
+ const records = await open();
179
+ await putAll(records, [['x'], ['x', '1'], ['x', '2', 'deep'], ['xy'], ['w'], ['x2']]);
180
+ deepEqual(await records.keys(['x']), [['x'], ['x', '1'], ['x', '2', 'deep']]);
181
+ deepEqual(await records.scan(['x', '2']), [{ key: ['x', '2', 'deep'], value: bytes('x/2/deep') }]);
182
+ deepEqual(await records.keys(['none']), []);
183
+ deepEqual(await records.keys(['x', '1', 'below']), []);
184
+ },
185
+ },
186
+ {
187
+ name: 'pages a scan by the keys it starts after and ends before, a number at a time, from either end',
188
+ run: async ({ open }) => {
189
+ const records = await open();
190
+ const digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
191
+ await putAll(records, [['o'], ...digits.map((digit) => ['p', digit]), ['q']]);
192
+ const page = async (options) => (await records.keys(['p'], options)).map((key) => key[1]);
193
+ deepEqual(await page({ limit: 3 }), ['0', '1', '2']);
194
+ deepEqual(await page({ after: ['p', '2'], limit: 3 }), ['3', '4', '5']);
195
+ deepEqual(await page({ after: ['p', '2', 'z'], limit: 1 }), ['3'], 'after a key that sorts between two');
196
+ deepEqual(await page({ before: ['p', '3'] }), ['0', '1', '2']);
197
+ deepEqual(await page({ after: ['p', '2'], before: ['p', '5'] }), ['3', '4']);
198
+ deepEqual(await page({ reverse: true, limit: 2 }), ['9', '8']);
199
+ deepEqual(await page({ reverse: true, before: ['p', '8'], limit: 2 }), ['7', '6']);
200
+ deepEqual(await page({ reverse: true, after: ['p', '7'] }), ['9', '8']);
201
+ deepEqual(await page({ after: ['p', '9'] }), []);
202
+ deepEqual(await page({ after: ['p', '5'], before: ['p', '5'] }), []);
203
+ deepEqual(await page({ after: ['p', '6'], before: ['p', '3'] }), [], 'a range that ends before it starts');
204
+ deepEqual(await page({ after: ['o'] }), digits, 'a bound outside the prefix bounds nothing');
205
+ deepEqual(await page({ before: ['q'] }), digits);
206
+ deepEqual((await records.scan(['p'], { reverse: true, limit: 1 })).map(({ key, value }) => [key, text(value)]), [[['p', '9'], 'p/9']]);
207
+ for (const limit of [0, -1, 1.5]) {
208
+ await rejects(() => records.keys(['p'], { limit }), { name: 'RangeError' }, `a limit of ${limit}`);
209
+ }
210
+ },
211
+ },
212
+ {
213
+ name: 'applies none of a transaction whose work throws, and fails with what it threw',
214
+ run: async ({ open }) => {
215
+ const records = await open();
216
+ await putAll(records, [['k'], ['k', 'child']]);
217
+ await rejects(() => records.transact(async (tx) => {
218
+ tx.put(['k'], bytes('changed'));
219
+ tx.put(['new'], bytes('new'));
220
+ tx.deletePrefix(['k']);
221
+ tx.put(['k', 'child', 'grandchild'], bytes('added'));
222
+ deepEqual(await tx.keys([]), [['k', 'child', 'grandchild'], ['new']], 'the transaction sees its own writes');
223
+ throw new Error('abandoned');
224
+ }), { message: /^abandoned$/ });
225
+ deepEqual(await records.scan([]), [{ key: ['k'], value: bytes('k') }, { key: ['k', 'child'], value: bytes('k/child') }]);
226
+ },
227
+ },
228
+ {
229
+ name: 'shows a transaction its own writes, and applies them together',
230
+ run: async ({ open }) => {
231
+ const records = await open();
232
+ const answer = await records.transact(async (tx) => {
233
+ tx.put(['t', '1'], bytes('one'));
234
+ deepEqual(await tx.get(['t', '1']), bytes('one'));
235
+ tx.delete(['t', '1']);
236
+ equal(await tx.get(['t', '1']), null);
237
+ tx.put(['t', '2'], bytes('two'));
238
+ tx.put(['t', '3'], bytes('three'));
239
+ tx.put(['u'], bytes('u'));
240
+ tx.deletePrefix(['t']);
241
+ deepEqual(await tx.keys(['t']), []);
242
+ tx.put(['t', '4'], bytes('four'));
243
+ deepEqual(await tx.scan(['t']), [{ key: ['t', '4'], value: bytes('four') }]);
244
+ deepEqual(await tx.keys([], { reverse: true }), [['u'], ['t', '4']]);
245
+ return 42;
246
+ });
247
+ equal(answer, 42);
248
+ deepEqual(await records.keys([]), [['t', '4'], ['u']]);
249
+ },
250
+ },
251
+ {
252
+ name: 'deletes a record, and every record under a prefix, and nothing else',
253
+ run: async ({ open }) => {
254
+ const records = await open();
255
+ await putAll(records, [['c'], ['d'], ['d', '1'], ['d', '1', 'x'], ['dd']]);
256
+ await records.transact(async (tx) => {
257
+ tx.delete(['c']);
258
+ tx.delete(['never', 'written']);
259
+ tx.deletePrefix(['d']);
260
+ tx.deletePrefix(['none']);
261
+ });
262
+ deepEqual(await records.keys([]), [['dd']]);
263
+ },
264
+ },
265
+ {
266
+ name: 'runs transactions one after another, so none loses another\'s write, through one adapter or two',
267
+ run: async ({ open }) => {
268
+ const [first, second] = [await open(), await open()];
269
+ const increment = (records) => records.transact(async (tx) => {
270
+ const current = await tx.get(['n']);
271
+ tx.put(['n'], bytes(`${current === null ? 1 : Number(text(current)) + 1}`));
272
+ });
273
+ await Promise.all(Array.from({ length: 24 }, (_, i) => increment(i % 2 === 0 ? first : second)));
274
+ deepEqual(await first.get(['n']), bytes('24'));
275
+ },
276
+ },
277
+ {
278
+ name: 'keeps its own copy of what it is given and what it gives, and only the bytes of a view',
279
+ run: async ({ open }) => {
280
+ const records = await open();
281
+ const given = new Uint8Array([1, 2, 3]);
282
+ await records.transact(async (tx) => {
283
+ tx.put(['given'], given);
284
+ });
285
+ given[0] = 9;
286
+ const read = (await records.get(['given']));
287
+ deepEqual(read, new Uint8Array([1, 2, 3]), 'changing what was put changes nothing stored');
288
+ read[1] = 9;
289
+ deepEqual(await records.get(['given']), new Uint8Array([1, 2, 3]), 'changing what was read changes nothing stored');
290
+ await records.transact(async (tx) => {
291
+ tx.put(['view'], new Uint8Array([0, 1, 2, 3, 4]).subarray(1, 3));
292
+ });
293
+ const view = (await records.get(['view']));
294
+ deepEqual(view, new Uint8Array([1, 2]));
295
+ equal(view.buffer.byteLength, 2, 'a view is kept as its bytes, not the whole buffer it is into');
296
+ },
297
+ },
298
+ {
299
+ name: 'refuses work that awaits anything but its own operations, and a transaction used after its work',
300
+ run: async ({ open }) => {
301
+ const records = await open();
302
+ await rejects(() => records.transact(async (tx) => {
303
+ await tx.get(['early']);
304
+ await new Promise((resolve) => setTimeout(resolve, 20));
305
+ tx.put(['late'], bytes('late'));
306
+ }), { name: 'RecordsTransactionError' }, 'an operation after a timer');
307
+ equal(await records.get(['late']), null, 'a write after the timer never applies');
308
+ await rejects(() => records.transact(async () => {
309
+ await new Promise((resolve) => setTimeout(resolve, 20));
310
+ }), { name: 'RecordsTransactionError' }, 'work that finishes after a timer');
311
+ let kept;
312
+ await records.transact(async (tx) => {
313
+ kept = tx;
314
+ });
315
+ throws(() => kept.put(['after'], bytes('after')), { name: 'RecordsTransactionError' }, 'a write after the work');
316
+ await rejects(() => kept.get(['after']), { name: 'RecordsTransactionError' }, 'a read after the work');
317
+ equal(await records.get(['after']), null);
318
+ await records.transact(async (tx) => {
319
+ tx.put(['after'], bytes('fine'));
320
+ });
321
+ deepEqual(await records.get(['after']), bytes('fine'), 'the adapter takes transactions still');
322
+ },
323
+ },
324
+ {
325
+ name: 'reads through another adapter over the same store what one wrote, and refuses a closed adapter',
326
+ run: async ({ open }) => {
327
+ const [first, second] = [await open(), await open()];
328
+ await first.transact(async (tx) => {
329
+ tx.put(['shared'], bytes('written by the first'));
330
+ });
331
+ deepEqual(await second.get(['shared']), bytes('written by the first'));
332
+ await first.close();
333
+ deepEqual(await second.get(['shared']), bytes('written by the first'), 'the records outlive the adapter that wrote them');
334
+ await rejects(() => first.get(['shared']), { name: 'AdapterClosedError' });
335
+ await rejects(() => first.keys([]), { name: 'AdapterClosedError' });
336
+ await rejects(() => first.transact(async () => undefined), { name: 'AdapterClosedError' });
337
+ deepEqual(await (await open()).keys([]), [['shared']], 'an adapter opened after reads them');
338
+ },
339
+ },
340
+ {
341
+ name: 'refuses a key that is not a list of strings',
342
+ run: async ({ open }) => {
343
+ const records = await open();
344
+ const bad = ['a', 1];
345
+ await rejects(() => records.get(bad), { name: 'TypeError' });
346
+ await rejects(() => records.scan(bad), { name: 'TypeError' });
347
+ await rejects(() => records.keys(['a'], { after: bad }), { name: 'TypeError' });
348
+ await rejects(() => records.get('a'), { name: 'TypeError' });
349
+ await rejects(() => records.transact(async (tx) => {
350
+ tx.put(bad, bytes('x'));
351
+ }), { name: 'TypeError' });
352
+ deepEqual(await records.keys([]), [], 'nothing was written');
353
+ },
354
+ },
355
+ ];
356
+ // =============================================================================
357
+ // Blobs
358
+ // =============================================================================
359
+ /**
360
+ * The blobs adapter's contract.
361
+ */
362
+ export const blobsContract = [
363
+ {
364
+ name: 'answers nothing for a blob never written',
365
+ run: async ({ blobs }) => {
366
+ equal(await blobs.read(['none']), null);
367
+ equal(await blobs.readRange(['none'], 0, 1), null);
368
+ equal(await blobs.stat(['none']), null);
369
+ equal(await blobs.delete(['none']), false);
370
+ equal(await blobs.read(['none', 'deeper']), null, 'nor under a key part no blob is under');
371
+ deepEqual(await blobs.list([]), []);
372
+ },
373
+ },
374
+ {
375
+ name: 'keeps a blob whole, with its size and when it was written',
376
+ run: async ({ blobs }) => {
377
+ const before = Date.now();
378
+ equal(await blobs.write(['a', 'b'], bytes('hello')), 5);
379
+ const after = Date.now();
380
+ deepEqual(await blobs.read(['a', 'b']), bytes('hello'));
381
+ const stat = await blobs.stat(['a', 'b']);
382
+ equal(stat?.size, 5);
383
+ ok(stat !== null && stat.lastModified >= before - 2000 && stat.lastModified <= after + 2000, `written at ${stat?.lastModified}, between ${before} and ${after}`);
384
+ },
385
+ },
386
+ {
387
+ name: 'writes a stream of chunks as one blob, and nothing as a blob of no bytes',
388
+ run: async ({ blobs }) => {
389
+ equal(await blobs.write(['s'], chunks('one, ', 'two, ', 'three')), 15);
390
+ deepEqual(await blobs.read(['s']), bytes('one, two, three'));
391
+ equal(await blobs.write(['empty'], new Uint8Array(0)), 0);
392
+ deepEqual(await blobs.read(['empty']), new Uint8Array(0));
393
+ equal((await blobs.stat(['empty']))?.size, 0);
394
+ equal(await blobs.write(['streamed-empty'], chunks()), 0);
395
+ deepEqual(await blobs.read(['streamed-empty']), new Uint8Array(0));
396
+ },
397
+ },
398
+ {
399
+ name: 'reads a range of a blob, short only at its end',
400
+ run: async ({ blobs }) => {
401
+ await blobs.write(['r'], bytes('0123456789'));
402
+ deepEqual(await blobs.readRange(['r'], 2, 3), bytes('234'));
403
+ deepEqual(await blobs.readRange(['r'], 7, 10), bytes('789'));
404
+ deepEqual(await blobs.readRange(['r'], 10, 5), new Uint8Array(0));
405
+ deepEqual(await blobs.readRange(['r'], 12, 5), new Uint8Array(0));
406
+ deepEqual(await blobs.readRange(['r'], 3, 0), new Uint8Array(0));
407
+ for (const [offset, length] of [[-1, 1], [0, -1], [1.5, 1], [0, 0.5]]) {
408
+ await rejects(() => blobs.readRange(['r'], offset, length), { name: 'RangeError' }, `a range of ${offset}, ${length}`);
409
+ }
410
+ },
411
+ },
412
+ {
413
+ name: 'replaces a blob whole',
414
+ run: async ({ blobs }) => {
415
+ await blobs.write(['w'], bytes('a long first blob'));
416
+ equal(await blobs.write(['w'], bytes('short')), 5);
417
+ deepEqual(await blobs.read(['w']), bytes('short'));
418
+ equal((await blobs.stat(['w']))?.size, 5);
419
+ deepEqual((await blobs.list([])).map(({ key }) => key), [['w']]);
420
+ },
421
+ },
422
+ {
423
+ name: 'deletes a blob, and says whether there was one',
424
+ run: async ({ blobs }) => {
425
+ await blobs.write(['gone'], bytes('x'));
426
+ await blobs.write(['kept'], bytes('y'));
427
+ equal(await blobs.delete(['gone']), true);
428
+ equal(await blobs.read(['gone']), null);
429
+ equal(await blobs.stat(['gone']), null);
430
+ equal(await blobs.delete(['gone']), false);
431
+ deepEqual(await blobs.read(['kept']), bytes('y'));
432
+ },
433
+ },
434
+ {
435
+ name: 'lists the blobs under a prefix in key order, the prefix\'s own among them, with their sizes',
436
+ run: async ({ blobs }) => {
437
+ for (const key of [['r', 'b'], ['r', 'a'], ['r', 'a', 'x'], ['r2'], ['r'], ['q']])
438
+ await blobs.write(key, bytes(key.join('/')));
439
+ const listed = async (prefix) => (await blobs.list(prefix)).map(({ key, size }) => [key, size]);
440
+ deepEqual(await listed(['r']), [[['r'], 1], [['r', 'a'], 3], [['r', 'a', 'x'], 5], [['r', 'b'], 3]]);
441
+ deepEqual(await listed(['r', 'a']), [[['r', 'a'], 3], [['r', 'a', 'x'], 5]]);
442
+ deepEqual(await listed([]), [[['q'], 1], [['r'], 1], [['r', 'a'], 3], [['r', 'a', 'x'], 5], [['r', 'b'], 3], [['r2'], 2]]);
443
+ deepEqual(await listed(['zz']), []);
444
+ deepEqual(await listed(['r', 'b', 'below']), []);
445
+ for (const { lastModified } of await blobs.list([]))
446
+ ok(Number.isFinite(lastModified), 'each lists when it was written');
447
+ },
448
+ },
449
+ {
450
+ name: 'keeps keys apart that differ only in case, and holds any character in a key',
451
+ run: async ({ blobs }) => {
452
+ const keys = [
453
+ ['Case'], ['case'], ['CASE'], ['a b'], ['%41'], ['A'], ['.'], ['..'], ['.hidden'], ['a/b'], ['a\\b'],
454
+ ['é'], ['日本'], ['~'], ['x~'], ['emoji 😀'], ['nul\u0000'], ['dir', 'Nested/Part'],
455
+ ];
456
+ for (const [i, key] of keys.entries())
457
+ await blobs.write(key, bytes(`${i}`));
458
+ for (const [i, key] of keys.entries())
459
+ deepEqual(await blobs.read(key), bytes(`${i}`), render(key));
460
+ deepEqual((await blobs.list([])).map(({ key }) => key), [...keys].sort(compareKeys));
461
+ },
462
+ },
463
+ {
464
+ name: 'refuses a key with no parts, or an empty part',
465
+ run: async ({ blobs }) => {
466
+ await rejects(() => blobs.write([], bytes('x')), { name: 'TypeError' });
467
+ await rejects(() => blobs.write(['a', ''], bytes('x')), { name: 'TypeError' });
468
+ await rejects(() => blobs.read(['']), { name: 'TypeError' });
469
+ await rejects(() => blobs.stat([]), { name: 'TypeError' });
470
+ await rejects(() => blobs.list(['a', '']), { name: 'TypeError' });
471
+ deepEqual(await blobs.list([]), [], 'nothing was written');
472
+ },
473
+ },
474
+ {
475
+ name: 'deletes every blob under a prefix, and no other',
476
+ run: async ({ blobs }) => {
477
+ for (const key of [['d'], ['d', '1'], ['d', '1', 'x'], ['dd'], ['c', 'd']])
478
+ await blobs.write(key, bytes('x'));
479
+ await blobs.deletePrefix(['d']);
480
+ deepEqual((await blobs.list([])).map(({ key }) => key), [['c', 'd'], ['dd']]);
481
+ await blobs.deletePrefix(['none']);
482
+ deepEqual((await blobs.list([])).map(({ key }) => key), [['c', 'd'], ['dd']]);
483
+ await blobs.deletePrefix([]);
484
+ deepEqual(await blobs.list([]), []);
485
+ },
486
+ },
487
+ {
488
+ name: 'leaves a blob as it was when a write of it fails, and no blob of a key it never finished',
489
+ run: async ({ blobs }) => {
490
+ await blobs.write(['f'], bytes('kept'));
491
+ await rejects(() => blobs.write(['f'], failing('partial')), { message: /the source failed/ });
492
+ deepEqual(await blobs.read(['f']), bytes('kept'));
493
+ await rejects(() => blobs.write(['g'], failing('partial')), { message: /the source failed/ });
494
+ equal(await blobs.read(['g']), null);
495
+ deepEqual((await blobs.list([])).map(({ key }) => key), [['f']]);
496
+ },
497
+ },
498
+ {
499
+ name: 'shows no blob before its write has finished',
500
+ run: async ({ blobs }) => {
501
+ await blobs.write(['replaced'], bytes('before'));
502
+ const fresh = gated('in ', 'flight');
503
+ const replacing = gated('af', 'ter');
504
+ const writing = [blobs.write(['fresh'], fresh.source), blobs.write(['replaced'], replacing.source)];
505
+ await Promise.all([fresh.waiting, replacing.waiting]);
506
+ equal(await blobs.read(['fresh']), null, 'a new blob is not there while its write is in flight');
507
+ equal(await blobs.stat(['fresh']), null);
508
+ deepEqual(await blobs.read(['replaced']), bytes('before'), 'a replaced blob is the old one while its write is in flight');
509
+ deepEqual((await blobs.list([])).map(({ key }) => key), [['replaced']]);
510
+ fresh.open();
511
+ replacing.open();
512
+ await Promise.all(writing);
513
+ deepEqual(await blobs.read(['fresh']), bytes('in flight'));
514
+ deepEqual(await blobs.read(['replaced']), bytes('after'));
515
+ },
516
+ },
517
+ {
518
+ name: 'keeps its own copy of what it is given and what it gives',
519
+ run: async ({ blobs }) => {
520
+ const given = new Uint8Array([1, 2, 3]);
521
+ await blobs.write(['given'], given);
522
+ given[0] = 9;
523
+ const read = (await blobs.read(['given']));
524
+ deepEqual(read, new Uint8Array([1, 2, 3]), 'changing what was written changes nothing stored');
525
+ read[1] = 9;
526
+ deepEqual(await blobs.read(['given']), new Uint8Array([1, 2, 3]), 'changing what was read changes nothing stored');
527
+ await blobs.write(['view'], new Uint8Array([0, 1, 2, 3, 4]).subarray(1, 3));
528
+ deepEqual(await blobs.read(['view']), new Uint8Array([1, 2]));
529
+ },
530
+ },
531
+ {
532
+ name: 'sweeps away no blob, and leaves a write in flight to finish',
533
+ run: async ({ blobs }) => {
534
+ await blobs.write(['kept'], bytes('kept'));
535
+ const flight = gated('in ', 'flight');
536
+ const writing = blobs.write(['inflight'], flight.source);
537
+ await flight.waiting;
538
+ equal((await blobs.sweep({ minAge: 60_000, dryRun: false })).deleted, 0, 'what a write in flight left is young');
539
+ flight.open();
540
+ equal(await writing, 9);
541
+ deepEqual(await blobs.read(['inflight']), bytes('in flight'));
542
+ equal((await blobs.sweep({ minAge: 0, dryRun: false })).deleted, 0, 'finished writes leave nothing behind');
543
+ deepEqual(await blobs.read(['kept']), bytes('kept'));
544
+ },
545
+ },
546
+ ];
547
+ // =============================================================================
548
+ // Locks
549
+ // =============================================================================
550
+ /**
551
+ * The locks adapter's contract.
552
+ */
553
+ export const locksContract = [
554
+ {
555
+ name: 'holds a name exclusively against every other request until it is released',
556
+ run: async ({ open }) => {
557
+ const locks = await open();
558
+ const held = await locks.acquire('r', 'exclusive');
559
+ ok(held !== null);
560
+ equal(await locks.acquire('r', 'exclusive'), null, 'a second exclusive request is refused');
561
+ equal(await locks.acquire('r', 'shared'), null, 'a shared request is refused');
562
+ deepEqual(await locks.held('r'), ['exclusive']);
563
+ const other = await locks.acquire('o', 'exclusive');
564
+ ok(other !== null, 'another name is held apart');
565
+ await other.release();
566
+ await held.release();
567
+ deepEqual(await locks.held('r'), []);
568
+ const again = await locks.acquire('r', 'exclusive');
569
+ ok(again !== null, 'a released name is taken again');
570
+ await again.release();
571
+ },
572
+ },
573
+ {
574
+ name: 'lets shared holds share a name, holding it against an exclusive request until the last is released, each once',
575
+ run: async ({ open }) => {
576
+ const locks = await open();
577
+ const first = await locks.acquire('r', 'shared');
578
+ const second = await locks.acquire('r', 'shared');
579
+ ok(first !== null && second !== null);
580
+ deepEqual(await locks.held('r'), ['shared', 'shared']);
581
+ equal(await locks.acquire('r', 'exclusive'), null);
582
+ await first.release();
583
+ await first.release();
584
+ deepEqual(await locks.held('r'), ['shared'], 'a hold released twice releases itself alone');
585
+ equal(await locks.acquire('r', 'exclusive'), null);
586
+ await second.release();
587
+ const exclusive = await locks.acquire('r', 'exclusive');
588
+ ok(exclusive !== null);
589
+ await exclusive.release();
590
+ },
591
+ },
592
+ {
593
+ name: 'grants a waiting request once the name is released, and refuses one whose wait runs out',
594
+ run: async ({ open }) => {
595
+ const locks = await open();
596
+ const held = (await locks.acquire('r', 'exclusive'));
597
+ const waiting = locks.acquire('r', 'exclusive', { wait: true });
598
+ equal(await locks.acquire('r', 'exclusive', { wait: true, timeout: 50 }), null, 'its wait ran out');
599
+ await held.release();
600
+ const granted = await waiting;
601
+ ok(granted !== null, 'the waiting request holds the name once it is released');
602
+ deepEqual(await locks.held('r'), ['exclusive']);
603
+ await granted.release();
604
+ deepEqual(await locks.held('r'), [], 'the request whose wait ran out never holds it');
605
+ const free = await locks.acquire('r', 'exclusive', { wait: true, timeout: 0 });
606
+ ok(free !== null, 'a free name is granted whatever the wait');
607
+ await free.release();
608
+ },
609
+ },
610
+ {
611
+ name: 'grants waiting requests in the order they were made, and refuses one that does not wait while one waits',
612
+ run: async ({ open }) => {
613
+ const locks = await open();
614
+ const shared = (await locks.acquire('r', 'shared'));
615
+ const granted = [];
616
+ const exclusive = locks.acquire('r', 'exclusive', { wait: true }).then((hold) => {
617
+ granted.push('exclusive');
618
+ return hold;
619
+ });
620
+ const later = locks.acquire('r', 'shared', { wait: true }).then((hold) => {
621
+ granted.push('shared');
622
+ return hold;
623
+ });
624
+ equal(await locks.acquire('r', 'shared'), null, 'refused while a request waits, though only shared holds hold the name');
625
+ await shared.release();
626
+ const first = (await exclusive);
627
+ deepEqual(await locks.held('r'), ['exclusive'], 'the exclusive request, made first, is granted first');
628
+ await first.release();
629
+ const second = (await later);
630
+ deepEqual(granted, ['exclusive', 'shared']);
631
+ await second.release();
632
+ },
633
+ },
634
+ {
635
+ name: 'keeps two sessions apart as two tabs, and frees what a closed session held',
636
+ run: async ({ open }) => {
637
+ const [first, second] = [await open(), await open()];
638
+ ok(first.session !== second.session, 'each session has an id of its own');
639
+ equal(await first.isAlive(first.session), true, 'a session is alive to itself');
640
+ equal(await first.isAlive(second.session), true);
641
+ equal(await second.isAlive(first.session), true);
642
+ const held = await first.acquire('ws', 'exclusive');
643
+ ok(held !== null);
644
+ equal(await second.acquire('ws', 'exclusive'), null, 'another session is refused');
645
+ equal(await second.acquire('ws', 'shared'), null);
646
+ deepEqual(await second.held('ws'), ['exclusive'], 'a session sees what another holds');
647
+ const waiting = second.acquire('ws', 'exclusive', { wait: true });
648
+ await first.close();
649
+ equal(await second.isAlive(first.session), false, 'a closed session is not alive');
650
+ const taken = await waiting;
651
+ ok(taken !== null, 'what the closed session held is free to the other');
652
+ await held.release();
653
+ deepEqual(await second.held('ws'), ['exclusive'], 'releasing a hold of a closed session releases nothing more');
654
+ await taken.release();
655
+ deepEqual(await second.held('ws'), []);
656
+ },
657
+ },
658
+ {
659
+ name: 'answers that a session no adapter opened is not alive',
660
+ run: async ({ open }) => {
661
+ const locks = await open();
662
+ equal(await locks.isAlive('00000000-0000-4000-8000-000000000000'), false);
663
+ },
664
+ },
665
+ {
666
+ name: 'refuses the requests a session has waiting when it closes, and every request after',
667
+ run: async ({ open }) => {
668
+ const [first, second] = [await open(), await open()];
669
+ const held = (await first.acquire('r', 'exclusive'));
670
+ const waiting = second.acquire('r', 'exclusive', { wait: true });
671
+ await second.close();
672
+ equal(await waiting, null, 'the waiting request is refused');
673
+ await rejects(() => second.acquire('r', 'exclusive'), { name: 'AdapterClosedError' });
674
+ await rejects(() => second.held('r'), { name: 'AdapterClosedError' });
675
+ await rejects(() => second.isAlive(first.session), { name: 'AdapterClosedError' });
676
+ await second.close();
677
+ await held.release();
678
+ deepEqual(await first.held('r'), [], 'the refused request never took the name');
679
+ },
680
+ },
681
+ {
682
+ name: 'refuses a mode that is neither exclusive nor shared, and a wait that is not a whole number of milliseconds',
683
+ run: async ({ open }) => {
684
+ const locks = await open();
685
+ await rejects(() => locks.acquire('r', 'both'), { name: 'TypeError' });
686
+ for (const timeout of [-1, 1.5, Number.POSITIVE_INFINITY]) {
687
+ await rejects(() => locks.acquire('r', 'exclusive', { wait: true, timeout }), { name: 'RangeError' }, `a wait of ${timeout}`);
688
+ }
689
+ deepEqual(await locks.held('r'), [], 'no refused request took the name');
690
+ },
691
+ },
692
+ ];
693
+ // =============================================================================
694
+ // Files
695
+ // =============================================================================
696
+ /**
697
+ * The files adapter's contract.
698
+ */
699
+ export const filesContract = [
700
+ {
701
+ name: 'writes a file whole, and reads it back in slices of its read size, with its size',
702
+ run: async ({ files, dir, join }) => {
703
+ const file = join(dir, 'delivery');
704
+ equal(await files.write(file, bytes('a delivery')), 10);
705
+ deepEqual((await readAll(files, file)).data, bytes('a delivery'));
706
+ equal((await files.stat(file))?.size, 10);
707
+ const large = new Uint8Array(300_000).map((_, i) => i % 251);
708
+ equal(await files.write(file, (async function* () {
709
+ for (let offset = 0; offset < large.length; offset += 75_000)
710
+ yield large.subarray(offset, offset + 75_000);
711
+ })()), 300_000);
712
+ const back = await readAll(files, file);
713
+ deepEqual(back.data, large);
714
+ equal(back.chunks.length, Math.ceil(large.length / CONTRACT_READ_CHUNK), `a large file is read in the slices its read size gives: ${render(back.chunks)}`);
715
+ ok(back.chunks.every((size) => size <= CONTRACT_READ_CHUNK), `no chunk is larger than the read size, ${CONTRACT_READ_CHUNK} bytes: ${render(back.chunks)}`);
716
+ },
717
+ },
718
+ {
719
+ name: 'answers nothing for a file that is not there, and refuses to read one, or a directory',
720
+ run: async ({ files, dir, join }) => {
721
+ const missing = join(dir, 'missing');
722
+ equal(await files.stat(missing), null);
723
+ await rejects(() => readAll(files, missing), { name: 'FileNotFoundError' });
724
+ equal(await files.remove(missing), false);
725
+ equal(await files.stat(dir), null, 'a directory is no file');
726
+ await rejects(() => readAll(files, dir), { name: 'FileNotFoundError' }, 'nor is it read as one');
727
+ },
728
+ },
729
+ {
730
+ name: 'writes a file only into a directory that is there, and makes directories',
731
+ run: async ({ files, dir, join }) => {
732
+ const nested = join(join(dir, 'a'), 'b');
733
+ const file = join(nested, 'file');
734
+ await rejects(() => files.write(file, bytes('x')), { name: 'FileNotFoundError' });
735
+ await files.mkdir(nested);
736
+ await files.mkdir(nested);
737
+ equal(await files.write(file, bytes('x')), 1);
738
+ deepEqual((await readAll(files, file)).data, bytes('x'));
739
+ },
740
+ },
741
+ {
742
+ name: 'replaces a file whole, and leaves it as it was when a write of it fails',
743
+ run: async ({ files, dir, join }) => {
744
+ const file = join(dir, 'replaced');
745
+ await files.write(file, bytes('a long first file'));
746
+ equal(await files.write(file, chunks('sh', 'ort')), 5);
747
+ deepEqual((await readAll(files, file)).data, bytes('short'));
748
+ await rejects(() => files.write(file, failing('partial')), { message: /the source failed/ });
749
+ deepEqual((await readAll(files, file)).data, bytes('short'));
750
+ const fresh = join(dir, 'fresh');
751
+ await rejects(() => files.write(fresh, failing('partial')), { message: /the source failed/ });
752
+ equal(await files.stat(fresh), null, 'a new file a failed write never finished is not there');
753
+ },
754
+ },
755
+ {
756
+ name: 'removes a file, and says whether there was one',
757
+ run: async ({ files, dir, join }) => {
758
+ const file = join(dir, 'removed');
759
+ await files.write(file, bytes('x'));
760
+ equal(await files.remove(file), true);
761
+ equal(await files.stat(file), null);
762
+ equal(await files.remove(file), false);
763
+ const sub = join(dir, 'sub');
764
+ await files.mkdir(sub);
765
+ equal(await files.remove(sub), false, 'a directory is left');
766
+ },
767
+ },
768
+ ];
769
+ //# sourceMappingURL=adapter-contract.js.map