sepal 0.1.0-alpha.2 → 0.3.0-alpha.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/README.md +16 -9
- package/cli.js +127 -10
- package/package.json +48 -21
- package/src/abort.js +129 -0
- package/src/app.js +13 -0
- package/src/dom.js +173 -60
- package/src/fetch.js +347 -80
- package/src/form-data.js +141 -0
- package/src/host-call.js +57 -0
- package/src/index.js +14 -12
- package/src/lru.js +45 -0
- package/src/native.js +32 -0
- package/src/notifications.js +60 -0
- package/src/os.js +35 -0
- package/src/process.js +164 -0
- package/src/protocol.js +3 -4
- package/src/renderer.js +140 -10
- package/src/secrets.js +42 -0
- package/src/settings.js +52 -0
- package/src/shims.js +111 -11
- package/src/stem.js +125 -0
- package/src/updater.js +3 -2
- package/src/url.js +65 -20
- package/src/web.js +86 -0
- package/src/window.js +6 -2
- package/types/abort.d.ts +46 -0
- package/types/app.d.ts +7 -0
- package/types/fetch.d.ts +118 -28
- package/types/form-data.d.ts +19 -0
- package/types/host-call.d.ts +18 -0
- package/types/index.d.ts +5 -2
- package/types/lru.d.ts +18 -0
- package/types/native.d.ts +22 -0
- package/types/notifications.d.ts +53 -0
- package/types/os.d.ts +53 -0
- package/types/process.d.ts +90 -0
- package/types/protocol.d.ts +0 -2
- package/types/renderer.d.ts +16 -1
- package/types/secrets.d.ts +22 -0
- package/types/settings.d.ts +29 -0
- package/types/shims.d.ts +13 -0
- package/types/stem.d.ts +21 -0
- package/types/updater.d.ts +6 -4
- package/types/url.d.ts +1 -1
- package/types/vite/adapter.d.ts +22 -0
- package/types/vite/add.d.ts +77 -0
- package/types/vite/app.d.ts +52 -1
- package/types/vite/licenses.d.ts +11 -0
- package/types/vite/native.d.ts +135 -0
- package/types/vite/page.d.ts +20 -0
- package/types/vite/permissions.d.ts +56 -0
- package/types/vite/sveltekit.d.ts +5 -34
- package/types/vite/vite.d.ts +86 -53
- package/types/web.d.ts +17 -0
- package/types/window.d.ts +5 -1
- package/vite/adapter.js +118 -0
- package/vite/add.js +254 -0
- package/vite/app.js +195 -27
- package/vite/head.js +2 -2
- package/vite/licenses.js +115 -0
- package/vite/native.js +535 -0
- package/vite/page.js +95 -0
- package/vite/permissions.d.ts +56 -0
- package/vite/sveltekit.js +11 -224
- package/vite/vite.js +377 -147
- package/ambient.d.ts +0 -20
- package/types/vite/navigation.d.ts +0 -1
- package/types/vite/router-entry.d.ts +0 -1
- package/types/vite/router.d.ts +0 -21
- package/types/vite/routes.d.ts +0 -41
- package/types/vite/routing.svelte.d.ts +0 -131
- package/types/vite/state.d.ts +0 -1
- package/vite/Router.svelte +0 -32
- package/vite/RouterError.svelte +0 -11
- package/vite/navigation.js +0 -3
- package/vite/router-entry.js +0 -12
- package/vite/router.js +0 -17
- package/vite/routes.js +0 -219
- package/vite/routing.svelte.js +0 -378
- package/vite/state.js +0 -2
- package/vite/virtual.d.ts +0 -9
package/src/host-call.js
CHANGED
|
@@ -40,3 +40,60 @@ export function settle_call(id, ok, payload) {
|
|
|
40
40
|
if (ok) waiting.resolve(JSON.parse(payload));
|
|
41
41
|
else waiting.reject(new Error(payload));
|
|
42
42
|
}
|
|
43
|
+
|
|
44
|
+
// Events from Rust (`emit` in a command or a plugin's setup), by name. The
|
|
45
|
+
// host queues a name's events for this runtime only once it listens for it
|
|
46
|
+
// (`events.listen`), so an event nobody hears costs nothing.
|
|
47
|
+
|
|
48
|
+
/** @typedef {(value: any, cut?: { dropped: number }) => void} Listener */
|
|
49
|
+
|
|
50
|
+
/** @type {Map<string, Set<Listener>>} */
|
|
51
|
+
const listeners = new Map();
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Listen for the event `name`. Listening starts with the host calls of this
|
|
55
|
+
* turn, in order, so a command called after this in the same turn cannot
|
|
56
|
+
* send one too early.
|
|
57
|
+
* @param {string} name
|
|
58
|
+
* @param {Listener} listener
|
|
59
|
+
* @returns {() => void} removes the listener
|
|
60
|
+
*/
|
|
61
|
+
export function listen(name, listener) {
|
|
62
|
+
let set = listeners.get(name);
|
|
63
|
+
if (set === undefined) {
|
|
64
|
+
const added = /** @type {Set<Listener>} */ (new Set());
|
|
65
|
+
set = added;
|
|
66
|
+
listeners.set(name, added);
|
|
67
|
+
host_call('events.listen', { name, listening: true }).catch((/** @type {Error} */ error) => {
|
|
68
|
+
// Refused (a plugin's event without its key): nothing will come.
|
|
69
|
+
if (listeners.get(name) === added) listeners.delete(name);
|
|
70
|
+
/** @type {any} */ (globalThis).console.error(error.message);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const own = set;
|
|
74
|
+
own.add(listener);
|
|
75
|
+
return () => {
|
|
76
|
+
if (!own.delete(listener) || own.size > 0 || listeners.get(name) !== own) return;
|
|
77
|
+
listeners.delete(name);
|
|
78
|
+
host_call('events.listen', { name, listening: false }).catch(() => {});
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* An event from the host, as JSON: `[name, value]`, or `[name, null, dropped]`
|
|
84
|
+
* where the host dropped `dropped` of them. Wired to `__sb.event`.
|
|
85
|
+
* @param {string} json
|
|
86
|
+
*/
|
|
87
|
+
export function deliver_event(json) {
|
|
88
|
+
const [name, value, dropped] = JSON.parse(json);
|
|
89
|
+
const set = listeners.get(name);
|
|
90
|
+
if (set === undefined) return;
|
|
91
|
+
for (const listener of [...set]) {
|
|
92
|
+
try {
|
|
93
|
+
if (dropped === undefined) listener(value);
|
|
94
|
+
else listener(undefined, { dropped });
|
|
95
|
+
} catch (error) {
|
|
96
|
+
/** @type {any} */ (globalThis).console.error(`Error in a listener for ${name}:`, error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,33 +1,29 @@
|
|
|
1
1
|
// Shims must be evaluated before Svelte's runtime.
|
|
2
|
-
import { run_timers, next_timer, track_rejection, report_rejections, rejection_count } from './shims.js';
|
|
2
|
+
import { run_timers, next_timer, run_frame, frame_pending, track_rejection, report_rejections, rejection_count } from './shims.js';
|
|
3
3
|
import { mount as svelte_mount, unmount } from 'svelte';
|
|
4
4
|
import renderer, { root, dispatch, take_batch, stats, keep_alive, host_hooks } from './renderer.js';
|
|
5
|
-
import { settle_call } from './host-call.js';
|
|
6
|
-
import
|
|
5
|
+
import { settle_call, deliver_event } from './host-call.js';
|
|
6
|
+
import './web.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Mount a component into the window
|
|
9
|
+
* Mount a component into the window: into the page's `<body>`, or into
|
|
10
|
+
* `target`, an element of the page (with the DOM layer,
|
|
11
|
+
* `document.getElementById('app')`).
|
|
10
12
|
*
|
|
11
13
|
* @template {Record<string, any>} Props
|
|
12
14
|
* @template {Record<string, any>} Exports
|
|
13
15
|
* @param {import('svelte').Component<Props, Exports, any>} component
|
|
14
|
-
* @param {{ props?: Props, context?: Map<any, any
|
|
16
|
+
* @param {{ props?: Props, context?: Map<any, any>, target?: object | null }} [options]
|
|
15
17
|
* @returns {Exports} the component's exports; pass it to `unmount`
|
|
16
18
|
*/
|
|
17
19
|
export function mount(component, options = {}) {
|
|
18
20
|
// Svelte's own signature makes `props` required when the component has
|
|
19
21
|
// required props; ours keeps it optional, hence the cast.
|
|
20
|
-
return svelte_mount(component, /** @type {any} */ ({ ...options, target: root, renderer }));
|
|
22
|
+
return svelte_mount(component, /** @type {any} */ ({ ...options, target: options.target ?? root, renderer }));
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export { unmount, renderer, root };
|
|
24
26
|
|
|
25
|
-
// The engine has no `fetch`. This one asks the host, which checks the app's
|
|
26
|
-
// permissions. The classes around it come along: libraries test for them.
|
|
27
|
-
for (const [name, value] of Object.entries({ fetch, Headers, Request, Response })) {
|
|
28
|
-
if (typeof (/** @type {any} */ (globalThis)[name]) !== 'function') /** @type {any} */ (globalThis)[name] = value;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
27
|
// The host's entry points into JS. It calls these and nothing else.
|
|
32
28
|
/** @type {any} */ (globalThis).__sb = {
|
|
33
29
|
/** Deliver a DOM event. Returns DISPATCH_* flags. */
|
|
@@ -38,8 +34,14 @@ for (const [name, value] of Object.entries({ fetch, Headers, Request, Response }
|
|
|
38
34
|
timers: run_timers,
|
|
39
35
|
/** The next timer deadline on the host clock, or -1. Read at the end of a turn. */
|
|
40
36
|
next_timer,
|
|
37
|
+
/** Run one `requestAnimationFrame` callback; `false` when this frame has none left. Called until it says so. */
|
|
38
|
+
frame: run_frame,
|
|
39
|
+
/** Whether a `requestAnimationFrame` callback waits for a frame. Read at the end of a turn. */
|
|
40
|
+
frame_pending,
|
|
41
41
|
/** Answer a call the app made to the host (see host-call.js). */
|
|
42
42
|
resolve: settle_call,
|
|
43
|
+
/** An event from Rust, `[name, value]` as JSON (see host-call.js). */
|
|
44
|
+
event: deliver_event,
|
|
43
45
|
/**
|
|
44
46
|
* The viewport size and the boxes that changed, `[id, x, y, width, height, …]`,
|
|
45
47
|
* for the nodes the app measures. Only called once it has measured one.
|
package/src/lru.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Caches of what is looked up by its text: selectors, links, URLs. Each has a
|
|
2
|
+
// limit. A desktop app runs for days, and libraries make selectors and URLs
|
|
3
|
+
// with generated ids in them (Bits UI's, an app's own), so a cache without
|
|
4
|
+
// one grows for as long as the app runs.
|
|
5
|
+
//
|
|
6
|
+
// For module-level caches only: every cache made here is kept for
|
|
7
|
+
// `__sb.stats()`, so one made per component or element would never be freed.
|
|
8
|
+
|
|
9
|
+
/** The caches made so far, by name, for `__sb.stats()`. @type {Map<string, Map<any, any>>} */
|
|
10
|
+
const made = new Map();
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A least-recently-used cache of at most `limit` entries, in a Map's order:
|
|
14
|
+
* a hit moves its entry to the end, and past the limit the oldest goes.
|
|
15
|
+
* `undefined` is never stored: it is what a miss returns.
|
|
16
|
+
* @template K, V
|
|
17
|
+
* @param {string} name for `cache_sizes`
|
|
18
|
+
* @param {number} limit
|
|
19
|
+
*/
|
|
20
|
+
export function lru(name, limit) {
|
|
21
|
+
/** @type {Map<K, V>} */
|
|
22
|
+
const map = new Map();
|
|
23
|
+
made.set(name, map);
|
|
24
|
+
return {
|
|
25
|
+
/** @param {K} key @returns {V | undefined} */
|
|
26
|
+
get(key) {
|
|
27
|
+
const value = map.get(key);
|
|
28
|
+
if (value === undefined) return undefined;
|
|
29
|
+
map.delete(key);
|
|
30
|
+
map.set(key, value);
|
|
31
|
+
return value;
|
|
32
|
+
},
|
|
33
|
+
/** @param {K} key @param {V} value */
|
|
34
|
+
set(key, value) {
|
|
35
|
+
map.delete(key);
|
|
36
|
+
map.set(key, value);
|
|
37
|
+
if (map.size > limit) map.delete(/** @type {K} */ (map.keys().next().value));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** How many entries each cache holds, by name. */
|
|
43
|
+
export function cache_sizes() {
|
|
44
|
+
return Object.fromEntries([...made].map(([name, map]) => [name, map.size]));
|
|
45
|
+
}
|
package/src/native.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Commands written in Rust: an app's own, registered in its `native/` crate
|
|
2
|
+
// (`sepal::App::new().command('tracker.import', …)`), and its plugins'
|
|
3
|
+
// (`watch.start`, usually called through the plugin's own package). The same
|
|
4
|
+
// in the page and in the stem. And the events they send back.
|
|
5
|
+
|
|
6
|
+
import { host_call, listen } from './host-call.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Call a command. A plugin's needs its key in `permissions`; an app's own
|
|
10
|
+
* needs none.
|
|
11
|
+
* @param {string} name e.g. `tracker.import`
|
|
12
|
+
* @param {unknown} [args] anything JSON can carry
|
|
13
|
+
* @returns {Promise<any>} the command's answer; rejects with its error
|
|
14
|
+
*/
|
|
15
|
+
export function invoke(name, args = null) {
|
|
16
|
+
return host_call(`native.${name}`, args);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Listen for an event a command or a plugin sends from Rust. A plugin's
|
|
21
|
+
* events (`watch.change`) need its key in `permissions`, as its commands do;
|
|
22
|
+
* without it the refusal is printed and nothing arrives. Events are dropped
|
|
23
|
+
* when they come faster than the app takes them: the listener is then called
|
|
24
|
+
* once with no value and the count, `listener(undefined, { dropped })`,
|
|
25
|
+
* where they would have been.
|
|
26
|
+
* @param {string} name e.g. `watch.change`
|
|
27
|
+
* @param {(value: any, cut?: { dropped: number }) => void} listener
|
|
28
|
+
* @returns {() => void} removes the listener
|
|
29
|
+
*/
|
|
30
|
+
export function on(name, listener) {
|
|
31
|
+
return listen(name, listener);
|
|
32
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// The system's notifications, under `permissions.notifications`: a title,
|
|
2
|
+
// a body, and a click on the notification itself. macOS shows them through
|
|
3
|
+
// Notification Centre, which asks the user first; Linux through the
|
|
4
|
+
// desktop's notification server, or the portal inside Flatpak.
|
|
5
|
+
|
|
6
|
+
import { host_call, listen } from './host-call.js';
|
|
7
|
+
|
|
8
|
+
/** @typedef {'granted' | 'denied' | 'default'} Permission the web's names; `default` is not asked yet */
|
|
9
|
+
/** @typedef {{ id: string, data: any }} Click the notification that was clicked: its id, and the `data` given to `notify()` */
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Whether the user lets this app show notifications. On Linux always
|
|
13
|
+
* `granted`: nothing asks there.
|
|
14
|
+
* @returns {Promise<Permission>}
|
|
15
|
+
*/
|
|
16
|
+
export function permission() {
|
|
17
|
+
return host_call('notifications.permission');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Ask the user, where the system asks and they have not answered yet;
|
|
22
|
+
* settles with their answer. `notify()` asks too, the first time, and
|
|
23
|
+
* does not settle while the prompt is unanswered: call this to choose the
|
|
24
|
+
* moment.
|
|
25
|
+
* @returns {Promise<Permission>}
|
|
26
|
+
*/
|
|
27
|
+
export function requestPermission() {
|
|
28
|
+
return host_call('notifications.request');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Show a notification. `data` is any JSON, up to 4 KB, and comes back with
|
|
33
|
+
* a click: put in it what says which thing the notification is about
|
|
34
|
+
* (`{ issue: 12 }`), since the id is only good for this run of the app.
|
|
35
|
+
* @param {{ title: string, body?: string, data?: unknown }} notification
|
|
36
|
+
* @returns {Promise<string>} its id; rejects when the user has turned the
|
|
37
|
+
* app's notifications off, or there is nothing to show them. On macOS
|
|
38
|
+
* the first call asks the user, and waits for the answer
|
|
39
|
+
*/
|
|
40
|
+
export function notify(notification) {
|
|
41
|
+
return host_call('notifications.notify', notification);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Hear clicks on this app's notifications. A click nobody had heard yet is
|
|
46
|
+
* kept for the first listener: on macOS a click can start the app, so call
|
|
47
|
+
* this when the app starts and that click is the first one it hears, with
|
|
48
|
+
* the `data` its notification was given by the run before.
|
|
49
|
+
*
|
|
50
|
+
* A click goes to the runtime that showed the notification (the page, or
|
|
51
|
+
* the server code), or to every runtime that listens once that one has gone.
|
|
52
|
+
* @param {(click: Click) => void} listener
|
|
53
|
+
* @returns {() => void} stops listening
|
|
54
|
+
*/
|
|
55
|
+
export function onClick(listener) {
|
|
56
|
+
return listen('notifications.click', (/** @type {Click | undefined} */ click) => {
|
|
57
|
+
// Dropped clicks (more than the host queues) are just missed.
|
|
58
|
+
if (click) listener(click);
|
|
59
|
+
});
|
|
60
|
+
}
|
package/src/os.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// The system the app runs on. `info()` needs `permissions.os`; an
|
|
2
|
+
// environment variable is read only by name, and only one listed in
|
|
3
|
+
// `permissions.os.env`: they often hold tokens. The locale is every app's,
|
|
4
|
+
// as `navigator.language`, with no permission.
|
|
5
|
+
|
|
6
|
+
import { host_call } from './host-call.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} Info
|
|
10
|
+
* @property {string} platform `macos`, `linux` or `windows`
|
|
11
|
+
* @property {string} arch `arm64` or `x64`, as the npm packages and the update feed name them
|
|
12
|
+
* @property {string | null} name the system's name: `macOS`, or the Linux distribution's (`Ubuntu`)
|
|
13
|
+
* @property {string | null} version the system's version: `15.3`, `24.04`
|
|
14
|
+
* @property {string} locale the user's preferred locale, `en-IN`
|
|
15
|
+
* @property {string[]} locales every locale the user prefers, in order
|
|
16
|
+
* @property {string | null} hostname
|
|
17
|
+
* @property {boolean | null} dark whether the user chose a dark appearance; null where the host cannot tell
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** @returns {Promise<Info>} */
|
|
21
|
+
export function info() {
|
|
22
|
+
return host_call('os.info');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* An environment variable listed in `permissions.os.env`, or null when it
|
|
27
|
+
* is not set. The app's own environment: a bundle opened from Finder does
|
|
28
|
+
* not see what a shell profile exports, so this can answer differently in
|
|
29
|
+
* dev and in the shipped app.
|
|
30
|
+
* @param {string} name
|
|
31
|
+
* @returns {Promise<string | null>}
|
|
32
|
+
*/
|
|
33
|
+
export function env(name) {
|
|
34
|
+
return host_call('os.env', { name });
|
|
35
|
+
}
|
package/src/process.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Running other programs, under `permissions.process`: `true`, or a list of
|
|
2
|
+
// program names and absolute paths. Never through a shell: a program and
|
|
3
|
+
// its arguments, as they are. The host collects a child's output and
|
|
4
|
+
// signals once (`process.output`); this takes what there is
|
|
5
|
+
// (`process.read`). Nothing here runs on a timer.
|
|
6
|
+
|
|
7
|
+
import { host_call, listen } from './host-call.js';
|
|
8
|
+
|
|
9
|
+
/** @typedef {{ code: number | null, signal: string | null }} Exit how it ended: its exit code, or the signal that ended it */
|
|
10
|
+
/** @typedef {{ stdout: string, stderr: string }} Output */
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} Options
|
|
13
|
+
* @property {string} [cwd] the folder it runs in. A bundled app's own is `/`: give one.
|
|
14
|
+
* @property {Record<string, string>} [env] variables added to the app's own
|
|
15
|
+
* @property {(output: Output) => void} [onOutput]
|
|
16
|
+
* hears what it writes from the first byte on; `child.onOutput` adds more
|
|
17
|
+
*/
|
|
18
|
+
/** @typedef {'SIGTERM' | 'SIGKILL' | 'SIGINT' | 'SIGHUP' | 'SIGQUIT'} Signal */
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {object} Child
|
|
21
|
+
* @property {number} pid
|
|
22
|
+
* @property {(listener: (output: Output) => void) => () => void} onOutput
|
|
23
|
+
* hear what it writes, as text, as it comes; returns what stops it. To
|
|
24
|
+
* miss nothing, give `onOutput` to `spawn()`: what is taken while nobody
|
|
25
|
+
* listens is dropped.
|
|
26
|
+
* @property {(data: string) => Promise<void>} write to its input
|
|
27
|
+
* @property {() => Promise<void>} close end its input
|
|
28
|
+
* @property {(signal?: Signal) => Promise<void>} kill
|
|
29
|
+
* signal it and everything it started (its process group); SIGTERM unless said
|
|
30
|
+
* @property {Promise<Exit>} exit settles once, after its last output
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/** What takes each running child's output, by id. @type {Map<number, () => void>} */
|
|
34
|
+
const children = new Map();
|
|
35
|
+
/** @type {(() => void) | null} */
|
|
36
|
+
let off = null;
|
|
37
|
+
|
|
38
|
+
/** `run()` keeps at most this much output, in characters. */
|
|
39
|
+
const RUN_LIMIT = 1 << 20;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Start a program. `program` is a name or an absolute path, and has to be
|
|
43
|
+
* in `permissions.process` as written (unless that is `true`). A name is
|
|
44
|
+
* looked for in the app's `PATH` and then in the usual install folders
|
|
45
|
+
* (`/usr/local/bin`, Homebrew's, `~/.local/bin`, `~/.cargo/bin`, the
|
|
46
|
+
* version managers' shim folders, …), because a bundled app opened from
|
|
47
|
+
* Finder does not get the shell profile's `PATH`. nvm's folders are not
|
|
48
|
+
* among them: list the absolute path.
|
|
49
|
+
*
|
|
50
|
+
* The child belongs to this runtime: when it goes (a reload, the window
|
|
51
|
+
* closing, the app quitting), the child and what it started are killed.
|
|
52
|
+
* A child that writes more than 256 KB nobody takes waits, as it would on
|
|
53
|
+
* a terminal nobody reads.
|
|
54
|
+
*
|
|
55
|
+
* Output is text (UTF-8). A program that writes binary data to its output
|
|
56
|
+
* comes out damaged: have it write to a file, and read that.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} program
|
|
59
|
+
* @param {string[]} [args]
|
|
60
|
+
* @param {Options} [options]
|
|
61
|
+
* @returns {Promise<Child>} rejects when it is not allowed, not found or cannot start
|
|
62
|
+
*/
|
|
63
|
+
export async function spawn(program, args = [], options = {}) {
|
|
64
|
+
off ??= listen('process.output', (/** @type {{ id: number } | undefined} */ signal, /** @type {unknown} */ cut) => {
|
|
65
|
+
// The host dropped signals: whose they were is lost, so every
|
|
66
|
+
// child takes what it has.
|
|
67
|
+
if (cut) for (const take of [...children.values()]) take();
|
|
68
|
+
else if (signal) children.get(signal.id)?.();
|
|
69
|
+
});
|
|
70
|
+
const { id, pid } = await host_call('process.spawn', { program, args, cwd: options.cwd, env: options.env });
|
|
71
|
+
/** @type {Set<(output: Output) => void>} */
|
|
72
|
+
const listeners = new Set();
|
|
73
|
+
if (options.onOutput) listeners.add(options.onOutput);
|
|
74
|
+
/** @type {(exit: Exit) => void} */
|
|
75
|
+
let ended = () => {};
|
|
76
|
+
const exit = /** @type {Promise<Exit>} */ (new Promise((resolve) => (ended = resolve)));
|
|
77
|
+
// One read at a time, in order; a signal during one means another.
|
|
78
|
+
let reading = false;
|
|
79
|
+
let again = false;
|
|
80
|
+
const take = () => {
|
|
81
|
+
if (reading) {
|
|
82
|
+
again = true;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
reading = true;
|
|
86
|
+
host_call('process.read', { id }).then(
|
|
87
|
+
(/** @type {Output & { exit?: Exit }} */ taken) => {
|
|
88
|
+
reading = false;
|
|
89
|
+
if (taken.stdout || taken.stderr) {
|
|
90
|
+
for (const listener of [...listeners]) listener({ stdout: taken.stdout, stderr: taken.stderr });
|
|
91
|
+
}
|
|
92
|
+
if (taken.exit) {
|
|
93
|
+
children.delete(id);
|
|
94
|
+
ended(taken.exit);
|
|
95
|
+
} else if (again) {
|
|
96
|
+
again = false;
|
|
97
|
+
take();
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
(/** @type {Error} */ error) => {
|
|
101
|
+
// The host no longer has it (it cannot say how it ended).
|
|
102
|
+
reading = false;
|
|
103
|
+
children.delete(id);
|
|
104
|
+
/** @type {any} */ (globalThis).console.error(error.message);
|
|
105
|
+
ended({ code: null, signal: null });
|
|
106
|
+
}
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
children.set(id, take);
|
|
110
|
+
// A signal that came before `process.spawn` answered found no child
|
|
111
|
+
// here, and counts as sent until something reads. Read now.
|
|
112
|
+
take();
|
|
113
|
+
return {
|
|
114
|
+
pid,
|
|
115
|
+
onOutput(listener) {
|
|
116
|
+
listeners.add(listener);
|
|
117
|
+
return () => void listeners.delete(listener);
|
|
118
|
+
},
|
|
119
|
+
write: (data) => host_call('process.write', { id, data }),
|
|
120
|
+
close: () => host_call('process.close', { id }),
|
|
121
|
+
kill: (signal = 'SIGTERM') => host_call('process.kill', { id, signal }),
|
|
122
|
+
exit
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Run a program to its end and collect what it wrote, up to 1 MB. Its
|
|
128
|
+
* input is closed. A program that fails still resolves: look at `code`.
|
|
129
|
+
* @param {string} program
|
|
130
|
+
* @param {string[]} [args]
|
|
131
|
+
* @param {Options} [options]
|
|
132
|
+
* @returns {Promise<Exit & Output>} rejects past 1 MB of output (the program is killed)
|
|
133
|
+
*/
|
|
134
|
+
export async function run(program, args = [], options = {}) {
|
|
135
|
+
let stdout = '';
|
|
136
|
+
let stderr = '';
|
|
137
|
+
let over = false;
|
|
138
|
+
const child = await spawn(program, args, {
|
|
139
|
+
...options,
|
|
140
|
+
onOutput(output) {
|
|
141
|
+
if (over) return;
|
|
142
|
+
stdout += output.stdout;
|
|
143
|
+
stderr += output.stderr;
|
|
144
|
+
if (stdout.length + stderr.length > RUN_LIMIT) {
|
|
145
|
+
over = true;
|
|
146
|
+
child.kill('SIGKILL').catch(() => {});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
child.close().catch(() => {});
|
|
151
|
+
const exit = await child.exit;
|
|
152
|
+
if (over) throw new Error(`${program} wrote more than 1 MB: use spawn() and take its output as it comes`);
|
|
153
|
+
return { ...exit, stdout, stderr };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The file that would run for a program in `permissions.process`, or null
|
|
158
|
+
* when there is none: to say so before the user needs it.
|
|
159
|
+
* @param {string} program a listed name or absolute path
|
|
160
|
+
* @returns {Promise<string | null>}
|
|
161
|
+
*/
|
|
162
|
+
export function which(program) {
|
|
163
|
+
return host_call('process.which', { program });
|
|
164
|
+
}
|
package/src/protocol.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
//
|
|
3
3
|
// A batch is { ops: Int32Array, strings: string[] }. `ops` is a flat list of
|
|
4
4
|
// records: an opcode followed by a fixed number of integer operands. Operands
|
|
5
|
-
// are node ids
|
|
5
|
+
// are node ids or indexes into `strings`. Node ids are never reused: the host
|
|
6
|
+
// numbers the nodes it parsed from index.html (1 to n, handed to JS once as
|
|
7
|
+
// `__sb_page`), and JS allocates the rest, from n + 1.
|
|
6
8
|
//
|
|
7
9
|
// Keep this file in sync with crates/host/src/ops.rs.
|
|
8
10
|
|
|
@@ -24,9 +26,6 @@ export const OP = {
|
|
|
24
26
|
SCROLL_INTO_VIEW: 15 // node, block, inline (scroll its ancestors; 0 start, 1 center, 2 end, 3 nearest)
|
|
25
27
|
};
|
|
26
28
|
|
|
27
|
-
/** Id of the mount target. The host maps it to <body>. */
|
|
28
|
-
export const ROOT_ID = 1;
|
|
29
|
-
|
|
30
29
|
// Flags returned from dispatch
|
|
31
30
|
export const DISPATCH_PREVENT_DEFAULT = 1;
|
|
32
31
|
export const DISPATCH_STOP_PROPAGATION = 2;
|
package/src/renderer.js
CHANGED
|
@@ -6,8 +6,13 @@
|
|
|
6
6
|
// that the host pulls once per turn with `take_batch()`. No call crosses the
|
|
7
7
|
// JS/Rust boundary while Svelte is rendering.
|
|
8
8
|
|
|
9
|
+
// Before Svelte: its runtime keeps engine functions it finds when it loads
|
|
10
|
+
// (`Object.getOwnPropertyDescriptor`), and shims.js fixes one in QuickJS.
|
|
11
|
+
// Whichever module loads Svelte first, this one comes before it.
|
|
12
|
+
import './shims.js';
|
|
9
13
|
import { createRenderer } from 'svelte/renderer';
|
|
10
|
-
import { OP,
|
|
14
|
+
import { OP, DISPATCH_PREVENT_DEFAULT, DISPATCH_STOP_PROPAGATION } from './protocol.js';
|
|
15
|
+
import { cache_sizes } from './lru.js';
|
|
11
16
|
|
|
12
17
|
const ELEMENT = 'element';
|
|
13
18
|
const TEXT = 'text';
|
|
@@ -57,7 +62,17 @@ export function take_batch() {
|
|
|
57
62
|
// Nodes
|
|
58
63
|
// ---------------------------------------------------------------------------
|
|
59
64
|
|
|
60
|
-
|
|
65
|
+
/**
|
|
66
|
+
* The page the host parsed from index.html, handed over once, before any
|
|
67
|
+
* script ran (`page.js` in the host): its nodes as ops in this file's own
|
|
68
|
+
* vocabulary, numbered from 1, and the first id this side may allocate.
|
|
69
|
+
* `held` are the nodes a script took before this module loaded.
|
|
70
|
+
* @type {{ ops: number[], strings: string[], next_id: number, held?: Map<number, object> }}
|
|
71
|
+
*/
|
|
72
|
+
const page = /** @type {any} */ (globalThis).__sb_page;
|
|
73
|
+
if (page === undefined) throw new Error('sepal: there is no page to render into. Apps run in calyx, the Sepal host, which loads them from index.html.');
|
|
74
|
+
|
|
75
|
+
let next_id = page.next_id;
|
|
61
76
|
|
|
62
77
|
export class BlitzNode {
|
|
63
78
|
/**
|
|
@@ -213,11 +228,60 @@ const elements = new Map();
|
|
|
213
228
|
|
|
214
229
|
const finalizer = has_weak_refs
|
|
215
230
|
? new FinalizationRegistry((/** @type {number} */ id) => {
|
|
231
|
+
if (by_tag !== null) by_tag.get(/** @type {any} */ (elements.get(id))?.tag)?.delete(id);
|
|
216
232
|
elements.delete(id);
|
|
217
233
|
ops.push(OP.DROP, id);
|
|
218
234
|
})
|
|
219
235
|
: null;
|
|
220
236
|
|
|
237
|
+
/**
|
|
238
|
+
* The ids of the elements alive, attached or not, by lower-case tag name:
|
|
239
|
+
* the DOM layer's selectors try only these when a selector ends in a tag
|
|
240
|
+
* (`meta[property=csp-nonce]`, `link[href=…]`, `title`), instead of walking
|
|
241
|
+
* the page. Made the first time it is asked for, so an app without the DOM
|
|
242
|
+
* layer never pays for it; after that, each element is added when it is made
|
|
243
|
+
* and deleted when it is collected. The tag rides on the element's `WeakRef`,
|
|
244
|
+
* for the finalizer, which gets only the id.
|
|
245
|
+
* @type {Map<string, Set<number>> | null}
|
|
246
|
+
*/
|
|
247
|
+
let by_tag = null;
|
|
248
|
+
|
|
249
|
+
/** @param {BlitzNode} node @param {any} entry its entry in `elements` */
|
|
250
|
+
function index_tag(node, entry) {
|
|
251
|
+
const tag = node.name.toLowerCase();
|
|
252
|
+
if (has_weak_refs) entry.tag = tag;
|
|
253
|
+
const ids = /** @type {Map<string, Set<number>>} */ (by_tag).get(tag);
|
|
254
|
+
if (ids === undefined) /** @type {Map<string, Set<number>>} */ (by_tag).set(tag, new Set([node.node_id]));
|
|
255
|
+
else ids.add(node.node_id);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The elements named `name` (lower case) that are alive, attached or not, in
|
|
260
|
+
* no particular order; `null` if there are more than `limit`.
|
|
261
|
+
* @param {string} name
|
|
262
|
+
* @param {number} limit
|
|
263
|
+
* @returns {BlitzNode[] | null}
|
|
264
|
+
*/
|
|
265
|
+
export function elements_named(name, limit) {
|
|
266
|
+
if (by_tag === null) {
|
|
267
|
+
by_tag = new Map();
|
|
268
|
+
for (const [id, entry] of elements) {
|
|
269
|
+
const node = lookup(id);
|
|
270
|
+
if (node !== null) index_tag(node, entry);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const ids = by_tag.get(name);
|
|
274
|
+
if (ids === undefined) return [];
|
|
275
|
+
if (ids.size > limit) return null;
|
|
276
|
+
/** @type {BlitzNode[]} */
|
|
277
|
+
const nodes = [];
|
|
278
|
+
for (const id of ids) {
|
|
279
|
+
const node = lookup(id);
|
|
280
|
+
if (node !== null) nodes.push(node);
|
|
281
|
+
}
|
|
282
|
+
return nodes;
|
|
283
|
+
}
|
|
284
|
+
|
|
221
285
|
/** @param {'element' | 'text' | 'comment'} type */
|
|
222
286
|
function create_node(type) {
|
|
223
287
|
const node = new BlitzNode(type, next_id++);
|
|
@@ -275,20 +339,84 @@ export function lookup(id) {
|
|
|
275
339
|
*
|
|
276
340
|
* @type {{ finalizer: FinalizationRegistry<number> | null, root: BlitzNode | null }}
|
|
277
341
|
*/
|
|
278
|
-
export const keep_alive = { finalizer, root: null };
|
|
342
|
+
export const keep_alive = { finalizer, root: /** @type {BlitzNode | null} */ (null) };
|
|
279
343
|
|
|
280
344
|
/** Counters for tests and leak hunting. */
|
|
281
345
|
export function stats() {
|
|
282
346
|
let live_elements = 0;
|
|
283
347
|
for (const id of elements.keys()) if (lookup(id) !== null) live_elements++;
|
|
284
|
-
return { created: next_id -
|
|
348
|
+
return { created: next_id - page.next_id, live_elements, pending_ops: ops.length, caches: cache_sizes() };
|
|
285
349
|
}
|
|
286
350
|
|
|
287
|
-
/**
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
351
|
+
/**
|
|
352
|
+
* Build the mirror of the page from the host's ops, writing none: the host
|
|
353
|
+
* has these nodes already. Returns the nodes, by id.
|
|
354
|
+
*/
|
|
355
|
+
function seed() {
|
|
356
|
+
/** @type {Map<number, BlitzNode>} */
|
|
357
|
+
const made = new Map();
|
|
358
|
+
const { ops: seeded, strings: text, held } = page;
|
|
359
|
+
/** @param {'element' | 'text' | 'comment'} type @param {number} id */
|
|
360
|
+
const make = (type, id) => {
|
|
361
|
+
let node = /** @type {BlitzNode | undefined} */ (held?.get(id));
|
|
362
|
+
if (node === undefined) node = new BlitzNode(type, id);
|
|
363
|
+
else {
|
|
364
|
+
// A script holds this object already (`document.currentScript`):
|
|
365
|
+
// it becomes the node, so what the script has is the real thing.
|
|
366
|
+
Object.setPrototypeOf(node, BlitzNode.prototype);
|
|
367
|
+
Object.assign(node, new BlitzNode(type, id));
|
|
368
|
+
}
|
|
369
|
+
finalizer?.register(node, id);
|
|
370
|
+
made.set(id, node);
|
|
371
|
+
return node;
|
|
372
|
+
};
|
|
373
|
+
for (let i = 0; i < seeded.length; ) {
|
|
374
|
+
const [code, a, b, c] = seeded.slice(i, i + 4);
|
|
375
|
+
switch (code) {
|
|
376
|
+
case OP.CREATE_ELEMENT: {
|
|
377
|
+
const node = make(ELEMENT, a);
|
|
378
|
+
node.name = text[b];
|
|
379
|
+
elements.set(a, has_weak_refs ? new WeakRef(node) : node);
|
|
380
|
+
i += 3;
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
case OP.CREATE_TEXT:
|
|
384
|
+
case OP.CREATE_COMMENT:
|
|
385
|
+
make(code === OP.CREATE_TEXT ? TEXT : COMMENT, a).data = text[b];
|
|
386
|
+
i += 3;
|
|
387
|
+
break;
|
|
388
|
+
case OP.SET_ATTR: {
|
|
389
|
+
const node = /** @type {BlitzNode} */ (made.get(a));
|
|
390
|
+
(node.attrs ??= new Map()).set(text[b], text[c]);
|
|
391
|
+
if (text[b] === 'value') node.value = text[c];
|
|
392
|
+
else if (text[b] === 'checked') node.checked = true;
|
|
393
|
+
i += 4;
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
case OP.INSERT:
|
|
397
|
+
link(/** @type {BlitzNode} */ (made.get(a)), /** @type {BlitzNode} */ (made.get(b)), null);
|
|
398
|
+
i += 4;
|
|
399
|
+
break;
|
|
400
|
+
default:
|
|
401
|
+
throw new Error(`sepal: the host sent a page with opcode ${code}`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return made;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** @param {BlitzNode} parent @param {string} name */
|
|
408
|
+
const child_named = (parent, name) => {
|
|
409
|
+
for (let node = parent.first; node !== null; node = node.next) if (node.name === name) return node;
|
|
410
|
+
throw new Error(`sepal: the page has no <${name}>`);
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
/** `<html>`: the page's root element. The parser always makes one, with a head and a body. */
|
|
414
|
+
export const html = [...seed().values()].find((node) => node.parent === null && node.name === 'html') ?? child_named(new BlitzNode(ELEMENT, 0), 'html');
|
|
415
|
+
/** `<head>`. */
|
|
416
|
+
export const head = child_named(html, 'head');
|
|
417
|
+
/** `<body>`: where `mount` puts the app unless told otherwise. */
|
|
418
|
+
export const root = child_named(html, 'body');
|
|
419
|
+
keep_alive.root = html;
|
|
292
420
|
|
|
293
421
|
// ---------------------------------------------------------------------------
|
|
294
422
|
// Tree bookkeeping
|
|
@@ -545,7 +673,9 @@ const renderer = createRenderer({
|
|
|
545
673
|
createElement(name) {
|
|
546
674
|
const node = create_node(ELEMENT);
|
|
547
675
|
node.name = name;
|
|
548
|
-
|
|
676
|
+
const entry = has_weak_refs ? new WeakRef(node) : node;
|
|
677
|
+
elements.set(node.node_id, entry);
|
|
678
|
+
if (by_tag !== null) index_tag(node, entry);
|
|
549
679
|
ops.push(OP.CREATE_ELEMENT, node.node_id, str(name));
|
|
550
680
|
return node;
|
|
551
681
|
},
|