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
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/issue_template.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: Tracking issue
about: New features.
title: "[DATE]: [FEATURE NAME]"
about: New Issue.
title: "[feat|bug|docs]: [issue title]"
labels: tracking issue, needs triage
assignees: mikbry
---
Expand Down
6 changes: 4 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
src
node_module
test
node_modules
*.DS_Store
*config*
yarn.lock
coverage
.github
.nyc_output
.nycrc
data
rollup.config.js
.eslintrc
commitlint.config.js
yarn-error.js
.travis.yml
.travis.yml
62 changes: 54 additions & 8 deletions src/ZipEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import path from 'path';
import stream from 'stream';
import util from 'util';

const fsp = fs.promises;
const pipeline = util.promisify(stream.pipeline);
const finished = util.promisify(stream.finished);
export default class ZipEntry {
Expand Down Expand Up @@ -55,6 +56,7 @@ export default class ZipEntry {
if (this.chunks) {
this.content = Buffer.concat(this.chunks);
}
this.stream = null;
});
if (this.filename && parameters.outputContent) {
this.chunks = [];
Expand All @@ -65,17 +67,20 @@ export default class ZipEntry {
return parameters;
}

async drain() {
this.stream.unpipe();
this.zipFile.readEntry();
}

async close() {
this.stream.unpipe();
async close(shouldDrain) {
if (this.stream) {
this.stream.unpipe();
}
if (shouldDrain) {
this.zipFile.readEntry();
}
}

async getContent() {
return finished(this.stream);
if (this.parameters.outputContent && !this.content && !this.saved && this.stream) {
await finished(this.stream);
}
return this.content;
}

async saveTo(outputPath, flattenPath = this.parameters.flattenPath) {
Expand All @@ -93,5 +98,46 @@ export default class ZipEntry {
// eslint-disable-next-line no-param-reassign
this.error = err;
}
return this.saved;
}

async proceed(opts, output) {
const {
pattern,
disableSave,
outputContent,
entryHandler,
outputPath,
flattenPath,
disableOutput,
} = this.parameters;
const { filename: name, directory } = this;
let shouldDrain = true;
if (!name && directory && !flattenPath && outputPath) {
await fsp.mkdir(path.join(outputPath, directory), { recursive: true });
} else if (this.filename) {
const data = { name };
if (directory) {
data.directory = directory;
}
if (!pattern || pattern.test(name)) {
if (!disableOutput || outputContent) {
output.files.push(data);
}
if (!entryHandler || (await entryHandler(this, data, opts))) {
if (!disableSave && outputPath) {
// saveTo
await this.saveTo(outputPath, flattenPath);
}
}
data.saved = this.saved;
const content = await this.getContent();
if (content) {
data.content = content;
}
shouldDrain = !(data.content || data.saved);
}
}
return this.close(shouldDrain);
}
}
47 changes: 2 additions & 45 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,12 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs';
import path from 'path';
import util from 'util';
import yauzl from 'yauzl';
import ZipEntry from './ZipEntry';

const fsp = fs.promises;
const zipOpen = util.promisify(yauzl.open);

const proceed = async (
entry,
{ pattern, disableSave, outputContent, entryHandler, outputPath, flattenPath, disableOutput },
opts,
output,
) => {
const { filename: name, directory } = entry;
if (!name && directory && !flattenPath && outputPath) {
await fsp.mkdir(path.join(outputPath, directory), { recursive: true });
} else if (entry.filename) {
const data = { name };
if (directory) {
data.directory = directory;
}
if (!pattern || pattern.test(name)) {
if (!disableOutput || outputContent) {
output.files.push(data);
}
if (!entryHandler || (await entryHandler(entry, data, opts))) {
if (!disableSave && outputPath) {
// saveTo
await entry.saveTo(outputPath, flattenPath);
data.saved = true;
} else if (outputContent) {
await entry.getContent();
}
}
if (entry.content) {
data.content = entry.content;
}
data.saved = entry.saved;
if (data.content || data.saved) {
return entry.close();
}
}
}
// autodrain
return entry.drain();
};

const anzip = async (
filename,
{
Expand All @@ -75,11 +32,11 @@ const anzip = async (
zipFile.readEntry();
zipFile.on('entry', async e => {
const entry = new ZipEntry(zipFile, e);
const parameters = await entry.init(
await entry.init(
{ pattern, disableSave, outputContent, entryHandler, outputPath, flattenPath, disableOutput },
rules,
);
await proceed(entry, parameters, opts, output);
await entry.proceed(opts, output);
});
zipFile.on('end', () => {
const hr = process.hrtime(hrstart);
Expand Down