Commit 4a04585f authored by Julien Fontanet's avatar Julien Fontanet Committed by GitHub

chore(createReadStream): rewrite in ES5 (#25)

parent bce42048
var Readable = require('stream').Readable;
var BigInt = require('../tools/bigint');
var request = require('../tools/smb2-forge').request;
// Where does it come from?!
var maxPacketSize = 0x00010000;
module.exports = function createReadStream(path, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
} else if (options == null) {
options = {}
}
var connection = this;
request('open', { path: path }, connection, function(err, file) {
if (err != null) {
return cb(err);
}
var offset = new BigInt(file.EndofFile.length, options.start || 0);
var fileSize = BigInt.fromBuffer(file.EndofFile);
var end = options.end
if (end != null) {
end = new BigInt(8, end + 1)
if (end.gt(fileSize)) {
fileSize = end
}
}
var close = request.bind(undefined, 'close', file, connection);
var stream = new Readable();
stream._destroy = function(err, cb) {
close(function(err2) {
if (err != null) {
return cb(err2);
}
cb(err);
});
};
var running = false;
stream._read = function(size) {
if (running) {
return;
}
if (offset.ge(fileSize)) {
return close(function () {
this.push(null)
})
}
running = true;
request(
'read',
{
FileId: file.FileId,
Length: Math.min(maxPacketSize, size),
Offset: offset.toBuffer(),
},
connection,
function(err, content) {
try {
if (err != null) {
return process.nextTick(stream.emit.bind(stream, 'error', err));
}
offset = offset.add(content.length);
stream.push(content);
} finally {
running = false;
}
}
);
};
cb(null, stream);
});
};
import Bigint from '../tools/bigint';
import Bluebird from 'bluebird';
import { Readable } from 'stream';
import { request } from '../tools/smb2-forge';
const requestAsync = Bluebird.promisify(request);
const maxPacketSize = 0x00010000;
class SmbReadableStream extends Readable {
constructor(connection, file, options = {}) {
super(options);
const { start = 0, end, encoding } = options;
this.connection = connection;
this.encoding = encoding;
this.file = file;
this.offset = new Bigint(8, start);
let fileLength = 0;
for (let i = 0; i < file.EndofFile.length; i++) {
fileLength += file.EndofFile[i] * Math.pow(2, i * 8);
}
this.fileLength = fileLength;
this.wait = false;
if (end >= 0 && end < fileLength) {
this.fileLength = end + 1;
}
}
async _read(size) {
while (this.offset.lt(this.fileLength) /* && size > 0*/) {
if (this.wait) {
return;
}
const rest = this.offset.sub(this.fileLength).neg();
const packetSize = Math.min(maxPacketSize, rest.toNumber() /*, size*/);
const offset = new Bigint(this.offset);
this.wait = true;
let content = await requestAsync(
'read',
{
FileId: this.file.FileId,
Length: packetSize,
Offset: offset.toBuffer(),
},
this.connection
);
this.wait = false;
if (this.encoding) {
content = content.toString(this.encoding);
}
this.offset = this.offset.add(packetSize);
// size -= packetSize
if (!this.push(content)) {
return;
}
}
if (this.offset.ge(this.fileLength)) {
this.push(null);
await requestAsync('close', this.file, this.connection);
}
}
}
export default function(path, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
request('open', { path }, this, (err, file) => {
if (err) {
if (err.code === 'STATUS_OBJECT_NAME_NOT_FOUND') {
err.code = 'ENOENT';
}
cb(err);
} else {
cb(null, new SmbReadableStream(this, file, options));
}
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment