UNPKG

@evan/wasm

Version:
601 lines (600 loc) 22.9 kB
var __defProp = Object.defineProperty; var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); var __export = (target, all) => { __markAsModule(target); for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __accessCheck = (obj, member, msg) => { if (!member.has(obj)) throw TypeError("Cannot " + msg); }; var __privateGet = (obj, member, getter) => { __accessCheck(obj, member, "read from private field"); return getter ? getter.call(obj) : member.get(obj); }; var __privateAdd = (obj, member, value) => { if (member.has(obj)) throw TypeError("Cannot add the same private member more than once"); member instanceof WeakSet ? member.add(obj) : member.set(obj, value); }; var __privateSet = (obj, member, value, setter) => { __accessCheck(obj, member, "write to private field"); setter ? setter.call(obj, value) : member.set(obj, value); return value; }; var __privateMethod = (obj, member, method) => { __accessCheck(obj, member, "access private method"); return method; }; __export(exports, { Rewriter: () => Rewriter }); var _a, _b, _c, _d, _e, _f, _g, _h, _ptr, _t, _init, init_fn, _ptr2, _wasm, _guard, guard_fn, _ptr3, _wasm2, _guard2, guard_fn2, _ptr4, _wasm3, _guard3, guard_fn3, _ptr5, _wasm4, _guard4, guard_fn4, _ptr6, _wasm5, _guard5, guard_fn5; let wasm; const rewriters = new Map(); const __c__ = Symbol("lhc"); const handlers = new WeakMap(); const callbacks = new WeakMap(); { const module2 = new WebAssembly.Module(require('fs').readFileSync(require('path').join(__dirname, '../../src/html/html.wasm'))); const instance = new WebAssembly.Instance(module2, { io: { drop(id) { const x = rewriters.get(id); handlers.delete(x), callbacks.delete(x), rewriters.delete(id); } }, env: { chunk(id, ptr) { callbacks.get(rewriters.get(id))(mem.u8(ptr, mem.length()).slice()); }, element(id, kind, index, ptr) { const r = rewriters.get(id); const h = handlers.get(r).on[index]; try { if (kind === 0) { const text = new Text(ptr, wasm); h.text(text), text[__c__](); } else if (kind === 1) { const element = new Element(ptr, wasm); h.element(element), element[__c__](); } else if (kind === 2) { const comment = new Comment(ptr, wasm); h.comments(comment), comment[__c__](); } } catch (err) { return rewriters.get(id).error = err, 0; } return 1; }, document(id, kind, index, ptr) { const r = rewriters.get(id); const h = handlers.get(r).document[index]; try { if (kind === 0) { const end = new End(ptr, wasm); h.end(end), end[__c__](); } else if (kind === 2) { const text = new Text(ptr, wasm); h.text(text), text[__c__](); } else if (kind === 1) { const doctype = new Doctype(ptr, wasm); h.doctype(doctype), doctype[__c__](); } else if (kind === 3) { const comment = new Comment(ptr, wasm); h.comments(comment), comment[__c__](); } } catch (err) { return rewriters.get(id).error = err, 0; } return 1; } } }); wasm = instance.exports; } class mem { static length() { return wasm.wlen(); } static token() { return wasm.wtoken(); } static alloc(size) { return wasm.walloc(size); } static free(ptr, size) { return wasm.wfree(ptr, size); } static u8(ptr, size) { return new Uint8Array(wasm.memory.buffer, ptr, size); } static u32(ptr, size) { return new Uint32Array(wasm.memory.buffer, ptr, size); } static gc(f) { return !("FinalizationRegistry" in globalThis) ? { delete(_) { }, add(_, __) { } } : { r: new FinalizationRegistry(f), delete(k) { this.r.unregister(k); }, add(k, v) { this.r.register(k, v, k); } }; } static copy_and_free(ptr, size) { let slice = mem.u8(ptr, size).slice(); return wasm.wfree(ptr, size), slice; } } const gc = mem.gc((ptr) => wasm.free(ptr)); const decode_utf16 = Function.prototype.apply.bind(String.fromCharCode, null); const decode_utf8 = (_c = (_b = (_a = globalThis.Deno) == null ? void 0 : _a.core) == null ? void 0 : _b.decode) != null ? _c : TextDecoder.prototype.decode.bind(new TextDecoder()); const encode_ascii = (x) => { const l = x.length; const u8 = new Uint8Array(l); for (let o = 0; o < l; o++) u8[o] = x.charCodeAt(o); return u8; }; const encode_utf16 = (x) => { const l = x.length; const u16 = new Uint16Array(l); for (let o = 0; o < l; o++) u16[o] = x.charCodeAt(o); return u16; }; const encode_utf8 = (_h = (_g = (_e = (_d = globalThis.Deno) == null ? void 0 : _d.core) == null ? void 0 : _e.encode) != null ? _g : (_f = globalThis.Buffer) == null ? void 0 : _f.from.bind(globalThis.Buffer)) != null ? _h : TextEncoder.prototype.encode.bind(new TextEncoder()); class Rewriter { constructor(fn) { __privateAdd(this, _init); __privateAdd(this, _ptr, 0); __privateAdd(this, _t, null); callbacks.set(this, fn); handlers.set(this, { on: [], document: [] }); } drop() { gc.delete(this); __privateSet(this, _ptr, wasm.free(__privateGet(this, _ptr))); } end() { if (__privateGet(this, _ptr) === 0) __privateMethod(this, _init, init_fn).call(this); else if (__privateGet(this, _ptr) == null) return; const c = wasm.end(__privateGet(this, _ptr)); gc.delete(this); __privateSet(this, _ptr, null); if (c === 3) throw this.error; if (c === 2) throw new Error("html: memory limit has been exceeded"); if (c === 1) throw new Error("html: parser has encountered a text content tag in the context where it is ambiguous whether this tag should be ignored or not"); } write(chunk) { if (__privateGet(this, _ptr) === 0) __privateMethod(this, _init, init_fn).call(this); else if (__privateGet(this, _ptr) == null) throw new Error("html: rewriter is ended"); if (typeof chunk === "string") chunk = encode_utf8(chunk); const ptr = mem.alloc(chunk.length); mem.u8(ptr, chunk.length).set(chunk); const c = wasm.write(__privateGet(this, _ptr), ptr, chunk.length); if (c === 3) throw this.drop(), this.error; if (c === 2) throw this.drop(), new Error("html: memory limit has been exceeded"); if (c === 1) throw this.drop(), new Error("html: parser has encountered a text content tag in the context where it is ambiguous whether this tag should be ignored or not"); } on(selector, handler) { if (__privateGet(this, _ptr) !== 0) throw new Error("html: cannot add handler after writing"); const buf = encode_utf8(selector); const ptr = mem.alloc(buf.length); mem.u8(ptr, buf.length).set(buf); const c = wasm.validate_selector(ptr, buf.length); if (c !== 0) throw new SyntaxError(`html: ${decode_utf8(mem.u8(c, mem.length()))}`); const h = handlers.get(this).on; h.push(handler); const meta = handler.meta = []; if ("text" in handler) meta.push({ kind: 0, name: selector, index: h.length - 1 }); if ("element" in handler) meta.push({ kind: 1, name: selector, index: h.length - 1 }); if ("comments" in handler) meta.push({ kind: 2, name: selector, index: h.length - 1 }); } onDocument(handler) { if (__privateGet(this, _ptr) !== 0) throw new Error("html: cannot add handler after writing"); const h = handlers.get(this).document; h.push(handler); const meta = handler.meta = []; if ("end" in handler) meta.push({ kind: 0, index: h.length - 1 }); if ("text" in handler) meta.push({ kind: 2, index: h.length - 1 }); if ("doctype" in handler) meta.push({ kind: 1, index: h.length - 1 }); if ("comments" in handler) meta.push({ kind: 3, index: h.length - 1 }); } } _ptr = new WeakMap(); _t = new WeakMap(); _init = new WeakSet(); init_fn = function() { const h = handlers.get(this); rewriters.set(__privateSet(this, _t, mem.token()), this); const json = encode_utf8(JSON.stringify({ strict: true, element_handlers: h.on.map((x) => x.meta).flat(), document_handlers: h.document.map((x) => x.meta).flat() })); const ptr = mem.alloc(json.length); mem.u8(ptr, json.length).set(json); gc.add(this, __privateSet(this, _ptr, wasm.new(__privateGet(this, _t), callbacks.get(this) ? 1 : 0, ptr, json.length))); }; class End { constructor(ptr, wasm2) { __privateAdd(this, _guard); __privateAdd(this, _ptr2, 0); __privateAdd(this, _wasm, null); __privateSet(this, _ptr2, ptr); __privateSet(this, _wasm, wasm2); } [__c__]() { __privateSet(this, _ptr2, __privateSet(this, _wasm, null)); } append(content, options = {}) { __privateMethod(this, _guard, guard_fn).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm).end_append(__privateGet(this, _ptr2), ptr, buffer.length, options.html ? 1 : 0); return this; } } _ptr2 = new WeakMap(); _wasm = new WeakMap(); _guard = new WeakSet(); guard_fn = function() { if (!__privateGet(this, _ptr2)) throw new Error("html: using html content past its handler return is forbidden"); }; class Doctype { constructor(ptr, wasm2) { __privateAdd(this, _guard2); __privateAdd(this, _ptr3, 0); __privateAdd(this, _wasm2, null); __privateSet(this, _ptr3, ptr); __privateSet(this, _wasm2, wasm2); } [__c__]() { __privateSet(this, _ptr3, __privateSet(this, _wasm2, null)); } get name() { __privateMethod(this, _guard2, guard_fn2).call(this); const ptr = __privateGet(this, _wasm2).doctype_name(__privateGet(this, _ptr3)); if (!ptr) return null; const str = decode_utf8(mem.u8(ptr, mem.length())); return mem.free(ptr, mem.length()), str; } get publicId() { __privateMethod(this, _guard2, guard_fn2).call(this); const ptr = __privateGet(this, _wasm2).doctype_public_id(__privateGet(this, _ptr3)); if (!ptr) return null; const str = decode_utf8(mem.u8(ptr, mem.length())); return mem.free(ptr, mem.length()), str; } get systemId() { __privateMethod(this, _guard2, guard_fn2).call(this); const ptr = __privateGet(this, _wasm2).doctype_system_id(__privateGet(this, _ptr3)); if (!ptr) return null; const str = decode_utf8(mem.u8(ptr, mem.length())); return mem.free(ptr, mem.length()), str; } } _ptr3 = new WeakMap(); _wasm2 = new WeakMap(); _guard2 = new WeakSet(); guard_fn2 = function() { if (!__privateGet(this, _ptr3)) throw new Error("html: using html content past its handler return is forbidden"); }; class Comment { constructor(ptr, wasm2) { __privateAdd(this, _guard3); __privateAdd(this, _ptr4, 0); __privateAdd(this, _wasm3, null); __privateSet(this, _ptr4, ptr); __privateSet(this, _wasm3, wasm2); } [__c__]() { __privateSet(this, _ptr4, __privateSet(this, _wasm3, null)); } remove() { __privateMethod(this, _guard3, guard_fn3).call(this); __privateGet(this, _wasm3).comment_remove(__privateGet(this, _ptr4)); return this; } get removed() { __privateMethod(this, _guard3, guard_fn3).call(this); return !!__privateGet(this, _wasm3).comment_removed(__privateGet(this, _ptr4)); } get text() { __privateMethod(this, _guard3, guard_fn3).call(this); const ptr = __privateGet(this, _wasm3).comment_text_get(__privateGet(this, _ptr4)); const str = decode_utf8(mem.u8(ptr, mem.length())); return mem.free(ptr, mem.length()), str; } set text(content) { __privateMethod(this, _guard3, guard_fn3).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); const c = __privateGet(this, _wasm3).comment_text_set(__privateGet(this, _ptr4), ptr, buffer.length); if (c === 2) throw new Error("html: comment text shouldn't contain comment closing sequence (`-->`)"); if (c === 1) throw new Error("html: comment text contains a character that can't be represented in utf8"); } after(content, options = {}) { __privateMethod(this, _guard3, guard_fn3).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm3).comment_after(__privateGet(this, _ptr4), ptr, buffer.length, options.html ? 1 : 0); return this; } before(content, options = {}) { __privateMethod(this, _guard3, guard_fn3).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm3).comment_before(__privateGet(this, _ptr4), ptr, buffer.length, options.html ? 1 : 0); return this; } replace(content, options = {}) { __privateMethod(this, _guard3, guard_fn3).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm3).comment_replace(__privateGet(this, _ptr4), ptr, buffer.length, options.html ? 1 : 0); return this; } } _ptr4 = new WeakMap(); _wasm3 = new WeakMap(); _guard3 = new WeakSet(); guard_fn3 = function() { if (!__privateGet(this, _ptr4)) throw new Error("html: using html content past its handler return is forbidden"); }; class Text { constructor(ptr, wasm2) { __privateAdd(this, _guard4); __privateAdd(this, _ptr5, 0); __privateAdd(this, _wasm4, null); __privateSet(this, _ptr5, ptr); __privateSet(this, _wasm4, wasm2); } [__c__]() { __privateSet(this, _ptr5, __privateSet(this, _wasm4, null)); } remove() { __privateMethod(this, _guard4, guard_fn4).call(this); __privateGet(this, _wasm4).text_remove(__privateGet(this, _ptr5)); return this; } get removed() { __privateMethod(this, _guard4, guard_fn4).call(this); return !!__privateGet(this, _wasm4).text_removed(__privateGet(this, _ptr5)); } get lastInTextNode() { __privateMethod(this, _guard4, guard_fn4).call(this); return !!__privateGet(this, _wasm4).text_last_in_text_node(__privateGet(this, _ptr5)); } get text() { __privateMethod(this, _guard4, guard_fn4).call(this); const ptr = __privateGet(this, _wasm4).text_text_get(__privateGet(this, _ptr5)); return ptr === 0 ? "" : decode_utf8(mem.u8(ptr, mem.length())); } get buffer() { __privateMethod(this, _guard4, guard_fn4).call(this); const ptr = __privateGet(this, _wasm4).text_text_get(__privateGet(this, _ptr5)); return ptr === 0 ? new Uint8Array(0) : mem.u8(ptr, mem.length()); } after(content, options = {}) { __privateMethod(this, _guard4, guard_fn4).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm4).text_after(__privateGet(this, _ptr5), ptr, buffer.length, options.html ? 1 : 0); return this; } before(content, options = {}) { __privateMethod(this, _guard4, guard_fn4).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm4).text_before(__privateGet(this, _ptr5), ptr, buffer.length, options.html ? 1 : 0); return this; } replace(content, options = {}) { __privateMethod(this, _guard4, guard_fn4).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm4).text_replace(__privateGet(this, _ptr5), ptr, buffer.length, options.html ? 1 : 0); return this; } } _ptr5 = new WeakMap(); _wasm4 = new WeakMap(); _guard4 = new WeakSet(); guard_fn4 = function() { if (!__privateGet(this, _ptr5)) throw new Error("html: using html content past its handler return is forbidden"); }; class Element { constructor(ptr, wasm2) { __privateAdd(this, _guard5); __privateAdd(this, _ptr6, 0); __privateAdd(this, _wasm5, null); __privateSet(this, _ptr6, ptr); __privateSet(this, _wasm5, wasm2); } [__c__]() { __privateSet(this, _ptr6, __privateSet(this, _wasm5, null)); } remove() { __privateMethod(this, _guard5, guard_fn5).call(this); __privateGet(this, _wasm5).element_remove(__privateGet(this, _ptr6)); return this; } get removed() { __privateMethod(this, _guard5, guard_fn5).call(this); return !!__privateGet(this, _wasm5).element_removed(__privateGet(this, _ptr6)); } removeAndKeepContent() { __privateMethod(this, _guard5, guard_fn5).call(this); __privateGet(this, _wasm5).element_remove_and_keep_content(__privateGet(this, _ptr6)); return this; } get namespaceURI() { __privateMethod(this, _guard5, guard_fn5).call(this); return decode_utf8(mem.u8(__privateGet(this, _wasm5).element_namespace_uri(__privateGet(this, _ptr6)), mem.length())); } get tagName() { __privateMethod(this, _guard5, guard_fn5).call(this); const ptr = __privateGet(this, _wasm5).element_tag_name(__privateGet(this, _ptr6)); const str = decode_utf8(mem.u8(ptr, mem.length())); return mem.free(ptr, mem.length()), str; } get attributes() { __privateMethod(this, _guard5, guard_fn5).call(this); const ptr = __privateGet(this, _wasm5).element_attributes(__privateGet(this, _ptr6)); const attributes = JSON.parse(decode_utf8(mem.u8(ptr, mem.length()))); return mem.free(ptr, mem.length()), attributes.values(); } removeAttribute(name) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(name); const bptr = mem.alloc(buffer.length); mem.u8(bptr, buffer.length).set(buffer); __privateGet(this, _wasm5).element_remove_attribute(__privateGet(this, _ptr6), bptr, buffer.length); return this; } hasAttribute(name) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(name); const bptr = mem.alloc(buffer.length); mem.u8(bptr, buffer.length).set(buffer); return !!__privateGet(this, _wasm5).element_has_attribute(__privateGet(this, _ptr6), bptr, buffer.length); } getAttribute(name) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(name); const bptr = mem.alloc(buffer.length); mem.u8(bptr, buffer.length).set(buffer); const ptr = __privateGet(this, _wasm5).element_get_attribute(__privateGet(this, _ptr6), bptr, buffer.length); if (!ptr) return null; const len = mem.length(); const str = decode_utf8(mem.u8(ptr, len)); return mem.free(ptr, len), str; } after(content, options = {}) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm5).element_after(__privateGet(this, _ptr6), ptr, buffer.length, options.html ? 1 : 0); return this; } append(content, options = {}) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm5).element_append(__privateGet(this, _ptr6), ptr, buffer.length, options.html ? 1 : 0); return this; } before(content, options = {}) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm5).element_before(__privateGet(this, _ptr6), ptr, buffer.length, options.html ? 1 : 0); return this; } prepend(content, options = {}) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm5).element_prepend(__privateGet(this, _ptr6), ptr, buffer.length, options.html ? 1 : 0); return this; } replace(content, options = {}) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm5).element_replace(__privateGet(this, _ptr6), ptr, buffer.length, options.html ? 1 : 0); return this; } setInnerContent(content, options = {}) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); __privateGet(this, _wasm5).element_set_inner_content(__privateGet(this, _ptr6), ptr, buffer.length, options.html ? 1 : 0); return this; } set tagName(content) { __privateMethod(this, _guard5, guard_fn5).call(this); const buffer = encode_utf8(content); const ptr = mem.alloc(buffer.length); mem.u8(ptr, buffer.length).set(buffer); const c = __privateGet(this, _wasm5).element_set_tag_name(__privateGet(this, _ptr6), ptr, buffer.length); if (c === 1) throw new Error("html: tag name can't be empty"); if (c === 3) throw new Error("html: forbidden character is in the tag name"); if (c === 2) throw new Error("html: tag name contains a character that can't be represented in utf8"); if (c === 4) throw new Error("html: first character of the tag name should be an ASCII alphabetical character"); } setAttribute(name, value) { __privateMethod(this, _guard5, guard_fn5).call(this); const v = encode_utf8(value); const key = encode_utf8(name); const vptr = mem.alloc(v.length); const kptr = mem.alloc(key.length); mem.u8(vptr, v.length).set(v); mem.u8(kptr, key.length).set(key); const c = __privateGet(this, _wasm5).element_set_attribute(__privateGet(this, _ptr6), kptr, key.length, vptr, v.length); if (c === 1) throw new Error("html: attribute name can't be empty"); if (c === 3) throw new Error("html: forbidden character is in the attribute name"); if (c === 2) throw new Error("html: attribute name contains a character that can't be represented in utf8"); return this; } } _ptr6 = new WeakMap(); _wasm5 = new WeakMap(); _guard5 = new WeakSet(); guard_fn5 = function() { if (!__privateGet(this, _ptr6)) throw new Error("html: using html content past its handler return is forbidden"); };