@notionhq/custom-blocks-host 0.2.10 → 0.2.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/dist/browser.js +1 -1
- package/dist/createCustomBlockHost.d.ts.map +1 -1
- package/dist/createCustomBlockHost.js +11 -11
- package/dist/lifecycle/initErrors.d.ts +21 -10
- package/dist/lifecycle/initErrors.d.ts.map +1 -1
- package/dist/lifecycle/initErrors.js +104 -155
- package/dist/lifecycle/initializationController.d.ts.map +1 -1
- package/dist/lifecycle/initializationController.js +11 -9
- package/dist/messages/sandboxMessageRouter.d.ts.map +1 -1
- package/dist/messages/sandboxMessageRouter.js +3 -3
- package/package.json +1 -1
- package/src/createCustomBlockHost.ts +18 -13
- package/src/lifecycle/initErrors.ts +120 -166
- package/src/lifecycle/initializationController.ts +21 -15
- package/src/messages/sandboxMessageRouter.ts +6 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initializationController.d.ts","sourceRoot":"","sources":["../../src/lifecycle/initializationController.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EACX,wBAAwB,EACxB,iBAAiB,EACjB,MAAM,mDAAmD,CAAA;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AACzC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"initializationController.d.ts","sourceRoot":"","sources":["../../src/lifecycle/initializationController.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EACX,wBAAwB,EACxB,iBAAiB,EACjB,MAAM,mDAAmD,CAAA;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAA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AAA;QACpC,KAAK,EAAE,wBAAwB,CAAA;QAC/B,qBAAqB,EAAE,MAAM,GAAG,SAAS,CAAA;KACzC,KAAK,SAAS,GAAG,MAAM,CAAA;IACxB,uBAAuB,EAAE,CAAC,IAAI,EAAE;QAC/B,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAA;QACpC,KAAK,EAAE,wBAAwB,CAAA;KAC/B,KAAK,IAAI,CAAA;IACV,YAAY,EAAE,MAAM,IAAI,CAAA;IACxB,IAAI,EAAE,MAAM,IAAI,CAAA;CAChB,CAAA;AAED,wBAAgB,8BAA8B,CAC7C,OAAO,EAAE,+BAA+B,GACtC,wBAAwB,CAgU1B"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DEFAULT_CONTRAST_MODE } from "../protocol/contrast.js";
|
|
2
2
|
import { unreachable } from "../utils.js";
|
|
3
|
-
import {
|
|
3
|
+
import { initialStateTimeoutError, invalidProtocolVersionError, noConnectError, noInitResultError, unknownInitializationError, unsupportedProtocolVersionError, } from "./initErrors.js";
|
|
4
4
|
import { isValidBridgeProtocolVersion } from "./protocolVersion.js";
|
|
5
5
|
const DEFAULT_NO_CONNECT_TIMEOUT_MS = 5_000;
|
|
6
6
|
const DEFAULT_INITIAL_STATE_TIMEOUT_MS = 5_000;
|
|
@@ -69,7 +69,7 @@ export function createInitializationController(options) {
|
|
|
69
69
|
if (initStatus !== "preparingInit") {
|
|
70
70
|
return;
|
|
71
71
|
}
|
|
72
|
-
const error =
|
|
72
|
+
const error = initialStateTimeoutError();
|
|
73
73
|
warn({
|
|
74
74
|
...error,
|
|
75
75
|
initializationId: connect.initializationId,
|
|
@@ -109,7 +109,7 @@ export function createInitializationController(options) {
|
|
|
109
109
|
return;
|
|
110
110
|
}
|
|
111
111
|
initStatus = "error";
|
|
112
|
-
const error =
|
|
112
|
+
const error = noInitResultError();
|
|
113
113
|
onConnectionFailure?.(error);
|
|
114
114
|
warn({
|
|
115
115
|
...error,
|
|
@@ -127,7 +127,7 @@ export function createInitializationController(options) {
|
|
|
127
127
|
});
|
|
128
128
|
sendInitError({
|
|
129
129
|
initializationId: connect.initializationId,
|
|
130
|
-
error:
|
|
130
|
+
error: unknownInitializationError(),
|
|
131
131
|
});
|
|
132
132
|
}
|
|
133
133
|
}
|
|
@@ -140,7 +140,7 @@ export function createInitializationController(options) {
|
|
|
140
140
|
}
|
|
141
141
|
// Verify the bridge protocol version is valid.
|
|
142
142
|
if (!isValidBridgeProtocolVersion(message.bridgeProtocolVersion)) {
|
|
143
|
-
const initError =
|
|
143
|
+
const initError = invalidProtocolVersionError({
|
|
144
144
|
currentBridgeProtocolVersion: message.bridgeProtocolVersion,
|
|
145
145
|
});
|
|
146
146
|
sendInitError({
|
|
@@ -150,7 +150,7 @@ export function createInitializationController(options) {
|
|
|
150
150
|
return;
|
|
151
151
|
}
|
|
152
152
|
if (message.bridgeProtocolVersion < minBridgeProtocolVersion) {
|
|
153
|
-
const initError =
|
|
153
|
+
const initError = unsupportedProtocolVersionError({
|
|
154
154
|
currentBridgeProtocolVersion: message.bridgeProtocolVersion,
|
|
155
155
|
minBridgeProtocolVersion,
|
|
156
156
|
});
|
|
@@ -187,10 +187,12 @@ export function createInitializationController(options) {
|
|
|
187
187
|
// when the malformed message includes a numeric version.
|
|
188
188
|
if (bridgeProtocolVersion !== undefined) {
|
|
189
189
|
if (!isValidBridgeProtocolVersion(bridgeProtocolVersion)) {
|
|
190
|
-
error =
|
|
190
|
+
error = invalidProtocolVersionError({
|
|
191
|
+
currentBridgeProtocolVersion: undefined,
|
|
192
|
+
});
|
|
191
193
|
}
|
|
192
194
|
else if (bridgeProtocolVersion < minBridgeProtocolVersion) {
|
|
193
|
-
error =
|
|
195
|
+
error = unsupportedProtocolVersionError({
|
|
194
196
|
currentBridgeProtocolVersion: bridgeProtocolVersion,
|
|
195
197
|
minBridgeProtocolVersion,
|
|
196
198
|
});
|
|
@@ -236,7 +238,7 @@ export function createInitializationController(options) {
|
|
|
236
238
|
return;
|
|
237
239
|
}
|
|
238
240
|
initStatus = "error";
|
|
239
|
-
const error =
|
|
241
|
+
const error = noConnectError();
|
|
240
242
|
warn(error);
|
|
241
243
|
onConnectionFailure?.(error);
|
|
242
244
|
}, noConnectTimeoutMs);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandboxMessageRouter.d.ts","sourceRoot":"","sources":["../../src/messages/sandboxMessageRouter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oEAAoE,CAAA;AAE/G,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wDAAwD,CAAA;AAC9F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uDAAuD,CAAA;AAC5F,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,8DAA8D,CAAA;AAC1G,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qDAAqD,CAAA;AAExF,OAAO,KAAK,EAAE,iCAAiC,EAAE,MAAM,yEAAyE,CAAA;AAChI,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;
|
|
1
|
+
{"version":3,"file":"sandboxMessageRouter.d.ts","sourceRoot":"","sources":["../../src/messages/sandboxMessageRouter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,cAAc,EAAE,MA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AI,CAAA;IAClD,iBAAiB,EAAE,CAAC,OAAO,EAAE,sBAAsB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5E,4BAA4B,EAAE,CAC7B,OAAO,EAAE,iCAAiC,KACtC,IAAI,CAAA;IACT,YAAY,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAClE,UAAU,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9D,SAAS,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5D,SAAS,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5D,WAAW,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAChE,YAAY,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAClE,QAAQ,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAA;CAC1C,CAAA;AAED,wBAAgB,0BAA0B,CACzC,OAAO,EAAE,2BAA2B,GAClC,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAsI/B"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isInitHandshakeMessage } from "../protocol/messages/isInitHandshakeMessage.js";
|
|
2
2
|
import { sandboxToHostMessageSchema } from "../protocol/messages/sandboxToHost.js";
|
|
3
3
|
import * as v from "valibot";
|
|
4
|
-
import {
|
|
4
|
+
import { invalidConnectPayloadError, invalidInitResultPayloadError, } from "../lifecycle/initErrors.js";
|
|
5
5
|
import { unreachable } from "../utils.js";
|
|
6
6
|
import { getInvalidSandboxMessageResponse } from "./invalidSandboxMessage.js";
|
|
7
7
|
const initializationIdentitySchema = v.object({
|
|
@@ -28,7 +28,7 @@ export function createSandboxMessageRouter(options) {
|
|
|
28
28
|
initializationId: parsedIdentity.success
|
|
29
29
|
? parsedIdentity.output.initializationId
|
|
30
30
|
: undefined,
|
|
31
|
-
error:
|
|
31
|
+
error: invalidConnectPayloadError(),
|
|
32
32
|
bridgeProtocolVersion: parsedVersion.success
|
|
33
33
|
? parsedVersion.output.bridgeProtocolVersion
|
|
34
34
|
: undefined,
|
|
@@ -44,7 +44,7 @@ export function createSandboxMessageRouter(options) {
|
|
|
44
44
|
initializationId: parsedIdentity.success
|
|
45
45
|
? parsedIdentity.output.initializationId
|
|
46
46
|
: undefined,
|
|
47
|
-
error:
|
|
47
|
+
error: invalidInitResultPayloadError(),
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
50
|
// Other messages get NACKed indicating they were invalid and skipped.
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
hostToSandboxMessageSchema,
|
|
11
11
|
} from "@notionhq/custom-blocks-protocol/messages/hostToSandbox.js"
|
|
12
12
|
import {
|
|
13
|
-
type
|
|
13
|
+
type CustomBlockInitErrorInfo,
|
|
14
14
|
initMessageSchema,
|
|
15
15
|
} from "@notionhq/custom-blocks-protocol/messages/init.js"
|
|
16
16
|
import type { InvalidSandboxMessage } from "@notionhq/custom-blocks-protocol/messages/invalidSandboxMessage.js"
|
|
@@ -27,7 +27,12 @@ import type { notionThemeSchema } from "@notionhq/custom-blocks-protocol/theme.j
|
|
|
27
27
|
import type { notionUserSchema } from "@notionhq/custom-blocks-protocol/users/user.js"
|
|
28
28
|
import type { InferInput } from "valibot"
|
|
29
29
|
import * as v from "valibot"
|
|
30
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
contextUnavailableError,
|
|
32
|
+
invalidPropertyBindingError,
|
|
33
|
+
missingDataSourceBindingError,
|
|
34
|
+
missingPropertyBindingError,
|
|
35
|
+
} from "./lifecycle/initErrors.js"
|
|
31
36
|
import { createInitializationController } from "./lifecycle/initializationController.js"
|
|
32
37
|
import { normalizeHostToSandboxMessage } from "./lifecycle/normalizeHostToSandboxMessage.js"
|
|
33
38
|
import type {
|
|
@@ -387,17 +392,17 @@ export function createCustomBlockHost(
|
|
|
387
392
|
) {
|
|
388
393
|
return {
|
|
389
394
|
status: "error",
|
|
390
|
-
error:
|
|
395
|
+
error: contextUnavailableError(),
|
|
391
396
|
}
|
|
392
397
|
}
|
|
393
|
-
const
|
|
398
|
+
const bindingError = getBindingError(
|
|
394
399
|
initialState.manifest,
|
|
395
400
|
initialState.dataSources,
|
|
396
401
|
)
|
|
397
|
-
if (
|
|
402
|
+
if (bindingError !== undefined) {
|
|
398
403
|
return {
|
|
399
404
|
status: "error",
|
|
400
|
-
error:
|
|
405
|
+
error: bindingError,
|
|
401
406
|
}
|
|
402
407
|
}
|
|
403
408
|
activeDataSources = initialState.dataSources
|
|
@@ -464,7 +469,7 @@ export function createCustomBlockHost(
|
|
|
464
469
|
initializationController.status === "waitingForInitResult" ||
|
|
465
470
|
initializationController.status === "initialized")
|
|
466
471
|
) {
|
|
467
|
-
stopWithFailure(
|
|
472
|
+
stopWithFailure(contextUnavailableError())
|
|
468
473
|
return
|
|
469
474
|
}
|
|
470
475
|
updateHostState({ page })
|
|
@@ -483,7 +488,7 @@ export function createCustomBlockHost(
|
|
|
483
488
|
if (
|
|
484
489
|
initializationController.hasConnectedSandbox &&
|
|
485
490
|
manifest !== undefined &&
|
|
486
|
-
|
|
491
|
+
getBindingError(manifest, normalizedDataSources) !== undefined
|
|
487
492
|
) {
|
|
488
493
|
emit("warn", {
|
|
489
494
|
message: "Ignored invalid data source bindings update",
|
|
@@ -523,10 +528,10 @@ function getCollectionSchema(
|
|
|
523
528
|
)?.collectionSchema
|
|
524
529
|
}
|
|
525
530
|
|
|
526
|
-
function
|
|
531
|
+
function getBindingError(
|
|
527
532
|
manifest: InferInput<typeof manifestSchema>,
|
|
528
533
|
dataSources: CustomBlockHostDataSourcesPayload,
|
|
529
|
-
):
|
|
534
|
+
): CustomBlockInitErrorInfo | undefined {
|
|
530
535
|
for (const [dataSourceKey, manifestDataSource] of Object.entries(
|
|
531
536
|
manifest.dataSources,
|
|
532
537
|
)) {
|
|
@@ -535,20 +540,20 @@ function getBindingErrorCode(
|
|
|
535
540
|
binding?.collectionPointer === undefined ||
|
|
536
541
|
binding.collectionSchema === undefined
|
|
537
542
|
) {
|
|
538
|
-
return
|
|
543
|
+
return missingDataSourceBindingError()
|
|
539
544
|
}
|
|
540
545
|
for (const [propertyKey, manifestProperty] of Object.entries(
|
|
541
546
|
manifestDataSource.properties ?? {},
|
|
542
547
|
)) {
|
|
543
548
|
const propertyId = binding.propertyIdsByKey?.[propertyKey]
|
|
544
549
|
if (propertyId === undefined) {
|
|
545
|
-
return
|
|
550
|
+
return missingPropertyBindingError()
|
|
546
551
|
}
|
|
547
552
|
if (
|
|
548
553
|
binding.collectionSchema.propertiesById[propertyId]?.type !==
|
|
549
554
|
manifestProperty.type
|
|
550
555
|
) {
|
|
551
|
-
return
|
|
556
|
+
return invalidPropertyBindingError()
|
|
552
557
|
}
|
|
553
558
|
}
|
|
554
559
|
}
|
|
@@ -1,171 +1,125 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CustomBlockInitErrorCode,
|
|
3
|
-
CustomBlockInitErrorInfo,
|
|
4
|
-
} from "@notionhq/custom-blocks-protocol/messages/init.js"
|
|
5
|
-
|
|
6
|
-
const UNKNOWN_ERROR_MESSAGE =
|
|
7
|
-
"Unexpected error while initializing custom block."
|
|
1
|
+
import type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/errors.js"
|
|
8
2
|
|
|
9
3
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* `connect` message. `isRetryable` means a fresh iframe / `init` attempt could plausibly succeed
|
|
14
|
-
* without changing block code or host configuration.
|
|
4
|
+
* Initialization handshakes are one-shot: after an error, this host instance will not answer
|
|
5
|
+
* another `connect` message. `isRetryable` means a fresh host and iframe could plausibly
|
|
6
|
+
* initialize without changing block code or host configuration.
|
|
15
7
|
*/
|
|
16
|
-
export function
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
code: reason,
|
|
133
|
-
message: "Sandbox did not acknowledge initialization.",
|
|
134
|
-
isRetryable: true,
|
|
135
|
-
}
|
|
136
|
-
case "invalid_init_result_payload":
|
|
137
|
-
return {
|
|
138
|
-
code: reason,
|
|
139
|
-
message: "Sandbox sent an invalid initialization result.",
|
|
140
|
-
isRetryable: false,
|
|
141
|
-
}
|
|
142
|
-
case "invalid_init_bindings":
|
|
143
|
-
return {
|
|
144
|
-
code: reason,
|
|
145
|
-
message: "Sandbox rejected the host-provided data source bindings.",
|
|
146
|
-
isRetryable: false,
|
|
147
|
-
}
|
|
148
|
-
case "not_in_iframe":
|
|
149
|
-
return {
|
|
150
|
-
code: reason,
|
|
151
|
-
message: "<NotionCustomBlock> only works inside an iframe.",
|
|
152
|
-
// The block must be embedded in an iframe.
|
|
153
|
-
isRetryable: false,
|
|
154
|
-
}
|
|
155
|
-
case "unknown_error":
|
|
156
|
-
return {
|
|
157
|
-
code: reason,
|
|
158
|
-
message: UNKNOWN_ERROR_MESSAGE,
|
|
159
|
-
// Treat unknown errors as non-retryable.
|
|
160
|
-
isRetryable: false,
|
|
161
|
-
}
|
|
162
|
-
default:
|
|
163
|
-
console.warn(`Unexpected custom block init error: ${reason}`)
|
|
164
|
-
return {
|
|
165
|
-
code: reason,
|
|
166
|
-
message: UNKNOWN_ERROR_MESSAGE,
|
|
167
|
-
// Treat unknown error codes as non-retryable.
|
|
168
|
-
isRetryable: false,
|
|
169
|
-
}
|
|
8
|
+
export function noConnectError(): CustomBlockErrorInfo<"no_connect"> {
|
|
9
|
+
return {
|
|
10
|
+
code: "no_connect",
|
|
11
|
+
message: "Sandbox did not send a connect message.",
|
|
12
|
+
// A fresh iframe may finish loading and send connect before the timeout.
|
|
13
|
+
isRetryable: true,
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function invalidConnectPayloadError(): CustomBlockErrorInfo<"invalid_connect_payload"> {
|
|
18
|
+
return {
|
|
19
|
+
code: "invalid_connect_payload",
|
|
20
|
+
message: "Sandbox sent an invalid connect message.",
|
|
21
|
+
// The sandbox must be updated to send a valid connect message.
|
|
22
|
+
isRetryable: false,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function invalidProtocolVersionError(args: {
|
|
27
|
+
currentBridgeProtocolVersion: number | undefined
|
|
28
|
+
}): CustomBlockErrorInfo<"invalid_protocol_version"> {
|
|
29
|
+
const { currentBridgeProtocolVersion } = args
|
|
30
|
+
const versionText = currentBridgeProtocolVersion
|
|
31
|
+
? `: ${currentBridgeProtocolVersion}`
|
|
32
|
+
: "."
|
|
33
|
+
return {
|
|
34
|
+
code: "invalid_protocol_version",
|
|
35
|
+
message: `Sandbox reported an invalid bridge protocol version ${versionText}`,
|
|
36
|
+
// The sandbox must be updated to a supported protocol version.
|
|
37
|
+
isRetryable: false,
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function unsupportedProtocolVersionError(args: {
|
|
42
|
+
currentBridgeProtocolVersion: number
|
|
43
|
+
minBridgeProtocolVersion: number
|
|
44
|
+
}): CustomBlockErrorInfo<"unsupported_protocol_version"> {
|
|
45
|
+
const { currentBridgeProtocolVersion, minBridgeProtocolVersion } = args
|
|
46
|
+
return {
|
|
47
|
+
code: "unsupported_protocol_version",
|
|
48
|
+
message: `Sandbox bridge protocol version ${currentBridgeProtocolVersion} is not supported. Minimum supported version is ${minBridgeProtocolVersion}.`,
|
|
49
|
+
// The sandbox must be updated to a supported protocol version.
|
|
50
|
+
isRetryable: false,
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function contextUnavailableError(): CustomBlockErrorInfo<"context_unavailable"> {
|
|
55
|
+
return {
|
|
56
|
+
code: "context_unavailable",
|
|
57
|
+
message: "Host page context is unavailable.",
|
|
58
|
+
// Page context may become available before a fresh initialization attempt.
|
|
59
|
+
isRetryable: true,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function initialStateTimeoutError(): CustomBlockErrorInfo<"initial_state_timeout"> {
|
|
64
|
+
return {
|
|
65
|
+
code: "initial_state_timeout",
|
|
66
|
+
message: "Host did not prepare initial state before the timeout.",
|
|
67
|
+
// A fresh attempt may finish loading host dependencies before the timeout.
|
|
68
|
+
isRetryable: true,
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function missingDataSourceBindingError(): CustomBlockErrorInfo<"missing_data_source_binding"> {
|
|
73
|
+
return {
|
|
74
|
+
code: "missing_data_source_binding",
|
|
75
|
+
message: "A required data source is not mapped.",
|
|
76
|
+
// Host configuration must be changed before this block can initialize.
|
|
77
|
+
isRetryable: false,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function missingPropertyBindingError(): CustomBlockErrorInfo<"missing_property_binding"> {
|
|
82
|
+
return {
|
|
83
|
+
code: "missing_property_binding",
|
|
84
|
+
message: "A required data source property is not mapped.",
|
|
85
|
+
// Host configuration must be changed before this block can initialize.
|
|
86
|
+
isRetryable: false,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function invalidPropertyBindingError(): CustomBlockErrorInfo<"invalid_property_binding"> {
|
|
91
|
+
return {
|
|
92
|
+
code: "invalid_property_binding",
|
|
93
|
+
message: "A mapped data source property is missing or incompatible.",
|
|
94
|
+
// The bound property's schema must be updated or the binding must be updated to
|
|
95
|
+
// a property with a compatible schema.
|
|
96
|
+
isRetryable: false,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function noInitResultError(): CustomBlockErrorInfo<"no_init_result"> {
|
|
101
|
+
return {
|
|
102
|
+
code: "no_init_result",
|
|
103
|
+
message: "Sandbox did not acknowledge initialization.",
|
|
104
|
+
// A fresh iframe may process init and acknowledge it before the timeout.
|
|
105
|
+
isRetryable: true,
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function invalidInitResultPayloadError(): CustomBlockErrorInfo<"invalid_init_result_payload"> {
|
|
110
|
+
return {
|
|
111
|
+
code: "invalid_init_result_payload",
|
|
112
|
+
message: "Sandbox sent an invalid initialization result.",
|
|
113
|
+
// The sandbox must be updated to send a valid initialization result.
|
|
114
|
+
isRetryable: false,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function unknownInitializationError(): CustomBlockErrorInfo<"unknown_error"> {
|
|
119
|
+
return {
|
|
120
|
+
code: "unknown_error",
|
|
121
|
+
message: "Unexpected error while initializing custom block.",
|
|
122
|
+
// Without a known transient cause, conservatively treat unknown errors as non-retryable.
|
|
123
|
+
isRetryable: false,
|
|
170
124
|
}
|
|
171
125
|
}
|
|
@@ -8,7 +8,14 @@ import type { InitResultMessage } from "@notionhq/custom-blocks-protocol/message
|
|
|
8
8
|
import type { InferInput } from "valibot"
|
|
9
9
|
import type { CustomBlockHostFailure } from "../messages/types.js"
|
|
10
10
|
import { unreachable } from "../utils.js"
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
initialStateTimeoutError,
|
|
13
|
+
invalidProtocolVersionError,
|
|
14
|
+
noConnectError,
|
|
15
|
+
noInitResultError,
|
|
16
|
+
unknownInitializationError,
|
|
17
|
+
unsupportedProtocolVersionError,
|
|
18
|
+
} from "./initErrors.js"
|
|
12
19
|
import { isValidBridgeProtocolVersion } from "./protocolVersion.js"
|
|
13
20
|
import type {
|
|
14
21
|
ConnectSuccessMessage,
|
|
@@ -141,7 +148,7 @@ export function createInitializationController(
|
|
|
141
148
|
if (initStatus !== "preparingInit") {
|
|
142
149
|
return
|
|
143
150
|
}
|
|
144
|
-
const error =
|
|
151
|
+
const error = initialStateTimeoutError()
|
|
145
152
|
warn({
|
|
146
153
|
...error,
|
|
147
154
|
initializationId: connect.initializationId,
|
|
@@ -183,7 +190,7 @@ export function createInitializationController(
|
|
|
183
190
|
return
|
|
184
191
|
}
|
|
185
192
|
initStatus = "error"
|
|
186
|
-
const error =
|
|
193
|
+
const error = noInitResultError()
|
|
187
194
|
onConnectionFailure?.(error)
|
|
188
195
|
warn({
|
|
189
196
|
...error,
|
|
@@ -200,7 +207,7 @@ export function createInitializationController(
|
|
|
200
207
|
})
|
|
201
208
|
sendInitError({
|
|
202
209
|
initializationId: connect.initializationId,
|
|
203
|
-
error:
|
|
210
|
+
error: unknownInitializationError(),
|
|
204
211
|
})
|
|
205
212
|
}
|
|
206
213
|
}
|
|
@@ -215,7 +222,7 @@ export function createInitializationController(
|
|
|
215
222
|
|
|
216
223
|
// Verify the bridge protocol version is valid.
|
|
217
224
|
if (!isValidBridgeProtocolVersion(message.bridgeProtocolVersion)) {
|
|
218
|
-
const initError =
|
|
225
|
+
const initError = invalidProtocolVersionError({
|
|
219
226
|
currentBridgeProtocolVersion: message.bridgeProtocolVersion,
|
|
220
227
|
})
|
|
221
228
|
sendInitError({
|
|
@@ -225,13 +232,10 @@ export function createInitializationController(
|
|
|
225
232
|
return
|
|
226
233
|
}
|
|
227
234
|
if (message.bridgeProtocolVersion < minBridgeProtocolVersion) {
|
|
228
|
-
const initError =
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
minBridgeProtocolVersion,
|
|
233
|
-
},
|
|
234
|
-
)
|
|
235
|
+
const initError = unsupportedProtocolVersionError({
|
|
236
|
+
currentBridgeProtocolVersion: message.bridgeProtocolVersion,
|
|
237
|
+
minBridgeProtocolVersion,
|
|
238
|
+
})
|
|
235
239
|
sendInitError({
|
|
236
240
|
initializationId: message.initializationId,
|
|
237
241
|
error: initError,
|
|
@@ -273,9 +277,11 @@ export function createInitializationController(
|
|
|
273
277
|
// when the malformed message includes a numeric version.
|
|
274
278
|
if (bridgeProtocolVersion !== undefined) {
|
|
275
279
|
if (!isValidBridgeProtocolVersion(bridgeProtocolVersion)) {
|
|
276
|
-
error =
|
|
280
|
+
error = invalidProtocolVersionError({
|
|
281
|
+
currentBridgeProtocolVersion: undefined,
|
|
282
|
+
})
|
|
277
283
|
} else if (bridgeProtocolVersion < minBridgeProtocolVersion) {
|
|
278
|
-
error =
|
|
284
|
+
error = unsupportedProtocolVersionError({
|
|
279
285
|
currentBridgeProtocolVersion: bridgeProtocolVersion,
|
|
280
286
|
minBridgeProtocolVersion,
|
|
281
287
|
})
|
|
@@ -329,7 +335,7 @@ export function createInitializationController(
|
|
|
329
335
|
return
|
|
330
336
|
}
|
|
331
337
|
initStatus = "error"
|
|
332
|
-
const error =
|
|
338
|
+
const error = noConnectError()
|
|
333
339
|
warn(error)
|
|
334
340
|
onConnectionFailure?.(error)
|
|
335
341
|
}, noConnectTimeoutMs)
|
|
@@ -13,7 +13,10 @@ import { sandboxToHostMessageSchema } from "@notionhq/custom-blocks-protocol/mes
|
|
|
13
13
|
import type { UnsubscribeDataSourceQueryMessage } from "@notionhq/custom-blocks-protocol/messages/unsubscribeDataSourceQuery.js"
|
|
14
14
|
import type { UpdatePageMessage } from "@notionhq/custom-blocks-protocol/messages/updatePage.js"
|
|
15
15
|
import * as v from "valibot"
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
invalidConnectPayloadError,
|
|
18
|
+
invalidInitResultPayloadError,
|
|
19
|
+
} from "../lifecycle/initErrors.js"
|
|
17
20
|
import type { InitializationController } from "../lifecycle/initializationController.js"
|
|
18
21
|
import type { InitStatus } from "../lifecycle/types.js"
|
|
19
22
|
import { unreachable } from "../utils.js"
|
|
@@ -95,7 +98,7 @@ export function createSandboxMessageRouter(
|
|
|
95
98
|
initializationId: parsedIdentity.success
|
|
96
99
|
? parsedIdentity.output.initializationId
|
|
97
100
|
: undefined,
|
|
98
|
-
error:
|
|
101
|
+
error: invalidConnectPayloadError(),
|
|
99
102
|
bridgeProtocolVersion: parsedVersion.success
|
|
100
103
|
? parsedVersion.output.bridgeProtocolVersion
|
|
101
104
|
: undefined,
|
|
@@ -112,7 +115,7 @@ export function createSandboxMessageRouter(
|
|
|
112
115
|
initializationId: parsedIdentity.success
|
|
113
116
|
? parsedIdentity.output.initializationId
|
|
114
117
|
: undefined,
|
|
115
|
-
error:
|
|
118
|
+
error: invalidInitResultPayloadError(),
|
|
116
119
|
})
|
|
117
120
|
}
|
|
118
121
|
|