/** * Return a readable stream for file. * * `fileStream.path` is the specified file ID i.e. `fileId`. * `fileStream` emits event `info` passing a single argument i.e. * `info` with the interface `{ uri }` where `uri` is the URI of the * file on Telegram servers. * * This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method, * which returns the full URI to the file on remote server. * * @param {String} fileId File identifier to get info about * @param {Object} [options] Additional Telegram query options * @return {stream.Readable} fileStream */ getFileStream(fileId, form = {}) { const fileStream = new stream.PassThrough(); fileStream.path = fileId; this.getFileLink(fileId, form) .then((fileURI) => { fileStream.emit('info', { uri: fileURI, }); pump(streamedRequest(Object.assign({ uri: fileURI }, this.options.request)), fileStream); }) .catch((error) => { fileStream.emit('error', error); }); return fileStream; }
function createReadStream(filePath, options) { const Stream = require("stream"); const PassThroughStream = Stream.PassThrough; const outStream = new PassThroughStream(); getFileStream(filePath, options) .then(stream => { stream.pipe(outStream); }) .catch(err => { outStream.emit("error", err); }); return outStream; }
it('should make a request with given options', function() { let response = new PassThrough(); let requestGetSpy = sandbox.stub(request, 'get').callsFake(function() { // deffer error emmition Promise.resolve().then(function() { response.emit('end'); }); return response; }); downloader.download(options3, 'jdk.zip').then(()=> { expect(requestGetSpy).to.be.calledOnce; expect(requestGetSpy).to.be.calledWith(options3); }); });
test('error', function (t) { t.plan(2) var s = new stream.PassThrough() concat(s, function (err, buf) { t.ok(err, 'got expected error') t.ok(!buf) }) s.write('abc') setTimeout(function () { s.write('123') }, 10) setTimeout(function () { s.write('456') }, 20) setTimeout(function () { s.emit('error', new Error('error')) }, 30) })
function readStream (cache, integrity, opts) { opts = ReadOpts(opts) const stream = new PassThrough() withContentSri(cache, integrity, (cpath, sri) => { return lstatAsync(cpath).then(stat => ({ cpath, sri, stat })) }).then(({ cpath, sri, stat }) => { return pipe( fs.createReadStream(cpath), ssri.integrityStream({ integrity: sri, size: opts.size }), stream ) }).catch(err => { stream.emit('error', err) }) return stream }
it('should call endHandler when end event is emitted', function() { let response = new Readable(); let stream = new PassThrough(); response._read = function() {}; sandbox.stub(request, 'get').callsFake(function() { // deffer error emmition Promise.resolve().then(function() { response.emit('end'); stream.emit('close'); }); return response; }); sandbox.stub(fs, 'createWriteStream').returns(stream); let endHandler = sandbox.stub(downloader, 'endHandler'); let d = downloader.download(options, 'jdk.zip'); return d.then(()=> { expect(endHandler).to.be.calledOnce; expect(endHandler).to.be.calledWith(stream); }); });
passThrough.emit('error', err); passThrough.end(); };
function createWriteStream(filePath, options, callback = NOOP) { const Stream = require("stream"); const PassThroughStream = Stream.PassThrough; const writeStream = new PassThroughStream(); const headers = {}; if (options.overwrite === false) { headers["If-None-Match"] = "*"; } const requestOptions = { url: joinURL(options.remoteURL, encodePath(filePath)), method: "PUT", headers, data: writeStream }; prepareRequestOptions(requestOptions, options); request(requestOptions) .then(response => { // Fire callback asynchronously to avoid errors setTimeout(callback, 0); return response; }) .then(responseHandlers.handleResponseCode) .catch(err => { writeStream.emit("error", err); }); return writeStream; }
it('should call errorHandler when error event is emitted and skip sucessHandler', function() { let response = new PassThrough(); let error = new Error('something bad happened'); sandbox.stub(request, 'get').callsFake(function() { // deffer error emmition Promise.resolve().then(function() { response.emit('error', error); }); return response; }); let errorHandler = sandbox.stub(downloader, 'errorHandler'); let successHandler = sandbox.stub(downloader, 'successHandler'); let p = downloader.download(options, 'jdk.zip'); return p.then(()=>{ expect(errorHandler).to.be.calledOnce; expect(successHandler).to.have.not.been.called; }).catch((err)=>{ expect.fail(); return Promise.rejects(err); }); });
it('should make a request with \'User-Agent\' header set', function() { let response = new PassThrough(); let requestGetSpy = sandbox.stub(request, 'get').callsFake(function() { // deffer error emmition Promise.resolve().then(function() { response.emit('end'); }); return response; }); let d = downloader.download(options, 'jdk.zip'); d.then(()=> { expect(requestGetSpy).to.be.calledOnce; expect(requestGetSpy.args[0][0].hasOwnProperty('headers')).to.be.true; expect(requestGetSpy.args[0][0].headers.hasOwnProperty('User-Agent')).to.be.true; }); });
it('should make a request with given url in options', function() { let response = new PassThrough(); let requestGetSpy = sandbox.stub(request, 'get').callsFake(function() { // deffer error emmition Promise.resolve().then(function() { response.emit('end'); }); return response; }); downloader.download(options, 'jdk.zip').then(()=> { expect(requestGetSpy).to.be.calledOnce; expect(requestGetSpy.args[0][0].hasOwnProperty('url')).to.be.true; expect(requestGetSpy.args[0][0].url).to.be.equal(options); }); });