@swf2es/web 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.
Files changed (58) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +43 -2
  3. package/dist/air-files.d.ts +40 -0
  4. package/dist/air-files.d.ts.map +1 -0
  5. package/dist/air-files.js +304 -0
  6. package/dist/air-files.js.map +1 -0
  7. package/dist/air-items.d.ts +28 -0
  8. package/dist/air-items.d.ts.map +1 -0
  9. package/dist/air-items.js +186 -0
  10. package/dist/air-items.js.map +1 -0
  11. package/dist/air.d.ts +81 -0
  12. package/dist/air.d.ts.map +1 -0
  13. package/dist/air.js +409 -0
  14. package/dist/air.js.map +1 -0
  15. package/dist/config.d.ts +123 -0
  16. package/dist/config.d.ts.map +1 -0
  17. package/dist/config.js +127 -0
  18. package/dist/config.js.map +1 -0
  19. package/dist/element.d.ts +80 -0
  20. package/dist/element.d.ts.map +1 -0
  21. package/dist/element.js +725 -0
  22. package/dist/element.js.map +1 -0
  23. package/dist/external.d.ts +49 -0
  24. package/dist/external.d.ts.map +1 -0
  25. package/dist/external.js +268 -0
  26. package/dist/external.js.map +1 -0
  27. package/dist/index.d.ts +9 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +18 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/layout.d.ts +47 -0
  32. package/dist/layout.d.ts.map +1 -0
  33. package/dist/layout.js +75 -0
  34. package/dist/layout.js.map +1 -0
  35. package/dist/playback.d.ts +107 -0
  36. package/dist/playback.d.ts.map +1 -0
  37. package/dist/playback.js +308 -0
  38. package/dist/playback.js.map +1 -0
  39. package/dist/replace.d.ts +22 -0
  40. package/dist/replace.d.ts.map +1 -0
  41. package/dist/replace.js +141 -0
  42. package/dist/replace.js.map +1 -0
  43. package/dist/stage3d.d.ts +25 -0
  44. package/dist/stage3d.d.ts.map +1 -0
  45. package/dist/stage3d.js +90 -0
  46. package/dist/stage3d.js.map +1 -0
  47. package/package.json +38 -3
  48. package/src/air-files.ts +371 -0
  49. package/src/air-items.ts +217 -0
  50. package/src/air.ts +497 -0
  51. package/src/config.ts +244 -0
  52. package/src/element.ts +902 -0
  53. package/src/external.ts +338 -0
  54. package/src/index.ts +33 -0
  55. package/src/layout.ts +127 -0
  56. package/src/playback.ts +388 -0
  57. package/src/replace.ts +166 -0
  58. package/src/stage3d.ts +112 -0
package/src/config.ts ADDED
@@ -0,0 +1,244 @@
1
+ // What every player on a page shares, set once: where the compiler and
2
+ // the libraries come from, how sockets reach their servers, whether
3
+ // compiled code is cached. Each is fetched once a page, on first use.
4
+ import { type Codegen, createCodegen } from "@swf2es/codegen";
5
+ import {
6
+ type AirApplication,
7
+ airLibrary,
8
+ builtinLibrary,
9
+ type EncryptedStoreHost,
10
+ type FetchRequest,
11
+ type FetchResult,
12
+ type FileHost,
13
+ type FileSystemHost,
14
+ type ModuleCache,
15
+ playerglobalLibrary,
16
+ type SocketHost,
17
+ } from "@swf2es/player";
18
+ import { indexedDbModuleCache } from "@swf2es/player-hosts/indexeddb";
19
+ import { webSocketSocketHost } from "@swf2es/player-hosts/websocket";
20
+ import type { WebWorkerHost } from "@swf2es/player-hosts/workers";
21
+ import type { Stage3DSurfaceFactory } from "./stage3d.js";
22
+
23
+ /** A flash.net.Socket's host and port, and the WebSocket relay that reaches it, as Ruffle's socketProxy lists them. */
24
+ export interface SocketProxy {
25
+ host: string;
26
+ port: number;
27
+ proxyUrl: string;
28
+ }
29
+
30
+ export interface Configuration {
31
+ /**
32
+ * The ABCs a SWF's ActionScript 3 links against, by URL, each in place
33
+ * of the one swf2es ships: avmplus' builtin.abc (builtinLibrary) and a
34
+ * playerglobal (playerglobalLibrary). Either left out is the shipped one.
35
+ */
36
+ libraries?: { builtin?: string; playerglobal?: string };
37
+ /** codegen.wasm's URL: by default what the page's import map gives `@swf2es/codegen/codegen.wasm`. */
38
+ codegen?: string;
39
+ /**
40
+ * Where flash.net.Socket connects: a function from the host and port it
41
+ * asks for to a WebSocket relay's URL, null for none, or a list. A
42
+ * socket with no relay fails as one refused (IOErrorEvent #2031).
43
+ */
44
+ socketProxy?: ((host: string, port: number) => string | null) | SocketProxy[];
45
+ /**
46
+ * A host of the embedder's own for flash.net.Socket, in place of
47
+ * socketProxy's relays: a desktop app's, which reaches TCP itself.
48
+ */
49
+ sockets?: SocketHost;
50
+ fileHost?: FileHost | null;
51
+ /**
52
+ * A fetch of the embedder's own for what the SWFs load, the SWF the
53
+ * element plays among it (its purpose "movie"), in place of the
54
+ * browser's: a desktop app's, which judges each request by Flash's
55
+ * sandboxes and policy files rather than CORS.
56
+ */
57
+ fetch?: (request: FetchRequest, signal: AbortSignal) => Promise<FetchResult>;
58
+ /** Takes the policy files a SWF names with Security.loadPolicyFile, for such a fetch. */
59
+ loadPolicyFile?: (url: string) => void;
60
+ /** Security.sandboxType of the SWFs the element plays next: "remote" by default. */
61
+ sandboxType?: string;
62
+ /**
63
+ * Whether the modules the compiler writes are kept in IndexedDB for the
64
+ * page's next visit, which then links them without compiling: off by
65
+ * default, as a first visit is some 9% slower for the writes.
66
+ */
67
+ cache?: boolean;
68
+ /** Experimental AVM2 compilation on first method-factory use; off by default. */
69
+ lazyCompilation?: boolean;
70
+ /** Experimental Stage3D surface factory; absent/null keeps ErrorEvent #3702. */
71
+ stage3D?: Stage3DSurfaceFactory | null;
72
+ /**
73
+ * Whether flash.system.Worker runs a SWF's background workers, each on
74
+ * a Web Worker: on by default, as in Flash; an element's `workers`
75
+ * attribute decides for its own SWFs. Off, Worker.isSupported is false.
76
+ * A shareable ByteArray, a Mutex or a Condition crosses to another
77
+ * worker only on a cross-origin isolated page (docs/workers.md).
78
+ */
79
+ workers?: boolean;
80
+ /**
81
+ * The Web Workers' script: by default @swf2es/player-hosts'
82
+ * dist/thread/player-hosts/web-worker.js beside the module the page
83
+ * loaded @swf2es/player-hosts/workers from, which resolves its imports
84
+ * without the page's import map. One of another origin, as a CDN's,
85
+ * starts through a `blob:` module that imports it.
86
+ */
87
+ workerUrl?: string | URL;
88
+ /** The page's build, a short commit or a version, which the context menu's About item names. */
89
+ build?: string;
90
+ /**
91
+ * Play the SWFs the element plays next as an AIR application's, as a
92
+ * desktop app runs one: AIR's library loaded after playerglobal, its API
93
+ * and its application sandbox, with the application the host runs and
94
+ * the file system its File reads, and where its EncryptedLocalStore
95
+ * keeps its items, in memory if not given. Null or absent: as Flash Player's
96
+ * plugin, AIR's API not defined.
97
+ */
98
+ air?: {
99
+ application: AirApplication;
100
+ fileSystem?: FileSystemHost | null;
101
+ encryptedStore?: EncryptedStoreHost | null;
102
+ } | null;
103
+ }
104
+
105
+ let config: Configuration = {};
106
+ let codegenModule: Promise<WebAssembly.Module> | null = null;
107
+ let libraryBytes: Promise<Uint8Array[]> | null = null;
108
+ let moduleCache: ModuleCache | null = null;
109
+
110
+ /**
111
+ * Set the page's configuration, once, before the first player loads: what
112
+ * is fetched is fetched once with the configuration of then. A second call
113
+ * merges into the first, for a page whose parts each set their own; what
114
+ * was already fetched stays.
115
+ */
116
+ export function configure(options: Configuration): void {
117
+ config = { ...config, ...options };
118
+ }
119
+
120
+ /** The configuration in force. */
121
+ export function configuration(): Readonly<Configuration> {
122
+ return config;
123
+ }
124
+
125
+ /**
126
+ * A compiler for one player. The page fetches and compiles codegen.wasm
127
+ * once and every player instantiates that module: a Codegen holds one
128
+ * player's domains, which a second player's reset would take over.
129
+ */
130
+ export async function codegenForPlayer(): Promise<Codegen> {
131
+ return createCodegen(await compiledCodegen());
132
+ }
133
+
134
+ /** codegen.wasm compiled, once a page. */
135
+ async function compiledCodegen(): Promise<WebAssembly.Module> {
136
+ codegenModule ??= (async () => {
137
+ const url = config.codegen ?? import.meta.resolve("@swf2es/codegen/codegen.wasm");
138
+ const response = await fetch(url);
139
+ if (!response.ok) {
140
+ throw new Error(`swf2es: codegen.wasm: ${response.status} from ${url}`);
141
+ }
142
+
143
+ return WebAssembly.compileStreaming(response);
144
+ })();
145
+ // A failed fetch is not kept: the next player tries again.
146
+ return codegenModule.catch((error) => {
147
+ codegenModule = null;
148
+ throw error;
149
+ });
150
+ }
151
+
152
+ /**
153
+ * The Web Workers of one player, whose release (as the player goes) lets
154
+ * the page's workers started ahead go once no player uses them. `ahead`
155
+ * starts some ahead, for a SWF that may start a worker and wait for it at
156
+ * once, as Crossbridge's do; others start one when the SWF asks.
157
+ */
158
+ export async function workerHostForPlayer(ahead: boolean): Promise<WebWorkerHost> {
159
+ // Loaded only where a SWF has workers: a page's import map that names no
160
+ // @swf2es/player-hosts/workers still loads the element.
161
+ const { webWorkerHost } = await import("@swf2es/player-hosts/workers");
162
+ return webWorkerHost({
163
+ codegen: await compiledCodegen(),
164
+ url: config.workerUrl,
165
+ spares: ahead ? undefined : 0,
166
+ });
167
+ }
168
+
169
+ /** The libraries a player loads: builtin and playerglobal, and AIR's after them for an AIR application (`air`). */
170
+ export async function playerLibraries(air: boolean): Promise<Uint8Array[]> {
171
+ const abcs = await libraries();
172
+ return air ? [...abcs, airLibrary] : abcs;
173
+ }
174
+
175
+ /**
176
+ * builtin.abc and playerglobal: the shipped ones, or those configured,
177
+ * fetched once a page; every player reads the same bytes.
178
+ */
179
+ export function libraries(): Promise<Uint8Array[]> {
180
+ const urls = config.libraries ?? {};
181
+ libraryBytes ??= Promise.all(
182
+ [
183
+ [urls.builtin, builtinLibrary] as const,
184
+ [urls.playerglobal, playerglobalLibrary] as const,
185
+ ].map(async ([url, shipped]) => {
186
+ if (url === undefined) {
187
+ return shipped;
188
+ }
189
+
190
+ const response = await fetch(url);
191
+ // A missing one would reach codegen as an empty ABC, refused as if the SWF's.
192
+ if (!response.ok) {
193
+ throw new Error(`swf2es: ${response.status} for the library at ${url}`);
194
+ }
195
+
196
+ return new Uint8Array(await response.arrayBuffer());
197
+ }),
198
+ ).catch((error) => {
199
+ libraryBytes = null;
200
+ throw error;
201
+ });
202
+
203
+ return libraryBytes;
204
+ }
205
+
206
+ /** flash.net.Socket's host: the embedder's, through the configured relays, or none. */
207
+ export function socketHost(): SocketHost | undefined {
208
+ if (config.sockets) {
209
+ return config.sockets;
210
+ }
211
+
212
+ const proxy = config.socketProxy;
213
+ if (!proxy) {
214
+ return undefined;
215
+ }
216
+
217
+ const urlFor =
218
+ typeof proxy === "function"
219
+ ? proxy
220
+ : (host: string, port: number) =>
221
+ proxy.find((p) => p.host === host && p.port === port)?.proxyUrl ?? null;
222
+ const relay = webSocketSocketHost((host, port) => urlFor(host, port) ?? "");
223
+ return {
224
+ connect(host, port, events) {
225
+ if (urlFor(host, port) === null) {
226
+ // Refused, as Flash reports a connection no server took.
227
+ queueMicrotask(() => events.error("Error #2031: Socket Error."));
228
+ return { send() {}, close() {} };
229
+ }
230
+
231
+ return relay.connect(host, port, events);
232
+ },
233
+ };
234
+ }
235
+
236
+ /** What Scripting is given for `cache`: one IndexedDB module cache, shared by every player on the page. */
237
+ export function moduleCacheOptions(): { moduleCache?: ModuleCache } {
238
+ if (!config.cache || typeof indexedDB === "undefined") {
239
+ return {};
240
+ }
241
+
242
+ moduleCache ??= indexedDbModuleCache();
243
+ return { moduleCache };
244
+ }