diff --git a/packages/server/package.json b/packages/server/package.json index 0b7e11793c..1c7731183c 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -14,7 +14,8 @@ "postbuild": "copyfiles -f ../client/dist/budibase-client.js ../client/manifest.json client && copyfiles -f ../../yarn.lock ./dist/", "check:types": "tsc -p tsconfig.json --noEmit --paths null", "build:isolated-vm-lib:string-templates": "esbuild --minify --bundle src/jsRunner/bundles/index-helpers.ts --outfile=src/jsRunner/bundles/index-helpers.ivm.bundle.js --platform=node --format=esm --external:handlebars", - "build:isolated-vm-libs": "yarn build:isolated-vm-lib:string-templates", + "build:isolated-vm-lib:bson": "esbuild --minify --bundle src/jsRunner/bundles/bsonPackage.ts --outfile=src/jsRunner/bundles/bson.ivm.bundle.js --platform=node --format=esm", + "build:isolated-vm-libs": "yarn build:isolated-vm-lib:string-templates && yarn build:isolated-vm-lib:bson", "build:dev": "yarn prebuild && tsc --build --watch --preserveWatchOutput", "debug": "yarn build && node --expose-gc --inspect=9222 dist/index.js", "jest": "NODE_OPTIONS=\"--no-node-snapshot $NODE_OPTIONS\" jest", diff --git a/packages/server/src/jsRunner/bundles/bson.ivm.bundle.js b/packages/server/src/jsRunner/bundles/bson.ivm.bundle.js new file mode 100644 index 0000000000..083b637a53 --- /dev/null +++ b/packages/server/src/jsRunner/bundles/bson.ivm.bundle.js @@ -0,0 +1,7 @@ +function kt(e){return["[object ArrayBuffer]","[object SharedArrayBuffer]"].includes(Object.prototype.toString.call(e))}function Lt(e){return Object.prototype.toString.call(e)==="[object Uint8Array]"}function qt(e){return Object.prototype.toString.call(e)==="[object RegExp]"}function Rt(e){return Object.prototype.toString.call(e)==="[object Map]"}function Zt(e){return Object.prototype.toString.call(e)==="[object Date]"}function j(e,t){return JSON.stringify(e,(n,s)=>typeof s=="bigint"?{$numberLong:`${s}`}:Rt(s)?Object.fromEntries(s):s)}function Kt(e){if(e!=null&&typeof e=="object"&&"stylize"in e&&typeof e.stylize=="function")return e.stylize}var mt=6,Dt=2147483647,jt=-2147483648,zt=Math.pow(2,63)-1,Ft=-Math.pow(2,63),Gt=Math.pow(2,53),Xt=-Math.pow(2,53),wt=1,Qt=2,vt=3,te=4,ee=5,ne=6,se=7,re=8,ie=9,oe=10,pt=11,fe=12,le=13,ce=14,he=15,St=16,ae=17,ge=18,ue=19,ye=255,me=127;var ct=4,cn=Object.freeze({double:1,string:2,object:3,array:4,binData:5,undefined:6,objectId:7,bool:8,date:9,null:10,regex:11,dbPointer:12,javascript:13,symbol:14,javascriptWithScope:15,int:16,timestamp:17,long:18,decimal:19,minKey:-1,maxKey:127}),o=class extends Error{get bsonError(){return!0}get name(){return"BSONError"}constructor(t,n){super(t,n)}static isBSONError(t){return t!=null&&typeof t=="object"&&"bsonError"in t&&t.bsonError===!0&&"name"in t&&"message"in t&&"stack"in t}},yt=class extends o{get name(){return"BSONVersionError"}constructor(){super(`Unsupported BSON version, bson types must be from bson ${mt}.x.x`)}},ht=class extends o{get name(){return"BSONRuntimeError"}constructor(t){super(t)}},we=128,pe=192,Se=224,Ne=240,Be=248,Oe=192,be=224,Ee=240,Te=128;function Mt(e,t,n){let s=0;for(let r=t;r20)return null;if(s===1&&e[t]<128)return String.fromCharCode(e[t]);if(s===2&&e[t]<128&&e[t+1]<128)return String.fromCharCode(e[t])+String.fromCharCode(e[t+1]);if(s===3&&e[t]<128&&e[t+1]<128&&e[t+2]<128)return String.fromCharCode(e[t])+String.fromCharCode(e[t+1])+String.fromCharCode(e[t+2]);let r=[];for(let i=t;i127)return null;r.push(u)}return String.fromCharCode(...r)}function Ie(e){return J.fromNumberArray(Array.from({length:e},()=>Math.floor(Math.random()*256)))}var de=await(async()=>{try{return(await import("crypto")).randomBytes}catch{return Ie}})(),J={toLocalBufferType(e){if(Buffer.isBuffer(e))return e;if(ArrayBuffer.isView(e))return Buffer.from(e.buffer,e.byteOffset,e.byteLength);let t=e?.[Symbol.toStringTag]??Object.prototype.toString.call(e);if(t==="ArrayBuffer"||t==="SharedArrayBuffer"||t==="[object ArrayBuffer]"||t==="[object SharedArrayBuffer]")return Buffer.from(e);throw new o(`Cannot create Buffer from ${String(e)}`)},allocate(e){return Buffer.alloc(e)},equals(e,t){return J.toLocalBufferType(e).equals(t)},fromNumberArray(e){return Buffer.from(e)},fromBase64(e){return Buffer.from(e,"base64")},toBase64(e){return J.toLocalBufferType(e).toString("base64")},fromISO88591(e){return Buffer.from(e,"binary")},toISO88591(e){return J.toLocalBufferType(e).toString("binary")},fromHex(e){return Buffer.from(e,"hex")},toHex(e){return J.toLocalBufferType(e).toString("hex")},fromUTF8(e){return Buffer.from(e,"utf8")},toUTF8(e,t,n,s){let r=n-t<=20?xt(e,t,n):null;if(r!=null)return r;let i=J.toLocalBufferType(e).toString("utf8",t,n);if(s){for(let u=0;uMath.floor(Math.random()*256)))}var _e=(()=>{let{crypto:e}=globalThis;if(e!=null&&typeof e.getRandomValues=="function")return t=>e.getRandomValues(G.allocate(t));if($e()){let{console:t}=globalThis;t?.warn?.("BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.")}return Ae})(),Nt=/(\d|[a-f])/i,G={toLocalBufferType(e){let t=e?.[Symbol.toStringTag]??Object.prototype.toString.call(e);if(t==="Uint8Array")return e;if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength));if(t==="ArrayBuffer"||t==="SharedArrayBuffer"||t==="[object ArrayBuffer]"||t==="[object SharedArrayBuffer]")return new Uint8Array(e);throw new o(`Cannot make a Uint8Array from ${String(e)}`)},allocate(e){if(typeof e!="number")throw new TypeError(`The "size" argument must be of type number. Received ${String(e)}`);return new Uint8Array(e)},equals(e,t){if(e.byteLength!==t.byteLength)return!1;for(let n=0;nt.charCodeAt(0))},toBase64(e){return btoa(G.toISO88591(e))},fromISO88591(e){return Uint8Array.from(e,t=>t.charCodeAt(0)&255)},toISO88591(e){return Array.from(Uint16Array.from(e),t=>String.fromCharCode(t)).join("")},fromHex(e){let t=e.length%2===0?e:e.slice(0,e.length-1),n=[];for(let s=0;st.toString(16).padStart(2,"0")).join("")},fromUTF8(e){return new TextEncoder().encode(e)},toUTF8(e,t,n,s){let r=n-t<=20?xt(e,t,n):null;if(r!=null)return r;if(s)try{return new TextDecoder("utf8",{fatal:s}).decode(e.slice(t,n))}catch(i){throw new o("Invalid UTF-8 string in BSON document",{cause:i})}return new TextDecoder("utf8",{fatal:s}).decode(e.slice(t,n))},utf8ByteLength(e){return G.fromUTF8(e).byteLength},encodeUTF8Into(e,t,n){let s=G.fromUTF8(t);return e.set(s,n),s.byteLength},randomBytes:_e},Ue=typeof Buffer=="function"&&Buffer.prototype?._isBuffer!==!0,h=Ue?J:G,X=class extends DataView{static fromUint8Array(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}},D=class{get[Symbol.for("@@mdb.bson.version")](){return mt}[Symbol.for("nodejs.util.inspect.custom")](t,n,s){return this.inspect(t,n,s)}},_=class e extends D{get _bsontype(){return"Binary"}constructor(t,n){if(super(),t!=null&&typeof t=="string"&&!ArrayBuffer.isView(t)&&!kt(t)&&!Array.isArray(t))throw new o("Binary can only be constructed from Uint8Array or number[]");this.sub_type=n??e.BSON_BINARY_SUBTYPE_DEFAULT,t==null?(this.buffer=h.allocate(e.BUFFER_SIZE),this.position=0):(this.buffer=Array.isArray(t)?h.fromNumberArray(t):h.toLocalBufferType(t),this.position=this.buffer.byteLength)}put(t){if(typeof t=="string"&&t.length!==1)throw new o("only accepts single character String");if(typeof t!="number"&&t.length!==1)throw new o("only accepts single character Uint8Array or Array");let n;if(typeof t=="string"?n=t.charCodeAt(0):typeof t=="number"?n=t:n=t[0],n<0||n>255)throw new o("only accepts number in a valid unsigned byte range 0-255");if(this.buffer.byteLength>this.position)this.buffer[this.position++]=n;else{let s=h.allocate(e.BUFFER_SIZE+this.buffer.length);s.set(this.buffer,0),this.buffer=s,this.buffer[this.position++]=n}}write(t,n){if(n=typeof n=="number"?n:this.position,this.buffer.byteLengththis.position?n+t.length:this.position;else if(typeof t=="string")throw new o("input cannot be string")}read(t,n){return n=n&&n>0?n:this.position,this.buffer.slice(t,t+n)}value(){return this.buffer.length===this.position?this.buffer:this.buffer.subarray(0,this.position)}length(){return this.position}toJSON(){return h.toBase64(this.buffer)}toString(t){return t==="hex"?h.toHex(this.buffer):t==="base64"?h.toBase64(this.buffer):t==="utf8"||t==="utf-8"?h.toUTF8(this.buffer,0,this.buffer.byteLength,!1):h.toUTF8(this.buffer,0,this.buffer.byteLength,!1)}toExtendedJSON(t){t=t||{};let n=h.toBase64(this.buffer),s=Number(this.sub_type).toString(16);return t.legacy?{$binary:n,$type:s.length===1?"0"+s:s}:{$binary:{base64:n,subType:s.length===1?"0"+s:s}}}toUUID(){if(this.sub_type===e.SUBTYPE_UUID)return new W(this.buffer.slice(0,this.position));throw new o(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${e.SUBTYPE_UUID}" is currently supported.`)}static createFromHexString(t,n){return new e(h.fromHex(t),n)}static createFromBase64(t,n){return new e(h.fromBase64(t),n)}static fromExtendedJSON(t,n){n=n||{};let s,r;if("$binary"in t?n.legacy&&typeof t.$binary=="string"&&"$type"in t?(r=t.$type?parseInt(t.$type,16):0,s=h.fromBase64(t.$binary)):typeof t.$binary!="string"&&(r=t.$binary.subType?parseInt(t.$binary.subType,16):0,s=h.fromBase64(t.$binary.base64)):"$uuid"in t&&(r=4,s=W.bytesFromString(t.$uuid)),!s)throw new o(`Unexpected Binary Extended JSON format ${JSON.stringify(t)}`);return r===ct?new W(s):new e(s,r)}inspect(t,n,s){s??=j;let r=h.toBase64(this.buffer.subarray(0,this.position)),i=s(r,n),u=s(this.sub_type,n);return`Binary.createFromBase64(${i}, ${u})`}};_.BSON_BINARY_SUBTYPE_DEFAULT=0;_.BUFFER_SIZE=256;_.SUBTYPE_DEFAULT=0;_.SUBTYPE_FUNCTION=1;_.SUBTYPE_BYTE_ARRAY=2;_.SUBTYPE_UUID_OLD=3;_.SUBTYPE_UUID=4;_.SUBTYPE_MD5=5;_.SUBTYPE_ENCRYPTED=6;_.SUBTYPE_COLUMN=7;_.SUBTYPE_USER_DEFINED=128;var gt=16,Le=/^[0-9A-F]{32}$/i,Re=/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,W=class e extends _{constructor(t){let n;if(t==null)n=e.generate();else if(t instanceof e)n=h.toLocalBufferType(new Uint8Array(t.buffer));else if(ArrayBuffer.isView(t)&&t.byteLength===gt)n=h.toLocalBufferType(t);else if(typeof t=="string")n=e.bytesFromString(t);else throw new o("Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).");super(n,ct)}get id(){return this.buffer}set id(t){this.buffer=t}toHexString(t=!0){return t?[h.toHex(this.buffer.subarray(0,4)),h.toHex(this.buffer.subarray(4,6)),h.toHex(this.buffer.subarray(6,8)),h.toHex(this.buffer.subarray(8,10)),h.toHex(this.buffer.subarray(10,16))].join("-"):h.toHex(this.buffer)}toString(t){return t==="hex"?h.toHex(this.id):t==="base64"?h.toBase64(this.id):this.toHexString()}toJSON(){return this.toHexString()}equals(t){if(!t)return!1;if(t instanceof e)return h.equals(t.id,this.id);try{return h.equals(new e(t).id,this.id)}catch{return!1}}toBinary(){return new _(this.id,_.SUBTYPE_UUID)}static generate(){let t=h.randomBytes(gt);return t[6]=t[6]&15|64,t[8]=t[8]&63|128,t}static isValid(t){return t?typeof t=="string"?e.isValidUUIDString(t):Lt(t)?t.byteLength===gt:t._bsontype==="Binary"&&t.sub_type===this.SUBTYPE_UUID&&t.buffer.byteLength===16:!1}static createFromHexString(t){let n=e.bytesFromString(t);return new e(n)}static createFromBase64(t){return new e(h.fromBase64(t))}static bytesFromString(t){if(!e.isValidUUIDString(t))throw new o("UUID string representation must be 32 hex digits or canonical hyphenated representation");return h.fromHex(t.replace(/-/g,""))}static isValidUUIDString(t){return Le.test(t)||Re.test(t)}inspect(t,n,s){return s??=j,`new UUID(${s(this.toHexString(),n)})`}},Y=class e extends D{get _bsontype(){return"Code"}constructor(t,n){super(),this.code=t.toString(),this.scope=n??null}toJSON(){return this.scope!=null?{code:this.code,scope:this.scope}:{code:this.code}}toExtendedJSON(){return this.scope?{$code:this.code,$scope:this.scope}:{$code:this.code}}static fromExtendedJSON(t){return new e(t.$code,t.$scope)}inspect(t,n,s){s??=j;let r=s(this.code,n),i=r.includes(` +`);this.scope!=null&&(r+=`,${i?` +`:" "}${s(this.scope,n)}`);let u=i&&this.scope===null;return`new Code(${i?` +`:""}${r}${u?` +`:""})`}};function Ht(e){return e!=null&&typeof e=="object"&&"$id"in e&&e.$id!=null&&"$ref"in e&&typeof e.$ref=="string"&&(!("$db"in e)||"$db"in e&&typeof e.$db=="string")}var P=class e extends D{get _bsontype(){return"DBRef"}constructor(t,n,s,r){super();let i=t.split(".");i.length===2&&(s=i.shift(),t=i.shift()),this.collection=t,this.oid=n,this.db=s,this.fields=r||{}}get namespace(){return this.collection}set namespace(t){this.collection=t}toJSON(){let t=Object.assign({$ref:this.collection,$id:this.oid},this.fields);return this.db!=null&&(t.$db=this.db),t}toExtendedJSON(t){t=t||{};let n={$ref:this.collection,$id:this.oid};return t.legacy||(this.db&&(n.$db=this.db),n=Object.assign(n,this.fields)),n}static fromExtendedJSON(t){let n=Object.assign({},t);return delete n.$ref,delete n.$id,delete n.$db,new e(t.$ref,t.$id,t.$db,n)}inspect(t,n,s){s??=j;let r=[s(this.namespace,n),s(this.oid,n),...this.db?[s(this.db,n)]:[],...Object.keys(this.fields).length>0?[s(this.fields,n)]:[]];return r[1]=s===j?`new ObjectId(${r[1]})`:r[1],`new DBRef(${r.join(", ")})`}},z;try{z=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}var Bt=65536,De=1<<24,K=Bt*Bt,Ct=K*K,Ot=Ct/2,bt={},Et={},je=20,ze=/^(\+?0|(\+|-)?[1-9][0-9]*)$/,f=class e extends D{get _bsontype(){return"Long"}get __isLong__(){return!0}constructor(t=0,n,s){super(),typeof t=="bigint"?Object.assign(this,e.fromBigInt(t,!!n)):typeof t=="string"?Object.assign(this,e.fromString(t,!!n)):(this.low=t|0,this.high=n|0,this.unsigned=!!s)}static fromBits(t,n,s){return new e(t,n,s)}static fromInt(t,n){let s,r,i;return n?(t>>>=0,(i=0<=t&&t<256)&&(r=Et[t],r)?r:(s=e.fromBits(t,(t|0)<0?-1:0,!0),i&&(Et[t]=s),s)):(t|=0,(i=-128<=t&&t<128)&&(r=bt[t],r)?r:(s=e.fromBits(t,t<0?-1:0,!1),i&&(bt[t]=s),s))}static fromNumber(t,n){if(isNaN(t))return n?e.UZERO:e.ZERO;if(n){if(t<0)return e.UZERO;if(t>=Ct)return e.MAX_UNSIGNED_VALUE}else{if(t<=-Ot)return e.MIN_VALUE;if(t+1>=Ot)return e.MAX_VALUE}return t<0?e.fromNumber(-t,n).neg():e.fromBits(t%K|0,t/K|0,n)}static fromBigInt(t,n){return e.fromString(t.toString(),n)}static fromString(t,n,s){if(t.length===0)throw new o("empty string");if(t==="NaN"||t==="Infinity"||t==="+Infinity"||t==="-Infinity")return e.ZERO;if(typeof n=="number"?(s=n,n=!1):n=!!n,s=s||10,s<2||360)throw new o("interior hyphen");if(r===0)return e.fromString(t.substring(1),n,s).neg();let i=e.fromNumber(Math.pow(s,8)),u=e.ZERO;for(let a=0;a>>16,s=this.high&65535,r=this.low>>>16,i=this.low&65535,u=t.high>>>16,a=t.high&65535,I=t.low>>>16,S=t.low&65535,w=0,m=0,O=0,d=0;return d+=i+S,O+=d>>>16,d&=65535,O+=r+I,m+=O>>>16,O&=65535,m+=s+a,w+=m>>>16,m&=65535,w+=n+u,w&=65535,e.fromBits(O<<16|d,w<<16|m,this.unsigned)}and(t){return e.isLong(t)||(t=e.fromValue(t)),e.fromBits(this.low&t.low,this.high&t.high,this.unsigned)}compare(t){if(e.isLong(t)||(t=e.fromValue(t)),this.eq(t))return 0;let n=this.isNegative(),s=t.isNegative();return n&&!s?-1:!n&&s?1:this.unsigned?t.high>>>0>this.high>>>0||t.high===this.high&&t.low>>>0>this.low>>>0?-1:1:this.sub(t).isNegative()?-1:1}comp(t){return this.compare(t)}divide(t){if(e.isLong(t)||(t=e.fromValue(t)),t.isZero())throw new o("division by zero");if(z){if(!this.unsigned&&this.high===-2147483648&&t.low===-1&&t.high===-1)return this;let i=(this.unsigned?z.div_u:z.div_s)(this.low,this.high,t.low,t.high);return e.fromBits(i,z.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?e.UZERO:e.ZERO;let n,s,r;if(this.unsigned){if(t.unsigned||(t=t.toUnsigned()),t.gt(this))return e.UZERO;if(t.gt(this.shru(1)))return e.UONE;r=e.UZERO}else{if(this.eq(e.MIN_VALUE))return t.eq(e.ONE)||t.eq(e.NEG_ONE)?e.MIN_VALUE:t.eq(e.MIN_VALUE)?e.ONE:(n=this.shr(1).div(t).shl(1),n.eq(e.ZERO)?t.isNegative()?e.ONE:e.NEG_ONE:(s=this.sub(t.mul(n)),r=n.add(s.div(t)),r));if(t.eq(e.MIN_VALUE))return this.unsigned?e.UZERO:e.ZERO;if(this.isNegative())return t.isNegative()?this.neg().div(t.neg()):this.neg().div(t).neg();if(t.isNegative())return this.div(t.neg()).neg();r=e.ZERO}for(s=this;s.gte(t);){n=Math.max(1,Math.floor(s.toNumber()/t.toNumber()));let i=Math.ceil(Math.log(n)/Math.LN2),u=i<=48?1:Math.pow(2,i-48),a=e.fromNumber(n),I=a.mul(t);for(;I.isNegative()||I.gt(s);)n-=u,a=e.fromNumber(n,this.unsigned),I=a.mul(t);a.isZero()&&(a=e.ONE),r=r.add(a),s=s.sub(I)}return r}div(t){return this.divide(t)}equals(t){return e.isLong(t)||(t=e.fromValue(t)),this.unsigned!==t.unsigned&&this.high>>>31===1&&t.high>>>31===1?!1:this.high===t.high&&this.low===t.low}eq(t){return this.equals(t)}getHighBits(){return this.high}getHighBitsUnsigned(){return this.high>>>0}getLowBits(){return this.low}getLowBitsUnsigned(){return this.low>>>0}getNumBitsAbs(){if(this.isNegative())return this.eq(e.MIN_VALUE)?64:this.neg().getNumBitsAbs();let t=this.high!==0?this.high:this.low,n;for(n=31;n>0&&!(t&1<0}gt(t){return this.greaterThan(t)}greaterThanOrEqual(t){return this.comp(t)>=0}gte(t){return this.greaterThanOrEqual(t)}ge(t){return this.greaterThanOrEqual(t)}isEven(){return(this.low&1)===0}isNegative(){return!this.unsigned&&this.high<0}isOdd(){return(this.low&1)===1}isPositive(){return this.unsigned||this.high>=0}isZero(){return this.high===0&&this.low===0}lessThan(t){return this.comp(t)<0}lt(t){return this.lessThan(t)}lessThanOrEqual(t){return this.comp(t)<=0}lte(t){return this.lessThanOrEqual(t)}modulo(t){if(e.isLong(t)||(t=e.fromValue(t)),z){let n=(this.unsigned?z.rem_u:z.rem_s)(this.low,this.high,t.low,t.high);return e.fromBits(n,z.get_high(),this.unsigned)}return this.sub(this.div(t).mul(t))}mod(t){return this.modulo(t)}rem(t){return this.modulo(t)}multiply(t){if(this.isZero())return e.ZERO;if(e.isLong(t)||(t=e.fromValue(t)),z){let x=z.mul(this.low,this.high,t.low,t.high);return e.fromBits(x,z.get_high(),this.unsigned)}if(t.isZero())return e.ZERO;if(this.eq(e.MIN_VALUE))return t.isOdd()?e.MIN_VALUE:e.ZERO;if(t.eq(e.MIN_VALUE))return this.isOdd()?e.MIN_VALUE:e.ZERO;if(this.isNegative())return t.isNegative()?this.neg().mul(t.neg()):this.neg().mul(t).neg();if(t.isNegative())return this.mul(t.neg()).neg();if(this.lt(e.TWO_PWR_24)&&t.lt(e.TWO_PWR_24))return e.fromNumber(this.toNumber()*t.toNumber(),this.unsigned);let n=this.high>>>16,s=this.high&65535,r=this.low>>>16,i=this.low&65535,u=t.high>>>16,a=t.high&65535,I=t.low>>>16,S=t.low&65535,w=0,m=0,O=0,d=0;return d+=i*S,O+=d>>>16,d&=65535,O+=r*S,m+=O>>>16,O&=65535,O+=i*I,m+=O>>>16,O&=65535,m+=s*S,w+=m>>>16,m&=65535,m+=r*I,w+=m>>>16,m&=65535,m+=i*a,w+=m>>>16,m&=65535,w+=n*S+s*I+r*a+i*u,w&=65535,e.fromBits(O<<16|d,w<<16|m,this.unsigned)}mul(t){return this.multiply(t)}negate(){return!this.unsigned&&this.eq(e.MIN_VALUE)?e.MIN_VALUE:this.not().add(e.ONE)}neg(){return this.negate()}not(){return e.fromBits(~this.low,~this.high,this.unsigned)}notEquals(t){return!this.equals(t)}neq(t){return this.notEquals(t)}ne(t){return this.notEquals(t)}or(t){return e.isLong(t)||(t=e.fromValue(t)),e.fromBits(this.low|t.low,this.high|t.high,this.unsigned)}shiftLeft(t){return e.isLong(t)&&(t=t.toInt()),(t&=63)===0?this:t<32?e.fromBits(this.low<>>32-t,this.unsigned):e.fromBits(0,this.low<>>t|this.high<<32-t,this.high>>t,this.unsigned):e.fromBits(this.high>>t-32,this.high>=0?0:-1,this.unsigned)}shr(t){return this.shiftRight(t)}shiftRightUnsigned(t){if(e.isLong(t)&&(t=t.toInt()),t&=63,t===0)return this;{let n=this.high;if(t<32){let s=this.low;return e.fromBits(s>>>t|n<<32-t,n>>>t,this.unsigned)}else return t===32?e.fromBits(n,0,this.unsigned):e.fromBits(n>>>t-32,0,this.unsigned)}}shr_u(t){return this.shiftRightUnsigned(t)}shru(t){return this.shiftRightUnsigned(t)}subtract(t){return e.isLong(t)||(t=e.fromValue(t)),this.add(t.neg())}sub(t){return this.subtract(t)}toInt(){return this.unsigned?this.low>>>0:this.low}toNumber(){return this.unsigned?(this.high>>>0)*K+(this.low>>>0):this.high*K+(this.low>>>0)}toBigInt(){return BigInt(this.toString())}toBytes(t){return t?this.toBytesLE():this.toBytesBE()}toBytesLE(){let t=this.high,n=this.low;return[n&255,n>>>8&255,n>>>16&255,n>>>24,t&255,t>>>8&255,t>>>16&255,t>>>24]}toBytesBE(){let t=this.high,n=this.low;return[t>>>24,t>>>16&255,t>>>8&255,t&255,n>>>24,n>>>16&255,n>>>8&255,n&255]}toSigned(){return this.unsigned?e.fromBits(this.low,this.high,!1):this}toString(t){if(t=t||10,t<2||36>>0).toString(t);if(s=i,s.isZero())return a+r;for(;a.length<6;)a="0"+a;r=""+a+r}}toUnsigned(){return this.unsigned?this:e.fromBits(this.low,this.high,!0)}xor(t){return e.isLong(t)||(t=e.fromValue(t)),e.fromBits(this.low^t.low,this.high^t.high,this.unsigned)}eqz(){return this.isZero()}le(t){return this.lessThanOrEqual(t)}toExtendedJSON(t){return t&&t.relaxed?this.toNumber():{$numberLong:this.toString()}}static fromExtendedJSON(t,n){let{useBigInt64:s=!1,relaxed:r=!0}={...n};if(t.$numberLong.length>je)throw new o("$numberLong string is too long");if(!ze.test(t.$numberLong))throw new o(`$numberLong string "${t.$numberLong}" is in an invalid format`);if(s){let u=BigInt(t.$numberLong);return BigInt.asIntN(64,u)}let i=e.fromString(t.$numberLong);return r?i.toNumber():i}inspect(t,n,s){s??=j;let r=s(this.toString(),n),i=this.unsigned?`, ${s(this.unsigned,n)}`:"";return`new Long(${r}${i})`}};f.TWO_PWR_24=f.fromInt(De);f.MAX_UNSIGNED_VALUE=f.fromBits(-1,-1,!0);f.ZERO=f.fromInt(0);f.UZERO=f.fromInt(0,!0);f.ONE=f.fromInt(1);f.UONE=f.fromInt(1,!0);f.NEG_ONE=f.fromInt(-1);f.MAX_VALUE=f.fromBits(-1,2147483647,!1);f.MIN_VALUE=f.fromBits(0,-2147483648,!1);var Fe=/^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/,Me=/^(\+|-)?(Infinity|inf)$/i,xe=/^(\+|-)?NaN$/i,Z=6111,et=-6176,Tt=6176,It=34,ut=h.fromNumberArray([124,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0].reverse()),dt=h.fromNumberArray([248,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0].reverse()),$t=h.fromNumberArray([120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0].reverse()),He=/^([-+])?(\d+)?$/,Ce=31,At=16383,Je=30,Pe=31;function _t(e){return!isNaN(parseInt(e,10))}function Ve(e){let t=f.fromNumber(1e9),n=f.fromNumber(0);if(!e.parts[0]&&!e.parts[1]&&!e.parts[2]&&!e.parts[3])return{quotient:e,rem:n};for(let s=0;s<=3;s++)n=n.shiftLeft(32),n=n.add(new f(e.parts[s],0)),e.parts[s]=n.div(t).low,n=n.modulo(t);return{quotient:e,rem:n}}function We(e,t){if(!e&&!t)return{high:f.fromNumber(0),low:f.fromNumber(0)};let n=e.shiftRightUnsigned(32),s=new f(e.getLowBits(),0),r=t.shiftRightUnsigned(32),i=new f(t.getLowBits(),0),u=n.multiply(r),a=n.multiply(i),I=s.multiply(r),S=s.multiply(i);return u=u.add(a.shiftRightUnsigned(32)),a=new f(a.getLowBits(),0).add(I).add(S.shiftRightUnsigned(32)),u=u.add(a.shiftRightUnsigned(32)),S=a.shiftLeft(32).add(new f(S.getLowBits(),0)),{high:u,low:S}}function Ye(e,t){let n=e.high>>>0,s=t.high>>>0;if(n>>0,i=t.low>>>0;if(r=7e3)throw new o(""+t+" not a valid Decimal128 string");let U=t.match(Fe),ft=t.match(Me),N=t.match(xe);if(!U&&!ft&&!N||t.length===0)throw new o(""+t+" not a valid Decimal128 string");if(U){let c=U[2],l=U[4],p=U[5],B=U[6];l&&B===void 0&&M(t,"missing exponent power"),l&&c===void 0&&M(t,"missing exponent base"),l===void 0&&(p||B)&&M(t,"missing e before exponent")}if((t[g]==="+"||t[g]==="-")&&(r=!0,s=t[g++]==="-"),!_t(t[g])&&t[g]!=="."){if(t[g]==="i"||t[g]==="I")return new e(s?dt:$t);if(t[g]==="N")return new e(ut)}for(;_t(t[g])||t[g]===".";){if(t[g]==="."){i&&M(t,"contains multiple periods"),i=!0,g=g+1;continue}d$+16384?$=et:$=$-w;$>Z;){if(E=E+1,E>=It){if(a===0){$=Z;break}M(t,"overflow")}$=$-1}if(n.allowRounding){for(;$=5&&(p=1,l===5)){p=O[E]%2===1?1:0;for(let B=m+E+2;B=0&&++O[B]>9;B--)if(O[B]=0,B===0)if($>8&255,A[g++]=b.low.low>>16&255,A[g++]=b.low.low>>24&255,A[g++]=b.low.high&255,A[g++]=b.low.high>>8&255,A[g++]=b.low.high>>16&255,A[g++]=b.low.high>>24&255,A[g++]=b.high.low&255,A[g++]=b.high.low>>8&255,A[g++]=b.high.low>>16&255,A[g++]=b.high.low>>24&255,A[g++]=b.high.high&255,A[g++]=b.high.high>>8&255,A[g++]=b.high.high>>16&255,A[g++]=b.high.high>>24&255,new e(A)}toString(){let t,n=0,s=new Array(36);for(let g=0;g>26&Ce;if(L>>3===3){if(L===Je)return w.join("")+"Infinity";if(L===Pe)return"NaN";t=E>>15&At,u=8+(E>>14&1)}else u=E>>14&7,t=E>>17&At;let T=t-Tt;if(a.parts[0]=(E&16383)+((u&15)<<14),a.parts[1]=x,a.parts[2]=d,a.parts[3]=O,a.parts[0]===0&&a.parts[1]===0&&a.parts[2]===0&&a.parts[3]===0)i=!0;else for(S=3;S>=0;S--){let g=0,U=Ve(a);if(a=U.quotient,g=U.rem.low,!!g)for(I=8;I>=0;I--)s[S*9+I]=g%10,g=Math.floor(g/10)}if(i)n=1,s[r]=0;else for(n=36;!s[r];)n=n-1,r=r+1;let F=n-1+T;if(F>=34||F<=-7||T>0){if(n>34)return w.push("0"),T>0?w.push(`E+${T}`):T<0&&w.push(`E${T}`),w.join("");w.push(`${s[r++]}`),n=n-1,n&&w.push(".");for(let g=0;g0?w.push(`+${F}`):w.push(`${F}`)}else if(T>=0)for(let g=0;g0)for(let U=0;U>8&255,s[9]=n>>16&255,s}toString(t){return t==="base64"?h.toBase64(this.id):t==="hex"?this.toHexString():this.toHexString()}toJSON(){return this.toHexString()}static is(t){return t!=null&&typeof t=="object"&&"_bsontype"in t&&t._bsontype==="ObjectId"}equals(t){if(t==null)return!1;if(e.is(t))return this[H][11]===t[H][11]&&h.equals(this[H],t[H]);if(typeof t=="string")return t.toLowerCase()===this.toHexString();if(typeof t=="object"&&typeof t.toHexString=="function"){let n=t.toHexString(),s=this.toHexString();return typeof n=="string"&&n.toLowerCase()===s}return!1}getTimestamp(){let t=new Date,n=X.fromUint8Array(this.id).getUint32(0,!1);return t.setTime(Math.floor(n)*1e3),t}static createPk(){return new e}static createFromTime(t){let n=h.fromNumberArray([0,0,0,0,0,0,0,0,0,0,0,0]);return X.fromUint8Array(n).setUint32(0,t,!1),new e(n)}static createFromHexString(t){if(t?.length!==24)throw new o("hex string must be 24 characters");return new e(h.fromHex(t))}static createFromBase64(t){if(t?.length!==16)throw new o("base64 string must be 16 characters");return new e(h.fromBase64(t))}static isValid(t){if(t==null)return!1;try{return new e(t),!0}catch{return!1}}toExtendedJSON(){return this.toHexString?{$oid:this.toHexString()}:{$oid:this.toString("hex")}}static fromExtendedJSON(t){return new e(t.$oid)}inspect(t,n,s){return s??=j,`new ObjectId(${s(this.toHexString(),n)})`}};k.index=Math.floor(Math.random()*16777215);function qe(e){return e.split("").sort().join("")}var q=class e extends D{get _bsontype(){return"BSONRegExp"}constructor(t,n){if(super(),this.pattern=t,this.options=qe(n??""),this.pattern.indexOf("\0")!==-1)throw new o(`BSON Regex patterns cannot contain null bytes, found: ${JSON.stringify(this.pattern)}`);if(this.options.indexOf("\0")!==-1)throw new o(`BSON Regex options cannot contain null bytes, found: ${JSON.stringify(this.options)}`);for(let s=0;sa);s??=j;let i=r(s(this.pattern),"regexp"),u=r(s(this.options),"regexp");return`new BSONRegExp(${i}, ${u})`}},it=class e extends D{get _bsontype(){return"BSONSymbol"}constructor(t){super(),this.value=t}valueOf(){return this.value}toString(){return this.value}toJSON(){return this.value}toExtendedJSON(){return{$symbol:this.value}}static fromExtendedJSON(t){return new e(t.$symbol)}inspect(t,n,s){return s??=j,`new BSONSymbol(${s(this.value,n)})`}},Ze=f,tt=class e extends Ze{get _bsontype(){return"Timestamp"}constructor(t){if(t==null)super(0,0,!0);else if(typeof t=="bigint")super(t,!0);else if(f.isLong(t))super(t.low,t.high,!0);else if(typeof t=="object"&&"t"in t&&"i"in t){if(typeof t.t!="number"&&(typeof t.t!="object"||t.t._bsontype!=="Int32"))throw new o("Timestamp constructed from { t, i } must provide t as a number");if(typeof t.i!="number"&&(typeof t.i!="object"||t.i._bsontype!=="Int32"))throw new o("Timestamp constructed from { t, i } must provide i as a number");let n=Number(t.t),s=Number(t.i);if(n<0||Number.isNaN(n))throw new o("Timestamp constructed from { t, i } must provide a positive t");if(s<0||Number.isNaN(s))throw new o("Timestamp constructed from { t, i } must provide a positive i");if(n>4294967295)throw new o("Timestamp constructed from { t, i } must provide t equal or less than uint32 max");if(s>4294967295)throw new o("Timestamp constructed from { t, i } must provide i equal or less than uint32 max");super(s,n,!0)}else throw new o("A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }")}toJSON(){return{$timestamp:this.toString()}}static fromInt(t){return new e(f.fromInt(t,!0))}static fromNumber(t){return new e(f.fromNumber(t,!0))}static fromBits(t,n){return new e({i:t,t:n})}static fromString(t,n){return new e(f.fromString(t,!0,n))}toExtendedJSON(){return{$timestamp:{t:this.high>>>0,i:this.low>>>0}}}static fromExtendedJSON(t){let n=f.isLong(t.$timestamp.i)?t.$timestamp.i.getLowBitsUnsigned():t.$timestamp.i,s=f.isLong(t.$timestamp.t)?t.$timestamp.t.getLowBitsUnsigned():t.$timestamp.t;return new e({t:s,i:n})}inspect(t,n,s){s??=j;let r=s(this.high>>>0,n),i=s(this.low>>>0,n);return`new Timestamp({ t: ${r}, i: ${i} })`}};tt.MAX_VALUE=f.MAX_UNSIGNED_VALUE;var Ke=f.fromNumber(Gt),Ge=f.fromNumber(Xt);function Xe(e,t,n){t=t??{};let s=t&&t.index?t.index:0,r=e[s]|e[s+1]<<8|e[s+2]<<16|e[s+3]<<24;if(r<5)throw new o(`bson size must be >= 5, is ${r}`);if(t.allowObjectSmallerThanBufferSize&&e.length= bson size ${r}`);if(!t.allowObjectSmallerThanBufferSize&&e.length!==r)throw new o(`buffer length ${e.length} must === bson size ${r}`);if(r+s>e.byteLength)throw new o(`(bson size ${r} + options.index ${s} must be <= buffer length ${e.byteLength})`);if(e[s+r-1]!==0)throw new o("One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00");return lt(e,s,t,n)}var Qe=/^\$ref$|^\$id$|^\$db$/;function lt(e,t,n,s=!1){let r=n.fieldsAsRaw==null?null:n.fieldsAsRaw,i=n.raw==null?!1:n.raw,u=typeof n.bsonRegExp=="boolean"?n.bsonRegExp:!1,a=n.promoteBuffers??!1,I=n.promoteLongs??!0,S=n.promoteValues??!0,w=n.useBigInt64??!1;if(w&&!S)throw new o("Must either request bigint or Long for int64 deserialization");if(w&&!I)throw new o("Must either request bigint or Long for int64 deserialization");let m=n.validation==null?{utf8:!0}:n.validation,O=!0,d,x=new Set,E=m.utf8;if(typeof E=="boolean")d=E;else{O=!1;let N=Object.keys(E).map(function(y){return E[y]});if(N.length===0)throw new o("UTF-8 validation setting cannot be empty");if(typeof N[0]!="boolean")throw new o("Invalid UTF-8 validation option, must specify boolean values");if(d=N[0],!N.every(y=>y===d))throw new o("Invalid UTF-8 validation option - keys must be all true or all false")}if(!O)for(let N of Object.keys(E))x.add(N);let $=t;if(e.length<5)throw new o("corrupt bson message < 5 bytes long");let L=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;if(L<5||L>e.length)throw new o("corrupt bson message");let T=s?[]:{},F=0,g=!1,U=s?!1:null,ft=new DataView(e.buffer,e.byteOffset,e.byteLength);for(;!g;){let N=e[t++];if(N===0)break;let y=t;for(;e[y]!==0&&y=e.byteLength)throw new o("Bad BSON Document: illegal CString");let b=s?F++:h.toUTF8(e,t,y,!1),A=!0;O||x.has(b)?A=d:A=!d,U!==!1&&b[0]==="$"&&(U=Qe.test(b));let c;if(t=y+1,N===Qt){let l=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;if(l<=0||l>e.length-t||e[t+l-1]!==0)throw new o("bad string length in bson");c=h.toUTF8(e,t,t+l-1,A),t=t+l}else if(N===se){let l=h.allocate(12);l.set(e.subarray(t,t+12)),c=new k(l),t=t+12}else if(N===St&&S===!1)c=new v(e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24);else if(N===St)c=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;else if(N===wt&&S===!1)c=new Q(ft.getFloat64(t,!0)),t=t+8;else if(N===wt)c=ft.getFloat64(t,!0),t=t+8;else if(N===ie){let l=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24,p=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;c=new Date(new f(l,p).toNumber())}else if(N===re){if(e[t]!==0&&e[t]!==1)throw new o("illegal boolean type value");c=e[t++]===1}else if(N===vt){let l=t,p=e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24;if(p<=0||p>e.length-t)throw new o("bad embedded document length in bson");if(i)c=e.slice(t,t+p);else{let B=n;O||(B={...n,validation:{utf8:A}}),c=lt(e,l,B,!1)}t=t+p}else if(N===te){let l=t,p=e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24,B=n,R=t+p;if(r&&r[b]&&(B={...n,raw:!0}),O||(B={...B,validation:{utf8:A}}),c=lt(e,l,B,!0),t=t+p,e[t-1]!==0)throw new o("invalid array terminator byte");if(t!==R)throw new o("corrupted array bson")}else if(N===ne)c=void 0;else if(N===oe)c=null;else if(N===ge){let l=X.fromUint8Array(e.subarray(t,t+8)),p=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24,B=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24,R=new f(p,B);w?c=l.getBigInt64(0,!0):I&&S===!0?c=R.lessThanOrEqual(Ke)&&R.greaterThanOrEqual(Ge)?R.toNumber():R:c=R}else if(N===ue){let l=h.allocate(16);l.set(e.subarray(t,t+16),0),t=t+16,c=new nt(l)}else if(N===ee){let l=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24,p=l,B=e[t++];if(l<0)throw new o("Negative binary type element size found");if(l>e.byteLength)throw new o("Binary type size larger than document size");if(e.slice!=null){if(B===_.SUBTYPE_BYTE_ARRAY){if(l=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24,l<0)throw new o("Negative binary type element size found for subtype 0x02");if(l>p-4)throw new o("Binary type with subtype 0x02 contains too long binary size");if(lp-4)throw new o("Binary type with subtype 0x02 contains too long binary size");if(l=e.length)throw new o("Bad BSON Document: illegal CString");let l=h.toUTF8(e,t,y,!1);for(t=y+1,y=t;e[y]!==0&&y=e.length)throw new o("Bad BSON Document: illegal CString");let p=h.toUTF8(e,t,y,!1);t=y+1;let B=new Array(p.length);for(y=0;y=e.length)throw new o("Bad BSON Document: illegal CString");let l=h.toUTF8(e,t,y,!1);for(t=y+1,y=t;e[y]!==0&&y=e.length)throw new o("Bad BSON Document: illegal CString");let p=h.toUTF8(e,t,y,!1);t=y+1,c=new q(l,p)}else if(N===ce){let l=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;if(l<=0||l>e.length-t||e[t+l-1]!==0)throw new o("bad string length in bson");let p=h.toUTF8(e,t,t+l-1,A);c=S?p:new it(p),t=t+l}else if(N===ae){let l=e[t++]+e[t++]*256+e[t++]*65536+e[t++]*16777216,p=e[t++]+e[t++]*256+e[t++]*65536+e[t++]*(1<<24);c=new tt({i:l,t:p})}else if(N===ye)c=new rt;else if(N===me)c=new st;else if(N===le){let l=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;if(l<=0||l>e.length-t||e[t+l-1]!==0)throw new o("bad string length in bson");let p=h.toUTF8(e,t,t+l-1,A);c=new Y(p),t=t+l}else if(N===he){let l=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;if(l<4+4+4+1)throw new o("code_w_scope total size shorter minimum expected length");let p=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;if(p<=0||p>e.length-t||e[t+p-1]!==0)throw new o("bad string length in bson");let B=h.toUTF8(e,t,t+p-1,A);t=t+p;let R=t,at=e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24,Yt=lt(e,R,n,!1);if(t=t+at,l<4+4+at+p)throw new o("code_w_scope total size is too short, truncating scope");if(l>4+4+at+p)throw new o("code_w_scope total size is too long, clips outer document");c=new Y(B,Yt)}else if(N===fe){let l=e[t++]|e[t++]<<8|e[t++]<<16|e[t++]<<24;if(l<=0||l>e.length-t||e[t+l-1]!==0)throw new o("bad string length in bson");if(m!=null&&m.utf8&&!Mt(e,t,t+l-1))throw new o("Invalid UTF-8 string in BSON document");let p=h.toUTF8(e,t,t+l-1,!1);t=t+l;let B=h.allocate(12);B.set(e.subarray(t,t+12),0);let R=new k(B);t=t+12,c=new P(p,R)}else throw new o(`Detected unknown BSON type ${N.toString(16)} for fieldname "${b}"`);b==="__proto__"?Object.defineProperty(T,b,{value:c,writable:!0,enumerable:!0,configurable:!0}):T[b]=c}if(L!==t-$)throw s?new o("corrupt array bson"):new o("corrupt object bson");if(!U)return T;if(Ht(T)){let N=Object.assign({},T);return delete N.$ref,delete N.$id,delete N.$db,new P(T.$ref,T.$id,T.$db,N)}return T}var Jt=new DataView(new ArrayBuffer(8),0,8),hn=new Uint8Array(Jt.buffer,0,4),an=new Uint8Array(Jt.buffer,0,8);function ve(e){return e!=null&&typeof e=="object"&&"_bsontype"in e&&typeof e._bsontype=="string"}var tn={$oid:k,$binary:_,$uuid:_,$symbol:it,$numberInt:v,$numberDecimal:nt,$numberDouble:Q,$numberLong:f,$minKey:rt,$maxKey:st,$regex:q,$regularExpression:q,$timestamp:tt};function Pt(e,t={}){if(typeof e=="number"){let s=e<=Dt&&e>=jt,r=e<=zt&&e>=Ft;if(t.relaxed||t.legacy)return e;if(Number.isInteger(e)&&!Object.is(e,-0)){if(s)return new v(e);if(r)return t.useBigInt64?BigInt(e):f.fromNumber(e)}return new Q(e)}if(e==null||typeof e!="object")return e;if(e.$undefined)return null;let n=Object.keys(e).filter(s=>s.startsWith("$")&&e[s]!=null);for(let s=0;su.startsWith("$")),i=!0;if(r.forEach(u=>{["$ref","$id","$db"].indexOf(u)===-1&&(i=!1)}),i)return P.fromExtendedJSON(s)}return e}function en(e,t){return e.map((n,s)=>{t.seenObjects.push({propertyName:`index ${s}`,obj:null});try{return C(n,t)}finally{t.seenObjects.pop()}})}function Ut(e){let t=e.toISOString();return e.getUTCMilliseconds()!==0?t:t.slice(0,-5)+"Z"}function C(e,t){if(e instanceof Map||Rt(e)){let n=Object.create(null);for(let[s,r]of e){if(typeof s!="string")throw new o("Can only serialize maps with string keys");n[s]=r}return C(n,t)}if((typeof e=="object"||typeof e=="function")&&e!==null){let n=t.seenObjects.findIndex(s=>s.obj===e);if(n!==-1){let s=t.seenObjects.map(w=>w.propertyName),r=s.slice(0,n).map(w=>`${w} -> `).join(""),i=s[n],u=" -> "+s.slice(n+1,s.length-1).map(w=>`${w} -> `).join(""),a=s[s.length-1],I=" ".repeat(r.length+i.length/2),S="-".repeat(u.length+(i.length+a.length)/2-1);throw new o(`Converting circular structure to EJSON: + ${r}${i}${u}${a} + ${I}\\${S}/`)}t.seenObjects[t.seenObjects.length-1].obj=e}if(Array.isArray(e))return en(e,t);if(e===void 0)return null;if(e instanceof Date||Zt(e)){let n=e.getTime(),s=n>-1&&n<2534023188e5;return t.legacy?t.relaxed&&s?{$date:e.getTime()}:{$date:Ut(e)}:t.relaxed&&s?{$date:Ut(e)}:{$date:{$numberLong:e.getTime().toString()}}}if(typeof e=="number"&&(!t.relaxed||!isFinite(e))){if(Number.isInteger(e)&&!Object.is(e,-0)){if(e>=jt&&e<=Dt)return{$numberInt:e.toString()};if(e>=Ft&&e<=zt)return{$numberLong:e.toString()}}return{$numberDouble:Object.is(e,-0)?"-0.0":e.toString()}}if(typeof e=="bigint")return t.relaxed?Number(BigInt.asIntN(64,e)):{$numberLong:BigInt.asIntN(64,e).toString()};if(e instanceof RegExp||qt(e)){let n=e.flags;if(n===void 0){let r=e.toString().match(/[gimuy]*$/);r&&(n=r[0])}return new q(e.source,n).toExtendedJSON(t)}return e!=null&&typeof e=="object"?sn(e,t):e}var nn={Binary:e=>new _(e.value(),e.sub_type),Code:e=>new Y(e.code,e.scope),DBRef:e=>new P(e.collection||e.namespace,e.oid,e.db,e.fields),Decimal128:e=>new nt(e.bytes),Double:e=>new Q(e.value),Int32:e=>new v(e.value),Long:e=>f.fromBits(e.low!=null?e.low:e.low_,e.low!=null?e.high:e.high_,e.low!=null?e.unsigned:e.unsigned_),MaxKey:()=>new st,MinKey:()=>new rt,ObjectId:e=>new k(e),BSONRegExp:e=>new q(e.pattern,e.options),BSONSymbol:e=>new it(e.value),Timestamp:e=>tt.fromBits(e.low,e.high)};function sn(e,t){if(e==null||typeof e!="object")throw new o("not an object instance");let n=e._bsontype;if(typeof n>"u"){let s={};for(let r of Object.keys(e)){t.seenObjects.push({propertyName:r,obj:null});try{let i=C(e[r],t);r==="__proto__"?Object.defineProperty(s,r,{value:i,writable:!0,enumerable:!0,configurable:!0}):s[r]=i}finally{t.seenObjects.pop()}}return s}else{if(e!=null&&typeof e=="object"&&typeof e._bsontype=="string"&&e[Symbol.for("@@mdb.bson.version")]!==mt)throw new yt;if(ve(e)){let s=e;if(typeof s.toExtendedJSON!="function"){let r=nn[e._bsontype];if(!r)throw new o("Unrecognized or invalid _bsontype: "+e._bsontype);s=r(s)}return n==="Code"&&s.scope?s=new Y(s.code,C(s.scope,t)):n==="DBRef"&&s.oid&&(s=new P(C(s.collection,t),C(s.oid,t),C(s.db,t),C(s.fields,t))),s.toExtendedJSON(t)}else throw new o("_bsontype must be a string, but was: "+typeof n)}}function Vt(e,t){let n={useBigInt64:t?.useBigInt64??!1,relaxed:t?.relaxed??!0,legacy:t?.legacy??!1};return JSON.parse(e,(s,r)=>{if(s.indexOf("\0")!==-1)throw new o(`BSON Document field names cannot contain null bytes, found: ${JSON.stringify(s)}`);return Pt(r,n)})}function Wt(e,t,n,s){n!=null&&typeof n=="object"&&(s=n,n=0),t!=null&&typeof t=="object"&&!Array.isArray(t)&&(s=t,t=void 0,n=0);let r=Object.assign({relaxed:!0,legacy:!1},s,{seenObjects:[{propertyName:"(root)",obj:null}]}),i=C(e,r);return JSON.stringify(i,t,n)}function rn(e,t){return t=t||{},JSON.parse(Wt(e,t))}function on(e,t){return t=t||{},Vt(JSON.stringify(e),t)}var ot=Object.create(null);ot.parse=Vt;ot.stringify=Wt;ot.serialize=rn;ot.deserialize=on;Object.freeze(ot);var fn=1024*1024*17,gn=h.allocate(fn);function ln(e,t={}){return Xe(h.toLocalBufferType(e),t)}export{ln as deserialize}; diff --git a/packages/server/src/jsRunner/bundles/bsonPackage.ts b/packages/server/src/jsRunner/bundles/bsonPackage.ts new file mode 100644 index 0000000000..0dc7385dc0 --- /dev/null +++ b/packages/server/src/jsRunner/bundles/bsonPackage.ts @@ -0,0 +1 @@ +export { deserialize } from "bson" diff --git a/packages/server/src/utilities/scriptRunner.ts b/packages/server/src/utilities/scriptRunner.ts index b6e597cc55..04123632fd 100644 --- a/packages/server/src/utilities/scriptRunner.ts +++ b/packages/server/src/utilities/scriptRunner.ts @@ -1,24 +1,27 @@ import ivm from "isolated-vm" +import bson from "bson" + const JS_TIMEOUT_MS = 1000 class ScriptRunner { vm: IsolatedVM constructor(script: string, context: any) { - const code = `let fn = () => {\n${script}\n}; results.out = fn();` - this.vm = new IsolatedVM({ memoryLimit: 8 }) + this.vm = new IsolatedVM({ memoryLimit: 64 }) this.vm.context = { ...context, + data: bson.BSON.serialize({ data: context.data }), results: { out: "" }, } + + const code = `let fn = () => {data=deserialize(data).data;\n${script}\n}; cb(JSON.parse(JSON.stringify(fn())));` this.vm.code = code } execute() { - this.vm.runScript() - const results = this.vm.getValue("results") - return results.out + const result = this.vm.runScript() + return result } } @@ -26,7 +29,8 @@ class IsolatedVM { isolate: ivm.Isolate vm: ivm.Context #jail: ivm.Reference - script: any + script: ivm.Module = undefined! + #bsonModule: ivm.Module = undefined! constructor({ memoryLimit }: { memoryLimit: number }) { this.isolate = new ivm.Isolate({ memoryLimit }) @@ -49,11 +53,38 @@ class IsolatedVM { } set code(code: string) { - this.script = this.isolate.compileScriptSync(code) + const bsonSource = require("../jsRunner/bundles/bson.ivm.bundle.js") + + this.#bsonModule = this.isolate.compileModuleSync(bsonSource) + this.#bsonModule.instantiateSync(this.vm, specifier => { + throw new Error(`No imports allowed. Required: ${specifier}`) + }) + + this.script = this.isolate.compileModuleSync( + `import {deserialize} from "compiled_module";${code}` + ) } runScript() { - this.script.runSync(this.vm, { timeout: JS_TIMEOUT_MS }) + this.script.instantiateSync(this.vm, specifier => { + if (specifier === "compiled_module") { + return this.#bsonModule + } + + throw new Error(`"${specifier}" import not allowed`) + }) + + let result + this.vm.global.setSync( + "cb", + new ivm.Callback((value: any) => { + result = value + }) + ) + + this.script.evaluateSync({ timeout: JS_TIMEOUT_MS }) + + return result } copyRefToVm(value: Object): ivm.Copy {