@swf2es/web 0.1.19 → 0.1.21

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.
@@ -0,0 +1,87 @@
1
+ // The fetches of an element's load: the SWF itself, and what its SWFs load,
2
+ // through an AIR application's files, the embedder's fetch or the browser's.
3
+ import type { FetchRequest, FetchResult } from "@swf2es/player";
4
+ import { configuration } from "./config.js";
5
+ import type { Session } from "./element.js";
6
+
7
+ /** The SWF at `url`, by the embedder's fetch where it gave one, and the URL a redirect took it to. */
8
+ export async function fetchMovie(
9
+ url: string,
10
+ signal: AbortSignal,
11
+ ): Promise<{ bytes: Uint8Array; url: string }> {
12
+ const fetchOf = configuration().fetch;
13
+ if (fetchOf) {
14
+ const result = await fetchOf(
15
+ { url, method: "GET", headers: [], body: null, purpose: "movie" },
16
+ signal,
17
+ );
18
+ if (!result.bytes) {
19
+ throw new Error(`swf2es: ${result.status} for ${url}`);
20
+ }
21
+
22
+ return { bytes: result.bytes, url: result.url ?? url };
23
+ }
24
+
25
+ const response = await fetch(url, { signal });
26
+ if (!response.ok) {
27
+ throw new Error(`swf2es: ${response.status} for ${url}`);
28
+ }
29
+
30
+ return { bytes: new Uint8Array(await response.arrayBuffer()), url: response.url || url };
31
+ }
32
+
33
+ /**
34
+ * What a session's SWFs load by: its AIR application's files, then the
35
+ * embedder's fetch or the browser's; `offline` (allowNetworking "none"),
36
+ * nothing past the application's own files, which are no network.
37
+ */
38
+ export function fetchFor(
39
+ session: Session,
40
+ offline: boolean,
41
+ ): (request: FetchRequest, signal: AbortSignal) => Promise<FetchResult> {
42
+ const fetchOf: (request: FetchRequest, signal: AbortSignal) => Promise<FetchResult> = offline
43
+ ? () => Promise.reject(new Error("allowNetworking is none"))
44
+ : (configuration().fetch ?? browserFetch);
45
+ const app = session.air?.app;
46
+ if (!app) {
47
+ return fetchOf;
48
+ }
49
+
50
+ return (request, signal) => {
51
+ const local = app.fetch(request, session.air?.files);
52
+ if (!local) {
53
+ return fetchOf(request, signal);
54
+ }
55
+
56
+ // A file the application does not hold, from the directory it came from: one the server
57
+ // has none of is a file that is not there, not its error's body.
58
+ const remote = local.bytes ? null : app.remote(request.url);
59
+ return remote
60
+ ? fetchOf({ ...request, url: remote, errorBody: false }, signal)
61
+ : Promise.resolve(local);
62
+ };
63
+ }
64
+
65
+ /** The host's fetch for what a SWF loads: the browser's, its status and headers as they come. */
66
+ async function browserFetch(request: FetchRequest, signal: AbortSignal): Promise<FetchResult> {
67
+ const response = await fetch(request.url, {
68
+ signal,
69
+ method: request.method,
70
+ headers: request.headers.map(([name, value]) => [name, value]),
71
+ body: request.body ? new Uint8Array(request.body).buffer : null,
72
+ });
73
+ const headers: [string, string][] = [];
74
+ response.headers.forEach((value, name) => {
75
+ headers.push([name, value]);
76
+ });
77
+
78
+ // An error's body only where it is asked for, as an AIR application's URLLoader reads it.
79
+ const read = response.ok || (!!request.errorBody && response.status >= 400);
80
+ return {
81
+ bytes: read ? new Uint8Array(await response.arrayBuffer()) : null,
82
+ status: response.status,
83
+ headers,
84
+ // Where a redirect took it: the SWF is judged and named by that.
85
+ ...(response.redirected ? { url: response.url } : {}),
86
+ };
87
+ }
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ import { defineElement } from "./element.js";
10
10
  // SWFs out for.
11
11
  export { filtersGreyOutFiles } from "@swf2es/player";
12
12
  export { AirApp, type AirAppFile, openAirApp } from "./air.js";
13
+ export { parseColor, parseFlashVars, scriptAccess } from "./attributes.js";
13
14
  export {
14
15
  type Configuration,
15
16
  configuration,
@@ -17,18 +18,10 @@ export {
17
18
  type SocketProxy,
18
19
  type WindowFullScreen,
19
20
  } from "./config.js";
20
- export {
21
- defineElement,
22
- parseColor,
23
- parseFlashVars,
24
- Swf2esPlayerElement,
25
- type SwfSource,
26
- scriptAccess,
27
- TAG,
28
- } from "./element.js";
21
+ export { defineElement, Swf2esPlayerElement, type SwfSource, TAG } from "./element.js";
29
22
  export { ExternalInterfaceError, type PageValue } from "./external.js";
30
23
  export { type Placement, place, type ScaleMode, scaleMode } from "./layout.js";
31
24
  export { flashAttributes, isFlash, replaceFlash, watchFlash } from "./replace.js";
32
- export type { Stage3DSurfaceFactory } from "./stage3d.js";
25
+ export type { Stage3DSurfaceFactory, Stage3DSurfaceLoader } from "./stage3d.js";
33
26
 
34
27
  defineElement();
package/src/stage3d.ts CHANGED
@@ -6,6 +6,11 @@ import type { Placement } from "./layout.js";
6
6
  /** A fresh, exclusively owned surface for each context requested by a player. */
7
7
  export type Stage3DSurfaceFactory = (slot: number) => Stage3DSurface;
8
8
 
9
+ /** A factory loaded when a SWF first requests a context, as a GPU backend imported then. */
10
+ export interface Stage3DSurfaceLoader {
11
+ load(): Promise<Stage3DSurfaceFactory>;
12
+ }
13
+
9
14
  /** One load's Stage3D layers, below its transparent display-list canvas. */
10
15
  export class Stage3DSession {
11
16
  private readonly layer = document.createElement("div");
@@ -23,13 +28,16 @@ export class Stage3DSession {
23
28
  constructor(
24
29
  box: HTMLElement,
25
30
  scripting: Scripting,
26
- create: Stage3DSurfaceFactory,
31
+ source: Stage3DSurfaceFactory | Stage3DSurfaceLoader,
27
32
  changed: () => void,
28
33
  ) {
29
34
  this.box = box;
30
35
  this.isolation = box.style.isolation;
31
36
  this.layer.style.cssText = "position:absolute;inset:0;pointer-events:none;z-index:0";
32
- const host = stage3DCanvasHost(this.layer, create);
37
+ let create = typeof source === "function" ? source : null;
38
+ // Asked once for this SWF: a failed load leaves its contexts unavailable.
39
+ let loading: Promise<unknown> | null = null;
40
+ const host = stage3DCanvasHost(this.layer, (slot) => (create as Stage3DSurfaceFactory)(slot));
33
41
  this.host = host;
34
42
 
35
43
  try {
@@ -37,6 +45,16 @@ export class Stage3DSession {
37
45
  // Keep canvases from different overlapping players in separate stacks.
38
46
  box.style.isolation = "isolate";
39
47
  this.close = installContext3D(scripting, {
48
+ ready: () => {
49
+ if (typeof source === "function" || create) {
50
+ return null;
51
+ }
52
+
53
+ loading ??= source.load().then((loaded) => {
54
+ create = loaded;
55
+ });
56
+ return loading;
57
+ },
40
58
  create: (slot, lifecycle) => {
41
59
  const context = host.create(slot, lifecycle);
42
60
  this.slots.set(slot, { x: 0, y: 0, visible: true });
package/src/workers.ts ADDED
@@ -0,0 +1,56 @@
1
+ // A SWF's Web Workers as the element starts them: whether its ActionScript
2
+ // names WorkerDomain, the host, and how long its first frame waits for them.
3
+ import { type Swf, tags } from "@swf2es/format";
4
+ import type { WebWorkerHost } from "@swf2es/player-hosts/workers";
5
+ import { workerHostForPlayer } from "./config.js";
6
+
7
+ /** The bytes of "WorkerDomain", the class a SWF's ActionScript names to start a worker. */
8
+ const WORKER_DOMAIN = new TextEncoder().encode("WorkerDomain");
9
+
10
+ /**
11
+ * Whether a SWF's ActionScript names WorkerDomain, in its ABCs' strings:
12
+ * one that does may start a worker and wait for it in the same script,
13
+ * which only a Web Worker started ahead can answer.
14
+ */
15
+ export function namesWorkers(swf: Swf): boolean {
16
+ const bytes = swf.bytes;
17
+ const first = WORKER_DOMAIN[0];
18
+ for (const tag of swf.tags) {
19
+ if (tag.code !== tags.DoABC && tag.code !== tags.DoABC2) {
20
+ continue;
21
+ }
22
+
23
+ const end = tag.offset + tag.length - WORKER_DOMAIN.length;
24
+ for (let at = bytes.indexOf(first, tag.offset); at !== -1 && at <= end; ) {
25
+ let k = 1;
26
+ while (k < WORKER_DOMAIN.length && bytes[at + k] === WORKER_DOMAIN[k]) {
27
+ k++;
28
+ }
29
+
30
+ if (k === WORKER_DOMAIN.length) {
31
+ return true;
32
+ }
33
+
34
+ at = bytes.indexOf(first, at + 1);
35
+ }
36
+ }
37
+
38
+ return false;
39
+ }
40
+
41
+ /**
42
+ * How long a SWF that names workers waits for the Web Workers started
43
+ * ahead before its first frame: past it, it plays on, and a worker it
44
+ * starts takes one still loading.
45
+ */
46
+ export const READY_WAIT = 10_000;
47
+
48
+ /** A player's Web Workers, or none where their module does not load, as under an import map that does not name it. */
49
+ export async function workersOrNone(ahead: boolean): Promise<WebWorkerHost | null> {
50
+ try {
51
+ return await workerHostForPlayer(ahead);
52
+ } catch (error) {
53
+ console.warn(`swf2es: workers are off, as their host did not load: ${error}`);
54
+ return null;
55
+ }
56
+ }