@yaks/durable-object 0.0.0-stage → 0.2.36

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 (91) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +554 -2
  3. package/_dist/alarm.d.ts +35 -0
  4. package/_dist/alarm.d.ts.map +1 -0
  5. package/_dist/ceiling.d.ts +167 -0
  6. package/_dist/ceiling.d.ts.map +1 -0
  7. package/_dist/compose.d.ts +79 -0
  8. package/_dist/compose.d.ts.map +1 -0
  9. package/_dist/copy.d.ts +67 -0
  10. package/_dist/copy.d.ts.map +1 -0
  11. package/_dist/door.d.ts +172 -0
  12. package/_dist/door.d.ts.map +1 -0
  13. package/_dist/job.d.ts +26 -0
  14. package/_dist/job.d.ts.map +1 -0
  15. package/_dist/keys.d.ts +14 -0
  16. package/_dist/keys.d.ts.map +1 -0
  17. package/_dist/mod.d.ts +65 -0
  18. package/_dist/mod.d.ts.map +1 -0
  19. package/_dist/object.d.ts +75 -0
  20. package/_dist/object.d.ts.map +1 -0
  21. package/_dist/profile.d.ts +32 -0
  22. package/_dist/profile.d.ts.map +1 -0
  23. package/_dist/query-memory.d.ts +27 -0
  24. package/_dist/query-memory.d.ts.map +1 -0
  25. package/_dist/resets.d.ts +208 -0
  26. package/_dist/resets.d.ts.map +1 -0
  27. package/_dist/rewind.d.ts +21 -0
  28. package/_dist/rewind.d.ts.map +1 -0
  29. package/_dist/sockets.d.ts +90 -0
  30. package/_dist/sockets.d.ts.map +1 -0
  31. package/_dist/sql.d.ts +72 -0
  32. package/_dist/sql.d.ts.map +1 -0
  33. package/_dist/store.d.ts +8 -0
  34. package/_dist/store.d.ts.map +1 -0
  35. package/alarm.js +126 -0
  36. package/alarm.js.map +1 -0
  37. package/alarm.ts +188 -0
  38. package/ceiling.js +536 -0
  39. package/ceiling.js.map +1 -0
  40. package/ceiling.ts +681 -0
  41. package/compose.js +201 -0
  42. package/compose.js.map +1 -0
  43. package/compose.ts +249 -0
  44. package/copy.js +394 -0
  45. package/copy.js.map +1 -0
  46. package/copy.ts +413 -0
  47. package/deno.json +1 -0
  48. package/door.js +364 -0
  49. package/door.js.map +1 -0
  50. package/door.ts +542 -0
  51. package/example.ts +108 -0
  52. package/holds.js +85 -0
  53. package/holds.js.map +1 -0
  54. package/holds.ts +72 -0
  55. package/job.js +151 -0
  56. package/job.js.map +1 -0
  57. package/job.ts +166 -0
  58. package/keys.js +59 -0
  59. package/keys.js.map +1 -0
  60. package/keys.ts +85 -0
  61. package/mod.js +65 -0
  62. package/mod.js.map +1 -0
  63. package/mod.ts +167 -0
  64. package/object.js +247 -0
  65. package/object.js.map +1 -0
  66. package/object.ts +385 -0
  67. package/package.json +68 -4
  68. package/profile.js +131 -0
  69. package/profile.js.map +1 -0
  70. package/profile.ts +150 -0
  71. package/query-memory.js +157 -0
  72. package/query-memory.js.map +1 -0
  73. package/query-memory.ts +240 -0
  74. package/resets.js +302 -0
  75. package/resets.js.map +1 -0
  76. package/resets.ts +353 -0
  77. package/rewind.js +68 -0
  78. package/rewind.js.map +1 -0
  79. package/rewind.ts +91 -0
  80. package/sockets.js +749 -0
  81. package/sockets.js.map +1 -0
  82. package/sockets.ts +991 -0
  83. package/sql.js +221 -0
  84. package/sql.js.map +1 -0
  85. package/sql.ts +330 -0
  86. package/store.js +58 -0
  87. package/store.js.map +1 -0
  88. package/store.ts +80 -0
  89. package/watches.js +8 -0
  90. package/watches.js.map +1 -0
  91. package/watches.ts +23 -0
package/store.ts ADDED
@@ -0,0 +1,80 @@
1
+ // The store, bound. One line of composition: a Durable Object's storage
2
+ // becomes a driver (./sql.ts), and @yaks/sqlite turns that driver plus a
3
+ // vocabulary into a whole `Storage` implementation — schema, reads, patches,
4
+ // cascading deletes, transactions. This package writes no SQL of its own, which
5
+ // is the point: a graph reads the same inside an object as it does on a
6
+ // server.
7
+
8
+ import type { Opts, Store } from '@yaks/sqlite'
9
+ import { storage as bind } from '@yaks/sqlite'
10
+ import { bounded } from './ceiling.js'
11
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
12
+ import {
13
+ driver,
14
+ type DurableStorage,
15
+ type Measure,
16
+ type Observe,
17
+ } from './sql.js'
18
+
19
+ export type { Opts, Store }
20
+
21
+ /**
22
+ * Bind a store to a Durable Object's storage and a vocabulary — the
23
+ * {@link https://jsr.io/@yaks/graph | @yaks/graph} `Storage` a graph applies
24
+ * changes to, implemented synchronously because the object's SQLite is.
25
+ *
26
+ * `install()` it once (create-if-not-exists, so a constructor may call it every
27
+ * time the object wakes). `base` options — a derived-property registry, a fixed
28
+ * `now` for time phrases — are applied to every read, and the registry's
29
+ * `text` expressions (how a property whose stored value is not its text reads
30
+ * as text) to the schema.
31
+ *
32
+ * ```ts
33
+ * // let store = storage(ctx.storage, vocab)
34
+ * // store.install()
35
+ * // let g = graph({ storage: store, vocab })
36
+ * ```
37
+ */
38
+ // The share of each window a maintenance unit may spend: most of it for a
39
+ // large unit, which could otherwise never fit beside the object's own work.
40
+ let share = (written: number) => written > 10_000 ? 0.9 : 0.5
41
+
42
+ export let storage = (
43
+ durable: DurableStorage,
44
+ vocab: Vocab,
45
+ base: Opts = {},
46
+ observe?: Observe,
47
+ measure?: Measure,
48
+ ): Store & { upkeep: (run?: boolean) => number | null } => {
49
+ let store = bind(driver(durable, observe, measure), vocab, base)
50
+ let bound = bounded(durable)
51
+ return Object.assign(store, {
52
+ // Previewing a unit arms a host without running maintenance at boot.
53
+ // A unit no window can hold (an index over a spine past its writes in a
54
+ // minute) is taken, as a deploy's install is: paced, it would wait for a
55
+ // window that never comes, and the object would serve without it for
56
+ // good. Its rows are the file's, once, so it stops nothing and is never
57
+ // told as a runaway (./ceiling.ts `take`).
58
+ upkeep: (run = true) => {
59
+ let wait = Infinity, huge = false
60
+ let worked = store.maintain((written) => {
61
+ let next = bound.under(share(written), written)
62
+ if (next == Infinity) {
63
+ huge = true
64
+ wait = 0
65
+ return false
66
+ }
67
+ wait = Math.min(wait, next)
68
+ return run && next == 0
69
+ })
70
+ if (!worked && huge && run) {
71
+ worked = bound.take(() =>
72
+ store.maintain((written) =>
73
+ bound.under(share(written), written) == Infinity
74
+ )
75
+ )
76
+ }
77
+ return worked ? 0 : Number.isFinite(wait) ? wait : null
78
+ },
79
+ })
80
+ }
package/watches.js ADDED
@@ -0,0 +1,8 @@
1
+ // Compact, lossless watch changes for the runtime's 2KB socket attachment.
2
+ // The map still lives in SQLite when even its compressed changes cannot fit.
3
+ import { deflateSync, inflateSync, strFromU8, strToU8 } from "fflate";
4
+ export let pack = (delta)=>btoa(Array.from(deflateSync(strToU8(JSON.stringify(delta)), {
5
+ level: 1
6
+ }), (byte)=>String.fromCharCode(byte)).join(''));
7
+ export let unpack = (delta)=>typeof delta == 'string' ? JSON.parse(strFromU8(inflateSync(Uint8Array.from(atob(delta), (char)=>char.charCodeAt(0))))) : delta;
8
+ //# sourceMappingURL=watches.js.map
package/watches.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watches.js","sources":["./watches.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,6EAA6E;AAE7E,SAAS,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,iBAA0B;AAI/E,OAAO,IAAI,OAAO,CAAC,QACjB,KACE,MAAM,IAAI,CACR,YAAY,QAAQ,KAAK,SAAS,CAAC,SAAS;IAAE,OAAO;EAAE,IACvD,CAAC,OAAS,OAAO,YAAY,CAAC,OAC9B,IAAI,CAAC,KACR;AAEH,OAAO,IAAI,SAAS,CAAC,QACnB,OAAO,SAAS,WACZ,KAAK,KAAK,CACV,UACE,YAAY,WAAW,IAAI,CAAC,KAAK,QAAQ,CAAC,OAAS,KAAK,UAAU,CAAC,SAGrE,MAAK"}
package/watches.ts ADDED
@@ -0,0 +1,23 @@
1
+ // Compact, lossless watch changes for the runtime's 2KB socket attachment.
2
+ // The map still lives in SQLite when even its compressed changes cannot fit.
3
+ import type { Ask } from 'jsr:@yaks/api@0.2.36'
4
+ import { deflateSync, inflateSync, strFromU8, strToU8 } from 'fflate'
5
+
6
+ export type Delta = Record<string, Ask | null>
7
+
8
+ export let pack = (delta: Delta): string =>
9
+ btoa(
10
+ Array.from(
11
+ deflateSync(strToU8(JSON.stringify(delta)), { level: 1 }),
12
+ (byte) => String.fromCharCode(byte),
13
+ ).join(''),
14
+ )
15
+
16
+ export let unpack = (delta: Delta | string): Delta =>
17
+ typeof delta == 'string'
18
+ ? JSON.parse(
19
+ strFromU8(
20
+ inflateSync(Uint8Array.from(atob(delta), (char) => char.charCodeAt(0))),
21
+ ),
22
+ )
23
+ : delta