forked from EvanOxfeld/node-unzip
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
378 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"checkJs": true, | ||
"target": "ES2022", | ||
"moduleResolution":"node", | ||
"types": ["node"] | ||
}, | ||
"exclude": [ | ||
"node_modules", | ||
"test", | ||
"coverage" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,137 @@ | ||
const Stream = require('stream'); | ||
const util = require('util'); | ||
const strFunction = 'function'; | ||
|
||
function PullStream() { | ||
if (!(this instanceof PullStream)) | ||
return new PullStream(); | ||
|
||
Stream.Duplex.call(this, {decodeStrings:false, objectMode:true}); | ||
this.buffer = Buffer.from(''); | ||
const self = this; | ||
self.on('finish', function() { | ||
self.finished = true; | ||
self.emit('chunk', false); | ||
}); | ||
} | ||
|
||
util.inherits(PullStream, Stream.Duplex); | ||
class PullStream extends Stream.Duplex { | ||
finished; | ||
match; | ||
__emittedError; | ||
constructor(opts) { | ||
super({decodeStrings:false, objectMode:true}); | ||
this._opts = opts; | ||
this.buffer = Buffer.from(''); | ||
this.on('finish', () => { | ||
this.finished = true; | ||
this.emit('chunk', false); | ||
}); | ||
} | ||
|
||
PullStream.prototype._write = function(chunk, e, cb) { | ||
this.buffer = Buffer.concat([this.buffer, chunk]); | ||
this.cb = cb; | ||
this.emit('chunk'); | ||
}; | ||
_write(chunk, e, cb) { | ||
this.buffer = Buffer.concat([this.buffer, chunk]); | ||
this.cb = cb; | ||
this.emit('chunk'); | ||
}; | ||
|
||
|
||
// The `eof` parameter is interpreted as `file_length` if the type is number | ||
// otherwise (i.e. buffer) it is interpreted as a pattern signaling end of stream | ||
PullStream.prototype.stream = function(eof, includeEof) { | ||
const p = Stream.PassThrough(); | ||
let done; | ||
const self= this; | ||
// The `eof` parameter is interpreted as `file_length` if the type is number | ||
// otherwise (i.e. buffer) it is interpreted as a pattern signaling end of stream | ||
stream(eof, includeEof) { | ||
const p = new Stream.PassThrough(); | ||
let done; | ||
const self= this; | ||
|
||
function cb() { | ||
if (typeof self.cb === strFunction) { | ||
const callback = self.cb; | ||
self.cb = undefined; | ||
return callback(); | ||
function cb() { | ||
if (typeof self.cb === strFunction) { | ||
const callback = self.cb; | ||
self.cb = undefined; | ||
return callback(); | ||
} | ||
} | ||
} | ||
|
||
function pull() { | ||
let packet; | ||
if (self.buffer && self.buffer.length) { | ||
if (typeof eof === 'number') { | ||
packet = self.buffer.slice(0, eof); | ||
self.buffer = self.buffer.slice(eof); | ||
eof -= packet.length; | ||
done = done || !eof; | ||
} else { | ||
let match = self.buffer.indexOf(eof); | ||
if (match !== -1) { | ||
function pull() { | ||
let packet; | ||
if (self.buffer && self.buffer.length) { | ||
if (typeof eof === 'number') { | ||
packet = self.buffer.slice(0, eof); | ||
self.buffer = self.buffer.slice(eof); | ||
eof -= packet.length; | ||
done = done || !eof; | ||
} else { | ||
let match = self.buffer.indexOf(eof); | ||
if (match !== -1) { | ||
// store signature match byte offset to allow us to reference | ||
// this for zip64 offset | ||
self.match = match; | ||
if (includeEof) match = match + eof.length; | ||
packet = self.buffer.slice(0, match); | ||
self.buffer = self.buffer.slice(match); | ||
done = true; | ||
} else { | ||
const len = self.buffer.length - eof.length; | ||
if (len <= 0) { | ||
cb(); | ||
self.match = match; | ||
if (includeEof) match = match + eof.length; | ||
packet = self.buffer.slice(0, match); | ||
self.buffer = self.buffer.slice(match); | ||
done = true; | ||
} else { | ||
packet = self.buffer.slice(0, len); | ||
self.buffer = self.buffer.slice(len); | ||
const len = self.buffer.length - eof.length; | ||
if (len <= 0) { | ||
cb(); | ||
} else { | ||
packet = self.buffer.slice(0, len); | ||
self.buffer = self.buffer.slice(len); | ||
} | ||
} | ||
} | ||
if (packet) p.write(packet, function() { | ||
if (self.buffer.length === 0 || (eof.length && self.buffer.length <= eof.length)) cb(); | ||
}); | ||
} | ||
if (packet) p.write(packet, function() { | ||
if (self.buffer.length === 0 || (eof.length && self.buffer.length <= eof.length)) cb(); | ||
}); | ||
} | ||
|
||
if (!done) { | ||
if (self.finished) { | ||
if (!done) { | ||
if (self.finished) { | ||
self.removeListener('chunk', pull); | ||
self.emit('error', new Error('FILE_ENDED')); | ||
return; | ||
} | ||
|
||
} else { | ||
self.removeListener('chunk', pull); | ||
self.emit('error', new Error('FILE_ENDED')); | ||
return; | ||
p.end(); | ||
} | ||
|
||
} else { | ||
self.removeListener('chunk', pull); | ||
p.end(); | ||
} | ||
} | ||
|
||
self.on('chunk', pull); | ||
pull(); | ||
return p; | ||
}; | ||
|
||
PullStream.prototype.pull = function(eof, includeEof) { | ||
if (eof === 0) return Promise.resolve(''); | ||
self.on('chunk', pull); | ||
pull(); | ||
return p; | ||
}; | ||
|
||
// If we already have the required data in buffer | ||
// we can resolve the request immediately | ||
if (!isNaN(eof) && this.buffer.length > eof) { | ||
const data = this.buffer.slice(0, eof); | ||
this.buffer = this.buffer.slice(eof); | ||
return Promise.resolve(data); | ||
} | ||
pull(eof, includeEof) { | ||
if (eof === 0) return Promise.resolve(''); | ||
|
||
// Otherwise we stream until we have it | ||
let buffer = Buffer.from(''); | ||
const self = this; | ||
// If we already have the required data in buffer | ||
// we can resolve the request immediately | ||
if (!isNaN(eof) && this.buffer.length > eof) { | ||
const data = this.buffer.slice(0, eof); | ||
this.buffer = this.buffer.slice(eof); | ||
return Promise.resolve(data); | ||
} | ||
|
||
const concatStream = new Stream.Transform(); | ||
concatStream._transform = function(d, e, cb) { | ||
buffer = Buffer.concat([buffer, d]); | ||
cb(); | ||
}; | ||
// Otherwise we stream until we have it | ||
let buffer = Buffer.from(''); | ||
const self = this; | ||
|
||
let rejectHandler; | ||
let pullStreamRejectHandler; | ||
return new Promise(function(resolve, reject) { | ||
rejectHandler = reject; | ||
pullStreamRejectHandler = function(e) { | ||
self.__emittedError = e; | ||
reject(e); | ||
const concatStream = new Stream.Transform(); | ||
concatStream._transform = function(d, e, cb) { | ||
buffer = Buffer.concat([buffer, d]); | ||
cb(); | ||
}; | ||
if (self.finished) | ||
return reject(new Error('FILE_ENDED')); | ||
self.once('error', pullStreamRejectHandler); // reject any errors from pullstream itself | ||
self.stream(eof, includeEof) | ||
.on('error', reject) | ||
.pipe(concatStream) | ||
.on('finish', function() {resolve(buffer);}) | ||
.on('error', reject); | ||
}) | ||
.finally(function() { | ||
self.removeListener('error', rejectHandler); | ||
self.removeListener('error', pullStreamRejectHandler); | ||
}); | ||
}; | ||
|
||
PullStream.prototype._read = function(){}; | ||
let rejectHandler; | ||
let pullStreamRejectHandler; | ||
return new Promise(function(resolve, reject) { | ||
rejectHandler = reject; | ||
pullStreamRejectHandler = function(e) { | ||
self.__emittedError = e; | ||
reject(e); | ||
}; | ||
if (self.finished) | ||
return reject(new Error('FILE_ENDED')); | ||
self.once('error', pullStreamRejectHandler); // reject any errors from pullstream itself | ||
self.stream(eof, includeEof) | ||
.on('error', reject) | ||
.pipe(concatStream) | ||
.on('finish', function() {resolve(buffer);}) | ||
.on('error', reject); | ||
}) | ||
.finally(function() { | ||
self.removeListener('error', rejectHandler); | ||
self.removeListener('error', pullStreamRejectHandler); | ||
}); | ||
}; | ||
_read(){}; | ||
} | ||
|
||
module.exports = PullStream; |
Oops, something went wrong.