GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fixed stream writes dropping data after short write (https://github.c… · zen-fs/core@ebb628a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/node
    • streams.ts
  • tests/common
    • streams.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node/streams.ts‎

Copy file name to clipboardExpand all lines: src/node/streams.ts
+16-2Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export interface StreamOperations {
2828
/** The default size of the chunks read by streams using `fs` operations, in bytes */
2929
const operationsChunkSize = 64 * 1024;
3030

31+
/** How many writes in a row may write nothing before a stream gives up */
32+
const maxWriteRetries = 5;
33+
3134
/**
3235
* Calls a callback-based operation, resolving with its result.
3336
*/
@@ -109,8 +112,19 @@ export class OperationsHandle {
109112

110113
return new WritableStream({
111114
async write(chunk) {
112-
const bytesWritten = await write(chunk);
113-
if (position !== undefined) position += bytesWritten;
115+
let retries = 0;
116+
for (let data = chunk; data.byteLength;) {
117+
const bytesWritten = await write(data).catch((error: NodeJS.ErrnoException) => {
118+
if (error?.code != 'EAGAIN') throw error;
119+
return 0;
120+
});
121+
122+
retries = bytesWritten ? 0 : retries + 1;
123+
if (retries > maxWriteRetries) throw Object.assign(new Error('write failed'), { code: 'ERR_SYSTEM_ERROR' });
124+
125+
if (position !== undefined) position += bytesWritten;
126+
data = data.subarray(bytesWritten);
127+
}
114128
},
115129
});
116130
}
Collapse file

‎tests/common/streams.test.ts‎

Copy file name to clipboardExpand all lines: tests/common/streams.test.ts
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,39 @@ suite('Streams', () => {
1818
fs.unlinkSync('/hwm.bin');
1919
}
2020
});
21+
22+
suite('fs option with short writes', () => {
23+
type WriteCallback = (error: Error | null, bytesWritten: number, buffers?: Uint8Array[]) => void;
24+
25+
const open = (_: unknown, flags: string, mode: number, callback: (error: Error | null, fd: number) => void) =>
26+
fs.open('/short.txt', flags, mode, callback as any);
27+
28+
function finish(stream: fs.WriteStream, data: string): Promise<void> {
29+
return new Promise((resolve, reject) => {
30+
stream.on('error', reject);
31+
stream.end(data, () => stream.on('close', resolve));
32+
});
33+
}
34+
35+
test('writev resumes after the bytes that were written', async () => {
36+
fs.writeFileSync('/short.txt', '0123456789');
37+
const writev = (fd: number, buffers: Uint8Array[], position: number | null, callback: WriteCallback) => {
38+
const data = Buffer.concat(buffers);
39+
fs.write(fd, data, 0, Math.min(data.length, 2), position ?? undefined, (error, bytesWritten = 0) =>
40+
callback(error, bytesWritten, buffers),
41+
);
42+
};
43+
const stream = fs.createWriteStream('/short.txt', { flags: 'r+', start: 2, fs: { open, writev, close: fs.close } as any });
44+
stream.cork();
45+
stream.write('abc');
46+
await finish(stream, 'de');
47+
assert.equal(fs.readFileSync('/short.txt', 'utf8'), '01abcde789');
48+
});
49+
50+
test('fails after repeatedly writing nothing', async () => {
51+
const write = (...args: unknown[]) => (args.at(-1) as WriteCallback)(null, 0);
52+
const stream = fs.createWriteStream('/short.txt', { fs: { open, write, close: fs.close } });
53+
await assert.rejects(finish(stream, 'abc'));
54+
});
55+
});
2156
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.