Skip to content

Commit 9296697

Browse files
committed
fix: relocate yargs shim generation to local directory and handle stream error callbacks in File.ts
1 parent ecc6028 commit 9296697

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

handwritten/storage/scripts/preload-yargs.cjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
const Module = require('module');
22
const fs = require('fs');
33
const path = require('path');
4-
const os = require('os');
54

65
const originalResolveFilename = Module._resolveFilename;
76

87
Module._resolveFilename = function(request, parent, isMain, options) {
98
if (request === 'yargs/yargs') {
109
const resolved = originalResolveFilename.apply(this, arguments);
11-
// Create a unique shim file in tmpdir based on the exact path to avoid collisions
10+
if (resolved.endsWith('.mjs')) {
11+
return resolved;
12+
}
13+
// Create a unique shim file in the local scripts directory to avoid shared temp directory vulnerabilities
1214
const safeHash = Buffer.from(resolved).toString('base64').replace(/[^a-zA-Z0-9]/g, '');
13-
const shimPath = path.join(os.tmpdir(), `yargs-shim-${safeHash}.cjs`);
15+
const shimPath = path.join(__dirname, `yargs-shim-${safeHash}.cjs`);
1416

1517
if (!fs.existsSync(shimPath)) {
1618
const content = fs.readFileSync(resolved, 'utf8');

handwritten/storage/src/file.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,13 +2188,25 @@ class File extends ServiceObject<File, FileMetadata> {
21882188
emitStream.removeListener('error', noop);
21892189

21902190
if (fileWriteStream.destroyed) {
2191-
fileWriteStream.once('error', (err: Error) => {
2192-
pipelineCallback(err);
2193-
});
2194-
// Call pipelineCallback immediately if there's no error to wait for,
2195-
// though duplexify might emit error on next tick.
2196-
// Also cleanup emitStream since pipeline won't do it.
2191+
let callbackCalled = false;
2192+
const onError = (err: Error) => {
2193+
if (!callbackCalled) {
2194+
callbackCalled = true;
2195+
pipelineCallback(err);
2196+
}
2197+
};
2198+
fileWriteStream.once('error', onError);
21972199
emitStream.destroy();
2200+
2201+
process.nextTick(() => {
2202+
fileWriteStream.removeListener('error', onError);
2203+
if (!callbackCalled) {
2204+
callbackCalled = true;
2205+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2206+
const err = (fileWriteStream as any).errored || new Error('Write stream destroyed');
2207+
pipelineCallback(err);
2208+
}
2209+
});
21982210
return;
21992211
}
22002212

0 commit comments

Comments
 (0)