Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support uncompressing symlink #31

Merged
merged 12 commits into from
Aug 24, 2023
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: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ coverage
.vscode
test/fixtures/types/*.js
test/fixtures/chinese-path-test.zip
yarn.lock
.DS_Store
yarn.lock
!test/fixtures/symlink/node_modules
11 changes: 10 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,20 @@
entryCount++;
pump(stream, fs.createWriteStream(fullpath, { mode: opts.mode || header.mode }), err => {
if (err) return reject(err);

successCount++;
done();
});
});
} else if (header.type === 'symlink') {
// symlink
const src = path.join(destDir, header.name);
const target = path.resolve(path.dirname(src), header.linkname);
entryCount++;
fs.symlink(target, src, err => {

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 14)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 14)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 14)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 14)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 16)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 16)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 16)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 16)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

Use 'fs.promises.symlink()' instead
if (err) return reject(err);
successCount++;
stream.resume();
});
} else { // directory
mkdirp(path.join(destDir, header.name), err => {
if (err) return reject(err);
Expand Down
Binary file added test/fixtures/symlink.tgz
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/symlink/README.md
1 change: 1 addition & 0 deletions test/fixtures/symlink/cli
30 changes: 30 additions & 0 deletions test/fixtures/symlink/node_modules/_enums@1.0.3@enums/History.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/fixtures/symlink/node_modules/_enums@1.0.3@enums/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions test/fixtures/symlink/node_modules/_enums@1.0.3@enums/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions test/fixtures/symlink/node_modules/_enums@1.0.3@enums/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/fixtures/symlink/node_modules/cli

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/symlink/node_modules/enums

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 39 additions & 23 deletions test/gzip/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,100 +6,116 @@ const path = require('path');
const uuid = require('uuid');
const compressing = require('../..');
const assert = require('assert');
const isWindows = os.platform() === 'win32';

describe('test/gzip/index.test.js', () => {
describe('gzip.compressFile()', () => {
it('gzip.compressFile(file, stream)', function* () {
it('gzip.compressFile(file, stream)', async () => {
const sourceFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const destFile = path.join(os.tmpdir(), uuid.v4() + '.log.gz');
console.log('destFile', destFile);
const fileStream = fs.createWriteStream(destFile);
yield compressing.gzip.compressFile(sourceFile, fileStream);
await compressing.gzip.compressFile(sourceFile, fileStream);
assert(fs.existsSync(destFile));
});

it('gzip.compressFile(file, destStream) should error if destStream emit error', function* () {
it('gzip.compressFile(file, destStream) should error if destStream emit error', async () => {
const sourceFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const destFile = path.join(os.tmpdir(), uuid.v4() + '.gz');
const fileStream = fs.createWriteStream(destFile);
setImmediate(() => fileStream.emit('error', new Error('xx')));

let err;
try {
yield compressing.gzip.compressFile(sourceFile, fileStream);
await compressing.gzip.compressFile(sourceFile, fileStream);
} catch (e) {
err = e;
}
assert(err && err.message === 'xx');
});

it('gzip.compressFile(buffer, stream)', function* () {
it('gzip.compressFile(buffer, stream)', async () => {
const sourceFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const sourceBuffer = fs.readFileSync(sourceFile);
const destFile = path.join(os.tmpdir(), uuid.v4() + '.log.gz');
console.log('destFile', destFile);
const fileStream = fs.createWriteStream(destFile);
yield compressing.gzip.compressFile(sourceBuffer, fileStream);
await compressing.gzip.compressFile(sourceBuffer, fileStream);
assert(fs.existsSync(destFile));
});

it('gzip.compressFile(sourceStream, destStream)', function* () {
it('gzip.compressFile(sourceStream, destStream)', async () => {
const sourceFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const sourceStream = fs.createReadStream(sourceFile);
const destFile = path.join(os.tmpdir(), uuid.v4() + '.log.gz');
console.log('destFile', destFile);
const fileStream = fs.createWriteStream(destFile);
yield compressing.gzip.compressFile(sourceStream, fileStream);
await compressing.gzip.compressFile(sourceStream, fileStream);
assert(fs.existsSync(destFile));
});
});

describe('gzip.uncompress()', () => {
it('gzip.uncompress(sourceFile, destStream)', function* () {
it('gzip.uncompress(sourceFile, destStream)', async () => {
const sourceFile = path.join(__dirname, '..', 'fixtures', 'xx.log.gz');
const originalFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const destFile = path.join(os.tmpdir(), uuid.v4() + '.log');
const fileStream = fs.createWriteStream(destFile);
yield compressing.gzip.uncompress(sourceFile, fileStream);
await compressing.gzip.uncompress(sourceFile, fileStream);
assert(fs.existsSync(destFile));
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
if (!isWindows) {
// EOL not equal to linux
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
}
});

it('gzip.uncompress(sourceStream, destStream)', function* () {
it('gzip.uncompress(sourceStream, destStream)', async () => {
const sourceStream = fs.createReadStream(path.join(__dirname, '..', 'fixtures', 'xx.log.gz'));
const originalFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const destFile = path.join(os.tmpdir(), uuid.v4() + '.log');
const fileStream = fs.createWriteStream(destFile);
yield compressing.gzip.uncompress(sourceStream, fileStream);
await compressing.gzip.uncompress(sourceStream, fileStream);
assert(fs.existsSync(destFile));
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
if (!isWindows) {
// EOL not equal to linux
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
}
});

it('gzip.uncompress(sourceStream, destFile)', function* () {
it('gzip.uncompress(sourceStream, destFile)', async () => {
const sourceStream = fs.createReadStream(path.join(__dirname, '..', 'fixtures', 'xx.log.gz'));
const originalFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const destFile = path.join(os.tmpdir(), uuid.v4() + '.log');
yield compressing.gzip.uncompress(sourceStream, destFile);
await compressing.gzip.uncompress(sourceStream, destFile);
assert(fs.existsSync(destFile));
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
if (!isWindows) {
// EOL not equal to linux
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
}
});

it('gzip.uncompress(sourceFile, destFile)', function* () {
it('gzip.uncompress(sourceFile, destFile)', async () => {
const sourceFile = path.join(__dirname, '..', 'fixtures', 'xx.log.gz');
const originalFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const destFile = path.join(os.tmpdir(), uuid.v4() + '.log');
yield compressing.gzip.uncompress(sourceFile, destFile);
await compressing.gzip.uncompress(sourceFile, destFile);
assert(fs.existsSync(destFile));
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
if (!isWindows) {
// EOL not equal to linux
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
}
});

it('gzip.uncompress(buffer, destFile)', function* () {
it('gzip.uncompress(buffer, destFile)', async () => {
const sourceBuffer = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'xx.log.gz'));
const originalFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
const destFile = path.join(os.tmpdir(), uuid.v4() + '.log');
yield compressing.gzip.uncompress(sourceBuffer, destFile);
await compressing.gzip.uncompress(sourceBuffer, destFile);
assert(fs.existsSync(destFile));
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
if (!isWindows) {
// EOL not equal to linux
assert(fs.readFileSync(destFile, 'utf8') === fs.readFileSync(originalFile, 'utf8'));
}
});
});
});
Loading
Loading