react-native-roku 0.0.0-stage → 0.2.0-preview.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +328 -0
- package/CONTRIBUTING.md +34 -0
- package/LICENSE +21 -0
- package/NOTICE.md +10 -0
- package/README.md +66 -2
- package/bin/react-native-roku.mjs +91 -0
- package/build/assets.cjs +311 -0
- package/build/assets.d.cts +50 -0
- package/build/compatibility.d.mts +40 -0
- package/build/compatibility.mjs +272 -0
- package/build/dev.d.mts +34 -0
- package/build/dev.mjs +297 -0
- package/build/devWorkspace.d.mts +11 -0
- package/build/devWorkspace.mjs +86 -0
- package/build/device.d.mts +47 -0
- package/build/device.mjs +310 -0
- package/build/environment.d.mts +29 -0
- package/build/environment.mjs +144 -0
- package/build/esbuild.d.mts +5 -0
- package/build/esbuild.mjs +118 -0
- package/build/fontVariants.d.mts +27 -0
- package/build/fontVariants.mjs +424 -0
- package/build/frontend.d.mts +52 -0
- package/build/frontend.mjs +75 -0
- package/build/identity.mjs +45 -0
- package/build/index.d.mts +22 -0
- package/build/index.mjs +87 -0
- package/build/intl.mjs +121 -0
- package/build/metro.cjs +38 -0
- package/build/metro.d.cts +37 -0
- package/build/metroFrontend.mjs +222 -0
- package/build/metroMetadata.mjs +119 -0
- package/build/metroModules.mjs +397 -0
- package/build/metroRefresh.d.mts +64 -0
- package/build/metroRefresh.mjs +346 -0
- package/build/metroTransformer.cjs +146 -0
- package/build/nativeTextMemo.mjs +296 -0
- package/build/nativewind.d.mts +9 -0
- package/build/nativewind.mjs +92 -0
- package/build/project.d.mts +111 -0
- package/build/project.mjs +615 -0
- package/build/reactCompiler.mjs +113 -0
- package/build/reactProfile.cjs +35 -0
- package/build/refreshRuntime.mjs +187 -0
- package/build/refreshSourceMap.mjs +53 -0
- package/build/release.mjs +99 -0
- package/build/resolution.d.mts +3 -0
- package/build/resolution.mjs +25 -0
- package/build/surfaces.d.mts +1 -0
- package/build/surfaces.mjs +42 -0
- package/build/testing/node.d.mts +29 -0
- package/build/testing/node.mjs +106 -0
- package/build/yoga.d.mts +8 -0
- package/build/yoga.mjs +186 -0
- package/compatibility/public-api.json +327 -0
- package/compatibility/registry.json +2258 -0
- package/config/release.json +15 -0
- package/config/toolchain.json +34 -0
- package/docs/compatibility.md +813 -0
- package/docs/getting-started.md +460 -0
- package/docs/maintenance.md +368 -0
- package/docs/qualification.md +241 -0
- package/docs/standalone-testing.md +56 -0
- package/examples/hello-roku/README.md +35 -0
- package/examples/hello-roku/package.template.json +31 -0
- package/examples/hello-roku/pnpm-workspace.yaml +11 -0
- package/examples/hello-roku/roku.config.json +22 -0
- package/examples/hello-roku/src/App.tsx +54 -0
- package/examples/hello-roku/src/Catalog.tsx +200 -0
- package/examples/hello-roku/src/assets.d.ts +16 -0
- package/examples/hello-roku/src/catalog-cover.png +0 -0
- package/examples/hello-roku/tsconfig.json +1 -0
- package/examples/hello-roku/tsconfig.react.json +1 -0
- package/native/components/RNRAppControl.brs +215 -0
- package/native/components/RNRAppControl.xml +13 -0
- package/native/components/RNRDeviceFacts.brs +19 -0
- package/native/components/RNRDeviceFacts.xml +9 -0
- package/native/components/RNRDeviceFactsTask.brs +74 -0
- package/native/components/RNRDeviceFactsTask.xml +9 -0
- package/native/components/RNRTextRaster.brs +15 -0
- package/native/components/RNRTextRaster.xml +9 -0
- package/native/components/RNRTextRasterTask.brs +77 -0
- package/native/components/RNRTextRasterTask.xml +9 -0
- package/native/manifest.json +7 -0
- package/package.json +183 -3
- package/src/animation/clock.ts +10 -0
- package/src/animation/derivedValue.ts +163 -0
- package/src/animation/legacyAnimated.ts +255 -0
- package/src/animation/reanimated.ts +243 -0
- package/src/animation/sharedValue.ts +285 -0
- package/src/animation/worklets.ts +23 -0
- package/src/application/AppRegistry.ts +31 -0
- package/src/application/mountApplication.ts +136 -0
- package/src/graphics/BitmapLayer.tsx +68 -0
- package/src/graphics/RoundedBorder.tsx +116 -0
- package/src/graphics/TextRasterCache.ts +259 -0
- package/src/graphics/gradient.ts +37 -0
- package/src/graphics/inlineRaster.ts +85 -0
- package/src/graphics/png.ts +77 -0
- package/src/graphics/rasterPixels.ts +89 -0
- package/src/graphics/surfaces.ts +105 -0
- package/src/graphics/textRaster.types.ts +60 -0
- package/src/graphics/textRasterGeometry.ts +116 -0
- package/src/host/host.types.ts +57 -0
- package/src/index.ts +22 -0
- package/src/interop/nativewind.ts +2 -0
- package/src/layout/FontResources.ts +252 -0
- package/src/layout/affine.ts +89 -0
- package/src/layout/flexLayout.ts +398 -0
- package/src/layout/fontFaces.d.mts +19 -0
- package/src/layout/fontFaces.mjs +85 -0
- package/src/layout/fontVariants.d.mts +2 -0
- package/src/layout/fontVariants.mjs +8 -0
- package/src/layout/inlineParagraph.ts +158 -0
- package/src/layout/inlineText.ts +68 -0
- package/src/layout/layoutReuse.ts +117 -0
- package/src/layout/nativeNodeFields.ts +179 -0
- package/src/layout/preparedFields.ts +78 -0
- package/src/layout/textLayout.ts +138 -0
- package/src/layout/transform.ts +113 -0
- package/src/media/SceneGraphAudio.ts +161 -0
- package/src/media/SceneGraphVideo.ts +167 -0
- package/src/media/audioSession.ts +84 -0
- package/src/media/index.ts +2 -0
- package/src/media/videoErrorDetails.ts +40 -0
- package/src/media/videoSession.ts +136 -0
- package/src/navigation/RouterStore.ts +244 -0
- package/src/navigation/expoRouter.ts +115 -0
- package/src/react-native/NativeInput.ts +494 -0
- package/src/react-native/NativeSystemState.ts +95 -0
- package/src/react-native/accessibility.ts +265 -0
- package/src/react-native/activityIndicator.ts +72 -0
- package/src/react-native/compilerPressable.ts +26 -0
- package/src/react-native/createNativeRoot.ts +2126 -0
- package/src/react-native/directionalFocus.ts +55 -0
- package/src/react-native/environment.ts +275 -0
- package/src/react-native/flatList.ts +1001 -0
- package/src/react-native/flatListViewability.ts +231 -0
- package/src/react-native/focusEvents.ts +85 -0
- package/src/react-native/image.ts +247 -0
- package/src/react-native/index.ts +54 -0
- package/src/react-native/interactionManager.ts +76 -0
- package/src/react-native/keyboard.ts +60 -0
- package/src/react-native/linearGradient.ts +94 -0
- package/src/react-native/modal.tsx +148 -0
- package/src/react-native/nativeHostContext.ts +80 -0
- package/src/react-native/nativeModules.ts +86 -0
- package/src/react-native/nativeRoot.types.ts +22 -0
- package/src/react-native/nativeViewHandle.ts +191 -0
- package/src/react-native/nativewindPressable.ts +38 -0
- package/src/react-native/panResponder.ts +73 -0
- package/src/react-native/primitives.ts +685 -0
- package/src/react-native/safeArea.ts +76 -0
- package/src/react-native/scrollView.ts +289 -0
- package/src/react-native/styleInterop.ts +8 -0
- package/src/react-native/styleSheet.ts +54 -0
- package/src/react-native/textInput.ts +334 -0
- package/src/react-native/unsupported.ts +25 -0
- package/src/renderer/SceneRoot.ts +1006 -0
- package/src/scene/SdkDeviceFacts.ts +151 -0
- package/src/scene/SdkDeviceServices.ts +72 -0
- package/src/scene/SdkSceneGraph.ts +124 -0
- package/src/scene/SdkTextEntry.ts +321 -0
- package/src/scene/SdkTextMetrics.ts +136 -0
- package/src/scene/SdkTextRasterTransport.ts +191 -0
- package/src/scene/SdkWebTransport.ts +122 -0
- package/src/scene/createRokuHost.ts +213 -0
- package/src/scene/sceneGraph.types.ts +38 -0
- package/src/services/DeviceServices.ts +53 -0
- package/src/services/RegistryStorage.ts +85 -0
- package/src/services/RokuKeySession.ts +29 -0
- package/src/services/asyncStorage.ts +23 -0
- package/src/services/base64.ts +52 -0
- package/src/services/crypto.ts +18 -0
- package/src/services/expoConstants.ts +11 -0
- package/src/services/expoLinking.ts +12 -0
- package/src/services/expoStatusBar.ts +4 -0
- package/src/services/installWebRuntime.ts +105 -0
- package/src/services/intlEnglish.ts +54 -0
- package/src/services/languageRuntime.ts +65 -0
- package/src/services/nativeTimers.ts +96 -0
- package/src/services/secureStore.ts +23 -0
- package/src/services/textEntryRequest.ts +22 -0
- package/src/services/utf8.ts +57 -0
- package/src/services/webRuntime.ts +346 -0
- package/src/testing/CanvasResources.ts +141 -0
- package/src/testing/CanvasSceneGraph.ts +496 -0
- package/src/testing/CanvasTextEntry.ts +237 -0
- package/src/testing/PreviewTextObservations.ts +82 -0
- package/src/testing/RecordingSceneGraph.ts +136 -0
- package/src/types/normalize-colors.d.ts +4 -0
- package/src/vector/dynamicSvg.tsx +226 -0
- package/src/vector/svg.ts +222 -0
- package/src/vector/svg.types.ts +4 -0
- package/src/vector/svgElements.ts +58 -0
- package/src/vector/svgKey.d.mts +4 -0
- package/src/vector/svgKey.mjs +16 -0
- package/src/vector/svgPath.ts +157 -0
- package/src/vector/svgPixels.ts +133 -0
- package/tsconfig.consumer.json +29 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** UTF-8 bytes for device encryption, independent of optional firmware TextEncoder globals. */
|
|
2
|
+
export function encodeUtf8(value: string): Uint8Array {
|
|
3
|
+
const bytes: number[] = [];
|
|
4
|
+
for (const character of value) {
|
|
5
|
+
let point = character.codePointAt(0)!;
|
|
6
|
+
if (point >= 0xd800 && point <= 0xdfff) point = 0xfffd;
|
|
7
|
+
if (point < 0x80) bytes.push(point);
|
|
8
|
+
else if (point < 0x800) bytes.push(0xc0 | (point >> 6), 0x80 | (point & 0x3f));
|
|
9
|
+
else if (point < 0x10000)
|
|
10
|
+
bytes.push(0xe0 | (point >> 12), 0x80 | ((point >> 6) & 0x3f), 0x80 | (point & 0x3f));
|
|
11
|
+
else
|
|
12
|
+
bytes.push(
|
|
13
|
+
0xf0 | (point >> 18),
|
|
14
|
+
0x80 | ((point >> 12) & 0x3f),
|
|
15
|
+
0x80 | ((point >> 6) & 0x3f),
|
|
16
|
+
0x80 | (point & 0x3f),
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return new Uint8Array(bytes);
|
|
20
|
+
}
|
|
21
|
+
export function decodeUtf8(bytes: Uint8Array): string {
|
|
22
|
+
let result = "";
|
|
23
|
+
for (let index = 0; index < bytes.length;) {
|
|
24
|
+
const first = bytes[index++];
|
|
25
|
+
let point = first,
|
|
26
|
+
count = 0,
|
|
27
|
+
minimum = 0;
|
|
28
|
+
if (first >= 0xc2 && first <= 0xdf) {
|
|
29
|
+
count = 1;
|
|
30
|
+
point = first & 0x1f;
|
|
31
|
+
minimum = 0x80;
|
|
32
|
+
} else if (first >= 0xe0 && first <= 0xef) {
|
|
33
|
+
count = 2;
|
|
34
|
+
point = first & 0x0f;
|
|
35
|
+
minimum = 0x800;
|
|
36
|
+
} else if (first >= 0xf0 && first <= 0xf4) {
|
|
37
|
+
count = 3;
|
|
38
|
+
point = first & 7;
|
|
39
|
+
minimum = 0x10000;
|
|
40
|
+
} else if (first >= 0x80) throw new Error("Invalid UTF-8 credential bytes");
|
|
41
|
+
for (let next = 0; next < count; next++) {
|
|
42
|
+
const byte = bytes[index++];
|
|
43
|
+
if (byte == null || (byte & 0xc0) !== 0x80) throw new Error("Invalid UTF-8 credential bytes");
|
|
44
|
+
point = (point << 6) | (byte & 0x3f);
|
|
45
|
+
}
|
|
46
|
+
if (point < minimum || point > 0x10ffff || (point >= 0xd800 && point <= 0xdfff))
|
|
47
|
+
throw new Error("Invalid UTF-8 credential bytes");
|
|
48
|
+
result += String.fromCodePoint(point);
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
export const toHex = (bytes: Uint8Array): string =>
|
|
53
|
+
Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
54
|
+
export function fromHex(value: string): Uint8Array {
|
|
55
|
+
if (!/^(?:[0-9a-f]{2})*$/i.test(value)) throw new Error("Invalid encrypted credential encoding");
|
|
56
|
+
return new Uint8Array(value.match(/../g)?.map((pair) => parseInt(pair, 16)) ?? []);
|
|
57
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { encodeUtf8 } from "./utf8";
|
|
2
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
3
|
+
|
|
4
|
+
export class PortableDOMException extends Error {
|
|
5
|
+
constructor(message = "", name = "Error") {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = name;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
type AbortListener = EventListenerOrEventListenerObject;
|
|
11
|
+
export class PortableAbortSignal {
|
|
12
|
+
aborted = false;
|
|
13
|
+
reason: unknown;
|
|
14
|
+
onabort: ((event: Event) => void) | null = null;
|
|
15
|
+
private readonly listeners = new Map<AbortListener, boolean>();
|
|
16
|
+
addEventListener(
|
|
17
|
+
type: string,
|
|
18
|
+
listener: AbortListener | null,
|
|
19
|
+
options?: boolean | AddEventListenerOptions,
|
|
20
|
+
) {
|
|
21
|
+
if (type === "abort" && listener && !this.listeners.has(listener))
|
|
22
|
+
this.listeners.set(listener, typeof options === "object" && options.once === true);
|
|
23
|
+
}
|
|
24
|
+
removeEventListener(type: string, listener: AbortListener | null) {
|
|
25
|
+
if (type === "abort" && listener) this.listeners.delete(listener);
|
|
26
|
+
}
|
|
27
|
+
throwIfAborted() {
|
|
28
|
+
if (this.aborted) throw this.reason;
|
|
29
|
+
}
|
|
30
|
+
abort(reason: unknown = new PortableDOMException("The operation was aborted", "AbortError")) {
|
|
31
|
+
if (this.aborted) return;
|
|
32
|
+
this.aborted = true;
|
|
33
|
+
this.reason = reason;
|
|
34
|
+
const event = {
|
|
35
|
+
type: "abort",
|
|
36
|
+
target: this,
|
|
37
|
+
currentTarget: this,
|
|
38
|
+
} as unknown as Event;
|
|
39
|
+
this.onabort?.(event);
|
|
40
|
+
for (const [listener, once] of [...this.listeners]) {
|
|
41
|
+
if (!this.listeners.has(listener)) continue;
|
|
42
|
+
if (once) this.listeners.delete(listener);
|
|
43
|
+
if (typeof listener === "function") listener.call(this as unknown as EventTarget, event);
|
|
44
|
+
else listener.handleEvent(event);
|
|
45
|
+
}
|
|
46
|
+
this.listeners.clear();
|
|
47
|
+
}
|
|
48
|
+
static abort(reason?: unknown) {
|
|
49
|
+
const signal = new PortableAbortSignal();
|
|
50
|
+
signal.abort(reason);
|
|
51
|
+
return signal;
|
|
52
|
+
}
|
|
53
|
+
static timeout(milliseconds: number) {
|
|
54
|
+
if (!Number.isInteger(milliseconds) || milliseconds < 0 || milliseconds > 2_147_483_647)
|
|
55
|
+
throw new RangeError("Abort timeout must fit a nonnegative native timer");
|
|
56
|
+
const signal = new PortableAbortSignal();
|
|
57
|
+
const timer = setTimeout(
|
|
58
|
+
() => signal.abort(new PortableDOMException("The operation timed out", "TimeoutError")),
|
|
59
|
+
milliseconds,
|
|
60
|
+
);
|
|
61
|
+
// Node host qualification must not stay alive solely for a retired deadline.
|
|
62
|
+
(timer as unknown as { unref?: () => void }).unref?.();
|
|
63
|
+
signal.addEventListener("abort", () => clearTimeout(timer), { once: true });
|
|
64
|
+
return signal;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export class PortableAbortController {
|
|
68
|
+
readonly signal = new PortableAbortSignal();
|
|
69
|
+
abort(reason?: unknown) {
|
|
70
|
+
this.signal.abort(reason);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
type HeaderInput = HeadersInit | PortableHeaders;
|
|
75
|
+
export class PortableHeaders implements Iterable<[string, string]> {
|
|
76
|
+
private readonly fields = new Map<string, string>();
|
|
77
|
+
constructor(input?: HeaderInput) {
|
|
78
|
+
if (!input) return;
|
|
79
|
+
if (typeof (input as Iterable<unknown>)[Symbol.iterator] === "function") {
|
|
80
|
+
for (const entry of input as Iterable<[string, string]>) {
|
|
81
|
+
if (!Array.isArray(entry) || entry.length !== 2)
|
|
82
|
+
throw new TypeError("Headers require name/value pairs");
|
|
83
|
+
this.append(entry[0], entry[1]);
|
|
84
|
+
}
|
|
85
|
+
} else for (const [name, value] of Object.entries(input)) this.append(name, value);
|
|
86
|
+
}
|
|
87
|
+
private name(value: string) {
|
|
88
|
+
const name = String(value).toLowerCase();
|
|
89
|
+
if (!/^[!#$%&'*+.^_`|~0-9a-z-]+$/.test(name)) throw new TypeError("Invalid header name");
|
|
90
|
+
return name;
|
|
91
|
+
}
|
|
92
|
+
private value(value: string) {
|
|
93
|
+
const text = String(value).replace(/^[\t ]+|[\t ]+$/g, "");
|
|
94
|
+
if (/[\0\r\n]/.test(text)) throw new TypeError("Invalid header value");
|
|
95
|
+
return text;
|
|
96
|
+
}
|
|
97
|
+
append(name: string, value: string) {
|
|
98
|
+
const key = this.name(name),
|
|
99
|
+
text = this.value(value);
|
|
100
|
+
this.fields.set(key, this.fields.has(key) ? `${this.fields.get(key)}, ${text}` : text);
|
|
101
|
+
}
|
|
102
|
+
set(name: string, value: string) {
|
|
103
|
+
this.fields.set(this.name(name), this.value(value));
|
|
104
|
+
}
|
|
105
|
+
delete(name: string) {
|
|
106
|
+
this.fields.delete(this.name(name));
|
|
107
|
+
}
|
|
108
|
+
get(name: string) {
|
|
109
|
+
return this.fields.get(this.name(name)) ?? null;
|
|
110
|
+
}
|
|
111
|
+
has(name: string) {
|
|
112
|
+
return this.fields.has(this.name(name));
|
|
113
|
+
}
|
|
114
|
+
*entries(): IterableIterator<[string, string]> {
|
|
115
|
+
yield* [...this.fields].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
116
|
+
}
|
|
117
|
+
*keys() {
|
|
118
|
+
for (const [key] of this.entries()) yield key;
|
|
119
|
+
}
|
|
120
|
+
*values() {
|
|
121
|
+
for (const [, value] of this.entries()) yield value;
|
|
122
|
+
}
|
|
123
|
+
[Symbol.iterator]() {
|
|
124
|
+
return this.entries();
|
|
125
|
+
}
|
|
126
|
+
forEach(
|
|
127
|
+
callback: (value: string, key: string, headers: PortableHeaders) => void,
|
|
128
|
+
thisArg?: unknown,
|
|
129
|
+
) {
|
|
130
|
+
for (const [key, value] of this.entries()) callback.call(thisArg, value, key, this);
|
|
131
|
+
}
|
|
132
|
+
getSetCookie(): string[] {
|
|
133
|
+
return [];
|
|
134
|
+
} // Native API requests use explicit credentials, never a browser cookie jar.
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export class PortableResponse {
|
|
138
|
+
readonly status: number;
|
|
139
|
+
readonly statusText: string;
|
|
140
|
+
readonly headers: PortableHeaders;
|
|
141
|
+
readonly ok: boolean;
|
|
142
|
+
readonly redirected = false;
|
|
143
|
+
readonly type = "default";
|
|
144
|
+
readonly body = null;
|
|
145
|
+
bodyUsed = false;
|
|
146
|
+
url = "";
|
|
147
|
+
private readonly data: string;
|
|
148
|
+
constructor(body: string | null = null, init: ResponseInit = {}) {
|
|
149
|
+
this.status = init.status ?? 200;
|
|
150
|
+
if (!Number.isInteger(this.status) || this.status < 200 || this.status > 599)
|
|
151
|
+
throw new RangeError("Invalid response status");
|
|
152
|
+
if ([204, 205, 304].includes(this.status) && body !== null)
|
|
153
|
+
throw new TypeError("This response status cannot have a body");
|
|
154
|
+
this.data = body ?? "";
|
|
155
|
+
this.statusText = init.statusText ?? "";
|
|
156
|
+
this.headers = new PortableHeaders(init.headers);
|
|
157
|
+
this.ok = this.status >= 200 && this.status < 300;
|
|
158
|
+
}
|
|
159
|
+
async text(): Promise<string> {
|
|
160
|
+
if (this.bodyUsed) throw new TypeError("Response body was already consumed");
|
|
161
|
+
this.bodyUsed = true;
|
|
162
|
+
return this.data;
|
|
163
|
+
}
|
|
164
|
+
async json(): Promise<unknown> {
|
|
165
|
+
return JSON.parse(await this.text());
|
|
166
|
+
}
|
|
167
|
+
async arrayBuffer(): Promise<ArrayBuffer> {
|
|
168
|
+
return encodeUtf8(await this.text()).buffer as ArrayBuffer;
|
|
169
|
+
}
|
|
170
|
+
clone(): PortableResponse {
|
|
171
|
+
if (this.bodyUsed) throw new TypeError("Response body was already consumed");
|
|
172
|
+
const result = new PortableResponse(this.data || null, {
|
|
173
|
+
status: this.status,
|
|
174
|
+
statusText: this.statusText,
|
|
175
|
+
headers: Object.fromEntries(this.headers),
|
|
176
|
+
});
|
|
177
|
+
result.url = this.url;
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
static json(value: unknown, init: ResponseInit = {}) {
|
|
181
|
+
const headers = new PortableHeaders(init.headers);
|
|
182
|
+
if (!headers.has("content-type")) headers.set("content-type", "application/json");
|
|
183
|
+
return new PortableResponse(JSON.stringify(value), {
|
|
184
|
+
...init,
|
|
185
|
+
headers: headers as unknown as Headers,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export class PortableRequest {
|
|
191
|
+
readonly url: string;
|
|
192
|
+
readonly method: string;
|
|
193
|
+
readonly headers: PortableHeaders;
|
|
194
|
+
readonly signal: AbortSignal;
|
|
195
|
+
readonly credentials: RequestCredentials;
|
|
196
|
+
readonly redirect: RequestRedirect;
|
|
197
|
+
bodyUsed = false;
|
|
198
|
+
private readonly data: string;
|
|
199
|
+
constructor(input: string | URL | PortableRequest, init: RequestInit = {}) {
|
|
200
|
+
const previous = input instanceof PortableRequest ? input : undefined;
|
|
201
|
+
this.url = new URL(previous?.url ?? String(input)).href;
|
|
202
|
+
this.method = String(init.method ?? previous?.method ?? "GET").toUpperCase();
|
|
203
|
+
this.headers = new PortableHeaders(init.headers ?? previous?.headers);
|
|
204
|
+
this.credentials = init.credentials ?? previous?.credentials ?? "same-origin";
|
|
205
|
+
this.redirect = init.redirect ?? previous?.redirect ?? "follow";
|
|
206
|
+
if (
|
|
207
|
+
!["omit", "same-origin", "include"].includes(this.credentials) ||
|
|
208
|
+
!["follow", "error", "manual"].includes(this.redirect)
|
|
209
|
+
)
|
|
210
|
+
throw new TypeError("Unsupported Request credential or redirect policy");
|
|
211
|
+
this.signal =
|
|
212
|
+
init.signal ??
|
|
213
|
+
previous?.signal ??
|
|
214
|
+
(new PortableAbortController().signal as unknown as AbortSignal);
|
|
215
|
+
const body = init.body ?? previous?.data ?? "";
|
|
216
|
+
if (typeof body !== "string")
|
|
217
|
+
throw new RokuHostError("C22_FETCH_BODY", "Request admits text bodies only");
|
|
218
|
+
if (["GET", "HEAD"].includes(this.method) && (init.body != null || body))
|
|
219
|
+
throw new TypeError("GET and HEAD requests cannot have a body");
|
|
220
|
+
this.data = body;
|
|
221
|
+
}
|
|
222
|
+
async text() {
|
|
223
|
+
if (this.bodyUsed) throw new TypeError("Request body was already consumed");
|
|
224
|
+
this.bodyUsed = true;
|
|
225
|
+
return this.data;
|
|
226
|
+
}
|
|
227
|
+
async json() {
|
|
228
|
+
return JSON.parse(await this.text());
|
|
229
|
+
}
|
|
230
|
+
clone() {
|
|
231
|
+
if (this.bodyUsed) throw new TypeError("Request body was already consumed");
|
|
232
|
+
return new PortableRequest(this);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface WebRequest {
|
|
237
|
+
readonly url: string;
|
|
238
|
+
readonly method: string;
|
|
239
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
240
|
+
readonly body?: string;
|
|
241
|
+
readonly signal?: AbortSignal;
|
|
242
|
+
readonly redirect?: WebRedirectMode;
|
|
243
|
+
}
|
|
244
|
+
export interface WebResponse {
|
|
245
|
+
readonly status: number;
|
|
246
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
247
|
+
readonly body: string;
|
|
248
|
+
readonly url?: string;
|
|
249
|
+
}
|
|
250
|
+
export type WebRedirectMode = "follow" | "error";
|
|
251
|
+
export type WebTransport = ((request: WebRequest) => Promise<WebResponse>) & {
|
|
252
|
+
/** Omission preserves the legacy follow-only contract. Each advertised mode
|
|
253
|
+
* must be enforced before a redirect hop, not inferred from a final response. */
|
|
254
|
+
readonly redirectModes?: readonly WebRedirectMode[];
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/** The SDK fetch takes a native Request, not the browser's second init argument. */
|
|
258
|
+
export function createPortableFetch(transport: WebTransport) {
|
|
259
|
+
const modes = [...(transport.redirectModes ?? ["follow"])];
|
|
260
|
+
if (modes.some((mode) => mode !== "follow" && mode !== "error"))
|
|
261
|
+
throw new TypeError("Unsupported transport redirect capability");
|
|
262
|
+
return async (input: RequestInfo | URL, init: RequestInit = {}): Promise<Response> => {
|
|
263
|
+
const request = typeof input === "object" && "url" in input ? (input as Request) : undefined;
|
|
264
|
+
const url = new URL(request?.url ?? String(input));
|
|
265
|
+
if (!["http:", "https:"].includes(url.protocol))
|
|
266
|
+
throw new TypeError("Fetch requires an HTTP URL");
|
|
267
|
+
if (url.username || url.password) throw new TypeError("Fetch URLs cannot contain credentials");
|
|
268
|
+
url.hash = "";
|
|
269
|
+
const signal = init.signal ?? request?.signal;
|
|
270
|
+
if (signal?.aborted)
|
|
271
|
+
throw signal.reason ?? new PortableDOMException("The operation was aborted", "AbortError");
|
|
272
|
+
if ((init.credentials ?? request?.credentials) === "include")
|
|
273
|
+
throw new RokuHostError("C22_FETCH_COOKIES", "Native requests have no browser cookie jar");
|
|
274
|
+
const redirect = init.redirect ?? request?.redirect ?? "follow";
|
|
275
|
+
if (!["follow", "error", "manual"].includes(redirect))
|
|
276
|
+
throw new TypeError("Invalid redirect policy");
|
|
277
|
+
if (!modes.includes(redirect as WebRedirectMode))
|
|
278
|
+
throw new RokuHostError(
|
|
279
|
+
"C22_FETCH_REDIRECT",
|
|
280
|
+
"This transport cannot enforce the requested redirect policy",
|
|
281
|
+
);
|
|
282
|
+
const method = String(init.method ?? request?.method ?? "GET").toUpperCase();
|
|
283
|
+
if (!["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"].includes(method))
|
|
284
|
+
throw new TypeError("Unsupported request method");
|
|
285
|
+
const headers = new PortableHeaders(init.headers ?? request?.headers);
|
|
286
|
+
let body = init.body;
|
|
287
|
+
if (body === undefined && request && !["GET", "HEAD"].includes(request.method))
|
|
288
|
+
body = await request.clone().text();
|
|
289
|
+
if (body instanceof URLSearchParams) {
|
|
290
|
+
if (!headers.has("content-type"))
|
|
291
|
+
headers.set("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
|
|
292
|
+
body = body.toString();
|
|
293
|
+
}
|
|
294
|
+
if (body != null && typeof body !== "string")
|
|
295
|
+
throw new RokuHostError(
|
|
296
|
+
"C22_FETCH_BODY",
|
|
297
|
+
"This native transport admits text request bodies only",
|
|
298
|
+
);
|
|
299
|
+
if (["GET", "HEAD"].includes(method) && body != null)
|
|
300
|
+
throw new TypeError("GET and HEAD requests cannot have a body");
|
|
301
|
+
// Reading a caller's cloned Request body can yield. An abort in that window
|
|
302
|
+
// predates listener registration and must still prevent native dispatch.
|
|
303
|
+
if (signal?.aborted)
|
|
304
|
+
throw signal.reason ?? new PortableDOMException("The operation was aborted", "AbortError");
|
|
305
|
+
let cancel: (() => void) | undefined;
|
|
306
|
+
const cancelled = new Promise<never>((_, reject) => {
|
|
307
|
+
cancel = () =>
|
|
308
|
+
reject(
|
|
309
|
+
signal?.reason ?? new PortableDOMException("The operation was aborted", "AbortError"),
|
|
310
|
+
);
|
|
311
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
312
|
+
});
|
|
313
|
+
try {
|
|
314
|
+
const result = await Promise.race([
|
|
315
|
+
transport({
|
|
316
|
+
url: url.href,
|
|
317
|
+
method,
|
|
318
|
+
redirect: redirect as WebRedirectMode,
|
|
319
|
+
headers: Object.fromEntries(headers),
|
|
320
|
+
...(body == null ? {} : { body }),
|
|
321
|
+
...(signal ? { signal } : {}),
|
|
322
|
+
}),
|
|
323
|
+
cancelled,
|
|
324
|
+
]);
|
|
325
|
+
if (signal?.aborted)
|
|
326
|
+
throw signal.reason ?? new PortableDOMException("The operation was aborted", "AbortError");
|
|
327
|
+
if (
|
|
328
|
+
redirect === "error" &&
|
|
329
|
+
([301, 302, 303, 307, 308].includes(result.status) ||
|
|
330
|
+
(result.url && result.url !== url.href))
|
|
331
|
+
)
|
|
332
|
+
throw new TypeError("A redirect is not allowed for this request");
|
|
333
|
+
const response = new PortableResponse(
|
|
334
|
+
[204, 205, 304].includes(result.status) ? null : result.body,
|
|
335
|
+
{
|
|
336
|
+
status: result.status,
|
|
337
|
+
headers: result.headers,
|
|
338
|
+
},
|
|
339
|
+
);
|
|
340
|
+
response.url = result.url ?? url.href;
|
|
341
|
+
return response as unknown as Response;
|
|
342
|
+
} finally {
|
|
343
|
+
if (cancel) signal?.removeEventListener("abort", cancel);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/** Shared, bounded registrations for exact (URI, family) font identities.
|
|
2
|
+
* A factory returns an independently releasable registration token. Native and
|
|
3
|
+
* browser Canvas hosts supply their own IO and keep their asset/URI policies. */
|
|
4
|
+
export function createCanvasFontRegistry<Key>(options: {
|
|
5
|
+
register(uri: string, family: string): Key | Promise<Key>;
|
|
6
|
+
unregister(key: Key): void;
|
|
7
|
+
limit?: number;
|
|
8
|
+
}) {
|
|
9
|
+
const limit = options.limit ?? 32;
|
|
10
|
+
if (!Number.isSafeInteger(limit) || limit < 1) throw Error("Invalid Canvas font budget");
|
|
11
|
+
interface Entry {
|
|
12
|
+
owners: Set<object>;
|
|
13
|
+
pending: Promise<void>;
|
|
14
|
+
key?: Key;
|
|
15
|
+
ready: boolean;
|
|
16
|
+
retired: boolean;
|
|
17
|
+
released: boolean;
|
|
18
|
+
failed: boolean;
|
|
19
|
+
}
|
|
20
|
+
const entries = new Map<string, Entry>();
|
|
21
|
+
const retiring = new Set<Entry>();
|
|
22
|
+
const retire = (name: string, entry: Entry) => {
|
|
23
|
+
if (entry.owners.size) return;
|
|
24
|
+
entry.retired = true;
|
|
25
|
+
if (entries.get(name) === entry) entries.delete(name);
|
|
26
|
+
if (entry.failed || entry.released) return;
|
|
27
|
+
retiring.add(entry);
|
|
28
|
+
if (entry.ready) {
|
|
29
|
+
options.unregister(entry.key!);
|
|
30
|
+
entry.released = true;
|
|
31
|
+
retiring.delete(entry);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const retireUnused = () => {
|
|
35
|
+
const failures: unknown[] = [];
|
|
36
|
+
const work: Array<[string, Entry]> = [
|
|
37
|
+
...entries,
|
|
38
|
+
...[...retiring].map((entry): [string, Entry] => ["", entry]),
|
|
39
|
+
];
|
|
40
|
+
for (const [name, entry] of work) {
|
|
41
|
+
try {
|
|
42
|
+
retire(name, entry);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
failures.push(error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (failures.length) throw failures[0];
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
retireUnused,
|
|
51
|
+
snapshot: () => ({
|
|
52
|
+
registrations: entries.size + retiring.size,
|
|
53
|
+
pending: [...entries.values(), ...retiring].filter((entry) => !entry.ready).length,
|
|
54
|
+
owners: [...entries.values()].reduce((sum, entry) => sum + entry.owners.size, 0),
|
|
55
|
+
limit,
|
|
56
|
+
}),
|
|
57
|
+
createOwner() {
|
|
58
|
+
const token = {},
|
|
59
|
+
owned = new Map<string, { entry: Entry; pending: number; ready: boolean }>();
|
|
60
|
+
let retired = false;
|
|
61
|
+
return {
|
|
62
|
+
async ready(uri: string, family: string, signal?: AbortSignal): Promise<void> {
|
|
63
|
+
if (retired) throw Error("Canvas font owner retired");
|
|
64
|
+
if (signal?.aborted) throw signal.reason;
|
|
65
|
+
const name = JSON.stringify([uri, family]);
|
|
66
|
+
let entry = entries.get(name);
|
|
67
|
+
if (!entry) {
|
|
68
|
+
if (entries.size + retiring.size >= limit) retireUnused();
|
|
69
|
+
if (entries.size + retiring.size >= limit)
|
|
70
|
+
throw Error("The Canvas font-family budget was exceeded");
|
|
71
|
+
entry = {
|
|
72
|
+
owners: new Set(),
|
|
73
|
+
pending: Promise.resolve(),
|
|
74
|
+
ready: false,
|
|
75
|
+
retired: false,
|
|
76
|
+
released: false,
|
|
77
|
+
failed: false,
|
|
78
|
+
};
|
|
79
|
+
// Reserve before invoking a factory that can reenter this owner.
|
|
80
|
+
entries.set(name, entry);
|
|
81
|
+
const current = entry;
|
|
82
|
+
current.pending = Promise.resolve()
|
|
83
|
+
.then(() => {
|
|
84
|
+
if (current.retired) throw Error("Canvas font preparation retired");
|
|
85
|
+
return options.register(uri, family);
|
|
86
|
+
})
|
|
87
|
+
.then(
|
|
88
|
+
(key) => {
|
|
89
|
+
current.key = key;
|
|
90
|
+
current.ready = true;
|
|
91
|
+
if (current.retired) retire(name, current);
|
|
92
|
+
},
|
|
93
|
+
(error) => {
|
|
94
|
+
current.failed = true;
|
|
95
|
+
retiring.delete(current);
|
|
96
|
+
if (entries.get(name) === current) entries.delete(name);
|
|
97
|
+
throw error;
|
|
98
|
+
},
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
entry.owners.add(token);
|
|
102
|
+
let lease = owned.get(name);
|
|
103
|
+
if (!lease || lease.entry !== entry) {
|
|
104
|
+
lease = { entry, pending: 0, ready: false };
|
|
105
|
+
owned.set(name, lease);
|
|
106
|
+
}
|
|
107
|
+
lease.pending++;
|
|
108
|
+
try {
|
|
109
|
+
await entry.pending;
|
|
110
|
+
if (retired) throw Error("Canvas font owner retired");
|
|
111
|
+
if (signal?.aborted) throw signal.reason;
|
|
112
|
+
lease.ready = true;
|
|
113
|
+
} finally {
|
|
114
|
+
lease.pending--;
|
|
115
|
+
if (!lease.pending && !lease.ready) {
|
|
116
|
+
entry.owners.delete(token);
|
|
117
|
+
if (owned.get(name) === lease) owned.delete(name);
|
|
118
|
+
// Preparation/abort remains authoritative; failed unregisters
|
|
119
|
+
// remain charged and retryable through retireUnused().
|
|
120
|
+
try {
|
|
121
|
+
retire(name, entry);
|
|
122
|
+
} catch {
|
|
123
|
+
/* Retained for retry. */
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
dispose() {
|
|
129
|
+
if (retired) return;
|
|
130
|
+
retired = true;
|
|
131
|
+
for (const [name, { entry }] of owned) {
|
|
132
|
+
entry.owners.delete(token);
|
|
133
|
+
// Pending completion cannot register an unowned, retired font.
|
|
134
|
+
if (!entry.ready && !entry.owners.size) retire(name, entry);
|
|
135
|
+
}
|
|
136
|
+
owned.clear();
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|