@rljson/fs-agent 0.0.81 → 0.0.82
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/blob-io.d.ts +37 -0
- package/dist/fs-agent.d.ts +42 -4
- package/dist/fs-agent.js +126 -15
- package/dist/fs-blob-adapter.d.ts +5 -1
- package/package.json +10 -4
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BlobProperties, Bs } from '@rljson/bs';
|
|
2
|
+
import { FileHandle } from 'fs/promises';
|
|
3
|
+
/**
|
|
4
|
+
* Above this size, a file is streamed into blob storage instead of read whole.
|
|
5
|
+
*
|
|
6
|
+
* **A threshold rather than "always stream", because both costs are real.** A
|
|
7
|
+
* stream bounds memory to one chunk; it also sets up a reader, a queue and a
|
|
8
|
+
* per-chunk round trip, and the scanner runs this once per file across trees of
|
|
9
|
+
* hundreds of thousands of small files. Reading a file no bigger than one chunk
|
|
10
|
+
* whole costs no more memory than streaming it would, so below the line the
|
|
11
|
+
* cheaper call is also the bounded one and there is nothing to trade.
|
|
12
|
+
*
|
|
13
|
+
* Above it, the whole-file read is what put 487 MB of ArrayBuffers on the cloud
|
|
14
|
+
* hub's heap and what made a 63 MB file unsyncable — a single socket message
|
|
15
|
+
* over the 50 MB cap, refused by the transport with no way around it.
|
|
16
|
+
*
|
|
17
|
+
* The number is deliberately the same order as `@rljson/bs`'s
|
|
18
|
+
* `BLOB_CHUNK_BYTES`, and deliberately NOT imported from it. Nothing breaks if
|
|
19
|
+
* the two drift: this decides which of two correct paths a file takes, not what
|
|
20
|
+
* either path does. A constant that has to match across a package boundary to
|
|
21
|
+
* stay correct is a contract, and a contract that lives in a tuning value is
|
|
22
|
+
* one nobody will keep.
|
|
23
|
+
*/
|
|
24
|
+
export declare const STREAM_ABOVE_BYTES: number;
|
|
25
|
+
/**
|
|
26
|
+
* Stores an already-open file in blob storage, streaming it when it is large.
|
|
27
|
+
*
|
|
28
|
+
* Takes an open handle rather than a path on purpose: opening is where a
|
|
29
|
+
* vanished file announces itself, and every caller here already has to classify
|
|
30
|
+
* that error its own way. By the time the bytes move, the descriptor is held, so
|
|
31
|
+
* a file deleted underneath the transfer cannot truncate it.
|
|
32
|
+
* @param bs - Where the blob goes.
|
|
33
|
+
* @param handle - The open file.
|
|
34
|
+
* @param size - Its size, already known to every caller from its stat.
|
|
35
|
+
* @returns Properties of the stored blob.
|
|
36
|
+
*/
|
|
37
|
+
export declare const storeFileAsBlob: (bs: Bs, handle: FileHandle, size: number) => Promise<BlobProperties>;
|
package/dist/fs-agent.d.ts
CHANGED
|
@@ -256,9 +256,16 @@ export declare class PartialRestoreError extends RestoreIncompleteError {
|
|
|
256
256
|
* The difference from {@link PartialRestoreError} is "not yet" versus "never".
|
|
257
257
|
* A locked file's bytes exist and the file is merely busy, so retrying is the
|
|
258
258
|
* right answer. A blob that cannot be retrieved may never become retrievable —
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
259
|
+
* an offline peer that held the only copy, a blob deleted between the tree and
|
|
260
|
+
* the fetch.
|
|
261
|
+
*
|
|
262
|
+
* **Size is no longer one of those reasons.** The case that produced this class
|
|
263
|
+
* was a file larger than the transport's `maxHttpBufferSize`, where no number of
|
|
264
|
+
* retries made 63 MB fit through a 50 MB socket. Since `@rljson/bs` 0.0.27 a
|
|
265
|
+
* blob crosses as a series of ranged pulls, so no single message carries the
|
|
266
|
+
* whole file and the cap no longer bounds file size. The class stays, because a
|
|
267
|
+
* blob can still be genuinely unreachable; the reason it was first needed is
|
|
268
|
+
* gone.
|
|
262
269
|
*
|
|
263
270
|
* It exists because the failure used to be a bare `throw` in the middle of
|
|
264
271
|
* `_restoreTree`, which abandoned the ENTIRE tree: no other file in it was
|
|
@@ -272,7 +279,8 @@ export declare class PartialRestoreError extends RestoreIncompleteError {
|
|
|
272
279
|
* Measured on four nodes: one 63 MB file (120% of the cap) left three of them
|
|
273
280
|
* holding a file the fourth had deleted, permanently, and `projekte-shape`'s
|
|
274
281
|
* deletion had still not propagated after 300 seconds. The customer's largest
|
|
275
|
-
* file
|
|
282
|
+
* file was 45.9 MB against the same 50 MB cap — close enough that the next
|
|
283
|
+
* revision of one document would have reproduced it in production.
|
|
276
284
|
*/
|
|
277
285
|
export declare class BlobUnavailableError extends RestoreIncompleteError {
|
|
278
286
|
readonly unavailablePaths: string[];
|
|
@@ -510,6 +518,36 @@ export declare class FsAgent {
|
|
|
510
518
|
* @param content - Bytes to write
|
|
511
519
|
*/
|
|
512
520
|
private static _atomicWriteFile;
|
|
521
|
+
/**
|
|
522
|
+
* Whether this error came from reading the blob rather than writing the file.
|
|
523
|
+
*
|
|
524
|
+
* The two have different answers. A blob that cannot be fetched costs one
|
|
525
|
+
* file and the tree applies without it; a file that cannot be written is
|
|
526
|
+
* usually a CARAT document a user still has open, and that one must not abort
|
|
527
|
+
* the rest of the restore either. Conflating them would report an offline peer
|
|
528
|
+
* as a locked file, and the field reports are read by people who act on that
|
|
529
|
+
* distinction.
|
|
530
|
+
* @param error - The error thrown while writing the file.
|
|
531
|
+
* @returns Whether it came from the blob source.
|
|
532
|
+
*/
|
|
533
|
+
private static _isBlobReadError;
|
|
534
|
+
/**
|
|
535
|
+
* Writes a file from a stream, holding one chunk at a time.
|
|
536
|
+
*
|
|
537
|
+
* The twin of {@link FsAgent._atomicWriteFile}, with the same platform rule
|
|
538
|
+
* and the same reason for it, but never materialising the whole file. A 500 MB
|
|
539
|
+
* file used to cost 500 MB of Buffer on the receiving agent, another copy in
|
|
540
|
+
* the socket parser, and — on the serving hub — the same again. That is the
|
|
541
|
+
* shape that killed the cloud EventHub: memory that is work in flight rather
|
|
542
|
+
* than garbage, so no collection can reclaim any of it.
|
|
543
|
+
*
|
|
544
|
+
* Read failures are tagged, because from here on the bytes arrive during the
|
|
545
|
+
* write rather than before it, and {@link FsAgent._isBlobReadError} is what
|
|
546
|
+
* keeps the caller's two error messages telling the truth.
|
|
547
|
+
* @param filePath - Where the file goes.
|
|
548
|
+
* @param stream - The bytes.
|
|
549
|
+
*/
|
|
550
|
+
private static _atomicWriteStream;
|
|
513
551
|
/**
|
|
514
552
|
* Wraps a promise with a timeout.
|
|
515
553
|
* Rejects with a descriptive error if the promise does not settle
|
package/dist/fs-agent.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BsMem } from "@rljson/bs";
|
|
2
2
|
import { Route, createTreesTableCfg } from "@rljson/rljson";
|
|
3
3
|
import { watch, writeFileSync, existsSync, readFileSync, appendFileSync } from "fs";
|
|
4
|
-
import { stat,
|
|
4
|
+
import { stat, open, mkdir, utimes, readdir, readFile, writeFile, rename, unlink, rm } from "fs/promises";
|
|
5
5
|
import { dirname, join } from "path";
|
|
6
6
|
import { hip } from "@rljson/hash";
|
|
7
7
|
import { stateBeaconEvent, Db } from "@rljson/db";
|
|
@@ -120,6 +120,13 @@ class FsAntiEntropy {
|
|
|
120
120
|
this._deps.repair(decision, hubRef, [...hubPredecessors], this._attempts);
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
|
+
const STREAM_ABOVE_BYTES = 4 * 1024 * 1024;
|
|
124
|
+
const storeFileAsBlob = async (bs, handle, size) => {
|
|
125
|
+
if (size <= STREAM_ABOVE_BYTES) {
|
|
126
|
+
return bs.setBlob(await handle.readFile());
|
|
127
|
+
}
|
|
128
|
+
return bs.setBlob(handle.readableWebStream());
|
|
129
|
+
};
|
|
123
130
|
class FsBlobAdapter {
|
|
124
131
|
_bs;
|
|
125
132
|
constructor(bs) {
|
|
@@ -141,8 +148,13 @@ class FsBlobAdapter {
|
|
|
141
148
|
const bs = options.bs || this._bs;
|
|
142
149
|
const includePath = options.includePath !== false;
|
|
143
150
|
const stats = await stat(filePath);
|
|
144
|
-
const
|
|
145
|
-
|
|
151
|
+
const handle = await open(filePath, "r");
|
|
152
|
+
let blobProps;
|
|
153
|
+
try {
|
|
154
|
+
blobProps = await storeFileAsBlob(bs, handle, stats.size);
|
|
155
|
+
} finally {
|
|
156
|
+
await handle.close();
|
|
157
|
+
}
|
|
146
158
|
const name = filePath.split("/").pop() || filePath.split("\\").pop() || "";
|
|
147
159
|
const metadata = {
|
|
148
160
|
name,
|
|
@@ -177,16 +189,25 @@ class FsBlobAdapter {
|
|
|
177
189
|
const bs = options.bs || this._bs;
|
|
178
190
|
const createDirs = options.createDirs !== false;
|
|
179
191
|
const preserveMtime = options.preserveMtime !== false;
|
|
180
|
-
const blob = await bs.getBlob(metadata.blobId);
|
|
181
192
|
if (createDirs) {
|
|
182
193
|
const dir = dirname(targetPath);
|
|
183
194
|
await mkdir(dir, { recursive: true });
|
|
184
195
|
}
|
|
185
|
-
await
|
|
196
|
+
const stream = await bs.getBlobStream(metadata.blobId);
|
|
197
|
+
const handle = await open(targetPath, "w");
|
|
198
|
+
try {
|
|
199
|
+
const reader = stream.getReader();
|
|
200
|
+
for (; ; ) {
|
|
201
|
+
const { done, value } = await reader.read();
|
|
202
|
+
if (done) break;
|
|
203
|
+
await handle.write(value);
|
|
204
|
+
}
|
|
205
|
+
} finally {
|
|
206
|
+
await handle.close();
|
|
207
|
+
}
|
|
186
208
|
if (preserveMtime && metadata.mtime) {
|
|
187
|
-
const { utimes: utimes2 } = await import("fs/promises");
|
|
188
209
|
const mtime = new Date(metadata.mtime);
|
|
189
|
-
await
|
|
210
|
+
await utimes(targetPath, mtime, mtime);
|
|
190
211
|
}
|
|
191
212
|
}
|
|
192
213
|
/**
|
|
@@ -202,7 +223,11 @@ class FsBlobAdapter {
|
|
|
202
223
|
}
|
|
203
224
|
}
|
|
204
225
|
/**
|
|
205
|
-
* Retrieves file content from blob storage
|
|
226
|
+
* Retrieves file content from blob storage.
|
|
227
|
+
*
|
|
228
|
+
* Returns a Buffer, so the whole blob is in memory by definition — that is
|
|
229
|
+
* what the caller asked for. Anything writing the bytes to a destination
|
|
230
|
+
* should use {@link FsBlobAdapter.blobToFile} instead, which streams.
|
|
206
231
|
* @param blobId - Blob ID
|
|
207
232
|
* @returns File content as Buffer
|
|
208
233
|
*/
|
|
@@ -683,9 +708,9 @@ class FsScanner {
|
|
|
683
708
|
if (cached && cached.mtime === mtimeMs && cached.size === childStats.size) {
|
|
684
709
|
blobId = cached.blobId;
|
|
685
710
|
} else {
|
|
686
|
-
let
|
|
711
|
+
let handle;
|
|
687
712
|
try {
|
|
688
|
-
|
|
713
|
+
handle = await open(childPath, "r");
|
|
689
714
|
} catch (error) {
|
|
690
715
|
if (FsScanner._isVanished(error)) throw error;
|
|
691
716
|
throw new Error(
|
|
@@ -694,11 +719,17 @@ class FsScanner {
|
|
|
694
719
|
}
|
|
695
720
|
let blobProps;
|
|
696
721
|
try {
|
|
697
|
-
blobProps = await
|
|
722
|
+
blobProps = await storeFileAsBlob(
|
|
723
|
+
this._bs,
|
|
724
|
+
handle,
|
|
725
|
+
childStats.size
|
|
726
|
+
);
|
|
698
727
|
} catch (error) {
|
|
699
728
|
throw new Error(
|
|
700
729
|
`Failed to store blob for file "${childRelPath}": ${error instanceof Error ? error.message : String(error)}`
|
|
701
730
|
);
|
|
731
|
+
} finally {
|
|
732
|
+
await handle.close();
|
|
702
733
|
}
|
|
703
734
|
if (!blobProps || !blobProps.blobId) {
|
|
704
735
|
throw new Error(
|
|
@@ -1587,6 +1618,79 @@ ${err.stack}` : String(err);
|
|
|
1587
1618
|
throw err;
|
|
1588
1619
|
}
|
|
1589
1620
|
}
|
|
1621
|
+
/**
|
|
1622
|
+
* Whether this error came from reading the blob rather than writing the file.
|
|
1623
|
+
*
|
|
1624
|
+
* The two have different answers. A blob that cannot be fetched costs one
|
|
1625
|
+
* file and the tree applies without it; a file that cannot be written is
|
|
1626
|
+
* usually a CARAT document a user still has open, and that one must not abort
|
|
1627
|
+
* the rest of the restore either. Conflating them would report an offline peer
|
|
1628
|
+
* as a locked file, and the field reports are read by people who act on that
|
|
1629
|
+
* distinction.
|
|
1630
|
+
* @param error - The error thrown while writing the file.
|
|
1631
|
+
* @returns Whether it came from the blob source.
|
|
1632
|
+
*/
|
|
1633
|
+
static _isBlobReadError(error) {
|
|
1634
|
+
return typeof error === "object" && error !== null && error.__blobRead === true;
|
|
1635
|
+
}
|
|
1636
|
+
/**
|
|
1637
|
+
* Writes a file from a stream, holding one chunk at a time.
|
|
1638
|
+
*
|
|
1639
|
+
* The twin of {@link FsAgent._atomicWriteFile}, with the same platform rule
|
|
1640
|
+
* and the same reason for it, but never materialising the whole file. A 500 MB
|
|
1641
|
+
* file used to cost 500 MB of Buffer on the receiving agent, another copy in
|
|
1642
|
+
* the socket parser, and — on the serving hub — the same again. That is the
|
|
1643
|
+
* shape that killed the cloud EventHub: memory that is work in flight rather
|
|
1644
|
+
* than garbage, so no collection can reclaim any of it.
|
|
1645
|
+
*
|
|
1646
|
+
* Read failures are tagged, because from here on the bytes arrive during the
|
|
1647
|
+
* write rather than before it, and {@link FsAgent._isBlobReadError} is what
|
|
1648
|
+
* keeps the caller's two error messages telling the truth.
|
|
1649
|
+
* @param filePath - Where the file goes.
|
|
1650
|
+
* @param stream - The bytes.
|
|
1651
|
+
*/
|
|
1652
|
+
static async _atomicWriteStream(filePath, stream) {
|
|
1653
|
+
const target = process.platform !== "win32" ? filePath : join(
|
|
1654
|
+
dirname(filePath),
|
|
1655
|
+
`${ATOMIC_TMP_PREFIX}${Date.now().toString(36)}-${Math.floor(
|
|
1656
|
+
Math.random() * 1e9
|
|
1657
|
+
).toString(36)}`
|
|
1658
|
+
);
|
|
1659
|
+
const handle = await open(target, "w");
|
|
1660
|
+
try {
|
|
1661
|
+
const reader = stream.getReader();
|
|
1662
|
+
for (; ; ) {
|
|
1663
|
+
let chunk;
|
|
1664
|
+
try {
|
|
1665
|
+
chunk = await reader.read();
|
|
1666
|
+
} catch (error) {
|
|
1667
|
+
throw Object.assign(
|
|
1668
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
1669
|
+
{ __blobRead: true }
|
|
1670
|
+
);
|
|
1671
|
+
}
|
|
1672
|
+
if (chunk.done) break;
|
|
1673
|
+
await handle.write(chunk.value);
|
|
1674
|
+
}
|
|
1675
|
+
} catch (error) {
|
|
1676
|
+
await handle.close();
|
|
1677
|
+
if (target !== filePath) {
|
|
1678
|
+
await unlink(target).catch(() => {
|
|
1679
|
+
});
|
|
1680
|
+
}
|
|
1681
|
+
throw error;
|
|
1682
|
+
}
|
|
1683
|
+
await handle.close();
|
|
1684
|
+
if (target !== filePath) {
|
|
1685
|
+
try {
|
|
1686
|
+
await rename(target, filePath);
|
|
1687
|
+
} catch (err) {
|
|
1688
|
+
await unlink(target).catch(() => {
|
|
1689
|
+
});
|
|
1690
|
+
throw err;
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1590
1694
|
/**
|
|
1591
1695
|
* Wraps a promise with a timeout.
|
|
1592
1696
|
* Rejects with a descriptive error if the promise does not settle
|
|
@@ -1804,9 +1908,9 @@ ${err.stack}` : String(err);
|
|
|
1804
1908
|
this._restoreSkipped++;
|
|
1805
1909
|
return;
|
|
1806
1910
|
}
|
|
1807
|
-
let
|
|
1911
|
+
let fileStream;
|
|
1808
1912
|
try {
|
|
1809
|
-
|
|
1913
|
+
fileStream = await this._bs.getBlobStream(meta.blobId);
|
|
1810
1914
|
} catch (error) {
|
|
1811
1915
|
console.warn(
|
|
1812
1916
|
`[FsAgent] cannot fetch blob for "${meta.relativePath}" (blobId: ${meta.blobId}): ${error instanceof Error ? error.message : String(error)} — skipping this file and applying the rest of the tree.`
|
|
@@ -1814,7 +1918,7 @@ ${err.stack}` : String(err);
|
|
|
1814
1918
|
this._restoreUnavailable.push(meta.relativePath);
|
|
1815
1919
|
return;
|
|
1816
1920
|
}
|
|
1817
|
-
if (!
|
|
1921
|
+
if (!fileStream) {
|
|
1818
1922
|
console.warn(
|
|
1819
1923
|
`[FsAgent] missing blob content for "${meta.relativePath}" (blobId: ${meta.blobId}) — skipping this file and applying the rest of the tree.`
|
|
1820
1924
|
);
|
|
@@ -1823,7 +1927,7 @@ ${err.stack}` : String(err);
|
|
|
1823
1927
|
}
|
|
1824
1928
|
await mkdir(dirname(filePath), { recursive: true });
|
|
1825
1929
|
try {
|
|
1826
|
-
await FsAgent.
|
|
1930
|
+
await FsAgent._atomicWriteStream(filePath, fileStream);
|
|
1827
1931
|
this._restoreWritten++;
|
|
1828
1932
|
if (meta.mtime) {
|
|
1829
1933
|
const mtime = new Date(meta.mtime);
|
|
@@ -1841,6 +1945,13 @@ ${err.stack}` : String(err);
|
|
|
1841
1945
|
});
|
|
1842
1946
|
}
|
|
1843
1947
|
} catch (error) {
|
|
1948
|
+
if (FsAgent._isBlobReadError(error)) {
|
|
1949
|
+
console.warn(
|
|
1950
|
+
`[FsAgent] blob transfer for "${meta.relativePath}" broke off (blobId: ${meta.blobId}): ${error instanceof Error ? error.message : String(error)} — skipping this file and applying the rest of the tree.`
|
|
1951
|
+
);
|
|
1952
|
+
this._restoreUnavailable.push(meta.relativePath);
|
|
1953
|
+
return;
|
|
1954
|
+
}
|
|
1844
1955
|
if (!FsAgent._isLocked(error)) throw error;
|
|
1845
1956
|
console.warn(
|
|
1846
1957
|
`[FsAgent] restore: "${meta.relativePath}" is held open by another process (${error.code}) — skipped, will retry`
|
|
@@ -74,7 +74,11 @@ export declare class FsBlobAdapter {
|
|
|
74
74
|
*/
|
|
75
75
|
blobsToFiles(metadataList: FileBlobMeta[], targetDir: string, options?: BlobToFileOptions): Promise<void>;
|
|
76
76
|
/**
|
|
77
|
-
* Retrieves file content from blob storage
|
|
77
|
+
* Retrieves file content from blob storage.
|
|
78
|
+
*
|
|
79
|
+
* Returns a Buffer, so the whole blob is in memory by definition — that is
|
|
80
|
+
* what the caller asked for. Anything writing the bytes to a destination
|
|
81
|
+
* should use {@link FsBlobAdapter.blobToFile} instead, which streams.
|
|
78
82
|
* @param blobId - Blob ID
|
|
79
83
|
* @returns File content as Buffer
|
|
80
84
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rljson/fs-agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.82",
|
|
4
4
|
"description": "Rljson fs-agent description",
|
|
5
5
|
"homepage": "https://github.com/rljson/fs-agent",
|
|
6
6
|
"bugs": "https://github.com/rljson/fs-agent/issues",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"updateGoldens": "cross-env UPDATE_GOLDENS=true pnpm test"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@rljson/server": "0.0.
|
|
31
|
+
"@rljson/server": "0.0.69",
|
|
32
32
|
"@types/node": "^25.3.1",
|
|
33
33
|
"@typescript-eslint/eslint-plugin": "^8.56.1",
|
|
34
34
|
"@typescript-eslint/parser": "^8.56.1",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"vitest-dom": "^0.1.1"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@rljson/bs": "0.0.
|
|
53
|
+
"@rljson/bs": "0.0.27",
|
|
54
54
|
"@rljson/db": "0.0.47",
|
|
55
55
|
"@rljson/hash": "0.0.19",
|
|
56
56
|
"@rljson/io": "0.0.80",
|
|
@@ -64,7 +64,13 @@
|
|
|
64
64
|
"esbuild"
|
|
65
65
|
],
|
|
66
66
|
"overrides": {
|
|
67
|
-
"@rljson/
|
|
67
|
+
"@rljson/bs": "0.0.27",
|
|
68
|
+
"@rljson/db": "0.0.47",
|
|
69
|
+
"@rljson/hash": "0.0.19",
|
|
70
|
+
"@rljson/io": "0.0.80",
|
|
71
|
+
"@rljson/json": "0.0.23",
|
|
72
|
+
"@rljson/rljson": "0.0.83",
|
|
73
|
+
"@rljson/server": "0.0.69"
|
|
68
74
|
}
|
|
69
75
|
},
|
|
70
76
|
"packageManager": "pnpm@10.11.0"
|