@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.
- package/LICENSE +201 -0
- package/README.md +9 -0
- package/dist/auth/createAuthFetchOptions.js +56 -0
- package/dist/auth/createAuthMethods.js +1070 -0
- package/dist/auth/createPkceChallenge.js +57 -0
- package/dist/auth/endBearerSession.js +26 -0
- package/dist/auth/getAuthorizationHeaders.js +31 -0
- package/dist/auth/validateAuthTransport.js +41 -0
- package/dist/claimCallerReload.js +41 -0
- package/dist/createCallAPI.js +39 -0
- package/dist/createCallRequest.js +34 -0
- package/dist/createHandleError.js +90 -0
- package/dist/createLowdefyCore.js +124 -0
- package/dist/createPageLifecycleManager.js +173 -0
- package/dist/getActiveLocale.js +26 -0
- package/dist/getContextKey.js +29 -0
- package/dist/hasOtherRoles.js +30 -0
- package/dist/index.js +32 -0
- package/dist/isOtherCaller.js +29 -0
- package/dist/recordCallerStamp.js +31 -0
- package/dist/recorder/buildEngineRecord.js +34 -0
- package/dist/recorder/buildInteractionRecord.js +60 -0
- package/dist/recorder/buildTraceEvent.js +62 -0
- package/dist/recorder/chunkRecords.js +78 -0
- package/dist/recorder/collectEventActions.js +22 -0
- package/dist/recorder/collectEventEndpoints.js +39 -0
- package/dist/recorder/collectEventRequests.js +38 -0
- package/dist/recorder/createPairingBuffer.js +177 -0
- package/dist/recorder/createPasswordRedactor.js +64 -0
- package/dist/recorder/createRecordBatcher.js +50 -0
- package/dist/recorder/createRecordingSession.js +60 -0
- package/dist/recorder/diffStateWrites.js +86 -0
- package/dist/recorder/isEventCall.js +26 -0
- package/dist/recorder/jsonType.js +35 -0
- package/dist/recorder/sendRecords.js +39 -0
- package/dist/recorder/toJsonSafe.js +26 -0
- package/dist/request.js +127 -0
- package/dist/setLowdefyUser.js +32 -0
- package/dist/shouldReloadForAuthError.js +39 -0
- package/dist/shouldReloadForBuild.js +45 -0
- package/dist/shouldReloadForCaller.js +50 -0
- package/dist/storage/clearUserStorage.js +36 -0
- package/dist/storage/clearUserStorageOnOwnerChange.js +42 -0
- package/dist/storage/createStorage.js +51 -0
- package/dist/storage/listUserStorageKeys.js +36 -0
- package/dist/storage/userStorageKey.js +24 -0
- package/dist/storage/userStoragePrefix.js +25 -0
- package/dist/test/createMemoryStore.js +40 -0
- package/dist/updateLowdefyPage.js +44 -0
- package/dist/url.js +29 -0
- package/dist/websocket/createWebSocketClient.js +428 -0
- package/package.json +57 -0
package/dist/request.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
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 { decodeServerError } from '@lowdefy/engine';
|
|
16
|
+
import { expectedErrorNames, lowdefyErrorTypes } from '@lowdefy/errors';
|
|
17
|
+
import { parseCallerStamp, translate, type } from '@lowdefy/helpers';
|
|
18
|
+
import endBearerSession from './auth/endBearerSession.js';
|
|
19
|
+
import getAuthorizationHeaders from './auth/getAuthorizationHeaders.js';
|
|
20
|
+
import recordCallerStamp from './recordCallerStamp.js';
|
|
21
|
+
import shouldReloadForAuthError from './shouldReloadForAuthError.js';
|
|
22
|
+
import shouldReloadForBuild from './shouldReloadForBuild.js';
|
|
23
|
+
import shouldReloadForCaller from './shouldReloadForCaller.js';
|
|
24
|
+
// buildId is the build this bundle was made from. The production server
|
|
25
|
+
// refuses a call from another build with a 409 before running anything, since
|
|
26
|
+
// an old page's payload can be the wrong shape for the new build's request and
|
|
27
|
+
// still be written. The host then moves once onto the current build
|
|
28
|
+
// (host.onBuildChanged; a reload on the web). The promise never settles: the
|
|
29
|
+
// page is being replaced, and a rejection would run the action's catch actions
|
|
30
|
+
// and report the refusal as an error. Only the production client carries a
|
|
31
|
+
// build id; the server lets a call naming none through (dev and e2e pages,
|
|
32
|
+
// third-party webhooks).
|
|
33
|
+
//
|
|
34
|
+
// lowdefy.user is the caller the page renders. The host also moves onto the
|
|
35
|
+
// server's caller (host.onCallerChanged; a reload on the web) when the server
|
|
36
|
+
// answered as another caller or with other roles, or refused the rendered
|
|
37
|
+
// caller in a way that means the page itself is no longer theirs
|
|
38
|
+
// (shouldReloadForCaller, shouldReloadForAuthError), so it never shows one
|
|
39
|
+
// caller's data to another. Otherwise the response's caller stamp is kept as
|
|
40
|
+
// the last one seen (recordCallerStamp).
|
|
41
|
+
// reloadOnAuthorizationError is set for page requests, which share the page's
|
|
42
|
+
// auth, and not for endpoints, which carry their own.
|
|
43
|
+
//
|
|
44
|
+
// A bearer host (a native shell) sends its session token as Authorization. A
|
|
45
|
+
// 401 to a call that carried one means the session is gone, so the token is
|
|
46
|
+
// cleared and the host moves onto the signed-out caller (endBearerSession),
|
|
47
|
+
// with no reload guard: once the token is cleared the next call cannot be
|
|
48
|
+
// refused for it again.
|
|
49
|
+
async function request({ buildId, url, method = 'GET', body, reloadOnAuthorizationError = false, lowdefy }) {
|
|
50
|
+
// Read per call: the rendered caller changes with the session.
|
|
51
|
+
const { user } = lowdefy;
|
|
52
|
+
const { host } = lowdefy._internal;
|
|
53
|
+
const authorizationHeaders = getAuthorizationHeaders({
|
|
54
|
+
host
|
|
55
|
+
});
|
|
56
|
+
const headers = {
|
|
57
|
+
'Content-Type': 'application/json',
|
|
58
|
+
...authorizationHeaders
|
|
59
|
+
};
|
|
60
|
+
if (!type.isNone(buildId)) {
|
|
61
|
+
headers['x-lowdefy-build'] = buildId;
|
|
62
|
+
}
|
|
63
|
+
const res = await host.fetch(url, {
|
|
64
|
+
method,
|
|
65
|
+
headers,
|
|
66
|
+
body: JSON.stringify(body)
|
|
67
|
+
});
|
|
68
|
+
const serverCaller = res.headers.get('x-lowdefy-caller');
|
|
69
|
+
if (res.status === 401 && !type.isNone(authorizationHeaders.Authorization)) {
|
|
70
|
+
await endBearerSession({
|
|
71
|
+
host,
|
|
72
|
+
serverCaller
|
|
73
|
+
});
|
|
74
|
+
return new Promise(()=>{});
|
|
75
|
+
}
|
|
76
|
+
if (shouldReloadForCaller({
|
|
77
|
+
serverCaller,
|
|
78
|
+
sessionStorage: host.sessionStorage,
|
|
79
|
+
user
|
|
80
|
+
})) {
|
|
81
|
+
host.onCallerChanged(parseCallerStamp(serverCaller));
|
|
82
|
+
return new Promise(()=>{});
|
|
83
|
+
}
|
|
84
|
+
recordCallerStamp({
|
|
85
|
+
lowdefy,
|
|
86
|
+
serverCaller
|
|
87
|
+
});
|
|
88
|
+
if (!res.ok) {
|
|
89
|
+
const body = await res.json();
|
|
90
|
+
if (res.status === 409 && shouldReloadForBuild({
|
|
91
|
+
bundleBuildId: buildId,
|
|
92
|
+
serverBuildId: body?.buildId,
|
|
93
|
+
sessionStorage: host.sessionStorage
|
|
94
|
+
})) {
|
|
95
|
+
host.onBuildChanged({
|
|
96
|
+
serverBuildId: body.buildId
|
|
97
|
+
});
|
|
98
|
+
return new Promise(()=>{});
|
|
99
|
+
}
|
|
100
|
+
if (body?.['~e']) {
|
|
101
|
+
throw decodeServerError(body);
|
|
102
|
+
}
|
|
103
|
+
// The server answers an expected outcome (an auth gate's 401 or 403, a
|
|
104
|
+
// UserError's 400) itself, as { name, message } with no serialized error.
|
|
105
|
+
// Rebuilding its class keeps the name the engine, handleError and the dev
|
|
106
|
+
// tools tell an expected outcome from a fault by.
|
|
107
|
+
if (expectedErrorNames.has(body?.name)) {
|
|
108
|
+
const ExpectedError = lowdefyErrorTypes[body.name];
|
|
109
|
+
const error = new ExpectedError(body.message);
|
|
110
|
+
if (shouldReloadForAuthError({
|
|
111
|
+
error,
|
|
112
|
+
reloadOnAuthorizationError,
|
|
113
|
+
sessionStorage: host.sessionStorage,
|
|
114
|
+
user
|
|
115
|
+
})) {
|
|
116
|
+
host.onCallerChanged(parseCallerStamp(serverCaller));
|
|
117
|
+
return new Promise(()=>{});
|
|
118
|
+
}
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
throw new Error(body.message || translate({
|
|
122
|
+
key: 'client.requestError'
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
return res.json();
|
|
126
|
+
}
|
|
127
|
+
export default request;
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
// Sets the caller the app renders. CallAPI results (lowdefy.apiResponses, read
|
|
17
|
+
// by _api) are app-level, so they are kept per organization as page contexts
|
|
18
|
+
// are (see the engine's getContext): when the caller's organization changes,
|
|
19
|
+
// the results fetched under the previous one are dropped, so none is read
|
|
20
|
+
// under another organization. Results fetched with no organization hold no
|
|
21
|
+
// organization's data and are kept when the caller gains one. A call still in
|
|
22
|
+
// flight writes into the dropped object. Author global state is left alone.
|
|
23
|
+
function setLowdefyUser({ lowdefy, user }) {
|
|
24
|
+
const organizationId = user?.organization_id ?? null;
|
|
25
|
+
const resultsOrganizationId = lowdefy._internal.apiResponsesOrganizationId;
|
|
26
|
+
if (!type.isNone(resultsOrganizationId) && resultsOrganizationId !== organizationId) {
|
|
27
|
+
lowdefy.apiResponses = {};
|
|
28
|
+
}
|
|
29
|
+
lowdefy._internal.apiResponsesOrganizationId = organizationId;
|
|
30
|
+
lowdefy.user = user ?? null;
|
|
31
|
+
}
|
|
32
|
+
export default setLowdefyUser;
|
|
@@ -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 { type } from '@lowdefy/helpers';
|
|
16
|
+
import claimCallerReload from './claimCallerReload.js';
|
|
17
|
+
// True when an auth refusal means the page shows a caller the server no longer
|
|
18
|
+
// admits, so it must reload and let the server render the page again. A 401
|
|
19
|
+
// while a user is rendered means the session ended; with no user it is an
|
|
20
|
+
// ordinary refusal on a public page and is thrown, so it cannot loop. A
|
|
21
|
+
// request's 403 reloads too: page requests share their page's auth, so the
|
|
22
|
+
// caller lost the page. An endpoint's 403 does not, since endpoints carry their
|
|
23
|
+
// own roles and a refusal is an ordinary outcome there. A two-factor enrolment
|
|
24
|
+
// refusal never reloads. Matched by name: plugins bundle their own
|
|
25
|
+
// @lowdefy/errors copy.
|
|
26
|
+
function shouldReloadForAuthError({ error, reloadOnAuthorizationError, sessionStorage, user }) {
|
|
27
|
+
if (error.name === 'AuthenticationError' && !type.isNone(user)) {
|
|
28
|
+
return claimCallerReload({
|
|
29
|
+
sessionStorage
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (error.name === 'AuthorizationError' && reloadOnAuthorizationError === true) {
|
|
33
|
+
return claimCallerReload({
|
|
34
|
+
sessionStorage
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
export default shouldReloadForAuthError;
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
// Session storage key holding the server build this tab last reloaded for.
|
|
17
|
+
const RELOADED_FOR_BUILD_KEY = 'lowdefy.reloadedForBuild';
|
|
18
|
+
// True when the server answered from a different build than the bundle this
|
|
19
|
+
// tab is running, so the tab must do a full load to pick up the current
|
|
20
|
+
// bundle. Without this a page config from the new build can reference _js
|
|
21
|
+
// functions the old bundle never shipped, and the old page's requests can
|
|
22
|
+
// name requests, endpoints or auth routes the new build renamed or removed.
|
|
23
|
+
//
|
|
24
|
+
// A tab reloads at most once per server build: if the freshly loaded bundle
|
|
25
|
+
// still disagrees with the server (a rolling deploy answering from mixed
|
|
26
|
+
// versions, a proxy caching old HTML), reloading again would loop forever.
|
|
27
|
+
// The old-config-on-old-bundle throw is the lesser failure in that case.
|
|
28
|
+
// sessionStorage is the host's session store (get, set).
|
|
29
|
+
function shouldReloadForBuild({ bundleBuildId, serverBuildId, sessionStorage }) {
|
|
30
|
+
if (type.isNone(serverBuildId) || serverBuildId === bundleBuildId) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
if (sessionStorage.get(RELOADED_FOR_BUILD_KEY) === serverBuildId) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
sessionStorage.set(RELOADED_FOR_BUILD_KEY, serverBuildId);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
// Browsers with storage disabled throw on sessionStorage access. Without
|
|
40
|
+
// the once-per-build record the loop guard cannot work, so do not reload.
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
export default shouldReloadForBuild;
|
|
@@ -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
|
+
*/ import { AuthenticationError } from '@lowdefy/errors';
|
|
16
|
+
import { parseCallerStamp } from '@lowdefy/helpers';
|
|
17
|
+
import claimCallerReload from './claimCallerReload.js';
|
|
18
|
+
import hasOtherRoles from './hasOtherRoles.js';
|
|
19
|
+
import isOtherCaller from './isOtherCaller.js';
|
|
20
|
+
// True when the page must reload because the server answered as another caller
|
|
21
|
+
// than the page renders - another person signed in, or the organization was
|
|
22
|
+
// switched, in another tab - or answered the same caller with other roles, so
|
|
23
|
+
// the page never shows two callers' data or access the person no longer holds.
|
|
24
|
+
// serverCaller is the response's x-lowdefy-caller header. A changed session or
|
|
25
|
+
// membership list with the same person, organization and roles does not reload.
|
|
26
|
+
// A second difference within the reload interval throws instead of reloading
|
|
27
|
+
// again (see claimCallerReload).
|
|
28
|
+
function shouldReloadForCaller({ serverCaller, sessionStorage, user }) {
|
|
29
|
+
const stamp = parseCallerStamp(serverCaller);
|
|
30
|
+
const otherCaller = isOtherCaller({
|
|
31
|
+
stamp,
|
|
32
|
+
user
|
|
33
|
+
});
|
|
34
|
+
if (!otherCaller && !hasOtherRoles({
|
|
35
|
+
stamp,
|
|
36
|
+
user
|
|
37
|
+
})) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (claimCallerReload({
|
|
41
|
+
sessionStorage
|
|
42
|
+
})) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
if (otherCaller) {
|
|
46
|
+
throw new AuthenticationError('The server answered as a different user or organization than this page shows. Reload the page.');
|
|
47
|
+
}
|
|
48
|
+
throw new AuthenticationError('The server answered with different roles than this page shows. Reload the page.');
|
|
49
|
+
}
|
|
50
|
+
export default shouldReloadForCaller;
|
|
@@ -0,0 +1,36 @@
|
|
|
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 listUserStorageKeys from './listUserStorageKeys.js';
|
|
17
|
+
// Removes every user key of this app not owned by keepUserId (all of them when keepUserId is
|
|
18
|
+
// null), and the session storage Lowdefy writes for the person browsing. Device preferences and
|
|
19
|
+
// the build reload markers stay. storage and sessionStorage are the host's stores.
|
|
20
|
+
function clearUserStorage({ basePath, keepUserId, sessionStorage, slug, storage }) {
|
|
21
|
+
try {
|
|
22
|
+
listUserStorageKeys({
|
|
23
|
+
basePath,
|
|
24
|
+
slug,
|
|
25
|
+
storage
|
|
26
|
+
}).filter(({ userId })=>type.isNone(keepUserId) || userId !== keepUserId).forEach(({ key })=>storage.remove(key));
|
|
27
|
+
} catch {
|
|
28
|
+
// Storage the browser blocks holds nothing to clear.
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
sessionStorage.remove('lowdefy_scroll');
|
|
32
|
+
} catch {
|
|
33
|
+
// Storage the browser blocks holds nothing to clear.
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export default clearUserStorage;
|
|
@@ -0,0 +1,42 @@
|
|
|
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 clearUserStorage from './clearUserStorage.js';
|
|
16
|
+
import listUserStorageKeys from './listUserStorageKeys.js';
|
|
17
|
+
// The person signed in (or nobody) may not be the one who left data on this browser: another
|
|
18
|
+
// person signed in on it, or a session ended without Logout running. Their keys go before any
|
|
19
|
+
// block reads storage.
|
|
20
|
+
function clearUserStorageOnOwnerChange({ basePath, sessionStorage, slug, storage, userId }) {
|
|
21
|
+
let entries;
|
|
22
|
+
try {
|
|
23
|
+
entries = listUserStorageKeys({
|
|
24
|
+
basePath,
|
|
25
|
+
slug,
|
|
26
|
+
storage
|
|
27
|
+
});
|
|
28
|
+
} catch {
|
|
29
|
+
// Storage the browser blocks holds nothing to clear.
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (entries.some((entry)=>entry.userId !== userId)) {
|
|
33
|
+
clearUserStorage({
|
|
34
|
+
basePath,
|
|
35
|
+
keepUserId: userId,
|
|
36
|
+
sessionStorage,
|
|
37
|
+
slug,
|
|
38
|
+
storage
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export default clearUserStorageOnOwnerChange;
|
|
@@ -0,0 +1,51 @@
|
|
|
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 { appStorageKey } from '@lowdefy/helpers';
|
|
16
|
+
import clearUserStorage from './clearUserStorage.js';
|
|
17
|
+
import clearUserStorageOnOwnerChange from './clearUserStorageOnOwnerChange.js';
|
|
18
|
+
import userStorageKey from './userStorageKey.js';
|
|
19
|
+
// Storage keys for this app, reached by blocks and actions through their methods. getUserId is
|
|
20
|
+
// read on every call, so a user key always names the person signed in now. storage and
|
|
21
|
+
// sessionStorage are the host's stores ({ get, set, remove, keys }).
|
|
22
|
+
function createStorage({ basePath, getUserId, sessionStorage, slug, storage }) {
|
|
23
|
+
return {
|
|
24
|
+
appKey: (name)=>appStorageKey({
|
|
25
|
+
basePath,
|
|
26
|
+
name,
|
|
27
|
+
slug
|
|
28
|
+
}),
|
|
29
|
+
clearUserStorage: ({ keepUserId })=>clearUserStorage({
|
|
30
|
+
basePath,
|
|
31
|
+
keepUserId,
|
|
32
|
+
sessionStorage,
|
|
33
|
+
slug,
|
|
34
|
+
storage
|
|
35
|
+
}),
|
|
36
|
+
clearUserStorageOnOwnerChange: ()=>clearUserStorageOnOwnerChange({
|
|
37
|
+
basePath,
|
|
38
|
+
sessionStorage,
|
|
39
|
+
slug,
|
|
40
|
+
storage,
|
|
41
|
+
userId: getUserId()
|
|
42
|
+
}),
|
|
43
|
+
userKey: (name)=>userStorageKey({
|
|
44
|
+
basePath,
|
|
45
|
+
name,
|
|
46
|
+
slug,
|
|
47
|
+
userId: getUserId()
|
|
48
|
+
})
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export default createStorage;
|
|
@@ -0,0 +1,36 @@
|
|
|
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 userStoragePrefix from './userStoragePrefix.js';
|
|
16
|
+
// Every user key of this app in storage (the host's store), with its owner's user id (null for
|
|
17
|
+
// an anonymous visitor). Keys are read before any is removed, since removing shifts storage
|
|
18
|
+
// indexes.
|
|
19
|
+
function listUserStorageKeys({ basePath, slug, storage }) {
|
|
20
|
+
const prefix = userStoragePrefix({
|
|
21
|
+
basePath,
|
|
22
|
+
slug
|
|
23
|
+
});
|
|
24
|
+
const entries = [];
|
|
25
|
+
storage.keys().forEach((key)=>{
|
|
26
|
+
if (key.startsWith(prefix)) {
|
|
27
|
+
const owner = key.slice(prefix.length, key.indexOf(':', prefix.length));
|
|
28
|
+
entries.push({
|
|
29
|
+
key,
|
|
30
|
+
userId: owner === '' ? null : decodeURIComponent(owner)
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return entries;
|
|
35
|
+
}
|
|
36
|
+
export default listUserStorageKeys;
|
|
@@ -0,0 +1,24 @@
|
|
|
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 userStoragePrefix from './userStoragePrefix.js';
|
|
16
|
+
// The user id is encoded so the owner always ends at the next ":", whatever the id holds. An
|
|
17
|
+
// anonymous visitor owns the empty id.
|
|
18
|
+
function userStorageKey({ basePath, name, slug, userId }) {
|
|
19
|
+
return `${userStoragePrefix({
|
|
20
|
+
basePath,
|
|
21
|
+
slug
|
|
22
|
+
})}${encodeURIComponent(userId ?? '')}:${name}`;
|
|
23
|
+
}
|
|
24
|
+
export default userStorageKey;
|
|
@@ -0,0 +1,25 @@
|
|
|
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 { appStorageKey } from '@lowdefy/helpers';
|
|
16
|
+
// A person's data sits under one name in the app's namespace, then the owner's user id. Keys set
|
|
17
|
+
// with SetLocalStorage can not start with "lowdefy_", so no app key reads as a user key.
|
|
18
|
+
function userStoragePrefix({ basePath, slug }) {
|
|
19
|
+
return appStorageKey({
|
|
20
|
+
basePath,
|
|
21
|
+
name: 'lowdefy_user:',
|
|
22
|
+
slug
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export default userStoragePrefix;
|
|
@@ -0,0 +1,40 @@
|
|
|
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 host store held in memory. throws makes every call throw, as a browser
|
|
16
|
+
// that blocks site data does.
|
|
17
|
+
function createMemoryStore({ stored = {}, throws = false } = {}) {
|
|
18
|
+
function check() {
|
|
19
|
+
if (throws) throw new Error('storage disabled');
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
get: (key)=>{
|
|
23
|
+
check();
|
|
24
|
+
return stored[key] ?? null;
|
|
25
|
+
},
|
|
26
|
+
set: (key, value)=>{
|
|
27
|
+
check();
|
|
28
|
+
stored[key] = String(value);
|
|
29
|
+
},
|
|
30
|
+
remove: (key)=>{
|
|
31
|
+
check();
|
|
32
|
+
delete stored[key];
|
|
33
|
+
},
|
|
34
|
+
keys: ()=>{
|
|
35
|
+
check();
|
|
36
|
+
return Object.keys(stored);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export default createMemoryStore;
|
|
@@ -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
|
+
*/ import { rememberPath } from '@lowdefy/engine';
|
|
16
|
+
import setLowdefyUser from './setLowdefyUser.js';
|
|
17
|
+
// Points the lowdefy object at the page being shown and the caller it renders.
|
|
18
|
+
// Runs on every render, so it sees the person signed in on load and any later
|
|
19
|
+
// change. matchedPath and pathParams are the server's match of the shown page:
|
|
20
|
+
// the path it was matched on (no basePath, no leading "/") and the values it
|
|
21
|
+
// carries.
|
|
22
|
+
function updateLowdefyPage({ config, lowdefy, matchedPath, pathParams, user }) {
|
|
23
|
+
lowdefy.home = config.rootConfig.home || {};
|
|
24
|
+
lowdefy.linkPaths = config.pageConfig.linkPaths;
|
|
25
|
+
lowdefy.menus = config.rootConfig.menus;
|
|
26
|
+
lowdefy.pageId = config.pageConfig.pageId;
|
|
27
|
+
lowdefy.pagePaths = config.rootConfig.pagePaths;
|
|
28
|
+
setLowdefyUser({
|
|
29
|
+
lowdefy,
|
|
30
|
+
user
|
|
31
|
+
});
|
|
32
|
+
lowdefy._internal.storage.clearUserStorageOnOwnerChange();
|
|
33
|
+
// The shown page's path joins the path memory before its context is built,
|
|
34
|
+
// so the first load's page is known before any plugin reads the memory.
|
|
35
|
+
rememberPath({
|
|
36
|
+
lowdefy,
|
|
37
|
+
path: matchedPath,
|
|
38
|
+
pageId: config.pageConfig.pageId,
|
|
39
|
+
pathParams,
|
|
40
|
+
pattern: config.pageConfig.path
|
|
41
|
+
});
|
|
42
|
+
return lowdefy;
|
|
43
|
+
}
|
|
44
|
+
export default updateLowdefyPage;
|
package/dist/url.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
function serializeQuery(query) {
|
|
17
|
+
if (type.isNone(query) || query === '') {
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
20
|
+
if (type.isString(query)) {
|
|
21
|
+
return query;
|
|
22
|
+
}
|
|
23
|
+
return new URLSearchParams(query).toString();
|
|
24
|
+
}
|
|
25
|
+
function createUrl({ basePath = '', pathname, query }) {
|
|
26
|
+
const serializedQuery = serializeQuery(query);
|
|
27
|
+
return `${basePath}${pathname}${serializedQuery ? `?${serializedQuery}` : ''}`;
|
|
28
|
+
}
|
|
29
|
+
export { createUrl, serializeQuery };
|