surrealdb
Version:
The official SurrealDB SDK for JavaScript.
4 lines • 196 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../src/index.ts", "../src/util/emitter.ts", "../src/cbor/gap.ts", "../src/cbor/index.ts", "../src/cbor/constants.ts", "../src/cbor/encoded.ts", "../src/errors.ts", "../src/cbor/error.ts", "../src/cbor/writer.ts", "../src/cbor/partial.ts", "../src/cbor/tagged.ts", "../src/cbor/encoder.ts", "../src/cbor/reader.ts", "../src/cbor/util.ts", "../src/cbor/decoder.ts", "../src/data/types/datetime.ts", "../src/data/value.ts", "../src/data/types/decimal.ts", "../src/data/types/duration.ts", "../src/data/types/future.ts", "../src/data/types/geometry.ts", "../src/util/equals.ts", "../src/util/escape.ts", "../src/data/types/uuid.ts", "../src/data/types/recordid.ts", "../src/data/types/table.ts", "../src/util/to-surrealql-string.ts", "../src/data/types/range.ts", "../src/data/cbor.ts", "../src/util/prepared-query.ts", "../src/util/tagged-template.ts", "../src/types.ts", "../src/util/jsonify.ts", "../src/util/version-check.ts", "../src/util/get-incremental-id.ts", "../src/util/string-prefixes.ts", "../src/engines/abstract.ts", "../src/util/process-auth-vars.ts", "../src/auth.ts", "../src/engines/abstract-remote.ts", "../src/engines/http.ts", "../src/engines/ws.ts", "../src/util/completable.ts", "../src/util/rand.ts", "../src/util/reconnect.ts", "../src/surreal.ts"],
"sourcesContent": ["export { Emitter, type Listener, type UnknownEvents } from \"./util/emitter.ts\";\nexport { surql, surrealql } from \"./util/tagged-template.ts\";\nexport { PreparedQuery } from \"./util/prepared-query.ts\";\nexport * as cbor from \"./cbor\";\nexport * from \"./cbor/gap\";\nexport * from \"./cbor/error\";\nexport * from \"./data\";\nexport * from \"./errors.ts\";\nexport * from \"./types.ts\";\nexport * from \"./util/equals.ts\";\nexport * from \"./util/jsonify.ts\";\nexport * from \"./util/version-check.ts\";\nexport * from \"./util/get-incremental-id.ts\";\nexport * from \"./util/string-prefixes.ts\";\nexport * from \"./util/to-surrealql-string.ts\";\nexport * from \"./util/escape.ts\";\nexport {\n\tConnectionStatus,\n\tAbstractEngine,\n\ttype Engine,\n\ttype Engines,\n\ttype EngineEvents,\n} from \"./engines/abstract.ts\";\nexport { Surreal, Surreal as default } from \"./surreal.ts\";\nexport { EngineAuth, AuthController } from \"./auth.ts\";\n", "export type Listener<Args extends unknown[] = unknown[]> = (\n\t...args: Args\n) => unknown;\nexport type UnknownEvents = Record<string, unknown[]>;\n\n/**\n * A class used to subscribe to and emit events\n */\nexport class Emitter<Events extends UnknownEvents = UnknownEvents> {\n\tprivate collectable: Partial<{\n\t\t[K in keyof Events]: Events[K][];\n\t}> = {};\n\n\tprivate listeners: Partial<{\n\t\t[K in keyof Events]: Listener<Events[K]>[];\n\t}> = {};\n\n\tprivate readonly interceptors: Partial<{\n\t\t[K in keyof Events]: (...args: Events[K]) => Promise<Events[K]>;\n\t}>;\n\n\tconstructor({\n\t\tinterceptors,\n\t}: {\n\t\tinterceptors?: Partial<{\n\t\t\t[K in keyof Events]: (...args: Events[K]) => Promise<Events[K]>;\n\t\t}>;\n\t} = {}) {\n\t\tthis.interceptors = interceptors ?? {};\n\t}\n\n\tsubscribe<Event extends keyof Events>(\n\t\tevent: Event,\n\t\tlistener: Listener<Events[Event]>,\n\t\thistoric = false,\n\t): void {\n\t\tif (!this.listeners[event]) {\n\t\t\tthis.listeners[event] = [];\n\t\t}\n\n\t\tif (!this.isSubscribed(event, listener)) {\n\t\t\tthis.listeners[event]?.push(listener);\n\n\t\t\tif (historic && this.collectable[event]) {\n\t\t\t\tconst buffer = this.collectable[event];\n\t\t\t\tdelete this.collectable[event];\n\t\t\t\tfor (const args of buffer) {\n\t\t\t\t\tlistener(...args);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tasync subscribeOnce<Event extends keyof Events>(\n\t\tevent: Event,\n\t\thistoric = false,\n\t): Promise<Events[Event]> {\n\t\tif (historic && this.collectable[event]) {\n\t\t\tconst args = this.collectable[event]?.shift();\n\t\t\tif (this.collectable[event]?.length === 0) {\n\t\t\t\tdelete this.collectable[event];\n\t\t\t}\n\n\t\t\tif (args) return args;\n\t\t}\n\n\t\treturn new Promise<Events[Event]>((resolve) => {\n\t\t\tlet resolved = false;\n\t\t\tconst listener = (...args: Events[Event]) => {\n\t\t\t\tif (!resolved) {\n\t\t\t\t\tresolved = true;\n\t\t\t\t\tthis.unSubscribe(event, listener);\n\t\t\t\t\tresolve(args);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tthis.subscribe(event, listener, false);\n\t\t});\n\t}\n\n\tunSubscribe<Event extends keyof Events>(\n\t\tevent: Event,\n\t\tlistener: Listener<Events[Event]>,\n\t): void {\n\t\tif (this.listeners[event]) {\n\t\t\tconst index = this.listeners[event]?.findIndex((v) => v === listener);\n\t\t\tif (index >= 0) {\n\t\t\t\tthis.listeners[event]?.splice(index, 1);\n\t\t\t\tif (this.listeners[event]?.length === 0) {\n\t\t\t\t\tdelete this.listeners[event];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tisSubscribed<Event extends keyof Events>(\n\t\tevent: Event,\n\t\tlistener: Listener<Events[Event]>,\n\t): boolean {\n\t\treturn !!this.listeners[event]?.includes(listener);\n\t}\n\n\tasync emit<Event extends keyof Events>(\n\t\tevent: Event,\n\t\targs: Events[Event],\n\t\tcollectable = false,\n\t): Promise<void> {\n\t\tconst interceptor = this.interceptors[event];\n\t\tconst computedArgs = interceptor ? await interceptor(...args) : args;\n\n\t\tif (\n\t\t\t(collectable && !this.listeners[event]) ||\n\t\t\tthis.listeners[event]?.length === 0\n\t\t) {\n\t\t\tif (!this.collectable[event]) {\n\t\t\t\tthis.collectable[event] = [];\n\t\t\t}\n\n\t\t\tthis.collectable[event]?.push(args);\n\t\t}\n\n\t\tfor (const listener of this.listeners[event] ?? []) {\n\t\t\tlistener(...computedArgs);\n\t\t}\n\t}\n\n\treset({\n\t\tcollectable,\n\t\tlisteners,\n\t}: {\n\t\tcollectable?: boolean | keyof Events | (keyof Events)[];\n\t\tlisteners?: boolean | keyof Events | (keyof Events)[];\n\t}): void {\n\t\tif (Array.isArray(collectable)) {\n\t\t\tfor (const k of collectable) {\n\t\t\t\tdelete this.collectable[k];\n\t\t\t}\n\t\t} else if (typeof collectable === \"string\") {\n\t\t\tdelete this.collectable[collectable];\n\t\t} else if (collectable !== false) {\n\t\t\tthis.collectable = {};\n\t\t}\n\n\t\tif (Array.isArray(listeners)) {\n\t\t\tfor (const k of listeners) {\n\t\t\t\tdelete this.listeners[k];\n\t\t\t}\n\t\t} else if (typeof listeners === \"string\") {\n\t\t\tdelete this.listeners[listeners];\n\t\t} else if (listeners !== false) {\n\t\t\tthis.listeners = {};\n\t\t}\n\t}\n\n\tscanListeners(filter?: (k: keyof Events) => boolean): (keyof Events)[] {\n\t\tlet listeners = Object.keys(this.listeners) as (keyof Events)[];\n\t\tif (filter) listeners = listeners.filter(filter);\n\t\treturn listeners;\n\t}\n}\n", "// Why is the default value being stored in an array? undefined, null, false, etc... are all valid defaults,\n// and specifying a field on a class as optional will make it undefined by default.\n\nexport type Fill<T = unknown> = [Gap<T>, T];\nexport class Gap<T = unknown> {\n\treadonly args: [T?] = [];\n\tconstructor(...args: [T?]) {\n\t\tthis.args = args;\n\t}\n\n\tfill(value: T): Fill<T> {\n\t\treturn [this, value];\n\t}\n\n\thasDefault(): boolean {\n\t\treturn this.args.length === 1;\n\t}\n\n\tget default(): T | undefined {\n\t\treturn this.args[0];\n\t}\n}\n", "export * from \"./constants\";\nexport * from \"./encoder\";\nexport * from \"./decoder\";\nexport * from \"./util\";\nexport * from \"./reader\";\nexport * from \"./writer\";\nexport * from \"./tagged\";\nexport * from \"./error\";\nexport * from \"./gap\";\nexport * from \"./partial\";\nexport * from \"./encoded\";\n", "export type Replacer = (v: unknown) => unknown;\nexport type Major = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;\nexport const POW_2_53: number = 2 ** 53;\nexport const POW_2_64: bigint = BigInt(2 ** 64);\n", "export class Encoded {\n\tconstructor(readonly encoded: ArrayBuffer) {}\n}\n", "export class SurrealDbError extends Error {}\n\nexport class NoActiveSocket extends SurrealDbError {\n\tname = \"NoActiveSocket\";\n\tmessage =\n\t\t\"No socket is currently connected to a SurrealDB instance. Please call the .connect() method first!\";\n}\n\nexport class NoConnectionDetails extends SurrealDbError {\n\tname = \"NoConnectionDetails\";\n\tmessage =\n\t\t\"No connection details for the HTTP api have been provided. Please call the .connect() method first!\";\n}\n\nexport class UnexpectedResponse extends SurrealDbError {\n\tname = \"UnexpectedResponse\";\n\tmessage =\n\t\t\"The returned response from the SurrealDB instance is in an unexpected format. Unable to process response!\";\n}\n\nexport class InvalidURLProvided extends SurrealDbError {\n\tname = \"InvalidURLProvided\";\n\tmessage =\n\t\t\"The provided string is either not a URL or is a URL but with an invalid protocol!\";\n}\n\nexport class NoURLProvided extends SurrealDbError {\n\tname = \"NoURLProvided\";\n\tmessage =\n\t\t\"Tried to establish a connection while no connection URL was provided\";\n}\n\nexport class EngineDisconnected extends SurrealDbError {\n\tname = \"EngineDisconnected\";\n\tmessage = \"The engine reported the connection to SurrealDB has dropped\";\n}\n\nexport class ReconnectFailed extends SurrealDbError {\n\tname = \"ReconnectFailed\";\n\tmessage = \"The engine failed to reconnect to SurrealDB\";\n}\n\nexport class ReconnectIterationError extends SurrealDbError {\n\tname = \"ReconnectIterationError\";\n\tmessage = \"The reconnect iterator failed to iterate\";\n}\n\nexport class UnexpectedServerResponse extends SurrealDbError {\n\tname = \"UnexpectedServerResponse\";\n\n\tconstructor(public readonly response: unknown) {\n\t\tsuper();\n\t\tthis.message = `${response}`;\n\t}\n}\n\nexport class UnexpectedConnectionError extends SurrealDbError {\n\tname = \"UnexpectedConnectionError\";\n\n\tconstructor(public readonly error: unknown) {\n\t\tsuper();\n\t\tthis.message = `${error}`;\n\t}\n}\n\nexport class UnsupportedEngine extends SurrealDbError {\n\tname = \"UnsupportedEngine\";\n\tmessage =\n\t\t\"The engine you are trying to connect to is not supported or configured.\";\n\n\tconstructor(public readonly engine: string) {\n\t\tsuper();\n\t}\n}\n\nexport class FeatureUnavailableForEngine extends SurrealDbError {\n\tname = \"FeatureUnavailableForEngine\";\n\tmessage =\n\t\t\"The feature you are trying to use is not available on this engine.\";\n}\n\nexport class ConnectionUnavailable extends SurrealDbError {\n\tname = \"ConnectionUnavailable\";\n\tmessage = \"There is no connection available at this moment.\";\n}\n\nexport class MissingNamespaceDatabase extends SurrealDbError {\n\tname = \"MissingNamespaceDatabase\";\n\tmessage = \"There is no namespace and/or database selected.\";\n}\n\nexport class HttpConnectionError extends SurrealDbError {\n\tname = \"HttpConnectionError\";\n\n\tconstructor(\n\t\tpublic readonly message: string,\n\t\tpublic readonly status: number,\n\t\tpublic readonly statusText: string,\n\t\tpublic readonly buffer: ArrayBuffer,\n\t) {\n\t\tsuper();\n\t}\n}\n\nexport class ResponseError extends SurrealDbError {\n\tname = \"ResponseError\";\n\n\tconstructor(public readonly message: string) {\n\t\tsuper();\n\t}\n}\n\nexport class NoNamespaceSpecified extends SurrealDbError {\n\tname = \"NoNamespaceSpecified\";\n\tmessage = \"Please specify a namespace to use.\";\n}\n\nexport class NoDatabaseSpecified extends SurrealDbError {\n\tname = \"NoDatabaseSpecified\";\n\tmessage = \"Please specify a database to use.\";\n}\n\nexport class NoTokenReturned extends SurrealDbError {\n\tname = \"NoTokenReturned\";\n\tmessage = \"Did not receive an authentication token.\";\n}\n\nexport class UnsupportedVersion extends SurrealDbError {\n\tname = \"UnsupportedVersion\";\n\tversion: string;\n\tsupportedRange: string;\n\n\tconstructor(version: string, supportedRange: string) {\n\t\tsuper();\n\t\tthis.version = version;\n\t\tthis.supportedRange = supportedRange;\n\t\tthis.message = `The version \"${version}\" reported by the engine is not supported by this library, expected a version that satisfies \"${supportedRange}\".`;\n\t}\n}\n\nexport class VersionRetrievalFailure extends SurrealDbError {\n\tname = \"VersionRetrievalFailure\";\n\tmessage =\n\t\t\"Failed to retrieve remote version. If the server is behind a proxy, make sure it's configured correctly.\";\n\n\tconstructor(readonly error?: Error | undefined) {\n\t\tsuper();\n\t}\n}\n", "import { SurrealDbError } from \"../errors\";\n\nexport abstract class CborError extends SurrealDbError {\n\tabstract readonly name: string;\n\treadonly message: string;\n\n\tconstructor(message: string) {\n\t\tsuper();\n\t\tthis.message = message;\n\t}\n}\n\nexport class CborNumberError extends CborError {\n\tname = \"CborNumberError\";\n}\n\nexport class CborRangeError extends CborError {\n\tname = \"CborRangeError\";\n}\n\nexport class CborInvalidMajorError extends CborError {\n\tname = \"CborInvalidMajorError\";\n}\n\nexport class CborBreak extends CborError {\n\tname = \"CborBreak\";\n\tconstructor() {\n\t\tsuper(\"Came across a break which was not intercepted by the decoder\");\n\t}\n}\n\nexport class CborPartialDisabled extends CborError {\n\tname = \"CborPartialDisabled\";\n\tconstructor() {\n\t\tsuper(\n\t\t\t\"Tried to insert a Gap into a CBOR value, while partial mode is not enabled\",\n\t\t);\n\t}\n}\n\nexport class CborFillMissing extends CborError {\n\tname = \"CborFillMissing\";\n\tconstructor() {\n\t\tsuper(\"Fill for a gap is missing, and gap has no default\");\n\t}\n}\n", "import type { Major, Replacer } from \"./constants\";\nimport type { Gap } from \"./gap\";\nimport { PartiallyEncoded } from \"./partial\";\n\nexport class Writer {\n\tprivate _chunks: [ArrayBuffer, Gap][] = [];\n\tprivate _pos = 0;\n\tprivate _buf: ArrayBuffer;\n\tprivate _view: DataView;\n\tprivate _byte: Uint8Array;\n\n\tconstructor(readonly byteLength = 256) {\n\t\tthis._buf = new ArrayBuffer(this.byteLength);\n\t\tthis._view = new DataView(this._buf);\n\t\tthis._byte = new Uint8Array(this._buf);\n\t}\n\n\tchunk(gap: Gap): void {\n\t\tthis._chunks.push([this._buf.slice(0, this._pos), gap]);\n\t\tthis._buf = new ArrayBuffer(this.byteLength);\n\t\tthis._view = new DataView(this._buf);\n\t\tthis._byte = new Uint8Array(this._buf);\n\t\tthis._pos = 0;\n\t}\n\n\tget chunks(): [ArrayBuffer, Gap][] {\n\t\treturn this._chunks;\n\t}\n\n\tget buffer(): ArrayBuffer {\n\t\treturn this._buf.slice(0, this._pos);\n\t}\n\n\tprivate claim(length: number) {\n\t\tconst pos = this._pos;\n\t\tthis._pos += length;\n\t\tif (this._pos <= this._buf.byteLength) return pos;\n\n\t\tlet newLen = this._buf.byteLength << 1;\n\t\twhile (newLen < this._pos) newLen <<= 1;\n\t\tif (newLen > this._buf.byteLength) {\n\t\t\tconst oldb = this._byte;\n\t\t\tthis._buf = new ArrayBuffer(newLen);\n\t\t\tthis._view = new DataView(this._buf);\n\t\t\tthis._byte = new Uint8Array(this._buf);\n\t\t\tthis._byte.set(oldb);\n\t\t}\n\t\treturn pos;\n\t}\n\n\twriteUint8(value: number): void {\n\t\tconst pos = this.claim(1);\n\t\tthis._view.setUint8(pos, value);\n\t}\n\n\twriteUint16(value: number): void {\n\t\tconst pos = this.claim(2);\n\t\tthis._view.setUint16(pos, value);\n\t}\n\n\twriteUint32(value: number): void {\n\t\tconst pos = this.claim(4);\n\t\tthis._view.setUint32(pos, value);\n\t}\n\n\twriteUint64(value: bigint): void {\n\t\tconst pos = this.claim(8);\n\t\tthis._view.setBigUint64(pos, value);\n\t}\n\n\twriteUint8Array(data: Uint8Array): void {\n\t\tif (data.byteLength === 0) return;\n\t\tconst pos = this.claim(data.byteLength);\n\t\tthis._byte.set(data, pos);\n\t}\n\n\twriteArrayBuffer(data: ArrayBuffer): void {\n\t\tif (data.byteLength === 0) return;\n\t\tthis.writeUint8Array(new Uint8Array(data));\n\t}\n\n\twritePartiallyEncoded(data: PartiallyEncoded): void {\n\t\tfor (const [buf, gap] of data.chunks) {\n\t\t\tthis.writeArrayBuffer(buf);\n\t\t\tthis.chunk(gap);\n\t\t}\n\n\t\tthis.writeArrayBuffer(data.end);\n\t}\n\n\twriteFloat32(value: number): void {\n\t\tconst pos = this.claim(4);\n\t\tthis._view.setFloat32(pos, value);\n\t}\n\n\twriteFloat64(value: number): void {\n\t\tconst pos = this.claim(8);\n\t\tthis._view.setFloat64(pos, value);\n\t}\n\n\twriteMajor(type: Major, length: number | bigint): void {\n\t\tconst base = type << 5;\n\t\tif (length < 24) {\n\t\t\tthis.writeUint8(base + Number(length));\n\t\t} else if (length < 0x100) {\n\t\t\tthis.writeUint8(base + 24);\n\t\t\tthis.writeUint8(Number(length));\n\t\t} else if (length < 0x10000) {\n\t\t\tthis.writeUint8(base + 25);\n\t\t\tthis.writeUint16(Number(length));\n\t\t} else if (length < 0x100000000) {\n\t\t\tthis.writeUint8(base + 26);\n\t\t\tthis.writeUint32(Number(length));\n\t\t} else {\n\t\t\tthis.writeUint8(base + 27);\n\t\t\tthis.writeUint64(BigInt(length));\n\t\t}\n\t}\n\n\toutput<Partial extends boolean = false>(\n\t\tpartial: Partial,\n\t\treplacer?: Replacer,\n\t): Partial extends true ? PartiallyEncoded : ArrayBuffer {\n\t\tif (partial) {\n\t\t\treturn new PartiallyEncoded(\n\t\t\t\tthis._chunks,\n\t\t\t\tthis.buffer,\n\t\t\t\treplacer,\n\t\t\t) as Partial extends true ? PartiallyEncoded : ArrayBuffer;\n\t\t}\n\n\t\treturn this.buffer as Partial extends true ? PartiallyEncoded : ArrayBuffer;\n\t}\n}\n", "import type { Replacer } from \"./constants\";\nimport { type EncoderOptions, encode } from \"./encoder\";\nimport { CborFillMissing } from \"./error\";\nimport type { Fill, Gap } from \"./gap\";\nimport { Writer } from \"./writer\";\n\nexport class PartiallyEncoded {\n\tconstructor(\n\t\treadonly chunks: [ArrayBuffer, Gap][],\n\t\treadonly end: ArrayBuffer,\n\t\treadonly replacer: Replacer | undefined,\n\t) {}\n\n\tbuild<Partial extends boolean = false>(\n\t\tfills: Fill[],\n\t\tpartial?: Partial,\n\t): Partial extends true ? PartiallyEncoded : ArrayBuffer {\n\t\tconst writer = new Writer();\n\t\tconst map = new Map(fills);\n\n\t\tfor (const [buffer, gap] of this.chunks) {\n\t\t\tconst hasValue = map.has(gap) || gap.hasDefault();\n\t\t\tif (!partial && !hasValue) throw new CborFillMissing();\n\t\t\twriter.writeArrayBuffer(buffer);\n\n\t\t\tif (hasValue) {\n\t\t\t\tconst data = map.get(gap) ?? gap.default;\n\t\t\t\tencode(data, {\n\t\t\t\t\twriter,\n\t\t\t\t\treplacer: this.replacer,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\twriter.chunk(gap);\n\t\t\t}\n\t\t}\n\n\t\twriter.writeArrayBuffer(this.end);\n\t\treturn writer.output<Partial>(!!partial as Partial, this.replacer);\n\t}\n}\n\nexport function partiallyEncodeObject(\n\tobject: Record<string, unknown>,\n\toptions?: EncoderOptions<true>,\n): Record<string, PartiallyEncoded> {\n\treturn Object.fromEntries(\n\t\tObject.entries(object).map(([k, v]) => [\n\t\t\tk,\n\t\t\tencode(v, { ...options, partial: true }),\n\t\t]),\n\t);\n}\n", "export class Tagged<T = unknown> {\n\tconstructor(\n\t\treadonly tag: number | bigint,\n\t\treadonly value: T,\n\t) {}\n}\n", "import { POW_2_53, POW_2_64, type Replacer } from \"./constants\";\nimport { Encoded } from \"./encoded\";\nimport { CborNumberError, CborPartialDisabled } from \"./error\";\nimport { type Fill, Gap } from \"./gap\";\nimport { PartiallyEncoded } from \"./partial\";\nimport { Tagged } from \"./tagged\";\nimport { Writer } from \"./writer\";\n\nlet textEncoder: TextEncoder;\n\nexport interface EncoderOptions<Partial extends boolean> {\n\treplacer?: Replacer;\n\twriter?: Writer;\n\tpartial?: Partial;\n\tfills?: Fill[];\n}\n\nexport function encode<Partial extends boolean = false>(\n\tinput: unknown,\n\toptions: EncoderOptions<Partial> = {},\n): Partial extends true ? PartiallyEncoded : ArrayBuffer {\n\tconst w = options.writer ?? new Writer();\n\tconst fillsMap = new Map(options.fills ?? []);\n\n\tfunction inner(input: unknown) {\n\t\tconst value = options.replacer ? options.replacer(input) : input;\n\n\t\tif (value === undefined) return w.writeUint8(0xf7);\n\t\tif (value === null) return w.writeUint8(0xf6);\n\t\tif (value === true) return w.writeUint8(0xf5);\n\t\tif (value === false) return w.writeUint8(0xf4);\n\n\t\tswitch (typeof value) {\n\t\t\tcase \"number\": {\n\t\t\t\tif (Number.isInteger(value)) {\n\t\t\t\t\tif (value >= 0 && value <= POW_2_53) {\n\t\t\t\t\t\tw.writeMajor(0, value);\n\t\t\t\t\t} else if (value < 0 && value >= -POW_2_53) {\n\t\t\t\t\t\tw.writeMajor(1, -(value + 1));\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new CborNumberError(\"Number too big to be encoded\");\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Better precision when encoded as 64-bit\n\t\t\t\t\tw.writeUint8(0xfb);\n\t\t\t\t\tw.writeFloat64(value);\n\t\t\t\t}\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tcase \"bigint\": {\n\t\t\t\tif (value >= 0 && value < POW_2_64) {\n\t\t\t\t\tw.writeMajor(0, value);\n\t\t\t\t} else if (value <= 0 && value >= -POW_2_64) {\n\t\t\t\t\tw.writeMajor(1, -(value + 1n));\n\t\t\t\t} else {\n\t\t\t\t\tthrow new CborNumberError(\"BigInt too big to be encoded\");\n\t\t\t\t}\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tcase \"string\": {\n\t\t\t\ttextEncoder ??= new TextEncoder();\n\t\t\t\tconst encoded = textEncoder.encode(value);\n\t\t\t\tw.writeMajor(3, encoded.byteLength);\n\t\t\t\tw.writeUint8Array(encoded);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tif (Array.isArray(value)) {\n\t\t\t\t\tw.writeMajor(4, value.length);\n\t\t\t\t\tfor (const v of value) {\n\t\t\t\t\t\tinner(v);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (value instanceof Tagged) {\n\t\t\t\t\tw.writeMajor(6, value.tag);\n\t\t\t\t\tinner(value.value);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (value instanceof Encoded) {\n\t\t\t\t\tw.writeArrayBuffer(value.encoded);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (value instanceof Gap) {\n\t\t\t\t\tif (fillsMap.has(value)) {\n\t\t\t\t\t\tinner(fillsMap.get(value));\n\t\t\t\t\t} else {\n\t\t\t\t\t\tif (!options.partial) throw new CborPartialDisabled();\n\t\t\t\t\t\tw.chunk(value);\n\t\t\t\t\t}\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (value instanceof PartiallyEncoded) {\n\t\t\t\t\tconst res = value.build<Partial>(\n\t\t\t\t\t\toptions.fills ?? [],\n\t\t\t\t\t\toptions.partial,\n\t\t\t\t\t);\n\t\t\t\t\tif (options.partial) {\n\t\t\t\t\t\tw.writePartiallyEncoded(res as PartiallyEncoded);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tw.writeArrayBuffer(res as ArrayBuffer);\n\t\t\t\t\t}\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (\n\t\t\t\t\tvalue instanceof Uint8Array ||\n\t\t\t\t\tvalue instanceof Uint16Array ||\n\t\t\t\t\tvalue instanceof Uint32Array ||\n\t\t\t\t\tvalue instanceof Int8Array ||\n\t\t\t\t\tvalue instanceof Int16Array ||\n\t\t\t\t\tvalue instanceof Int32Array ||\n\t\t\t\t\tvalue instanceof Float32Array ||\n\t\t\t\t\tvalue instanceof Float64Array ||\n\t\t\t\t\tvalue instanceof ArrayBuffer\n\t\t\t\t) {\n\t\t\t\t\tconst v = new Uint8Array(value);\n\t\t\t\t\tw.writeMajor(2, v.byteLength);\n\t\t\t\t\tw.writeUint8Array(v);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst entries =\n\t\t\t\t\tvalue instanceof Map\n\t\t\t\t\t\t? Array.from(value.entries())\n\t\t\t\t\t\t: Object.entries(value);\n\n\t\t\t\tw.writeMajor(5, entries.length);\n\t\t\t\tfor (const v of entries.flat()) {\n\t\t\t\t\tinner(v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tinner(input);\n\treturn w.output<Partial>(!!options.partial as Partial, options.replacer);\n}\n", "import { type Major, POW_2_53 } from \"./constants\";\nimport { CborInvalidMajorError, CborRangeError } from \"./error\";\n\nexport class Reader {\n\tprivate _buf: ArrayBufferLike;\n\tprivate _view: DataView;\n\tprivate _byte: Uint8Array;\n\tprivate _pos = 0;\n\n\tconstructor(buffer: ArrayBufferLike) {\n\t\tthis._buf = new ArrayBuffer(buffer.byteLength);\n\t\tthis._view = new DataView(this._buf);\n\t\tthis._byte = new Uint8Array(this._buf);\n\t\tthis._byte.set(new Uint8Array(buffer));\n\t}\n\n\tprivate read<T>(amount: number, res: T): T {\n\t\tthis._pos += amount;\n\t\treturn res;\n\t}\n\n\treadUint8(): number {\n\t\ttry {\n\t\t\treturn this.read(1, this._view.getUint8(this._pos));\n\t\t} catch (e) {\n\t\t\tif (e instanceof RangeError) throw new CborRangeError(e.message);\n\t\t\tthrow e;\n\t\t}\n\t}\n\n\treadUint16(): number {\n\t\ttry {\n\t\t\treturn this.read(2, this._view.getUint16(this._pos));\n\t\t} catch (e) {\n\t\t\tif (e instanceof RangeError) throw new CborRangeError(e.message);\n\t\t\tthrow e;\n\t\t}\n\t}\n\n\treadUint32(): number {\n\t\ttry {\n\t\t\treturn this.read(4, this._view.getUint32(this._pos));\n\t\t} catch (e) {\n\t\t\tif (e instanceof RangeError) throw new CborRangeError(e.message);\n\t\t\tthrow e;\n\t\t}\n\t}\n\n\treadUint64(): bigint {\n\t\ttry {\n\t\t\treturn this.read(8, this._view.getBigUint64(this._pos));\n\t\t} catch (e) {\n\t\t\tif (e instanceof RangeError) throw new CborRangeError(e.message);\n\t\t\tthrow e;\n\t\t}\n\t}\n\n\t// https://stackoverflow.com/a/5684578\n\treadFloat16(): number {\n\t\tconst bytes = this.readUint16();\n\t\tconst s = (bytes & 0x8000) >> 15;\n\t\tconst e = (bytes & 0x7c00) >> 10;\n\t\tconst f = bytes & 0x03ff;\n\n\t\tif (e === 0) {\n\t\t\treturn (s ? -1 : 1) * 2 ** -14 * (f / 2 ** 10);\n\t\t}\n\n\t\tif (e === 0x1f) {\n\t\t\treturn f ? Number.NaN : (s ? -1 : 1) * Number.POSITIVE_INFINITY;\n\t\t}\n\n\t\treturn (s ? -1 : 1) * 2 ** (e - 15) * (1 + f / 2 ** 10);\n\t}\n\n\treadFloat32(): number {\n\t\ttry {\n\t\t\treturn this.read(4, this._view.getFloat32(this._pos));\n\t\t} catch (e) {\n\t\t\tif (e instanceof RangeError) throw new CborRangeError(e.message);\n\t\t\tthrow e;\n\t\t}\n\t}\n\n\treadFloat64(): number {\n\t\ttry {\n\t\t\treturn this.read(8, this._view.getFloat64(this._pos));\n\t\t} catch (e) {\n\t\t\tif (e instanceof RangeError) throw new CborRangeError(e.message);\n\t\t\tthrow e;\n\t\t}\n\t}\n\n\treadBytes(amount: number): Uint8Array {\n\t\tconst available = this._byte.length - this._pos;\n\t\tif (available < amount)\n\t\t\tthrow new CborRangeError(\n\t\t\t\t`The argument must be between 0 and ${available}`,\n\t\t\t);\n\n\t\treturn this.read(amount, this._byte.slice(this._pos, this._pos + amount));\n\t}\n\n\treadMajor(): [Major, number] {\n\t\tconst byte = this.readUint8();\n\t\tconst major = (byte >> 5) as Major;\n\t\tif (major < 0 || major > 7)\n\t\t\tthrow new CborInvalidMajorError(\"Received invalid major type\");\n\t\treturn [major, byte & 0x1f];\n\t}\n\n\treadMajorLength(length: number): number | bigint {\n\t\tif (length <= 23) return length;\n\n\t\tswitch (length) {\n\t\t\tcase 24:\n\t\t\t\treturn this.readUint8();\n\t\t\tcase 25:\n\t\t\t\treturn this.readUint16();\n\t\t\tcase 26:\n\t\t\t\treturn this.readUint32();\n\t\t\tcase 27: {\n\t\t\t\tconst read = this.readUint64();\n\t\t\t\treturn read > POW_2_53 ? read : Number(read);\n\t\t\t}\n\t\t}\n\n\t\tthrow new CborRangeError(\"Expected a final length\");\n\t}\n}\n", "import type { Major } from \"./constants\";\nimport { CborInvalidMajorError, CborRangeError } from \"./error\";\nimport type { Reader } from \"./reader\";\nimport { Writer } from \"./writer\";\n\nexport function infiniteBytes(r: Reader, forMajor: Major): ArrayBuffer {\n\tconst w = new Writer();\n\twhile (true) {\n\t\tconst [major, len] = r.readMajor();\n\n\t\t// Received break signal\n\t\tif (major === 7 && len === 31) break;\n\n\t\t// Resource type has to match\n\t\tif (major !== forMajor)\n\t\t\tthrow new CborInvalidMajorError(\n\t\t\t\t`Expected a resource of the same major (${forMajor}) while processing an infinite resource`,\n\t\t\t);\n\n\t\t// Cannot have an infinite resource in an infinite resource\n\t\tif (len === 31)\n\t\t\tthrow new CborRangeError(\n\t\t\t\t\"Expected a finite resource while processing an infinite resource\",\n\t\t\t);\n\n\t\tw.writeUint8Array(r.readBytes(Number(r.readMajorLength(len))));\n\t}\n\n\treturn w.buffer;\n}\n", "import type { Replacer } from \"./constants\";\nimport { CborBreak, CborInvalidMajorError } from \"./error\";\nimport { Reader } from \"./reader\";\nimport { Tagged } from \"./tagged\";\nimport { infiniteBytes } from \"./util\";\n\nlet textDecoder: TextDecoder;\n\nexport interface DecodeOptions {\n\tmap?: \"object\" | \"map\";\n\treplacer?: Replacer;\n}\n\nexport function decode(\n\tinput: ArrayBufferLike | Reader,\n\toptions: DecodeOptions = {},\n\t// biome-ignore lint/suspicious/noExplicitAny: Don't know what it will return\n): any {\n\tconst r = input instanceof Reader ? input : new Reader(input);\n\n\tfunction inner() {\n\t\tconst [major, len] = r.readMajor();\n\t\tswitch (major) {\n\t\t\tcase 0:\n\t\t\t\treturn r.readMajorLength(len);\n\t\t\tcase 1: {\n\t\t\t\tconst l = r.readMajorLength(len);\n\t\t\t\treturn typeof l === \"bigint\" ? -(l + 1n) : -(l + 1);\n\t\t\t}\n\t\t\tcase 2: {\n\t\t\t\tif (len === 31) return infiniteBytes(r, 2);\n\t\t\t\treturn r.readBytes(Number(r.readMajorLength(len))).buffer;\n\t\t\t}\n\t\t\tcase 3: {\n\t\t\t\tconst encoded =\n\t\t\t\t\tlen === 31\n\t\t\t\t\t\t? infiniteBytes(r, 3)\n\t\t\t\t\t\t: r.readBytes(Number(r.readMajorLength(len)));\n\n\t\t\t\ttextDecoder ??= new TextDecoder();\n\t\t\t\treturn textDecoder.decode(encoded);\n\t\t\t}\n\n\t\t\tcase 4: {\n\t\t\t\tif (len === 31) {\n\t\t\t\t\tconst arr = [];\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tarr.push(decode());\n\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\tif (e instanceof CborBreak) break;\n\t\t\t\t\t\t\tthrow e;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn arr;\n\t\t\t\t}\n\n\t\t\t\tconst l = r.readMajorLength(len);\n\t\t\t\tconst arr = Array(l);\n\t\t\t\tfor (let i = 0; i < l; i++) arr[i] = decode();\n\t\t\t\treturn arr;\n\t\t\t}\n\n\t\t\tcase 5: {\n\t\t\t\tconst entries: [string, unknown][] = [];\n\t\t\t\tif (len === 31) {\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\tlet key: string;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tkey = decode();\n\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\tif (e instanceof CborBreak) break;\n\t\t\t\t\t\t\tthrow e;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst value = decode();\n\t\t\t\t\t\tentries.push([key, value]);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tconst l = r.readMajorLength(len);\n\t\t\t\t\tfor (let i = 0; i < l; i++) {\n\t\t\t\t\t\tconst key = decode();\n\t\t\t\t\t\tconst value = decode();\n\t\t\t\t\t\tentries[i] = [key, value];\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn options.map === \"map\"\n\t\t\t\t\t? new Map(entries)\n\t\t\t\t\t: Object.fromEntries(entries);\n\t\t\t}\n\n\t\t\tcase 6: {\n\t\t\t\tconst tag = r.readMajorLength(len);\n\t\t\t\tconst value = decode();\n\t\t\t\treturn new Tagged(tag, value);\n\t\t\t}\n\n\t\t\tcase 7: {\n\t\t\t\tswitch (len) {\n\t\t\t\t\tcase 20:\n\t\t\t\t\t\treturn false;\n\t\t\t\t\tcase 21:\n\t\t\t\t\t\treturn true;\n\t\t\t\t\tcase 22:\n\t\t\t\t\t\treturn null;\n\t\t\t\t\tcase 23:\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\tcase 25:\n\t\t\t\t\t\treturn r.readFloat16();\n\t\t\t\t\tcase 26:\n\t\t\t\t\t\treturn r.readFloat32();\n\t\t\t\t\tcase 27:\n\t\t\t\t\t\treturn r.readFloat64();\n\t\t\t\t\tcase 31:\n\t\t\t\t\t\tthrow new CborBreak();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthrow new CborInvalidMajorError(\n\t\t\t`Unable to decode value with major tag ${major}`,\n\t\t);\n\t}\n\n\t// biome-ignore lint/suspicious/noExplicitAny: Don't know what it will return\n\tfunction decode(): any {\n\t\treturn options.replacer ? options.replacer(inner()) : inner();\n\t}\n\n\treturn decode();\n}\n", "export function msToNs(ms: number): number {\n\treturn ms * 1000000;\n}\n\nexport function nsToMs(ns: number): number {\n\treturn Math.floor(ns / 1000000);\n}\n\nexport function dateToCborCustomDate(date: Date): [number, number] {\n\tconst s = Math.floor(date.getTime() / 1000);\n\tconst ms = date.getTime() - s * 1000;\n\treturn [s, ms * 1000000];\n}\n\nexport function cborCustomDateToDate([s, ns]: [number, number]): Date {\n\tconst date = new Date(0);\n\tdate.setUTCSeconds(Number(s));\n\tdate.setMilliseconds(Math.floor(Number(ns) / 1000000));\n\treturn date;\n}\n", "/**\n * A complex SurrealQL value type\n */\nexport abstract class Value {\n\t/**\n\t * Compare equality with another value.\n\t */\n\tabstract equals(other: unknown): boolean;\n\n\t/**\n\t * Convert this value to a serializable string\n\t */\n\tabstract toJSON(): unknown;\n\n\t/**\n\t * Convert this value to a string representation\n\t */\n\tabstract toString(): string;\n}\n", "import { Value } from \"../value\";\n\n/**\n * A SurrealQL decimal value.\n */\nexport class Decimal extends Value {\n\treadonly decimal: string;\n\n\tconstructor(decimal: string | number | Decimal) {\n\t\tsuper();\n\t\tthis.decimal = decimal.toString();\n\t}\n\n\tequals(other: unknown): boolean {\n\t\tif (!(other instanceof Decimal)) return false;\n\t\treturn this.decimal === other.decimal;\n\t}\n\n\ttoString(): string {\n\t\treturn this.decimal;\n\t}\n\n\ttoJSON(): string {\n\t\treturn this.decimal;\n\t}\n}\n", "import { SurrealDbError } from \"../../errors\";\nimport { Value } from \"../value\";\n\nconst millisecond = 1;\nconst microsecond = millisecond / 1000;\nconst nanosecond = microsecond / 1000;\nconst second = 1000 * millisecond;\nconst minute = 60 * second;\nconst hour = 60 * minute;\nconst day = 24 * hour;\nconst week = 7 * day;\n\nconst units = new Map([\n\t[\"ns\", nanosecond],\n\t[\"\u00B5s\", microsecond],\n\t[\"\u03BCs\", microsecond], // They look similar, but this unit is a different charachter than the one above it.\n\t[\"us\", microsecond], // needs to come last to be the displayed unit\n\t[\"ms\", millisecond],\n\t[\"s\", second],\n\t[\"m\", minute],\n\t[\"h\", hour],\n\t[\"d\", day],\n\t[\"w\", week],\n]);\n\nconst unitsReverse = Array.from(units).reduce((map, [unit, size]) => {\n\tmap.set(size, unit);\n\treturn map;\n}, new Map<number, string>());\n\nconst durationPartRegex = new RegExp(\n\t`^(\\\\d+)(${Array.from(units.keys()).join(\"|\")})`,\n);\n\n/**\n * A SurrealQL duration value.\n */\nexport class Duration extends Value {\n\treadonly _milliseconds: number;\n\n\tconstructor(input: Duration | number | string) {\n\t\tsuper();\n\n\t\tif (input instanceof Duration) {\n\t\t\tthis._milliseconds = input._milliseconds;\n\t\t} else if (typeof input === \"string\") {\n\t\t\tthis._milliseconds = Duration.parseString(input);\n\t\t} else {\n\t\t\tthis._milliseconds = input;\n\t\t}\n\t}\n\n\tstatic fromCompact([s, ns]: [number, number] | [number] | []): Duration {\n\t\ts = s ?? 0;\n\t\tns = ns ?? 0;\n\t\tconst ms = s * 1000 + ns / 1000000;\n\t\treturn new Duration(ms);\n\t}\n\n\tequals(other: unknown): boolean {\n\t\tif (!(other instanceof Duration)) return false;\n\t\treturn this._milliseconds === other._milliseconds;\n\t}\n\n\ttoCompact(): [number, number] | [number] | [] {\n\t\tconst s = Math.floor(this._milliseconds / 1000);\n\t\tconst ns = Math.floor((this._milliseconds - s * 1000) * 1000000);\n\t\treturn ns > 0 ? [s, ns] : s > 0 ? [s] : [];\n\t}\n\n\ttoString(): string {\n\t\tlet left = this._milliseconds;\n\t\tlet result = \"\";\n\t\tfunction scrap(size: number) {\n\t\t\tconst num = Math.floor(left / size);\n\t\t\tif (num > 0) left = left % size;\n\t\t\treturn num;\n\t\t}\n\n\t\tfor (const [size, unit] of Array.from(unitsReverse).reverse()) {\n\t\t\tconst scrapped = scrap(size);\n\t\t\tif (scrapped > 0) result += `${scrapped}${unit}`;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\ttoJSON(): string {\n\t\treturn this.toString();\n\t}\n\n\tstatic parseString(input: string): number {\n\t\tlet ms = 0;\n\t\tlet left = input;\n\t\twhile (left !== \"\") {\n\t\t\tconst match = left.match(durationPartRegex);\n\t\t\tif (match) {\n\t\t\t\tconst amount = Number.parseInt(match[1]);\n\t\t\t\tconst factor = units.get(match[2]);\n\t\t\t\tif (factor === undefined)\n\t\t\t\t\tthrow new SurrealDbError(`Invalid duration unit: ${match[2]}`);\n\n\t\t\t\tms += amount * factor;\n\t\t\t\tleft = left.slice(match[0].length);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tthrow new SurrealDbError(\"Could not match a next duration part\");\n\t\t}\n\n\t\treturn ms;\n\t}\n\n\tstatic nanoseconds(nanoseconds: number): Duration {\n\t\treturn new Duration(Math.floor(nanoseconds * nanosecond));\n\t}\n\n\tstatic microseconds(microseconds: number): Duration {\n\t\treturn new Duration(Math.floor(microseconds * microsecond));\n\t}\n\n\tstatic milliseconds(milliseconds: number): Duration {\n\t\treturn new Duration(milliseconds);\n\t}\n\n\tstatic seconds(seconds: number): Duration {\n\t\treturn new Duration(seconds * second);\n\t}\n\n\tstatic minutes(minutes: number): Duration {\n\t\treturn new Duration(minutes * minute);\n\t}\n\n\tstatic hours(hours: number): Duration {\n\t\treturn new Duration(hours * hour);\n\t}\n\n\tstatic days(days: number): Duration {\n\t\treturn new Duration(days * day);\n\t}\n\n\tstatic weeks(weeks: number): Duration {\n\t\treturn new Duration(weeks * week);\n\t}\n\n\tget microseconds(): number {\n\t\treturn Math.floor(this._milliseconds / microsecond);\n\t}\n\n\tget nanoseconds(): number {\n\t\treturn Math.floor(this._milliseconds / nanosecond);\n\t}\n\n\tget milliseconds(): number {\n\t\treturn Math.floor(this._milliseconds);\n\t}\n\n\tget seconds(): number {\n\t\treturn Math.floor(this._milliseconds / second);\n\t}\n\n\tget minutes(): number {\n\t\treturn Math.floor(this._milliseconds / minute);\n\t}\n\n\tget hours(): number {\n\t\treturn Math.floor(this._milliseconds / hour);\n\t}\n\n\tget days(): number {\n\t\treturn Math.floor(this._milliseconds / day);\n\t}\n\n\tget weeks(): number {\n\t\treturn Math.floor(this._milliseconds / week);\n\t}\n}\n", "import { Value } from \"../value\";\n\n/**\n * An uncomputed SurrealQL future value.\n */\nexport class Future extends Value {\n\tconstructor(readonly inner: string) {\n\t\tsuper();\n\t}\n\n\tequals(other: unknown): boolean {\n\t\tif (!(other instanceof Future)) return false;\n\t\treturn this.inner === other.inner;\n\t}\n\n\ttoJSON(): string {\n\t\treturn this.toString();\n\t}\n\n\ttoString(): string {\n\t\treturn `<future> ${this.inner}`;\n\t}\n}\n", "import { Value } from \"../value.ts\";\nimport { Decimal } from \"./decimal.ts\";\n\n/**\n * A SurrealQL geometry value.\n */\nexport abstract class Geometry extends Value {\n\tabstract toJSON(): GeoJson;\n\tabstract is(geometry: Geometry): boolean;\n\tabstract clone(): Geometry;\n\n\tequals(other: unknown): boolean {\n\t\tif (!(other instanceof Geometry)) return false;\n\t\treturn this.is(other);\n\t}\n\n\ttoString(): string {\n\t\treturn JSON.stringify(this.toJSON());\n\t}\n}\n\nfunction f(num: number | Decimal) {\n\tif (num instanceof Decimal) return Number.parseFloat(num.decimal);\n\treturn num;\n}\n\n/**\n * A SurrealQL point geometry value.\n */\nexport class GeometryPoint extends Geometry {\n\treadonly point: [number, number];\n\n\tconstructor(point: [number | Decimal, number | Decimal] | GeometryPoint) {\n\t\tsuper();\n\t\tif (point instanceof GeometryPoint) {\n\t\t\tthis.point = point.clone().point;\n\t\t} else {\n\t\t\tthis.point = [f(point[0]), f(point[1])];\n\t\t}\n\t}\n\n\ttoJSON(): GeoJsonPoint {\n\t\treturn {\n\t\t\ttype: \"Point\" as const,\n\t\t\tcoordinates: this.coordinates,\n\t\t};\n\t}\n\n\tget coordinates(): GeoJsonPoint[\"coordinates\"] {\n\t\treturn this.point;\n\t}\n\n\tis(geometry: Geometry): geometry is GeometryPoint {\n\t\tif (!(geometry instanceof GeometryPoint)) return false;\n\t\treturn (\n\t\t\tthis.point[0] === geometry.point[0] && this.point[1] === geometry.point[1]\n\t\t);\n\t}\n\n\tclone(): GeometryPoint {\n\t\treturn new GeometryPoint([...this.point]);\n\t}\n}\n\n/**\n * A SurrealQL line geometry value.\n */\nexport class GeometryLine extends Geometry {\n\treadonly line: [GeometryPoint, GeometryPoint, ...GeometryPoint[]];\n\n\t// SurrealDB only has the concept of a \"Line\", which by spec is two points.\n\t// SurrealDB's \"Line\" however, is actually a \"LineString\" under the hood, which accepts two or more points\n\tconstructor(\n\t\tline: [GeometryPoint, GeometryPoint, ...GeometryPoint[]] | GeometryLine,\n\t) {\n\t\tsuper();\n\t\tthis.line = line instanceof GeometryLine ? line.clone().line : line;\n\t}\n\n\ttoJSON(): GeoJsonLineString {\n\t\treturn {\n\t\t\ttype: \"LineString\" as const,\n\t\t\tcoordinates: this.coordinates,\n\t\t};\n\t}\n\n\tget coordinates(): GeoJsonLineString[\"coordinates\"] {\n\t\treturn this.line.map(\n\t\t\t(g) => g.coordinates,\n\t\t) as GeoJsonLineString[\"coordinates\"];\n\t}\n\n\tclose(): void {\n\t\tif (!this.line[0].is(this.line.at(-1) as GeometryPoint)) {\n\t\t\tthis.line.push(this.line[0]);\n\t\t}\n\t}\n\n\tis(geometry: Geometry): geometry is GeometryLine {\n\t\tif (!(geometry instanceof GeometryLine)) return false;\n\t\tif (this.line.length !== geometry.line.length) return false;\n\t\tfor (let i = 0; i < this.line.length; i++) {\n\t\t\tif (!this.line[i].is(geometry.line[i])) return false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tclone(): GeometryLine {\n\t\treturn new GeometryLine(\n\t\t\tthis.line.map((p) => p.clone()) as [\n\t\t\t\tGeometryPoint,\n\t\t\t\tGeometryPoint,\n\t\t\t\t...GeometryPoint[],\n\t\t\t],\n\t\t);\n\t}\n}\n\n/**\n * A SurrealQL polygon geometry value.\n */\nexport class GeometryPolygon extends Geometry {\n\treadonly polygon: [GeometryLine, ...GeometryLine[]];\n\n\tconstructor(polygon: [GeometryLine, ...GeometryLine[]] | GeometryPolygon) {\n\t\tsuper();\n\t\tthis.polygon =\n\t\t\tpolygon instanceof GeometryPolygon\n\t\t\t\t? polygon.clone().polygon\n\t\t\t\t: (polygon.map((l) => {\n\t\t\t\t\t\tconst line = l.clone();\n\t\t\t\t\t\tline.close();\n\t\t\t\t\t\treturn line;\n\t\t\t\t\t}) as [GeometryLine, ...GeometryLine[]]);\n\t}\n\n\ttoJSON(): GeoJsonPolygon {\n\t\treturn {\n\t\t\ttype: \"Polygon\" as const,\n\t\t\tcoordinates: this.coordinates,\n\t\t};\n\t}\n\n\tget coordinates(): GeoJsonPolygon[\"coordinates\"] {\n\t\treturn this.polygon.map(\n\t\t\t(g) => g.coordinates,\n\t\t) as GeoJsonPolygon[\"coordinates\"];\n\t}\n\n\tis(geometry: Geometry): geometry is GeometryPolygon {\n\t\tif (!(geometry instanceof GeometryPolygon)) return false;\n\t\tif (this.polygon.length !== geometry.polygon.length) return false;\n\t\tfor (let i = 0; i < this.polygon.length; i++) {\n\t\t\tif (!this.polygon[i].is(geometry.polygon[i])) return false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tclone(): GeometryPolygon {\n\t\treturn new GeometryPolygon(\n\t\t\tthis.polygon.map((p) => p.clone()) as [GeometryLine, ...GeometryLine[]],\n\t\t);\n\t}\n}\n\n/**\n * A SurrealQL multi-point geometry value.\n */\nexport class GeometryMultiPoint extends Geometry {\n\treadonly points: [GeometryPoint, ...GeometryPoint[]];\n\n\tconstructor(\n\t\tpoints: [GeometryPoint, ...GeometryPoint[]] | GeometryMultiPoint,\n\t) {\n\t\tsuper();\n\t\tthis.points = points instanceof GeometryMultiPoint ? points.points : points;\n\t}\n\n\ttoJSON(): GeoJsonMultiPoint {\n\t\treturn {\n\t\t\ttype: \"MultiPoint\" as const,\n\t\t\tcoordinates: this.coordinates,\n\t\t};\n\t}\n\n\tget coordinates(): GeoJsonMultiPoint[\"coordinates\"] {\n\t\treturn this.points.map(\n\t\t\t(g) => g.coordinates,\n\t\t) as GeoJsonMultiPoint[\"coordinates\"];\n\t}\n\n\tis(geometry: Geometry): geometry is GeometryMultiPoint {\n\t\tif (!(geometry instanceof GeometryMultiPoint)) return false;\n\t\tif (this.points.length !== geometry.points.length) return false;\n\t\tfor (let i = 0; i < this.points.length; i++) {\n\t\t\tif (!this.points[i].is(geometry.points[i])) return false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tclone(): GeometryMultiPoint {\n\t\treturn new GeometryMultiPoint(\n\t\t\tthis.points.map((p) => p.clone()) as [GeometryPoint, ...GeometryPoint[]],\n\t\t);\n\t}\n}\n\n/**\n * A SurrealQL multi-line geometry value.\n */\nexport class GeometryMultiLine extends Geometry {\n\treadonly lines: [GeometryLine, ...GeometryLine[]];\n\n\tconstructor(lines: [GeometryLine, ...GeometryLine[]] | GeometryMultiLine) {\n\t\tsuper();\n\t\tthis.lines = lines instanceof GeometryMultiLine ? lines.lines : lines;\n\t}\n\n\ttoJSON(): GeoJsonMultiLineString {\n\t\treturn {\n\t\t\ttype: \"MultiLineString\" as const,\n\t\t\tcoordinates: this.coordinates,\n\t\t};\n\t}\n\n\tget coordinates(): GeoJsonMultiLineString[\"coordinates\"] {\n\t\treturn this.lines.map(\n\t\t\t(g) => g.coordinates,\n\t\t) as GeoJsonMultiLineString[\"coordinates\"];\n\t}\n\n\tis(geometry: Geometry): geometry is GeometryMultiLine {\n\t\tif (!(geometry instanceof GeometryMultiLine)) return false;\n\t\tif (this.lines.length !== geometry.lines.length) return false;\n\t\tfor (let i = 0; i < this.lines.length; i++) {\n\t\t\tif (!this.lines[i].is(geometry.lines[i])) return false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tclone(): GeometryMultiLine {\n\t\treturn new GeometryMultiLine(\n\t\t\tthis.lines.map((p) => p.clone()) as [GeometryLine, ...GeometryLine[]],\n\t\t);\n\t}\n}\n\n/**\n * A SurrealQL multi-polygon geometry value.\n */\nexport class GeometryMultiPolygon extends Geometry {\n\treadonly polygons: [GeometryPolygon, ...GeometryPolygon[]];\n\n\tconstructor(\n\t\tpolygons: [GeometryPolygon, ...GeometryPolygon[]] | GeometryMultiPolygon,\n\t) {\n\t\tsuper();\n\t\tthis.polygons =\n\t\t\tpolygons instanceof GeometryMultiPolygon ? polygons.polygons : polygons;\n\t}\n\n\ttoJSON(): GeoJsonMultiPolygon {\n\t\treturn {\n\t\t\ttype: \"MultiPolygon\" as const,\n\t\t\tcoordinates: this.coordinates,\n\t\t};\n\t}\n\n\tget coordinates(): GeoJsonMultiPolygon[\"coordinates\"] {\n\t\treturn this.polygons.map(\n\t\t\t(g) => g.coordinates,\n\t\t) as GeoJsonMultiPolygon[\"coordinates\"];\n\t}\n\n\tis(geometry: Geometry): geometry is GeometryMultiPolygon {\n\t\tif (!(geometry instanceof GeometryMultiPolygon)) return false;\n\t\tif (this.polygons.length !== geometry.polygons.length) return false;\n\t\tfor (let i = 0; i < this.polygons.length; i++) {\n\t\t\tif (!this.polygons[i].is(geometry.polygons[i])) return false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tclone(): GeometryMultiPolygon {\n\t\treturn new GeometryMultiPolygon(\n\t\t\tthis.polygons.map((p) => p.clone()) as [\n\t\t\t\tGeometryPolygon,\n\t\t\t\t...GeometryPolygon[],\n\t\t\t],\n\t\t);\n\t}\n}\n\n/**\n * A SurrealQL geometry collection value.\n */\nexport class GeometryCollection extends Geometry {\n\treadonly collection: [Geometry, ...Geometry[]];\n\n\tconstructor(collection: [Geometry, ...Geometry[]] | GeometryCollection) {\n\t\tsuper();\n\t\tthis.collection =\n\t\t\tcollection instanceof GeometryCollection\n\t\t\t\t? collection.collection\n\t\t\t\t: collection;\n\t}\n\n\ttoJSON(): GeoJsonCollection {\n\t\treturn {\n\t\t\ttype: \"GeometryCollection\" as const,\n\t\t\tgeometries: this.geometries,\n\t\t};\n\t}\n\n\tget geometries(): GeoJsonCollection[\"geometries\"] {\n\t\treturn this.collection.map((g) =>\n\t\t\tg.toJSON(),\n\t\t) as GeoJsonCollection[\"geometries\"];\n\t}\n\n\tis(geometry: Geometry): geometry is GeometryCollection {\n\t\tif (!(geometry instanceof GeometryCollection)) return false;\n\t\tif (this.collection.length !== geometry.collection.length) return false;\n\t\tfor (let i = 0; i < this.collection.length; i++) {\n\t\t\tif (!this.collection[i].is(geometry.collection[i])) return false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tclone(): GeometryCollection {\n\t\treturn new GeometryCollection(\n\t\t\tthis.collection.map((p) => p.clone()) as [Geometry, ...Geometry[]],\n\t\t);\n\t}\n}\n\n// Geo Json Types\n\ntype GeoJson =\n\t| GeoJsonPoint\n\t| GeoJsonLineString\n\t| GeoJsonPolygon\n\t| GeoJsonMultiPoint\n\t| GeoJsonMultiLineString\n\t| GeoJsonMultiPolygon\n\t| GeoJsonCollection;\n\nexport type GeoJsonPoint = {\n\ttype: \"Point\";\n\tcoordinates: [number, number];\n};\n\nexport type GeoJsonLineString = {\n\ttype: \"LineString\";\n\tcoordinates: [\n\t\tGeoJsonPoint[\"coordinates\"],\n\t\tGeoJsonPoint[\"coordinates\"],\n\t\t...GeoJsonPoint[\"coordinates\"][],\n\t];\n};\n\nexport type GeoJsonPolygon = {\n\ttype: \"Polygon\";\n\tcoordinates: [\n\t\tGeoJsonLineString[\"coordinates\"],\n\t\t...GeoJsonLineString[\"coordinates\"][],\n\t];\n};\n\nexport type GeoJsonMultiPoint = {\n\ttype: \"MultiPoint\";\n\tcoordinates: [GeoJsonPoint[\"coordinates\"], ...GeoJsonPoint[\"coordinates\"][]];\n};\n\nexport type GeoJsonMultiLineString = {\n\ttype: \"MultiLineString\";\n\tcoordinates: [\n\t\tGeoJsonLineString[\"coordinates\"],\n\t\t...GeoJsonLineString[\"coordinates\"][],\n\t];\n};\n\nexport type GeoJsonMultiPolygon = {\n\ttype: \"MultiPolygon\";\n\tcoordinates: [\n\t\tGeoJsonPolygon[\"coordinates\"],\n\t\t...GeoJsonPolygon[\"coordinates\"][],\n\t];\n};\n\nexport type GeoJsonCollection = {\n\ttype: \"GeometryCollection\";\n\tgeometries: GeoJson[];\n};\n", "import { Value } from \"../data/value\";\n\n/**\n * Recursively compare supported SurrealQL values for equality.\n *\n * @param x The first value to compare\n * @param y The second value to compare\n * @returns Whether the two values are recursively equal\n */\nexport function equals(x: unknown, y: unknown): boolean {\n\tif (Object.is(x, y)) return true;\n\tif (x instanceof Date && y instanceof Date) {\n\t\treturn x.getTime() === y.getTime();\n\t}\n\tif (x instanceof RegExp && y instanceof RegExp) {\n\t\treturn x.toString() === y.toString();\n\t}\n\tif (x instanceof Value && y instanceof Value) {\n\t\treturn x.equals(y);\n\t}\n\tif (\n\t\ttypeof x !== \"object\" ||\n\t\tx === null ||\n\t\ttypeof y !== \"object\" ||\n\t\ty === null\n\t) {\n\t\treturn false;\n\t}\n\tconst keysX = Reflect.ownKeys(x as unknown as object) as (keyof typeof x)[];\n\tconst keysY = Reflect.ownKeys(y as unknown as object);\n\tif (keysX.length !== keysY.length) return false;\n\tfor (let i = 0; i < keysX.length; i++) {\n\t\tif (!Reflect.has(y as unknown as object, keysX[i])) return false;\n\t\tif (!equals(x[keysX[i]], y[keysX[i]])) return false;\n\t}\n\treturn true;\n}\n", "const MAX_i64 = 9223372036854775807n;\n/**\n * Escape a given string to be used as a valid SurrealQL ident.\n * @param str - The string to escape\n * @returns Optionally escaped string\n */\nexport function escapeIdent(str) {\n // String which looks like a number should always be escaped, to prevent it from being parsed as a number\n if (isOnlyNumbers(str)) {\n return `\u27E8${str}\u27E9`;\n }\n // Empty string should always be escaped\n if (str === \"\") {\n return \"\u27E8\u27E9\";\n }\n let code;\n let i;\n let len;\n for (i = 0, len = str.length; i < len; i++) {\n code = str.charCodeAt(i);\n if (!(code > 47 && code < 58) && // numeric (0-9)\n !(code > 64 && code < 91) && // upper al