@silvermine/quarry 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +90 -2
- package/index.d.ts +274 -0
- package/index.js +198 -0
- package/lambda/bootstrap +0 -0
- package/package.json +34 -5
- package/quarry-node.darwin-arm64.node +0 -0
- package/quarry-node.linux-x64-gnu.node +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jeremy Thomerson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# quarry-node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Node.js binding for Quarry: the npm package `@silvermine/quarry`. It gives a JavaScript
|
|
4
|
+
or TypeScript host the [producer API][producer-api] and the
|
|
5
|
+
engine's [entry points][hosts]. The package also contains
|
|
6
|
+
`lambda/bootstrap`, the [Lambda hosts][lambda-hosts] built for
|
|
7
|
+
Linux x64, so one package supplies a deployment's Node.js functions and its build and task
|
|
8
|
+
functions.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
cd crates/quarry-node
|
|
12
|
+
npm run build -- --linux
|
|
13
|
+
npm test
|
|
14
|
+
npm pack
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`npm run build` builds the binding for this machine. With `--linux`, it also builds the
|
|
18
|
+
binding and `lambda/bootstrap` for Linux x64 with `cargo zigbuild`. Install `zig` and
|
|
19
|
+
`cargo-zigbuild` first, and add the Rust target `x86_64-unknown-linux-gnu`. The package
|
|
20
|
+
contains one `.node` file for each platform, and `index.js` loads the file for the
|
|
21
|
+
platform it runs on: macOS on arm64, or Linux on x64 with glibc 2.28 or later. Git ignores
|
|
22
|
+
every file that the build makes, and `npm pack` stops when one of them is not there or is
|
|
23
|
+
older than the source.
|
|
24
|
+
|
|
25
|
+
## Use
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @silvermine/quarry
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
const { open } = require('@silvermine/quarry');
|
|
33
|
+
|
|
34
|
+
const quarry = await open({ durableBucket: 'my-durable', servingBucket: 'my-serving--use1-az4--x-s3' });
|
|
35
|
+
|
|
36
|
+
const transaction = await quarry.beginReplace('articles', { schema });
|
|
37
|
+
|
|
38
|
+
await transaction.upsert({
|
|
39
|
+
id: 'article-48112',
|
|
40
|
+
fields: { title: { exact: [ 'storm', 'closes', 'harbor', 'bridge' ] } },
|
|
41
|
+
stored: { title: 'Storm Closes Harbor Bridge' },
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const commitId = await transaction.commit();
|
|
45
|
+
|
|
46
|
+
await quarry.build('articles');
|
|
47
|
+
await quarry.status('articles', commitId); // { state: 'applied', epoch: 1 }
|
|
48
|
+
|
|
49
|
+
const response = await quarry.search({ index: 'articles', tokens: { exact: [ 'harbor' ] } });
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
[`index.d.ts`][types] declares every function, option, and error code.
|
|
53
|
+
|
|
54
|
+
* **One `Quarry` for each process.** `open` makes one engine, with its memory cache,
|
|
55
|
+
and one producer. Call it one time and keep the result. A `Quarry` reads the list of
|
|
56
|
+
epochs again only after the index's `refreshSeconds`, as each
|
|
57
|
+
[query worker][reader-protocol] does.
|
|
58
|
+
* **Stores.** `durableBucket` and `servingBucket` select the S3 adapters, with the
|
|
59
|
+
credentials and the region of the environment. `dataDirectory` selects two local
|
|
60
|
+
folders, as the [`quarry` command][command]
|
|
61
|
+
does. The binding has no [allow list][allow-list],
|
|
62
|
+
so the host selects the buckets.
|
|
63
|
+
* **JSON.** Documents, schemas, requests, responses, and records are the JSON that the
|
|
64
|
+
Quarry documents specify. The binding sends them to Rust as text and does not rename
|
|
65
|
+
a field.
|
|
66
|
+
* **Errors.** Each failure rejects with an `Error`. Its `message` is Quarry's text, and
|
|
67
|
+
its `code` is the kind of failure: `InvalidRequest` for a document or a request that
|
|
68
|
+
Quarry refuses, `NotFound`, `CorruptData`, `Other` for storage and all other
|
|
69
|
+
failures, and `InvalidArg` for an argument that the binding refuses.
|
|
70
|
+
* **Transactions.** An invalid document rejects, and the transaction stays open without
|
|
71
|
+
it. Await each call before the next call. After `commit`, the transaction is closed.
|
|
72
|
+
* **Build and garbage collection.** `build` and `gc` run in the process, on a thread of
|
|
73
|
+
their own, in the `single` [rebuild mode][rebuild-modes]. A
|
|
74
|
+
function host with a time limit uses the build host in `lambda/bootstrap` for
|
|
75
|
+
rebuilds and merges.
|
|
76
|
+
|
|
77
|
+
## On AWS Lambda
|
|
78
|
+
|
|
79
|
+
[Quarry on AWS Lambda][lambda] tells how a function sets up the memory
|
|
80
|
+
cache, the disk cache, and the local replica, how it keeps copies running after each
|
|
81
|
+
response, and which indexes it copies at start.
|
|
82
|
+
|
|
83
|
+
[producer-api]: https://github.com/silvermine/quarry/blob/master/docs/producers.md#the-producer-api
|
|
84
|
+
[hosts]: https://github.com/silvermine/quarry/blob/master/docs/run-modes.md#hosts
|
|
85
|
+
[lambda-hosts]: https://github.com/silvermine/quarry/blob/master/docs/run-modes.md#lambda-hosts
|
|
86
|
+
[types]: https://github.com/silvermine/quarry/blob/master/crates/quarry-node/index.d.ts
|
|
87
|
+
[reader-protocol]: https://github.com/silvermine/quarry/blob/master/docs/query.md#reader-protocol
|
|
88
|
+
[command]: https://github.com/silvermine/quarry/blob/master/docs/run-modes.md#the-command-line-tool
|
|
89
|
+
[allow-list]: https://github.com/silvermine/quarry/blob/master/docs/run-modes.md#the-safety-allow-list
|
|
90
|
+
[rebuild-modes]: https://github.com/silvermine/quarry/blob/master/docs/builder.md#rebuild-modes
|
|
91
|
+
[lambda]: https://github.com/silvermine/quarry/blob/master/docs/lambda.md
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// The producer API (docs/producers.md) and the engine's entry points (docs/run-modes.md)
|
|
2
|
+
// for a Node.js host. Requests, responses, documents, and records are the JSON those
|
|
3
|
+
// documents and docs/query.md specify.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What an error's `code` says failed. Every rejection is an `Error` with Quarry's text as
|
|
7
|
+
* its `message` and one of these as its `code`:
|
|
8
|
+
*
|
|
9
|
+
* * `InvalidArg`: the binding refused an argument, such as a version that is not a
|
|
10
|
+
* whole number, options that name no store, or a transaction used after its commit.
|
|
11
|
+
* * `InvalidRequest`: Quarry refused a document, schema, request, setting, or ID.
|
|
12
|
+
* * `NotFound`: an object Quarry expected does not exist, such as an index with no
|
|
13
|
+
* epoch yet.
|
|
14
|
+
* * `CorruptData`: an object's bytes do not decode as its format.
|
|
15
|
+
* * `Other`: anything else, such as a storage or network failure.
|
|
16
|
+
*
|
|
17
|
+
* An argument of the wrong JavaScript type rejects with a `TypeError` whose `code` is
|
|
18
|
+
* napi's, such as `StringExpected`.
|
|
19
|
+
*/
|
|
20
|
+
export type QuarryErrorCode = 'InvalidArg' | 'InvalidRequest' | 'NotFound' | 'CorruptData' | 'Other';
|
|
21
|
+
|
|
22
|
+
export interface QuarryError extends Error {
|
|
23
|
+
code: QuarryErrorCode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Give either `durableBucket` (S3) or `dataDirectory` (local), never both. */
|
|
27
|
+
export interface QuarryOptions {
|
|
28
|
+
|
|
29
|
+
/** S3: the durable (general purpose) bucket. */
|
|
30
|
+
durableBucket?: string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* S3: the serving (S3 Express One Zone directory) bucket. Omit to serve from the
|
|
34
|
+
* durable bucket.
|
|
35
|
+
*/
|
|
36
|
+
servingBucket?: string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Local stores instead of S3: a data directory, as the command-line tool's `--data`.
|
|
40
|
+
* For tests and local runs.
|
|
41
|
+
*/
|
|
42
|
+
dataDirectory?: string;
|
|
43
|
+
|
|
44
|
+
/** The memory cache's size in MiB (the Lambda hosts' `CACHE_MB`). */
|
|
45
|
+
cacheMb?: number;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Disk cache for the serving tier's small reads: a folder, for example `/tmp`, and
|
|
49
|
+
* its size in MiB (the Lambda query host's `DISK_CACHE_MB`). Give both or neither.
|
|
50
|
+
* The cache keeps its files in the subfolder `quarry-disk-cache`. `open` empties that
|
|
51
|
+
* subfolder and changes nothing else in the folder.
|
|
52
|
+
*/
|
|
53
|
+
diskCacheDirectory?: string;
|
|
54
|
+
diskCacheMb?: number;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Local replica of the serving tier's vector objects: a folder, for example `/tmp`,
|
|
58
|
+
* and its size in MiB (the Lambda query host's `REPLICA_MB`). Give both or neither.
|
|
59
|
+
* Vector reads are never cached in memory, so without a replica every vector query
|
|
60
|
+
* reads its matrices from storage. The replica keeps its copies in the subfolder
|
|
61
|
+
* `quarry-replica`. `open` empties that subfolder and changes nothing else in the
|
|
62
|
+
* folder.
|
|
63
|
+
*/
|
|
64
|
+
replicaDirectory?: string;
|
|
65
|
+
replicaMb?: number;
|
|
66
|
+
|
|
67
|
+
/** Ranged reads in flight while copying to the replica; 4 when omitted. */
|
|
68
|
+
replicaStreams?: number;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* For a process that serves many indexes: count the queries of each index, keep only
|
|
72
|
+
* the objects of this many most queried indexes in the memory cache, and copy an index
|
|
73
|
+
* to the replica only when it is queried (the Lambda query host's `HOT_INDEXES`).
|
|
74
|
+
* Without it, the replica copies the vector objects of each index on their first read.
|
|
75
|
+
*/
|
|
76
|
+
hotIndexes?: number;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* With `hotIndexes`, the query count an index needs before the replica copies its
|
|
80
|
+
* objects; 4 when omitted.
|
|
81
|
+
*/
|
|
82
|
+
replicaMinQueries?: number;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Indexes to copy to the replica as soon as `open` resolves, most wanted first, for the
|
|
86
|
+
* indexes that the process expects to be busy. `Quarry.warm` copies more later.
|
|
87
|
+
*/
|
|
88
|
+
warmIndexes?: string[];
|
|
89
|
+
|
|
90
|
+
/** Which objects of a warm index to copy; `all` when omitted. */
|
|
91
|
+
warmObjects?: WarmObjects;
|
|
92
|
+
|
|
93
|
+
/** Connections to the serving bucket (the Lambda hosts' `MAX_SOCKETS`); 64 when omitted. */
|
|
94
|
+
maxSockets?: number;
|
|
95
|
+
|
|
96
|
+
/** Compress the parts a transaction writes with gzip, for a slow link to storage. */
|
|
97
|
+
compressParts?: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* `all`: every object a query may read. `vectors`: the small objects that every query
|
|
102
|
+
* reads and the vector objects, which are the only reads that the memory cache never
|
|
103
|
+
* keeps. `none`: nothing, which makes an index warm no more and removes its copies.
|
|
104
|
+
*/
|
|
105
|
+
export type WarmObjects = 'all' | 'vectors' | 'none';
|
|
106
|
+
|
|
107
|
+
export interface ReplicaStats {
|
|
108
|
+
readyObjects: number;
|
|
109
|
+
readyBytes: number;
|
|
110
|
+
pendingObjects: number;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface QuarryStats {
|
|
114
|
+
|
|
115
|
+
/** What the local replica holds, or `null` without one. */
|
|
116
|
+
replica: ReplicaStats | null;
|
|
117
|
+
isAfterResponseStarted: boolean;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface ReplaceOptions {
|
|
121
|
+
|
|
122
|
+
/** The index's new schema (docs/schema.md). Required when the index does not exist yet. */
|
|
123
|
+
schema?: object;
|
|
124
|
+
|
|
125
|
+
/** The replace version; the begin time when omitted. */
|
|
126
|
+
replaceVersion?: number;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface GetOptions {
|
|
130
|
+
|
|
131
|
+
/** The epoch to read; the newest when omitted. */
|
|
132
|
+
epoch?: number;
|
|
133
|
+
|
|
134
|
+
/** The searcher's claims (docs/query.md). */
|
|
135
|
+
claims?: object[];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type TransactionStatus =
|
|
139
|
+
{ state: 'pending' } |
|
|
140
|
+
{ state: 'applied'; epoch: number } |
|
|
141
|
+
{ state: 'rejected'; report: QuarantineReport };
|
|
142
|
+
|
|
143
|
+
/** Why a builder rejected a whole transaction. */
|
|
144
|
+
export interface QuarantineReport {
|
|
145
|
+
commit: string;
|
|
146
|
+
file: string;
|
|
147
|
+
reason: string;
|
|
148
|
+
line: number | null;
|
|
149
|
+
epoch: number;
|
|
150
|
+
builder: string;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* On AWS Lambda, keeps each invocation open after its response while copies to a local
|
|
155
|
+
* replica remain, for up to `budgetMs` (5,000 when omitted), so that a replica fills
|
|
156
|
+
* between requests (docs/lambda.md). Call it while the module of the handler loads: Lambda
|
|
157
|
+
* refuses it later. It blocks until Lambda answers. Returns false outside Lambda. The
|
|
158
|
+
* handler must then record each response, which `wrapHandler` does.
|
|
159
|
+
*/
|
|
160
|
+
export function startAfterResponse(options?: { budgetMs?: number }): boolean;
|
|
161
|
+
|
|
162
|
+
/** Tells the binding that the handler has its response. `wrapHandler` calls it. */
|
|
163
|
+
export function recordResponse(): void;
|
|
164
|
+
|
|
165
|
+
/** A handler that records its response when it resolves or rejects. */
|
|
166
|
+
export function wrapHandler<T extends (...args: any[]) => Promise<unknown>>(handler: T): T;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Opens the stores and makes the engine and the producer that the returned `Quarry`
|
|
170
|
+
* shares for its lifetime. Call it once per process and keep the result.
|
|
171
|
+
*/
|
|
172
|
+
export function open(options: QuarryOptions): Promise<Quarry>;
|
|
173
|
+
|
|
174
|
+
/** One engine, with its caches, and one producer. Made by `open`. */
|
|
175
|
+
export class Quarry {
|
|
176
|
+
|
|
177
|
+
private constructor();
|
|
178
|
+
|
|
179
|
+
/** Opens a transaction against the index's live schema. The index must exist. */
|
|
180
|
+
begin(indexId: string): Promise<Transaction>;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Opens a replace: a transaction that replaces every document of the index and, when
|
|
184
|
+
* a schema is given, the schema. A replace with a schema creates an index.
|
|
185
|
+
*/
|
|
186
|
+
beginReplace(indexId: string, options?: ReplaceOptions): Promise<Transaction>;
|
|
187
|
+
|
|
188
|
+
/** What became of a committed transaction. */
|
|
189
|
+
status(indexId: string, commitId: string): Promise<TransactionStatus>;
|
|
190
|
+
|
|
191
|
+
/** Commits a settings change alone. Resolves with the commit ID. */
|
|
192
|
+
setSettings(indexId: string, settings: Record<string, number>): Promise<string>;
|
|
193
|
+
|
|
194
|
+
/** Runs one query. The request and the response are the JSON of docs/query.md. */
|
|
195
|
+
search(request: object): Promise<object>;
|
|
196
|
+
|
|
197
|
+
/** Lists the rewrites a search request would make, without searching. */
|
|
198
|
+
suggest(request: object): Promise<object>;
|
|
199
|
+
|
|
200
|
+
/** Reads documents by ID: `{ index, epoch, documents, reads }`. */
|
|
201
|
+
get(indexId: string, ids: string[], options?: GetOptions): Promise<object>;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Queues copies of the objects of these indexes to the local replica, most wanted
|
|
205
|
+
* index first, and pins them. The copies run in the background. Resolves with the
|
|
206
|
+
* count of objects; 0 without a replica. `options.objects` is `warmObjects` of `open`
|
|
207
|
+
* when omitted, and replaces what an earlier call gave for the same index.
|
|
208
|
+
*
|
|
209
|
+
* A warm index stays warm: when a search reads a new epoch of it, the objects of that
|
|
210
|
+
* epoch are copied, and the copies of objects that the epoch no longer has are
|
|
211
|
+
* removed. `objects: 'none'` ends that, for example for an index that another
|
|
212
|
+
* replaced.
|
|
213
|
+
*/
|
|
214
|
+
warm(indexIds: string[], options?: { objects?: WarmObjects }): Promise<{ objects: number }>;
|
|
215
|
+
|
|
216
|
+
/** What the local replica holds, and whether `startAfterResponse` registered. */
|
|
217
|
+
stats(): Promise<QuarryStats>;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Runs the builder loop for one index until nothing is due. Resolves with one build
|
|
221
|
+
* record (docs/observability.md) per epoch it committed or attempted.
|
|
222
|
+
*/
|
|
223
|
+
build(indexId: string): Promise<object[]>;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Runs garbage collection for one index, serving-tier sweep included. Resolves with
|
|
227
|
+
* the collection record (docs/observability.md).
|
|
228
|
+
*/
|
|
229
|
+
gc(indexId: string): Promise<object>;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* A group of changes to one index that a builder applies all at once, or not at all.
|
|
234
|
+
* Every version is a whole number from 0 through 2^53 - 1; the transaction's begin time
|
|
235
|
+
* in milliseconds when omitted. Await each call before the next.
|
|
236
|
+
*/
|
|
237
|
+
export class Transaction {
|
|
238
|
+
|
|
239
|
+
private constructor();
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Adds or replaces a whole document: an upsert operation (docs/producers.md) without
|
|
243
|
+
* its `op` and `version`. Rejects with `InvalidRequest` for an invalid document; the
|
|
244
|
+
* transaction stays usable and the document is not added.
|
|
245
|
+
*/
|
|
246
|
+
upsert(document: object, version?: number): Promise<void>;
|
|
247
|
+
|
|
248
|
+
/** Deletes one document by ID. */
|
|
249
|
+
delete(id: string, version?: number): Promise<void>;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Deletes every document that matches all the filters and whose version is less than
|
|
253
|
+
* `version`.
|
|
254
|
+
*/
|
|
255
|
+
deleteWhere(filters: object[], version?: number): Promise<void>;
|
|
256
|
+
|
|
257
|
+
/** Records the analysis fingerprint of each field variant's tokens. */
|
|
258
|
+
setAnalysis(fingerprints: Record<string, string>): Promise<void>;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Changes index settings (docs/builder.md) as of the epoch that applies the transaction.
|
|
262
|
+
* A replace that creates an index sets them for its first epoch.
|
|
263
|
+
*/
|
|
264
|
+
setSettings(settings: Record<string, number>): Promise<void>;
|
|
265
|
+
|
|
266
|
+
/** Waits for every part written so far. Optional: `commit` waits too. */
|
|
267
|
+
flush(): Promise<void>;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Writes the commit record and resolves with the commit ID, a KSUID. The transaction
|
|
271
|
+
* cannot be used afterwards, whether or not the commit succeeded.
|
|
272
|
+
*/
|
|
273
|
+
commit(): Promise<string>;
|
|
274
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
// The binaries this package ships, by Node's platform and architecture. Lambda runs
|
|
6
|
+
// Linux on glibc, which is what the linux build links against.
|
|
7
|
+
const BINARIES = {
|
|
8
|
+
'darwin-arm64': 'quarry-node.darwin-arm64.node',
|
|
9
|
+
'linux-x64': 'quarry-node.linux-x64-gnu.node',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const binary = BINARIES[`${process.platform}-${process.arch}`];
|
|
13
|
+
|
|
14
|
+
if (!binary) {
|
|
15
|
+
throw new Error(`@silvermine/quarry has no binary for ${process.platform}-${process.arch}`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const native = require(path.join(__dirname, binary));
|
|
19
|
+
|
|
20
|
+
function invalidArg(message) {
|
|
21
|
+
const error = new TypeError(message);
|
|
22
|
+
|
|
23
|
+
error.code = 'InvalidArg';
|
|
24
|
+
return error;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Requests, documents, and options cross into Rust as JSON text.
|
|
28
|
+
function toJson(value, name) {
|
|
29
|
+
const json = JSON.stringify(value);
|
|
30
|
+
|
|
31
|
+
if (typeof json !== 'string') {
|
|
32
|
+
throw invalidArg(`${name} must be a value that JSON can hold; it is ${String(value)}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return json;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toOptionalJson(value, name) {
|
|
39
|
+
return value === undefined ? undefined : toJson(value, name);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// A napi `async` method can reject only with napi's own status, so every native method
|
|
43
|
+
// resolves with either its result as JSON text or Quarry's error as a value, and this
|
|
44
|
+
// rejects with an `Error` whose `code` names the kind of failure.
|
|
45
|
+
async function settle(pending) {
|
|
46
|
+
const { value, error } = await pending;
|
|
47
|
+
|
|
48
|
+
if (error) {
|
|
49
|
+
const thrown = new Error(error.message);
|
|
50
|
+
|
|
51
|
+
thrown.code = error.code;
|
|
52
|
+
throw thrown;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return typeof value === 'string' ? JSON.parse(value) : undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
class Transaction {
|
|
59
|
+
|
|
60
|
+
#native;
|
|
61
|
+
|
|
62
|
+
constructor(nativeTransaction) {
|
|
63
|
+
this.#native = nativeTransaction;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async upsert(document, version) {
|
|
67
|
+
await settle(this.#native.upsert(toJson(document, 'document'), version));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async delete(id, version) {
|
|
71
|
+
await settle(this.#native.delete(id, version));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async deleteWhere(filters, version) {
|
|
75
|
+
await settle(this.#native.deleteWhere(toJson(filters, 'filters'), version));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async setAnalysis(fingerprints) {
|
|
79
|
+
await settle(this.#native.setAnalysis(toJson(fingerprints, 'fingerprints')));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async setSettings(settings) {
|
|
83
|
+
await settle(this.#native.setSettings(toJson(settings, 'settings')));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async flush() {
|
|
87
|
+
await settle(this.#native.flush());
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async commit() {
|
|
91
|
+
return settle(this.#native.commit());
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
class Quarry {
|
|
97
|
+
|
|
98
|
+
#native;
|
|
99
|
+
|
|
100
|
+
constructor(nativeQuarry) {
|
|
101
|
+
this.#native = nativeQuarry;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async begin(indexId) {
|
|
105
|
+
const transaction = this.#native.transaction();
|
|
106
|
+
|
|
107
|
+
await settle(transaction.begin(indexId));
|
|
108
|
+
return new Transaction(transaction);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async beginReplace(indexId, options) {
|
|
112
|
+
const transaction = this.#native.transaction();
|
|
113
|
+
|
|
114
|
+
await settle(transaction.beginReplace(indexId, toOptionalJson(options, 'options')));
|
|
115
|
+
return new Transaction(transaction);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async status(indexId, commitId) {
|
|
119
|
+
return settle(this.#native.status(indexId, commitId));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async setSettings(indexId, settings) {
|
|
123
|
+
return settle(this.#native.setSettings(indexId, toJson(settings, 'settings')));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async search(request) {
|
|
127
|
+
return settle(this.#native.search(toJson(request, 'request')));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async suggest(request) {
|
|
131
|
+
return settle(this.#native.suggest(toJson(request, 'request')));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async get(indexId, ids, options) {
|
|
135
|
+
return settle(this.#native.get(indexId, ids, toOptionalJson(options, 'options')));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async warm(indexIds, options) {
|
|
139
|
+
return settle(this.#native.warm(indexIds, toOptionalJson(options, 'options')));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async stats() {
|
|
143
|
+
return settle(this.#native.stats());
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async build(indexId) {
|
|
147
|
+
return settle(this.#native.build(indexId));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async gc(indexId) {
|
|
151
|
+
return settle(this.#native.gc(indexId));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function open(options) {
|
|
157
|
+
const quarry = new native.Quarry();
|
|
158
|
+
|
|
159
|
+
await settle(quarry.open(toJson(options, 'options')));
|
|
160
|
+
return new Quarry(quarry);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Lambda takes the registration of an extension only while the function starts, and a
|
|
164
|
+
// module that loads cannot wait for a promise, so this call blocks until Lambda answers.
|
|
165
|
+
function startAfterResponse(options) {
|
|
166
|
+
const budgetMs = options === undefined ? 5000 : options.budgetMs;
|
|
167
|
+
|
|
168
|
+
if (typeof budgetMs !== 'number') {
|
|
169
|
+
throw invalidArg('budgetMs must be a number of milliseconds');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return native.startAfterResponse(budgetMs);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function recordResponse() {
|
|
176
|
+
native.recordResponse();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// The extension waits for each handler call to end before it lets background work go on,
|
|
180
|
+
// so a handler that never records its response holds its invocation open.
|
|
181
|
+
function wrapHandler(handler) {
|
|
182
|
+
return async function(...args) {
|
|
183
|
+
try {
|
|
184
|
+
return await handler.apply(this, args);
|
|
185
|
+
} finally {
|
|
186
|
+
recordResponse();
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
module.exports = {
|
|
192
|
+
open,
|
|
193
|
+
startAfterResponse,
|
|
194
|
+
recordResponse,
|
|
195
|
+
wrapHandler,
|
|
196
|
+
Quarry,
|
|
197
|
+
Transaction,
|
|
198
|
+
};
|
package/lambda/bootstrap
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@silvermine/quarry",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node.js binding for Quarry, a serverless hybrid search engine that keeps its index in object storage",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"index.d.ts",
|
|
10
|
+
"*.node",
|
|
11
|
+
"lambda/bootstrap"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "node scripts/build.mjs",
|
|
15
|
+
"prepack": "node scripts/check-package.mjs",
|
|
16
|
+
"test": "node --test test/*.test.js"
|
|
17
|
+
},
|
|
18
|
+
"author": "Jeremy Thomerson",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/silvermine/quarry.git",
|
|
23
|
+
"directory": "crates/quarry-node"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/silvermine/quarry/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/silvermine/quarry#readme",
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=20"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
Binary file
|
|
Binary file
|