UNPKG

write-excel-file

Version:

Write simple `*.xlsx` files in a browser or Node.js

140 lines (132 loc) 5.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _archiver = _interopRequireDefault(require("archiver")); var _fs = _interopRequireDefault(require("fs")); var _stream = require("stream"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } 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); } // Copy-pasted from: // https://github.com/catamphetamine/serverless-functions/blob/master/source/deploy/archive.js // Uses `archiver` library. // https://www.npmjs.com/package/archiver // https://www.archiverjs.com/docs/archiver // import { WritableStream } from 'memory-streams' // `PassThrough` is available since Node version `0.10`. /** * A server-side *.zip archive creator. */ var Archive = exports["default"] = /*#__PURE__*/function () { function Archive(outputPath) { var _this = this; _classCallCheck(this, Archive); if (outputPath) { this.outputStream = _fs["default"].createWriteStream(outputPath); } else { // // Won't work for memory streams. // // https://github.com/archiverjs/node-archiver/issues/336 // this.outputStream = new WritableStream() this.outputStream = new _stream.PassThrough(); } var archive = (0, _archiver["default"])('zip', { // // Sets the compression level. // zlib: { level: 9 } }); this.archive = archive; // `archive` has a `.pipe()` method which kinda makes it usable as a readable stream. // Although it's still not a "proper" implementation of a `Readable` stream. // https://github.com/archiverjs/node-archiver/issues/765 // To get a "proper" implementation of a `Readable` stream, people use a workaround: // `const passThroughStream = new PassThrough()` and then `archive.pipe(passThroughStream)`. // An alternative way of how to create a `ReadableStream` from an `archive`. // https://github.com/archiverjs/node-archiver/issues/759 // // const readableStream = new ReadableStream({ // start(controller) { // archive.on('data', chunk => controller.enqueue(chunk)); // archive.on('end', () => controller.close()); // archive.on('error', error => controller.error(error)); // // archive.append(...); // archive.append(...); // archive.finalize(); // } // }); this.promise = new Promise(function (resolve, reject) { // listen for all archive data to be written // 'close' event is fired only when a file descriptor is involved _this.outputStream.on('close', function () { resolve({ size: archive.pointer() }); }); // // This event is fired when the data source is drained no matter what was the data source. // // It is not part of this library but rather from the NodeJS Stream API. // // @see: https://nodejs.org/api/stream.html#stream_event_end // archive.on('end', function() { // console.log('Data has been drained') // resolve({ // // output: outputPath ? undefined : this.outputStream.toBuffer(), // size: archive.pointer() // }) // }) // good practice to catch warnings (ie stat failures and other non-blocking errors) archive.on('warning', function (error) { if (error.code === 'ENOENT') { // log warning console.warn(error); } else { reject(error); } }); // good practice to catch this error explicitly archive.on('error', reject); // pipe archive data to the file archive.pipe(_this.outputStream); }); } return _createClass(Archive, [{ key: "file", value: function file(filePath, internalPath) { this.archive.file(filePath, { name: internalPath }); } }, { key: "directory", value: function directory(directoryPath, internalPath) { this.archive.directory(directoryPath, internalPath); } }, { key: "append", value: function append(content, internalPath) { this.archive.append(content, { name: internalPath }); } }, { key: "write", value: function write() { // `.finalize()` returns some kind of `Promise` but it's not meant to be `await`ed. // https://github.com/archiverjs/node-archiver/issues/772 this.archive.finalize(); } }, { key: "getStream", value: function getStream() { return this.outputStream; } }, { key: "getPromise", value: function getPromise() { return this.promise; } }]); }(); //# sourceMappingURL=archive.js.map