Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions handwritten/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"samples-test": "npm link && cd samples/ && npm link ../ && npm test && cd ../",
"system-test:esm": "mocha build/esm/system-test --timeout 600000 --exit",
"system-test": "mocha build/cjs/system-test --timeout 600000 --exit",
"test": "cross-env NODE_OPTIONS='--no-deprecation' c8 mocha build/cjs/test"
"test": "cross-env NODE_OPTIONS=\"--require ./scripts/preload-yargs.cjs --no-deprecation\" c8 mocha build/cjs/test"
},
"dependencies": {
"@google-cloud/paginator": "^5.0.0",
Expand Down Expand Up @@ -106,7 +106,7 @@
"@types/request": "^2.48.4",
"@types/sinon": "^17.0.0",
"@types/tmp": "0.2.6",
"@types/yargs": "^17.0.10",
"@types/yargs": "^17.0.35",
"c8": "^9.0.0",
"form-data": "^4.0.4",
"gapic-tools": "^0.4.0",
Expand All @@ -125,7 +125,7 @@
"path-to-regexp": "6.3.0",
"tmp": "^0.2.0",
"typescript": "^5.1.6",
"yargs": "^17.3.1",
"yargs": "^17.7.2",
"cross-env": "^7.0.3"
},
"homepage": "https://github.com/googleapis/google-cloud-node/tree/main/handwritten/storage"
Expand Down
39 changes: 39 additions & 0 deletions handwritten/storage/scripts/preload-yargs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Module = require('module');
const fs = require('fs');
const path = require('path');

const originalResolveFilename = Module._resolveFilename;

Module._resolveFilename = function(request, parent, isMain, options) {
if (request === 'yargs/yargs') {
const resolved = originalResolveFilename.apply(this, arguments);
if (resolved.endsWith('.mjs')) {
return resolved;
}
// Create a unique shim file in the local scripts directory to avoid shared temp directory vulnerabilities
const safeHash = Buffer.from(resolved).toString('base64').replace(/[^a-zA-Z0-9]/g, '');
const shimPath = path.join(__dirname, `yargs-shim-${safeHash}.cjs`);

if (!fs.existsSync(shimPath)) {
const content = fs.readFileSync(resolved, 'utf8');
// Replace `./build/index.cjs` with the absolute path
const buildIndexPath = path.join(path.dirname(resolved), 'build', 'index.cjs');
// We must replace ALL relative requires. Luckily yargs/yargs only requires `./build/index.cjs`
const newContent = content.replace(/require\(['"]\.\/build\/index\.cjs['"]\)/g, `require(${JSON.stringify(buildIndexPath)})`);
fs.writeFileSync(shimPath, newContent);
}

global.__yargsShimCleanups = global.__yargsShimCleanups || new Set();
if (!global.__yargsShimCleanups.has(shimPath)) {
global.__yargsShimCleanups.add(shimPath);
process.on('exit', () => {
try {
fs.unlinkSync(shimPath);
} catch (e) {}
});
}

return shimPath;
}
return originalResolveFilename.apply(this, arguments);
};
Comment thread
thiyaguk09 marked this conversation as resolved.
23 changes: 23 additions & 0 deletions handwritten/storage/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,29 @@ class File extends ServiceObject<File, FileMetadata> {
// remove temporary noop listener as we now create a pipeline that handles the errors
emitStream.removeListener('error', noop);

if (fileWriteStream.destroyed) {
let callbackCalled = false;
const onError = (err: Error) => {
if (!callbackCalled) {
callbackCalled = true;
pipelineCallback(err);
}
};
fileWriteStream.once('error', onError);
emitStream.destroy();

process.nextTick(() => {
fileWriteStream.removeListener('error', onError);
if (!callbackCalled) {
callbackCalled = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const err = (fileWriteStream as any).errored || new Error('Write stream destroyed');
pipelineCallback(err);
}
});
return;
}

pipeline(
emitStream,
...(transformStreams as [Transform]),
Expand Down
12 changes: 10 additions & 2 deletions handwritten/storage/test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ const fakePromisify = {
};

const fsCached = fs;
const fakeFs = {...fsCached};
const safeFs: any = {};
const descriptors = Object.getOwnPropertyDescriptors(fsCached);
for (const key of Object.keys(descriptors)) {
const desc = descriptors[key];
if (desc && !desc.get) {
Object.defineProperty(safeFs, key, desc);
}
}
Comment thread
thiyaguk09 marked this conversation as resolved.
const fakeFs = {...safeFs} as typeof fs;

const zlibCached = zlib;
let createGunzipOverride: Function | null;
Expand Down Expand Up @@ -223,7 +231,7 @@ describe('File', () => {
});

beforeEach(() => {
Object.assign(fakeFs, fsCached);
Object.assign(fakeFs, safeFs);
Object.assign(fakeOs, osCached);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
FakeServiceObject.prototype.request = util.noop as any;
Expand Down
Loading