zip-stream
Advanced tools
Comparing version 6.0.1 to 7.0.0
306
index.js
@@ -0,1 +1,4 @@ | ||
import { ZipArchiveOutputStream, ZipArchiveEntry } from "compress-commons"; | ||
import { dateify, sanitizePath } from "./utils.js"; | ||
/** | ||
@@ -8,181 +11,152 @@ * ZipStream | ||
*/ | ||
var inherits = require('util').inherits; | ||
export default class ZipStream extends ZipArchiveOutputStream { | ||
/** | ||
* @constructor | ||
* @extends external:ZipArchiveOutputStream | ||
* @param {Object} [options] | ||
* @param {String} [options.comment] Sets the zip archive comment. | ||
* @param {Boolean} [options.forceLocalTime=false] Forces the archive to contain local file times instead of UTC. | ||
* @param {Boolean} [options.forceZip64=false] Forces the archive to contain ZIP64 headers. | ||
* @param {Boolean} [options.store=false] Sets the compression method to STORE. | ||
* @param {Object} [options.zlib] Passed to [zlib]{@link https://nodejs.org/api/zlib.html#zlib_class_options} | ||
* to control compression. | ||
*/ | ||
constructor(options) { | ||
options = options || {}; | ||
options.zlib = options.zlib || {}; | ||
var ZipArchiveOutputStream = require('compress-commons').ZipArchiveOutputStream; | ||
var ZipArchiveEntry = require('compress-commons').ZipArchiveEntry; | ||
if (typeof options.level === "number" && options.level >= 0) { | ||
options.zlib.level = options.level; | ||
delete options.level; | ||
} | ||
var util = require('archiver-utils'); | ||
if ( | ||
!options.forceZip64 && | ||
typeof options.zlib.level === "number" && | ||
options.zlib.level === 0 | ||
) { | ||
options.store = true; | ||
} | ||
/** | ||
* @constructor | ||
* @extends external:ZipArchiveOutputStream | ||
* @param {Object} [options] | ||
* @param {String} [options.comment] Sets the zip archive comment. | ||
* @param {Boolean} [options.forceLocalTime=false] Forces the archive to contain local file times instead of UTC. | ||
* @param {Boolean} [options.forceZip64=false] Forces the archive to contain ZIP64 headers. | ||
* @param {Boolean} [options.store=false] Sets the compression method to STORE. | ||
* @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) { | ||
if (!(this instanceof ZipStream)) { | ||
return new ZipStream(options); | ||
} | ||
options.namePrependSlash = options.namePrependSlash || false; | ||
options = this.options = options || {}; | ||
options.zlib = options.zlib || {}; | ||
super(options); | ||
ZipArchiveOutputStream.call(this, options); | ||
if (typeof options.level === 'number' && options.level >= 0) { | ||
options.zlib.level = options.level; | ||
delete options.level; | ||
if (options.comment && options.comment.length > 0) { | ||
this.setComment(options.comment); | ||
} | ||
} | ||
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) { | ||
data = util.defaults(data, { | ||
type: 'file', | ||
name: null, | ||
namePrependSlash: this.options.namePrependSlash, | ||
linkname: null, | ||
date: null, | ||
mode: null, | ||
store: this.options.store, | ||
comment: '' | ||
}); | ||
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) === '/') { | ||
isDir = true; | ||
data.type = 'directory'; | ||
} else if (isDir) { | ||
data.name += '/'; | ||
/** | ||
* Normalizes entry data with fallbacks for key properties. | ||
* | ||
* @private | ||
* @param {Object} data | ||
* @return {Object} | ||
*/ | ||
_normalizeFileData(data) { | ||
data = { | ||
type: "file", | ||
name: null, | ||
namePrependSlash: this.options.namePrependSlash, | ||
linkname: null, | ||
date: null, | ||
mode: null, | ||
store: this.options.store, | ||
comment: "", | ||
...data, | ||
}; | ||
let isDir = data.type === "directory"; | ||
const isSymlink = data.type === "symlink"; | ||
if (data.name) { | ||
data.name = sanitizePath(data.name); | ||
if (!isSymlink && data.name.slice(-1) === "/") { | ||
isDir = true; | ||
data.type = "directory"; | ||
} else if (isDir) { | ||
data.name += "/"; | ||
} | ||
} | ||
if (isDir || isSymlink) { | ||
data.store = true; | ||
} | ||
data.date = dateify(data.date); | ||
return data; | ||
} | ||
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). | ||
* | ||
* @param {(Buffer|Stream|String)} source The input source. | ||
* @param {Object} data | ||
* @param {String} data.name Sets the entry name including internal path. | ||
* @param {String} [data.comment] Sets the entry comment. | ||
* @param {(String|Date)} [data.date=NOW()] Sets the entry date. | ||
* @param {Number} [data.mode=D:0755/F:0644] Sets the entry permissions. | ||
* @param {Boolean} [data.store=options.store] Sets the compression method to STORE. | ||
* @param {String} [data.type=file] Sets the entry type. Defaults to `directory` | ||
* if name ends with trailing slash. | ||
* @param {Function} callback | ||
* @return this | ||
*/ | ||
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')); | ||
return; | ||
} | ||
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')); | ||
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') { | ||
data.mode = 40960; // 0120000 | ||
} | ||
if (typeof data.mode === 'number') { | ||
if (data.type === 'symlink') { | ||
data.mode |= 40960; | ||
/** | ||
* Appends an entry given an input source (text string, buffer, or stream). | ||
* | ||
* @param {(Buffer|Stream|String)} source The input source. | ||
* @param {Object} data | ||
* @param {String} data.name Sets the entry name including internal path. | ||
* @param {String} [data.comment] Sets the entry comment. | ||
* @param {(String|Date)} [data.date=NOW()] Sets the entry date. | ||
* @param {Number} [data.mode=D:0755/F:0644] Sets the entry permissions. | ||
* @param {Boolean} [data.store=options.store] Sets the compression method to STORE. | ||
* @param {String} [data.type=file] Sets the entry type. Defaults to `directory` | ||
* if name ends with trailing slash. | ||
* @param {Function} callback | ||
* @return this | ||
*/ | ||
entry(source, data, callback) { | ||
if (typeof callback !== "function") { | ||
callback = this._emitErrorCallback.bind(this); | ||
} | ||
entry.setUnixMode(data.mode); | ||
data = this._normalizeFileData(data); | ||
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")); | ||
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", | ||
), | ||
); | ||
return; | ||
} | ||
const 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") { | ||
data.mode = 40960; // 0120000 | ||
} | ||
if (typeof data.mode === "number") { | ||
if (data.type === "symlink") { | ||
data.mode |= 40960; | ||
} | ||
entry.setUnixMode(data.mode); | ||
} | ||
if (data.type === "symlink" && typeof data.linkname === "string") { | ||
source = Buffer.from(data.linkname); | ||
} | ||
return super.entry(entry, source, callback); | ||
} | ||
if (data.type === 'symlink' && typeof data.linkname === 'string') { | ||
source = Buffer.from(data.linkname); | ||
/** | ||
* Finalizes the instance and prevents further appending to the archive | ||
* structure (queue will continue til drained). | ||
* | ||
* @return void | ||
*/ | ||
finalize() { | ||
this.finish(); | ||
} | ||
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() { | ||
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} | ||
*/ | ||
} |
{ | ||
"name": "zip-stream", | ||
"version": "6.0.1", | ||
"version": "7.0.0", | ||
"description": "a streaming zip archive generator.", | ||
@@ -18,3 +18,4 @@ "homepage": "https://github.com/archiverjs/node-zip-stream", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"type": "module", | ||
"exports": "index.js", | ||
"files": [ | ||
@@ -24,3 +25,3 @@ "index.js" | ||
"engines": { | ||
"node": ">= 14" | ||
"node": ">=18" | ||
}, | ||
@@ -32,4 +33,4 @@ "scripts": { | ||
"dependencies": { | ||
"archiver-utils": "^5.0.0", | ||
"compress-commons": "^6.0.2", | ||
"compress-commons": "^7.0.0", | ||
"normalize-path": "^3.0.0", | ||
"readable-stream": "^4.0.0" | ||
@@ -39,8 +40,9 @@ }, | ||
"archiver-jsdoc-theme": "1.1.3", | ||
"chai": "4.4.1", | ||
"jsdoc": "4.0.2", | ||
"chai": "5.1.1", | ||
"jsdoc": "4.0.3", | ||
"minami": "1.2.3", | ||
"mkdirp": "3.0.1", | ||
"mocha": "10.3.0", | ||
"rimraf": "5.0.5" | ||
"mocha": "10.7.3", | ||
"prettier": "3.3.3", | ||
"rimraf": "5.0.10" | ||
}, | ||
@@ -47,0 +49,0 @@ "keywords": [ |
@@ -24,6 +24,6 @@ # ZipStream | ||
```js | ||
const Packer = require('zip-stream'); | ||
const archive = new Packer(); // OR new Packer(options) | ||
import { ZipStream } from "zip-stream": | ||
const archive = new ZipStream(); // OR new ZipStream(options) | ||
archive.on('error', function(err) { | ||
archive.on("error", function (err) { | ||
throw err; | ||
@@ -35,5 +35,5 @@ }); | ||
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; | ||
@@ -40,0 +40,0 @@ archive.finish(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Yes
8860
8
152
+ Addednormalize-path@^3.0.0
+ Addedcompress-commons@7.0.0(transitive)
+ Addedcrc32-stream@7.0.1(transitive)
+ Addedis-stream@4.0.1(transitive)
- Removedarchiver-utils@^5.0.0
- Removed@isaacs/cliui@8.0.2(transitive)
- Removed@pkgjs/parseargs@0.11.0(transitive)
- Removedansi-regex@5.0.16.1.0(transitive)
- Removedansi-styles@4.3.06.2.1(transitive)
- Removedarchiver-utils@5.0.2(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@2.0.1(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedcompress-commons@6.0.2(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedcrc32-stream@6.0.0(transitive)
- Removedcross-spawn@7.0.3(transitive)
- Removedeastasianwidth@0.2.0(transitive)
- Removedemoji-regex@8.0.09.2.2(transitive)
- Removedforeground-child@3.3.0(transitive)
- Removedglob@10.4.5(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-fullwidth-code-point@3.0.0(transitive)
- Removedis-stream@2.0.1(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedjackspeak@3.4.3(transitive)
- Removedlazystream@1.0.1(transitive)
- Removedlodash@4.17.21(transitive)
- Removedlru-cache@10.4.3(transitive)
- Removedminimatch@9.0.5(transitive)
- Removedminipass@7.1.2(transitive)
- Removedpackage-json-from-dist@1.0.1(transitive)
- Removedpath-key@3.1.1(transitive)
- Removedpath-scurry@1.11.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedshebang-command@2.0.0(transitive)
- Removedshebang-regex@3.0.0(transitive)
- Removedsignal-exit@4.1.0(transitive)
- Removedstring-width@4.2.35.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedstrip-ansi@6.0.17.1.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwhich@2.0.2(transitive)
- Removedwrap-ansi@7.0.08.1.0(transitive)
Updatedcompress-commons@^7.0.0