@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.
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+
3
+ const { protocolError } = require("./errors.js");
4
+ const {
5
+ requireObject,
6
+ requireProtocolFinite,
7
+ requireProtocolString,
8
+ } = require("./validation.js");
9
+
10
+ function normalizeValue(value) {
11
+ requireObject(value, "cell value");
12
+ switch (value.kind) {
13
+ case "blank":
14
+ case "unsupported":
15
+ return { kind: value.kind };
16
+ case "number":
17
+ requireProtocolFinite(value.numberValue, "native number value");
18
+ return { kind: "number", value: value.numberValue };
19
+ case "text":
20
+ requireProtocolString(value.textValue, "native text value");
21
+ return { kind: "text", value: value.textValue };
22
+ case "logical":
23
+ if (typeof value.logicalValue !== "boolean") {
24
+ throw protocolError("native logical value is missing");
25
+ }
26
+ return { kind: "logical", value: value.logicalValue };
27
+ case "error":
28
+ requireProtocolString(value.errorValue, "native error value");
29
+ return { kind: "error", value: value.errorValue };
30
+ default:
31
+ throw protocolError("native cell value kind is unknown");
32
+ }
33
+ }
34
+
35
+ function normalizeResult(result) {
36
+ if (result === null || result === undefined) {
37
+ return null;
38
+ }
39
+ requireObject(result, "calculation result");
40
+ if (result.kind === "value" && result.value) {
41
+ return { kind: "value", value: normalizeValue(result.value) };
42
+ }
43
+ if (
44
+ result.kind === "unavailable" &&
45
+ typeof result.code === "string" &&
46
+ typeof result.message === "string"
47
+ ) {
48
+ return {
49
+ kind: "unavailable",
50
+ code: result.code,
51
+ message: result.message,
52
+ detail: result.detail ?? null,
53
+ };
54
+ }
55
+ throw protocolError("native calculation result is malformed");
56
+ }
57
+
58
+ function normalizeRangePage(page) {
59
+ requireObject(page, "range page");
60
+ return {
61
+ schemaVersion: page.schemaVersion,
62
+ sheet: page.sheet,
63
+ start: page.start,
64
+ end: page.end,
65
+ totalCells: page.totalCells,
66
+ offset: page.offset,
67
+ nextOffset: page.nextOffset ?? null,
68
+ cells: page.cells.map((cell) => ({
69
+ address: cell.address,
70
+ formula: cell.formula ?? null,
71
+ sourceValue: normalizeValue(cell.sourceValue),
72
+ sourceValueState: cell.sourceValueState,
73
+ calculated: normalizeResult(cell.calculated),
74
+ })),
75
+ };
76
+ }
77
+
78
+ function normalizeSummary(summary) {
79
+ requireObject(summary, "workbook summary");
80
+ return {
81
+ schemaVersion: summary.schemaVersion,
82
+ semanticRevision: BigInt(summary.semanticRevision),
83
+ documentBacked: summary.documentBacked,
84
+ documentKind: summary.documentKind,
85
+ dateSystem: summary.dateSystem,
86
+ diagnosticCount: summary.diagnosticCount,
87
+ sheets: summary.sheets.map((sheet) => ({
88
+ id: sheet.id,
89
+ name: sheet.name,
90
+ visibility: sheet.visibility,
91
+ cellCount: sheet.cellCount,
92
+ usedRange: sheet.usedRange ?? null,
93
+ })),
94
+ };
95
+ }
96
+
97
+ function normalizeCalculationReport(report) {
98
+ return {
99
+ schemaVersion: report.schemaVersion,
100
+ semanticRevision: BigInt(report.semanticRevision),
101
+ formulaCount: report.formulaCount,
102
+ valueCount: report.valueCount,
103
+ unavailableCount: report.unavailableCount,
104
+ materializedCellCount: report.materializedCellCount,
105
+ };
106
+ }
107
+
108
+ function normalizeCalculationDelta(delta) {
109
+ requireObject(delta, "calculation delta");
110
+ return {
111
+ schemaVersion: delta.schemaVersion,
112
+ cursor: BigInt(delta.cursor),
113
+ baseRevision: BigInt(delta.baseRevision),
114
+ resultRevision: BigInt(delta.resultRevision),
115
+ mode: delta.mode,
116
+ reason: delta.reason,
117
+ dirtyCount: delta.dirtyCount,
118
+ evaluatedCount: delta.evaluatedCount,
119
+ parsedFormulaCount: delta.parsedFormulaCount,
120
+ changedCells: delta.changedCells.map((change) => ({
121
+ cell: normalizeCellReference(change.cell),
122
+ origin: change.origin,
123
+ anchor:
124
+ change.anchor === null || change.anchor === undefined
125
+ ? null
126
+ : normalizeCellReference(change.anchor),
127
+ range: change.range ?? null,
128
+ result: normalizeResult(change.result),
129
+ })),
130
+ removedMaterializedCells: delta.removedMaterializedCells.map(
131
+ normalizeCellReference,
132
+ ),
133
+ };
134
+ }
135
+
136
+ function normalizeCalculationDeltaPage(page) {
137
+ requireObject(page, "calculation delta page");
138
+ return {
139
+ schemaVersion: page.schemaVersion,
140
+ requestedCursor: BigInt(page.requestedCursor),
141
+ nextCursor:
142
+ page.nextCursor === null || page.nextCursor === undefined
143
+ ? null
144
+ : BigInt(page.nextCursor),
145
+ deltas: page.deltas.map(normalizeCalculationDelta),
146
+ };
147
+ }
148
+
149
+ function normalizeEditReceipt(receipt) {
150
+ requireObject(receipt, "edit receipt");
151
+ return {
152
+ schemaVersion: receipt.schemaVersion,
153
+ baseRevision: BigInt(receipt.baseRevision),
154
+ resultRevision: BigInt(receipt.resultRevision),
155
+ appliedChangeCount: receipt.appliedChangeCount,
156
+ changedCells: receipt.changedCells.map(normalizeCellReference),
157
+ calculationChangedCells: receipt.calculationChangedCells.map(
158
+ normalizeCellReference,
159
+ ),
160
+ createdSheetIds: receipt.createdSheetIds,
161
+ topologyChanged: receipt.topologyChanged,
162
+ calculationMetadataChanged: receipt.calculationMetadataChanged,
163
+ };
164
+ }
165
+
166
+ function normalizeFunctionUsage(report) {
167
+ return {
168
+ schemaVersion: report.schemaVersion,
169
+ formulaCount: report.formulaCount,
170
+ parsedFormulaCount: report.parsedFormulaCount,
171
+ unparsedFormulaCount: report.unparsedFormulaCount,
172
+ entries: report.entries.map((entry) => ({
173
+ name: entry.name,
174
+ supported: entry.supported,
175
+ callCount: entry.callCount,
176
+ formulaCount: entry.formulaCount,
177
+ sampleCells: entry.sampleCells.map(normalizeCellReference),
178
+ })),
179
+ };
180
+ }
181
+
182
+ function normalizeWriteReport(report) {
183
+ return {
184
+ schemaVersion: report.schemaVersion,
185
+ complete: report.complete,
186
+ policy: report.policy,
187
+ materializedCount: report.materializedCount,
188
+ invalidatedCells: report.invalidatedCells.map(normalizeCellReference),
189
+ changedParts: report.changedParts,
190
+ removedParts: report.removedParts,
191
+ diagnosticCount: report.diagnosticCount,
192
+ };
193
+ }
194
+
195
+ function normalizeCellReference(cell) {
196
+ return {
197
+ sheetId: cell.sheetId,
198
+ sheetName: cell.sheetName,
199
+ address: cell.address,
200
+ };
201
+ }
202
+
203
+ module.exports = {
204
+ normalizeCalculationDelta,
205
+ normalizeCalculationDeltaPage,
206
+ normalizeCalculationReport,
207
+ normalizeEditReceipt,
208
+ normalizeFunctionUsage,
209
+ normalizeRangePage,
210
+ normalizeSummary,
211
+ normalizeWriteReport,
212
+ };
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ const { inputError, protocolError } = require("./errors.js");
4
+
5
+ function requireObject(value, name) {
6
+ if (value === null || typeof value !== "object") {
7
+ throw protocolError(`${name} must be an object`);
8
+ }
9
+ }
10
+
11
+ function requireString(value, name) {
12
+ if (typeof value !== "string") {
13
+ throw inputError(`${name} must be a string`);
14
+ }
15
+ }
16
+
17
+ function requireFinite(value, name) {
18
+ if (typeof value !== "number" || !Number.isFinite(value)) {
19
+ throw inputError(`${name} must be a finite number`);
20
+ }
21
+ }
22
+
23
+ function requireProtocolFinite(value, name) {
24
+ if (typeof value !== "number" || !Number.isFinite(value)) {
25
+ throw protocolError(`${name} must be a finite number`);
26
+ }
27
+ }
28
+
29
+ function requireProtocolString(value, name) {
30
+ if (typeof value !== "string") {
31
+ throw protocolError(`${name} must be a string`);
32
+ }
33
+ }
34
+
35
+ function requireOptionalFinite(value, name) {
36
+ if (value !== undefined) {
37
+ requireFinite(value, name);
38
+ }
39
+ }
40
+
41
+ function requireOptionalBoolean(value, name) {
42
+ if (value !== undefined && typeof value !== "boolean") {
43
+ throw inputError(`${name} must be a boolean`);
44
+ }
45
+ }
46
+
47
+ function requireNonNegativeInteger(value, name) {
48
+ if (!Number.isSafeInteger(value) || value < 0) {
49
+ throw inputError(`${name} must be a non-negative safe integer`);
50
+ }
51
+ }
52
+
53
+ function requireU64BigInt(value, name) {
54
+ if (
55
+ typeof value !== "bigint" ||
56
+ value < 0n ||
57
+ value > 18446744073709551615n
58
+ ) {
59
+ throw inputError(`${name} must be an unsigned 64-bit bigint`);
60
+ }
61
+ }
62
+
63
+ function requireOptions(value) {
64
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
65
+ throw inputError("options must be an object");
66
+ }
67
+ const prototype = Object.getPrototypeOf(value);
68
+ if (prototype !== Object.prototype && prototype !== null) {
69
+ throw inputError("options must be a plain object");
70
+ }
71
+ }
72
+
73
+ module.exports = {
74
+ requireFinite,
75
+ requireNonNegativeInteger,
76
+ requireObject,
77
+ requireOptionalBoolean,
78
+ requireOptionalFinite,
79
+ requireOptions,
80
+ requireProtocolFinite,
81
+ requireProtocolString,
82
+ requireString,
83
+ requireU64BigInt,
84
+ };
package/native.d.ts ADDED
@@ -0,0 +1,170 @@
1
+ /* auto-generated by NAPI-RS */
2
+ /* eslint-disable */
3
+ export declare class NativeWorkbook {
4
+ get closed(): boolean
5
+ close(): void
6
+ summary(): NativeWorkbookSummary
7
+ readRange(sheet: string, start: string, end: string, offset: number, limit: number): NativeRangePage
8
+ functionUsage(): NativeFunctionUsageReport
9
+ calculate(todaySerial?: number | undefined | null, nowSerial?: number | undefined | null): Promise<NativeCalculationReport>
10
+ recalculate(mode: string, todaySerial?: number | undefined | null, nowSerial?: number | undefined | null): Promise<NativeCalculationDelta>
11
+ applyChanges(expectedRevision: string, batchJson: string): NativeEditReceipt
12
+ changesSince(cursor: string, limit: number): NativeCalculationDeltaPage
13
+ cancelCalculation(): boolean
14
+ calculationActive(): boolean
15
+ setBlank(sheet: string, address: string): void
16
+ setNumber(sheet: string, address: string, value: number): void
17
+ setText(sheet: string, address: string, value: string): void
18
+ setLogical(sheet: string, address: string, value: boolean): void
19
+ setError(sheet: string, address: string, value: string): void
20
+ setFormula(sheet: string, address: string, formula: string, dynamicRange?: string | undefined | null): void
21
+ clearCell(sheet: string, address: string): boolean
22
+ addSheet(name: string): number
23
+ renameSheet(currentName: string, newName: string): void
24
+ toBytes(invalidateUnavailable: boolean): Promise<Buffer>
25
+ savePath(path: string, invalidateUnavailable: boolean, replaceExisting: boolean): Promise<NativeWriteReport>
26
+ }
27
+
28
+ export declare function createWorkbook(): NativeWorkbook
29
+
30
+ export interface NativeCalculationDelta {
31
+ schemaVersion: number
32
+ cursor: string
33
+ baseRevision: string
34
+ resultRevision: string
35
+ mode: string
36
+ reason: string
37
+ dirtyCount: number
38
+ evaluatedCount: number
39
+ parsedFormulaCount: number
40
+ changedCells: Array<NativeCalculationDeltaCell>
41
+ removedMaterializedCells: Array<NativeCellReference>
42
+ }
43
+
44
+ export interface NativeCalculationDeltaCell {
45
+ cell: NativeCellReference
46
+ origin: string
47
+ anchor?: NativeCellReference
48
+ range?: string
49
+ result: NativeCalculationResult
50
+ }
51
+
52
+ export interface NativeCalculationDeltaPage {
53
+ schemaVersion: number
54
+ requestedCursor: string
55
+ nextCursor?: string
56
+ deltas: Array<NativeCalculationDelta>
57
+ }
58
+
59
+ export interface NativeCalculationReport {
60
+ schemaVersion: number
61
+ semanticRevision: string
62
+ formulaCount: number
63
+ valueCount: number
64
+ unavailableCount: number
65
+ materializedCellCount: number
66
+ }
67
+
68
+ export interface NativeCalculationResult {
69
+ kind: string
70
+ value?: NativeCellValue
71
+ code?: string
72
+ message?: string
73
+ detail?: string
74
+ }
75
+
76
+ export interface NativeCell {
77
+ address: string
78
+ formula?: string
79
+ sourceValue: NativeCellValue
80
+ sourceValueState: string
81
+ calculated?: NativeCalculationResult
82
+ }
83
+
84
+ export interface NativeCellReference {
85
+ sheetId: number
86
+ sheetName: string
87
+ address: string
88
+ }
89
+
90
+ export interface NativeCellValue {
91
+ kind: string
92
+ numberValue?: number
93
+ textValue?: string
94
+ logicalValue?: boolean
95
+ errorValue?: string
96
+ }
97
+
98
+ export interface NativeEditReceipt {
99
+ schemaVersion: number
100
+ baseRevision: string
101
+ resultRevision: string
102
+ appliedChangeCount: number
103
+ changedCells: Array<NativeCellReference>
104
+ calculationChangedCells: Array<NativeCellReference>
105
+ createdSheetIds: Array<number>
106
+ topologyChanged: boolean
107
+ calculationMetadataChanged: boolean
108
+ }
109
+
110
+ export interface NativeFunctionUsageEntry {
111
+ name: string
112
+ supported: boolean
113
+ callCount: number
114
+ formulaCount: number
115
+ sampleCells: Array<NativeCellReference>
116
+ }
117
+
118
+ export interface NativeFunctionUsageReport {
119
+ schemaVersion: number
120
+ formulaCount: number
121
+ parsedFormulaCount: number
122
+ unparsedFormulaCount: number
123
+ entries: Array<NativeFunctionUsageEntry>
124
+ }
125
+
126
+ export interface NativeRangePage {
127
+ schemaVersion: number
128
+ sheet: string
129
+ start: string
130
+ end: string
131
+ totalCells: number
132
+ offset: number
133
+ nextOffset?: number
134
+ cells: Array<NativeCell>
135
+ }
136
+
137
+ export interface NativeSheetSummary {
138
+ id: number
139
+ name: string
140
+ visibility: string
141
+ cellCount: number
142
+ usedRange?: string
143
+ }
144
+
145
+ export interface NativeWorkbookSummary {
146
+ schemaVersion: number
147
+ semanticRevision: string
148
+ documentBacked: boolean
149
+ documentKind: string
150
+ dateSystem: string
151
+ diagnosticCount: number
152
+ sheets: Array<NativeSheetSummary>
153
+ }
154
+
155
+ export interface NativeWriteReport {
156
+ schemaVersion: number
157
+ complete: boolean
158
+ policy: string
159
+ materializedCount: number
160
+ invalidatedCells: Array<NativeCellReference>
161
+ changedParts: Array<string>
162
+ removedParts: Array<string>
163
+ diagnosticCount: number
164
+ }
165
+
166
+ export declare function openWorkbookBytes(bytes: Buffer): Promise<NativeWorkbook>
167
+
168
+ export declare function openWorkbookPath(path: string): Promise<NativeWorkbook>
169
+
170
+ export declare function schemaVersion(): number