@ilyankin/tachibana 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-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +104 -2
- package/THIRD_PARTY_NOTICES.md +36 -0
- package/dist/client.d.ts +103 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +276 -0
- package/dist/client.js.map +1 -0
- package/dist/coordinator.d.ts +74 -0
- package/dist/coordinator.d.ts.map +1 -0
- package/dist/coordinator.js +484 -0
- package/dist/coordinator.js.map +1 -0
- package/dist/error.d.ts +31 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +57 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.d.ts +93 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +36 -0
- package/dist/protocol.js.map +1 -0
- package/dist/sudachi-opfs.d.ts +37 -0
- package/dist/sudachi-opfs.d.ts.map +1 -0
- package/dist/sudachi-opfs.js +279 -0
- package/dist/sudachi-opfs.js.map +1 -0
- package/dist/types.d.ts +92 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/worker.d.ts +8 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +194 -0
- package/dist/worker.js.map +1 -0
- package/licenses/kanji-processor-LICENSE +21 -0
- package/licenses/sudachi-LICENSE +201 -0
- package/licenses/unicode-license.txt +39 -0
- package/package.json +62 -4
- package/wasm/tachibana_wasm.d.ts +2166 -0
- package/wasm/tachibana_wasm.js +1264 -0
- package/wasm/tachibana_wasm_bg.wasm +0 -0
- package/wasm/tachibana_wasm_bg.wasm.d.ts +65 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { Coordinator } from "./coordinator.js";
|
|
2
|
+
/**
|
|
3
|
+
* Browser client for the Tachibana offline Japanese dictionary engine.
|
|
4
|
+
*
|
|
5
|
+
* Runs SQLite + WebAssembly inside a Dedicated Web Worker (persisting dictionaries in OPFS via
|
|
6
|
+
* `opfs-sahpool` without requiring COOP/COEP headers) and coordinates multiple open tabs
|
|
7
|
+
* transparently through Web Locks + `BroadcastChannel`.
|
|
8
|
+
*/
|
|
9
|
+
export class Tachibana {
|
|
10
|
+
coordinator;
|
|
11
|
+
nextRequestId = 1n;
|
|
12
|
+
constructor(coordinator) {
|
|
13
|
+
this.coordinator = coordinator;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Opens a Tachibana dictionary installation in the browser.
|
|
17
|
+
*
|
|
18
|
+
* Defaults to persistent OPFS storage (`directory: "tachibana"`) with automatic multi-tab
|
|
19
|
+
* coordination. Pass `{ storage: "memory" }` for ephemeral sessions (tests, incognito, HTTP).
|
|
20
|
+
*/
|
|
21
|
+
static async open(options = {}) {
|
|
22
|
+
const coordinator = await Coordinator.open({
|
|
23
|
+
storage: options.storage ?? "opfs",
|
|
24
|
+
directory: options.directory ?? "tachibana",
|
|
25
|
+
initialCapacity: options.initialCapacity,
|
|
26
|
+
autoLoadSudachi: options.autoLoadSudachi,
|
|
27
|
+
workerUrl: options.workerUrl,
|
|
28
|
+
wasmUrl: options.wasmUrl,
|
|
29
|
+
});
|
|
30
|
+
return new Tachibana(coordinator);
|
|
31
|
+
}
|
|
32
|
+
/** Whether this tab currently owns the Dedicated Worker (`true` in memory mode or leader tab). */
|
|
33
|
+
get isLeader() {
|
|
34
|
+
return this.coordinator.isLeader;
|
|
35
|
+
}
|
|
36
|
+
/** Unique identifier of this tab instance (matches `ChangeEvent.sourceTabId`). */
|
|
37
|
+
get tabId() {
|
|
38
|
+
return this.coordinator.tabId;
|
|
39
|
+
}
|
|
40
|
+
/** Subscribes to dictionary/configuration/Sudachi mutations across all open tabs. */
|
|
41
|
+
onChange(listener) {
|
|
42
|
+
return this.coordinator.onChange(listener);
|
|
43
|
+
}
|
|
44
|
+
allocRequestId(explicit) {
|
|
45
|
+
if (explicit !== undefined)
|
|
46
|
+
return explicit;
|
|
47
|
+
const id = this.nextRequestId;
|
|
48
|
+
this.nextRequestId += 1n;
|
|
49
|
+
return id;
|
|
50
|
+
}
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// Installation management
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
/** Installs a Yomitan dictionary or frequency ZIP archive and returns its catalog summary. */
|
|
55
|
+
async install(source, options = {}) {
|
|
56
|
+
const request = options.replace !== undefined ? { replace: options.replace } : undefined;
|
|
57
|
+
const onProgress = options.onProgress
|
|
58
|
+
? (p) => options.onProgress?.(p)
|
|
59
|
+
: undefined;
|
|
60
|
+
return await this.coordinator.call("install", [source, request], onProgress);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Aborts an in-flight import on the leader tab by terminating and restarting the Worker,
|
|
64
|
+
* then automatically reclaiming any partial snapshot files.
|
|
65
|
+
*/
|
|
66
|
+
async cancelImport() {
|
|
67
|
+
await this.coordinator.restartWorker();
|
|
68
|
+
}
|
|
69
|
+
/** Lists all installed dictionaries in lookup priority order. */
|
|
70
|
+
async list() {
|
|
71
|
+
return await this.coordinator.call("list");
|
|
72
|
+
}
|
|
73
|
+
/** Deletes dictionary `dictionaryId` and immediately reclaims its OPFS snapshot file. */
|
|
74
|
+
async delete(dictionaryId) {
|
|
75
|
+
await this.coordinator.call("delete", [dictionaryId]);
|
|
76
|
+
}
|
|
77
|
+
/** Sets or clears (`null`/`undefined`) the user-visible display title for `dictionaryId`. */
|
|
78
|
+
async rename(dictionaryId, title) {
|
|
79
|
+
await this.coordinator.call("rename", [dictionaryId, title ?? undefined]);
|
|
80
|
+
}
|
|
81
|
+
/** Enables or disables `dictionaryId` for subsequent lookups. */
|
|
82
|
+
async setEnabled(dictionaryId, enabled) {
|
|
83
|
+
await this.coordinator.call("setEnabled", [dictionaryId, enabled]);
|
|
84
|
+
}
|
|
85
|
+
/** Sets the dictionary lookup priority order; `ids` must include every installed ID once. */
|
|
86
|
+
async setOrder(ids) {
|
|
87
|
+
await this.coordinator.call("setOrder", [ids]);
|
|
88
|
+
}
|
|
89
|
+
/** Removes unreferenced snapshots and leftovers from interrupted imports. */
|
|
90
|
+
async collectGarbage() {
|
|
91
|
+
await this.coordinator.call("collectGarbage");
|
|
92
|
+
}
|
|
93
|
+
/** Updates engine configuration (for example, enabling installed frequency ranking). */
|
|
94
|
+
async configure(options) {
|
|
95
|
+
await this.coordinator.call("configure", [options]);
|
|
96
|
+
}
|
|
97
|
+
/** Verifies SQLite integrity of the catalog and all active snapshots; returns verified count. */
|
|
98
|
+
async verifyIntegrity() {
|
|
99
|
+
return await this.coordinator.call("verifyIntegrity");
|
|
100
|
+
}
|
|
101
|
+
/** Returns current storage, dictionary count, frequency, pool capacity, and Sudachi state. */
|
|
102
|
+
async status() {
|
|
103
|
+
return await this.coordinator.call("status");
|
|
104
|
+
}
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
// Lookup & reading
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
/**
|
|
109
|
+
* Looks up a reader tap and returns ready-to-render cards with articles, tags, frequency,
|
|
110
|
+
* pitch accents, IPA, and optional Sudachi context analysis.
|
|
111
|
+
*
|
|
112
|
+
* `position` defaults to UTF-16 code units (matching JS `string` indices and DOM ranges).
|
|
113
|
+
*/
|
|
114
|
+
async lookupReaderCards(input) {
|
|
115
|
+
return await this.coordinator.call("lookupReaderCards", [
|
|
116
|
+
{
|
|
117
|
+
request_id: this.allocRequestId(input.request_id),
|
|
118
|
+
text: input.text,
|
|
119
|
+
position: input.position,
|
|
120
|
+
unit: input.unit ?? "Utf16CodeUnits",
|
|
121
|
+
ruby: input.ruby ?? [],
|
|
122
|
+
options: input.options,
|
|
123
|
+
},
|
|
124
|
+
]);
|
|
125
|
+
}
|
|
126
|
+
/** Phase-1 reader tap lookup returning grouped headwords and highlight span without article bodies. */
|
|
127
|
+
async lookupReader(input) {
|
|
128
|
+
return await this.coordinator.call("lookupReader", [
|
|
129
|
+
{
|
|
130
|
+
request_id: this.allocRequestId(input.request_id),
|
|
131
|
+
text: input.text,
|
|
132
|
+
position: input.position,
|
|
133
|
+
unit: input.unit ?? "Utf16CodeUnits",
|
|
134
|
+
ruby: input.ruby ?? [],
|
|
135
|
+
options: input.options,
|
|
136
|
+
},
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
/** Exact term or reading lookup. */
|
|
140
|
+
async lookupExact(input) {
|
|
141
|
+
return await this.coordinator.call("lookupExact", [
|
|
142
|
+
{
|
|
143
|
+
request_id: this.allocRequestId(input.request_id),
|
|
144
|
+
query: input.query,
|
|
145
|
+
limit: input.limit ?? 20,
|
|
146
|
+
},
|
|
147
|
+
]);
|
|
148
|
+
}
|
|
149
|
+
/** Direct prefix scan at `input.position` without morphological deinflection. */
|
|
150
|
+
async lookupAt(input) {
|
|
151
|
+
return await this.coordinator.call("lookupAt", [
|
|
152
|
+
{
|
|
153
|
+
request_id: this.allocRequestId(input.request_id),
|
|
154
|
+
text: input.text,
|
|
155
|
+
position: input.position,
|
|
156
|
+
unit: input.unit ?? "Utf16CodeUnits",
|
|
157
|
+
options: input.options,
|
|
158
|
+
},
|
|
159
|
+
]);
|
|
160
|
+
}
|
|
161
|
+
/** Whole-query Japanese normalization and deinflection lookup. */
|
|
162
|
+
async lookupJapanese(input) {
|
|
163
|
+
return await this.coordinator.call("lookupJapanese", [
|
|
164
|
+
{
|
|
165
|
+
request_id: this.allocRequestId(input.request_id),
|
|
166
|
+
query: input.query,
|
|
167
|
+
options: input.options,
|
|
168
|
+
},
|
|
169
|
+
]);
|
|
170
|
+
}
|
|
171
|
+
/** Position-based Japanese normalization, deinflection, and optional context lookup. */
|
|
172
|
+
async lookupJapaneseAt(input) {
|
|
173
|
+
return await this.coordinator.call("lookupJapaneseAt", [
|
|
174
|
+
{
|
|
175
|
+
request_id: this.allocRequestId(input.request_id),
|
|
176
|
+
text: input.text,
|
|
177
|
+
position: input.position,
|
|
178
|
+
unit: input.unit ?? "Utf16CodeUnits",
|
|
179
|
+
options: input.options,
|
|
180
|
+
},
|
|
181
|
+
]);
|
|
182
|
+
}
|
|
183
|
+
/** Tokenizes `input.text` with readings and spans using the loaded Sudachi analyzer. */
|
|
184
|
+
async analyzeText(input) {
|
|
185
|
+
return await this.coordinator.call("analyzeText", [
|
|
186
|
+
{
|
|
187
|
+
request_id: this.allocRequestId(input.request_id),
|
|
188
|
+
text: input.text,
|
|
189
|
+
},
|
|
190
|
+
]);
|
|
191
|
+
}
|
|
192
|
+
/** Traverses outgoing `FormOf` links from `input.entry`. */
|
|
193
|
+
async resolveFormOf(input) {
|
|
194
|
+
return await this.coordinator.call("resolveFormOf", [
|
|
195
|
+
{
|
|
196
|
+
request_id: this.allocRequestId(input.request_id),
|
|
197
|
+
entry: input.entry,
|
|
198
|
+
options: input.options,
|
|
199
|
+
},
|
|
200
|
+
]);
|
|
201
|
+
}
|
|
202
|
+
/** Loads the article body for a single `EntryRef`. */
|
|
203
|
+
async loadArticle(entry, requestId) {
|
|
204
|
+
return await this.coordinator.call("loadArticle", [
|
|
205
|
+
{ request_id: this.allocRequestId(requestId), entry },
|
|
206
|
+
]);
|
|
207
|
+
}
|
|
208
|
+
/** Loads multiple article bodies in one batch call. */
|
|
209
|
+
async loadArticles(entries, requestId) {
|
|
210
|
+
return await this.coordinator.call("articles", [
|
|
211
|
+
{ request_id: this.allocRequestId(requestId), entries },
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
/** Loads raw bytes for a dictionary resource file (`styles.css`, images, etc.) or `null` if absent. */
|
|
215
|
+
async loadResource(snapshotId, path, requestId) {
|
|
216
|
+
return await this.coordinator.call("loadResource", [
|
|
217
|
+
{ request_id: this.allocRequestId(requestId), snapshot_id: snapshotId, path },
|
|
218
|
+
]);
|
|
219
|
+
}
|
|
220
|
+
/** Reads `index.json` metadata of a pinned snapshot. */
|
|
221
|
+
async metadata(snapshotId, requestId) {
|
|
222
|
+
return await this.coordinator.call("metadata", [
|
|
223
|
+
{ request_id: this.allocRequestId(requestId), snapshot_id: snapshotId },
|
|
224
|
+
]);
|
|
225
|
+
}
|
|
226
|
+
/** Fetches aggregated frequency, pitch, and IPA metadata for the requested `(term, reading)` keys. */
|
|
227
|
+
async termMeta(input) {
|
|
228
|
+
return await this.coordinator.call("termMeta", [
|
|
229
|
+
{ request_id: this.allocRequestId(input.request_id), keys: input.keys },
|
|
230
|
+
]);
|
|
231
|
+
}
|
|
232
|
+
/** Reads the tag bank definitions of a pinned snapshot. */
|
|
233
|
+
async tags(snapshotId, requestId) {
|
|
234
|
+
return await this.coordinator.call("tags", [
|
|
235
|
+
{ request_id: this.allocRequestId(requestId), snapshot_id: snapshotId },
|
|
236
|
+
]);
|
|
237
|
+
}
|
|
238
|
+
// ---------------------------------------------------------------------------
|
|
239
|
+
// Sudachi morphological analyzer (optional, progressive enhancement)
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
/**
|
|
242
|
+
* Downloads or reads a Sudachi `system.dic` (optionally `.gz`), caches it in OPFS (in OPFS mode),
|
|
243
|
+
* and streams it into WASM in 2 MiB chunks to minimize peak RAM usage.
|
|
244
|
+
*/
|
|
245
|
+
async installSudachi(source, options = {}) {
|
|
246
|
+
const { onProgress, ...serializableOpts } = options;
|
|
247
|
+
const forwardProgress = onProgress
|
|
248
|
+
? (p) => onProgress(p)
|
|
249
|
+
: undefined;
|
|
250
|
+
return await this.coordinator.call("installSudachi", [source, serializableOpts], forwardProgress);
|
|
251
|
+
}
|
|
252
|
+
/** Loads a previously cached Sudachi `system.dic` directly from OPFS without network traffic. */
|
|
253
|
+
async loadCachedSudachi(options) {
|
|
254
|
+
const forwardProgress = options?.onProgress
|
|
255
|
+
? (p) => options.onProgress?.(p)
|
|
256
|
+
: undefined;
|
|
257
|
+
return await this.coordinator.call("loadCachedSudachi", [], forwardProgress);
|
|
258
|
+
}
|
|
259
|
+
/** Unloads the Sudachi analyzer from WASM memory while keeping any OPFS cached file intact. */
|
|
260
|
+
async unloadSudachi() {
|
|
261
|
+
await this.coordinator.call("unloadSudachi");
|
|
262
|
+
}
|
|
263
|
+
/** Unloads Sudachi from memory and deletes the cached `system.dic` from OPFS. */
|
|
264
|
+
async deleteSudachi() {
|
|
265
|
+
await this.coordinator.call("deleteSudachi");
|
|
266
|
+
}
|
|
267
|
+
/** Returns `true` when a complete cached `system.dic` is stored in OPFS. */
|
|
268
|
+
async hasCachedSudachi() {
|
|
269
|
+
return await this.coordinator.call("hasCachedSudachi");
|
|
270
|
+
}
|
|
271
|
+
/** Closes the client, releasing OPFS locks/handles and allowing standby tabs to take over. */
|
|
272
|
+
async close() {
|
|
273
|
+
await this.coordinator.close();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAsC/C;;;;;;GAMG;AACH,MAAM,OAAO,SAAS;IACH,WAAW,CAAc;IAClC,aAAa,GAAG,EAAE,CAAC;IAE3B,YAAoB,WAAwB;QAC1C,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,UAAuB,EAAE;QACzC,MAAM,WAAW,GAAG,MAAM,WAAW,CAAC,IAAI,CAAC;YACzC,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,MAAM;YAClC,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,WAAW;YAC3C,eAAe,EAAE,OAAO,CAAC,eAAe;YACxC,eAAe,EAAE,OAAO,CAAC,eAAe;YACxC,SAAS,EAAE,OAAO,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AAW,CAAC,IAAI,CAChC,gBAAgB,EAChB,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAC1B,eAAe,CAChB,CAAC;IACJ,CAAC;IAED,iGAAiG;IACjG,KAAK,CAAC,iBAAiB,CAAC,OAEvB;QACC,MAAM,eAAe,GAAG,OAAO,EAAE,UAAU;YACzC,CAAC,CAAC,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,CAAoB,CAAC;YAC5D,CAAC,CAAC,SAAS,CAAC;QACd,OAAO,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAa,mBAAmB,EAAE,EAAE,EAAE,eAAe,CAAC,CAAC;IAC3F,CAAC;IAED,+FAA+F;IAC/F,KAAK,CAAC,aAAa;QACjB,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAO,eAAe,CAAC,CAAC;IACrD,CAAC;IAED,iFAAiF;IACjF,KAAK,CAAC,aAAa;QACjB,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAO,eAAe,CAAC,CAAC;IACrD,CAAC;IAED,4EAA4E;IAC5E,KAAK,CAAC,gBAAgB;QACpB,OAAO,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAU,kBAAkB,CAAC,CAAC;IAClE,CAAC;IAED,8FAA8F;IAC9F,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;IACjC,CAAC;CACF"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type ChangeEvent, type RpcMethod } from "./protocol.js";
|
|
2
|
+
export interface CoordinatorOptions {
|
|
3
|
+
readonly storage: "opfs" | "memory";
|
|
4
|
+
readonly directory: string;
|
|
5
|
+
readonly initialCapacity?: number;
|
|
6
|
+
readonly autoLoadSudachi?: boolean;
|
|
7
|
+
readonly workerUrl?: string | URL;
|
|
8
|
+
readonly wasmUrl?: string | URL;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Coordinates Worker ownership across tabs for OPFS `sahpool`.
|
|
12
|
+
*
|
|
13
|
+
* - In `"memory"` mode (or without Web Locks / `BroadcastChannel`), runs an isolated local Worker.
|
|
14
|
+
* - In `"opfs"` mode, elects one leader tab via `navigator.locks` to own the Dedicated Worker and
|
|
15
|
+
* proxies calls from follower tabs over `BroadcastChannel`. If the leader tab closes or crashes,
|
|
16
|
+
* a standby follower automatically acquires the lock, spawns a Worker, and replays idempotent reads.
|
|
17
|
+
*/
|
|
18
|
+
export declare class Coordinator {
|
|
19
|
+
readonly tabId: string;
|
|
20
|
+
private readonly opts;
|
|
21
|
+
private readonly lockName;
|
|
22
|
+
private readonly channelName;
|
|
23
|
+
private role;
|
|
24
|
+
private worker;
|
|
25
|
+
private workerReady;
|
|
26
|
+
private channel;
|
|
27
|
+
private releaseLock;
|
|
28
|
+
private standbyAbort;
|
|
29
|
+
private nextId;
|
|
30
|
+
private readonly localPending;
|
|
31
|
+
private readonly followerPending;
|
|
32
|
+
private readonly changeListeners;
|
|
33
|
+
private leaderReadyWaiters;
|
|
34
|
+
private activeLeaderId;
|
|
35
|
+
private constructor();
|
|
36
|
+
static open(opts: CoordinatorOptions): Promise<Coordinator>;
|
|
37
|
+
get isLeader(): boolean;
|
|
38
|
+
onChange(listener: (event: ChangeEvent) => void): () => void;
|
|
39
|
+
private emitChange;
|
|
40
|
+
private start;
|
|
41
|
+
/** Probes Web Locks without blocking; if available, holds the lock until `close()`. */
|
|
42
|
+
private tryAcquireLeadershipNow;
|
|
43
|
+
/** Enqueues a standby lock request so the browser wakes this tab if the leader dies. */
|
|
44
|
+
private startStandbyElection;
|
|
45
|
+
private promoteToLeader;
|
|
46
|
+
private waitForLeader;
|
|
47
|
+
private resolveLeaderWaiters;
|
|
48
|
+
private spawnWorker;
|
|
49
|
+
private spawnAndInitWorker;
|
|
50
|
+
private allocId;
|
|
51
|
+
private sendToWorker;
|
|
52
|
+
private handleWorkerMessage;
|
|
53
|
+
private rejectAllLocalPending;
|
|
54
|
+
private postBroadcast;
|
|
55
|
+
private handleBroadcast;
|
|
56
|
+
private proxyFollowerRequest;
|
|
57
|
+
/** Replays idempotent reads and fails non-idempotent writes after a leader change. */
|
|
58
|
+
private handleFailoverPendingCalls;
|
|
59
|
+
/**
|
|
60
|
+
* Executes an RPC method either directly on the local Worker (leader/isolated) or via
|
|
61
|
+
* `BroadcastChannel` (follower), wrapping raw large buffers as `Blob` across tabs.
|
|
62
|
+
*/
|
|
63
|
+
call<T>(method: RpcMethod, args?: unknown[], onProgress?: (progress: unknown) => void): Promise<T>;
|
|
64
|
+
/**
|
|
65
|
+
* Aborts a blocking import by terminating the Dedicated Worker, restarting it cleanly,
|
|
66
|
+
* and letting `HostInstaller::open_store` run garbage collection on partial snapshots.
|
|
67
|
+
*/
|
|
68
|
+
restartWorker(): Promise<void>;
|
|
69
|
+
/** Cleanly pauses OPFS handles, terminates any local Worker, and releases cross-tab locks. */
|
|
70
|
+
close(): Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
/** Converts large binary arguments to `Blob` for lighter structured cloning across tabs. */
|
|
73
|
+
export declare function prepareFollowerArgs(method: RpcMethod, args: unknown[]): unknown[];
|
|
74
|
+
//# sourceMappingURL=coordinator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinator.d.ts","sourceRoot":"","sources":["../src/coordinator.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,WAAW,EAEhB,KAAK,SAAS,EAIf,MAAM,eAAe,CAAC;AAcvB,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,QAAQ,CAAC;IACpC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;IACnC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;I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