UNPKG

docxml

Version:

TypeScript (component) library for building and parsing a DOCX file

129 lines (128 loc) 5.82 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _Archive_zip, _Archive_promises; Object.defineProperty(exports, "__esModule", { value: true }); exports.Archive = void 0; const dntShim = __importStar(require("../../_dnt.shims.js")); const mod_js_1 = require("../../deps/deno.land/x/jszip@0.11.0/mod.js"); const dom_js_1 = require("../utilities/dom.js"); class Archive { constructor(zip) { Object.defineProperty(this, "location", { enumerable: true, configurable: true, writable: true, value: void 0 }); _Archive_zip.set(this, void 0); _Archive_promises.set(this, []); __classPrivateFieldSet(this, _Archive_zip, zip || new mod_js_1.JSZip(), "f"); } /** * @deprecated For testing purposes only. */ get $$$fileNames() { return __classPrivateFieldGet(this, _Archive_zip, "f").files(); } hasFile(location) { return !!__classPrivateFieldGet(this, _Archive_zip, "f").files()[location]; } readText(location) { if (location.startsWith('/') || location.startsWith('./')) { location = location.substring(location.indexOf('/') + 1); } try { return __classPrivateFieldGet(this, _Archive_zip, "f").file(location).async('string'); } catch (error) { throw new Error(`Could not read "${location}" from archive: ${error.message}. The only files in this archive are: ${Object.keys(__classPrivateFieldGet(this, _Archive_zip, "f").files()).join(', ')}`); } } async asUint8Array() { for await (const { location, promise } of __classPrivateFieldGet(this, _Archive_promises, "f")) { __classPrivateFieldGet(this, _Archive_zip, "f").addFile(location, await promise); } return __classPrivateFieldGet(this, _Archive_zip, "f").generateAsync({ type: 'uint8array' }); } async readXml(location) { return (0, dom_js_1.parse)(await this.readText(location)); } readBinary(location) { return __classPrivateFieldGet(this, _Archive_zip, "f").file(location).async('uint8array'); } /** * Create a new XML file in the DOCX archive. */ addXmlFile(location, node) { return this.addTextFile(location, `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${(0, dom_js_1.serialize)(node)}`); } /** * Create a new JSON file in the DOCX archive. */ // deno-lint-ignore no-explicit-any addJsonFile(location, js) { return this.addTextFile(location, JSON.stringify(js, null, '\t')); } /** * Create a new text file in the DOCX archive. */ addTextFile(location, contents) { __classPrivateFieldGet(this, _Archive_zip, "f").addFile(location, contents); return this; } /** * Create a new text file in the DOCX archive. * * In order to keep this method (and methods that use it, eg. Docx#toArchive) synchronous, * we're only writing a promise to memory for now and leave the asynchronous operations for * output time (see also Archive#toUint8Array). */ addBinaryFile(location, promised) { __classPrivateFieldGet(this, _Archive_promises, "f").push({ location, promise: promised }); return this; } static async fromUInt8Array(data) { return new Archive(await new mod_js_1.JSZip().loadAsync(data)); } static async fromFile(location) { return new Archive(await (0, mod_js_1.readZip)(location)); } async toFile(location) { await dntShim.Deno.writeFile(location, await this.asUint8Array()); } } exports.Archive = Archive; _Archive_zip = new WeakMap(), _Archive_promises = new WeakMap();