yauzl-clone
Advanced tools
Comparing version 1.0.4 to 2.0.0
@@ -13,2 +13,3 @@ /* -------------------- | ||
// Exports | ||
module.exports = { | ||
@@ -21,5 +22,10 @@ clone, | ||
/** | ||
* yauzlClone.clone( yauzl [, options] ) | ||
* Clone `yauzl` object and optionally subclass `yauzl.ZipFile` + `yauzl.Entry`. | ||
* | ||
* Clone `yauzl` object and optionally subclass `yauzl.ZipFile` + `yauzl.Entry` | ||
* If ZipFile is subclassed, all access methods (`.open()`, `.fromFd()` etc) are | ||
* patched to callback with an instance of the ZipFile subclass. | ||
* | ||
* If Entry is subclassed, emitted 'entry' events are intercepted and re-emitted | ||
* so entries are instance of the entry subclass. | ||
* | ||
* @param {Object} yauzl - yauzl module object | ||
@@ -35,21 +41,16 @@ * @param {Object} [options] - options object | ||
* @returns {Object} - Cloned yauzl module object | ||
* | ||
* If ZipFile is subclassed, all access methods (`.open()`, `.fromFd()` etc) are | ||
* patched to callback with an instance of the ZipFile subclass. | ||
* | ||
* If Entry is subclassed, emitted 'entry' events are intercepted and re-emitted | ||
* so entries are instance of the entry subclass. | ||
*/ | ||
function clone(yauzl, options) { | ||
// Conform options | ||
options = Object.assign({ | ||
options = { | ||
clone: true, | ||
subclassZipFile: false, | ||
subclassEntry: false, | ||
eventsIntercept: false | ||
}, options); | ||
eventsIntercept: false, | ||
...options | ||
}; | ||
if (options.subclassEntry) options.eventsIntercept = true; | ||
// Clone main object | ||
if (options.clone) yauzl = Object.assign({}, yauzl); | ||
if (options.clone) yauzl = {...yauzl}; | ||
@@ -60,4 +61,4 @@ // Subclass ZipFile | ||
const original = yauzl.ZipFile; | ||
yauzl.ZipFile = function ZipFile() { | ||
original.apply(this, arguments); | ||
yauzl.ZipFile = function ZipFile(...args) { | ||
original.apply(this, args); | ||
}; | ||
@@ -67,2 +68,3 @@ util.inherits(yauzl.ZipFile, original); | ||
// Patch access methods to callback with instance of ZipFile subclass | ||
// eslint-disable-next-line no-shadow | ||
patchAll(yauzl, original => zipFilePatcher(original, yauzl.ZipFile)); | ||
@@ -84,4 +86,4 @@ } | ||
const original = yauzl.Entry; | ||
yauzl.Entry = function Entry() { | ||
original.apply(this, arguments); | ||
yauzl.Entry = function Entry(...args) { | ||
original.apply(this, args); | ||
}; | ||
@@ -92,2 +94,3 @@ util.inherits(yauzl.Entry, original); | ||
// which re-emits instances of Entry subclass | ||
// eslint-disable-next-line no-shadow | ||
patchAll(yauzl, original => entryPatcher(original, yauzl.Entry)); | ||
@@ -101,11 +104,10 @@ } | ||
/** | ||
* yauzlClone.patchAll( yauzl, fn ) | ||
* Patch all access methods with patcher function `fn`. | ||
* | ||
* Patch all access methods with patcher function `fn` | ||
* Patcher function will be called with arguments `(original)` which is original | ||
* method (see below for complications). | ||
* | ||
* @param {Object} yauzl - yauzl module object | ||
* @param {Function} fn - Patcher function | ||
* @returns {undefined} | ||
* | ||
* Patcher function will be called with arguments `(original)` which is original | ||
* method (see below for complications) | ||
*/ | ||
@@ -120,10 +122,4 @@ function patchAll(yauzl, fn) { | ||
/** | ||
* yauzlClone.patch( yauzl, fnName, fn ) | ||
* Patch access method with patcher function `fn`. | ||
* | ||
* Patch access method with patcher function `fn` | ||
* @param {Object} yauzl - yauzl module object | ||
* @param {String} fnName - Name of method to patch | ||
* @param {Function} fn - Patcher function | ||
* @returns {Function} - Patched function | ||
* | ||
* Patcher function will be called with arguments `(original)` which is original | ||
@@ -139,2 +135,3 @@ * method. | ||
* e.g.: | ||
* ```js | ||
* patchMethod(yauzl, 'open', function(original) { | ||
@@ -148,2 +145,8 @@ * return function(path, _, options, callback) { | ||
* }); | ||
* ``` | ||
* | ||
* @param {Object} yauzl - yauzl module object | ||
* @param {string} methodName - Name of method to patch | ||
* @param {Function} fn - Patcher function | ||
* @returns {Function} - Patched function | ||
*/ | ||
@@ -155,7 +158,7 @@ function patch(yauzl, methodName, fn) { | ||
const original = yauzl[methodName]; | ||
if (methodName == 'fromRandomAccessReader') { | ||
if (methodName === 'fromRandomAccessReader') { | ||
const shimmed = fn(original); | ||
yauzl.fromRandomAccessReader = function(reader, totalSize, options, callback) { | ||
if (typeof options == 'function') { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
@@ -170,8 +173,8 @@ options = {}; | ||
} else { | ||
const shimmed = fn(function(path, unused, options, callback) { // jshint ignore:line | ||
return original.call(this, path, options, callback); | ||
const shimmed = fn(function(path, unused, options, callback) { | ||
return original.call(this, path, options, callback); // eslint-disable-line no-invalid-this | ||
}); | ||
yauzl[methodName] = function(path, options, callback) { | ||
if (typeof options == 'function') { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
@@ -193,3 +196,6 @@ options = {}; | ||
* Patcher to make all access methods callback with instance of ZipFile subclass | ||
* (rather than original yauzl.ZipFile class) | ||
* (rather than original yauzl.ZipFile class). | ||
* @param {Function} original - Original function | ||
* @param {Function} ZipFile - `ZipFile` subclass | ||
* @returns {Function} - Patched function | ||
*/ | ||
@@ -201,8 +207,12 @@ function zipFilePatcher(original, ZipFile) { | ||
const {lazyEntries} = options, | ||
hasLazyEntries = options.hasOwnProperty('lazyEntries'); | ||
hasLazyEntries = Object.prototype.hasOwnProperty.call(options, 'lazyEntries'); | ||
if (!lazyEntries) options.lazyEntries = true; | ||
// Call original method | ||
return original.call(this, path, totalSize, options, function(err, zipFile) { | ||
if (err) return callback(err); | ||
// eslint-disable-next-line no-invalid-this | ||
return original.call(this, path, totalSize, options, (err, zipFile) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
@@ -239,10 +249,17 @@ // Convert to instance of subclass | ||
* Patcher to make all access methods attach event interceptor to zipFiles | ||
* which intercept 'entry' events and re-emit entries from Entry subclass | ||
* which intercept 'entry' events and re-emit entries from Entry subclass. | ||
* @param {Function} original - Original function | ||
* @param {Function} Entry - `Entry` subclass | ||
* @returns {Function} - Patched function | ||
*/ | ||
function entryPatcher(original, Entry) { | ||
return function(path, totalSize, options, callback) { | ||
return original.call(this, path, totalSize, options, function(err, zipFile) { | ||
if (err) return callback(err); | ||
// eslint-disable-next-line no-invalid-this | ||
return original.call(this, path, totalSize, options, (err, zipFile) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
zipFile.intercept('entry', function(entry, cb) { | ||
zipFile.intercept('entry', (entry, cb) => { | ||
entry = Object.assign(Object.create(Entry.prototype), entry); | ||
@@ -249,0 +266,0 @@ cb(null, entry); |
{ | ||
"name": "yauzl-clone", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"description": "Clone yauzl for patching", | ||
"main": "./lib/", | ||
"main": "index.js", | ||
"files": [ | ||
"lib/**/*.js" | ||
], | ||
"author": { | ||
@@ -20,10 +23,12 @@ "name": "Overlook Motel" | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"coveralls": "^3.0.1", | ||
"cross-env": "^5.1.6", | ||
"fd-slicer": "^1.0.1", | ||
"istanbul": "^0.4.5", | ||
"jshint": "^2.9.5", | ||
"mocha": "^5.2.0", | ||
"yauzl": "^2.9.1" | ||
"@overlookmotel/eslint-config": "^10.1.0", | ||
"@overlookmotel/eslint-config-jest": "^6.0.1", | ||
"@overlookmotel/eslint-config-node": "^4.1.0", | ||
"eslint": "^8.39.0", | ||
"fd-slicer": "^1.1.0", | ||
"jest": "^29.5.0", | ||
"jest-extended": "^3.2.4", | ||
"jest-runner-eslint": "^2.0.0", | ||
"npm-run-all": "^4.1.5", | ||
"yauzl": "^2.10.0" | ||
}, | ||
@@ -37,12 +42,12 @@ "keywords": [ | ||
"scripts": { | ||
"test": "npm run jshint && npm run test-main", | ||
"jshint": "jshint lib test", | ||
"test-main": "mocha --check-leaks --colors -t 10000 -R spec \"test/**/*.test.js\"", | ||
"cover": "npm run cover-main && rm -rf coverage", | ||
"coveralls": "npm run cover-main && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", | ||
"cover-main": "cross-env COVERAGE=true istanbul cover _mocha --report lcovonly -- -t 10000 -R spec \"test/**/*.test.js\"", | ||
"travis": "if [ $COVERAGE ]; then npm run coveralls; else npm test; fi" | ||
"test": "npm run lint && npm run test-main", | ||
"lint": "jest --config=jest-eslint.config.js", | ||
"lint-fix": "eslint . --fix", | ||
"test-main": "jest", | ||
"cover": "npm-run-all -c cover-main cover-cleanup", | ||
"cover-main": "jest --coverage", | ||
"cover-cleanup": "rm -rf coverage" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=16" | ||
}, | ||
@@ -49,0 +54,0 @@ "readmeFilename": "README.md", |
@@ -1,17 +0,10 @@ | ||
# yauzl-clone.js | ||
# Clone yauzl for patching | ||
## Current status | ||
[](https://www.npmjs.com/package/yauzl-clone) | ||
[](http://travis-ci.org/overlookmotel/yauzl-clone) | ||
[](https://david-dm.org/overlookmotel/yauzl-clone) | ||
[](https://david-dm.org/overlookmotel/yauzl-clone) | ||
[](https://greenkeeper.io/) | ||
[](https://github.com/overlookmotel/yauzl-clone/actions) | ||
[](https://coveralls.io/r/overlookmotel/yauzl-clone) | ||
# Clone `yauzl` for patching | ||
## Purpose | ||
This module does not have any useful function in itself. It is purely designed to help with creating modules that modify [yauzl](https://www.npmjs.com/package/yauzl) unzipping library in some way. | ||
This module contains tools to help with creating modules that modify [yauzl](https://www.npmjs.com/package/yauzl) unzipping library in some way. | ||
@@ -39,3 +32,3 @@ [yauzl-promise](https://www.npmjs.com/package/yauzl-promise) and [yauzl-crc](https://www.npmjs.com/package/yauzl-crc), for example, use this module internally. | ||
#### clone | ||
#### `clone` | ||
@@ -51,9 +44,9 @@ Clones the yauzl object. Equivalent to `Object.assign({}, yauzl)`. | ||
#### subclassZipFile | ||
#### `subclassZipFile` | ||
Creates a subclass of `yauzl.ZipFile`. The prototype of `yauzl.ZipFile` can then be altered without affecting the original. | ||
This option also monkey patches the access methods (`.open()`, `.fromFd()`, `.fromBuffer()`, `.testFromRandomAccessReader()`) to callback with instances of this `ZipFile` subclass. | ||
This option also monkey-patches the access methods (`.open()`, `.fromFd()`, `.fromBuffer()`, `.testFromRandomAccessReader()`) to callback with instances of this `ZipFile` subclass. | ||
#### subclassEntry | ||
#### `subclassEntry` | ||
@@ -64,3 +57,3 @@ Creates a subclass of `yauzl.Entry` (same idea as `subclassZipFile`). | ||
#### eventsIntercept | ||
#### `eventsIntercept` | ||
@@ -102,4 +95,4 @@ Adds [events-intercept](https://www.npmjs.com/package/events-intercept) methods to `ZipFile` prototype. This option is automatically set to `true` if `subclassEntry` option is `true`. | ||
yauzlClone.patch( yauzl, 'open', function(original) { | ||
return function(path, unused, options, callback) { | ||
// NB Notice `unused` argument above | ||
return function(path, _unused, options, callback) { | ||
// NB Notice `_unused` argument above | ||
original(reader, null, options, function(err, zipFile) { | ||
@@ -128,2 +121,8 @@ if (err) return callback(err); | ||
## Versioning | ||
This module follows [semver](https://semver.org/). Breaking changes will only be made in major version updates. | ||
All active NodeJS release lines are supported (v16+ at time of writing). After a release line of NodeJS reaches end of life according to [Node's LTS schedule](https://nodejs.org/en/about/releases/), support for that version of Node may be dropped at any time, and this will not be considered a breaking change. Dropping support for a Node version will be made in a minor version update (e.g. 1.2.0 to 1.3.0). If you are using a Node version which is approaching end of life, pin your dependency of this module to patch updates only using tilde (`~`) e.g. `~1.2.3` to avoid breakages. | ||
## Tests | ||
@@ -146,4 +145,4 @@ | ||
* ensure all tests pass before submitting PR | ||
* add an entry to changelog | ||
* add tests for new features | ||
* document new functionality/API additions in README | ||
* do not add an entry to Changelog (Changelog is created when cutting releases) |
Sorry, the diff of this file is not supported yet
16098
233
10
143