[Pkg-javascript-commits] [node-yauzl] 02/08: Imported Upstream version 2.1.0

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Fri Oct 17 06:01:01 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-yauzl.

commit 7c2c9619ce12370a4496965465c650c582bf6fab
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Fri Oct 17 05:56:39 2014 +0000

    Imported Upstream version 2.1.0
---
 README.md    | 42 +++++++++++++++++++++++++-----------------
 index.js     | 31 ++++++++++++++++++++++---------
 package.json |  9 ++++++---
 test/test.js |  8 ++++++++
 4 files changed, 61 insertions(+), 29 deletions(-)

diff --git a/README.md b/README.md
index 2566f8e..6c0a1fc 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,7 @@
 # yauzl
 
-yet another unzip library for node.
+yet another unzip library for node. For zipping, see
+[yazl](https://github.com/thejoshwolfe/yazl).
 
 Design principles:
 
@@ -51,7 +52,7 @@ function defaultCallback(err) {
 
 ### open(path, [options], [callback])
 
-Calls `fs.open(path, "r")` and gives the `fd`, `options`, and `callback` to `fromFd` below.
+Calls `fs.open(path, "r")` and gives the `fd`, `options`, and `callback` to `fromFd()` below.
 
 `options` may be omitted or `null` and defaults to `{autoClose: true}`.
 
@@ -77,9 +78,9 @@ zipfile.once("end", function() {
 
 ### fromBuffer(buffer, [callback])
 
-Like `fromFd`, but reads from a RAM buffer instead of an open file.
+Like `fromFd()`, but reads from a RAM buffer instead of an open file.
 `buffer` is a `Buffer`.
-`callback` is effectively passed directly to `fromFd`.
+`callback` is effectively passed directly to `fromFd()`.
 
 If a `ZipFile` is acquired from this method,
 it will never emit the `close` event,
@@ -95,7 +96,7 @@ so the returned object will use the local timezone.
 ### Class: ZipFile
 
 The constructor for the class is not part of the public API.
-Use `open`, `fromFd`, or `fromBuffer` instead.
+Use `open()`, `fromFd()`, or `fromBuffer()` instead.
 
 #### Event: "entry"
 
@@ -108,31 +109,38 @@ Emitted after the last `entry` event has been emitted.
 #### Event: "close"
 
 Emitted after the fd is actually closed.
-This is after calling `close` (or after the `end` event when `autoClose` is `true`),
-and after all streams created from `openReadStream` have emitted their `end` events.
+This is after calling `close()` (or after the `end` event when `autoClose` is `true`),
+and after all streams created from `openReadStream()` have emitted their `end` events.
 
 This event is never emitted if this `ZipFile` was acquired from `fromBuffer()`.
 
+#### Event: "error"
+
+Emitted in the case of errors with reading the zip file.
+(Note that other errors can be emitted from the streams created from `openReadStream()` as well.)
+After this event has been emitted, no further `entry`, `end`, or `error` events will be emitted,
+but the `close` event may still be emitted.
+
 #### openReadStream(entry, [callback])
 
 `entry` must be an `Entry` object from this `ZipFile`.
 `callback` gets `(err, readStream)`, where `readStream` is a `Readable Stream`.
 If the entry is compressed (with a supported compression method),
 the read stream provides the decompressed data.
-If this zipfile is already closed (see `close`), the `callback` will receive an `err`.
+If this zipfile is already closed (see `close()`), the `callback` will receive an `err`.
 
-#### close([callback])
+#### close()
 
-Causes all future calls to `openReadStream` to fail,
-and calls `fs.close(fd, callback)` after all streams created by `openReadStream` have emitted their `end` events.
+Causes all future calls to `openReadStream()` to fail,
+and closes the fd after all streams created by `openReadStream()` have emitted their `end` events.
 If this object's `end` event has not been emitted yet, this function causes undefined behavior.
 
-If `autoClose` is `true` in the original `open` or `fromFd` call,
+If `autoClose` is `true` in the original `open()` or `fromFd()` call,
 this function will be called automatically effectively in response to this object's `end` event.
 
 #### isOpen
 
-`Boolean`. `true` until `close` is called; then it's `false`.
+`Boolean`. `true` until `close()` is called; then it's `false`.
 
 #### entryCount
 
@@ -199,8 +207,8 @@ If you want to handle errors more gracefully than this,
 be sure to do the following:
 
  * Provide `callback` parameters where they are allowed, and check the `err` parameter.
- * Attach a listener for the `error` event on any `ZipFile` object you get from `open`, `fromFd`, or `fromBuffer`.
- * Attach a listener for the `error` event on any stream you get from `openReadStream`.
+ * Attach a listener for the `error` event on any `ZipFile` object you get from `open()`, `fromFd()`, or `fromBuffer()`.
+ * Attach a listener for the `error` event on any stream you get from `openReadStream()`.
 
 ## Limitations
 
@@ -210,7 +218,7 @@ This library does not support multi-disk zip files.
 The multi-disk fields in the zipfile spec were intended for a zip file to span multiple floppy disks,
 which probably never happens now.
 If the "number of this disk" field in the End of Central Directory Record is not `0`,
-the `open`, `fromFd`, or `fromBuffer` `callback` will receive an `err`.
+the `open()`, `fromFd()`, or `fromBuffer()` `callback` will receive an `err`.
 By extension the following zip file fields are ignored by this library and not provided to clients:
 
  * Disk where central directory starts
@@ -245,7 +253,7 @@ because this library doesn't support the complete zip file spec at any version,
 Regarding the `compressionMethod` field of `Entry` objects,
 only method `0` (stored with no compression)
 and method `8` (deflated) are supported.
-Any of the other 15 official methods will cause the `openReadStream` `callback` to receive an `err`.
+Any of the other 15 official methods will cause the `openReadStream()` `callback` to receive an `err`.
 
 ### No ZIP64 Support
 
diff --git a/index.js b/index.js
index 71efbbf..e6f8cc9 100644
--- a/index.js
+++ b/index.js
@@ -4,7 +4,6 @@ var FdSlicer = require("fd-slicer");
 var util = require("util");
 var EventEmitter = require("events").EventEmitter;
 var PassThrough = require("stream").PassThrough;
-var Iconv = require("iconv").Iconv;
 
 exports.open = open;
 exports.fromFd = fromFd;
@@ -107,7 +106,7 @@ function ZipFile(fdSlicer, cdOffset, fileSize, entryCount, comment, autoClose) {
   // forward close events
   self.fdSlicer.on("error", function(err) {
     // error closing the fd
-    self.emit("error", err);
+    emitError(self, err);
   });
   self.fdSlicer.once("close", function() {
     self.emit("close");
@@ -119,6 +118,7 @@ function ZipFile(fdSlicer, cdOffset, fileSize, entryCount, comment, autoClose) {
   self.entriesRead = 0;
   self.autoClose = !!autoClose;
   self.isOpen = true;
+  self.emittedError = false;
   // make sure events don't fire outta here until the client has a chance to attach listeners
   setImmediate(function() { readEntries(self); });
 }
@@ -130,6 +130,11 @@ ZipFile.prototype.close = function() {
 
 function emitErrorAndAutoClose(self, err) {
   if (self.autoClose) self.close();
+  emitError(self, err);
+}
+function emitError(self, err) {
+  if (self.emittedError) return;
+  self.emittedError = true;
   self.emit("error", err);
 }
 
@@ -137,11 +142,14 @@ function readEntries(self) {
   if (self.entryCount === self.entriesRead) {
     // done with metadata
     if (self.autoClose) self.close();
+    if (self.emittedError) return;
     return self.emit("end");
   }
+  if (self.emittedError) return;
   var buffer = new Buffer(46);
   readFdSlicerNoEof(self.fdSlicer, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
     if (err) return emitErrorAndAutoClose(self, err);
+    if (self.emittedError) return;
     var entry = new Entry();
     // 0 - Central directory file header signature
     var signature = buffer.readUInt32LE(0);
@@ -183,6 +191,7 @@ function readEntries(self) {
     buffer = new Buffer(entry.fileNameLength + entry.extraFieldLength + entry.fileCommentLength);
     readFdSlicerNoEof(self.fdSlicer, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
       if (err) return emitErrorAndAutoClose(self, err);
+      if (self.emittedError) return;
       // 46 - File name
       var isUtf8 = entry.generalPurposeBitFlag & 0x800
       try {
@@ -232,7 +241,7 @@ function readEntries(self) {
 
 ZipFile.prototype.openReadStream = function(entry, callback) {
   var self = this;
-  if (!self.isOpen) return self.emit("error", new Error("closed"));
+  if (!self.isOpen) return callback(new Error("closed"));
   // make sure we don't lose the fd before we open the actual read stream
   self.fdSlicer.ref();
   var buffer = new Buffer(30);
@@ -309,23 +318,27 @@ function dosDateTimeToDate(date, time) {
 }
 
 function readFdSlicerNoEof(fdSlicer, buffer, offset, length, position, callback) {
+  if (length === 0) {
+    // fs.read will throw an out-of-bounds error if you try to read 0 bytes from a 0 byte file
+    return setImmediate(function() { callback(null, new Buffer(0)); });
+  }
   fdSlicer.read(buffer, offset, length, position, function(err, bytesRead) {
     if (err) return callback(err);
     if (bytesRead < length) return callback(new Error("unexpected EOF"));
     callback();
   });
 }
-var cp437_to_utf8 = new Iconv("cp437", "utf8");
+
+var cp437 = '\u0000☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ';
 function bufferToString(buffer, start, end, isUtf8) {
   if (isUtf8) {
     return buffer.toString("utf8", start, end);
   } else {
-    var slice = buffer.slice(start, end);
-    if (slice.length === 0) {
-      return "";
-    } else {
-      return cp437_to_utf8.convert(slice).toString("utf8");
+    var result = "";
+    for (var i = start; i < end; i++) {
+      result += cp437[buffer[i]];
     }
+    return result;
   }
 }
 
diff --git a/package.json b/package.json
index 5fd2856..961df30 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "yauzl",
-  "version": "2.0.1",
+  "version": "2.1.0",
   "description": "yet another unzip library for node",
   "main": "index.js",
   "scripts": {
@@ -11,7 +11,11 @@
     "url": "https://github.com/thejoshwolfe/yauzl.git"
   },
   "keywords": [
-    "unzip"
+    "unzip",
+    "zip",
+    "stream",
+    "archive",
+    "file"
   ],
   "author": "Josh Wolfe <thejoshwolfe at gmail.com>",
   "license": "MIT",
@@ -21,7 +25,6 @@
   "homepage": "https://github.com/thejoshwolfe/yauzl",
   "dependencies": {
     "fd-slicer": "~0.2.1",
-    "iconv": "~2.1.4",
     "pend": "~1.1.3"
   }
 }
diff --git a/test/test.js b/test/test.js
index c01a78d..7dae8b1 100644
--- a/test/test.js
+++ b/test/test.js
@@ -104,14 +104,18 @@ listZipFiles(path.join(__dirname, "success")).forEach(function(zipfilePath) {
 listZipFiles(path.join(__dirname, "failure")).forEach(function(zipfilePath) {
   var expectedErrorMessage = path.basename(zipfilePath).replace(/\.zip$/, "");
   var failedYet = false;
+  var emittedError = false;
   pend.go(function(cb) {
     var operationsInProgress = 0;
     yauzl.open(zipfilePath, function(err, zipfile) {
       if (err) return checkErrorMessage(err);
       zipfile.on("error", function(err) {
+        noEventsAllowedAfterError();
+        emittedError = true;
         checkErrorMessage(err);
       });
       zipfile.on("entry", function(entry) {
+        noEventsAllowedAfterError();
         // let's also try to read directories, cuz whatever.
         operationsInProgress += 1;
         zipfile.openReadStream(entry, function(err, stream) {
@@ -129,6 +133,7 @@ listZipFiles(path.join(__dirname, "failure")).forEach(function(zipfilePath) {
       });
       operationsInProgress += 1;
       zipfile.on("end", function() {
+        noEventsAllowedAfterError();
         doneWithSomething();
       });
       function doneWithSomething() {
@@ -149,6 +154,9 @@ listZipFiles(path.join(__dirname, "failure")).forEach(function(zipfilePath) {
       operationsInProgress = -Infinity;
       cb();
     }
+    function noEventsAllowedAfterError() {
+      if (emittedError) throw new Error("events emitted after error event");
+    }
   });
 });
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-yauzl.git



More information about the Pkg-javascript-commits mailing list