UNPKG

4.02 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.VirtualBuffer = undefined;
7
8var _regenerator = require('babel-runtime/regenerator');
9
10var _regenerator2 = _interopRequireDefault(_regenerator);
11
12var _promise = require('babel-runtime/core-js/promise');
13
14var _promise2 = _interopRequireDefault(_promise);
15
16var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
17
18var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
19
20var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
21
22var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
23
24var _createClass2 = require('babel-runtime/helpers/createClass');
25
26var _createClass3 = _interopRequireDefault(_createClass2);
27
28var _pipette = require('pipette');
29
30function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
32var chunkSize = 1024 * 1024;
33
34var VirtualBuffer = exports.VirtualBuffer = function () {
35 function VirtualBuffer(readStream) {
36 (0, _classCallCheck3.default)(this, VirtualBuffer);
37
38 this.slicer = new _pipette.Slicer(readStream);
39 this.position = 0;
40 this.promise = null;
41 }
42
43 (0, _createClass3.default)(VirtualBuffer, [{
44 key: 'readChunk',
45
46
47 // length = -1 means 'until the end'
48 value: function () {
49 var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(length, label) {
50 var _this2 = this;
51
52 var _this, chunks, error, res;
53
54 return _regenerator2.default.wrap(function _callee$(_context) {
55 while (1) {
56 switch (_context.prev = _context.next) {
57 case 0:
58 _this = this;
59
60 if (!(this.promise !== null)) {
61 _context.next = 3;
62 break;
63 }
64
65 throw new Error('pomise already there !!!', this.promise);
66
67 case 3:
68 if (!(length === -1)) {
69 _context.next = 15;
70 break;
71 }
72
73 chunks = [];
74 error = false;
75
76 case 6:
77 _context.next = 8;
78 return new _promise2.default(function (resolve, reject) {
79 _this2.slicer.read(chunkSize, function (error, length, data, offset) {
80 if (error !== false && error !== true) {
81 reject(error);
82 } else {
83 resolve({ error: error, data: data });
84 }
85 });
86 });
87
88 case 8:
89 res = _context.sent;
90
91 error = res.error;
92 chunks.push(res.data);
93
94 case 11:
95 if (error === false) {
96 _context.next = 6;
97 break;
98 }
99
100 case 12:
101 return _context.abrupt('return', Buffer.concat(chunks));
102
103 case 15:
104 this.promise = label;
105 return _context.abrupt('return', new _promise2.default(function (resolve, reject) {
106 _this2.slicer.read(length, function (error, actualLength, data, offset) {
107 if (error !== false && error !== true) {
108 _this.promise = null;
109 reject(error);
110 } else {
111 _this.promise = null;
112 _this.position += data.length;
113 resolve(data);
114 }
115 });
116 }));
117
118 case 17:
119 case 'end':
120 return _context.stop();
121 }
122 }
123 }, _callee, this);
124 }));
125
126 function readChunk(_x, _x2) {
127 return _ref.apply(this, arguments);
128 }
129
130 return readChunk;
131 }()
132 }, {
133 key: 'isDepleted',
134 get: function get() {
135 return !this.slicer.readable;
136 }
137 }]);
138 return VirtualBuffer;
139}();
140//# sourceMappingURL=virtual-buffer.js.map
\No newline at end of file