@jsb188/mday 1.1.10 → 1.1.12
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 +140 -54
- package/src/constants/agentMemory.ts +123 -0
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +10 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +236 -0
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +21 -21
- package/src/constants/dataTableFieldTypes.generated.ts +59 -0
- package/src/constants/docAccess.ts +33 -0
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +125 -7
- package/src/constants/fieldDoc.ts +104 -0
- package/src/constants/graphqlReactivity.ts +196 -0
- package/src/constants/integration.ts +8 -9
- package/src/constants/log.ts +14 -408
- package/src/constants/logDisplay/README.md +88 -0
- package/src/constants/logDisplay/defaults.ts +39 -0
- package/src/constants/logDisplay/index.ts +27 -0
- package/src/constants/logDisplay/operations/arable.ts +16 -0
- package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
- package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
- package/src/constants/logDisplay/shared/accounting.ts +13 -0
- package/src/constants/logDisplay/shared/interactions.ts +13 -0
- package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
- package/src/constants/logTaxonomy.generated.ts +2207 -0
- package/src/constants/noteDoc.ts +77 -0
- package/src/constants/organization.ts +18 -21
- package/src/constants/pageRoute.ts +23 -0
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +126 -14
- package/src/constants/sms.ts +18 -0
- package/src/constants/square.ts +6 -6
- package/src/constants/storage.ts +80 -7
- package/src/constants/usage.ts +82 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +836 -780
- package/src/schemas/inboundContact.ts +60 -55
- package/src/schemas/logsQuery.ts +55 -49
- package/src/types/account.d.ts +4 -1
- package/src/types/agentMemory.d.ts +98 -0
- package/src/types/agentNote.d.ts +443 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1533 -0
- package/src/types/contact.d.ts +15 -0
- package/src/types/csvExport.d.ts +213 -0
- package/src/types/dataAnalysis.d.ts +44 -0
- package/src/types/dataTable.d.ts +115 -403
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/docAccess.d.ts +96 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +184 -24
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +702 -0
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +10 -1
- package/src/types/log.d.ts +254 -295
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +91 -43
- package/src/types/pageRoute.d.ts +48 -0
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +678 -129
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +449 -129
- package/src/types/usage.d.ts +106 -0
- package/src/types/workflow.d.ts +315 -54
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +43 -0
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +1174 -0
- package/src/utils/campaignAI.ts +72 -0
- package/src/utils/campaignAccess.ts +239 -0
- package/src/utils/campaignEmail.ts +601 -0
- package/src/utils/campaignEmailCompiler.ts +372 -0
- package/src/utils/campaignEmailImages.ts +60 -0
- package/src/utils/campaignEmailStyles.ts +544 -0
- package/src/utils/campaignLifecycle.ts +104 -0
- package/src/utils/campaignPreview.ts +285 -0
- package/src/utils/contact.ts +50 -0
- package/src/utils/csvExport.ts +19 -0
- package/src/utils/dataTable.ts +95 -618
- package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/docAccess.ts +383 -0
- package/src/utils/documentName.ts +53 -0
- package/src/utils/email.ts +317 -51
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +157 -0
- package/src/utils/fieldDocAnswers.ts +128 -0
- package/src/utils/fieldDocDefaults.ts +36 -0
- package/src/utils/fieldDocProgram.ts +36 -0
- package/src/utils/fieldDocSummary.ts +30 -0
- package/src/utils/log.ts +276 -192
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logRegistryValue.ts +90 -0
- package/src/utils/logStoreRead.ts +262 -0
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomy.ts +76 -0
- package/src/utils/logTaxonomyDisplay.ts +395 -0
- package/src/utils/noteDoc.ts +609 -0
- package/src/utils/organization.ts +337 -84
- package/src/utils/pageRoute.ts +71 -0
- package/src/utils/qrCode.helpers.ts +189 -0
- package/src/utils/qrCode.test.ts +519 -0
- package/src/utils/qrCode.ts +273 -0
- package/src/utils/richTextHTML.ts +385 -0
- package/src/utils/sheet.ts +199 -107
- package/src/utils/sheetAxisMove.ts +616 -0
- package/src/utils/sheetBootstrap.ts +1402 -0
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +358 -0
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +186 -0
- package/src/utils/sheetFeatureReference.ts +183 -0
- package/src/utils/sheetFormulaArgs.ts +168 -0
- package/src/utils/sheetFormulaCompile.ts +475 -0
- package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
- package/src/utils/sheetFormulaCriteria.ts +656 -0
- package/src/utils/sheetFormulaDate.ts +444 -0
- package/src/utils/sheetFormulaEvaluation.ts +899 -0
- package/src/utils/sheetFormulaEvaluator.ts +997 -0
- package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
- package/src/utils/sheetFormulaLogical.ts +116 -0
- package/src/utils/sheetFormulaLookup.ts +488 -0
- package/src/utils/sheetFormulaMath.ts +48 -0
- package/src/utils/sheetFormulaParser.ts +729 -0
- package/src/utils/sheetFormulaRange.ts +330 -0
- package/src/utils/sheetFormulaReference.ts +703 -0
- package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
- package/src/utils/sheetFormulaReferenceDates.ts +237 -0
- package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
- package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
- package/src/utils/sheetFormulaReferenceMath.ts +95 -0
- package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
- package/src/utils/sheetFormulaReferenceText.ts +462 -0
- package/src/utils/sheetFormulaSyntax.ts +1027 -0
- package/src/utils/sheetFormulaText.ts +430 -0
- package/src/utils/sheetJournal.ts +200 -0
- package/src/utils/sheetNumberFormat.ts +1012 -0
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +118 -0
- package/src/utils/sheetSnapshot.ts +739 -0
- package/src/utils/sheetStructureEdit.ts +914 -0
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/square.ts +22 -12
- package/src/utils/storage.ts +198 -0
- package/src/utils/table-route.ts +127 -11
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +334 -40
- package/src/utils/workflowLogFingerprint.ts +302 -0
- package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
- package/src/utils/workflowProgress.ts +280 -0
- package/src/utils/workflowTriggers.ts +61 -0
- package/tests/agentNote.test.ts +395 -0
- package/tests/campaignAI.test.ts +119 -0
- package/tests/campaignAccess.test.ts +239 -0
- package/tests/campaignEmail.test.ts +441 -0
- package/tests/campaignLifecycle.test.ts +134 -0
- package/tests/campaignPublication.test.ts +309 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/docAccess.test.ts +525 -0
- package/tests/editLogEntry.test.ts +112 -0
- package/tests/email.test.ts +39 -0
- package/tests/fieldDocProgram.test.ts +114 -0
- package/tests/logDisplay.fixtures.ts +67 -0
- package/tests/logDisplay.test.ts +227 -0
- package/tests/noteDoc.test.ts +514 -0
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +352 -0
- package/tests/sheetBootstrap.test.ts +1967 -0
- package/tests/sheetFeatureReference.test.ts +22 -0
- package/tests/sheetFormulaCriteria.test.ts +269 -0
- package/tests/sheetFormulaEvaluator.test.ts +468 -0
- package/tests/sheetFormulaFunctions.test.ts +538 -0
- package/tests/sheetFormulaLanguage.test.ts +361 -0
- package/tests/sheetFormulaParity.fixtures.ts +872 -0
- package/tests/sheetFormulaParity.test.ts +188 -0
- package/tests/sheetFormulaRange.test.ts +167 -0
- package/tests/sheetFormulaReference.test.ts +78 -0
- package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
- package/tests/sheetFormulaTextDate.test.ts +512 -0
- package/tests/sheetSequence.test.ts +22 -0
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +537 -0
- package/tests/sheetStructureReferences.test.ts +589 -0
- package/tests/workflowLifecycleStatus.test.ts +60 -0
- package/tests/workflowLogFingerprint.test.ts +203 -0
- package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
- package/tests/workflowProgress.test.ts +180 -0
- package/tests/workflowTriggers.test.ts +29 -0
- package/tsconfig.json +10 -10
- package/vitest.config.ts +8 -0
- package/src/constants/log-activityTexts.ts +0 -235
- package/src/constants/product.ts +0 -42
- package/src/constants/report.ts +0 -44
- package/src/schemas/organization.ts +0 -65
- package/src/schemas/productsQuery.ts +0 -53
- package/src/schemas/reportsQuery.ts +0 -20
- package/src/types/product.d.ts +0 -84
- package/src/types/report.d.ts +0 -290
- package/src/utils/product-calEvent.ts +0 -455
- package/src/utils/report.ts +0 -158
|
@@ -0,0 +1,1402 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SheetAccessInfoGQL,
|
|
3
|
+
SheetAuthoritativeCellGQL,
|
|
4
|
+
SheetBootstrapGQL,
|
|
5
|
+
SheetBootstrapMode,
|
|
6
|
+
SheetBootstrapStatus,
|
|
7
|
+
SheetComputedCellGQL,
|
|
8
|
+
SheetDeletedCellCoordGQL,
|
|
9
|
+
SheetGQL,
|
|
10
|
+
SheetGridPageInfoGQL,
|
|
11
|
+
SheetGridViewportObj,
|
|
12
|
+
SheetJournalSeq,
|
|
13
|
+
SheetRealtimeCapabilityGQL,
|
|
14
|
+
SheetStructureOperationEnum,
|
|
15
|
+
SheetTabChangeGQL,
|
|
16
|
+
SheetTabGQL,
|
|
17
|
+
SheetTabSummaryGQL,
|
|
18
|
+
} from '../types/sheet.d.ts';
|
|
19
|
+
import { SHEET_MAX_COLUMN_COUNT, SHEET_MAX_ROW_COUNT, SHEET_STRUCTURE_OPERATION_ENUMS } from '../constants/sheet.ts';
|
|
20
|
+
import { getSheetViewFullViewport, normalizeSheetDesign, normalizeSheetViewport } from './sheetDesign.ts';
|
|
21
|
+
import type { SheetAxisMoveInput } from './sheetAxisMove.ts';
|
|
22
|
+
import {
|
|
23
|
+
isHardNewSheetCellsJournalOrderedTargetGroups,
|
|
24
|
+
isSheetComputedJournalCell,
|
|
25
|
+
SHEET_CELLS_JOURNAL_VERSION,
|
|
26
|
+
SHEET_COMPUTED_JOURNAL_VERSION,
|
|
27
|
+
SHEET_DESIGN_JOURNAL_VERSION,
|
|
28
|
+
SHEET_STRUCTURE_JOURNAL_VERSION,
|
|
29
|
+
} from './sheetJournal.ts';
|
|
30
|
+
import {
|
|
31
|
+
compareSheetJournalSeq,
|
|
32
|
+
parseSheetJournalSeq,
|
|
33
|
+
SHEET_JOURNAL_MAX_SEQ,
|
|
34
|
+
SHEET_JOURNAL_ZERO_SEQ,
|
|
35
|
+
sheetJournalSeqIsPositive,
|
|
36
|
+
} from './sheetSequence.ts';
|
|
37
|
+
import { decodeSheetSnapshot, isSupportedSheetSnapshotEncodingVersion } 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
|
+
/*
|
|
59
|
+
* One decoded, integrity-checked tab image at a journal seq: the base a
|
|
60
|
+
* SNAPSHOT install replays its tail over. The client's tab store installs it
|
|
61
|
+
* and applies the tail rows through the same reducer as live packets.
|
|
62
|
+
*/
|
|
63
|
+
export type SheetBootstrapTabImage = Readonly<{
|
|
64
|
+
organizationId: string;
|
|
65
|
+
sheetId: string;
|
|
66
|
+
sheetTabId: string;
|
|
67
|
+
seq: SheetJournalSeq;
|
|
68
|
+
cells: SheetAuthoritativeCellGQL[];
|
|
69
|
+
|
|
70
|
+
pageInfo: SheetGridPageInfoGQL | null;
|
|
71
|
+
/* The tab image at `seq`, design and designRevision included. */
|
|
72
|
+
sheetTab: SheetTabGQL;
|
|
73
|
+
}>;
|
|
74
|
+
|
|
75
|
+
export type SheetBootstrapDeniedResult = {
|
|
76
|
+
contractVersion: typeof SHEET_BOOTSTRAP_CONTRACT_VERSION;
|
|
77
|
+
status: 'DENIED';
|
|
78
|
+
access: ValidatedSheetBootstrapAccess;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export type SheetBootstrapDeferredResult = {
|
|
82
|
+
contractVersion: typeof SHEET_BOOTSTRAP_CONTRACT_VERSION;
|
|
83
|
+
status: 'RETRY' | 'UNAVAILABLE';
|
|
84
|
+
access: ValidatedSheetBootstrapAccess;
|
|
85
|
+
unavailableReason: string;
|
|
86
|
+
sheet: SheetGQL | null;
|
|
87
|
+
tabs: SheetTabSummaryGQL[];
|
|
88
|
+
selectedTab: SheetTabGQL | null;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export type SheetBootstrapReadyResult = {
|
|
92
|
+
contractVersion: typeof SHEET_BOOTSTRAP_CONTRACT_VERSION;
|
|
93
|
+
status: 'READY';
|
|
94
|
+
access: ValidatedSheetBootstrapAccess;
|
|
95
|
+
realtimeToken: string;
|
|
96
|
+
sheet: SheetGQL;
|
|
97
|
+
tabs: SheetTabSummaryGQL[];
|
|
98
|
+
selectedTab: SheetTabGQL | null;
|
|
99
|
+
mode: SheetBootstrapMode;
|
|
100
|
+
currentSeq: SheetJournalSeq;
|
|
101
|
+
servedViewport: SheetGridViewportObj;
|
|
102
|
+
|
|
103
|
+
/* SNAPSHOT: the decoded snapshot image the rows replay over; DELTA: null. */
|
|
104
|
+
image: SheetBootstrapTabImage | null;
|
|
105
|
+
/*
|
|
106
|
+
* The validated journal rows in seq order: the DELTA rows after the
|
|
107
|
+
* requested `sinceSeq`, or the tail after the snapshot image.
|
|
108
|
+
*/
|
|
109
|
+
rows: SheetBootstrapRowPatch[];
|
|
110
|
+
/* The seq the rows start after: `sinceSeq` for DELTA, the snapshot seq otherwise. */
|
|
111
|
+
rowsAfterSeq: SheetJournalSeq;
|
|
112
|
+
/*
|
|
113
|
+
* SNAPSHOT answers to a request with `sinceSeq`: the topology rows the
|
|
114
|
+
* image absorbed after it (S-4); null for every other answer.
|
|
115
|
+
*/
|
|
116
|
+
topology: SheetBootstrapTopology | null;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/*
|
|
120
|
+
* The topology rows a SNAPSHOT image absorbed after the request's applied seq
|
|
121
|
+
* (S-4): every STRUCTURE row (a row/column insert, delete or move; the op
|
|
122
|
+
* only, never applied to the image) in (afterSeq, image seq] when `complete`.
|
|
123
|
+
* A client re-maps its pending work through them; an incomplete set is empty
|
|
124
|
+
* and the client fails that work closed.
|
|
125
|
+
*/
|
|
126
|
+
export type SheetBootstrapTopology = {
|
|
127
|
+
afterSeq: SheetJournalSeq;
|
|
128
|
+
complete: boolean;
|
|
129
|
+
rows: SheetBootstrapRowPatch[];
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export type SheetBootstrapApplyResult = SheetBootstrapDeniedResult | SheetBootstrapDeferredResult | SheetBootstrapReadyResult;
|
|
133
|
+
|
|
134
|
+
export type SheetBootstrapSnapshotScope = {
|
|
135
|
+
organizationId: string;
|
|
136
|
+
sheetId: string;
|
|
137
|
+
sheetTabId: string;
|
|
138
|
+
viewport: SheetGridViewportObj;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
type SheetBootstrapScope = SheetBootstrapSnapshotScope;
|
|
142
|
+
|
|
143
|
+
/* One replayable DESIGN row: the tab image it carries and its design revision. */
|
|
144
|
+
export type SheetBootstrapDesignPatch = {
|
|
145
|
+
/* The row's tab image (design, name, ...); rename/reorder rows carry no design. */
|
|
146
|
+
tab: Record<string, unknown>;
|
|
147
|
+
designRevision: SheetJournalSeq;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/*
|
|
151
|
+
* One replayable structure operation with its design revision fence and command identity.
|
|
152
|
+
*/
|
|
153
|
+
export type SheetBootstrapStructurePatch = {
|
|
154
|
+
baseDesignRevision: SheetJournalSeq;
|
|
155
|
+
|
|
156
|
+
designRevision: SheetJournalSeq;
|
|
157
|
+
opId: string | null;
|
|
158
|
+
seq: SheetJournalSeq;
|
|
159
|
+
op:
|
|
160
|
+
| {
|
|
161
|
+
kind: 'INSERT_DELETE';
|
|
162
|
+
operation: SheetStructureOperationEnum;
|
|
163
|
+
index: number;
|
|
164
|
+
}
|
|
165
|
+
| { kind: 'AXIS_MOVE'; axisMoveOps: SheetAxisMoveInput[] };
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/*
|
|
169
|
+
* One validated journal row, applied as cells, computed values, tombstones and design. Structure operations apply first.
|
|
170
|
+
*/
|
|
171
|
+
export type SheetBootstrapRowPatch = {
|
|
172
|
+
kind: 'CELLS' | 'COMPUTED' | 'DESIGN' | 'STRUCTURE';
|
|
173
|
+
seq: SheetJournalSeq;
|
|
174
|
+
commandId: string | null;
|
|
175
|
+
cells: SheetAuthoritativeCellGQL[];
|
|
176
|
+
deletedCellCoords: SheetDeletedCellCoordGQL[];
|
|
177
|
+
/* Derived write-backs of one COMPUTED row; [] for every other kind. */
|
|
178
|
+
computedCells: SheetComputedCellGQL[];
|
|
179
|
+
/* Tab image of one DESIGN row; absent for every other kind. */
|
|
180
|
+
design?: SheetBootstrapDesignPatch;
|
|
181
|
+
/* Topology op of one STRUCTURE row; absent for every other kind. */
|
|
182
|
+
structureOp?: SheetBootstrapStructurePatch;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export const SHEET_BOOTSTRAP_CONTRACT_VERSION = 4;
|
|
186
|
+
|
|
187
|
+
const FILE_CONSTANTS = {
|
|
188
|
+
checksumPattern: /^[a-f0-9]{64}$/,
|
|
189
|
+
canonicalIntegerPattern: /^(0|[1-9]\d*)$/,
|
|
190
|
+
/* A canonical positive journal seq of at most 19 digits (bigint range). */
|
|
191
|
+
positiveSeqPattern: /^[1-9]\d{0,18}$/,
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Error raised when an openSheet response cannot safely become local state.
|
|
196
|
+
*/
|
|
197
|
+
export class SheetBootstrapValidationError extends Error {
|
|
198
|
+
readonly code: SheetBootstrapValidationCode;
|
|
199
|
+
|
|
200
|
+
/*
|
|
201
|
+
* Preserve a stable machine-readable failure code for snapshot fallback and
|
|
202
|
+
* telemetry while retaining a precise human-readable integrity failure.
|
|
203
|
+
*/
|
|
204
|
+
constructor(code: SheetBootstrapValidationCode, message: string) {
|
|
205
|
+
super(message);
|
|
206
|
+
this.name = 'SheetBootstrapValidationError';
|
|
207
|
+
this.code = code;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/*
|
|
212
|
+
* Throw one typed bootstrap validation failure.
|
|
213
|
+
*/
|
|
214
|
+
function failSheetBootstrap(code: SheetBootstrapValidationCode, message: string): never {
|
|
215
|
+
throw new SheetBootstrapValidationError(code, message);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/*
|
|
219
|
+
* Return one non-array object or fail closed on malformed transport data.
|
|
220
|
+
*/
|
|
221
|
+
function requireSheetBootstrapRecord(
|
|
222
|
+
value: unknown,
|
|
223
|
+
label: string,
|
|
224
|
+
code: SheetBootstrapValidationCode = 'INVALID_RESPONSE',
|
|
225
|
+
): Record<string, any> {
|
|
226
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
227
|
+
return failSheetBootstrap(code, `${label} must be an object.`);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return value as Record<string, any>;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/*
|
|
234
|
+
* Validate the explicit client/server bootstrap contract.
|
|
235
|
+
*/
|
|
236
|
+
function validateSheetBootstrapContract(bootstrap: SheetBootstrapGQL): {
|
|
237
|
+
contractVersion: typeof SHEET_BOOTSTRAP_CONTRACT_VERSION;
|
|
238
|
+
} {
|
|
239
|
+
if (bootstrap.contractVersion !== SHEET_BOOTSTRAP_CONTRACT_VERSION) {
|
|
240
|
+
return failSheetBootstrap('INVALID_CONTRACT', `Unsupported openSheet contract version: ${String(bootstrap.contractVersion)}.`);
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
contractVersion: SHEET_BOOTSTRAP_CONTRACT_VERSION,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/*
|
|
248
|
+
* Return whether a response carries any authorization-scoped workbook data.
|
|
249
|
+
*/
|
|
250
|
+
function sheetBootstrapHasScopedReadData(bootstrap: SheetBootstrapGQL): boolean {
|
|
251
|
+
return (
|
|
252
|
+
bootstrap.sheet != null ||
|
|
253
|
+
bootstrap.tabs != null ||
|
|
254
|
+
bootstrap.selectedTab != null ||
|
|
255
|
+
bootstrap.mode != null ||
|
|
256
|
+
bootstrap.currentSeq != null ||
|
|
257
|
+
bootstrap.servedViewport != null ||
|
|
258
|
+
bootstrap.snapshot != null ||
|
|
259
|
+
bootstrap.changes != null ||
|
|
260
|
+
bootstrap.realtimeToken != null
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/*
|
|
265
|
+
* Return one bounded signed realtime capability without normalizing its bytes.
|
|
266
|
+
*/
|
|
267
|
+
function requireSheetBootstrapRealtimeToken(value: unknown, label: string): string {
|
|
268
|
+
if (typeof value !== 'string' || !value || value !== value.trim() || value.length > 16_384) {
|
|
269
|
+
return failSheetBootstrap('INVALID_ACCESS', `${label} must be a bounded non-empty string.`);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return value;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/*
|
|
276
|
+
* Return a required non-empty GraphQL identifier as an exact string.
|
|
277
|
+
*/
|
|
278
|
+
function requireSheetBootstrapId(value: unknown, label: string, code: SheetBootstrapValidationCode = 'SCOPE_MISMATCH'): string {
|
|
279
|
+
if (typeof value !== 'string' || !value) {
|
|
280
|
+
return failSheetBootstrap(code, `${label} must be a non-empty string.`);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return value;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/*
|
|
287
|
+
* Return a canonical on-wire journal sequence without ever using Number math.
|
|
288
|
+
*/
|
|
289
|
+
function requireSheetBootstrapSeq(
|
|
290
|
+
value: unknown,
|
|
291
|
+
label: string,
|
|
292
|
+
code: SheetBootstrapValidationCode = 'INVALID_SEQUENCE',
|
|
293
|
+
): SheetJournalSeq {
|
|
294
|
+
if (typeof value !== 'string') {
|
|
295
|
+
return failSheetBootstrap(code, `${label} must be a canonical base-10 string.`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const parsed = parseSheetJournalSeq(value);
|
|
299
|
+
if (parsed === null || parsed !== value) {
|
|
300
|
+
return failSheetBootstrap(code, `${label} is not a canonical journal sequence.`);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return parsed;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/*
|
|
307
|
+
* Return a required positive grid index as an exact integer.
|
|
308
|
+
*/
|
|
309
|
+
function requireSheetBootstrapGridIndex(value: unknown, label: string): number {
|
|
310
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value <= 0) {
|
|
311
|
+
return failSheetBootstrap('INVALID_VIEWPORT', `${label} must be a positive safe integer.`);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return value;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/*
|
|
318
|
+
* Return a required non-negative integer metadata value.
|
|
319
|
+
*/
|
|
320
|
+
function requireSheetBootstrapCount(value: unknown, label: string): number {
|
|
321
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) {
|
|
322
|
+
return failSheetBootstrap('INVALID_SNAPSHOT', `${label} must be a non-negative safe integer.`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return value;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/*
|
|
329
|
+
* Validate and copy one full-grid viewport.
|
|
330
|
+
*/
|
|
331
|
+
function requireSheetBootstrapViewport(value: unknown, label: string): SheetGridViewportObj {
|
|
332
|
+
const viewport = requireSheetBootstrapRecord(value, label, 'INVALID_VIEWPORT');
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
startRowIndex: requireSheetBootstrapGridIndex(viewport.startRowIndex, `${label}.startRowIndex`),
|
|
336
|
+
startColumnIndex: requireSheetBootstrapGridIndex(viewport.startColumnIndex, `${label}.startColumnIndex`),
|
|
337
|
+
rowCount: requireSheetBootstrapGridIndex(viewport.rowCount, `${label}.rowCount`),
|
|
338
|
+
columnCount: requireSheetBootstrapGridIndex(viewport.columnCount, `${label}.columnCount`),
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/*
|
|
343
|
+
* Return whether two viewport values describe the exact same grid rectangle.
|
|
344
|
+
*/
|
|
345
|
+
function sheetBootstrapViewportsAreEqual(left: SheetGridViewportObj, right: SheetGridViewportObj): boolean {
|
|
346
|
+
return (
|
|
347
|
+
left.startRowIndex === right.startRowIndex &&
|
|
348
|
+
left.startColumnIndex === right.startColumnIndex &&
|
|
349
|
+
left.rowCount === right.rowCount &&
|
|
350
|
+
left.columnCount === right.columnCount
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/*
|
|
355
|
+
* Return the canonical full viewport implied by one selected tab design.
|
|
356
|
+
*/
|
|
357
|
+
function getExpectedSheetBootstrapViewport(selectedTab: SheetTabGQL): SheetGridViewportObj {
|
|
358
|
+
return getSheetViewFullViewport(
|
|
359
|
+
normalizeSheetDesign({
|
|
360
|
+
grid: selectedTab.design?.grid as any,
|
|
361
|
+
}),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/*
|
|
366
|
+
* Validate the embedded effective access facts before reading scoped data.
|
|
367
|
+
*/
|
|
368
|
+
function validateSheetBootstrapAccess(value: unknown, expectedSheetId: string): ValidatedSheetBootstrapAccess {
|
|
369
|
+
const access = requireSheetBootstrapRecord(value, 'openSheet.access', 'INVALID_ACCESS') as SheetAccessInfoGQL;
|
|
370
|
+
const sheetId = requireSheetBootstrapId(access.sheetId, 'openSheet.access.sheetId', 'INVALID_ACCESS');
|
|
371
|
+
if (sheetId !== expectedSheetId) {
|
|
372
|
+
failSheetBootstrap('INVALID_ACCESS', 'openSheet access belongs to a different sheet.');
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (typeof access.canView !== 'boolean' || typeof access.canEdit !== 'boolean' || typeof access.signInRequired !== 'boolean') {
|
|
376
|
+
failSheetBootstrap('INVALID_ACCESS', 'openSheet access flags must be explicit booleans.');
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const organizationId =
|
|
380
|
+
access.organizationId === null || access.organizationId === undefined
|
|
381
|
+
? null
|
|
382
|
+
: requireSheetBootstrapId(access.organizationId, 'openSheet.access.organizationId', 'INVALID_ACCESS');
|
|
383
|
+
if ((access.canView && !organizationId) || (access.canEdit && !access.canView) || (access.signInRequired && access.canView)) {
|
|
384
|
+
failSheetBootstrap('INVALID_ACCESS', 'openSheet access flags are internally inconsistent.');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return {
|
|
388
|
+
sheetId,
|
|
389
|
+
organizationId,
|
|
390
|
+
canView: access.canView,
|
|
391
|
+
canEdit: access.canEdit,
|
|
392
|
+
signInRequired: access.signInRequired,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export type ValidatedSheetRealtimeCapability = {
|
|
397
|
+
access: ValidatedSheetBootstrapAccess;
|
|
398
|
+
realtimeToken: string | null;
|
|
399
|
+
|
|
400
|
+
status: SheetBootstrapStatus;
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
/*
|
|
404
|
+
* Validate one capability renewal against the Sheet it renews. READY carries a
|
|
405
|
+
* bounded Sheet token; every other status carries no token at all.
|
|
406
|
+
*/
|
|
407
|
+
export function validateSheetRealtimeCapability(value: unknown, expectedSheetId: string): ValidatedSheetRealtimeCapability {
|
|
408
|
+
const capability = requireSheetBootstrapRecord(value, 'renewSheetRealtimeCapability') as SheetRealtimeCapabilityGQL;
|
|
409
|
+
if (!['DENIED', 'READY', 'RETRY', 'UNAVAILABLE'].includes(capability.status)) {
|
|
410
|
+
failSheetBootstrap('INVALID_STATUS', 'renewSheetRealtimeCapability returned an unknown status.');
|
|
411
|
+
}
|
|
412
|
+
const access = validateSheetBootstrapAccess(capability.access, expectedSheetId);
|
|
413
|
+
if (capability.status !== 'READY') {
|
|
414
|
+
return {
|
|
415
|
+
access,
|
|
416
|
+
realtimeToken: null,
|
|
417
|
+
|
|
418
|
+
status: capability.status,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
if (!access.canView) {
|
|
422
|
+
failSheetBootstrap('INVALID_ACCESS', 'A READY capability renewal needs view access.');
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
access,
|
|
427
|
+
realtimeToken: requireSheetBootstrapRealtimeToken(capability.realtimeToken, 'renewSheetRealtimeCapability.realtimeToken'),
|
|
428
|
+
|
|
429
|
+
status: 'READY',
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/*
|
|
434
|
+
* Validate one workbook's identity against the authorized bootstrap scope.
|
|
435
|
+
*/
|
|
436
|
+
function validateSheetBootstrapWorkbook(value: unknown, access: ValidatedSheetBootstrapAccess): SheetGQL {
|
|
437
|
+
const sheet = requireSheetBootstrapRecord(value, 'openSheet.sheet') as SheetGQL;
|
|
438
|
+
if (
|
|
439
|
+
requireSheetBootstrapId(sheet.id, 'openSheet.sheet.id') !== access.sheetId ||
|
|
440
|
+
requireSheetBootstrapId(sheet.organizationId, 'openSheet.sheet.organizationId') !== access.organizationId
|
|
441
|
+
) {
|
|
442
|
+
failSheetBootstrap('SCOPE_MISMATCH', 'openSheet workbook does not match its authorized scope.');
|
|
443
|
+
}
|
|
444
|
+
requireSheetBootstrapSeq(sheet.manifestRevision, 'openSheet.sheet.manifestRevision');
|
|
445
|
+
|
|
446
|
+
return sheet;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/*
|
|
450
|
+
* Validate tab summaries, their exact cursors, and same-workbook references.
|
|
451
|
+
*/
|
|
452
|
+
function validateSheetBootstrapTabSummaries(value: unknown): SheetTabSummaryGQL[] {
|
|
453
|
+
if (!Array.isArray(value)) {
|
|
454
|
+
return failSheetBootstrap('INVALID_RESPONSE', 'openSheet.tabs must be an array.');
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const tabIds = new Set<string>();
|
|
458
|
+
const tabs = value.map((item, index) => {
|
|
459
|
+
const tab = requireSheetBootstrapRecord(item, `openSheet.tabs[${index}]`) as SheetTabSummaryGQL;
|
|
460
|
+
const tabId = requireSheetBootstrapId(tab.id, `openSheet.tabs[${index}].id`);
|
|
461
|
+
if (tabIds.has(tabId)) {
|
|
462
|
+
failSheetBootstrap('SCOPE_MISMATCH', 'openSheet contains duplicate tab summaries.');
|
|
463
|
+
}
|
|
464
|
+
tabIds.add(tabId);
|
|
465
|
+
if (typeof tab.name !== 'string' || !tab.name.trim() || tab.name !== tab.name.trim()) {
|
|
466
|
+
failSheetBootstrap('INVALID_RESPONSE', `openSheet.tabs[${index}].name must be a canonical non-empty string.`);
|
|
467
|
+
}
|
|
468
|
+
if (typeof tab.hidden !== 'boolean') {
|
|
469
|
+
failSheetBootstrap('INVALID_RESPONSE', `openSheet.tabs[${index}].hidden must be a boolean.`);
|
|
470
|
+
}
|
|
471
|
+
requireSheetBootstrapSeq(tab.currentSeq, `openSheet.tabs[${index}].currentSeq`);
|
|
472
|
+
if (!Array.isArray(tab.referencedTabIds)) {
|
|
473
|
+
failSheetBootstrap('INVALID_RESPONSE', `openSheet.tabs[${index}].referencedTabIds must be an array.`);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return tab;
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
tabs.forEach((tab, tabIndex) => {
|
|
480
|
+
const referenceIds = new Set<string>();
|
|
481
|
+
(tab.referencedTabIds || []).forEach((referenceId, referenceIndex) => {
|
|
482
|
+
const id = requireSheetBootstrapId(referenceId, `openSheet.tabs[${tabIndex}].referencedTabIds[${referenceIndex}]`);
|
|
483
|
+
if (!tabIds.has(id) || referenceIds.has(id)) {
|
|
484
|
+
failSheetBootstrap('SCOPE_MISMATCH', 'openSheet tab references must be unique visible workbook tabs.');
|
|
485
|
+
}
|
|
486
|
+
referenceIds.add(id);
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
return tabs;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/*
|
|
494
|
+
* Validate one selected tab against its workbook and access ancestry.
|
|
495
|
+
*/
|
|
496
|
+
function validateSelectedSheetBootstrapTab(
|
|
497
|
+
value: unknown,
|
|
498
|
+
access: ValidatedSheetBootstrapAccess,
|
|
499
|
+
tabs: SheetTabSummaryGQL[],
|
|
500
|
+
): SheetTabGQL {
|
|
501
|
+
const selectedTab = requireSheetBootstrapRecord(value, 'openSheet.selectedTab') as SheetTabGQL;
|
|
502
|
+
const selectedTabId = requireSheetBootstrapId(selectedTab.id, 'openSheet.selectedTab.id');
|
|
503
|
+
if (
|
|
504
|
+
requireSheetBootstrapId(selectedTab.organizationId, 'openSheet.selectedTab.organizationId') !== access.organizationId ||
|
|
505
|
+
requireSheetBootstrapId(selectedTab.sheetId, 'openSheet.selectedTab.sheetId') !== access.sheetId ||
|
|
506
|
+
!tabs.some((tab) => tab.id === selectedTabId)
|
|
507
|
+
) {
|
|
508
|
+
failSheetBootstrap('SCOPE_MISMATCH', 'openSheet selected tab is outside the authorized workbook.');
|
|
509
|
+
}
|
|
510
|
+
requireSheetBootstrapSeq(selectedTab.designRevision, 'openSheet.selectedTab.designRevision');
|
|
511
|
+
|
|
512
|
+
return selectedTab;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/*
|
|
516
|
+
* Require one tab-owned record to carry the exact authorized ancestry.
|
|
517
|
+
*/
|
|
518
|
+
function validateSheetBootstrapRecordScope(value: Record<string, any>, scope: SheetBootstrapScope, label: string): void {
|
|
519
|
+
if (
|
|
520
|
+
requireSheetBootstrapId(value.organizationId, `${label}.organizationId`) !== scope.organizationId ||
|
|
521
|
+
requireSheetBootstrapId(value.sheetId, `${label}.sheetId`) !== scope.sheetId ||
|
|
522
|
+
requireSheetBootstrapId(value.sheetTabId, `${label}.sheetTabId`) !== scope.sheetTabId
|
|
523
|
+
) {
|
|
524
|
+
failSheetBootstrap('SCOPE_MISMATCH', `${label} belongs to a different sheet scope.`);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/*
|
|
529
|
+
* Validate one coordinate and require it to be inside the served viewport.
|
|
530
|
+
*/
|
|
531
|
+
function validateSheetBootstrapCoordinate(
|
|
532
|
+
value: Record<string, any>,
|
|
533
|
+
scope: SheetBootstrapScope,
|
|
534
|
+
label: string,
|
|
535
|
+
): { rowIndex: number; columnIndex: number } {
|
|
536
|
+
const rowIndex = requireSheetBootstrapGridIndex(value.rowIndex, `${label}.rowIndex`);
|
|
537
|
+
const columnIndex = requireSheetBootstrapGridIndex(value.columnIndex, `${label}.columnIndex`);
|
|
538
|
+
const lastRowIndex = scope.viewport.startRowIndex + scope.viewport.rowCount - 1;
|
|
539
|
+
const lastColumnIndex = scope.viewport.startColumnIndex + scope.viewport.columnCount - 1;
|
|
540
|
+
if (
|
|
541
|
+
rowIndex < scope.viewport.startRowIndex ||
|
|
542
|
+
rowIndex > lastRowIndex ||
|
|
543
|
+
columnIndex < scope.viewport.startColumnIndex ||
|
|
544
|
+
columnIndex > lastColumnIndex
|
|
545
|
+
) {
|
|
546
|
+
failSheetBootstrap('INVALID_VIEWPORT', `${label} is outside the served viewport.`);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return { rowIndex, columnIndex };
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/*
|
|
553
|
+
* Validate one replay cell and return its canonical coordinate key.
|
|
554
|
+
*/
|
|
555
|
+
function validateSheetBootstrapCell(value: unknown, scope: SheetBootstrapScope, label: string): string {
|
|
556
|
+
const cell = requireSheetBootstrapRecord(value, label, 'INVALID_CHANGE');
|
|
557
|
+
validateSheetBootstrapRecordScope(cell, scope, label);
|
|
558
|
+
requireSheetBootstrapId(cell.id, `${label}.id`, 'INVALID_CHANGE');
|
|
559
|
+
const revision = requireSheetBootstrapSeq(cell.revision, `${label}.revision`);
|
|
560
|
+
if (!sheetJournalSeqIsPositive(revision)) {
|
|
561
|
+
failSheetBootstrap('INVALID_SEQUENCE', `${label}.revision must be positive.`);
|
|
562
|
+
}
|
|
563
|
+
const coordinate = validateSheetBootstrapCoordinate(cell, scope, label);
|
|
564
|
+
|
|
565
|
+
return `${coordinate.rowIndex}:${coordinate.columnIndex}`;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/*
|
|
569
|
+
* Return whether one decoded snapshot cell passes every check
|
|
570
|
+
* validateSheetBootstrapCell makes, without building labels or BigInts. A
|
|
571
|
+
* false result only means the caller must run validateSheetBootstrapCell to
|
|
572
|
+
* raise the exact failure.
|
|
573
|
+
*/
|
|
574
|
+
function isValidSheetBootstrapSnapshotCell(
|
|
575
|
+
value: unknown,
|
|
576
|
+
scope: SheetBootstrapScope,
|
|
577
|
+
lastRowIndex: number,
|
|
578
|
+
lastColumnIndex: number,
|
|
579
|
+
): value is Record<string, any> {
|
|
580
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
const cell = value as Record<string, any>;
|
|
585
|
+
const { id, revision, rowIndex, columnIndex } = cell;
|
|
586
|
+
return (
|
|
587
|
+
typeof cell.organizationId === 'string' &&
|
|
588
|
+
cell.organizationId !== '' &&
|
|
589
|
+
cell.organizationId === scope.organizationId &&
|
|
590
|
+
typeof cell.sheetId === 'string' &&
|
|
591
|
+
cell.sheetId !== '' &&
|
|
592
|
+
cell.sheetId === scope.sheetId &&
|
|
593
|
+
typeof cell.sheetTabId === 'string' &&
|
|
594
|
+
cell.sheetTabId !== '' &&
|
|
595
|
+
cell.sheetTabId === scope.sheetTabId &&
|
|
596
|
+
typeof id === 'string' &&
|
|
597
|
+
id !== '' &&
|
|
598
|
+
typeof revision === 'string' &&
|
|
599
|
+
FILE_CONSTANTS.positiveSeqPattern.test(revision) &&
|
|
600
|
+
(revision.length < 19 || revision <= SHEET_JOURNAL_MAX_SEQ) &&
|
|
601
|
+
typeof rowIndex === 'number' &&
|
|
602
|
+
Number.isSafeInteger(rowIndex) &&
|
|
603
|
+
rowIndex > 0 &&
|
|
604
|
+
typeof columnIndex === 'number' &&
|
|
605
|
+
Number.isSafeInteger(columnIndex) &&
|
|
606
|
+
columnIndex > 0 &&
|
|
607
|
+
!(
|
|
608
|
+
rowIndex < scope.viewport.startRowIndex ||
|
|
609
|
+
rowIndex > lastRowIndex ||
|
|
610
|
+
columnIndex < scope.viewport.startColumnIndex ||
|
|
611
|
+
columnIndex > lastColumnIndex
|
|
612
|
+
)
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/*
|
|
617
|
+
* Validate one deletion tombstone and return its canonical coordinate key.
|
|
618
|
+
*/
|
|
619
|
+
function validateSheetBootstrapDeletedCoordinate(value: unknown, scope: SheetBootstrapScope, label: string): string {
|
|
620
|
+
const coordinate = requireSheetBootstrapRecord(value, label, 'INVALID_CHANGE');
|
|
621
|
+
if (requireSheetBootstrapId(coordinate.sheetTabId, `${label}.sheetTabId`, 'INVALID_CHANGE') !== scope.sheetTabId) {
|
|
622
|
+
failSheetBootstrap('SCOPE_MISMATCH', `${label} belongs to a different Sheet tab.`);
|
|
623
|
+
}
|
|
624
|
+
const indexes = validateSheetBootstrapCoordinate(coordinate, scope, label);
|
|
625
|
+
if (coordinate.id === null && coordinate.revision === null) {
|
|
626
|
+
return `${indexes.rowIndex}:${indexes.columnIndex}`;
|
|
627
|
+
}
|
|
628
|
+
if (typeof coordinate.id !== 'string' || !coordinate.id || coordinate.id.trim() !== coordinate.id) {
|
|
629
|
+
failSheetBootstrap('INVALID_CHANGE', `${label}.id must be a non-empty string or explicit null.`);
|
|
630
|
+
}
|
|
631
|
+
const revision = requireSheetBootstrapSeq(coordinate.revision, `${label}.revision`);
|
|
632
|
+
if (!sheetJournalSeqIsPositive(revision)) {
|
|
633
|
+
failSheetBootstrap('INVALID_SEQUENCE', `${label}.revision must be positive.`);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
return `${indexes.rowIndex}:${indexes.columnIndex}`;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/*
|
|
640
|
+
* Compute the lowercase SHA-256 checksum for an exact UTF-8 snapshot payload.
|
|
641
|
+
*/
|
|
642
|
+
export async function getSheetBootstrapPayloadChecksum(payload: string): Promise<string> {
|
|
643
|
+
if (!globalThis.crypto?.subtle) {
|
|
644
|
+
return failSheetBootstrap('INVALID_SNAPSHOT', 'SHA-256 is unavailable in this runtime.');
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(payload));
|
|
648
|
+
|
|
649
|
+
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/*
|
|
653
|
+
* Decode and verify every immutable snapshot integrity field before replay.
|
|
654
|
+
*/
|
|
655
|
+
export async function decodeVerifiedSheetBootstrapSnapshot(params: {
|
|
656
|
+
snapshot: unknown;
|
|
657
|
+
currentSeq: SheetJournalSeq;
|
|
658
|
+
scope: SheetBootstrapSnapshotScope;
|
|
659
|
+
}): Promise<{
|
|
660
|
+
seq: SheetJournalSeq;
|
|
661
|
+
encodingVersion: number;
|
|
662
|
+
checksum: string;
|
|
663
|
+
data: ReturnType<typeof decodeSheetSnapshot>;
|
|
664
|
+
}> {
|
|
665
|
+
const snapshot = requireSheetBootstrapRecord(params.snapshot, 'openSheet.snapshot', 'INVALID_SNAPSHOT');
|
|
666
|
+
const seq = requireSheetBootstrapSeq(snapshot.seq, 'openSheet.snapshot.seq');
|
|
667
|
+
if (compareSheetJournalSeq(seq, params.currentSeq) > 0) {
|
|
668
|
+
failSheetBootstrap('INVALID_SEQUENCE', 'openSheet snapshot sequence is ahead of the selected tab.');
|
|
669
|
+
}
|
|
670
|
+
if (!isSupportedSheetSnapshotEncodingVersion(snapshot.encodingVersion)) {
|
|
671
|
+
failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot encoding version is unsupported.');
|
|
672
|
+
}
|
|
673
|
+
// Contract 3 selects only {seq, encodingVersion, checksum, payload}: the
|
|
674
|
+
// viewport, byte and cell counts are verified when a server still sends
|
|
675
|
+
// them; otherwise the image is checked against its own full grid.
|
|
676
|
+
const servedViewport =
|
|
677
|
+
snapshot.viewport === null || snapshot.viewport === undefined
|
|
678
|
+
? null
|
|
679
|
+
: requireSheetBootstrapViewport(snapshot.viewport, 'openSheet.snapshot.viewport');
|
|
680
|
+
const viewport = servedViewport ?? params.scope.viewport;
|
|
681
|
+
if (typeof snapshot.payload !== 'string' || !snapshot.payload) {
|
|
682
|
+
failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot payload must be a non-empty string.');
|
|
683
|
+
}
|
|
684
|
+
if (typeof snapshot.checksum !== 'string' || !FILE_CONSTANTS.checksumPattern.test(snapshot.checksum)) {
|
|
685
|
+
failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot checksum must be lowercase SHA-256.');
|
|
686
|
+
}
|
|
687
|
+
if (snapshot.payloadBytes !== null && snapshot.payloadBytes !== undefined) {
|
|
688
|
+
if (typeof snapshot.payloadBytes !== 'string' || !FILE_CONSTANTS.canonicalIntegerPattern.test(snapshot.payloadBytes)) {
|
|
689
|
+
failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot payloadBytes must be a canonical integer string.');
|
|
690
|
+
}
|
|
691
|
+
const payloadBytes = new TextEncoder().encode(snapshot.payload).byteLength;
|
|
692
|
+
if (BigInt(snapshot.payloadBytes) !== BigInt(payloadBytes)) {
|
|
693
|
+
failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot UTF-8 byte length does not match metadata.');
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
const cellCount =
|
|
697
|
+
snapshot.cellCount === null || snapshot.cellCount === undefined
|
|
698
|
+
? null
|
|
699
|
+
: requireSheetBootstrapCount(snapshot.cellCount, 'openSheet.snapshot.cellCount');
|
|
700
|
+
if ((await getSheetBootstrapPayloadChecksum(snapshot.payload)) !== snapshot.checksum) {
|
|
701
|
+
failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot SHA-256 checksum verification failed.');
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
let data: ReturnType<typeof decodeSheetSnapshot>;
|
|
705
|
+
try {
|
|
706
|
+
data = decodeSheetSnapshot(
|
|
707
|
+
snapshot.payload,
|
|
708
|
+
{
|
|
709
|
+
organizationId: params.scope.organizationId,
|
|
710
|
+
sheetId: params.scope.sheetId,
|
|
711
|
+
sheetTabId: params.scope.sheetTabId,
|
|
712
|
+
seq,
|
|
713
|
+
},
|
|
714
|
+
viewport,
|
|
715
|
+
);
|
|
716
|
+
} catch {
|
|
717
|
+
return failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot payload could not be decoded.');
|
|
718
|
+
}
|
|
719
|
+
if (cellCount !== null && data.cells.length !== cellCount) {
|
|
720
|
+
failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot decoded cell count does not match metadata.');
|
|
721
|
+
}
|
|
722
|
+
// Without a served viewport the image's own grid at its seq bounds it: the
|
|
723
|
+
// selected tab at currentSeq may have lost lines the tail deletes.
|
|
724
|
+
const scope: SheetBootstrapScope = {
|
|
725
|
+
...params.scope,
|
|
726
|
+
viewport:
|
|
727
|
+
servedViewport ?? (data.sheetTab?.design?.grid ? getExpectedSheetBootstrapViewport(data.sheetTab as SheetTabGQL) : viewport),
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
const cellCoordinates = new Set<string>();
|
|
731
|
+
const lastRowIndex = scope.viewport.startRowIndex + scope.viewport.rowCount - 1;
|
|
732
|
+
const lastColumnIndex = scope.viewport.startColumnIndex + scope.viewport.columnCount - 1;
|
|
733
|
+
data.cells.forEach((cell, index) => {
|
|
734
|
+
// Fast accept; any failing cell reruns the full validator for its exact error
|
|
735
|
+
const key = isValidSheetBootstrapSnapshotCell(cell, scope, lastRowIndex, lastColumnIndex)
|
|
736
|
+
? `${cell.rowIndex}:${cell.columnIndex}`
|
|
737
|
+
: validateSheetBootstrapCell(cell, scope, `snapshot.cells[${index}]`);
|
|
738
|
+
if (cellCoordinates.has(key)) {
|
|
739
|
+
failSheetBootstrap('INVALID_SNAPSHOT', 'openSheet snapshot contains duplicate cell coordinates.');
|
|
740
|
+
}
|
|
741
|
+
cellCoordinates.add(key);
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
requireSheetBootstrapRecord(data.pageInfo, 'snapshot.pageInfo', 'INVALID_SNAPSHOT');
|
|
745
|
+
|
|
746
|
+
return {
|
|
747
|
+
seq,
|
|
748
|
+
encodingVersion: snapshot.encodingVersion,
|
|
749
|
+
checksum: snapshot.checksum,
|
|
750
|
+
data,
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/*
|
|
755
|
+
* Parse one journal row's JSON payload into a record, failing closed on a
|
|
756
|
+
* missing, non-string, malformed, or non-object payload.
|
|
757
|
+
*/
|
|
758
|
+
function parseSheetBootstrapChangePayload(change: SheetTabChangeGQL, index: number): Record<string, any> {
|
|
759
|
+
if (typeof change.payload !== 'string' || !change.payload) {
|
|
760
|
+
return failSheetBootstrap('INVALID_CHANGE', `openSheet.changes[${index}].payload must be a JSON string.`);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
try {
|
|
764
|
+
return requireSheetBootstrapRecord(JSON.parse(change.payload), `openSheet.changes[${index}].payload`, 'INVALID_CHANGE');
|
|
765
|
+
} catch (error) {
|
|
766
|
+
if (error instanceof SheetBootstrapValidationError) {
|
|
767
|
+
throw error;
|
|
768
|
+
}
|
|
769
|
+
return failSheetBootstrap('INVALID_CHANGE', `openSheet.changes[${index}].payload is invalid JSON.`);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/*
|
|
774
|
+
* Return the command id a journal row carries, or null for an anonymous row.
|
|
775
|
+
*/
|
|
776
|
+
function getSheetBootstrapRowCommandId(change: SheetTabChangeGQL) {
|
|
777
|
+
return typeof change.commandId === 'string' && change.commandId ? change.commandId : null;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/*
|
|
781
|
+
* Validate one row's cells and tombstones against the scope and return them
|
|
782
|
+
* typed; duplicate cell coordinates inside one row fail closed.
|
|
783
|
+
*/
|
|
784
|
+
function validateSheetBootstrapRowCells(payload: Record<string, any>, scope: SheetBootstrapScope, index: number) {
|
|
785
|
+
const patchCoordinates = new Set<string>();
|
|
786
|
+
payload.cells.forEach((cell: unknown, cellIndex: number) => {
|
|
787
|
+
const key = validateSheetBootstrapCell(cell, scope, `openSheet.changes[${index}].cells[${cellIndex}]`);
|
|
788
|
+
if (patchCoordinates.has(key)) {
|
|
789
|
+
failSheetBootstrap('INVALID_CHANGE', `openSheet.changes[${index}] contains duplicate cell coordinates.`);
|
|
790
|
+
}
|
|
791
|
+
patchCoordinates.add(key);
|
|
792
|
+
});
|
|
793
|
+
payload.deletedCellCoords.forEach((coordinate: unknown, coordinateIndex: number) => {
|
|
794
|
+
validateSheetBootstrapDeletedCoordinate(coordinate, scope, `openSheet.changes[${index}].deletedCellCoords[${coordinateIndex}]`);
|
|
795
|
+
});
|
|
796
|
+
return {
|
|
797
|
+
cells: payload.cells as SheetAuthoritativeCellGQL[],
|
|
798
|
+
deletedCellCoords: payload.deletedCellCoords as SheetDeletedCellCoordGQL[],
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/*
|
|
803
|
+
* Validate one replayable v3 CELLS journal row and return its typed patch.
|
|
804
|
+
*/
|
|
805
|
+
function parseSheetBootstrapCellPatch(
|
|
806
|
+
change: SheetTabChangeGQL,
|
|
807
|
+
scope: SheetBootstrapScope,
|
|
808
|
+
index: number,
|
|
809
|
+
seq: SheetJournalSeq,
|
|
810
|
+
): SheetBootstrapRowPatch {
|
|
811
|
+
const payload = parseSheetBootstrapChangePayload(change, index);
|
|
812
|
+
if (
|
|
813
|
+
payload.v !== SHEET_CELLS_JOURNAL_VERSION ||
|
|
814
|
+
!Array.isArray(payload.cells) ||
|
|
815
|
+
!Array.isArray(payload.deletedCellCoords) ||
|
|
816
|
+
!isHardNewSheetCellsJournalOrderedTargetGroups(payload.orderedTargetGroups) ||
|
|
817
|
+
payload.truncated ||
|
|
818
|
+
payload.requiresSnapshot
|
|
819
|
+
) {
|
|
820
|
+
failSheetBootstrap('INVALID_CHANGE', `openSheet.changes[${index}] is not a replayable CELLS payload.`);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
return {
|
|
824
|
+
...validateSheetBootstrapRowCells(payload, scope, index),
|
|
825
|
+
commandId: getSheetBootstrapRowCommandId(change),
|
|
826
|
+
computedCells: [],
|
|
827
|
+
kind: 'CELLS',
|
|
828
|
+
seq,
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
/*
|
|
833
|
+
* Validate one replayable v1 COMPUTED journal row and return its typed patch:
|
|
834
|
+
* every item must be an exact derived-fields-only item of the selected tab,
|
|
835
|
+
* inside the scope's grid, with no duplicate coordinates.
|
|
836
|
+
*/
|
|
837
|
+
function parseSheetBootstrapComputedPatch(
|
|
838
|
+
change: SheetTabChangeGQL,
|
|
839
|
+
scope: SheetBootstrapScope,
|
|
840
|
+
index: number,
|
|
841
|
+
seq: SheetJournalSeq,
|
|
842
|
+
): SheetBootstrapRowPatch {
|
|
843
|
+
const payload = parseSheetBootstrapChangePayload(change, index);
|
|
844
|
+
if (payload.v !== SHEET_COMPUTED_JOURNAL_VERSION || !Array.isArray(payload.cells) || payload.truncated || payload.requiresSnapshot) {
|
|
845
|
+
failSheetBootstrap('INVALID_CHANGE', `openSheet.changes[${index}] is not a replayable COMPUTED payload.`);
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
const patchCoordinates = new Set<string>();
|
|
849
|
+
payload.cells.forEach((cell: unknown, cellIndex: number) => {
|
|
850
|
+
const label = `openSheet.changes[${index}].cells[${cellIndex}]`;
|
|
851
|
+
if (!isSheetComputedJournalCell(cell, scope.sheetTabId)) {
|
|
852
|
+
failSheetBootstrap('INVALID_CHANGE', `${label} is not an exact derived-fields-only cell of the selected tab.`);
|
|
853
|
+
}
|
|
854
|
+
const coordinate = validateSheetBootstrapCoordinate(cell, scope, label);
|
|
855
|
+
const key = `${coordinate.rowIndex}:${coordinate.columnIndex}`;
|
|
856
|
+
if (patchCoordinates.has(key)) {
|
|
857
|
+
failSheetBootstrap('INVALID_CHANGE', `openSheet.changes[${index}] contains duplicate cell coordinates.`);
|
|
858
|
+
}
|
|
859
|
+
patchCoordinates.add(key);
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
return {
|
|
863
|
+
cells: [],
|
|
864
|
+
commandId: getSheetBootstrapRowCommandId(change),
|
|
865
|
+
computedCells: payload.cells as SheetComputedCellGQL[],
|
|
866
|
+
deletedCellCoords: [],
|
|
867
|
+
kind: 'COMPUTED',
|
|
868
|
+
seq,
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/*
|
|
873
|
+
* Return whether one unknown value is a positive safe integer grid index.
|
|
874
|
+
*/
|
|
875
|
+
function isSheetBootstrapGridIndex(value: unknown): value is number {
|
|
876
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/*
|
|
880
|
+
* Validate one replayable v2 DESIGN journal row and return its typed patch.
|
|
881
|
+
* The row applies from its full tab image; a rename or reorder row carries no
|
|
882
|
+
* design and only moves the revision. Ordering metadata is server-side state
|
|
883
|
+
* the replay does not consume, so it is not validated here.
|
|
884
|
+
*/
|
|
885
|
+
function parseSheetBootstrapDesignPatch(change: SheetTabChangeGQL, index: number, seq: SheetJournalSeq): SheetBootstrapRowPatch {
|
|
886
|
+
const label = `openSheet.changes[${index}]`;
|
|
887
|
+
const payload = parseSheetBootstrapChangePayload(change, index);
|
|
888
|
+
if (
|
|
889
|
+
payload.v !== SHEET_DESIGN_JOURNAL_VERSION ||
|
|
890
|
+
payload.truncated ||
|
|
891
|
+
payload.requiresSnapshot ||
|
|
892
|
+
!payload.tab ||
|
|
893
|
+
typeof payload.tab !== 'object' ||
|
|
894
|
+
Array.isArray(payload.tab)
|
|
895
|
+
) {
|
|
896
|
+
failSheetBootstrap('INVALID_CHANGE', `${label} is not a replayable DESIGN payload.`);
|
|
897
|
+
}
|
|
898
|
+
const designRevision = requireSheetBootstrapSeq(payload.designRevision, `${label}.designRevision`, 'INVALID_CHANGE');
|
|
899
|
+
const tab = payload.tab as Record<string, unknown>;
|
|
900
|
+
if (String(tab.designRevision) !== designRevision) {
|
|
901
|
+
failSheetBootstrap('INVALID_CHANGE', `${label}.tab does not carry the row's design revision.`);
|
|
902
|
+
}
|
|
903
|
+
if (tab.design !== undefined && tab.design !== null && (typeof tab.design !== 'object' || Array.isArray(tab.design))) {
|
|
904
|
+
failSheetBootstrap('INVALID_CHANGE', `${label}.tab.design must be an object when present.`);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
return {
|
|
908
|
+
cells: [],
|
|
909
|
+
commandId: getSheetBootstrapRowCommandId(change),
|
|
910
|
+
computedCells: [],
|
|
911
|
+
deletedCellCoords: [],
|
|
912
|
+
design: { designRevision, tab: { ...tab, designRevision } },
|
|
913
|
+
kind: 'DESIGN',
|
|
914
|
+
seq,
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/*
|
|
919
|
+
* Validate the geometry of one axis move; its fit against the replayed grid
|
|
920
|
+
* is checked when it is applied.
|
|
921
|
+
*/
|
|
922
|
+
function validateSheetBootstrapAxisMove(value: unknown, label: string): SheetAxisMoveInput {
|
|
923
|
+
const axisMove = requireSheetBootstrapRecord(value, label, 'INVALID_CHANGE');
|
|
924
|
+
if (
|
|
925
|
+
(axisMove.axis !== 'ROW' && axisMove.axis !== 'COLUMN') ||
|
|
926
|
+
!isSheetBootstrapGridIndex(axisMove.sourceStartIndex) ||
|
|
927
|
+
!isSheetBootstrapGridIndex(axisMove.sourceEndIndex) ||
|
|
928
|
+
!isSheetBootstrapGridIndex(axisMove.targetStartIndex)
|
|
929
|
+
) {
|
|
930
|
+
failSheetBootstrap('INVALID_CHANGE', `${label} is not a row or column move.`);
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
return {
|
|
934
|
+
axis: axisMove.axis,
|
|
935
|
+
sourceEndIndex: axisMove.sourceEndIndex,
|
|
936
|
+
sourceStartIndex: axisMove.sourceStartIndex,
|
|
937
|
+
targetStartIndex: axisMove.targetStartIndex,
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
/*
|
|
942
|
+
* Validate one replayable v2 STRUCTURE journal row and return its typed patch.
|
|
943
|
+
* A row that restored state its op cannot express (`requiresSnapshot: true`,
|
|
944
|
+
* a structure undo re-inserting a deleted axis) fails closed so the caller
|
|
945
|
+
* falls back to a snapshot; the server excludes such rows from replayable
|
|
946
|
+
* tails already. `opOnly` reads a topology row (S-4) for its op alone, which
|
|
947
|
+
* stands whether or not the row itself replays.
|
|
948
|
+
*/
|
|
949
|
+
function parseSheetBootstrapStructurePatch(
|
|
950
|
+
change: SheetTabChangeGQL,
|
|
951
|
+
index: number,
|
|
952
|
+
seq: SheetJournalSeq,
|
|
953
|
+
opOnly = false,
|
|
954
|
+
): SheetBootstrapRowPatch {
|
|
955
|
+
const label = `openSheet.changes[${index}]`;
|
|
956
|
+
const payload = parseSheetBootstrapChangePayload(change, index);
|
|
957
|
+
if (
|
|
958
|
+
payload.v !== SHEET_STRUCTURE_JOURNAL_VERSION ||
|
|
959
|
+
(payload.truncated && !opOnly) ||
|
|
960
|
+
!payload.structureOp ||
|
|
961
|
+
typeof payload.structureOp !== 'object' ||
|
|
962
|
+
Array.isArray(payload.structureOp)
|
|
963
|
+
) {
|
|
964
|
+
failSheetBootstrap('INVALID_CHANGE', `${label} is not a replayable STRUCTURE payload.`);
|
|
965
|
+
}
|
|
966
|
+
if (payload.requiresSnapshot && !opOnly) {
|
|
967
|
+
failSheetBootstrap('INVALID_CHANGE', `${label} restored state its structure op cannot express and demands a snapshot.`);
|
|
968
|
+
}
|
|
969
|
+
const baseDesignRevision = requireSheetBootstrapSeq(payload.baseDesignRevision, `${label}.baseDesignRevision`, 'INVALID_CHANGE');
|
|
970
|
+
const designRevision = requireSheetBootstrapSeq(payload.designRevision, `${label}.designRevision`, 'INVALID_CHANGE');
|
|
971
|
+
|
|
972
|
+
const structureOp = payload.structureOp as Record<string, unknown>;
|
|
973
|
+
if (
|
|
974
|
+
requireSheetBootstrapSeq(structureOp.structureSeq, `${label}.structureOp.structureSeq`, 'INVALID_CHANGE') !== seq ||
|
|
975
|
+
(structureOp.opId !== null && (typeof structureOp.opId !== 'string' || !structureOp.opId))
|
|
976
|
+
) {
|
|
977
|
+
failSheetBootstrap('INVALID_CHANGE', `${label}.structureOp does not describe this row.`);
|
|
978
|
+
}
|
|
979
|
+
const patch = {
|
|
980
|
+
baseDesignRevision,
|
|
981
|
+
|
|
982
|
+
designRevision,
|
|
983
|
+
opId: structureOp.opId === null ? null : String(structureOp.opId),
|
|
984
|
+
seq,
|
|
985
|
+
};
|
|
986
|
+
const row = {
|
|
987
|
+
cells: [],
|
|
988
|
+
commandId: getSheetBootstrapRowCommandId(change) ?? patch.opId,
|
|
989
|
+
computedCells: [],
|
|
990
|
+
deletedCellCoords: [],
|
|
991
|
+
kind: 'STRUCTURE' as const,
|
|
992
|
+
seq,
|
|
993
|
+
};
|
|
994
|
+
if (structureOp.kind === 'INSERT_DELETE') {
|
|
995
|
+
const operation = structureOp.operation as SheetStructureOperationEnum;
|
|
996
|
+
if (!SHEET_STRUCTURE_OPERATION_ENUMS.includes(operation) || !isSheetBootstrapGridIndex(structureOp.index)) {
|
|
997
|
+
failSheetBootstrap('INVALID_CHANGE', `${label}.structureOp is not a row or column insert/delete.`);
|
|
998
|
+
}
|
|
999
|
+
return {
|
|
1000
|
+
...row,
|
|
1001
|
+
structureOp: {
|
|
1002
|
+
...patch,
|
|
1003
|
+
op: { kind: 'INSERT_DELETE', operation, index: structureOp.index },
|
|
1004
|
+
},
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
if (
|
|
1008
|
+
structureOp.kind !== 'AXIS_MOVE' ||
|
|
1009
|
+
structureOp.operation !== 'MOVE_AXIS' ||
|
|
1010
|
+
!Array.isArray(payload.axisMoveOps) ||
|
|
1011
|
+
!payload.axisMoveOps.length
|
|
1012
|
+
) {
|
|
1013
|
+
failSheetBootstrap('INVALID_CHANGE', `${label}.structureOp is not a known structure op.`);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
return {
|
|
1017
|
+
...row,
|
|
1018
|
+
structureOp: {
|
|
1019
|
+
...patch,
|
|
1020
|
+
op: {
|
|
1021
|
+
kind: 'AXIS_MOVE',
|
|
1022
|
+
axisMoveOps: payload.axisMoveOps.map((op: unknown, opIndex: number) =>
|
|
1023
|
+
validateSheetBootstrapAxisMove(op, `${label}.axisMoveOps[${opIndex}]`),
|
|
1024
|
+
),
|
|
1025
|
+
},
|
|
1026
|
+
},
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/*
|
|
1031
|
+
* Validate a complete, contiguous, selected-tab-only journal tail of
|
|
1032
|
+
* replayable CELLS / COMPUTED / DESIGN / STRUCTURE / REGION (v2) rows and
|
|
1033
|
+
* return one typed patch per row in seq order. Design revisions and topology
|
|
1034
|
+
* are applied by the client's tab reducer, which knows the state the tail
|
|
1035
|
+
* replays over.
|
|
1036
|
+
*/
|
|
1037
|
+
export function validateSheetBootstrapChanges(params: {
|
|
1038
|
+
value: unknown;
|
|
1039
|
+
afterSeq: SheetJournalSeq;
|
|
1040
|
+
currentSeq: SheetJournalSeq;
|
|
1041
|
+
scope: SheetBootstrapSnapshotScope;
|
|
1042
|
+
}): SheetBootstrapRowPatch[] {
|
|
1043
|
+
if (!Array.isArray(params.value)) {
|
|
1044
|
+
return failSheetBootstrap('INVALID_CHANGE', 'openSheet.changes must be an array.');
|
|
1045
|
+
}
|
|
1046
|
+
if (compareSheetJournalSeq(params.afterSeq, params.currentSeq) > 0) {
|
|
1047
|
+
failSheetBootstrap('INVALID_SEQUENCE', 'openSheet replay base is ahead of currentSeq.');
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
let expectedSeq = BigInt(params.afterSeq) + 1n;
|
|
1051
|
+
const patches = params.value.map((item, index) => {
|
|
1052
|
+
const change = requireSheetBootstrapRecord(item, `openSheet.changes[${index}]`, 'INVALID_CHANGE') as SheetTabChangeGQL;
|
|
1053
|
+
if (
|
|
1054
|
+
requireSheetBootstrapId(change.sheetTabId, `openSheet.changes[${index}].sheetTabId`, 'INVALID_CHANGE') !==
|
|
1055
|
+
params.scope.sheetTabId ||
|
|
1056
|
+
(change.kind !== 'CELLS' && change.kind !== 'COMPUTED' && change.kind !== 'DESIGN' && change.kind !== 'STRUCTURE')
|
|
1057
|
+
) {
|
|
1058
|
+
failSheetBootstrap(
|
|
1059
|
+
'SCOPE_MISMATCH',
|
|
1060
|
+
`openSheet.changes[${index}] is not a selected-tab CELLS, COMPUTED, DESIGN, or STRUCTURE row.`,
|
|
1061
|
+
);
|
|
1062
|
+
}
|
|
1063
|
+
const seq = requireSheetBootstrapSeq(change.seq, `openSheet.changes[${index}].seq`);
|
|
1064
|
+
if (BigInt(seq) !== expectedSeq) {
|
|
1065
|
+
failSheetBootstrap('INVALID_SEQUENCE', `openSheet changes are not contiguous after ${params.afterSeq}.`);
|
|
1066
|
+
}
|
|
1067
|
+
expectedSeq += 1n;
|
|
1068
|
+
|
|
1069
|
+
switch (change.kind) {
|
|
1070
|
+
case 'COMPUTED':
|
|
1071
|
+
return parseSheetBootstrapComputedPatch(change, params.scope, index, seq);
|
|
1072
|
+
case 'DESIGN':
|
|
1073
|
+
return parseSheetBootstrapDesignPatch(change, index, seq);
|
|
1074
|
+
case 'STRUCTURE':
|
|
1075
|
+
return parseSheetBootstrapStructurePatch(change, index, seq);
|
|
1076
|
+
|
|
1077
|
+
default:
|
|
1078
|
+
return parseSheetBootstrapCellPatch(change, params.scope, index, seq);
|
|
1079
|
+
}
|
|
1080
|
+
});
|
|
1081
|
+
|
|
1082
|
+
if (expectedSeq - 1n !== BigInt(params.currentSeq)) {
|
|
1083
|
+
failSheetBootstrap('INVALID_SEQUENCE', 'openSheet changes do not end at currentSeq.');
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
return patches;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
/*
|
|
1090
|
+
* Validate the topology rows (S-4) of one SNAPSHOT answer to a request with
|
|
1091
|
+
* `afterSeq`: STRUCTURE rows of the selected tab, in strictly increasing seq
|
|
1092
|
+
* order within (afterSeq, throughSeq], each read for its op. Null when the
|
|
1093
|
+
* request sent no applied seq or the answer carries no verdict. A set the
|
|
1094
|
+
* server marked incomplete, or one that fails validation, is returned empty
|
|
1095
|
+
* and incomplete: the client fails its pending work closed, as without it.
|
|
1096
|
+
*/
|
|
1097
|
+
export function validateSheetBootstrapTopology(params: {
|
|
1098
|
+
afterSeq: SheetJournalSeq | null;
|
|
1099
|
+
complete: unknown;
|
|
1100
|
+
sheetTabId: string;
|
|
1101
|
+
throughSeq: SheetJournalSeq;
|
|
1102
|
+
value: unknown;
|
|
1103
|
+
}): SheetBootstrapTopology | null {
|
|
1104
|
+
const { afterSeq } = params;
|
|
1105
|
+
if (!afterSeq || typeof params.complete !== 'boolean') {
|
|
1106
|
+
return null;
|
|
1107
|
+
}
|
|
1108
|
+
const incomplete: SheetBootstrapTopology = {
|
|
1109
|
+
afterSeq,
|
|
1110
|
+
complete: false,
|
|
1111
|
+
rows: [],
|
|
1112
|
+
};
|
|
1113
|
+
if (!params.complete || !Array.isArray(params.value)) {
|
|
1114
|
+
return incomplete;
|
|
1115
|
+
}
|
|
1116
|
+
try {
|
|
1117
|
+
let previousSeq = afterSeq;
|
|
1118
|
+
const rows = params.value.map((item, index) => {
|
|
1119
|
+
const change = requireSheetBootstrapRecord(item, `openSheet.topologyChanges[${index}]`, 'INVALID_CHANGE') as SheetTabChangeGQL;
|
|
1120
|
+
const seq = requireSheetBootstrapSeq(change.seq, `openSheet.topologyChanges[${index}].seq`, 'INVALID_CHANGE');
|
|
1121
|
+
if (
|
|
1122
|
+
change.kind !== 'STRUCTURE' ||
|
|
1123
|
+
requireSheetBootstrapId(change.sheetTabId, `openSheet.topologyChanges[${index}].sheetTabId`, 'INVALID_CHANGE') !==
|
|
1124
|
+
params.sheetTabId ||
|
|
1125
|
+
compareSheetJournalSeq(seq, previousSeq) <= 0 ||
|
|
1126
|
+
compareSheetJournalSeq(seq, params.throughSeq) > 0
|
|
1127
|
+
) {
|
|
1128
|
+
failSheetBootstrap(
|
|
1129
|
+
'INVALID_CHANGE',
|
|
1130
|
+
`openSheet.topologyChanges[${index}] is not an ordered STRUCTURE row of the image's gap.`,
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
previousSeq = seq;
|
|
1134
|
+
return parseSheetBootstrapStructurePatch(change, index, seq, true);
|
|
1135
|
+
});
|
|
1136
|
+
return { afterSeq, complete: true, rows };
|
|
1137
|
+
} catch (error) {
|
|
1138
|
+
if (error instanceof SheetBootstrapValidationError) {
|
|
1139
|
+
return incomplete;
|
|
1140
|
+
}
|
|
1141
|
+
throw error;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/*
|
|
1146
|
+
* Return the replay scope's grid: the full-tab viewport widened to the grid
|
|
1147
|
+
* caps, so a tail row written before a later row shrank the grid still
|
|
1148
|
+
* validates (the reducer, not the validator, owns geometry).
|
|
1149
|
+
*/
|
|
1150
|
+
function getSheetBootstrapReplayScope(scope: SheetBootstrapScope): SheetBootstrapScope {
|
|
1151
|
+
return {
|
|
1152
|
+
...scope,
|
|
1153
|
+
viewport: {
|
|
1154
|
+
columnCount: Math.max(scope.viewport.columnCount, SHEET_MAX_COLUMN_COUNT),
|
|
1155
|
+
rowCount: Math.max(scope.viewport.rowCount, SHEET_MAX_ROW_COUNT),
|
|
1156
|
+
startColumnIndex: 1,
|
|
1157
|
+
startRowIndex: 1,
|
|
1158
|
+
},
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
/*
|
|
1163
|
+
* Apply a validated SNAPSHOT response: decode and verify the snapshot image,
|
|
1164
|
+
* then validate the tail after its seq. The client's tab reducer installs the
|
|
1165
|
+
* image and replays the tail rows.
|
|
1166
|
+
*/
|
|
1167
|
+
async function applySnapshotSheetBootstrap(params: {
|
|
1168
|
+
bootstrap: SheetBootstrapGQL;
|
|
1169
|
+
currentSeq: SheetJournalSeq;
|
|
1170
|
+
scope: SheetBootstrapScope;
|
|
1171
|
+
selectedTab: SheetTabGQL;
|
|
1172
|
+
}): Promise<{ image: SheetBootstrapTabImage; rows: SheetBootstrapRowPatch[] }> {
|
|
1173
|
+
const snapshot = await decodeVerifiedSheetBootstrapSnapshot({
|
|
1174
|
+
snapshot: params.bootstrap.snapshot,
|
|
1175
|
+
currentSeq: params.currentSeq,
|
|
1176
|
+
scope: params.scope,
|
|
1177
|
+
});
|
|
1178
|
+
const rows = validateSheetBootstrapChanges({
|
|
1179
|
+
value: params.bootstrap.changes,
|
|
1180
|
+
afterSeq: snapshot.seq,
|
|
1181
|
+
currentSeq: params.currentSeq,
|
|
1182
|
+
scope: getSheetBootstrapReplayScope(params.scope),
|
|
1183
|
+
});
|
|
1184
|
+
const snapshotTab = snapshot.data.sheetTab;
|
|
1185
|
+
requireSheetBootstrapSeq(snapshotTab.designRevision, 'snapshot.sheetTab.designRevision');
|
|
1186
|
+
|
|
1187
|
+
return {
|
|
1188
|
+
image: {
|
|
1189
|
+
cells: snapshot.data.cells as SheetAuthoritativeCellGQL[],
|
|
1190
|
+
organizationId: params.scope.organizationId,
|
|
1191
|
+
pageInfo: snapshot.data.pageInfo ? { ...snapshot.data.pageInfo } : null,
|
|
1192
|
+
|
|
1193
|
+
seq: snapshot.seq,
|
|
1194
|
+
sheetId: params.scope.sheetId,
|
|
1195
|
+
sheetTab: {
|
|
1196
|
+
...params.selectedTab,
|
|
1197
|
+
...snapshotTab,
|
|
1198
|
+
cellsRevision: snapshot.seq,
|
|
1199
|
+
designRevision: String(snapshotTab.designRevision),
|
|
1200
|
+
} as SheetTabGQL,
|
|
1201
|
+
sheetTabId: params.scope.sheetTabId,
|
|
1202
|
+
},
|
|
1203
|
+
rows,
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
/*
|
|
1208
|
+
* Validate and atomically decode one access-aware openSheet / syncSheetTab
|
|
1209
|
+
* response (contract 4). A DELTA carries the rows after the caller's
|
|
1210
|
+
* `sinceSeq` (the client's live applied seq, DESIGN §0 D10) and no image; a
|
|
1211
|
+
* SNAPSHOT carries the decoded image and its tail. Nothing here merges into
|
|
1212
|
+
* client state: the tab reducer applies the image and rows.
|
|
1213
|
+
*/
|
|
1214
|
+
export async function applySheetBootstrap(params: {
|
|
1215
|
+
bootstrap: unknown;
|
|
1216
|
+
expectedSheetId: string;
|
|
1217
|
+
/* The applied seq the request was negotiated from (DELTA base). */
|
|
1218
|
+
sinceSeq?: SheetJournalSeq | null;
|
|
1219
|
+
}): Promise<SheetBootstrapApplyResult> {
|
|
1220
|
+
const expectedSheetId = requireSheetBootstrapId(params.expectedSheetId, 'expectedSheetId', 'INVALID_RESPONSE');
|
|
1221
|
+
const bootstrap = requireSheetBootstrapRecord(params.bootstrap, 'openSheet') as SheetBootstrapGQL;
|
|
1222
|
+
const contract = validateSheetBootstrapContract(bootstrap);
|
|
1223
|
+
const access = validateSheetBootstrapAccess(bootstrap.access, expectedSheetId);
|
|
1224
|
+
if (!access.canView) {
|
|
1225
|
+
if (bootstrap.status === 'DENIED') {
|
|
1226
|
+
if (bootstrap.unavailableReason || sheetBootstrapHasScopedReadData(bootstrap)) {
|
|
1227
|
+
failSheetBootstrap('INVALID_ACCESS', 'Denied openSheet response exposed scoped workbook data.');
|
|
1228
|
+
}
|
|
1229
|
+
return { ...contract, status: 'DENIED', access };
|
|
1230
|
+
}
|
|
1231
|
+
if (bootstrap.status !== 'RETRY' || typeof bootstrap.unavailableReason !== 'string' || !bootstrap.unavailableReason.trim()) {
|
|
1232
|
+
failSheetBootstrap('INVALID_STATUS', 'Denied openSheet responses require DENIED or an explicit RETRY status.');
|
|
1233
|
+
}
|
|
1234
|
+
if (sheetBootstrapHasScopedReadData(bootstrap)) {
|
|
1235
|
+
failSheetBootstrap('INVALID_ACCESS', 'Denied openSheet retry exposed scoped workbook data.');
|
|
1236
|
+
}
|
|
1237
|
+
return {
|
|
1238
|
+
...contract,
|
|
1239
|
+
status: 'RETRY',
|
|
1240
|
+
access,
|
|
1241
|
+
unavailableReason: bootstrap.unavailableReason,
|
|
1242
|
+
sheet: null,
|
|
1243
|
+
tabs: [],
|
|
1244
|
+
selectedTab: null,
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
if (bootstrap.status !== 'READY' && bootstrap.status !== 'RETRY' && bootstrap.status !== 'UNAVAILABLE') {
|
|
1249
|
+
return failSheetBootstrap('INVALID_STATUS', 'openSheet returned an unknown or missing status.');
|
|
1250
|
+
}
|
|
1251
|
+
if (bootstrap.status !== 'READY') {
|
|
1252
|
+
if (bootstrap.realtimeToken !== null && bootstrap.realtimeToken !== undefined) {
|
|
1253
|
+
failSheetBootstrap('INVALID_ACCESS', 'Non-ready openSheet responses cannot carry a realtime capability.');
|
|
1254
|
+
}
|
|
1255
|
+
if (typeof bootstrap.unavailableReason !== 'string' || !bootstrap.unavailableReason.trim()) {
|
|
1256
|
+
failSheetBootstrap('INVALID_STATUS', 'Non-ready openSheet responses require an unavailable reason.');
|
|
1257
|
+
}
|
|
1258
|
+
const sheet =
|
|
1259
|
+
bootstrap.sheet === null || bootstrap.sheet === undefined ? null : validateSheetBootstrapWorkbook(bootstrap.sheet, access);
|
|
1260
|
+
const tabs = bootstrap.tabs === null || bootstrap.tabs === undefined ? [] : validateSheetBootstrapTabSummaries(bootstrap.tabs);
|
|
1261
|
+
const selectedTab = bootstrap.selectedTab ? validateSelectedSheetBootstrapTab(bootstrap.selectedTab, access, tabs) : null;
|
|
1262
|
+
|
|
1263
|
+
return {
|
|
1264
|
+
...contract,
|
|
1265
|
+
status: bootstrap.status,
|
|
1266
|
+
access,
|
|
1267
|
+
unavailableReason: bootstrap.unavailableReason,
|
|
1268
|
+
sheet,
|
|
1269
|
+
tabs,
|
|
1270
|
+
selectedTab,
|
|
1271
|
+
};
|
|
1272
|
+
}
|
|
1273
|
+
// syncSheetTab responses carry no workbook, tab list or token: the caller
|
|
1274
|
+
// composes them with its cached manifest (lean transport).
|
|
1275
|
+
const realtimeToken =
|
|
1276
|
+
bootstrap.realtimeToken === null || bootstrap.realtimeToken === undefined
|
|
1277
|
+
? ''
|
|
1278
|
+
: requireSheetBootstrapRealtimeToken(bootstrap.realtimeToken, 'openSheet.realtimeToken');
|
|
1279
|
+
const sheet =
|
|
1280
|
+
bootstrap.sheet === null || bootstrap.sheet === undefined ? null : validateSheetBootstrapWorkbook(bootstrap.sheet, access);
|
|
1281
|
+
const tabs = bootstrap.tabs === null || bootstrap.tabs === undefined ? null : validateSheetBootstrapTabSummaries(bootstrap.tabs);
|
|
1282
|
+
const selectedTab = bootstrap.selectedTab
|
|
1283
|
+
? validateSelectedSheetBootstrapTab(
|
|
1284
|
+
bootstrap.selectedTab,
|
|
1285
|
+
access,
|
|
1286
|
+
tabs || [{ id: String(bootstrap.selectedTab.id || '') } as SheetTabSummaryGQL],
|
|
1287
|
+
)
|
|
1288
|
+
: null;
|
|
1289
|
+
if (bootstrap.unavailableReason) {
|
|
1290
|
+
failSheetBootstrap('INVALID_STATUS', 'READY openSheet responses cannot have an unavailable reason.');
|
|
1291
|
+
}
|
|
1292
|
+
if (bootstrap.mode !== 'SNAPSHOT' && bootstrap.mode !== 'DELTA') {
|
|
1293
|
+
return failSheetBootstrap('INVALID_STATUS', 'READY openSheet responses require a known mode.');
|
|
1294
|
+
}
|
|
1295
|
+
const currentSeq = requireSheetBootstrapSeq(bootstrap.currentSeq, 'openSheet.currentSeq');
|
|
1296
|
+
|
|
1297
|
+
if (!selectedTab) {
|
|
1298
|
+
if (
|
|
1299
|
+
!sheet ||
|
|
1300
|
+
(tabs && tabs.length) ||
|
|
1301
|
+
bootstrap.mode !== 'SNAPSHOT' ||
|
|
1302
|
+
currentSeq !== SHEET_JOURNAL_ZERO_SEQ ||
|
|
1303
|
+
bootstrap.snapshot ||
|
|
1304
|
+
!Array.isArray(bootstrap.changes) ||
|
|
1305
|
+
bootstrap.changes.length
|
|
1306
|
+
) {
|
|
1307
|
+
failSheetBootstrap('INVALID_RESPONSE', 'Empty-workbook openSheet response is internally inconsistent.');
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
return {
|
|
1311
|
+
...contract,
|
|
1312
|
+
status: 'READY',
|
|
1313
|
+
access,
|
|
1314
|
+
realtimeToken,
|
|
1315
|
+
sheet: sheet as SheetGQL,
|
|
1316
|
+
tabs: tabs || [],
|
|
1317
|
+
selectedTab: null,
|
|
1318
|
+
mode: 'SNAPSHOT',
|
|
1319
|
+
currentSeq,
|
|
1320
|
+
servedViewport: normalizeSheetViewport({}),
|
|
1321
|
+
|
|
1322
|
+
image: null,
|
|
1323
|
+
rows: [],
|
|
1324
|
+
rowsAfterSeq: currentSeq,
|
|
1325
|
+
topology: null,
|
|
1326
|
+
};
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
const sheetTabId = requireSheetBootstrapId(selectedTab.id, 'openSheet.selectedTab.id');
|
|
1330
|
+
const selectedTabSeq = requireSheetBootstrapSeq(selectedTab.cellsRevision, 'openSheet.selectedTab.cellsRevision');
|
|
1331
|
+
const selectedSummary = tabs?.find((tab) => tab.id === sheetTabId);
|
|
1332
|
+
if (
|
|
1333
|
+
selectedTabSeq !== currentSeq ||
|
|
1334
|
+
(tabs && requireSheetBootstrapSeq(selectedSummary?.currentSeq, 'openSheet selected tab summary currentSeq') !== currentSeq)
|
|
1335
|
+
) {
|
|
1336
|
+
failSheetBootstrap('INVALID_SEQUENCE', 'openSheet selected-tab cursors do not agree.');
|
|
1337
|
+
}
|
|
1338
|
+
const servedViewport =
|
|
1339
|
+
bootstrap.servedViewport === null || bootstrap.servedViewport === undefined
|
|
1340
|
+
? getExpectedSheetBootstrapViewport(selectedTab)
|
|
1341
|
+
: requireSheetBootstrapViewport(bootstrap.servedViewport, 'openSheet.servedViewport');
|
|
1342
|
+
const scope: SheetBootstrapScope = {
|
|
1343
|
+
organizationId: access.organizationId as string,
|
|
1344
|
+
sheetId: access.sheetId,
|
|
1345
|
+
sheetTabId,
|
|
1346
|
+
viewport: servedViewport,
|
|
1347
|
+
};
|
|
1348
|
+
const ready = {
|
|
1349
|
+
...contract,
|
|
1350
|
+
status: 'READY' as const,
|
|
1351
|
+
access,
|
|
1352
|
+
realtimeToken,
|
|
1353
|
+
sheet: sheet as SheetGQL,
|
|
1354
|
+
tabs: tabs || [],
|
|
1355
|
+
selectedTab,
|
|
1356
|
+
currentSeq,
|
|
1357
|
+
servedViewport,
|
|
1358
|
+
};
|
|
1359
|
+
if (bootstrap.mode === 'SNAPSHOT') {
|
|
1360
|
+
const snapshot = await applySnapshotSheetBootstrap({
|
|
1361
|
+
bootstrap,
|
|
1362
|
+
currentSeq,
|
|
1363
|
+
scope,
|
|
1364
|
+
selectedTab,
|
|
1365
|
+
});
|
|
1366
|
+
return {
|
|
1367
|
+
...ready,
|
|
1368
|
+
mode: 'SNAPSHOT',
|
|
1369
|
+
image: snapshot.image,
|
|
1370
|
+
rows: snapshot.rows,
|
|
1371
|
+
rowsAfterSeq: snapshot.image.seq,
|
|
1372
|
+
topology: validateSheetBootstrapTopology({
|
|
1373
|
+
afterSeq: parseSheetJournalSeq(params.sinceSeq),
|
|
1374
|
+
complete: bootstrap.topologyComplete,
|
|
1375
|
+
sheetTabId,
|
|
1376
|
+
throughSeq: snapshot.image.seq,
|
|
1377
|
+
value: bootstrap.topologyChanges,
|
|
1378
|
+
}),
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
if (bootstrap.snapshot !== null && bootstrap.snapshot !== undefined) {
|
|
1382
|
+
failSheetBootstrap('INVALID_RESPONSE', 'openSheet DELTA must not include a replacement snapshot.');
|
|
1383
|
+
}
|
|
1384
|
+
const sinceSeq = parseSheetJournalSeq(params.sinceSeq);
|
|
1385
|
+
if (!sinceSeq) {
|
|
1386
|
+
return failSheetBootstrap('INVALID_RESPONSE', 'openSheet DELTA requires the applied seq the request was negotiated from.');
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
return {
|
|
1390
|
+
...ready,
|
|
1391
|
+
mode: 'DELTA',
|
|
1392
|
+
image: null,
|
|
1393
|
+
rows: validateSheetBootstrapChanges({
|
|
1394
|
+
value: bootstrap.changes,
|
|
1395
|
+
afterSeq: sinceSeq,
|
|
1396
|
+
currentSeq,
|
|
1397
|
+
scope: getSheetBootstrapReplayScope(scope),
|
|
1398
|
+
}),
|
|
1399
|
+
rowsAfterSeq: sinceSeq,
|
|
1400
|
+
topology: null,
|
|
1401
|
+
};
|
|
1402
|
+
}
|