@doxsync/core 0.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +193 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +9 -0
- package/dist/node.js +19 -0
- package/dist/runtime.js +85 -0
- package/dist/wasm/doxsync.d.ts +61 -0
- package/dist/wasm/doxsync.js +520 -0
- package/dist/wasm/doxsync_bg.wasm +0 -0
- package/dist/wasm/doxsync_bg.wasm.d.ts +18 -0
- package/examples/browser.css +110 -0
- package/examples/browser.html +94 -0
- package/examples/browser.js +347 -0
- package/examples/node.mjs +19 -0
- package/package.json +35 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Peterlits Zo
|
|
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
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# @doxsync/core
|
|
2
|
+
|
|
3
|
+
ESM bindings for the doxsync Rust synchronization engine, for modern browsers
|
|
4
|
+
and Node.js 22+. Rust owns documents, diffing, message encoding, decoding, and
|
|
5
|
+
state. JavaScript provides loading and a small synchronous API.
|
|
6
|
+
|
|
7
|
+
## Build from this repository
|
|
8
|
+
|
|
9
|
+
Install Rust, Node.js 22+, and `wasm-pack`, then run:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
rustup target add wasm32-unknown-unknown
|
|
13
|
+
cd doxsync-js
|
|
14
|
+
npm run build
|
|
15
|
+
node examples/node.mjs
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`npm run build` runs `wasm-pack` in release mode with the locked Cargo
|
|
19
|
+
dependencies and the `wasm` feature, then copies the JS wrapper and declarations
|
|
20
|
+
into `dist/`. There are no npm runtime dependencies. To create a local npm
|
|
21
|
+
tarball, run `npm pack`; it builds first and includes the WASM artifact.
|
|
22
|
+
Installing that tarball does not require Rust or wasm-pack.
|
|
23
|
+
|
|
24
|
+
The Rust crate continues to support native use without JS dependencies:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
cargo build --manifest-path ../doxsync-rs/Cargo.toml
|
|
28
|
+
cargo build --manifest-path ../doxsync-rs/Cargo.toml \
|
|
29
|
+
--target wasm32-unknown-unknown --features wasm --release
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The second command produces the Rust WASM artifact. Use `npm run build` to
|
|
33
|
+
also generate the JS glue required to load it.
|
|
34
|
+
|
|
35
|
+
## Synchronize a document
|
|
36
|
+
|
|
37
|
+
After installing the package from the local tarball:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import { init, Producer, Consumer } from "@doxsync/core";
|
|
41
|
+
|
|
42
|
+
await init();
|
|
43
|
+
const producer = new Producer({ count: 1n, ratio: 0.5 }, [1]);
|
|
44
|
+
const consumer = new Consumer();
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
consumer.consumeDiff(producer.produceDiff()); // First message is a snapshot.
|
|
48
|
+
producer.replace({ count: 2n, ratio: 0.75 });
|
|
49
|
+
|
|
50
|
+
const message = producer.produceDiff();
|
|
51
|
+
if (message !== undefined) consumer.consumeDiff(message);
|
|
52
|
+
|
|
53
|
+
console.log(consumer.document()); // { count: 2n, ratio: 0.75 }
|
|
54
|
+
console.log(producer.produceDiff()); // undefined: no changes
|
|
55
|
+
} finally {
|
|
56
|
+
producer.free();
|
|
57
|
+
consumer.free();
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Each `consumeDiff` call takes one complete `Uint8Array` message. Transport is
|
|
62
|
+
up to the application. Messages must be delivered reliably, exactly once, and
|
|
63
|
+
in order to the corresponding consumer: the producer advances its baseline
|
|
64
|
+
when it returns a message. Retain those bytes until delivery; calling
|
|
65
|
+
`produceDiff` again does not retransmit them. For a new connection without
|
|
66
|
+
shared state, create a new producer and consumer and start with a snapshot.
|
|
67
|
+
|
|
68
|
+
`replace` copies and validates the input before changing the producer. It
|
|
69
|
+
does not emit a message. `document()` returns `undefined` before a snapshot,
|
|
70
|
+
and an independent JS value afterwards (including `null` for a null document).
|
|
71
|
+
Mutating an input, returned document, or returned message does not change the
|
|
72
|
+
engine's stored document. Invalid messages leave consumer state unchanged.
|
|
73
|
+
|
|
74
|
+
Call `free()` when finished to release WASM resources promptly. It is
|
|
75
|
+
idempotent; business methods throw after release. The generated bindings also
|
|
76
|
+
provide garbage-collection cleanup, but its timing is not deterministic.
|
|
77
|
+
|
|
78
|
+
## WASM initialization
|
|
79
|
+
|
|
80
|
+
Call and await `init()` before creating either class. Initialization is the
|
|
81
|
+
only asynchronous operation. Concurrent calls share the first call's promise
|
|
82
|
+
and source; successful initialization is reused, and failed initialization
|
|
83
|
+
can be retried.
|
|
84
|
+
|
|
85
|
+
- Browsers fetch the WASM next to the generated JS. Serve `dist/` over HTTP;
|
|
86
|
+
retain the `dist/wasm/` directory and serve `.wasm` as `application/wasm`.
|
|
87
|
+
- Node.js uses the package's conditional export to read the same packaged
|
|
88
|
+
WASM from disk. Browser entry points contain no Node.js imports.
|
|
89
|
+
- `init(source)` also accepts a URL string, `URL`, `ArrayBuffer`, `Uint8Array`,
|
|
90
|
+
or compiled `WebAssembly.Module`. Strings are URLs, not filesystem paths.
|
|
91
|
+
Node.js additionally reads `file:` URLs from disk.
|
|
92
|
+
- Bundlers must deploy the WASM asset. If their asset handling changes its
|
|
93
|
+
location, pass the deployed URL or bytes explicitly; the package exports
|
|
94
|
+
the asset as `@doxsync/core/doxsync.wasm` for resolution by build tools.
|
|
95
|
+
|
|
96
|
+
For a browser example, serve this directory and open `/examples/browser.html`:
|
|
97
|
+
|
|
98
|
+
```sh
|
|
99
|
+
python3 -m http.server 8000
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The browser demo changes the producer randomly every 1.5 seconds and delivers
|
|
103
|
+
messages after a simulated 1-second delay. It shows both documents, highlights
|
|
104
|
+
updated lines, and lists the actual message bytes and delivery status. Adjust
|
|
105
|
+
the delay or update interval, pause automatic changes, step manually, or reset
|
|
106
|
+
the session. Pausing lets queued messages finish; changing latency never
|
|
107
|
+
reorders messages. Some iterations deliberately leave the document unchanged
|
|
108
|
+
to demonstrate that no message is produced.
|
|
109
|
+
|
|
110
|
+
Browsers must support WebAssembly, BigInt, ESM, and private class fields. No
|
|
111
|
+
CommonJS entry point or polyfills are supplied.
|
|
112
|
+
|
|
113
|
+
## Values and errors
|
|
114
|
+
|
|
115
|
+
| JavaScript | doxsync |
|
|
116
|
+
| --- | --- |
|
|
117
|
+
| `null`, `boolean`, `string` | Null, Bool, UTF-8 text |
|
|
118
|
+
| `number` | Float, including `-0`, NaN, and infinities |
|
|
119
|
+
| `bigint` | Int in `[-2^64, 2^64 - 1]` |
|
|
120
|
+
| `Uint8Array` (including Node Buffer) | Binary string, copied |
|
|
121
|
+
| Dense arrays | Arrays, copied recursively |
|
|
122
|
+
| Plain or null-prototype objects | Maps with string keys |
|
|
123
|
+
|
|
124
|
+
`42` and `42n` are different document values. Integers from Rust always read
|
|
125
|
+
back as `bigint`, including small integers. Floating-point NaN values remain
|
|
126
|
+
NaN, but JS does not promise to preserve NaN payload bits.
|
|
127
|
+
|
|
128
|
+
Maps use own enumerable string properties, not inherited or non-enumerable
|
|
129
|
+
properties. Output maps are ordinary objects; keys such as `__proto__` are
|
|
130
|
+
defined as safe own data properties. Arrays serialize their indexed elements;
|
|
131
|
+
extra named properties are not part of the document. Object identity and
|
|
132
|
+
insertion order are not preserved. Shared references are copied by value.
|
|
133
|
+
|
|
134
|
+
Unsupported values are rejected: `undefined`, functions, symbols (including
|
|
135
|
+
enumerable symbol map keys), sparse arrays, cyclic references, accessor
|
|
136
|
+
properties used as document values, out-of-range integers, unpaired UTF-16
|
|
137
|
+
surrogates in strings or keys, and objects such as Date, Map, Set, or class
|
|
138
|
+
instances. Use values from the same JS realm as the library; custom prototypes
|
|
139
|
+
and proxies are not supported as a document model.
|
|
140
|
+
|
|
141
|
+
Rust and conversion errors are JS `Error` instances with `kind` equal to
|
|
142
|
+
`Internal`, `InvalidData`, or `UnexpectedType`. Initialization, lifecycle,
|
|
143
|
+
and host JavaScript exceptions may be ordinary errors without `kind`.
|
|
144
|
+
TypeScript declarations include `SyncValue`, `InitSource`, and `DoxsyncError`.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
try {
|
|
148
|
+
consumer.consumeDiff(receivedBytes);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.error(error.kind, error.message);
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The JS package exposes only `init`, `Producer`, and `Consumer` at runtime.
|
|
155
|
+
Rust's lower-level Document, Value, and Message APIs remain internal to this
|
|
156
|
+
binding, and the wire protocol is shared with native Rust.
|
|
157
|
+
|
|
158
|
+
### Protocol negotiation
|
|
159
|
+
|
|
160
|
+
The second `Producer` constructor argument is required: pass an array of protocol
|
|
161
|
+
versions supported by the consumer, such as `[1]`. Versions must be integers in
|
|
162
|
+
`[0, 2^32 - 1]`. The producer selects the highest version supported by both peers;
|
|
163
|
+
order and duplicates do not matter. Currently only version `1` is implemented.
|
|
164
|
+
Empty or incompatible lists, omitted arguments, and invalid values throw an error
|
|
165
|
+
with `kind: "InvalidData"`.
|
|
166
|
+
|
|
167
|
+
The first packed message starts its metadata with protocol instruction `2` followed
|
|
168
|
+
by the selected version, currently `1`; both are unsigned variable-length integers.
|
|
169
|
+
Later messages omit this declaration and retain the same protocol for the stream.
|
|
170
|
+
The consumer rejects first messages without a leading protocol declaration,
|
|
171
|
+
unsupported versions, and repeated declarations. Legacy messages without version
|
|
172
|
+
metadata are no longer accepted. Both peers must be upgraded together.
|
|
173
|
+
|
|
174
|
+
Failed encoding or consumption preserves protocol initialization state as well as
|
|
175
|
+
pool state, allowing retries. Keep and deliver packed messages in order as usual.
|
|
176
|
+
|
|
177
|
+
### Path key encoding
|
|
178
|
+
|
|
179
|
+
Protocol v1 path definitions use the low two bits of each segment's unsigned
|
|
180
|
+
variable-length integer as a tag: `0b00` carries a UTF-8 byte length followed by
|
|
181
|
+
the key bytes, `0b01` carries an array index, and `0b10` carries a string pool
|
|
182
|
+
index. The payload is shifted left by two bits before adding the tag; `0b11`
|
|
183
|
+
is reserved and rejected.
|
|
184
|
+
|
|
185
|
+
A cached key uses `0b10` only when its reference is strictly shorter than its
|
|
186
|
+
literal encoding. Uncached keys remain inline without adding string pool entries.
|
|
187
|
+
The initial snapshot populates the string pool, so subsequent path definitions
|
|
188
|
+
can reuse those keys. Once a whole path is cached, actions reuse its path pool ID.
|
|
189
|
+
String pool updates precede path definitions; referenced strings stay resident
|
|
190
|
+
through message encoding and are resolved when the consumer reads the definition.
|
|
191
|
+
|
|
192
|
+
This extends v1 directly. Both peers must be upgraded together: older decoders
|
|
193
|
+
reject the new `0b10` path segment tag.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** Numbers are floats; bigint values are integers in [-2^64, 2^64 - 1]. */
|
|
2
|
+
export type SyncValue =
|
|
3
|
+
| null
|
|
4
|
+
| boolean
|
|
5
|
+
| string
|
|
6
|
+
| number
|
|
7
|
+
| bigint
|
|
8
|
+
| Uint8Array
|
|
9
|
+
| SyncValue[]
|
|
10
|
+
| { [key: string]: SyncValue };
|
|
11
|
+
|
|
12
|
+
export type InitSource =
|
|
13
|
+
| string
|
|
14
|
+
| URL
|
|
15
|
+
| ArrayBuffer
|
|
16
|
+
| Uint8Array
|
|
17
|
+
| WebAssembly.Module;
|
|
18
|
+
|
|
19
|
+
/** Rust errors thrown by synchronization and value conversion. */
|
|
20
|
+
export interface DoxsyncError extends Error {
|
|
21
|
+
kind: "Internal" | "InvalidData" | "UnexpectedType";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Initialize once before constructing Producer or Consumer.
|
|
26
|
+
* Defaults to the packaged WASM. Concurrent calls share the first call's source
|
|
27
|
+
* and promise; failures can be retried. Later successful calls are no-ops.
|
|
28
|
+
* Strings are URLs, not filesystem paths; Node also accepts file: URLs.
|
|
29
|
+
*/
|
|
30
|
+
export function init(source?: InitSource): Promise<void>;
|
|
31
|
+
|
|
32
|
+
export class Producer {
|
|
33
|
+
#private;
|
|
34
|
+
/**
|
|
35
|
+
* Copies the value into WASM and selects the highest common protocol version.
|
|
36
|
+
* protocols lists the consumer's supported versions as u32 integers.
|
|
37
|
+
* Currently only 1 is supported. Missing, invalid, empty, or incompatible
|
|
38
|
+
* lists throw InvalidData errors. Order and duplicates do not matter.
|
|
39
|
+
*/
|
|
40
|
+
constructor(value: SyncValue, protocols: readonly number[]);
|
|
41
|
+
/** Replaces the document after validating and copying the entire value. */
|
|
42
|
+
replace(value: SyncValue): void;
|
|
43
|
+
/**
|
|
44
|
+
* Returns a snapshot with protocol metadata on the first call, then changes.
|
|
45
|
+
* Returns undefined if unchanged. Retain each message until delivered in order.
|
|
46
|
+
*/
|
|
47
|
+
produceDiff(): Uint8Array | undefined;
|
|
48
|
+
/** Release WASM resources. Safe to call repeatedly. */
|
|
49
|
+
free(): void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class Consumer {
|
|
53
|
+
#private;
|
|
54
|
+
constructor();
|
|
55
|
+
/**
|
|
56
|
+
* Applies one complete message. The first message must declare protocol 1.
|
|
57
|
+
* Failures leave the document, pools, and protocol state intact.
|
|
58
|
+
*/
|
|
59
|
+
consumeDiff(bytes: Uint8Array): void;
|
|
60
|
+
/** Returns an independent copy, or undefined before the first snapshot. */
|
|
61
|
+
document(): SyncValue | undefined;
|
|
62
|
+
/** Release WASM resources. Safe to call repeatedly. */
|
|
63
|
+
free(): void;
|
|
64
|
+
}
|
package/dist/index.js
ADDED
package/dist/node.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { initialize } from "./runtime.js";
|
|
3
|
+
|
|
4
|
+
export { Producer, Consumer } from "./runtime.js";
|
|
5
|
+
|
|
6
|
+
export function init(source) {
|
|
7
|
+
return initialize(() => {
|
|
8
|
+
if (source === undefined) {
|
|
9
|
+
return readFile(new URL("./wasm/doxsync_bg.wasm", import.meta.url));
|
|
10
|
+
}
|
|
11
|
+
if (source instanceof URL && source.protocol === "file:") {
|
|
12
|
+
return readFile(source);
|
|
13
|
+
}
|
|
14
|
+
if (typeof source === "string" && source.startsWith("file:")) {
|
|
15
|
+
return readFile(new URL(source));
|
|
16
|
+
}
|
|
17
|
+
return source;
|
|
18
|
+
});
|
|
19
|
+
}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import initWasm, {
|
|
2
|
+
Producer as WasmProducer,
|
|
3
|
+
Consumer as WasmConsumer,
|
|
4
|
+
} from "./wasm/doxsync.js";
|
|
5
|
+
|
|
6
|
+
let initialization;
|
|
7
|
+
let initialized = false;
|
|
8
|
+
|
|
9
|
+
export function initialize(loadSource) {
|
|
10
|
+
if (!initialization) {
|
|
11
|
+
initialization = Promise.resolve()
|
|
12
|
+
.then(loadSource)
|
|
13
|
+
.then((source) => initWasm({ module_or_path: source }))
|
|
14
|
+
.then(() => { initialized = true; })
|
|
15
|
+
.catch((error) => {
|
|
16
|
+
initialization = undefined;
|
|
17
|
+
throw error;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return initialization;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function requireInitialized() {
|
|
24
|
+
if (!initialized) {
|
|
25
|
+
throw new Error("Call and await init() before creating a doxsync object");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class Producer {
|
|
30
|
+
#handle;
|
|
31
|
+
|
|
32
|
+
constructor(value, protocols) {
|
|
33
|
+
requireInitialized();
|
|
34
|
+
this.#handle = new WasmProducer(value, protocols);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#live() {
|
|
38
|
+
if (!this.#handle) throw new Error("Producer has been freed");
|
|
39
|
+
return this.#handle;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
replace(value) {
|
|
43
|
+
this.#live().replace(value);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
produceDiff() {
|
|
47
|
+
return this.#live().produceDiff();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
free() {
|
|
51
|
+
if (this.#handle) {
|
|
52
|
+
this.#handle.free();
|
|
53
|
+
this.#handle = undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export class Consumer {
|
|
59
|
+
#handle;
|
|
60
|
+
|
|
61
|
+
constructor() {
|
|
62
|
+
requireInitialized();
|
|
63
|
+
this.#handle = new WasmConsumer();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#live() {
|
|
67
|
+
if (!this.#handle) throw new Error("Consumer has been freed");
|
|
68
|
+
return this.#handle;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
consumeDiff(bytes) {
|
|
72
|
+
this.#live().consumeDiff(bytes);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
document() {
|
|
76
|
+
return this.#live().document();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
free() {
|
|
80
|
+
if (this.#handle) {
|
|
81
|
+
this.#handle.free();
|
|
82
|
+
this.#handle = undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export class Consumer {
|
|
5
|
+
free(): void;
|
|
6
|
+
[Symbol.dispose](): void;
|
|
7
|
+
consumeDiff(bytes: any): void;
|
|
8
|
+
document(): any;
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class Producer {
|
|
13
|
+
free(): void;
|
|
14
|
+
[Symbol.dispose](): void;
|
|
15
|
+
constructor(value: any, protocols: any);
|
|
16
|
+
produceDiff(): any;
|
|
17
|
+
replace(value: any): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
21
|
+
|
|
22
|
+
export interface InitOutput {
|
|
23
|
+
readonly memory: WebAssembly.Memory;
|
|
24
|
+
readonly __wbg_consumer_free: (a: number, b: number) => void;
|
|
25
|
+
readonly __wbg_producer_free: (a: number, b: number) => void;
|
|
26
|
+
readonly consumer_consumeDiff: (a: number, b: any) => [number, number];
|
|
27
|
+
readonly consumer_document: (a: number) => [number, number, number];
|
|
28
|
+
readonly consumer_new: () => number;
|
|
29
|
+
readonly producer_new: (a: any, b: any) => [number, number, number];
|
|
30
|
+
readonly producer_produceDiff: (a: number) => [number, number, number];
|
|
31
|
+
readonly producer_replace: (a: number, b: any) => [number, number];
|
|
32
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
33
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
34
|
+
readonly __wbindgen_exn_store: (a: number) => void;
|
|
35
|
+
readonly __externref_table_alloc: () => number;
|
|
36
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
37
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
38
|
+
readonly __wbindgen_start: () => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
45
|
+
* a precompiled `WebAssembly.Module`.
|
|
46
|
+
*
|
|
47
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
48
|
+
*
|
|
49
|
+
* @returns {InitOutput}
|
|
50
|
+
*/
|
|
51
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
55
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
56
|
+
*
|
|
57
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
58
|
+
*
|
|
59
|
+
* @returns {Promise<InitOutput>}
|
|
60
|
+
*/
|
|
61
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|