@olivia-db/client 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -2
- package/dist/index.d.ts +200 -0
- package/dist/index.js +239 -0
- package/dist/native.d.ts +216 -0
- package/dist/native.js +1200 -0
- package/package.json +36 -4
- package/src/index.ts +361 -0
- package/src/native.ts +1254 -0
package/dist/native.d.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import * as tls from "node:tls";
|
|
2
|
+
import { type Json } from "./index.ts";
|
|
3
|
+
/** A frame the server sent that this client cannot read (a protocol error). */
|
|
4
|
+
export declare class NativeProtocolError extends Error {
|
|
5
|
+
}
|
|
6
|
+
/** A logical type of a field or key part (`schema.md` 4.1). */
|
|
7
|
+
export type Type = {
|
|
8
|
+
kind: "string" | "date" | "timestamp" | "bool" | "float64" | "text" | "ref";
|
|
9
|
+
} | {
|
|
10
|
+
kind: "bytes";
|
|
11
|
+
length: number;
|
|
12
|
+
} | {
|
|
13
|
+
kind: "int";
|
|
14
|
+
signed: boolean;
|
|
15
|
+
} | {
|
|
16
|
+
kind: "decimal";
|
|
17
|
+
scale: number;
|
|
18
|
+
} | {
|
|
19
|
+
kind: "enum";
|
|
20
|
+
members: Array<[string, string]>;
|
|
21
|
+
};
|
|
22
|
+
export declare function parseType(t: string): Type;
|
|
23
|
+
/** A field of a descriptor: where it lies in a node's data and whether the caller sees it. */
|
|
24
|
+
export interface Field {
|
|
25
|
+
number: number;
|
|
26
|
+
name: string;
|
|
27
|
+
type: Type;
|
|
28
|
+
/** In the tail (`offset` is then the index among the tail fields). */
|
|
29
|
+
tail: boolean;
|
|
30
|
+
offset: number;
|
|
31
|
+
length: number;
|
|
32
|
+
scale: number;
|
|
33
|
+
/** 0 hidden, 1 shown, 2 masked (spec section 5). */
|
|
34
|
+
shown: number;
|
|
35
|
+
/** A variant case's discriminator and values, when the field belongs to one. */
|
|
36
|
+
caseOf?: {
|
|
37
|
+
disc: number;
|
|
38
|
+
values: string[];
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export interface KeyPart {
|
|
42
|
+
name: string;
|
|
43
|
+
type: Type;
|
|
44
|
+
descending: boolean;
|
|
45
|
+
}
|
|
46
|
+
/** A type descriptor (spec section 5). */
|
|
47
|
+
export declare class Desc {
|
|
48
|
+
readonly strs: Array<[number, number]>;
|
|
49
|
+
readonly layout: string;
|
|
50
|
+
readonly typePath: string;
|
|
51
|
+
readonly bucket: number;
|
|
52
|
+
readonly ordinal: boolean;
|
|
53
|
+
readonly key: KeyPart[];
|
|
54
|
+
readonly fixed: number;
|
|
55
|
+
readonly fields: Field[];
|
|
56
|
+
readonly defaults: Array<[Field, Uint8Array]>;
|
|
57
|
+
readonly children: Map<number, string>;
|
|
58
|
+
constructor(layout: string, typePath: string, bucket: number, ordinal: boolean, key: KeyPart[], fixed: number, fields: Field[], defaults: Array<[Field, Uint8Array]>, children: Map<number, string>);
|
|
59
|
+
/** The node type's name (the last segment of its type path). */
|
|
60
|
+
get typeName(): string;
|
|
61
|
+
/** The aggregate's name. */
|
|
62
|
+
get aggregate(): string;
|
|
63
|
+
/** A field by name, to read it from nodes of this descriptor. */
|
|
64
|
+
field(name: string): Field | undefined;
|
|
65
|
+
}
|
|
66
|
+
type Descs = ReadonlyMap<number, Desc>;
|
|
67
|
+
/** A decimal's text with `scale` digits after the point. */
|
|
68
|
+
export declare function decimalText(unscaled: bigint, scale: number): string;
|
|
69
|
+
/** `YYYY-MM-DD` of days since 1970-01-01. */
|
|
70
|
+
export declare function dateText(days: number): string;
|
|
71
|
+
/** `YYYY-MM-DDTHH:MM:SS.ffffffZ` of microseconds since the epoch. */
|
|
72
|
+
export declare function timestampText(us: bigint): string;
|
|
73
|
+
/** A key part's value. */
|
|
74
|
+
export type KeyValue = bigint | number | boolean | Uint8Array | string;
|
|
75
|
+
/** One node of a result: its bytes are views into the frame it came in. */
|
|
76
|
+
export declare class Node {
|
|
77
|
+
readonly desc: Desc;
|
|
78
|
+
readonly depth: number;
|
|
79
|
+
/** The node's own key component, as stored (`path-keys.md`). */
|
|
80
|
+
readonly key: Uint8Array;
|
|
81
|
+
/** The value as received: layout bytes, with the fixed strings trimmed (`trim`). */
|
|
82
|
+
private readonly data;
|
|
83
|
+
/** Per fixed string: layout offset, n, offset in data, trimmed length. */
|
|
84
|
+
private readonly trim;
|
|
85
|
+
private readonly masks;
|
|
86
|
+
/** The granted child listings (a tree read), by type name. */
|
|
87
|
+
readonly lists: string[] | undefined;
|
|
88
|
+
/** LOOKUP: the indexed node's path. */
|
|
89
|
+
readonly hit: string | undefined;
|
|
90
|
+
/** SEARCH: the parent path and the score. */
|
|
91
|
+
readonly parent: string | undefined;
|
|
92
|
+
readonly score: number | undefined;
|
|
93
|
+
constructor(desc: Desc, depth: number, key: Uint8Array, data: Uint8Array, trim: Array<[number, number, number, number]>, masks: Array<[number, string]>, lists: string[] | undefined, hit: string | undefined, parent: string | undefined, score: number | undefined);
|
|
94
|
+
private span;
|
|
95
|
+
private wireAt;
|
|
96
|
+
private tails;
|
|
97
|
+
private present;
|
|
98
|
+
private bytesOf;
|
|
99
|
+
/** A string, text, enum member or reference field (fixed strings without their padding);
|
|
100
|
+
* a masked field's masked text. Undefined: hidden, or not in this node's variant case. */
|
|
101
|
+
str(f: Field): string | undefined;
|
|
102
|
+
/** An integer field. */
|
|
103
|
+
int(f: Field): bigint | undefined;
|
|
104
|
+
/** A decimal field: the unscaled value and the scale. */
|
|
105
|
+
decimal(f: Field): {
|
|
106
|
+
unscaled: bigint;
|
|
107
|
+
scale: number;
|
|
108
|
+
} | undefined;
|
|
109
|
+
/** A timestamp field: microseconds since 1970-01-01T00:00:00Z. */
|
|
110
|
+
timestamp(f: Field): bigint | undefined;
|
|
111
|
+
/** A date field: days since 1970-01-01. */
|
|
112
|
+
date(f: Field): number | undefined;
|
|
113
|
+
/** A field's value as the HTTP API's JSON shows it. */
|
|
114
|
+
json(f: Field): unknown;
|
|
115
|
+
/** The node's key parts, decoded from its component (`path-keys.md`, format 2). */
|
|
116
|
+
keyParts(): KeyValue[];
|
|
117
|
+
/** The ordinal of a twin, as hexadecimal (`~` in paths). */
|
|
118
|
+
ordinal(): string | undefined;
|
|
119
|
+
/** The node's level as a path segment (`key,key~ordinal`, percent-encoded). */
|
|
120
|
+
segment(): string;
|
|
121
|
+
private keyJson;
|
|
122
|
+
/** The node as the HTTP API's JSON shows it, under `parent` (a path, "" for a root). */
|
|
123
|
+
toJson(parent: string, defaults: boolean): Json;
|
|
124
|
+
}
|
|
125
|
+
/** An entry of a LOOKUP answer: a node, or a record whose root the profile does not show. */
|
|
126
|
+
export type Entry = {
|
|
127
|
+
node: Node;
|
|
128
|
+
} | {
|
|
129
|
+
path: string;
|
|
130
|
+
hitPath: string;
|
|
131
|
+
};
|
|
132
|
+
/** Where a value's trimmed fixed strings are (spec section 6.3). */
|
|
133
|
+
export declare function trimmed(desc: Desc, data: Uint8Array): Array<[number, number, number, number]>;
|
|
134
|
+
/** A read's answer: its frames as received, nodes read from them on demand. */
|
|
135
|
+
export declare class Rows {
|
|
136
|
+
frames: Array<[Uint8Array, Descs]>;
|
|
137
|
+
version: bigint | undefined;
|
|
138
|
+
/** A listing page's `after` for the next page. */
|
|
139
|
+
next: string | undefined;
|
|
140
|
+
/** The path of the depth-0 nodes' parent. */
|
|
141
|
+
base: string;
|
|
142
|
+
appliedThrough: {
|
|
143
|
+
through: bigint;
|
|
144
|
+
caughtUp: boolean;
|
|
145
|
+
} | undefined;
|
|
146
|
+
tokens: bigint | undefined;
|
|
147
|
+
/** Every entry (node, or LOOKUP's path-only entry) in order. */
|
|
148
|
+
entries(): Generator<Entry>;
|
|
149
|
+
/** The nodes, in order (pre-order for a tree). */
|
|
150
|
+
nodes(): Node[];
|
|
151
|
+
/** The answer as the HTTP API's JSON for a GET of a node (`listing` false) or a listing. */
|
|
152
|
+
toJson(listing: boolean): Json;
|
|
153
|
+
}
|
|
154
|
+
/** TLS settings of `NativeClient.connectTls`, besides the options of `node:tls`. */
|
|
155
|
+
export interface TlsOptions extends tls.ConnectionOptions {
|
|
156
|
+
/** Trust the certificates of this PEM file instead of the platform's roots. */
|
|
157
|
+
caFile?: string;
|
|
158
|
+
/** The name to verify in the server's certificate, when it is not the host connected to. */
|
|
159
|
+
serverName?: string;
|
|
160
|
+
}
|
|
161
|
+
/** A native-protocol connection. Concurrent requests share it, each on its own stream. */
|
|
162
|
+
export declare class NativeClient {
|
|
163
|
+
private streams;
|
|
164
|
+
private nextStream;
|
|
165
|
+
private descs;
|
|
166
|
+
private buf;
|
|
167
|
+
private closedWhy;
|
|
168
|
+
private handshake;
|
|
169
|
+
private readonly sock;
|
|
170
|
+
private constructor();
|
|
171
|
+
/** Connects over plain TCP (the server serves plain text on loopback only, unless configured
|
|
172
|
+
* otherwise) and authenticates with `scheme` (`token` or `bearer`) and the credential the
|
|
173
|
+
* HTTP Authorization header would carry. */
|
|
174
|
+
static connectTcp(host: string, port: number, scheme: string, credential: string): Promise<NativeClient>;
|
|
175
|
+
/** Connects over TLS (spec `native-protocol.md` section 1): TLS from the first byte, ALPN
|
|
176
|
+
* `oliviadb/1` offered and required, the certificate chain verified against the platform's
|
|
177
|
+
* roots (`tls.getCACertificates("system")`, Node 22.15 and later; earlier Node versions use
|
|
178
|
+
* Node's own roots) or `options.caFile` / `options.ca`, and the server name
|
|
179
|
+
* (`options.serverName`, else `host`) verified against the certificate. Other `options` are
|
|
180
|
+
* passed to `node:tls`. */
|
|
181
|
+
static connectTls(host: string, port: number, scheme: string, credential: string, options?: TlsOptions): Promise<NativeClient>;
|
|
182
|
+
/** Connects over a Unix socket. */
|
|
183
|
+
static connectUnix(path: string, scheme: string, credential: string): Promise<NativeClient>;
|
|
184
|
+
private static start;
|
|
185
|
+
private onData;
|
|
186
|
+
/** Frames to their streams, descriptors kept in arrival order. */
|
|
187
|
+
private dispatch;
|
|
188
|
+
private onClose;
|
|
189
|
+
private send;
|
|
190
|
+
private rows;
|
|
191
|
+
/** `GET /v1/aggregates/{path}` (a node, `tree=true` with `each`, or a listing page). */
|
|
192
|
+
get(path: string, params?: Record<string, string>): Promise<Rows>;
|
|
193
|
+
/** `GET /v1/lookup/{index}`. */
|
|
194
|
+
lookup(index: string, params?: Record<string, string>): Promise<Rows>;
|
|
195
|
+
/** `POST /v1/search/{aggregate}` with the HTTP body. */
|
|
196
|
+
search(aggregate: string, body: Json): Promise<Rows>;
|
|
197
|
+
private json;
|
|
198
|
+
/** `POST /v1/txn`: the HTTP route's status and answer. */
|
|
199
|
+
txn(body: Json): Promise<[number, Json]>;
|
|
200
|
+
/** `POST`, `PATCH` or `DELETE /v1/aggregates/{path}`: the status and the JSON answer. */
|
|
201
|
+
write(method: string, path: string, body: Json): Promise<[number, Json]>;
|
|
202
|
+
/** The change stream of native aggregates under `paths`, from a position (`start`, `now` or
|
|
203
|
+
* a record's): its records as they come, until the iteration stops (the stream is then
|
|
204
|
+
* cancelled). */
|
|
205
|
+
changes(paths: string[], from: string): AsyncGenerator<Change>;
|
|
206
|
+
close(): void;
|
|
207
|
+
}
|
|
208
|
+
/** One change record. */
|
|
209
|
+
export declare class Change {
|
|
210
|
+
private readonly body;
|
|
211
|
+
private readonly descs;
|
|
212
|
+
constructor(body: Uint8Array, descs: Descs);
|
|
213
|
+
/** The record as the HTTP change stream's JSON object. */
|
|
214
|
+
toJson(): Json;
|
|
215
|
+
}
|
|
216
|
+
export {};
|