electron-cast 0.2.0 → 0.3.0
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 +10 -0
- package/README.md +49 -20
- package/dist/electron/main.d.ts +15 -2
- package/dist/electron/main.js +200 -14
- package/dist/electron/main.js.map +1 -1
- package/dist/electron/preload.d.ts +13 -1
- package/dist/electron/preload.js +50 -1
- package/dist/electron/preload.js.map +1 -1
- package/dist/electron/presentation-envelope.d.ts +60 -0
- package/dist/electron/presentation-envelope.js +296 -0
- package/dist/electron/presentation-envelope.js.map +1 -0
- package/dist/electron/presentation-handler.d.ts +100 -0
- package/dist/electron/presentation-handler.js +487 -0
- package/dist/electron/presentation-handler.js.map +1 -0
- package/dist/polyfill/presentation-standin.d.ts +13 -0
- package/dist/polyfill/presentation-standin.js +275 -0
- package/dist/polyfill/presentation-standin.js.map +1 -0
- package/package.json +4 -3
- package/src/electron/main.ts +197 -14
- package/src/electron/preload.ts +59 -1
- package/src/electron/presentation-envelope.ts +311 -0
- package/src/electron/presentation-handler.ts +518 -0
- package/src/polyfill/presentation-standin.ts +271 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Installs a page-side stand-in for the Presentation API (`PresentationRequest`, `PresentationConnection`,
|
|
3
|
+
* `PresentationAvailability` and `navigator.presentation`) backed by the IPC bridge the preload script
|
|
4
|
+
* published at `globalThis[bridgeKey]`, so Google's own Cast sender script (clank.js) can run unmodified
|
|
5
|
+
* in Electron. Returns `true` when the stand-in was installed, `false` when the bridge is missing (in
|
|
6
|
+
* which case nothing is changed).
|
|
7
|
+
*
|
|
8
|
+
* Electron runs this function in the page's main world through `contextBridge.executeInMainWorld`, which
|
|
9
|
+
* serializes it to source; the body must therefore stay self-contained: no imports, no module-level
|
|
10
|
+
* helpers or state, and every host global reached through `globalThis`. Type annotations are stripped and
|
|
11
|
+
* leave no runtime references behind.
|
|
12
|
+
*/
|
|
13
|
+
export function installPresentationStandIn(bridgeKey: string): boolean {
|
|
14
|
+
const g: any = globalThis;
|
|
15
|
+
const bridge: any = g[bridgeKey];
|
|
16
|
+
if (!bridge || typeof bridge.call !== "function") return false;
|
|
17
|
+
|
|
18
|
+
const ET: any = g.EventTarget;
|
|
19
|
+
const EV: any = g.Event;
|
|
20
|
+
const ME: any = g.MessageEvent;
|
|
21
|
+
const DE: any = g.DOMException;
|
|
22
|
+
const URLC: any = g.URL;
|
|
23
|
+
const ST: any = g.setTimeout;
|
|
24
|
+
const uuid = (): string => g.crypto.randomUUID();
|
|
25
|
+
|
|
26
|
+
const MAX_QUEUED_MESSAGES = 1000;
|
|
27
|
+
const connections = new Map<string, any>();
|
|
28
|
+
const watches = new Map<string, any>();
|
|
29
|
+
let pendingStart = false;
|
|
30
|
+
|
|
31
|
+
// A queued message may be delivered only once the connection is "connected" AND a message handler
|
|
32
|
+
// exists (an onmessage value or an addEventListener("message") listener).
|
|
33
|
+
function messageReady(conn: any): boolean {
|
|
34
|
+
return conn.state === "connected"
|
|
35
|
+
&& (typeof conn.__h_message === "function" || conn.__msgListeners > 0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function maybeFlush(conn: any): void {
|
|
39
|
+
if (conn.__flushScheduled || !messageReady(conn)) return;
|
|
40
|
+
conn.__flushScheduled = true;
|
|
41
|
+
ST(() => {
|
|
42
|
+
conn.__flushScheduled = false;
|
|
43
|
+
while (conn.__queue.length > 0 && messageReady(conn)) {
|
|
44
|
+
const data = conn.__queue.shift();
|
|
45
|
+
conn.dispatchEvent(new ME("message", { data }));
|
|
46
|
+
}
|
|
47
|
+
}, 0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Defines the on<name> getter/setter pairs (the setter stores a function or null) and wraps
|
|
51
|
+
// dispatchEvent so the stored handler is invoked in addition to addEventListener listeners.
|
|
52
|
+
const wireEvents = (proto: any, names: string[], onHandlerSet?: (instance: any) => void): void => {
|
|
53
|
+
for (const name of names) {
|
|
54
|
+
const slot = "__h_" + name;
|
|
55
|
+
Object.defineProperty(proto, "on" + name, {
|
|
56
|
+
configurable: true,
|
|
57
|
+
enumerable: true,
|
|
58
|
+
get(this: any) { return this[slot] ?? null; },
|
|
59
|
+
set(this: any, value: any) {
|
|
60
|
+
this[slot] = typeof value === "function" ? value : null;
|
|
61
|
+
if (typeof onHandlerSet === "function") onHandlerSet(this);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const nativeDispatch = proto.dispatchEvent;
|
|
66
|
+
proto.dispatchEvent = function (this: any, event: any) {
|
|
67
|
+
const handled = nativeDispatch.call(this, event);
|
|
68
|
+
const handler = this["__h_" + event.type];
|
|
69
|
+
if (typeof handler === "function") handler.call(this, event);
|
|
70
|
+
return handled;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
class PresentationAvailability extends ET {
|
|
75
|
+
__value: boolean;
|
|
76
|
+
constructor(value: boolean) {
|
|
77
|
+
super();
|
|
78
|
+
this.__value = !!value;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
Object.defineProperty(PresentationAvailability.prototype, "value", {
|
|
82
|
+
configurable: true,
|
|
83
|
+
enumerable: true,
|
|
84
|
+
get(this: any) { return this.__value; },
|
|
85
|
+
});
|
|
86
|
+
wireEvents(PresentationAvailability.prototype, ["change"]);
|
|
87
|
+
|
|
88
|
+
class PresentationConnection extends ET {
|
|
89
|
+
connectionId: string;
|
|
90
|
+
id: string;
|
|
91
|
+
url: string;
|
|
92
|
+
state: string;
|
|
93
|
+
binaryType: string;
|
|
94
|
+
__queue: string[];
|
|
95
|
+
__flushScheduled: boolean;
|
|
96
|
+
__msgListeners: number;
|
|
97
|
+
__h_message: any;
|
|
98
|
+
constructor(connectionId: string, state: string) {
|
|
99
|
+
super();
|
|
100
|
+
this.connectionId = connectionId;
|
|
101
|
+
this.id = "";
|
|
102
|
+
this.url = "";
|
|
103
|
+
this.state = state;
|
|
104
|
+
this.binaryType = "arraybuffer";
|
|
105
|
+
this.__queue = [];
|
|
106
|
+
this.__flushScheduled = false;
|
|
107
|
+
this.__msgListeners = 0;
|
|
108
|
+
this.__h_message = null;
|
|
109
|
+
}
|
|
110
|
+
send(data: any): void {
|
|
111
|
+
if (this.state !== "connected") throw new DE("The connection is not open.", "InvalidStateError");
|
|
112
|
+
if (typeof data !== "string") throw new TypeError("send() accepts strings only; binary data is not supported.");
|
|
113
|
+
bridge.send(this.connectionId, data);
|
|
114
|
+
}
|
|
115
|
+
close(): void { this.__end("closed"); }
|
|
116
|
+
terminate(): void { this.__end("terminated"); }
|
|
117
|
+
__end(state: string): void {
|
|
118
|
+
if (this.state === "closed" || this.state === "terminated") return;
|
|
119
|
+
this.state = state;
|
|
120
|
+
bridge.call(state === "closed" ? "close" : "terminate", { connectionId: this.connectionId });
|
|
121
|
+
connections.delete(this.connectionId);
|
|
122
|
+
this.dispatchEvent(new EV(state === "closed" ? "close" : "terminate"));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
wireEvents(PresentationConnection.prototype, ["message", "close", "terminate", "connect"], (conn) => maybeFlush(conn));
|
|
126
|
+
const nativeAdd = PresentationConnection.prototype.addEventListener;
|
|
127
|
+
const nativeRemove = PresentationConnection.prototype.removeEventListener;
|
|
128
|
+
PresentationConnection.prototype.addEventListener = function (this: any, type: any, listener: any, options: any) {
|
|
129
|
+
if (type === "message") {
|
|
130
|
+
this.__msgListeners += 1;
|
|
131
|
+
maybeFlush(this);
|
|
132
|
+
}
|
|
133
|
+
return nativeAdd.call(this, type, listener, options);
|
|
134
|
+
};
|
|
135
|
+
PresentationConnection.prototype.removeEventListener = function (this: any, type: any, listener: any, options: any) {
|
|
136
|
+
if (type === "message" && this.__msgListeners > 0) this.__msgListeners -= 1;
|
|
137
|
+
return nativeRemove.call(this, type, listener, options);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
function settleConnection(conn: any, connectionId: string, result: any): any {
|
|
141
|
+
if (result && result.ok) {
|
|
142
|
+
const value = result.value || {};
|
|
143
|
+
conn.id = value.id ?? "";
|
|
144
|
+
conn.url = value.url ?? "";
|
|
145
|
+
conn.state = "connected";
|
|
146
|
+
conn.dispatchEvent(new EV("connect"));
|
|
147
|
+
maybeFlush(conn);
|
|
148
|
+
return conn;
|
|
149
|
+
}
|
|
150
|
+
connections.delete(connectionId);
|
|
151
|
+
throw new DE((result && result.description) ?? "", (result && result.code) ?? "OperationError");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
class PresentationRequest extends ET {
|
|
155
|
+
urls: string[];
|
|
156
|
+
constructor(urls: any) {
|
|
157
|
+
super();
|
|
158
|
+
const list: string[] = Array.isArray(urls) ? urls.slice() : [urls];
|
|
159
|
+
if (list.length === 0) {
|
|
160
|
+
throw new DE("PresentationRequest requires at least one url.", "NotSupportedError");
|
|
161
|
+
}
|
|
162
|
+
for (const u of list) {
|
|
163
|
+
try {
|
|
164
|
+
new URLC(String(u));
|
|
165
|
+
} catch {
|
|
166
|
+
throw new DE("Invalid URL: " + String(u), "SyntaxError");
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
this.urls = Object.freeze(list) as string[];
|
|
170
|
+
}
|
|
171
|
+
start(): Promise<any> {
|
|
172
|
+
const nav = g.navigator;
|
|
173
|
+
const ua = nav ? nav.userActivation : undefined;
|
|
174
|
+
if (ua && ua.isActive === false) {
|
|
175
|
+
return Promise.reject(new DE("Presentation.start() requires a user gesture.", "AbortError"));
|
|
176
|
+
}
|
|
177
|
+
if (pendingStart) {
|
|
178
|
+
return Promise.reject(new DE("A call to start() is already in progress.", "OperationError"));
|
|
179
|
+
}
|
|
180
|
+
pendingStart = true;
|
|
181
|
+
const connectionId = uuid();
|
|
182
|
+
const conn = new PresentationConnection(connectionId, "connecting");
|
|
183
|
+
connections.set(connectionId, conn);
|
|
184
|
+
return Promise.resolve(bridge.call("start", { connectionId, urls: [...this.urls], hasGesture: !(ua && ua.isActive === false) }))
|
|
185
|
+
.then((result: any) => settleConnection(conn, connectionId, result))
|
|
186
|
+
.finally(() => {
|
|
187
|
+
pendingStart = false;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
reconnect(presentationId: any): Promise<any> {
|
|
191
|
+
const connectionId = uuid();
|
|
192
|
+
const conn = new PresentationConnection(connectionId, "connecting");
|
|
193
|
+
connections.set(connectionId, conn);
|
|
194
|
+
return Promise.resolve(bridge.call("reconnect", { connectionId, urls: [...this.urls], presentationId }))
|
|
195
|
+
.then((result: any) => settleConnection(conn, connectionId, result));
|
|
196
|
+
}
|
|
197
|
+
getAvailability(): Promise<any> {
|
|
198
|
+
const watchId = uuid();
|
|
199
|
+
const avail = new PresentationAvailability(false);
|
|
200
|
+
watches.set(watchId, avail);
|
|
201
|
+
return Promise.resolve(bridge.call("getAvailability", { watchId, urls: [...this.urls] }))
|
|
202
|
+
.then((result: any) => {
|
|
203
|
+
avail.__value = !!(result && result.ok && result.value && result.value.available);
|
|
204
|
+
return avail;
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
wireEvents(PresentationRequest.prototype, ["connectionavailable"]);
|
|
209
|
+
|
|
210
|
+
bridge.onEvent((e: any) => {
|
|
211
|
+
if (!e || typeof e !== "object") return;
|
|
212
|
+
if (e.type === "availability") {
|
|
213
|
+
const avail = watches.get(e.watchId);
|
|
214
|
+
if (!avail) return;
|
|
215
|
+
const next = !!e.value;
|
|
216
|
+
if (next !== avail.__value) {
|
|
217
|
+
avail.__value = next;
|
|
218
|
+
avail.dispatchEvent(new EV("change"));
|
|
219
|
+
}
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (e.type === "message") {
|
|
223
|
+
const conn = connections.get(e.connectionId);
|
|
224
|
+
if (!conn) return;
|
|
225
|
+
conn.__queue.push(e.data);
|
|
226
|
+
if (conn.__queue.length > MAX_QUEUED_MESSAGES) conn.__queue.shift();
|
|
227
|
+
maybeFlush(conn);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (e.type === "statechange") {
|
|
231
|
+
const conn = connections.get(e.connectionId);
|
|
232
|
+
if (!conn) return;
|
|
233
|
+
const terminated = e.state === "terminated";
|
|
234
|
+
conn.state = terminated ? "terminated" : "closed";
|
|
235
|
+
connections.delete(e.connectionId);
|
|
236
|
+
const ev = new EV(terminated ? "terminate" : "close");
|
|
237
|
+
ev.reason = e.reason ?? null;
|
|
238
|
+
ev.message = e.message ?? null;
|
|
239
|
+
conn.dispatchEvent(ev);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (e.type === "connectionavailable") {
|
|
243
|
+
const conn = new PresentationConnection(e.connectionId, "connected");
|
|
244
|
+
conn.id = e.id ?? "";
|
|
245
|
+
conn.url = e.url ?? "";
|
|
246
|
+
connections.set(e.connectionId, conn);
|
|
247
|
+
const pres = g.navigator ? g.navigator.presentation : null;
|
|
248
|
+
const req = pres ? pres.defaultRequest : null;
|
|
249
|
+
if (req instanceof PresentationRequest) {
|
|
250
|
+
const ev = new EV("connectionavailable");
|
|
251
|
+
ev.connection = conn;
|
|
252
|
+
req.dispatchEvent(ev);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const pres: any = { defaultRequest: null, receiver: null };
|
|
258
|
+
if (g.navigator) {
|
|
259
|
+
Object.defineProperty(g.navigator, "presentation", {
|
|
260
|
+
configurable: true,
|
|
261
|
+
enumerable: true,
|
|
262
|
+
get() { return pres; },
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
Object.defineProperty(g, "PresentationRequest", { value: PresentationRequest, writable: true, configurable: true, enumerable: true });
|
|
266
|
+
Object.defineProperty(g, "PresentationConnection", { value: PresentationConnection, writable: true, configurable: true, enumerable: true });
|
|
267
|
+
Object.defineProperty(g, "PresentationAvailability", { value: PresentationAvailability, writable: true, configurable: true, enumerable: true });
|
|
268
|
+
if (!g.chrome) g.chrome = {};
|
|
269
|
+
|
|
270
|
+
return true;
|
|
271
|
+
}
|