UNPKG

@firecrawl/anydoc-wasm

Version:

WebAssembly bindings for anydoc: convert documents (doc, docx, odt, rtf, epub, pdf, presentations, spreadsheets, csv) to GitHub-Flavored Markdown in the browser

419 lines (368 loc) • 14.4 kB
/* @ts-self-types="./anydoc_wasm.d.ts" */ /** * Detect the format from the content itself: the signature and identity each * container specification designates (PDF header, RTF open group, OLE stream * names, ZIP package mimetype/content types). Plain-text formats (CSV) carry * no signature and return `undefined`; so does anything unrecognized. * @param {Uint8Array} bytes * @returns {Format | undefined} */ export function formatFromBytes(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_export); const len0 = WASM_VECTOR_LEN; const ret = wasm.formatFromBytes(ptr0, len0); return __wbindgen_enum_Format[ret]; } /** * The format an extension names, with or without a leading dot. * @param {string} extension * @returns {Format | undefined} */ export function formatFromExtension(extension) { const ptr0 = passStringToWasm0(extension, wasm.__wbindgen_export, wasm.__wbindgen_export2); const len0 = WASM_VECTOR_LEN; const ret = wasm.formatFromExtension(ptr0, len0); return __wbindgen_enum_Format[ret]; } /** * The format a path's (or file name's) extension names. * @param {string} path * @returns {Format | undefined} */ export function formatFromPath(path) { const ptr0 = passStringToWasm0(path, wasm.__wbindgen_export, wasm.__wbindgen_export2); const len0 = WASM_VECTOR_LEN; const ret = wasm.formatFromPath(ptr0, len0); return __wbindgen_enum_Format[ret]; } /** * Parse an in-memory document into the document model, which also carries * the embedded assets. Without a format, it is detected from the content. * * Unsupported for `pdf`: PDF conversion produces Markdown directly and has * no document-model form; use `toMarkdownBytes`. * * Throws an `Error` carrying a `ConvertErrorCode` on `code`. * @param {Uint8Array} bytes * @param {Format | null} [format] * @returns {Document} */ export function toDocument(bytes, format) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_export); const len0 = WASM_VECTOR_LEN; wasm.toDocument(retptr, ptr0, len0, isLikeNone(format) ? 13 : ((__wbindgen_enum_Format.indexOf(format) + 1 || 13) - 1)); var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true); var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true); var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true); if (r2) { throw takeObject(r1); } return takeObject(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Convert an in-memory document to Markdown. Without a format, it is * detected from the content, which signature-less formats (CSV) have to name * explicitly. * * Throws an `Error` carrying a `ConvertErrorCode` on `code`. * @param {Uint8Array} bytes * @param {Format | null} [format] * @returns {string} */ export function toMarkdownBytes(bytes, format) { let deferred3_0; let deferred3_1; try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_export); const len0 = WASM_VECTOR_LEN; wasm.toMarkdownBytes(retptr, ptr0, len0, isLikeNone(format) ? 13 : ((__wbindgen_enum_Format.indexOf(format) + 1 || 13) - 1)); var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true); var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true); var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true); var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true); var ptr2 = r0; var len2 = r1; if (r3) { ptr2 = 0; len2 = 0; throw takeObject(r2); } deferred3_0 = ptr2; deferred3_1 = len2; return getStringFromWasm0(ptr2, len2); } finally { wasm.__wbindgen_add_to_stack_pointer(16); wasm.__wbindgen_export4(deferred3_0, deferred3_1, 1); } } function __wbg_get_imports() { const import0 = { __proto__: null, __wbg_String_8564e559799eccda: function(arg0, arg1) { const ret = String(getObject(arg1)); const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2); const len1 = WASM_VECTOR_LEN; getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); }, __wbg___wbindgen_throw_344f42d3211c4765: function(arg0, arg1) { throw new Error(getStringFromWasm0(arg0, arg1)); }, __wbg_from_13e323c65fc8f464: function(arg0) { const ret = Array.from(getObject(arg0)); return addHeapObject(ret); }, __wbg_getRandomValues_cc7f052a444bb2ce: function() { return handleError(function (arg0, arg1) { globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1)); }, arguments); }, __wbg_new_32b398fb48b6d94a: function() { const ret = new Array(); return addHeapObject(ret); }, __wbg_new_b667d279fd5aa943: function(arg0, arg1) { const ret = new Error(getStringFromWasm0(arg0, arg1)); return addHeapObject(ret); }, __wbg_new_cd45aabdf6073e84: function(arg0) { const ret = new Uint8Array(getObject(arg0)); return addHeapObject(ret); }, __wbg_new_da52cf8fe3429cb2: function() { const ret = new Object(); return addHeapObject(ret); }, __wbg_push_d2ae3af0c1217ae6: function(arg0, arg1) { const ret = getObject(arg0).push(getObject(arg1)); return ret; }, __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) { getObject(arg0)[takeObject(arg1)] = takeObject(arg2); }, __wbg_set_8535240470bf2500: function() { return handleError(function (arg0, arg1, arg2) { const ret = Reflect.set(getObject(arg0), getObject(arg1), getObject(arg2)); return ret; }, arguments); }, __wbg_set_8a16b38e4805b298: function(arg0, arg1, arg2) { getObject(arg0)[arg1 >>> 0] = takeObject(arg2); }, __wbindgen_cast_0000000000000001: function(arg0) { // Cast intrinsic for `F64 -> Externref`. const ret = arg0; return addHeapObject(ret); }, __wbindgen_cast_0000000000000002: function(arg0, arg1) { // Cast intrinsic for `Ref(Slice(U8)) -> NamedExternref("Uint8Array")`. const ret = getArrayU8FromWasm0(arg0, arg1); return addHeapObject(ret); }, __wbindgen_cast_0000000000000003: function(arg0, arg1) { // Cast intrinsic for `Ref(String) -> Externref`. const ret = getStringFromWasm0(arg0, arg1); return addHeapObject(ret); }, __wbindgen_object_clone_ref: function(arg0) { const ret = getObject(arg0); return addHeapObject(ret); }, __wbindgen_object_drop_ref: function(arg0) { takeObject(arg0); }, }; return { __proto__: null, "./anydoc_wasm_bg.js": import0, }; } const __wbindgen_enum_Format = ["doc", "docx", "odt", "pdf", "ppt", "pptx", "rtf", "epub", "xlsx", "ods", "odp", "csv"]; function addHeapObject(obj) { if (heap_next === heap.length) heap.push(heap.length + 1); const idx = heap_next; heap_next = heap[idx]; heap[idx] = obj; return idx; } function dropObject(idx) { if (idx < 1028) return; heap[idx] = heap_next; heap_next = idx; } function getArrayU8FromWasm0(ptr, len) { ptr = ptr >>> 0; return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); } let cachedDataViewMemory0 = null; function getDataViewMemory0() { if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { cachedDataViewMemory0 = new DataView(wasm.memory.buffer); } return cachedDataViewMemory0; } function getStringFromWasm0(ptr, len) { return decodeText(ptr >>> 0, len); } let cachedUint8ArrayMemory0 = null; function getUint8ArrayMemory0() { if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); } return cachedUint8ArrayMemory0; } function getObject(idx) { return heap[idx]; } function handleError(f, args) { try { return f.apply(this, args); } catch (e) { wasm.__wbindgen_export3(addHeapObject(e)); } } let heap = new Array(1024).fill(undefined); heap.push(undefined, null, true, false); let heap_next = heap.length; function isLikeNone(x) { return x === undefined || x === null; } function passArray8ToWasm0(arg, malloc) { const ptr = malloc(arg.length * 1, 1) >>> 0; getUint8ArrayMemory0().set(arg, ptr / 1); WASM_VECTOR_LEN = arg.length; return ptr; } function passStringToWasm0(arg, malloc, realloc) { if (realloc === undefined) { const buf = cachedTextEncoder.encode(arg); const ptr = malloc(buf.length, 1) >>> 0; getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); WASM_VECTOR_LEN = buf.length; return ptr; } let len = arg.length; let ptr = malloc(len, 1) >>> 0; const mem = getUint8ArrayMemory0(); let offset = 0; for (; offset < len; offset++) { const code = arg.charCodeAt(offset); if (code > 0x7F) break; mem[ptr + offset] = code; } if (offset !== len) { if (offset !== 0) { arg = arg.slice(offset); } ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); const ret = cachedTextEncoder.encodeInto(arg, view); offset += ret.written; ptr = realloc(ptr, len, offset, 1) >>> 0; } WASM_VECTOR_LEN = offset; return ptr; } function takeObject(idx) { const ret = getObject(idx); dropObject(idx); return ret; } let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); const MAX_SAFARI_DECODE_BYTES = 2146435072; let numBytesDecoded = 0; function decodeText(ptr, len) { numBytesDecoded += len; if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) { cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); numBytesDecoded = len; } return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); } const cachedTextEncoder = new TextEncoder(); if (!('encodeInto' in cachedTextEncoder)) { cachedTextEncoder.encodeInto = function (arg, view) { const buf = cachedTextEncoder.encode(arg); view.set(buf); return { read: arg.length, written: buf.length }; }; } let WASM_VECTOR_LEN = 0; let wasmModule, wasmInstance, wasm; function __wbg_finalize_init(instance, module) { wasmInstance = instance; wasm = instance.exports; wasmModule = module; cachedDataViewMemory0 = null; cachedUint8ArrayMemory0 = null; return wasm; } async function __wbg_load(module, imports) { if (typeof Response === 'function' && module instanceof Response) { if (typeof WebAssembly.instantiateStreaming === 'function') { try { return await WebAssembly.instantiateStreaming(module, imports); } catch (e) { const validResponse = module.ok && expectedResponseType(module.type); if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') { console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); } else { throw e; } } } const bytes = await module.arrayBuffer(); return await WebAssembly.instantiate(bytes, imports); } else { const instance = await WebAssembly.instantiate(module, imports); if (instance instanceof WebAssembly.Instance) { return { instance, module }; } else { return instance; } } function expectedResponseType(type) { switch (type) { case 'basic': case 'cors': case 'default': return true; } return false; } } function initSync(module) { if (wasm !== undefined) return wasm; if (module !== undefined) { if (Object.getPrototypeOf(module) === Object.prototype) { ({module} = module) } else { console.warn('using deprecated parameters for `initSync()`; pass a single object instead') } } const imports = __wbg_get_imports(); if (!(module instanceof WebAssembly.Module)) { module = new WebAssembly.Module(module); } const instance = new WebAssembly.Instance(module, imports); return __wbg_finalize_init(instance, module); } async function __wbg_init(module_or_path) { if (wasm !== undefined) return wasm; if (module_or_path !== undefined) { if (Object.getPrototypeOf(module_or_path) === Object.prototype) { ({module_or_path} = module_or_path) } else { console.warn('using deprecated parameters for the initialization function; pass a single object instead') } } if (module_or_path === undefined) { module_or_path = new URL('anydoc_wasm_bg.wasm', import.meta.url); } const imports = __wbg_get_imports(); if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { module_or_path = fetch(module_or_path); } const { instance, module } = await __wbg_load(await module_or_path, imports); return __wbg_finalize_init(instance, module); } export { initSync, __wbg_init as default };