worker.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. (function() {
  2. 'use strict';
  3. /*
  4. * Copyright 2017 Sam Thorogood. All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. * use this file except in compliance with the License. You may obtain a copy of
  8. * the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. * License for the specific language governing permissions and limitations under
  16. * the License.
  17. */
  18. /**
  19. * @fileoverview Polyfill for TextEncoder and TextDecoder.
  20. *
  21. * You probably want `text.min.js`, and not this file directly.
  22. */
  23. (function(scope) {
  24. // fail early
  25. if (scope['TextEncoder'] && scope['TextDecoder']) {
  26. return false;
  27. }
  28. /**
  29. * @constructor
  30. * @param {string=} utfLabel
  31. */
  32. function FastTextEncoder(utfLabel = 'utf-8') {
  33. if (utfLabel !== 'utf-8') {
  34. throw new RangeError(`Failed to construct 'TextEncoder': The encoding label provided ('${utfLabel}') is invalid.`);
  35. }
  36. }
  37. Object.defineProperty(FastTextEncoder.prototype, 'encoding', { value: 'utf-8' });
  38. /**
  39. * @param {string} string
  40. * @param {{stream: boolean}=} options
  41. * @return {!Uint8Array}
  42. */
  43. FastTextEncoder.prototype.encode = function(string, options = { stream: false }) {
  44. if (options.stream) {
  45. throw new Error(`Failed to encode: the 'stream' option is unsupported.`);
  46. }
  47. let pos = 0;
  48. const len = string.length;
  49. let at = 0; // output position
  50. let tlen = Math.max(32, len + (len >> 1) + 7); // 1.5x size
  51. let target = new Uint8Array((tlen >> 3) << 3); // ... but at 8 byte offset
  52. while (pos < len) {
  53. let value = string.charCodeAt(pos++);
  54. if (value >= 0xd800 && value <= 0xdbff) {
  55. // high surrogate
  56. if (pos < len) {
  57. const extra = string.charCodeAt(pos);
  58. if ((extra & 0xfc00) === 0xdc00) {
  59. ++pos;
  60. value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
  61. }
  62. }
  63. if (value >= 0xd800 && value <= 0xdbff) {
  64. continue; // drop lone surrogate
  65. }
  66. }
  67. // expand the buffer if we couldn't write 4 bytes
  68. if (at + 4 > target.length) {
  69. tlen += 8; // minimum extra
  70. tlen *= 1.0 + (pos / string.length) * 2; // take 2x the remaining
  71. tlen = (tlen >> 3) << 3; // 8 byte offset
  72. const update = new Uint8Array(tlen);
  73. update.set(target);
  74. target = update;
  75. }
  76. if ((value & 0xffffff80) === 0) {
  77. // 1-byte
  78. target[at++] = value; // ASCII
  79. continue;
  80. } else if ((value & 0xfffff800) === 0) {
  81. // 2-byte
  82. target[at++] = ((value >> 6) & 0x1f) | 0xc0;
  83. } else if ((value & 0xffff0000) === 0) {
  84. // 3-byte
  85. target[at++] = ((value >> 12) & 0x0f) | 0xe0;
  86. target[at++] = ((value >> 6) & 0x3f) | 0x80;
  87. } else if ((value & 0xffe00000) === 0) {
  88. // 4-byte
  89. target[at++] = ((value >> 18) & 0x07) | 0xf0;
  90. target[at++] = ((value >> 12) & 0x3f) | 0x80;
  91. target[at++] = ((value >> 6) & 0x3f) | 0x80;
  92. } else {
  93. // FIXME: do we care
  94. continue;
  95. }
  96. target[at++] = (value & 0x3f) | 0x80;
  97. }
  98. return target.slice(0, at);
  99. };
  100. /**
  101. * @constructor
  102. * @param {string=} utfLabel
  103. * @param {{fatal: boolean}=} options
  104. */
  105. function FastTextDecoder(utfLabel = 'utf-8', options = { fatal: false }) {
  106. if (utfLabel !== 'utf-8') {
  107. throw new RangeError(`Failed to construct 'TextDecoder': The encoding label provided ('${utfLabel}') is invalid.`);
  108. }
  109. if (options.fatal) {
  110. throw new Error(`Failed to construct 'TextDecoder': the 'fatal' option is unsupported.`);
  111. }
  112. }
  113. Object.defineProperty(FastTextDecoder.prototype, 'encoding', { value: 'utf-8' });
  114. Object.defineProperty(FastTextDecoder.prototype, 'fatal', { value: false });
  115. Object.defineProperty(FastTextDecoder.prototype, 'ignoreBOM', { value: false });
  116. /**
  117. * @param {(!ArrayBuffer|!ArrayBufferView)} buffer
  118. * @param {{stream: boolean}=} options
  119. */
  120. FastTextDecoder.prototype.decode = function(buffer, options = { stream: false }) {
  121. if (options['stream']) {
  122. throw new Error(`Failed to decode: the 'stream' option is unsupported.`);
  123. }
  124. const bytes = new Uint8Array(buffer);
  125. let pos = 0;
  126. const len = bytes.length;
  127. const out = [];
  128. while (pos < len) {
  129. const byte1 = bytes[pos++];
  130. if (byte1 === 0) {
  131. break; // NULL
  132. }
  133. if ((byte1 & 0x80) === 0) {
  134. // 1-byte
  135. out.push(byte1);
  136. } else if ((byte1 & 0xe0) === 0xc0) {
  137. // 2-byte
  138. const byte2 = bytes[pos++] & 0x3f;
  139. out.push(((byte1 & 0x1f) << 6) | byte2);
  140. } else if ((byte1 & 0xf0) === 0xe0) {
  141. const byte2 = bytes[pos++] & 0x3f;
  142. const byte3 = bytes[pos++] & 0x3f;
  143. out.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
  144. } else if ((byte1 & 0xf8) === 0xf0) {
  145. const byte2 = bytes[pos++] & 0x3f;
  146. const byte3 = bytes[pos++] & 0x3f;
  147. const byte4 = bytes[pos++] & 0x3f;
  148. // this can be > 0xffff, so possibly generate surrogates
  149. let codepoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
  150. if (codepoint > 0xffff) {
  151. // codepoint &= ~0x10000;
  152. codepoint -= 0x10000;
  153. out.push(((codepoint >>> 10) & 0x3ff) | 0xd800);
  154. codepoint = 0xdc00 | (codepoint & 0x3ff);
  155. }
  156. out.push(codepoint);
  157. }
  158. }
  159. return String.fromCharCode.apply(null, out);
  160. };
  161. scope['TextEncoder'] = FastTextEncoder;
  162. scope['TextDecoder'] = FastTextDecoder;
  163. })(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : self);
  164. (function() {
  165. const __exports = {};
  166. let wasm;
  167. const heap = new Array(32);
  168. heap.fill(undefined);
  169. heap.push(undefined, null, true, false);
  170. let stack_pointer = 32;
  171. function addBorrowedObject(obj) {
  172. if (stack_pointer == 1) throw new Error('out of js stack');
  173. heap[--stack_pointer] = obj;
  174. return stack_pointer;
  175. }
  176. let cachegetUint8Memory = null;
  177. function getUint8Memory() {
  178. if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
  179. cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
  180. }
  181. return cachegetUint8Memory;
  182. }
  183. function getArrayU8FromWasm(ptr, len) {
  184. return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
  185. }
  186. let cachedGlobalArgumentPtr = null;
  187. function globalArgumentPtr() {
  188. if (cachedGlobalArgumentPtr === null) {
  189. cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
  190. }
  191. return cachedGlobalArgumentPtr;
  192. }
  193. let cachegetUint32Memory = null;
  194. function getUint32Memory() {
  195. if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
  196. cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
  197. }
  198. return cachegetUint32Memory;
  199. }
  200. /**
  201. * @param {any} raw_data
  202. * @returns {Uint8Array}
  203. */
  204. __exports.import_to_xlsx = function(raw_data) {
  205. const retptr = globalArgumentPtr();
  206. try {
  207. wasm.import_to_xlsx(retptr, addBorrowedObject(raw_data));
  208. const mem = getUint32Memory();
  209. const rustptr = mem[retptr / 4];
  210. const rustlen = mem[retptr / 4 + 1];
  211. const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
  212. wasm.__wbindgen_free(rustptr, rustlen * 1);
  213. return realRet;
  214. } finally {
  215. heap[stack_pointer++] = undefined;
  216. }
  217. };
  218. let cachedTextEncoder = new TextEncoder('utf-8');
  219. let WASM_VECTOR_LEN = 0;
  220. let passStringToWasm;
  221. if (typeof cachedTextEncoder.encodeInto === 'function') {
  222. passStringToWasm = function(arg) {
  223. let size = arg.length;
  224. let ptr = wasm.__wbindgen_malloc(size);
  225. let writeOffset = 0;
  226. // eslint-disable-next-line no-constant-condition
  227. while (true) {
  228. const view = getUint8Memory().subarray(ptr + writeOffset, ptr + size);
  229. const { read, written } = cachedTextEncoder.encodeInto(arg, view);
  230. arg = arg.substring(read);
  231. writeOffset += written;
  232. if (arg.length === 0) {
  233. break;
  234. }
  235. ptr = wasm.__wbindgen_realloc(ptr, size, size * 2);
  236. size *= 2;
  237. }
  238. WASM_VECTOR_LEN = writeOffset;
  239. return ptr;
  240. };
  241. } else {
  242. passStringToWasm = function(arg) {
  243. const buf = cachedTextEncoder.encode(arg);
  244. const ptr = wasm.__wbindgen_malloc(buf.length);
  245. getUint8Memory().set(buf, ptr);
  246. WASM_VECTOR_LEN = buf.length;
  247. return ptr;
  248. };
  249. }
  250. function getObject(idx) {
  251. return heap[idx];
  252. }
  253. __exports.__wbindgen_json_serialize = function(idx, ptrptr) {
  254. const ptr = passStringToWasm(JSON.stringify(getObject(idx)));
  255. getUint32Memory()[ptrptr / 4] = ptr;
  256. return WASM_VECTOR_LEN;
  257. };
  258. let heap_next = heap.length;
  259. function dropObject(idx) {
  260. if (idx < 36) return;
  261. heap[idx] = heap_next;
  262. heap_next = idx;
  263. }
  264. __exports.__wbindgen_object_drop_ref = function(i) {
  265. dropObject(i);
  266. };
  267. // eslint-disable-next-line no-unused-vars
  268. function init(module_or_path, maybe_memory) {
  269. let result;
  270. const imports = { './xlsx_import': __exports };
  271. if (module_or_path instanceof URL || typeof module_or_path === 'string' || module_or_path instanceof Request) {
  272. const response = fetch(module_or_path);
  273. if (typeof WebAssembly.instantiateStreaming === 'function') {
  274. result = WebAssembly.instantiateStreaming(response, imports).catch(e => {
  275. console.warn(
  276. '`WebAssembly.instantiateStreaming` failed. Assuming this is because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n',
  277. e
  278. );
  279. return response.then(r => r.arrayBuffer()).then(bytes => WebAssembly.instantiate(bytes, imports));
  280. });
  281. } else {
  282. result = response.then(r => r.arrayBuffer()).then(bytes => WebAssembly.instantiate(bytes, imports));
  283. }
  284. } else {
  285. result = WebAssembly.instantiate(module_or_path, imports).then(instance => {
  286. return { instance, module: module_or_path };
  287. });
  288. }
  289. return result.then(({ instance, module }) => {
  290. wasm = instance.exports;
  291. init.__wbindgen_wasm_module = module;
  292. return wasm;
  293. });
  294. }
  295. self.wasm_bindgen = Object.assign(init, __exports);
  296. })();
  297. onmessage = function(e) {
  298. if (e.data.type === 'convert') {
  299. doConvert(e.data);
  300. }
  301. };
  302. let import_to_xlsx = null;
  303. function doConvert(config) {
  304. if (import_to_xlsx) {
  305. const result = import_to_xlsx(config.data);
  306. const blob = new Blob([result], {
  307. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,',
  308. });
  309. postMessage({
  310. uid: config.uid || new Date().valueOf(),
  311. type: 'ready',
  312. blob,
  313. });
  314. } else {
  315. const path = config.wasmPath || 'https://cdn.dhtmlx.com/libs/json2excel/1.0/lib.wasm';
  316. // eslint-disable-next-line no-undef
  317. wasm_bindgen(path)
  318. .then(() => {
  319. // eslint-disable-next-line no-undef
  320. import_to_xlsx = wasm_bindgen.import_to_xlsx;
  321. doConvert(config);
  322. })
  323. .catch(e => console.log(e));
  324. }
  325. }
  326. })();