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
3 changes: 1 addition & 2 deletions packages/contentstack-bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
"@oclif/core": "^4.14.0",
"inquirer": "12.11.1",
"mkdirp": "^2.1.6",
"tar": "^7.5.23",
"tar": "^8.1.2",
"tslib": "^2.9.1"
},
"devDependencies": {
"@oclif/test": "^4.2.0",
"@types/inquirer": "^9.0.10",
"@types/mkdirp": "^1.0.2",
"@types/node": "^18.19.130",
"@types/tar": "^6.1.13",
"chai": "^4.5.0",
"eslint": "^10.14.0",
"mocha": "10.8.2",
Expand Down
23 changes: 5 additions & 18 deletions packages/contentstack-bootstrap/src/bootstrap/github/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Stream } from 'stream';
import { pipeline, Readable, Stream } from 'stream';
import * as zlib from 'zlib';
import * as tar from 'tar';
const mkdirp = require('mkdirp')
Expand Down Expand Up @@ -86,22 +86,9 @@ export default class GitHubClient {
}

async extract(destination: string, stream: Stream): Promise<any> {
return new Promise((resolve, reject) => {
const unzip = zlib.createUnzip();
stream.on('error', reject);
unzip.on('error', reject);
stream
.pipe(unzip)
.pipe(
tar.extract({
cwd: destination,
strip: 1,
}),
)
.on('end', () => {
resolve('done');
})
.on('error', reject);
});
// pipeline forwards download and gunzip errors to tar, so extract() rejects with them.
const unzipped = pipeline(stream as Readable, zlib.createUnzip(), () => {});
await tar.extract({ input: unzipped, cwd: destination, strip: 1 });
return 'done';
}
}
1 change: 0 additions & 1 deletion packages/contentstack-import-setup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"@types/node": "^14.18.63",
"@types/rewire": "^2.5.30",
"@types/sinon": "^21.0.1",
"@types/tar": "^7.0.87",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"chai": "^4.5.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/contentstack-seed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@contentstack/cli-utilities": "~2.0.2",
"inquirer": "12.11.1",
"mkdirp": "^1.0.4",
"tar": "^7.5.23",
"tar": "^8.1.2",
"tmp": "^0.2.7",
"tslib": "^2.9.1"
},
Expand All @@ -20,7 +20,6 @@
"@types/jest": "^26.0.24",
"@types/mkdirp": "^1.0.2",
"@types/node": "^18.19.130",
"@types/tar": "^7.0.87",
"@types/tmp": "^0.2.6",
"eslint": "^10.14.0",
"eslint-config-oclif": "^6.0.179",
Expand Down
17 changes: 4 additions & 13 deletions packages/contentstack-seed/src/seed/github/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as tar from 'tar';
import * as zlib from 'zlib';
import * as https from 'https';
import { Stream } from 'stream';
import { pipeline, Readable, Stream } from 'stream';
import mkdirp = require('mkdirp');
import { HttpClient } from '@contentstack/cli-utilities';

Expand Down Expand Up @@ -147,18 +147,9 @@ export default class GitHubClient {
}

async extract(destination: string, stream: Stream): Promise<void> {
return new Promise((resolve, reject) => {
stream
.pipe(zlib.createUnzip())
.pipe(
tar.extract({
cwd: destination,
strip: 1,
}),
)
.on('end', () => resolve())
.on('error', reject);
});
// pipeline forwards download and gunzip errors to tar, so extract() rejects with them.
const unzipped = pipeline(stream as Readable, zlib.createUnzip(), () => {});
await tar.extract({ input: unzipped, cwd: destination, strip: 1 });
}

private buildError(error: any) {
Expand Down
65 changes: 31 additions & 34 deletions packages/contentstack-seed/test/seed/github/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as tar from 'tar';
import * as zlib from 'zlib';
import * as https from 'https';
import * as mkdirp from 'mkdirp';
import { Stream } from 'stream';
import { Readable, Stream, Transform } from 'stream';

describe('GitHubClient', () => {
let mockHttpClient: any;
Expand Down Expand Up @@ -132,54 +132,51 @@ describe('GitHubClient', () => {
});

describe('extract', () => {
// tar 8: extract() takes the archive as `input` and returns a promise.
// The fake reads its input the way tar does, so stream errors reach it.
const readAll = async ({ input }: { input: AsyncIterable<unknown> }) => {
for await (const _chunk of input) {
// drain
}
};

beforeEach(() => {
(zlib.createUnzip as jest.Mock) = jest.fn().mockImplementation(
() => new Transform({ transform: (chunk, _encoding, done) => done(null, chunk) }),
);
});

it('should extract tarball to destination', async () => {
const mockStream = new Stream();
const mockUnzip = new Stream();
const mockExtract = new Stream();

(zlib.createUnzip as jest.Mock) = jest.fn().mockReturnValue(mockUnzip);
(tar.extract as unknown as jest.Mock) = jest.fn().mockReturnValue(mockExtract);

// Mock pipe chain
mockStream.pipe = jest.fn().mockReturnValue(mockUnzip);
mockUnzip.pipe = jest.fn().mockReturnValue(mockExtract);
mockExtract.on = jest.fn().mockImplementation((event, callback) => {
if (event === 'end') {
setTimeout(() => callback(), 0);
}
return mockExtract;
});
(tar.extract as unknown as jest.Mock) = jest.fn().mockImplementation(readAll);

await githubClient.extract('/tmp/dest', mockStream);
await githubClient.extract('/tmp/dest', Readable.from([Buffer.from('tarball')]));

expect(zlib.createUnzip).toHaveBeenCalled();
expect(tar.extract).toHaveBeenCalledWith({
input: expect.any(Transform),
cwd: '/tmp/dest',
strip: 1,
});
});

it('should reject on extraction error', async () => {
const mockStream = new Stream();
const mockUnzip = new Stream();
const mockExtract = new Stream();

(zlib.createUnzip as jest.Mock) = jest.fn().mockReturnValue(mockUnzip);
(tar.extract as unknown as jest.Mock) = jest.fn().mockReturnValue(mockExtract);

mockStream.pipe = jest.fn().mockReturnValue(mockUnzip);
mockUnzip.pipe = jest.fn().mockReturnValue(mockExtract);
mockExtract.on = jest.fn().mockImplementation((event, callback) => {
if (event === 'error') {
setTimeout(() => callback(new Error('Extraction failed')), 0);
}
return mockExtract;
});
(tar.extract as unknown as jest.Mock) = jest.fn().mockRejectedValue(new Error('Extraction failed'));

await expect(githubClient.extract('/tmp/dest', mockStream)).rejects.toThrow(
await expect(githubClient.extract('/tmp/dest', Readable.from([Buffer.from('tarball')]))).rejects.toThrow(
'Extraction failed',
);
});

it('should reject when the download stream fails', async () => {
(tar.extract as unknown as jest.Mock) = jest.fn().mockImplementation(readAll);
const download = new Readable({
read() {
this.destroy(new Error('socket hang up'));
},
});

await expect(githubClient.extract('/tmp/dest', download)).rejects.toThrow('socket hang up');
});
});

describe('getLatest', () => {
Expand Down
Loading
Loading