@jsb188/mday 1.1.8 → 1.1.11
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/package.json +105 -35
- package/src/constants/agentDoc.ts +12 -0
- package/src/constants/agentMemory.ts +154 -0
- package/src/constants/aiChat.ts +5 -0
- package/src/constants/campaign.ts +127 -0
- package/src/constants/dataTable.ts +48 -0
- package/src/constants/dataTableFieldTypes.generated.ts +63 -0
- package/src/constants/docAccess.ts +32 -0
- package/src/constants/email.ts +44 -0
- package/src/constants/fieldDoc.ts +34 -0
- package/src/constants/graphqlReactivity.ts +113 -0
- package/src/constants/inboundContact.ts +8 -0
- package/src/constants/integration.ts +13 -0
- package/src/constants/log.ts +7 -366
- package/src/constants/logActivityGroupColors.generated.ts +19 -0
- package/src/constants/logTaxonomy.generated.ts +1103 -0
- package/src/constants/noteDoc.ts +77 -0
- package/src/constants/organization.ts +18 -11
- package/src/constants/pageRoute.ts +23 -0
- package/src/constants/schedule.ts +220 -0
- package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
- package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
- package/src/constants/sheet.ts +168 -0
- package/src/constants/sheetRegionSources.generated.ts +578 -0
- package/src/constants/square.ts +16 -0
- package/src/constants/storage.ts +61 -0
- package/src/constants/usage.ts +79 -0
- package/src/schemas/editLogEntry.ts +1017 -738
- package/src/schemas/fieldDocsQuery.ts +12 -0
- package/src/schemas/inboundContact.ts +77 -0
- package/src/schemas/logsQuery.ts +57 -316
- package/src/schemas/organization.ts +53 -213
- package/src/types/account.d.ts +81 -0
- package/src/types/agentDoc.d.ts +64 -0
- package/src/types/agentMemory.d.ts +74 -0
- package/src/types/campaign.d.ts +683 -0
- package/src/types/dataTable.d.ts +366 -0
- package/src/types/docAccess.d.ts +96 -0
- package/src/types/email.d.ts +142 -0
- package/src/types/fieldDoc.d.ts +273 -0
- package/src/types/inboundContact.d.ts +57 -0
- package/src/types/integration.d.ts +42 -0
- package/src/types/log.d.ts +169 -190
- package/src/types/noteDoc.d.ts +86 -0
- package/src/types/organization.d.ts +175 -76
- package/src/types/pageRoute.d.ts +48 -0
- package/src/types/schedule.d.ts +391 -0
- package/src/types/sheet.d.ts +1130 -0
- package/src/types/storage.d.ts +242 -0
- package/src/types/usage.d.ts +106 -0
- package/src/types/workflow.d.ts +305 -0
- package/src/utils/agentDoc.ts +109 -0
- package/src/utils/aiChat.ts +26 -0
- package/src/utils/campaign.ts +741 -0
- package/src/utils/campaignFormEditor.ts +871 -0
- package/src/utils/dataTable.ts +849 -0
- package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
- package/src/utils/dataTableRealtimeAuthority.ts +209 -0
- package/src/utils/dataTableRealtimeSelection.ts +134 -0
- package/src/utils/dataTableRowDocument.ts +508 -0
- package/src/utils/dataTableSource.ts +230 -0
- package/src/utils/docAccess.ts +383 -0
- package/src/utils/documentName.ts +53 -0
- package/src/utils/email.ts +172 -0
- package/src/utils/fieldDoc.ts +113 -0
- package/src/utils/log.ts +173 -778
- package/src/utils/logRegistryValue.ts +90 -0
- package/src/utils/logStoreRead.ts +266 -0
- package/src/utils/logTaxonomy.ts +76 -0
- package/src/utils/logTaxonomyDisplay.ts +439 -0
- package/src/utils/noteDoc.ts +827 -0
- package/src/utils/organization.ts +683 -81
- package/src/utils/pageRoute.ts +71 -0
- package/src/utils/schedule.ts +799 -0
- package/src/utils/scheduleEvaluation.ts +1994 -0
- package/src/utils/scheduleICS.ts +246 -0
- package/src/utils/scheduleRuleTemplates.ts +188 -0
- package/src/utils/sheet.ts +4973 -0
- package/src/utils/sheetAxisMove.ts +830 -0
- package/src/utils/sheetBootstrap.ts +1769 -0
- package/src/utils/sheetCommand.ts +423 -0
- package/src/utils/sheetExport.ts +238 -0
- package/src/utils/sheetFormulaEvaluation.ts +914 -0
- package/src/utils/sheetFormulaEvaluator.ts +852 -0
- package/src/utils/sheetJournal.ts +78 -0
- package/src/utils/sheetNumberFormat.ts +1211 -0
- package/src/utils/sheetRegion.ts +374 -0
- package/src/utils/sheetRegionFilter.ts +1259 -0
- package/src/utils/sheetRegionSourceRegistry.ts +594 -0
- package/src/utils/sheetSequence.ts +148 -0
- package/src/utils/sheetSnapshot.ts +992 -0
- package/src/utils/sheetStructureEdit.ts +1284 -0
- package/src/utils/square.ts +32 -0
- package/src/utils/storage.ts +152 -0
- package/src/utils/table-route.ts +262 -0
- package/src/utils/workflow.ts +380 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
- package/tests/docAccess.test.ts +525 -0
- package/tests/editLogEntry.test.ts +112 -0
- package/tests/noteDoc.test.ts +669 -0
- package/tests/schedule.test.ts +693 -0
- package/tests/scheduleEvaluation.test.ts +1970 -0
- package/tests/scheduleICS.test.ts +208 -0
- package/tests/scheduleRuleTemplates.test.ts +176 -0
- package/tests/scheduleSweepInvariants.test.ts +428 -0
- package/tests/sheetAxisMove.test.ts +343 -0
- package/tests/sheetFormulaEvaluator.test.ts +575 -0
- package/tests/sheetRegionFilter.test.ts +1135 -0
- package/tests/sheetSequence.test.ts +41 -0
- package/tests/sheetStructureEdit.test.ts +82 -0
- package/tsconfig.json +12 -0
- package/vitest.config.ts +8 -0
- package/src/constants/log-activityTexts.ts +0 -209
- package/src/constants/product.ts +0 -66
- package/src/constants/report.ts +0 -25
- package/src/schemas/childOrganizationsQuery.ts +0 -44
- package/src/schemas/productsQuery.ts +0 -145
- package/src/schemas/reportsQuery.ts +0 -107
- package/src/types/action.d.ts +0 -42
- package/src/types/product.d.ts +0 -173
- package/src/types/report.d.ts +0 -203
- package/src/utils/product-calEvent.ts +0 -508
- package/src/utils/route-helpers.ts +0 -583
|
@@ -0,0 +1,1769 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SheetAccessInfoGQL,
|
|
3
|
+
SheetAuthoritativeCellGQL,
|
|
4
|
+
SheetAuthoritativeRegionGQL,
|
|
5
|
+
SheetBootstrapGQL,
|
|
6
|
+
SheetBootstrapMode,
|
|
7
|
+
SheetCellGQL,
|
|
8
|
+
SheetDeletedCellCoordGQL,
|
|
9
|
+
SheetGQL,
|
|
10
|
+
SheetGridViewportObj,
|
|
11
|
+
SheetJournalSeq,
|
|
12
|
+
SheetRangeGQL,
|
|
13
|
+
SheetReferencedDataTableSchemaGQL,
|
|
14
|
+
SheetRegionGQL,
|
|
15
|
+
SheetTabChangeGQL,
|
|
16
|
+
SheetTabGQL,
|
|
17
|
+
SheetTabSummaryGQL,
|
|
18
|
+
SheetViewGQL,
|
|
19
|
+
} from '../types/sheet.d.ts';
|
|
20
|
+
import {
|
|
21
|
+
getSheetViewFullViewport,
|
|
22
|
+
normalizeSheetDesign,
|
|
23
|
+
normalizeSheetViewport,
|
|
24
|
+
} from './sheet.ts';
|
|
25
|
+
import {
|
|
26
|
+
isHardNewSheetCellsJournalOrderedTargetGroups,
|
|
27
|
+
SHEET_CELLS_JOURNAL_VERSION,
|
|
28
|
+
} from './sheetJournal.ts';
|
|
29
|
+
import {
|
|
30
|
+
compareSheetJournalSeq,
|
|
31
|
+
parseSheetJournalSeq,
|
|
32
|
+
SHEET_JOURNAL_ZERO_SEQ,
|
|
33
|
+
} from './sheetSequence.ts';
|
|
34
|
+
import {
|
|
35
|
+
decodeSheetSnapshot,
|
|
36
|
+
isSupportedSheetSnapshotEncodingVersion,
|
|
37
|
+
} from './sheetSnapshot.ts';
|
|
38
|
+
|
|
39
|
+
export type SheetBootstrapValidationCode =
|
|
40
|
+
| 'INVALID_RESPONSE'
|
|
41
|
+
| 'INVALID_CONTRACT'
|
|
42
|
+
| 'INVALID_ACCESS'
|
|
43
|
+
| 'INVALID_STATUS'
|
|
44
|
+
| 'SCOPE_MISMATCH'
|
|
45
|
+
| 'INVALID_SEQUENCE'
|
|
46
|
+
| 'INVALID_VIEWPORT'
|
|
47
|
+
| 'INVALID_SNAPSHOT'
|
|
48
|
+
| 'INVALID_CHANGE';
|
|
49
|
+
|
|
50
|
+
export type ValidatedSheetBootstrapAccess = {
|
|
51
|
+
sheetId: string;
|
|
52
|
+
organizationId: string | null;
|
|
53
|
+
canView: boolean;
|
|
54
|
+
canEdit: boolean;
|
|
55
|
+
signInRequired: boolean;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type SheetBootstrapBaseSnapshotIdentity = Readonly<{
|
|
59
|
+
organizationId: string;
|
|
60
|
+
sheetId: string;
|
|
61
|
+
sheetTabId: string;
|
|
62
|
+
seq: SheetJournalSeq;
|
|
63
|
+
encodingVersion: number;
|
|
64
|
+
checksum: string;
|
|
65
|
+
viewport: Readonly<SheetGridViewportObj>;
|
|
66
|
+
}>;
|
|
67
|
+
|
|
68
|
+
export type SheetBootstrapTabState = Readonly<{
|
|
69
|
+
organizationId: string;
|
|
70
|
+
sheetId: string;
|
|
71
|
+
sheetTabId: string;
|
|
72
|
+
baseSnapshot: SheetBootstrapBaseSnapshotIdentity;
|
|
73
|
+
appliedSeq: SheetJournalSeq;
|
|
74
|
+
view: SheetViewGQL;
|
|
75
|
+
}>;
|
|
76
|
+
|
|
77
|
+
export type SheetBootstrapDeniedResult = {
|
|
78
|
+
contractVersion: typeof SHEET_BOOTSTRAP_CONTRACT_VERSION;
|
|
79
|
+
status: 'DENIED';
|
|
80
|
+
access: ValidatedSheetBootstrapAccess;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export type SheetBootstrapDeferredResult = {
|
|
84
|
+
contractVersion: typeof SHEET_BOOTSTRAP_CONTRACT_VERSION;
|
|
85
|
+
status: 'RETRY' | 'UNAVAILABLE';
|
|
86
|
+
access: ValidatedSheetBootstrapAccess;
|
|
87
|
+
unavailableReason: string;
|
|
88
|
+
sheet: SheetGQL | null;
|
|
89
|
+
tabs: SheetTabSummaryGQL[];
|
|
90
|
+
selectedTab: SheetTabGQL | null;
|
|
91
|
+
referencedDataTables: SheetReferencedDataTableSchemaGQL[];
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export type SheetBootstrapReadyResult = {
|
|
95
|
+
contractVersion: typeof SHEET_BOOTSTRAP_CONTRACT_VERSION;
|
|
96
|
+
status: 'READY';
|
|
97
|
+
access: ValidatedSheetBootstrapAccess;
|
|
98
|
+
realtimeToken: string;
|
|
99
|
+
sheet: SheetGQL;
|
|
100
|
+
tabs: SheetTabSummaryGQL[];
|
|
101
|
+
selectedTab: SheetTabGQL | null;
|
|
102
|
+
mode: SheetBootstrapMode;
|
|
103
|
+
currentSeq: SheetJournalSeq;
|
|
104
|
+
servedViewport: SheetGridViewportObj;
|
|
105
|
+
referencedDataTables: SheetReferencedDataTableSchemaGQL[];
|
|
106
|
+
state: SheetBootstrapTabState | null;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export type SheetBootstrapApplyResult =
|
|
110
|
+
| SheetBootstrapDeniedResult
|
|
111
|
+
| SheetBootstrapDeferredResult
|
|
112
|
+
| SheetBootstrapReadyResult;
|
|
113
|
+
|
|
114
|
+
export type SheetBootstrapSnapshotScope = {
|
|
115
|
+
organizationId: string;
|
|
116
|
+
sheetId: string;
|
|
117
|
+
sheetTabId: string;
|
|
118
|
+
viewport: SheetGridViewportObj;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
type SheetBootstrapScope = SheetBootstrapSnapshotScope;
|
|
122
|
+
|
|
123
|
+
type SheetBootstrapCellPatch = {
|
|
124
|
+
cells: SheetAuthoritativeCellGQL[];
|
|
125
|
+
deletedCellCoords: SheetDeletedCellCoordGQL[];
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export const SHEET_BOOTSTRAP_CONTRACT_VERSION = 2;
|
|
129
|
+
|
|
130
|
+
const FILE_CONSTANTS = {
|
|
131
|
+
checksumPattern: /^[a-f0-9]{64}$/,
|
|
132
|
+
canonicalIntegerPattern: /^(0|[1-9]\d*)$/,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Error raised when an openSheet response cannot safely become local state.
|
|
137
|
+
*/
|
|
138
|
+
export class SheetBootstrapValidationError extends Error {
|
|
139
|
+
readonly code: SheetBootstrapValidationCode;
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
* Preserve a stable machine-readable failure code for snapshot fallback and
|
|
143
|
+
* telemetry while retaining a precise human-readable integrity failure.
|
|
144
|
+
*/
|
|
145
|
+
constructor(code: SheetBootstrapValidationCode, message: string) {
|
|
146
|
+
super(message);
|
|
147
|
+
this.name = 'SheetBootstrapValidationError';
|
|
148
|
+
this.code = code;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/*
|
|
153
|
+
* Throw one typed bootstrap validation failure.
|
|
154
|
+
*/
|
|
155
|
+
function failSheetBootstrap(
|
|
156
|
+
code: SheetBootstrapValidationCode,
|
|
157
|
+
message: string,
|
|
158
|
+
): never {
|
|
159
|
+
throw new SheetBootstrapValidationError(code, message);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
* Return one non-array object or fail closed on malformed transport data.
|
|
164
|
+
*/
|
|
165
|
+
function requireSheetBootstrapRecord(
|
|
166
|
+
value: unknown,
|
|
167
|
+
label: string,
|
|
168
|
+
code: SheetBootstrapValidationCode = 'INVALID_RESPONSE',
|
|
169
|
+
): Record<string, any> {
|
|
170
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
171
|
+
return failSheetBootstrap(code, `${label} must be an object.`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return value as Record<string, any>;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/*
|
|
178
|
+
* Validate the explicit client/server bootstrap contract.
|
|
179
|
+
*/
|
|
180
|
+
function validateSheetBootstrapContract(bootstrap: SheetBootstrapGQL): {
|
|
181
|
+
contractVersion: typeof SHEET_BOOTSTRAP_CONTRACT_VERSION;
|
|
182
|
+
} {
|
|
183
|
+
if (bootstrap.contractVersion !== SHEET_BOOTSTRAP_CONTRACT_VERSION) {
|
|
184
|
+
return failSheetBootstrap(
|
|
185
|
+
'INVALID_CONTRACT',
|
|
186
|
+
`Unsupported openSheet contract version: ${String(bootstrap.contractVersion)}.`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
contractVersion: SHEET_BOOTSTRAP_CONTRACT_VERSION,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/*
|
|
195
|
+
* Return whether a response carries any authorization-scoped workbook data.
|
|
196
|
+
*/
|
|
197
|
+
function sheetBootstrapHasScopedReadData(
|
|
198
|
+
bootstrap: SheetBootstrapGQL,
|
|
199
|
+
): boolean {
|
|
200
|
+
return (
|
|
201
|
+
bootstrap.sheet != null ||
|
|
202
|
+
bootstrap.tabs != null ||
|
|
203
|
+
bootstrap.selectedTab != null ||
|
|
204
|
+
bootstrap.mode != null ||
|
|
205
|
+
bootstrap.currentSeq != null ||
|
|
206
|
+
bootstrap.servedViewport != null ||
|
|
207
|
+
bootstrap.snapshot != null ||
|
|
208
|
+
bootstrap.changes != null ||
|
|
209
|
+
bootstrap.referencedDataTables != null ||
|
|
210
|
+
bootstrap.realtimeToken != null
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/*
|
|
215
|
+
* Return one bounded signed realtime capability without normalizing its bytes.
|
|
216
|
+
*/
|
|
217
|
+
function requireSheetBootstrapRealtimeToken(
|
|
218
|
+
value: unknown,
|
|
219
|
+
label: string,
|
|
220
|
+
): string {
|
|
221
|
+
if (
|
|
222
|
+
typeof value !== 'string' ||
|
|
223
|
+
!value ||
|
|
224
|
+
value !== value.trim() ||
|
|
225
|
+
value.length > 16_384
|
|
226
|
+
) {
|
|
227
|
+
return failSheetBootstrap(
|
|
228
|
+
'INVALID_ACCESS',
|
|
229
|
+
`${label} must be a bounded non-empty string.`,
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return value;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/*
|
|
237
|
+
* Return a required non-empty GraphQL identifier as an exact string.
|
|
238
|
+
*/
|
|
239
|
+
function requireSheetBootstrapId(
|
|
240
|
+
value: unknown,
|
|
241
|
+
label: string,
|
|
242
|
+
code: SheetBootstrapValidationCode = 'SCOPE_MISMATCH',
|
|
243
|
+
): string {
|
|
244
|
+
if (typeof value !== 'string' || !value) {
|
|
245
|
+
return failSheetBootstrap(code, `${label} must be a non-empty string.`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return value;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/*
|
|
252
|
+
* Return a canonical on-wire journal sequence without ever using Number math.
|
|
253
|
+
*/
|
|
254
|
+
function requireSheetBootstrapSeq(
|
|
255
|
+
value: unknown,
|
|
256
|
+
label: string,
|
|
257
|
+
): SheetJournalSeq {
|
|
258
|
+
if (typeof value !== 'string') {
|
|
259
|
+
return failSheetBootstrap(
|
|
260
|
+
'INVALID_SEQUENCE',
|
|
261
|
+
`${label} must be a canonical base-10 string.`,
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const parsed = parseSheetJournalSeq(value);
|
|
266
|
+
if (parsed === null || parsed !== value) {
|
|
267
|
+
return failSheetBootstrap(
|
|
268
|
+
'INVALID_SEQUENCE',
|
|
269
|
+
`${label} is not a canonical journal sequence.`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return parsed;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/*
|
|
277
|
+
* Return a required positive grid index as an exact integer.
|
|
278
|
+
*/
|
|
279
|
+
function requireSheetBootstrapGridIndex(value: unknown, label: string): number {
|
|
280
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value <= 0) {
|
|
281
|
+
return failSheetBootstrap(
|
|
282
|
+
'INVALID_VIEWPORT',
|
|
283
|
+
`${label} must be a positive safe integer.`,
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return value;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/*
|
|
291
|
+
* Return a required non-negative integer metadata value.
|
|
292
|
+
*/
|
|
293
|
+
function requireSheetBootstrapCount(value: unknown, label: string): number {
|
|
294
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) {
|
|
295
|
+
return failSheetBootstrap(
|
|
296
|
+
'INVALID_SNAPSHOT',
|
|
297
|
+
`${label} must be a non-negative safe integer.`,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return value;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/*
|
|
305
|
+
* Validate and copy one full-grid viewport.
|
|
306
|
+
*/
|
|
307
|
+
function requireSheetBootstrapViewport(
|
|
308
|
+
value: unknown,
|
|
309
|
+
label: string,
|
|
310
|
+
): SheetGridViewportObj {
|
|
311
|
+
const viewport = requireSheetBootstrapRecord(
|
|
312
|
+
value,
|
|
313
|
+
label,
|
|
314
|
+
'INVALID_VIEWPORT',
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
startRowIndex: requireSheetBootstrapGridIndex(
|
|
319
|
+
viewport.startRowIndex,
|
|
320
|
+
`${label}.startRowIndex`,
|
|
321
|
+
),
|
|
322
|
+
startColumnIndex: requireSheetBootstrapGridIndex(
|
|
323
|
+
viewport.startColumnIndex,
|
|
324
|
+
`${label}.startColumnIndex`,
|
|
325
|
+
),
|
|
326
|
+
rowCount: requireSheetBootstrapGridIndex(
|
|
327
|
+
viewport.rowCount,
|
|
328
|
+
`${label}.rowCount`,
|
|
329
|
+
),
|
|
330
|
+
columnCount: requireSheetBootstrapGridIndex(
|
|
331
|
+
viewport.columnCount,
|
|
332
|
+
`${label}.columnCount`,
|
|
333
|
+
),
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/*
|
|
338
|
+
* Return whether two viewport values describe the exact same grid rectangle.
|
|
339
|
+
*/
|
|
340
|
+
function sheetBootstrapViewportsAreEqual(
|
|
341
|
+
left: SheetGridViewportObj,
|
|
342
|
+
right: SheetGridViewportObj,
|
|
343
|
+
): boolean {
|
|
344
|
+
return (
|
|
345
|
+
left.startRowIndex === right.startRowIndex &&
|
|
346
|
+
left.startColumnIndex === right.startColumnIndex &&
|
|
347
|
+
left.rowCount === right.rowCount &&
|
|
348
|
+
left.columnCount === right.columnCount
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/*
|
|
353
|
+
* Require an exact viewport match at a negotiated integrity boundary.
|
|
354
|
+
*/
|
|
355
|
+
function requireMatchingSheetBootstrapViewport(
|
|
356
|
+
actual: SheetGridViewportObj,
|
|
357
|
+
expected: SheetGridViewportObj,
|
|
358
|
+
label: string,
|
|
359
|
+
): void {
|
|
360
|
+
if (!sheetBootstrapViewportsAreEqual(actual, expected)) {
|
|
361
|
+
failSheetBootstrap(
|
|
362
|
+
'INVALID_VIEWPORT',
|
|
363
|
+
`${label} does not match the negotiated full-tab viewport.`,
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/*
|
|
369
|
+
* Return the canonical full viewport implied by one selected tab design.
|
|
370
|
+
*/
|
|
371
|
+
function getExpectedSheetBootstrapViewport(
|
|
372
|
+
selectedTab: SheetTabGQL,
|
|
373
|
+
): SheetGridViewportObj {
|
|
374
|
+
return getSheetViewFullViewport(
|
|
375
|
+
normalizeSheetDesign({
|
|
376
|
+
grid: selectedTab.design?.grid as any,
|
|
377
|
+
}),
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/*
|
|
382
|
+
* Validate the embedded effective access facts before reading scoped data.
|
|
383
|
+
*/
|
|
384
|
+
function validateSheetBootstrapAccess(
|
|
385
|
+
value: unknown,
|
|
386
|
+
expectedSheetId: string,
|
|
387
|
+
): ValidatedSheetBootstrapAccess {
|
|
388
|
+
const access = requireSheetBootstrapRecord(
|
|
389
|
+
value,
|
|
390
|
+
'openSheet.access',
|
|
391
|
+
'INVALID_ACCESS',
|
|
392
|
+
) as SheetAccessInfoGQL;
|
|
393
|
+
const sheetId = requireSheetBootstrapId(
|
|
394
|
+
access.sheetId,
|
|
395
|
+
'openSheet.access.sheetId',
|
|
396
|
+
'INVALID_ACCESS',
|
|
397
|
+
);
|
|
398
|
+
if (sheetId !== expectedSheetId) {
|
|
399
|
+
failSheetBootstrap(
|
|
400
|
+
'INVALID_ACCESS',
|
|
401
|
+
'openSheet access belongs to a different sheet.',
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (
|
|
406
|
+
typeof access.canView !== 'boolean' ||
|
|
407
|
+
typeof access.canEdit !== 'boolean' ||
|
|
408
|
+
typeof access.signInRequired !== 'boolean'
|
|
409
|
+
) {
|
|
410
|
+
failSheetBootstrap(
|
|
411
|
+
'INVALID_ACCESS',
|
|
412
|
+
'openSheet access flags must be explicit booleans.',
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const organizationId =
|
|
417
|
+
access.organizationId === null || access.organizationId === undefined
|
|
418
|
+
? null
|
|
419
|
+
: requireSheetBootstrapId(
|
|
420
|
+
access.organizationId,
|
|
421
|
+
'openSheet.access.organizationId',
|
|
422
|
+
'INVALID_ACCESS',
|
|
423
|
+
);
|
|
424
|
+
if (
|
|
425
|
+
(access.canView && !organizationId) ||
|
|
426
|
+
(access.canEdit && !access.canView) ||
|
|
427
|
+
(access.signInRequired && access.canView)
|
|
428
|
+
) {
|
|
429
|
+
failSheetBootstrap(
|
|
430
|
+
'INVALID_ACCESS',
|
|
431
|
+
'openSheet access flags are internally inconsistent.',
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return {
|
|
436
|
+
sheetId,
|
|
437
|
+
organizationId,
|
|
438
|
+
canView: access.canView,
|
|
439
|
+
canEdit: access.canEdit,
|
|
440
|
+
signInRequired: access.signInRequired,
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/*
|
|
445
|
+
* Validate one workbook's identity against the authorized bootstrap scope.
|
|
446
|
+
*/
|
|
447
|
+
function validateSheetBootstrapWorkbook(
|
|
448
|
+
value: unknown,
|
|
449
|
+
access: ValidatedSheetBootstrapAccess,
|
|
450
|
+
): SheetGQL {
|
|
451
|
+
const sheet = requireSheetBootstrapRecord(
|
|
452
|
+
value,
|
|
453
|
+
'openSheet.sheet',
|
|
454
|
+
) as SheetGQL;
|
|
455
|
+
if (
|
|
456
|
+
requireSheetBootstrapId(sheet.id, 'openSheet.sheet.id') !==
|
|
457
|
+
access.sheetId ||
|
|
458
|
+
requireSheetBootstrapId(
|
|
459
|
+
sheet.organizationId,
|
|
460
|
+
'openSheet.sheet.organizationId',
|
|
461
|
+
) !== access.organizationId
|
|
462
|
+
) {
|
|
463
|
+
failSheetBootstrap(
|
|
464
|
+
'SCOPE_MISMATCH',
|
|
465
|
+
'openSheet workbook does not match its authorized scope.',
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
return sheet;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/*
|
|
473
|
+
* Validate tab summaries, their exact cursors, and same-workbook references.
|
|
474
|
+
*/
|
|
475
|
+
function validateSheetBootstrapTabSummaries(
|
|
476
|
+
value: unknown,
|
|
477
|
+
): SheetTabSummaryGQL[] {
|
|
478
|
+
if (!Array.isArray(value)) {
|
|
479
|
+
return failSheetBootstrap(
|
|
480
|
+
'INVALID_RESPONSE',
|
|
481
|
+
'openSheet.tabs must be an array.',
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const tabIds = new Set<string>();
|
|
486
|
+
const tabs = value.map((item, index) => {
|
|
487
|
+
const tab = requireSheetBootstrapRecord(
|
|
488
|
+
item,
|
|
489
|
+
`openSheet.tabs[${index}]`,
|
|
490
|
+
) as SheetTabSummaryGQL;
|
|
491
|
+
const tabId = requireSheetBootstrapId(
|
|
492
|
+
tab.id,
|
|
493
|
+
`openSheet.tabs[${index}].id`,
|
|
494
|
+
);
|
|
495
|
+
if (tabIds.has(tabId)) {
|
|
496
|
+
failSheetBootstrap(
|
|
497
|
+
'SCOPE_MISMATCH',
|
|
498
|
+
'openSheet contains duplicate tab summaries.',
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
tabIds.add(tabId);
|
|
502
|
+
if (
|
|
503
|
+
typeof tab.name !== 'string' ||
|
|
504
|
+
!tab.name.trim() ||
|
|
505
|
+
tab.name !== tab.name.trim()
|
|
506
|
+
) {
|
|
507
|
+
failSheetBootstrap(
|
|
508
|
+
'INVALID_RESPONSE',
|
|
509
|
+
`openSheet.tabs[${index}].name must be a canonical non-empty string.`,
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
requireSheetBootstrapSeq(
|
|
513
|
+
tab.currentSeq,
|
|
514
|
+
`openSheet.tabs[${index}].currentSeq`,
|
|
515
|
+
);
|
|
516
|
+
if (!Array.isArray(tab.referencedTabIds)) {
|
|
517
|
+
failSheetBootstrap(
|
|
518
|
+
'INVALID_RESPONSE',
|
|
519
|
+
`openSheet.tabs[${index}].referencedTabIds must be an array.`,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return tab;
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
tabs.forEach((tab, tabIndex) => {
|
|
527
|
+
const referenceIds = new Set<string>();
|
|
528
|
+
(tab.referencedTabIds || []).forEach((referenceId, referenceIndex) => {
|
|
529
|
+
const id = requireSheetBootstrapId(
|
|
530
|
+
referenceId,
|
|
531
|
+
`openSheet.tabs[${tabIndex}].referencedTabIds[${referenceIndex}]`,
|
|
532
|
+
);
|
|
533
|
+
if (!tabIds.has(id) || referenceIds.has(id)) {
|
|
534
|
+
failSheetBootstrap(
|
|
535
|
+
'SCOPE_MISMATCH',
|
|
536
|
+
'openSheet tab references must be unique visible workbook tabs.',
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
referenceIds.add(id);
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
return tabs;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/*
|
|
547
|
+
* Validate one selected tab against its workbook and access ancestry.
|
|
548
|
+
*/
|
|
549
|
+
function validateSelectedSheetBootstrapTab(
|
|
550
|
+
value: unknown,
|
|
551
|
+
access: ValidatedSheetBootstrapAccess,
|
|
552
|
+
tabs: SheetTabSummaryGQL[],
|
|
553
|
+
): SheetTabGQL {
|
|
554
|
+
const selectedTab = requireSheetBootstrapRecord(
|
|
555
|
+
value,
|
|
556
|
+
'openSheet.selectedTab',
|
|
557
|
+
) as SheetTabGQL;
|
|
558
|
+
const selectedTabId = requireSheetBootstrapId(
|
|
559
|
+
selectedTab.id,
|
|
560
|
+
'openSheet.selectedTab.id',
|
|
561
|
+
);
|
|
562
|
+
if (
|
|
563
|
+
requireSheetBootstrapId(
|
|
564
|
+
selectedTab.organizationId,
|
|
565
|
+
'openSheet.selectedTab.organizationId',
|
|
566
|
+
) !== access.organizationId ||
|
|
567
|
+
requireSheetBootstrapId(
|
|
568
|
+
selectedTab.sheetId,
|
|
569
|
+
'openSheet.selectedTab.sheetId',
|
|
570
|
+
) !== access.sheetId ||
|
|
571
|
+
!tabs.some((tab) => tab.id === selectedTabId)
|
|
572
|
+
) {
|
|
573
|
+
failSheetBootstrap(
|
|
574
|
+
'SCOPE_MISMATCH',
|
|
575
|
+
'openSheet selected tab is outside the authorized workbook.',
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
requireSheetBootstrapSeq(
|
|
579
|
+
selectedTab.designRevision,
|
|
580
|
+
'openSheet.selectedTab.designRevision',
|
|
581
|
+
);
|
|
582
|
+
|
|
583
|
+
return selectedTab;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/*
|
|
587
|
+
* Validate the small referenced-table schema set and reject duplicate ids.
|
|
588
|
+
*/
|
|
589
|
+
function validateSheetBootstrapReferencedTables(
|
|
590
|
+
value: unknown,
|
|
591
|
+
): SheetReferencedDataTableSchemaGQL[] {
|
|
592
|
+
if (!Array.isArray(value)) {
|
|
593
|
+
return failSheetBootstrap(
|
|
594
|
+
'INVALID_RESPONSE',
|
|
595
|
+
'openSheet.referencedDataTables must be an array.',
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const ids = new Set<string>();
|
|
600
|
+
return value.map((item, index) => {
|
|
601
|
+
const table = requireSheetBootstrapRecord(
|
|
602
|
+
item,
|
|
603
|
+
`openSheet.referencedDataTables[${index}]`,
|
|
604
|
+
) as SheetReferencedDataTableSchemaGQL;
|
|
605
|
+
const id = requireSheetBootstrapId(
|
|
606
|
+
table.id,
|
|
607
|
+
`openSheet.referencedDataTables[${index}].id`,
|
|
608
|
+
);
|
|
609
|
+
if (ids.has(id)) {
|
|
610
|
+
failSheetBootstrap(
|
|
611
|
+
'SCOPE_MISMATCH',
|
|
612
|
+
'openSheet contains duplicate referenced table schemas.',
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
requireSheetBootstrapRealtimeToken(
|
|
616
|
+
table.realtimeToken,
|
|
617
|
+
`openSheet.referencedDataTables[${index}].realtimeToken`,
|
|
618
|
+
);
|
|
619
|
+
ids.add(id);
|
|
620
|
+
return table;
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/*
|
|
625
|
+
* Require one tab-owned record to carry the exact authorized ancestry.
|
|
626
|
+
*/
|
|
627
|
+
function validateSheetBootstrapRecordScope(
|
|
628
|
+
value: Record<string, any>,
|
|
629
|
+
scope: SheetBootstrapScope,
|
|
630
|
+
label: string,
|
|
631
|
+
): void {
|
|
632
|
+
if (
|
|
633
|
+
requireSheetBootstrapId(value.organizationId, `${label}.organizationId`) !==
|
|
634
|
+
scope.organizationId ||
|
|
635
|
+
requireSheetBootstrapId(value.sheetId, `${label}.sheetId`) !==
|
|
636
|
+
scope.sheetId ||
|
|
637
|
+
requireSheetBootstrapId(value.sheetTabId, `${label}.sheetTabId`) !==
|
|
638
|
+
scope.sheetTabId
|
|
639
|
+
) {
|
|
640
|
+
failSheetBootstrap(
|
|
641
|
+
'SCOPE_MISMATCH',
|
|
642
|
+
`${label} belongs to a different sheet scope.`,
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/*
|
|
648
|
+
* Validate one coordinate and require it to be inside the served viewport.
|
|
649
|
+
*/
|
|
650
|
+
function validateSheetBootstrapCoordinate(
|
|
651
|
+
value: Record<string, any>,
|
|
652
|
+
scope: SheetBootstrapScope,
|
|
653
|
+
label: string,
|
|
654
|
+
): { rowIndex: number; columnIndex: number } {
|
|
655
|
+
const rowIndex = requireSheetBootstrapGridIndex(
|
|
656
|
+
value.rowIndex,
|
|
657
|
+
`${label}.rowIndex`,
|
|
658
|
+
);
|
|
659
|
+
const columnIndex = requireSheetBootstrapGridIndex(
|
|
660
|
+
value.columnIndex,
|
|
661
|
+
`${label}.columnIndex`,
|
|
662
|
+
);
|
|
663
|
+
const lastRowIndex =
|
|
664
|
+
scope.viewport.startRowIndex + scope.viewport.rowCount - 1;
|
|
665
|
+
const lastColumnIndex =
|
|
666
|
+
scope.viewport.startColumnIndex + scope.viewport.columnCount - 1;
|
|
667
|
+
if (
|
|
668
|
+
rowIndex < scope.viewport.startRowIndex ||
|
|
669
|
+
rowIndex > lastRowIndex ||
|
|
670
|
+
columnIndex < scope.viewport.startColumnIndex ||
|
|
671
|
+
columnIndex > lastColumnIndex
|
|
672
|
+
) {
|
|
673
|
+
failSheetBootstrap(
|
|
674
|
+
'INVALID_VIEWPORT',
|
|
675
|
+
`${label} is outside the served viewport.`,
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
return { rowIndex, columnIndex };
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/*
|
|
683
|
+
* Validate one replay cell and return its canonical coordinate key.
|
|
684
|
+
*/
|
|
685
|
+
function validateSheetBootstrapCell(
|
|
686
|
+
value: unknown,
|
|
687
|
+
scope: SheetBootstrapScope,
|
|
688
|
+
label: string,
|
|
689
|
+
): string {
|
|
690
|
+
const cell = requireSheetBootstrapRecord(value, label, 'INVALID_CHANGE');
|
|
691
|
+
validateSheetBootstrapRecordScope(cell, scope, label);
|
|
692
|
+
requireSheetBootstrapId(cell.id, `${label}.id`, 'INVALID_CHANGE');
|
|
693
|
+
requireSheetBootstrapSeq(cell.revision, `${label}.revision`);
|
|
694
|
+
const coordinate = validateSheetBootstrapCoordinate(cell, scope, label);
|
|
695
|
+
|
|
696
|
+
return `${coordinate.rowIndex}:${coordinate.columnIndex}`;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/*
|
|
700
|
+
* Validate one deletion tombstone and return its canonical coordinate key.
|
|
701
|
+
*/
|
|
702
|
+
function validateSheetBootstrapDeletedCoordinate(
|
|
703
|
+
value: unknown,
|
|
704
|
+
scope: SheetBootstrapScope,
|
|
705
|
+
label: string,
|
|
706
|
+
): string {
|
|
707
|
+
const coordinate = requireSheetBootstrapRecord(
|
|
708
|
+
value,
|
|
709
|
+
label,
|
|
710
|
+
'INVALID_CHANGE',
|
|
711
|
+
);
|
|
712
|
+
if (
|
|
713
|
+
requireSheetBootstrapId(
|
|
714
|
+
coordinate.sheetTabId,
|
|
715
|
+
`${label}.sheetTabId`,
|
|
716
|
+
'INVALID_CHANGE',
|
|
717
|
+
) !== scope.sheetTabId
|
|
718
|
+
) {
|
|
719
|
+
failSheetBootstrap(
|
|
720
|
+
'SCOPE_MISMATCH',
|
|
721
|
+
`${label} belongs to a different Sheet tab.`,
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
const indexes = validateSheetBootstrapCoordinate(coordinate, scope, label);
|
|
725
|
+
if (
|
|
726
|
+
coordinate.id !== null &&
|
|
727
|
+
coordinate.id !== undefined &&
|
|
728
|
+
(typeof coordinate.id !== 'string' || !coordinate.id)
|
|
729
|
+
) {
|
|
730
|
+
failSheetBootstrap(
|
|
731
|
+
'INVALID_CHANGE',
|
|
732
|
+
`${label}.id must be a non-empty string when present.`,
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
return `${indexes.rowIndex}:${indexes.columnIndex}`;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/*
|
|
740
|
+
* Validate one decoded formatting range and its full-tab ancestry and bounds.
|
|
741
|
+
*/
|
|
742
|
+
function validateSheetBootstrapRange(
|
|
743
|
+
value: SheetRangeGQL,
|
|
744
|
+
scope: SheetBootstrapScope,
|
|
745
|
+
index: number,
|
|
746
|
+
): void {
|
|
747
|
+
const label = `snapshot.ranges[${index}]`;
|
|
748
|
+
const range = requireSheetBootstrapRecord(value, label, 'INVALID_SNAPSHOT');
|
|
749
|
+
validateSheetBootstrapRecordScope(range, scope, label);
|
|
750
|
+
requireSheetBootstrapId(range.id, `${label}.id`, 'INVALID_SNAPSHOT');
|
|
751
|
+
const start = validateSheetBootstrapCoordinate(
|
|
752
|
+
{
|
|
753
|
+
rowIndex: range.startRowIndex,
|
|
754
|
+
columnIndex: range.startColumnIndex,
|
|
755
|
+
},
|
|
756
|
+
scope,
|
|
757
|
+
`${label}.start`,
|
|
758
|
+
);
|
|
759
|
+
const end = validateSheetBootstrapCoordinate(
|
|
760
|
+
{
|
|
761
|
+
rowIndex: range.endRowIndex,
|
|
762
|
+
columnIndex: range.endColumnIndex,
|
|
763
|
+
},
|
|
764
|
+
scope,
|
|
765
|
+
`${label}.end`,
|
|
766
|
+
);
|
|
767
|
+
if (end.rowIndex < start.rowIndex || end.columnIndex < start.columnIndex) {
|
|
768
|
+
failSheetBootstrap('INVALID_SNAPSHOT', `${label} has inverted bounds.`);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/*
|
|
773
|
+
* Validate one decoded region's ancestry and anchor coordinate.
|
|
774
|
+
*/
|
|
775
|
+
function validateSheetBootstrapRegion(
|
|
776
|
+
value: SheetRegionGQL,
|
|
777
|
+
scope: SheetBootstrapScope,
|
|
778
|
+
index: number,
|
|
779
|
+
): void {
|
|
780
|
+
const label = `snapshot.regions[${index}]`;
|
|
781
|
+
const region = requireSheetBootstrapRecord(value, label, 'INVALID_SNAPSHOT');
|
|
782
|
+
validateSheetBootstrapRecordScope(region, scope, label);
|
|
783
|
+
requireSheetBootstrapId(region.id, `${label}.id`, 'INVALID_SNAPSHOT');
|
|
784
|
+
validateSheetBootstrapCoordinate(
|
|
785
|
+
{
|
|
786
|
+
rowIndex: region.startRowIndex,
|
|
787
|
+
columnIndex: region.startColumnIndex,
|
|
788
|
+
},
|
|
789
|
+
scope,
|
|
790
|
+
`${label}.start`,
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/*
|
|
795
|
+
* Compute the lowercase SHA-256 checksum for an exact UTF-8 snapshot payload.
|
|
796
|
+
*/
|
|
797
|
+
export async function getSheetBootstrapPayloadChecksum(
|
|
798
|
+
payload: string,
|
|
799
|
+
): Promise<string> {
|
|
800
|
+
if (!globalThis.crypto?.subtle) {
|
|
801
|
+
return failSheetBootstrap(
|
|
802
|
+
'INVALID_SNAPSHOT',
|
|
803
|
+
'SHA-256 is unavailable in this runtime.',
|
|
804
|
+
);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
const digest = await globalThis.crypto.subtle.digest(
|
|
808
|
+
'SHA-256',
|
|
809
|
+
new TextEncoder().encode(payload),
|
|
810
|
+
);
|
|
811
|
+
|
|
812
|
+
return Array.from(new Uint8Array(digest), (byte) =>
|
|
813
|
+
byte.toString(16).padStart(2, '0'),
|
|
814
|
+
).join('');
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/*
|
|
818
|
+
* Decode and verify every immutable snapshot integrity field before replay.
|
|
819
|
+
*/
|
|
820
|
+
export async function decodeVerifiedSheetBootstrapSnapshot(params: {
|
|
821
|
+
snapshot: unknown;
|
|
822
|
+
currentSeq: SheetJournalSeq;
|
|
823
|
+
scope: SheetBootstrapSnapshotScope;
|
|
824
|
+
}): Promise<{
|
|
825
|
+
seq: SheetJournalSeq;
|
|
826
|
+
encodingVersion: number;
|
|
827
|
+
checksum: string;
|
|
828
|
+
data: ReturnType<typeof decodeSheetSnapshot>;
|
|
829
|
+
}> {
|
|
830
|
+
const snapshot = requireSheetBootstrapRecord(
|
|
831
|
+
params.snapshot,
|
|
832
|
+
'openSheet.snapshot',
|
|
833
|
+
'INVALID_SNAPSHOT',
|
|
834
|
+
);
|
|
835
|
+
const seq = requireSheetBootstrapSeq(snapshot.seq, 'openSheet.snapshot.seq');
|
|
836
|
+
if (compareSheetJournalSeq(seq, params.currentSeq) > 0) {
|
|
837
|
+
failSheetBootstrap(
|
|
838
|
+
'INVALID_SEQUENCE',
|
|
839
|
+
'openSheet snapshot sequence is ahead of the selected tab.',
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
if (!isSupportedSheetSnapshotEncodingVersion(snapshot.encodingVersion)) {
|
|
843
|
+
failSheetBootstrap(
|
|
844
|
+
'INVALID_SNAPSHOT',
|
|
845
|
+
'openSheet snapshot encoding version is unsupported.',
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
const viewport = requireSheetBootstrapViewport(
|
|
849
|
+
snapshot.viewport,
|
|
850
|
+
'openSheet.snapshot.viewport',
|
|
851
|
+
);
|
|
852
|
+
requireMatchingSheetBootstrapViewport(
|
|
853
|
+
viewport,
|
|
854
|
+
params.scope.viewport,
|
|
855
|
+
'openSheet snapshot viewport',
|
|
856
|
+
);
|
|
857
|
+
if (typeof snapshot.payload !== 'string' || !snapshot.payload) {
|
|
858
|
+
failSheetBootstrap(
|
|
859
|
+
'INVALID_SNAPSHOT',
|
|
860
|
+
'openSheet snapshot payload must be a non-empty string.',
|
|
861
|
+
);
|
|
862
|
+
}
|
|
863
|
+
if (
|
|
864
|
+
typeof snapshot.checksum !== 'string' ||
|
|
865
|
+
!FILE_CONSTANTS.checksumPattern.test(snapshot.checksum)
|
|
866
|
+
) {
|
|
867
|
+
failSheetBootstrap(
|
|
868
|
+
'INVALID_SNAPSHOT',
|
|
869
|
+
'openSheet snapshot checksum must be lowercase SHA-256.',
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
if (
|
|
873
|
+
typeof snapshot.payloadBytes !== 'string' ||
|
|
874
|
+
!FILE_CONSTANTS.canonicalIntegerPattern.test(snapshot.payloadBytes)
|
|
875
|
+
) {
|
|
876
|
+
failSheetBootstrap(
|
|
877
|
+
'INVALID_SNAPSHOT',
|
|
878
|
+
'openSheet snapshot payloadBytes must be a canonical integer string.',
|
|
879
|
+
);
|
|
880
|
+
}
|
|
881
|
+
const cellCount = requireSheetBootstrapCount(
|
|
882
|
+
snapshot.cellCount,
|
|
883
|
+
'openSheet.snapshot.cellCount',
|
|
884
|
+
);
|
|
885
|
+
const payloadBytes = new TextEncoder().encode(snapshot.payload).byteLength;
|
|
886
|
+
if (BigInt(snapshot.payloadBytes) !== BigInt(payloadBytes)) {
|
|
887
|
+
failSheetBootstrap(
|
|
888
|
+
'INVALID_SNAPSHOT',
|
|
889
|
+
'openSheet snapshot UTF-8 byte length does not match metadata.',
|
|
890
|
+
);
|
|
891
|
+
}
|
|
892
|
+
if (
|
|
893
|
+
(await getSheetBootstrapPayloadChecksum(snapshot.payload)) !==
|
|
894
|
+
snapshot.checksum
|
|
895
|
+
) {
|
|
896
|
+
failSheetBootstrap(
|
|
897
|
+
'INVALID_SNAPSHOT',
|
|
898
|
+
'openSheet snapshot SHA-256 checksum verification failed.',
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
let data: ReturnType<typeof decodeSheetSnapshot>;
|
|
903
|
+
try {
|
|
904
|
+
data = decodeSheetSnapshot(
|
|
905
|
+
snapshot.payload,
|
|
906
|
+
{
|
|
907
|
+
organizationId: params.scope.organizationId,
|
|
908
|
+
sheetId: params.scope.sheetId,
|
|
909
|
+
sheetTabId: params.scope.sheetTabId,
|
|
910
|
+
seq,
|
|
911
|
+
},
|
|
912
|
+
viewport,
|
|
913
|
+
);
|
|
914
|
+
} catch {
|
|
915
|
+
return failSheetBootstrap(
|
|
916
|
+
'INVALID_SNAPSHOT',
|
|
917
|
+
'openSheet snapshot payload could not be decoded.',
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
if (data.cells.length !== cellCount) {
|
|
921
|
+
failSheetBootstrap(
|
|
922
|
+
'INVALID_SNAPSHOT',
|
|
923
|
+
'openSheet snapshot decoded cell count does not match metadata.',
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
const cellCoordinates = new Set<string>();
|
|
928
|
+
data.cells.forEach((cell, index) => {
|
|
929
|
+
const key = validateSheetBootstrapCell(
|
|
930
|
+
cell,
|
|
931
|
+
params.scope,
|
|
932
|
+
`snapshot.cells[${index}]`,
|
|
933
|
+
);
|
|
934
|
+
if (cellCoordinates.has(key)) {
|
|
935
|
+
failSheetBootstrap(
|
|
936
|
+
'INVALID_SNAPSHOT',
|
|
937
|
+
'openSheet snapshot contains duplicate cell coordinates.',
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
cellCoordinates.add(key);
|
|
941
|
+
});
|
|
942
|
+
data.ranges.forEach((range, index) => {
|
|
943
|
+
validateSheetBootstrapRange(range, params.scope, index);
|
|
944
|
+
});
|
|
945
|
+
data.regions.forEach((region, index) => {
|
|
946
|
+
validateSheetBootstrapRegion(region, params.scope, index);
|
|
947
|
+
});
|
|
948
|
+
requireSheetBootstrapRecord(
|
|
949
|
+
data.pageInfo,
|
|
950
|
+
'snapshot.pageInfo',
|
|
951
|
+
'INVALID_SNAPSHOT',
|
|
952
|
+
);
|
|
953
|
+
|
|
954
|
+
return {
|
|
955
|
+
seq,
|
|
956
|
+
encodingVersion: snapshot.encodingVersion,
|
|
957
|
+
checksum: snapshot.checksum,
|
|
958
|
+
data,
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/*
|
|
963
|
+
* Parse and validate one replayable hard-NEW v3 CELLS journal payload.
|
|
964
|
+
*/
|
|
965
|
+
function parseSheetBootstrapCellPatch(
|
|
966
|
+
change: SheetTabChangeGQL,
|
|
967
|
+
scope: SheetBootstrapScope,
|
|
968
|
+
index: number,
|
|
969
|
+
): SheetBootstrapCellPatch {
|
|
970
|
+
if (typeof change.payload !== 'string' || !change.payload) {
|
|
971
|
+
return failSheetBootstrap(
|
|
972
|
+
'INVALID_CHANGE',
|
|
973
|
+
`openSheet.changes[${index}].payload must be a JSON string.`,
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
let payload: Record<string, any>;
|
|
978
|
+
try {
|
|
979
|
+
payload = requireSheetBootstrapRecord(
|
|
980
|
+
JSON.parse(change.payload),
|
|
981
|
+
`openSheet.changes[${index}].payload`,
|
|
982
|
+
'INVALID_CHANGE',
|
|
983
|
+
);
|
|
984
|
+
} catch (error) {
|
|
985
|
+
if (error instanceof SheetBootstrapValidationError) {
|
|
986
|
+
throw error;
|
|
987
|
+
}
|
|
988
|
+
return failSheetBootstrap(
|
|
989
|
+
'INVALID_CHANGE',
|
|
990
|
+
`openSheet.changes[${index}].payload is invalid JSON.`,
|
|
991
|
+
);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
if (
|
|
995
|
+
payload.v !== SHEET_CELLS_JOURNAL_VERSION ||
|
|
996
|
+
!Array.isArray(payload.cells) ||
|
|
997
|
+
!Array.isArray(payload.deletedCellCoords) ||
|
|
998
|
+
!isHardNewSheetCellsJournalOrderedTargetGroups(
|
|
999
|
+
payload.orderedTargetGroups,
|
|
1000
|
+
) ||
|
|
1001
|
+
payload.truncated ||
|
|
1002
|
+
payload.requiresSnapshot
|
|
1003
|
+
) {
|
|
1004
|
+
failSheetBootstrap(
|
|
1005
|
+
'INVALID_CHANGE',
|
|
1006
|
+
`openSheet.changes[${index}] is not a replayable CELLS payload.`,
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
const patchCoordinates = new Set<string>();
|
|
1011
|
+
payload.cells.forEach((cell: unknown, cellIndex: number) => {
|
|
1012
|
+
const key = validateSheetBootstrapCell(
|
|
1013
|
+
cell,
|
|
1014
|
+
scope,
|
|
1015
|
+
`openSheet.changes[${index}].cells[${cellIndex}]`,
|
|
1016
|
+
);
|
|
1017
|
+
if (patchCoordinates.has(key)) {
|
|
1018
|
+
failSheetBootstrap(
|
|
1019
|
+
'INVALID_CHANGE',
|
|
1020
|
+
`openSheet.changes[${index}] contains duplicate cell coordinates.`,
|
|
1021
|
+
);
|
|
1022
|
+
}
|
|
1023
|
+
patchCoordinates.add(key);
|
|
1024
|
+
});
|
|
1025
|
+
payload.deletedCellCoords.forEach(
|
|
1026
|
+
(coordinate: unknown, coordinateIndex: number) => {
|
|
1027
|
+
validateSheetBootstrapDeletedCoordinate(
|
|
1028
|
+
coordinate,
|
|
1029
|
+
scope,
|
|
1030
|
+
`openSheet.changes[${index}].deletedCellCoords[${coordinateIndex}]`,
|
|
1031
|
+
);
|
|
1032
|
+
},
|
|
1033
|
+
);
|
|
1034
|
+
|
|
1035
|
+
return {
|
|
1036
|
+
cells: payload.cells as SheetAuthoritativeCellGQL[],
|
|
1037
|
+
deletedCellCoords: payload.deletedCellCoords as SheetDeletedCellCoordGQL[],
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
/*
|
|
1042
|
+
* Validate a complete, contiguous, selected-tab-only CELLS journal tail.
|
|
1043
|
+
*/
|
|
1044
|
+
export function validateSheetBootstrapChanges(params: {
|
|
1045
|
+
value: unknown;
|
|
1046
|
+
afterSeq: SheetJournalSeq;
|
|
1047
|
+
currentSeq: SheetJournalSeq;
|
|
1048
|
+
scope: SheetBootstrapSnapshotScope;
|
|
1049
|
+
}): SheetBootstrapCellPatch[] {
|
|
1050
|
+
if (!Array.isArray(params.value)) {
|
|
1051
|
+
return failSheetBootstrap(
|
|
1052
|
+
'INVALID_CHANGE',
|
|
1053
|
+
'openSheet.changes must be an array.',
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
if (compareSheetJournalSeq(params.afterSeq, params.currentSeq) > 0) {
|
|
1057
|
+
failSheetBootstrap(
|
|
1058
|
+
'INVALID_SEQUENCE',
|
|
1059
|
+
'openSheet replay base is ahead of currentSeq.',
|
|
1060
|
+
);
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
let expectedSeq = BigInt(params.afterSeq) + 1n;
|
|
1064
|
+
const patches = params.value.map((item, index) => {
|
|
1065
|
+
const change = requireSheetBootstrapRecord(
|
|
1066
|
+
item,
|
|
1067
|
+
`openSheet.changes[${index}]`,
|
|
1068
|
+
'INVALID_CHANGE',
|
|
1069
|
+
) as SheetTabChangeGQL;
|
|
1070
|
+
if (
|
|
1071
|
+
requireSheetBootstrapId(
|
|
1072
|
+
change.sheetTabId,
|
|
1073
|
+
`openSheet.changes[${index}].sheetTabId`,
|
|
1074
|
+
'INVALID_CHANGE',
|
|
1075
|
+
) !== params.scope.sheetTabId ||
|
|
1076
|
+
change.kind !== 'CELLS'
|
|
1077
|
+
) {
|
|
1078
|
+
failSheetBootstrap(
|
|
1079
|
+
'SCOPE_MISMATCH',
|
|
1080
|
+
`openSheet.changes[${index}] is not a selected-tab CELLS row.`,
|
|
1081
|
+
);
|
|
1082
|
+
}
|
|
1083
|
+
const seq = requireSheetBootstrapSeq(
|
|
1084
|
+
change.seq,
|
|
1085
|
+
`openSheet.changes[${index}].seq`,
|
|
1086
|
+
);
|
|
1087
|
+
if (BigInt(seq) !== expectedSeq) {
|
|
1088
|
+
failSheetBootstrap(
|
|
1089
|
+
'INVALID_SEQUENCE',
|
|
1090
|
+
`openSheet changes are not contiguous after ${params.afterSeq}.`,
|
|
1091
|
+
);
|
|
1092
|
+
}
|
|
1093
|
+
expectedSeq += 1n;
|
|
1094
|
+
|
|
1095
|
+
return parseSheetBootstrapCellPatch(change, params.scope, index);
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
if (expectedSeq - 1n !== BigInt(params.currentSeq)) {
|
|
1099
|
+
failSheetBootstrap(
|
|
1100
|
+
'INVALID_SEQUENCE',
|
|
1101
|
+
'openSheet changes do not end at currentSeq.',
|
|
1102
|
+
);
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
return patches;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
/*
|
|
1109
|
+
* Return whether one incoming same-id cell is newer than the stored version.
|
|
1110
|
+
*/
|
|
1111
|
+
function sheetBootstrapCellShouldReplace(
|
|
1112
|
+
existingCell: SheetCellGQL | undefined,
|
|
1113
|
+
incomingCell: SheetCellGQL,
|
|
1114
|
+
): boolean {
|
|
1115
|
+
if (!existingCell || String(existingCell.id) !== String(incomingCell.id)) {
|
|
1116
|
+
return true;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
const existingRevision = parseSheetJournalSeq(existingCell.revision);
|
|
1120
|
+
const incomingRevision = parseSheetJournalSeq(incomingCell.revision);
|
|
1121
|
+
return (
|
|
1122
|
+
!existingRevision ||
|
|
1123
|
+
!incomingRevision ||
|
|
1124
|
+
compareSheetJournalSeq(incomingRevision, existingRevision) > 0
|
|
1125
|
+
);
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/*
|
|
1129
|
+
* Compare identifiers by Unicode code units without locale/ICU variability.
|
|
1130
|
+
*/
|
|
1131
|
+
function compareSheetBootstrapIds(left: unknown, right: unknown): number {
|
|
1132
|
+
const leftId = String(left);
|
|
1133
|
+
const rightId = String(right);
|
|
1134
|
+
|
|
1135
|
+
return leftId < rightId ? -1 : leftId > rightId ? 1 : 0;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/*
|
|
1139
|
+
* Atomically replay validated journal patches into a fresh coordinate map.
|
|
1140
|
+
*/
|
|
1141
|
+
function applySheetBootstrapCellPatches(
|
|
1142
|
+
baseCells: SheetAuthoritativeCellGQL[],
|
|
1143
|
+
patches: SheetBootstrapCellPatch[],
|
|
1144
|
+
): SheetAuthoritativeCellGQL[] {
|
|
1145
|
+
const cellsByCoordinate = new Map(
|
|
1146
|
+
baseCells.map((cell) => [`${cell.rowIndex}:${cell.columnIndex}`, cell]),
|
|
1147
|
+
);
|
|
1148
|
+
|
|
1149
|
+
patches.forEach((patch) => {
|
|
1150
|
+
patch.cells.forEach((cell) => {
|
|
1151
|
+
const key = `${cell.rowIndex}:${cell.columnIndex}`;
|
|
1152
|
+
if (sheetBootstrapCellShouldReplace(cellsByCoordinate.get(key), cell)) {
|
|
1153
|
+
cellsByCoordinate.set(key, cell);
|
|
1154
|
+
}
|
|
1155
|
+
});
|
|
1156
|
+
patch.deletedCellCoords.forEach((coordinate) => {
|
|
1157
|
+
const key = `${coordinate.rowIndex}:${coordinate.columnIndex}`;
|
|
1158
|
+
const existingCell = cellsByCoordinate.get(key);
|
|
1159
|
+
if (
|
|
1160
|
+
existingCell &&
|
|
1161
|
+
(!coordinate.id || String(existingCell.id) === String(coordinate.id))
|
|
1162
|
+
) {
|
|
1163
|
+
cellsByCoordinate.delete(key);
|
|
1164
|
+
}
|
|
1165
|
+
});
|
|
1166
|
+
});
|
|
1167
|
+
|
|
1168
|
+
return [...cellsByCoordinate.values()].sort(
|
|
1169
|
+
(left, right) =>
|
|
1170
|
+
Number(left.rowIndex) - Number(right.rowIndex) ||
|
|
1171
|
+
Number(left.columnIndex) - Number(right.columnIndex) ||
|
|
1172
|
+
compareSheetBootstrapIds(left.id, right.id),
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
/*
|
|
1177
|
+
* Validate a reusable decoded state before using it as a DELTA replay base.
|
|
1178
|
+
*/
|
|
1179
|
+
function validateCurrentSheetBootstrapState(
|
|
1180
|
+
state: SheetBootstrapTabState,
|
|
1181
|
+
scope: SheetBootstrapScope,
|
|
1182
|
+
): void {
|
|
1183
|
+
if (
|
|
1184
|
+
state.organizationId !== scope.organizationId ||
|
|
1185
|
+
state.sheetId !== scope.sheetId ||
|
|
1186
|
+
state.sheetTabId !== scope.sheetTabId ||
|
|
1187
|
+
state.baseSnapshot.organizationId !== scope.organizationId ||
|
|
1188
|
+
state.baseSnapshot.sheetId !== scope.sheetId ||
|
|
1189
|
+
state.baseSnapshot.sheetTabId !== scope.sheetTabId
|
|
1190
|
+
) {
|
|
1191
|
+
failSheetBootstrap(
|
|
1192
|
+
'SCOPE_MISMATCH',
|
|
1193
|
+
'Cached bootstrap state belongs to a different sheet scope.',
|
|
1194
|
+
);
|
|
1195
|
+
}
|
|
1196
|
+
const appliedSeq = requireSheetBootstrapSeq(
|
|
1197
|
+
state.appliedSeq,
|
|
1198
|
+
'cached bootstrap appliedSeq',
|
|
1199
|
+
);
|
|
1200
|
+
const baseSeq = requireSheetBootstrapSeq(
|
|
1201
|
+
state.baseSnapshot.seq,
|
|
1202
|
+
'cached bootstrap baseSnapshot.seq',
|
|
1203
|
+
);
|
|
1204
|
+
if (
|
|
1205
|
+
compareSheetJournalSeq(baseSeq, appliedSeq) > 0 ||
|
|
1206
|
+
!isSupportedSheetSnapshotEncodingVersion(
|
|
1207
|
+
state.baseSnapshot.encodingVersion,
|
|
1208
|
+
) ||
|
|
1209
|
+
!FILE_CONSTANTS.checksumPattern.test(state.baseSnapshot.checksum)
|
|
1210
|
+
) {
|
|
1211
|
+
failSheetBootstrap(
|
|
1212
|
+
'INVALID_SNAPSHOT',
|
|
1213
|
+
'Cached bootstrap base snapshot identity is invalid.',
|
|
1214
|
+
);
|
|
1215
|
+
}
|
|
1216
|
+
requireMatchingSheetBootstrapViewport(
|
|
1217
|
+
state.baseSnapshot.viewport as SheetGridViewportObj,
|
|
1218
|
+
scope.viewport,
|
|
1219
|
+
'Cached bootstrap viewport',
|
|
1220
|
+
);
|
|
1221
|
+
if (
|
|
1222
|
+
state.view.sheetId !== scope.sheetId ||
|
|
1223
|
+
state.view.sheetTabId !== scope.sheetTabId ||
|
|
1224
|
+
requireSheetBootstrapSeq(
|
|
1225
|
+
state.view.cellsRevision,
|
|
1226
|
+
'cached bootstrap view.cellsRevision',
|
|
1227
|
+
) !== appliedSeq ||
|
|
1228
|
+
!Array.isArray(state.view.cells) ||
|
|
1229
|
+
!Array.isArray(state.view.ranges) ||
|
|
1230
|
+
!Array.isArray(state.view.regions) ||
|
|
1231
|
+
!state.view.pageInfo
|
|
1232
|
+
) {
|
|
1233
|
+
failSheetBootstrap(
|
|
1234
|
+
'INVALID_RESPONSE',
|
|
1235
|
+
'Cached bootstrap view is incomplete or inconsistent.',
|
|
1236
|
+
);
|
|
1237
|
+
}
|
|
1238
|
+
const viewDesignRevision = requireSheetBootstrapSeq(
|
|
1239
|
+
state.view.designRevision,
|
|
1240
|
+
'cached bootstrap view.designRevision',
|
|
1241
|
+
);
|
|
1242
|
+
if (
|
|
1243
|
+
requireSheetBootstrapId(state.view.id, 'cached bootstrap view.id') !==
|
|
1244
|
+
scope.sheetTabId
|
|
1245
|
+
) {
|
|
1246
|
+
failSheetBootstrap(
|
|
1247
|
+
'SCOPE_MISMATCH',
|
|
1248
|
+
'Cached bootstrap view id does not match its tab scope.',
|
|
1249
|
+
);
|
|
1250
|
+
}
|
|
1251
|
+
const sheetTab = requireSheetBootstrapRecord(
|
|
1252
|
+
state.view.sheetTab,
|
|
1253
|
+
'cached bootstrap view.sheetTab',
|
|
1254
|
+
);
|
|
1255
|
+
const sheetTabDesignRevision = requireSheetBootstrapSeq(
|
|
1256
|
+
sheetTab.designRevision,
|
|
1257
|
+
'cached bootstrap view.sheetTab.designRevision',
|
|
1258
|
+
);
|
|
1259
|
+
if (sheetTabDesignRevision !== viewDesignRevision) {
|
|
1260
|
+
failSheetBootstrap(
|
|
1261
|
+
'INVALID_SEQUENCE',
|
|
1262
|
+
'Cached bootstrap design revisions do not agree.',
|
|
1263
|
+
);
|
|
1264
|
+
}
|
|
1265
|
+
if (
|
|
1266
|
+
requireSheetBootstrapId(
|
|
1267
|
+
sheetTab.id,
|
|
1268
|
+
'cached bootstrap view.sheetTab.id',
|
|
1269
|
+
) !== scope.sheetTabId ||
|
|
1270
|
+
requireSheetBootstrapId(
|
|
1271
|
+
sheetTab.organizationId,
|
|
1272
|
+
'cached bootstrap view.sheetTab.organizationId',
|
|
1273
|
+
) !== scope.organizationId ||
|
|
1274
|
+
requireSheetBootstrapId(
|
|
1275
|
+
sheetTab.sheetId,
|
|
1276
|
+
'cached bootstrap view.sheetTab.sheetId',
|
|
1277
|
+
) !== scope.sheetId
|
|
1278
|
+
) {
|
|
1279
|
+
failSheetBootstrap(
|
|
1280
|
+
'SCOPE_MISMATCH',
|
|
1281
|
+
'Cached bootstrap tab belongs to a different sheet scope.',
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
const cellCoordinates = new Set<string>();
|
|
1286
|
+
(state.view.cells || []).forEach((cell, index) => {
|
|
1287
|
+
const key = validateSheetBootstrapCell(
|
|
1288
|
+
cell,
|
|
1289
|
+
scope,
|
|
1290
|
+
`cached bootstrap cells[${index}]`,
|
|
1291
|
+
);
|
|
1292
|
+
if (cellCoordinates.has(key)) {
|
|
1293
|
+
failSheetBootstrap(
|
|
1294
|
+
'INVALID_RESPONSE',
|
|
1295
|
+
'Cached bootstrap view contains duplicate cell coordinates.',
|
|
1296
|
+
);
|
|
1297
|
+
}
|
|
1298
|
+
cellCoordinates.add(key);
|
|
1299
|
+
});
|
|
1300
|
+
(state.view.ranges || []).forEach((range, index) => {
|
|
1301
|
+
validateSheetBootstrapRange(range, scope, index);
|
|
1302
|
+
});
|
|
1303
|
+
(state.view.regions || []).forEach((region, index) => {
|
|
1304
|
+
validateSheetBootstrapRegion(region, scope, index);
|
|
1305
|
+
});
|
|
1306
|
+
requireSheetBootstrapRecord(
|
|
1307
|
+
state.view.pageInfo,
|
|
1308
|
+
'cached bootstrap view.pageInfo',
|
|
1309
|
+
'INVALID_RESPONSE',
|
|
1310
|
+
);
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
/*
|
|
1314
|
+
* Build one SheetView-compatible value without mutating its decoded base.
|
|
1315
|
+
*/
|
|
1316
|
+
function buildSheetBootstrapView(params: {
|
|
1317
|
+
baseView?: SheetViewGQL | null;
|
|
1318
|
+
decodedView?: ReturnType<typeof decodeSheetSnapshot> | null;
|
|
1319
|
+
selectedTab: SheetTabGQL;
|
|
1320
|
+
currentSeq: SheetJournalSeq;
|
|
1321
|
+
cells: SheetAuthoritativeCellGQL[];
|
|
1322
|
+
}): SheetViewGQL {
|
|
1323
|
+
const source = params.baseView || params.decodedView;
|
|
1324
|
+
if (!source) {
|
|
1325
|
+
return failSheetBootstrap(
|
|
1326
|
+
'INVALID_RESPONSE',
|
|
1327
|
+
'No SheetView-compatible replay base was provided.',
|
|
1328
|
+
);
|
|
1329
|
+
}
|
|
1330
|
+
const sheetTabId = requireSheetBootstrapId(
|
|
1331
|
+
params.selectedTab.id,
|
|
1332
|
+
'selected tab id',
|
|
1333
|
+
);
|
|
1334
|
+
const sheetId = requireSheetBootstrapId(
|
|
1335
|
+
params.selectedTab.sheetId,
|
|
1336
|
+
'selected tab sheetId',
|
|
1337
|
+
);
|
|
1338
|
+
const designRevision = requireSheetBootstrapSeq(
|
|
1339
|
+
params.selectedTab.designRevision,
|
|
1340
|
+
'selected tab designRevision',
|
|
1341
|
+
);
|
|
1342
|
+
|
|
1343
|
+
return {
|
|
1344
|
+
...(source as SheetViewGQL),
|
|
1345
|
+
id: sheetTabId,
|
|
1346
|
+
sheetId,
|
|
1347
|
+
sheetTabId,
|
|
1348
|
+
cellsRevision: params.currentSeq,
|
|
1349
|
+
designRevision,
|
|
1350
|
+
fromSeq: null,
|
|
1351
|
+
toSeq: null,
|
|
1352
|
+
requiresSnapshot: false,
|
|
1353
|
+
sheetTab: {
|
|
1354
|
+
...(source.sheetTab || {}),
|
|
1355
|
+
...params.selectedTab,
|
|
1356
|
+
cellsRevision: params.currentSeq,
|
|
1357
|
+
designRevision,
|
|
1358
|
+
},
|
|
1359
|
+
cells: params.cells,
|
|
1360
|
+
ranges: [...(source.ranges || [])],
|
|
1361
|
+
regions: [...(source.regions || [])] as SheetAuthoritativeRegionGQL[],
|
|
1362
|
+
pageInfo: source.pageInfo ? { ...source.pageInfo } : null,
|
|
1363
|
+
};
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
/*
|
|
1367
|
+
* Freeze the immutable identity used for every future delta negotiation.
|
|
1368
|
+
*/
|
|
1369
|
+
function createSheetBootstrapBaseSnapshotIdentity(params: {
|
|
1370
|
+
scope: SheetBootstrapScope;
|
|
1371
|
+
seq: SheetJournalSeq;
|
|
1372
|
+
encodingVersion: number;
|
|
1373
|
+
checksum: string;
|
|
1374
|
+
}): SheetBootstrapBaseSnapshotIdentity {
|
|
1375
|
+
const viewport = Object.freeze({ ...params.scope.viewport });
|
|
1376
|
+
|
|
1377
|
+
return Object.freeze({
|
|
1378
|
+
organizationId: params.scope.organizationId,
|
|
1379
|
+
sheetId: params.scope.sheetId,
|
|
1380
|
+
sheetTabId: params.scope.sheetTabId,
|
|
1381
|
+
seq: params.seq,
|
|
1382
|
+
encodingVersion: params.encodingVersion,
|
|
1383
|
+
checksum: params.checksum,
|
|
1384
|
+
viewport,
|
|
1385
|
+
});
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
/*
|
|
1389
|
+
* Apply a validated SNAPSHOT response and its complete journal tail.
|
|
1390
|
+
*/
|
|
1391
|
+
async function applySnapshotSheetBootstrap(params: {
|
|
1392
|
+
bootstrap: SheetBootstrapGQL;
|
|
1393
|
+
selectedTab: SheetTabGQL;
|
|
1394
|
+
currentSeq: SheetJournalSeq;
|
|
1395
|
+
scope: SheetBootstrapScope;
|
|
1396
|
+
}): Promise<SheetBootstrapTabState> {
|
|
1397
|
+
const snapshot = await decodeVerifiedSheetBootstrapSnapshot({
|
|
1398
|
+
snapshot: params.bootstrap.snapshot,
|
|
1399
|
+
currentSeq: params.currentSeq,
|
|
1400
|
+
scope: params.scope,
|
|
1401
|
+
});
|
|
1402
|
+
if (
|
|
1403
|
+
requireSheetBootstrapSeq(
|
|
1404
|
+
snapshot.data.sheetTab.designRevision,
|
|
1405
|
+
'snapshot.sheetTab.designRevision',
|
|
1406
|
+
) !==
|
|
1407
|
+
requireSheetBootstrapSeq(
|
|
1408
|
+
params.selectedTab.designRevision,
|
|
1409
|
+
'openSheet.selectedTab.designRevision',
|
|
1410
|
+
)
|
|
1411
|
+
) {
|
|
1412
|
+
failSheetBootstrap(
|
|
1413
|
+
'INVALID_SEQUENCE',
|
|
1414
|
+
'Snapshot and selected-tab design revisions do not agree.',
|
|
1415
|
+
);
|
|
1416
|
+
}
|
|
1417
|
+
const patches = validateSheetBootstrapChanges({
|
|
1418
|
+
value: params.bootstrap.changes,
|
|
1419
|
+
afterSeq: snapshot.seq,
|
|
1420
|
+
currentSeq: params.currentSeq,
|
|
1421
|
+
scope: params.scope,
|
|
1422
|
+
});
|
|
1423
|
+
const cells = applySheetBootstrapCellPatches(
|
|
1424
|
+
snapshot.data.cells as SheetAuthoritativeCellGQL[],
|
|
1425
|
+
patches,
|
|
1426
|
+
);
|
|
1427
|
+
const baseSnapshot = createSheetBootstrapBaseSnapshotIdentity({
|
|
1428
|
+
scope: params.scope,
|
|
1429
|
+
seq: snapshot.seq,
|
|
1430
|
+
encodingVersion: snapshot.encodingVersion,
|
|
1431
|
+
checksum: snapshot.checksum,
|
|
1432
|
+
});
|
|
1433
|
+
|
|
1434
|
+
return {
|
|
1435
|
+
organizationId: params.scope.organizationId,
|
|
1436
|
+
sheetId: params.scope.sheetId,
|
|
1437
|
+
sheetTabId: params.scope.sheetTabId,
|
|
1438
|
+
baseSnapshot,
|
|
1439
|
+
appliedSeq: params.currentSeq,
|
|
1440
|
+
view: buildSheetBootstrapView({
|
|
1441
|
+
decodedView: snapshot.data,
|
|
1442
|
+
selectedTab: params.selectedTab,
|
|
1443
|
+
currentSeq: params.currentSeq,
|
|
1444
|
+
cells,
|
|
1445
|
+
}),
|
|
1446
|
+
};
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
/*
|
|
1450
|
+
* Apply a validated DELTA response while retaining the exact base identity.
|
|
1451
|
+
*/
|
|
1452
|
+
function applyDeltaSheetBootstrap(params: {
|
|
1453
|
+
bootstrap: SheetBootstrapGQL;
|
|
1454
|
+
selectedTab: SheetTabGQL;
|
|
1455
|
+
currentSeq: SheetJournalSeq;
|
|
1456
|
+
scope: SheetBootstrapScope;
|
|
1457
|
+
currentState: SheetBootstrapTabState | null | undefined;
|
|
1458
|
+
}): SheetBootstrapTabState {
|
|
1459
|
+
if (!params.currentState) {
|
|
1460
|
+
return failSheetBootstrap(
|
|
1461
|
+
'INVALID_RESPONSE',
|
|
1462
|
+
'openSheet DELTA requires a decoded local base state.',
|
|
1463
|
+
);
|
|
1464
|
+
}
|
|
1465
|
+
if (
|
|
1466
|
+
params.bootstrap.snapshot !== null &&
|
|
1467
|
+
params.bootstrap.snapshot !== undefined
|
|
1468
|
+
) {
|
|
1469
|
+
failSheetBootstrap(
|
|
1470
|
+
'INVALID_RESPONSE',
|
|
1471
|
+
'openSheet DELTA must not include a replacement snapshot.',
|
|
1472
|
+
);
|
|
1473
|
+
}
|
|
1474
|
+
validateCurrentSheetBootstrapState(params.currentState, params.scope);
|
|
1475
|
+
if (
|
|
1476
|
+
requireSheetBootstrapSeq(
|
|
1477
|
+
params.currentState.view.designRevision,
|
|
1478
|
+
'cached bootstrap view.designRevision',
|
|
1479
|
+
) !==
|
|
1480
|
+
requireSheetBootstrapSeq(
|
|
1481
|
+
params.selectedTab.designRevision,
|
|
1482
|
+
'openSheet.selectedTab.designRevision',
|
|
1483
|
+
)
|
|
1484
|
+
) {
|
|
1485
|
+
failSheetBootstrap(
|
|
1486
|
+
'INVALID_SEQUENCE',
|
|
1487
|
+
'Cached and selected-tab design revisions do not agree.',
|
|
1488
|
+
);
|
|
1489
|
+
}
|
|
1490
|
+
const patches = validateSheetBootstrapChanges({
|
|
1491
|
+
value: params.bootstrap.changes,
|
|
1492
|
+
afterSeq: params.currentState.appliedSeq,
|
|
1493
|
+
currentSeq: params.currentSeq,
|
|
1494
|
+
scope: params.scope,
|
|
1495
|
+
});
|
|
1496
|
+
const cells = applySheetBootstrapCellPatches(
|
|
1497
|
+
params.currentState.view.cells || [],
|
|
1498
|
+
patches,
|
|
1499
|
+
);
|
|
1500
|
+
|
|
1501
|
+
return {
|
|
1502
|
+
organizationId: params.scope.organizationId,
|
|
1503
|
+
sheetId: params.scope.sheetId,
|
|
1504
|
+
sheetTabId: params.scope.sheetTabId,
|
|
1505
|
+
baseSnapshot: params.currentState.baseSnapshot,
|
|
1506
|
+
appliedSeq: params.currentSeq,
|
|
1507
|
+
view: buildSheetBootstrapView({
|
|
1508
|
+
baseView: params.currentState.view,
|
|
1509
|
+
selectedTab: params.selectedTab,
|
|
1510
|
+
currentSeq: params.currentSeq,
|
|
1511
|
+
cells,
|
|
1512
|
+
}),
|
|
1513
|
+
};
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
/*
|
|
1517
|
+
* Validate and atomically apply one access-aware openSheet response.
|
|
1518
|
+
*/
|
|
1519
|
+
export async function applySheetBootstrap(params: {
|
|
1520
|
+
bootstrap: unknown;
|
|
1521
|
+
expectedSheetId: string;
|
|
1522
|
+
currentState?: SheetBootstrapTabState | null;
|
|
1523
|
+
}): Promise<SheetBootstrapApplyResult> {
|
|
1524
|
+
const expectedSheetId = requireSheetBootstrapId(
|
|
1525
|
+
params.expectedSheetId,
|
|
1526
|
+
'expectedSheetId',
|
|
1527
|
+
'INVALID_RESPONSE',
|
|
1528
|
+
);
|
|
1529
|
+
const bootstrap = requireSheetBootstrapRecord(
|
|
1530
|
+
params.bootstrap,
|
|
1531
|
+
'openSheet',
|
|
1532
|
+
) as SheetBootstrapGQL;
|
|
1533
|
+
const contract = validateSheetBootstrapContract(bootstrap);
|
|
1534
|
+
const access = validateSheetBootstrapAccess(
|
|
1535
|
+
bootstrap.access,
|
|
1536
|
+
expectedSheetId,
|
|
1537
|
+
);
|
|
1538
|
+
if (!access.canView) {
|
|
1539
|
+
if (bootstrap.status === 'DENIED') {
|
|
1540
|
+
if (
|
|
1541
|
+
bootstrap.unavailableReason ||
|
|
1542
|
+
sheetBootstrapHasScopedReadData(bootstrap)
|
|
1543
|
+
) {
|
|
1544
|
+
failSheetBootstrap(
|
|
1545
|
+
'INVALID_ACCESS',
|
|
1546
|
+
'Denied openSheet response exposed scoped workbook data.',
|
|
1547
|
+
);
|
|
1548
|
+
}
|
|
1549
|
+
return { ...contract, status: 'DENIED', access };
|
|
1550
|
+
}
|
|
1551
|
+
if (
|
|
1552
|
+
bootstrap.status !== 'RETRY' ||
|
|
1553
|
+
typeof bootstrap.unavailableReason !== 'string' ||
|
|
1554
|
+
!bootstrap.unavailableReason.trim()
|
|
1555
|
+
) {
|
|
1556
|
+
failSheetBootstrap(
|
|
1557
|
+
'INVALID_STATUS',
|
|
1558
|
+
'Denied openSheet responses require DENIED or an explicit RETRY status.',
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1561
|
+
if (sheetBootstrapHasScopedReadData(bootstrap)) {
|
|
1562
|
+
failSheetBootstrap(
|
|
1563
|
+
'INVALID_ACCESS',
|
|
1564
|
+
'Denied openSheet retry exposed scoped workbook data.',
|
|
1565
|
+
);
|
|
1566
|
+
}
|
|
1567
|
+
return {
|
|
1568
|
+
...contract,
|
|
1569
|
+
status: 'RETRY',
|
|
1570
|
+
access,
|
|
1571
|
+
unavailableReason: bootstrap.unavailableReason,
|
|
1572
|
+
sheet: null,
|
|
1573
|
+
tabs: [],
|
|
1574
|
+
selectedTab: null,
|
|
1575
|
+
referencedDataTables: [],
|
|
1576
|
+
};
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
if (
|
|
1580
|
+
bootstrap.status !== 'READY' &&
|
|
1581
|
+
bootstrap.status !== 'RETRY' &&
|
|
1582
|
+
bootstrap.status !== 'UNAVAILABLE'
|
|
1583
|
+
) {
|
|
1584
|
+
return failSheetBootstrap(
|
|
1585
|
+
'INVALID_STATUS',
|
|
1586
|
+
'openSheet returned an unknown or missing status.',
|
|
1587
|
+
);
|
|
1588
|
+
}
|
|
1589
|
+
if (bootstrap.status !== 'READY') {
|
|
1590
|
+
if (
|
|
1591
|
+
bootstrap.realtimeToken !== null &&
|
|
1592
|
+
bootstrap.realtimeToken !== undefined
|
|
1593
|
+
) {
|
|
1594
|
+
failSheetBootstrap(
|
|
1595
|
+
'INVALID_ACCESS',
|
|
1596
|
+
'Non-ready openSheet responses cannot carry a realtime capability.',
|
|
1597
|
+
);
|
|
1598
|
+
}
|
|
1599
|
+
if (
|
|
1600
|
+
typeof bootstrap.unavailableReason !== 'string' ||
|
|
1601
|
+
!bootstrap.unavailableReason.trim()
|
|
1602
|
+
) {
|
|
1603
|
+
failSheetBootstrap(
|
|
1604
|
+
'INVALID_STATUS',
|
|
1605
|
+
'Non-ready openSheet responses require an unavailable reason.',
|
|
1606
|
+
);
|
|
1607
|
+
}
|
|
1608
|
+
const sheet =
|
|
1609
|
+
bootstrap.sheet === null || bootstrap.sheet === undefined
|
|
1610
|
+
? null
|
|
1611
|
+
: validateSheetBootstrapWorkbook(bootstrap.sheet, access);
|
|
1612
|
+
const tabs =
|
|
1613
|
+
bootstrap.tabs === null || bootstrap.tabs === undefined
|
|
1614
|
+
? []
|
|
1615
|
+
: validateSheetBootstrapTabSummaries(bootstrap.tabs);
|
|
1616
|
+
const selectedTab = bootstrap.selectedTab
|
|
1617
|
+
? validateSelectedSheetBootstrapTab(bootstrap.selectedTab, access, tabs)
|
|
1618
|
+
: null;
|
|
1619
|
+
const referencedDataTables =
|
|
1620
|
+
bootstrap.referencedDataTables === null ||
|
|
1621
|
+
bootstrap.referencedDataTables === undefined
|
|
1622
|
+
? []
|
|
1623
|
+
: validateSheetBootstrapReferencedTables(
|
|
1624
|
+
bootstrap.referencedDataTables,
|
|
1625
|
+
);
|
|
1626
|
+
return {
|
|
1627
|
+
...contract,
|
|
1628
|
+
status: bootstrap.status,
|
|
1629
|
+
access,
|
|
1630
|
+
unavailableReason: bootstrap.unavailableReason,
|
|
1631
|
+
sheet,
|
|
1632
|
+
tabs,
|
|
1633
|
+
selectedTab,
|
|
1634
|
+
referencedDataTables,
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
const realtimeToken = requireSheetBootstrapRealtimeToken(
|
|
1638
|
+
bootstrap.realtimeToken,
|
|
1639
|
+
'openSheet.realtimeToken',
|
|
1640
|
+
);
|
|
1641
|
+
const sheet = validateSheetBootstrapWorkbook(bootstrap.sheet, access);
|
|
1642
|
+
const tabs = validateSheetBootstrapTabSummaries(bootstrap.tabs);
|
|
1643
|
+
const selectedTab = bootstrap.selectedTab
|
|
1644
|
+
? validateSelectedSheetBootstrapTab(bootstrap.selectedTab, access, tabs)
|
|
1645
|
+
: null;
|
|
1646
|
+
if (bootstrap.unavailableReason) {
|
|
1647
|
+
failSheetBootstrap(
|
|
1648
|
+
'INVALID_STATUS',
|
|
1649
|
+
'READY openSheet responses cannot have an unavailable reason.',
|
|
1650
|
+
);
|
|
1651
|
+
}
|
|
1652
|
+
if (bootstrap.mode !== 'SNAPSHOT' && bootstrap.mode !== 'DELTA') {
|
|
1653
|
+
return failSheetBootstrap(
|
|
1654
|
+
'INVALID_STATUS',
|
|
1655
|
+
'READY openSheet responses require a known mode.',
|
|
1656
|
+
);
|
|
1657
|
+
}
|
|
1658
|
+
const currentSeq = requireSheetBootstrapSeq(
|
|
1659
|
+
bootstrap.currentSeq,
|
|
1660
|
+
'openSheet.currentSeq',
|
|
1661
|
+
);
|
|
1662
|
+
const servedViewport = requireSheetBootstrapViewport(
|
|
1663
|
+
bootstrap.servedViewport,
|
|
1664
|
+
'openSheet.servedViewport',
|
|
1665
|
+
);
|
|
1666
|
+
const referencedDataTables = validateSheetBootstrapReferencedTables(
|
|
1667
|
+
bootstrap.referencedDataTables,
|
|
1668
|
+
);
|
|
1669
|
+
|
|
1670
|
+
if (!selectedTab) {
|
|
1671
|
+
requireMatchingSheetBootstrapViewport(
|
|
1672
|
+
servedViewport,
|
|
1673
|
+
normalizeSheetViewport({}),
|
|
1674
|
+
'Empty-workbook servedViewport',
|
|
1675
|
+
);
|
|
1676
|
+
if (
|
|
1677
|
+
tabs.length ||
|
|
1678
|
+
bootstrap.mode !== 'SNAPSHOT' ||
|
|
1679
|
+
currentSeq !== SHEET_JOURNAL_ZERO_SEQ ||
|
|
1680
|
+
bootstrap.snapshot ||
|
|
1681
|
+
!Array.isArray(bootstrap.changes) ||
|
|
1682
|
+
bootstrap.changes.length
|
|
1683
|
+
) {
|
|
1684
|
+
failSheetBootstrap(
|
|
1685
|
+
'INVALID_RESPONSE',
|
|
1686
|
+
'Empty-workbook openSheet response is internally inconsistent.',
|
|
1687
|
+
);
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
return {
|
|
1691
|
+
...contract,
|
|
1692
|
+
status: 'READY',
|
|
1693
|
+
access,
|
|
1694
|
+
realtimeToken,
|
|
1695
|
+
sheet,
|
|
1696
|
+
tabs,
|
|
1697
|
+
selectedTab: null,
|
|
1698
|
+
mode: 'SNAPSHOT',
|
|
1699
|
+
currentSeq,
|
|
1700
|
+
servedViewport,
|
|
1701
|
+
referencedDataTables,
|
|
1702
|
+
state: null,
|
|
1703
|
+
};
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
const sheetTabId = requireSheetBootstrapId(
|
|
1707
|
+
selectedTab.id,
|
|
1708
|
+
'openSheet.selectedTab.id',
|
|
1709
|
+
);
|
|
1710
|
+
const selectedTabSeq = requireSheetBootstrapSeq(
|
|
1711
|
+
selectedTab.cellsRevision,
|
|
1712
|
+
'openSheet.selectedTab.cellsRevision',
|
|
1713
|
+
);
|
|
1714
|
+
const selectedSummary = tabs.find((tab) => tab.id === sheetTabId);
|
|
1715
|
+
if (
|
|
1716
|
+
selectedTabSeq !== currentSeq ||
|
|
1717
|
+
requireSheetBootstrapSeq(
|
|
1718
|
+
selectedSummary?.currentSeq,
|
|
1719
|
+
'openSheet selected tab summary currentSeq',
|
|
1720
|
+
) !== currentSeq
|
|
1721
|
+
) {
|
|
1722
|
+
failSheetBootstrap(
|
|
1723
|
+
'INVALID_SEQUENCE',
|
|
1724
|
+
'openSheet selected-tab cursors do not agree.',
|
|
1725
|
+
);
|
|
1726
|
+
}
|
|
1727
|
+
const expectedViewport = getExpectedSheetBootstrapViewport(selectedTab);
|
|
1728
|
+
requireMatchingSheetBootstrapViewport(
|
|
1729
|
+
servedViewport,
|
|
1730
|
+
expectedViewport,
|
|
1731
|
+
'openSheet servedViewport',
|
|
1732
|
+
);
|
|
1733
|
+
const scope: SheetBootstrapScope = {
|
|
1734
|
+
organizationId: access.organizationId as string,
|
|
1735
|
+
sheetId: access.sheetId,
|
|
1736
|
+
sheetTabId,
|
|
1737
|
+
viewport: servedViewport,
|
|
1738
|
+
};
|
|
1739
|
+
const state =
|
|
1740
|
+
bootstrap.mode === 'SNAPSHOT'
|
|
1741
|
+
? await applySnapshotSheetBootstrap({
|
|
1742
|
+
bootstrap,
|
|
1743
|
+
selectedTab,
|
|
1744
|
+
currentSeq,
|
|
1745
|
+
scope,
|
|
1746
|
+
})
|
|
1747
|
+
: applyDeltaSheetBootstrap({
|
|
1748
|
+
bootstrap,
|
|
1749
|
+
selectedTab,
|
|
1750
|
+
currentSeq,
|
|
1751
|
+
scope,
|
|
1752
|
+
currentState: params.currentState,
|
|
1753
|
+
});
|
|
1754
|
+
|
|
1755
|
+
return {
|
|
1756
|
+
...contract,
|
|
1757
|
+
status: 'READY',
|
|
1758
|
+
access,
|
|
1759
|
+
realtimeToken,
|
|
1760
|
+
sheet,
|
|
1761
|
+
tabs,
|
|
1762
|
+
selectedTab,
|
|
1763
|
+
mode: bootstrap.mode,
|
|
1764
|
+
currentSeq,
|
|
1765
|
+
servedViewport,
|
|
1766
|
+
referencedDataTables,
|
|
1767
|
+
state,
|
|
1768
|
+
};
|
|
1769
|
+
}
|