lucivy-wasm 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 +22 -0
- package/README.md +117 -0
- package/lucivy_wasm.d.ts +121 -0
- package/lucivy_wasm.js +685 -0
- package/lucivy_wasm_bg.wasm +0 -0
- package/lucivy_wasm_bg.wasm.d.ts +29 -0
- package/package.json +36 -0
- package/snippets/wasm-bindgen-rayon-38edf6e439f6d70d/src/workerHelpers.js +107 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Luciform Research
|
|
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.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# @lucivy/wasm
|
|
2
|
+
|
|
3
|
+
Fast BM25 full-text search for browsers — WASM build of Lucivy with OPFS persistence. Runs in a Web Worker.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lucivy/wasm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import init, { Index } from '@lucivy/wasm';
|
|
15
|
+
|
|
16
|
+
await init();
|
|
17
|
+
|
|
18
|
+
const index = new Index('/my_index', JSON.stringify([
|
|
19
|
+
{ name: 'title', type: 'text' },
|
|
20
|
+
{ name: 'body', type: 'text' },
|
|
21
|
+
]));
|
|
22
|
+
|
|
23
|
+
index.add(1, JSON.stringify({ title: 'Rust Programming', body: 'Systems programming with memory safety' }));
|
|
24
|
+
index.add(2, JSON.stringify({ title: 'Python Guide', body: 'Data science and web development' }));
|
|
25
|
+
index.commit();
|
|
26
|
+
|
|
27
|
+
const results = JSON.parse(index.search(JSON.stringify('programming'), 10, true));
|
|
28
|
+
for (const r of results) {
|
|
29
|
+
console.log(r.doc_id, r.score, r.highlights);
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## API
|
|
34
|
+
|
|
35
|
+
### Create / open
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
// Create a new index
|
|
39
|
+
const index = new Index('/my_index', JSON.stringify([
|
|
40
|
+
{ name: 'title', type: 'text' },
|
|
41
|
+
{ name: 'body', type: 'text' },
|
|
42
|
+
{ name: 'tag', type: 'keyword' },
|
|
43
|
+
]));
|
|
44
|
+
|
|
45
|
+
// Open from OPFS files (Map of filename → Uint8Array)
|
|
46
|
+
const index2 = Index.open('/my_index', filesMap);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Add / update / delete
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
index.add(1, JSON.stringify({ title: 'Hello', body: 'World' }));
|
|
53
|
+
index.addMany(JSON.stringify([
|
|
54
|
+
{ docId: 2, title: 'Foo', body: 'Bar' },
|
|
55
|
+
{ docId: 3, title: 'Baz', body: 'Qux' },
|
|
56
|
+
]));
|
|
57
|
+
index.update(1, JSON.stringify({ title: 'Updated', body: 'Content' }));
|
|
58
|
+
index.remove(2);
|
|
59
|
+
index.commit();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Search
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
// String query (contains_split across all text fields)
|
|
66
|
+
const results = JSON.parse(index.search(JSON.stringify('rust programming'), 10));
|
|
67
|
+
|
|
68
|
+
// Structured query
|
|
69
|
+
const results2 = JSON.parse(index.search(JSON.stringify({
|
|
70
|
+
type: 'contains', field: 'body', value: 'program'
|
|
71
|
+
}), 10, true));
|
|
72
|
+
|
|
73
|
+
// Pre-filtered by doc IDs
|
|
74
|
+
const results3 = JSON.parse(index.searchFiltered(
|
|
75
|
+
JSON.stringify('programming'), 10, new Uint32Array([1, 3]), true
|
|
76
|
+
));
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### OPFS persistence
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
// Get dirty files (modified since last export) for incremental OPFS sync
|
|
83
|
+
const dirty = index.exportDirtyFiles(); // Map<string, Uint8Array>
|
|
84
|
+
|
|
85
|
+
// Get all files
|
|
86
|
+
const all = index.exportAllFiles(); // Map<string, Uint8Array>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Snapshots (export / import)
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
// Export to Uint8Array (.luce format)
|
|
93
|
+
const snapshot = index.exportSnapshot();
|
|
94
|
+
|
|
95
|
+
// Import from Uint8Array
|
|
96
|
+
const restored = Index.importSnapshot(snapshot, '/restored_index');
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Properties
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
index.numDocs // number of documents
|
|
103
|
+
index.path // index path
|
|
104
|
+
index.schemaJson // JSON string of field definitions
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## OPFS + Web Worker pattern
|
|
108
|
+
|
|
109
|
+
This package is designed to run in a Web Worker with OPFS for persistence:
|
|
110
|
+
|
|
111
|
+
1. On startup: read files from OPFS → `Index.open(path, filesMap)`
|
|
112
|
+
2. After mutations + commit: `index.exportDirtyFiles()` → write to OPFS
|
|
113
|
+
3. For backup/transfer: `index.exportSnapshot()` → portable `.luce` blob
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
MIT
|
package/lucivy_wasm.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export class Index {
|
|
5
|
+
free(): void;
|
|
6
|
+
[Symbol.dispose](): void;
|
|
7
|
+
add(doc_id: number, fields_json: string): void;
|
|
8
|
+
addMany(docs_json: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* Commit changes to the in-memory directory.
|
|
11
|
+
* After this, call `exportDirtyFiles()` to get files to persist to OPFS.
|
|
12
|
+
*/
|
|
13
|
+
commit(): void;
|
|
14
|
+
/**
|
|
15
|
+
* Create a new index.
|
|
16
|
+
* `fields_json`: JSON array of field definitions.
|
|
17
|
+
* `stemmer`: language string ("english", "french", ...) or empty.
|
|
18
|
+
* Returns an Index. Caller should then call `export_all_files()` to persist to OPFS.
|
|
19
|
+
*/
|
|
20
|
+
constructor(path: string, fields_json: string, stemmer: string);
|
|
21
|
+
/**
|
|
22
|
+
* Export ALL files (for initial persist after create).
|
|
23
|
+
* Returns: `[[path, Uint8Array], ...]`
|
|
24
|
+
*/
|
|
25
|
+
exportAllFiles(): any;
|
|
26
|
+
/**
|
|
27
|
+
* Export dirty files (modified + deleted) since last export.
|
|
28
|
+
* Returns a JS object: `{ modified: [[path, Uint8Array], ...], deleted: [path, ...] }`
|
|
29
|
+
* The JS side writes modified files to OPFS and removes deleted ones.
|
|
30
|
+
*/
|
|
31
|
+
exportDirtyFiles(): any;
|
|
32
|
+
/**
|
|
33
|
+
* Export the index as a LUCE snapshot blob.
|
|
34
|
+
* The index must have no uncommitted changes (call `commit()` first).
|
|
35
|
+
* Returns a `Uint8Array`.
|
|
36
|
+
*/
|
|
37
|
+
exportSnapshot(): Uint8Array;
|
|
38
|
+
/**
|
|
39
|
+
* Import a LUCE snapshot blob and return a new Index.
|
|
40
|
+
* `data`: the LUCE blob as `Uint8Array`.
|
|
41
|
+
* `path`: the logical path for the imported index.
|
|
42
|
+
*/
|
|
43
|
+
static importSnapshot(data: Uint8Array, path: string): Index;
|
|
44
|
+
/**
|
|
45
|
+
* Open an existing index from files previously read from OPFS.
|
|
46
|
+
* `files`: JS Map of filename → Uint8Array.
|
|
47
|
+
* The JS side reads all files from OPFS and passes them here.
|
|
48
|
+
*/
|
|
49
|
+
static open(path: string, files: Map<any, any>): Index;
|
|
50
|
+
remove(doc_id: number): void;
|
|
51
|
+
rollback(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Search the index.
|
|
54
|
+
* `query_json`: a JSON string (contains_split on all text fields) or a query object.
|
|
55
|
+
* Returns JSON: `[{docId, score, highlights?}, ...]`
|
|
56
|
+
*/
|
|
57
|
+
search(query_json: string, limit: number, highlights?: boolean | null): string;
|
|
58
|
+
/**
|
|
59
|
+
* Search with allowed_ids filter.
|
|
60
|
+
* `allowed_ids`: JS array of u32 doc IDs.
|
|
61
|
+
*/
|
|
62
|
+
searchFiltered(query_json: string, limit: number, allowed_ids: Uint32Array, highlights?: boolean | null): string;
|
|
63
|
+
update(doc_id: number, fields_json: string): void;
|
|
64
|
+
readonly numDocs: number;
|
|
65
|
+
readonly path: string;
|
|
66
|
+
readonly schemaJson: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
70
|
+
|
|
71
|
+
export interface InitOutput {
|
|
72
|
+
readonly memory: WebAssembly.Memory;
|
|
73
|
+
readonly __wbg_index_free: (a: number, b: number) => void;
|
|
74
|
+
readonly index_create: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
|
|
75
|
+
readonly index_open: (a: number, b: number, c: any) => [number, number, number];
|
|
76
|
+
readonly index_add: (a: number, b: number, c: number, d: number) => [number, number];
|
|
77
|
+
readonly index_addMany: (a: number, b: number, c: number) => [number, number];
|
|
78
|
+
readonly index_remove: (a: number, b: number) => [number, number];
|
|
79
|
+
readonly index_update: (a: number, b: number, c: number, d: number) => [number, number];
|
|
80
|
+
readonly index_commit: (a: number) => [number, number];
|
|
81
|
+
readonly index_rollback: (a: number) => [number, number];
|
|
82
|
+
readonly index_exportDirtyFiles: (a: number) => [number, number, number];
|
|
83
|
+
readonly index_exportAllFiles: (a: number) => [number, number, number];
|
|
84
|
+
readonly index_exportSnapshot: (a: number) => [number, number, number, number];
|
|
85
|
+
readonly index_importSnapshot: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
86
|
+
readonly index_search: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
87
|
+
readonly index_searchFiltered: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
|
|
88
|
+
readonly index_numDocs: (a: number) => number;
|
|
89
|
+
readonly index_path: (a: number) => [number, number];
|
|
90
|
+
readonly index_schemaJson: (a: number) => [number, number];
|
|
91
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
92
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
93
|
+
readonly __wbindgen_exn_store: (a: number) => void;
|
|
94
|
+
readonly __externref_table_alloc: () => number;
|
|
95
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
96
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
97
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
98
|
+
readonly __wbindgen_start: () => void;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
105
|
+
* a precompiled `WebAssembly.Module`.
|
|
106
|
+
*
|
|
107
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
108
|
+
*
|
|
109
|
+
* @returns {InitOutput}
|
|
110
|
+
*/
|
|
111
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
115
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
116
|
+
*
|
|
117
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
118
|
+
*
|
|
119
|
+
* @returns {Promise<InitOutput>}
|
|
120
|
+
*/
|
|
121
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
package/lucivy_wasm.js
ADDED
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
/* @ts-self-types="./lucivy_wasm.d.ts" */
|
|
2
|
+
|
|
3
|
+
export class Index {
|
|
4
|
+
static __wrap(ptr) {
|
|
5
|
+
ptr = ptr >>> 0;
|
|
6
|
+
const obj = Object.create(Index.prototype);
|
|
7
|
+
obj.__wbg_ptr = ptr;
|
|
8
|
+
IndexFinalization.register(obj, obj.__wbg_ptr, obj);
|
|
9
|
+
return obj;
|
|
10
|
+
}
|
|
11
|
+
__destroy_into_raw() {
|
|
12
|
+
const ptr = this.__wbg_ptr;
|
|
13
|
+
this.__wbg_ptr = 0;
|
|
14
|
+
IndexFinalization.unregister(this);
|
|
15
|
+
return ptr;
|
|
16
|
+
}
|
|
17
|
+
free() {
|
|
18
|
+
const ptr = this.__destroy_into_raw();
|
|
19
|
+
wasm.__wbg_index_free(ptr, 0);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @param {number} doc_id
|
|
23
|
+
* @param {string} fields_json
|
|
24
|
+
*/
|
|
25
|
+
add(doc_id, fields_json) {
|
|
26
|
+
const ptr0 = passStringToWasm0(fields_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
27
|
+
const len0 = WASM_VECTOR_LEN;
|
|
28
|
+
const ret = wasm.index_add(this.__wbg_ptr, doc_id, ptr0, len0);
|
|
29
|
+
if (ret[1]) {
|
|
30
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @param {string} docs_json
|
|
35
|
+
*/
|
|
36
|
+
addMany(docs_json) {
|
|
37
|
+
const ptr0 = passStringToWasm0(docs_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
38
|
+
const len0 = WASM_VECTOR_LEN;
|
|
39
|
+
const ret = wasm.index_addMany(this.__wbg_ptr, ptr0, len0);
|
|
40
|
+
if (ret[1]) {
|
|
41
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Commit changes to the in-memory directory.
|
|
46
|
+
* After this, call `exportDirtyFiles()` to get files to persist to OPFS.
|
|
47
|
+
*/
|
|
48
|
+
commit() {
|
|
49
|
+
const ret = wasm.index_commit(this.__wbg_ptr);
|
|
50
|
+
if (ret[1]) {
|
|
51
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create a new index.
|
|
56
|
+
* `fields_json`: JSON array of field definitions.
|
|
57
|
+
* `stemmer`: language string ("english", "french", ...) or empty.
|
|
58
|
+
* Returns an Index. Caller should then call `export_all_files()` to persist to OPFS.
|
|
59
|
+
* @param {string} path
|
|
60
|
+
* @param {string} fields_json
|
|
61
|
+
* @param {string} stemmer
|
|
62
|
+
*/
|
|
63
|
+
constructor(path, fields_json, stemmer) {
|
|
64
|
+
const ptr0 = passStringToWasm0(path, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
65
|
+
const len0 = WASM_VECTOR_LEN;
|
|
66
|
+
const ptr1 = passStringToWasm0(fields_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
67
|
+
const len1 = WASM_VECTOR_LEN;
|
|
68
|
+
const ptr2 = passStringToWasm0(stemmer, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
69
|
+
const len2 = WASM_VECTOR_LEN;
|
|
70
|
+
const ret = wasm.index_create(ptr0, len0, ptr1, len1, ptr2, len2);
|
|
71
|
+
if (ret[2]) {
|
|
72
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
73
|
+
}
|
|
74
|
+
this.__wbg_ptr = ret[0] >>> 0;
|
|
75
|
+
IndexFinalization.register(this, this.__wbg_ptr, this);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Export ALL files (for initial persist after create).
|
|
80
|
+
* Returns: `[[path, Uint8Array], ...]`
|
|
81
|
+
* @returns {any}
|
|
82
|
+
*/
|
|
83
|
+
exportAllFiles() {
|
|
84
|
+
const ret = wasm.index_exportAllFiles(this.__wbg_ptr);
|
|
85
|
+
if (ret[2]) {
|
|
86
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
87
|
+
}
|
|
88
|
+
return takeFromExternrefTable0(ret[0]);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Export dirty files (modified + deleted) since last export.
|
|
92
|
+
* Returns a JS object: `{ modified: [[path, Uint8Array], ...], deleted: [path, ...] }`
|
|
93
|
+
* The JS side writes modified files to OPFS and removes deleted ones.
|
|
94
|
+
* @returns {any}
|
|
95
|
+
*/
|
|
96
|
+
exportDirtyFiles() {
|
|
97
|
+
const ret = wasm.index_exportDirtyFiles(this.__wbg_ptr);
|
|
98
|
+
if (ret[2]) {
|
|
99
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
100
|
+
}
|
|
101
|
+
return takeFromExternrefTable0(ret[0]);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Export the index as a LUCE snapshot blob.
|
|
105
|
+
* The index must have no uncommitted changes (call `commit()` first).
|
|
106
|
+
* Returns a `Uint8Array`.
|
|
107
|
+
* @returns {Uint8Array}
|
|
108
|
+
*/
|
|
109
|
+
exportSnapshot() {
|
|
110
|
+
const ret = wasm.index_exportSnapshot(this.__wbg_ptr);
|
|
111
|
+
if (ret[3]) {
|
|
112
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
113
|
+
}
|
|
114
|
+
var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
115
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
116
|
+
return v1;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Import a LUCE snapshot blob and return a new Index.
|
|
120
|
+
* `data`: the LUCE blob as `Uint8Array`.
|
|
121
|
+
* `path`: the logical path for the imported index.
|
|
122
|
+
* @param {Uint8Array} data
|
|
123
|
+
* @param {string} path
|
|
124
|
+
* @returns {Index}
|
|
125
|
+
*/
|
|
126
|
+
static importSnapshot(data, path) {
|
|
127
|
+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
128
|
+
const len0 = WASM_VECTOR_LEN;
|
|
129
|
+
const ptr1 = passStringToWasm0(path, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
130
|
+
const len1 = WASM_VECTOR_LEN;
|
|
131
|
+
const ret = wasm.index_importSnapshot(ptr0, len0, ptr1, len1);
|
|
132
|
+
if (ret[2]) {
|
|
133
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
134
|
+
}
|
|
135
|
+
return Index.__wrap(ret[0]);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* @returns {number}
|
|
139
|
+
*/
|
|
140
|
+
get numDocs() {
|
|
141
|
+
const ret = wasm.index_numDocs(this.__wbg_ptr);
|
|
142
|
+
return ret >>> 0;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Open an existing index from files previously read from OPFS.
|
|
146
|
+
* `files`: JS Map of filename → Uint8Array.
|
|
147
|
+
* The JS side reads all files from OPFS and passes them here.
|
|
148
|
+
* @param {string} path
|
|
149
|
+
* @param {Map<any, any>} files
|
|
150
|
+
* @returns {Index}
|
|
151
|
+
*/
|
|
152
|
+
static open(path, files) {
|
|
153
|
+
const ptr0 = passStringToWasm0(path, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
154
|
+
const len0 = WASM_VECTOR_LEN;
|
|
155
|
+
const ret = wasm.index_open(ptr0, len0, files);
|
|
156
|
+
if (ret[2]) {
|
|
157
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
158
|
+
}
|
|
159
|
+
return Index.__wrap(ret[0]);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* @returns {string}
|
|
163
|
+
*/
|
|
164
|
+
get path() {
|
|
165
|
+
let deferred1_0;
|
|
166
|
+
let deferred1_1;
|
|
167
|
+
try {
|
|
168
|
+
const ret = wasm.index_path(this.__wbg_ptr);
|
|
169
|
+
deferred1_0 = ret[0];
|
|
170
|
+
deferred1_1 = ret[1];
|
|
171
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
172
|
+
} finally {
|
|
173
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @param {number} doc_id
|
|
178
|
+
*/
|
|
179
|
+
remove(doc_id) {
|
|
180
|
+
const ret = wasm.index_remove(this.__wbg_ptr, doc_id);
|
|
181
|
+
if (ret[1]) {
|
|
182
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
rollback() {
|
|
186
|
+
const ret = wasm.index_rollback(this.__wbg_ptr);
|
|
187
|
+
if (ret[1]) {
|
|
188
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* @returns {string}
|
|
193
|
+
*/
|
|
194
|
+
get schemaJson() {
|
|
195
|
+
let deferred1_0;
|
|
196
|
+
let deferred1_1;
|
|
197
|
+
try {
|
|
198
|
+
const ret = wasm.index_schemaJson(this.__wbg_ptr);
|
|
199
|
+
deferred1_0 = ret[0];
|
|
200
|
+
deferred1_1 = ret[1];
|
|
201
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
202
|
+
} finally {
|
|
203
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Search the index.
|
|
208
|
+
* `query_json`: a JSON string (contains_split on all text fields) or a query object.
|
|
209
|
+
* Returns JSON: `[{docId, score, highlights?}, ...]`
|
|
210
|
+
* @param {string} query_json
|
|
211
|
+
* @param {number} limit
|
|
212
|
+
* @param {boolean | null} [highlights]
|
|
213
|
+
* @returns {string}
|
|
214
|
+
*/
|
|
215
|
+
search(query_json, limit, highlights) {
|
|
216
|
+
let deferred3_0;
|
|
217
|
+
let deferred3_1;
|
|
218
|
+
try {
|
|
219
|
+
const ptr0 = passStringToWasm0(query_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
220
|
+
const len0 = WASM_VECTOR_LEN;
|
|
221
|
+
const ret = wasm.index_search(this.__wbg_ptr, ptr0, len0, limit, isLikeNone(highlights) ? 0xFFFFFF : highlights ? 1 : 0);
|
|
222
|
+
var ptr2 = ret[0];
|
|
223
|
+
var len2 = ret[1];
|
|
224
|
+
if (ret[3]) {
|
|
225
|
+
ptr2 = 0; len2 = 0;
|
|
226
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
227
|
+
}
|
|
228
|
+
deferred3_0 = ptr2;
|
|
229
|
+
deferred3_1 = len2;
|
|
230
|
+
return getStringFromWasm0(ptr2, len2);
|
|
231
|
+
} finally {
|
|
232
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Search with allowed_ids filter.
|
|
237
|
+
* `allowed_ids`: JS array of u32 doc IDs.
|
|
238
|
+
* @param {string} query_json
|
|
239
|
+
* @param {number} limit
|
|
240
|
+
* @param {Uint32Array} allowed_ids
|
|
241
|
+
* @param {boolean | null} [highlights]
|
|
242
|
+
* @returns {string}
|
|
243
|
+
*/
|
|
244
|
+
searchFiltered(query_json, limit, allowed_ids, highlights) {
|
|
245
|
+
let deferred4_0;
|
|
246
|
+
let deferred4_1;
|
|
247
|
+
try {
|
|
248
|
+
const ptr0 = passStringToWasm0(query_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
249
|
+
const len0 = WASM_VECTOR_LEN;
|
|
250
|
+
const ptr1 = passArray32ToWasm0(allowed_ids, wasm.__wbindgen_malloc);
|
|
251
|
+
const len1 = WASM_VECTOR_LEN;
|
|
252
|
+
const ret = wasm.index_searchFiltered(this.__wbg_ptr, ptr0, len0, limit, ptr1, len1, isLikeNone(highlights) ? 0xFFFFFF : highlights ? 1 : 0);
|
|
253
|
+
var ptr3 = ret[0];
|
|
254
|
+
var len3 = ret[1];
|
|
255
|
+
if (ret[3]) {
|
|
256
|
+
ptr3 = 0; len3 = 0;
|
|
257
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
258
|
+
}
|
|
259
|
+
deferred4_0 = ptr3;
|
|
260
|
+
deferred4_1 = len3;
|
|
261
|
+
return getStringFromWasm0(ptr3, len3);
|
|
262
|
+
} finally {
|
|
263
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* @param {number} doc_id
|
|
268
|
+
* @param {string} fields_json
|
|
269
|
+
*/
|
|
270
|
+
update(doc_id, fields_json) {
|
|
271
|
+
const ptr0 = passStringToWasm0(fields_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
272
|
+
const len0 = WASM_VECTOR_LEN;
|
|
273
|
+
const ret = wasm.index_update(this.__wbg_ptr, doc_id, ptr0, len0);
|
|
274
|
+
if (ret[1]) {
|
|
275
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (Symbol.dispose) Index.prototype[Symbol.dispose] = Index.prototype.free;
|
|
280
|
+
|
|
281
|
+
function __wbg_get_imports() {
|
|
282
|
+
const import0 = {
|
|
283
|
+
__proto__: null,
|
|
284
|
+
__wbg_Error_8c4e43fe74559d73: function(arg0, arg1) {
|
|
285
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
286
|
+
return ret;
|
|
287
|
+
},
|
|
288
|
+
__wbg___wbindgen_debug_string_0bc8482c6e3508ae: function(arg0, arg1) {
|
|
289
|
+
const ret = debugString(arg1);
|
|
290
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
291
|
+
const len1 = WASM_VECTOR_LEN;
|
|
292
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
293
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
294
|
+
},
|
|
295
|
+
__wbg___wbindgen_string_get_72fb696202c56729: function(arg0, arg1) {
|
|
296
|
+
const obj = arg1;
|
|
297
|
+
const ret = typeof(obj) === 'string' ? obj : undefined;
|
|
298
|
+
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
299
|
+
var len1 = WASM_VECTOR_LEN;
|
|
300
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
301
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
302
|
+
},
|
|
303
|
+
__wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) {
|
|
304
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
305
|
+
},
|
|
306
|
+
__wbg_done_57b39ecd9addfe81: function(arg0) {
|
|
307
|
+
const ret = arg0.done;
|
|
308
|
+
return ret;
|
|
309
|
+
},
|
|
310
|
+
__wbg_entries_200a450561fa274f: function(arg0) {
|
|
311
|
+
const ret = arg0.entries();
|
|
312
|
+
return ret;
|
|
313
|
+
},
|
|
314
|
+
__wbg_from_bddd64e7d5ff6941: function(arg0) {
|
|
315
|
+
const ret = Array.from(arg0);
|
|
316
|
+
return ret;
|
|
317
|
+
},
|
|
318
|
+
__wbg_getRandomValues_9c5c1b115e142bb8: function() { return handleError(function (arg0, arg1) {
|
|
319
|
+
globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1));
|
|
320
|
+
}, arguments); },
|
|
321
|
+
__wbg_get_9b94d73e6221f75c: function(arg0, arg1) {
|
|
322
|
+
const ret = arg0[arg1 >>> 0];
|
|
323
|
+
return ret;
|
|
324
|
+
},
|
|
325
|
+
__wbg_length_32ed9a279acd054c: function(arg0) {
|
|
326
|
+
const ret = arg0.length;
|
|
327
|
+
return ret;
|
|
328
|
+
},
|
|
329
|
+
__wbg_new_361308b2356cecd0: function() {
|
|
330
|
+
const ret = new Object();
|
|
331
|
+
return ret;
|
|
332
|
+
},
|
|
333
|
+
__wbg_new_3eb36ae241fe6f44: function() {
|
|
334
|
+
const ret = new Array();
|
|
335
|
+
return ret;
|
|
336
|
+
},
|
|
337
|
+
__wbg_new_dd2b680c8bf6ae29: function(arg0) {
|
|
338
|
+
const ret = new Uint8Array(arg0);
|
|
339
|
+
return ret;
|
|
340
|
+
},
|
|
341
|
+
__wbg_new_from_slice_a3d2629dc1826784: function(arg0, arg1) {
|
|
342
|
+
const ret = new Uint8Array(getArrayU8FromWasm0(arg0, arg1));
|
|
343
|
+
return ret;
|
|
344
|
+
},
|
|
345
|
+
__wbg_next_3482f54c49e8af19: function() { return handleError(function (arg0) {
|
|
346
|
+
const ret = arg0.next();
|
|
347
|
+
return ret;
|
|
348
|
+
}, arguments); },
|
|
349
|
+
__wbg_prototypesetcall_bdcdcc5842e4d77d: function(arg0, arg1, arg2) {
|
|
350
|
+
Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
|
|
351
|
+
},
|
|
352
|
+
__wbg_push_8ffdcb2063340ba5: function(arg0, arg1) {
|
|
353
|
+
const ret = arg0.push(arg1);
|
|
354
|
+
return ret;
|
|
355
|
+
},
|
|
356
|
+
__wbg_set_6cb8631f80447a67: function() { return handleError(function (arg0, arg1, arg2) {
|
|
357
|
+
const ret = Reflect.set(arg0, arg1, arg2);
|
|
358
|
+
return ret;
|
|
359
|
+
}, arguments); },
|
|
360
|
+
__wbg_value_0546255b415e96c1: function(arg0) {
|
|
361
|
+
const ret = arg0.value;
|
|
362
|
+
return ret;
|
|
363
|
+
},
|
|
364
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
365
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
366
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
367
|
+
return ret;
|
|
368
|
+
},
|
|
369
|
+
__wbindgen_init_externref_table: function() {
|
|
370
|
+
const table = wasm.__wbindgen_externrefs;
|
|
371
|
+
const offset = table.grow(4);
|
|
372
|
+
table.set(0, undefined);
|
|
373
|
+
table.set(offset + 0, undefined);
|
|
374
|
+
table.set(offset + 1, null);
|
|
375
|
+
table.set(offset + 2, true);
|
|
376
|
+
table.set(offset + 3, false);
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
return {
|
|
380
|
+
__proto__: null,
|
|
381
|
+
"./lucivy_wasm_bg.js": import0,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const IndexFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
386
|
+
? { register: () => {}, unregister: () => {} }
|
|
387
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_index_free(ptr >>> 0, 1));
|
|
388
|
+
|
|
389
|
+
function addToExternrefTable0(obj) {
|
|
390
|
+
const idx = wasm.__externref_table_alloc();
|
|
391
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
392
|
+
return idx;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function debugString(val) {
|
|
396
|
+
// primitive types
|
|
397
|
+
const type = typeof val;
|
|
398
|
+
if (type == 'number' || type == 'boolean' || val == null) {
|
|
399
|
+
return `${val}`;
|
|
400
|
+
}
|
|
401
|
+
if (type == 'string') {
|
|
402
|
+
return `"${val}"`;
|
|
403
|
+
}
|
|
404
|
+
if (type == 'symbol') {
|
|
405
|
+
const description = val.description;
|
|
406
|
+
if (description == null) {
|
|
407
|
+
return 'Symbol';
|
|
408
|
+
} else {
|
|
409
|
+
return `Symbol(${description})`;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (type == 'function') {
|
|
413
|
+
const name = val.name;
|
|
414
|
+
if (typeof name == 'string' && name.length > 0) {
|
|
415
|
+
return `Function(${name})`;
|
|
416
|
+
} else {
|
|
417
|
+
return 'Function';
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
// objects
|
|
421
|
+
if (Array.isArray(val)) {
|
|
422
|
+
const length = val.length;
|
|
423
|
+
let debug = '[';
|
|
424
|
+
if (length > 0) {
|
|
425
|
+
debug += debugString(val[0]);
|
|
426
|
+
}
|
|
427
|
+
for(let i = 1; i < length; i++) {
|
|
428
|
+
debug += ', ' + debugString(val[i]);
|
|
429
|
+
}
|
|
430
|
+
debug += ']';
|
|
431
|
+
return debug;
|
|
432
|
+
}
|
|
433
|
+
// Test for built-in
|
|
434
|
+
const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
|
|
435
|
+
let className;
|
|
436
|
+
if (builtInMatches && builtInMatches.length > 1) {
|
|
437
|
+
className = builtInMatches[1];
|
|
438
|
+
} else {
|
|
439
|
+
// Failed to match the standard '[object ClassName]'
|
|
440
|
+
return toString.call(val);
|
|
441
|
+
}
|
|
442
|
+
if (className == 'Object') {
|
|
443
|
+
// we're a user defined class or Object
|
|
444
|
+
// JSON.stringify avoids problems with cycles, and is generally much
|
|
445
|
+
// easier than looping through ownProperties of `val`.
|
|
446
|
+
try {
|
|
447
|
+
return 'Object(' + JSON.stringify(val) + ')';
|
|
448
|
+
} catch (_) {
|
|
449
|
+
return 'Object';
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
// errors
|
|
453
|
+
if (val instanceof Error) {
|
|
454
|
+
return `${val.name}: ${val.message}\n${val.stack}`;
|
|
455
|
+
}
|
|
456
|
+
// TODO we could test for more things here, like `Set`s and `Map`s.
|
|
457
|
+
return className;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
461
|
+
ptr = ptr >>> 0;
|
|
462
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
let cachedDataViewMemory0 = null;
|
|
466
|
+
function getDataViewMemory0() {
|
|
467
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
468
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
469
|
+
}
|
|
470
|
+
return cachedDataViewMemory0;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function getStringFromWasm0(ptr, len) {
|
|
474
|
+
ptr = ptr >>> 0;
|
|
475
|
+
return decodeText(ptr, len);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
let cachedUint32ArrayMemory0 = null;
|
|
479
|
+
function getUint32ArrayMemory0() {
|
|
480
|
+
if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) {
|
|
481
|
+
cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer);
|
|
482
|
+
}
|
|
483
|
+
return cachedUint32ArrayMemory0;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
let cachedUint8ArrayMemory0 = null;
|
|
487
|
+
function getUint8ArrayMemory0() {
|
|
488
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
489
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
490
|
+
}
|
|
491
|
+
return cachedUint8ArrayMemory0;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function handleError(f, args) {
|
|
495
|
+
try {
|
|
496
|
+
return f.apply(this, args);
|
|
497
|
+
} catch (e) {
|
|
498
|
+
const idx = addToExternrefTable0(e);
|
|
499
|
+
wasm.__wbindgen_exn_store(idx);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function isLikeNone(x) {
|
|
504
|
+
return x === undefined || x === null;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function passArray32ToWasm0(arg, malloc) {
|
|
508
|
+
const ptr = malloc(arg.length * 4, 4) >>> 0;
|
|
509
|
+
getUint32ArrayMemory0().set(arg, ptr / 4);
|
|
510
|
+
WASM_VECTOR_LEN = arg.length;
|
|
511
|
+
return ptr;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
515
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
516
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
517
|
+
WASM_VECTOR_LEN = arg.length;
|
|
518
|
+
return ptr;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
522
|
+
if (realloc === undefined) {
|
|
523
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
524
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
525
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
526
|
+
WASM_VECTOR_LEN = buf.length;
|
|
527
|
+
return ptr;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
let len = arg.length;
|
|
531
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
532
|
+
|
|
533
|
+
const mem = getUint8ArrayMemory0();
|
|
534
|
+
|
|
535
|
+
let offset = 0;
|
|
536
|
+
|
|
537
|
+
for (; offset < len; offset++) {
|
|
538
|
+
const code = arg.charCodeAt(offset);
|
|
539
|
+
if (code > 0x7F) break;
|
|
540
|
+
mem[ptr + offset] = code;
|
|
541
|
+
}
|
|
542
|
+
if (offset !== len) {
|
|
543
|
+
if (offset !== 0) {
|
|
544
|
+
arg = arg.slice(offset);
|
|
545
|
+
}
|
|
546
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
547
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
548
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
549
|
+
|
|
550
|
+
offset += ret.written;
|
|
551
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
WASM_VECTOR_LEN = offset;
|
|
555
|
+
return ptr;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function takeFromExternrefTable0(idx) {
|
|
559
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
560
|
+
wasm.__externref_table_dealloc(idx);
|
|
561
|
+
return value;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
565
|
+
cachedTextDecoder.decode();
|
|
566
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
567
|
+
let numBytesDecoded = 0;
|
|
568
|
+
function decodeText(ptr, len) {
|
|
569
|
+
numBytesDecoded += len;
|
|
570
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
571
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
572
|
+
cachedTextDecoder.decode();
|
|
573
|
+
numBytesDecoded = len;
|
|
574
|
+
}
|
|
575
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const cachedTextEncoder = new TextEncoder();
|
|
579
|
+
|
|
580
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
581
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
582
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
583
|
+
view.set(buf);
|
|
584
|
+
return {
|
|
585
|
+
read: arg.length,
|
|
586
|
+
written: buf.length
|
|
587
|
+
};
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
let WASM_VECTOR_LEN = 0;
|
|
592
|
+
|
|
593
|
+
let wasmModule, wasm;
|
|
594
|
+
function __wbg_finalize_init(instance, module) {
|
|
595
|
+
wasm = instance.exports;
|
|
596
|
+
wasmModule = module;
|
|
597
|
+
cachedDataViewMemory0 = null;
|
|
598
|
+
cachedUint32ArrayMemory0 = null;
|
|
599
|
+
cachedUint8ArrayMemory0 = null;
|
|
600
|
+
wasm.__wbindgen_start();
|
|
601
|
+
return wasm;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
async function __wbg_load(module, imports) {
|
|
605
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
606
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
607
|
+
try {
|
|
608
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
609
|
+
} catch (e) {
|
|
610
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
611
|
+
|
|
612
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
613
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
614
|
+
|
|
615
|
+
} else { throw e; }
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
const bytes = await module.arrayBuffer();
|
|
620
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
621
|
+
} else {
|
|
622
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
623
|
+
|
|
624
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
625
|
+
return { instance, module };
|
|
626
|
+
} else {
|
|
627
|
+
return instance;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function expectedResponseType(type) {
|
|
632
|
+
switch (type) {
|
|
633
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
634
|
+
}
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function initSync(module) {
|
|
640
|
+
if (wasm !== undefined) return wasm;
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
if (module !== undefined) {
|
|
644
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
645
|
+
({module} = module)
|
|
646
|
+
} else {
|
|
647
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const imports = __wbg_get_imports();
|
|
652
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
653
|
+
module = new WebAssembly.Module(module);
|
|
654
|
+
}
|
|
655
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
656
|
+
return __wbg_finalize_init(instance, module);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
async function __wbg_init(module_or_path) {
|
|
660
|
+
if (wasm !== undefined) return wasm;
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
if (module_or_path !== undefined) {
|
|
664
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
665
|
+
({module_or_path} = module_or_path)
|
|
666
|
+
} else {
|
|
667
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
if (module_or_path === undefined) {
|
|
672
|
+
module_or_path = new URL('lucivy_wasm_bg.wasm', import.meta.url);
|
|
673
|
+
}
|
|
674
|
+
const imports = __wbg_get_imports();
|
|
675
|
+
|
|
676
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
677
|
+
module_or_path = fetch(module_or_path);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
681
|
+
|
|
682
|
+
return __wbg_finalize_init(instance, module);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const __wbg_index_free: (a: number, b: number) => void;
|
|
5
|
+
export const index_create: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
|
|
6
|
+
export const index_open: (a: number, b: number, c: any) => [number, number, number];
|
|
7
|
+
export const index_add: (a: number, b: number, c: number, d: number) => [number, number];
|
|
8
|
+
export const index_addMany: (a: number, b: number, c: number) => [number, number];
|
|
9
|
+
export const index_remove: (a: number, b: number) => [number, number];
|
|
10
|
+
export const index_update: (a: number, b: number, c: number, d: number) => [number, number];
|
|
11
|
+
export const index_commit: (a: number) => [number, number];
|
|
12
|
+
export const index_rollback: (a: number) => [number, number];
|
|
13
|
+
export const index_exportDirtyFiles: (a: number) => [number, number, number];
|
|
14
|
+
export const index_exportAllFiles: (a: number) => [number, number, number];
|
|
15
|
+
export const index_exportSnapshot: (a: number) => [number, number, number, number];
|
|
16
|
+
export const index_importSnapshot: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
17
|
+
export const index_search: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
18
|
+
export const index_searchFiltered: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
|
|
19
|
+
export const index_numDocs: (a: number) => number;
|
|
20
|
+
export const index_path: (a: number) => [number, number];
|
|
21
|
+
export const index_schemaJson: (a: number) => [number, number];
|
|
22
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
23
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
24
|
+
export const __wbindgen_exn_store: (a: number) => void;
|
|
25
|
+
export const __externref_table_alloc: () => number;
|
|
26
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
27
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
28
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
29
|
+
export const __wbindgen_start: () => void;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lucivy-wasm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Fast BM25 full-text search for browsers — WASM build of Lucivy with OPFS persistence",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Luciform Research",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/L-Defraiteur/lucivy",
|
|
11
|
+
"directory": "bindings/wasm"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"search",
|
|
15
|
+
"full-text",
|
|
16
|
+
"bm25",
|
|
17
|
+
"fuzzy",
|
|
18
|
+
"substring",
|
|
19
|
+
"wasm",
|
|
20
|
+
"browser",
|
|
21
|
+
"opfs"
|
|
22
|
+
],
|
|
23
|
+
"files": [
|
|
24
|
+
"lucivy_wasm_bg.wasm",
|
|
25
|
+
"lucivy_wasm.js",
|
|
26
|
+
"lucivy_wasm.d.ts",
|
|
27
|
+
"lucivy_wasm_bg.wasm.d.ts",
|
|
28
|
+
"snippets",
|
|
29
|
+
"README.md"
|
|
30
|
+
],
|
|
31
|
+
"main": "lucivy_wasm.js",
|
|
32
|
+
"types": "lucivy_wasm.d.ts",
|
|
33
|
+
"sideEffects": [
|
|
34
|
+
"./snippets/*"
|
|
35
|
+
]
|
|
36
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 Google Inc. All Rights Reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
* See the License for the specific language governing permissions and
|
|
11
|
+
* limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// Note: we use `wasm_bindgen_worker_`-prefixed message types to make sure
|
|
15
|
+
// we can handle bundling into other files, which might happen to have their
|
|
16
|
+
// own `postMessage`/`onmessage` communication channels.
|
|
17
|
+
//
|
|
18
|
+
// If we didn't take that into the account, we could send much simpler signals
|
|
19
|
+
// like just `0` or whatever, but the code would be less resilient.
|
|
20
|
+
|
|
21
|
+
function waitForMsgType(target, type) {
|
|
22
|
+
return new Promise(resolve => {
|
|
23
|
+
target.addEventListener('message', function onMsg({ data }) {
|
|
24
|
+
if (data?.type !== type) return;
|
|
25
|
+
target.removeEventListener('message', onMsg);
|
|
26
|
+
resolve(data);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
waitForMsgType(self, 'wasm_bindgen_worker_init').then(async ({ init, receiver }) => {
|
|
32
|
+
// # Note 1
|
|
33
|
+
// Our JS should have been generated in
|
|
34
|
+
// `[out-dir]/snippets/wasm-bindgen-rayon-[hash]/workerHelpers.js`,
|
|
35
|
+
// resolve the main module via `../../..`.
|
|
36
|
+
//
|
|
37
|
+
// This might need updating if the generated structure changes on wasm-bindgen
|
|
38
|
+
// side ever in the future, but works well with bundlers today. The whole
|
|
39
|
+
// point of this crate, after all, is to abstract away unstable features
|
|
40
|
+
// and temporary bugs so that you don't need to deal with them in your code.
|
|
41
|
+
//
|
|
42
|
+
// # Note 2
|
|
43
|
+
// This could be a regular import, but then some bundlers complain about
|
|
44
|
+
// circular deps.
|
|
45
|
+
//
|
|
46
|
+
// Dynamic import could be cheap if this file was inlined into the parent,
|
|
47
|
+
// which would require us just using `../../..` in `new Worker` below,
|
|
48
|
+
// but that doesn't work because wasm-pack unconditionally adds
|
|
49
|
+
// "sideEffects":false (see below).
|
|
50
|
+
//
|
|
51
|
+
// OTOH, even though it can't be inlined, it should be still reasonably
|
|
52
|
+
// cheap since the requested file is already in cache (it was loaded by
|
|
53
|
+
// the main thread).
|
|
54
|
+
const pkg = await import('../../..');
|
|
55
|
+
await pkg.default(init);
|
|
56
|
+
postMessage({ type: 'wasm_bindgen_worker_ready' });
|
|
57
|
+
pkg.wbg_rayon_start_worker(receiver);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Note: this is never used, but necessary to prevent a bug in Firefox
|
|
61
|
+
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1702191) where it collects
|
|
62
|
+
// Web Workers that have a shared WebAssembly memory with the main thread,
|
|
63
|
+
// but are not explicitly rooted via a `Worker` instance.
|
|
64
|
+
//
|
|
65
|
+
// By storing them in a variable, we can keep `Worker` objects around and
|
|
66
|
+
// prevent them from getting GC-d.
|
|
67
|
+
let _workers;
|
|
68
|
+
|
|
69
|
+
export async function startWorkers(module, memory, builder) {
|
|
70
|
+
if (builder.numThreads() === 0) {
|
|
71
|
+
throw new Error(`num_threads must be > 0.`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const workerInit = {
|
|
75
|
+
type: 'wasm_bindgen_worker_init',
|
|
76
|
+
init: { module_or_path: module, memory },
|
|
77
|
+
receiver: builder.receiver()
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
_workers = await Promise.all(
|
|
81
|
+
Array.from({ length: builder.numThreads() }, async () => {
|
|
82
|
+
// Self-spawn into a new Worker.
|
|
83
|
+
//
|
|
84
|
+
// TODO: while `new URL('...', import.meta.url) becomes a semi-standard
|
|
85
|
+
// way to get asset URLs relative to the module across various bundlers
|
|
86
|
+
// and browser, ideally we should switch to `import.meta.resolve`
|
|
87
|
+
// once it becomes a standard.
|
|
88
|
+
//
|
|
89
|
+
// Note: we could use `../../..` as the URL here to inline workerHelpers.js
|
|
90
|
+
// into the parent entry instead of creating another split point -
|
|
91
|
+
// this would be preferable from optimization perspective -
|
|
92
|
+
// however, Webpack then eliminates all message handler code
|
|
93
|
+
// because wasm-pack produces "sideEffects":false in package.json
|
|
94
|
+
// unconditionally.
|
|
95
|
+
//
|
|
96
|
+
// The only way to work around that is to have side effect code
|
|
97
|
+
// in an entry point such as Worker file itself.
|
|
98
|
+
const worker = new Worker(new URL('./workerHelpers.js', import.meta.url), {
|
|
99
|
+
type: 'module'
|
|
100
|
+
});
|
|
101
|
+
worker.postMessage(workerInit);
|
|
102
|
+
await waitForMsgType(worker, 'wasm_bindgen_worker_ready');
|
|
103
|
+
return worker;
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
builder.build();
|
|
107
|
+
}
|