UNPKG

@microsoft/connected-workbooks

Version:

Microsoft backed, Excel advanced xlsx workbook generation JavaScript library

195 lines (194 loc) 8.87 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var constants_1 = require("./constants"); var generators_1 = require("../generators"); var buffer_1 = require("buffer"); var xmldom_qsa_1 = require("xmldom-qsa"); var getBase64 = function (zip) { return __awaiter(void 0, void 0, void 0, function () { var mashup; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, getDataMashupFile(zip)]; case 1: mashup = _a.sent(); return [2 /*return*/, mashup.value]; } }); }); }; var setBase64 = function (zip, base64) { return __awaiter(void 0, void 0, void 0, function () { var newXml, encoded, mashup; return __generator(this, function (_a) { switch (_a.label) { case 0: newXml = (0, generators_1.generateMashupXMLTemplate)(base64); encoded = buffer_1.Buffer.from(constants_1.BOM + newXml, "ucs2"); return [4 /*yield*/, getDataMashupFile(zip)]; case 1: mashup = _a.sent(); zip.file(mashup === null || mashup === void 0 ? void 0 : mashup.path, encoded); return [2 /*return*/]; } }); }); }; var getDataMashupFile = function (zip) { return __awaiter(void 0, void 0, void 0, function () { var mashup, _i, _a, url, item; return __generator(this, function (_b) { switch (_b.label) { case 0: _i = 0, _a = constants_1.URLS.PQ; _b.label = 1; case 1: if (!(_i < _a.length)) return [3 /*break*/, 4]; url = _a[_i]; return [4 /*yield*/, getCustomXmlFile(zip, url)]; case 2: item = _b.sent(); if (item.found) { mashup = item; return [3 /*break*/, 4]; } _b.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: if (!mashup) { throw new Error("DataMashup XML is not found"); } return [2 /*return*/, mashup]; } }); }); }; var getCustomXmlFile = function (zip, url) { return __awaiter(void 0, void 0, void 0, function () { var parser, itemsArray, found, path, xmlString, value, i, xmlValue, buffer, encoding, doc; var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: parser = new xmldom_qsa_1.DOMParser(); return [4 /*yield*/, zip.file(/customXml\/item\d.xml/)]; case 1: itemsArray = _c.sent(); if (!itemsArray || itemsArray.length === 0) { throw new Error("No customXml files were found!"); } found = false; value = null; i = 1; _c.label = 2; case 2: if (!(i <= itemsArray.length)) return [3 /*break*/, 5]; path = (0, generators_1.generateCustomXmlFilePath)(i); return [4 /*yield*/, ((_a = zip.file(path)) === null || _a === void 0 ? void 0 : _a.async("uint8array"))]; case 3: xmlValue = _c.sent(); if (xmlValue === undefined) { return [3 /*break*/, 5]; } buffer = buffer_1.Buffer.from(xmlValue); encoding = detectEncoding(xmlValue); if (!encoding) { throw new Error("Failed to detect xml encoding"); } xmlString = buffer .toString(encoding) .replace(/^\ufeff/, ""); doc = parser.parseFromString(xmlString, "text/xml"); found = ((_b = doc === null || doc === void 0 ? void 0 : doc.documentElement) === null || _b === void 0 ? void 0 : _b.namespaceURI) === url; if (found) { value = doc.documentElement.textContent; return [3 /*break*/, 5]; } _c.label = 4; case 4: i++; return [3 /*break*/, 2]; case 5: return [2 /*return*/, { found: found, path: path, xmlString: xmlString, value: value }]; } }); }); }; var queryNameHasInvalidChars = function (queryName) { var invalidQueryNameChars = ['"', "."]; // Control characters as defined in Unicode for (var c = 0; c <= 0x001f; ++c) { invalidQueryNameChars.push(String.fromCharCode(c)); } for (var c = 0x007f; c <= 0x009f; ++c) { invalidQueryNameChars.push(String.fromCharCode(c)); } return queryName.split("").some(function (ch) { return invalidQueryNameChars.indexOf(ch) !== -1; }); }; var validateQueryName = function (queryName) { if (queryName) { if (queryName.length > constants_1.maxQueryLength) { throw new Error(constants_1.QueryNameMaxLengthErr); } if (queryNameHasInvalidChars(queryName)) { throw new Error(constants_1.QueryNameInvalidCharsErr); } } if (!queryName.trim()) { throw new Error(constants_1.EmptyQueryNameErr); } }; var detectEncoding = function (xmlBytes) { if (!xmlBytes) { return null; } if (xmlBytes.length >= 3 && xmlBytes[0] === 0xEF && xmlBytes[1] === 0xBB && xmlBytes[2] === 0xBF) { return 'utf-8'; } if (xmlBytes.length >= 3 && xmlBytes[0] === 0xFF && xmlBytes[1] === 0xFE) { return 'utf-16le'; } if (xmlBytes.length >= 3 && xmlBytes[0] === 0xFE && xmlBytes[1] === 0xFF) { return 'utf-16be'; } // Default to utf-8, that not required a BOM for encoding. return 'utf-8'; }; exports.default = { getBase64: getBase64, setBase64: setBase64, getCustomXmlFile: getCustomXmlFile, getDataMashupFile: getDataMashupFile, validateQueryName: validateQueryName, };