@lowdefy/api 7.3.0 → 7.4.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.
@@ -0,0 +1,30 @@
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
+ */ const HTML_TAG = /<[a-zA-Z/!]/;
16
+ // The html rule: without policy.html, no string the client may draw as HTML
17
+ // holds tag syntax. A string drawn as escaped text shows tag syntax as written.
18
+ // subject names what the string is in the message ("String" or "Key").
19
+ function checkHtmlTag({ value, path, textKind, subject, walk }) {
20
+ const { errors, policy } = walk;
21
+ if (policy.html || textKind === 'text' || !HTML_TAG.test(value)) {
22
+ return;
23
+ }
24
+ errors.push({
25
+ path,
26
+ rule: 'policy.html',
27
+ message: `${subject} contains HTML tag syntax. Dynamic blocks policy "${policy.id}" does not allow HTML.`
28
+ });
29
+ }
30
+ export default checkHtmlTag;
@@ -0,0 +1,41 @@
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 checkHtmlTag from './checkHtmlTag.js';
16
+ import checkTextUrls from './checkTextUrls.js';
17
+ // A block can draw an object key, not only a value: Descriptions draws the keys
18
+ // of an items object as labels, Tabs and Collapse draw slot names as titles,
19
+ // both through renderHtml. No schema marks how a key is drawn, so a key keeps
20
+ // every check a string no schema marks gets: the html rule and every URL scan.
21
+ // path is the path of the key's value, which names the key.
22
+ function checkKey({ key, path, walk }) {
23
+ // Data is stored or sent, never drawn or followed by the content.
24
+ if (walk.isData) {
25
+ return;
26
+ }
27
+ checkHtmlTag({
28
+ value: key,
29
+ path,
30
+ textKind: null,
31
+ subject: 'Key',
32
+ walk
33
+ });
34
+ checkTextUrls({
35
+ value: key,
36
+ path,
37
+ textKind: null,
38
+ walk
39
+ });
40
+ }
41
+ export default checkKey;
@@ -16,6 +16,7 @@
16
16
  import { getPossibleOperators, isNestedDeeperThan, MAX_DATA_DEPTH } from '@lowdefy/operators';
17
17
  import getPropertiesSchemaErrors from '../getPropertiesSchemaErrors.js';
18
18
  import checkEvents from './checkEvents.js';
19
+ import checkKey from './checkKey.js';
19
20
  import checkValue from './checkValue.js';
20
21
  import collectSchemaKinds from './collectSchemaKinds.js';
21
22
  import findDataRows from './findDataRows.js';
@@ -239,6 +240,20 @@ function checkBlock({ block, path, depth, walk }) {
239
240
  walk
240
241
  });
241
242
  }
243
+ // Tabs and Collapse draw a slot name as its title when no tabs or panels are given.
244
+ [
245
+ 'areas',
246
+ 'slots'
247
+ ].forEach((containerKey)=>{
248
+ Object.keys(block[containerKey] ?? {}).forEach((name)=>{
249
+ if (name.startsWith('~')) return;
250
+ checkKey({
251
+ key: name,
252
+ path: `${path}.${containerKey}.${name}`,
253
+ walk
254
+ });
255
+ });
256
+ });
242
257
  childBlockLists(block).forEach((list)=>{
243
258
  // eslint-disable-next-line no-use-before-define
244
259
  checkBlockList({
@@ -0,0 +1,92 @@
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 findEmbeddedUrls from './findEmbeddedUrls.js';
16
+ import findHtmlUrls from './findHtmlUrls.js';
17
+ import isAllowedUrl from './isAllowedUrl.js';
18
+ // Which URLs a string can carry depends on how its block draws it (textKind,
19
+ // from the block schema). Text and HTML a block draws are no link and render no
20
+ // markdown, so a whole value, a markdown target or a protocol-relative word in
21
+ // them is words. HTML can carry a style, a link or a source attribute only when
22
+ // the policy allows tags (findHtmlUrls). A string no schema marks may be drawn
23
+ // as markdown, a style or a link the schema does not mark, so every scan applies.
24
+ function getUrlScans({ textKind, policy }) {
25
+ if (textKind === 'text') {
26
+ return {
27
+ wholeValue: false,
28
+ markdown: false,
29
+ css: false
30
+ };
31
+ }
32
+ if (textKind === 'html') {
33
+ return {
34
+ wholeValue: false,
35
+ markdown: false,
36
+ css: policy.html === true
37
+ };
38
+ }
39
+ return {
40
+ wholeValue: true,
41
+ markdown: true,
42
+ css: true
43
+ };
44
+ }
45
+ // Searches a string that is not itself a URL value for the URLs it can make
46
+ // the client load or navigate to, and reports each one the policy does not allow.
47
+ function checkTextUrls({ value, path, textKind, walk }) {
48
+ const { errors, policy, routes } = walk;
49
+ findEmbeddedUrls({
50
+ value,
51
+ ...getUrlScans({
52
+ textKind,
53
+ policy
54
+ })
55
+ }).forEach((url)=>{
56
+ if (!isAllowedUrl({
57
+ value: url,
58
+ policy,
59
+ navigation: false,
60
+ schemeless: false,
61
+ routes
62
+ })) {
63
+ errors.push({
64
+ path,
65
+ rule: 'policy.urls',
66
+ message: `URL "${url}" is not an origin dynamic blocks policy "${policy.id}" allows.`
67
+ });
68
+ }
69
+ });
70
+ // With tags allowed, any string a block may draw as HTML can carry a link or
71
+ // a source in an attribute.
72
+ if (policy.html === true && textKind !== 'text') {
73
+ findHtmlUrls({
74
+ value
75
+ }).forEach(({ url, navigation })=>{
76
+ if (!isAllowedUrl({
77
+ value: url,
78
+ policy,
79
+ navigation,
80
+ schemeless: false,
81
+ routes
82
+ })) {
83
+ errors.push({
84
+ path,
85
+ rule: 'policy.urls',
86
+ message: `HTML attribute URL "${url}" is not a page or origin dynamic blocks policy "${policy.id}" allows.`
87
+ });
88
+ }
89
+ });
90
+ }
91
+ }
92
+ export default checkTextUrls;
@@ -14,8 +14,9 @@
14
14
  limitations under the License.
15
15
  */ import { type } from '@lowdefy/helpers';
16
16
  import { getPossibleOperators } from '@lowdefy/operators';
17
- import findEmbeddedUrls from './findEmbeddedUrls.js';
18
- import findHtmlUrls from './findHtmlUrls.js';
17
+ import checkHtmlTag from './checkHtmlTag.js';
18
+ import checkKey from './checkKey.js';
19
+ import checkTextUrls from './checkTextUrls.js';
19
20
  import isAllowedUrl from './isAllowedUrl.js';
20
21
  import isUnderState from './isUnderState.js';
21
22
  // How the client uses a URL value, for values no schema marks. A block schema
@@ -35,7 +36,6 @@ const URL_KEY_KINDS = {
35
36
  srcSet: 'srcSet',
36
37
  url: 'url'
37
38
  };
38
- const HTML_TAG = /<[a-zA-Z/!]/;
39
39
  function joinPath(path, key) {
40
40
  return path ? `${path}.${key}` : `${key}`;
41
41
  }
@@ -52,34 +52,6 @@ function listUrls({ value, urlKind }) {
52
52
  value
53
53
  ];
54
54
  }
55
- // Which URLs a string can carry depends on how its block draws it (textKind,
56
- // from the block schema). A value is a URL where it is used as a link (a URL
57
- // key or urlKind mark, judged whole above); text and HTML a block draws are no
58
- // link and render no markdown, so a whole value, a markdown target or a
59
- // protocol-relative word in them is words. HTML can carry a style, a link or a
60
- // source attribute only when the policy allows tags (findHtmlUrls). A value no schema marks may be drawn as
61
- // markdown, a style or a link the schema does not mark, so every scan applies.
62
- function getUrlScans({ textKind, policy }) {
63
- if (textKind === 'text') {
64
- return {
65
- wholeValue: false,
66
- markdown: false,
67
- css: false
68
- };
69
- }
70
- if (textKind === 'html') {
71
- return {
72
- wholeValue: false,
73
- markdown: false,
74
- css: policy.html === true
75
- };
76
- }
77
- return {
78
- wholeValue: true,
79
- markdown: true,
80
- css: true
81
- };
82
- }
83
55
  function checkString({ value, key, path, walk }) {
84
56
  const { errors, policy, routes } = walk;
85
57
  // Data is stored or sent, never drawn or followed by the content, so every
@@ -88,14 +60,13 @@ function checkString({ value, key, path, walk }) {
88
60
  return;
89
61
  }
90
62
  const textKind = walk.textKinds?.get(path) ?? null;
91
- // A string drawn as escaped text shows tag syntax as written.
92
- if (!policy.html && textKind !== 'text' && HTML_TAG.test(value)) {
93
- errors.push({
94
- path,
95
- rule: 'policy.html',
96
- message: `String contains HTML tag syntax. Dynamic blocks policy "${policy.id}" does not allow HTML.`
97
- });
98
- }
63
+ checkHtmlTag({
64
+ value,
65
+ path,
66
+ textKind,
67
+ subject: 'String',
68
+ walk
69
+ });
99
70
  if (key === 'pageId' && !policy.links.pages.includes(value)) {
100
71
  errors.push({
101
72
  path,
@@ -133,48 +104,12 @@ function checkString({ value, key, path, walk }) {
133
104
  return;
134
105
  }
135
106
  // A URL value was judged whole above; other strings are searched.
136
- findEmbeddedUrls({
107
+ checkTextUrls({
137
108
  value,
138
- ...getUrlScans({
139
- textKind,
140
- policy
141
- })
142
- }).forEach((url)=>{
143
- if (!isAllowedUrl({
144
- value: url,
145
- policy,
146
- navigation: false,
147
- schemeless: false,
148
- routes
149
- })) {
150
- errors.push({
151
- path,
152
- rule: 'policy.urls',
153
- message: `URL "${url}" is not an origin dynamic blocks policy "${policy.id}" allows.`
154
- });
155
- }
109
+ path,
110
+ textKind,
111
+ walk
156
112
  });
157
- // With tags allowed, any string a block may draw as HTML can carry a link or
158
- // a source in an attribute.
159
- if (policy.html === true && textKind !== 'text') {
160
- findHtmlUrls({
161
- value
162
- }).forEach(({ url, navigation })=>{
163
- if (!isAllowedUrl({
164
- value: url,
165
- policy,
166
- navigation,
167
- schemeless: false,
168
- routes
169
- })) {
170
- errors.push({
171
- path,
172
- rule: 'policy.urls',
173
- message: `HTML attribute URL "${url}" is not a page or origin dynamic blocks policy "${policy.id}" allows.`
174
- });
175
- }
176
- });
177
- }
178
113
  }
179
114
  // A _state read may only name a literal key under the policy's state, so
180
115
  // content cannot read other page state and send it on (a CallAPI payload).
@@ -294,10 +229,16 @@ function checkValue({ value, key = null, path, walk: parentWalk }) {
294
229
  });
295
230
  Object.keys(value).forEach((childKey)=>{
296
231
  if (childKey.startsWith('~')) return;
232
+ const childPath = joinPath(path, childKey);
233
+ checkKey({
234
+ key: childKey,
235
+ path: childPath,
236
+ walk
237
+ });
297
238
  checkValue({
298
239
  value: value[childKey],
299
240
  key: childKey,
300
- path: joinPath(path, childKey),
241
+ path: childPath,
301
242
  walk
302
243
  });
303
244
  });
@@ -20,8 +20,10 @@ import createEvaluateOperators from '../../context/createEvaluateOperators.js';
20
20
  import createWireProjection from '../../response/createWireProjection.js';
21
21
  import prepareChannel from './prepareChannel.js';
22
22
  import wrapResolverError from '../request/wrapResolverError.js';
23
- const MAX_RETRIES = 5;
23
+ // A failed source is restarted for as long as its channel has subscribers, 1 s
24
+ // after the first failure, doubling up to 60 s apart.
24
25
  const RETRY_BASE_MS = 1000;
26
+ const RETRY_CAP_MS = 60 * 1000;
25
27
  const HEALTHY_RESET_MS = 60 * 1000;
26
28
  // One registry per server process. The unit of sharing is the evaluated
27
29
  // subscription: subscribers whose evaluated connection and websocket
@@ -96,8 +98,8 @@ function createChannelRegistry() {
96
98
  channel.abortController?.abort();
97
99
  channels.delete(channel.key);
98
100
  }
99
- function handleResolverError({ channel, error, startedAt }) {
100
- if (channel.abortController?.signal.aborted) {
101
+ function handleResolverError({ channel, error, run }) {
102
+ if (run.abortController.signal.aborted) {
101
103
  // Errors thrown while winding down are expected (closed cursors, etc).
102
104
  channel.logger.debug({
103
105
  event: 'ws_source_stopped_error_ignored',
@@ -105,6 +107,8 @@ function createChannelRegistry() {
105
107
  }, error.message);
106
108
  return;
107
109
  }
110
+ // Anything the failed run left behind can no longer publish or report it is ready.
111
+ run.abortController.abort();
108
112
  const wrapped = wrapSourceError({
109
113
  channel,
110
114
  error
@@ -112,21 +116,27 @@ function createChannelRegistry() {
112
116
  channel.logger.debug({
113
117
  err: wrapped
114
118
  }, wrapped.message);
115
- channel.context.handleError(wrapped);
116
- broadcastError(channel, wrapped);
117
- // A run that stayed healthy resets the backoff window.
118
- if (Date.now() - startedAt > HEALTHY_RESET_MS) {
119
+ // A run that stayed ready this long was healthy, and resets the backoff. Time
120
+ // spent starting does not count: a source waiting on an unreachable database
121
+ // can take a minute to fail.
122
+ if (run.readyAt !== null && Date.now() - run.readyAt > HEALTHY_RESET_MS) {
119
123
  channel.retryCount = 0;
120
124
  }
121
- if (channel.retryCount >= MAX_RETRIES) {
122
- channel.logger.error({
123
- event: 'ws_source_max_retries',
124
- websocketId: channel.websocketId
125
- });
126
- removeChannel(channel);
127
- return;
125
+ const delay = Math.min(RETRY_BASE_MS * 2 ** channel.retryCount, RETRY_CAP_MS);
126
+ // An outage lasts from the source's first failure until it is ready again, and
127
+ // is reported once: the restarts that fail during it are warnings.
128
+ if (channel.failing) {
129
+ channel.logger.warn({
130
+ event: 'ws_source_restart_failed',
131
+ websocketId: channel.websocketId,
132
+ retryCount: channel.retryCount,
133
+ retryInMs: delay
134
+ }, wrapped.message);
135
+ } else {
136
+ channel.failing = true;
137
+ channel.context.handleError(wrapped);
128
138
  }
129
- const delay = RETRY_BASE_MS * 2 ** channel.retryCount;
139
+ broadcastError(channel, wrapped);
130
140
  channel.retryCount += 1;
131
141
  channel.restartTimer = setTimeout(()=>{
132
142
  channel.restartTimer = null;
@@ -146,11 +156,41 @@ function createChannelRegistry() {
146
156
  function startResolver(channel) {
147
157
  const abortController = new AbortController();
148
158
  channel.abortController = abortController;
149
- const startedAt = Date.now();
159
+ const run = {
160
+ abortController,
161
+ readyAt: null
162
+ };
163
+ // A source calls ready once it is listening, and a publish shows it is. The
164
+ // source heard nothing while it was down, so after an outage each subscriber
165
+ // gets a subscribed frame for its subscribe, which fires the page's
166
+ // onSubscribe for it to fetch what it missed.
167
+ function ready() {
168
+ var _run;
169
+ if (abortController.signal.aborted) {
170
+ return;
171
+ }
172
+ (_run = run).readyAt ?? (_run.readyAt = Date.now());
173
+ if (!channel.failing) {
174
+ return;
175
+ }
176
+ channel.failing = false;
177
+ channel.logger.info({
178
+ event: 'ws_source_recovered',
179
+ websocketId: channel.websocketId
180
+ });
181
+ channel.subscribers.forEach((subscriber)=>{
182
+ subscriber.send(JSON.stringify({
183
+ type: 'subscribed',
184
+ websocketId: channel.websocketId,
185
+ requestId: channel.subscribeRequestIds.get(subscriber)
186
+ }));
187
+ });
188
+ }
150
189
  function publish({ data }) {
151
190
  if (abortController.signal.aborted) {
152
191
  return;
153
192
  }
193
+ ready();
154
194
  broadcast(channel, {
155
195
  type: 'message',
156
196
  websocketId: channel.websocketId,
@@ -164,16 +204,17 @@ function createChannelRegistry() {
164
204
  connection: channel.connectionProperties,
165
205
  properties: channel.properties,
166
206
  publish,
207
+ ready,
167
208
  signal: abortController.signal,
168
209
  logger: channel.logger,
169
210
  tenant: channel.tenant ?? null
170
211
  })).catch((error)=>handleResolverError({
171
212
  channel,
172
213
  error,
173
- startedAt
214
+ run
174
215
  }));
175
216
  }
176
- async function subscribe(context, { websocketId, payload, subscriber }) {
217
+ async function subscribe(context, { websocketId, payload, requestId, subscriber }) {
177
218
  // One subscription per websocketId per connection — a re-subscribe (e.g.
178
219
  // with a new payload) replaces the previous one. The client stops reading
179
220
  // the previous one when it sends the re-subscribe, so it is removed before
@@ -208,9 +249,14 @@ function createChannelRegistry() {
208
249
  properties,
209
250
  tenant,
210
251
  subscribers: new Set(),
252
+ // subscriber → the requestId of the subscribe that joined it. The client
253
+ // reads a subscribed frame with any other requestId as a reply to a
254
+ // subscribe it has since replaced, and ignores it.
255
+ subscribeRequestIds: new Map(),
211
256
  abortController: null,
212
257
  restartTimer: null,
213
258
  retryCount: 0,
259
+ failing: false,
214
260
  publish: null,
215
261
  context,
216
262
  logger: context.logger
@@ -219,6 +265,7 @@ function createChannelRegistry() {
219
265
  startResolver(channel);
220
266
  }
221
267
  channel.subscribers.add(subscriber);
268
+ channel.subscribeRequestIds.set(subscriber, requestId);
222
269
  subscriber.subscriptions.set(websocketId, key);
223
270
  if (!type.isNone(websocketConfig.check)) {
224
271
  if (!checkedSubscriptions.has(subscriber)) {
@@ -247,6 +294,7 @@ function createChannelRegistry() {
247
294
  return;
248
295
  }
249
296
  channel.subscribers.delete(subscriber);
297
+ channel.subscribeRequestIds.delete(subscriber);
250
298
  if (channel.subscribers.size === 0) {
251
299
  removeChannel(channel);
252
300
  }
@@ -143,6 +143,7 @@ function createWebSocketConnection(context, { closeSocket, headers, registry, se
143
143
  await registry.subscribe(context, {
144
144
  websocketId,
145
145
  payload,
146
+ requestId,
146
147
  subscriber
147
148
  });
148
149
  send(JSON.stringify({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/api",
3
- "version": "7.3.0",
3
+ "version": "7.4.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -38,16 +38,16 @@
38
38
  "@better-auth/cimd": "1.7.7",
39
39
  "@better-auth/oauth-provider": "1.7.7",
40
40
  "@better-auth/passkey": "1.7.7",
41
- "@lowdefy/ajv": "7.3.0",
42
- "@lowdefy/build": "7.3.0",
43
- "@lowdefy/connection-smtp": "7.3.0",
44
- "@lowdefy/email-templates": "7.3.0",
45
- "@lowdefy/errors": "7.3.0",
46
- "@lowdefy/helpers": "7.3.0",
47
- "@lowdefy/node-utils": "7.3.0",
48
- "@lowdefy/nunjucks": "7.3.0",
49
- "@lowdefy/operators": "7.3.0",
50
- "@lowdefy/operators-js": "7.3.0",
41
+ "@lowdefy/ajv": "7.4.0",
42
+ "@lowdefy/build": "7.4.0",
43
+ "@lowdefy/connection-smtp": "7.4.0",
44
+ "@lowdefy/email-templates": "7.4.0",
45
+ "@lowdefy/errors": "7.4.0",
46
+ "@lowdefy/helpers": "7.4.0",
47
+ "@lowdefy/node-utils": "7.4.0",
48
+ "@lowdefy/nunjucks": "7.4.0",
49
+ "@lowdefy/operators": "7.4.0",
50
+ "@lowdefy/operators-js": "7.4.0",
51
51
  "@modelcontextprotocol/sdk": "1.32.2",
52
52
  "better-auth": "1.7.7",
53
53
  "jose": "^6.2.3",
@@ -55,8 +55,8 @@
55
55
  "nodemailer": "10.1.7"
56
56
  },
57
57
  "devDependencies": {
58
- "@lowdefy/connection-mongodb": "7.3.0",
59
- "@lowdefy/plugin-better-auth": "7.3.0",
58
+ "@lowdefy/connection-mongodb": "7.4.0",
59
+ "@lowdefy/plugin-better-auth": "7.4.0",
60
60
  "@opentelemetry/api": "1.9.1",
61
61
  "@swc/cli": "0.8.1",
62
62
  "@swc/core": "1.15.32",