UNPKG

kantara-db

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