@glassly/miniapp 0.4.1 → 0.4.2-dev.1176
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/README.md +1 -0
- package/dist/display/index.d.ts +14 -4
- package/dist/display/index.d.ts.map +1 -1
- package/dist/display/index.js +11 -4
- package/dist/display/index.js.map +1 -1
- package/dist/display/profiles/index.d.ts +1 -1
- package/dist/display/profiles/index.d.ts.map +1 -1
- package/dist/display/profiles/index.js +1 -1
- package/dist/display/profiles/index.js.map +1 -1
- package/dist/display/profiles/nimo.d.ts +6 -0
- package/dist/display/profiles/nimo.d.ts.map +1 -1
- package/dist/display/profiles/nimo.js +17 -0
- package/dist/display/profiles/nimo.js.map +1 -1
- package/dist/display/scene/anchor.d.ts +16 -0
- package/dist/display/scene/anchor.d.ts.map +1 -0
- package/dist/display/scene/anchor.js +32 -0
- package/dist/display/scene/anchor.js.map +1 -0
- package/dist/display/scene/degrade.d.ts +25 -0
- package/dist/display/scene/degrade.d.ts.map +1 -0
- package/dist/display/scene/degrade.js +67 -0
- package/dist/display/scene/degrade.js.map +1 -0
- package/dist/display/scene/differ.d.ts +50 -0
- package/dist/display/scene/differ.d.ts.map +1 -0
- package/dist/display/scene/differ.js +129 -0
- package/dist/display/scene/differ.js.map +1 -0
- package/dist/display/scene/process.d.ts +41 -0
- package/dist/display/scene/process.d.ts.map +1 -0
- package/dist/display/scene/process.js +408 -0
- package/dist/display/scene/process.js.map +1 -0
- package/dist/display/scene/store.d.ts +48 -0
- package/dist/display/scene/store.d.ts.map +1 -0
- package/dist/display/scene/store.js +0 -0
- package/dist/display/scene/store.js.map +1 -0
- package/dist/display/scene/svgPath.d.ts +9 -0
- package/dist/display/scene/svgPath.d.ts.map +1 -0
- package/dist/display/scene/svgPath.js +167 -0
- package/dist/display/scene/svgPath.js.map +1 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +7 -0
- package/dist/host/index.js.map +1 -0
- package/dist/host/uiShim.d.ts +38 -0
- package/dist/host/uiShim.d.ts.map +1 -0
- package/dist/host/uiShim.js +383 -0
- package/dist/host/uiShim.js.map +1 -0
- package/dist/host/webviewGlobals.d.ts +60 -0
- package/dist/host/webviewGlobals.d.ts.map +1 -0
- package/dist/host/webviewGlobals.js +211 -0
- package/dist/host/webviewGlobals.js.map +1 -0
- package/dist/mirror/canvas.d.ts +13 -2
- package/dist/mirror/canvas.d.ts.map +1 -1
- package/dist/mirror/canvas.js +42 -21
- package/dist/mirror/canvas.js.map +1 -1
- package/dist/mirror/index.d.ts +1 -1
- package/dist/mirror/index.d.ts.map +1 -1
- package/dist/mirror/index.js.map +1 -1
- package/dist/modules/display.d.ts +10 -0
- package/dist/modules/display.d.ts.map +1 -1
- package/dist/modules/display.js.map +1 -1
- package/dist/ui/index.d.ts +1 -1
- package/dist/ui/index.js +1 -1
- package/package.json +6 -2
- package/src/display/index.ts +15 -3
- package/src/display/profiles/index.ts +1 -1
- package/src/display/profiles/nimo.ts +18 -0
- package/src/display/scene/anchor.ts +38 -0
- package/src/display/scene/degrade.ts +83 -0
- package/src/display/scene/differ.ts +152 -0
- package/src/display/scene/process.ts +443 -0
- package/src/display/scene/store.ts +0 -0
- package/src/display/scene/svgPath.ts +163 -0
- package/src/host/index.ts +14 -0
- package/src/host/uiShim.ts +389 -0
- package/src/host/webviewGlobals.ts +251 -0
- package/src/mirror/canvas.ts +86 -43
- package/src/mirror/index.ts +1 -1
- package/src/modules/display.ts +4 -0
- package/src/ui/index.ts +1 -1
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* window.glassly — WebView-side bridge to the per-miniapp background JSContext.
|
|
3
|
+
*
|
|
4
|
+
* Injected into every UI WebView via `injectedJavaScriptBeforeContentLoaded`
|
|
5
|
+
* (iOS) or `injectedJavaScriptObject` + a bundler-emitted inline `<script>`
|
|
6
|
+
* (Android, where the `BeforeContentLoaded` prop is documented unreliable
|
|
7
|
+
* per react-native-webview issues #1099 / #1609).
|
|
8
|
+
*
|
|
9
|
+
* The shim:
|
|
10
|
+
* - Buffers `glassly.send(channel, payload)` calls until `glassly.ready()`
|
|
11
|
+
* fires + the host acks the channel is open. WebView-side BUFFERS (the
|
|
12
|
+
* WebView is the short-lived side and shouldn't drop user input).
|
|
13
|
+
* - Installs `window.__host.recv(envelope)` for the host to push
|
|
14
|
+
* background → WebView messages via `webview.injectJavaScript(...)`.
|
|
15
|
+
* - Sends `__heartbeat__` every 5s; host considers the WebView gone
|
|
16
|
+
* after 15s of silence.
|
|
17
|
+
* - Maintains monotonic seq numbers; the host's per-WebView dedup window
|
|
18
|
+
* drops replays during reconnect.
|
|
19
|
+
*
|
|
20
|
+
* This file exports a `buildUiShim()` function returning the JS
|
|
21
|
+
* source as a string. The host's UIRouter wraps it inside the
|
|
22
|
+
* existing WebView setup so a single bundler-time string emission covers
|
|
23
|
+
* both platforms.
|
|
24
|
+
*
|
|
25
|
+
* IMPORTANT: the shim runs in the WebView's bare ECMAScript scope before
|
|
26
|
+
* the miniapp's bundle. It must not import anything; the source is one
|
|
27
|
+
* IIFE. The only injected dependencies are
|
|
28
|
+
* `window.ReactNativeWebView.postMessage` (provided by react-native-webview)
|
|
29
|
+
* and (on Android) `window.ReactNativeWebView.injectedObjectJson()`
|
|
30
|
+
* (provided when the host sets `injectedJavaScriptObject`).
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
export interface UiShimOptions {
|
|
34
|
+
packageName: string
|
|
35
|
+
/** @deprecated No longer used — heartbeat removed since WebViews are foreground-only. */
|
|
36
|
+
heartbeatIntervalMs?: number
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function buildUiShim(options: UiShimOptions): string {
|
|
40
|
+
const packageNameJson = JSON.stringify(options.packageName)
|
|
41
|
+
// The IIFE assembles a typed `glassly` global plus an internal
|
|
42
|
+
// `__host` for host inbound calls. Quoted strings are JSON-safe so
|
|
43
|
+
// direct injection inside another JS literal also works.
|
|
44
|
+
return `
|
|
45
|
+
(function () {
|
|
46
|
+
if (typeof window === 'undefined') return;
|
|
47
|
+
if (window.glassly && window.__host) return;
|
|
48
|
+
var packageName = ${packageNameJson};
|
|
49
|
+
// Resolve window.ReactNativeWebView.postMessage LAZILY, not once at shim-eval.
|
|
50
|
+
// On Android the shim is injected as a WebViewCompat document-start script
|
|
51
|
+
// (so it beats the page bundle and the ready handshake can't be raced — see
|
|
52
|
+
// the react-native-webview patch). But the order in which that document-start
|
|
53
|
+
// script runs vs. the WebMessageListener that installs
|
|
54
|
+
// window.ReactNativeWebView is not contractually fixed on Android the way it
|
|
55
|
+
// is on iOS (postMessageScript is registered before atStartScript there). If
|
|
56
|
+
// we captured rnPost once and the bridge object wasn't installed yet, every
|
|
57
|
+
// postMessage — including {type:'ready'} — would be silently dropped and the
|
|
58
|
+
// splash would hang forever. Resolving per-call means we always pick up the
|
|
59
|
+
// bridge once it exists, which it reliably does by the time the bundle mounts
|
|
60
|
+
// and calls glassly.ready().
|
|
61
|
+
function getRnPost() {
|
|
62
|
+
try {
|
|
63
|
+
return window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage === 'function'
|
|
64
|
+
? window.ReactNativeWebView.postMessage.bind(window.ReactNativeWebView)
|
|
65
|
+
: null;
|
|
66
|
+
} catch (e) { return null; }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Outbound seq number — purely a log-correlation aid (the host's
|
|
70
|
+
// router doesn't dedup anymore since reconnect can't happen: one
|
|
71
|
+
// WebView, foreground-only, destroyed on close → fresh shim on
|
|
72
|
+
// next open).
|
|
73
|
+
var outboundSeq = 1;
|
|
74
|
+
var ready = false;
|
|
75
|
+
var channelHandlers = Object.create(null);
|
|
76
|
+
var openHandlers = [];
|
|
77
|
+
var closeHandlers = [];
|
|
78
|
+
// Queued glassly.send calls before glassly.ready() acks. FIFO drained
|
|
79
|
+
// on ack. Buffered so user input (button taps, etc.) isn't dropped
|
|
80
|
+
// when the WebView mounts faster than the host expects.
|
|
81
|
+
var outboundQueue = [];
|
|
82
|
+
// Inbound payloads that arrived before any glassly.on subscriber was
|
|
83
|
+
// registered for the channel. The background side fires
|
|
84
|
+
// session.ui.onOpen handlers as soon as the WebView posts {type:'ready'},
|
|
85
|
+
// and on a fast bridge the resulting ui.send round-trips back before
|
|
86
|
+
// React useEffects have had a chance to attach listeners. Without
|
|
87
|
+
// buffering, the very first snapshot (the one a controller pushes from
|
|
88
|
+
// its ui.onOpen handler to hydrate the fresh UI) is silently dropped.
|
|
89
|
+
//
|
|
90
|
+
// Drain policy: the queue for a channel is flushed into the FIRST
|
|
91
|
+
// glassly.on(channel, ...) subscriber, then discarded. Subsequent
|
|
92
|
+
// sends with no listener fall back to the old drop-on-floor behaviour
|
|
93
|
+
// — we don't want a re-subscribe to replay stale state.
|
|
94
|
+
//
|
|
95
|
+
// Cap: 32 entries per channel, oldest dropped on overflow. Bounds the
|
|
96
|
+
// memory cost when a controller pushes to a channel that no UI page
|
|
97
|
+
// ever subscribes to.
|
|
98
|
+
var pendingByChannel = Object.create(null);
|
|
99
|
+
var PENDING_CAP_PER_CHANNEL = 32;
|
|
100
|
+
|
|
101
|
+
function postEnvelope(envelope) {
|
|
102
|
+
var rnPost = getRnPost();
|
|
103
|
+
if (!rnPost) return;
|
|
104
|
+
try { rnPost(JSON.stringify(envelope)); } catch (_) {}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function send(channel, payload) {
|
|
108
|
+
var seq = outboundSeq++;
|
|
109
|
+
var envelope = { type: 'msg', seq: seq, channel: String(channel), payload: payload };
|
|
110
|
+
if (!ready) {
|
|
111
|
+
outboundQueue.push(envelope);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
postEnvelope(envelope);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function flushQueue() {
|
|
118
|
+
while (outboundQueue.length > 0) {
|
|
119
|
+
postEnvelope(outboundQueue.shift());
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// NOTE: console.* tap lives in webviewGlobals.ts (injected alongside
|
|
124
|
+
// this shim for every UI WebView, two-layer or single-bundle). That
|
|
125
|
+
// shim posts {payload:{type:"dev_log", ...}} envelopes which
|
|
126
|
+
// LocalMiniappRuntime forwards to the dev sidecar tagged source:"ui".
|
|
127
|
+
// We don't re-wrap here to avoid double-delivery.
|
|
128
|
+
|
|
129
|
+
function on(channel, cb) {
|
|
130
|
+
if (typeof cb !== 'function') return function () {};
|
|
131
|
+
var list = channelHandlers[channel];
|
|
132
|
+
var firstSubscriber = !list || list.length === 0;
|
|
133
|
+
if (!list) { list = []; channelHandlers[channel] = list; }
|
|
134
|
+
list.push(cb);
|
|
135
|
+
// Drain any payloads buffered before this channel had a listener.
|
|
136
|
+
// Only fired on the first subscriber for the channel — a later
|
|
137
|
+
// re-subscribe (after an unsubscribe) does NOT replay history.
|
|
138
|
+
if (firstSubscriber) {
|
|
139
|
+
var pending = pendingByChannel[channel];
|
|
140
|
+
if (pending) {
|
|
141
|
+
delete pendingByChannel[channel];
|
|
142
|
+
for (var i = 0; i < pending.length; i++) {
|
|
143
|
+
try { cb(pending[i]); } catch (e) {}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return function () {
|
|
148
|
+
var idx = list.indexOf(cb);
|
|
149
|
+
if (idx >= 0) list.splice(idx, 1);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ── RPC ─────────────────────────────────────────────────────────────
|
|
154
|
+
// Outbound RPC: every glassly.request() generates a request id, sends a
|
|
155
|
+
// 'msg' envelope tagged with requestId, and stashes a one-shot resolver
|
|
156
|
+
// keyed on the id. The background-side reply is a recv('msg') with the
|
|
157
|
+
// same channel + requestId; we route it directly to the resolver.
|
|
158
|
+
//
|
|
159
|
+
// Cancellation: caller's AbortSignal triggers a 'cancel' envelope to
|
|
160
|
+
// background and rejects the local promise with a DOM-standard
|
|
161
|
+
// AbortError. The background side's UI_CANCEL handler aborts the
|
|
162
|
+
// handler's ctx.signal and drops the reply.
|
|
163
|
+
var rpcCounter = 0;
|
|
164
|
+
var rpcInflight = Object.create(null);
|
|
165
|
+
|
|
166
|
+
function makeRequestId() {
|
|
167
|
+
rpcCounter += 1;
|
|
168
|
+
return 'r' + rpcCounter + '-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 8);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function RpcError(message, code, stage, transport) {
|
|
172
|
+
var e = new Error(message);
|
|
173
|
+
e.name = 'RpcError';
|
|
174
|
+
if (code) {
|
|
175
|
+
try { e.cause = {code: code}; } catch (_) { /* old runtimes */ }
|
|
176
|
+
e.code = code;
|
|
177
|
+
}
|
|
178
|
+
// Diagnostic fields ride as own props so a miniapp can show exactly which
|
|
179
|
+
// pipeline stage and transport failed, not just a message string.
|
|
180
|
+
if (stage) e.stage = stage;
|
|
181
|
+
if (transport) e.transport = transport;
|
|
182
|
+
return e;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function RpcTimeoutError(message) {
|
|
186
|
+
var e = new Error(message || 'RPC timed out');
|
|
187
|
+
e.name = 'RpcTimeoutError';
|
|
188
|
+
return e;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function makeAbortError() {
|
|
192
|
+
var e;
|
|
193
|
+
try { e = new DOMException('aborted', 'AbortError'); } catch (_) {
|
|
194
|
+
e = new Error('aborted');
|
|
195
|
+
e.name = 'AbortError';
|
|
196
|
+
}
|
|
197
|
+
return e;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function request(channel, payload, options) {
|
|
201
|
+
var opts = options || {};
|
|
202
|
+
var signal = opts.signal;
|
|
203
|
+
var timeoutMs = typeof opts.timeout === 'number' ? opts.timeout : null;
|
|
204
|
+
var id = makeRequestId();
|
|
205
|
+
|
|
206
|
+
return new Promise(function (resolve, reject) {
|
|
207
|
+
var done = false;
|
|
208
|
+
var timeoutHandle = null;
|
|
209
|
+
var abortListener = null;
|
|
210
|
+
|
|
211
|
+
function cleanup() {
|
|
212
|
+
if (done) return;
|
|
213
|
+
done = true;
|
|
214
|
+
delete rpcInflight[id];
|
|
215
|
+
if (timeoutHandle != null) { clearTimeout(timeoutHandle); timeoutHandle = null; }
|
|
216
|
+
if (signal && abortListener) {
|
|
217
|
+
try { signal.removeEventListener('abort', abortListener); } catch (_) {}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function settle(envelope) {
|
|
222
|
+
if (done) return;
|
|
223
|
+
cleanup();
|
|
224
|
+
if (envelope && envelope.ok === true) {
|
|
225
|
+
resolve(envelope.result);
|
|
226
|
+
} else if (envelope && envelope.ok === false) {
|
|
227
|
+
reject(RpcError(envelope.error && envelope.error.message || 'RPC failed', envelope.error && envelope.error.code, envelope.error && envelope.error.stage, envelope.error && envelope.error.transport));
|
|
228
|
+
} else {
|
|
229
|
+
reject(RpcError('malformed RPC reply'));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
rpcInflight[id] = settle;
|
|
234
|
+
|
|
235
|
+
if (signal) {
|
|
236
|
+
if (signal.aborted) {
|
|
237
|
+
cleanup();
|
|
238
|
+
reject(makeAbortError());
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
abortListener = function () {
|
|
242
|
+
if (done) return;
|
|
243
|
+
cleanup();
|
|
244
|
+
postEnvelope({type: 'cancel', requestId: id});
|
|
245
|
+
reject(makeAbortError());
|
|
246
|
+
};
|
|
247
|
+
try { signal.addEventListener('abort', abortListener); } catch (_) {}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (timeoutMs != null) {
|
|
251
|
+
timeoutHandle = setTimeout(function () {
|
|
252
|
+
if (done) return;
|
|
253
|
+
cleanup();
|
|
254
|
+
postEnvelope({type: 'cancel', requestId: id});
|
|
255
|
+
reject(RpcTimeoutError());
|
|
256
|
+
}, timeoutMs);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
var envelope = {type: 'msg', seq: outboundSeq++, channel: String(channel), payload: payload, requestId: id};
|
|
260
|
+
if (!ready) {
|
|
261
|
+
outboundQueue.push(envelope);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
postEnvelope(envelope);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function onOpen(cb) {
|
|
269
|
+
if (typeof cb !== 'function') return function () {};
|
|
270
|
+
openHandlers.push(cb);
|
|
271
|
+
if (ready) {
|
|
272
|
+
try { cb(); } catch (e) {}
|
|
273
|
+
}
|
|
274
|
+
return function () {
|
|
275
|
+
var idx = openHandlers.indexOf(cb);
|
|
276
|
+
if (idx >= 0) openHandlers.splice(idx, 1);
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function onClose(cb) {
|
|
281
|
+
if (typeof cb !== 'function') return function () {};
|
|
282
|
+
closeHandlers.push(cb);
|
|
283
|
+
return function () {
|
|
284
|
+
var idx = closeHandlers.indexOf(cb);
|
|
285
|
+
if (idx >= 0) closeHandlers.splice(idx, 1);
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function readyFn() {
|
|
290
|
+
if (ready) return;
|
|
291
|
+
ready = true;
|
|
292
|
+
postEnvelope({ type: 'ready' });
|
|
293
|
+
flushQueue();
|
|
294
|
+
for (var i = 0; i < openHandlers.length; i++) {
|
|
295
|
+
try { openHandlers[i](); } catch (e) {}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function setBackgroundColorFn(color) {
|
|
300
|
+
// Tells the host what to paint the native WebView layer (the surface
|
|
301
|
+
// exposed during keyboard push-up / resize animations, which page CSS
|
|
302
|
+
// cannot reach). Host validates and falls back to its theme background.
|
|
303
|
+
if (typeof color !== 'string' || !color) return;
|
|
304
|
+
postEnvelope({ type: 'set_background', color: color });
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
var backHandler = null;
|
|
308
|
+
|
|
309
|
+
function setBackActionFn(handler) {
|
|
310
|
+
backHandler = typeof handler === 'function' ? handler : null;
|
|
311
|
+
postEnvelope({ type: 'set_back_action', shown: backHandler !== null });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function fireChannel(channel, payload) {
|
|
315
|
+
var list = channelHandlers[channel];
|
|
316
|
+
if (!list || list.length === 0) {
|
|
317
|
+
// No subscriber yet — stash for the first glassly.on(channel, ...).
|
|
318
|
+
// Bounded FIFO: drop the oldest when capped so a stuck channel
|
|
319
|
+
// can't grow unbounded.
|
|
320
|
+
var q = pendingByChannel[channel];
|
|
321
|
+
if (!q) { q = []; pendingByChannel[channel] = q; }
|
|
322
|
+
q.push(payload);
|
|
323
|
+
if (q.length > PENDING_CAP_PER_CHANNEL) q.shift();
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
for (var i = 0; i < list.length; i++) {
|
|
327
|
+
try { list[i](payload); } catch (e) {}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function recv(envelope) {
|
|
332
|
+
if (!envelope || typeof envelope !== 'object') return;
|
|
333
|
+
var type = envelope.type;
|
|
334
|
+
if (type === 'msg' && typeof envelope.channel === 'string') {
|
|
335
|
+
// RPC frames carry a requestId; broadcast frames don't. Treat them
|
|
336
|
+
// as disjoint — never fall through from RPC to broadcast. An orphan
|
|
337
|
+
// RPC reply (caller already aborted/timed out) is silently dropped.
|
|
338
|
+
if (typeof envelope.requestId === 'string') {
|
|
339
|
+
var settle = rpcInflight[envelope.requestId];
|
|
340
|
+
if (settle) settle(envelope.payload);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
fireChannel(envelope.channel, envelope.payload);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (type === 'open') {
|
|
347
|
+
for (var j = 0; j < openHandlers.length; j++) {
|
|
348
|
+
try { openHandlers[j](); } catch (e) {}
|
|
349
|
+
}
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (type === 'close') {
|
|
353
|
+
for (var k = 0; k < closeHandlers.length; k++) {
|
|
354
|
+
try { closeHandlers[k](); } catch (e) {}
|
|
355
|
+
}
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (type === 'back') {
|
|
359
|
+
if (backHandler) {
|
|
360
|
+
try { backHandler(); } catch (e) {}
|
|
361
|
+
}
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (type === 'cancel' && typeof envelope.requestId === 'string') {
|
|
365
|
+
// Background-side cancel (reserved; currently unused). If we had a
|
|
366
|
+
// future bidirectional RPC this would abort an in-flight UI handler.
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if (type === 'ack') {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
window.glassly = {
|
|
375
|
+
send: send,
|
|
376
|
+
on: on,
|
|
377
|
+
request: request,
|
|
378
|
+
onOpen: onOpen,
|
|
379
|
+
onClose: onClose,
|
|
380
|
+
ready: readyFn,
|
|
381
|
+
setBackgroundColor: setBackgroundColorFn,
|
|
382
|
+
setBackAction: setBackActionFn,
|
|
383
|
+
/** @internal — packageName for diagnostic logs only. */
|
|
384
|
+
_packageName: packageName,
|
|
385
|
+
};
|
|
386
|
+
window.__host = { recv: recv };
|
|
387
|
+
})();
|
|
388
|
+
`.trim()
|
|
389
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builder for the window.Glassly globals a host injects into every miniapp
|
|
3
|
+
* WebView before its content loads. The Glassly App and the desktop simulator
|
|
4
|
+
* both inject this script, so a page sees the same API surface in either.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {MiniappColorScheme} from "../globals"
|
|
8
|
+
|
|
9
|
+
export type {MiniappColorScheme}
|
|
10
|
+
|
|
11
|
+
export interface MiniappSafeArea {
|
|
12
|
+
top: number
|
|
13
|
+
bottom: number
|
|
14
|
+
left: number
|
|
15
|
+
right: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The host's capsule menu in the WebView's own coordinate space. `corner`
|
|
20
|
+
* floats over the page's top-right; `rail` sits in the iPhone Duo's status
|
|
21
|
+
* rail, inside the side safe-area inset and clear of the page content.
|
|
22
|
+
*/
|
|
23
|
+
export interface CapsuleMenuRect {
|
|
24
|
+
top: number
|
|
25
|
+
right: number
|
|
26
|
+
bottom: number
|
|
27
|
+
left: number
|
|
28
|
+
width: number
|
|
29
|
+
height: number
|
|
30
|
+
placement: "corner" | "rail"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface BuildWebviewGlobalsOptions {
|
|
34
|
+
platform: string
|
|
35
|
+
packageName?: string
|
|
36
|
+
capabilities?: string[]
|
|
37
|
+
miniappLocal?: boolean
|
|
38
|
+
miniappDeveloperMode?: boolean
|
|
39
|
+
safeAreaInsets: MiniappSafeArea
|
|
40
|
+
/** Where the host draws its capsule menu over this WebView. */
|
|
41
|
+
capsuleMenu: CapsuleMenuRect
|
|
42
|
+
/** Current host color scheme. Miniapps may follow this to match the phone. */
|
|
43
|
+
colorScheme?: MiniappColorScheme
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// CSS custom properties that mirror the safe-area and capsule menu data, so
|
|
47
|
+
// miniapp CSS (or Tailwind arbitrary values) can read them without touching
|
|
48
|
+
// JS: e.g. `style="margin-top: var(--glassly-capsule-top)"`.
|
|
49
|
+
function layoutCssVars(insets: MiniappSafeArea, capsule: CapsuleMenuRect): Record<string, string> {
|
|
50
|
+
return {
|
|
51
|
+
"--glassly-safe-top": `${insets.top}px`,
|
|
52
|
+
"--glassly-safe-bottom": `${insets.bottom}px`,
|
|
53
|
+
"--glassly-safe-left": `${insets.left}px`,
|
|
54
|
+
"--glassly-safe-right": `${insets.right}px`,
|
|
55
|
+
"--glassly-capsule-top": `${capsule.top}px`,
|
|
56
|
+
"--glassly-capsule-bottom": `${capsule.bottom}px`,
|
|
57
|
+
"--glassly-capsule-left": `${capsule.left}px`,
|
|
58
|
+
"--glassly-capsule-right": `${capsule.right}px`,
|
|
59
|
+
"--glassly-capsule-width": `${capsule.width}px`,
|
|
60
|
+
"--glassly-capsule-height": `${capsule.height}px`,
|
|
61
|
+
"--glassly-capsule-center-y": `${capsule.top + capsule.height / 2}px`,
|
|
62
|
+
// Right-side gutter to reserve so content doesn't slide under the
|
|
63
|
+
// capsule: its width + 16px breathing room, none when it sits in the rail.
|
|
64
|
+
"--glassly-capsule-gutter": `${capsule.placement === "rail" ? 0 : capsule.width + 16}px`,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* JS to inject into a running miniapp when the host's safe area or capsule
|
|
70
|
+
* menu moves (an iPhone Duo folding, or its rail's reserved regions growing):
|
|
71
|
+
* updates window.Glassly and the CSS variables, then fires
|
|
72
|
+
* `glassly:safe-area-change`.
|
|
73
|
+
*/
|
|
74
|
+
export function buildSafeAreaUpdateScript(insets: MiniappSafeArea, capsuleMenu: CapsuleMenuRect): string {
|
|
75
|
+
const sets = Object.entries(layoutCssVars(insets, capsuleMenu))
|
|
76
|
+
.map(([k, v]) => `document.documentElement.style.setProperty(${JSON.stringify(k)}, ${JSON.stringify(v)});`)
|
|
77
|
+
.join("\n ")
|
|
78
|
+
return `
|
|
79
|
+
window.Glassly = window.Glassly || {};
|
|
80
|
+
window.Glassly.safeAreaInsets = ${JSON.stringify(insets)};
|
|
81
|
+
window.Glassly.capsuleMenu = ${JSON.stringify(capsuleMenu)};
|
|
82
|
+
try {
|
|
83
|
+
${sets}
|
|
84
|
+
} catch (e) { /* ignore */ }
|
|
85
|
+
window.dispatchEvent(new Event("glassly:safe-area-change"));
|
|
86
|
+
true;
|
|
87
|
+
`
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Returns the JS string to inject into a miniapp WebView before its content
|
|
92
|
+
* loads. Sets window.Glassly with the standard fields miniapps read:
|
|
93
|
+
*
|
|
94
|
+
* window.Glassly = {
|
|
95
|
+
* platform, packageName?, capabilities, safeAreaInsets, capsuleMenu,
|
|
96
|
+
* miniappLocal?, miniappDeveloperMode?
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* Also defines a stub window.receiveNativeMessage so the miniapp can safely
|
|
100
|
+
* assign to it before the native bridge wires in.
|
|
101
|
+
*/
|
|
102
|
+
export function buildWebviewGlobalsScript(opts: BuildWebviewGlobalsOptions): string {
|
|
103
|
+
const globals: Record<string, unknown> = {
|
|
104
|
+
platform: opts.platform,
|
|
105
|
+
capabilities: opts.capabilities ?? ["share", "open_url", "copy_clipboard", "download"],
|
|
106
|
+
safeAreaInsets: opts.safeAreaInsets,
|
|
107
|
+
capsuleMenu: opts.capsuleMenu,
|
|
108
|
+
}
|
|
109
|
+
if (opts.packageName) globals.packageName = opts.packageName
|
|
110
|
+
if (opts.miniappLocal) globals.miniappLocal = true
|
|
111
|
+
if (opts.miniappDeveloperMode) globals.miniappDeveloperMode = true
|
|
112
|
+
if (opts.colorScheme) globals.colorScheme = opts.colorScheme
|
|
113
|
+
|
|
114
|
+
const cssVars = layoutCssVars(opts.safeAreaInsets, opts.capsuleMenu)
|
|
115
|
+
const cssVarsBlock = Object.entries(cssVars)
|
|
116
|
+
.map(([k, v]) => `${k}: ${v};`)
|
|
117
|
+
.join(" ")
|
|
118
|
+
|
|
119
|
+
// Console-tap shim: wrap console.log/warn/error/info/debug so each call
|
|
120
|
+
// also forwards a `dev_log` envelope back to the phone via
|
|
121
|
+
// ReactNativeWebView.postMessage. Dev mode forwards to the laptop's
|
|
122
|
+
// `glassly dev` terminal; non-dev mode falls back to the React
|
|
123
|
+
// Native log stream (Metro / Xcode console / adb logcat) so installed-
|
|
124
|
+
// miniapp errors are still inspectable when there's no laptop sidecar.
|
|
125
|
+
//
|
|
126
|
+
// Also tap window.onerror and window.onunhandledrejection so synchronous
|
|
127
|
+
// throws and unhandled promise rejections surface even when the bundle
|
|
128
|
+
// never gets a chance to call console.error itself.
|
|
129
|
+
const consoleTapBlock = `
|
|
130
|
+
(function() {
|
|
131
|
+
try {
|
|
132
|
+
var levels = ["log", "warn", "error", "info", "debug"];
|
|
133
|
+
var seen = new WeakSet();
|
|
134
|
+
function safeSerialize(arg) {
|
|
135
|
+
if (arg instanceof Error) return {__error: true, message: arg.message, stack: arg.stack};
|
|
136
|
+
if (arg === null || typeof arg !== "object") return arg;
|
|
137
|
+
try {
|
|
138
|
+
return JSON.parse(JSON.stringify(arg, function(k, v) {
|
|
139
|
+
if (v !== null && typeof v === "object") {
|
|
140
|
+
if (seen.has(v)) return "[Circular]";
|
|
141
|
+
seen.add(v);
|
|
142
|
+
}
|
|
143
|
+
return v;
|
|
144
|
+
}));
|
|
145
|
+
} catch (e) {
|
|
146
|
+
try { return String(arg); } catch (_) { return "[unserializable]"; }
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
levels.forEach(function(level) {
|
|
150
|
+
var original = console[level];
|
|
151
|
+
if (typeof original !== "function") return;
|
|
152
|
+
console[level] = function() {
|
|
153
|
+
try { original.apply(console, arguments); } catch (_) {}
|
|
154
|
+
try {
|
|
155
|
+
var args = Array.prototype.slice.call(arguments).map(safeSerialize);
|
|
156
|
+
if (window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage === "function") {
|
|
157
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
158
|
+
payload: {
|
|
159
|
+
type: "dev_log",
|
|
160
|
+
level: level,
|
|
161
|
+
args: args,
|
|
162
|
+
packageName: (window.Glassly && window.Glassly.packageName) || null,
|
|
163
|
+
timestamp: Date.now()
|
|
164
|
+
}
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
} catch (_) { /* swallow */ }
|
|
168
|
+
};
|
|
169
|
+
});
|
|
170
|
+
} catch (_) { /* ignore */ }
|
|
171
|
+
|
|
172
|
+
// Surface synchronous errors and unhandled promise rejections via the
|
|
173
|
+
// same dev_log channel — many bundle-startup crashes never get to call
|
|
174
|
+
// console.error themselves.
|
|
175
|
+
try {
|
|
176
|
+
window.addEventListener("error", function(e) {
|
|
177
|
+
try {
|
|
178
|
+
if (window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage === "function") {
|
|
179
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
180
|
+
payload: {
|
|
181
|
+
type: "dev_log",
|
|
182
|
+
level: "error",
|
|
183
|
+
args: ["[window.error] " + (e.message || "unknown"), {filename: e.filename, lineno: e.lineno, colno: e.colno, stack: e.error && e.error.stack}],
|
|
184
|
+
packageName: (window.Glassly && window.Glassly.packageName) || null,
|
|
185
|
+
timestamp: Date.now()
|
|
186
|
+
}
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
} catch (_) {}
|
|
190
|
+
});
|
|
191
|
+
window.addEventListener("unhandledrejection", function(e) {
|
|
192
|
+
try {
|
|
193
|
+
if (window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage === "function") {
|
|
194
|
+
var reason = e.reason;
|
|
195
|
+
var serialized = reason instanceof Error
|
|
196
|
+
? {__error: true, message: reason.message, stack: reason.stack}
|
|
197
|
+
: (typeof reason === "object" ? JSON.stringify(reason) : String(reason));
|
|
198
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
199
|
+
payload: {
|
|
200
|
+
type: "dev_log",
|
|
201
|
+
level: "error",
|
|
202
|
+
args: ["[unhandledrejection]", serialized],
|
|
203
|
+
packageName: (window.Glassly && window.Glassly.packageName) || null,
|
|
204
|
+
timestamp: Date.now()
|
|
205
|
+
}
|
|
206
|
+
}));
|
|
207
|
+
}
|
|
208
|
+
} catch (_) {}
|
|
209
|
+
});
|
|
210
|
+
} catch (_) {}
|
|
211
|
+
})();
|
|
212
|
+
`
|
|
213
|
+
|
|
214
|
+
// Lock zoom so miniapps feel like apps, not pages: force a non-scalable
|
|
215
|
+
// viewport meta (overriding whatever the miniapp shipped) and disable
|
|
216
|
+
// double-tap-zoom via CSS. Re-applies on DOMContentLoaded in case the
|
|
217
|
+
// miniapp re-writes <head> during boot.
|
|
218
|
+
return `
|
|
219
|
+
window.Glassly = ${JSON.stringify(globals)};
|
|
220
|
+
window.receiveNativeMessage = window.receiveNativeMessage || function() {};
|
|
221
|
+
(function() {
|
|
222
|
+
try {
|
|
223
|
+
var styleEl = document.createElement("style");
|
|
224
|
+
styleEl.setAttribute("data-glassly-injected", "1");
|
|
225
|
+
styleEl.textContent =
|
|
226
|
+
":root { ${cssVarsBlock} }" +
|
|
227
|
+
"html, body { touch-action: manipulation; -ms-content-zooming: none; }";
|
|
228
|
+
(document.head || document.documentElement).appendChild(styleEl);
|
|
229
|
+
} catch (e) { /* ignore */ }
|
|
230
|
+
|
|
231
|
+
function lockViewport() {
|
|
232
|
+
try {
|
|
233
|
+
var content = "width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover";
|
|
234
|
+
var existing = document.querySelector('meta[name="viewport"]');
|
|
235
|
+
if (existing) {
|
|
236
|
+
existing.setAttribute("content", content);
|
|
237
|
+
} else {
|
|
238
|
+
var meta = document.createElement("meta");
|
|
239
|
+
meta.setAttribute("name", "viewport");
|
|
240
|
+
meta.setAttribute("content", content);
|
|
241
|
+
(document.head || document.documentElement).appendChild(meta);
|
|
242
|
+
}
|
|
243
|
+
} catch (e) { /* ignore */ }
|
|
244
|
+
}
|
|
245
|
+
lockViewport();
|
|
246
|
+
document.addEventListener("DOMContentLoaded", lockViewport);
|
|
247
|
+
})();
|
|
248
|
+
${consoleTapBlock}
|
|
249
|
+
true;
|
|
250
|
+
`
|
|
251
|
+
}
|