Comparing version 1.1.1 to 1.1.2
@@ -6,3 +6,3 @@ { | ||
"keywords": ["tar", "untar", "asynchronous", "stream", "async", "chunk", "chunked"], | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
138
tar.js
@@ -0,1 +1,2 @@ | ||
/*jshint strict:true node:true es5:true onevar:true laxcomma:true laxbreak:true*/ | ||
(function () { | ||
@@ -6,3 +7,4 @@ "use strict"; | ||
Stream = require('stream').Stream, | ||
header = require("./header"), | ||
EventEmitter = require('events').EventEmitter, | ||
Header = require("./header"), | ||
utils = require("./utils"), | ||
@@ -46,2 +48,11 @@ recordSize = 512, | ||
function updateChecksum(key) { | ||
/*jshint validthis:true*/ | ||
var i, value = this.data[key], length; | ||
for (i = 0, length = value.length; i < length; i += 1) { | ||
this.checksum += value.charCodeAt(i); | ||
} | ||
} | ||
Tar.prototype.append = function (filepath, input, opts, callback) { | ||
@@ -56,5 +67,45 @@ var data, | ||
tape = this, | ||
extraBytes, | ||
treatAsBuffer, | ||
inputStream, | ||
realInput, | ||
headerContainer, | ||
headerBuf; | ||
function emitFakeStream() { | ||
inputStream.emit('data', realInput); | ||
inputStream.emit('end', realInput); | ||
} | ||
function addChunkToTar(chunk) { | ||
tape.emit('data', chunk); | ||
tape.written += chunk.length; | ||
} | ||
function checkQueue() { | ||
tape.processing = false; | ||
if (typeof callback === 'function') { | ||
callback(); | ||
} | ||
if (!queue.length) { | ||
return; | ||
} | ||
var elem = queue.splice(0, 1)[0]; | ||
tape.append(elem.filepath, elem.input, elem.opts, elem.cb); | ||
} | ||
function padTarAndEnd() { | ||
var extraBytes = recordSize - (size % recordSize || recordSize) | ||
; | ||
addChunkToTar(utils.clean(extraBytes)); | ||
process.nextTick(checkQueue); | ||
} | ||
// callback mangling | ||
if (typeof opts === 'function') { | ||
@@ -65,3 +116,10 @@ callback = opts; | ||
// If we're in the middle of something, wait until we | ||
// finish to queue up the next | ||
if (this.processing || queue.length) { | ||
if ('function' === typeof input.pause) { | ||
input.pause(); | ||
} | ||
// TODO check for drain before resuming | ||
queue.push({ | ||
@@ -73,2 +131,3 @@ filepath: filepath, | ||
}); | ||
return; | ||
@@ -80,3 +139,3 @@ } | ||
mode = opts.mode || parseInt('777', 8) & 0xfff; | ||
mtime = opts.mtime || parseInt(+new Date() / 1000); | ||
mtime = opts.mtime || parseInt(Date.now() / 1000, 10); | ||
uid = opts.uid || 0; | ||
@@ -105,56 +164,43 @@ gid = opts.gid || 0; | ||
// calculate the checksum | ||
checksum = 0; | ||
Object.keys(data).forEach(function (key) { | ||
var i, value = data[key], length; | ||
headerContainer = { data: data, checksum: 0 }; | ||
Object.keys(data).forEach(updateChecksum, headerContainer); | ||
checksum = headerContainer.checksum; | ||
for (i = 0, length = value.length; i < length; i += 1) { | ||
checksum += value.charCodeAt(i); | ||
} | ||
}); | ||
data.checksum = utils.pad(checksum, 6) + "\u0000 "; | ||
headerBuf = header.format(data); | ||
this.emit('data', header.format(data)); | ||
headerBuf = Header.format(data); | ||
this.emit('data', headerBuf); | ||
this.written += headerBuf.length; | ||
if (typeof input === 'string') { | ||
this.emit('data', input); | ||
this.written += input.length; | ||
// a step towards making this browser-usable code | ||
if ('undefined' !== typeof Buffer) { | ||
if (input instanceof Buffer) { | ||
treatAsBuffer = true; | ||
} | ||
if ('string' === typeof input) { | ||
// get correct number of bytes with unicode chars | ||
input = new Buffer(input); | ||
} | ||
} | ||
extraBytes = recordSize - (size % recordSize || recordSize); | ||
this.emit('data', utils.clean(recordSize - (size % recordSize))); | ||
this.written += extraBytes; | ||
// TODO get correct number of bytes for unicode characters | ||
// when the environment isn't NodeJS (no Buffer) | ||
if (typeof input === 'string' || treatAsBuffer) { | ||
inputStream = new EventEmitter(); | ||
if (typeof callback === 'function') { | ||
callback(); | ||
} | ||
// emitFakeStream uses realInput | ||
realInput = input; | ||
process.nextTick(emitFakeStream); | ||
} else { | ||
this.processing = true; | ||
inputStream = input; | ||
} | ||
input.on('data', function (chunk) { | ||
tape.emit('data', chunk); | ||
tape.written += chunk.length; | ||
}); | ||
tape.processing = true; | ||
input.on('end', function () { | ||
extraBytes = recordSize - (size % recordSize || recordSize); | ||
tape.emit('data', utils.clean(extraBytes)); | ||
tape.written += extraBytes; | ||
if ('function' === typeof inputStream.resume) { | ||
inputStream.resume(); | ||
} | ||
if (queue.length) { | ||
setTimeout(function () { | ||
var elem = queue.splice(0, 1)[0]; | ||
tape.append(elem.filepath, elem.input, elem.opts, elem.cb); | ||
}, 0); | ||
tape.processing = false; | ||
} | ||
if (typeof callback === 'function') { | ||
callback(); | ||
} | ||
}); | ||
} | ||
inputStream.on('data', addChunkToTar); | ||
inputStream.on('end', padTarAndEnd); | ||
}; | ||
@@ -161,0 +207,0 @@ |
12
utils.js
@@ -0,1 +1,2 @@ | ||
/*jshint strict:true node:true es5:true onevar:true laxcomma:true laxbreak:true*/ | ||
(function () { | ||
@@ -5,7 +6,8 @@ "use strict"; | ||
function clean(length) { | ||
var i, buffer = new Buffer(length); | ||
for (i = 0; i < length; i += 1) { | ||
buffer[i] = 0; | ||
} | ||
return buffer; | ||
var buf = new Buffer(length) | ||
; | ||
buf.fill(0); | ||
return buf; | ||
} | ||
@@ -12,0 +14,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
13073
495
0
1