@cellrune/node 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/wrapper.js ADDED
@@ -0,0 +1,282 @@
1
+ "use strict";
2
+
3
+ const native = require("./native.js");
4
+ const { serializeWorkbookChange } = require("./lib/changes.js");
5
+ const {
6
+ CellRuneError,
7
+ closedError,
8
+ inputError,
9
+ withErrors,
10
+ withSyncErrors,
11
+ } = require("./lib/errors.js");
12
+ const {
13
+ normalizeCalculationDelta,
14
+ normalizeCalculationDeltaPage,
15
+ normalizeCalculationReport,
16
+ normalizeEditReceipt,
17
+ normalizeFunctionUsage,
18
+ normalizeRangePage,
19
+ normalizeSummary,
20
+ normalizeWriteReport,
21
+ } = require("./lib/normalization.js");
22
+ const {
23
+ requireFinite,
24
+ requireNonNegativeInteger,
25
+ requireOptionalBoolean,
26
+ requireOptionalFinite,
27
+ requireOptions,
28
+ requireString,
29
+ requireU64BigInt,
30
+ } = require("./lib/validation.js");
31
+
32
+ class Workbook {
33
+ #native;
34
+
35
+ constructor(nativeWorkbook) {
36
+ this.#native = nativeWorkbook;
37
+ }
38
+
39
+ static create() {
40
+ return new Workbook(native.createWorkbook());
41
+ }
42
+
43
+ static async openPath(path) {
44
+ requireString(path, "path");
45
+ return new Workbook(await withErrors(native.openWorkbookPath(path)));
46
+ }
47
+
48
+ static async fromBytes(bytes) {
49
+ if (!(bytes instanceof Uint8Array)) {
50
+ throw inputError("bytes must be a Buffer or Uint8Array");
51
+ }
52
+ return new Workbook(
53
+ await withErrors(native.openWorkbookBytes(Buffer.from(bytes))),
54
+ );
55
+ }
56
+
57
+ get closed() {
58
+ return this.#native === null;
59
+ }
60
+
61
+ close() {
62
+ const session = this.#native;
63
+ if (session === null) {
64
+ return;
65
+ }
66
+ withSyncErrors(() => session.close());
67
+ this.#native = null;
68
+ }
69
+
70
+ summary() {
71
+ return normalizeSummary(withSyncErrors(() => this.#session().summary()));
72
+ }
73
+
74
+ readRange(sheet, start, end, options = {}) {
75
+ requireString(sheet, "sheet");
76
+ requireString(start, "start");
77
+ requireString(end, "end");
78
+ requireOptions(options);
79
+ const offset = options.offset ?? 0;
80
+ const limit = options.limit ?? 0;
81
+ requireNonNegativeInteger(offset, "offset");
82
+ requireNonNegativeInteger(limit, "limit");
83
+ return normalizeRangePage(
84
+ withSyncErrors(() =>
85
+ this.#session().readRange(sheet, start, end, offset, limit),
86
+ ),
87
+ );
88
+ }
89
+
90
+ functionUsage() {
91
+ return normalizeFunctionUsage(
92
+ withSyncErrors(() => this.#session().functionUsage()),
93
+ );
94
+ }
95
+
96
+ async calculate(options = {}) {
97
+ requireOptions(options);
98
+ requireOptionalFinite(options.todaySerial, "todaySerial");
99
+ requireOptionalFinite(options.nowSerial, "nowSerial");
100
+ const task = withSyncErrors(() =>
101
+ this.#session().calculate(
102
+ options.todaySerial ?? null,
103
+ options.nowSerial ?? null,
104
+ ),
105
+ );
106
+ return normalizeCalculationReport(await withErrors(task));
107
+ }
108
+
109
+ async recalculate(options = {}) {
110
+ requireOptions(options);
111
+ const mode = options.mode ?? "auto";
112
+ if (!["auto", "incremental", "full"].includes(mode)) {
113
+ throw inputError("mode must be auto, incremental, or full");
114
+ }
115
+ requireOptionalFinite(options.todaySerial, "todaySerial");
116
+ requireOptionalFinite(options.nowSerial, "nowSerial");
117
+ const task = withSyncErrors(() =>
118
+ this.#session().recalculate(
119
+ mode,
120
+ options.todaySerial ?? null,
121
+ options.nowSerial ?? null,
122
+ ),
123
+ );
124
+ return normalizeCalculationDelta(await withErrors(task));
125
+ }
126
+
127
+ applyChanges(expectedRevision, changes) {
128
+ requireU64BigInt(expectedRevision, "expectedRevision");
129
+ if (!Array.isArray(changes) || changes.length === 0) {
130
+ throw inputError("changes must be a non-empty array");
131
+ }
132
+ const payload = {
133
+ changes: changes.map((change, index) =>
134
+ serializeWorkbookChange(change, index),
135
+ ),
136
+ };
137
+ return normalizeEditReceipt(
138
+ withSyncErrors(() =>
139
+ this.#session().applyChanges(
140
+ expectedRevision.toString(),
141
+ JSON.stringify(payload),
142
+ ),
143
+ ),
144
+ );
145
+ }
146
+
147
+ changesSince(cursor = 0n, options = {}) {
148
+ requireU64BigInt(cursor, "cursor");
149
+ requireOptions(options);
150
+ const limit = options.limit ?? 0;
151
+ requireNonNegativeInteger(limit, "limit");
152
+ return normalizeCalculationDeltaPage(
153
+ withSyncErrors(() =>
154
+ this.#session().changesSince(cursor.toString(), limit),
155
+ ),
156
+ );
157
+ }
158
+
159
+ cancelCalculation() {
160
+ return withSyncErrors(() => this.#session().cancelCalculation());
161
+ }
162
+
163
+ calculationActive() {
164
+ return withSyncErrors(() => this.#session().calculationActive());
165
+ }
166
+
167
+ setBlank(sheet, address) {
168
+ this.#edit(sheet, address, () => this.#session().setBlank(sheet, address));
169
+ }
170
+
171
+ setNumber(sheet, address, value) {
172
+ requireFinite(value, "value");
173
+ this.#edit(sheet, address, () =>
174
+ this.#session().setNumber(sheet, address, value),
175
+ );
176
+ }
177
+
178
+ setText(sheet, address, value) {
179
+ requireString(value, "value");
180
+ this.#edit(sheet, address, () =>
181
+ this.#session().setText(sheet, address, value),
182
+ );
183
+ }
184
+
185
+ setLogical(sheet, address, value) {
186
+ if (typeof value !== "boolean") {
187
+ throw inputError("value must be a boolean");
188
+ }
189
+ this.#edit(sheet, address, () =>
190
+ this.#session().setLogical(sheet, address, value),
191
+ );
192
+ }
193
+
194
+ setError(sheet, address, value) {
195
+ requireString(value, "value");
196
+ this.#edit(sheet, address, () =>
197
+ this.#session().setError(sheet, address, value),
198
+ );
199
+ }
200
+
201
+ setFormula(sheet, address, formula, options = {}) {
202
+ requireString(formula, "formula");
203
+ requireOptions(options);
204
+ if (options.dynamicRange !== undefined) {
205
+ requireString(options.dynamicRange, "dynamicRange");
206
+ }
207
+ this.#edit(sheet, address, () =>
208
+ this.#session().setFormula(
209
+ sheet,
210
+ address,
211
+ formula,
212
+ options.dynamicRange ?? null,
213
+ ),
214
+ );
215
+ }
216
+
217
+ clearCell(sheet, address) {
218
+ requireString(sheet, "sheet");
219
+ requireString(address, "address");
220
+ return withSyncErrors(() => this.#session().clearCell(sheet, address));
221
+ }
222
+
223
+ addSheet(name) {
224
+ requireString(name, "name");
225
+ return withSyncErrors(() => this.#session().addSheet(name));
226
+ }
227
+
228
+ renameSheet(currentName, newName) {
229
+ requireString(currentName, "currentName");
230
+ requireString(newName, "newName");
231
+ withSyncErrors(() => this.#session().renameSheet(currentName, newName));
232
+ }
233
+
234
+ async toBytes(options = {}) {
235
+ requireOptions(options);
236
+ requireOptionalBoolean(
237
+ options.invalidateUnavailable,
238
+ "invalidateUnavailable",
239
+ );
240
+ const task = withSyncErrors(() =>
241
+ this.#session().toBytes(options.invalidateUnavailable ?? false),
242
+ );
243
+ return withErrors(task);
244
+ }
245
+
246
+ async save(path, options = {}) {
247
+ requireString(path, "path");
248
+ requireOptions(options);
249
+ requireOptionalBoolean(
250
+ options.invalidateUnavailable,
251
+ "invalidateUnavailable",
252
+ );
253
+ requireOptionalBoolean(options.replaceExisting, "replaceExisting");
254
+ const task = withSyncErrors(() =>
255
+ this.#session().savePath(
256
+ path,
257
+ options.invalidateUnavailable ?? false,
258
+ options.replaceExisting ?? false,
259
+ ),
260
+ );
261
+ return normalizeWriteReport(await withErrors(task));
262
+ }
263
+
264
+ #session() {
265
+ if (this.#native === null) {
266
+ throw closedError();
267
+ }
268
+ return this.#native;
269
+ }
270
+
271
+ #edit(sheet, address, operation) {
272
+ requireString(sheet, "sheet");
273
+ requireString(address, "address");
274
+ withSyncErrors(operation);
275
+ }
276
+ }
277
+
278
+ module.exports = {
279
+ SCHEMA_VERSION: native.schemaVersion(),
280
+ CellRuneError,
281
+ Workbook,
282
+ };