@contentstack/studio-client 3.1.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -0
- package/dist/breakpoints/BreakpointManager.js +7 -5
- package/dist/breakpoints/BreakpointManager.js.map +1 -1
- package/dist/breakpoints/breakpoint.js +3 -2
- package/dist/breakpoints/breakpoint.js.map +1 -1
- package/dist/canvas/canvas.utils.js +13 -1
- package/dist/canvas/canvas.utils.js.map +1 -1
- package/dist/composable-studio/build-scoped-data.js +53 -31
- package/dist/composable-studio/build-scoped-data.js.map +1 -1
- package/dist/composable-studio/collect-data-needs.js +22 -4
- package/dist/composable-studio/collect-data-needs.js.map +1 -1
- package/dist/composable-studio/sdk.js +1288 -453
- package/dist/composable-studio/sdk.js.map +1 -1
- package/dist/composable-studio/section-data.js +316 -0
- package/dist/composable-studio/section-data.js.map +1 -0
- package/dist/composable-studio/studio-intelligence.manager.js +5 -2
- package/dist/composable-studio/studio-intelligence.manager.js.map +1 -1
- package/dist/config/config.schema.js +45 -8
- package/dist/config/config.schema.js.map +1 -1
- package/dist/config/default-config.js +10 -3
- package/dist/config/default-config.js.map +1 -1
- package/dist/config/process-user-config.js +37 -1
- package/dist/config/process-user-config.js.map +1 -1
- package/dist/config/stack-sdk-wrapper.js +71 -2
- package/dist/config/stack-sdk-wrapper.js.map +1 -1
- package/dist/context-menu/context-menu.js +5 -2
- package/dist/context-menu/context-menu.js.map +1 -1
- package/dist/design/handle-active-style-change.js +3 -2
- package/dist/design/handle-active-style-change.js.map +1 -1
- package/dist/dnd/dnd-event.js +2 -1
- package/dist/dnd/dnd-event.js.map +1 -1
- package/dist/dnd/dnd-manager.js +3 -1
- package/dist/dnd/dnd-manager.js.map +1 -1
- package/dist/editor.d.ts +250 -0
- package/dist/editor.js +11 -0
- package/dist/editor.js.map +1 -0
- package/dist/get-active-node-screenshot/get-node-screenshot-event.js +2 -1
- package/dist/get-active-node-screenshot/get-node-screenshot-event.js.map +1 -1
- package/dist/hidden-element-notification/hidden-element-notification-event.js +2 -1
- package/dist/hidden-element-notification/hidden-element-notification-event.js.map +1 -1
- package/dist/highlight-node/highlight-node-manager.js +225 -15
- package/dist/highlight-node/highlight-node-manager.js.map +1 -1
- package/dist/highlight-node/highlight-node.js +42 -6
- package/dist/highlight-node/highlight-node.js.map +1 -1
- package/dist/highlight-spacing/highlight-spacing-events.js +2 -1
- package/dist/highlight-spacing/highlight-spacing-events.js.map +1 -1
- package/dist/index.d.ts +1334 -467
- package/dist/index.js +14 -14
- package/dist/index.js.map +1 -1
- package/dist/url-change/url-change.js +4 -3
- package/dist/url-change/url-change.js.map +1 -1
- package/dist/url-metadata/build-template-query.js +27 -6
- package/dist/url-metadata/build-template-query.js.map +1 -1
- package/dist/utils/canvas.js +2 -1
- package/dist/utils/canvas.js.map +1 -1
- package/dist/utils/check-bindings.js +233 -0
- package/dist/utils/check-bindings.js.map +1 -0
- package/dist/utils/classify-response-error.js +288 -0
- package/dist/utils/classify-response-error.js.map +1 -0
- package/dist/utils/composable-studio-error.js +36 -20
- package/dist/utils/composable-studio-error.js.map +1 -1
- package/dist/utils/composition-not-found.js +52 -0
- package/dist/utils/composition-not-found.js.map +1 -0
- package/dist/utils/conditionBlock.js +80 -3
- package/dist/utils/conditionBlock.js.map +1 -1
- package/dist/utils/constants.js +21 -2
- package/dist/utils/constants.js.map +1 -1
- package/dist/utils/degraded-finding.js +166 -0
- package/dist/utils/degraded-finding.js.map +1 -0
- package/dist/utils/editor-mode.js +1 -1
- package/dist/utils/http-error.constants.js +12 -46
- package/dist/utils/http-error.constants.js.map +1 -1
- package/dist/utils/include-path-rules.js +141 -0
- package/dist/utils/include-path-rules.js.map +1 -0
- package/dist/utils/is-composition-being-edited.js +1 -1
- package/dist/utils/is-inside-iframe.js +1 -1
- package/dist/utils/is-inside-visual-builder-frame.js +1 -1
- package/dist/utils/parse-ui.js +60 -0
- package/dist/utils/parse-ui.js.map +1 -0
- package/dist/utils/query-param.js +1 -1
- package/dist/utils/query-param.js.map +1 -1
- package/dist/utils/query-url-budget.js +79 -0
- package/dist/utils/query-url-budget.js.map +1 -0
- package/dist/utils/reference-stub.js +75 -0
- package/dist/utils/reference-stub.js.map +1 -0
- package/dist/utils/reference_fetcher.js +59 -4
- package/dist/utils/reference_fetcher.js.map +1 -1
- package/dist/utils/require-stack.js +141 -0
- package/dist/utils/require-stack.js.map +1 -0
- package/dist/utils/served-by.js +33 -0
- package/dist/utils/served-by.js.map +1 -0
- package/dist/utils/spec-analyzer.js +2 -2
- package/dist/utils/spec-analyzer.js.map +1 -1
- package/dist/utils/studio-error-codes.js +253 -0
- package/dist/utils/studio-error-codes.js.map +1 -0
- package/dist/utils/studio-error.js +122 -0
- package/dist/utils/studio-error.js.map +1 -0
- package/dist/utils/studio-messaging.js +25 -0
- package/dist/utils/studio-messaging.js.map +1 -0
- package/dist/visual-builder/composition-save-event.js +2 -1
- package/dist/visual-builder/composition-save-event.js.map +1 -1
- package/package.json +19 -7
- package/LICENSE +0 -21
- package/dist/utils/error-messages.default.js +0 -141
- package/dist/utils/error-messages.default.js.map +0 -1
- package/dist/utils/error-messages.js +0 -47
- package/dist/utils/error-messages.js.map +0 -1
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { renderStudioErrorMessage } from './studio-error.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The findings that are not errors: the page rendered, and something on it is
|
|
5
|
+
* wrong.
|
|
6
|
+
*
|
|
7
|
+
* The cut is the one drawn by [the setup-before-the-request decision]: a fault
|
|
8
|
+
* that stopped the composition loading is fatal and is raised as an error, and a
|
|
9
|
+
* fault that left the page working is never an error anywhere. These are the
|
|
10
|
+
* second kind. **None of them throws**, because throwing hands the application an
|
|
11
|
+
* error and an application with an error renders its 500 page in place of a site
|
|
12
|
+
* that works.
|
|
13
|
+
*
|
|
14
|
+
* So they log, and logging is the whole of the contract. They are not on the
|
|
15
|
+
* diagnostic Studio receives: nothing reads them there, and the two findings a
|
|
16
|
+
* surface does act on are derived by comparing values Studio already holds rather
|
|
17
|
+
* than reported from here. A channel with no sender and no receiver is a guess
|
|
18
|
+
* about a design nobody has done yet.
|
|
19
|
+
*
|
|
20
|
+
* **They draw no cards either.** A blank element, an empty list and an
|
|
21
|
+
* unresolved reference all show themselves on the page, and a popover apiece
|
|
22
|
+
* would teach an author to dismiss the popover on sight — including the one that
|
|
23
|
+
* says their api key is wrong.
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* The cases where the page came up and the content did not.
|
|
27
|
+
*
|
|
28
|
+
* A code is the identifier a developer reads in the log line and quotes in a
|
|
29
|
+
* support conversation, so it is stable once published — the same rule the error
|
|
30
|
+
* codes follow. Nothing branches on these, because nothing receives them.
|
|
31
|
+
*/
|
|
32
|
+
const DegradedCode = {
|
|
33
|
+
/**
|
|
34
|
+
* A reference field came back as a two-key stub rather than as the entry it
|
|
35
|
+
* points at, so anything bound past that point has nothing to read.
|
|
36
|
+
*
|
|
37
|
+
* Either the composition nests deeper than one request can follow, or the
|
|
38
|
+
* entry it points at was deleted. Both produce the identical shape, so this
|
|
39
|
+
* names the symptom and ranks the two rather than asserting either.
|
|
40
|
+
*/
|
|
41
|
+
REFERENCES_UNRESOLVED: "REFERENCES_UNRESOLVED",
|
|
42
|
+
/**
|
|
43
|
+
* A binding reads a path that does not exist on the entry we hold.
|
|
44
|
+
*
|
|
45
|
+
* Ambiguous on purpose. A renamed or deleted field and a field the author has
|
|
46
|
+
* simply left empty are indistinguishable from here — we hold the entry, not
|
|
47
|
+
* the content type's schema — so this carries both causes and asserts
|
|
48
|
+
* neither.
|
|
49
|
+
*/
|
|
50
|
+
BINDING_PATH_MISSING: "BINDING_PATH_MISSING",
|
|
51
|
+
/**
|
|
52
|
+
* A binding that reads one value resolved to a group or a repeating block.
|
|
53
|
+
*
|
|
54
|
+
* Provable from the value's own type, so unlike the case above this one is
|
|
55
|
+
* asserted: the field the binding points at holds a different kind of value
|
|
56
|
+
* than the element can show. Usually the binding was pointed at a container
|
|
57
|
+
* rather than at a field inside it.
|
|
58
|
+
*/
|
|
59
|
+
BINDING_SHAPE_MISMATCH: "BINDING_SHAPE_MISMATCH",
|
|
60
|
+
/**
|
|
61
|
+
* Fewer entries came back than were asked for, so the elements bound to the
|
|
62
|
+
* missing ones have nothing to read.
|
|
63
|
+
*
|
|
64
|
+
* A comparison of uids in memory, and worth making because the failure is
|
|
65
|
+
* otherwise silent — a batch that quietly drops one entry looks exactly like a
|
|
66
|
+
* page where one element was never bound.
|
|
67
|
+
*/
|
|
68
|
+
FEWER_ENTRIES_THAN_REQUESTED: "FEWER_ENTRIES_THAN_REQUESTED",
|
|
69
|
+
/**
|
|
70
|
+
* A query data source ran and matched nothing.
|
|
71
|
+
*
|
|
72
|
+
* Not a failure by any measure available here — a filter that matches nothing
|
|
73
|
+
* today may match tomorrow — which is why it carries no causes and states only
|
|
74
|
+
* what happened. It is reported because "why is my product list empty" is the
|
|
75
|
+
* question this whole group exists to answer.
|
|
76
|
+
*/
|
|
77
|
+
QUERY_RETURNED_NO_ROWS: "QUERY_RETURNED_NO_ROWS",
|
|
78
|
+
/**
|
|
79
|
+
* An asset read through a dotted projection came back null on the preview
|
|
80
|
+
* service, on a request that also projected a bare field.
|
|
81
|
+
*
|
|
82
|
+
* A verified Preview API defect rather than anything in the project: the same
|
|
83
|
+
* request against the delivery CDN returns the asset. It takes the place of
|
|
84
|
+
* the missing-binding report when its conditions hold, because the two look
|
|
85
|
+
* identical and only this one stops an author rebuilding a binding that was
|
|
86
|
+
* never broken.
|
|
87
|
+
*
|
|
88
|
+
* It ranks rather than asserts. The defect also needs the other projected
|
|
89
|
+
* field to be a modular block, and nothing here holds the content type's
|
|
90
|
+
* schema to check that, so the second cause stays on the list.
|
|
91
|
+
*/
|
|
92
|
+
PREVIEW_ASSET_PROJECTION_NULL: "PREVIEW_ASSET_PROJECTION_NULL",
|
|
93
|
+
/**
|
|
94
|
+
* A query data source's request did not complete, so its list is empty for a
|
|
95
|
+
* reason that has nothing to do with its filters.
|
|
96
|
+
*
|
|
97
|
+
* The sibling of the case above and the reason that one can be trusted. Both
|
|
98
|
+
* end with an empty list on the page, and telling them apart is the whole
|
|
99
|
+
* point: one says "nothing matched", which sends an author to their filters,
|
|
100
|
+
* and this one says the question was never answered, which is nobody's
|
|
101
|
+
* content problem.
|
|
102
|
+
*/
|
|
103
|
+
QUERY_REQUEST_FAILED: "QUERY_REQUEST_FAILED",
|
|
104
|
+
/**
|
|
105
|
+
* A composition's stored data sources will not parse, so none of its queries
|
|
106
|
+
* ran and every element bound to one is blank.
|
|
107
|
+
*
|
|
108
|
+
* Ours to fix, like the layout case, and named with the composition it belongs
|
|
109
|
+
* to so the reader can tell which page in a project is affected. Not fatal:
|
|
110
|
+
* the layout still rendered, and taking the page down over it would turn a
|
|
111
|
+
* partly-empty page into no page at all.
|
|
112
|
+
*/
|
|
113
|
+
DATA_SOURCES_UNREADABLE: "DATA_SOURCES_UNREADABLE",
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Whether this finding has already been reported on this page.
|
|
117
|
+
*
|
|
118
|
+
* These findings repeat: a binding inside a repeater resolves once per item, and
|
|
119
|
+
* a section's references are walked once per instance. Reporting each time turns
|
|
120
|
+
* one fact into fifty lines and buries whatever else the console had to say. The
|
|
121
|
+
* key is the rendered message, so two findings collapse only when a reader could
|
|
122
|
+
* not have told them apart anyway.
|
|
123
|
+
*
|
|
124
|
+
* Module-scoped, which is the same lifetime as the page: a reload starts over,
|
|
125
|
+
* which is what someone re-running a broken page expects.
|
|
126
|
+
*/
|
|
127
|
+
const alreadyReported = new Set();
|
|
128
|
+
/**
|
|
129
|
+
* Whether the console should hear about any of this at all.
|
|
130
|
+
*
|
|
131
|
+
* On in development, because that is where somebody is looking. Off in
|
|
132
|
+
* production, because a customer's production console is theirs and not a place
|
|
133
|
+
* to put our warnings — unless their developer asks for it with `debug`, which is
|
|
134
|
+
* the only way to diagnose a deployed site that has no Studio chrome on it.
|
|
135
|
+
*
|
|
136
|
+
* Exported because two of these findings cost something to detect — one walks
|
|
137
|
+
* the whole fetched entry tree, one walks every binding — and doing that work
|
|
138
|
+
* to build a report nobody will read is a tax on every page that is fine. A
|
|
139
|
+
* site running in production pays for neither.
|
|
140
|
+
*/
|
|
141
|
+
function degradedReportingIsOn(options) {
|
|
142
|
+
if (options?.debug === true)
|
|
143
|
+
return true;
|
|
144
|
+
return process.env.NODE_ENV === "development";
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Reports a finding that did not stop the page.
|
|
148
|
+
*
|
|
149
|
+
* The message carries the code, the sentence and every cause, because a console
|
|
150
|
+
* shows the first argument and nothing else. The findings go alongside as an
|
|
151
|
+
* object rather than flattened into the prose: one collapsed row a reader opens
|
|
152
|
+
* when they want it, and one that copies as structured data rather than as text
|
|
153
|
+
* somebody has to parse back.
|
|
154
|
+
*/
|
|
155
|
+
function reportDegraded(input, options) {
|
|
156
|
+
if (!degradedReportingIsOn(options))
|
|
157
|
+
return;
|
|
158
|
+
const message = renderStudioErrorMessage(input);
|
|
159
|
+
if (alreadyReported.has(message))
|
|
160
|
+
return;
|
|
161
|
+
alreadyReported.add(message);
|
|
162
|
+
console.warn(message, input.findings);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export { DegradedCode, degradedReportingIsOn, reportDegraded };
|
|
166
|
+
//# sourceMappingURL=degraded-finding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"degraded-finding.js","sources":["../../src/utils/degraded-finding.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;;;;;;;AAqBG;AAMH;;;;;;AAMG;AACI,MAAM,YAAY,GAAG;AAC1B;;;;;;;AAOG;AACH,IAAA,qBAAqB,EAAE,uBAAuB;AAC9C;;;;;;;AAOG;AACH,IAAA,oBAAoB,EAAE,sBAAsB;AAC5C;;;;;;;AAOG;AACH,IAAA,sBAAsB,EAAE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|
/**
|
|
13
10
|
* Contentstack API error codes (from Contentstack API responses).
|
|
14
11
|
* @internal
|
|
15
12
|
*/
|
|
16
13
|
const ContentstackErrorCode = {
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
/** No stack answers to this API key on this host. Arrives as 412/109. */
|
|
15
|
+
STACK_NOT_FOUND: 109,
|
|
16
|
+
/** The queried content type is not on the stack. Arrives as 422/118. */
|
|
17
|
+
CONTENT_TYPE_NOT_FOUND: 118,
|
|
18
|
+
/**
|
|
19
|
+
* Overloaded four ways — missing environment, missing language, missing
|
|
20
|
+
* entry, include-reference processing — separated only by `error_message`.
|
|
21
|
+
* The name records the meaning an unmatched instance falls back to.
|
|
22
|
+
*/
|
|
19
23
|
ENTRY_NOT_FOUND: 141,
|
|
20
24
|
};
|
|
21
25
|
/**
|
|
@@ -41,44 +45,6 @@ function getStatusCode(err) {
|
|
|
41
45
|
const code = err?.status ?? err?.statusCode;
|
|
42
46
|
return typeof code === "number" && Number.isFinite(code) ? code : undefined;
|
|
43
47
|
}
|
|
44
|
-
/**
|
|
45
|
-
* Returns true if the status code indicates an authentication/authorization error (401 or 403).
|
|
46
|
-
* @internal
|
|
47
|
-
*/
|
|
48
|
-
function isAuthError(statusCode) {
|
|
49
|
-
return (statusCode === HttpStatus.UNAUTHORIZED ||
|
|
50
|
-
statusCode === HttpStatus.FORBIDDEN);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Returns true if the status code indicates a server error (5xx).
|
|
54
|
-
* @internal
|
|
55
|
-
*/
|
|
56
|
-
function isServerError(statusCode) {
|
|
57
|
-
return (statusCode >= HttpStatus.SERVER_ERROR_MIN &&
|
|
58
|
-
statusCode <= HttpStatus.SERVER_ERROR_MAX);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Returns true if the error is a Contentstack auth error by error_code (104 or 105).
|
|
62
|
-
* @internal
|
|
63
|
-
*/
|
|
64
|
-
function isContentstackAuthError(err) {
|
|
65
|
-
const code = err?.error_code;
|
|
66
|
-
return (code === ContentstackErrorCode.UNAUTHORIZED ||
|
|
67
|
-
code === ContentstackErrorCode.INVALID_CREDENTIALS);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Returns true if the error has Contentstack auth error shape (errors.api_key or errors.authorization)
|
|
71
|
-
* without necessarily having an HTTP status. Used when API returns auth failure in body only.
|
|
72
|
-
* @internal
|
|
73
|
-
*/
|
|
74
|
-
function hasAuthErrorShape(err) {
|
|
75
|
-
if (!err || typeof err !== "object")
|
|
76
|
-
return false;
|
|
77
|
-
const apiKey = err?.errors?.api_key;
|
|
78
|
-
const auth = err?.errors?.authorization;
|
|
79
|
-
return ((Array.isArray(apiKey) && apiKey.length > 0) ||
|
|
80
|
-
(Array.isArray(auth) && auth.length > 0));
|
|
81
|
-
}
|
|
82
48
|
/**
|
|
83
49
|
* Returns true if the error indicates a network-level failure (no HTTP response).
|
|
84
50
|
* @internal
|
|
@@ -101,5 +67,5 @@ function isNetworkError(err) {
|
|
|
101
67
|
return false;
|
|
102
68
|
}
|
|
103
69
|
|
|
104
|
-
export { ContentstackErrorCode, HttpStatus, NETWORK_ERROR_CODES, NETWORK_ERROR_TYPE, NetworkErrorCode, getStatusCode,
|
|
70
|
+
export { ContentstackErrorCode, HttpStatus, NETWORK_ERROR_CODES, NETWORK_ERROR_TYPE, NetworkErrorCode, getStatusCode, isNetworkError };
|
|
105
71
|
//# sourceMappingURL=http-error.constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-error.constants.js","sources":["../../src/utils/http-error.constants.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;AAGG;AAEI,MAAM,UAAU,GAAG;AACxB,
|
|
1
|
+
{"version":3,"file":"http-error.constants.js","sources":["../../src/utils/http-error.constants.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;AAGG;AAEI,MAAM,UAAU,GAAG;AACxB,IACA,mBAAmB,EAAE,GAAG;AACxB,IACA,SAAS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IACb;AAEA,IAAA,OAAO,KAAK;AACd;;;;"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { DATA_SOURCES } from '@contentstack/studio-registry';
|
|
2
|
+
import { getBindingStringForCS } from './sdk.js';
|
|
3
|
+
|
|
4
|
+
const EMPTY_INDEX = {
|
|
5
|
+
containers: new Set(),
|
|
6
|
+
blockPaths: new Set(),
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Read a page's saved UI and note every modular block the author used.
|
|
10
|
+
*
|
|
11
|
+
* Pass the page's own UI, not a section's. The paths here start at the page
|
|
12
|
+
* entry, so a section's UI would give paths that point at nothing.
|
|
13
|
+
*
|
|
14
|
+
* Only repeaters bound to the template count. A repeater bound to a pinned
|
|
15
|
+
* entry, or to another repeater, is not looping over a field of the page, so
|
|
16
|
+
* what it loops over is not a field on the page either.
|
|
17
|
+
*/
|
|
18
|
+
function indexModularBlocks(ui) {
|
|
19
|
+
if (!ui)
|
|
20
|
+
return EMPTY_INDEX;
|
|
21
|
+
const containers = new Set();
|
|
22
|
+
const blockPaths = new Set();
|
|
23
|
+
/** Repeater uid, and the field it loops over. */
|
|
24
|
+
const repeaterField = new Map();
|
|
25
|
+
const walk = (node) => {
|
|
26
|
+
if (!node || typeof node !== "object")
|
|
27
|
+
return;
|
|
28
|
+
if (node.type === "repeater" && node.uid) {
|
|
29
|
+
const binding = node.props?.items?.binding;
|
|
30
|
+
if (binding?.type === DATA_SOURCES.TEMPLATE) {
|
|
31
|
+
const field = getBindingStringForCS(binding);
|
|
32
|
+
if (field)
|
|
33
|
+
repeaterField.set(node.uid, field);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const condition = node.metadata?.condition;
|
|
37
|
+
if (node.type === "condition-block" &&
|
|
38
|
+
condition?.type === "modular_block") {
|
|
39
|
+
const blockUid = condition.value;
|
|
40
|
+
const container = repeaterField.get(condition.conditionBinding?.value?.repeaterUID);
|
|
41
|
+
if (typeof blockUid === "string" && blockUid && container) {
|
|
42
|
+
containers.add(container);
|
|
43
|
+
blockPaths.add(`${container}.${blockUid}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (const child of node.children ?? [])
|
|
47
|
+
walk(child);
|
|
48
|
+
for (const slotChildren of Object.values(node.slots ?? {})) {
|
|
49
|
+
for (const child of slotChildren ?? [])
|
|
50
|
+
walk(child);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
walk(ui);
|
|
54
|
+
return { containers, blockPaths };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Does this path stop at a modular block, or at the field holding the blocks?
|
|
58
|
+
*
|
|
59
|
+
* Worth knowing before adding it to `include[]`. An include path only does
|
|
60
|
+
* something if it ends at a reference. End it on blocks instead and the Delivery
|
|
61
|
+
* API takes the path and ignores it, so we have spent URL space we are short of
|
|
62
|
+
* and got nothing back.
|
|
63
|
+
*
|
|
64
|
+
* Ask before adding a path, not after. Every path this catches comes from one
|
|
65
|
+
* place — the loop that walks where a section sits on the page — so there is no
|
|
66
|
+
* reason to build them and take them out again.
|
|
67
|
+
*
|
|
68
|
+
* We only say yes for a block the author actually picked. If we cannot place a
|
|
69
|
+
* path we say no, because not recognising it does not make it wrong: dropping a
|
|
70
|
+
* real reference leaves a section blank on the live site, which is far worse
|
|
71
|
+
* than sending a few spare bytes.
|
|
72
|
+
*
|
|
73
|
+
* This catches less than we would like, and deliberately so. The right question
|
|
74
|
+
* is the opposite one — does this path end at a reference? — but answering it
|
|
75
|
+
* needs the field types, and the SDK has no content type schema. Blocks are the
|
|
76
|
+
* only kind we can prove from the composition alone, because the author picked
|
|
77
|
+
* them by hand. A path stopping at a group, a file, or a block we did not index
|
|
78
|
+
* is just as useless in `include[]` and still gets through. Measured on the
|
|
79
|
+
* alpha demo stack that is 17 paths of 270, about 1 KB spread over 11 pages.
|
|
80
|
+
* Closing it needs field types from the Studio API.
|
|
81
|
+
*/
|
|
82
|
+
function isModularBlockPath(path, index) {
|
|
83
|
+
return index.containers.has(path) || index.blockPaths.has(path);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Remove the `include[]` paths a longer path already covers.
|
|
87
|
+
*
|
|
88
|
+
* The longest path brings in the ones above it. Asking for `a.ref.nested_ref`
|
|
89
|
+
* resolves `a.ref` too, so a path that is just the start of a longer one is
|
|
90
|
+
* asking twice. We checked this against the live API rather than assuming it:
|
|
91
|
+
* the two requests came back byte for byte the same.
|
|
92
|
+
*
|
|
93
|
+
* This has to run once everything has been merged. Whether a path is the start
|
|
94
|
+
* of a longer one depends on paths that other sections, the page itself, and
|
|
95
|
+
* the caller's own extra references have yet to add.
|
|
96
|
+
*/
|
|
97
|
+
function dropImpliedParents(paths) {
|
|
98
|
+
return paths.filter((path) => !paths.some((other) => other !== path && other.startsWith(`${path}.`)));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Version from which a composition's persisted `resolvedReferences` can be
|
|
102
|
+
* trusted to hold every include path the page needs.
|
|
103
|
+
*
|
|
104
|
+
* Below it the set was accumulated by union, and before the save-time derive
|
|
105
|
+
* existed at all it held only the author's ticks. See `SCHEMA_VERSION`.
|
|
106
|
+
*/
|
|
107
|
+
const COMPLETE_INCLUDES_FROM_SCHEMA_VERSION = "1.1.0";
|
|
108
|
+
/** Compare two dotted numeric versions. Missing segments count as 0. */
|
|
109
|
+
function compareVersions(a, b) {
|
|
110
|
+
const pa = a.split(".");
|
|
111
|
+
const pb = b.split(".");
|
|
112
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
113
|
+
const na = Number.parseInt(pa[i] ?? "0", 10) || 0;
|
|
114
|
+
const nb = Number.parseInt(pb[i] ?? "0", 10) || 0;
|
|
115
|
+
if (na !== nb)
|
|
116
|
+
return na - nb;
|
|
117
|
+
}
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Whether this composition's persisted `resolvedReferences` is the whole story,
|
|
122
|
+
* so the SDK does not need to work out include paths of its own.
|
|
123
|
+
*
|
|
124
|
+
* Deliberately conservative. An absent or unparseable `schema_version` is read as
|
|
125
|
+
* "too old", because the cost of the two mistakes is not symmetric: deriving when
|
|
126
|
+
* it was unnecessary sends a few paths that resolve nothing, while NOT deriving
|
|
127
|
+
* when it was necessary leaves every bound-but-un-ticked reference unfetched and
|
|
128
|
+
* the section renders blank.
|
|
129
|
+
*
|
|
130
|
+
* A composition saved by the new Studio but not touched since the change still
|
|
131
|
+
* reads the old version and keeps the safety net — the same save that would clear
|
|
132
|
+
* the flag is the one that populates the set properly.
|
|
133
|
+
*/
|
|
134
|
+
function compositionCarriesCompleteIncludes(schemaVersion) {
|
|
135
|
+
if (!schemaVersion || !/^\d+(\.\d+)*$/.test(schemaVersion.trim()))
|
|
136
|
+
return false;
|
|
137
|
+
return (compareVersions(schemaVersion.trim(), COMPLETE_INCLUDES_FROM_SCHEMA_VERSION) >= 0);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { COMPLETE_INCLUDES_FROM_SCHEMA_VERSION, compositionCarriesCompleteIncludes, dropImpliedParents, indexModularBlocks, isModularBlockPath };
|
|
141
|
+
//# sourceMappingURL=include-path-rules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"include-path-rules.js","sources":["../../src/utils/include-path-rules.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AA2BA,MAAM,WAAW,GAAuB;IACtC,UAAU,EAAE,IAAI,GAAG,EAAU;IAC7B,UAAU,EAAE,IAAI,GAAG,EAAU;CAC9B;AAED;;;;;;;;;AASG;AACG,SAAU,kBAAkB,CAChC,EAA2B,EAAA;AAE3B,IAAA,IAAI,CAAC,EAAE;AAAE,QAAA,OAAO,WAAW;AAE3B,IAAA,MAAM,UAAU,GAAG,IAAI,GAAG,EAAU;AACpC,IAAA,MAAM,UAAU,GAAG,IAAI,GAAG,EAAU;;AAEpC,IAAA,MAAM,aAAa,GAAG,IAAI,GAAG,EAAkB;AAE/C,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AC,IAAI,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC;QACjD,IAAI,EAAE,KAAK,EAAE;YAAE,OAAO,EAAE,GAAG,EAAE;IAC/B;AACA,IAAA,OAAO,CAAC;AACV;AAEA;;;;;;;;;;;;;AAaG;AACG,SAAU,kCAAkC,CAChD,aAA6B,EAAA;AAE7B,IAAA,IAAI,CAAC,aAAa,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;AAC/D,QAAA,OAAO,KAAK;AACd,IAAA,QACE,eAAe,CACb,aAAa,CAAC,IAAI,EAAE,EACpB,qCAAqC,CACtC,IAAI,CAAC;AAEV;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { COMPOSABLE_STUDIO_QUERY_PARAM } from '@contentstack/studio-messenger';
|
|
1
|
+
import { COMPOSABLE_STUDIO_QUERY_PARAM } from '@contentstack/studio-messenger/constants';
|
|
2
2
|
import { queryParam } from './query-param.js';
|
|
3
3
|
|
|
4
4
|
function isCompositionBeingEdited(compositionUid) {
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createStudioError, buildStudioErrorContext } from './studio-error.js';
|
|
2
|
+
import { StudioErrorCode } from './studio-error-codes.js';
|
|
3
|
+
import { decompressSpec } from '../studio-internal/dist/utils/spec-compression.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The one way a stored layout is read.
|
|
7
|
+
*
|
|
8
|
+
* Specs are deflated and base64'd, with legacy raw JSON still accepted, so a
|
|
9
|
+
* damaged one fails inside a compression library and surfaces as whatever that
|
|
10
|
+
* library said — "invalid distance too far back", or a JSON parse error at some
|
|
11
|
+
* offset. A developer reading that has no way to know it is about a composition,
|
|
12
|
+
* let alone which one.
|
|
13
|
+
*
|
|
14
|
+
* Every reader goes through here rather than calling the decompressor directly,
|
|
15
|
+
* which is what makes the diagnosis unavoidable instead of something each site
|
|
16
|
+
* has to remember. It lives in its own module so the shared reference fetcher can
|
|
17
|
+
* use it without importing the SDK back.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Reads a stored layout, raising `SPEC_UNREADABLE` when it will not open.
|
|
21
|
+
*
|
|
22
|
+
* `subject` is the composition or section uid when the caller knows it. It is
|
|
23
|
+
* optional because some readers genuinely do not, and naming the case is worth
|
|
24
|
+
* having either way.
|
|
25
|
+
*
|
|
26
|
+
* One cause is deliberately absent, and it is the tempting one: a spec written by
|
|
27
|
+
* a newer Studio. Those bytes inflate and parse perfectly well and fail later as
|
|
28
|
+
* an unknown component type, so offering version skew here sends the reader to
|
|
29
|
+
* upgrade something that was never the problem.
|
|
30
|
+
*/
|
|
31
|
+
function parseUI(ui, subject) {
|
|
32
|
+
try {
|
|
33
|
+
return decompressSpec(ui);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
throw createStudioError({
|
|
37
|
+
code: StudioErrorCode.SPEC_UNREADABLE,
|
|
38
|
+
headline: subject
|
|
39
|
+
? `The stored layout for ${subject} could not be read.`
|
|
40
|
+
: "A stored layout could not be read.",
|
|
41
|
+
causes: [
|
|
42
|
+
{
|
|
43
|
+
cause: "The stored layout is damaged.",
|
|
44
|
+
fix: "Nothing in this project will fix it — report it.",
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
context: buildStudioErrorContext(),
|
|
48
|
+
// The bytes themselves never travel: a diagnosis is built to be pasted
|
|
49
|
+
// somewhere public, and a stored layout is the customer's content.
|
|
50
|
+
findings: {
|
|
51
|
+
subject,
|
|
52
|
+
wasCompressed: ui.startsWith("zlib:"),
|
|
53
|
+
byteLength: ui.length,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { parseUI };
|
|
60
|
+
//# sourceMappingURL=parse-ui.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-ui.js","sources":["../../src/utils/parse-ui.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAAA;;;;;;;;;;;;;AAaG;AAOH;;;;;;;;;;;AAWG;AACG,SAAU,OAAO,CAAC,EAAU,EAAE,OAAgB,EAAA;AAClD,IAAA,IAAI;AACF,QAAA,OAAO,cAAc,CAAC,EAAE,CAAC;IAC3B;AAAE,IAAA,MAAM;AACN,QAAA,MAAM,iBAAiB,CAAC;YACtB,IAAI,EAAE,eAAe,CAAC,eAAe;AACrC,YAAA,QAAQ,EAAE;kBACN,CAAA,sBAAA,EAAyB,OAAO,CAAA,mBAAA;AAClC,kBAAE,oCAAoC;AACxC,YAAA,MAAM,EAAE;AACN,gBAAA;AACE,oBAAA,KAAK,EAAE,+BAA+B;AACtC,oBAAA,GAAG,EAAE,kDAAkD;AACxD,iBAAA;AACF,aAAA;YACD,OAAO,EAAE,uBAAuB,EAAE;;;AAGlC,YAAA,QAAQ,EAAE;gBACR,OAAO;AACP,gBAAA,aAAa,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC;gBACrC,UAAU,EAAE,EAAE,CAAC,MAAM;AACtB,aAAA;AACF,SAAA,CAAC;IACJ;AACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query-param.js","sources":["../../src/utils/query-param.ts"],"sourcesContent":[null],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"query-param.js","sources":["../../src/utils/query-param.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAGO,MAAM,UAAU,GAAG;AACxB,IAAA,GAAG,CAAC,GAAoC,EAAA;QACtC,IAAI,UAAU,EAAE;AACd,YAAA,OAAO,IAAI,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC;QAC7D;AAEA,QAAA,OAAO,IAAI;IACb,CAAC;AACD,IAAA,GAAG,CAAC,GAAoC,EAAA;QACtC,IAAI,UAAU,EAAE;AACd,YAAA,OAAO,IAAI,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC;QAC7D;AAEA,QAAA,OAAO,KAAK;IACd,CAAC;;AAGI,MAAM,mBAAmB,GAAG,CAAC,IAAwB,KAAI;AAC9D,IAAA,MAAM,eAAe,GAAG,IAAI,GAAG,EAAE;IACjC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EAAE,KAAK,CAAC,KAAI;QAClC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE;AACnC,YAAA,eAAe,CAAC,GAAG,CAAC,SAAS,EAAE,EAAE,CAAC;QACpC;QACA,eAAe,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;AAC5C,IAAA,CAAC,CAAC;IACF,MAAM,UAAU,GAA6B,EAAE;IAC/C,eAAe,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,SAAS,KAAI;AAC5C,QAAA,UAAU,CAAC,CAAA,KAAA,EAAQ,SAAS,GAAG,CAAC,GAAG,MAAM;AAC3C,IAAA,CAAC,CAAC;AACF,IAAA,OAAO,UAAU;AACnB;;;;"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { sumBy } from 'lodash-es';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Delivery API entry queries are GET requests, so every projected field and
|
|
5
|
+
* every included reference costs URL bytes. Past a certain request-line length
|
|
6
|
+
* the edge rejects the call outright with `414 Request-URI Too Large` (and, near
|
|
7
|
+
* the boundary, `431`) — an HTML error the SDK surfaces as an opaque failure,
|
|
8
|
+
* which downstream turns into a missing page rather than a visible outage.
|
|
9
|
+
*
|
|
10
|
+
* The measured cut-off on `cdn.contentstack.io` is ~16,380 bytes, and it is
|
|
11
|
+
* applied to the *percent-encoded* request line: literal `[` and `]` are
|
|
12
|
+
* normalised to `%5B`/`%5D` before the length is checked, so sending raw
|
|
13
|
+
* brackets buys nothing. The estimates here therefore count the encoded form.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Budget we hold ourselves to. Deliberately below the measured cut-off: the
|
|
17
|
+
* estimate cannot see headers the stack SDK adds, and a request that squeaks
|
|
18
|
+
* under the limit today breaks the next time an author adds a section.
|
|
19
|
+
*
|
|
20
|
+
* Measured on cdn.contentstack.io: 15,092 bytes returns 200, 15,292 returns 431,
|
|
21
|
+
* 16,469 returns 414. Nothing in a real stack sits between roughly 6KB and 21KB,
|
|
22
|
+
* so lowering this from 15,000 buys headroom and changes which requests give up
|
|
23
|
+
* their projection not at all.
|
|
24
|
+
*/
|
|
25
|
+
const URL_BUDGET_BYTES = 12_000;
|
|
26
|
+
/**
|
|
27
|
+
* Allowance for the parts of the URL this module cannot see — origin, content
|
|
28
|
+
* type path, environment, locale, variant, and the `query` JSON.
|
|
29
|
+
*/
|
|
30
|
+
const BASE_ALLOWANCE_BYTES = 512;
|
|
31
|
+
/** `only%5B<parent>%5D%5B%5D=<field>&` minus the two variable segments. */
|
|
32
|
+
const ONLY_PARAM_OVERHEAD_BYTES = 18;
|
|
33
|
+
/** `include%5B%5D=<path>&` minus the path. */
|
|
34
|
+
const INCLUDE_PARAM_OVERHEAD_BYTES = 15;
|
|
35
|
+
/**
|
|
36
|
+
* Approximate the encoded byte length of the entry query this shape produces.
|
|
37
|
+
*
|
|
38
|
+
* Approximate is enough: it decides whether to drop an optimisation, so being a
|
|
39
|
+
* few hundred bytes pessimistic costs nothing while being optimistic costs the
|
|
40
|
+
* whole request.
|
|
41
|
+
*/
|
|
42
|
+
function estimateEntryQueryBytes({ references, only, }) {
|
|
43
|
+
const includeBytes = sumBy(references, (path) => INCLUDE_PARAM_OVERHEAD_BYTES + encodeURIComponent(path).length);
|
|
44
|
+
const onlyBytes = sumBy(only, ([parent, field]) => ONLY_PARAM_OVERHEAD_BYTES +
|
|
45
|
+
encodeURIComponent(parent).length +
|
|
46
|
+
encodeURIComponent(field).length);
|
|
47
|
+
return BASE_ALLOWANCE_BYTES + includeBytes + onlyBytes;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Keep an entry query inside the Delivery API's URL limit.
|
|
51
|
+
*
|
|
52
|
+
* `include[]` is load-bearing — dropping a reference path leaves the entry
|
|
53
|
+
* unresolved and the page renders blank — so it is never touched. `only[]` is
|
|
54
|
+
* an optimisation: without it the API returns every field, which is a larger
|
|
55
|
+
* response but a correct one. When the query cannot fit, the projection is what
|
|
56
|
+
* gives way.
|
|
57
|
+
*/
|
|
58
|
+
function applyUrlBudget(shape, budgetBytes = URL_BUDGET_BYTES) {
|
|
59
|
+
const estimatedBytes = estimateEntryQueryBytes(shape);
|
|
60
|
+
if (estimatedBytes <= budgetBytes) {
|
|
61
|
+
return { only: shape.only, dropped: false, estimatedBytes };
|
|
62
|
+
}
|
|
63
|
+
const withoutOnly = estimateEntryQueryBytes({
|
|
64
|
+
references: shape.references,
|
|
65
|
+
only: [],
|
|
66
|
+
});
|
|
67
|
+
return { only: [], dropped: true, estimatedBytes: withoutOnly };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Whether a query is still over budget once the projection is gone, i.e. the
|
|
71
|
+
* reference paths alone will not fit. Nothing can be shed at this point, so the
|
|
72
|
+
* caller should warn rather than fail silently.
|
|
73
|
+
*/
|
|
74
|
+
function referencesExceedBudget(references, budgetBytes = URL_BUDGET_BYTES) {
|
|
75
|
+
return estimateEntryQueryBytes({ references, only: [] }) > budgetBytes;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { URL_BUDGET_BYTES, applyUrlBudget, estimateEntryQueryBytes, referencesExceedBudget };
|
|
79
|
+
//# sourceMappingURL=query-url-budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-url-budget.js","sources":["../../src/utils/query-url-budget.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;;;;;;;;;;;AAWG;AAEH;;;;;;;;;AASG;AACI,MAAM,gBAAgB,GAAG;AAEhC;;;AAGG;AACH,MAAM,oB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