UNPKG

tus-js-client-olalonde

Version:

A pure JavaScript client for the tus resumable upload protocol

123 lines (100 loc) 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); exports.getSource = getSource; var _stream = require("stream"); var _fs = require("fs"); var _commonStreams = require("common-streams"); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var BufferSource = function () { function BufferSource(buffer) { _classCallCheck(this, BufferSource); this._buffer = buffer; this.size = buffer.length; } _createClass(BufferSource, [{ key: "slice", value: function slice(start, end) { var buf = this._buffer.slice(start, end); buf.size = buf.length; return buf; } }, { key: "close", value: function close() {} }]); return BufferSource; }(); var FileSource = function () { function FileSource(stream) { _classCallCheck(this, FileSource); this._stream = stream; this._path = stream.path.toString(); } _createClass(FileSource, [{ key: "slice", value: function slice(start, end) { var stream = (0, _fs.createReadStream)(this._path, { start: start, end: end, autoClose: true }); stream.size = end - start; return stream; } }, { key: "close", value: function close() { this._stream.destroy(); } }]); return FileSource; }(); var StreamSource = function () { function StreamSource(stream) { _classCallCheck(this, StreamSource); // "block" stream this._stream = stream.pipe(new _stream.PassThrough()); // Setting the size to null indicates that we have no calculation available // for how much data this stream will emit requiring the user to specify // it manually (see the `uploadSize` option). this.size = null; } _createClass(StreamSource, [{ key: "slice", value: function slice(start, end) { var slicingStream = this._stream.pipe(new _commonStreams.DiscardStream(start)).pipe(new _commonStreams.MeterStream(end)).on("error", function (err) { if (err instanceof _commonStreams.MeterStream.OverflowError) { slicingStream.end(); } }) // dont make meterstream error visible to outside... .pipe(new _stream.PassThrough()); slicingStream.size = end - start; return slicingStream; } }, { key: "close", value: function close() { // https://github.com/mafintosh/pump/blob/master/index.js#L39 ? //this._stream. } }]); return StreamSource; }(); function getSource(input) { if (Buffer.isBuffer(input)) { return new BufferSource(input); } if (input instanceof _fs.ReadStream && input.path != null) { return new FileSource(input); } // instanceof Readable will not work with some streams // maybe use https://github.com/sindresorhus/is-stream#isstreamreadablestream ? if (typeof input.pipe === "function") { return new StreamSource(input); } throw new Error("source object may only be an instance of Buffer or Readable in this environment"); }