@baukit/data-contracts 0.4.0 → 0.5.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.
- package/README.md +207 -1
- package/dist/durable-draft.d.ts +90 -0
- package/dist/durable-draft.d.ts.map +1 -0
- package/dist/durable-draft.js +270 -0
- package/dist/durable-draft.js.map +1 -0
- package/dist/export.d.ts +21 -0
- package/dist/export.d.ts.map +1 -0
- package/dist/export.js +104 -0
- package/dist/export.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/limits.d.ts +23 -0
- package/dist/limits.d.ts.map +1 -1
- package/dist/limits.js +88 -0
- package/dist/limits.js.map +1 -1
- package/dist/revisioned-writes.d.ts +96 -0
- package/dist/revisioned-writes.d.ts.map +1 -0
- package/dist/revisioned-writes.js +221 -0
- package/dist/revisioned-writes.js.map +1 -0
- package/package.json +30 -11
- package/dist/uuid-v7.d.ts +0 -15
- package/dist/uuid-v7.d.ts.map +0 -1
- package/dist/uuid-v7.js +0 -31
- package/dist/uuid-v7.js.map +0 -1
package/README.md
CHANGED
|
@@ -54,6 +54,32 @@ with `trimmedUnicodeScalarCount(value)`, and replace a `JSON.stringify` plus `Te
|
|
|
54
54
|
with `compactJsonUtf8Bytes(value)`. Unlike raw `JSON.stringify`, the helper rejects values that JSON
|
|
55
55
|
would omit or replace with `null`.
|
|
56
56
|
|
|
57
|
+
### Limits policy files
|
|
58
|
+
|
|
59
|
+
`parseLimitsPolicy(value, schema)` validates a product's limits policy file, such as the
|
|
60
|
+
`limits.json` the templates generate. The schema names the supported version and the keys of each
|
|
61
|
+
section. The parser requires a string `$comment`, the exact version, the exact section and key sets,
|
|
62
|
+
and positive safe integers. Keys listed in `allowZero` as `section.key` also accept zero. Any other
|
|
63
|
+
shape throws `LimitsPolicyError`, whose message names the failing path but never a value. Pass the
|
|
64
|
+
schema `as const` so the result type has one `number` property per declared key.
|
|
65
|
+
|
|
66
|
+
`enforceLimit(field, reason, check)` runs one check and turns `LimitExceededError` into
|
|
67
|
+
`LimitError`, which carries `reason`, `field`, `measured`, and `allowed`. Other errors pass through.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { checkTrimmedUnicodeScalars, enforceLimit, parseLimitsPolicy } from '@baukit/data-contracts/limits';
|
|
71
|
+
|
|
72
|
+
const schema = { version: 1, sections: { text: ['max_characters'] } } as const;
|
|
73
|
+
const policy = parseLimitsPolicy(limitsJson, schema);
|
|
74
|
+
|
|
75
|
+
enforceLimit('title', 'text_too_long', () =>
|
|
76
|
+
checkTrimmedUnicodeScalars(title, policy.text.max_characters),
|
|
77
|
+
);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Cross-field rules, such as one limit that must not exceed another, stay in product code after the
|
|
81
|
+
parse.
|
|
82
|
+
|
|
57
83
|
## Import envelopes
|
|
58
84
|
|
|
59
85
|
The `/import-envelope` subpath separates import safety from a product's file format. The product
|
|
@@ -95,6 +121,186 @@ This API is additive. Existing import code can migrate by wrapping its current d
|
|
|
95
121
|
planner first, then moving the write loop into `commitImportEnvelope`. It does not require a file
|
|
96
122
|
format or database migration.
|
|
97
123
|
|
|
124
|
+
## Exports
|
|
125
|
+
|
|
126
|
+
The `/export` subpath is the counterpart of the import envelope. `encodeCsv` turns rows into an RFC
|
|
127
|
+
4180 CSV string: every record ends with CRLF, and a cell is quoted when it contains a comma, a double
|
|
128
|
+
quote, CR, or LF. A row holding one empty cell is written as `""` so a reader still sees one field.
|
|
129
|
+
The helper has no runtime dependencies; the product writes the string to a file and shares it.
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { csvNumeric, encodeCsv } from '@baukit/data-contracts/export';
|
|
133
|
+
|
|
134
|
+
const csv = encodeCsv(
|
|
135
|
+
[
|
|
136
|
+
['note', 'amount', 'price'],
|
|
137
|
+
['=HYPERLINK("https://example.test")', -5, csvNumeric('1.50')],
|
|
138
|
+
],
|
|
139
|
+
{ byteOrderMark: true },
|
|
140
|
+
);
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Formula neutralization is on by default. A text cell gets a leading apostrophe when its first
|
|
144
|
+
character is `=`, `+`, `-`, `@`, tab, or CR, or when `=`, `+`, `-`, or `@` follows leading spaces,
|
|
145
|
+
tabs, CR, or LF. Only these ASCII characters are checked. The apostrophe changes the value, so a
|
|
146
|
+
file that your own importer reads back must either strip it or be written with
|
|
147
|
+
`neutralizeFormulas: false`. Use the opt-out only for machine-read files that never open in a
|
|
148
|
+
spreadsheet.
|
|
149
|
+
|
|
150
|
+
Text that looks like a negative number, such as `'-5'`, is neutralized. Pass real numbers as a
|
|
151
|
+
finite `number` or as `csvNumeric(text)` when the formatting matters, for example `'1.50'`. Numeric
|
|
152
|
+
cells must match the JSON number grammar and are written unchanged. `null` is an empty cell.
|
|
153
|
+
`byteOrderMark: true` starts the output with U+FEFF for spreadsheet programs that need it to detect
|
|
154
|
+
UTF-8.
|
|
155
|
+
|
|
156
|
+
`CsvEncodeError.code` is `invalid_numeric_cell` for a non-finite number or a numeric cell outside
|
|
157
|
+
the JSON grammar, `invalid_unicode` for a string with an unpaired surrogate, or `unsupported_cell`
|
|
158
|
+
for any other runtime value. The error carries `rowIndex` and `columnIndex` and never the cell
|
|
159
|
+
value. `baukit_core::export::encode_csv` in Rust passes the same vectors
|
|
160
|
+
(`fixtures/export-csv/csv-encoding-v1.json`).
|
|
161
|
+
|
|
162
|
+
`ShareOutcome` names what happened after the product handed the file to the platform:
|
|
163
|
+
|
|
164
|
+
- `shared`: a share sheet or the Web Share API accepted the file. Some native share sheets report
|
|
165
|
+
this even when the user closed them without picking a target.
|
|
166
|
+
- `saved`: the file was written to a place the user can open, such as a browser download.
|
|
167
|
+
- `cancelled`: the user dismissed the share or save UI and the platform said so, for example with a
|
|
168
|
+
Web Share `AbortError`.
|
|
169
|
+
- `unavailable`: the runtime has no share or save mechanism, so nothing was attempted.
|
|
170
|
+
- `failed`: a mechanism was attempted and reported an error other than cancellation.
|
|
171
|
+
|
|
172
|
+
`SHARE_OUTCOMES` lists the same values. The package ships no Expo or browser implementation yet.
|
|
173
|
+
|
|
174
|
+
## Revisioned writes
|
|
175
|
+
|
|
176
|
+
The `/revisioned-writes` subpath sends edits of one server document one at a time, each checked
|
|
177
|
+
against a revision. It has no React dependency. Wrap it in a hook with `useSyncExternalStore`: the
|
|
178
|
+
snapshot object only changes when state changes.
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import { createRevisionedWriteQueue } from '@baukit/data-contracts/revisioned-writes';
|
|
182
|
+
|
|
183
|
+
const queue = createRevisionedWriteQueue({
|
|
184
|
+
initial: { scope: accountId, document: noteId, acknowledgedRevision: loaded.revision },
|
|
185
|
+
write: async ({ scope, document, value, expectedRevision, afterUnknownOutcome, signal }) => {
|
|
186
|
+
const response = await api.saveNote(scope, document, value, {
|
|
187
|
+
ifMatch: expectedRevision,
|
|
188
|
+
idempotencyKey: await keyFor(document, value, expectedRevision),
|
|
189
|
+
signal,
|
|
190
|
+
});
|
|
191
|
+
if (response.status === 409) {
|
|
192
|
+
return { kind: 'conflict', currentRevision: response.revision, conflict: response.body };
|
|
193
|
+
}
|
|
194
|
+
if (!response.ok) {
|
|
195
|
+
return { kind: 'rejected', error: response.problem };
|
|
196
|
+
}
|
|
197
|
+
return { kind: 'accepted', revision: response.revision };
|
|
198
|
+
},
|
|
199
|
+
signal: pageLifetime.signal,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
queue.enqueue(editedValue);
|
|
203
|
+
await queue.flush();
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
The snapshot keeps two values apart. `acknowledgedRevision` is the last revision the server
|
|
207
|
+
confirmed. `unsent` is the local value no write has carried yet. A write sends the value it took
|
|
208
|
+
from `unsent` with the acknowledged revision as `expectedRevision`. Edits made while that write is
|
|
209
|
+
in flight go into a fresh `unsent` and the status stays `dirty`. An acknowledgement for the older
|
|
210
|
+
value never marks the newer one saved. Queued edits are combined with `coalesce(older, newer)`,
|
|
211
|
+
which keeps the newer value by default. Pass your own function when a write carries a patch instead
|
|
212
|
+
of the whole document.
|
|
213
|
+
|
|
214
|
+
`enqueue` never sends. Debounce in the product and call `flush`. Concurrent `flush` calls share one
|
|
215
|
+
drain, which resolves with the snapshot once the queue is idle or paused.
|
|
216
|
+
|
|
217
|
+
The write callback reports one of three outcomes:
|
|
218
|
+
|
|
219
|
+
- `accepted` stores the new revision and sends the next unsent value, if any.
|
|
220
|
+
- `rejected` means the server refused the write before accepting it. The value goes back into
|
|
221
|
+
`unsent`, merged with newer edits, and the status is `failed`. `flush` or `retry` sends it again.
|
|
222
|
+
- `conflict` pauses the queue with the server's revision and conflict payload. Nothing more is sent
|
|
223
|
+
until the product calls `reset` with a revision it trusts. To keep the local value, `reset` to
|
|
224
|
+
the current server revision and `enqueue` the value again.
|
|
225
|
+
|
|
226
|
+
A throw from the write callback, or a rejected promise, means the outcome is unknown. The server may
|
|
227
|
+
have applied the write and lost the response. The queue moves to `unknown-outcome` and keeps the
|
|
228
|
+
exact value and expected revision in `uncertain`, apart from newer edits. `flush` does nothing in
|
|
229
|
+
that state. `retry` sends the same value and revision with `afterUnknownOutcome: true`, so the
|
|
230
|
+
product can reuse its idempotency key. Otherwise read the server and call `reset`. The queue never
|
|
231
|
+
treats an unknown outcome as a failure that happened before the server accepted the write.
|
|
232
|
+
|
|
233
|
+
`reset(next)` is the generation fence for an account or document switch. It aborts the current
|
|
234
|
+
write through the `signal` passed to the callback, drops unsent and uncertain state, and ignores
|
|
235
|
+
any late result of the old generation. `cancel()` does the same and keeps the queue in `cancelled`
|
|
236
|
+
until the next `reset`. Aborting the injected `signal` option calls `cancel`.
|
|
237
|
+
|
|
238
|
+
Status is `cancelled`, `writing`, `failed`, `unknown-outcome`, `conflict`, `dirty`, or `idle`, in
|
|
239
|
+
that order of precedence. `error` holds the `rejected` error or the thrown value.
|
|
240
|
+
|
|
241
|
+
## Durable drafts
|
|
242
|
+
|
|
243
|
+
The `/durable-draft` subpath keeps an unsent form value in any `KeyValueStore`, so it survives a
|
|
244
|
+
reload or crash. It has no React dependency.
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
import { createDurableDraft, type DraftCodec } from '@baukit/data-contracts/durable-draft';
|
|
248
|
+
|
|
249
|
+
const noteCodec: DraftCodec<NoteDraft> = {
|
|
250
|
+
version: 2,
|
|
251
|
+
encode: (value) => ({ body: value.body, baseRevision: value.baseRevision }),
|
|
252
|
+
decode: (value, version) => decodeNoteDraft(value, version),
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const draft = createDurableDraft({
|
|
256
|
+
store,
|
|
257
|
+
key: (scope: { accountId: string; noteId: string }) =>
|
|
258
|
+
`note-draft:${scope.accountId}:${scope.noteId}`,
|
|
259
|
+
codec: noteCodec,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
await draft.open({ accountId, noteId }, { body: loaded.body, baseRevision: loaded.revision });
|
|
263
|
+
draft.update({ body: editedBody, baseRevision: loaded.revision });
|
|
264
|
+
await draft.save();
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Storage holds `{ version, value }`. The codec owns the value: `encode` returns JSON, and `decode`
|
|
268
|
+
receives unchecked JSON plus the stored version and returns `decoded`, `corrupt`, or
|
|
269
|
+
`unsupported-version`. Validate every field in `decode`. A decoded value from an older version
|
|
270
|
+
counts as upgraded, so the draft opens dirty and the next `save` rewrites it. The helper returns
|
|
271
|
+
`unsupported-version` without calling `decode` when the stored version is above `codec.version`,
|
|
272
|
+
and `corrupt` when the envelope is malformed or `decode` throws. Keep a server base revision or ETag
|
|
273
|
+
inside the value when the product needs it for a later conflict check.
|
|
274
|
+
|
|
275
|
+
An open snapshot has two groups of state. `recovery` says what `open` found: `none`, `restored`,
|
|
276
|
+
`corrupt`, `unsupported-version`, or `unavailable` when the read failed. `persistence` says what
|
|
277
|
+
storage is doing now: `loading`, `idle`, `saving`, `clearing`, or `failed`. `dirty` is true when
|
|
278
|
+
the value differs from what storage holds, and `localRevision` counts local changes. A restored
|
|
279
|
+
draft can also be dirty.
|
|
280
|
+
|
|
281
|
+
`open` never writes or deletes. While recovery is `corrupt` or `unsupported-version`, `save`
|
|
282
|
+
resolves `blocked` so the unreadable data stays in place. Call `clear({ reason: 'discarded' })` to
|
|
283
|
+
remove it.
|
|
284
|
+
|
|
285
|
+
`clear` takes the reason for the deletion:
|
|
286
|
+
|
|
287
|
+
- `{ reason: 'submitted', localRevision }` reports that the server confirmed the value at that local
|
|
288
|
+
revision. The snapshot shows `submission: 'confirmed'` and keeps the value visible. If the user
|
|
289
|
+
edited after that revision, the stored draft stays and `clear` resolves `newer-edits-kept`.
|
|
290
|
+
- `{ reason: 'discarded' }` deletes the draft and returns the value to the one passed to `open`.
|
|
291
|
+
|
|
292
|
+
A failed deletion rejects with a `DraftPersistenceError` and stays in the snapshot as
|
|
293
|
+
`persistence: 'failed'`. After a submission, `submission` stays `confirmed`, so the product can tell
|
|
294
|
+
"the server has it, but the local copy may come back" apart from "nothing was sent". Read, write,
|
|
295
|
+
and delete failures use `DraftPersistenceError` with `code: 'draft_persistence_failed'` and an
|
|
296
|
+
`operation`. The message never includes the key or the value.
|
|
297
|
+
|
|
298
|
+
`open` for a new scope fences the previous one. Storage work of the old scope that had not started
|
|
299
|
+
is skipped and resolves `stale`. Work that already started finishes but does not change the new
|
|
300
|
+
snapshot. All storage work runs in order, so switching from A to B and back to A reads what A's last
|
|
301
|
+
write left. `close()` does not save; call `save` on `pagehide` or when the app moves to the
|
|
302
|
+
background.
|
|
303
|
+
|
|
98
304
|
## Authenticated partitions
|
|
99
305
|
|
|
100
306
|
`deriveScopedStoreName(namespace, subject)` hashes a length-delimited canonical
|
|
@@ -130,7 +336,7 @@ adoption or an outbox push.
|
|
|
130
336
|
|
|
131
337
|
## Proving an adapter
|
|
132
338
|
|
|
133
|
-
Vitest helpers are isolated in a test-only subpath. Vitest is deliberately not a peer dependency, so it is never installed in Jest or production consumers. Install Vitest in an adapter project's development dependencies before importing the subpath, then register the applicable suites:
|
|
339
|
+
Vitest helpers are isolated in a test-only subpath. Vitest is deliberately not a peer dependency, so it is never installed in Jest or production consumers. The subpath has no `default` export condition, so a Jest suite cannot resolve it by accident. Install Vitest in an adapter project's development dependencies before importing the subpath, then register the applicable suites:
|
|
134
340
|
|
|
135
341
|
```ts
|
|
136
342
|
import {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type JsonValue, type KeyValueStore } from './contracts.js';
|
|
2
|
+
/** The result of decoding a stored draft value. Return `corrupt` for any value that fails validation. */
|
|
3
|
+
export type DraftDecodeResult<TValue> = {
|
|
4
|
+
readonly kind: 'decoded';
|
|
5
|
+
readonly value: TValue;
|
|
6
|
+
} | {
|
|
7
|
+
readonly kind: 'corrupt';
|
|
8
|
+
} | {
|
|
9
|
+
readonly kind: 'unsupported-version';
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Converts a form value to and from stored JSON. The helper stores `{ version, value }` and never
|
|
13
|
+
* calls `decode` for a version above `version`. `decode` receives unchecked JSON and must validate
|
|
14
|
+
* it; an older `version` must be upgraded to the current value shape.
|
|
15
|
+
*/
|
|
16
|
+
export interface DraftCodec<TValue> {
|
|
17
|
+
/** The positive integer written with every save. */
|
|
18
|
+
readonly version: number;
|
|
19
|
+
encode(value: TValue): JsonValue;
|
|
20
|
+
decode(value: JsonValue, version: number): DraftDecodeResult<TValue>;
|
|
21
|
+
}
|
|
22
|
+
export type DraftPersistenceOperation = 'read' | 'write' | 'delete';
|
|
23
|
+
/** A storage failure. The message names only the operation, never keys or draft content. */
|
|
24
|
+
export declare class DraftPersistenceError extends Error {
|
|
25
|
+
readonly operation: DraftPersistenceOperation;
|
|
26
|
+
readonly name = "DraftPersistenceError";
|
|
27
|
+
readonly code = "draft_persistence_failed";
|
|
28
|
+
constructor(operation: DraftPersistenceOperation, options?: ErrorOptions);
|
|
29
|
+
}
|
|
30
|
+
/** What `open` found in storage. */
|
|
31
|
+
export type DraftRecovery = 'none' | 'restored' | 'corrupt' | 'unsupported-version' | 'unavailable';
|
|
32
|
+
export type DraftPersistence = 'loading' | 'idle' | 'saving' | 'clearing' | 'failed';
|
|
33
|
+
export type DraftSubmission = 'none' | 'confirmed';
|
|
34
|
+
export interface ClosedDurableDraftSnapshot {
|
|
35
|
+
readonly open: false;
|
|
36
|
+
}
|
|
37
|
+
export interface OpenDurableDraftSnapshot<TScope, TValue> {
|
|
38
|
+
readonly open: true;
|
|
39
|
+
readonly scope: TScope;
|
|
40
|
+
readonly value: TValue;
|
|
41
|
+
/** Increments on every local change. Pass it to `clear` after a submission. */
|
|
42
|
+
readonly localRevision: number;
|
|
43
|
+
/** True when `value` differs from what storage holds for this scope. */
|
|
44
|
+
readonly dirty: boolean;
|
|
45
|
+
readonly recovery: DraftRecovery;
|
|
46
|
+
readonly persistence: DraftPersistence;
|
|
47
|
+
readonly error: DraftPersistenceError | null;
|
|
48
|
+
/** `confirmed` once the product reported a successful server submission through `clear`. */
|
|
49
|
+
readonly submission: DraftSubmission;
|
|
50
|
+
}
|
|
51
|
+
export type DurableDraftSnapshot<TScope, TValue> = ClosedDurableDraftSnapshot | OpenDurableDraftSnapshot<TScope, TValue>;
|
|
52
|
+
export type DraftClearRequest = {
|
|
53
|
+
readonly reason: 'discarded';
|
|
54
|
+
} | {
|
|
55
|
+
readonly reason: 'submitted';
|
|
56
|
+
readonly localRevision: number;
|
|
57
|
+
};
|
|
58
|
+
/** `stale` means the scope changed or closed before the operation started, so storage was not touched. */
|
|
59
|
+
export type DraftSaveOutcome = 'saved' | 'clean' | 'blocked' | 'stale';
|
|
60
|
+
export type DraftClearOutcome = 'cleared' | 'newer-edits-kept' | 'stale';
|
|
61
|
+
export interface DurableDraftOptions<TScope, TValue> {
|
|
62
|
+
readonly store: KeyValueStore;
|
|
63
|
+
/** Encodes the scope to a storage key. Include the identity and document. */
|
|
64
|
+
readonly key: (scope: TScope) => string;
|
|
65
|
+
readonly codec: DraftCodec<TValue>;
|
|
66
|
+
}
|
|
67
|
+
export interface DurableDraft<TScope, TValue> {
|
|
68
|
+
getSnapshot(): DurableDraftSnapshot<TScope, TValue>;
|
|
69
|
+
subscribe(listener: () => void): () => void;
|
|
70
|
+
/** Switches to a scope and reads its stored draft. Late work of the previous scope is ignored. */
|
|
71
|
+
open(scope: TScope, initial: TValue): Promise<void>;
|
|
72
|
+
/** Replaces the value in memory. Debounce and call `save` to persist it. */
|
|
73
|
+
update(value: TValue): void;
|
|
74
|
+
/**
|
|
75
|
+
* Writes the current value. Resolves `blocked` without writing while recovery is `corrupt` or
|
|
76
|
+
* `unsupported-version`. Rejects with `DraftPersistenceError` when storage fails.
|
|
77
|
+
*/
|
|
78
|
+
save(): Promise<DraftSaveOutcome>;
|
|
79
|
+
/**
|
|
80
|
+
* Deletes the stored draft. `submitted` keeps the value visible and keeps newer edits made after
|
|
81
|
+
* `localRevision`; `discarded` returns to the initial value. Rejects with a `delete`
|
|
82
|
+
* `DraftPersistenceError` that stays in the snapshot.
|
|
83
|
+
*/
|
|
84
|
+
clear(request: DraftClearRequest): Promise<DraftClearOutcome>;
|
|
85
|
+
/** Closes the scope without saving and resolves after in-flight storage work settles. */
|
|
86
|
+
close(): Promise<void>;
|
|
87
|
+
}
|
|
88
|
+
/** Creates a framework-free durable draft over a `KeyValueStore`. */
|
|
89
|
+
export declare function createDurableDraft<TScope, TValue>(options: DurableDraftOptions<TScope, TValue>): DurableDraft<TScope, TValue>;
|
|
90
|
+
//# sourceMappingURL=durable-draft.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"durable-draft.d.ts","sourceRoot":"","sources":["../src/durable-draft.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,aAAa,EAAyB,MAAM,gBAAgB,CAAC;AAE3F,yGAAyG;AACzG,MAAM,MAAM,iBAAiB,CAAC,MAAM,IAChC;IAAE,QAAQ,C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MAAM,IAAI,CAAC;IAC5C,kGAAkG;IAClG,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpD,4EAA4E;IAC5E,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B;;;OAGG;IACH,IAAI,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAClC;;;;OAIG;IACH,KAAK,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAC9D,yFAAyF;IACzF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AA4SD,qEAAqE;AACrE,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAC/C,OAAO,EAAE,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,GAC3C,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,CAE9B"}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { normalizeStorageError } from './contracts.js';
|
|
2
|
+
/** A storage failure. The message names only the operation, never keys or draft content. */
|
|
3
|
+
export class DraftPersistenceError extends Error {
|
|
4
|
+
operation;
|
|
5
|
+
name = 'DraftPersistenceError';
|
|
6
|
+
code = 'draft_persistence_failed';
|
|
7
|
+
constructor(operation, options) {
|
|
8
|
+
super(`Draft ${operation} failed.`, options);
|
|
9
|
+
this.operation = operation;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function isEnvelope(stored) {
|
|
13
|
+
if (typeof stored !== 'object' || stored === null || Array.isArray(stored)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const version = stored['version'];
|
|
17
|
+
return (typeof version === 'number' && Number.isSafeInteger(version) && version > 0 && 'value' in stored);
|
|
18
|
+
}
|
|
19
|
+
function decodeStored(stored, codec) {
|
|
20
|
+
if (stored === undefined) {
|
|
21
|
+
return { recovery: 'none' };
|
|
22
|
+
}
|
|
23
|
+
if (!isEnvelope(stored)) {
|
|
24
|
+
return { recovery: 'corrupt' };
|
|
25
|
+
}
|
|
26
|
+
if (stored.version > codec.version) {
|
|
27
|
+
return { recovery: 'unsupported-version' };
|
|
28
|
+
}
|
|
29
|
+
let result;
|
|
30
|
+
try {
|
|
31
|
+
result = codec.decode(stored.value, stored.version);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return { recovery: 'corrupt' };
|
|
35
|
+
}
|
|
36
|
+
if (result.kind !== 'decoded') {
|
|
37
|
+
return { recovery: result.kind };
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
recovery: 'restored',
|
|
41
|
+
value: result.value,
|
|
42
|
+
upgraded: stored.version < codec.version,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function isBlocked(recovery) {
|
|
46
|
+
return recovery === 'corrupt' || recovery === 'unsupported-version';
|
|
47
|
+
}
|
|
48
|
+
function persistenceError(operation, cause) {
|
|
49
|
+
return new DraftPersistenceError(operation, { cause: normalizeStorageError(cause) });
|
|
50
|
+
}
|
|
51
|
+
const CLOSED = { open: false };
|
|
52
|
+
class DefaultDurableDraft {
|
|
53
|
+
#store;
|
|
54
|
+
#key;
|
|
55
|
+
#codec;
|
|
56
|
+
#listeners = new Set();
|
|
57
|
+
#session = null;
|
|
58
|
+
#persistedRevision = 0;
|
|
59
|
+
#tail = Promise.resolve();
|
|
60
|
+
#snapshot = CLOSED;
|
|
61
|
+
constructor(options) {
|
|
62
|
+
const { version } = options.codec;
|
|
63
|
+
if (!Number.isSafeInteger(version) || version < 1) {
|
|
64
|
+
throw new RangeError('Draft codec version must be a positive integer.');
|
|
65
|
+
}
|
|
66
|
+
this.#store = options.store;
|
|
67
|
+
this.#key = options.key;
|
|
68
|
+
this.#codec = options.codec;
|
|
69
|
+
}
|
|
70
|
+
getSnapshot() {
|
|
71
|
+
return this.#snapshot;
|
|
72
|
+
}
|
|
73
|
+
subscribe(listener) {
|
|
74
|
+
this.#listeners.add(listener);
|
|
75
|
+
return () => {
|
|
76
|
+
this.#listeners.delete(listener);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
open(scope, initial) {
|
|
80
|
+
let key;
|
|
81
|
+
try {
|
|
82
|
+
key = this.#key(scope);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
return Promise.reject(error instanceof Error ? error : new Error(String(error)));
|
|
86
|
+
}
|
|
87
|
+
const session = { scope, key, initial };
|
|
88
|
+
this.#session = session;
|
|
89
|
+
this.#persistedRevision = 0;
|
|
90
|
+
this.#publish({
|
|
91
|
+
open: true,
|
|
92
|
+
scope,
|
|
93
|
+
value: initial,
|
|
94
|
+
localRevision: 0,
|
|
95
|
+
dirty: false,
|
|
96
|
+
recovery: 'none',
|
|
97
|
+
persistence: 'loading',
|
|
98
|
+
error: null,
|
|
99
|
+
submission: 'none',
|
|
100
|
+
});
|
|
101
|
+
return this.#enqueue(() => this.#load(session));
|
|
102
|
+
}
|
|
103
|
+
update(value) {
|
|
104
|
+
const current = this.#current();
|
|
105
|
+
if (current === null) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
this.#publishOpen({
|
|
109
|
+
value,
|
|
110
|
+
localRevision: current.localRevision + 1,
|
|
111
|
+
submission: 'none',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
save() {
|
|
115
|
+
const session = this.#session;
|
|
116
|
+
if (session === null) {
|
|
117
|
+
return Promise.resolve('stale');
|
|
118
|
+
}
|
|
119
|
+
return this.#enqueue(() => this.#write(session));
|
|
120
|
+
}
|
|
121
|
+
clear(request) {
|
|
122
|
+
const session = this.#session;
|
|
123
|
+
if (session === null) {
|
|
124
|
+
return Promise.resolve('stale');
|
|
125
|
+
}
|
|
126
|
+
return this.#enqueue(() => this.#delete(session, request));
|
|
127
|
+
}
|
|
128
|
+
async close() {
|
|
129
|
+
this.#session = null;
|
|
130
|
+
this.#publish(CLOSED);
|
|
131
|
+
await this.#tail;
|
|
132
|
+
}
|
|
133
|
+
async #load(session) {
|
|
134
|
+
if (session !== this.#session) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
let stored;
|
|
138
|
+
try {
|
|
139
|
+
stored = await this.#store.get(session.key);
|
|
140
|
+
}
|
|
141
|
+
catch (cause) {
|
|
142
|
+
this.#publishFor(session, {
|
|
143
|
+
recovery: 'unavailable',
|
|
144
|
+
persistence: 'failed',
|
|
145
|
+
error: persistenceError('read', cause),
|
|
146
|
+
});
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const current = this.#currentFor(session);
|
|
150
|
+
if (current === null) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const loaded = decodeStored(stored, this.#codec);
|
|
154
|
+
if (loaded.recovery === 'restored' && current.localRevision === 0) {
|
|
155
|
+
this.#persistedRevision = loaded.upgraded ? null : 0;
|
|
156
|
+
this.#publishOpen({ value: loaded.value, recovery: 'restored', persistence: 'idle' });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.#publishOpen({
|
|
160
|
+
recovery: loaded.recovery === 'restored' ? 'none' : loaded.recovery,
|
|
161
|
+
persistence: 'idle',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
async #write(session) {
|
|
165
|
+
const current = this.#currentFor(session);
|
|
166
|
+
if (current === null) {
|
|
167
|
+
return 'stale';
|
|
168
|
+
}
|
|
169
|
+
if (!current.dirty) {
|
|
170
|
+
return 'clean';
|
|
171
|
+
}
|
|
172
|
+
if (isBlocked(current.recovery)) {
|
|
173
|
+
return 'blocked';
|
|
174
|
+
}
|
|
175
|
+
const { localRevision, value } = current;
|
|
176
|
+
this.#publishOpen({ persistence: 'saving', error: null });
|
|
177
|
+
try {
|
|
178
|
+
const envelope = { version: this.#codec.version, value: this.#codec.encode(value) };
|
|
179
|
+
await this.#store.set(session.key, envelope);
|
|
180
|
+
}
|
|
181
|
+
catch (cause) {
|
|
182
|
+
const error = persistenceError('write', cause);
|
|
183
|
+
this.#publishFor(session, { persistence: 'failed', error });
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
if (this.#currentFor(session) !== null) {
|
|
187
|
+
this.#persistedRevision = localRevision;
|
|
188
|
+
this.#publishOpen({ persistence: 'idle', error: null });
|
|
189
|
+
}
|
|
190
|
+
return 'saved';
|
|
191
|
+
}
|
|
192
|
+
async #delete(session, request) {
|
|
193
|
+
const current = this.#currentFor(session);
|
|
194
|
+
if (current === null) {
|
|
195
|
+
return 'stale';
|
|
196
|
+
}
|
|
197
|
+
const submitted = request.reason === 'submitted';
|
|
198
|
+
if (submitted && request.localRevision !== current.localRevision) {
|
|
199
|
+
this.#publishOpen({ submission: 'confirmed' });
|
|
200
|
+
return 'newer-edits-kept';
|
|
201
|
+
}
|
|
202
|
+
const { localRevision } = current;
|
|
203
|
+
const submission = submitted ? 'confirmed' : current.submission;
|
|
204
|
+
this.#publishOpen({ persistence: 'clearing', error: null, submission });
|
|
205
|
+
try {
|
|
206
|
+
await this.#store.delete(session.key);
|
|
207
|
+
}
|
|
208
|
+
catch (cause) {
|
|
209
|
+
const error = persistenceError('delete', cause);
|
|
210
|
+
this.#publishFor(session, { persistence: 'failed', error });
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
if (this.#currentFor(session) !== null) {
|
|
214
|
+
this.#applyCleared(session, localRevision, submitted);
|
|
215
|
+
}
|
|
216
|
+
return 'cleared';
|
|
217
|
+
}
|
|
218
|
+
#applyCleared(session, localRevision, submitted) {
|
|
219
|
+
const base = { recovery: 'none', persistence: 'idle', error: null };
|
|
220
|
+
if (submitted) {
|
|
221
|
+
this.#persistedRevision = localRevision;
|
|
222
|
+
this.#publishOpen(base);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const current = this.#current();
|
|
226
|
+
const nextRevision = (current?.localRevision ?? localRevision) + 1;
|
|
227
|
+
this.#persistedRevision = nextRevision;
|
|
228
|
+
this.#publishOpen({
|
|
229
|
+
...base,
|
|
230
|
+
value: session.initial,
|
|
231
|
+
localRevision: nextRevision,
|
|
232
|
+
submission: 'none',
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
#enqueue(operation) {
|
|
236
|
+
const run = this.#tail.then(operation);
|
|
237
|
+
this.#tail = run.catch(() => undefined);
|
|
238
|
+
return run;
|
|
239
|
+
}
|
|
240
|
+
#current() {
|
|
241
|
+
return this.#snapshot.open ? this.#snapshot : null;
|
|
242
|
+
}
|
|
243
|
+
#currentFor(session) {
|
|
244
|
+
return session === this.#session ? this.#current() : null;
|
|
245
|
+
}
|
|
246
|
+
#publishFor(session, changes) {
|
|
247
|
+
if (this.#currentFor(session) !== null) {
|
|
248
|
+
this.#publishOpen(changes);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
#publishOpen(changes) {
|
|
252
|
+
const current = this.#current();
|
|
253
|
+
if (current === null) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const next = { ...current, ...changes };
|
|
257
|
+
this.#publish({ ...next, dirty: next.localRevision !== this.#persistedRevision });
|
|
258
|
+
}
|
|
259
|
+
#publish(snapshot) {
|
|
260
|
+
this.#snapshot = snapshot;
|
|
261
|
+
for (const listener of this.#listeners) {
|
|
262
|
+
listener();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/** Creates a framework-free durable draft over a `KeyValueStore`. */
|
|
267
|
+
export function createDurableDraft(options) {
|
|
268
|
+
return new DefaultDurableDraft(options);
|
|
269
|
+
}
|
|
270
|
+
//# sourceMappingURL=durable-draft.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"durable-draft.js","sourceRoot":"","sources":["../src/durable-draft.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAsB3F,4FAA4F;AAC5F,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAK5B;IAJO,IAAI,GAAG,u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AI,OAAO,KAAK,IAAI,EAAE,CAAC;YACrB,OAAO;QACT,CAAC;QACD,MAAM,IAAI,GAAG,EAAE,GAAG,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ,CAAC,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,aAAa,KAAK,IAAI,CAAC,kBAAkB,EAAE,CAAC,CAAC;IACpF,CAAC;IAED,QAAQ,CAAC,QAA8C;QACrD,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACvC,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;CACF;AAED,qEAAqE;AACrE,MAAM,UAAU,kBAAkB,CAChC,OAA4C;IAE5C,OAAO,IAAI,mBAAmB,CAAC,OAAO,CAAC,CAAC;AAC1C,CAAC"}
|
package/dist/export.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface CsvNumericCell {
|
|
2
|
+
readonly numeric: string;
|
|
3
|
+
}
|
|
4
|
+
export type CsvCell = string | number | CsvNumericCell | null;
|
|
5
|
+
export interface EncodeCsvOptions {
|
|
6
|
+
readonly neutralizeFormulas?: boolean;
|
|
7
|
+
readonly byteOrderMark?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export type CsvEncodeErrorCode = 'invalid_numeric_cell' | 'invalid_unicode' | 'unsupported_cell';
|
|
10
|
+
export declare class CsvEncodeError extends Error {
|
|
11
|
+
readonly name = "CsvEncodeError";
|
|
12
|
+
readonly code: CsvEncodeErrorCode;
|
|
13
|
+
readonly rowIndex: number;
|
|
14
|
+
readonly columnIndex: number;
|
|
15
|
+
constructor(code: CsvEncodeErrorCode, rowIndex: number, columnIndex: number);
|
|
16
|
+
}
|
|
17
|
+
export declare const SHARE_OUTCOMES: readonly ["shared", "saved", "cancelled", "unavailable", "failed"];
|
|
18
|
+
export type ShareOutcome = (typeof SHARE_OUTCOMES)[number];
|
|
19
|
+
export declare function csvNumeric(value: string): CsvNumericCell;
|
|
20
|
+
export declare function encodeCsv(rows: Iterable<readonly CsvCell[]>, options?: EncodeCsvOptions): string;
|
|
21
|
+
//# sourceMappingURL=export.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"export.d.ts","sourceRoot":"","sources":["../src/export.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,cAAc,GAAG,IAAI,CAAC;AAE9D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IACtC,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;CAClC;AAED,MAAM,MAAM,kBAAkB,GAAG,sBAAsB,GAAG,iBAAiB,GAAG,kBAAkB,CAAC;AAEjG,qBAAa,cAAe,SAAQ,KAAK;IACvC,SAAyB,IAAI,oBAAoB;IACjD,SAAgB,IAAI,EAAE,kBAAkB,CAAC;IACzC,SAAgB,QAAQ,EAAE,MAAM,CAAC;IACjC,SAAgB,WAAW,EAAE,MAAM,CAAC;gBAEjB,IAAI,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM;CAMnF;AAED,eAAO,MAAM,cAAc,oEAAqE,CAAC;AAEjG,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAoB3D,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAExD;AAED,wBAAgB,SAAS,CACvB,IAAI,EAAE,QAAQ,CAAC,SAAS,OAAO,EAAE,CAAC,EAClC,OAAO,GAAE,gBAAqB,GAC7B,MAAM,CASR"}
|