Skip to content

Commit

Permalink
use Buffer.allocUnsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoshwolfe committed Nov 12, 2018
1 parent a6eb2c8 commit 6ee7c2e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ zipfile.outputStream.pipe(fs.createWriteStream("output.zip")).on("close", functi
});
// alternate apis for adding files:
zipfile.addReadStream(process.stdin, "stdin.txt");
zipfile.addBuffer(new Buffer("hello"), "hello.txt");
zipfile.addBuffer(Buffer.from("hello"), "hello.txt");
// call end() after all the files have been added
zipfile.end();
```
Expand Down
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ function getEndOfCentralDirectoryRecord(self, actuallyJustTellMeHowLongItWouldBe
}
}

var eocdrBuffer = Buffer.alloc(END_OF_CENTRAL_DIRECTORY_RECORD_SIZE);
var eocdrBuffer = Buffer.allocUnsafe(END_OF_CENTRAL_DIRECTORY_RECORD_SIZE);
// end of central dir signature 4 bytes (0x06054b50)
eocdrBuffer.writeUInt32LE(0x06054b50, 0);
// number of this disk 2 bytes
Expand All @@ -301,7 +301,7 @@ function getEndOfCentralDirectoryRecord(self, actuallyJustTellMeHowLongItWouldBe

// ZIP64 format
// ZIP64 End of Central Directory Record
var zip64EocdrBuffer = Buffer.alloc(ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_SIZE);
var zip64EocdrBuffer = Buffer.allocUnsafe(ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_SIZE);
// zip64 end of central dir signature 4 bytes (0x06064b50)
zip64EocdrBuffer.writeUInt32LE(0x06064b50, 0);
// size of zip64 end of central directory record 8 bytes
Expand All @@ -327,7 +327,7 @@ function getEndOfCentralDirectoryRecord(self, actuallyJustTellMeHowLongItWouldBe


// ZIP64 End of Central Directory Locator
var zip64EocdlBuffer = Buffer.alloc(ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIZE);
var zip64EocdlBuffer = Buffer.allocUnsafe(ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIZE);
// zip64 end of central dir locator signature 4 bytes (0x07064b50)
zip64EocdlBuffer.writeUInt32LE(0x07064b50, 0);
// number of the disk with the start of the zip64 end of central directory 4 bytes
Expand Down Expand Up @@ -437,7 +437,7 @@ Entry.prototype.getLocalFileHeader = function() {
uncompressedSize = this.uncompressedSize;
}

var fixedSizeStuff = Buffer.alloc(LOCAL_FILE_HEADER_FIXED_SIZE);
var fixedSizeStuff = Buffer.allocUnsafe(LOCAL_FILE_HEADER_FIXED_SIZE);
var generalPurposeBitFlag = FILE_NAME_IS_UTF8;
if (!this.crcAndFileSizeKnown) generalPurposeBitFlag |= UNKNOWN_CRC32_AND_FILE_SIZES;

Expand Down Expand Up @@ -476,10 +476,10 @@ var ZIP64_DATA_DESCRIPTOR_SIZE = 24;
Entry.prototype.getDataDescriptor = function() {
if (this.crcAndFileSizeKnown) {
// the Mac Archive Utility requires this not be present unless we set general purpose bit 3
return Buffer.alloc(0);
return Buffer.allocUnsafe(0);
}
if (!this.useZip64Format()) {
var buffer = Buffer.alloc(DATA_DESCRIPTOR_SIZE);
var buffer = Buffer.allocUnsafe(DATA_DESCRIPTOR_SIZE);
// optional signature (required according to Archive Utility)
buffer.writeUInt32LE(0x08074b50, 0);
// crc-32 4 bytes
Expand All @@ -491,7 +491,7 @@ Entry.prototype.getDataDescriptor = function() {
return buffer;
} else {
// ZIP64 format
var buffer = Buffer.alloc(ZIP64_DATA_DESCRIPTOR_SIZE);
var buffer = Buffer.allocUnsafe(ZIP64_DATA_DESCRIPTOR_SIZE);
// optional signature (unknown if anyone cares about this)
buffer.writeUInt32LE(0x08074b50, 0);
// crc-32 4 bytes
Expand All @@ -506,7 +506,7 @@ Entry.prototype.getDataDescriptor = function() {
var CENTRAL_DIRECTORY_RECORD_FIXED_SIZE = 46;
var ZIP64_EXTENDED_INFORMATION_EXTRA_FIELD_SIZE = 28;
Entry.prototype.getCentralDirectoryRecord = function() {
var fixedSizeStuff = Buffer.alloc(CENTRAL_DIRECTORY_RECORD_FIXED_SIZE);
var fixedSizeStuff = Buffer.allocUnsafe(CENTRAL_DIRECTORY_RECORD_FIXED_SIZE);
var generalPurposeBitFlag = FILE_NAME_IS_UTF8;
if (!this.crcAndFileSizeKnown) generalPurposeBitFlag |= UNKNOWN_CRC32_AND_FILE_SIZES;

Expand All @@ -522,7 +522,7 @@ Entry.prototype.getCentralDirectoryRecord = function() {
versionNeededToExtract = VERSION_NEEDED_TO_EXTRACT_ZIP64;

// ZIP64 extended information extra field
zeiefBuffer = Buffer.alloc(ZIP64_EXTENDED_INFORMATION_EXTRA_FIELD_SIZE);
zeiefBuffer = Buffer.allocUnsafe(ZIP64_EXTENDED_INFORMATION_EXTRA_FIELD_SIZE);
// 0x0001 2 bytes Tag for this "extra" block type
zeiefBuffer.writeUInt16LE(0x0001, 0);
// Size 2 bytes Size of this "extra" block
Expand All @@ -537,7 +537,7 @@ Entry.prototype.getCentralDirectoryRecord = function() {
// (omit)
} else {
versionNeededToExtract = VERSION_NEEDED_TO_EXTRACT_UTF8;
zeiefBuffer = Buffer.alloc(0);
zeiefBuffer = Buffer.allocUnsafe(0);
}

// central file header signature 4 bytes (0x02014b50)
Expand Down

0 comments on commit 6ee7c2e

Please sign in to comment.