Skip to content

Commit

Permalink
esm
Browse files Browse the repository at this point in the history
  • Loading branch information
ctalkington committed Oct 15, 2024
1 parent 4709099 commit b944352
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 409 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ jobs:
node-version: [14.x, 16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v4.2.1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4.0.4
with:
node-version: ${{ matrix.node-version }}
- name: npm install and test
run: |
npm ci
npm test
env:
CI: true
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
name: tmp-zip-node-v${{ matrix.node-version }}
path: tmp/*.zip
- uses: actions/checkout@v4.2.1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4.0.4
with:
node-version: ${{ matrix.node-version }}
- name: npm install and test
run: |
npm ci
npm test
env:
CI: true
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
name: tmp-zip-node-v${{ matrix.node-version }}
path: tmp/*.zip
14 changes: 7 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#### Code Style Guide

* code should be indented with 2 spaces
* single quotes should be used where feasible
* commas should be followed by a single space (function params, etc)
* variable declaration should include `var`, [no multiple declarations](http://benalman.com/news/2012/05/multiple-var-statements-javascript/)
- code should be indented with 2 spaces
- single quotes should be used where feasible
- commas should be followed by a single space (function params, etc)
- variable declaration should include `var`, [no multiple declarations](http://benalman.com/news/2012/05/multiple-var-statements-javascript/)

#### Tests

* tests should be added to the nodeunit configs in `test/`
* tests can be run with `npm test`
* see existing tests for guidance
- tests should be added to the nodeunit configs in `test/`
- tests can be run with `npm test`
- see existing tests for guidance
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ This module is meant to be wrapped internally by other modules and therefore lac
If you want a module that handles entry queueing and much more, you should check out [archiver](https://npmjs.org/package/archiver) which uses this module internally.

```js
const Packer = require('zip-stream');
const Packer = require("zip-stream");
const archive = new Packer(); // OR new Packer(options)

archive.on('error', function(err) {
archive.on("error", function (err) {
throw err;
});

// pipe archive where you want it (ie fs, http, etc)
// listen to the destination's end, close, or finish event

archive.entry('string contents', { name: 'string.txt' }, function(err, entry) {
archive.entry("string contents", { name: "string.txt" }, function (err, entry) {
if (err) throw err;
archive.entry(null, { name: 'directory/' }, function(err, entry) {
archive.entry(null, { name: "directory/" }, function (err, entry) {
if (err) throw err;
archive.finish();
});
Expand Down
125 changes: 52 additions & 73 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { inherits as inherits$0 } from "util";
import { ZipArchiveOutputStream as ZipArchiveOutputStream$0 } from "compress-commons";
import { ZipArchiveEntry as ZipArchiveEntry$0 } from "compress-commons";
import util from "archiver-utils";
/**
* ZipStream
*
* @ignore
* @license [MIT]{@link https://github.com/archiverjs/node-zip-stream/blob/master/LICENSE}
* @copyright (c) 2014 Chris Talkington, contributors.
*/
var inherits = require('util').inherits;

var ZipArchiveOutputStream = require('compress-commons').ZipArchiveOutputStream;
var ZipArchiveEntry = require('compress-commons').ZipArchiveEntry;

var util = require('archiver-utils');

var inherits = { inherits: inherits$0 }.inherits;
var ZipArchiveOutputStream = {
ZipArchiveOutputStream: ZipArchiveOutputStream$0,
}.ZipArchiveOutputStream;
var ZipArchiveEntry = { ZipArchiveEntry: ZipArchiveEntry$0 }.ZipArchiveEntry;
/**
* @constructor
* @extends external:ZipArchiveOutputStream
Expand All @@ -23,76 +25,65 @@ var util = require('archiver-utils');
* @param {Object} [options.zlib] Passed to [zlib]{@link https://nodejs.org/api/zlib.html#zlib_class_options}
* to control compression.
*/
var ZipStream = module.exports = function(options) {
var ZipStream = function (options) {
if (!(this instanceof ZipStream)) {
return new ZipStream(options);
}

options = this.options = options || {};
options.zlib = options.zlib || {};

ZipArchiveOutputStream.call(this, options);

if (typeof options.level === 'number' && options.level >= 0) {
if (typeof options.level === "number" && options.level >= 0) {
options.zlib.level = options.level;
delete options.level;
}

if (!options.forceZip64 && typeof options.zlib.level === 'number' && options.zlib.level === 0) {
if (
!options.forceZip64 &&
typeof options.zlib.level === "number" &&
options.zlib.level === 0
) {
options.store = true;
}

options.namePrependSlash = options.namePrependSlash || false;

if (options.comment && options.comment.length > 0) {
this.setComment(options.comment);
}
};

inherits(ZipStream, ZipArchiveOutputStream);

/**
* Normalizes entry data with fallbacks for key properties.
*
* @private
* @param {Object} data
* @return {Object}
*/
ZipStream.prototype._normalizeFileData = function(data) {
ZipStream.prototype._normalizeFileData = function (data) {
data = util.defaults(data, {
type: 'file',
type: "file",
name: null,
namePrependSlash: this.options.namePrependSlash,
linkname: null,
date: null,
mode: null,
store: this.options.store,
comment: ''
comment: "",
});

var isDir = data.type === 'directory';
var isSymlink = data.type === 'symlink';

var isDir = data.type === "directory";
var isSymlink = data.type === "symlink";
if (data.name) {
data.name = util.sanitizePath(data.name);

if (!isSymlink && data.name.slice(-1) === '/') {
if (!isSymlink && data.name.slice(-1) === "/") {
isDir = true;
data.type = 'directory';
data.type = "directory";
} else if (isDir) {
data.name += '/';
data.name += "/";
}
}

if (isDir || isSymlink) {
data.store = true;
}

data.date = util.dateify(data.date);

return data;
};

/**
* Appends an entry given an input source (text string, buffer, or stream).
*
Expand All @@ -108,80 +99,68 @@ ZipStream.prototype._normalizeFileData = function(data) {
* @param {Function} callback
* @return this
*/
ZipStream.prototype.entry = function(source, data, callback) {
if (typeof callback !== 'function') {
ZipStream.prototype.entry = function (source, data, callback) {
if (typeof callback !== "function") {
callback = this._emitErrorCallback.bind(this);
}

data = this._normalizeFileData(data);

if (data.type !== 'file' && data.type !== 'directory' && data.type !== 'symlink') {
callback(new Error(data.type + ' entries not currently supported'));
if (
data.type !== "file" &&
data.type !== "directory" &&
data.type !== "symlink"
) {
callback(new Error(data.type + " entries not currently supported"));
return;
}

if (typeof data.name !== 'string' || data.name.length === 0) {
callback(new Error('entry name must be a non-empty string value'));
if (typeof data.name !== "string" || data.name.length === 0) {
callback(new Error("entry name must be a non-empty string value"));
return;
}

if (data.type === 'symlink' && typeof data.linkname !== 'string') {
callback(new Error('entry linkname must be a non-empty string value when type equals symlink'));
if (data.type === "symlink" && typeof data.linkname !== "string") {
callback(
new Error(
"entry linkname must be a non-empty string value when type equals symlink",
),
);
return;
}

var entry = new ZipArchiveEntry(data.name);
entry.setTime(data.date, this.options.forceLocalTime);

if (data.namePrependSlash) {
entry.setName(data.name, true);
}

if (data.store) {
entry.setMethod(0);
}

if (data.comment.length > 0) {
entry.setComment(data.comment);
}

if (data.type === 'symlink' && typeof data.mode !== 'number') {
if (data.type === "symlink" && typeof data.mode !== "number") {
data.mode = 40960; // 0120000
}

if (typeof data.mode === 'number') {
if (data.type === 'symlink') {
if (typeof data.mode === "number") {
if (data.type === "symlink") {
data.mode |= 40960;
}

entry.setUnixMode(data.mode);
}

if (data.type === 'symlink' && typeof data.linkname === 'string') {
if (data.type === "symlink" && typeof data.linkname === "string") {
source = Buffer.from(data.linkname);
}

return ZipArchiveOutputStream.prototype.entry.call(this, entry, source, callback);
return ZipArchiveOutputStream.prototype.entry.call(
this,
entry,
source,
callback,
);
};

/**
* Finalizes the instance and prevents further appending to the archive
* structure (queue will continue til drained).
*
* @return void
*/
ZipStream.prototype.finalize = function() {
ZipStream.prototype.finalize = function () {
this.finish();
};

/**
* Returns the current number of bytes written to this stream.
* @function ZipStream#getBytesWritten
* @returns {Number}
*/

/**
* Compress Commons ZipArchiveOutputStream
* @external ZipArchiveOutputStream
* @see {@link https://github.com/archiverjs/node-compress-commons}
*/
export default ZipStream;
22 changes: 22 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"minami": "1.2.3",
"mkdirp": "3.0.1",
"mocha": "10.7.3",
"prettier": "3.3.3",
"rimraf": "5.0.10"
},
"keywords": [
Expand Down
Loading

0 comments on commit b944352

Please sign in to comment.