@c9up/orbit 0.1.0 → 0.1.2

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/Cargo.lock +9 -16
  2. package/Cargo.toml +4 -0
  3. package/README.md +7 -1
  4. package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +74 -1
  5. package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +67 -4
  6. package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +90 -8
  7. package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +23 -4
  8. package/crates/orbit-host/Cargo.toml +1 -1
  9. package/crates/orbit-host/src/lib.rs +580 -115
  10. package/crates/orbit-host/src/motion.rs +6 -0
  11. package/crates/orbit-host/src/vector.rs +312 -56
  12. package/dist/color.d.ts.map +1 -1
  13. package/dist/color.js +3 -3
  14. package/dist/color.js.map +1 -1
  15. package/dist/commit.d.ts +38 -0
  16. package/dist/commit.d.ts.map +1 -1
  17. package/dist/commit.js +154 -13
  18. package/dist/commit.js.map +1 -1
  19. package/dist/errors.d.ts +60 -26
  20. package/dist/errors.d.ts.map +1 -1
  21. package/dist/errors.js +85 -11
  22. package/dist/errors.js.map +1 -1
  23. package/dist/host.d.ts.map +1 -1
  24. package/dist/host.js +4 -2
  25. package/dist/host.js.map +1 -1
  26. package/dist/index.d.ts +5 -2
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +6 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/motion.d.ts.map +1 -1
  31. package/dist/motion.js +7 -6
  32. package/dist/motion.js.map +1 -1
  33. package/dist/pages.d.ts.map +1 -1
  34. package/dist/pages.js +3 -3
  35. package/dist/pages.js.map +1 -1
  36. package/dist/react-native/index.d.ts +9 -1
  37. package/dist/react-native/index.d.ts.map +1 -1
  38. package/dist/react-native/index.js +55 -6
  39. package/dist/react-native/index.js.map +1 -1
  40. package/dist/report.d.ts +13 -0
  41. package/dist/report.d.ts.map +1 -0
  42. package/dist/report.js +22 -0
  43. package/dist/report.js.map +1 -0
  44. package/dist/server.d.ts +27 -3
  45. package/dist/server.d.ts.map +1 -1
  46. package/dist/server.js +15 -7
  47. package/dist/server.js.map +1 -1
  48. package/dist/stack.d.ts.map +1 -1
  49. package/dist/stack.js +3 -3
  50. package/dist/stack.js.map +1 -1
  51. package/dist/styles.d.ts.map +1 -1
  52. package/dist/styles.js +2 -2
  53. package/dist/styles.js.map +1 -1
  54. package/dist/template.d.ts.map +1 -1
  55. package/dist/template.js +56 -12
  56. package/dist/template.js.map +1 -1
  57. package/dist/textDecoder.d.ts +29 -0
  58. package/dist/textDecoder.d.ts.map +1 -0
  59. package/dist/textDecoder.js +149 -0
  60. package/dist/textDecoder.js.map +1 -0
  61. package/dist/url.d.ts.map +1 -1
  62. package/dist/url.js +4 -3
  63. package/dist/url.js.map +1 -1
  64. package/package.json +13 -7
  65. package/scripts/build-android.sh +0 -0
  66. package/src/color.ts +5 -10
  67. package/src/commit.ts +197 -18
  68. package/src/errors.ts +174 -44
  69. package/src/host.ts +4 -5
  70. package/src/index.ts +10 -1
  71. package/src/motion.ts +8 -6
  72. package/src/pages.ts +3 -6
  73. package/src/react-native/index.ts +70 -8
  74. package/src/react-native/react-native.d.ts +8 -0
  75. package/src/report.ts +25 -0
  76. package/src/server.ts +50 -14
  77. package/src/stack.ts +3 -9
  78. package/src/styles.ts +2 -5
  79. package/src/template.ts +65 -41
  80. package/src/textDecoder.ts +162 -0
  81. package/src/url.ts +10 -3
package/src/template.ts CHANGED
@@ -43,7 +43,14 @@ import {
43
43
  type Root,
44
44
  treeHost,
45
45
  } from "./commit.js";
46
- import { OrbitError } from "./errors.js";
46
+ import {
47
+ E_ORBIT_BLOCK_IN_TEXT,
48
+ E_ORBIT_DUPLICATE_KEY,
49
+ E_ORBIT_IMAGE_SOURCE,
50
+ E_ORBIT_INSECURE_IMAGE_URL,
51
+ E_ORBIT_INVALID_SCROLL_BINDING,
52
+ E_ORBIT_UNSUPPORTED_ELEMENT,
53
+ } from "./errors.js";
47
54
  import {
48
55
  bindMotion,
49
56
  bindScrollY,
@@ -60,6 +67,7 @@ import {
60
67
  rawText,
61
68
  type Style,
62
69
  } from "./node.js";
70
+ import { report, setErrorListener } from "./report.js";
63
71
  import { Stack } from "./stack.js";
64
72
  import {
65
73
  type Computed,
@@ -410,10 +418,7 @@ function keyedRegion(
410
418
  items.forEach((element, position) => {
411
419
  const key = list.key(element, position);
412
420
  if (next.has(key)) {
413
- throw new OrbitError(
414
- "E_ORBIT_DUPLICATE_KEY",
415
- `[orbit] each(): two elements share the key ${String(key)} — a key names one row`,
416
- );
421
+ throw new E_ORBIT_DUPLICATE_KEY([String(key)]);
417
422
  }
418
423
  const kept = rows.get(key);
419
424
  if (kept) {
@@ -514,10 +519,7 @@ function lazyRegion(
514
519
  );
515
520
  if (new Set(all).size !== all.length) {
516
521
  const duplicate = all.find((key, i) => all.indexOf(key) !== i);
517
- throw new OrbitError(
518
- "E_ORBIT_DUPLICATE_KEY",
519
- `[orbit] each(): two elements share the key ${String(duplicate)} — a key names one row`,
520
- );
522
+ throw new E_ORBIT_DUPLICATE_KEY([String(duplicate)]);
521
523
  }
522
524
  return all;
523
525
  });
@@ -755,11 +757,7 @@ function readAttrs(el: TElement, values: readonly unknown[]): Attrs {
755
757
  const value = typeof slot === "number" ? values[slot] : undefined;
756
758
  if (attr.name === "motion") attrs.motion = readMotion(value);
757
759
  else if (value instanceof MotionValue) attrs.scrollY = value;
758
- else
759
- throw new OrbitError(
760
- "E_ORBIT_INVALID_SCROLL_BINDING",
761
- "[orbit] scroll-y expects a motion value",
762
- );
760
+ else throw new E_ORBIT_INVALID_SCROLL_BINDING();
763
761
  } else if (attr.name.startsWith("?")) {
764
762
  // aurora's boolean attribute: present while its value is truthy.
765
763
  attrs.attributes.set(attr.name.slice(1), attrValue(attr, values));
@@ -1145,6 +1143,11 @@ let bytesVersion = 0;
1145
1143
 
1146
1144
  const SCHEME = /^([a-z][a-z0-9+.-]*):/i;
1147
1145
 
1146
+ /** An address as an error quotes it: a `data:` URL can be megabytes long. */
1147
+ function quoted(src: string): string {
1148
+ return src.length > 80 ? `${src.slice(0, 80)}…` : src;
1149
+ }
1150
+
1148
1151
  /**
1149
1152
  * Where `src` points, in the form the host loads: `https://…`, `file:<path>`,
1150
1153
  * `content://…` (what Android's pickers return), `asset:<name>` for a file
@@ -1163,6 +1166,13 @@ function imageSource(node: NativeNode, value: unknown): string {
1163
1166
  if (src === "") return "";
1164
1167
  const scheme = SCHEME.exec(src)?.[1]?.toLowerCase();
1165
1168
  if (scheme === undefined) {
1169
+ // `//host/a.png` borrows a page's scheme in a browser; an app has no page.
1170
+ if (src.startsWith("//")) {
1171
+ throw new E_ORBIT_IMAGE_SOURCE([
1172
+ quoted(src),
1173
+ "a scheme-relative address names no scheme: write https://",
1174
+ ]);
1175
+ }
1166
1176
  return src.startsWith("/")
1167
1177
  ? `file:${src}`
1168
1178
  : `asset:${src.replace(/^\.\//, "")}`;
@@ -1174,21 +1184,14 @@ function imageSource(node: NativeNode, value: unknown): string {
1174
1184
  try {
1175
1185
  return `file:${decodeURIComponent(path)}`;
1176
1186
  } catch {
1177
- throw new OrbitError(
1178
- "E_ORBIT_IMAGE_SOURCE",
1179
- `[orbit] <img src="${src}">: not a valid file URL`,
1180
- );
1187
+ throw new E_ORBIT_IMAGE_SOURCE([quoted(src), "not a valid file URL"]);
1181
1188
  }
1182
1189
  }
1183
- if (scheme === "http")
1184
- throw new OrbitError(
1185
- "E_ORBIT_INSECURE_IMAGE_URL",
1186
- `[orbit] <img src="${src}">: an image is fetched over https only`,
1187
- );
1188
- throw new OrbitError(
1189
- "E_ORBIT_IMAGE_SOURCE",
1190
- `[orbit] <img src="${src}">: ${scheme}: is not an image source (https:, file:, content:, a bundled asset or bytes)`,
1191
- );
1190
+ if (scheme === "http") throw new E_ORBIT_INSECURE_IMAGE_URL([quoted(src)]);
1191
+ throw new E_ORBIT_IMAGE_SOURCE([
1192
+ quoted(src),
1193
+ `${scheme}: is not an image source (https:, file:, content:, a bundled asset or bytes)`,
1194
+ ]);
1192
1195
  }
1193
1196
 
1194
1197
  /**
@@ -1210,10 +1213,33 @@ function imgElement(
1210
1213
  const node = new NativeNode("OrbitImage", {}, []);
1211
1214
  const style = bindStyle(node, () => boxOf(s.computed(), ctx));
1212
1215
  const src = raw.get("src");
1216
+ // An address written in the template is the developer's: a refusal throws.
1217
+ // One that comes from data (a slot) is the content's: that image shows
1218
+ // nothing and the refusal goes to `mount`'s `onError`, as a browser shows a
1219
+ // broken image and draws the rest of the page.
1220
+ const fromData =
1221
+ el.attrs
1222
+ .find((attr) => attr.name === "src")
1223
+ ?.parts.some((part) => typeof part === "number") ?? false;
1224
+ const source = (value: unknown): string => {
1225
+ if (!fromData) return imageSource(node, value);
1226
+ try {
1227
+ return imageSource(node, value);
1228
+ } catch (error) {
1229
+ if (
1230
+ error instanceof E_ORBIT_INSECURE_IMAGE_URL ||
1231
+ error instanceof E_ORBIT_IMAGE_SOURCE
1232
+ ) {
1233
+ report(error);
1234
+ return "";
1235
+ }
1236
+ throw error;
1237
+ }
1238
+ };
1213
1239
  const initial = isGetter(src) ? untrack(src) : src;
1214
1240
  node.props = {
1215
1241
  ...flatten(style),
1216
- src: imageSource(node, initial),
1242
+ src: source(initial),
1217
1243
  alt: str(raw.get("alt")),
1218
1244
  };
1219
1245
  if (isGetter(src)) {
@@ -1224,7 +1250,7 @@ function imgElement(
1224
1250
  first = false;
1225
1251
  return;
1226
1252
  }
1227
- node.setProps({ src: imageSource(node, next) });
1253
+ node.setProps({ src: source(next) });
1228
1254
  });
1229
1255
  }
1230
1256
  return withMotion(withEvents(node, attrs), attrs);
@@ -1297,10 +1323,10 @@ function svgPaint(value: string): string | number {
1297
1323
  /** An svg element and what it holds, read now (inside the drawing's effect). */
1298
1324
  function svgTree(el: TElement, values: readonly unknown[]): SvgNode {
1299
1325
  if (!SVG_TAGS.has(el.tag)) {
1300
- throw new OrbitError(
1301
- "E_ORBIT_UNSUPPORTED_ELEMENT",
1302
- `[orbit] <${el.tag}> inside <svg> is not drawn: paths, basic shapes and g are`,
1303
- );
1326
+ throw new E_ORBIT_UNSUPPORTED_ELEMENT([
1327
+ el.tag,
1328
+ "inside <svg> is not drawn: paths, basic shapes and g are",
1329
+ ]);
1304
1330
  }
1305
1331
  const a: SvgNode["a"] = {};
1306
1332
  for (const attr of el.attrs) {
@@ -1409,10 +1435,10 @@ function element(
1409
1435
  ctx: Ctx,
1410
1436
  ): NativeNode {
1411
1437
  if (UNSUPPORTED.has(el.tag)) {
1412
- throw new OrbitError(
1413
- "E_ORBIT_UNSUPPORTED_ELEMENT",
1414
- `[orbit] <${el.tag}> has no native mapping yet`,
1415
- );
1438
+ throw new E_ORBIT_UNSUPPORTED_ELEMENT([
1439
+ el.tag,
1440
+ "has no native mapping yet",
1441
+ ]);
1416
1442
  }
1417
1443
  const attrs = readAttrs(el, values);
1418
1444
  const s = styled(el.tag, attrs, ctx);
@@ -1459,10 +1485,7 @@ function virtualText(
1459
1485
  ctx: Ctx,
1460
1486
  ): NativeNode {
1461
1487
  if (!isInline(el) && !TEXT_BLOCK.has(el.tag)) {
1462
- throw new OrbitError(
1463
- "E_ORBIT_BLOCK_IN_TEXT",
1464
- `[orbit] <${el.tag}> cannot sit inside text on native`,
1465
- );
1488
+ throw new E_ORBIT_BLOCK_IN_TEXT([el.tag]);
1466
1489
  }
1467
1490
  const attrs = readAttrs(el, values);
1468
1491
  const s = styled(el.tag, attrs, ctx);
@@ -1624,6 +1647,7 @@ const ROOT_TEXT: Style = { fontSize: 16, color: "#000000", lineHeight: "1.5x" };
1624
1647
  * `flush()` commits synchronously.
1625
1648
  */
1626
1649
  export function mount(result: TemplateResult, options: MountOptions): Root {
1650
+ setErrorListener(options.onError ?? null);
1627
1651
  viewportSize(options.viewport);
1628
1652
  const top: Ctx = {
1629
1653
  text: () => ROOT_TEXT,
@@ -0,0 +1,162 @@
1
+ /**
2
+ * A standard UTF-8 `TextDecoder` for the phone, installed when the engine has
3
+ * none: Hermes has `TextEncoder` but not `TextDecoder`, so a library shared
4
+ * with the web (msgpack, protobuf, any binary protocol) dies at load with
5
+ * "Property 'TextDecoder' doesn't exist".
6
+ *
7
+ * The WHATWG Encoding standard's UTF-8 decoder: invalid sequences become
8
+ * U+FFFD by its maximal-subpart rule (or throw a TypeError with `fatal`), a
9
+ * leading BOM is dropped unless `ignoreBOM`, and `{ stream: true }` keeps an
10
+ * incomplete sequence for the next call. UTF-8 only: another label throws a
11
+ * RangeError, as the standard does for a label it does not know.
12
+ */
13
+
14
+ import {
15
+ E_ORBIT_INVALID_DECODER_INPUT,
16
+ E_ORBIT_INVALID_ENCODED_DATA,
17
+ E_ORBIT_UNSUPPORTED_ENCODING,
18
+ } from "./errors.js";
19
+
20
+ const LABELS = new Set([
21
+ "utf-8",
22
+ "utf8",
23
+ "unicode-1-1-utf-8",
24
+ "unicode11utf8",
25
+ "unicode20utf8",
26
+ "x-unicode20utf8",
27
+ ]);
28
+
29
+ /** How many code points become one string at a time (fromCodePoint's argument list). */
30
+ const CHUNK = 8192;
31
+
32
+ export interface StandardTextDecoderOptions {
33
+ fatal?: boolean;
34
+ ignoreBOM?: boolean;
35
+ }
36
+
37
+ function bytesOf(input: ArrayBuffer | ArrayBufferView | undefined): Uint8Array {
38
+ if (input === undefined) return new Uint8Array(0);
39
+ if (input instanceof ArrayBuffer) return new Uint8Array(input);
40
+ if (ArrayBuffer.isView(input)) {
41
+ return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
42
+ }
43
+ throw new E_ORBIT_INVALID_DECODER_INPUT(
44
+ "The provided value is not of type '(ArrayBuffer or ArrayBufferView)'",
45
+ );
46
+ }
47
+
48
+ export class StandardTextDecoder {
49
+ readonly encoding = "utf-8";
50
+ readonly fatal: boolean;
51
+ readonly ignoreBOM: boolean;
52
+ #needed = 0;
53
+ #seen = 0;
54
+ #codePoint = 0;
55
+ #lower = 0x80;
56
+ #upper = 0xbf;
57
+ /** Whether the next code point is the first of the stream (a BOM there is dropped). */
58
+ #start = true;
59
+
60
+ constructor(label = "utf-8", options: StandardTextDecoderOptions = {}) {
61
+ if (!LABELS.has(String(label).trim().toLowerCase())) {
62
+ throw new E_ORBIT_UNSUPPORTED_ENCODING(
63
+ `The encoding label provided ('${label}') is invalid.`,
64
+ );
65
+ }
66
+ this.fatal = options.fatal === true;
67
+ this.ignoreBOM = options.ignoreBOM === true;
68
+ }
69
+
70
+ decode(
71
+ input?: ArrayBuffer | ArrayBufferView,
72
+ options: { stream?: boolean } = {},
73
+ ): string {
74
+ const bytes = bytesOf(input);
75
+ const stream = options.stream === true;
76
+ const out: string[] = [];
77
+ const points: number[] = [];
78
+ const emit = (point: number): void => {
79
+ if (this.#start) {
80
+ this.#start = false;
81
+ if (point === 0xfeff && !this.ignoreBOM) return;
82
+ }
83
+ points.push(point);
84
+ if (points.length === CHUNK) {
85
+ out.push(String.fromCodePoint(...points));
86
+ points.length = 0;
87
+ }
88
+ };
89
+ const error = (): void => {
90
+ if (this.fatal) {
91
+ this.#reset();
92
+ this.#start = true;
93
+ throw new E_ORBIT_INVALID_ENCODED_DATA(
94
+ "The encoded data was not valid for encoding utf-8",
95
+ );
96
+ }
97
+ emit(0xfffd);
98
+ };
99
+ for (let i = 0; i < bytes.length; i++) {
100
+ const byte = bytes[i] ?? 0;
101
+ if (this.#needed === 0) {
102
+ if (byte <= 0x7f) emit(byte);
103
+ else if (byte >= 0xc2 && byte <= 0xdf) {
104
+ this.#needed = 1;
105
+ this.#codePoint = byte & 0x1f;
106
+ } else if (byte >= 0xe0 && byte <= 0xef) {
107
+ if (byte === 0xe0) this.#lower = 0xa0;
108
+ if (byte === 0xed) this.#upper = 0x9f;
109
+ this.#needed = 2;
110
+ this.#codePoint = byte & 0x0f;
111
+ } else if (byte >= 0xf0 && byte <= 0xf4) {
112
+ if (byte === 0xf0) this.#lower = 0x90;
113
+ if (byte === 0xf4) this.#upper = 0x8f;
114
+ this.#needed = 3;
115
+ this.#codePoint = byte & 0x07;
116
+ } else error();
117
+ continue;
118
+ }
119
+ if (byte < this.#lower || byte > this.#upper) {
120
+ // The sequence so far is one error; this byte starts afresh.
121
+ this.#reset();
122
+ error();
123
+ i--;
124
+ continue;
125
+ }
126
+ this.#lower = 0x80;
127
+ this.#upper = 0xbf;
128
+ this.#codePoint = (this.#codePoint << 6) | (byte & 0x3f);
129
+ this.#seen++;
130
+ if (this.#seen === this.#needed) {
131
+ const point = this.#codePoint;
132
+ this.#reset();
133
+ emit(point);
134
+ }
135
+ }
136
+ if (!stream) {
137
+ if (this.#needed !== 0) {
138
+ this.#reset();
139
+ error();
140
+ }
141
+ this.#start = true;
142
+ }
143
+ if (points.length > 0) out.push(String.fromCodePoint(...points));
144
+ return out.join("");
145
+ }
146
+
147
+ #reset(): void {
148
+ this.#needed = 0;
149
+ this.#seen = 0;
150
+ this.#codePoint = 0;
151
+ this.#lower = 0x80;
152
+ this.#upper = 0xbf;
153
+ }
154
+ }
155
+
156
+ /** Put a standard UTF-8 `TextDecoder` in place when the engine has none; true when it did. */
157
+ export function installTextDecoder(): boolean {
158
+ if (typeof Reflect.get(globalThis, "TextDecoder") === "function")
159
+ return false;
160
+ Reflect.set(globalThis, "TextDecoder", StandardTextDecoder);
161
+ return true;
162
+ }
package/src/url.ts CHANGED
@@ -13,6 +13,11 @@
13
13
  * an opaque one (`mailto:`, `tel:`, `blob:`) is kept as written.
14
14
  */
15
15
 
16
+ import {
17
+ E_ORBIT_INVALID_URL,
18
+ E_ORBIT_OBJECT_URL_UNAVAILABLE,
19
+ } from "./errors.js";
20
+
16
21
  const SCHEME = /^([a-zA-Z][a-zA-Z\d+.-]*):/;
17
22
  const DEFAULT_PORTS: { [scheme: string]: string } = {
18
23
  "http:": "80",
@@ -179,7 +184,9 @@ export class StandardURL {
179
184
 
180
185
  static createObjectURL(blob: Blob): string {
181
186
  if (platformUrl === undefined)
182
- throw new TypeError("URL.createObjectURL is not available");
187
+ throw new E_ORBIT_OBJECT_URL_UNAVAILABLE(
188
+ "URL.createObjectURL is not available",
189
+ );
183
190
  return platformUrl.createObjectURL(blob);
184
191
  }
185
192
 
@@ -207,10 +214,10 @@ export class StandardURL {
207
214
  ? base.#parts
208
215
  : parseAbsolute(String(base).trim());
209
216
  if (from === null)
210
- throw new TypeError(`Invalid base URL: ${String(base)}`);
217
+ throw new E_ORBIT_INVALID_URL(`Invalid base URL: ${String(base)}`);
211
218
  parts = resolve(input, from);
212
219
  }
213
- if (parts === null) throw new TypeError(`Invalid URL: ${input}`);
220
+ if (parts === null) throw new E_ORBIT_INVALID_URL(`Invalid URL: ${input}`);
214
221
  this.#parts = parts;
215
222
  }
216
223