@livestore/utils 0.4.0-dev.20 → 0.4.0-dev.21
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/dist/.tsbuildinfo.json +1 -1
- package/dist/NoopTracer.d.ts.map +1 -1
- package/dist/NoopTracer.js +4 -1
- package/dist/NoopTracer.js.map +1 -1
- package/dist/browser/Opfs/utils.d.ts +7 -4
- package/dist/browser/Opfs/utils.d.ts.map +1 -1
- package/dist/browser/Opfs/utils.js +30 -18
- package/dist/browser/Opfs/utils.js.map +1 -1
- package/dist/effect/Debug.d.ts +28 -30
- package/dist/effect/Debug.d.ts.map +1 -1
- package/dist/effect/Debug.js +280 -221
- package/dist/effect/Debug.js.map +1 -1
- package/dist/effect/Schema/debug-diff.test.js +1 -1
- package/dist/effect/Schema/debug-diff.test.js.map +1 -1
- package/dist/effect/mod.d.ts +1 -1
- package/dist/effect/mod.d.ts.map +1 -1
- package/dist/effect/mod.js +1 -1
- package/dist/effect/mod.js.map +1 -1
- package/dist/global.d.ts +3 -0
- package/dist/global.d.ts.map +1 -1
- package/dist/global.js.map +1 -1
- package/dist/guards.d.ts +14 -0
- package/dist/guards.d.ts.map +1 -1
- package/dist/guards.js +14 -0
- package/dist/guards.js.map +1 -1
- package/dist/mod.d.ts +195 -3
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +149 -4
- package/dist/mod.js.map +1 -1
- package/package.json +20 -20
- package/src/NoopTracer.ts +4 -1
- package/src/browser/Opfs/utils.ts +36 -20
- package/src/effect/Debug.ts +369 -292
- package/src/effect/Schema/debug-diff.test.ts +2 -2
- package/src/effect/mod.ts +3 -0
- package/src/global.ts +4 -0
- package/src/guards.ts +15 -0
- package/src/mod.ts +197 -5
package/dist/mod.js
CHANGED
|
@@ -14,13 +14,36 @@ export * from "./set.js";
|
|
|
14
14
|
export * from "./string.js";
|
|
15
15
|
export * from "./time.js";
|
|
16
16
|
import { objectToString } from "./misc.js";
|
|
17
|
+
/** Returns a Promise that resolves after `ms` milliseconds. */
|
|
17
18
|
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
+
/**
|
|
20
|
+
* Creates a mutable reference object with a `current` property.
|
|
21
|
+
* Similar to React's `useRef` but works outside of React.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const counter = ref(0)
|
|
26
|
+
* counter.current += 1
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
18
29
|
export const ref = (val) => ({ current: val });
|
|
30
|
+
/**
|
|
31
|
+
* Calls a function `n` times with the current index.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* times(3, (i) => console.log(i)) // logs 0, 1, 2
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
19
38
|
export const times = (n, fn) => {
|
|
20
39
|
for (let i = 0; i < n; i++) {
|
|
21
40
|
fn(i);
|
|
22
41
|
}
|
|
23
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* Wraps a function call in a try/catch that triggers the debugger on error.
|
|
45
|
+
* Useful for debugging exceptions during development.
|
|
46
|
+
*/
|
|
24
47
|
export const debugCatch = (try_) => {
|
|
25
48
|
try {
|
|
26
49
|
return try_();
|
|
@@ -31,6 +54,10 @@ export const debugCatch = (try_) => {
|
|
|
31
54
|
throw e;
|
|
32
55
|
}
|
|
33
56
|
};
|
|
57
|
+
/**
|
|
58
|
+
* Recursively removes `undefined` values from an object or array in place.
|
|
59
|
+
* Mutates the input value.
|
|
60
|
+
*/
|
|
34
61
|
export const recRemoveUndefinedValues = (val) => {
|
|
35
62
|
if (Array.isArray(val)) {
|
|
36
63
|
val.forEach(recRemoveUndefinedValues);
|
|
@@ -50,8 +77,19 @@ export const recRemoveUndefinedValues = (val) => {
|
|
|
50
77
|
* Replace non-alphanumeric characters with a dash.
|
|
51
78
|
*/
|
|
52
79
|
export const sluggify = (str, separator = '-') => str.replace(/[^a-zA-Z0-9]/g, separator);
|
|
80
|
+
/**
|
|
81
|
+
* Creates a property accessor function for use in pipelines.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const users = [{ name: 'Alice' }, { name: 'Bob' }]
|
|
86
|
+
* const names = users.map(prop('name')) // ['Alice', 'Bob']
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
53
89
|
export const prop = (key) => (obj) => obj[key];
|
|
90
|
+
/** Capitalizes the first letter of a string. */
|
|
54
91
|
export const capitalizeFirstLetter = (str) => str.charAt(0).toUpperCase() + str.slice(1);
|
|
92
|
+
/** Type guard that checks if a value is a readonly array. */
|
|
55
93
|
export const isReadonlyArray = (value) => Array.isArray(value);
|
|
56
94
|
/**
|
|
57
95
|
* Use this to make assertion at end of if-else chain that all members of a
|
|
@@ -62,6 +100,14 @@ export function casesHandled(unexpectedCase) {
|
|
|
62
100
|
debugger;
|
|
63
101
|
throw new Error(`A case was not handled for value: ${truncate(objectToString(unexpectedCase), 1000)}`);
|
|
64
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Throws if the condition is false. Use for runtime assertions that should never fail.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* assertNever(user !== undefined, 'User must be loaded')
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
65
111
|
export const assertNever = (failIfFalse, msg) => {
|
|
66
112
|
if (failIfFalse === false) {
|
|
67
113
|
// biome-ignore lint/suspicious/noDebugger: debugging
|
|
@@ -69,6 +115,14 @@ export const assertNever = (failIfFalse, msg) => {
|
|
|
69
115
|
throw new Error(`This should never happen: ${msg}`);
|
|
70
116
|
}
|
|
71
117
|
};
|
|
118
|
+
/**
|
|
119
|
+
* Identity function that triggers the debugger. Useful for debugging pipelines.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* data.pipe(transform, debuggerPipe, format) // Pauses debugger here
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
72
126
|
export const debuggerPipe = (val) => {
|
|
73
127
|
// biome-ignore lint/suspicious/noDebugger: debugging
|
|
74
128
|
debugger;
|
|
@@ -82,12 +136,35 @@ const truncate = (str, length) => {
|
|
|
82
136
|
return str;
|
|
83
137
|
}
|
|
84
138
|
};
|
|
139
|
+
/**
|
|
140
|
+
* Placeholder for unimplemented code paths. Triggers debugger and throws.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* const parseFormat = (format: Format) => {
|
|
145
|
+
* switch (format) {
|
|
146
|
+
* case 'json': return parseJson
|
|
147
|
+
* case 'xml': return notYetImplemented('XML parsing')
|
|
148
|
+
* }
|
|
149
|
+
* }
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
85
152
|
export const notYetImplemented = (msg) => {
|
|
86
153
|
// biome-ignore lint/suspicious/noDebugger: debugging
|
|
87
154
|
debugger;
|
|
88
155
|
throw new Error(`Not yet implemented: ${msg}`);
|
|
89
156
|
};
|
|
157
|
+
/** A function that does nothing. Useful as a default callback. */
|
|
90
158
|
export const noop = () => { };
|
|
159
|
+
/**
|
|
160
|
+
* If the input is a function, calls it and returns the result. Otherwise returns the value directly.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* unwrapThunk(5) // 5
|
|
165
|
+
* unwrapThunk(() => 5) // 5
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
91
168
|
export const unwrapThunk = (_) => {
|
|
92
169
|
if (typeof _ === 'function') {
|
|
93
170
|
return _();
|
|
@@ -96,11 +173,32 @@ export const unwrapThunk = (_) => {
|
|
|
96
173
|
return _;
|
|
97
174
|
}
|
|
98
175
|
};
|
|
99
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* Creates an array of numbers from `start` (inclusive) to `end` (exclusive).
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* range(0, 5) // [0, 1, 2, 3, 4]
|
|
182
|
+
* range(3, 7) // [3, 4, 5, 6]
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
100
185
|
export const range = (start, end) => {
|
|
101
186
|
const length = end - start;
|
|
102
187
|
return Array.from({ length }, (_, i) => start + i);
|
|
103
188
|
};
|
|
189
|
+
/**
|
|
190
|
+
* Rate-limits function calls to at most once per `ms` milliseconds.
|
|
191
|
+
* Trailing calls are preserved—if called during the wait period, the function
|
|
192
|
+
* will be called again after the timeout.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* const throttledSave = throttle(() => saveData(), 1000)
|
|
197
|
+
* throttledSave() // Executes immediately
|
|
198
|
+
* throttledSave() // Queued, executes after 1 second
|
|
199
|
+
* throttledSave() // Ignored (already queued)
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
104
202
|
export const throttle = (fn, ms) => {
|
|
105
203
|
let shouldWait = false;
|
|
106
204
|
let shouldCallAgain = false;
|
|
@@ -124,18 +222,45 @@ export const throttle = (fn, ms) => {
|
|
|
124
222
|
setTimeout(timeoutFunc, ms);
|
|
125
223
|
};
|
|
126
224
|
};
|
|
225
|
+
/**
|
|
226
|
+
* Generates a W3C Trace Context `traceparent` header from an OpenTelemetry span.
|
|
227
|
+
* @see https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers
|
|
228
|
+
*/
|
|
127
229
|
export const getTraceParentHeader = (parentSpan) => {
|
|
128
230
|
const spanContext = parentSpan.spanContext();
|
|
129
|
-
// Format: {version}-{trace_id}-{span_id}-{trace_flags}
|
|
130
|
-
// https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers
|
|
131
231
|
return `00-${spanContext.traceId}-${spanContext.spanId}-01`;
|
|
132
232
|
};
|
|
233
|
+
/**
|
|
234
|
+
* Asserts that a tagged union value has a specific tag, narrowing its type.
|
|
235
|
+
* Throws if the tag doesn't match.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* type Result = { _tag: 'ok'; value: number } | { _tag: 'error'; message: string }
|
|
240
|
+
* const result: Result = ...
|
|
241
|
+
* const ok = assertTag(result, 'ok') // Type is { _tag: 'ok'; value: number }
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
133
244
|
export const assertTag = (obj, tag) => {
|
|
134
245
|
if (obj._tag !== tag) {
|
|
135
246
|
throw new Error(`Expected tag ${tag} but got ${obj._tag}`);
|
|
136
247
|
}
|
|
137
248
|
return obj;
|
|
138
249
|
};
|
|
250
|
+
/**
|
|
251
|
+
* Memoizes a function by JSON-stringifying its arguments as the cache key.
|
|
252
|
+
* Suitable for functions with serializable arguments.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* const expensiveCalc = memoizeByStringifyArgs((a: number, b: number) => {
|
|
257
|
+
* console.log('Computing...')
|
|
258
|
+
* return a + b
|
|
259
|
+
* })
|
|
260
|
+
* expensiveCalc(1, 2) // logs 'Computing...', returns 3
|
|
261
|
+
* expensiveCalc(1, 2) // returns 3 (cached, no log)
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
139
264
|
export const memoizeByStringifyArgs = (fn) => {
|
|
140
265
|
const cache = new Map();
|
|
141
266
|
return ((...args) => {
|
|
@@ -148,6 +273,18 @@ export const memoizeByStringifyArgs = (fn) => {
|
|
|
148
273
|
return result;
|
|
149
274
|
});
|
|
150
275
|
};
|
|
276
|
+
/**
|
|
277
|
+
* Memoizes a single-argument function using reference equality for cache lookup.
|
|
278
|
+
* Suitable for functions where arguments are objects that should be compared by reference.
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* const processUser = memoizeByRef((user: User) => expensiveTransform(user))
|
|
283
|
+
* processUser(userA) // Computes
|
|
284
|
+
* processUser(userA) // Returns cached (same reference)
|
|
285
|
+
* processUser(userB) // Computes (different reference)
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
151
288
|
export const memoizeByRef = (fn) => {
|
|
152
289
|
const cache = new Map();
|
|
153
290
|
return ((arg) => {
|
|
@@ -159,12 +296,20 @@ export const memoizeByRef = (fn) => {
|
|
|
159
296
|
return result;
|
|
160
297
|
});
|
|
161
298
|
};
|
|
299
|
+
/** Type guard that checks if a value is a non-empty string. */
|
|
162
300
|
export const isNonEmptyString = (str) => {
|
|
163
301
|
return typeof str === 'string' && str.length > 0;
|
|
164
302
|
};
|
|
303
|
+
/** Type guard that checks if a value is a Promise (has a `then` method). */
|
|
165
304
|
export const isPromise = (value) => typeof value?.then === 'function';
|
|
305
|
+
/** Type guard that checks if a value is iterable (has a `Symbol.iterator` method). */
|
|
166
306
|
export const isIterable = (value) => typeof value?.[Symbol.iterator] === 'function';
|
|
167
|
-
/**
|
|
307
|
+
/**
|
|
308
|
+
* Type-level utility that removes `undefined` from all property types.
|
|
309
|
+
* Used for compatibility with libraries that don't type optionals as `| undefined`.
|
|
310
|
+
*
|
|
311
|
+
* Note: This is a type-level lie—the runtime value is unchanged.
|
|
312
|
+
*/
|
|
168
313
|
export const omitUndefineds = (rec) => {
|
|
169
314
|
return rec;
|
|
170
315
|
};
|
package/dist/mod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,cAAc,CAAA;AACrD,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,UAAU,CAAA;AACxB,cAAc,sBAAsB,CAAA;AACpC,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,EAAE,MAAM,SAAS,CAAA;AAC7B,cA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
"type": "module",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"./src/global.ts",
|
|
@@ -50,29 +50,29 @@
|
|
|
50
50
|
"qrcode-generator": "2.0.4"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@effect/ai": "
|
|
54
|
-
"@effect/cli": "
|
|
55
|
-
"@effect/cluster": "
|
|
56
|
-
"@effect/experimental": "
|
|
53
|
+
"@effect/ai": "0.29.0",
|
|
54
|
+
"@effect/cli": "0.71.0",
|
|
55
|
+
"@effect/cluster": "0.50.6",
|
|
56
|
+
"@effect/experimental": "0.56.0",
|
|
57
57
|
"@effect/opentelemetry": "0.58.0",
|
|
58
|
-
"@effect/platform": "
|
|
59
|
-
"@effect/platform-browser": "
|
|
60
|
-
"@effect/platform-bun": "
|
|
61
|
-
"@effect/platform-node": "
|
|
62
|
-
"@effect/printer": "
|
|
63
|
-
"@effect/printer-ansi": "
|
|
64
|
-
"@effect/rpc": "
|
|
65
|
-
"@effect/sql": "
|
|
66
|
-
"@effect/typeclass": "
|
|
67
|
-
"@effect/vitest": "
|
|
68
|
-
"@effect/workflow": "
|
|
58
|
+
"@effect/platform": "0.92.1",
|
|
59
|
+
"@effect/platform-browser": "0.72.0",
|
|
60
|
+
"@effect/platform-bun": "0.81.1",
|
|
61
|
+
"@effect/platform-node": "0.98.4",
|
|
62
|
+
"@effect/printer": "0.46.0",
|
|
63
|
+
"@effect/printer-ansi": "0.46.0",
|
|
64
|
+
"@effect/rpc": "0.71.1",
|
|
65
|
+
"@effect/sql": "0.46.0",
|
|
66
|
+
"@effect/typeclass": "0.37.0",
|
|
67
|
+
"@effect/vitest": "0.26.0",
|
|
68
|
+
"@effect/workflow": "0.11.5",
|
|
69
69
|
"@opentelemetry/api": "1.9.0",
|
|
70
70
|
"@opentelemetry/resources": "2.0.1",
|
|
71
|
-
"@types/bun": "
|
|
71
|
+
"@types/bun": "1.2.21",
|
|
72
72
|
"@types/jsdom": "^21.1.7",
|
|
73
73
|
"@types/node": "24.10.1",
|
|
74
|
-
"@types/web": "
|
|
75
|
-
"effect": "3.
|
|
74
|
+
"@types/web": "0.0.264",
|
|
75
|
+
"effect": "3.19.9",
|
|
76
76
|
"jsdom": "^26.1.0",
|
|
77
77
|
"vitest": "3.2.4"
|
|
78
78
|
},
|
|
@@ -99,7 +99,7 @@
|
|
|
99
99
|
"@effect/typeclass": "^0.37.0",
|
|
100
100
|
"@opentelemetry/api": "^1.9.0",
|
|
101
101
|
"@opentelemetry/resources": "^2.0.1",
|
|
102
|
-
"effect": "^3.
|
|
102
|
+
"effect": "^3.19.9"
|
|
103
103
|
},
|
|
104
104
|
"publishConfig": {
|
|
105
105
|
"access": "public"
|
package/src/NoopTracer.ts
CHANGED
|
@@ -21,7 +21,10 @@ export const makeNoopSpan = () => {
|
|
|
21
21
|
;(span as any)._duration = [durationSecs, durationRestNs]
|
|
22
22
|
},
|
|
23
23
|
spanContext: () => {
|
|
24
|
-
return {
|
|
24
|
+
return {
|
|
25
|
+
traceId: `livestore-noop-trace-id${crypto.randomUUID()}`,
|
|
26
|
+
spanId: `livestore-noop-span-id${crypto.randomUUID()}`,
|
|
27
|
+
}
|
|
25
28
|
},
|
|
26
29
|
_duration: [0, 0],
|
|
27
30
|
} as unknown as otel.Span
|
|
@@ -210,6 +210,9 @@ export const getMetadata = Effect.fn('@livestore/utils:Opfs.getMetadata')(functi
|
|
|
210
210
|
*
|
|
211
211
|
* @param path - Slash-delimited file path.
|
|
212
212
|
* @param data - Bytes to persist.
|
|
213
|
+
*
|
|
214
|
+
* @remarks
|
|
215
|
+
* - Only available in Safari 26 or higher (as of Dec 2025, not yet widely available).
|
|
213
216
|
*/
|
|
214
217
|
export const writeFile = Effect.fn('@livestore/utils:Opfs.writeFile')(function* (path: string, data: Uint8Array) {
|
|
215
218
|
if (isRootPath(path)) {
|
|
@@ -234,10 +237,10 @@ export const writeFile = Effect.fn('@livestore/utils:Opfs.writeFile')(function*
|
|
|
234
237
|
})
|
|
235
238
|
|
|
236
239
|
/**
|
|
237
|
-
* Synchronously write bytes to
|
|
240
|
+
* Synchronously write bytes to an OPFS path, creating or replacing the target file.
|
|
238
241
|
*
|
|
239
|
-
* @param
|
|
240
|
-
* @param
|
|
242
|
+
* @param path - Slash-delimited file path.
|
|
243
|
+
* @param data - Bytes to persist.
|
|
241
244
|
* @returns Effect that resolves once every byte is flushed to durable storage.
|
|
242
245
|
*
|
|
243
246
|
* @remarks
|
|
@@ -246,25 +249,38 @@ export const writeFile = Effect.fn('@livestore/utils:Opfs.writeFile')(function*
|
|
|
246
249
|
* For atomic replacement, prefer `writeFile` or a temp-file pattern with two prepared handles.
|
|
247
250
|
*/
|
|
248
251
|
export const syncWriteFile = Effect.fn('@livestore/utils:Opfs.syncWriteFile')(function* (
|
|
249
|
-
|
|
250
|
-
|
|
252
|
+
path: string,
|
|
253
|
+
data: Uint8Array,
|
|
251
254
|
) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
+
if (isRootPath(path)) {
|
|
256
|
+
return yield* new OpfsError({
|
|
257
|
+
message: `Invalid OPFS path '${path}': cannot write file directly to the OPFS root`,
|
|
258
|
+
})
|
|
259
|
+
}
|
|
255
260
|
|
|
256
|
-
yield*
|
|
261
|
+
const pathSegments = yield* parsePathSegments(path)
|
|
262
|
+
const { parentSegments, leafSegment: fileName } = splitPathSegments(pathSegments)
|
|
257
263
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
264
|
+
return yield* Effect.scoped(
|
|
265
|
+
Effect.gen(function* () {
|
|
266
|
+
const parentDirHandle = yield* traverseDirectoryPath(parentSegments)
|
|
267
|
+
const fileHandle = yield* Opfs.getFileHandle(parentDirHandle, fileName, { create: true })
|
|
268
|
+
const syncHandle = yield* Opfs.createSyncAccessHandle(fileHandle)
|
|
269
|
+
|
|
270
|
+
yield* Opfs.syncTruncate(syncHandle, 0)
|
|
271
|
+
|
|
272
|
+
let offset = 0
|
|
273
|
+
while (offset < data.byteLength) {
|
|
274
|
+
const wrote = yield* Opfs.syncWrite(syncHandle, data.subarray(offset), { at: offset })
|
|
275
|
+
if (wrote === 0) {
|
|
276
|
+
return yield* new OpfsError({
|
|
277
|
+
message: `Short write: wrote ${offset} of ${data.byteLength} bytes.`,
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
offset += Number(wrote)
|
|
281
|
+
}
|
|
268
282
|
|
|
269
|
-
|
|
283
|
+
yield* Opfs.syncFlush(syncHandle)
|
|
284
|
+
}),
|
|
285
|
+
)
|
|
270
286
|
})
|