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.
Files changed (81) hide show
  1. package/README.md +16 -9
  2. package/cli.js +127 -10
  3. package/package.json +48 -21
  4. package/src/abort.js +129 -0
  5. package/src/app.js +13 -0
  6. package/src/dom.js +173 -60
  7. package/src/fetch.js +347 -80
  8. package/src/form-data.js +141 -0
  9. package/src/host-call.js +57 -0
  10. package/src/index.js +14 -12
  11. package/src/lru.js +45 -0
  12. package/src/native.js +32 -0
  13. package/src/notifications.js +60 -0
  14. package/src/os.js +35 -0
  15. package/src/process.js +164 -0
  16. package/src/protocol.js +3 -4
  17. package/src/renderer.js +140 -10
  18. package/src/secrets.js +42 -0
  19. package/src/settings.js +52 -0
  20. package/src/shims.js +111 -11
  21. package/src/stem.js +125 -0
  22. package/src/updater.js +3 -2
  23. package/src/url.js +65 -20
  24. package/src/web.js +86 -0
  25. package/src/window.js +6 -2
  26. package/types/abort.d.ts +46 -0
  27. package/types/app.d.ts +7 -0
  28. package/types/fetch.d.ts +118 -28
  29. package/types/form-data.d.ts +19 -0
  30. package/types/host-call.d.ts +18 -0
  31. package/types/index.d.ts +5 -2
  32. package/types/lru.d.ts +18 -0
  33. package/types/native.d.ts +22 -0
  34. package/types/notifications.d.ts +53 -0
  35. package/types/os.d.ts +53 -0
  36. package/types/process.d.ts +90 -0
  37. package/types/protocol.d.ts +0 -2
  38. package/types/renderer.d.ts +16 -1
  39. package/types/secrets.d.ts +22 -0
  40. package/types/settings.d.ts +29 -0
  41. package/types/shims.d.ts +13 -0
  42. package/types/stem.d.ts +21 -0
  43. package/types/updater.d.ts +6 -4
  44. package/types/url.d.ts +1 -1
  45. package/types/vite/adapter.d.ts +22 -0
  46. package/types/vite/add.d.ts +77 -0
  47. package/types/vite/app.d.ts +52 -1
  48. package/types/vite/licenses.d.ts +11 -0
  49. package/types/vite/native.d.ts +135 -0
  50. package/types/vite/page.d.ts +20 -0
  51. package/types/vite/permissions.d.ts +56 -0
  52. package/types/vite/sveltekit.d.ts +5 -34
  53. package/types/vite/vite.d.ts +86 -53
  54. package/types/web.d.ts +17 -0
  55. package/types/window.d.ts +5 -1
  56. package/vite/adapter.js +118 -0
  57. package/vite/add.js +254 -0
  58. package/vite/app.js +195 -27
  59. package/vite/head.js +2 -2
  60. package/vite/licenses.js +115 -0
  61. package/vite/native.js +535 -0
  62. package/vite/page.js +95 -0
  63. package/vite/permissions.d.ts +56 -0
  64. package/vite/sveltekit.js +11 -224
  65. package/vite/vite.js +377 -147
  66. package/ambient.d.ts +0 -20
  67. package/types/vite/navigation.d.ts +0 -1
  68. package/types/vite/router-entry.d.ts +0 -1
  69. package/types/vite/router.d.ts +0 -21
  70. package/types/vite/routes.d.ts +0 -41
  71. package/types/vite/routing.svelte.d.ts +0 -131
  72. package/types/vite/state.d.ts +0 -1
  73. package/vite/Router.svelte +0 -32
  74. package/vite/RouterError.svelte +0 -11
  75. package/vite/navigation.js +0 -3
  76. package/vite/router-entry.js +0 -12
  77. package/vite/router.js +0 -17
  78. package/vite/routes.js +0 -219
  79. package/vite/routing.svelte.js +0 -378
  80. package/vite/state.js +0 -2
  81. package/vite/virtual.d.ts +0 -9
package/src/dom.js CHANGED
@@ -13,10 +13,11 @@
13
13
  // The tree is the renderer's own. An element here is a `BlitzNode`, and every
14
14
  // write goes through the renderer into the same op buffer as Svelte's.
15
15
 
16
- import { host_call } from './host-call.js';
16
+ import { AbortController, AbortSignal } from './abort.js';
17
17
  import { TextDecoder, TextEncoder } from './text.js';
18
+ import { lru } from './lru.js';
18
19
  import { URL, URLSearchParams } from './url.js';
19
- import renderer, { BlitzEvent, BlitzNode, add_listener, deliver, host_hooks, lookup, outer_targets, remove_listener, request_focus, request_measure, root, scroll_state } from './renderer.js';
20
+ import renderer, { BlitzEvent, BlitzNode, add_listener, deliver, elements_named, head, host_hooks, html, lookup, outer_targets, remove_listener, request_focus, request_measure, root, scroll_state } from './renderer.js';
20
21
 
21
22
  const g = /** @type {any} */ (globalThis);
22
23
  const proto = /** @type {any} */ (BlitzNode.prototype);
@@ -42,8 +43,12 @@ function define(target, properties) {
42
43
  const SIMPLE =
43
44
  /^(?:([a-zA-Z][\w-]*|\*)|#((?:\\.|[\w-])+)|\.((?:\\.|[\w-])+)|\[\s*([\w:-]+)\s*(?:([~|^$*]?=)\s*(?:"([^"]*)"|'([^']*)'|([^\]\s]+))\s*(?:[is]\s*)?)?\]|:not\(((?:[^()]|\([^()]*\))*)\)|:([\w-]+)(?:\([^)]*\))?)/;
44
45
 
45
- /** @type {Map<string, { compounds: Compound[], combinators: string[] }[]>} */
46
- const parsed = new Map();
46
+ /**
47
+ * Selectors as parsed, by their text. Libraries build selectors with
48
+ * generated ids in them, so it has a limit.
49
+ * @type {ReturnType<typeof lru<string, { compounds: Compound[], combinators: string[] }[]>>}
50
+ */
51
+ const parsed = lru('selectors', 256);
47
52
 
48
53
  /** @param {string} selector */
49
54
  function parse_selector(selector) {
@@ -145,8 +150,43 @@ function matches_complex(node, complex, index = complex.compounds.length - 1) {
145
150
  return false;
146
151
  }
147
152
 
153
+ /**
154
+ * The test for a selector, built once. Walks of the page call it on every
155
+ * element: SvelteKit looks for `a` and `[autofocus]` after every navigation,
156
+ * 3,500 elements on the Tracker's Board, and going through the general
157
+ * matcher for each cost 1.8 and 2.4 ms. One tag, one attribute that must be
158
+ * there, one id or one class gets a test of its own; any other compound
159
+ * without combinators is matched directly; the rest as a list. Kept by
160
+ * selector, as many as the parsed ones.
161
+ * @type {ReturnType<typeof lru<string, (node: any) => boolean>>}
162
+ */
163
+ const tests = lru('selector tests', 256);
164
+
165
+ /** @param {string} selector */
166
+ function test_for(selector) {
167
+ let test = tests.get(selector);
168
+ if (test !== undefined) return test;
169
+ const list = parse_selector(selector);
170
+ const one = list.length === 1 && list[0].compounds.length === 1 ? list[0].compounds[0] : null;
171
+ if (one === null) {
172
+ test = (node) => list.some((complex) => matches_complex(node, complex));
173
+ } else {
174
+ const parts = (one.tag !== null ? 1 : 0) + (one.id !== null ? 1 : 0) + one.classes.length + one.attrs.length + one.nots.length + one.pseudos.length;
175
+ const { tag, id } = one;
176
+ const [name] = one.classes;
177
+ const attr = one.attrs[0];
178
+ if (parts === 1 && tag !== null) test = (node) => node.type === 'element' && (node.name === tag || node.name.toLowerCase() === tag);
179
+ else if (parts === 1 && id !== null) test = (node) => node.type === 'element' && node.attrs !== null && node.attrs.get('id') === id;
180
+ else if (parts === 1 && name !== undefined) test = (node) => node.type === 'element' && node.attrs !== null && (node.attrs.get('class') ?? '').split(/\s+/).includes(name);
181
+ else if (parts === 1 && attr !== undefined && attr.op === null) test = (node) => node.type === 'element' && node.attrs !== null && node.attrs.has(attr.name);
182
+ else test = (node) => matches_compound(node, one);
183
+ }
184
+ tests.set(selector, test);
185
+ return test;
186
+ }
187
+
148
188
  /** @param {any} node @param {string} selector */
149
- const matches = (node, selector) => parse_selector(selector).some((complex) => matches_complex(node, complex));
189
+ const matches = (node, selector) => test_for(selector)(node);
150
190
 
151
191
  /** Every element below `scope`, in document order. @param {any} scope @param {(node: any) => boolean | void} visit returns true to stop */
152
192
  function walk(scope, visit) {
@@ -157,21 +197,82 @@ function walk(scope, visit) {
157
197
  return false;
158
198
  }
159
199
 
200
+ /**
201
+ * When every alternative of the selector ends in a tag name, and few
202
+ * elements have those names: the ones below `scope` that match, in no
203
+ * particular order. `null` means walk. A walk visits every element of the
204
+ * page, and Vite's preload helper alone looks for `meta[property=csp-nonce]`
205
+ * several times a navigation; a page has no such element.
206
+ * ponytail: a common tag (div, span) still walks; ordering many candidates
207
+ * would need position keys kept up to date.
208
+ * @param {any} scope @param {string} selector
209
+ */
210
+ function indexed(scope, selector) {
211
+ /** @type {Set<any>} */
212
+ const found = new Set();
213
+ for (const complex of parse_selector(selector)) {
214
+ const tag = complex.compounds[complex.compounds.length - 1].tag;
215
+ if (tag === null) return null;
216
+ const named = elements_named(tag, 64);
217
+ if (named === null) return null;
218
+ for (const node of named) if (below(node, scope) && matches_complex(node, complex)) found.add(node);
219
+ }
220
+ return found;
221
+ }
222
+
223
+ /** @param {any} node @param {any} scope */
224
+ function below(node, scope) {
225
+ for (let parent = node.parent; parent !== null; parent = parent.parent) if (parent === scope) return true;
226
+ return false;
227
+ }
228
+
229
+ /** Whether `a` comes before `b` in the page. @param {any} a @param {any} b */
230
+ function precedes(a, b) {
231
+ /** @param {any} node */
232
+ const path = (node) => {
233
+ const nodes = [];
234
+ for (; node !== null; node = node.parent) nodes.push(node);
235
+ return nodes;
236
+ };
237
+ const from_a = path(a);
238
+ const from_b = path(b);
239
+ let i = from_a.length - 1;
240
+ let j = from_b.length - 1;
241
+ while (i >= 0 && j >= 0 && from_a[i] === from_b[j]) {
242
+ i--;
243
+ j--;
244
+ }
245
+ // One is inside the other: the outer one comes first.
246
+ if (i < 0 || j < 0) return i < 0;
247
+ for (let node = from_a[i].next; node !== null; node = node.next) if (node === from_b[j]) return true;
248
+ return false;
249
+ }
250
+
160
251
  /** @param {any} scope @param {string} selector */
161
252
  function query_all(scope, selector) {
253
+ const candidates = indexed(scope, selector);
254
+ if (candidates !== null) return [...candidates].sort((a, b) => (precedes(a, b) ? -1 : 1));
162
255
  /** @type {any[]} */
163
256
  const found = [];
257
+ const test = test_for(selector);
164
258
  walk(scope, (node) => {
165
- if (matches(node, selector)) found.push(node);
259
+ if (test(node)) found.push(node);
166
260
  });
167
261
  return found;
168
262
  }
169
263
 
170
264
  /** @param {any} scope @param {string} selector */
171
265
  function query(scope, selector) {
266
+ const candidates = indexed(scope, selector);
267
+ if (candidates !== null) {
268
+ let first = null;
269
+ for (const node of candidates) if (first === null || precedes(node, first)) first = node;
270
+ return first;
271
+ }
172
272
  let found = null;
273
+ const test = test_for(selector);
173
274
  walk(scope, (node) => {
174
- if (!matches(node, selector)) return false;
275
+ if (!test(node)) return false;
175
276
  found = node;
176
277
  return true;
177
278
  });
@@ -332,31 +433,6 @@ function unlisten(target, type, handler, options) {
332
433
  remove_listener(target, type, wrappers.get(handler)?.get(capture ? `${type} capture` : type) ?? handler, capture);
333
434
  }
334
435
 
335
- class AbortSignal extends EventTarget {
336
- constructor() {
337
- super();
338
- this.aborted = false;
339
- /** @type {any} */
340
- this.reason = undefined;
341
- }
342
- throwIfAborted() {
343
- if (this.aborted) throw this.reason;
344
- }
345
- }
346
-
347
- class AbortController {
348
- constructor() {
349
- this.signal = new AbortSignal();
350
- }
351
- /** @param {any} [reason] */
352
- abort(reason) {
353
- if (this.signal.aborted) return;
354
- this.signal.aborted = true;
355
- this.signal.reason = reason ?? new Error('aborted');
356
- this.signal.dispatchEvent(new Event('abort'));
357
- }
358
- }
359
-
360
436
  // ---------------------------------------------------------------------------
361
437
  // Elements
362
438
  // ---------------------------------------------------------------------------
@@ -608,7 +684,7 @@ define(proto, {
608
684
  ownerDocument: { get: () => document },
609
685
  isConnected: {
610
686
  get() {
611
- for (let node = this; node !== null; node = node.parent) if (node === root) return true;
687
+ for (let node = this; node !== null; node = node.parent) if (node === html) return true;
612
688
  return false;
613
689
  }
614
690
  },
@@ -679,6 +755,10 @@ define(proto, {
679
755
  className: reflected('class'),
680
756
  title: reflected('title'),
681
757
  role: reflected('role'),
758
+ // A <link>'s, which Vite's preloading sets as properties: a stylesheet
759
+ // it adds ahead of a dynamic import is only one with these on the page.
760
+ rel: reflected('rel'),
761
+ as: reflected('as'),
682
762
  // For a link, the address it leads to, in full: routers read this and not
683
763
  // the attribute.
684
764
  href: {
@@ -686,7 +766,7 @@ define(proto, {
686
766
  get() {
687
767
  const written = this.attrs?.get('href');
688
768
  if (written === undefined) return '';
689
- return URL.parse(written, address.href)?.href ?? written;
769
+ return resolve_href(written, address.href);
690
770
  },
691
771
  /** @this {any} @param {any} value */
692
772
  set(value) {
@@ -819,7 +899,7 @@ Object.assign(proto, /** @type {Record<string, any> & ThisType<any>} */ ({
819
899
  if (other.contains(this)) return 8 | 2;
820
900
  /** @type {any[]} */
821
901
  const order = [];
822
- walk(root, (node) => void order.push(node));
902
+ walk(html, (node) => void order.push(node));
823
903
  return order.indexOf(this) < order.indexOf(other) ? 4 : 2;
824
904
  },
825
905
  getRootNode() {
@@ -909,6 +989,29 @@ function is_focusable(node) {
909
989
  return /^(button|input|select|textarea|summary)$/i.test(node.name) || (/^(a|area)$/i.test(node.name) && node.attrs?.has('href'));
910
990
  }
911
991
 
992
+ /**
993
+ * Where a link's `href` attribute leads from a page, by both. SvelteKit reads
994
+ * every link's `href` after each navigation, to set up preloading, and
995
+ * resolving one builds a whole `URL`: 316 of them were 1.8 ms of a Board
996
+ * visit in the Tracker. Keyed by the page's address as well, which a
997
+ * relative attribute depends on. One entry per link and page: the limit is
998
+ * a few pages of links.
999
+ * @type {ReturnType<typeof lru<string, string>>}
1000
+ */
1001
+ const resolved_hrefs = lru('links', 1024);
1002
+
1003
+ /** @param {string} written @param {string} base */
1004
+ function resolve_href(written, base) {
1005
+ // The base's length first, so that no attribute can make two pairs one key.
1006
+ const key = `${base.length}:${base}${written}`;
1007
+ let href = resolved_hrefs.get(key);
1008
+ if (href === undefined) {
1009
+ href = URL.parse(written, base)?.href ?? written;
1010
+ resolved_hrefs.set(key, href);
1011
+ }
1012
+ return href;
1013
+ }
1014
+
912
1015
  /** A DOM class that elements with one of these tag names count as. @param {((node: any) => boolean)} test */
913
1016
  const node_class = (test) => {
914
1017
  const stand_in = function () {
@@ -929,9 +1032,6 @@ const tag_class = (tags) => node_class((node) => is_element(node) && new RegExp(
929
1032
  // document and window
930
1033
  // ---------------------------------------------------------------------------
931
1034
 
932
- /** Stands in for <html> and <head>: something to hold attributes and styles that go nowhere. */
933
- const detached = () => new BlitzNode('element', 0);
934
-
935
1035
  // ---------------------------------------------------------------------------
936
1036
  // The address, and the history of it
937
1037
  //
@@ -1053,7 +1153,14 @@ function memory_storage() {
1053
1153
  );
1054
1154
  }
1055
1155
 
1056
- let page_title = '';
1156
+ /** Says once that something the page asked for is not done here. @type {Set<string>} */
1157
+ const unsupported = new Set();
1158
+ /** @param {string} what */
1159
+ function not_supported(what) {
1160
+ if (unsupported.has(what)) return;
1161
+ unsupported.add(what);
1162
+ g.console.warn(`${what} is not supported in Sepal, and did nothing`);
1163
+ }
1057
1164
 
1058
1165
  const document = {
1059
1166
  nodeType: 9,
@@ -1064,21 +1171,31 @@ const document = {
1064
1171
  return address.href;
1065
1172
  },
1066
1173
  location,
1067
- // The page's title is the window's. Changing the window needs
1068
- // `permissions.window`; without it the title is only remembered here.
1174
+ // The page's <title>, which the window shows, as a browser tab does.
1069
1175
  get title() {
1070
- return page_title;
1176
+ const title = query(html, 'title');
1177
+ return title === null ? '' : String(/** @type {any} */ (title).textContent).replace(/\s+/g, ' ').trim();
1071
1178
  },
1072
1179
  set title(value) {
1073
- page_title = String(value);
1074
- host_call('window.set_title', { title: page_title }).catch(() => {});
1180
+ /** @type {any} */
1181
+ let title = query(html, 'title');
1182
+ // As in a browser: a page without one gets a <title> in its head.
1183
+ if (title === null) renderer.insert(head, (title = renderer.createElement('title')), null);
1184
+ title.textContent = String(value);
1185
+ },
1186
+ /** The `<script>` element running now, if it is a classic script of the page. */
1187
+ get currentScript() {
1188
+ const id = g.__sb_page?.script;
1189
+ return id ? lookup(id) : null;
1075
1190
  },
1191
+ write: () => not_supported('document.write()'),
1192
+ writeln: () => not_supported('document.writeln()'),
1076
1193
  nodeName: '#document',
1077
1194
  /** @type {Map<string, Function[]> | null} */
1078
1195
  listeners: null,
1079
1196
  body: root,
1080
- documentElement: Object.assign(detached(), { name: 'html' }),
1081
- head: Object.assign(detached(), { name: 'head' }),
1197
+ documentElement: html,
1198
+ head,
1082
1199
  defaultView: g,
1083
1200
  visibilityState: 'visible',
1084
1201
  hidden: false,
@@ -1100,11 +1217,11 @@ const document = {
1100
1217
  createComment: (data) => renderer.createComment(data),
1101
1218
  createDocumentFragment: () => renderer.createFragment(),
1102
1219
  /** @param {string} name */
1103
- getElementsByTagName: (name) => query_all(root, name === '*' ? '*' : name.toLowerCase()),
1220
+ getElementsByTagName: (name) => query_all(html, name === '*' ? '*' : name.toLowerCase()),
1104
1221
  /** @param {string} id */
1105
1222
  getElementById: (id) => {
1106
1223
  let found = null;
1107
- walk(root, (node) => {
1224
+ walk(html, (node) => {
1108
1225
  if (node.attrs?.get('id') !== id) return false;
1109
1226
  found = node;
1110
1227
  return true;
@@ -1112,11 +1229,11 @@ const document = {
1112
1229
  return found;
1113
1230
  },
1114
1231
  /** @param {string} selector */
1115
- querySelector: (selector) => (matches(root, selector) ? root : query(root, selector)),
1232
+ querySelector: (selector) => (matches(html, selector) ? html : query(html, selector)),
1116
1233
  /** @param {string} selector */
1117
- querySelectorAll: (selector) => query_all(root, selector),
1234
+ querySelectorAll: (selector) => [...(matches(html, selector) ? [html] : []), ...query_all(html, selector)],
1118
1235
  /** @param {any} node */
1119
- contains: (node) => /** @type {any} */ (root).contains(node),
1236
+ contains: (node) => /** @type {any} */ (html).contains(node),
1120
1237
  getRootNode: () => document,
1121
1238
  /** @param {string} type @param {any} handler @param {any} [options] */
1122
1239
  addEventListener(type, handler, options) {
@@ -1133,11 +1250,8 @@ const document = {
1133
1250
  return !event.defaultPrevented;
1134
1251
  }
1135
1252
  };
1136
- document.documentElement.parent = null;
1137
- host_hooks.outside = document.documentElement;
1138
- // <html> is not in the tree (the host's root is <body>), but it is asked
1139
- // whether a node is in the page.
1140
- /** @type {any} */ (document.documentElement).contains = (/** @type {any} */ node) => node === document.documentElement || /** @type {any} */ (root).contains(node);
1253
+ // An event that hit nothing the app made (the area around <body>) is <html>'s.
1254
+ host_hooks.outside = html;
1141
1255
  // <html> is the viewport: the same numbers as `innerWidth` and `innerHeight`,
1142
1256
  // so that their difference, the width of a scrollbar, comes out as zero.
1143
1257
  define(document.documentElement, { clientWidth: { get: () => viewport.width }, clientHeight: { get: () => viewport.height } });
@@ -1146,9 +1260,9 @@ reported_box(root);
1146
1260
 
1147
1261
  /** `window` is the global object; these are its listeners. */
1148
1262
  const window_target = { listeners: /** @type {Map<string, Function[]> | null} */ (null) };
1149
- // Above <body>: <html>, where routers listen for clicks on links, then the
1150
- // document, then the window.
1151
- outer_targets.push(document.documentElement, document, window_target);
1263
+ // Above <html>, where routers listen for clicks on links: the document,
1264
+ // then the window.
1265
+ outer_targets.push(document, window_target);
1152
1266
 
1153
1267
  // The host moves focus on its own when the user clicks or tabs. Follow it.
1154
1268
  add_listener(document, 'focusin', (/** @type {any} */ event) => {
@@ -1185,7 +1299,6 @@ provide({
1185
1299
  window: g,
1186
1300
  self: g,
1187
1301
  document,
1188
- navigator: { userAgent: 'sepal', platform: '', language: 'en-US', languages: ['en-US'], maxTouchPoints: 0, onLine: true },
1189
1302
  location,
1190
1303
  history,
1191
1304
  sessionStorage: memory_storage(),