UNPKG

fauna

Version:

A driver to query Fauna databases in browsers, Node.js, and other Javascript runtimes

7 lines (6 loc) 37 kB
var er=Object.create;var Et=Object.defineProperty;var tr=Object.getOwnPropertyDescriptor;var rr=Object.getOwnPropertyNames;var nr=Object.getPrototypeOf,sr=Object.prototype.hasOwnProperty;var nt=(t=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(t,{get:(e,r)=>(typeof require<"u"?require:e)[r]}):t)(function(t){if(typeof require<"u")return require.apply(this,arguments);throw new Error('Dynamic require of "'+t+'" is not supported')});var or=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var ar=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of rr(e))!sr.call(t,n)&&n!==r&&Et(t,n,{get:()=>e[n],enumerable:!(s=tr(e,n))||s.enumerable});return t};var ir=(t,e,r)=>(r=t!=null?er(nr(t)):{},ar(e||!t||!t.__esModule?Et(r,"default",{value:t,enumerable:!0}):r,t));var st=(t,e,r)=>{if(!e.has(t))throw TypeError("Cannot "+r)};var a=(t,e,r)=>(st(t,e,"read from private field"),r?r.call(t):e.get(t)),u=(t,e,r)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,r)},f=(t,e,r,s)=>(st(t,e,"write to private field"),s?s.call(t,r):e.set(t,r),r),ot=(t,e,r,s)=>({set _(n){f(t,e,n,r)},get _(){return a(t,e,s)}}),l=(t,e,r)=>(st(t,e,"access private method"),r);var Qt=or(Ve=>{"use strict";Ve.byteLength=lr;Ve.toByteArray=pr;Ve.fromByteArray=hr;var I=[],C=[],mr=typeof Uint8Array<"u"?Uint8Array:Array,mt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(te=0,Pt=mt.length;te<Pt;++te)I[te]=mt[te],C[mt.charCodeAt(te)]=te;var te,Pt;C["-".charCodeAt(0)]=62;C["_".charCodeAt(0)]=63;function Rt(t){var e=t.length;if(e%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var r=t.indexOf("=");r===-1&&(r=e);var s=r===e?0:4-r%4;return[r,s]}function lr(t){var e=Rt(t),r=e[0],s=e[1];return(r+s)*3/4-s}function fr(t,e,r){return(e+r)*3/4-r}function pr(t){var e,r=Rt(t),s=r[0],n=r[1],o=new mr(fr(t,s,n)),i=0,d=n>0?s-4:s,c;for(c=0;c<d;c+=4)e=C[t.charCodeAt(c)]<<18|C[t.charCodeAt(c+1)]<<12|C[t.charCodeAt(c+2)]<<6|C[t.charCodeAt(c+3)],o[i++]=e>>16&255,o[i++]=e>>8&255,o[i++]=e&255;return n===2&&(e=C[t.charCodeAt(c)]<<2|C[t.charCodeAt(c+1)]>>4,o[i++]=e&255),n===1&&(e=C[t.charCodeAt(c)]<<10|C[t.charCodeAt(c+1)]<<4|C[t.charCodeAt(c+2)]>>2,o[i++]=e>>8&255,o[i++]=e&255),o}function yr(t){return I[t>>18&63]+I[t>>12&63]+I[t>>6&63]+I[t&63]}function gr(t,e,r){for(var s,n=[],o=e;o<r;o+=3)s=(t[o]<<16&16711680)+(t[o+1]<<8&65280)+(t[o+2]&255),n.push(yr(s));return n.join("")}function hr(t){for(var e,r=t.length,s=r%3,n=[],o=16383,i=0,d=r-s;i<d;i+=o)n.push(gr(t,i,i+o>d?d:i+o));return s===1?(e=t[r-1],n.push(I[e>>2]+I[e<<4&63]+"==")):s===2&&(e=(t[r-2]<<8)+t[r-1],n.push(I[e>>10]+I[e>>4&63]+I[e<<2&63]+"=")),n.join("")}});var at={default:new URL("https://db.fauna.com"),local:new URL("http://localhost:8443"),localhost:new URL("http://localhost:8443")};var Q=class extends Error{constructor(...e){super(...e)}},h=class extends Q{httpStatus;code;queryInfo;constraint_failures;constructor(e,r){super(e.error.message),Error.captureStackTrace&&Error.captureStackTrace(this,h),this.name="ServiceError",this.code=e.error.code,this.httpStatus=r;let s={txn_ts:e.txn_ts,summary:e.summary,query_tags:e.query_tags,stats:e.stats};this.queryInfo=s,this.constraint_failures=e.error.constraint_failures}},ae=class extends h{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,ae),this.name="QueryRuntimeError"}},G=class extends h{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,G),this.name="QueryCheckError"}},Z=class extends h{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,Z),this.name="InvalidRequestError"}},ve=class extends h{constraint_failures;constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,G),this.name="ConstraintFailureError",this.constraint_failures=e.error.constraint_failures}},Fe=class extends h{abort;constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,G),this.name="AbortError",this.abort=e.error.abort}},ie=class extends h{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,ie),this.name="AuthenticationError"}},ue=class extends h{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,ue),this.name="AuthorizationError"}},Oe=class extends h{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,Z),this.name="ContendedTransactionError"}},V=class extends h{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,V),this.name="ThrottlingError"}},ce=class extends h{stats;constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,ce),this.name="QueryTimeoutError",this.stats=e.stats}},de=class extends h{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,de),this.name="ServiceInternalError"}},y=class extends Q{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,y),this.name="ClientError"}},v=class extends Q{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,v),this.name="ClientClosedError"}},w=class extends Q{constructor(e,r){super(e,r),Error.captureStackTrace&&Error.captureStackTrace(this,w),this.name="NetworkError"}},F=class extends Q{httpStatus;constructor(e){super(e.message),Error.captureStackTrace&&Error.captureStackTrace(this,F),this.name="ProtocolError",this.httpStatus=e.httpStatus}},O=(t,e)=>{switch(t.error.code){case"invalid_query":return new G(t,e);case"invalid_request":return new Z(t,e);case"abort":if(t.error.abort!==void 0)return new Fe(t,e);break;case"constraint_failure":if(t.error.constraint_failures!==void 0)return new ve(t,e);break;case"unauthorized":return new ie(t,e);case"forbidden":return new ue(t,e);case"contended_transaction":return new Oe(t,e);case"limit_exceeded":return new V(t,e);case"time_out":return new ce(t,e);case"internal_error":return new de(t,e)}return new ae(t,e)};var j={QUERY:"/query/1",STREAM:"/stream/1",EVENT_FEED:"/feed/1"};var be=class{#e;#t=j.QUERY;#r=j.STREAM;#n;constructor({url:e,fetch_keepalive:r}){this.#e=e,this.#n=r}#s(e){return new URL(e,this.#e).toString()}async request({data:e,headers:r,method:s,client_timeout_ms:n,path:o=this.#t}){let i=AbortSignal.timeout===void 0?(()=>{let g=new AbortController,x=g.signal;return setTimeout(()=>g.abort(),n),x})():AbortSignal.timeout(n),d=await fetch(this.#s(o),{method:s,headers:{...r,"Content-Type":"application/json"},body:JSON.stringify(e),signal:i,keepalive:this.#n}).catch(g=>{throw new w("The network connection encountered a problem.",{cause:g})}),c=d.status,m={};d.headers.forEach((g,x)=>m[x]=g);let p=await d.text();return{status:c,body:p,headers:m}}stream({data:e,headers:r,method:s,path:n=this.#r}){let o=new Request(this.#s(n),{method:s,headers:{...r,"Content-Type":"application/json"},body:JSON.stringify(e),keepalive:this.#n}),i=new AbortController,d={signal:i.signal};async function*c(){let m=await fetch(o,d).catch(T=>{throw new w("The network connection encountered a problem.",{cause:T})}),p=m.status;if(!(p>=200&&p<400)){let T=await m.json();throw O(T,p)}let g=m.body;if(!g)throw new Error("Response body is undefined.");let x=g.getReader();try{for await(let T of ur(x))yield T}catch(T){throw new w("The network connection encountered a problem while streaming events.",{cause:T})}}return{read:c(),close:()=>{i.abort("Stream closed by the client.")}}}close(){}};async function*ur(t){let e=new TextDecoder,r="";for await(let s of cr(t)){let n=e.decode(s),o=(r+n).split(` `);for(let i=0;i<o.length-1;i++)yield o[i].trim();r=o[o.length-1]}r.trim()!==""&&(yield r)}async function*cr(t){let e=!1;do{let r=await t.read();r.value!==void 0&&(yield r.value),e=r.done}while(!e)}var k;try{k=nt("node:http2")}catch{k=void 0}var me,Se,xe,we,z,E,Ie,qe,Ne,_t,le,ke,Ee,it,De,Ct,He,At,K=class{constructor({http2_session_idle_ms:e,url:r,http2_max_streams:s}){u(this,le);u(this,Ee);u(this,De);u(this,He);u(this,Se,void 0);u(this,xe,void 0);u(this,we,void 0);u(this,z,0);u(this,E,void 0);u(this,Ie,j.QUERY);u(this,qe,j.STREAM);if(k===void 0)throw new Error("Your platform does not support Node's http2 library");f(this,Se,e),f(this,xe,s),f(this,we,r),f(this,E,null)}static getClient(e){var n;let r=l(n=K,Ne,_t).call(n,e);a(K,me).has(r)||a(K,me).set(r,new K(e));let s=a(K,me).get(r);return ot(s,z)._++,s}async request(e){let r=0,s;do try{return await l(this,De,Ct).call(this,e)}catch(n){if(n?.code!=="ERR_HTTP2_GOAWAY_SESSION")throw new w("The network connection encountered a problem.",{cause:n});s=n,r++}while(r<3);throw new w("The network connection encountered a problem.",{cause:s})}stream(e){return l(this,He,At).call(this,e)}close(){this.isClosed()||(ot(this,z)._--,a(this,z)===0&&a(this,E)&&!a(this,E).closed&&a(this,E).close())}isClosed(){return a(this,z)===0}},ee=K;me=new WeakMap,Se=new WeakMap,xe=new WeakMap,we=new WeakMap,z=new WeakMap,E=new WeakMap,Ie=new WeakMap,qe=new WeakMap,Ne=new WeakSet,_t=function({http2_session_idle_ms:e,url:r}){return`${r}|${e}`},le=new WeakSet,ke=function(){f(this,z,0),a(this,E)&&!a(this,E).closed&&a(this,E).close()},Ee=new WeakSet,it=function(){if(!a(this,E)||a(this,E).closed||a(this,E).destroyed){let e=k.connect(a(this,we),{peerMaxConcurrentStreams:a(this,xe)}).once("error",()=>l(this,le,ke).call(this)).once("goaway",()=>l(this,le,ke).call(this));e.setTimeout(a(this,Se),()=>{l(this,le,ke).call(this)}),f(this,E,e)}return a(this,E)},De=new WeakSet,Ct=function({client_timeout_ms:e,data:r,headers:s,method:n,path:o=a(this,Ie)}){return new Promise((i,d)=>{let c,m=p=>{let g=Number(p[k.constants.HTTP2_HEADER_STATUS]),x="";c.on("data",T=>{x+=T}),c.on("end",()=>{i({status:g,body:x,headers:p})})};try{let p={...s,[k.constants.HTTP2_HEADER_PATH]:o,[k.constants.HTTP2_HEADER_METHOD]:n};c=l(this,Ee,it).call(this).request(p).setEncoding("utf8").on("error",x=>{d(x)}).on("response",m),c.write(JSON.stringify(r),"utf8"),c.setTimeout(e,()=>{c.destroy(new Error("Client timeout"))}),c.end()}catch(p){d(p)}})},He=new WeakSet,At=function({data:e,headers:r,method:s,path:n=a(this,qe)}){let o,i,d=()=>new Promise((T,oe)=>{o=T,i=oe}),c=d(),m,p=T=>{let oe=Number(T[k.constants.HTTP2_HEADER_STATUS]);if(oe>=200&&oe<400){let $="";m.on("data",R=>{let X=($+R).split(` `);o(X.map(rt=>rt.trim()).slice(0,-1)),c=d(),$=X[X.length-1]}),m.on("end",()=>{o([$])})}else{let $="";m.on("data",R=>{$+=R}),m.on("end",()=>{try{let R=JSON.parse($);i(O(R,oe))}catch(R){i(new w("Could not process query failure.",{cause:R}))}})}},g=this;async function*x(){var R;let T={...r,[k.constants.HTTP2_HEADER_PATH]:n,[k.constants.HTTP2_HEADER_METHOD]:s};m=l(R=g,Ee,it).call(R).request(T).setEncoding("utf8").on("error",X=>{i(X)}).on("response",p);let $=JSON.stringify(e);for(m.write($,"utf8"),m.end();;){let X=await c;for(let rt of X)yield rt}}return{read:x(),close:()=>{m&&m.close()}}},u(ee,Ne),u(ee,me,new Map);var ut=t=>dr()?ee.getClient(t):new be(t),ct=t=>t instanceof Object&&"body"in t&&"headers"in t&&"status"in t,dt=t=>"stream"in t&&typeof t.stream=="function",dr=()=>{if(typeof process<"u"&&process&&process.release?.name==="node")try{return nt("node:http2"),!0}catch{return!1}return!1};var pt=ir(Qt());var Tr=/(?:\d{4}|[\u2212-]\d{4,}|\+\d{5,})/,br=/(?:0[1-9]|1[0-2])/,Sr=/(?:0[1-9]|[12]\d|3[01])/,vt=/(?:[01][0-9]|2[0-3])/,_e=/(?:[0-5][0-9])/,xr=/(?:\.\d+)/,lt=new RegExp(`(${Tr.source}-(${br.source})-(${Sr.source}))`),wr=new RegExp(`(${vt.source}:${_e.source}:${_e.source}${xr.source}?)`),Er=new RegExp(`([zZ]|[+\u2212-]${vt.source}(?::?${_e.source}|:${_e.source}:${_e.source}))`),Ft=new RegExp(`^${lt.source}$`),Ot=new RegExp(`^${lt.source}`),kt=new RegExp(`^${lt.source}T${wr.source}${Er.source}$`);var q=class{isoString;constructor(e){this.isoString=e}static from(e){if(typeof e!="string")throw new TypeError(`Expected string but received ${typeof e}: ${e}`);if(kt.exec(e)===null)throw new RangeError(`(regex) Expected an ISO date string but received '${e}'`);return new q(e)}static fromDate(e){return new q(e.toISOString())}toDate(){let e=new Date(this.isoString);if(e.toString()==="Invalid Date")throw new RangeError("Fauna Date could not be converted to Javascript Date");return e}toString(){return`TimeStub("${this.isoString}")`}},N=class{dateString;constructor(e){this.dateString=e}static from(e){if(typeof e!="string")throw new TypeError(`Expected string but received ${typeof e}: ${e}`);let r=Ft.exec(e);if(r===null)throw new RangeError(`Expected a plain date string but received '${e}'`);return new N(r[0])}static fromDate(e){let r=e.toISOString(),s=Ot.exec(r);if(s===null)throw new y(`Failed to parse date '${e}'`);return new N(s[0])}toDate(){let e=new Date(this.dateString+"T00:00:00Z");if(e.toString()==="Invalid Date")throw new RangeError("Fauna Date could not be converted to Javascript Date");return e}toString(){return`DateStub("${this.dateString}")`}};var M=class{coll;id;constructor({coll:e,id:r}){this.id=r,typeof e=="string"?this.coll=new W(e):this.coll=e}},fe=class extends M{ts;ttl;constructor(e){let{coll:r,id:s,ts:n,...o}=e;super({coll:r,id:s}),this.ts=n,Object.assign(this,o)}toObject(){return{...this}}},Y=class{coll;name;constructor({coll:e,name:r}){this.name=r,typeof e=="string"?this.coll=new W(e):this.coll=e}},pe=class extends Y{ts;data;constructor(e){let{coll:r,name:s,ts:n,data:o,...i}=e;super({coll:r,name:s}),this.ts=n,this.data=o||{},Object.assign(this,i)}toObject(){return{...this}}},W=class{name;constructor(e){this.name=e}},re=class{ref;cause;constructor(e,r){this.ref=e,this.cause=r}};var D=class{data;after;constructor({data:e,after:r}){this.data=e,this.after=r}},L=class{after;constructor(e){this.after=e}},U=class{#e;constructor(e,r,s){if(s=s??{},r instanceof Function)this.#e=Cr(e,r,s);else if(r instanceof D||r instanceof L)this.#e=It(e,r,s);else throw new TypeError(`Expected 'Page<T> | EmbeddedSet | (() => Promise<T | Page<T> | EmbeddedSet>)', but received ${JSON.stringify(r)}`)}static fromQuery(e,r,s){return new U(e,async()=>(await e.query(r,s)).data,s)}static fromPageable(e,r,s){return new U(e,r,s)}flatten(){return new je(this)}async next(){return this.#e.next()}async return(){return this.#e.return()}async throw(e){return this.#e.throw(e)}[Symbol.asyncIterator](){return this}},je=class{#e;constructor(e){this.#e=Ar(e)}async next(){return this.#e.next()}async return(){return this.#e.return()}async throw(e){return this.#e.throw(e)}[Symbol.asyncIterator](){return this}};async function*It(t,e,r){let s=e;for(s instanceof D&&(yield s.data);s.after;){let n=ft`Set.paginate(${s.after})`;s=(await t.query(n,r)).data,yield s.data}}async function*Cr(t,e,r){let s=await e();if(s instanceof D||s instanceof L){for await(let n of It(t,s,r))yield n;return}yield[s]}async function*Ar(t){for await(let e of t)for(let r of e)yield r}function Ae(t){return typeof t.token=="string"}var ne=class{token;constructor(e){this.token=e}},Ce=class{events;cursor;hasNext;stats;constructor({events:e,cursor:r,has_next:s,stats:n}){this.events=this.#e(e),this.cursor=r,this.hasNext=s,this.stats=n}*#e(e){for(let r of e){if(r.type==="error")throw O(r);yield r}}};var H=class{static encode(e){return ye(e)}static encodeInterpolation(e){return Me(e)}static decode(e,r){return JSON.parse(e,(s,n)=>{if(n==null)return null;if(n["@mod"])return new W(n["@mod"]);if(n["@doc"]){if(typeof n["@doc"]=="string"){let[i,d]=n["@doc"].split(":");return new M({coll:i,id:d})}let o=n["@doc"];return o.id?new fe(o):new pe(o)}else if(n["@ref"]){let o=n["@ref"],i;return o.id?i=new M(o):i=new Y(o),"exists"in o&&o.exists===!1?new re(i,o.cause):i}else{if(n["@set"])return typeof n["@set"]=="string"?new L(n["@set"]):new D(n["@set"]);if(n["@int"])return Number(n["@int"]);if(n["@long"]){let o=BigInt(n["@long"]);return r.long_type==="number"?((o>Number.MAX_SAFE_INTEGER||o<Number.MIN_SAFE_INTEGER)&&console.warn("Value is too large to be represented as a number. Returning as Number with loss of precision. Use long_type 'bigint' instead."),Number(o)):o}else{if(n["@double"])return Number(n["@double"]);if(n["@date"])return N.from(n["@date"]);if(n["@time"])return q.from(n["@time"]);if(n["@object"])return n["@object"];if(n["@stream"])return new ne(n["@stream"]);if(n["@bytes"])return vr(n["@bytes"])}}return n})}},Ht=BigInt("-9223372036854775808"),Vt=BigInt("9223372036854775807"),qt=-(2**31),Nt=2**31-1,b={bigint:t=>{if(t<Ht||t>Vt)throw new RangeError("BigInt value exceeds max magnitude for a 64-bit Fauna long. Use a 'number' to represent doubles beyond that limit.");return t>=qt&&t<=Nt?{"@int":t.toString()}:{"@long":t.toString()}},number:t=>{if(t===Number.POSITIVE_INFINITY||t===Number.NEGATIVE_INFINITY)throw new RangeError(`Cannot convert ${t} to a Fauna type.`);return Number.isInteger(t)?t>=qt&&t<=Nt?{"@int":t.toString()}:Number.isSafeInteger(t)?{"@long":t.toString()}:{"@double":t.toString()}:{"@double":t.toString()}},string:t=>t,object:t=>{let e=!1,r={};for(let s in t)s.startsWith("@")&&(e=!0),t[s]!==void 0&&(r[s]=ye(t[s]));return e?{"@object":r}:r},array:t=>t.map(ye),date:t=>({"@time":t.toISOString()}),faunadate:t=>({"@date":t.dateString}),faunatime:t=>({"@time":t.isoString}),module:t=>({"@mod":t.name}),documentReference:t=>({"@ref":{id:t.id,coll:{"@mod":t.coll.name}}}),document:t=>({"@ref":{id:t.id,coll:{"@mod":t.coll.name}}}),namedDocumentReference:t=>({"@ref":{name:t.name,coll:{"@mod":t.coll.name}}}),namedDocument:t=>({"@ref":{name:t.name,coll:{"@mod":t.coll.name}}}),set:t=>{throw new y("Page could not be encoded. Fauna does not accept encoded Set values, yet. Use Page.data and Page.after as arguments, instead.")},streamToken:t=>t.token,bytes:t=>({"@bytes":Fr(t)})},ye=t=>{switch(typeof t){case"bigint":return b.bigint(t);case"string":return b.string(t);case"number":return b.number(t);case"boolean":return t;case"object":if(t==null)return null;if(Array.isArray(t))return b.array(t);if(t instanceof Date)return b.date(t);if(t instanceof N)return b.faunadate(t);if(t instanceof q)return b.faunatime(t);if(t instanceof W)return b.module(t);if(t instanceof fe)return b.document(t);if(t instanceof M)return b.documentReference(t);if(t instanceof pe)return b.namedDocument(t);if(t instanceof Y)return b.namedDocumentReference(t);if(t instanceof re)return ye(t.ref);if(t instanceof D)return b.set(t);if(t instanceof L)return b.set(t);if(t instanceof ne)return b.streamToken(t);if(t instanceof Uint8Array||t instanceof ArrayBuffer)return b.bytes(t);if(ArrayBuffer.isView(t))throw new y("Error encoding TypedArray to Fauna Bytes. Convert your TypedArray to Uint8Array or ArrayBuffer before passing it to Fauna. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray");if(t instanceof P)throw new TypeError("Cannot encode instance of type 'Query'. Try using TaggedTypeFormat.encodeInterpolation instead.");return b.object(t);default:throw new TypeError(`Passing ${typeof t} as a QueryArgument is not supported`)}},Me=t=>{switch(typeof t){case"bigint":case"string":case"number":case"boolean":return Dt(ye(t));case"object":return t==null||t instanceof Date||t instanceof N||t instanceof q||t instanceof W||t instanceof M||t instanceof Y||t instanceof D||t instanceof L||t instanceof ne||t instanceof Uint8Array||t instanceof ArrayBuffer||ArrayBuffer.isView(t)?Dt(ye(t)):t instanceof re?Me(t.ref):t instanceof P?Qr(t):Array.isArray(t)?Rr(t):Pr(t);default:throw new TypeError(`Passing ${typeof t} as a QueryArgument is not supported`)}},Pr=t=>{let e={};for(let r in t)t[r]!==void 0&&(e[r]=Me(t[r]));return{object:e}},Rr=t=>({array:t.map(Me)}),Qr=t=>t.encode(),Dt=t=>({value:t});function vr(t){return pt.default.toByteArray(t)}function Fr(t){let e=t instanceof Uint8Array?t:new Uint8Array(t);return pt.default.fromByteArray(e)}function ft(t,...e){return new P(t,...e)}var P=class{#e;#t;#r;constructor(e,...r){if(e.length===0||e.length!==r.length+1)throw new Error("invalid query constructed");this.#e=e,this.#t=r,this.#r=void 0}encode(){if(this.#e.length===1)return{fql:[this.#e[0]]};let e=this.#e.flatMap((r,s)=>{if(s===this.#e.length-1)return r===""?[]:[r];let n=this.#t[s],o=H.encodeInterpolation(n);return[r,o]});return e=e.filter(r=>r!==""),{fql:e}}};var jt="2.3.0";var We;try{We=nt("node:os")}catch{We=void 0}var yt=()=>{let t={driver:["javascript",jt].join("-"),env:"unknown",os:"unknown",runtime:"unknown"};try{let e=typeof window>"u"&&typeof process<"u"&&process.versions!=null&&process.versions.node!=null,r=typeof window<"u"&&typeof window.document<"u",s=typeof self=="object"&&self.constructor&&self.constructor.name==="DedicatedWorkerGlobalScope";e?(t.runtime=["nodejs",process.version].join("-"),t.env=kr(),t.os=[We.platform(),We.release()].join("-")):s?(t.runtime=Mt(navigator),t.env="Service Worker",t.os=Wt(navigator)):r?(t.runtime=Mt(navigator),t.env="browser",t.os=Wt(navigator)):typeof EdgeRuntime!="string"&&(t.runtime="Vercel Edge Runtime",t.env="edge")}catch{}return Object.entries(t).filter(([e,r])=>r!=="unknown").map(e=>e.join("=")).join("; ")},Mt=t=>{let e=t.appName,r=""+parseFloat(t.appVersion),s,n,o;return(n=t.userAgent.indexOf("Opera"))!=-1?(e="Opera",r=t.userAgent.substring(n+6),(n=t.userAgent.indexOf("Version"))!=-1&&(r=t.userAgent.substring(n+8))):(n=t.userAgent.indexOf("MSIE"))!=-1?(e="Microsoft Internet Explorer",r=t.userAgent.substring(n+5)):e=="Netscape"&&t.userAgent.indexOf("Trident/")!=-1?(e="Microsoft Internet Explorer",r=t.userAgent.substring(n+5),(n=t.userAgent.indexOf("rv:"))!=-1&&(r=t.userAgent.substring(n+3))):(n=t.userAgent.indexOf("Chrome"))!=-1?(e="Chrome",r=t.userAgent.substring(n+7)):(n=t.userAgent.indexOf("Safari"))!=-1?(e="Safari",r=t.userAgent.substring(n+7),(n=t.userAgent.indexOf("Version"))!=-1&&(r=t.userAgent.substring(n+8)),t.userAgent.indexOf("CriOS")!=-1&&(e="Chrome")):(n=t.userAgent.indexOf("Firefox"))!=-1?(e="Firefox",r=t.userAgent.substring(n+8)):(s=t.userAgent.lastIndexOf(" ")+1)<(n=t.userAgent.lastIndexOf("/"))&&(e=t.userAgent.substring(s,n),r=t.userAgent.substring(n+1),e.toLowerCase()==e.toUpperCase()&&(e=t.appName)),(o=r.indexOf(";"))!=-1&&(r=r.substring(0,o)),(o=r.indexOf(" "))!=-1&&(r=r.substring(0,o)),(o=r.indexOf(")"))!=-1&&(r=r.substring(0,o)),[e,r].join("-")},Wt=t=>{let e="unknown",r=[{s:"Windows 10",r:/(Windows 10.0|Windows NT 10.0)/},{s:"Windows 8.1",r:/(Windows 8.1|Windows NT 6.3)/},{s:"Windows 8",r:/(Windows 8|Windows NT 6.2)/},{s:"Windows 7",r:/(Windows 7|Windows NT 6.1)/},{s:"Windows Vista",r:/Windows NT 6.0/},{s:"Windows Server 2003",r:/Windows NT 5.2/},{s:"Windows XP",r:/(Windows NT 5.1|Windows XP)/},{s:"Windows 2000",r:/(Windows NT 5.0|Windows 2000)/},{s:"Windows ME",r:/(Win 9x 4.90|Windows ME)/},{s:"Windows 98",r:/(Windows 98|Win98)/},{s:"Windows 95",r:/(Windows 95|Win95|Windows_95)/},{s:"Windows NT 4.0",r:/(Windows NT 4.0|WinNT4.0|WinNT|Windows NT)/},{s:"Windows CE",r:/Windows CE/},{s:"Windows 3.11",r:/Win16/},{s:"Android",r:/Android/},{s:"Open BSD",r:/OpenBSD/},{s:"Sun OS",r:/SunOS/},{s:"Chrome OS",r:/CrOS/},{s:"Linux",r:/(Linux|X11(?!.*CrOS))/},{s:"iOS",r:/(iPhone|iPad|iPod)/},{s:"Mac OS X",r:/Mac OS X/},{s:"Mac OS",r:/(Mac OS|MacPPC|MacIntel|Mac_PowerPC|Macintosh)/},{s:"QNX",r:/QNX/},{s:"UNIX",r:/UNIX/},{s:"BeOS",r:/BeOS/},{s:"OS/2",r:/OS\/2/},{s:"Search Bot",r:/(nuhk|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves\/Teoma|ia_archiver)/}];for(let n in r){let o=r[n];if(o.r.test(t.userAgent)){e=o.s;break}}let s="unknown";if(/Windows/.test(e)){let n=/Windows (.*)/.exec(e);n&&(s=n[1]),e="Windows"}switch(e){case"Mac OS":case"Mac OS X":case"Android":{let n=/(?:Android|Mac OS|Mac OS X|MacPPC|MacIntel|Mac_PowerPC|Macintosh) ([._\d]+)/.exec(t.userAgent);n&&(s=n[1]);break}case"iOS":{let n=/OS (\d+)_(\d+)_?(\d+)?/.exec(t.appVersion);n&&(s=n[1]+"."+n[2]+"."+(n[3]??0));break}}return[e,s].join("-")},Or=typeof window<"u"?window:typeof globalThis<"u"?globalThis:typeof global<"u"?global:self,kr=()=>{if(!(typeof process<"u"&&process&&process.env&&typeof process.env=="object"))return"unknown";let e=[{name:"Netlify",check:function(){return!!process.env.NETLIFY_IMAGES_CDN_DOMAIN}},{name:"Vercel",check:function(){return!!process.env.VERCEL}},{name:"Heroku",check:function(){return!!process.env.PATH&&process.env.PATH.indexOf(".heroku")!==-1}},{name:"AWS Lambda",check:function(){return!!process.env.AWS_LAMBDA_FUNCTION_VERSION}},{name:"GCP Cloud Functions",check:function(){return!!process.env._&&process.env._.indexOf("google")!==-1}},{name:"GCP Compute Instances",check:function(){return!!process.env.GOOGLE_CLOUD_PROJECT}},{name:"Azure Cloud Functions",check:function(){return!!process.env.WEBSITE_FUNCTIONS_AZUREMONITOR_CATEGORIES}},{name:"Azure Compute",check:function(){return!!process.env.ORYX_ENV_TYPE&&!!process.env.WEBSITE_INSTANCE_ID&&process.env.ORYX_ENV_TYPE==="AppService"}},{name:"Mongo Stitch",check:function(){return typeof Or?.StitchError=="function"}},{name:"Render",check:function(){return!!process.env.RENDER_SERVICE_ID}},{name:"Begin",check:function(){return!!process.env.BEGIN_DATA_SCOPE_ID}}].find(r=>r.check());return e?e.name:"unknown"};var gt=async(t,{maxAttempts:e,maxBackoff:r,shouldRetry:s=()=>!0,attempt:n=0,sleepFn:o=setTimeout})=>{let i=n>0?Math.min(Math.random()*2**n,r)*1e3:0;n+=1;try{return await t()}catch(d){if(n>=e||s(d)!==!0)throw d;return await new Promise(c=>o(c,i)),gt(t,{maxAttempts:e,maxBackoff:r,shouldRetry:s,attempt:n,sleepFn:o})}};var Lt=t=>t instanceof Object&&"data"in t,ht=t=>t instanceof Object&&"error"in t&&t.error instanceof Object&&"code"in t.error&&"message"in t.error;var Tt={client_timeout_buffer_ms:5e3,format:"tagged",http2_session_idle_ms:5e3,http2_max_streams:100,long_type:"number",fetch_keepalive:!1,query_timeout_ms:5e3,max_attempts:3,max_backoff:20},Be,_,B,A,J,Re,bt,Qe,St,$e,Ut,Ge,Bt,ze,$t,Ye,Gt,Je,zt,xt=class{constructor(e,r){u(this,Re);u(this,Qe);u(this,$e);u(this,Ge);u(this,ze);u(this,Ye);u(this,Je);u(this,_,void 0);u(this,B,void 0);u(this,A,void 0);u(this,J,!1);f(this,_,{...Tt,...e,secret:l(this,$e,Ut).call(this,e),endpoint:l(this,Ge,Bt).call(this,e)}),l(this,Je,zt).call(this),r?f(this,B,r):f(this,B,ut({url:a(this,_).endpoint.toString(),http2_session_idle_ms:a(this,_).http2_session_idle_ms,http2_max_streams:a(this,_).http2_max_streams,fetch_keepalive:a(this,_).fetch_keepalive}))}get lastTxnTs(){return a(this,A)}set lastTxnTs(e){e!==void 0&&f(this,A,a(this,A)?Math.max(e,a(this,A)):e)}get clientConfiguration(){let{...e}=a(this,_);return e}close(){if(a(this,J))throw new v("Your client is closed. You cannot close it again.");a(this,B).close(),f(this,J,!0)}paginate(e,r){return e instanceof P?U.fromQuery(this,e,r):U.fromPageable(this,e,r)}async query(e,r){if(a(this,J))throw new v("Your client is closed. No further requests can be issued.");let s={query:e.encode()};return r?.arguments&&(s.arguments=H.encode(r.arguments)),l(this,Re,bt).call(this,s,r)}stream(e,r){if(a(this,J))throw new v("Your client is closed. No further requests can be issued.");let s=a(this,B);if(dt(s)){let n={...a(this,_),httpStreamClient:s,...r};if(n.cursor!==void 0&&e instanceof P)throw new y("The `cursor` configuration can only be used with a stream token.");let o=e instanceof P?()=>this.query(e).then(i=>i.data):e;return new Ue(o,n)}else throw new y("Streaming is not supported by this client.")}feed(e,r){if(a(this,J))throw new v("Your client is closed. No further requests can be issued.");let s={...a(this,_),httpClient:a(this,B),...r},n=e instanceof P?()=>this.query(e).then(o=>o.data):e;return new Pe(n,s)}},Le=xt;Be=new WeakMap,_=new WeakMap,B=new WeakMap,A=new WeakMap,J=new WeakMap,Re=new WeakSet,bt=async function(e,r,s=0){let n=this.clientConfiguration.max_backoff??Tt.max_backoff,o=this.clientConfiguration.max_attempts??Tt.max_attempts,i=Math.min(Math.random()*2**s,n)*1e3;s+=1;try{return await l(this,ze,$t).call(this,e,r,s)}catch(d){if(d instanceof V&&s<o)return await Kt(i),l(this,Re,bt).call(this,e,r,s);throw d}},Qe=new WeakSet,St=function(e){if(e instanceof y||e instanceof w||e instanceof F||e instanceof h)return e;if(ct(e)){if(ht(e.body)){let r=e.body,s=e.status;return O(r,s)}return new F({message:`Response is in an unkown format: ${e.body}`,httpStatus:e.status})}return new y("A client level error occurred. Fauna was not called.",{cause:e})},$e=new WeakSet,Ut=function(e){let r;typeof process<"u"&&process&&typeof process=="object"&&process.env&&typeof process.env=="object"&&(r=process.env.FAUNA_SECRET);let s=e?.secret??r;if(s===void 0)throw new TypeError("You must provide a secret to the driver. Set it in an environmental variable named FAUNA_SECRET or pass it to the Client constructor.");return s},Ge=new WeakSet,Bt=function(e){if(e&&"endpoint"in e&&e.endpoint===void 0)throw new TypeError("ClientConfiguration option endpoint must be defined.");let r;return typeof process<"u"&&process&&typeof process=="object"&&process.env&&typeof process.env=="object"&&(r=process.env.FAUNA_ENDPOINT?new URL(process.env.FAUNA_ENDPOINT):void 0),e?.endpoint??r??at.default},ze=new WeakSet,$t=async function(e,r,s=0){try{let n={...a(this,_),...r},o={Authorization:`Bearer ${n.secret}`};l(this,Ye,Gt).call(this,n,o);let i=n.format==="tagged",d=n.query_timeout_ms+a(this,_).client_timeout_buffer_ms,c=await a(this,B).request({client_timeout_ms:d,data:e,headers:o,method:"POST"}),m;try{if(m={...c,body:i?H.decode(c.body,{long_type:n.long_type}):JSON.parse(c.body)},m.body.query_tags){let x=m.body.query_tags.split(",").map(T=>T.split("="));m.body.query_tags=Object.fromEntries(x)}}catch(x){throw new F({message:`Error parsing response as JSON: ${x}`,httpStatus:c.status})}if(!Lt(m.body))throw l(this,Qe,St).call(this,m);let p=m.body.txn_ts;(a(this,A)===void 0&&p!==void 0||p!==void 0&&a(this,A)!==void 0&&a(this,A)<p)&&f(this,A,p);let g=m.body;return g.stats&&(g.stats.attempts=s),g}catch(n){throw l(this,Qe,St).call(this,n)}},Ye=new WeakSet,Gt=function(e,r){let s=(n,o,i=d=>String(d))=>{o!==void 0&&(r[n]=i(o))};s("x-format",e.format),s("x-typecheck",e.typecheck),s("x-query-timeout-ms",e.query_timeout_ms),s("x-linearized",e.linearized),s("x-max-contention-retries",e.max_contention_retries),s("traceparent",e.traceparent),s("x-query-tags",e.query_tags,n=>Object.entries(n).map(o=>o.join("=")).join(",")),s("x-last-txn-ts",a(this,A),n=>n),s("x-driver-env",a(xt,Be))},Je=new WeakSet,zt=function(){let e=a(this,_);if(["client_timeout_buffer_ms","endpoint","format","http2_session_idle_ms","long_type","query_timeout_ms","fetch_keepalive","http2_max_streams","max_backoff","max_attempts"].forEach(s=>{if(e[s]===void 0)throw new TypeError(`ClientConfiguration option '${s}' must be defined.`)}),e.http2_max_streams<=0)throw new RangeError("'http2_max_streams' must be greater than zero.");if(e.client_timeout_buffer_ms<=0)throw new RangeError("'client_timeout_buffer_ms' must be greater than zero.");if(e.query_timeout_ms<=0)throw new RangeError("'query_timeout_ms' must be greater than zero.");if(e.max_backoff<=0)throw new RangeError("'max_backoff' must be greater than zero.");if(e.max_attempts<=0)throw new RangeError("'max_attempts' must be greater than zero.")},u(Le,Be,yt());var Ue=class{closed=!1;#e;#t=0;#r;#n;#s;#o;#a;constructor(e,r){Ae(e)?this.#r=()=>Promise.resolve(e):this.#r=e,this.#e=r,this.#u()}start(e,r){if(typeof e!="function")throw new TypeError(`Expected a function as the 'onEvent' argument, but received ${typeof e}. Please provide a valid function.`);if(r&&typeof r!="function")throw new TypeError(`Expected a function as the 'onError' argument, but received ${typeof r}. Please provide a valid function.`);(async()=>{try{for await(let n of this)e(n)}catch(n){r&&r(n)}})()}async*[Symbol.asyncIterator](){if(this.closed)throw new y("The stream has been closed and cannot be reused.");for(this.#a||(this.#a=await this.#r().then(e=>{if(!Ae(e))throw new y(`Error requesting a stream token. Expected a EventSource as the query result, but received ${typeof e}. Your query must return the result of '<Set>.eventSource' or '<Set>.eventsOn') Query result: ${JSON.stringify(e,null)}`);return e})),this.#t=1;!this.closed;){let e=Math.min(Math.random()*2**this.#t,this.#e.max_backoff)*1e3;try{for await(let r of this.#i())yield r}catch(r){if(r instanceof Q||this.#t>=this.#e.max_attempts)throw this.close(),r;this.#t+=1,await Kt(e)}}}close(){this.#o&&(this.#o.close(),this.#o=void 0),this.closed=!0}get last_ts(){return this.#n}async*#i(){let e=this.#a,r={Authorization:`Bearer ${this.#e.secret}`},s=this.#e.httpStreamClient.stream({data:{token:e.token,cursor:this.#s||this.#e.cursor},headers:r,method:"POST"});this.#o=s;for await(let n of s.read){let o=H.decode(n,{long_type:this.#e.long_type});if(o.type==="error")throw this.close(),O(o);this.#n=o.txn_ts,this.#s=o.cursor,o.type==="start"&&(o.type="status"),!(!this.#e.status_events&&o.type==="status")&&(yield o)}}#u(){let e=this.#e;if(["long_type","httpStreamClient","max_backoff","max_attempts","secret"].forEach(s=>{if(e[s]===void 0)throw new TypeError(`ClientConfiguration option '${s}' must be defined.`)}),e.max_backoff<=0)throw new RangeError("'max_backoff' must be greater than zero.");if(e.max_attempts<=0)throw new RangeError("'max_attempts' must be greater than zero.")}},Xe,ge,S,se,he,Te,Ze,Yt,Ke,Jt,et,Xt,tt,Zt,wt=class{constructor(e,r){u(this,Ze);u(this,Ke);u(this,et);u(this,tt);u(this,ge,void 0);u(this,S,void 0);u(this,se,void 0);u(this,he,void 0);u(this,Te,void 0);Ae(e)?f(this,ge,()=>Promise.resolve(e)):f(this,ge,e),f(this,S,r),f(this,se,r.cursor),l(this,tt,Zt).call(this)}async*[Symbol.asyncIterator](){for(;!a(this,Te);)yield await this.nextPage()}async nextPage(){if(a(this,Te))throw new y("The event feed has no more pages to fetch.");let{httpClient:e}=a(this,S),r=await l(this,Ke,Jt).call(this),s=await gt(()=>e.request(r),{maxAttempts:a(this,S).max_attempts,maxBackoff:a(this,S).max_backoff,shouldRetry:i=>i instanceof V}),n;try{n=H.decode(s.body,{long_type:a(this,S).long_type})}catch(i){throw new F({message:`Error parsing response as JSON: ${i}`,httpStatus:s.status})}if(ht(n))throw O(n,s.status);let o=new Ce(n);return f(this,se,o.cursor),f(this,Te,!o.hasNext),o}async*flatten(){for await(let e of this)for(let r of e.events)yield r}},Pe=wt;Xe=new WeakMap,ge=new WeakMap,S=new WeakMap,se=new WeakMap,he=new WeakMap,Te=new WeakMap,Ze=new WeakSet,Yt=function(){return{Authorization:`Bearer ${a(this,S).secret}`,"x-format":"tagged","x-driver-env":a(wt,Xe),"x-query-timeout-ms":a(this,S).query_timeout_ms.toString()}},Ke=new WeakSet,Jt=async function(){a(this,he)||f(this,he,await l(this,et,Xt).call(this,a(this,ge)));let r={headers:l(this,Ze,Yt).call(this),client_timeout_ms:a(this,S).client_timeout_buffer_ms+a(this,S).query_timeout_ms,data:{token:a(this,he).token},method:"POST",path:j.EVENT_FEED};return a(this,S).page_size&&(r.data.page_size=a(this,S).page_size),a(this,se)?r.data.cursor=a(this,se):a(this,S).start_ts&&(r.data.start_ts=a(this,S).start_ts),r},et=new WeakSet,Xt=async function(e){return await e().then(r=>{if(!Ae(r))throw new y(`Error requesting a stream token. Expected a EventSource as the query result, but received ${typeof r}. Your query must return the result of '<Set>.eventSource' or '<Set>.eventsOn') Query result: ${JSON.stringify(r,null)}`);return r})},tt=new WeakSet,Zt=function(){let e=a(this,S);if(["long_type","httpClient","max_backoff","max_attempts","client_timeout_buffer_ms","query_timeout_ms","secret"].forEach(s=>{if(e[s]===void 0)throw new TypeError(`ClientConfiguration option '${s}' must be defined.`)}),e.max_backoff<=0)throw new RangeError("'max_backoff' must be greater than zero.");if(e.max_attempts<=0)throw new RangeError("'max_attempts' must be greater than zero.");if(e.query_timeout_ms<=0)throw new RangeError("'query_timeout_ms' must be greater than zero.");if(e.client_timeout_buffer_ms<0)throw new RangeError("'client_timeout_buffer_ms' must be greater than or equal to zero.");if(e.start_ts!==void 0&&e.cursor!==void 0)throw new TypeError("Only one of 'start_ts' or 'cursor' can be defined in the client configuration.");if(e.cursor!==void 0&&typeof e.cursor!="string")throw new TypeError("'cursor' must be a string.")},u(Pe,Xe,yt());function Kt(t){return new Promise(e=>setTimeout(e,t))}export{Fe as AbortError,ie as AuthenticationError,ue as AuthorizationError,Le as Client,v as ClientClosedError,y as ClientError,ve as ConstraintFailureError,Oe as ContendedTransactionError,N as DateStub,fe as Document,M as DocumentReference,L as EmbeddedSet,j as FaunaAPIPaths,Q as FaunaError,Pe as FeedClient,Ce as FeedPage,be as FetchClient,je as FlattenedSetIterator,Z as InvalidRequestError,Vt as LONG_MAX,Ht as LONG_MIN,W as Module,pe as NamedDocument,Y as NamedDocumentReference,w as NetworkError,ee as NodeHTTP2Client,re as NullDocument,D as Page,F as ProtocolError,G as QueryCheckError,ae as QueryRuntimeError,ce as QueryTimeoutError,h as ServiceError,de as ServiceInternalError,U as SetIterator,Ue as StreamClient,ne as StreamToken,H as TaggedTypeFormat,V as ThrottlingError,q as TimeStub,at as endpoints,ft as fql,ut as getDefaultHTTPClient,ct as isHTTPResponse,dt as isStreamClient}; //# sourceMappingURL=index.js.map