@protobi/exceljs
Version:
Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge
458 lines (445 loc) • 21.4 kB
JavaScript
"use strict";
function _regeneratorRuntime() { "use strict"; var r = _regenerator(), e = r.m(_regeneratorRuntime), t = (Object.getPrototypeOf ? Object.getPrototypeOf(e) : e.__proto__).constructor; function n(r) { var e = "function" == typeof r && r.constructor; return !!e && (e === t || "GeneratorFunction" === (e.displayName || e.name)); } var o = { throw: 1, return: 2, break: 3, continue: 3 }; function a(r) { var e, t; return function (n) { e || (e = { stop: function stop() { return t(n.a, 2); }, catch: function _catch() { return n.v; }, abrupt: function abrupt(r, e) { return t(n.a, o[r], e); }, delegateYield: function delegateYield(r, o, a) { return e.resultName = o, t(n.d, _regeneratorValues(r), a); }, finish: function finish(r) { return t(n.f, r); } }, t = function t(r, _t, o) { n.p = e.prev, n.n = e.next; try { return r(_t, o); } finally { e.next = n.n; } }), e.resultName && (e[e.resultName] = n.v, e.resultName = void 0), e.sent = n.v, e.next = n.n; try { return r.call(this, e); } finally { n.p = e.prev, n.n = e.next; } }; } return (_regeneratorRuntime = function _regeneratorRuntime() { return { wrap: function wrap(e, t, n, o) { return r.w(a(e), t, n, o && o.reverse()); }, isGeneratorFunction: n, mark: r.m, awrap: function awrap(r, e) { return new _OverloadYield(r, e); }, AsyncIterator: _regeneratorAsyncIterator, async: function async(r, e, t, o, u) { return (n(e) ? _regeneratorAsyncGen : _regeneratorAsync)(a(r), e, t, o, u); }, keys: _regeneratorKeys, values: _regeneratorValues }; })(); }
function _regeneratorValues(e) { if (null != e) { var t = e["function" == typeof Symbol && Symbol.iterator || "@@iterator"], r = 0; if (t) return t.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) return { next: function next() { return e && r >= e.length && (e = void 0), { value: e && e[r++], done: !e }; } }; } throw new TypeError(_typeof(e) + " is not iterable"); }
function _regeneratorKeys(e) { var n = Object(e), r = []; for (var t in n) r.unshift(t); return function e() { for (; r.length;) if ((t = r.pop()) in n) return e.value = t, e.done = !1, e; return e.done = !0, e; }; }
function _regeneratorAsync(n, e, r, t, o) { var a = _regeneratorAsyncGen(n, e, r, t, o); return a.next().then(function (n) { return n.done ? n.value : a.next(); }); }
function _regeneratorAsyncGen(r, e, t, o, n) { return new _regeneratorAsyncIterator(_regenerator().w(r, e, t, o), n || Promise); }
function _regeneratorAsyncIterator(t, e) { function n(r, o, i, f) { try { var c = t[r](o), u = c.value; return u instanceof _OverloadYield ? e.resolve(u.v).then(function (t) { n("next", t, i, f); }, function (t) { n("throw", t, i, f); }) : e.resolve(u).then(function (t) { c.value = t, i(c); }, function (t) { return n("throw", t, i, f); }); } catch (t) { f(t); } } var r; this.next || (_regeneratorDefine2(_regeneratorAsyncIterator.prototype), _regeneratorDefine2(_regeneratorAsyncIterator.prototype, "function" == typeof Symbol && Symbol.asyncIterator || "@asyncIterator", function () { return this; })), _regeneratorDefine2(this, "_invoke", function (t, o, i) { function f() { return new e(function (e, r) { n(t, i, e, r); }); } return r = r ? r.then(f, f) : f(); }, !0); }
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
function _OverloadYield(e, d) { this.v = e, this.k = d; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/* eslint-disable max-classes-per-file */
var Stream = require('readable-stream');
var utils = require('./utils');
var StringBuf = require('./string-buf');
// =============================================================================
// data chunks - encapsulating incoming data
var StringChunk = /*#__PURE__*/function () {
function StringChunk(data, encoding) {
_classCallCheck(this, StringChunk);
this._data = data;
this._encoding = encoding;
}
_createClass(StringChunk, [{
key: "length",
get: function get() {
return this.toBuffer().length;
}
// copy to target buffer
}, {
key: "copy",
value: function copy(target, targetOffset, offset, length) {
return this.toBuffer().copy(target, targetOffset, offset, length);
}
}, {
key: "toBuffer",
value: function toBuffer() {
if (!this._buffer) {
this._buffer = Buffer.from(this._data, this._encoding);
}
return this._buffer;
}
}]);
return StringChunk;
}();
var StringBufChunk = /*#__PURE__*/function () {
function StringBufChunk(data) {
_classCallCheck(this, StringBufChunk);
this._data = data;
}
_createClass(StringBufChunk, [{
key: "length",
get: function get() {
return this._data.length;
}
// copy to target buffer
}, {
key: "copy",
value: function copy(target, targetOffset, offset, length) {
// eslint-disable-next-line no-underscore-dangle
return this._data._buf.copy(target, targetOffset, offset, length);
}
}, {
key: "toBuffer",
value: function toBuffer() {
return this._data.toBuffer();
}
}]);
return StringBufChunk;
}();
var BufferChunk = /*#__PURE__*/function () {
function BufferChunk(data) {
_classCallCheck(this, BufferChunk);
this._data = data;
}
_createClass(BufferChunk, [{
key: "length",
get: function get() {
return this._data.length;
}
// copy to target buffer
}, {
key: "copy",
value: function copy(target, targetOffset, offset, length) {
this._data.copy(target, targetOffset, offset, length);
}
}, {
key: "toBuffer",
value: function toBuffer() {
return this._data;
}
}]);
return BufferChunk;
}(); // =============================================================================
// ReadWriteBuf - a single buffer supporting simple read-write
var ReadWriteBuf = /*#__PURE__*/function () {
function ReadWriteBuf(size) {
_classCallCheck(this, ReadWriteBuf);
this.size = size;
// the buffer
this.buffer = Buffer.alloc(size);
// read index
this.iRead = 0;
// write index
this.iWrite = 0;
}
_createClass(ReadWriteBuf, [{
key: "toBuffer",
value: function toBuffer() {
if (this.iRead === 0 && this.iWrite === this.size) {
return this.buffer;
}
var buf = Buffer.alloc(this.iWrite - this.iRead);
this.buffer.copy(buf, 0, this.iRead, this.iWrite);
return buf;
}
}, {
key: "length",
get: function get() {
return this.iWrite - this.iRead;
}
}, {
key: "eod",
get: function get() {
return this.iRead === this.iWrite;
}
}, {
key: "full",
get: function get() {
return this.iWrite === this.size;
}
}, {
key: "read",
value: function read(size) {
var buf;
// read size bytes from buffer and return buffer
if (size === 0) {
// special case - return null if no data requested
return null;
}
if (size === undefined || size >= this.length) {
// if no size specified or size is at least what we have then return all of the bytes
buf = this.toBuffer();
this.iRead = this.iWrite;
return buf;
}
// otherwise return a chunk
buf = Buffer.alloc(size);
this.buffer.copy(buf, 0, this.iRead, size);
this.iRead += size;
return buf;
}
}, {
key: "write",
value: function write(chunk, offset, length) {
// write as many bytes from data from optional source offset
// and return number of bytes written
var size = Math.min(length, this.size - this.iWrite);
chunk.copy(this.buffer, this.iWrite, offset, offset + size);
this.iWrite += size;
return size;
}
}]);
return ReadWriteBuf;
}(); // =============================================================================
// StreamBuf - a multi-purpose read-write stream
// As MemBuf - write as much data as you like. Then call toBuffer() to consolidate
// As StreamHub - pipe to multiple writables
// As readable stream - feed data into the writable part and have some other code read from it.
// Note: Not sure why but StreamBuf does not like JS "class" sugar. It fails the
// integration tests
var StreamBuf = function StreamBuf(options) {
options = options || {};
this.bufSize = options.bufSize || 1024 * 1024;
this.buffers = [];
// batch mode fills a buffer completely before passing the data on
// to pipes or 'readable' event listeners
this.batch = options.batch || false;
this.corked = false;
// where in the current writable buffer we're up to
this.inPos = 0;
// where in the current readable buffer we've read up to
this.outPos = 0;
// consuming pipe streams go here
this.pipes = [];
// controls emit('data')
this.paused = false;
this.encoding = null;
};
utils.inherits(StreamBuf, Stream.Duplex, {
toBuffer: function toBuffer() {
switch (this.buffers.length) {
case 0:
return null;
case 1:
return this.buffers[0].toBuffer();
default:
return Buffer.concat(this.buffers.map(function (rwBuf) {
return rwBuf.toBuffer();
}));
}
},
// writable
// event drain - if write returns false (which it won't), indicates when safe to write again.
// finish - end() has been called
// pipe(src) - pipe() has been called on readable
// unpipe(src) - unpipe() has been called on readable
// error - duh
_getWritableBuffer: function _getWritableBuffer() {
if (this.buffers.length) {
var last = this.buffers[this.buffers.length - 1];
if (!last.full) {
return last;
}
}
var buf = new ReadWriteBuf(this.bufSize);
this.buffers.push(buf);
return buf;
},
_pipe: function _pipe(chunk) {
var _this = this;
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
var write;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
write = function write(pipe) {
return new Promise(function (resolve) {
pipe.write(chunk.toBuffer(), function () {
resolve();
});
});
};
_context.next = 3;
return Promise.all(_this.pipes.map(write));
case 3:
case "end":
return _context.stop();
}
}, _callee);
}))();
},
_writeToBuffers: function _writeToBuffers(chunk) {
var inPos = 0;
var inLen = chunk.length;
while (inPos < inLen) {
// find writable buffer
var buffer = this._getWritableBuffer();
// write some data
inPos += buffer.write(chunk, inPos, inLen - inPos);
}
},
write: function write(data, encoding, callback) {
var _this2 = this;
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
var chunk;
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
if (encoding instanceof Function) {
callback = encoding;
encoding = 'utf8';
}
callback = callback || utils.nop;
// encapsulate data into a chunk
if (!(data instanceof StringBuf)) {
_context2.next = 6;
break;
}
chunk = new StringBufChunk(data);
_context2.next = 15;
break;
case 6:
if (!(data instanceof Buffer)) {
_context2.next = 10;
break;
}
chunk = new BufferChunk(data);
_context2.next = 15;
break;
case 10:
if (!(typeof data === 'string' || data instanceof String || data instanceof ArrayBuffer)) {
_context2.next = 14;
break;
}
chunk = new StringChunk(data, encoding);
_context2.next = 15;
break;
case 14:
throw new Error('Chunk must be one of type String, Buffer or StringBuf.');
case 15:
if (!_this2.pipes.length) {
_context2.next = 31;
break;
}
if (!_this2.batch) {
_context2.next = 21;
break;
}
_this2._writeToBuffers(chunk);
while (!_this2.corked && _this2.buffers.length > 1) {
_this2._pipe(_this2.buffers.shift());
}
_context2.next = 29;
break;
case 21:
if (_this2.corked) {
_context2.next = 27;
break;
}
_context2.next = 24;
return _this2._pipe(chunk);
case 24:
callback();
_context2.next = 29;
break;
case 27:
_this2._writeToBuffers(chunk);
process.nextTick(callback);
case 29:
_context2.next = 34;
break;
case 31:
if (!_this2.paused) {
_this2.emit('data', chunk.toBuffer());
}
_this2._writeToBuffers(chunk);
_this2.emit('readable');
case 34:
return _context2.abrupt("return", true);
case 35:
case "end":
return _context2.stop();
}
}, _callee2);
}))();
},
cork: function cork() {
this.corked = true;
},
_flush: function _flush() {
// if we have comsumers...
if (this.pipes.length) {
// and there's stuff not written
while (this.buffers.length) {
this._pipe(this.buffers.shift());
}
}
} /* destination */,
uncork: function uncork() {
this.corked = false;
this._flush();
},
end: function end(chunk, encoding, callback) {
var _this3 = this;
var writeComplete = function writeComplete(error) {
if (error) {
callback(error);
} else {
_this3._flush();
_this3.pipes.forEach(function (pipe) {
pipe.end();
});
_this3.emit('finish');
}
};
if (chunk) {
this.write(chunk, encoding, writeComplete);
} else {
writeComplete();
}
},
// readable
// event readable - some data is now available
// event data - switch to flowing mode - feeds chunks to handler
// event end - no more data
// event close - optional, indicates upstream close
// event error - duh
read: function read(size) {
var buffers;
// read min(buffer, size || infinity)
if (size) {
buffers = [];
while (size && this.buffers.length && !this.buffers[0].eod) {
var first = this.buffers[0];
var buffer = first.read(size);
size -= buffer.length;
buffers.push(buffer);
if (first.eod && first.full) {
this.buffers.shift();
}
}
return Buffer.concat(buffers);
}
buffers = this.buffers.map(function (buf) {
return buf.toBuffer();
}).filter(Boolean);
this.buffers = [];
return Buffer.concat(buffers);
},
setEncoding: function setEncoding(encoding) {
// causes stream.read or stream.on('data) to return strings of encoding instead of Buffer objects
this.encoding = encoding;
},
pause: function pause() {
this.paused = true;
},
resume: function resume() {
this.paused = false;
},
isPaused: function isPaused() {
return !!this.paused;
},
pipe: function pipe(destination) {
// add destination to pipe list & write current buffer
this.pipes.push(destination);
if (!this.paused && this.buffers.length) {
this.end();
}
},
unpipe: function unpipe(destination) {
// remove destination from pipe list
this.pipes = this.pipes.filter(function (pipe) {
return pipe !== destination;
});
},
unshift: function unshift() {
// some numpty has read some data that's not for them and they want to put it back!
// Might implement this some day
throw new Error('Not Implemented');
} /* chunk */,
wrap: function wrap() {
// not implemented
throw new Error('Not Implemented');
} /* stream */
});
module.exports = StreamBuf;
//# sourceMappingURL=stream-buf.js.map