update: 更新bobj demo版本
This commit is contained in:
1 parent
01fb8bb7c2
commit
0267fac3a1
8 files changed
+6
-24
No files matched your search
@@ -0,0 +1 @@
|
||||
(function(){"use strict";var y=e=>{throw TypeError(e)},g=(e,t,r)=>t.has(e)||y("Cannot "+r),l=(e,t,r)=>(g(e,t,"read from private field"),r?r.call(e):t.get(e)),w=(e,t,r)=>t.has(e)?y("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(e):t.set(e,r);const c=new WeakMap,f=e=>{if(e instanceof Uint8Array)return e.length;{if(c.has(e))return c.get(e);const t=e.reduce((r,n)=>r+f(n),0);return c.set(e,t),t}},h=(e,t)=>{t=t||1;const r=[];for(;e>0||t>0;)r.unshift(e&255),e=e>>8,t--;return new Uint8Array(r)},p=e=>{const{key:t,valueType:r,value:n,textEncoder:i=new TextEncoder}=e,a=i.encode(t),s=h(f(n));return[new Uint8Array([a.length,r.length,s.length]),a,r,s,n]};function A(e){const t=new ArrayBuffer(8);return new DataView(t).setFloat64(0,e,!0),new Uint8Array(Array.from(new Uint8Array(t)))}const u=async e=>e instanceof Promise?await e:e,U=[{filter:e=>e instanceof Object,targetType:new Uint8Array([0]),async serialize(e){const t=new TextEncoder,r=[];for(const n in e.target){const i=e.target[n],a=await e.serializer.filterPlugin(i),s=a.targetType;if(!s)throw new Error("Unknown value type");const v=await u(a.serialize({target:i,serializer:e.serializer}))??new Uint8Array(0);r.push(p({key:n,valueType:s,value:v,textEncoder:t}))}return r}},{filter:e=>e instanceof Array,targetType:new Uint8Array([1]),async serialize(e){const t={...e.target,l:e.target.length};return await e.serializer.serialize(t)}},{filter:e=>e instanceof Uint8Array,targetType:new Uint8Array([2]),serialize(e){return e.target}},{filter(e){return typeof e=="string"},targetType:new Uint8Array([3]),serialize(e){return(e.textEncoder||new TextEncoder).encode(e.target)}},{filter(e){return typeof e=="number"},targetType:new Uint8Array([4]),serialize(e){return A(e.target)}},{filter(e){return typeof e=="boolean"},targetType:new Uint8Array([5]),serialize(e){return new Uint8Array([e.target?1:0])}},{filter(e){return typeof e>"u"},targetType:new Uint8Array([6]),serialize(){return new Uint8Array(0)}},{filter(e){return e===null},targetType:new Uint8Array([7]),serialize(){return new Uint8Array(0)}}];function z(e,t){const r=t??e.reduce((a,s)=>a+s.length,0),n=new Uint8Array(r);let i=0;for(const a of e)n.set(a,i),i+=a.length;return n}var o;class d{constructor(){w(this,o,[]),U.forEach(t=>{this.registerPlugin(t)})}async serialize(t){for(const r of l(this,o))if(r.filter(t)){const n=await u(r.serialize({target:t,serializer:this}));return n instanceof Uint8Array?n:z(n.flat(1/0),f(n))}}async filterPlugin(t){for(const r of l(this,o))if(r.filter(t))return r}registerPlugin(t){l(this,o).unshift(t)}}o=new WeakMap;const T=new d;self.onmessage=async e=>{const t=e.data,r=await T.serialize(t);self.postMessage(r)}})();
|
||||
Reference in new issue
Block a user