@lowdefy/client-core 0.0.0-experimental-20261019052246

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 (52) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +9 -0
  3. package/dist/auth/createAuthFetchOptions.js +56 -0
  4. package/dist/auth/createAuthMethods.js +1070 -0
  5. package/dist/auth/createPkceChallenge.js +57 -0
  6. package/dist/auth/endBearerSession.js +26 -0
  7. package/dist/auth/getAuthorizationHeaders.js +31 -0
  8. package/dist/auth/validateAuthTransport.js +41 -0
  9. package/dist/claimCallerReload.js +41 -0
  10. package/dist/createCallAPI.js +39 -0
  11. package/dist/createCallRequest.js +34 -0
  12. package/dist/createHandleError.js +90 -0
  13. package/dist/createLowdefyCore.js +124 -0
  14. package/dist/createPageLifecycleManager.js +173 -0
  15. package/dist/getActiveLocale.js +26 -0
  16. package/dist/getContextKey.js +29 -0
  17. package/dist/hasOtherRoles.js +30 -0
  18. package/dist/index.js +32 -0
  19. package/dist/isOtherCaller.js +29 -0
  20. package/dist/recordCallerStamp.js +31 -0
  21. package/dist/recorder/buildEngineRecord.js +34 -0
  22. package/dist/recorder/buildInteractionRecord.js +60 -0
  23. package/dist/recorder/buildTraceEvent.js +62 -0
  24. package/dist/recorder/chunkRecords.js +78 -0
  25. package/dist/recorder/collectEventActions.js +22 -0
  26. package/dist/recorder/collectEventEndpoints.js +39 -0
  27. package/dist/recorder/collectEventRequests.js +38 -0
  28. package/dist/recorder/createPairingBuffer.js +177 -0
  29. package/dist/recorder/createPasswordRedactor.js +64 -0
  30. package/dist/recorder/createRecordBatcher.js +50 -0
  31. package/dist/recorder/createRecordingSession.js +60 -0
  32. package/dist/recorder/diffStateWrites.js +86 -0
  33. package/dist/recorder/isEventCall.js +26 -0
  34. package/dist/recorder/jsonType.js +35 -0
  35. package/dist/recorder/sendRecords.js +39 -0
  36. package/dist/recorder/toJsonSafe.js +26 -0
  37. package/dist/request.js +127 -0
  38. package/dist/setLowdefyUser.js +32 -0
  39. package/dist/shouldReloadForAuthError.js +39 -0
  40. package/dist/shouldReloadForBuild.js +45 -0
  41. package/dist/shouldReloadForCaller.js +50 -0
  42. package/dist/storage/clearUserStorage.js +36 -0
  43. package/dist/storage/clearUserStorageOnOwnerChange.js +42 -0
  44. package/dist/storage/createStorage.js +51 -0
  45. package/dist/storage/listUserStorageKeys.js +36 -0
  46. package/dist/storage/userStorageKey.js +24 -0
  47. package/dist/storage/userStoragePrefix.js +25 -0
  48. package/dist/test/createMemoryStore.js +40 -0
  49. package/dist/updateLowdefyPage.js +44 -0
  50. package/dist/url.js +29 -0
  51. package/dist/websocket/createWebSocketClient.js +428 -0
  52. package/package.json +57 -0
@@ -0,0 +1,177 @@
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 { pairTraceEvents, type } from '@lowdefy/helpers';
16
+ import buildEngineRecord from './buildEngineRecord.js';
17
+ import buildInteractionRecord from './buildInteractionRecord.js';
18
+ import buildTraceEvent from './buildTraceEvent.js';
19
+ // How long an interaction waits for the events it caused. The pairing rule
20
+ // decides by when an event's actions started (within its debounce plus 1 s of
21
+ // the interaction), but the trace hook reports an event when its actions
22
+ // finish, and a click's Request can take seconds. Holding each interaction
23
+ // this long lets a slow event, and a `bubble: true` sibling, still join it.
24
+ const HOLD_MS = 10000;
25
+ const PAIRED_KINDS = [
26
+ 'click',
27
+ 'change',
28
+ 'key'
29
+ ];
30
+ function hasTargetIdentity(target) {
31
+ return !type.isNone(target) && (type.isString(target.block_id) && target.block_id !== '' || type.isString(target.text) && target.text !== '');
32
+ }
33
+ // Turns interactions (DOM events on the web, block presses and changes on a
34
+ // phone) and engine trace payloads into finished v1 trace records, paired on
35
+ // the client with the shared pairing rule. Each record carries
36
+ // the build getBuild() named when its interaction was captured or its event
37
+ // arrived, never when its hold closes: a hold can outlast a config reload.
38
+ //
39
+ // - addInteraction({ t, kind, element, target, pageId, pathParams, value, key, url, build }):
40
+ // pageview and back are recorded at once; click, change and key wait until
41
+ // their hold ends, collecting the events the rule pairs with them. `build`
42
+ // is given only by a caller that knows it better than getBuild() does now
43
+ // (a pageview, recorded once its page config has rendered).
44
+ // - addEvent(payload, { urlAfter }): the event is built into a trace event
45
+ // now (its state writes are read against the state as it is now), then
46
+ // paired with the latest open interaction that could have caused it, or
47
+ // recorded at once as kind engine. Every interaction that could cause an
48
+ // event happened before the event started, so it is already in the buffer.
49
+ // - flushAll(): ends every hold now (pagehide, config reload, headless flush).
50
+ function createPairingBuffer({ onRecord, now = Date.now, getSession, getRoles = ()=>[], getBuild = ()=>null, redactor }) {
51
+ let open = [];
52
+ let nextId = 1;
53
+ let timer = null;
54
+ function emit(record) {
55
+ onRecord(redactor === undefined ? record : redactor.redact(record));
56
+ }
57
+ function finalise(interaction) {
58
+ let paired = [];
59
+ if (interaction.events.length > 0) {
60
+ const { pairs } = pairTraceEvents({
61
+ interactions: [
62
+ interaction
63
+ ],
64
+ events: interaction.events
65
+ });
66
+ const order = pairs[0]?.eventIds ?? interaction.events.map((event)=>event.id);
67
+ paired = order.map((id)=>interaction.events.find((event)=>event.id === id).traceEvent);
68
+ }
69
+ emit(buildInteractionRecord({
70
+ interaction,
71
+ paired,
72
+ session: getSession(),
73
+ roles: getRoles()
74
+ }));
75
+ }
76
+ function schedule() {
77
+ if (timer !== null) {
78
+ clearTimeout(timer);
79
+ timer = null;
80
+ }
81
+ if (open.length === 0) return;
82
+ const next = Math.min(...open.map((interaction)=>interaction.closesAt));
83
+ timer = setTimeout(closeDue, Math.max(0, next - now()));
84
+ }
85
+ function closeDue() {
86
+ timer = null;
87
+ const time = now();
88
+ const due = open.filter((interaction)=>interaction.closesAt <= time);
89
+ open = open.filter((interaction)=>interaction.closesAt > time);
90
+ due.forEach(finalise);
91
+ schedule();
92
+ }
93
+ function addInteraction({ t, kind, element, target, pageId, pathParams, value, key, url, build: named }) {
94
+ const time = t ?? now();
95
+ const build = type.isUndefined(named) ? getBuild() : named;
96
+ if (!PAIRED_KINDS.includes(kind)) {
97
+ emit(buildInteractionRecord({
98
+ interaction: {
99
+ t: time,
100
+ kind,
101
+ pageId,
102
+ pathParams,
103
+ url,
104
+ target: null,
105
+ build
106
+ },
107
+ session: getSession(),
108
+ roles: getRoles()
109
+ }));
110
+ return;
111
+ }
112
+ // A click or change the runner could not target (no block, no text) can
113
+ // be neither replayed nor paired.
114
+ if (kind !== 'key' && !hasTargetIdentity(target)) return;
115
+ if (redactor !== undefined && !type.isNone(element) && redactor.isPassword(element)) {
116
+ redactor.remember(target?.block_id);
117
+ }
118
+ open.push({
119
+ id: `i${nextId++}`,
120
+ t: time,
121
+ kind,
122
+ pageId,
123
+ value,
124
+ key,
125
+ target: kind === 'key' && !hasTargetIdentity(target) ? null : target,
126
+ blockIds: target?.block_ids ?? [],
127
+ build,
128
+ events: [],
129
+ closesAt: time + HOLD_MS
130
+ });
131
+ schedule();
132
+ }
133
+ function addEvent(payload, { urlAfter } = {}) {
134
+ const engineEvent = {
135
+ id: `e${nextId++}`,
136
+ blockId: payload.blockId,
137
+ eventName: payload.eventName,
138
+ startTimestamp: new Date(payload.record?.startTimestamp ?? now()).getTime(),
139
+ debounceMs: payload.debounceMs ?? 0,
140
+ scope: payload.scope,
141
+ pageId: payload.pageId,
142
+ build: getBuild(),
143
+ traceEvent: buildTraceEvent({
144
+ payload,
145
+ urlAfter
146
+ })
147
+ };
148
+ const { pairs } = pairTraceEvents({
149
+ interactions: open,
150
+ events: [
151
+ engineEvent
152
+ ]
153
+ });
154
+ if (pairs.length === 0) {
155
+ emit(buildEngineRecord({
156
+ engineEvent,
157
+ session: getSession(),
158
+ roles: getRoles()
159
+ }));
160
+ return;
161
+ }
162
+ open.find((interaction)=>interaction.id === pairs[0].interactionId).events.push(engineEvent);
163
+ }
164
+ function flushAll() {
165
+ const all = open;
166
+ open = [];
167
+ all.forEach(finalise);
168
+ schedule();
169
+ }
170
+ return {
171
+ addEvent,
172
+ addInteraction,
173
+ flushAll
174
+ };
175
+ }
176
+ export { HOLD_MS };
177
+ export default createPairingBuffer;
@@ -0,0 +1,64 @@
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
+ */ // A write holds a password block's value when its path is the block's id, or
16
+ // an ancestor of it: arrays are diffed as leaves, so a password block inside a
17
+ // List is written as the whole list array.
18
+ function holdsPassword(path, passwordBlocks) {
19
+ if (passwordBlocks.has(path)) return true;
20
+ return [
21
+ ...passwordBlocks
22
+ ].some((blockId)=>blockId.startsWith(`${path}.`));
23
+ }
24
+ function redactWrites(event, passwordBlocks) {
25
+ if (event === null || event === undefined) return;
26
+ (event.state_writes ?? []).forEach((write)=>{
27
+ if (holdsPassword(write.path, passwordBlocks)) {
28
+ write.value = null;
29
+ write.redacted = true;
30
+ }
31
+ });
32
+ }
33
+ // Password inputs are never recorded. A block is a password block when the
34
+ // host's isPassword says the interaction's element is one: on the web an
35
+ // `<input type="password">` (server-dev isPasswordElement), on a phone a
36
+ // TextInput of type password. It is remembered for the rest of the recording
37
+ // session, so its value is redacted from every later change and from every
38
+ // state write to its path, a later SetState echo included. It is remembered
39
+ // from any interaction with the block, a click included: on the web the
40
+ // visibility toggle turns the input into `type="text"`, and a click on it
41
+ // comes before the typing.
42
+ function createPasswordRedactor({ isPassword }) {
43
+ const passwordBlocks = new Set();
44
+ function remember(blockId) {
45
+ if (typeof blockId === 'string' && blockId !== '') {
46
+ passwordBlocks.add(blockId);
47
+ }
48
+ }
49
+ function redact(record) {
50
+ if (record.kind === 'change' && passwordBlocks.has(record.target?.block_id)) {
51
+ record.value = null;
52
+ record.redacted = true;
53
+ }
54
+ redactWrites(record.event, passwordBlocks);
55
+ (record.also ?? []).forEach((event)=>redactWrites(event, passwordBlocks));
56
+ return record;
57
+ }
58
+ return {
59
+ isPassword,
60
+ redact,
61
+ remember
62
+ };
63
+ }
64
+ export default createPasswordRedactor;
@@ -0,0 +1,50 @@
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 BATCH_SIZE = 20;
16
+ const BATCH_DELAY_MS = 5000;
17
+ // Collects finished records and sends them: at 20 records, 5 s after the
18
+ // first record of a batch, or when flush() is called (pagehide, the stream's
19
+ // reload event, a headless run's flush). flush() resolves when every send it
20
+ // started has settled.
21
+ function createRecordBatcher({ send }) {
22
+ let batch = [];
23
+ let timer = null;
24
+ function flush() {
25
+ if (timer !== null) {
26
+ clearTimeout(timer);
27
+ timer = null;
28
+ }
29
+ if (batch.length === 0) return Promise.resolve();
30
+ const records = batch;
31
+ batch = [];
32
+ return Promise.resolve(send(records)).catch(()=>{});
33
+ }
34
+ function add(record) {
35
+ batch.push(record);
36
+ if (batch.length >= BATCH_SIZE) {
37
+ flush();
38
+ return;
39
+ }
40
+ if (timer === null) {
41
+ timer = setTimeout(flush, BATCH_DELAY_MS);
42
+ }
43
+ }
44
+ return {
45
+ add,
46
+ flush
47
+ };
48
+ }
49
+ export { BATCH_DELAY_MS, BATCH_SIZE };
50
+ export default createRecordBatcher;
@@ -0,0 +1,60 @@
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 { createTraceId, isTraceId } from '@lowdefy/helpers';
16
+ const STORAGE_KEY = 'lowdefy_dev_recording';
17
+ const IDLE_MS = 10 * 60 * 1000;
18
+ function readStored(storage) {
19
+ try {
20
+ const stored = JSON.parse(storage.getItem(STORAGE_KEY));
21
+ if (isTraceId(stored?.id) && typeof stored.lastAt === 'number') return stored;
22
+ } catch {
23
+ // Blocked or malformed storage: start an unremembered session.
24
+ }
25
+ return null;
26
+ }
27
+ function writeStored(storage, value) {
28
+ try {
29
+ storage.setItem(STORAGE_KEY, JSON.stringify(value));
30
+ } catch {
31
+ // A browser that blocks site data records under an unremembered session.
32
+ }
33
+ }
34
+ // The tab's recording session (or the phone's, over the shell's device store).
35
+ // sessionStorage lives as long as the tab, so a
36
+ // session survives the reload a dev server restart triggers; a gap of more
37
+ // than 10 minutes starts a new one.
38
+ function createRecordingSession({ storage, now = Date.now }) {
39
+ let current = null;
40
+ function getId() {
41
+ const time = now();
42
+ const stored = readStored(storage) ?? current;
43
+ const session = stored !== null && time - stored.lastAt <= IDLE_MS ? {
44
+ id: stored.id,
45
+ lastAt: time
46
+ } : {
47
+ id: createTraceId({
48
+ now: time
49
+ }),
50
+ lastAt: time
51
+ };
52
+ current = session;
53
+ writeStored(storage, session);
54
+ return session.id;
55
+ }
56
+ return {
57
+ getId
58
+ };
59
+ }
60
+ export default createRecordingSession;
@@ -0,0 +1,86 @@
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 jsonType from './jsonType.js';
17
+ import toJsonSafe from './toJsonSafe.js';
18
+ // Objects are walked to their leaves so a write to one field of a form object
19
+ // reports that field's path; arrays are leaves, because a journey expectation
20
+ // on "rows.3.name" locks an ordering the app never promised.
21
+ // `seen` closes the cycle: page state is app data and an app is free to put a
22
+ // self-referencing object in it, which the walk would otherwise follow forever.
23
+ function collectLeaves({ into, path, seen = new WeakSet(), value }) {
24
+ if (type.isObject(value) && Object.keys(value).length > 0 && !seen.has(value)) {
25
+ seen.add(value);
26
+ Object.keys(value).forEach((key)=>{
27
+ collectLeaves({
28
+ into,
29
+ path: path === '' ? key : `${path}.${key}`,
30
+ seen,
31
+ value: value[key]
32
+ });
33
+ });
34
+ return into;
35
+ }
36
+ into.set(path, value);
37
+ return into;
38
+ }
39
+ function isSameValue(a, b) {
40
+ if (type.isDate(a) && type.isDate(b)) {
41
+ return a.getTime() === b.getTime();
42
+ }
43
+ try {
44
+ return JSON.stringify(a ?? null) === JSON.stringify(b ?? null);
45
+ } catch {
46
+ // A value that cannot be serialized cannot be compared; report it as written.
47
+ return false;
48
+ }
49
+ }
50
+ // The paths an action chain wrote, as the difference between the state before
51
+ // the chain ran and the state after. `values` is the dev-only branch: in
52
+ // production the recorder never asks for it, so a value cannot reach the wire.
53
+ function diffStateWrites({ after, before, values = false }) {
54
+ const beforeLeaves = collectLeaves({
55
+ into: new Map(),
56
+ path: '',
57
+ value: before ?? {}
58
+ });
59
+ const afterLeaves = collectLeaves({
60
+ into: new Map(),
61
+ path: '',
62
+ value: after ?? {}
63
+ });
64
+ const writes = [];
65
+ afterLeaves.forEach((value, path)=>{
66
+ if (path === '') return;
67
+ if (beforeLeaves.has(path) && isSameValue(beforeLeaves.get(path), value)) return;
68
+ writes.push(values ? {
69
+ path,
70
+ type: jsonType(value),
71
+ value: toJsonSafe(value)
72
+ } : {
73
+ path,
74
+ type: jsonType(value)
75
+ });
76
+ });
77
+ beforeLeaves.forEach((_, path)=>{
78
+ if (path === '' || afterLeaves.has(path)) return;
79
+ writes.push({
80
+ path,
81
+ type: 'undefined'
82
+ });
83
+ });
84
+ return writes;
85
+ }
86
+ export default diffStateWrites;
@@ -0,0 +1,26 @@
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
+ // Whether the newest call of a request or endpoint was made by this event.
17
+ // Action ids are config ids, not unique across blocks or runs, so an id in
18
+ // the event's responses is not enough: the call must come from the event's
19
+ // own block, and from an action this run did not skip (a skipped action keeps
20
+ // its response entry while the call history still holds an earlier run's).
21
+ function isEventCall({ blockId, call, responses }) {
22
+ if (type.isNone(call) || type.isNone(call.actionId)) return false;
23
+ if (call.blockId !== blockId || !(call.actionId in responses)) return false;
24
+ return responses[call.actionId]?.skipped !== true;
25
+ }
26
+ export default isEventCall;
@@ -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 { type } from '@lowdefy/helpers';
16
+ // The JSON type name a trace event carries for a written state value. Dates are
17
+ // named as dates rather than collapsed into "object": the engine keeps real Date
18
+ // objects in state, and a journey expectation compiled from the trace has to know
19
+ // it is comparing a date, not a bag of fields.
20
+ function jsonType(value) {
21
+ if (type.isNone(value)) {
22
+ return value === null ? 'null' : 'undefined';
23
+ }
24
+ if (type.isArray(value)) {
25
+ return 'array';
26
+ }
27
+ if (type.isDate(value)) {
28
+ return 'date';
29
+ }
30
+ if (type.isObject(value)) {
31
+ return 'object';
32
+ }
33
+ return typeof value;
34
+ }
35
+ export default jsonType;
@@ -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
+ */ import chunkRecords from './chunkRecords.js';
16
+ // One POST per chunk to the dev server (`basePath` is the page's base path on
17
+ // the web, the dev server's URL on a phone). keepalive lets a send started on pagehide
18
+ // finish after the tab is gone, as a beacon would, with one code path. A
19
+ // failed send is dropped: recording must never get in the developer's way.
20
+ async function sendRecords({ basePath, records, fetch }) {
21
+ const chunks = chunkRecords({
22
+ records
23
+ });
24
+ await Promise.all(chunks.map(async (chunk)=>{
25
+ try {
26
+ await fetch(`${basePath}/api/dev-recording`, {
27
+ method: 'POST',
28
+ keepalive: true,
29
+ headers: {
30
+ 'Content-Type': 'application/json'
31
+ },
32
+ body: JSON.stringify(chunk)
33
+ });
34
+ } catch {
35
+ // Dropped silently.
36
+ }
37
+ }));
38
+ }
39
+ export default sendRecords;
@@ -0,0 +1,26 @@
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
+ */ // Dev traces carry real values, and a value is whatever a block handed the
16
+ // event: a Date, a DOM node, a structure with a cycle in it. The batch is
17
+ // JSON, so a value that cannot be JSON is recorded as null rather than
18
+ // throwing away the batch it travels in.
19
+ function toJsonSafe(value) {
20
+ try {
21
+ return JSON.parse(JSON.stringify(value ?? null));
22
+ } catch {
23
+ return null;
24
+ }
25
+ }
26
+ export default toJsonSafe;