react-native-roku 0.0.0-stage → 0.2.0-preview.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.
Files changed (200) hide show
  1. package/CHANGELOG.md +318 -0
  2. package/CONTRIBUTING.md +34 -0
  3. package/LICENSE +21 -0
  4. package/NOTICE.md +10 -0
  5. package/README.md +66 -2
  6. package/bin/react-native-roku.mjs +91 -0
  7. package/build/assets.cjs +311 -0
  8. package/build/assets.d.cts +50 -0
  9. package/build/compatibility.d.mts +40 -0
  10. package/build/compatibility.mjs +272 -0
  11. package/build/dev.d.mts +34 -0
  12. package/build/dev.mjs +297 -0
  13. package/build/devWorkspace.d.mts +11 -0
  14. package/build/devWorkspace.mjs +86 -0
  15. package/build/device.d.mts +47 -0
  16. package/build/device.mjs +310 -0
  17. package/build/environment.d.mts +29 -0
  18. package/build/environment.mjs +144 -0
  19. package/build/esbuild.d.mts +5 -0
  20. package/build/esbuild.mjs +118 -0
  21. package/build/fontVariants.d.mts +27 -0
  22. package/build/fontVariants.mjs +424 -0
  23. package/build/frontend.d.mts +52 -0
  24. package/build/frontend.mjs +75 -0
  25. package/build/identity.mjs +45 -0
  26. package/build/index.d.mts +22 -0
  27. package/build/index.mjs +87 -0
  28. package/build/intl.mjs +121 -0
  29. package/build/metro.cjs +38 -0
  30. package/build/metro.d.cts +37 -0
  31. package/build/metroFrontend.mjs +222 -0
  32. package/build/metroMetadata.mjs +119 -0
  33. package/build/metroModules.mjs +397 -0
  34. package/build/metroRefresh.d.mts +64 -0
  35. package/build/metroRefresh.mjs +346 -0
  36. package/build/metroTransformer.cjs +146 -0
  37. package/build/nativeTextMemo.mjs +296 -0
  38. package/build/nativewind.d.mts +9 -0
  39. package/build/nativewind.mjs +92 -0
  40. package/build/project.d.mts +111 -0
  41. package/build/project.mjs +615 -0
  42. package/build/reactCompiler.mjs +113 -0
  43. package/build/reactProfile.cjs +35 -0
  44. package/build/refreshRuntime.mjs +187 -0
  45. package/build/refreshSourceMap.mjs +53 -0
  46. package/build/release.mjs +99 -0
  47. package/build/resolution.d.mts +3 -0
  48. package/build/resolution.mjs +25 -0
  49. package/build/surfaces.d.mts +1 -0
  50. package/build/surfaces.mjs +42 -0
  51. package/build/testing/node.d.mts +29 -0
  52. package/build/testing/node.mjs +106 -0
  53. package/build/yoga.d.mts +8 -0
  54. package/build/yoga.mjs +186 -0
  55. package/compatibility/public-api.json +327 -0
  56. package/compatibility/registry.json +2258 -0
  57. package/config/release.json +15 -0
  58. package/config/toolchain.json +34 -0
  59. package/docs/compatibility.md +813 -0
  60. package/docs/getting-started.md +460 -0
  61. package/docs/maintenance.md +362 -0
  62. package/docs/qualification.md +241 -0
  63. package/docs/standalone-testing.md +56 -0
  64. package/examples/hello-roku/README.md +35 -0
  65. package/examples/hello-roku/package.template.json +31 -0
  66. package/examples/hello-roku/pnpm-workspace.yaml +11 -0
  67. package/examples/hello-roku/roku.config.json +22 -0
  68. package/examples/hello-roku/src/App.tsx +54 -0
  69. package/examples/hello-roku/src/Catalog.tsx +200 -0
  70. package/examples/hello-roku/src/assets.d.ts +16 -0
  71. package/examples/hello-roku/src/catalog-cover.png +0 -0
  72. package/examples/hello-roku/tsconfig.json +1 -0
  73. package/examples/hello-roku/tsconfig.react.json +1 -0
  74. package/native/components/RNRAppControl.brs +215 -0
  75. package/native/components/RNRAppControl.xml +13 -0
  76. package/native/components/RNRDeviceFacts.brs +19 -0
  77. package/native/components/RNRDeviceFacts.xml +9 -0
  78. package/native/components/RNRDeviceFactsTask.brs +74 -0
  79. package/native/components/RNRDeviceFactsTask.xml +9 -0
  80. package/native/components/RNRTextRaster.brs +15 -0
  81. package/native/components/RNRTextRaster.xml +9 -0
  82. package/native/components/RNRTextRasterTask.brs +77 -0
  83. package/native/components/RNRTextRasterTask.xml +9 -0
  84. package/native/manifest.json +7 -0
  85. package/package.json +183 -3
  86. package/src/animation/clock.ts +10 -0
  87. package/src/animation/derivedValue.ts +163 -0
  88. package/src/animation/legacyAnimated.ts +255 -0
  89. package/src/animation/reanimated.ts +243 -0
  90. package/src/animation/sharedValue.ts +285 -0
  91. package/src/animation/worklets.ts +23 -0
  92. package/src/application/AppRegistry.ts +31 -0
  93. package/src/application/mountApplication.ts +136 -0
  94. package/src/graphics/BitmapLayer.tsx +68 -0
  95. package/src/graphics/RoundedBorder.tsx +116 -0
  96. package/src/graphics/TextRasterCache.ts +259 -0
  97. package/src/graphics/gradient.ts +37 -0
  98. package/src/graphics/inlineRaster.ts +85 -0
  99. package/src/graphics/png.ts +77 -0
  100. package/src/graphics/rasterPixels.ts +89 -0
  101. package/src/graphics/surfaces.ts +105 -0
  102. package/src/graphics/textRaster.types.ts +60 -0
  103. package/src/graphics/textRasterGeometry.ts +116 -0
  104. package/src/host/host.types.ts +57 -0
  105. package/src/index.ts +22 -0
  106. package/src/interop/nativewind.ts +2 -0
  107. package/src/layout/FontResources.ts +252 -0
  108. package/src/layout/affine.ts +89 -0
  109. package/src/layout/flexLayout.ts +398 -0
  110. package/src/layout/fontFaces.d.mts +19 -0
  111. package/src/layout/fontFaces.mjs +85 -0
  112. package/src/layout/fontVariants.d.mts +2 -0
  113. package/src/layout/fontVariants.mjs +8 -0
  114. package/src/layout/inlineParagraph.ts +158 -0
  115. package/src/layout/inlineText.ts +68 -0
  116. package/src/layout/layoutReuse.ts +117 -0
  117. package/src/layout/nativeNodeFields.ts +179 -0
  118. package/src/layout/preparedFields.ts +78 -0
  119. package/src/layout/textLayout.ts +138 -0
  120. package/src/layout/transform.ts +113 -0
  121. package/src/media/SceneGraphAudio.ts +161 -0
  122. package/src/media/SceneGraphVideo.ts +167 -0
  123. package/src/media/audioSession.ts +84 -0
  124. package/src/media/index.ts +2 -0
  125. package/src/media/videoErrorDetails.ts +40 -0
  126. package/src/media/videoSession.ts +136 -0
  127. package/src/navigation/RouterStore.ts +244 -0
  128. package/src/navigation/expoRouter.ts +115 -0
  129. package/src/react-native/NativeInput.ts +494 -0
  130. package/src/react-native/NativeSystemState.ts +95 -0
  131. package/src/react-native/accessibility.ts +265 -0
  132. package/src/react-native/activityIndicator.ts +72 -0
  133. package/src/react-native/compilerPressable.ts +26 -0
  134. package/src/react-native/createNativeRoot.ts +2126 -0
  135. package/src/react-native/directionalFocus.ts +55 -0
  136. package/src/react-native/environment.ts +275 -0
  137. package/src/react-native/flatList.ts +1001 -0
  138. package/src/react-native/flatListViewability.ts +231 -0
  139. package/src/react-native/focusEvents.ts +85 -0
  140. package/src/react-native/image.ts +247 -0
  141. package/src/react-native/index.ts +54 -0
  142. package/src/react-native/interactionManager.ts +76 -0
  143. package/src/react-native/keyboard.ts +60 -0
  144. package/src/react-native/linearGradient.ts +94 -0
  145. package/src/react-native/modal.tsx +148 -0
  146. package/src/react-native/nativeHostContext.ts +80 -0
  147. package/src/react-native/nativeModules.ts +86 -0
  148. package/src/react-native/nativeRoot.types.ts +22 -0
  149. package/src/react-native/nativeViewHandle.ts +191 -0
  150. package/src/react-native/nativewindPressable.ts +38 -0
  151. package/src/react-native/panResponder.ts +73 -0
  152. package/src/react-native/primitives.ts +685 -0
  153. package/src/react-native/safeArea.ts +76 -0
  154. package/src/react-native/scrollView.ts +289 -0
  155. package/src/react-native/styleInterop.ts +8 -0
  156. package/src/react-native/styleSheet.ts +54 -0
  157. package/src/react-native/textInput.ts +334 -0
  158. package/src/react-native/unsupported.ts +25 -0
  159. package/src/renderer/SceneRoot.ts +1006 -0
  160. package/src/scene/SdkDeviceFacts.ts +151 -0
  161. package/src/scene/SdkDeviceServices.ts +72 -0
  162. package/src/scene/SdkSceneGraph.ts +124 -0
  163. package/src/scene/SdkTextEntry.ts +321 -0
  164. package/src/scene/SdkTextMetrics.ts +136 -0
  165. package/src/scene/SdkTextRasterTransport.ts +191 -0
  166. package/src/scene/SdkWebTransport.ts +122 -0
  167. package/src/scene/createRokuHost.ts +213 -0
  168. package/src/scene/sceneGraph.types.ts +38 -0
  169. package/src/services/DeviceServices.ts +53 -0
  170. package/src/services/RegistryStorage.ts +85 -0
  171. package/src/services/RokuKeySession.ts +29 -0
  172. package/src/services/asyncStorage.ts +23 -0
  173. package/src/services/base64.ts +52 -0
  174. package/src/services/crypto.ts +18 -0
  175. package/src/services/expoConstants.ts +11 -0
  176. package/src/services/expoLinking.ts +12 -0
  177. package/src/services/expoStatusBar.ts +4 -0
  178. package/src/services/installWebRuntime.ts +105 -0
  179. package/src/services/intlEnglish.ts +54 -0
  180. package/src/services/languageRuntime.ts +65 -0
  181. package/src/services/nativeTimers.ts +96 -0
  182. package/src/services/secureStore.ts +23 -0
  183. package/src/services/textEntryRequest.ts +22 -0
  184. package/src/services/utf8.ts +57 -0
  185. package/src/services/webRuntime.ts +346 -0
  186. package/src/testing/CanvasResources.ts +141 -0
  187. package/src/testing/CanvasSceneGraph.ts +496 -0
  188. package/src/testing/CanvasTextEntry.ts +237 -0
  189. package/src/testing/PreviewTextObservations.ts +82 -0
  190. package/src/testing/RecordingSceneGraph.ts +136 -0
  191. package/src/types/normalize-colors.d.ts +4 -0
  192. package/src/vector/dynamicSvg.tsx +226 -0
  193. package/src/vector/svg.ts +222 -0
  194. package/src/vector/svg.types.ts +4 -0
  195. package/src/vector/svgElements.ts +58 -0
  196. package/src/vector/svgKey.d.mts +4 -0
  197. package/src/vector/svgKey.mjs +16 -0
  198. package/src/vector/svgPath.ts +157 -0
  199. package/src/vector/svgPixels.ts +133 -0
  200. package/tsconfig.consumer.json +29 -0
@@ -0,0 +1,397 @@
1
+ import ts from "typescript";
2
+ import { createHash } from "node:crypto";
3
+ import { mkdirSync, writeFileSync } from "node:fs";
4
+ import { basename, dirname, relative, resolve } from "node:path";
5
+ import { GenMapping, addMapping, setSourceContent, toEncodedMap } from "@jridgewell/gen-mapping";
6
+ import { TraceMap, eachMapping } from "@jridgewell/trace-mapping";
7
+ import { rokuBuildProfile } from "./environment.mjs";
8
+ import { Script } from "node:vm";
9
+
10
+ /** Preserve Metro's module factories/require cache while bounding SDK inputs.
11
+ * Unknown serializer shapes fail before writing, rather than moving executable
12
+ * statements across lexical or lifetime boundaries. */
13
+ export function writeMetroModules({
14
+ code,
15
+ map,
16
+ mapUrl,
17
+ outfile,
18
+ exports: names = ["mountApplication"],
19
+ entryModuleId,
20
+ profile,
21
+ metadata,
22
+ maxModuleBytes = 2 * 1024 * 1024,
23
+ refresh,
24
+ refreshMap,
25
+ }) {
26
+ if (!Number.isSafeInteger(maxModuleBytes) || maxModuleBytes <= 0)
27
+ throw Error("Invalid Metro module budget");
28
+ const reserved = new Set([
29
+ "default",
30
+ "eval",
31
+ "arguments",
32
+ "__r",
33
+ "class",
34
+ "const",
35
+ "let",
36
+ "var",
37
+ "function",
38
+ "import",
39
+ "export",
40
+ "return",
41
+ "await",
42
+ "yield",
43
+ "delete",
44
+ "new",
45
+ "this",
46
+ "super",
47
+ "null",
48
+ "true",
49
+ "false",
50
+ "if",
51
+ "else",
52
+ "for",
53
+ "while",
54
+ "switch",
55
+ "case",
56
+ "throw",
57
+ "try",
58
+ "catch",
59
+ "finally",
60
+ "break",
61
+ "continue",
62
+ "do",
63
+ "in",
64
+ "instanceof",
65
+ "typeof",
66
+ "void",
67
+ "with",
68
+ "debugger",
69
+ "extends",
70
+ "enum",
71
+ "implements",
72
+ "interface",
73
+ "package",
74
+ "private",
75
+ "protected",
76
+ "public",
77
+ "static",
78
+ ]);
79
+ if (
80
+ !Array.isArray(names) ||
81
+ !names.length ||
82
+ new Set(names).size !== names.length ||
83
+ names.some(
84
+ (name) =>
85
+ typeof name !== "string" ||
86
+ !/^[A-Za-z_$][\w$]*$/.test(name) ||
87
+ reserved.has(name) ||
88
+ name.startsWith("__rnr"),
89
+ )
90
+ )
91
+ throw Error("Metro frontend exports need distinct supported identifiers");
92
+ // TypeScript's syntax parser accepts ESM declarations inside functions.
93
+ // Metro factories must already be executable scripts. Parse without running
94
+ // any application code before creating a generation or its output files.
95
+ try {
96
+ new Script(code, { filename: "metro.bundle.js" });
97
+ } catch (cause) {
98
+ throw Error("Review invalid Metro bundle serialization before partitioning", { cause });
99
+ }
100
+ const source = ts.createSourceFile(
101
+ "metro.js",
102
+ code,
103
+ ts.ScriptTarget.Latest,
104
+ true,
105
+ ts.ScriptKind.JS,
106
+ );
107
+ const refuse = () => {
108
+ throw Error("Review changed Metro bundle serialization before partitioning");
109
+ };
110
+ if (source.parseDiagnostics.length) refuse();
111
+ const call = (node, name) =>
112
+ ts.isExpressionStatement(node) &&
113
+ ts.isCallExpression(node.expression) &&
114
+ ts.isIdentifier(node.expression.expression) &&
115
+ node.expression.expression.text === name;
116
+ const statements = [...source.statements];
117
+ const first = statements.findIndex((node) => call(node, "__d"));
118
+ if (first < 1) refuse();
119
+ const footerIndex = statements.findIndex((node, index) => index > first && !call(node, "__d"));
120
+ if (
121
+ footerIndex < 0 ||
122
+ statements
123
+ .slice(footerIndex)
124
+ .some((node) => !call(node, "__r") || node.expression.arguments.length !== 1)
125
+ )
126
+ refuse();
127
+ const definitions = statements.slice(first, footerIndex);
128
+ for (const node of definitions)
129
+ if (
130
+ node.expression.arguments.length < 3 ||
131
+ !ts.isFunctionExpression(node.expression.arguments[0])
132
+ )
133
+ refuse();
134
+ const footer = statements.slice(footerIndex);
135
+ const id = footer.at(-1).expression.arguments[0];
136
+ if (!ts.isNumericLiteral(id) && !ts.isStringLiteral(id)) refuse();
137
+ const actualId = ts.isNumericLiteral(id) ? Number(id.text) : id.text;
138
+ if (entryModuleId !== undefined && actualId !== entryModuleId)
139
+ throw Error("Metro entry module differs from the requested entry");
140
+ const bindings = [];
141
+ for (const node of statements.slice(0, first)) {
142
+ if (ts.isVariableStatement(node)) {
143
+ for (const declaration of node.declarationList.declarations) {
144
+ if (!ts.isIdentifier(declaration.name)) refuse();
145
+ if (
146
+ profile !== undefined &&
147
+ declaration.name.text === "process" &&
148
+ node.declarationList.flags & ts.NodeFlags.Const
149
+ )
150
+ refuse();
151
+ bindings.push(declaration.name.text);
152
+ }
153
+ } else if (ts.isFunctionDeclaration(node) && node.name) bindings.push(node.name.text);
154
+ else if (!ts.isExpressionStatement(node)) refuse();
155
+ }
156
+ if (
157
+ bindings.some((name) => name === "__d" || name === "__r" || reserved.has(name)) ||
158
+ new Set(bindings).size !== bindings.length
159
+ )
160
+ refuse();
161
+ const compilation = profile === undefined ? null : rokuBuildProfile(profile);
162
+ if (compilation && !bindings.includes("process")) refuse();
163
+ const key = createHash("sha256")
164
+ .update(code)
165
+ .update(JSON.stringify(map ?? null))
166
+ .update(JSON.stringify(names))
167
+ .update(JSON.stringify(compilation?.name ?? null))
168
+ .update(String(maxModuleBytes))
169
+ .update(JSON.stringify(refresh ?? null))
170
+ .digest("hex")
171
+ .slice(0, 20);
172
+ const directory = resolve(dirname(outfile), "metro-modules", key);
173
+ const output = [];
174
+ const descriptions = new Map();
175
+ const trace = map ? new TraceMap(map, mapUrl) : null;
176
+ const mappings = [];
177
+ if (trace) eachMapping(trace, (mapping) => mappings.push(mapping));
178
+ const emit = (path, start, end, prefix = "", suffix = "") => {
179
+ const content = prefix + code.slice(start, end) + suffix;
180
+ const mapName = basename(path) + ".map";
181
+ const contents = content + (trace ? `\n//# sourceMappingURL=${mapName}\n` : "\n");
182
+ if (Buffer.byteLength(contents) > maxModuleBytes)
183
+ throw Error(
184
+ `Metro module budget exceeded: ${basename(path)} (${Buffer.byteLength(contents)} bytes)`,
185
+ );
186
+ output.push({ path, contents });
187
+ if (trace) {
188
+ const generated = new GenMapping({ file: basename(path) });
189
+ const first = source.getLineAndCharacterOfPosition(start),
190
+ last = source.getLineAndCharacterOfPosition(end);
191
+ const prefixLines = prefix.split("\n");
192
+ const used = new Set();
193
+ for (const mapping of mappings) {
194
+ const line = mapping.generatedLine - 1,
195
+ column = mapping.generatedColumn;
196
+ if (
197
+ line < first.line ||
198
+ line > last.line ||
199
+ (line === first.line && column < first.character) ||
200
+ (line === last.line && column >= last.character)
201
+ )
202
+ continue;
203
+ const relativeLine = line - first.line;
204
+ const point = {
205
+ line: relativeLine + prefixLines.length,
206
+ column:
207
+ column -
208
+ (relativeLine === 0 ? first.character : 0) +
209
+ (relativeLine === 0 ? prefixLines.at(-1).length : 0),
210
+ };
211
+ if (mapping.source === null) addMapping(generated, { generated: point });
212
+ else {
213
+ used.add(mapping.source);
214
+ addMapping(generated, {
215
+ generated: point,
216
+ original: { line: mapping.originalLine, column: mapping.originalColumn },
217
+ source: mapping.source,
218
+ ...(mapping.name === null ? {} : { name: mapping.name }),
219
+ });
220
+ }
221
+ }
222
+ for (const name of used) {
223
+ const index = trace.resolvedSources.indexOf(name);
224
+ if (trace.sourcesContent?.[index] != null)
225
+ setSourceContent(generated, name, trace.sourcesContent[index]);
226
+ }
227
+ output.push({ path: path + ".map", contents: JSON.stringify(toEncodedMap(generated)) });
228
+ }
229
+ };
230
+ const runtime = resolve(directory, "runtime.mjs");
231
+ if (metadata)
232
+ descriptions.set(runtime, {
233
+ inputs: metadata.scriptInputs(code.slice(0, statements[first].pos)),
234
+ exports: [...bindings, "__d", "__r"],
235
+ imports: [],
236
+ });
237
+ emit(
238
+ runtime,
239
+ 0,
240
+ statements[first].pos,
241
+ "",
242
+ `\n${compilation ? `process=Object.assign({},process,{env:Object.assign({},process.env,{NODE_ENV:${JSON.stringify(compilation.name)}})});\n` : ""}const __d=globalThis.__d,__r=globalThis.__r;\n${refresh ? `globalThis.__RNR_ROKU_REFRESH__.attachRuntime(${JSON.stringify(refresh)},{${[...bindings, "__d", "__r"].join(",")}});\n` : ""}export {${[...bindings, "__d", "__r"].join(",")}};\n`,
243
+ );
244
+ const prefix = `import {${[...bindings, "__d"].join(",")}} from './runtime.mjs';\n`;
245
+ const describeModule = (path, start, end) => {
246
+ if (!metadata) return;
247
+ const inputs = {};
248
+ for (const node of definitions.filter((node) => node.pos >= start && node.end <= end)) {
249
+ const id = node.expression.arguments[1];
250
+ if (!ts.isNumericLiteral(id) && !ts.isStringLiteral(id)) refuse();
251
+ const input = metadata.moduleInput(
252
+ ts.isNumericLiteral(id) ? Number(id.text) : id.text,
253
+ Buffer.byteLength(code.slice(node.pos, node.end)),
254
+ );
255
+ if (input)
256
+ inputs[input.path] = {
257
+ bytesInOutput: (inputs[input.path]?.bytesInOutput ?? 0) + input.bytesInOutput,
258
+ };
259
+ }
260
+ descriptions.set(path, {
261
+ inputs,
262
+ imports: [{ path: metadata.outputName(runtime), kind: "import-statement" }],
263
+ exports: [],
264
+ });
265
+ };
266
+ const modules = [];
267
+ let start = definitions[0].pos,
268
+ end = start,
269
+ bytes = Buffer.byteLength(prefix) + 128;
270
+ for (const node of definitions) {
271
+ const size = Buffer.byteLength(code.slice(node.pos, node.end));
272
+ if (bytes + size > maxModuleBytes && end > start) {
273
+ const path = resolve(directory, `module-${modules.length}.mjs`);
274
+ emit(path, start, end, prefix);
275
+ describeModule(path, start, end);
276
+ modules.push(path);
277
+ start = node.pos;
278
+ bytes = Buffer.byteLength(prefix) + 128;
279
+ }
280
+ bytes += size;
281
+ end = node.end;
282
+ }
283
+ const final = resolve(directory, `module-${modules.length}.mjs`);
284
+ emit(final, start, end, prefix);
285
+ describeModule(final, start, end);
286
+ modules.push(final);
287
+ const imports = modules
288
+ .map(
289
+ (path) =>
290
+ `import ${JSON.stringify("./" + relative(dirname(outfile), path).split("\\").join("/"))};`,
291
+ )
292
+ .join("\n");
293
+ const runtimeImport = `import {__r} from ${JSON.stringify("./" + relative(dirname(outfile), runtime).split("\\").join("/"))};\n`;
294
+ const bootstrap =
295
+ imports +
296
+ "\n" +
297
+ runtimeImport +
298
+ footer
299
+ .slice(0, -1)
300
+ .map((node) => node.getText(source))
301
+ .join("\n") +
302
+ `\nconst __rnrMetroEntry=__r(${id.getText(source)});\n` +
303
+ names
304
+ .map((name) => `const ${name}=__rnrMetroEntry[${JSON.stringify(name)}];export {${name}};`)
305
+ .join("\n") +
306
+ "\n";
307
+ if (Buffer.byteLength(bootstrap) > maxModuleBytes)
308
+ throw Error("Metro entry exceeds module budget");
309
+ output.push({ path: resolve(outfile), contents: bootstrap });
310
+ if (metadata)
311
+ descriptions.set(resolve(outfile), {
312
+ inputs: {},
313
+ imports: [...modules, runtime].map((path) => ({
314
+ path: metadata.outputName(path),
315
+ kind: "import-statement",
316
+ })),
317
+ exports: names,
318
+ });
319
+ const metafile = metadata?.metafile(
320
+ Object.fromEntries(
321
+ output.map((file) => [
322
+ metadata.outputName(file.path),
323
+ {
324
+ bytes: Buffer.byteLength(file.contents),
325
+ inputs: {},
326
+ imports: [],
327
+ exports: [],
328
+ ...descriptions.get(file.path),
329
+ },
330
+ ]),
331
+ ),
332
+ );
333
+ let refreshGraph;
334
+ if (refresh) {
335
+ if (
336
+ compilation?.name !== "development" ||
337
+ !metadata?.moduleRefresh ||
338
+ bindings.some((name) => name.startsWith("__rnr"))
339
+ )
340
+ throw Error("Roku refresh requires an attested development Metro graph");
341
+ const literal = (node) => {
342
+ if (ts.isNumericLiteral(node)) return Number(node.text);
343
+ if (ts.isStringLiteral(node)) return node.text;
344
+ throw Error("Review the Metro refresh dependency map");
345
+ };
346
+ refreshGraph = {
347
+ ...refresh,
348
+ bindings: [...bindings, "__d", "__r"],
349
+ maxModuleBytes,
350
+ ...(refreshMap ? { sourceMap: refreshMap } : {}),
351
+ runtimeSha256: createHash("sha256")
352
+ .update(code.slice(0, statements[first].pos))
353
+ .digest("hex"),
354
+ bootstrapSha256: createHash("sha256")
355
+ .update(JSON.stringify([actualId, names, footer.map((node) => node.getText(source))]))
356
+ .digest("hex"),
357
+ modules: definitions.map((node) => {
358
+ const args = node.expression.arguments;
359
+ if (args.length > 4 || !ts.isArrayLiteralExpression(args[2]))
360
+ throw Error("Review the Metro refresh serialization");
361
+ const id = literal(args[1]);
362
+ const raw = node.getText(source);
363
+ const start = source.getLineAndCharacterOfPosition(node.getStart(source)),
364
+ end = source.getLineAndCharacterOfPosition(node.end);
365
+ return {
366
+ id,
367
+ ...metadata.moduleRefresh(id),
368
+ dependencies: args[2].elements.map(literal),
369
+ code: args.length === 3 ? raw.replace(/\);\s*$/, ",undefined);") : raw,
370
+ span: {
371
+ startLine: start.line,
372
+ startColumn: start.character,
373
+ endLine: end.line,
374
+ endColumn: end.character,
375
+ },
376
+ sha256: createHash("sha256").update(raw).digest("hex"),
377
+ };
378
+ }),
379
+ };
380
+ }
381
+ for (const file of output) {
382
+ mkdirSync(dirname(file.path), { recursive: true });
383
+ writeFileSync(file.path, file.contents);
384
+ }
385
+ return {
386
+ outfile: resolve(outfile),
387
+ modules: definitions.length,
388
+ entryModuleId: actualId,
389
+ ...(refreshGraph ? { refreshGraph } : {}),
390
+ ...(metafile ? { metafile } : {}),
391
+ files: output.map((file) => ({
392
+ path: file.path,
393
+ bytes: Buffer.byteLength(file.contents),
394
+ sha256: createHash("sha256").update(file.contents).digest("hex"),
395
+ })),
396
+ };
397
+ }
@@ -0,0 +1,64 @@
1
+ export type RokuRefreshPlan =
2
+ | { type: "reload"; reason: string; sessionId: string; generation: number }
3
+ | {
4
+ type: "refresh";
5
+ sessionId: string;
6
+ fromGeneration: number;
7
+ generation: number;
8
+ updateId: string;
9
+ paths: string[];
10
+ moduleIds: Array<number | string>;
11
+ changed: string[];
12
+ };
13
+ export interface RokuMetroRefreshGraph {
14
+ sessionId: string;
15
+ generation: number;
16
+ runtimeSha256: string;
17
+ bootstrapSha256: string;
18
+ bindings: string[];
19
+ maxModuleBytes: number;
20
+ sourceMap?: { path: string; sha256: string; url: string };
21
+ modules: Array<{
22
+ id: string | number;
23
+ source: string;
24
+ safe: boolean;
25
+ dependencies: Array<string | number>;
26
+ code: string;
27
+ span: { startLine: number; startColumn: number; endLine: number; endColumn: number };
28
+ sha256: string;
29
+ }>;
30
+ }
31
+ export interface RokuMetroRefreshSession {
32
+ readonly id: string;
33
+ readonly directory: string;
34
+ createModuleIdFactory(
35
+ original: () => (path: string) => number | string,
36
+ ): () => (path: string) => number | string;
37
+ classify(path: string, source: string | null): boolean;
38
+ stage(options: {
39
+ generation: number;
40
+ graph: RokuMetroRefreshGraph;
41
+ identity: string;
42
+ outputDirectory: string;
43
+ }): RokuRefreshPlan;
44
+ plan(generation: number): RokuRefreshPlan | null;
45
+ discard(generation: number): void;
46
+ publish(generation: number): void;
47
+ dispose(): void;
48
+ }
49
+ export interface RokuMetroRefreshContext {
50
+ session: RokuMetroRefreshSession;
51
+ generation: number;
52
+ }
53
+ export declare function createRokuMetroRefreshSession(options: {
54
+ root?: string;
55
+ directory: string;
56
+ sourceRoots?: string[];
57
+ }): RokuMetroRefreshSession;
58
+ export declare function isRefreshSafeSource(path: string, contents: string): boolean;
59
+ export declare function writeRokuRefreshBootstrap(options: {
60
+ root?: string;
61
+ session: RokuMetroRefreshSession;
62
+ generation: number;
63
+ outfile: string;
64
+ }): string;