@gmitch215/folio 1.0.1 → 1.2.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 (130) hide show
  1. package/README.md +98 -460
  2. package/dist/boot.d.ts +1 -0
  3. package/dist/boot.js +1 -0
  4. package/dist/browser/index.d.ts +73 -0
  5. package/dist/browser/index.js +277 -0
  6. package/dist/browser.d.ts +1 -68
  7. package/dist/browser.js +1 -274
  8. package/dist/continuation.d.ts +1 -0
  9. package/dist/continuation.js +1 -0
  10. package/dist/{compiler.d.ts → execution/compiler.d.ts} +1 -1
  11. package/dist/{compiler.js → execution/compiler.js} +5 -12
  12. package/dist/{coordinator.d.ts → execution/coordinator.d.ts} +1 -1
  13. package/dist/{coordinator.js → execution/coordinator.js} +55 -8
  14. package/dist/{extract.d.ts → execution/extract.d.ts} +22 -13
  15. package/dist/{extract.js → execution/extract.js} +137 -10
  16. package/dist/execution/fold.d.ts +44 -0
  17. package/dist/execution/fold.js +105 -0
  18. package/dist/{guards.d.ts → execution/guards.d.ts} +2 -1
  19. package/dist/execution/runs.d.ts +44 -0
  20. package/dist/execution/runs.js +182 -0
  21. package/dist/{transaction.d.ts → execution/transaction.d.ts} +10 -0
  22. package/dist/execution/types.d.ts +221 -0
  23. package/dist/folds/affine-state.d.ts +142 -0
  24. package/dist/folds/affine-state.js +737 -0
  25. package/dist/folds/affine.d.ts +148 -0
  26. package/dist/folds/affine.js +562 -0
  27. package/dist/folds/boundaries.d.ts +48 -0
  28. package/dist/folds/boundaries.js +119 -0
  29. package/dist/folds/certified.d.ts +55 -0
  30. package/dist/folds/certified.js +178 -0
  31. package/dist/folds/certify.d.ts +87 -0
  32. package/dist/folds/certify.js +2611 -0
  33. package/dist/footprints/memo.d.ts +74 -0
  34. package/dist/footprints/memo.js +256 -0
  35. package/dist/footprints/ranges.d.ts +78 -0
  36. package/dist/footprints/ranges.js +263 -0
  37. package/dist/footprints/read-scan.d.ts +18 -0
  38. package/dist/footprints/read-scan.js +94 -0
  39. package/dist/footprints/read-windows.d.ts +77 -0
  40. package/dist/footprints/read-windows.js +245 -0
  41. package/dist/index.d.ts +16 -4
  42. package/dist/index.js +14 -2
  43. package/dist/iteration/admission.d.ts +149 -0
  44. package/dist/iteration/admission.js +201 -0
  45. package/dist/iteration/index.d.ts +239 -0
  46. package/dist/iteration/index.js +1210 -0
  47. package/dist/iteration/inline.d.ts +182 -0
  48. package/dist/{inline.js → iteration/inline.js} +277 -21
  49. package/dist/{iteration-owner.d.ts → iteration/owner.d.ts} +16 -3
  50. package/dist/{iteration-owner.js → iteration/owner.js} +6 -5
  51. package/dist/iteration/worker.js +216 -0
  52. package/dist/{managed.d.ts → managed/index.d.ts} +75 -3
  53. package/dist/managed/index.js +817 -0
  54. package/dist/managed/outbox.d.ts +128 -0
  55. package/dist/managed/outbox.js +156 -0
  56. package/dist/managed/pool.d.ts +221 -0
  57. package/dist/managed/pool.js +310 -0
  58. package/dist/managed/tasks.d.ts +154 -0
  59. package/dist/managed/tasks.js +183 -0
  60. package/dist/{bake.d.ts → memory/bake.d.ts} +12 -6
  61. package/dist/{bake.js → memory/bake.js} +9 -7
  62. package/dist/memory/continuation.d.ts +141 -0
  63. package/dist/memory/continuation.js +495 -0
  64. package/dist/memory/elastic-peer.d.ts +20 -0
  65. package/dist/memory/elastic-peer.js +39 -0
  66. package/dist/memory/elastic-sql.d.ts +11 -0
  67. package/dist/memory/elastic-sql.js +35 -0
  68. package/dist/memory/elastic.d.ts +130 -0
  69. package/dist/memory/elastic.js +475 -0
  70. package/dist/memory/growth-governor.d.ts +16 -0
  71. package/dist/memory/growth-governor.js +42 -0
  72. package/dist/memory/imports.d.ts +10 -0
  73. package/dist/memory/imports.js +89 -0
  74. package/dist/{iteration-layout.d.ts → memory/iteration-layout.d.ts} +61 -1
  75. package/dist/{iteration-layout.js → memory/iteration-layout.js} +54 -3
  76. package/dist/memory/lease.d.ts +45 -0
  77. package/dist/memory/lease.js +274 -0
  78. package/dist/{pages.d.ts → memory/pages.d.ts} +3 -3
  79. package/dist/memory.d.ts +5 -10
  80. package/dist/memory.js +5 -89
  81. package/dist/{context.d.ts → node/context.d.ts} +17 -3
  82. package/dist/{context.js → node/context.js} +60 -14
  83. package/dist/{host.d.ts → node/host.d.ts} +1 -1
  84. package/dist/{host.js → node/host.js} +2 -2
  85. package/dist/{node.d.ts → node/index.d.ts} +46 -49
  86. package/dist/{node.js → node/index.js} +101 -26
  87. package/dist/{tasks.d.ts → node/tasks.d.ts} +16 -17
  88. package/dist/{tasks.js → node/tasks.js} +12 -11
  89. package/dist/node/worker.d.ts +1 -0
  90. package/dist/{worker.js → node/worker.js} +2 -2
  91. package/dist/ownership/index.d.ts +161 -0
  92. package/dist/ownership/index.js +6565 -0
  93. package/dist/ownership/redirect.d.ts +30 -0
  94. package/dist/ownership/redirect.js +179 -0
  95. package/dist/ownership/slices.d.ts +44 -0
  96. package/dist/ownership/slices.js +72 -0
  97. package/dist/{frames.d.ts → remote/frames.d.ts} +6 -2
  98. package/dist/remote/frames.js +306 -0
  99. package/dist/{remote.d.ts → remote/index.d.ts} +7 -2
  100. package/dist/{remote.js → remote/index.js} +7 -2
  101. package/dist/routing/index.d.ts +203 -0
  102. package/dist/routing/index.js +671 -0
  103. package/dist/routing/worker.d.ts +1 -0
  104. package/dist/routing/worker.js +4 -0
  105. package/dist/slices/index.d.ts +126 -0
  106. package/dist/slices/index.js +441 -0
  107. package/dist/slices/worker.d.ts +1 -0
  108. package/dist/slices/worker.js +38 -0
  109. package/dist/workers.d.ts +5 -0
  110. package/dist/workers.js +5 -0
  111. package/package.json +32 -46
  112. package/dist/frames.js +0 -159
  113. package/dist/inline.d.ts +0 -68
  114. package/dist/iteration-admission.d.ts +0 -76
  115. package/dist/iteration-admission.js +0 -72
  116. package/dist/iteration-worker.js +0 -150
  117. package/dist/iteration.d.ts +0 -163
  118. package/dist/iteration.js +0 -435
  119. package/dist/managed.js +0 -421
  120. /package/dist/{browser-worker.d.ts → browser/worker.d.ts} +0 -0
  121. /package/dist/{browser-worker.js → browser/worker.js} +0 -0
  122. /package/dist/{guards.js → execution/guards.js} +0 -0
  123. /package/dist/{import-order.d.ts → execution/import-order.d.ts} +0 -0
  124. /package/dist/{import-order.js → execution/import-order.js} +0 -0
  125. /package/dist/{transaction.js → execution/transaction.js} +0 -0
  126. /package/dist/{iteration-worker.d.ts → execution/types.js} +0 -0
  127. /package/dist/{worker.d.ts → iteration/worker.d.ts} +0 -0
  128. /package/dist/{pages.js → memory/pages.js} +0 -0
  129. /package/dist/{values.d.ts → remote/values.d.ts} +0 -0
  130. /package/dist/{values.js → remote/values.js} +0 -0
@@ -61,7 +61,10 @@ export interface BakeOptions {
61
61
  /** Bake an image that reports changed table slots; the caller then restores them itself. */
62
62
  allowTableChanges?: boolean;
63
63
  }
64
- /** What `applyImage` requires of an image; a value the image does not carry or differs in is refused. */
64
+ /**
65
+ * What `applyImage` requires of an image. A value the image does not carry, or one that differs,
66
+ * is refused.
67
+ */
65
68
  export interface ApplyExpectation {
66
69
  /** sha256 hex of the module bytes the memory belongs to. */
67
70
  module?: string;
@@ -97,7 +100,10 @@ export declare class ImageMismatchError extends Error {
97
100
  readonly code: ImageMismatchCode;
98
101
  constructor(code: ImageMismatchCode, detail: string);
99
102
  }
100
- /** Why bytes are not an image: a wrong `magic` or `version`, `truncated` input, or `invalid` contents. */
103
+ /**
104
+ * Why bytes are not an image: a wrong `magic` or `version`, `truncated` input, or `invalid`
105
+ * contents.
106
+ */
101
107
  export type ImageFormatCode = 'magic' | 'version' | 'truncated' | 'invalid';
102
108
  /** Bytes are not an encoded image, or an image object is inconsistent. */
103
109
  export declare class ImageFormatError extends Error {
@@ -105,7 +111,7 @@ export declare class ImageFormatError extends Error {
105
111
  readonly code: ImageFormatCode;
106
112
  constructor(code: ImageFormatCode, detail: string);
107
113
  }
108
- /** Default cap on one data segment (64 MiB); it bounds the string and heap copy made per segment. */
114
+ /** Default cap on one data segment (64 MiB). It bounds the string and heap copy made per segment. */
109
115
  export declare const maxSegmentBytes: number;
110
116
  /** Lowercase sha256 hex of module bytes, the form `MemoryImage.module` holds. */
111
117
  export declare function moduleDigest(bytes: Uint8Array): Promise<string>;
@@ -172,8 +178,8 @@ export declare function findInImage(image: MemoryImage, needles: Uint8Array[]):
172
178
  * flag and memory64 flag stay as they are, and one already large enough is untouched.
173
179
  *
174
180
  * Throws `ImageMismatchError` for a wrong module digest (taken over `bytes` as passed), a module
175
- * with no memory or with more than one, an image over the memory maximum, a global that cannot be rewritten and changed
176
- * table slots (unless `allowTableChanges`). A start function is refused because it would run
177
- * again over the captured state.
181
+ * with no memory or with more than one, an image over the memory maximum, a global that cannot be
182
+ * rewritten, and changed table slots (unless `allowTableChanges`). A start function is refused
183
+ * because it would run again over the captured state.
178
184
  */
179
185
  export declare function bakeModule(bytes: Uint8Array, image: MemoryImage, options?: BakeOptions): Promise<Uint8Array>;
@@ -1,4 +1,4 @@
1
- import { memoryImports } from "./memory.js";
1
+ import { memoryImports } from "./imports.js";
2
2
  import { pageBytes } from "./pages.js";
3
3
  /** The image cannot be baked into this module. */
4
4
  export class ImageMismatchError extends Error {
@@ -20,7 +20,7 @@ export class ImageFormatError extends Error {
20
20
  this.code = code;
21
21
  }
22
22
  }
23
- /** Default cap on one data segment (64 MiB); it bounds the string and heap copy made per segment. */
23
+ /** Default cap on one data segment (64 MiB). It bounds the string and heap copy made per segment. */
24
24
  export const maxSegmentBytes = 64 * 1024 * 1024;
25
25
  const zeroPage = new Uint8Array(pageBytes);
26
26
  const hexDigest = /^[0-9a-f]{64}$/;
@@ -390,7 +390,9 @@ function name(c) {
390
390
  c.at += length;
391
391
  return value;
392
392
  }
393
- /** Calls `visit` with a section's id, a cursor at its body, its end and its start; stops on `false`. */
393
+ /**
394
+ * Calls `visit` with a section's id, a cursor at its body, its end and its start. Stops on `false`.
395
+ */
394
396
  function eachSection(bytes, visit) {
395
397
  const header = [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00];
396
398
  if (bytes.length < 8 || !header.every((value, i) => bytes[i] === value))
@@ -597,7 +599,7 @@ const latin1 = (data) => {
597
599
  text += String.fromCharCode(...data.subarray(at, at + 8192));
598
600
  return text;
599
601
  };
600
- /** appends one active segment through the C entry point; setMemory drops the module's own segments */
602
+ // appends through the C entry point; setMemory drops the module's own segments
601
603
  function addSegment(binaryen, mod, segment, memory, offset, data, memory64) {
602
604
  const api = binaryen;
603
605
  const pointers = [];
@@ -646,9 +648,9 @@ function setGlobal(binaryen, mod, index, value) {
646
648
  * flag and memory64 flag stay as they are, and one already large enough is untouched.
647
649
  *
648
650
  * Throws `ImageMismatchError` for a wrong module digest (taken over `bytes` as passed), a module
649
- * with no memory or with more than one, an image over the memory maximum, a global that cannot be rewritten and changed
650
- * table slots (unless `allowTableChanges`). A start function is refused because it would run
651
- * again over the captured state.
651
+ * with no memory or with more than one, an image over the memory maximum, a global that cannot be
652
+ * rewritten, and changed table slots (unless `allowTableChanges`). A start function is refused
653
+ * because it would run again over the captured state.
652
654
  */
653
655
  export async function bakeModule(bytes, image, options = {}) {
654
656
  assertImage(image);
@@ -0,0 +1,141 @@
1
+ /** The subset of a Durable Object SQL handle this module uses. */
2
+ export interface SqlStorage {
3
+ exec(query: string, ...bindings: (string | number | null | ArrayBuffer)[]): {
4
+ toArray(): Record<string, unknown>[];
5
+ };
6
+ }
7
+ /** The subset of a Durable Object storage handle this module uses. */
8
+ export interface DurableObjectStorage {
9
+ readonly sql: SqlStorage;
10
+ transactionSync<T>(closure: () => T): T;
11
+ sync(): Promise<void>;
12
+ setAlarm(scheduledTime: number | Date): Promise<void>;
13
+ }
14
+ /** The platform CPU warning, read from the `__instance_signal` word of a guest. */
15
+ export interface CpuSignal {
16
+ /** Byte address of the u32 word in the guest memory. */
17
+ readonly address: number;
18
+ /** True when the word is nonzero. */
19
+ readonly raised: boolean;
20
+ /** Writes 0 to the word. */
21
+ clear(): void;
22
+ }
23
+ /** The CPU warning of an instance, or undefined when it does not export `__instance_signal`. */
24
+ export declare function cpuSignal(instance: WebAssembly.Instance, memory?: WebAssembly.Memory): CpuSignal | undefined;
25
+ /**
26
+ * Hashes each 64 KiB page into two u32 words (low, high). A change detector, not a security hash;
27
+ * the words are read little-endian, as every supported host is.
28
+ */
29
+ export declare function hashPages(memory: WebAssembly.Memory | Uint8Array): Uint32Array;
30
+ /** Page indexes whose hash differs from the previous checkpoint, or all pages when there is none. */
31
+ export declare function changedPages(current: Uint32Array, previous: Uint32Array | undefined): number[];
32
+ /** One committed checkpoint. */
33
+ export interface CheckpointManifest {
34
+ epoch: number;
35
+ byteLength: number;
36
+ hashes: Uint32Array;
37
+ globals: Record<string, number | bigint>;
38
+ state?: string;
39
+ }
40
+ /**
41
+ * Storage behind a checkpoint. `commit` is all or nothing; `pages` returns the newest copy at or
42
+ * below `epoch`. `flush`, when the store has one, resolves once every earlier write survives a
43
+ * killed invocation.
44
+ */
45
+ export interface CheckpointStore {
46
+ latest(): CheckpointManifest | undefined;
47
+ pages(epoch: number, indexes: number[]): Map<number, Uint8Array>;
48
+ commit(manifest: CheckpointManifest, pages: Map<number, Uint8Array>): void;
49
+ flush?(): Promise<void>;
50
+ }
51
+ /** A checkpoint that cannot be loaded: a `hash`, `size`, `global` or `manifest` mismatch. */
52
+ export declare class CheckpointError extends Error {
53
+ readonly code: 'hash' | 'size' | 'global' | 'manifest';
54
+ constructor(code: 'hash' | 'size' | 'global' | 'manifest', detail: string);
55
+ }
56
+ /** In-process store for tests and Node. Keeps the newest copy of each page. */
57
+ export declare function memoryCheckpointStore(): CheckpointStore;
58
+ interface SqlStoreOptions {
59
+ prefix?: string;
60
+ groupPages?: number;
61
+ /** Becomes the store's `flush`; pass `() => storage.sync()` inside a Durable Object. */
62
+ sync?: () => Promise<void>;
63
+ }
64
+ /**
65
+ * DO SQLite store with one row per group of `groupPages` pages (default 16, 1 MiB). Keep one value
66
+ * of `groupPages` per prefix. Commit must run in one synchronous span.
67
+ */
68
+ export declare function sqlCheckpointStore(sql: SqlStorage, options?: SqlStoreOptions): CheckpointStore;
69
+ /** Options for `saveCheckpoint`. */
70
+ export interface SaveOptions {
71
+ globals?: Record<string, WebAssembly.Global>;
72
+ state?: string;
73
+ stateJson?: unknown;
74
+ }
75
+ /** Writes the pages that changed since the last manifest, with the globals and host state. */
76
+ export declare function saveCheckpoint(store: CheckpointStore, memory: WebAssembly.Memory, options?: SaveOptions): {
77
+ epoch: number;
78
+ pagesWritten: number;
79
+ bytesWritten: number;
80
+ };
81
+ /**
82
+ * Restores the newest checkpoint into the memory, growing it as needed. Every page is verified
83
+ * before anything is written. Returns undefined when the store is empty. For a runSlice checkpoint,
84
+ * `json()` returns `{budget, measured, steps, host}`.
85
+ */
86
+ export declare function loadCheckpoint(store: CheckpointStore, memory: WebAssembly.Memory, options?: {
87
+ globals?: Record<string, WebAssembly.Global>;
88
+ }): {
89
+ epoch: number;
90
+ state?: string;
91
+ json<T>(): T;
92
+ } | undefined;
93
+ /** Why a slice ended. */
94
+ export type StopReason = 'finished' | 'signal' | 'budget';
95
+ /** Options for `runSlice`. */
96
+ export interface SliceOptions {
97
+ storage: DurableObjectStorage;
98
+ memory: WebAssembly.Memory;
99
+ globals?: Record<string, WebAssembly.Global>;
100
+ /** From `cpuSignal()`; cleared at the start of each slice. */
101
+ signal?: CpuSignal;
102
+ /** Runs up to `budgetSteps` guest steps and returns at a quiescent point. */
103
+ run(budgetSteps: number): StopReason | Promise<StopReason>;
104
+ /** The guest's monotonic step counter. */
105
+ steps(): number;
106
+ /** Step budget of the first slice, default 2000. */
107
+ initialBudget?: number;
108
+ /** Host state saved with each checkpoint. */
109
+ state?(): string | undefined;
110
+ /** Called after a restore and before `run`. */
111
+ restore?(state: string | undefined): void;
112
+ }
113
+ /** A slice whose previous attempt died before it committed. */
114
+ export interface SliceLoss {
115
+ /** The event number, which the retry keeps. */
116
+ k: number;
117
+ /** Budget of the attempt that died. */
118
+ from: number;
119
+ /** Budget the retry runs with. */
120
+ to: number;
121
+ }
122
+ /**
123
+ * Runs one alarm event of a guest: restores or commits as needed, runs the guest with the event
124
+ * budget, and commits at a budget stop. A signal stop writes nothing before it re-arms.
125
+ * The manifest state holds a runSlice record (budget, step count, host state), so
126
+ * `loadCheckpoint(...).json()` returns that record for checkpoints written here.
127
+ * Before the guest runs, an attempt row with the event number and budget is written and flushed.
128
+ * An event that starts in a new isolate and finds an attempt with no commit after it knows the
129
+ * previous attempt was killed: it halves that budget (never below `initialBudget`), stops doubling
130
+ * and reports the halving in `lost`.
131
+ * Call it from alarm().
132
+ */
133
+ export declare function runSlice(options: SliceOptions): Promise<{
134
+ finished: boolean;
135
+ restored: boolean;
136
+ stop: StopReason | 'cut';
137
+ epoch: number;
138
+ budget: number;
139
+ lost?: SliceLoss;
140
+ }>;
141
+ export {};