@c9up/orbit 0.0.0-stage → 0.1.1
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/Cargo.lock +139 -0
- package/Cargo.toml +20 -0
- package/LICENSE +21 -0
- package/README.md +320 -2
- package/android/orbit.cmake +31 -0
- package/android/src/main/cpp/NativeOrbit.cpp +309 -0
- package/android/src/main/cpp/NativeOrbit.h +35 -0
- package/android/src/main/cpp/orbit_host.h +34 -0
- package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +286 -0
- package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +258 -0
- package/android/src/main/java/com/c9up/orbit/OrbitImage.kt +346 -0
- package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +161 -0
- package/android/src/main/java/com/c9up/orbit/OrbitPress.kt +110 -0
- package/android/src/main/java/com/c9up/orbit/OrbitVector.kt +115 -0
- package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +362 -0
- package/crates/orbit-host/Cargo.toml +14 -0
- package/crates/orbit-host/src/gesture.rs +561 -0
- package/crates/orbit-host/src/lib.rs +2042 -0
- package/crates/orbit-host/src/motion.rs +328 -0
- package/crates/orbit-host/src/vector.rs +1173 -0
- package/dist/color.d.ts +10 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +62 -0
- package/dist/color.js.map +1 -0
- package/dist/commit.d.ts +77 -0
- package/dist/commit.d.ts.map +1 -0
- package/dist/commit.js +302 -0
- package/dist/commit.js.map +1 -0
- package/dist/errors.d.ts +76 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +89 -0
- package/dist/errors.js.map +1 -0
- package/dist/host.d.ts +49 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +20 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/motion.d.ts +18 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +67 -0
- package/dist/motion.js.map +1 -0
- package/dist/node.d.ts +46 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +110 -0
- package/dist/node.js.map +1 -0
- package/dist/pages.d.ts +56 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +157 -0
- package/dist/pages.js.map +1 -0
- package/dist/react-native/index.d.ts +33 -0
- package/dist/react-native/index.d.ts.map +1 -0
- package/dist/react-native/index.js +71 -0
- package/dist/react-native/index.js.map +1 -0
- package/dist/report.d.ts +13 -0
- package/dist/report.d.ts.map +1 -0
- package/dist/report.js +22 -0
- package/dist/report.js.map +1 -0
- package/dist/server.d.ts +109 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +290 -0
- package/dist/server.js.map +1 -0
- package/dist/stack.d.ts +117 -0
- package/dist/stack.d.ts.map +1 -0
- package/dist/stack.js +299 -0
- package/dist/stack.js.map +1 -0
- package/dist/styles.d.ts +138 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +920 -0
- package/dist/styles.js.map +1 -0
- package/dist/template.d.ts +27 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +1436 -0
- package/dist/template.js.map +1 -0
- package/dist/textDecoder.d.ts +29 -0
- package/dist/textDecoder.d.ts.map +1 -0
- package/dist/textDecoder.js +149 -0
- package/dist/textDecoder.js.map +1 -0
- package/dist/url.d.ts +45 -0
- package/dist/url.d.ts.map +1 -0
- package/dist/url.js +303 -0
- package/dist/url.js.map +1 -0
- package/package.json +96 -3
- package/scripts/build-android.sh +27 -0
- package/scripts/test-styles.mjs +31 -0
- package/specs/NativeOrbit.ts +34 -0
- package/src/color.ts +68 -0
- package/src/commit.ts +373 -0
- package/src/errors.ts +178 -0
- package/src/host.ts +91 -0
- package/src/index.ts +64 -0
- package/src/motion.ts +88 -0
- package/src/node.ts +133 -0
- package/src/pages.ts +207 -0
- package/src/react-native/index.ts +115 -0
- package/src/react-native/react-native.d.ts +39 -0
- package/src/report.ts +25 -0
- package/src/server.ts +395 -0
- package/src/stack.ts +428 -0
- package/src/styles.ts +1021 -0
- package/src/template.ts +1681 -0
- package/src/textDecoder.ts +162 -0
- package/src/url.ts +333 -0
|
@@ -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
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A standard `URL` for the phone, installed when the platform's is not one.
|
|
3
|
+
*
|
|
4
|
+
* React Native's `URL` (0.87) parses with regular expressions: a relative
|
|
5
|
+
* reference is appended to the base (`new URL("/b", "https://h/a")` gives
|
|
6
|
+
* `https://h/a/b`), and `searchParams` is a copy that `search` never sees.
|
|
7
|
+
* photon's router — and any code an app shares with the browser — resolves
|
|
8
|
+
* links and builds queries through it. `installStandardUrl()` probes the
|
|
9
|
+
* global `URL` and, when it gets either wrong, puts this one in its place.
|
|
10
|
+
*
|
|
11
|
+
* Scope: hierarchical URLs (`http:`, `https:`, `ws:`, `wss:`, `file:`…)
|
|
12
|
+
* resolved as RFC 3986 says, parts read and written as WHATWG's `URL` does;
|
|
13
|
+
* an opaque one (`mailto:`, `tel:`, `blob:`) is kept as written.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
E_ORBIT_INVALID_URL,
|
|
18
|
+
E_ORBIT_OBJECT_URL_UNAVAILABLE,
|
|
19
|
+
} from "./errors.js";
|
|
20
|
+
|
|
21
|
+
const SCHEME = /^([a-zA-Z][a-zA-Z\d+.-]*):/;
|
|
22
|
+
const DEFAULT_PORTS: { [scheme: string]: string } = {
|
|
23
|
+
"http:": "80",
|
|
24
|
+
"https:": "443",
|
|
25
|
+
"ws:": "80",
|
|
26
|
+
"wss:": "443",
|
|
27
|
+
"ftp:": "21",
|
|
28
|
+
};
|
|
29
|
+
const SPECIAL = new Set(["http:", "https:", "ws:", "wss:", "ftp:", "file:"]);
|
|
30
|
+
|
|
31
|
+
interface Parts {
|
|
32
|
+
protocol: string;
|
|
33
|
+
username: string;
|
|
34
|
+
password: string;
|
|
35
|
+
hostname: string;
|
|
36
|
+
port: string;
|
|
37
|
+
pathname: string;
|
|
38
|
+
search: string;
|
|
39
|
+
hash: string;
|
|
40
|
+
/** An opaque URL (`mailto:x`): what follows the scheme, as written. */
|
|
41
|
+
opaque: string | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** RFC 3986 §5.2.4. */
|
|
45
|
+
function removeDotSegments(path: string): string {
|
|
46
|
+
const out: string[] = [];
|
|
47
|
+
const segments = path.split("/");
|
|
48
|
+
for (let i = 0; i < segments.length; i++) {
|
|
49
|
+
const segment = segments[i] ?? "";
|
|
50
|
+
const last = i === segments.length - 1;
|
|
51
|
+
if (segment === ".") {
|
|
52
|
+
if (last) out.push("");
|
|
53
|
+
} else if (segment === "..") {
|
|
54
|
+
if (out.length > 1) out.pop();
|
|
55
|
+
if (last) out.push("");
|
|
56
|
+
} else {
|
|
57
|
+
out.push(segment);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const joined = out.join("/");
|
|
61
|
+
return joined.startsWith("/") ? joined : `/${joined}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function splitTail(rest: string): {
|
|
65
|
+
path: string;
|
|
66
|
+
search: string;
|
|
67
|
+
hash: string;
|
|
68
|
+
} {
|
|
69
|
+
const hashAt = rest.indexOf("#");
|
|
70
|
+
const hash = hashAt >= 0 ? rest.slice(hashAt) : "";
|
|
71
|
+
const beforeHash = hashAt >= 0 ? rest.slice(0, hashAt) : rest;
|
|
72
|
+
const queryAt = beforeHash.indexOf("?");
|
|
73
|
+
const search = queryAt >= 0 ? beforeHash.slice(queryAt) : "";
|
|
74
|
+
const path = queryAt >= 0 ? beforeHash.slice(0, queryAt) : beforeHash;
|
|
75
|
+
return {
|
|
76
|
+
path,
|
|
77
|
+
search: search === "?" ? "" : search,
|
|
78
|
+
hash: hash === "#" ? "" : hash,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parseAbsolute(input: string): Parts | null {
|
|
83
|
+
const scheme = SCHEME.exec(input);
|
|
84
|
+
if (scheme === null) return null;
|
|
85
|
+
const protocol = `${(scheme[1] ?? "").toLowerCase()}:`;
|
|
86
|
+
let rest = input.slice(scheme[0].length).replace(/\\/g, "/");
|
|
87
|
+
if (!SPECIAL.has(protocol) && !rest.startsWith("//")) {
|
|
88
|
+
return {
|
|
89
|
+
protocol,
|
|
90
|
+
username: "",
|
|
91
|
+
password: "",
|
|
92
|
+
hostname: "",
|
|
93
|
+
port: "",
|
|
94
|
+
pathname: rest,
|
|
95
|
+
search: "",
|
|
96
|
+
hash: "",
|
|
97
|
+
opaque: rest,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
rest = rest.replace(/^\/*/, "");
|
|
101
|
+
const end = rest.search(/[/?#]/);
|
|
102
|
+
const authority = end >= 0 ? rest.slice(0, end) : rest;
|
|
103
|
+
const tail = splitTail(end >= 0 ? rest.slice(end) : "");
|
|
104
|
+
const at = authority.lastIndexOf("@");
|
|
105
|
+
const userInfo = at >= 0 ? authority.slice(0, at) : "";
|
|
106
|
+
const hostPort = at >= 0 ? authority.slice(at + 1) : authority;
|
|
107
|
+
const colon = userInfo.indexOf(":");
|
|
108
|
+
const portMatch = /:(\d*)$/.exec(hostPort);
|
|
109
|
+
const hostname = (
|
|
110
|
+
portMatch ? hostPort.slice(0, portMatch.index) : hostPort
|
|
111
|
+
).toLowerCase();
|
|
112
|
+
if (hostname === "" && protocol !== "file:") return null;
|
|
113
|
+
const port = portMatch?.[1] ?? "";
|
|
114
|
+
return {
|
|
115
|
+
protocol,
|
|
116
|
+
username: colon >= 0 ? userInfo.slice(0, colon) : userInfo,
|
|
117
|
+
password: colon >= 0 ? userInfo.slice(colon + 1) : "",
|
|
118
|
+
hostname,
|
|
119
|
+
port: port === DEFAULT_PORTS[protocol] ? "" : port.replace(/^0+(?=\d)/, ""),
|
|
120
|
+
pathname: removeDotSegments(tail.path === "" ? "/" : tail.path),
|
|
121
|
+
search: tail.search,
|
|
122
|
+
hash: tail.hash,
|
|
123
|
+
opaque: null,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** RFC 3986 §5.2.2: `reference` against an absolute `base`. */
|
|
128
|
+
function resolve(reference: string, base: Parts): Parts | null {
|
|
129
|
+
const ref = reference.trim();
|
|
130
|
+
if (SCHEME.test(ref)) return parseAbsolute(ref);
|
|
131
|
+
if (base.opaque !== null) return null;
|
|
132
|
+
if (ref.startsWith("//") || ref.startsWith("\\\\")) {
|
|
133
|
+
return parseAbsolute(`${base.protocol}${ref}`);
|
|
134
|
+
}
|
|
135
|
+
const tail = splitTail(ref.replace(/\\/g, "/"));
|
|
136
|
+
const next: Parts = { ...base, hash: tail.hash };
|
|
137
|
+
if (tail.path === "") {
|
|
138
|
+
if (tail.search !== "" || ref.includes("?")) next.search = tail.search;
|
|
139
|
+
return next;
|
|
140
|
+
}
|
|
141
|
+
next.search = tail.search;
|
|
142
|
+
if (tail.path.startsWith("/")) {
|
|
143
|
+
next.pathname = removeDotSegments(tail.path);
|
|
144
|
+
} else {
|
|
145
|
+
const dir = base.pathname.slice(0, base.pathname.lastIndexOf("/") + 1);
|
|
146
|
+
next.pathname = removeDotSegments(`${dir}${tail.path}`);
|
|
147
|
+
}
|
|
148
|
+
return next;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A query whose changes reach the URL it belongs to, as WHATWG's does. */
|
|
152
|
+
class LinkedSearchParams extends URLSearchParams {
|
|
153
|
+
readonly #changed: (query: string) => void;
|
|
154
|
+
constructor(query: string, changed: (query: string) => void) {
|
|
155
|
+
super(query);
|
|
156
|
+
this.#changed = changed;
|
|
157
|
+
}
|
|
158
|
+
override append(name: string, value: string): void {
|
|
159
|
+
super.append(name, value);
|
|
160
|
+
this.#changed(this.toString());
|
|
161
|
+
}
|
|
162
|
+
override delete(name: string): void {
|
|
163
|
+
super.delete(name);
|
|
164
|
+
this.#changed(this.toString());
|
|
165
|
+
}
|
|
166
|
+
override set(name: string, value: string): void {
|
|
167
|
+
super.set(name, value);
|
|
168
|
+
this.#changed(this.toString());
|
|
169
|
+
}
|
|
170
|
+
override sort(): void {
|
|
171
|
+
super.sort();
|
|
172
|
+
this.#changed(this.toString());
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The platform's own `URL`, kept for its `createObjectURL` (blobs). */
|
|
177
|
+
type PlatformUrl = typeof URL;
|
|
178
|
+
|
|
179
|
+
let platformUrl: PlatformUrl | undefined;
|
|
180
|
+
|
|
181
|
+
export class StandardURL {
|
|
182
|
+
#parts: Parts;
|
|
183
|
+
#query: LinkedSearchParams | null = null;
|
|
184
|
+
|
|
185
|
+
static createObjectURL(blob: Blob): string {
|
|
186
|
+
if (platformUrl === undefined)
|
|
187
|
+
throw new E_ORBIT_OBJECT_URL_UNAVAILABLE(
|
|
188
|
+
"URL.createObjectURL is not available",
|
|
189
|
+
);
|
|
190
|
+
return platformUrl.createObjectURL(blob);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
static revokeObjectURL(url: string): void {
|
|
194
|
+
platformUrl?.revokeObjectURL(url);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static canParse(url: string, base?: string | StandardURL): boolean {
|
|
198
|
+
try {
|
|
199
|
+
new StandardURL(url, base);
|
|
200
|
+
return true;
|
|
201
|
+
} catch {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
constructor(url: string | StandardURL, base?: string | StandardURL) {
|
|
207
|
+
const input = String(url);
|
|
208
|
+
let parts: Parts | null;
|
|
209
|
+
if (base === undefined) {
|
|
210
|
+
parts = parseAbsolute(input.trim());
|
|
211
|
+
} else {
|
|
212
|
+
const from =
|
|
213
|
+
base instanceof StandardURL
|
|
214
|
+
? base.#parts
|
|
215
|
+
: parseAbsolute(String(base).trim());
|
|
216
|
+
if (from === null)
|
|
217
|
+
throw new E_ORBIT_INVALID_URL(`Invalid base URL: ${String(base)}`);
|
|
218
|
+
parts = resolve(input, from);
|
|
219
|
+
}
|
|
220
|
+
if (parts === null) throw new E_ORBIT_INVALID_URL(`Invalid URL: ${input}`);
|
|
221
|
+
this.#parts = parts;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
get protocol(): string {
|
|
225
|
+
return this.#parts.protocol;
|
|
226
|
+
}
|
|
227
|
+
get username(): string {
|
|
228
|
+
return this.#parts.username;
|
|
229
|
+
}
|
|
230
|
+
get password(): string {
|
|
231
|
+
return this.#parts.password;
|
|
232
|
+
}
|
|
233
|
+
get hostname(): string {
|
|
234
|
+
return this.#parts.hostname;
|
|
235
|
+
}
|
|
236
|
+
get port(): string {
|
|
237
|
+
return this.#parts.port;
|
|
238
|
+
}
|
|
239
|
+
get host(): string {
|
|
240
|
+
const { hostname, port } = this.#parts;
|
|
241
|
+
return port === "" ? hostname : `${hostname}:${port}`;
|
|
242
|
+
}
|
|
243
|
+
get origin(): string {
|
|
244
|
+
const { protocol, opaque } = this.#parts;
|
|
245
|
+
if (opaque !== null || protocol === "file:") return "null";
|
|
246
|
+
return `${protocol}//${this.host}`;
|
|
247
|
+
}
|
|
248
|
+
get pathname(): string {
|
|
249
|
+
return this.#parts.pathname;
|
|
250
|
+
}
|
|
251
|
+
set pathname(value: string) {
|
|
252
|
+
if (this.#parts.opaque !== null) return;
|
|
253
|
+
this.#parts.pathname = removeDotSegments(
|
|
254
|
+
value.startsWith("/") ? value : `/${value}`,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
get search(): string {
|
|
258
|
+
return this.#parts.search;
|
|
259
|
+
}
|
|
260
|
+
set search(value: string) {
|
|
261
|
+
const query = value.startsWith("?") ? value.slice(1) : value;
|
|
262
|
+
this.#parts.search = query === "" ? "" : `?${query}`;
|
|
263
|
+
this.#query = null;
|
|
264
|
+
}
|
|
265
|
+
get hash(): string {
|
|
266
|
+
return this.#parts.hash;
|
|
267
|
+
}
|
|
268
|
+
set hash(value: string) {
|
|
269
|
+
const fragment = value.startsWith("#") ? value.slice(1) : value;
|
|
270
|
+
this.#parts.hash = fragment === "" ? "" : `#${fragment}`;
|
|
271
|
+
}
|
|
272
|
+
get searchParams(): URLSearchParams {
|
|
273
|
+
if (this.#query === null) {
|
|
274
|
+
this.#query = new LinkedSearchParams(this.#parts.search, (query) => {
|
|
275
|
+
this.#parts.search = query === "" ? "" : `?${query}`;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
return this.#query;
|
|
279
|
+
}
|
|
280
|
+
get href(): string {
|
|
281
|
+
const { protocol, username, password, opaque, pathname, search, hash } =
|
|
282
|
+
this.#parts;
|
|
283
|
+
if (opaque !== null) return `${protocol}${opaque}`;
|
|
284
|
+
const credentials =
|
|
285
|
+
username === "" && password === ""
|
|
286
|
+
? ""
|
|
287
|
+
: `${username}${password === "" ? "" : `:${password}`}@`;
|
|
288
|
+
return `${protocol}//${credentials}${this.host}${pathname}${search}${hash}`;
|
|
289
|
+
}
|
|
290
|
+
toString(): string {
|
|
291
|
+
return this.href;
|
|
292
|
+
}
|
|
293
|
+
toJSON(): string {
|
|
294
|
+
return this.href;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** Whether `Url` resolves references and links its query as the standard says. */
|
|
299
|
+
function isStandard(Url: PlatformUrl): boolean {
|
|
300
|
+
try {
|
|
301
|
+
if (
|
|
302
|
+
new Url("/b?x=1", "https://h.test/a/c").href !== "https://h.test/b?x=1"
|
|
303
|
+
) {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
if (new Url("../d", "https://h.test/a/b/c").href !== "https://h.test/a/d") {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
const url = new Url("https://h.test/");
|
|
310
|
+
url.searchParams.append("q", "1");
|
|
311
|
+
return url.search === "?q=1";
|
|
312
|
+
} catch {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Put a standard `URL` in place of the platform's when the platform's is not
|
|
319
|
+
* one; true when it did. The platform's `createObjectURL` (blobs) stays
|
|
320
|
+
* reachable through the new one.
|
|
321
|
+
*/
|
|
322
|
+
export function installStandardUrl(): boolean {
|
|
323
|
+
const current: unknown = Reflect.get(globalThis, "URL");
|
|
324
|
+
if (typeof current !== "function") {
|
|
325
|
+
Reflect.set(globalThis, "URL", StandardURL);
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
const platform: PlatformUrl = Reflect.get(globalThis, "URL");
|
|
329
|
+
if (isStandard(platform)) return false;
|
|
330
|
+
platformUrl = platform;
|
|
331
|
+
Reflect.set(globalThis, "URL", StandardURL);
|
|
332
|
+
return true;
|
|
333
|
+
}
|