@opengeni/contracts 0.40.0 → 0.50.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/dist/atlassian.d.ts +278 -0
- package/dist/atlassian.js +57 -0
- package/dist/atlassian.js.map +1 -0
- package/dist/canonical-human-identities.d.ts +157 -0
- package/dist/canonical-human-identities.js +23 -0
- package/dist/canonical-human-identities.js.map +1 -0
- package/dist/{chunk-7JZMI22I.js → chunk-5UDNP7AO.js} +34 -2
- package/dist/chunk-5UDNP7AO.js.map +1 -0
- package/dist/chunk-BVGIYW7D.js +11 -0
- package/dist/chunk-BVGIYW7D.js.map +1 -0
- package/dist/chunk-CADSX6UN.js +1145 -0
- package/dist/chunk-CADSX6UN.js.map +1 -0
- package/dist/chunk-CM6BMECR.js +24 -0
- package/dist/chunk-CM6BMECR.js.map +1 -0
- package/dist/chunk-D2HKF2EF.js +1460 -0
- package/dist/chunk-D2HKF2EF.js.map +1 -0
- package/dist/chunk-JBCIY7QD.js +194 -0
- package/dist/chunk-JBCIY7QD.js.map +1 -0
- package/dist/chunk-JUFK3T4Q.js +80 -0
- package/dist/chunk-JUFK3T4Q.js.map +1 -0
- package/dist/chunk-LSB7GRLG.js +779 -0
- package/dist/chunk-LSB7GRLG.js.map +1 -0
- package/dist/chunk-LW7OH6CS.js +382 -0
- package/dist/chunk-LW7OH6CS.js.map +1 -0
- package/dist/chunk-M5ZGRVIQ.js +719 -0
- package/dist/chunk-M5ZGRVIQ.js.map +1 -0
- package/dist/chunk-MYBZRDTY.js +340 -0
- package/dist/chunk-MYBZRDTY.js.map +1 -0
- package/dist/chunk-Q6A7XT2N.js +16734 -0
- package/dist/chunk-Q6A7XT2N.js.map +1 -0
- package/dist/chunk-RIY7GYQS.js +1204 -0
- package/dist/chunk-RIY7GYQS.js.map +1 -0
- package/dist/chunk-TZ6YEYN4.js +386 -0
- package/dist/chunk-TZ6YEYN4.js.map +1 -0
- package/dist/chunk-Y3MLSLB4.js +392 -0
- package/dist/chunk-Y3MLSLB4.js.map +1 -0
- package/dist/chunk-YRKRZ6OF.js +134 -0
- package/dist/chunk-YRKRZ6OF.js.map +1 -0
- package/dist/codex-provider-account-authority.d.ts +23 -0
- package/dist/codex-provider-account-authority.js +23 -0
- package/dist/codex-provider-account-authority.js.map +1 -0
- package/dist/company-profile.d.ts +629 -0
- package/dist/document-artifact-commands.d.ts +282 -0
- package/dist/document-artifact-commands.js +54 -0
- package/dist/document-artifact-commands.js.map +1 -0
- package/dist/document-artifact-query.d.ts +2 -0
- package/dist/document-artifact-query.js +54 -0
- package/dist/document-artifact-query.js.map +1 -0
- package/dist/editable-artifact-binary.d.ts +56 -0
- package/dist/editable-artifact-causal-frontier.d.ts +12 -0
- package/dist/editable-artifact-causal-frontier.js +18 -0
- package/dist/editable-artifact-causal-frontier.js.map +1 -0
- package/dist/editable-artifact-codec-registry.d.ts +48 -0
- package/dist/editable-artifact-codec-registry.js +20 -0
- package/dist/editable-artifact-codec-registry.js.map +1 -0
- package/dist/editable-artifact-committed-transaction.d.ts +35 -0
- package/dist/editable-artifact-committed-transaction.js +19 -0
- package/dist/editable-artifact-committed-transaction.js.map +1 -0
- package/dist/editable-artifact-live.d.ts +276 -0
- package/dist/editable-artifact-live.js +907 -0
- package/dist/editable-artifact-live.js.map +1 -0
- package/dist/editable-artifact-serialized-commit.d.ts +53 -0
- package/dist/editable-artifact-serialized-commit.js +26 -0
- package/dist/editable-artifact-serialized-commit.js.map +1 -0
- package/dist/editable-artifact-versions.d.ts +6 -0
- package/dist/editable-artifact-versions.js +11 -0
- package/dist/editable-artifact-versions.js.map +1 -0
- package/dist/editable-artifacts.d.ts +46 -0
- package/dist/editable-artifacts.js +236 -0
- package/dist/editable-artifacts.js.map +1 -0
- package/dist/event-preview.d.ts +1 -1
- package/dist/google-drive.d.ts +114 -2
- package/dist/google-drive.js +72 -14
- package/dist/google-drive.js.map +1 -1
- package/dist/image-generation.d.ts +70 -0
- package/dist/index.d.ts +5661 -166
- package/dist/index.js +1438 -21
- package/dist/interaction.d.ts +8290 -0
- package/dist/knowledge.d.ts +543 -0
- package/dist/memory-slack-delivery.d.ts +397 -0
- package/dist/presentation-artifact-commands.d.ts +349 -0
- package/dist/presentation-artifact-commands.js +66 -0
- package/dist/presentation-artifact-commands.js.map +1 -0
- package/dist/presentation-artifact-query.d.ts +2 -0
- package/dist/presentation-artifact-query.js +66 -0
- package/dist/presentation-artifact-query.js.map +1 -0
- package/dist/retained-output.d.ts +24 -0
- package/dist/sandbox-snapshots.d.ts +9 -0
- package/dist/scoped-knowledge.d.ts +14 -13
- package/dist/slack-bot-scopes.d.ts +4 -0
- package/dist/slack-bot-scopes.js +3 -1
- package/dist/slack-task-policy.d.ts +294 -0
- package/dist/spreadsheet-artifact-commands.d.ts +101 -0
- package/dist/spreadsheet-artifact-commands.js +27 -0
- package/dist/spreadsheet-artifact-commands.js.map +1 -0
- package/dist/spreadsheet-artifact-date.d.ts +4 -0
- package/dist/spreadsheet-artifact-query.d.ts +120 -0
- package/dist/spreadsheet-artifact-query.js +48 -0
- package/dist/spreadsheet-artifact-query.js.map +1 -0
- package/dist/task-notes.d.ts +128 -0
- package/dist/tool-catalog.d.ts +576 -0
- package/dist/video-generation.d.ts +580 -0
- package/dist/video-generation.js +59 -0
- package/dist/video-generation.js.map +1 -0
- package/dist/workspace-learning-policy.d.ts +208 -0
- package/dist/xai-provider-account-authority.d.ts +23 -0
- package/dist/xai-provider-account-authority.js +9 -0
- package/dist/xai-provider-account-authority.js.map +1 -0
- package/package.json +73 -1
- package/src/artifacts.ts +1 -1
- package/src/atlassian.ts +160 -0
- package/src/canonical-human-identities.ts +93 -0
- package/src/codex-provider-account-authority.ts +37 -0
- package/src/company-profile.ts +306 -0
- package/src/document-artifact-commands.ts +1709 -0
- package/src/document-artifact-query.ts +2 -0
- package/src/editable-artifact-binary.ts +396 -0
- package/src/editable-artifact-causal-frontier.ts +154 -0
- package/src/editable-artifact-codec-registry.ts +261 -0
- package/src/editable-artifact-committed-transaction.ts +478 -0
- package/src/editable-artifact-live.ts +1379 -0
- package/src/editable-artifact-serialized-commit.ts +494 -0
- package/src/editable-artifact-versions.ts +8 -0
- package/src/editable-artifacts.ts +473 -0
- package/src/google-drive.ts +68 -15
- package/src/image-generation.ts +106 -0
- package/src/index.ts +2920 -326
- package/src/interaction.ts +3173 -0
- package/src/knowledge.ts +207 -0
- package/src/memory-slack-delivery.ts +183 -0
- package/src/presentation-artifact-commands.ts +2035 -0
- package/src/presentation-artifact-query.ts +2 -0
- package/src/retained-output.ts +149 -2
- package/src/sandbox-snapshots.ts +28 -1
- package/src/scoped-knowledge.ts +18 -4
- package/src/slack-bot-scopes.ts +34 -1
- package/src/slack-task-policy.ts +215 -0
- package/src/spreadsheet-artifact-commands.ts +999 -0
- package/src/spreadsheet-artifact-date.ts +21 -0
- package/src/spreadsheet-artifact-query.ts +1411 -0
- package/src/task-notes.ts +92 -0
- package/src/tool-catalog.ts +334 -0
- package/src/video-generation.ts +431 -0
- package/src/workspace-learning-policy.ts +233 -0
- package/src/xai-provider-account-authority.ts +37 -0
- package/dist/chunk-5HR2GPSC.js +0 -11031
- package/dist/chunk-5HR2GPSC.js.map +0 -1
- package/dist/chunk-7JZMI22I.js.map +0 -1
|
@@ -0,0 +1,999 @@
|
|
|
1
|
+
import {
|
|
2
|
+
canonicalSpreadsheetDateFromMilliseconds,
|
|
3
|
+
canonicalSpreadsheetDateMilliseconds,
|
|
4
|
+
} from "./spreadsheet-artifact-date";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Canonical, identity-free spreadsheet mutation commands nested inside
|
|
8
|
+
* `OGATX001`. Artifact, actor, transaction, delivery, and causal identity must
|
|
9
|
+
* never be added here: the outer authored-intent envelope is their sole wire
|
|
10
|
+
* authority.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const SPREADSHEET_ARTIFACT_COMMAND_VERSION = 1 as const;
|
|
14
|
+
export const SPREADSHEET_ARTIFACT_COMMAND_MAX_BYTES = 4 * 1024 * 1024;
|
|
15
|
+
export const SPREADSHEET_ARTIFACT_COMMAND_MAX_COMMANDS = 4_096;
|
|
16
|
+
export const SPREADSHEET_ARTIFACT_COMMAND_MAX_CELLS = 1_000_000;
|
|
17
|
+
export const SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES = 1 * 1024 * 1024;
|
|
18
|
+
export const SPREADSHEET_ARTIFACT_SHEET_NAME_MAX_UTF16_UNITS = 31;
|
|
19
|
+
|
|
20
|
+
const MAGIC = new TextEncoder().encode("OGASC001");
|
|
21
|
+
const HEADER_BYTES = 8 + 2 + 2 + 4 + 8;
|
|
22
|
+
const CHECKSUM_BYTES = 8;
|
|
23
|
+
const textEncoder = new TextEncoder();
|
|
24
|
+
const textDecoder = new TextDecoder("utf-8", { fatal: true });
|
|
25
|
+
const LOWER_HEX_ID = /^[0-9a-f]{32}$/u;
|
|
26
|
+
const UINT64_HEX_ZERO = "0".repeat(16);
|
|
27
|
+
const UINT32_MAX = 0xffff_ffff;
|
|
28
|
+
const FNV_OFFSET = 0xcbf29ce484222325n;
|
|
29
|
+
const FNV_PRIME = 0x100000001b3n;
|
|
30
|
+
|
|
31
|
+
declare const stableIdBrand: unique symbol;
|
|
32
|
+
|
|
33
|
+
/** Fixed-width lowercase hexadecimal 128-bit id. Runtime helpers enforce use. */
|
|
34
|
+
export type EditableArtifactStableId = string & {
|
|
35
|
+
readonly [stableIdBrand]: "EditableArtifactStableId";
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A concrete sheet generation. The object id follows the offline allocator
|
|
40
|
+
* invariant (both 64-bit halves nonzero); a derived operation id reserves only
|
|
41
|
+
* the all-zero 128-bit value.
|
|
42
|
+
*/
|
|
43
|
+
export type SpreadsheetSheetGeneration = Readonly<{
|
|
44
|
+
kind: "generation";
|
|
45
|
+
sheetId: EditableArtifactStableId;
|
|
46
|
+
creationOperationId: EditableArtifactStableId;
|
|
47
|
+
}>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Reference to a sheet created by an earlier command in this same batch.
|
|
51
|
+
* This avoids recursively embedding the creation operation id, which is
|
|
52
|
+
* derived from the hash of the complete outer intent.
|
|
53
|
+
*/
|
|
54
|
+
export type SpreadsheetPriorCreatePrecondition = Readonly<{
|
|
55
|
+
kind: "created-in-batch";
|
|
56
|
+
sheetId: EditableArtifactStableId;
|
|
57
|
+
createCommandIndex: number;
|
|
58
|
+
}>;
|
|
59
|
+
|
|
60
|
+
export type SpreadsheetSheetPrecondition =
|
|
61
|
+
| SpreadsheetSheetGeneration
|
|
62
|
+
| SpreadsheetPriorCreatePrecondition;
|
|
63
|
+
|
|
64
|
+
export type SpreadsheetCellPoint = Readonly<{ row: number; column: number }>;
|
|
65
|
+
export type SpreadsheetCellRange = Readonly<{
|
|
66
|
+
start: SpreadsheetCellPoint;
|
|
67
|
+
end: SpreadsheetCellPoint;
|
|
68
|
+
}>;
|
|
69
|
+
|
|
70
|
+
export type SpreadsheetFormulaError = Readonly<{ error: string }>;
|
|
71
|
+
export type SpreadsheetDateValue = Readonly<{ date: string }>;
|
|
72
|
+
export type SpreadsheetCellScalar =
|
|
73
|
+
| null
|
|
74
|
+
| boolean
|
|
75
|
+
| number
|
|
76
|
+
| string
|
|
77
|
+
| SpreadsheetDateValue
|
|
78
|
+
| SpreadsheetFormulaError;
|
|
79
|
+
export type SpreadsheetFormulaCell = Readonly<{
|
|
80
|
+
formula: string;
|
|
81
|
+
cached: SpreadsheetCellScalar;
|
|
82
|
+
}>;
|
|
83
|
+
export type SpreadsheetCellInput = SpreadsheetCellScalar | SpreadsheetFormulaCell;
|
|
84
|
+
|
|
85
|
+
export type SpreadsheetCreateSheetCommand = Readonly<{
|
|
86
|
+
kind: "sheet.create";
|
|
87
|
+
sheetId: EditableArtifactStableId;
|
|
88
|
+
name: string;
|
|
89
|
+
after: SpreadsheetSheetPrecondition | null;
|
|
90
|
+
}>;
|
|
91
|
+
|
|
92
|
+
export type SpreadsheetRenameSheetCommand = Readonly<{
|
|
93
|
+
kind: "sheet.rename";
|
|
94
|
+
sheet: SpreadsheetSheetPrecondition;
|
|
95
|
+
name: string;
|
|
96
|
+
}>;
|
|
97
|
+
|
|
98
|
+
export type SpreadsheetDeleteSheetCommand = Readonly<{
|
|
99
|
+
kind: "sheet.delete";
|
|
100
|
+
sheet: SpreadsheetSheetPrecondition;
|
|
101
|
+
}>;
|
|
102
|
+
|
|
103
|
+
/** Row-major rectangular write; `cells.length === rows * columns`. */
|
|
104
|
+
export type SpreadsheetSetCellsCommand = Readonly<{
|
|
105
|
+
kind: "cells.set";
|
|
106
|
+
sheet: SpreadsheetSheetPrecondition;
|
|
107
|
+
anchor: SpreadsheetCellPoint;
|
|
108
|
+
rows: number;
|
|
109
|
+
columns: number;
|
|
110
|
+
cells: readonly SpreadsheetCellInput[];
|
|
111
|
+
}>;
|
|
112
|
+
|
|
113
|
+
export type SpreadsheetClearRangeCommand = Readonly<{
|
|
114
|
+
kind: "range.clear";
|
|
115
|
+
sheet: SpreadsheetSheetPrecondition;
|
|
116
|
+
range: SpreadsheetCellRange;
|
|
117
|
+
}>;
|
|
118
|
+
|
|
119
|
+
export type SpreadsheetArtifactCommand =
|
|
120
|
+
| SpreadsheetCreateSheetCommand
|
|
121
|
+
| SpreadsheetRenameSheetCommand
|
|
122
|
+
| SpreadsheetDeleteSheetCommand
|
|
123
|
+
| SpreadsheetSetCellsCommand
|
|
124
|
+
| SpreadsheetClearRangeCommand;
|
|
125
|
+
|
|
126
|
+
export type SpreadsheetArtifactCommandBatch = Readonly<{
|
|
127
|
+
version: typeof SPREADSHEET_ARTIFACT_COMMAND_VERSION;
|
|
128
|
+
commands: readonly SpreadsheetArtifactCommand[];
|
|
129
|
+
}>;
|
|
130
|
+
|
|
131
|
+
/** Validates a generic nonzero 128-bit id and returns its nominal type. */
|
|
132
|
+
export function editableArtifactStableId(value: string): EditableArtifactStableId {
|
|
133
|
+
return genericStableId(value, "stable id");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Validates a sheet object id with nonzero allocator namespace and counter. */
|
|
137
|
+
export function spreadsheetSheetId(value: string): EditableArtifactStableId {
|
|
138
|
+
return sheetObjectId(value, "sheet id");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function encodeSpreadsheetArtifactCommandBatch(
|
|
142
|
+
input: SpreadsheetArtifactCommandBatch,
|
|
143
|
+
): Uint8Array {
|
|
144
|
+
const batch = batchRecord(input);
|
|
145
|
+
const payload = new BinaryWriter(
|
|
146
|
+
SPREADSHEET_ARTIFACT_COMMAND_MAX_BYTES - HEADER_BYTES - CHECKSUM_BYTES,
|
|
147
|
+
);
|
|
148
|
+
const context = new CommandContext();
|
|
149
|
+
for (let index = 0; index < batch.commands.length; index += 1) {
|
|
150
|
+
const command = ownArrayElement(batch.commands, index, "commands");
|
|
151
|
+
encodeCommand(payload, command, index, context);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const payloadBytes = payload.finish();
|
|
155
|
+
const output = new BinaryWriter(SPREADSHEET_ARTIFACT_COMMAND_MAX_BYTES);
|
|
156
|
+
output.bytes(MAGIC);
|
|
157
|
+
output.u16(SPREADSHEET_ARTIFACT_COMMAND_VERSION);
|
|
158
|
+
output.u16(0);
|
|
159
|
+
output.u32(batch.commands.length);
|
|
160
|
+
output.u64(BigInt(payloadBytes.byteLength));
|
|
161
|
+
output.bytes(payloadBytes);
|
|
162
|
+
output.u64(fnv1a64(output.view()));
|
|
163
|
+
return output.finish();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function decodeSpreadsheetArtifactCommandBatch(
|
|
167
|
+
bytes: Uint8Array,
|
|
168
|
+
): SpreadsheetArtifactCommandBatch {
|
|
169
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
170
|
+
throw new TypeError("spreadsheet command bytes must be a Uint8Array");
|
|
171
|
+
}
|
|
172
|
+
if (bytes.byteLength > SPREADSHEET_ARTIFACT_COMMAND_MAX_BYTES) {
|
|
173
|
+
throw new RangeError("spreadsheet command envelope exceeds its byte limit");
|
|
174
|
+
}
|
|
175
|
+
if (bytes.byteLength < HEADER_BYTES + CHECKSUM_BYTES) {
|
|
176
|
+
throw new TypeError("truncated spreadsheet command envelope");
|
|
177
|
+
}
|
|
178
|
+
const header = new BinaryReader(bytes);
|
|
179
|
+
if (!equalBytes(header.bytes(MAGIC.byteLength), MAGIC)) {
|
|
180
|
+
throw new TypeError("invalid spreadsheet command magic");
|
|
181
|
+
}
|
|
182
|
+
const version = header.u16();
|
|
183
|
+
if (version !== SPREADSHEET_ARTIFACT_COMMAND_VERSION) {
|
|
184
|
+
throw new TypeError(`unsupported spreadsheet command version: ${version}`);
|
|
185
|
+
}
|
|
186
|
+
if (header.u16() !== 0) {
|
|
187
|
+
throw new TypeError("spreadsheet command reserved flags must be zero");
|
|
188
|
+
}
|
|
189
|
+
const commandCount = header.u32();
|
|
190
|
+
if (commandCount < 1 || commandCount > SPREADSHEET_ARTIFACT_COMMAND_MAX_COMMANDS) {
|
|
191
|
+
throw new RangeError("spreadsheet command count is outside its limit");
|
|
192
|
+
}
|
|
193
|
+
const payloadLength = safeLength(header.u64(), "spreadsheet command payload");
|
|
194
|
+
const expectedLength = HEADER_BYTES + payloadLength + CHECKSUM_BYTES;
|
|
195
|
+
if (bytes.byteLength < expectedLength) {
|
|
196
|
+
throw new TypeError("truncated spreadsheet command envelope");
|
|
197
|
+
}
|
|
198
|
+
if (bytes.byteLength > expectedLength) {
|
|
199
|
+
throw new TypeError("spreadsheet command envelope contains trailing bytes");
|
|
200
|
+
}
|
|
201
|
+
const advertisedChecksum = readU64At(bytes, expectedLength - CHECKSUM_BYTES);
|
|
202
|
+
const actualChecksum = fnv1a64(bytes.subarray(0, expectedLength - CHECKSUM_BYTES));
|
|
203
|
+
if (advertisedChecksum !== actualChecksum) {
|
|
204
|
+
throw new TypeError("spreadsheet command checksum does not match");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const payload = new BinaryReader(bytes.subarray(HEADER_BYTES, HEADER_BYTES + payloadLength));
|
|
208
|
+
const context = new CommandContext();
|
|
209
|
+
const commands: SpreadsheetArtifactCommand[] = [];
|
|
210
|
+
for (let index = 0; index < commandCount; index += 1) {
|
|
211
|
+
commands.push(decodeCommand(payload, index, context));
|
|
212
|
+
}
|
|
213
|
+
payload.done("spreadsheet command payload contains trailing bytes");
|
|
214
|
+
return Object.freeze({
|
|
215
|
+
version: SPREADSHEET_ARTIFACT_COMMAND_VERSION,
|
|
216
|
+
commands: Object.freeze(commands),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Decodes, re-encodes, and proves byte-for-byte canonicality. */
|
|
221
|
+
export function assertCanonicalSpreadsheetArtifactCommandBytes(bytes: Uint8Array): Uint8Array {
|
|
222
|
+
const canonical = encodeSpreadsheetArtifactCommandBatch(
|
|
223
|
+
decodeSpreadsheetArtifactCommandBatch(bytes),
|
|
224
|
+
);
|
|
225
|
+
if (!equalBytes(bytes, canonical)) {
|
|
226
|
+
throw new TypeError("spreadsheet command envelope is not canonical");
|
|
227
|
+
}
|
|
228
|
+
return bytes;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
class CommandContext {
|
|
232
|
+
readonly createdByIndex = new Map<number, string>();
|
|
233
|
+
readonly createdSheetIds = new Set<string>();
|
|
234
|
+
totalCells = 0;
|
|
235
|
+
|
|
236
|
+
registerCreate(index: number, sheetId: string): void {
|
|
237
|
+
if (this.createdSheetIds.has(sheetId)) {
|
|
238
|
+
throw commandError(index, "sheet id is created more than once in the batch");
|
|
239
|
+
}
|
|
240
|
+
this.createdByIndex.set(index, sheetId);
|
|
241
|
+
this.createdSheetIds.add(sheetId);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
addCells(index: number, rows: number, columns: number): number {
|
|
245
|
+
const count = rows * columns;
|
|
246
|
+
if (!Number.isSafeInteger(count) || count < 1) {
|
|
247
|
+
throw commandError(index, "cell block dimensions are invalid");
|
|
248
|
+
}
|
|
249
|
+
const next = this.totalCells + count;
|
|
250
|
+
if (!Number.isSafeInteger(next) || next > SPREADSHEET_ARTIFACT_COMMAND_MAX_CELLS) {
|
|
251
|
+
throw commandError(index, "cell count exceeds its transaction limit");
|
|
252
|
+
}
|
|
253
|
+
this.totalCells = next;
|
|
254
|
+
return count;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function encodeCommand(
|
|
259
|
+
writer: BinaryWriter,
|
|
260
|
+
value: unknown,
|
|
261
|
+
index: number,
|
|
262
|
+
context: CommandContext,
|
|
263
|
+
): void {
|
|
264
|
+
const command = plainRecord(value, `spreadsheet command ${index}`);
|
|
265
|
+
switch (command.kind) {
|
|
266
|
+
case "sheet.create": {
|
|
267
|
+
exactKeys(command, ["after", "kind", "name", "sheetId"], `spreadsheet command ${index}`);
|
|
268
|
+
const sheetId = sheetObjectId(command.sheetId, `spreadsheet command ${index} sheetId`);
|
|
269
|
+
const name = sheetName(command.name, index);
|
|
270
|
+
writer.u8(0);
|
|
271
|
+
writer.stableId(sheetId);
|
|
272
|
+
writer.string(name, SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES);
|
|
273
|
+
if (command.after === null) {
|
|
274
|
+
writer.u8(0);
|
|
275
|
+
} else {
|
|
276
|
+
writer.u8(1);
|
|
277
|
+
encodeSheetPrecondition(writer, command.after, index, context);
|
|
278
|
+
}
|
|
279
|
+
context.registerCreate(index, sheetId);
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
case "sheet.rename": {
|
|
283
|
+
exactKeys(command, ["kind", "name", "sheet"], `spreadsheet command ${index}`);
|
|
284
|
+
writer.u8(1);
|
|
285
|
+
encodeSheetPrecondition(writer, command.sheet, index, context);
|
|
286
|
+
writer.string(sheetName(command.name, index), SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES);
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
case "sheet.delete": {
|
|
290
|
+
exactKeys(command, ["kind", "sheet"], `spreadsheet command ${index}`);
|
|
291
|
+
writer.u8(2);
|
|
292
|
+
encodeSheetPrecondition(writer, command.sheet, index, context);
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
case "cells.set": {
|
|
296
|
+
exactKeys(
|
|
297
|
+
command,
|
|
298
|
+
["anchor", "cells", "columns", "kind", "rows", "sheet"],
|
|
299
|
+
`spreadsheet command ${index}`,
|
|
300
|
+
);
|
|
301
|
+
const anchor = point(command.anchor, `spreadsheet command ${index} anchor`);
|
|
302
|
+
const rows = positiveU32(command.rows, `spreadsheet command ${index} rows`);
|
|
303
|
+
const columns = positiveU32(command.columns, `spreadsheet command ${index} columns`);
|
|
304
|
+
assertExtent(anchor.row, rows, "row", index);
|
|
305
|
+
assertExtent(anchor.column, columns, "column", index);
|
|
306
|
+
const count = context.addCells(index, rows, columns);
|
|
307
|
+
if (!Array.isArray(command.cells) || command.cells.length !== count) {
|
|
308
|
+
throw commandError(index, `cells must contain exactly ${count} row-major values`);
|
|
309
|
+
}
|
|
310
|
+
writer.u8(3);
|
|
311
|
+
encodeSheetPrecondition(writer, command.sheet, index, context);
|
|
312
|
+
writer.u32(anchor.row);
|
|
313
|
+
writer.u32(anchor.column);
|
|
314
|
+
writer.u32(rows);
|
|
315
|
+
writer.u32(columns);
|
|
316
|
+
for (let cellIndex = 0; cellIndex < count; cellIndex += 1) {
|
|
317
|
+
writer.cell(
|
|
318
|
+
ownArrayElement(command.cells, cellIndex, `spreadsheet command ${index} cells`),
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
case "range.clear": {
|
|
324
|
+
exactKeys(command, ["kind", "range", "sheet"], `spreadsheet command ${index}`);
|
|
325
|
+
const range = cellRange(command.range, index);
|
|
326
|
+
writer.u8(4);
|
|
327
|
+
encodeSheetPrecondition(writer, command.sheet, index, context);
|
|
328
|
+
writer.u32(range.start.row);
|
|
329
|
+
writer.u32(range.start.column);
|
|
330
|
+
writer.u32(range.end.row);
|
|
331
|
+
writer.u32(range.end.column);
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
334
|
+
default:
|
|
335
|
+
throw commandError(index, "unknown command kind");
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function decodeCommand(
|
|
340
|
+
reader: BinaryReader,
|
|
341
|
+
index: number,
|
|
342
|
+
context: CommandContext,
|
|
343
|
+
): SpreadsheetArtifactCommand {
|
|
344
|
+
switch (reader.u8()) {
|
|
345
|
+
case 0: {
|
|
346
|
+
const sheetId = reader.sheetObjectId();
|
|
347
|
+
const name = decodedSheetName(
|
|
348
|
+
reader.string(SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES),
|
|
349
|
+
index,
|
|
350
|
+
);
|
|
351
|
+
const afterFlag = reader.u8();
|
|
352
|
+
if (afterFlag > 1) throw commandError(index, "after presence flag is invalid");
|
|
353
|
+
const after = afterFlag === 1 ? decodeSheetPrecondition(reader, index, context) : null;
|
|
354
|
+
context.registerCreate(index, sheetId);
|
|
355
|
+
return Object.freeze({ kind: "sheet.create", sheetId, name, after });
|
|
356
|
+
}
|
|
357
|
+
case 1:
|
|
358
|
+
return Object.freeze({
|
|
359
|
+
kind: "sheet.rename",
|
|
360
|
+
sheet: decodeSheetPrecondition(reader, index, context),
|
|
361
|
+
name: decodedSheetName(reader.string(SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES), index),
|
|
362
|
+
});
|
|
363
|
+
case 2:
|
|
364
|
+
return Object.freeze({
|
|
365
|
+
kind: "sheet.delete",
|
|
366
|
+
sheet: decodeSheetPrecondition(reader, index, context),
|
|
367
|
+
});
|
|
368
|
+
case 3: {
|
|
369
|
+
const sheet = decodeSheetPrecondition(reader, index, context);
|
|
370
|
+
const anchor = frozenPoint(reader.u32(), reader.u32());
|
|
371
|
+
const rows = reader.u32();
|
|
372
|
+
const columns = reader.u32();
|
|
373
|
+
if (rows === 0 || columns === 0) {
|
|
374
|
+
throw commandError(index, "cell block dimensions must be nonzero");
|
|
375
|
+
}
|
|
376
|
+
assertExtent(anchor.row, rows, "row", index);
|
|
377
|
+
assertExtent(anchor.column, columns, "column", index);
|
|
378
|
+
const count = context.addCells(index, rows, columns);
|
|
379
|
+
const cells: SpreadsheetCellInput[] = [];
|
|
380
|
+
for (let cellIndex = 0; cellIndex < count; cellIndex += 1) cells.push(reader.cell());
|
|
381
|
+
return Object.freeze({
|
|
382
|
+
kind: "cells.set",
|
|
383
|
+
sheet,
|
|
384
|
+
anchor,
|
|
385
|
+
rows,
|
|
386
|
+
columns,
|
|
387
|
+
cells: Object.freeze(cells),
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
case 4: {
|
|
391
|
+
const sheet = decodeSheetPrecondition(reader, index, context);
|
|
392
|
+
const start = frozenPoint(reader.u32(), reader.u32());
|
|
393
|
+
const end = frozenPoint(reader.u32(), reader.u32());
|
|
394
|
+
if (start.row > end.row || start.column > end.column) {
|
|
395
|
+
throw commandError(index, "clear range endpoints are not ordered");
|
|
396
|
+
}
|
|
397
|
+
return Object.freeze({
|
|
398
|
+
kind: "range.clear",
|
|
399
|
+
sheet,
|
|
400
|
+
range: Object.freeze({ start, end }),
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
default:
|
|
404
|
+
throw commandError(index, "unknown binary command tag");
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function encodeSheetPrecondition(
|
|
409
|
+
writer: BinaryWriter,
|
|
410
|
+
value: unknown,
|
|
411
|
+
commandIndex: number,
|
|
412
|
+
context: CommandContext,
|
|
413
|
+
): void {
|
|
414
|
+
const input = plainRecord(value, `spreadsheet command ${commandIndex} sheet precondition`);
|
|
415
|
+
if (input.kind === "generation") {
|
|
416
|
+
exactKeys(
|
|
417
|
+
input,
|
|
418
|
+
["creationOperationId", "kind", "sheetId"],
|
|
419
|
+
`spreadsheet command ${commandIndex} sheet generation`,
|
|
420
|
+
);
|
|
421
|
+
writer.u8(0);
|
|
422
|
+
writer.stableId(sheetObjectId(input.sheetId, "sheet generation sheetId"));
|
|
423
|
+
writer.stableId(genericStableId(input.creationOperationId, "sheet creation operation id"));
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (input.kind === "created-in-batch") {
|
|
427
|
+
exactKeys(
|
|
428
|
+
input,
|
|
429
|
+
["createCommandIndex", "kind", "sheetId"],
|
|
430
|
+
`spreadsheet command ${commandIndex} prior-create precondition`,
|
|
431
|
+
);
|
|
432
|
+
const sheetId = sheetObjectId(input.sheetId, "prior-create sheetId");
|
|
433
|
+
const createCommandIndex = nonnegativeU32(
|
|
434
|
+
input.createCommandIndex,
|
|
435
|
+
"prior-create command index",
|
|
436
|
+
);
|
|
437
|
+
assertPriorCreate(context, commandIndex, createCommandIndex, sheetId);
|
|
438
|
+
writer.u8(1);
|
|
439
|
+
writer.stableId(sheetId);
|
|
440
|
+
writer.u32(createCommandIndex);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
throw commandError(commandIndex, "unknown sheet precondition kind");
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function decodeSheetPrecondition(
|
|
447
|
+
reader: BinaryReader,
|
|
448
|
+
commandIndex: number,
|
|
449
|
+
context: CommandContext,
|
|
450
|
+
): SpreadsheetSheetPrecondition {
|
|
451
|
+
switch (reader.u8()) {
|
|
452
|
+
case 0:
|
|
453
|
+
return Object.freeze({
|
|
454
|
+
kind: "generation",
|
|
455
|
+
sheetId: reader.sheetObjectId(),
|
|
456
|
+
creationOperationId: reader.genericStableId(),
|
|
457
|
+
});
|
|
458
|
+
case 1: {
|
|
459
|
+
const sheetId = reader.sheetObjectId();
|
|
460
|
+
const createCommandIndex = reader.u32();
|
|
461
|
+
assertPriorCreate(context, commandIndex, createCommandIndex, sheetId);
|
|
462
|
+
return Object.freeze({
|
|
463
|
+
kind: "created-in-batch",
|
|
464
|
+
sheetId,
|
|
465
|
+
createCommandIndex,
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
default:
|
|
469
|
+
throw commandError(commandIndex, "unknown sheet precondition tag");
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function assertPriorCreate(
|
|
474
|
+
context: CommandContext,
|
|
475
|
+
commandIndex: number,
|
|
476
|
+
createCommandIndex: number,
|
|
477
|
+
sheetId: string,
|
|
478
|
+
): void {
|
|
479
|
+
if (createCommandIndex >= commandIndex) {
|
|
480
|
+
throw commandError(commandIndex, "prior-create reference must point to an earlier command");
|
|
481
|
+
}
|
|
482
|
+
if (context.createdByIndex.get(createCommandIndex) !== sheetId) {
|
|
483
|
+
throw commandError(
|
|
484
|
+
commandIndex,
|
|
485
|
+
"prior-create reference must match the referenced create command and sheet id",
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function batchRecord(input: unknown): SpreadsheetArtifactCommandBatch {
|
|
491
|
+
const batch = plainRecord(input, "spreadsheet command batch");
|
|
492
|
+
exactKeys(batch, ["commands", "version"], "spreadsheet command batch");
|
|
493
|
+
if (batch.version !== SPREADSHEET_ARTIFACT_COMMAND_VERSION) {
|
|
494
|
+
throw new TypeError("spreadsheet command batch version must be 1");
|
|
495
|
+
}
|
|
496
|
+
if (!Array.isArray(batch.commands)) throw new TypeError("commands must be an array");
|
|
497
|
+
if (
|
|
498
|
+
batch.commands.length < 1 ||
|
|
499
|
+
batch.commands.length > SPREADSHEET_ARTIFACT_COMMAND_MAX_COMMANDS
|
|
500
|
+
) {
|
|
501
|
+
throw new RangeError("spreadsheet command count is outside its limit");
|
|
502
|
+
}
|
|
503
|
+
return input as SpreadsheetArtifactCommandBatch;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function cellRange(value: unknown, index: number): SpreadsheetCellRange {
|
|
507
|
+
const range = plainRecord(value, `spreadsheet command ${index} range`);
|
|
508
|
+
exactKeys(range, ["end", "start"], `spreadsheet command ${index} range`);
|
|
509
|
+
const start = point(range.start, `spreadsheet command ${index} range start`);
|
|
510
|
+
const end = point(range.end, `spreadsheet command ${index} range end`);
|
|
511
|
+
if (start.row > end.row || start.column > end.column) {
|
|
512
|
+
throw commandError(index, "clear range endpoints are not ordered");
|
|
513
|
+
}
|
|
514
|
+
return { start, end };
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function point(value: unknown, label: string): SpreadsheetCellPoint {
|
|
518
|
+
const input = plainRecord(value, label);
|
|
519
|
+
exactKeys(input, ["column", "row"], label);
|
|
520
|
+
return {
|
|
521
|
+
row: nonnegativeU32(input.row, `${label} row`),
|
|
522
|
+
column: nonnegativeU32(input.column, `${label} column`),
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function frozenPoint(row: number, column: number): SpreadsheetCellPoint {
|
|
527
|
+
return Object.freeze({ row, column });
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function sheetName(value: unknown, index: number): string {
|
|
531
|
+
if (typeof value !== "string") throw commandError(index, "sheet name must be a string");
|
|
532
|
+
return decodedSheetName(value, index);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function decodedSheetName(value: string, index: number): string {
|
|
536
|
+
const bytes = strictUtf8(value, "sheet name");
|
|
537
|
+
const forbidden = /[\\/?*[\]:\0]/u.test(value);
|
|
538
|
+
if (
|
|
539
|
+
bytes.byteLength === 0 ||
|
|
540
|
+
value.trim() !== value ||
|
|
541
|
+
value.length > SPREADSHEET_ARTIFACT_SHEET_NAME_MAX_UTF16_UNITS ||
|
|
542
|
+
forbidden
|
|
543
|
+
) {
|
|
544
|
+
throw commandError(index, "sheet name does not match the public spreadsheet model");
|
|
545
|
+
}
|
|
546
|
+
return value;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function genericStableId(value: unknown, label: string): EditableArtifactStableId {
|
|
550
|
+
if (typeof value !== "string" || !LOWER_HEX_ID.test(value) || /^0+$/u.test(value)) {
|
|
551
|
+
throw new TypeError(`${label} must be nonzero fixed-width lowercase hexadecimal text`);
|
|
552
|
+
}
|
|
553
|
+
return value as EditableArtifactStableId;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function sheetObjectId(value: unknown, label: string): EditableArtifactStableId {
|
|
557
|
+
const id = genericStableId(value, label);
|
|
558
|
+
if (id.slice(0, 16) === UINT64_HEX_ZERO || id.slice(16) === UINT64_HEX_ZERO) {
|
|
559
|
+
throw new TypeError(`${label} must have a nonzero namespace and counter`);
|
|
560
|
+
}
|
|
561
|
+
return id;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function positiveU32(value: unknown, label: string): number {
|
|
565
|
+
const output = nonnegativeU32(value, label);
|
|
566
|
+
if (output === 0) throw new TypeError(`${label} must be positive`);
|
|
567
|
+
return output;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function nonnegativeU32(value: unknown, label: string): number {
|
|
571
|
+
if (!Number.isInteger(value) || (value as number) < 0 || (value as number) > UINT32_MAX) {
|
|
572
|
+
throw new TypeError(`${label} must be a uint32`);
|
|
573
|
+
}
|
|
574
|
+
return value as number;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function assertExtent(start: number, length: number, axis: string, index: number): void {
|
|
578
|
+
if (length - 1 > UINT32_MAX - start) {
|
|
579
|
+
throw commandError(index, `${axis} extent exceeds uint32 coordinates`);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function safeLength(value: bigint, label: string): number {
|
|
584
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) throw new RangeError(`${label} length is unsafe`);
|
|
585
|
+
const length = Number(value);
|
|
586
|
+
if (length > SPREADSHEET_ARTIFACT_COMMAND_MAX_BYTES - HEADER_BYTES - CHECKSUM_BYTES) {
|
|
587
|
+
throw new RangeError(`${label} exceeds its byte limit`);
|
|
588
|
+
}
|
|
589
|
+
return length;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function plainRecord(value: unknown, label: string): Record<string, unknown> {
|
|
593
|
+
if (
|
|
594
|
+
typeof value !== "object" ||
|
|
595
|
+
value === null ||
|
|
596
|
+
Array.isArray(value) ||
|
|
597
|
+
Object.getPrototypeOf(value) !== Object.prototype
|
|
598
|
+
) {
|
|
599
|
+
throw new TypeError(`${label} must be a plain record`);
|
|
600
|
+
}
|
|
601
|
+
return value as Record<string, unknown>;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function exactKeys(
|
|
605
|
+
value: Record<string, unknown>,
|
|
606
|
+
expected: readonly string[],
|
|
607
|
+
label: string,
|
|
608
|
+
): void {
|
|
609
|
+
const keys = Reflect.ownKeys(value);
|
|
610
|
+
if (keys.some((key) => typeof key !== "string")) {
|
|
611
|
+
throw new TypeError(`${label} contains unsupported symbol fields`);
|
|
612
|
+
}
|
|
613
|
+
const actual = (keys as string[]).sort(compareCodeUnits);
|
|
614
|
+
if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
|
|
615
|
+
throw new TypeError(`${label} fields must be exactly: ${expected.join(", ")}`);
|
|
616
|
+
}
|
|
617
|
+
for (const key of actual) {
|
|
618
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
619
|
+
if (!descriptor || !("value" in descriptor) || !descriptor.enumerable) {
|
|
620
|
+
throw new TypeError(`${label} fields must be enumerable data properties`);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
function ownArrayElement(value: readonly unknown[], index: number, label: string): unknown {
|
|
626
|
+
if (!Object.hasOwn(value, index)) throw new TypeError(`${label} must be dense`);
|
|
627
|
+
return value[index];
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function commandError(index: number, message: string): TypeError {
|
|
631
|
+
return new TypeError(`spreadsheet command ${index}: ${message}`);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function strictUtf8(value: string, label: string): Uint8Array {
|
|
635
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
636
|
+
const code = value.charCodeAt(index);
|
|
637
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
638
|
+
const next = value.charCodeAt(index + 1);
|
|
639
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) {
|
|
640
|
+
throw new TypeError(`${label} contains an unpaired surrogate`);
|
|
641
|
+
}
|
|
642
|
+
index += 1;
|
|
643
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
644
|
+
throw new TypeError(`${label} contains an unpaired surrogate`);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return textEncoder.encode(value);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function compareCodeUnits(left: string, right: string): number {
|
|
651
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
function fnv1a64(bytes: Uint8Array): bigint {
|
|
655
|
+
let hash = FNV_OFFSET;
|
|
656
|
+
for (const byte of bytes) {
|
|
657
|
+
hash ^= BigInt(byte);
|
|
658
|
+
hash = BigInt.asUintN(64, hash * FNV_PRIME);
|
|
659
|
+
}
|
|
660
|
+
return hash;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function equalBytes(left: Uint8Array, right: Uint8Array): boolean {
|
|
664
|
+
return (
|
|
665
|
+
left.byteLength === right.byteLength && left.every((value, index) => value === right[index])
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function readU64At(bytes: Uint8Array, offset: number): bigint {
|
|
670
|
+
let value = 0n;
|
|
671
|
+
for (let index = 0; index < 8; index += 1) {
|
|
672
|
+
value |= BigInt(bytes[offset + index]!) << BigInt(index * 8);
|
|
673
|
+
}
|
|
674
|
+
return value;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
const STANDARD_ERROR_TAGS = Object.freeze({
|
|
678
|
+
"#NULL!": 0,
|
|
679
|
+
"#DIV/0!": 1,
|
|
680
|
+
"#VALUE!": 2,
|
|
681
|
+
"#REF!": 3,
|
|
682
|
+
"#NAME?": 4,
|
|
683
|
+
"#NUM!": 5,
|
|
684
|
+
"#N/A": 6,
|
|
685
|
+
"#SPILL!": 7,
|
|
686
|
+
"#CALC!": 8,
|
|
687
|
+
} satisfies Record<string, number>);
|
|
688
|
+
|
|
689
|
+
const STANDARD_ERRORS = Object.freeze([
|
|
690
|
+
"#NULL!",
|
|
691
|
+
"#DIV/0!",
|
|
692
|
+
"#VALUE!",
|
|
693
|
+
"#REF!",
|
|
694
|
+
"#NAME?",
|
|
695
|
+
"#NUM!",
|
|
696
|
+
"#N/A",
|
|
697
|
+
"#SPILL!",
|
|
698
|
+
"#CALC!",
|
|
699
|
+
] as const);
|
|
700
|
+
|
|
701
|
+
class BinaryWriter {
|
|
702
|
+
private buffer: Uint8Array;
|
|
703
|
+
private viewBuffer: DataView;
|
|
704
|
+
private length = 0;
|
|
705
|
+
|
|
706
|
+
constructor(private readonly maximum: number) {
|
|
707
|
+
this.buffer = new Uint8Array(Math.min(1_024, maximum));
|
|
708
|
+
this.viewBuffer = new DataView(this.buffer.buffer);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
view(): Uint8Array {
|
|
712
|
+
return this.buffer.subarray(0, this.length);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
finish(): Uint8Array {
|
|
716
|
+
return this.buffer.slice(0, this.length);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
u8(value: number): void {
|
|
720
|
+
this.reserve(1);
|
|
721
|
+
this.viewBuffer.setUint8(this.length, value);
|
|
722
|
+
this.length += 1;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
u16(value: number): void {
|
|
726
|
+
this.reserve(2);
|
|
727
|
+
this.viewBuffer.setUint16(this.length, value, true);
|
|
728
|
+
this.length += 2;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
u32(value: number): void {
|
|
732
|
+
this.reserve(4);
|
|
733
|
+
this.viewBuffer.setUint32(this.length, value, true);
|
|
734
|
+
this.length += 4;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
u64(value: bigint): void {
|
|
738
|
+
this.reserve(8);
|
|
739
|
+
this.viewBuffer.setBigUint64(this.length, value, true);
|
|
740
|
+
this.length += 8;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
i64(value: bigint): void {
|
|
744
|
+
this.reserve(8);
|
|
745
|
+
this.viewBuffer.setBigInt64(this.length, value, true);
|
|
746
|
+
this.length += 8;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
f64(value: number): void {
|
|
750
|
+
this.reserve(8);
|
|
751
|
+
this.viewBuffer.setFloat64(this.length, value === 0 ? 0 : value, true);
|
|
752
|
+
this.length += 8;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
stableId(value: EditableArtifactStableId): void {
|
|
756
|
+
// Text is namespace || counter; canonical model bytes are counter LE then
|
|
757
|
+
// namespace LE, matching Rust `StableId::to_le_bytes`.
|
|
758
|
+
this.u64(BigInt(`0x${value.slice(16)}`));
|
|
759
|
+
this.u64(BigInt(`0x${value.slice(0, 16)}`));
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
string(value: string, maximum: number): void {
|
|
763
|
+
const bytes = strictUtf8(value, "spreadsheet string");
|
|
764
|
+
if (bytes.byteLength > maximum)
|
|
765
|
+
throw new RangeError("spreadsheet string exceeds its byte limit");
|
|
766
|
+
this.u32(bytes.byteLength);
|
|
767
|
+
this.bytes(bytes);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
cell(value: unknown): void {
|
|
771
|
+
if (isFormulaCell(value)) {
|
|
772
|
+
const cell = plainRecord(value, "formula cell");
|
|
773
|
+
exactKeys(cell, ["cached", "formula"], "formula cell");
|
|
774
|
+
if (typeof cell.formula !== "string" || cell.formula.length === 0) {
|
|
775
|
+
throw new TypeError("formula source must be a nonempty string");
|
|
776
|
+
}
|
|
777
|
+
this.u8(1);
|
|
778
|
+
this.string(cell.formula, SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES);
|
|
779
|
+
this.scalar(cell.cached);
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
this.u8(0);
|
|
783
|
+
this.scalar(value);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
private scalar(value: unknown): void {
|
|
787
|
+
if (value === null) {
|
|
788
|
+
this.u8(0);
|
|
789
|
+
} else if (value === false) {
|
|
790
|
+
this.u8(1);
|
|
791
|
+
} else if (value === true) {
|
|
792
|
+
this.u8(2);
|
|
793
|
+
} else if (typeof value === "number") {
|
|
794
|
+
if (!Number.isFinite(value)) throw new TypeError("spreadsheet numbers must be finite");
|
|
795
|
+
this.u8(3);
|
|
796
|
+
this.f64(value);
|
|
797
|
+
} else if (typeof value === "string") {
|
|
798
|
+
this.u8(4);
|
|
799
|
+
this.string(value, SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES);
|
|
800
|
+
} else if (isDateValue(value)) {
|
|
801
|
+
const date = plainRecord(value, "date cell");
|
|
802
|
+
exactKeys(date, ["date"], "date cell");
|
|
803
|
+
this.u8(6);
|
|
804
|
+
this.i64(BigInt(canonicalDateMilliseconds(date.date)));
|
|
805
|
+
} else if (isFormulaError(value)) {
|
|
806
|
+
const error = plainRecord(value, "formula error");
|
|
807
|
+
exactKeys(error, ["error"], "formula error");
|
|
808
|
+
this.u8(5);
|
|
809
|
+
const tag = STANDARD_ERROR_TAGS[error.error as keyof typeof STANDARD_ERROR_TAGS];
|
|
810
|
+
if (tag !== undefined) {
|
|
811
|
+
this.u8(tag);
|
|
812
|
+
} else {
|
|
813
|
+
this.u8(9);
|
|
814
|
+
this.string(error.error as string, SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES);
|
|
815
|
+
}
|
|
816
|
+
} else {
|
|
817
|
+
throw new TypeError("spreadsheet cell has an unsupported value shape");
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
bytes(value: Uint8Array): void {
|
|
822
|
+
this.reserve(value.byteLength);
|
|
823
|
+
this.buffer.set(value, this.length);
|
|
824
|
+
this.length += value.byteLength;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
private reserve(additional: number): void {
|
|
828
|
+
const needed = this.length + additional;
|
|
829
|
+
if (!Number.isSafeInteger(needed) || needed > this.maximum) {
|
|
830
|
+
throw new RangeError("spreadsheet command envelope exceeds its byte limit");
|
|
831
|
+
}
|
|
832
|
+
if (needed <= this.buffer.byteLength) return;
|
|
833
|
+
let size = Math.max(1, this.buffer.byteLength);
|
|
834
|
+
while (size < needed) size = Math.min(this.maximum, size * 2);
|
|
835
|
+
const next = new Uint8Array(size);
|
|
836
|
+
next.set(this.buffer.subarray(0, this.length));
|
|
837
|
+
this.buffer = next;
|
|
838
|
+
this.viewBuffer = new DataView(next.buffer);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
class BinaryReader {
|
|
843
|
+
private offset = 0;
|
|
844
|
+
|
|
845
|
+
constructor(private readonly input: Uint8Array) {}
|
|
846
|
+
|
|
847
|
+
u8(): number {
|
|
848
|
+
return this.bytes(1)[0]!;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
u16(): number {
|
|
852
|
+
const bytes = this.bytes(2);
|
|
853
|
+
return bytes[0]! | (bytes[1]! << 8);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
u32(): number {
|
|
857
|
+
const bytes = this.bytes(4);
|
|
858
|
+
return bytes[0]! + bytes[1]! * 2 ** 8 + bytes[2]! * 2 ** 16 + bytes[3]! * 2 ** 24;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
u64(): bigint {
|
|
862
|
+
const bytes = this.bytes(8);
|
|
863
|
+
let value = 0n;
|
|
864
|
+
for (let index = 0; index < 8; index += 1) {
|
|
865
|
+
value |= BigInt(bytes[index]!) << BigInt(index * 8);
|
|
866
|
+
}
|
|
867
|
+
return value;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
i64(): bigint {
|
|
871
|
+
const bytes = this.bytes(8);
|
|
872
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getBigInt64(0, true);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
f64(): number {
|
|
876
|
+
const bytes = this.bytes(8);
|
|
877
|
+
const value = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getFloat64(
|
|
878
|
+
0,
|
|
879
|
+
true,
|
|
880
|
+
);
|
|
881
|
+
if (!Number.isFinite(value)) throw new TypeError("spreadsheet numbers must be finite");
|
|
882
|
+
if (Object.is(value, -0)) {
|
|
883
|
+
throw new TypeError("spreadsheet numbers must encode zero with a positive sign");
|
|
884
|
+
}
|
|
885
|
+
return value;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
sheetObjectId(): EditableArtifactStableId {
|
|
889
|
+
return sheetObjectId(this.stableIdText(), "sheet id");
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
genericStableId(): EditableArtifactStableId {
|
|
893
|
+
return genericStableId(this.stableIdText(), "stable id");
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
string(maximum: number): string {
|
|
897
|
+
const length = this.u32();
|
|
898
|
+
if (length > maximum) throw new RangeError("spreadsheet string exceeds its byte limit");
|
|
899
|
+
return textDecoder.decode(this.bytes(length));
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
cell(): SpreadsheetCellInput {
|
|
903
|
+
const formulaTag = this.u8();
|
|
904
|
+
if (formulaTag > 1) throw new TypeError("spreadsheet formula presence tag is invalid");
|
|
905
|
+
const formula =
|
|
906
|
+
formulaTag === 1
|
|
907
|
+
? this.nonemptyFormula(this.string(SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES))
|
|
908
|
+
: null;
|
|
909
|
+
const cached = this.scalar();
|
|
910
|
+
return formula === null ? cached : Object.freeze({ formula, cached });
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
done(message: string): void {
|
|
914
|
+
if (this.offset !== this.input.byteLength) throw new TypeError(message);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
bytes(length: number): Uint8Array {
|
|
918
|
+
const end = this.offset + length;
|
|
919
|
+
if (!Number.isSafeInteger(length) || length < 0 || end > this.input.byteLength) {
|
|
920
|
+
throw new TypeError("truncated spreadsheet command envelope");
|
|
921
|
+
}
|
|
922
|
+
const result = this.input.subarray(this.offset, end);
|
|
923
|
+
this.offset = end;
|
|
924
|
+
return result;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
private stableIdText(): string {
|
|
928
|
+
const counter = this.u64().toString(16).padStart(16, "0");
|
|
929
|
+
const namespace = this.u64().toString(16).padStart(16, "0");
|
|
930
|
+
return `${namespace}${counter}`;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
private nonemptyFormula(value: string): string {
|
|
934
|
+
if (value.length === 0) throw new TypeError("formula source must not be empty");
|
|
935
|
+
return value;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
private scalar(): SpreadsheetCellScalar {
|
|
939
|
+
switch (this.u8()) {
|
|
940
|
+
case 0:
|
|
941
|
+
return null;
|
|
942
|
+
case 1:
|
|
943
|
+
return false;
|
|
944
|
+
case 2:
|
|
945
|
+
return true;
|
|
946
|
+
case 3:
|
|
947
|
+
return this.f64();
|
|
948
|
+
case 4:
|
|
949
|
+
return this.string(SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES);
|
|
950
|
+
case 5: {
|
|
951
|
+
const tag = this.u8();
|
|
952
|
+
if (tag <= 8) return Object.freeze({ error: STANDARD_ERRORS[tag]! });
|
|
953
|
+
if (tag === 9) {
|
|
954
|
+
return Object.freeze({
|
|
955
|
+
error: this.string(SPREADSHEET_ARTIFACT_COMMAND_MAX_STRING_BYTES),
|
|
956
|
+
});
|
|
957
|
+
}
|
|
958
|
+
throw new TypeError("spreadsheet formula error tag is invalid");
|
|
959
|
+
}
|
|
960
|
+
case 6:
|
|
961
|
+
return Object.freeze({
|
|
962
|
+
date: canonicalSpreadsheetDateFromMilliseconds(this.i64()),
|
|
963
|
+
});
|
|
964
|
+
default:
|
|
965
|
+
throw new TypeError("spreadsheet scalar tag is invalid");
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
function isFormulaCell(value: unknown): value is SpreadsheetFormulaCell {
|
|
971
|
+
return (
|
|
972
|
+
typeof value === "object" &&
|
|
973
|
+
value !== null &&
|
|
974
|
+
!Array.isArray(value) &&
|
|
975
|
+
Object.hasOwn(value, "formula")
|
|
976
|
+
);
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
function isFormulaError(value: unknown): value is SpreadsheetFormulaError {
|
|
980
|
+
return (
|
|
981
|
+
typeof value === "object" &&
|
|
982
|
+
value !== null &&
|
|
983
|
+
!Array.isArray(value) &&
|
|
984
|
+
typeof (value as Record<string, unknown>).error === "string"
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
function isDateValue(value: unknown): value is SpreadsheetDateValue {
|
|
989
|
+
return (
|
|
990
|
+
typeof value === "object" &&
|
|
991
|
+
value !== null &&
|
|
992
|
+
!Array.isArray(value) &&
|
|
993
|
+
Object.hasOwn(value, "date")
|
|
994
|
+
);
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
function canonicalDateMilliseconds(value: unknown): number {
|
|
998
|
+
return canonicalSpreadsheetDateMilliseconds(value);
|
|
999
|
+
}
|