UNPKG

@kittycad/lib

Version:

Javascript library for KittyCAD API

1 lines 319 kB
import{OAuth2AuthCodePKCE as t,EErrorOAuth2 as e}from"@kittycad/oauth2-auth-code-pkce";try{if("undefined"==typeof fetch&&"undefined"!=typeof process&&process.versions?.node){new Function("m","return import(m)")("cross-fetch/polyfill").catch((()=>{}))}}catch{}class n{constructor(t){const e="undefined"!=typeof process?process.env:void 0,n=e?.KITTYCAD_TOKEN||e?.KITTYCAD_API_TOKEN||e?.ZOO_AI_TOKEN,l=e?.ZOO_HOST;"string"==typeof t?this.token=t:t&&"object"==typeof t&&(this.token=t.token,this.baseUrl=t.baseUrl,this.fetch=t.fetch,this.clientId=t.clientId,this.redirectUrl=t.redirectUrl,this.scopes=t.scopes,this.onAccessTokenExpiry=t.onAccessTokenExpiry,this.onInvalidGrant=t.onInvalidGrant),this.token??=n,this.baseUrl??=l,this.clientId&&"undefined"!=typeof localStorage&&(this.oauth2=this.createOAuth2Client(),this.oauth2.isHTTPDecoratorActive(!0),this.fetch=this.oauth2.decorateFetchHTTPClient(this.fetch||fetch))}authorize(t){return this.oauth2.fetchAuthorizationCode(t)}isReturningFromAuthServer(){return this.oauth2.isReturningFromAuthServer()}async getAccessToken(){const t=await this.oauth2.getAccessToken();return this.updateTokenFromAccessContext(t),t}resetOAuth2(){this.oauth2.reset(),this.token=void 0}createOAuth2Client(){const e=this.baseUrl||"https://api.zoo.dev",n=this.redirectUrl||("undefined"==typeof location?void 0:`${location.origin}${location.pathname}`);if(!n)throw new Error("OAuth2 requires redirectUrl when the current browser URL is unavailable.");return new t({authorizationUrl:l(e,"/oauth2/authorize"),tokenUrl:l(e,"/oauth2/token"),clientId:this.clientId,redirectUrl:n,scopes:this.scopes||[],onAccessTokenExpiry:async t=>{const e=await(this.onAccessTokenExpiry?this.onAccessTokenExpiry(t):t());return this.updateTokenFromAccessContext(e),e},onInvalidGrant:this.onInvalidGrant||(()=>{})})}updateTokenFromAccessContext(t){t?.token?.value&&(this.token=t.token.value)}}function l(t,e){return`${t.replace(/\/+$/,"")}/${e.replace(/^\/+/,"")}`}function i(t){const e=new URLSearchParams;for(const[n,l]of Object.entries(t))if(void 0!==l)if(Array.isArray(l))for(const t of l)e.append(n,String(t));else e.append(n,String(l));const n=e.toString();return n?`?${n}`:""}function o(t){const e=new URLSearchParams;for(const[n,l]of Object.entries(t||{}))if(void 0!==l)if(Array.isArray(l))for(const t of l)e.append(n,String(t));else e.append(n,String(l));return e}try{if("undefined"!=typeof process&&process.versions?.node&&"win32"===process.platform){new Function("m","return import(m)")("win-ca")}}catch{}class c extends Error{constructor(t,e,n){super(n||e?.message||`HTTP ${t}`),this.name="ApiError",this.status=t,this.error_code=e?.error_code,this.request_id=e?.request_id,this.body=e}}async function a(t){if(t.ok)return;let e;try{e=await t.json()}catch{try{const n=await t.text();e=n?{message:n}:void 0}catch{e=void 0}}throw new c(t.status,e)}class s{started=!1;constructor(t,e,n="page_token"){this.fetchPage=t,this.baseParams={...e},this.tokenField=n}hasNext(){return!this.started||!!this.nextToken}reset(){this.started=!1,this.nextToken=void 0}async next(){if(this.started&&!this.nextToken)return[];const t={...this.baseParams};this.started&&this.nextToken&&(t[this.tokenField]=this.nextToken);const e=await this.fetchPage(t);return this.started=!0,this.nextToken=e?.next_page??null,e?.items??[]}}function d(t,e,n="page_token"){return new s(t,e,n)}async function b({client:t,id:e,limit:n,page_token:l,sort_by:o}){const c=`/users/${e}/api-calls`+i({limit:n,page_token:l,sort_by:o}),s=(t?.baseUrl||"https://api.zoo.dev")+c,d=t&&t.token||"",b={};d&&(b.Authorization=`Bearer ${d}`);const Z={method:"GET",headers:b},m=t?.fetch||fetch,r=await m(s,Z);await a(r);return await r.json()}async function Z({client:t,limit:e,page_token:n,sort_by:l}){const o="/org/api-calls"+i({limit:e,page_token:n,sort_by:l}),c=(t?.baseUrl||"https://api.zoo.dev")+o,s=t&&t.token||"",d={};s&&(d.Authorization=`Bearer ${s}`);const b={method:"GET",headers:d},Z=t?.fetch||fetch,m=await Z(c,b);await a(m);return await m.json()}async function m({client:t,limit:e,page_token:n,sort_by:l}){const o="/user/api-calls"+i({limit:e,page_token:n,sort_by:l}),c=(t?.baseUrl||"https://api.zoo.dev")+o,s=t&&t.token||"",d={};s&&(d.Authorization=`Bearer ${s}`);const b={method:"GET",headers:d},Z=t?.fetch||fetch,m=await Z(c,b);await a(m);return await m.json()}async function r({client:t,limit:e,page_token:n,sort_by:l}){const o="/user/api-tokens"+i({limit:e,page_token:n,sort_by:l}),c=(t?.baseUrl||"https://api.zoo.dev")+o,s=t&&t.token||"",d={};s&&(d.Authorization=`Bearer ${s}`);const b={method:"GET",headers:d},Z=t?.fetch||fetch,m=await Z(c,b);await a(m);return await m.json()}const u=(()=>{const t=Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype),Symbol.toStringTag).get;return e=>t.call(e)})();function h(t){return"Uint8Array"===u(t)}function p(t){return"object"==typeof t&&null!=t&&Symbol.toStringTag in t&&("ArrayBuffer"===t[Symbol.toStringTag]||"SharedArrayBuffer"===t[Symbol.toStringTag])}function y(t){return t instanceof RegExp||"[object RegExp]"===Object.prototype.toString.call(t)}function X(t){return"object"==typeof t&&null!=t&&Symbol.toStringTag in t&&"Map"===t[Symbol.toStringTag]}function G(t){return t instanceof Date||"[object Date]"===Object.prototype.toString.call(t)}function V(t,e){return JSON.stringify(t,((t,e)=>"bigint"==typeof e?{$numberLong:`${e}`}:X(e)?Object.fromEntries(e):e))}const W=7,f=Symbol.for("@@mdb.bson.version"),g=2147483647,Y=-2147483648,I=Math.pow(2,63)-1,R=-Math.pow(2,63),J=Math.pow(2,53),S=-Math.pow(2,53),K=1,T=2,L=3,z=4,x=5,U=6,N=7,w=8,k=9,H=10,C=11,B=12,v=13,j=14,M=15,P=16,F=17,Q=18,O=19,E=255,_=127,D=0,A=4,$=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});class q extends Error{get bsonError(){return!0}get name(){return"BSONError"}constructor(t,e){super(t,e)}static isBSONError(t){return null!=t&&"object"==typeof t&&"bsonError"in t&&!0===t.bsonError&&"name"in t&&"message"in t&&"stack"in t}}class tt extends q{get name(){return"BSONVersionError"}constructor(){super(`Unsupported BSON version, bson types must be from bson ${W}.x.x`)}}class et extends q{get name(){return"BSONRuntimeError"}constructor(t){super(t)}}class nt extends q{get name(){return"BSONOffsetError"}offset;constructor(t,e,n){super(`${t}. offset: ${e}`,n),this.offset=e}}let lt,it;function ot(t,e,n,l){if(l){lt??=new TextDecoder("utf8",{fatal:!0});try{return lt.decode(t.subarray(e,n))}catch(t){throw new q("Invalid UTF-8 string in BSON document",{cause:t})}}return it??=new TextDecoder("utf8",{fatal:!1}),it.decode(t.subarray(e,n))}function ct(t,e,n){if(0===t.length)return"";const l=n-e;if(0===l)return"";if(l>20)return null;if(1===l&&t[e]<128)return String.fromCharCode(t[e]);if(2===l&&t[e]<128&&t[e+1]<128)return String.fromCharCode(t[e])+String.fromCharCode(t[e+1]);if(3===l&&t[e]<128&&t[e+1]<128&&t[e+2]<128)return String.fromCharCode(t[e])+String.fromCharCode(t[e+1])+String.fromCharCode(t[e+2]);const i=[];for(let l=e;l<n;l++){const e=t[l];if(e>127)return null;i.push(e)}return String.fromCharCode(...i)}function at(t){return bt.fromNumberArray(Array.from({length:t},(()=>Math.floor(256*Math.random()))))}function st(t){return crypto.getRandomValues(bt.allocate(t))}const dt=(()=>{const{crypto:t}=globalThis;return null!=t&&"function"==typeof t.getRandomValues?st:at})(),bt={isUint8Array:h,toLocalBufferType(t){if(Buffer.isBuffer(t))return t;if(ArrayBuffer.isView(t))return Buffer.from(t.buffer,t.byteOffset,t.byteLength);const e=t?.[Symbol.toStringTag]??Object.prototype.toString.call(t);if("ArrayBuffer"===e||"SharedArrayBuffer"===e||"[object ArrayBuffer]"===e||"[object SharedArrayBuffer]"===e)return Buffer.from(t);throw new q("Cannot create Buffer from the passed potentialBuffer.")},allocate:t=>Buffer.alloc(t),allocateUnsafe:t=>Buffer.allocUnsafe(t),compare:(t,e)=>bt.toLocalBufferType(t).compare(e),concat:t=>Buffer.concat(t),copy:(t,e,n,l,i)=>bt.toLocalBufferType(t).copy(e,n??0,l??0,i??t.length),equals:(t,e)=>bt.toLocalBufferType(t).equals(e),fromNumberArray:t=>Buffer.from(t),fromBase64:t=>Buffer.from(t,"base64"),fromUTF8:t=>Buffer.from(t,"utf8"),toBase64:t=>bt.toLocalBufferType(t).toString("base64"),fromISO88591:t=>Buffer.from(t,"binary"),toISO88591:t=>bt.toLocalBufferType(t).toString("binary"),fromHex:t=>Buffer.from(t,"hex"),toHex:t=>bt.toLocalBufferType(t).toString("hex"),toUTF8(t,e,n,l){const i=n-e<=20?ct(t,e,n):null;if(null!=i)return i;const o=bt.toLocalBufferType(t).toString("utf8",e,n);if(l)for(let l=0;l<o.length;l++)if(65533===o.charCodeAt(l)){ot(t,e,n,!0);break}return o},utf8ByteLength:t=>Buffer.byteLength(t,"utf8"),encodeUTF8Into(t,e,n){const l=function(t,e,n){if(0===e.length)return 0;if(e.length>25)return null;if(t.length-n<e.length)return null;for(let l=0,i=n;l<e.length;l++,i++){const n=e.charCodeAt(l);if(n>127)return null;t[i]=n}return e.length}(t,e,n);return null!=l?l:bt.toLocalBufferType(t).write(e,n,void 0,"utf8")},randomBytes:dt,swap32:t=>bt.toLocalBufferType(t).swap32()};function Zt(t){if(t<0)throw new RangeError(`The argument 'byteLength' is invalid. Received ${t}`);return ut.fromNumberArray(Array.from({length:t},(()=>Math.floor(256*Math.random()))))}const mt=(()=>{const{crypto:t}=globalThis;if(null!=t&&"function"==typeof t.getRandomValues)return e=>t.getRandomValues(ut.allocate(e));if(function(){const{navigator:t}=globalThis;return"object"==typeof t&&"ReactNative"===t.product}()){const{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 Zt})(),rt=/(\d|[a-f])/i,ut={isUint8Array:h,toLocalBufferType(t){const e=t?.[Symbol.toStringTag]??Object.prototype.toString.call(t);if("Uint8Array"===e)return t;if(ArrayBuffer.isView(t))return new Uint8Array(t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength));if("ArrayBuffer"===e||"SharedArrayBuffer"===e||"[object ArrayBuffer]"===e||"[object SharedArrayBuffer]"===e)return new Uint8Array(t);throw new q("Cannot make a Uint8Array from passed potentialBuffer.")},allocate(t){if("number"!=typeof t)throw new TypeError(`The "size" argument must be of type number. Received ${String(t)}`);return new Uint8Array(t)},allocateUnsafe:t=>ut.allocate(t),compare(t,e){if(t===e)return 0;const n=Math.min(t.length,e.length);for(let l=0;l<n;l++){if(t[l]<e[l])return-1;if(t[l]>e[l])return 1}return t.length<e.length?-1:t.length>e.length?1:0},concat(t){if(0===t.length)return ut.allocate(0);let e=0;for(const n of t)e+=n.length;const n=ut.allocate(e);let l=0;for(const e of t)n.set(e,l),l+=e.length;return n},copy(t,e,n,l,i){if(void 0!==i&&i<0)throw new RangeError(`The value of "sourceEnd" is out of range. It must be >= 0. Received ${i}`);if(i=i??t.length,void 0!==l&&(l<0||l>i))throw new RangeError(`The value of "sourceStart" is out of range. It must be >= 0 and <= ${i}. Received ${l}`);if(l=l??0,void 0!==n&&n<0)throw new RangeError(`The value of "targetStart" is out of range. It must be >= 0. Received ${n}`);n=n??0;const o=t.subarray(l,i),c=Math.min(o.length,e.length-n);return c<=0?0:(e.set(o.subarray(0,c),n),c)},equals(t,e){if(t.byteLength!==e.byteLength)return!1;for(let n=0;n<t.byteLength;n++)if(t[n]!==e[n])return!1;return!0},fromNumberArray:t=>Uint8Array.from(t),fromBase64:t=>Uint8Array.from(atob(t),(t=>t.charCodeAt(0))),fromUTF8:t=>(new TextEncoder).encode(t),toBase64:t=>btoa(ut.toISO88591(t)),fromISO88591:t=>Uint8Array.from(t,(t=>255&t.charCodeAt(0))),toISO88591:t=>Array.from(Uint16Array.from(t),(t=>String.fromCharCode(t))).join(""),fromHex(t){const e=t.length%2==0?t:t.slice(0,t.length-1),n=[];for(let t=0;t<e.length;t+=2){const l=e[t],i=e[t+1];if(!rt.test(l))break;if(!rt.test(i))break;const o=Number.parseInt(`${l}${i}`,16);n.push(o)}return Uint8Array.from(n)},toHex:t=>Array.from(t,(t=>t.toString(16).padStart(2,"0"))).join(""),toUTF8(t,e,n,l){const i=n-e<=20?ct(t,e,n):null;return null!=i?i:ot(t,e,n,l)},utf8ByteLength:t=>(new TextEncoder).encode(t).byteLength,encodeUTF8Into(t,e,n){const l=(new TextEncoder).encode(e);return t.set(l,n),l.byteLength},randomBytes:mt,swap32(t){if(t.length%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(let e=0;e<t.length;e+=4){const n=t[e],l=t[e+1],i=t[e+2],o=t[e+3];t[e]=o,t[e+1]=i,t[e+2]=l,t[e+3]=n}return t}},ht="function"==typeof Buffer&&!0!==Buffer.prototype?._isBuffer?bt:ut,pt=Symbol.for("@@mdb.bson.type");class yt{get[pt](){return this._bsontype}get[f](){return W}[Symbol.for("nodejs.util.inspect.custom")](t,e,n){return this.inspect(t,e,n)}}const Xt=new Float64Array(1),Gt=new Uint8Array(Xt.buffer,0,8);Xt[0]=-1;const Vt=0===Gt[7],Wt={isBigEndian:Vt,getNonnegativeInt32LE(t,e){if(t[e+3]>127)throw new RangeError(`Size cannot be negative at offset: ${e}`);return t[e]|t[e+1]<<8|t[e+2]<<16|t[e+3]<<24},getInt32LE:(t,e)=>t[e]|t[e+1]<<8|t[e+2]<<16|t[e+3]<<24,getUint32LE:(t,e)=>t[e]+256*t[e+1]+65536*t[e+2]+16777216*t[e+3],getUint32BE:(t,e)=>t[e+3]+256*t[e+2]+65536*t[e+1]+16777216*t[e],getBigInt64LE:(t,e)=>(BigInt(t[e+4]+256*t[e+5]+65536*t[e+6]+(t[e+7]<<24))<<32n)+BigInt(t[e]+256*t[e+1]+65536*t[e+2]+16777216*t[e+3]),getFloat64LE:Vt?(t,e)=>(Gt[7]=t[e],Gt[6]=t[e+1],Gt[5]=t[e+2],Gt[4]=t[e+3],Gt[3]=t[e+4],Gt[2]=t[e+5],Gt[1]=t[e+6],Gt[0]=t[e+7],Xt[0]):(t,e)=>(Gt[0]=t[e],Gt[1]=t[e+1],Gt[2]=t[e+2],Gt[3]=t[e+3],Gt[4]=t[e+4],Gt[5]=t[e+5],Gt[6]=t[e+6],Gt[7]=t[e+7],Xt[0]),setInt32BE:(t,e,n)=>(t[e+3]=n,n>>>=8,t[e+2]=n,n>>>=8,t[e+1]=n,n>>>=8,t[e]=n,4),setInt32LE:(t,e,n)=>(t[e]=n,n>>>=8,t[e+1]=n,n>>>=8,t[e+2]=n,n>>>=8,t[e+3]=n,4),setBigInt64LE(t,e,n){const l=0xffffffffn;let i=Number(n&l);t[e]=i,i>>=8,t[e+1]=i,i>>=8,t[e+2]=i,i>>=8,t[e+3]=i;let o=Number(n>>32n&l);return t[e+4]=o,o>>=8,t[e+5]=o,o>>=8,t[e+6]=o,o>>=8,t[e+7]=o,8},setFloat64LE:Vt?(t,e,n)=>(Xt[0]=n,t[e]=Gt[7],t[e+1]=Gt[6],t[e+2]=Gt[5],t[e+3]=Gt[4],t[e+4]=Gt[3],t[e+5]=Gt[2],t[e+6]=Gt[1],t[e+7]=Gt[0],8):(t,e,n)=>(Xt[0]=n,t[e]=Gt[0],t[e+1]=Gt[1],t[e+2]=Gt[2],t[e+3]=Gt[3],t[e+4]=Gt[4],t[e+5]=Gt[5],t[e+6]=Gt[6],t[e+7]=Gt[7],8)};class ft extends yt{get _bsontype(){return"Binary"}static BSON_BINARY_SUBTYPE_DEFAULT=0;static BUFFER_SIZE=256;static SUBTYPE_DEFAULT=0;static SUBTYPE_FUNCTION=1;static SUBTYPE_BYTE_ARRAY=2;static SUBTYPE_UUID_OLD=3;static SUBTYPE_UUID=4;static SUBTYPE_MD5=5;static SUBTYPE_ENCRYPTED=6;static SUBTYPE_COLUMN=7;static SUBTYPE_SENSITIVE=8;static SUBTYPE_VECTOR=9;static SUBTYPE_USER_DEFINED=128;static VECTOR_TYPE=Object.freeze({Int8:3,Float32:39,PackedBit:16});buffer;sub_type;position;constructor(t,e){if(super(),null!=t&&"string"==typeof t&&!ArrayBuffer.isView(t)&&!p(t)&&!Array.isArray(t))throw new q("Binary can only be constructed from Uint8Array or number[]");this.sub_type=e??ft.BSON_BINARY_SUBTYPE_DEFAULT,null==t?(this.buffer=ht.allocate(ft.BUFFER_SIZE),this.position=0):(this.buffer=Array.isArray(t)?ht.fromNumberArray(t):ht.toLocalBufferType(t),this.position=this.buffer.byteLength)}put(t){if("string"==typeof t&&1!==t.length)throw new q("only accepts single character String");if("number"!=typeof t&&1!==t.length)throw new q("only accepts single character Uint8Array or Array");let e;if(e="string"==typeof t?t.charCodeAt(0):"number"==typeof t?t:t[0],e<0||e>255)throw new q("only accepts number in a valid unsigned byte range 0-255");if(this.buffer.byteLength>this.position)this.buffer[this.position++]=e;else{const t=ht.allocate(ft.BUFFER_SIZE+this.buffer.length);t.set(this.buffer,0),this.buffer=t,this.buffer[this.position++]=e}}write(t,e){if(e="number"==typeof e?e:this.position,this.buffer.byteLength<e+t.length){const e=ht.allocate(this.buffer.byteLength+t.length);e.set(this.buffer,0),this.buffer=e}if(ArrayBuffer.isView(t))this.buffer.set(ht.toLocalBufferType(t),e),this.position=e+t.byteLength>this.position?e+t.length:this.position;else if("string"==typeof t)throw new q("input cannot be string")}read(t,e){const n=t+(e=e&&e>0?e:this.position);return this.buffer.subarray(t,n>this.position?this.position:n)}value(){return this.buffer.length===this.position?this.buffer:this.buffer.subarray(0,this.position)}length(){return this.position}toJSON(){return ht.toBase64(this.buffer.subarray(0,this.position))}toString(t){return"hex"===t?ht.toHex(this.buffer.subarray(0,this.position)):"base64"===t?ht.toBase64(this.buffer.subarray(0,this.position)):ht.toUTF8(this.buffer,0,this.position,!1)}toExtendedJSON(t){t=t||{},this.sub_type===ft.SUBTYPE_VECTOR&&gt(this);const e=ht.toBase64(this.buffer),n=Number(this.sub_type).toString(16);return t.legacy?{$binary:e,$type:1===n.length?"0"+n:n}:{$binary:{base64:e,subType:1===n.length?"0"+n:n}}}toUUID(){if(this.sub_type===ft.SUBTYPE_UUID)return new Rt(this.buffer.subarray(0,this.position));throw new q(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${ft.SUBTYPE_UUID}" is currently supported.`)}static createFromHexString(t,e){return new ft(ht.fromHex(t),e)}static createFromBase64(t,e){return new ft(ht.fromBase64(t),e)}static fromExtendedJSON(t,e){let n,l;if(e=e||{},"$binary"in t?e.legacy&&"string"==typeof t.$binary&&"$type"in t?(l=t.$type?parseInt(t.$type,16):0,n=ht.fromBase64(t.$binary)):"string"!=typeof t.$binary&&(l=t.$binary.subType?parseInt(t.$binary.subType,16):0,n=ht.fromBase64(t.$binary.base64)):"$uuid"in t&&(l=4,n=Rt.bytesFromString(t.$uuid)),!n)throw new q(`Unexpected Binary Extended JSON format ${JSON.stringify(t)}`);return l===A?new Rt(n):new ft(n,l)}inspect(t,e,n){n??=V;return`Binary.createFromBase64(${n(ht.toBase64(this.buffer.subarray(0,this.position)),e)}, ${n(this.sub_type,e)})`}toInt8Array(){if(this.sub_type!==ft.SUBTYPE_VECTOR)throw new q("Binary sub_type is not Vector");if(this.buffer[0]!==ft.VECTOR_TYPE.Int8)throw new q("Binary datatype field is not Int8");return gt(this),new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset+2,this.buffer.byteOffset+this.position))}toFloat32Array(){if(this.sub_type!==ft.SUBTYPE_VECTOR)throw new q("Binary sub_type is not Vector");if(this.buffer[0]!==ft.VECTOR_TYPE.Float32)throw new q("Binary datatype field is not Float32");gt(this);const t=new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset+2,this.buffer.byteOffset+this.position));return Wt.isBigEndian&&ht.swap32(t),new Float32Array(t.buffer)}toPackedBits(){if(this.sub_type!==ft.SUBTYPE_VECTOR)throw new q("Binary sub_type is not Vector");if(this.buffer[0]!==ft.VECTOR_TYPE.PackedBit)throw new q("Binary datatype field is not packed bit");return gt(this),new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset+2,this.buffer.byteOffset+this.position))}toBits(){if(this.sub_type!==ft.SUBTYPE_VECTOR)throw new q("Binary sub_type is not Vector");if(this.buffer[0]!==ft.VECTOR_TYPE.PackedBit)throw new q("Binary datatype field is not packed bit");gt(this);const t=8*(this.length()-2)-this.buffer[1],e=new Int8Array(t);for(let t=0;t<e.length;t++){const n=t/8|0,l=this.buffer[n+2]>>7-t%8&1;e[t]=l}return e}static fromInt8Array(t){const e=ht.allocate(t.byteLength+2);e[0]=ft.VECTOR_TYPE.Int8,e[1]=0;const n=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);e.set(n,2);const l=new this(e,this.SUBTYPE_VECTOR);return gt(l),l}static fromFloat32Array(t){const e=ht.allocate(t.byteLength+2);e[0]=ft.VECTOR_TYPE.Float32,e[1]=0;const n=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);e.set(n,2),Wt.isBigEndian&&ht.swap32(new Uint8Array(e.buffer,2));const l=new this(e,this.SUBTYPE_VECTOR);return gt(l),l}static fromPackedBits(t,e=0){const n=ht.allocate(t.byteLength+2);n[0]=ft.VECTOR_TYPE.PackedBit,n[1]=e,n.set(t,2);const l=new this(n,this.SUBTYPE_VECTOR);return gt(l),l}static fromBits(t){const e=t.length+7>>>3,n=new Uint8Array(e+2);n[0]=ft.VECTOR_TYPE.PackedBit;const l=t.length%8;n[1]=0===l?0:8-l;for(let e=0;e<t.length;e++){const l=e>>>3,i=t[e];if(0!==i&&1!==i)throw new q(`Invalid bit value at ${e}: must be 0 or 1, found ${t[e]}`);if(0===i)continue;const o=7-e%8;n[l+2]|=i<<o}return new this(n,ft.SUBTYPE_VECTOR)}}function gt(t){if(t.sub_type!==ft.SUBTYPE_VECTOR)return;const e=t.position,n=t.buffer[0],l=t.buffer[1];if((n===ft.VECTOR_TYPE.Float32||n===ft.VECTOR_TYPE.Int8)&&0!==l)throw new q("Invalid Vector: padding must be zero for int8 and float32 vectors");if(n===ft.VECTOR_TYPE.Float32&&0!==e&&e-2!=0&&(e-2)%4!=0)throw new q("Invalid Vector: Float32 vector must contain a multiple of 4 bytes");if(n===ft.VECTOR_TYPE.PackedBit&&0!==l&&2===e)throw new q("Invalid Vector: padding must be zero for packed bit vectors that are empty");if(n===ft.VECTOR_TYPE.PackedBit&&l>7)throw new q(`Invalid Vector: padding must be a value between 0 and 7. found: ${l}`)}const Yt=/^[0-9A-F]{32}$/i,It=/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;class Rt extends ft{constructor(t){let e;if(null==t)e=Rt.generate();else if(t instanceof Rt)e=ht.toLocalBufferType(new Uint8Array(t.buffer));else if(ArrayBuffer.isView(t)&&16===t.byteLength)e=ht.toLocalBufferType(t);else{if("string"!=typeof t)throw new q("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).");e=Rt.bytesFromString(t)}super(e,A)}get id(){return this.buffer}set id(t){this.buffer=t}toHexString(t=!0){return t?[ht.toHex(this.buffer.subarray(0,4)),ht.toHex(this.buffer.subarray(4,6)),ht.toHex(this.buffer.subarray(6,8)),ht.toHex(this.buffer.subarray(8,10)),ht.toHex(this.buffer.subarray(10,16))].join("-"):ht.toHex(this.buffer)}toString(t){return"hex"===t?ht.toHex(this.id):"base64"===t?ht.toBase64(this.id):this.toHexString()}toJSON(){return this.toHexString()}equals(t){if(!t)return!1;if(t instanceof Rt)return ht.equals(t.id,this.id);try{return ht.equals(new Rt(t).id,this.id)}catch{return!1}}toBinary(){return new ft(this.id,ft.SUBTYPE_UUID)}static generate(){const t=ht.randomBytes(16);return t[6]=15&t[6]|64,t[8]=63&t[8]|128,t}static isValid(t){return!!t&&("string"==typeof t?Rt.isValidUUIDString(t):h(t)?16===t.byteLength:"Binary"===t._bsontype&&t.sub_type===this.SUBTYPE_UUID&&16===t.buffer.byteLength)}static createFromHexString(t){const e=Rt.bytesFromString(t);return new Rt(e)}static createFromBase64(t){return new Rt(ht.fromBase64(t))}static bytesFromString(t){if(!Rt.isValidUUIDString(t))throw new q("UUID string representation must be 32 hex digits or canonical hyphenated representation");return ht.fromHex(t.replace(/-/g,""))}static isValidUUIDString(t){return Yt.test(t)||It.test(t)}inspect(t,e,n){return n??=V,`new UUID(${n(this.toHexString(),e)})`}}class Jt extends yt{get _bsontype(){return"Code"}code;scope;constructor(t,e){super(),this.code=t.toString(),this.scope=e??null}toJSON(){return null!=this.scope?{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 Jt(t.$code,t.$scope)}inspect(t,e,n){n??=V;let l=n(this.code,e);const i=l.includes("\n");null!=this.scope&&(l+=`,${i?"\n":" "}${n(this.scope,e)}`);return`new Code(${i?"\n":""}${l}${i&&null===this.scope?"\n":""})`}}function St(t){return null!=t&&"object"==typeof t&&"$id"in t&&null!=t.$id&&"$ref"in t&&"string"==typeof t.$ref&&(!("$db"in t)||"$db"in t&&"string"==typeof t.$db)}class Kt extends yt{get _bsontype(){return"DBRef"}collection;oid;db;fields;constructor(t,e,n,l){super();const i=t.split(".");2===i.length&&(n=i.shift(),t=i.shift()),this.collection=t,this.oid=e,this.db=n,this.fields=l||{}}get namespace(){return this.collection}set namespace(t){this.collection=t}toJSON(){const t=Object.assign({$ref:this.collection,$id:this.oid},this.fields);return null!=this.db&&(t.$db=this.db),t}toExtendedJSON(t){t=t||{};let e={$ref:this.collection,$id:this.oid};return t.legacy||(this.db&&(e.$db=this.db),e=Object.assign(e,this.fields)),e}static fromExtendedJSON(t){const e=Object.assign({},t);return delete e.$ref,delete e.$id,delete e.$db,new Kt(t.$ref,t.$id,t.$db,e)}inspect(t,e,n){n??=V;const l=[n(this.namespace,e),n(this.oid,e),...this.db?[n(this.db,e)]:[],...Object.keys(this.fields).length>0?[n(this.fields,e)]:[]];return l[1]=n===V?`new ObjectId(${l[1]})`:l[1],`new DBRef(${l.join(", ")})`}}function Tt(t){if(""===t)return t;let e=0;const n="-"===t[e],l="+"===t[e];(l||n)&&(e+=1);let i=!1;for(;e<t.length&&"0"===t[e];++e)i=!0;return i?`${n?"-":""}${t.length===e?"0":t.slice(e)}`:l?t.slice(1):t}let Lt;try{Lt=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{}const zt=4294967296,xt=0x10000000000000000,Ut=xt/2,Nt={},wt={},kt=/^(\+?0|(\+|-)?[1-9][0-9]*)$/;class Ht extends yt{get _bsontype(){return"Long"}get __isLong__(){return!0}high;low;unsigned;constructor(t=0,e,n){super();const l="boolean"==typeof e?e:Boolean(n),i="number"==typeof e?e:0,o="string"==typeof t?Ht.fromString(t,l):"bigint"==typeof t?Ht.fromBigInt(t,l):{low:0|t,high:0|i,unsigned:l};this.low=o.low,this.high=o.high,this.unsigned=o.unsigned}static TWO_PWR_24=Ht.fromInt(16777216);static MAX_UNSIGNED_VALUE=Ht.fromBits(-1,-1,!0);static ZERO=Ht.fromInt(0);static UZERO=Ht.fromInt(0,!0);static ONE=Ht.fromInt(1);static UONE=Ht.fromInt(1,!0);static NEG_ONE=Ht.fromInt(-1);static MAX_VALUE=Ht.fromBits(-1,2147483647,!1);static MIN_VALUE=Ht.fromBits(0,-2147483648,!1);static fromBits(t,e,n){return new Ht(t,e,n)}static fromInt(t,e){let n,l,i;return e?(i=0<=(t>>>=0)&&t<256)&&(l=wt[t],l)?l:(n=Ht.fromBits(t,(0|t)<0?-1:0,!0),i&&(wt[t]=n),n):(i=-128<=(t|=0)&&t<128)&&(l=Nt[t],l)?l:(n=Ht.fromBits(t,t<0?-1:0,!1),i&&(Nt[t]=n),n)}static fromNumber(t,e){if(isNaN(t))return e?Ht.UZERO:Ht.ZERO;if(e){if(t<0)return Ht.UZERO;if(t>=xt)return Ht.MAX_UNSIGNED_VALUE}else{if(t<=-Ut)return Ht.MIN_VALUE;if(t+1>=Ut)return Ht.MAX_VALUE}return t<0?Ht.fromNumber(-t,e).neg():Ht.fromBits(t%zt|0,t/zt|0,e)}static fromBigInt(t,e){const n=0xffffffffn;return new Ht(Number(t&n),Number(t>>32n&n),e)}static _fromString(t,e,n){if(0===t.length)throw new q("empty string");if(n<2||36<n)throw new q("radix");let l;if((l=t.indexOf("-"))>0)throw new q("interior hyphen");if(0===l)return Ht._fromString(t.substring(1),e,n).neg();const i=Ht.fromNumber(Math.pow(n,8));let o=Ht.ZERO;for(let e=0;e<t.length;e+=8){const l=Math.min(8,t.length-e),c=parseInt(t.substring(e,e+l),n);if(l<8){const t=Ht.fromNumber(Math.pow(n,l));o=o.mul(t).add(Ht.fromNumber(c))}else o=o.mul(i),o=o.add(Ht.fromNumber(c))}return o.unsigned=e,o}static fromStringStrict(t,e,n){let l=!1;if("number"==typeof e?(n=e,e=!1):l=!!e,n??=10,t.trim()!==t)throw new q(`Input: '${t}' contains leading and/or trailing whitespace`);if(!function(t,e){const n="0123456789abcdefghijklmnopqrstuvwxyz".slice(0,e=e??10);return!new RegExp(`[^-+${n}]`,"i").test(t)&&t}(t,n))throw new q(`Input: '${t}' contains invalid characters for radix: ${n}`);const i=Tt(t),o=Ht._fromString(i,l,n);if(o.toString(n).toLowerCase()!==i.toLowerCase())throw new q(`Input: ${t} is not representable as ${o.unsigned?"an unsigned":"a signed"} 64-bit Long ${null!=n?`with radix: ${n}`:""}`);return o}static fromString(t,e,n){let l=!1;return"number"==typeof e?(n=e,e=!1):l=!!e,n??=10,"NaN"===t&&n<24||("Infinity"===t||"+Infinity"===t||"-Infinity"===t)&&n<35?Ht.ZERO:Ht._fromString(t,l,n)}static fromBytes(t,e,n){return n?Ht.fromBytesLE(t,e):Ht.fromBytesBE(t,e)}static fromBytesLE(t,e){return new Ht(t[0]|t[1]<<8|t[2]<<16|t[3]<<24,t[4]|t[5]<<8|t[6]<<16|t[7]<<24,e)}static fromBytesBE(t,e){return new Ht(t[4]<<24|t[5]<<16|t[6]<<8|t[7],t[0]<<24|t[1]<<16|t[2]<<8|t[3],e)}static isLong(t){return null!=t&&"object"==typeof t&&"__isLong__"in t&&!0===t.__isLong__}static fromValue(t,e){return"number"==typeof t?Ht.fromNumber(t,e):"string"==typeof t?Ht.fromString(t,e):Ht.fromBits(t.low,t.high,"boolean"==typeof e?e:t.unsigned)}add(t){Ht.isLong(t)||(t=Ht.fromValue(t));const e=this.high>>>16,n=65535&this.high,l=this.low>>>16,i=65535&this.low,o=t.high>>>16,c=65535&t.high,a=t.low>>>16;let s=0,d=0,b=0,Z=0;return Z+=i+(65535&t.low),b+=Z>>>16,Z&=65535,b+=l+a,d+=b>>>16,b&=65535,d+=n+c,s+=d>>>16,d&=65535,s+=e+o,s&=65535,Ht.fromBits(b<<16|Z,s<<16|d,this.unsigned)}and(t){return Ht.isLong(t)||(t=Ht.fromValue(t)),Ht.fromBits(this.low&t.low,this.high&t.high,this.unsigned)}compare(t){if(Ht.isLong(t)||(t=Ht.fromValue(t)),this.eq(t))return 0;const e=this.isNegative(),n=t.isNegative();return e&&!n?-1:!e&&n?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(Ht.isLong(t)||(t=Ht.fromValue(t)),t.isZero())throw new q("division by zero");if(Lt){if(!this.unsigned&&-2147483648===this.high&&-1===t.low&&-1===t.high)return this;const e=(this.unsigned?Lt.div_u:Lt.div_s)(this.low,this.high,t.low,t.high);return Ht.fromBits(e,Lt.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?Ht.UZERO:Ht.ZERO;let e,n,l;if(this.unsigned){if(t.unsigned||(t=t.toUnsigned()),t.gt(this))return Ht.UZERO;if(t.gt(this.shru(1)))return Ht.UONE;l=Ht.UZERO}else{if(this.eq(Ht.MIN_VALUE)){if(t.eq(Ht.ONE)||t.eq(Ht.NEG_ONE))return Ht.MIN_VALUE;if(t.eq(Ht.MIN_VALUE))return Ht.ONE;return e=this.shr(1).div(t).shl(1),e.eq(Ht.ZERO)?t.isNegative()?Ht.ONE:Ht.NEG_ONE:(n=this.sub(t.mul(e)),l=e.add(n.div(t)),l)}if(t.eq(Ht.MIN_VALUE))return this.unsigned?Ht.UZERO:Ht.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();l=Ht.ZERO}for(n=this;n.gte(t);){e=Math.max(1,Math.floor(n.toNumber()/t.toNumber()));const i=Math.ceil(Math.log(e)/Math.LN2),o=i<=48?1:Math.pow(2,i-48);let c=Ht.fromNumber(e),a=c.mul(t);for(;a.isNegative()||a.gt(n);)e-=o,c=Ht.fromNumber(e,this.unsigned),a=c.mul(t);c.isZero()&&(c=Ht.ONE),l=l.add(c),n=n.sub(a)}return l}div(t){return this.divide(t)}equals(t){return Ht.isLong(t)||(t=Ht.fromValue(t)),(this.unsigned===t.unsigned||this.high>>>31!=1||t.high>>>31!=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(Ht.MIN_VALUE)?64:this.neg().getNumBitsAbs();const t=0!==this.high?this.high:this.low;let e;for(e=31;e>0&&!(t&1<<e);e--);return 0!==this.high?e+33:e+1}greaterThan(t){return this.comp(t)>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!(1&this.low)}isNegative(){return!this.unsigned&&this.high<0}isOdd(){return!(1&~this.low)}isPositive(){return this.unsigned||this.high>=0}isZero(){return 0===this.high&&0===this.low}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(Ht.isLong(t)||(t=Ht.fromValue(t)),Lt){const e=(this.unsigned?Lt.rem_u:Lt.rem_s)(this.low,this.high,t.low,t.high);return Ht.fromBits(e,Lt.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 Ht.ZERO;if(Ht.isLong(t)||(t=Ht.fromValue(t)),Lt){const e=Lt.mul(this.low,this.high,t.low,t.high);return Ht.fromBits(e,Lt.get_high(),this.unsigned)}if(t.isZero())return Ht.ZERO;if(this.eq(Ht.MIN_VALUE))return t.isOdd()?Ht.MIN_VALUE:Ht.ZERO;if(t.eq(Ht.MIN_VALUE))return this.isOdd()?Ht.MIN_VALUE:Ht.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(Ht.TWO_PWR_24)&&t.lt(Ht.TWO_PWR_24))return Ht.fromNumber(this.toNumber()*t.toNumber(),this.unsigned);const e=this.high>>>16,n=65535&this.high,l=this.low>>>16,i=65535&this.low,o=t.high>>>16,c=65535&t.high,a=t.low>>>16,s=65535&t.low;let d=0,b=0,Z=0,m=0;return m+=i*s,Z+=m>>>16,m&=65535,Z+=l*s,b+=Z>>>16,Z&=65535,Z+=i*a,b+=Z>>>16,Z&=65535,b+=n*s,d+=b>>>16,b&=65535,b+=l*a,d+=b>>>16,b&=65535,b+=i*c,d+=b>>>16,b&=65535,d+=e*s+n*a+l*c+i*o,d&=65535,Ht.fromBits(Z<<16|m,d<<16|b,this.unsigned)}mul(t){return this.multiply(t)}negate(){return!this.unsigned&&this.eq(Ht.MIN_VALUE)?Ht.MIN_VALUE:this.not().add(Ht.ONE)}neg(){return this.negate()}not(){return Ht.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 Ht.isLong(t)||(t=Ht.fromValue(t)),Ht.fromBits(this.low|t.low,this.high|t.high,this.unsigned)}shiftLeft(t){return Ht.isLong(t)&&(t=t.toInt()),0==(t&=63)?this:t<32?Ht.fromBits(this.low<<t,this.high<<t|this.low>>>32-t,this.unsigned):Ht.fromBits(0,this.low<<t-32,this.unsigned)}shl(t){return this.shiftLeft(t)}shiftRight(t){return Ht.isLong(t)&&(t=t.toInt()),0==(t&=63)?this:t<32?Ht.fromBits(this.low>>>t|this.high<<32-t,this.high>>t,this.unsigned):Ht.fromBits(this.high>>t-32,this.high>=0?0:-1,this.unsigned)}shr(t){return this.shiftRight(t)}shiftRightUnsigned(t){if(Ht.isLong(t)&&(t=t.toInt()),0===(t&=63))return this;{const e=this.high;if(t<32){const n=this.low;return Ht.fromBits(n>>>t|e<<32-t,e>>>t,this.unsigned)}return 32===t?Ht.fromBits(e,0,this.unsigned):Ht.fromBits(e>>>t-32,0,this.unsigned)}}shr_u(t){return this.shiftRightUnsigned(t)}shru(t){return this.shiftRightUnsigned(t)}subtract(t){return Ht.isLong(t)||(t=Ht.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)*zt+(this.low>>>0):this.high*zt+(this.low>>>0)}toBigInt(){return BigInt(this.toString())}toBytes(t){return t?this.toBytesLE():this.toBytesBE()}toBytesLE(){const t=this.high,e=this.low;return[255&e,e>>>8&255,e>>>16&255,e>>>24,255&t,t>>>8&255,t>>>16&255,t>>>24]}toBytesBE(){const t=this.high,e=this.low;return[t>>>24,t>>>16&255,t>>>8&255,255&t,e>>>24,e>>>16&255,e>>>8&255,255&e]}toSigned(){return this.unsigned?Ht.fromBits(this.low,this.high,!1):this}toString(t){if((t=t||10)<2||36<t)throw new q("radix");if(this.isZero())return"0";if(this.isNegative()){if(this.eq(Ht.MIN_VALUE)){const e=Ht.fromNumber(t),n=this.div(e),l=n.mul(e).sub(this);return n.toString(t)+l.toInt().toString(t)}return"-"+this.neg().toString(t)}const e=Ht.fromNumber(Math.pow(t,6),this.unsigned);let n=this,l="";for(;;){const i=n.div(e);let o=(n.sub(i.mul(e)).toInt()>>>0).toString(t);if(n=i,n.isZero())return o+l;for(;o.length<6;)o="0"+o;l=""+o+l}}toUnsigned(){return this.unsigned?this:Ht.fromBits(this.low,this.high,!0)}xor(t){return Ht.isLong(t)||(t=Ht.fromValue(t)),Ht.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,e){const{useBigInt64:n=!1,relaxed:l=!0}={...e};if(t.$numberLong.length>20)throw new q("$numberLong string is too long");if(!kt.test(t.$numberLong))throw new q(`$numberLong string "${t.$numberLong}" is in an invalid format`);if(n){const e=BigInt(t.$numberLong);return BigInt.asIntN(64,e)}const i=Ht.fromString(t.$numberLong);return l?i.toNumber():i}inspect(t,e,n){n??=V;return`new Long(${n(this.toString(),e)}${this.unsigned?`, ${n(this.unsigned,e)}`:""})`}}const Ct=/^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/,Bt=/^(\+|-)?(Infinity|inf)$/i,vt=/^(\+|-)?NaN$/i,jt=6111,Mt=-6176,Pt=ht.fromNumberArray([124,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0].reverse()),Ft=ht.fromNumberArray([248,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0].reverse()),Qt=ht.fromNumberArray([120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0].reverse()),Ot=/^([-+])?(\d+)?$/;function Et(t){return!isNaN(parseInt(t,10))}function _t(t){const e=Ht.fromNumber(1e9);let n=Ht.fromNumber(0);if(!(t.parts[0]||t.parts[1]||t.parts[2]||t.parts[3]))return{quotient:t,rem:n};for(let l=0;l<=3;l++)n=n.shiftLeft(32),n=n.add(new Ht(t.parts[l],0)),t.parts[l]=n.div(e).low,n=n.modulo(e);return{quotient:t,rem:n}}function Dt(t,e){throw new q(`"${t}" is not a valid Decimal128 string - ${e}`)}class At extends yt{get _bsontype(){return"Decimal128"}bytes;constructor(t){if(super(),"string"==typeof t)this.bytes=At.fromString(t).bytes;else{if(!(t instanceof Uint8Array||h(t)))throw new q("Decimal128 must take a Buffer or string");if(16!==t.byteLength)throw new q("Decimal128 must take a Buffer of 16 bytes");this.bytes=t}}static fromString(t){return At._fromString(t,{allowRounding:!1})}static fromStringWithRounding(t){return At._fromString(t,{allowRounding:!0})}static _fromString(t,e){let n=!1,l=!1,i=!1,o=!1,c=0,a=0,s=0,d=0,b=0;const Z=[0];let m=0,r=0,u=0,h=0,p=new Ht(0,0),y=new Ht(0,0),X=0,G=0;if(t.length>=7e3)throw new q(t+" not a valid Decimal128 string");const V=t.match(Ct),W=t.match(Bt),f=t.match(vt);if(!V&&!W&&!f||0===t.length)throw new q(t+" not a valid Decimal128 string");if(V){const e=V[2],n=V[4],l=V[5],i=V[6];n&&void 0===i&&Dt(t,"missing exponent power"),n&&void 0===e&&Dt(t,"missing exponent base"),void 0===n&&(l||i)&&Dt(t,"missing e before exponent")}if("+"!==t[G]&&"-"!==t[G]||(l=!0,n="-"===t[G++]),!Et(t[G])&&"."!==t[G]){if("i"===t[G]||"I"===t[G])return new At(n?Ft:Qt);if("N"===t[G])return new At(Pt)}for(;Et(t[G])||"."===t[G];)"."!==t[G]?(m<34&&("0"!==t[G]||o)&&(o||(b=a),o=!0,Z[r++]=parseInt(t[G],10),m+=1),o&&(s+=1),i&&(d+=1),a+=1,G+=1):(i&&Dt(t,"contains multiple periods"),i=!0,G+=1);if(i&&!a)throw new q(t+" not a valid Decimal128 string");if("e"===t[G]||"E"===t[G]){const e=t.substr(++G).match(Ot);if(!e||!e[2])return new At(Pt);h=parseInt(e[0],10),G+=e[0].length}if(t[G])return new At(Pt);if(m){if(u=m-1,c=s,1!==c)for(;"0"===t[b+c-1+Number(l)+Number(i)];)c-=1}else Z[0]=0,s=1,m=1,c=0;for(h<=d&&d>h+16384?h=Mt:h-=d;h>jt;){if(u+=1,u>=34){if(0===c){h=jt;break}Dt(t,"overflow")}h-=1}if(e.allowRounding){for(;h<Mt||m<s;){if(0===u&&c<m){h=Mt,c=0;break}if(m<s?s-=1:u-=1,h<jt)h+=1;else{if(Z.join("").match(/^0+$/)){h=jt;break}Dt(t,"overflow")}}if(u+1<c){let e=a;i&&(b+=1,e+=1),l&&(b+=1,e+=1);const o=parseInt(t[b+u+1],10);let c=0;if(o>=5&&(c=1,5===o)){c=Z[u]%2==1?1:0;for(let n=b+u+2;n<e;n++)if(parseInt(t[n],10)){c=1;break}}if(c){let t=u;for(;t>=0&&++Z[t]>9;t--)if(Z[t]=0,0===t){if(!(h<jt))return new At(n?Ft:Qt);h+=1,Z[t]=1}}}}else{for(;h<Mt||m<s;){if(0===u){if(0===c){h=Mt;break}Dt(t,"exponent underflow")}m<s?("0"!==t[s-1+Number(l)+Number(i)]&&0!==c&&Dt(t,"inexact rounding"),s-=1):(0!==Z[u]&&Dt(t,"inexact rounding"),u-=1),h<jt?h+=1:Dt(t,"overflow")}if(u+1<c){i&&(b+=1),l&&(b+=1);0!==parseInt(t[b+u+1],10)&&Dt(t,"inexact rounding")}}if(p=Ht.fromNumber(0),y=Ht.fromNumber(0),0===c)p=Ht.fromNumber(0),y=Ht.fromNumber(0);else if(u<17){let t=0;for(y=Ht.fromNumber(Z[t++]),p=new Ht(0,0);t<=u;t++)y=y.multiply(Ht.fromNumber(10)),y=y.add(Ht.fromNumber(Z[t]))}else{let t=0;for(p=Ht.fromNumber(Z[t++]);t<=u-17;t++)p=p.multiply(Ht.fromNumber(10)),p=p.add(Ht.fromNumber(Z[t]));for(y=Ht.fromNumber(Z[t++]);t<=u;t++)y=y.multiply(Ht.fromNumber(10)),y=y.add(Ht.fromNumber(Z[t]))}const g=function(t,e){if(!t&&!e)return{high:Ht.fromNumber(0),low:Ht.fromNumber(0)};const n=t.shiftRightUnsigned(32),l=new Ht(t.getLowBits(),0),i=e.shiftRightUnsigned(32),o=new Ht(e.getLowBits(),0);let c=n.multiply(i),a=n.multiply(o);const s=l.multiply(i);let d=l.multiply(o);return c=c.add(a.shiftRightUnsigned(32)),a=new Ht(a.getLowBits(),0).add(s).add(d.shiftRightUnsigned(32)),c=c.add(a.shiftRightUnsigned(32)),d=a.shiftLeft(32).add(new Ht(d.getLowBits(),0)),{high:c,low:d}}(p,Ht.fromString("100000000000000000"));g.low=g.low.add(y),function(t,e){const n=t.high>>>0,l=e.high>>>0;if(n<l)return!0;if(n===l&&t.low>>>0<e.low>>>0)return!0;return!1}(g.low,y)&&(g.high=g.high.add(Ht.fromNumber(1))),X=h+6176;const Y={low:Ht.fromNumber(0),high:Ht.fromNumber(0)};g.high.shiftRightUnsigned(49).and(Ht.fromNumber(1)).equals(Ht.fromNumber(1))?(Y.high=Y.high.or(Ht.fromNumber(3).shiftLeft(61)),Y.high=Y.high.or(Ht.fromNumber(X).and(Ht.fromNumber(16383).shiftLeft(47))),Y.high=Y.high.or(g.high.and(Ht.fromNumber(0x7fffffffffff)))):(Y.high=Y.high.or(Ht.fromNumber(16383&X).shiftLeft(49)),Y.high=Y.high.or(g.high.and(Ht.fromNumber(562949953421311)))),Y.low=g.low,n&&(Y.high=Y.high.or(Ht.fromString("9223372036854775808")));const I=ht.allocateUnsafe(16);return G=0,I[G++]=255&Y.low.low,I[G++]=Y.low.low>>8&255,I[G++]=Y.low.low>>16&255,I[G++]=Y.low.low>>24&255,I[G++]=255&Y.low.high,I[G++]=Y.low.high>>8&255,I[G++]=Y.low.high>>16&255,I[G++]=Y.low.high>>24&255,I[G++]=255&Y.high.low,I[G++]=Y.high.low>>8&255,I[G++]=Y.high.low>>16&255,I[G++]=Y.high.low>>24&255,I[G++]=255&Y.high.high,I[G++]=Y.high.high>>8&255,I[G++]=Y.high.high>>16&255,I[G++]=Y.high.high>>24&255,new At(I)}toString(){let t,e=0;const n=new Array(36);for(let t=0;t<n.length;t++)n[t]=0;let l,i,o,c=0,a=!1,s={parts:[0,0,0,0]};const d=[];c=0;const b=this.bytes,Z=b[c++]|b[c++]<<8|b[c++]<<16|b[c++]<<24,m=b[c++]|b[c++]<<8|b[c++]<<16|b[c++]<<24,r=b[c++]|b[c++]<<8|b[c++]<<16|b[c++]<<24,u=b[c++]|b[c++]<<8|b[c++]<<16|b[c++]<<24;c=0;({low:new Ht(Z,m),high:new Ht(r,u)}).high.lessThan(Ht.ZERO)&&d.push("-");const h=u>>26&31;if(h>>3==3){if(30===h)return d.join("")+"Infinity";if(31===h)return"NaN";t=u>>15&16383,l=8+(u>>14&1)}else l=u>>14&7,t=u>>17&16383;const p=t-6176;if(s.parts[0]=(16383&u)+((15&l)<<14),s.parts[1]=r,s.parts[2]=m,s.parts[3]=Z,0===s.parts[0]&&0===s.parts[1]&&0===s.parts[2]&&0===s.parts[3])a=!0;else for(o=3;o>=0;o--){let t=0;const e=_t(s);if(s=e.quotient,t=e.rem.low,t)for(i=8;i>=0;i--)n[9*o+i]=t%10,t=Math.floor(t/10)}if(a)e=1,n[c]=0;else for(e=36;!n[c];)e-=1,c+=1;const y=e-1+p;if(y>=34||y<=-7||p>0){if(e>34)return d.push("0"),p>0?d.push(`E+${p}`):p<0&&d.push(`E${p}`),d.join("");d.push(`${n[c++]}`),e-=1,e&&d.push(".");for(let t=0;t<e;t++)d.push(`${n[c++]}`);d.push("E"),y>0?d.push(`+${y}`):d.push(`${y}`)}else if(p>=0)for(let t=0;t<e;t++)d.push(`${n[c++]}`);else{let t=e+p;if(t>0)for(let e=0;e<t;e++)d.push(`${n[c++]}`);else d.push("0");for(d.push(".");t++<0;)d.push("0");for(let l=0;l<e-Math.max(t-1,0);l++)d.push(`${n[c++]}`)}return d.join("")}toJSON(){return{$numberDecimal:this.toString()}}toExtendedJSON(){return{$numberDecimal:this.toString()}}static fromExtendedJSON(t){return At.fromString(t.$numberDecimal)}inspect(t,e,n){n??=V;return`new Decimal128(${n(this.toString(),e)})`}}class $t extends yt{get _bsontype(){return"Double"}value;constructor(t){super(),t instanceof Number&&(t=t.valueOf()),this.value=+t}static fromString(t){const e=Number(t);if("NaN"===t)return new $t(NaN);if("Infinity"===t)return new $t(1/0);if("-Infinity"===t)return new $t(-1/0);if(!Number.isFinite(e))throw new q(`Input: ${t} is not representable as a Double`);if(t.trim()!==t)throw new q(`Input: '${t}' contains whitespace`);if(""===t)throw new q("Input is an empty string");if(/[^-0-9.+eE]/.test(t))throw new q(`Input: '${t}' is not in decimal or exponential notation`);return new $t(e)}valueOf(){return this.value}toJSON(){return this.value}toString(t){return this.value.toString(t)}toExtendedJSON(t){return t&&(t.legacy||t.relaxed&&isFinite(this.value))?this.value:Object.is(Math.sign(this.value),-0)?{$numberDouble:"-0.0"}:{$numberDouble:Number.isInteger(this.value)?this.value.toFixed(1):this.value.toString()}}static fromExtendedJSON(t,e){const n=parseFloat(t.$numberDouble);return e&&e.relaxed?n:new $t(n)}inspect(t,e,n){return n??=V,`new Double(${n(this.value,e)})`}}class qt extends yt{get _bsontype(){return"Int32"}value;constructor(t){super(),t instanceof Number&&(t=t.valueOf()),this.value=0|+t}static fromString(t){const e=Tt(t),n=Number(t);if(g<n)throw new q(`Input: '${t}' is larger than the maximum value for Int32`);if(Y>n)throw new q(`Input: '${t}' is smaller than the minimum value for Int32`);if(!Number.isSafeInteger(n))throw new q(`Input: '${t}' is not a safe integer`);if(n.toString()!==e)throw new q(`Input: '${t}' is not a valid Int32 string`);return new qt(n)}valueOf(){return this.value}toString(t){return this.value.toString(t)}toJSON(){return this.value}toExtendedJSON(t){return t&&(t.relaxed||t.legacy)?this.value:{$numberInt:this.value.toString()}}static fromExtendedJSON(t,e){return e&&e.relaxed?parseInt(t.$numberInt,10):new qt(t.$numberInt)}inspect(t,e,n){return n??=V,`new Int32(${n(this.value,e)})`}}class te extends yt{get _bsontype(){return"MaxKey"}toExtendedJSON(){return{$maxKey:1}}static fromExtendedJSON(){return new te}inspect(){return"new MaxKey()"}}class ee extends yt{get _bsontype(){return"MinKey"}toExtendedJSON(){return{$minKey:1}}static fromExtendedJSON(){return new ee}inspect(){return"new MinKey()"}}let ne=null;const le=new WeakMap;class ie extends yt{get _bsontype(){return"ObjectId"}static index=Math.floor(16777215*Math.random());static cacheHexString;buffer;constructor(t){let e;if(super(),"object"==typeof t&&t&&"id"in t){if("string"!=typeof t.id&&!ArrayBuffer.isView(t.id))throw new q("Argument passed in must have an id that is of type string or Buffer");e="toHexString"in t&&"function"==typeof t.toHexString?ht.fromHex(t.toHexString()):t.id}else e=t;if(null==e)this.buffer=ie.generate();else if(ArrayBuffer.isView(e)&&12===e.byteLength)this.buffer=ht.toLocalBufferType(e);else{if("string"!=typeof e)throw new q("Argument passed in does not match the accepted types");if(!ie.validateHexString(e))throw new q("input must be a 24 character hex string, 12 byte Uint8Array, or an integer");this.buffer=ht.fromHex(e),ie.cacheHexString&&le.set(this,e)}}get id(){return this.buffer}set id(t){this.buffer=t,ie.cacheHexString&&le.set(this,ht.toHex(t))}static validateHexString(t){if(24!==t?.length)return!1;for(let e=0;e<24;e++){const n=t.charCodeAt(e);if(!(n>=48&&n<=57||n>=97&&n<=102||n>=65&&n<=70))return!1}return!0}toHexString(){if(ie.cacheHexString){const t=le.get(this);if(t)return t}const t=ht.toHex(this.id);return ie.cacheHexString&&le.set(this,t),t}static getInc(){return ie.index=(ie.index+1)%16777215}static generate(t){"number"!=typeof t&&(t=Math.floor(Date.now()/1e3));const e=ie.getInc(),n=ht.allocateUnsafe(12);return Wt.setInt32BE(n,0,t),null===ne&&(ne=ht.randomBytes(5)),n[4]=ne[0],n[5]=ne[1],n[6]=ne[2],n[7]=ne[3],n[8]=ne[4],n[11]=255&e,n[10]=e>>8&255,n[9]=e>>16&255,n}toString(t){return"base64"===t?ht.toBase64(this.id):this.toHexString()}toJSON(){return this.toHexString()}static is(t){return null!=t&&"object"==typeof t&&"_bsontype"in t&&"ObjectId"===t._bsontype}equals(t){if(null==t)return!1;if(ie.is(t))return this.buffer[11]===t.buffer[11]&&ht.equals(this.buffer,t.buffer);if("string"==typeof t)return t.toLowerCase()===this.toHexString();if("object"==typeof t&&"function"==typeof t.toHexString){const e=t.toHexString(),n=this.toHexString();return"string"==typeof e&&e.toLowerCase()===n}return!1}getTimestamp(){const t=new Date,e=Wt.getUint32BE(this.buffer,0);return t.setTime(1e3*Math.floor(e)),t}static createPk(){return new ie}serializeInto(t,e){return t[e]=this.buffer[0],t[e+1]=this.buffer[1],t[e+2]=this.buffer[2],t[e+3]=this.buffer[3],t[e+4]=this.buffer[4],t[e+5]=this.buffer[5],t[e+6]=this.buffer[6],t[e+7]=this.buffer[7],t[e+8]=this.buffer[8],t[e+9]=this.buffer[9],t[e+10]=this.buffer[10],t[e+11]=this.buffer[11],12}static createFromTime(t){const e=ht.allocate(12);for(let t=11;t>=4;t--)e[t]=0;return Wt.setInt32BE(e,0,t),new ie(e)}static createFromHexString(t){if(24!==t?.length)throw new q("hex string must be 24 characters");return new ie(ht.fromHex(t))}static createFromBase64(t){if(16!==t?.length)throw new q("base64 string must be 16 characters");return new ie(ht.fromBase64(t))}static isValid(t){if(null==t)return!1;if("string"==typeof t)return ie.validateHexString(t);try{return new ie(t),!0}catch{return!1}}toExtendedJSON(){return this.toHexString?{$oid:this.toHexString()}:{$oid:this.toString("hex")}}static fromExtendedJSON(t){return new ie(t.$oid)}isCached(){return ie.cacheHexString&&le.has(this)}inspect(t,e,n){return n??=V,`new ObjectId(${n(this.toHexString(),e)})`}}function oe(t,e,n){let l=5;if(Array.isArray(t))for(let i=0;i<t.length;i++)l+=ce(i.toString(),t[i],e,!0,n);else{"function"==typeof t?.toBSON&&(t=t.toBSON());for(const i of Object.keys(t))l+=ce(i,t[i],e,!1,n)}return l}function ce(t,e,n=!1,l=!1,i=!1){switch("function"==typeof e?.toBSON&&(e=e.toBSON()),typeof e){case"string":return 1+ht.utf8ByteLength(t)+1+4+ht.utf8ByteLength(e)+1;case"number":return Math.floor(e)===e&&e>=S&&e<=J&&e>=Y&&e<=g?(null!=t?ht.utf8ByteLength(t)+1:0)+5:(null!=t?ht.utf8ByteLength(t)+1:0)+9;case"undefined":return l||!i?(null!=t?ht.utf8ByteLength(t)+1:0)+1:0;case"boolean":return(null!=t?ht.utf8ByteLength(t)+1:0)+2;case"object":if(null!=e&&"string"==typeof e._bsontype&&e[f]!==W)throw new tt;if(null==e||"MinKey"===e._bsontype||"MaxKey"===e._bsontype)return(null!=t?ht.utf8ByteLength(t)+1:0)+1;if("ObjectId"===e._bsontype)return(null!=t?ht.utf8ByteLength(t)+1:0)+13;if(e instanceof Date||G(e))return(null!=t?ht.utf8ByteLength(t)+1:0)+9;if(ArrayBuffer.isView(e)||e instanceof ArrayBuffer||p(e))return(null!=t?ht.utf8ByteLength(t)+1:0)+6+e.byteLength;if("Long"===e._bsontype||"Double"===e._bsontype||"Timestamp"===e._bsontype)return(null!=t?ht.utf8ByteLength(t)+1:0)+9;if("Decimal128"===e._bsontype)return(null!=t?ht.utf8ByteLength(t)+1:0)+17;if("Code"===e._bsontype)return null!=e.scope&&Object.keys(e.scope).length>0?(null!=t?ht.utf8ByteLength(t)+1:0)+1+4+4+ht.utf8ByteLength(e.code.toString())+1+oe(e.scope,n,i):(null!=t?ht.utf8ByteLength(t)+1:0)+1+4+ht.utf8ByteLength(e.code.toString())+1;if("Binary"===e._bsontype){const