@valbuild/server 0.146.4 → 0.147.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/browser/dist/valbuild-server-browser.cjs.d.ts +2 -0
- package/browser/dist/valbuild-server-browser.cjs.dev.js +146 -0
- package/browser/dist/valbuild-server-browser.cjs.js +7 -0
- package/browser/dist/valbuild-server-browser.cjs.prod.js +146 -0
- package/browser/dist/valbuild-server-browser.esm.js +142 -0
- package/browser/package.json +4 -0
- package/dist/ValRouterCore-3fd3d444.cjs.dev.js +9897 -0
- package/dist/ValRouterCore-7ffcc08c.cjs.prod.js +9897 -0
- package/dist/ValRouterCore-f5004f8f.esm.js +9866 -0
- package/dist/declarations/src/ValOps.d.ts +13 -1
- package/dist/declarations/src/ValOpsFS.d.ts +1 -0
- package/dist/declarations/src/ValOpsHttp.d.ts +1 -0
- package/dist/declarations/src/ValOpsMemory.d.ts +9 -0
- package/dist/declarations/src/ValRouter.d.ts +1 -1
- package/dist/declarations/src/ValServer.d.ts +9 -2
- package/dist/declarations/src/browser/inTabFetch.d.ts +27 -0
- package/dist/declarations/src/browser/index.d.ts +9 -0
- package/dist/declarations/src/fixHandlers.d.ts +1 -1
- package/dist/declarations/src/linkCheck/probeUrl.d.ts +40 -0
- package/dist/declarations/src/platform.d.ts +39 -0
- package/dist/getJsonEntryAtCommit-1e08d724.esm.js +136 -0
- package/dist/getJsonEntryAtCommit-df65d861.cjs.prod.js +143 -0
- package/dist/getJsonEntryAtCommit-e96beb9e.cjs.dev.js +143 -0
- package/dist/jsonValuesModule-427d68c8.esm.js +273 -0
- package/dist/jsonValuesModule-8ae503a8.cjs.dev.js +288 -0
- package/dist/jsonValuesModule-d1bb8d8f.cjs.prod.js +288 -0
- package/dist/ops-0d05feaf.esm.js +353 -0
- package/dist/ops-b15239ac.cjs.prod.js +361 -0
- package/dist/ops-f37d18c6.cjs.dev.js +361 -0
- package/dist/publishKit-580f558a.cjs.prod.js +41 -0
- package/dist/publishKit-5865095c.cjs.dev.js +41 -0
- package/dist/publishKit-bc8df53d.esm.js +31 -0
- package/dist/valbuild-server.cjs.dev.js +7902 -18215
- package/dist/valbuild-server.cjs.prod.js +7902 -18215
- package/dist/valbuild-server.esm.js +7758 -18070
- package/package.json +18 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @valbuild/server
|
|
2
2
|
|
|
3
|
+
## 0.147.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#876](https://github.com/valbuild/val/pull/876) [`cb04e58`](https://github.com/valbuild/val/commit/cb04e586db1e3ffd20ec1fee744561b1c8f8cffd) Thanks [@freekh](https://github.com/freekh)! - Demo mode: run the Val Studio with no account and no backend (TanStack Start).
|
|
8
|
+
|
|
9
|
+
`<ValApp demo>` starts Val's own server in the visitor's browser tab, and the Studio talks to it there instead of to `/api/val`. Edits show up in the page as they are typed and are held in the tab's memory: nothing is sent anywhere, and a reload starts over. Images and videos can be uploaded too: a small service worker, served by `initValDemoServer()`, hands the upload and the unpublished files to the server in the tab, so an uploaded photo or video shows in the page with nothing leaving the browser. Uploads are capped by their size after encoding. Pair `<ValApp demo>` with `<ValProvider demo>` on the site and `initValDemoServer()` from the new `@valbuild/tanstack/demo` entry on the API route. In a demo the Studio opens with a guided tour, Publish explains what it would do instead of publishing, and Preview in a new tab is shown but disabled.
|
|
10
|
+
|
|
11
|
+
Also:
|
|
12
|
+
|
|
13
|
+
- `@valbuild/server/browser` is a new entry: Val's request handling, bundleable for a browser. The TypeScript compiler, which only publishing and history need, is loaded when they ask for it, so a browser bundle of the server is about 140 KB rather than 3.6 MB.
|
|
14
|
+
- `s.image({ encode })` now scales an image down in browsers that cannot write WebP (Safari), as a JPEG or PNG in the image's own format, instead of uploading it at full size.
|
|
15
|
+
- `GET /profiles` answers with no profiles, rather than an error, when edits are stored locally and no project is configured.
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [[`2f09cd5`](https://github.com/valbuild/val/commit/2f09cd5e2f4fc37f06917899f24f9ad2ee8d3a8b), [`cb04e58`](https://github.com/valbuild/val/commit/cb04e586db1e3ffd20ec1fee744561b1c8f8cffd), [`cb04e58`](https://github.com/valbuild/val/commit/cb04e586db1e3ffd20ec1fee744561b1c8f8cffd)]:
|
|
20
|
+
- @valbuild/core@0.147.0
|
|
21
|
+
- @valbuild/shared@0.147.0
|
|
22
|
+
- @valbuild/ui@0.147.0
|
|
23
|
+
|
|
3
24
|
## 0.146.4
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "../../dist/declarations/src/browser/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsYnVpbGQtc2VydmVyLWJyb3dzZXIuY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9kaXN0L2RlY2xhcmF0aW9ucy9zcmMvYnJvd3Nlci9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@valbuild/core');
|
|
6
|
+
var ValRouterCore = require('../../dist/ValRouterCore-3fd3d444.cjs.dev.js');
|
|
7
|
+
require('buffer');
|
|
8
|
+
require('@valbuild/shared/internal');
|
|
9
|
+
require('zod');
|
|
10
|
+
require('zod-validation-error');
|
|
11
|
+
require('@valbuild/core/fp');
|
|
12
|
+
require('@valbuild/core/patch');
|
|
13
|
+
require('image-size');
|
|
14
|
+
|
|
15
|
+
const NO_SESSIONS = "Val: sessions are not available here -- this server runs in the browser, " + "has no secret to sign them with, and serves without authenticating.";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The platform for a server running in a browser tab.
|
|
19
|
+
*
|
|
20
|
+
* Memory mode only: there is no filesystem for `fs` mode and no credential for
|
|
21
|
+
* `http` mode. Everything else answers as a server without those things does
|
|
22
|
+
* -- no `val login` token, no credential for remote files, no link checks --
|
|
23
|
+
* and the session functions refuse, because a tab has no secret to sign with
|
|
24
|
+
* and the server is constructed with `unsafelyAllowUnauthenticated`, so
|
|
25
|
+
* nothing asks for one.
|
|
26
|
+
*/
|
|
27
|
+
const browserPlatform = {
|
|
28
|
+
createOps: (valModules, options) => {
|
|
29
|
+
if (options.mode !== "memory") {
|
|
30
|
+
throw new Error(`Val: a server in the browser runs in memory mode, not '${options.mode}'.`);
|
|
31
|
+
}
|
|
32
|
+
return new ValRouterCore.ValOpsMemory(valModules, {
|
|
33
|
+
formatter: options.formatter,
|
|
34
|
+
config: options.config,
|
|
35
|
+
sourceFiles: options.sourceFiles,
|
|
36
|
+
patchStore: options.patchStore,
|
|
37
|
+
unsafelyAllowUnauthenticated: options.unsafelyAllowUnauthenticated,
|
|
38
|
+
// Nothing can reach a server in a tab but the page it runs in.
|
|
39
|
+
warnUnauthenticated: false,
|
|
40
|
+
contentUrl: options.valContentUrl
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
probeUrl: async () => ({
|
|
44
|
+
kind: "skipped",
|
|
45
|
+
message: "Links are not checked in the browser."
|
|
46
|
+
}),
|
|
47
|
+
jwt: {
|
|
48
|
+
verifyJwt: () => ({
|
|
49
|
+
success: false,
|
|
50
|
+
reason: "missing-secret",
|
|
51
|
+
message: NO_SESSIONS
|
|
52
|
+
}),
|
|
53
|
+
decodeJwtWithoutVerifying: () => ({
|
|
54
|
+
success: false,
|
|
55
|
+
reason: "missing-secret",
|
|
56
|
+
message: NO_SESSIONS
|
|
57
|
+
}),
|
|
58
|
+
encodeJwt: () => {
|
|
59
|
+
throw new Error(NO_SESSIONS);
|
|
60
|
+
},
|
|
61
|
+
getExpire: () => Math.floor(Date.now() / 1000) + 60 * 60
|
|
62
|
+
},
|
|
63
|
+
readValLoginToken: async () => null,
|
|
64
|
+
resolveRemoteFileAuth: async () => ({
|
|
65
|
+
status: "error",
|
|
66
|
+
errorCode: "api-key-missing",
|
|
67
|
+
message: "Remote files are not available in the browser."
|
|
68
|
+
})
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** A `fetch`, as far as the Studio's client calls one. */
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Val's API, served in this tab: a `fetch` that never touches the network.
|
|
75
|
+
*
|
|
76
|
+
* The whole server -- the router's request parsing, `ValServer`'s routes,
|
|
77
|
+
* memory mode's patch chain -- runs here, exactly as it runs on a server.
|
|
78
|
+
* What is removed is the hop between them, and nothing else, which is why the
|
|
79
|
+
* demo built on it cannot drift from the product: there is no second
|
|
80
|
+
* implementation to drift. See `docs/plans/demo-mode.md`.
|
|
81
|
+
*
|
|
82
|
+
* Memory mode with no source files, because nothing here publishes (only a
|
|
83
|
+
* publish reads `.val.ts` text), and without authentication, because the only
|
|
84
|
+
* caller is the page this runs in.
|
|
85
|
+
*/
|
|
86
|
+
function createInTabValFetch(valModules, options) {
|
|
87
|
+
const route = options.route ?? "/api/val";
|
|
88
|
+
const serverConfig = {
|
|
89
|
+
mode: "memory",
|
|
90
|
+
route,
|
|
91
|
+
sourceFiles: {},
|
|
92
|
+
patchStore: options.patchStore,
|
|
93
|
+
unsafelyAllowUnauthenticated: true,
|
|
94
|
+
valContentUrl: core.DEFAULT_CONTENT_HOST,
|
|
95
|
+
config: options.config
|
|
96
|
+
};
|
|
97
|
+
const server = ValRouterCore.ValServer(valModules, serverConfig, {
|
|
98
|
+
// Draft content is the only content a demo has.
|
|
99
|
+
isEnabled: async () => true,
|
|
100
|
+
onEnable: async () => {},
|
|
101
|
+
onDisable: async () => {}
|
|
102
|
+
}, browserPlatform);
|
|
103
|
+
const handle = ValRouterCore.createValRequestHandler(route, Promise.resolve(server), toResponse);
|
|
104
|
+
return (input, init) => {
|
|
105
|
+
const base = typeof window === "undefined" ? "http://localhost" : window.location.origin;
|
|
106
|
+
// A `Request` already has an absolute URL, and `init` overrides what it
|
|
107
|
+
// carries, as it does for `fetch`. Only a string or URL is resolved.
|
|
108
|
+
return handle(input instanceof Request ? new Request(input, init) : new Request(new URL(String(input), base).href, init));
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The server's answer as a `Response`. The same conversion a host makes,
|
|
114
|
+
* less the cookies: a `Response` made in the page cannot set one, and a server
|
|
115
|
+
* that never authenticates has none to set.
|
|
116
|
+
*/
|
|
117
|
+
function toResponse(valRes) {
|
|
118
|
+
const headers = new Headers();
|
|
119
|
+
const valResHeaders = "headers" in valRes && valRes.headers || {};
|
|
120
|
+
for (const key in valResHeaders) {
|
|
121
|
+
const value = valResHeaders[key];
|
|
122
|
+
if (typeof value === "string") {
|
|
123
|
+
headers.set(key, value);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if ("json" in valRes) {
|
|
127
|
+
headers.set("Content-Type", "application/json");
|
|
128
|
+
return new Response(JSON.stringify(valRes.json), {
|
|
129
|
+
headers,
|
|
130
|
+
status: valRes.status
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (valRes.status === 302) {
|
|
134
|
+
headers.set("Location", valRes.redirectTo);
|
|
135
|
+
return new Response(null, {
|
|
136
|
+
status: valRes.status,
|
|
137
|
+
headers
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return new Response("body" in valRes ? valRes.body : null, {
|
|
141
|
+
headers,
|
|
142
|
+
status: valRes.status
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
exports.createInTabValFetch = createInTabValFetch;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@valbuild/core');
|
|
6
|
+
var ValRouterCore = require('../../dist/ValRouterCore-7ffcc08c.cjs.prod.js');
|
|
7
|
+
require('buffer');
|
|
8
|
+
require('@valbuild/shared/internal');
|
|
9
|
+
require('zod');
|
|
10
|
+
require('zod-validation-error');
|
|
11
|
+
require('@valbuild/core/fp');
|
|
12
|
+
require('@valbuild/core/patch');
|
|
13
|
+
require('image-size');
|
|
14
|
+
|
|
15
|
+
const NO_SESSIONS = "Val: sessions are not available here -- this server runs in the browser, " + "has no secret to sign them with, and serves without authenticating.";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The platform for a server running in a browser tab.
|
|
19
|
+
*
|
|
20
|
+
* Memory mode only: there is no filesystem for `fs` mode and no credential for
|
|
21
|
+
* `http` mode. Everything else answers as a server without those things does
|
|
22
|
+
* -- no `val login` token, no credential for remote files, no link checks --
|
|
23
|
+
* and the session functions refuse, because a tab has no secret to sign with
|
|
24
|
+
* and the server is constructed with `unsafelyAllowUnauthenticated`, so
|
|
25
|
+
* nothing asks for one.
|
|
26
|
+
*/
|
|
27
|
+
const browserPlatform = {
|
|
28
|
+
createOps: (valModules, options) => {
|
|
29
|
+
if (options.mode !== "memory") {
|
|
30
|
+
throw new Error(`Val: a server in the browser runs in memory mode, not '${options.mode}'.`);
|
|
31
|
+
}
|
|
32
|
+
return new ValRouterCore.ValOpsMemory(valModules, {
|
|
33
|
+
formatter: options.formatter,
|
|
34
|
+
config: options.config,
|
|
35
|
+
sourceFiles: options.sourceFiles,
|
|
36
|
+
patchStore: options.patchStore,
|
|
37
|
+
unsafelyAllowUnauthenticated: options.unsafelyAllowUnauthenticated,
|
|
38
|
+
// Nothing can reach a server in a tab but the page it runs in.
|
|
39
|
+
warnUnauthenticated: false,
|
|
40
|
+
contentUrl: options.valContentUrl
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
probeUrl: async () => ({
|
|
44
|
+
kind: "skipped",
|
|
45
|
+
message: "Links are not checked in the browser."
|
|
46
|
+
}),
|
|
47
|
+
jwt: {
|
|
48
|
+
verifyJwt: () => ({
|
|
49
|
+
success: false,
|
|
50
|
+
reason: "missing-secret",
|
|
51
|
+
message: NO_SESSIONS
|
|
52
|
+
}),
|
|
53
|
+
decodeJwtWithoutVerifying: () => ({
|
|
54
|
+
success: false,
|
|
55
|
+
reason: "missing-secret",
|
|
56
|
+
message: NO_SESSIONS
|
|
57
|
+
}),
|
|
58
|
+
encodeJwt: () => {
|
|
59
|
+
throw new Error(NO_SESSIONS);
|
|
60
|
+
},
|
|
61
|
+
getExpire: () => Math.floor(Date.now() / 1000) + 60 * 60
|
|
62
|
+
},
|
|
63
|
+
readValLoginToken: async () => null,
|
|
64
|
+
resolveRemoteFileAuth: async () => ({
|
|
65
|
+
status: "error",
|
|
66
|
+
errorCode: "api-key-missing",
|
|
67
|
+
message: "Remote files are not available in the browser."
|
|
68
|
+
})
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** A `fetch`, as far as the Studio's client calls one. */
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Val's API, served in this tab: a `fetch` that never touches the network.
|
|
75
|
+
*
|
|
76
|
+
* The whole server -- the router's request parsing, `ValServer`'s routes,
|
|
77
|
+
* memory mode's patch chain -- runs here, exactly as it runs on a server.
|
|
78
|
+
* What is removed is the hop between them, and nothing else, which is why the
|
|
79
|
+
* demo built on it cannot drift from the product: there is no second
|
|
80
|
+
* implementation to drift. See `docs/plans/demo-mode.md`.
|
|
81
|
+
*
|
|
82
|
+
* Memory mode with no source files, because nothing here publishes (only a
|
|
83
|
+
* publish reads `.val.ts` text), and without authentication, because the only
|
|
84
|
+
* caller is the page this runs in.
|
|
85
|
+
*/
|
|
86
|
+
function createInTabValFetch(valModules, options) {
|
|
87
|
+
const route = options.route ?? "/api/val";
|
|
88
|
+
const serverConfig = {
|
|
89
|
+
mode: "memory",
|
|
90
|
+
route,
|
|
91
|
+
sourceFiles: {},
|
|
92
|
+
patchStore: options.patchStore,
|
|
93
|
+
unsafelyAllowUnauthenticated: true,
|
|
94
|
+
valContentUrl: core.DEFAULT_CONTENT_HOST,
|
|
95
|
+
config: options.config
|
|
96
|
+
};
|
|
97
|
+
const server = ValRouterCore.ValServer(valModules, serverConfig, {
|
|
98
|
+
// Draft content is the only content a demo has.
|
|
99
|
+
isEnabled: async () => true,
|
|
100
|
+
onEnable: async () => {},
|
|
101
|
+
onDisable: async () => {}
|
|
102
|
+
}, browserPlatform);
|
|
103
|
+
const handle = ValRouterCore.createValRequestHandler(route, Promise.resolve(server), toResponse);
|
|
104
|
+
return (input, init) => {
|
|
105
|
+
const base = typeof window === "undefined" ? "http://localhost" : window.location.origin;
|
|
106
|
+
// A `Request` already has an absolute URL, and `init` overrides what it
|
|
107
|
+
// carries, as it does for `fetch`. Only a string or URL is resolved.
|
|
108
|
+
return handle(input instanceof Request ? new Request(input, init) : new Request(new URL(String(input), base).href, init));
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The server's answer as a `Response`. The same conversion a host makes,
|
|
114
|
+
* less the cookies: a `Response` made in the page cannot set one, and a server
|
|
115
|
+
* that never authenticates has none to set.
|
|
116
|
+
*/
|
|
117
|
+
function toResponse(valRes) {
|
|
118
|
+
const headers = new Headers();
|
|
119
|
+
const valResHeaders = "headers" in valRes && valRes.headers || {};
|
|
120
|
+
for (const key in valResHeaders) {
|
|
121
|
+
const value = valResHeaders[key];
|
|
122
|
+
if (typeof value === "string") {
|
|
123
|
+
headers.set(key, value);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if ("json" in valRes) {
|
|
127
|
+
headers.set("Content-Type", "application/json");
|
|
128
|
+
return new Response(JSON.stringify(valRes.json), {
|
|
129
|
+
headers,
|
|
130
|
+
status: valRes.status
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (valRes.status === 302) {
|
|
134
|
+
headers.set("Location", valRes.redirectTo);
|
|
135
|
+
return new Response(null, {
|
|
136
|
+
status: valRes.status,
|
|
137
|
+
headers
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return new Response("body" in valRes ? valRes.body : null, {
|
|
141
|
+
headers,
|
|
142
|
+
status: valRes.status
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
exports.createInTabValFetch = createInTabValFetch;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { DEFAULT_CONTENT_HOST } from '@valbuild/core';
|
|
2
|
+
import { j as ValOpsMemory, k as ValServer, l as createValRequestHandler } from '../../dist/ValRouterCore-f5004f8f.esm.js';
|
|
3
|
+
import 'buffer';
|
|
4
|
+
import '@valbuild/shared/internal';
|
|
5
|
+
import 'zod';
|
|
6
|
+
import 'zod-validation-error';
|
|
7
|
+
import '@valbuild/core/fp';
|
|
8
|
+
import '@valbuild/core/patch';
|
|
9
|
+
import 'image-size';
|
|
10
|
+
|
|
11
|
+
const NO_SESSIONS = "Val: sessions are not available here -- this server runs in the browser, " + "has no secret to sign them with, and serves without authenticating.";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The platform for a server running in a browser tab.
|
|
15
|
+
*
|
|
16
|
+
* Memory mode only: there is no filesystem for `fs` mode and no credential for
|
|
17
|
+
* `http` mode. Everything else answers as a server without those things does
|
|
18
|
+
* -- no `val login` token, no credential for remote files, no link checks --
|
|
19
|
+
* and the session functions refuse, because a tab has no secret to sign with
|
|
20
|
+
* and the server is constructed with `unsafelyAllowUnauthenticated`, so
|
|
21
|
+
* nothing asks for one.
|
|
22
|
+
*/
|
|
23
|
+
const browserPlatform = {
|
|
24
|
+
createOps: (valModules, options) => {
|
|
25
|
+
if (options.mode !== "memory") {
|
|
26
|
+
throw new Error(`Val: a server in the browser runs in memory mode, not '${options.mode}'.`);
|
|
27
|
+
}
|
|
28
|
+
return new ValOpsMemory(valModules, {
|
|
29
|
+
formatter: options.formatter,
|
|
30
|
+
config: options.config,
|
|
31
|
+
sourceFiles: options.sourceFiles,
|
|
32
|
+
patchStore: options.patchStore,
|
|
33
|
+
unsafelyAllowUnauthenticated: options.unsafelyAllowUnauthenticated,
|
|
34
|
+
// Nothing can reach a server in a tab but the page it runs in.
|
|
35
|
+
warnUnauthenticated: false,
|
|
36
|
+
contentUrl: options.valContentUrl
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
probeUrl: async () => ({
|
|
40
|
+
kind: "skipped",
|
|
41
|
+
message: "Links are not checked in the browser."
|
|
42
|
+
}),
|
|
43
|
+
jwt: {
|
|
44
|
+
verifyJwt: () => ({
|
|
45
|
+
success: false,
|
|
46
|
+
reason: "missing-secret",
|
|
47
|
+
message: NO_SESSIONS
|
|
48
|
+
}),
|
|
49
|
+
decodeJwtWithoutVerifying: () => ({
|
|
50
|
+
success: false,
|
|
51
|
+
reason: "missing-secret",
|
|
52
|
+
message: NO_SESSIONS
|
|
53
|
+
}),
|
|
54
|
+
encodeJwt: () => {
|
|
55
|
+
throw new Error(NO_SESSIONS);
|
|
56
|
+
},
|
|
57
|
+
getExpire: () => Math.floor(Date.now() / 1000) + 60 * 60
|
|
58
|
+
},
|
|
59
|
+
readValLoginToken: async () => null,
|
|
60
|
+
resolveRemoteFileAuth: async () => ({
|
|
61
|
+
status: "error",
|
|
62
|
+
errorCode: "api-key-missing",
|
|
63
|
+
message: "Remote files are not available in the browser."
|
|
64
|
+
})
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** A `fetch`, as far as the Studio's client calls one. */
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Val's API, served in this tab: a `fetch` that never touches the network.
|
|
71
|
+
*
|
|
72
|
+
* The whole server -- the router's request parsing, `ValServer`'s routes,
|
|
73
|
+
* memory mode's patch chain -- runs here, exactly as it runs on a server.
|
|
74
|
+
* What is removed is the hop between them, and nothing else, which is why the
|
|
75
|
+
* demo built on it cannot drift from the product: there is no second
|
|
76
|
+
* implementation to drift. See `docs/plans/demo-mode.md`.
|
|
77
|
+
*
|
|
78
|
+
* Memory mode with no source files, because nothing here publishes (only a
|
|
79
|
+
* publish reads `.val.ts` text), and without authentication, because the only
|
|
80
|
+
* caller is the page this runs in.
|
|
81
|
+
*/
|
|
82
|
+
function createInTabValFetch(valModules, options) {
|
|
83
|
+
const route = options.route ?? "/api/val";
|
|
84
|
+
const serverConfig = {
|
|
85
|
+
mode: "memory",
|
|
86
|
+
route,
|
|
87
|
+
sourceFiles: {},
|
|
88
|
+
patchStore: options.patchStore,
|
|
89
|
+
unsafelyAllowUnauthenticated: true,
|
|
90
|
+
valContentUrl: DEFAULT_CONTENT_HOST,
|
|
91
|
+
config: options.config
|
|
92
|
+
};
|
|
93
|
+
const server = ValServer(valModules, serverConfig, {
|
|
94
|
+
// Draft content is the only content a demo has.
|
|
95
|
+
isEnabled: async () => true,
|
|
96
|
+
onEnable: async () => {},
|
|
97
|
+
onDisable: async () => {}
|
|
98
|
+
}, browserPlatform);
|
|
99
|
+
const handle = createValRequestHandler(route, Promise.resolve(server), toResponse);
|
|
100
|
+
return (input, init) => {
|
|
101
|
+
const base = typeof window === "undefined" ? "http://localhost" : window.location.origin;
|
|
102
|
+
// A `Request` already has an absolute URL, and `init` overrides what it
|
|
103
|
+
// carries, as it does for `fetch`. Only a string or URL is resolved.
|
|
104
|
+
return handle(input instanceof Request ? new Request(input, init) : new Request(new URL(String(input), base).href, init));
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The server's answer as a `Response`. The same conversion a host makes,
|
|
110
|
+
* less the cookies: a `Response` made in the page cannot set one, and a server
|
|
111
|
+
* that never authenticates has none to set.
|
|
112
|
+
*/
|
|
113
|
+
function toResponse(valRes) {
|
|
114
|
+
const headers = new Headers();
|
|
115
|
+
const valResHeaders = "headers" in valRes && valRes.headers || {};
|
|
116
|
+
for (const key in valResHeaders) {
|
|
117
|
+
const value = valResHeaders[key];
|
|
118
|
+
if (typeof value === "string") {
|
|
119
|
+
headers.set(key, value);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if ("json" in valRes) {
|
|
123
|
+
headers.set("Content-Type", "application/json");
|
|
124
|
+
return new Response(JSON.stringify(valRes.json), {
|
|
125
|
+
headers,
|
|
126
|
+
status: valRes.status
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
if (valRes.status === 302) {
|
|
130
|
+
headers.set("Location", valRes.redirectTo);
|
|
131
|
+
return new Response(null, {
|
|
132
|
+
status: valRes.status,
|
|
133
|
+
headers
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return new Response("body" in valRes ? valRes.body : null, {
|
|
137
|
+
headers,
|
|
138
|
+
status: valRes.status
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export { createInTabValFetch };
|