magpie-multi-usage 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.
package/page/boot.js ADDED
@@ -0,0 +1,161 @@
1
+ /*
2
+ * Sandboxed Magpie page launcher for magpie-multi-usage.
3
+ *
4
+ * Runs inside sandbox="allow-scripts" and connect-src 'none':
5
+ * performs zero network requests and zero storage/cookie access, redirecting
6
+ * directly via location.replace() to magpie.service or 127.0.0.1:<port>.
7
+ */
8
+ (async () => {
9
+ const DEFAULT_PORT = 18792;
10
+ let port = DEFAULT_PORT;
11
+ let theme = "";
12
+ let lang = "";
13
+ let tokens = null;
14
+ let serviceUrl = "";
15
+
16
+ try {
17
+ const ready = await window.magpie?.ready;
18
+ if (ready) {
19
+ if (typeof ready.dark === "boolean") {
20
+ theme = ready.dark ? "dark" : "light";
21
+ }
22
+ if (typeof ready.lang === "string" && ready.lang.trim()) {
23
+ lang = ready.lang.trim();
24
+ }
25
+ if (ready.tokens && typeof ready.tokens === "object") {
26
+ tokens = ready.tokens;
27
+ }
28
+ }
29
+ } catch {
30
+ // Fall back to default theme when bridge metadata is unavailable.
31
+ }
32
+
33
+ try {
34
+ const candidate = window.magpie?.service;
35
+ if (typeof candidate === "string" && /^https?:\/\//i.test(candidate)) {
36
+ serviceUrl = candidate;
37
+ }
38
+ } catch {
39
+ // Older hosts may not expose magpie.service.
40
+ }
41
+
42
+ try {
43
+ const opts = await window.magpie?.options?.get?.();
44
+ const configuredPort = Number(opts?.port);
45
+ if (Number.isInteger(configuredPort) && configuredPort >= 1024 && configuredPort <= 65535) {
46
+ port = configuredPort;
47
+ }
48
+ } catch {
49
+ // Keep DEFAULT_PORT.
50
+ }
51
+
52
+ const params = new URLSearchParams();
53
+ if (theme) {
54
+ params.set("theme", theme);
55
+ }
56
+ if (lang) {
57
+ params.set("lang", lang);
58
+ }
59
+
60
+ const cleanTokens = {};
61
+ for (const [key, value] of Object.entries(tokens || {})) {
62
+ const str = String(value ?? "").trim();
63
+ if (str) {
64
+ cleanTokens[key] = str;
65
+ }
66
+ }
67
+ if (Object.keys(cleanTokens).length > 0) {
68
+ try {
69
+ const bytes = new TextEncoder().encode(JSON.stringify(cleanTokens));
70
+ let binary = "";
71
+ for (let i = 0; i < bytes.length; i += 1) {
72
+ binary += String.fromCharCode(bytes[i]);
73
+ }
74
+ params.set("tokens", btoa(binary));
75
+ } catch {
76
+ // Proceed without custom CSS tokens if encoding fails.
77
+ }
78
+ }
79
+
80
+ function armFallbackNotice(targetPort) {
81
+ setTimeout(() => {
82
+ const status = document.getElementById("status");
83
+ if (!status) {
84
+ return;
85
+ }
86
+ status.textContent = `Local Multi-Device Usage service is not responding on 127.0.0.1:${targetPort}.`;
87
+ if (document.getElementById("retry-btn")) {
88
+ return;
89
+ }
90
+ const btn = document.createElement("button");
91
+ btn.id = "retry-btn";
92
+ btn.type = "button";
93
+ btn.textContent = "Retry";
94
+ btn.addEventListener("click", () => {
95
+ location.reload();
96
+ });
97
+ status.parentNode.appendChild(btn);
98
+ }, 5000);
99
+ }
100
+
101
+ const query = params.toString();
102
+ if (serviceUrl) {
103
+ const sep = serviceUrl.includes("?") ? "&" : "?";
104
+ armFallbackNotice(port);
105
+ location.replace(query ? `${serviceUrl}${sep}${query}` : serviceUrl);
106
+ return;
107
+ }
108
+
109
+ const directTarget = query
110
+ ? `http://127.0.0.1:${port}/?${query}`
111
+ : `http://127.0.0.1:${port}/`;
112
+
113
+ if (!isLoopbackHostname(location.hostname)) {
114
+ renderPortForwardHelp(port, directTarget);
115
+ return;
116
+ }
117
+
118
+ armFallbackNotice(port);
119
+ location.replace(directTarget);
120
+ })();
121
+
122
+ function isLoopbackHostname(host) {
123
+ const clean = String(host || "").toLowerCase().replace(/^\[|\]$/g, "");
124
+ return (
125
+ clean === "" ||
126
+ clean === "localhost" ||
127
+ clean.endsWith(".localhost") ||
128
+ clean === "::1" ||
129
+ /^127\./.test(clean)
130
+ );
131
+ }
132
+
133
+ function renderPortForwardHelp(port, target) {
134
+ const status = document.getElementById("status");
135
+ if (!status) {
136
+ return;
137
+ }
138
+ const host = String(location.hostname || "").replace(/^\[|\]$/g, "") || "magpie-host";
139
+ status.textContent = "";
140
+
141
+ const addParagraph = (text) => {
142
+ const p = document.createElement("p");
143
+ p.textContent = text;
144
+ status.appendChild(p);
145
+ return p;
146
+ };
147
+
148
+ addParagraph(`This service listens strictly on 127.0.0.1:${port} on the Magpie host.`);
149
+ addParagraph("Forward the local port over SSH, then click Continue:");
150
+ const cmd = addParagraph(`ssh -N -L ${port}:127.0.0.1:${port} <user>@${host}`);
151
+ cmd.style.fontFamily = "ui-monospace, Menlo, Consolas, monospace";
152
+ cmd.style.userSelect = "all";
153
+
154
+ const btn = document.createElement("button");
155
+ btn.type = "button";
156
+ btn.textContent = "Continue";
157
+ btn.addEventListener("click", () => {
158
+ location.replace(target);
159
+ });
160
+ status.appendChild(btn);
161
+ }
package/page/icon.svg ADDED
@@ -0,0 +1,6 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round">
2
+ <rect x="2" y="2.5" width="5" height="4" rx="1"/>
3
+ <rect x="9" y="2.5" width="5" height="4" rx="1"/>
4
+ <rect x="5.5" y="9.5" width="5" height="4" rx="1"/>
5
+ <path d="M4.5 6.5v1.5h7V6.5M8 8v1.5"/>
6
+ </svg>
@@ -0,0 +1,46 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>Multi-Device Usage</title>
7
+ <style>
8
+ :root { color-scheme: light dark; }
9
+ html, body { margin: 0; height: 100%; }
10
+ body {
11
+ display: grid;
12
+ place-items: center;
13
+ font: 13px/1.5 var(--magpie-font, -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif);
14
+ background: var(--magpie-bg, Canvas);
15
+ color: var(--magpie-fg, CanvasText);
16
+ }
17
+ .box {
18
+ text-align: center;
19
+ color: var(--magpie-muted, GrayText);
20
+ max-width: 520px;
21
+ padding: 0 16px;
22
+ }
23
+ .box strong {
24
+ display: block;
25
+ color: var(--magpie-fg, CanvasText);
26
+ font-size: 14px;
27
+ margin-bottom: 4px;
28
+ }
29
+ .box p { margin: 6px 0; }
30
+ .box button {
31
+ margin-top: 8px;
32
+ font: inherit;
33
+ padding: 4px 14px;
34
+ cursor: pointer;
35
+ }
36
+ </style>
37
+ </head>
38
+ <body>
39
+ <div class="box">
40
+ <strong>Multi-Device Usage</strong>
41
+ <div id="status">Connecting to local Multi-Device Usage service...</div>
42
+ </div>
43
+ <script src="_magpie/bridge.js"></script>
44
+ <script src="boot.js"></script>
45
+ </body>
46
+ </html>