@lowdefy/api 6.0.0 → 7.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.
Files changed (208) hide show
  1. package/dist/context/applySystemTrust.js +45 -0
  2. package/dist/context/createApiContext.js +18 -3
  3. package/dist/context/createAuthorizeOutcome.js +91 -0
  4. package/dist/context/createBoundSystemContext.js +33 -0
  5. package/dist/context/createEvaluateOperators.js +10 -3
  6. package/dist/context/createRequestSignal.js +47 -0
  7. package/dist/context/createSystemContext.js +80 -0
  8. package/dist/context/getCanonicalUrl.js +27 -0
  9. package/dist/context/getCurrentEnvironment.js +27 -0
  10. package/dist/context/isSameOriginRequest.js +85 -0
  11. package/dist/context/isWebSocketOriginAllowed.js +63 -0
  12. package/dist/context/normalizeInjectedCaller.js +58 -0
  13. package/dist/context/parseRequestBody.js +32 -0
  14. package/dist/context/readMemberships.js +71 -0
  15. package/dist/context/resolveAuthentication.js +758 -0
  16. package/dist/context/resolveStrategyCaller.js +7 -3
  17. package/dist/csp/addNonceToElements.js +23 -0
  18. package/dist/csp/createContentSecurityPolicy.js +45 -0
  19. package/dist/csp/getWebSocketSources.js +36 -0
  20. package/dist/email/renderAuthEmail.js +99 -0
  21. package/dist/index.js +22 -5
  22. package/dist/response/buildEndpointResult.js +3 -1
  23. package/dist/response/createWireProjection.js +88 -0
  24. package/dist/response/formatErrorForAgent.js +35 -0
  25. package/dist/response/normalizeErrorSources.js +10 -13
  26. package/dist/response/redactErrorResponse.js +42 -8
  27. package/dist/response/redactResponse.js +17 -10
  28. package/dist/routes/agent/authorizeAgent.js +17 -5
  29. package/dist/routes/agent/callAgent.js +12 -2
  30. package/dist/routes/agent/collectChatFiles.js +39 -0
  31. package/dist/routes/agent/prepareAgent.js +29 -4
  32. package/dist/routes/auth/buildAdminPlugin.js +33 -0
  33. package/dist/routes/auth/buildCaptchaPlugin.js +61 -0
  34. package/dist/routes/auth/buildMcpTokenPlugin.js +77 -0
  35. package/dist/routes/auth/buildOauthPostLogin.js +73 -0
  36. package/dist/routes/auth/buildPhoneNumberPlugin.js +57 -0
  37. package/dist/routes/auth/buildProviders.js +61 -0
  38. package/dist/routes/auth/createAsMetadataHandler.js +27 -0
  39. package/dist/routes/auth/createAuthLogger.js +31 -0
  40. package/dist/routes/auth/createMcpAccessTokenClaims.js +52 -0
  41. package/dist/routes/auth/createOnAPIError.js +41 -0
  42. package/dist/routes/auth/createSendEmail.js +54 -0
  43. package/dist/routes/auth/ensureMcpTokenIndexes.js +48 -0
  44. package/dist/routes/auth/getAuthEnforcement.js +41 -0
  45. package/dist/routes/auth/getBetterAuth.js +107 -0
  46. package/dist/routes/auth/getBetterAuthConfig.js +767 -0
  47. package/dist/routes/auth/handleAuthRequest.js +43 -0
  48. package/dist/routes/auth/hooks/authHookPoints.js +204 -0
  49. package/dist/routes/auth/hooks/buildEngineHooks.js +68 -0
  50. package/dist/routes/auth/hooks/buildHooks.js +112 -0
  51. package/dist/routes/auth/{callbacks/addUserFieldsToSession.js → hooks/composeAfterSlot.js} +11 -13
  52. package/dist/routes/auth/hooks/composeBeforeSlot.js +41 -0
  53. package/dist/routes/auth/hooks/createHookDispatch.js +59 -0
  54. package/dist/routes/auth/hooks/createUserAfterHook.js +36 -0
  55. package/dist/routes/auth/hooks/createUserBeforeHook.js +50 -0
  56. package/dist/routes/auth/hooks/findHookUser.js +27 -0
  57. package/dist/routes/auth/modelNames.js +30 -0
  58. package/dist/routes/auth/organizations/OrganizationKeyError.js +28 -0
  59. package/dist/routes/auth/organizations/authorizeRole.js +27 -0
  60. package/dist/routes/auth/organizations/buildOrganizationPlugin.js +178 -0
  61. package/dist/routes/auth/organizations/createAcceptActiveOrgGuardHook.js +57 -0
  62. package/dist/routes/auth/organizations/createAcceptExistingMemberHook.js +120 -0
  63. package/dist/routes/auth/organizations/createActiveOrgPolicyHook.js +403 -0
  64. package/dist/routes/auth/organizations/createAdmissionGateHook.js +50 -0
  65. package/dist/routes/auth/organizations/createAfterAcceptInvitationHook.js +90 -0
  66. package/dist/routes/auth/organizations/createAutoJoinHook.js +68 -0
  67. package/dist/routes/auth/organizations/createEmailSendGate.js +59 -0
  68. package/dist/routes/auth/organizations/createExpiredInvitationGate.js +76 -0
  69. package/dist/routes/auth/organizations/ensureAuthIndexes.js +122 -0
  70. package/dist/routes/auth/organizations/ensureOrganization.js +132 -0
  71. package/dist/routes/auth/organizations/findPendingInvitation.js +43 -0
  72. package/dist/routes/auth/organizations/getHookRequestHeaders.js +24 -0
  73. package/dist/routes/auth/organizations/getOrganizationBinding.js +65 -0
  74. package/dist/routes/auth/organizations/isEmailAdmitted.js +89 -0
  75. package/dist/routes/auth/organizations/organizationAccessControl.js +94 -0
  76. package/dist/routes/auth/organizations/resolvePinnedOrganization.js +62 -0
  77. package/dist/routes/auth/requestHooks/beginTwoFactorChallenge.js +145 -0
  78. package/dist/routes/auth/requestHooks/buildRequestHooks.js +288 -0
  79. package/dist/routes/auth/requestHooks/createOauthPostLoginHook.js +43 -0
  80. package/dist/routes/auth/requestHooks/createTwoFactorChallengeHook.js +59 -0
  81. package/dist/routes/auth/requestHooks/dispatchRequestHooks.js +47 -0
  82. package/dist/routes/auth/requestHooks/redirectToChallenge.js +79 -0
  83. package/dist/routes/auth/requestHooks/resolveTwoFactorPageUrl.js +31 -0
  84. package/dist/routes/auth/resolveCookiePrefix.js +30 -0
  85. package/dist/routes/auth/sanitizeSessionResponse.js +27 -0
  86. package/dist/routes/auth/strategies/getAuthStrategies.js +1 -1
  87. package/dist/routes/connections/checkUnwalledReach.js +37 -0
  88. package/dist/routes/connections/getConnectionConfig.js +6 -1
  89. package/dist/routes/connections/resolveTenantPreflight.js +226 -0
  90. package/dist/routes/connections/resolveUnwalledReach.js +144 -0
  91. package/dist/routes/endpoints/acceptDetachedEndpoint.js +50 -0
  92. package/dist/routes/endpoints/authorizeApiEndpoint.js +13 -4
  93. package/dist/routes/endpoints/callEndpoint.js +23 -7
  94. package/dist/routes/endpoints/control/controlFor.js +4 -7
  95. package/dist/routes/endpoints/control/controlIf.js +4 -8
  96. package/dist/routes/endpoints/control/controlLog.js +6 -14
  97. package/dist/routes/endpoints/control/controlParallelFor.js +65 -29
  98. package/dist/routes/endpoints/control/controlReject.js +36 -16
  99. package/dist/routes/endpoints/control/controlReturn.js +32 -9
  100. package/dist/routes/endpoints/control/controlSetState.js +4 -8
  101. package/dist/routes/endpoints/control/controlSwitch.js +4 -8
  102. package/dist/routes/endpoints/control/controlThrow.js +20 -21
  103. package/dist/routes/endpoints/control/controlTry.js +30 -11
  104. package/dist/routes/endpoints/control/controlWhile.js +55 -0
  105. package/dist/routes/endpoints/control/handleControl.js +3 -1
  106. package/dist/routes/endpoints/createWebhookAnswer.js +78 -0
  107. package/dist/routes/endpoints/evaluateRoutineOperators.js +27 -0
  108. package/dist/routes/endpoints/forwardScheduledEndpoint.js +21 -15
  109. package/dist/routes/endpoints/getEndpointConfig.js +18 -8
  110. package/dist/routes/endpoints/getEnvironmentSchedules.js +5 -7
  111. package/dist/routes/endpoints/getSchemaValidator.js +39 -0
  112. package/dist/routes/endpoints/handleAgentCall.js +25 -9
  113. package/dist/routes/endpoints/handleAuthStep.js +214 -0
  114. package/dist/routes/endpoints/handleDescribeDynamicPolicy.js +64 -0
  115. package/dist/routes/endpoints/handleEndpointCall.js +46 -12
  116. package/dist/routes/endpoints/handleRenderNotification.js +29 -12
  117. package/dist/routes/endpoints/handleRequest.js +13 -6
  118. package/dist/routes/endpoints/handleValidateDynamic.js +106 -0
  119. package/dist/routes/endpoints/handleValidateSchema.js +22 -22
  120. package/dist/routes/endpoints/invokeEndpoint.js +27 -3
  121. package/dist/routes/endpoints/operatorScope.js +28 -0
  122. package/dist/routes/{auth/events/createEventPlugins.js → endpoints/parseWebhookBody.js} +10 -6
  123. package/dist/routes/endpoints/resolveCallBinding.js +91 -0
  124. package/dist/routes/endpoints/resolveCronEnvironment.js +7 -6
  125. package/dist/routes/endpoints/resolveStepOrganizationId.js +64 -0
  126. package/dist/routes/endpoints/runDetachedEndpoint.js +64 -18
  127. package/dist/routes/endpoints/runRoutine.js +64 -5
  128. package/dist/routes/endpoints/runScheduledEndpoint.js +19 -11
  129. package/dist/routes/endpoints/runWebhookEndpoint.js +70 -23
  130. package/dist/routes/endpoints/runWebhookVerify.js +129 -0
  131. package/dist/routes/endpoints/scheduleBackground.js +10 -0
  132. package/dist/routes/endpoints/test/runTest.js +48 -11
  133. package/dist/routes/endpoints/validatePayload.js +144 -0
  134. package/dist/routes/log/logClientError.js +7 -4
  135. package/dist/routes/mcp/createMcpServer.js +143 -64
  136. package/dist/routes/mcp/getMcpJwks.js +93 -0
  137. package/dist/routes/mcp/getMcpResourceBinding.js +41 -0
  138. package/dist/routes/mcp/getMcpUri.js +69 -0
  139. package/dist/routes/mcp/oauthResourceLifecycle.js +122 -0
  140. package/dist/routes/mcp/runAfterToolCall.js +50 -0
  141. package/dist/routes/notifications/resolveNotificationLinks.js +39 -11
  142. package/dist/routes/page/dynamic/checkDynamicContent.js +81 -0
  143. package/dist/routes/page/dynamic/checkLiteralContent.js +242 -0
  144. package/dist/routes/page/dynamic/createContentError.js +34 -0
  145. package/dist/routes/page/dynamic/flagIconsOutsidePage.js +39 -0
  146. package/dist/routes/page/dynamic/flagTypesOutsidePage.js +44 -0
  147. package/dist/routes/page/dynamic/getPropertiesSchemaErrors.js +35 -0
  148. package/dist/routes/page/dynamic/loadDynamicArtifacts.js +52 -0
  149. package/dist/routes/page/dynamic/policy/checkAction.js +204 -0
  150. package/dist/routes/page/dynamic/policy/checkEvents.js +163 -0
  151. package/dist/routes/page/dynamic/policy/checkPolicy.js +313 -0
  152. package/dist/routes/page/dynamic/policy/checkValue.js +214 -0
  153. package/dist/routes/page/dynamic/policy/collectUrlKinds.js +117 -0
  154. package/dist/routes/page/dynamic/policy/findEmbeddedUrls.js +54 -0
  155. package/dist/routes/page/dynamic/policy/findSerializerKeys.js +50 -0
  156. package/dist/routes/page/dynamic/policy/isAllowedUrl.js +53 -0
  157. package/dist/routes/{auth/createAdapter.js → page/dynamic/policy/isUnderState.js} +5 -9
  158. package/dist/routes/page/dynamic/policy/normalizeUrlText.js +25 -0
  159. package/dist/routes/page/dynamic/resolveDynamicContent.js +89 -37
  160. package/dist/routes/page/dynamic/unescapeOperators.js +21 -4
  161. package/dist/routes/page/dynamic/validateFragment.js +17 -58
  162. package/dist/routes/page/getPageConfig.js +76 -6
  163. package/dist/routes/request/authorizeRequest.js +21 -6
  164. package/dist/routes/request/callRequest.js +21 -3
  165. package/dist/routes/request/callRequestResolver.js +55 -6
  166. package/dist/routes/request/checkConnectionRead.js +1 -1
  167. package/dist/routes/request/checkConnectionWrite.js +1 -1
  168. package/dist/routes/request/createWalledResolver.js +75 -0
  169. package/dist/routes/request/evaluateOperators.js +8 -12
  170. package/dist/routes/request/getRequestConfig.js +19 -3
  171. package/dist/routes/request/maskCredentialProperties.js +34 -0
  172. package/dist/routes/request/resolveTenancy.js +223 -0
  173. package/dist/routes/request/validateSchemas.js +7 -6
  174. package/dist/routes/{auth/callbacks/createCallbackPlugins.js → rootConfig/getAppEvents.js} +7 -6
  175. package/dist/routes/rootConfig/getHomeAndMenus.js +23 -16
  176. package/dist/routes/{auth/events/createSessionEvent.js → rootConfig/getPagePaths.js} +16 -18
  177. package/dist/routes/rootConfig/getRootConfig.js +9 -3
  178. package/dist/routes/rootConfig/menus/filterMenuList.js +5 -2
  179. package/dist/routes/websocket/authorizeWebsocket.js +19 -4
  180. package/dist/routes/websocket/createChannelRegistry.js +39 -14
  181. package/dist/routes/websocket/createWebSocketConnection.js +162 -28
  182. package/dist/routes/websocket/getCallerFingerprint.js +31 -0
  183. package/dist/routes/websocket/prepareChannel.js +29 -1
  184. package/dist/routes/websocket/resolveWebsocketCaller.js +40 -0
  185. package/dist/test/testContext.js +16 -11
  186. package/package.json +28 -12
  187. package/dist/context/createAuthorize.js +0 -45
  188. package/dist/response/omitErrorProps.js +0 -51
  189. package/dist/routes/auth/callbacks/addUserFieldsToToken.js +0 -30
  190. package/dist/routes/auth/callbacks/createCallbacks.js +0 -46
  191. package/dist/routes/auth/callbacks/createJWTCallback.js +0 -77
  192. package/dist/routes/auth/callbacks/createRedirectCallback.js +0 -41
  193. package/dist/routes/auth/callbacks/createSessionCallback.js +0 -81
  194. package/dist/routes/auth/callbacks/createSignInCallback.js +0 -40
  195. package/dist/routes/auth/callbacks/validateSessionRoles.js +0 -33
  196. package/dist/routes/auth/createLogger.js +0 -35
  197. package/dist/routes/auth/createPrefixedCookies.js +0 -52
  198. package/dist/routes/auth/createProviders.js +0 -25
  199. package/dist/routes/auth/events/createCreateUserEvent.js +0 -41
  200. package/dist/routes/auth/events/createEvents.js +0 -56
  201. package/dist/routes/auth/events/createLinkAccountEvent.js +0 -43
  202. package/dist/routes/auth/events/createSignInEvent.js +0 -46
  203. package/dist/routes/auth/events/createSignOutEvent.js +0 -43
  204. package/dist/routes/auth/events/createUpdateUserEvent.js +0 -41
  205. package/dist/routes/auth/getAuthConfig.js +0 -86
  206. package/dist/routes/auth/resolveCookies.js +0 -37
  207. package/dist/routes/log/formatValidationError.js +0 -31
  208. /package/dist/{routes/notifications → email}/resolveThemeLogo.js +0 -0
@@ -0,0 +1,242 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { ConfigError } from '@lowdefy/errors';
16
+ import { type } from '@lowdefy/helpers';
17
+ import { findDataOrigin, getPossibleOperators } from '@lowdefy/operators';
18
+ function joinPath(path, key) {
19
+ return `${path}.${key}`;
20
+ }
21
+ // An object the client would run as an operator must have been written as one.
22
+ // The per-result scan refuses operators in data as it is returned; data merged
23
+ // with config afterwards (_object.assign, a key set to undefined) can still end
24
+ // up with an operator key alone, so the finished content is checked here.
25
+ function findDataOperator({ blocks, literalData }) {
26
+ const pending = [
27
+ {
28
+ node: blocks,
29
+ path: 'blocks'
30
+ }
31
+ ];
32
+ while(pending.length > 0){
33
+ const { node, path } = pending.pop();
34
+ if (type.isObject(node)) {
35
+ const [possible] = getPossibleOperators({
36
+ value: node,
37
+ operators: literalData.clientOperators
38
+ });
39
+ const origin = possible ? findDataOrigin({
40
+ literalData,
41
+ value: node
42
+ }) : null;
43
+ if (origin !== null) {
44
+ return {
45
+ operator: possible.operator,
46
+ origin,
47
+ path
48
+ };
49
+ }
50
+ }
51
+ if (type.isObject(node) || type.isArray(node)) {
52
+ const keys = Object.keys(node);
53
+ for(let index = keys.length - 1; index >= 0; index -= 1){
54
+ pending.push({
55
+ node: node[keys[index]],
56
+ path: joinPath(path, keys[index])
57
+ });
58
+ }
59
+ }
60
+ }
61
+ return null;
62
+ }
63
+ // The items a block, an event map, an action list or an action holds, in the
64
+ // order they are checked: a block's events, skeleton (rendered through
65
+ // LoadingBlock, like content) and child block lists; control branches (:if,
66
+ // :switch) of an action list. A :return item holds no action.
67
+ function getStructureChildren({ kind, node, path }) {
68
+ if (kind === 'block') {
69
+ const children = [
70
+ {
71
+ kind: 'events',
72
+ node: node.events,
73
+ path: `${path}.events`
74
+ },
75
+ {
76
+ kind: 'block',
77
+ node: node.skeleton,
78
+ path: `${path}.skeleton`
79
+ }
80
+ ];
81
+ if (type.isArray(node.blocks)) {
82
+ children.push({
83
+ kind: 'blocks',
84
+ node: node.blocks,
85
+ path: `${path}.blocks`
86
+ });
87
+ }
88
+ [
89
+ 'areas',
90
+ 'slots'
91
+ ].forEach((containerKey)=>{
92
+ if (!type.isObject(node[containerKey])) return;
93
+ Object.keys(node[containerKey]).forEach((name)=>{
94
+ children.push({
95
+ kind: 'blocks',
96
+ node: node[containerKey][name]?.blocks,
97
+ path: `${path}.${containerKey}.${name}.blocks`
98
+ });
99
+ });
100
+ });
101
+ return children;
102
+ }
103
+ if (kind === 'events') {
104
+ return Object.keys(node).flatMap((eventName)=>{
105
+ const event = node[eventName];
106
+ const eventPath = joinPath(path, eventName);
107
+ if (type.isArray(event)) {
108
+ return [
109
+ {
110
+ kind: 'actions',
111
+ node: event,
112
+ path: eventPath
113
+ }
114
+ ];
115
+ }
116
+ return [
117
+ {
118
+ kind: 'actions',
119
+ node: event?.try,
120
+ path: `${eventPath}.try`
121
+ },
122
+ {
123
+ kind: 'actions',
124
+ node: event?.catch,
125
+ path: `${eventPath}.catch`
126
+ }
127
+ ];
128
+ });
129
+ }
130
+ if (kind === 'blocks' || kind === 'actions') {
131
+ const itemKind = kind === 'blocks' ? 'block' : 'action';
132
+ return node.map((item, index)=>({
133
+ kind: itemKind,
134
+ node: item,
135
+ path: joinPath(path, index)
136
+ }));
137
+ }
138
+ if (':if' in node) {
139
+ return [
140
+ {
141
+ kind: 'actions',
142
+ node: node[':then'],
143
+ path: `${path}.:then`
144
+ },
145
+ {
146
+ kind: 'actions',
147
+ node: node[':else'],
148
+ path: `${path}.:else`
149
+ }
150
+ ];
151
+ }
152
+ if (':switch' in node) {
153
+ const cases = type.isArray(node[':switch']) ? node[':switch'] : [];
154
+ return [
155
+ ...cases.map((caseObject, caseIndex)=>({
156
+ kind: 'actions',
157
+ node: caseObject?.[':then'],
158
+ path: `${path}.:switch.${caseIndex}.:then`
159
+ })),
160
+ {
161
+ kind: 'actions',
162
+ node: node[':default'],
163
+ path: `${path}.:default`
164
+ }
165
+ ];
166
+ }
167
+ return [];
168
+ }
169
+ function isStructureNode({ kind, node }) {
170
+ if (kind === 'blocks' || kind === 'actions') {
171
+ return type.isArray(node);
172
+ }
173
+ return type.isObject(node);
174
+ }
175
+ // The first block or action in the content that is data, walked with an
176
+ // explicit stack.
177
+ function findDataStructure({ blocks, literalData }) {
178
+ const pending = [
179
+ {
180
+ kind: 'blocks',
181
+ node: blocks,
182
+ path: 'blocks'
183
+ }
184
+ ];
185
+ while(pending.length > 0){
186
+ const item = pending.pop();
187
+ if (isStructureNode(item)) {
188
+ const isAction = item.kind === 'action' && !(':if' in item.node) && !(':switch' in item.node) && !(':return' in item.node);
189
+ if (item.kind === 'block' || isAction) {
190
+ const origin = findDataOrigin({
191
+ literalData,
192
+ value: item.node
193
+ });
194
+ if (origin !== null) {
195
+ return {
196
+ what: item.kind === 'block' ? 'Block' : 'Action',
197
+ path: item.path,
198
+ origin
199
+ };
200
+ }
201
+ }
202
+ const children = getStructureChildren(item);
203
+ for(let index = children.length - 1; index >= 0; index -= 1){
204
+ pending.push(children[index]);
205
+ }
206
+ }
207
+ }
208
+ return null;
209
+ }
210
+ // Checks the content a Dynamic block's endpoint returns, as :return built it.
211
+ // Data may fill values inside the blocks the :return writes, but without a
212
+ // dynamic blocks policy it may not be a block or an action itself: a block or
213
+ // action that is data an operator returned, or a copy a copying read made of
214
+ // it, is refused. Config is never matched by content. With a policy the policy
215
+ // checks every block and action at page get instead. Under both, data may not
216
+ // become an operator.
217
+ function checkLiteralContent({ blocks, configKey, literalData }) {
218
+ if (!type.isArray(blocks)) {
219
+ return;
220
+ }
221
+ if (type.isNone(literalData.policyId)) {
222
+ const structure = findDataStructure({
223
+ blocks,
224
+ literalData
225
+ });
226
+ if (structure) {
227
+ throw new ConfigError(`${structure.what} at "${structure.path}" is data returned by "${structure.origin}". A Dynamic block without a dynamic blocks policy renders only blocks and actions written in its endpoint's :return config. Map data into blocks inside :return, or check stored blocks with a ValidateDynamic step under a policy.`, {
228
+ configKey
229
+ });
230
+ }
231
+ }
232
+ const operator = findDataOperator({
233
+ blocks,
234
+ literalData
235
+ });
236
+ if (operator) {
237
+ throw new ConfigError(`Data returned by "${operator.origin}" can run as the operator "${operator.operator}" at "${operator.path}" once it is merged with other config. Operators in endpoint data do not run in Dynamic block content.`, {
238
+ configKey
239
+ });
240
+ }
241
+ }
242
+ export default checkLiteralContent;
@@ -0,0 +1,34 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { ConfigError } from '@lowdefy/errors';
16
+ // Content without a policy fails on the build or fragment check's own error,
17
+ // which already names the Dynamic block. Policy violations are listed with the
18
+ // path and rule of each, and kept on the error for logging.
19
+ function createContentError({ block, errors, pageId, policy }) {
20
+ if (policy === null) {
21
+ return errors[0].cause;
22
+ }
23
+ const lines = errors.map((error)=>` - ${error.path}: ${error.message} [${error.rule}]`);
24
+ const contentError = new ConfigError(`Dynamic block "${block.blockId}" on page "${pageId}" resolved content violates dynamic blocks policy "${policy.id}":\n${lines.join('\n')}`, {
25
+ configKey: block['~k']
26
+ });
27
+ contentError.policyErrors = errors.map(({ path, rule, message })=>({
28
+ path,
29
+ rule,
30
+ message
31
+ }));
32
+ return contentError;
33
+ }
34
+ export default createContentError;
@@ -0,0 +1,39 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ // The production client loads each page's own icons. Dynamic content can name
16
+ // any icon the app bundles (iconImports), so when the resolved page names one
17
+ // its page set lacks, the client loads the app-wide icons before the first
18
+ // render instead of drawing a placeholder until they arrive. The scan is the
19
+ // build's own discovery scan; the static part of the page adds nothing, since
20
+ // its icons are in the page set already. Unlike types, icons outside the page
21
+ // are expected here - content is only known at runtime - so there is no warning.
22
+ function flagIconsOutsidePage({ collectIconNames, iconImports, pageConfig, pageTypeSets }) {
23
+ // Dev builds write null: the dev client bundles every icon the app names.
24
+ if (pageTypeSets === null) {
25
+ return;
26
+ }
27
+ const pageIcons = new Set(pageTypeSets[pageConfig.pageId].icons);
28
+ const appIcons = new Set(iconImports);
29
+ const names = collectIconNames({
30
+ text: JSON.stringify(pageConfig)
31
+ });
32
+ for (const name of names){
33
+ if (appIcons.has(name) && !pageIcons.has(name)) {
34
+ pageConfig.loadAllIcons = true;
35
+ return;
36
+ }
37
+ }
38
+ }
39
+ export default flagIconsOutsidePage;
@@ -0,0 +1,44 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ // The production client loads each page's own client types. A fragment may use
16
+ // any type the app bundles (buildDynamicBlocks checks app-wide membership), so a
17
+ // fragment that reaches outside its page's types asks the client for the full
18
+ // set, and the warning names what the Dynamic block should declare.
19
+ function flagTypesOutsidePage(context, { pageConfig, pageTypeSets, usedTypes }) {
20
+ // Dev builds write null: the dev client bundles every installed type.
21
+ if (pageTypeSets === null) {
22
+ return;
23
+ }
24
+ const pageTypeSet = pageTypeSets[pageConfig.pageId];
25
+ const outside = [];
26
+ Object.entries(usedTypes).forEach(([category, used])=>{
27
+ const pageTypes = new Set(pageTypeSet[category]);
28
+ used.forEach((typeName)=>{
29
+ if (!pageTypes.has(typeName)) {
30
+ outside.push(typeName);
31
+ }
32
+ });
33
+ });
34
+ if (outside.length === 0) {
35
+ return;
36
+ }
37
+ pageConfig.loadAllTypes = true;
38
+ context.logger.warn({
39
+ event: 'dynamic_types_outside_page',
40
+ pageId: pageConfig.pageId,
41
+ types: outside
42
+ }, `Dynamic content on page "${pageConfig.pageId}" uses types not declared in its Dynamic blocks' properties.types: ${outside.join(', ')}. The page loads the full client type set.`);
43
+ }
44
+ export default flagTypesOutsidePage;
@@ -0,0 +1,35 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { getConfigSchemaErrors } from '@lowdefy/ajv';
16
+ import { type } from '@lowdefy/helpers';
17
+ // Returns "properties/... message" strings for a block's schema violations,
18
+ // judged as the build judges config: see getConfigSchemaErrors.
19
+ function getPropertiesSchemaErrors({ block, blockSchemas }) {
20
+ const properties = block.properties;
21
+ if (!type.isObject(properties)) {
22
+ return [];
23
+ }
24
+ // Block schemas validate the whole pre-build block shape; the plugin's
25
+ // properties schema sits at schema.properties.properties.
26
+ const propertiesSchema = blockSchemas[block.type]?.properties?.properties;
27
+ if (type.isNone(propertiesSchema)) {
28
+ return [];
29
+ }
30
+ return getConfigSchemaErrors({
31
+ schema: propertiesSchema,
32
+ data: properties
33
+ }).map((error)=>`properties${error.instancePath || ''} ${error.message}`);
34
+ }
35
+ export default getPropertiesSchemaErrors;
@@ -0,0 +1,52 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ // Build artifacts every Dynamic content check reads. readConfigFile caches
16
+ // files per server lifetime, and the build package loads only when a page or
17
+ // routine first needs it.
18
+ // readConfigFile returns the same cached array per server lifetime, so the set
19
+ // is built once.
20
+ const clientOperatorSets = new WeakMap();
21
+ function getClientOperatorSet(names) {
22
+ if (names === null) {
23
+ return null;
24
+ }
25
+ if (!clientOperatorSets.has(names)) {
26
+ clientOperatorSets.set(names, new Set(names));
27
+ }
28
+ return clientOperatorSets.get(names);
29
+ }
30
+ async function loadDynamicArtifacts(context) {
31
+ const [{ default: buildDynamicBlocks }, types, blockMetas, blockSchemas, clientOperators, dynamicPolicies, routes] = await Promise.all([
32
+ import('@lowdefy/build/dynamic'),
33
+ context.readConfigFile('types.json'),
34
+ context.readConfigFile('plugins/blockMetas.json'),
35
+ context.readConfigFile('plugins/blockSchemas.json'),
36
+ context.readConfigFile('plugins/clientOperators.json'),
37
+ context.readConfigFile('dynamicPolicies.json'),
38
+ context.readConfigFile('routes.json')
39
+ ]);
40
+ return {
41
+ blockMetas: blockMetas ?? {},
42
+ blockSchemas: blockSchemas ?? {},
43
+ buildDynamicBlocks,
44
+ // Every client operator name the app knows, so only keys the client would
45
+ // run count as operators. Without the list every operator-shaped key counts.
46
+ clientOperators: getClientOperatorSet(clientOperators),
47
+ dynamicPolicies: dynamicPolicies ?? {},
48
+ routes,
49
+ types: types ?? {}
50
+ };
51
+ }
52
+ export default loadDynamicArtifacts;
@@ -0,0 +1,204 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { type } from '@lowdefy/helpers';
16
+ import { getPossibleOperators } from '@lowdefy/operators';
17
+ import checkValue from './checkValue.js';
18
+ import isUnderState from './isUnderState.js';
19
+ function isLiteralObject({ value, walk }) {
20
+ return type.isObject(value) && getPossibleOperators({
21
+ value,
22
+ operators: walk.clientOperators
23
+ }).length === 0;
24
+ }
25
+ function literalParamsError({ path, action, policy }) {
26
+ return {
27
+ path,
28
+ rule: 'policy.literal',
29
+ message: `${action} params must be literal under dynamic blocks policy "${policy.id}", so the policy can see their target.`
30
+ };
31
+ }
32
+ function checkLink({ params, path, walk }) {
33
+ const { errors, policy } = walk;
34
+ if (type.isString(params)) {
35
+ // The string form is the pageId shorthand (actions-core Link).
36
+ if (!policy.links.pages.includes(params)) {
37
+ errors.push({
38
+ path,
39
+ rule: 'policy.links',
40
+ message: `Page "${params}" is not in dynamic blocks policy "${policy.id}" links.pages.`
41
+ });
42
+ }
43
+ return;
44
+ }
45
+ if (!isLiteralObject({
46
+ value: params,
47
+ walk
48
+ })) {
49
+ errors.push(literalParamsError({
50
+ path,
51
+ action: 'Link',
52
+ policy
53
+ }));
54
+ return;
55
+ }
56
+ if (getPossibleOperators({
57
+ value: params.home,
58
+ operators: walk.clientOperators
59
+ }).length > 0) {
60
+ errors.push(literalParamsError({
61
+ path: `${path}.home`,
62
+ action: 'Link',
63
+ policy
64
+ }));
65
+ }
66
+ }
67
+ function checkCallApi({ params, path, walk }) {
68
+ const { errors, policy } = walk;
69
+ if (!isLiteralObject({
70
+ value: params,
71
+ walk
72
+ }) || !type.isString(params.endpointId)) {
73
+ errors.push(literalParamsError({
74
+ path,
75
+ action: 'CallAPI',
76
+ policy
77
+ }));
78
+ return;
79
+ }
80
+ if (!policy.endpoints.includes(params.endpointId)) {
81
+ errors.push({
82
+ path: `${path}.endpointId`,
83
+ rule: 'policy.endpoints',
84
+ message: `Endpoint "${params.endpointId}" is not in dynamic blocks policy "${policy.id}" endpoints.`
85
+ });
86
+ }
87
+ }
88
+ // Request params take the forms engine/src/Requests.js reads, except { all },
89
+ // which would call every request on the page.
90
+ function getRequestIds({ params, walk }) {
91
+ if (type.isString(params)) return [
92
+ params
93
+ ];
94
+ if (type.isArray(params)) return params;
95
+ const literal = isLiteralObject({
96
+ value: params,
97
+ walk
98
+ });
99
+ if (literal && type.isString(params.requestId)) return [
100
+ params.requestId
101
+ ];
102
+ if (literal && type.isArray(params.requestIds)) return params.requestIds;
103
+ return null;
104
+ }
105
+ function checkRequest({ params, path, walk }) {
106
+ const { errors, policy } = walk;
107
+ const requestIds = getRequestIds({
108
+ params,
109
+ walk
110
+ });
111
+ if (requestIds === null || !requestIds.every((requestId)=>type.isString(requestId))) {
112
+ errors.push({
113
+ path,
114
+ rule: 'policy.requests',
115
+ message: `Request params must be literal request ids under dynamic blocks policy "${policy.id}". "all" is not allowed.`
116
+ });
117
+ return;
118
+ }
119
+ requestIds.forEach((requestId)=>{
120
+ if (!policy.requests.includes(requestId)) {
121
+ errors.push({
122
+ path,
123
+ rule: 'policy.requests',
124
+ message: `Request "${requestId}" is not in dynamic blocks policy "${policy.id}" requests.`
125
+ });
126
+ }
127
+ });
128
+ }
129
+ function checkSetState({ params, path, walk }) {
130
+ const { errors, policy } = walk;
131
+ if (!isLiteralObject({
132
+ value: params,
133
+ walk
134
+ })) {
135
+ errors.push(literalParamsError({
136
+ path,
137
+ action: 'SetState',
138
+ policy
139
+ }));
140
+ return;
141
+ }
142
+ if (type.isNone(policy.state)) {
143
+ return;
144
+ }
145
+ Object.keys(params).forEach((key)=>{
146
+ if (key.startsWith('~')) return;
147
+ if (!isUnderState({
148
+ key,
149
+ state: policy.state
150
+ })) {
151
+ errors.push({
152
+ path: `${path}.${key}`,
153
+ rule: 'policy.state',
154
+ message: `SetState key "${key}" is outside "${policy.state}", the state dynamic blocks policy "${policy.id}" allows.`
155
+ });
156
+ }
157
+ });
158
+ }
159
+ const ACTION_RULES = {
160
+ CallAPI: checkCallApi,
161
+ Link: checkLink,
162
+ Request: checkRequest,
163
+ SetState: checkSetState
164
+ };
165
+ function checkAction({ action, path, walk }) {
166
+ const { errors, policy } = walk;
167
+ if (!isLiteralObject({
168
+ value: action,
169
+ walk
170
+ }) || !type.isString(action.id) || !type.isString(action.type)) {
171
+ errors.push({
172
+ path,
173
+ rule: 'policy.structure',
174
+ message: 'Actions must be objects with a literal string "id" and "type".'
175
+ });
176
+ return;
177
+ }
178
+ if (walk.actionIds.has(action.id)) {
179
+ errors.push({
180
+ path: `${path}.id`,
181
+ rule: 'policy.structure',
182
+ message: `Action id "${action.id}" is used more than once in this event.`
183
+ });
184
+ }
185
+ walk.actionIds.add(action.id);
186
+ if (!policy.actions.includes(action.type)) {
187
+ errors.push({
188
+ path: `${path}.type`,
189
+ rule: 'policy.actions',
190
+ message: `Action type "${action.type}" is not in dynamic blocks policy "${policy.id}" actions.`
191
+ });
192
+ }
193
+ ACTION_RULES[action.type]?.({
194
+ params: action.params,
195
+ path: `${path}.params`,
196
+ walk
197
+ });
198
+ checkValue({
199
+ value: action,
200
+ path,
201
+ walk
202
+ });
203
+ }
204
+ export default checkAction;