index.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. var fs = require("fs");
  2. var zlib = require("zlib");
  3. var fd_slicer = require("fd-slicer");
  4. var util = require("util");
  5. var EventEmitter = require("events").EventEmitter;
  6. var Transform = require("stream").Transform;
  7. var PassThrough = require("stream").PassThrough;
  8. var Writable = require("stream").Writable;
  9. exports.open = open;
  10. exports.fromFd = fromFd;
  11. exports.fromBuffer = fromBuffer;
  12. exports.fromRandomAccessReader = fromRandomAccessReader;
  13. exports.dosDateTimeToDate = dosDateTimeToDate;
  14. exports.ZipFile = ZipFile;
  15. exports.Entry = Entry;
  16. exports.RandomAccessReader = RandomAccessReader;
  17. function open(path, options, callback) {
  18. if (typeof options === "function") {
  19. callback = options;
  20. options = null;
  21. }
  22. if (options == null) options = {};
  23. if (options.autoClose == null) options.autoClose = true;
  24. if (options.lazyEntries == null) options.lazyEntries = false;
  25. if (callback == null) callback = defaultCallback;
  26. fs.open(path, "r", function(err, fd) {
  27. if (err) return callback(err);
  28. fromFd(fd, options, function(err, zipfile) {
  29. if (err) fs.close(fd, defaultCallback);
  30. callback(err, zipfile);
  31. });
  32. });
  33. }
  34. function fromFd(fd, options, callback) {
  35. if (typeof options === "function") {
  36. callback = options;
  37. options = null;
  38. }
  39. if (options == null) options = {};
  40. if (options.autoClose == null) options.autoClose = false;
  41. if (options.lazyEntries == null) options.lazyEntries = false;
  42. if (callback == null) callback = defaultCallback;
  43. fs.fstat(fd, function(err, stats) {
  44. if (err) return callback(err);
  45. var reader = fd_slicer.createFromFd(fd, {autoClose: true});
  46. fromRandomAccessReader(reader, stats.size, options, callback);
  47. });
  48. }
  49. function fromBuffer(buffer, options, callback) {
  50. if (typeof options === "function") {
  51. callback = options;
  52. options = null;
  53. }
  54. if (options == null) options = {};
  55. options.autoClose = false;
  56. if (options.lazyEntries == null) options.lazyEntries = false;
  57. // i got your open file right here.
  58. var reader = fd_slicer.createFromBuffer(buffer);
  59. fromRandomAccessReader(reader, buffer.length, options, callback);
  60. }
  61. function fromRandomAccessReader(reader, totalSize, options, callback) {
  62. if (typeof options === "function") {
  63. callback = options;
  64. options = null;
  65. }
  66. if (options == null) options = {};
  67. if (options.autoClose == null) options.autoClose = true;
  68. if (options.lazyEntries == null) options.lazyEntries = false;
  69. if (callback == null) callback = defaultCallback;
  70. if (typeof totalSize !== "number") throw new Error("expected totalSize parameter to be a number");
  71. if (totalSize > Number.MAX_SAFE_INTEGER) {
  72. throw new Error("zip file too large. only file sizes up to 2^52 are supported due to JavaScript's Number type being an IEEE 754 double.");
  73. }
  74. // the matching unref() call is in zipfile.close()
  75. reader.ref();
  76. // eocdr means End of Central Directory Record.
  77. // search backwards for the eocdr signature.
  78. // the last field of the eocdr is a variable-length comment.
  79. // the comment size is encoded in a 2-byte field in the eocdr, which we can't find without trudging backwards through the comment to find it.
  80. // as a consequence of this design decision, it's possible to have ambiguous zip file metadata if a coherent eocdr was in the comment.
  81. // we search backwards for a eocdr signature, and hope that whoever made the zip file was smart enough to forbid the eocdr signature in the comment.
  82. var eocdrWithoutCommentSize = 22;
  83. var maxCommentSize = 0x10000; // 2-byte size
  84. var bufferSize = Math.min(eocdrWithoutCommentSize + maxCommentSize, totalSize);
  85. var buffer = new Buffer(bufferSize);
  86. var bufferReadStart = totalSize - buffer.length;
  87. readAndAssertNoEof(reader, buffer, 0, bufferSize, bufferReadStart, function(err) {
  88. if (err) return callback(err);
  89. for (var i = bufferSize - eocdrWithoutCommentSize; i >= 0; i -= 1) {
  90. if (buffer.readUInt32LE(i) !== 0x06054b50) continue;
  91. // found eocdr
  92. var eocdrBuffer = buffer.slice(i);
  93. // 0 - End of central directory signature = 0x06054b50
  94. // 4 - Number of this disk
  95. var diskNumber = eocdrBuffer.readUInt16LE(4);
  96. if (diskNumber !== 0) return callback(new Error("multi-disk zip files are not supported: found disk number: " + diskNumber));
  97. // 6 - Disk where central directory starts
  98. // 8 - Number of central directory records on this disk
  99. // 10 - Total number of central directory records
  100. var entryCount = eocdrBuffer.readUInt16LE(10);
  101. // 12 - Size of central directory (bytes)
  102. // 16 - Offset of start of central directory, relative to start of archive
  103. var centralDirectoryOffset = eocdrBuffer.readUInt32LE(16);
  104. // 20 - Comment length
  105. var commentLength = eocdrBuffer.readUInt16LE(20);
  106. var expectedCommentLength = eocdrBuffer.length - eocdrWithoutCommentSize;
  107. if (commentLength !== expectedCommentLength) {
  108. return callback(new Error("invalid comment length. expected: " + expectedCommentLength + ". found: " + commentLength));
  109. }
  110. // 22 - Comment
  111. // the encoding is always cp437.
  112. var comment = bufferToString(eocdrBuffer, 22, eocdrBuffer.length, false);
  113. if (!(entryCount === 0xffff || centralDirectoryOffset === 0xffffffff)) {
  114. return callback(null, new ZipFile(reader, centralDirectoryOffset, totalSize, entryCount, comment, options.autoClose, options.lazyEntries));
  115. }
  116. // ZIP64 format
  117. // ZIP64 Zip64 end of central directory locator
  118. var zip64EocdlBuffer = new Buffer(20);
  119. var zip64EocdlOffset = bufferReadStart + i - zip64EocdlBuffer.length;
  120. readAndAssertNoEof(reader, zip64EocdlBuffer, 0, zip64EocdlBuffer.length, zip64EocdlOffset, function(err) {
  121. if (err) return callback(err);
  122. // 0 - zip64 end of central dir locator signature = 0x07064b50
  123. if (zip64EocdlBuffer.readUInt32LE(0) !== 0x07064b50) {
  124. return callback(new Error("invalid ZIP64 End of Central Directory Locator signature"));
  125. }
  126. // 4 - number of the disk with the start of the zip64 end of central directory
  127. // 8 - relative offset of the zip64 end of central directory record
  128. var zip64EocdrOffset = readUInt64LE(zip64EocdlBuffer, 8);
  129. // 16 - total number of disks
  130. // ZIP64 end of central directory record
  131. var zip64EocdrBuffer = new Buffer(56);
  132. readAndAssertNoEof(reader, zip64EocdrBuffer, 0, zip64EocdrBuffer.length, zip64EocdrOffset, function(err) {
  133. if (err) return callback(err);
  134. // 0 - zip64 end of central dir signature 4 bytes (0x06064b50)
  135. if (zip64EocdrBuffer.readUInt32LE(0) !== 0x06064b50) return callback(new Error("invalid ZIP64 end of central directory record signature"));
  136. // 4 - size of zip64 end of central directory record 8 bytes
  137. // 12 - version made by 2 bytes
  138. // 14 - version needed to extract 2 bytes
  139. // 16 - number of this disk 4 bytes
  140. // 20 - number of the disk with the start of the central directory 4 bytes
  141. // 24 - total number of entries in the central directory on this disk 8 bytes
  142. // 32 - total number of entries in the central directory 8 bytes
  143. entryCount = readUInt64LE(zip64EocdrBuffer, 32);
  144. // 40 - size of the central directory 8 bytes
  145. // 48 - offset of start of central directory with respect to the starting disk number 8 bytes
  146. centralDirectoryOffset = readUInt64LE(zip64EocdrBuffer, 48);
  147. // 56 - zip64 extensible data sector (variable size)
  148. return callback(null, new ZipFile(reader, centralDirectoryOffset, totalSize, entryCount, comment, options.autoClose, options.lazyEntries));
  149. });
  150. });
  151. return;
  152. }
  153. callback(new Error("end of central directory record signature not found"));
  154. });
  155. }
  156. util.inherits(ZipFile, EventEmitter);
  157. function ZipFile(reader, centralDirectoryOffset, fileSize, entryCount, comment, autoClose, lazyEntries) {
  158. var self = this;
  159. EventEmitter.call(self);
  160. self.reader = reader;
  161. // forward close events
  162. self.reader.on("error", function(err) {
  163. // error closing the fd
  164. emitError(self, err);
  165. });
  166. self.reader.once("close", function() {
  167. self.emit("close");
  168. });
  169. self.readEntryCursor = centralDirectoryOffset;
  170. self.fileSize = fileSize;
  171. self.entryCount = entryCount;
  172. self.comment = comment;
  173. self.entriesRead = 0;
  174. self.autoClose = !!autoClose;
  175. self.lazyEntries = !!lazyEntries;
  176. self.isOpen = true;
  177. self.emittedError = false;
  178. if (!self.lazyEntries) self.readEntry();
  179. }
  180. ZipFile.prototype.close = function() {
  181. if (!this.isOpen) return;
  182. this.isOpen = false;
  183. this.reader.unref();
  184. };
  185. function emitErrorAndAutoClose(self, err) {
  186. if (self.autoClose) self.close();
  187. emitError(self, err);
  188. }
  189. function emitError(self, err) {
  190. if (self.emittedError) return;
  191. self.emittedError = true;
  192. self.emit("error", err);
  193. }
  194. ZipFile.prototype.readEntry = function() {
  195. var self = this;
  196. if (self.entryCount === self.entriesRead) {
  197. // done with metadata
  198. setImmediate(function() {
  199. if (self.autoClose) self.close();
  200. if (self.emittedError) return;
  201. self.emit("end");
  202. });
  203. return;
  204. }
  205. if (self.emittedError) return;
  206. var buffer = new Buffer(46);
  207. readAndAssertNoEof(self.reader, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
  208. if (err) return emitErrorAndAutoClose(self, err);
  209. if (self.emittedError) return;
  210. var entry = new Entry();
  211. // 0 - Central directory file header signature
  212. var signature = buffer.readUInt32LE(0);
  213. if (signature !== 0x02014b50) return emitErrorAndAutoClose(self, new Error("invalid central directory file header signature: 0x" + signature.toString(16)));
  214. // 4 - Version made by
  215. entry.versionMadeBy = buffer.readUInt16LE(4);
  216. // 6 - Version needed to extract (minimum)
  217. entry.versionNeededToExtract = buffer.readUInt16LE(6);
  218. // 8 - General purpose bit flag
  219. entry.generalPurposeBitFlag = buffer.readUInt16LE(8);
  220. // 10 - Compression method
  221. entry.compressionMethod = buffer.readUInt16LE(10);
  222. // 12 - File last modification time
  223. entry.lastModFileTime = buffer.readUInt16LE(12);
  224. // 14 - File last modification date
  225. entry.lastModFileDate = buffer.readUInt16LE(14);
  226. // 16 - CRC-32
  227. entry.crc32 = buffer.readUInt32LE(16);
  228. // 20 - Compressed size
  229. entry.compressedSize = buffer.readUInt32LE(20);
  230. // 24 - Uncompressed size
  231. entry.uncompressedSize = buffer.readUInt32LE(24);
  232. // 28 - File name length (n)
  233. entry.fileNameLength = buffer.readUInt16LE(28);
  234. // 30 - Extra field length (m)
  235. entry.extraFieldLength = buffer.readUInt16LE(30);
  236. // 32 - File comment length (k)
  237. entry.fileCommentLength = buffer.readUInt16LE(32);
  238. // 34 - Disk number where file starts
  239. // 36 - Internal file attributes
  240. entry.internalFileAttributes = buffer.readUInt16LE(36);
  241. // 38 - External file attributes
  242. entry.externalFileAttributes = buffer.readUInt32LE(38);
  243. // 42 - Relative offset of local file header
  244. entry.relativeOffsetOfLocalHeader = buffer.readUInt32LE(42);
  245. self.readEntryCursor += 46;
  246. buffer = new Buffer(entry.fileNameLength + entry.extraFieldLength + entry.fileCommentLength);
  247. readAndAssertNoEof(self.reader, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
  248. if (err) return emitErrorAndAutoClose(self, err);
  249. if (self.emittedError) return;
  250. // 46 - File name
  251. var isUtf8 = entry.generalPurposeBitFlag & 0x800
  252. try {
  253. entry.fileName = bufferToString(buffer, 0, entry.fileNameLength, isUtf8);
  254. } catch (e) {
  255. return emitErrorAndAutoClose(self, e);
  256. }
  257. // 46+n - Extra field
  258. var fileCommentStart = entry.fileNameLength + entry.extraFieldLength;
  259. var extraFieldBuffer = buffer.slice(entry.fileNameLength, fileCommentStart);
  260. entry.extraFields = [];
  261. var i = 0;
  262. while (i < extraFieldBuffer.length) {
  263. var headerId = extraFieldBuffer.readUInt16LE(i + 0);
  264. var dataSize = extraFieldBuffer.readUInt16LE(i + 2);
  265. var dataStart = i + 4;
  266. var dataEnd = dataStart + dataSize;
  267. var dataBuffer = new Buffer(dataSize);
  268. extraFieldBuffer.copy(dataBuffer, 0, dataStart, dataEnd);
  269. entry.extraFields.push({
  270. id: headerId,
  271. data: dataBuffer,
  272. });
  273. i = dataEnd;
  274. }
  275. // 46+n+m - File comment
  276. try {
  277. entry.fileComment = bufferToString(buffer, fileCommentStart, fileCommentStart + entry.fileCommentLength, isUtf8);
  278. } catch (e) {
  279. return emitErrorAndAutoClose(self, e);
  280. }
  281. self.readEntryCursor += buffer.length;
  282. self.entriesRead += 1;
  283. if (entry.uncompressedSize === 0xffffffff ||
  284. entry.compressedSize === 0xffffffff ||
  285. entry.relativeOffsetOfLocalHeader === 0xffffffff) {
  286. // ZIP64 format
  287. // find the Zip64 Extended Information Extra Field
  288. var zip64EiefBuffer = null;
  289. for (var i = 0; i < entry.extraFields.length; i++) {
  290. var extraField = entry.extraFields[i];
  291. if (extraField.id === 0x0001) {
  292. zip64EiefBuffer = extraField.data;
  293. break;
  294. }
  295. }
  296. if (zip64EiefBuffer == null) return emitErrorAndAutoClose(self, new Error("expected Zip64 Extended Information Extra Field"));
  297. var index = 0;
  298. // 0 - Original Size 8 bytes
  299. if (entry.uncompressedSize === 0xffffffff) {
  300. if (index + 8 > zip64EiefBuffer.length) return emitErrorAndAutoClose(self, new Error("Zip64 Extended Information Extra Field does not include Original Size"));
  301. entry.uncompressedSize = readUInt64LE(zip64EiefBuffer, index);
  302. index += 8;
  303. }
  304. // 8 - Compressed Size 8 bytes
  305. if (entry.compressedSize === 0xffffffff) {
  306. if (index + 8 > zip64EiefBuffer.length) return emitErrorAndAutoClose(self, new Error("Zip64 Extended Information Extra Field does not include Compressed Size"));
  307. entry.compressedSize = readUInt64LE(zip64EiefBuffer, index);
  308. index += 8;
  309. }
  310. // 16 - Relative Header Offset 8 bytes
  311. if (entry.relativeOffsetOfLocalHeader === 0xffffffff) {
  312. if (index + 8 > zip64EiefBuffer.length) return emitErrorAndAutoClose(self, new Error("Zip64 Extended Information Extra Field does not include Relative Header Offset"));
  313. entry.relativeOffsetOfLocalHeader = readUInt64LE(zip64EiefBuffer, index);
  314. index += 8;
  315. }
  316. // 24 - Disk Start Number 4 bytes
  317. }
  318. // validate file size
  319. if (entry.compressionMethod === 0) {
  320. if (entry.compressedSize !== entry.uncompressedSize) {
  321. var msg = "compressed/uncompressed size mismatch for stored file: " + entry.compressedSize + " != " + entry.uncompressedSize;
  322. return emitErrorAndAutoClose(self, new Error(msg));
  323. }
  324. }
  325. // validate file name
  326. if (entry.fileName.indexOf("\\") !== -1) return emitErrorAndAutoClose(self, new Error("invalid characters in fileName: " + entry.fileName));
  327. if (/^[a-zA-Z]:/.test(entry.fileName) || /^\//.test(entry.fileName)) return emitErrorAndAutoClose(self, new Error("absolute path: " + entry.fileName));
  328. if (entry.fileName.split("/").indexOf("..") !== -1) return emitErrorAndAutoClose(self, new Error("invalid relative path: " + entry.fileName));
  329. self.emit("entry", entry);
  330. if (!self.lazyEntries) self.readEntry();
  331. });
  332. });
  333. };
  334. ZipFile.prototype.openReadStream = function(entry, callback) {
  335. var self = this;
  336. if (!self.isOpen) return callback(new Error("closed"));
  337. // make sure we don't lose the fd before we open the actual read stream
  338. self.reader.ref();
  339. var buffer = new Buffer(30);
  340. readAndAssertNoEof(self.reader, buffer, 0, buffer.length, entry.relativeOffsetOfLocalHeader, function(err) {
  341. try {
  342. if (err) return callback(err);
  343. // 0 - Local file header signature = 0x04034b50
  344. var signature = buffer.readUInt32LE(0);
  345. if (signature !== 0x04034b50) return callback(new Error("invalid local file header signature: 0x" + signature.toString(16)));
  346. // all this should be redundant
  347. // 4 - Version needed to extract (minimum)
  348. // 6 - General purpose bit flag
  349. // 8 - Compression method
  350. // 10 - File last modification time
  351. // 12 - File last modification date
  352. // 14 - CRC-32
  353. // 18 - Compressed size
  354. // 22 - Uncompressed size
  355. // 26 - File name length (n)
  356. var fileNameLength = buffer.readUInt16LE(26);
  357. // 28 - Extra field length (m)
  358. var extraFieldLength = buffer.readUInt16LE(28);
  359. // 30 - File name
  360. // 30+n - Extra field
  361. var localFileHeaderEnd = entry.relativeOffsetOfLocalHeader + buffer.length + fileNameLength + extraFieldLength;
  362. var compressed;
  363. if (entry.compressionMethod === 0) {
  364. // 0 - The file is stored (no compression)
  365. compressed = false;
  366. } else if (entry.compressionMethod === 8) {
  367. // 8 - The file is Deflated
  368. compressed = true;
  369. } else {
  370. return callback(new Error("unsupported compression method: " + entry.compressionMethod));
  371. }
  372. var fileDataStart = localFileHeaderEnd;
  373. var fileDataEnd = fileDataStart + entry.compressedSize;
  374. if (entry.compressedSize !== 0) {
  375. // bounds check now, because the read streams will probably not complain loud enough.
  376. // since we're dealing with an unsigned offset plus an unsigned size,
  377. // we only have 1 thing to check for.
  378. if (fileDataEnd > self.fileSize) {
  379. return callback(new Error("file data overflows file bounds: " +
  380. fileDataStart + " + " + entry.compressedSize + " > " + self.fileSize));
  381. }
  382. }
  383. var readStream = self.reader.createReadStream({start: fileDataStart, end: fileDataEnd});
  384. var endpointStream = readStream;
  385. if (compressed) {
  386. var destroyed = false;
  387. var inflateFilter = zlib.createInflateRaw();
  388. readStream.on("error", function(err) {
  389. // setImmediate here because errors can be emitted during the first call to pipe()
  390. setImmediate(function() {
  391. if (!destroyed) inflateFilter.emit("error", err);
  392. });
  393. });
  394. var checkerStream = new AssertByteCountStream(entry.uncompressedSize);
  395. inflateFilter.on("error", function(err) {
  396. // forward zlib errors to the client-visible stream
  397. setImmediate(function() {
  398. if (!destroyed) checkerStream.emit("error", err);
  399. });
  400. });
  401. checkerStream.destroy = function() {
  402. destroyed = true;
  403. inflateFilter.unpipe(checkerStream);
  404. readStream.unpipe(inflateFilter);
  405. // TODO: the inflateFilter now causes a memory leak. see Issue #27.
  406. readStream.destroy();
  407. };
  408. endpointStream = readStream.pipe(inflateFilter).pipe(checkerStream);
  409. }
  410. callback(null, endpointStream);
  411. } finally {
  412. self.reader.unref();
  413. }
  414. });
  415. };
  416. function Entry() {
  417. }
  418. Entry.prototype.getLastModDate = function() {
  419. return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime);
  420. };
  421. function dosDateTimeToDate(date, time) {
  422. var day = date & 0x1f; // 1-31
  423. var month = (date >> 5 & 0xf) - 1; // 1-12, 0-11
  424. var year = (date >> 9 & 0x7f) + 1980; // 0-128, 1980-2108
  425. var millisecond = 0;
  426. var second = (time & 0x1f) * 2; // 0-29, 0-58 (even numbers)
  427. var minute = time >> 5 & 0x3f; // 0-59
  428. var hour = time >> 11 & 0x1f; // 0-23
  429. return new Date(year, month, day, hour, minute, second, millisecond);
  430. }
  431. function readAndAssertNoEof(reader, buffer, offset, length, position, callback) {
  432. if (length === 0) {
  433. // fs.read will throw an out-of-bounds error if you try to read 0 bytes from a 0 byte file
  434. return setImmediate(function() { callback(null, new Buffer(0)); });
  435. }
  436. reader.read(buffer, offset, length, position, function(err, bytesRead) {
  437. if (err) return callback(err);
  438. if (bytesRead < length) return callback(new Error("unexpected EOF"));
  439. callback();
  440. });
  441. }
  442. util.inherits(AssertByteCountStream, Transform);
  443. function AssertByteCountStream(byteCount) {
  444. Transform.call(this);
  445. this.actualByteCount = 0;
  446. this.expectedByteCount = byteCount;
  447. }
  448. AssertByteCountStream.prototype._transform = function(chunk, encoding, cb) {
  449. this.actualByteCount += chunk.length;
  450. if (this.actualByteCount > this.expectedByteCount) {
  451. var msg = "too many bytes in the stream. expected " + this.expectedByteCount + ". got at least " + this.actualByteCount;
  452. return cb(new Error(msg));
  453. }
  454. cb(null, chunk);
  455. };
  456. AssertByteCountStream.prototype._flush = function(cb) {
  457. if (this.actualByteCount < this.expectedByteCount) {
  458. var msg = "not enough bytes in the stream. expected " + this.expectedByteCount + ". got only " + this.actualByteCount;
  459. return cb(new Error(msg));
  460. }
  461. cb();
  462. };
  463. util.inherits(RandomAccessReader, EventEmitter);
  464. function RandomAccessReader() {
  465. EventEmitter.call(this);
  466. this.refCount = 0;
  467. }
  468. RandomAccessReader.prototype.ref = function() {
  469. this.refCount += 1;
  470. };
  471. RandomAccessReader.prototype.unref = function() {
  472. var self = this;
  473. self.refCount -= 1;
  474. if (self.refCount > 0) return;
  475. if (self.refCount < 0) throw new Error("invalid unref");
  476. self.close(onCloseDone);
  477. function onCloseDone(err) {
  478. if (err) return self.emit('error', err);
  479. self.emit('close');
  480. }
  481. };
  482. RandomAccessReader.prototype.createReadStream = function(options) {
  483. var start = options.start;
  484. var end = options.end;
  485. if (start === end) {
  486. var emptyStream = new PassThrough();
  487. setImmediate(function() {
  488. emptyStream.end();
  489. });
  490. return emptyStream;
  491. }
  492. var stream = this._readStreamForRange(start, end);
  493. var destroyed = false;
  494. var refUnrefFilter = new RefUnrefFilter(this);
  495. stream.on("error", function(err) {
  496. setImmediate(function() {
  497. if (!destroyed) refUnrefFilter.emit("error", err);
  498. });
  499. });
  500. refUnrefFilter.destroy = function() {
  501. stream.unpipe(refUnrefFilter);
  502. refUnrefFilter.unref();
  503. stream.destroy();
  504. };
  505. var byteCounter = new AssertByteCountStream(end - start);
  506. refUnrefFilter.on("error", function(err) {
  507. setImmediate(function() {
  508. if (!destroyed) byteCounter.emit("error", err);
  509. });
  510. });
  511. byteCounter.destroy = function() {
  512. destroyed = true;
  513. refUnrefFilter.unpipe(byteCounter);
  514. refUnrefFilter.destroy();
  515. };
  516. return stream.pipe(refUnrefFilter).pipe(byteCounter);
  517. };
  518. RandomAccessReader.prototype._readStreamForRange = function(start, end) {
  519. throw new Error("not implemented");
  520. };
  521. RandomAccessReader.prototype.read = function(buffer, offset, length, position, callback) {
  522. var readStream = this.createReadStream({start: position, end: position + length});
  523. var writeStream = new Writable();
  524. var written = 0;
  525. writeStream._write = function(chunk, encoding, cb) {
  526. chunk.copy(buffer, offset + written, 0, chunk.length);
  527. written += chunk.length;
  528. cb();
  529. };
  530. writeStream.on("finish", callback);
  531. readStream.on("error", function(error) {
  532. callback(error);
  533. });
  534. readStream.pipe(writeStream);
  535. };
  536. RandomAccessReader.prototype.close = function(callback) {
  537. setImmediate(callback);
  538. };
  539. util.inherits(RefUnrefFilter, PassThrough);
  540. function RefUnrefFilter(context) {
  541. PassThrough.call(this);
  542. this.context = context;
  543. this.context.ref();
  544. this.unreffedYet = false;
  545. }
  546. RefUnrefFilter.prototype._flush = function(cb) {
  547. this.unref();
  548. cb();
  549. };
  550. RefUnrefFilter.prototype.unref = function(cb) {
  551. if (this.unreffedYet) return;
  552. this.unreffedYet = true;
  553. this.context.unref();
  554. };
  555. var cp437 = '\u0000☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ';
  556. function bufferToString(buffer, start, end, isUtf8) {
  557. if (isUtf8) {
  558. return buffer.toString("utf8", start, end);
  559. } else {
  560. var result = "";
  561. for (var i = start; i < end; i++) {
  562. result += cp437[buffer[i]];
  563. }
  564. return result;
  565. }
  566. }
  567. function readUInt64LE(buffer, offset) {
  568. // there is no native function for this, because we can't actually store 64-bit integers precisely.
  569. // after 53 bits, JavaScript's Number type (IEEE 754 double) can't store individual integers anymore.
  570. // but since 53 bits is a whole lot more than 32 bits, we do our best anyway.
  571. var lower32 = buffer.readUInt32LE(offset);
  572. var upper32 = buffer.readUInt32LE(offset + 4);
  573. // we can't use bitshifting here, because JavaScript bitshifting only works on 32-bit integers.
  574. return upper32 * 0x100000000 + lower32;
  575. // as long as we're bounds checking the result of this function against the total file size,
  576. // we'll catch any overflow errors, because we already made sure the total file size was within reason.
  577. }
  578. function defaultCallback(err) {
  579. if (err) throw err;
  580. }