@sparkelf/dsh-mobile-bridge-server 0.2.11 → 0.2.12
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/package.json +1 -1
- package/src/phone.ts +69 -10
- package/src/pwa.spec.ts +211 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sparkelf/dsh-mobile-bridge-server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Multi-user bridge server connecting mobile browsers to local DeepSeek Harness instances through outbound tunnels.",
|
|
6
6
|
"license": "MIT",
|
package/src/phone.ts
CHANGED
|
@@ -45,35 +45,94 @@ export const PWA_MANIFEST = JSON.stringify({
|
|
|
45
45
|
],
|
|
46
46
|
})
|
|
47
47
|
|
|
48
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* Always-visible install entry for the paired phone view: the floating button
|
|
50
|
+
* shows on every relayed page without waiting for Chrome's engagement-gated
|
|
51
|
+
* beforeinstallprompt. A captured prompt installs in one tap; otherwise the
|
|
52
|
+
* button opens per-browser manual steps (iOS share sheet / Android ⋮ menu /
|
|
53
|
+
* switch-browser advice). Installing hides the button and persists a flag so
|
|
54
|
+
* it never returns; standalone display mode is hidden from the first load.
|
|
55
|
+
*/
|
|
49
56
|
export const PWA_INSTALL_SOURCE = `/* DSH mobile bridge PWA install guide. */
|
|
50
57
|
(function () {
|
|
51
|
-
|
|
58
|
+
var INSTALLED_KEY = 'dsh-bridge-pwa-installed'
|
|
59
|
+
function isInstalled() {
|
|
60
|
+
try { if (localStorage.getItem(INSTALLED_KEY) === '1') return true } catch (err) {}
|
|
61
|
+
return window.matchMedia('(display-mode: standalone)').matches || navigator.standalone === true
|
|
62
|
+
}
|
|
63
|
+
if (isInstalled()) return
|
|
52
64
|
var deferred = null
|
|
53
65
|
var btn = null
|
|
66
|
+
function hideBtn() { if (btn) { btn.remove(); btn = null } }
|
|
54
67
|
function ensureBtn() {
|
|
55
68
|
if (btn) return btn
|
|
69
|
+
if (isInstalled()) return null
|
|
56
70
|
btn = document.createElement('button')
|
|
71
|
+
btn.id = 'dsh-pwa-install-btn'
|
|
57
72
|
btn.textContent = '安装到主屏幕'
|
|
58
73
|
btn.style.cssText = 'position:fixed;left:50%;transform:translateX(-50%);bottom:18px;z-index:99999;height:44px;padding:0 22px;border:0;border-radius:22px;background:#405de6;color:#fff;font:600 14px system-ui,sans-serif;box-shadow:0 6px 24px rgba(64,93,230,.4)'
|
|
59
74
|
btn.onclick = function () {
|
|
60
|
-
if (deferred) {
|
|
61
|
-
|
|
75
|
+
if (deferred) {
|
|
76
|
+
var promptEvent = deferred
|
|
77
|
+
deferred = null
|
|
78
|
+
promptEvent.prompt()
|
|
79
|
+
promptEvent.userChoice.then(function (choice) { if (choice && choice.outcome === 'accepted') hideBtn() })
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
showGuide()
|
|
62
83
|
}
|
|
63
84
|
document.body.appendChild(btn)
|
|
64
85
|
return btn
|
|
65
86
|
}
|
|
66
|
-
function
|
|
87
|
+
function guideKind() {
|
|
88
|
+
var ua = navigator.userAgent || ''
|
|
89
|
+
if (/iphone|ipad|ipod/i.test(ua) || (/macintosh/i.test(ua) && navigator.maxTouchPoints > 1)) return 'ios'
|
|
90
|
+
if (/android/i.test(ua) && /chrome\\//i.test(ua)) return 'android'
|
|
91
|
+
return 'other'
|
|
92
|
+
}
|
|
93
|
+
function showGuide() {
|
|
94
|
+
var kind = guideKind()
|
|
95
|
+
var ua = navigator.userAgent || ''
|
|
96
|
+
var title = '安装到主屏幕'
|
|
97
|
+
var steps
|
|
98
|
+
if (kind === 'ios') {
|
|
99
|
+
title = '添加到主屏幕'
|
|
100
|
+
var safari = /safari/i.test(ua) && !/crios|fxios|edgios/i.test(ua)
|
|
101
|
+
steps = (safari ? '' : '请先在 Safari 中打开本页,然后:\\n') + '1. 点 Safari 底部「分享」按钮(方框向上箭头)\\n2. 选择「添加到主屏幕」\\n3. 点右上角「添加」,即可像 App 一样打开'
|
|
102
|
+
} else if (kind === 'android') {
|
|
103
|
+
title = '安装应用'
|
|
104
|
+
steps = '1. 点浏览器右上角 ⋮(三个点)菜单\\n2. 选择「安装应用」或「添加到主屏幕」\\n3. 确认安装,即可像 App 一样打开'
|
|
105
|
+
} else {
|
|
106
|
+
steps = '当前浏览器暂不支持一键安装。\\n请用 Chrome(Android)或 Safari(iPhone/iPad)打开本页,\\n再点「安装到主屏幕」按钮。'
|
|
107
|
+
}
|
|
67
108
|
var ov = document.createElement('div')
|
|
109
|
+
ov.id = 'dsh-pwa-guide'
|
|
68
110
|
ov.style.cssText = 'position:fixed;inset:0;z-index:100000;background:rgba(13,16,21,.92);color:#f4f7fb;display:grid;place-items:center;font:14px/1.7 system-ui,sans-serif;text-align:center;padding:24px'
|
|
69
|
-
|
|
70
|
-
|
|
111
|
+
var card = document.createElement('div')
|
|
112
|
+
var heading = document.createElement('p')
|
|
113
|
+
heading.style.cssText = 'font-size:16px;font-weight:650;margin:0 0 10px'
|
|
114
|
+
heading.textContent = title
|
|
115
|
+
var body = document.createElement('p')
|
|
116
|
+
body.style.cssText = 'margin:0;white-space:pre-line'
|
|
117
|
+
body.textContent = steps
|
|
118
|
+
var ok = document.createElement('button')
|
|
119
|
+
ok.textContent = '知道了'
|
|
120
|
+
ok.style.cssText = 'margin-top:18px;height:40px;padding:0 20px;border:0;border-radius:20px;background:#405de6;color:#fff;font:600 14px system-ui,sans-serif'
|
|
121
|
+
ok.onclick = function () { ov.remove() }
|
|
122
|
+
card.appendChild(heading)
|
|
123
|
+
card.appendChild(body)
|
|
124
|
+
card.appendChild(ok)
|
|
125
|
+
ov.appendChild(card)
|
|
71
126
|
document.body.appendChild(ov)
|
|
72
127
|
}
|
|
73
128
|
window.addEventListener('beforeinstallprompt', function (e) { e.preventDefault(); deferred = e; ensureBtn() })
|
|
74
|
-
window.addEventListener('appinstalled', function () {
|
|
75
|
-
|
|
76
|
-
|
|
129
|
+
window.addEventListener('appinstalled', function () {
|
|
130
|
+
try { localStorage.setItem(INSTALLED_KEY, '1') } catch (err) {}
|
|
131
|
+
deferred = null
|
|
132
|
+
hideBtn()
|
|
133
|
+
})
|
|
134
|
+
if (document.body) ensureBtn()
|
|
135
|
+
else document.addEventListener('DOMContentLoaded', function () { ensureBtn() })
|
|
77
136
|
})()
|
|
78
137
|
`
|
|
79
138
|
|
package/src/pwa.spec.ts
CHANGED
|
@@ -5,8 +5,8 @@ import { join } from 'node:path'
|
|
|
5
5
|
import type { AddressInfo } from 'node:net'
|
|
6
6
|
import { createBridgeServer } from './server.ts'
|
|
7
7
|
import { UserStore } from './store.ts'
|
|
8
|
-
import { CLIENT_SOURCE, PWA_HEAD, SW_SOURCE } from './phone.ts'
|
|
9
|
-
import { Script } from 'node:vm'
|
|
8
|
+
import { CLIENT_SOURCE, PWA_HEAD, PWA_INSTALL_SOURCE, SW_SOURCE } from './phone.ts'
|
|
9
|
+
import { Script, createContext } from 'node:vm'
|
|
10
10
|
|
|
11
11
|
const open: Array<{ close: () => void }> = []
|
|
12
12
|
afterEach(() => { for (const entry of open.splice(0)) entry.close() })
|
|
@@ -73,3 +73,212 @@ describe('phone PWA install surface', () => {
|
|
|
73
73
|
expect(() => new Script(SW_SOURCE)).not.toThrow()
|
|
74
74
|
})
|
|
75
75
|
})
|
|
76
|
+
|
|
77
|
+
/** Minimal DOM element stub: only what the install guide touches. */
|
|
78
|
+
type StubElement = {
|
|
79
|
+
tagName: string
|
|
80
|
+
id: string
|
|
81
|
+
textContent: string
|
|
82
|
+
style: { cssText: string }
|
|
83
|
+
children: StubElement[]
|
|
84
|
+
parent: StubElement | null
|
|
85
|
+
onclick: (() => void) | null
|
|
86
|
+
appendChild(child: StubElement): StubElement
|
|
87
|
+
remove(): void
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function makeElement(tagName: string): StubElement {
|
|
91
|
+
const el: StubElement = {
|
|
92
|
+
tagName,
|
|
93
|
+
id: '',
|
|
94
|
+
textContent: '',
|
|
95
|
+
style: { cssText: '' },
|
|
96
|
+
children: [],
|
|
97
|
+
parent: null,
|
|
98
|
+
onclick: null,
|
|
99
|
+
appendChild(child) { child.parent = el; el.children.push(child); return child },
|
|
100
|
+
remove() {
|
|
101
|
+
if (el.parent) el.parent.children = el.parent.children.filter(child => child !== el)
|
|
102
|
+
el.parent = null
|
|
103
|
+
},
|
|
104
|
+
}
|
|
105
|
+
return el
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const UA = {
|
|
109
|
+
androidChrome: 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36',
|
|
110
|
+
iphoneSafari: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1',
|
|
111
|
+
ipadMac: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15',
|
|
112
|
+
iphoneChrome: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/126.0.6478.54 Mobile/15E148 Safari/604.1',
|
|
113
|
+
desktopFirefox: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0',
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Run the real install guide in a vm with a stub DOM; returns observation handles. */
|
|
117
|
+
function runInstallGuide(opts: { ua: string; standalone?: boolean; iosStandalone?: boolean; installed?: boolean; touchPoints?: number; bodyReady?: boolean }) {
|
|
118
|
+
const windowListeners = new Map<string, Array<(event: unknown) => void>>()
|
|
119
|
+
const docListeners = new Map<string, Array<() => void>>()
|
|
120
|
+
const storage = new Map<string, string>()
|
|
121
|
+
if (opts.installed) storage.set('dsh-bridge-pwa-installed', '1')
|
|
122
|
+
const body = makeElement('body')
|
|
123
|
+
const doc = {
|
|
124
|
+
body: opts.bodyReady === false ? null : body,
|
|
125
|
+
createElement: makeElement,
|
|
126
|
+
addEventListener: (name: string, fn: () => void) => {
|
|
127
|
+
const list = docListeners.get(name) ?? []
|
|
128
|
+
list.push(fn)
|
|
129
|
+
docListeners.set(name, list)
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
const sandbox = {
|
|
133
|
+
window: {
|
|
134
|
+
matchMedia: (query: string) => ({ matches: query === '(display-mode: standalone)' && opts.standalone === true }),
|
|
135
|
+
addEventListener: (name: string, fn: (event: unknown) => void) => {
|
|
136
|
+
const list = windowListeners.get(name) ?? []
|
|
137
|
+
list.push(fn)
|
|
138
|
+
windowListeners.set(name, list)
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
navigator: { userAgent: opts.ua, standalone: opts.iosStandalone === true ? true : undefined, maxTouchPoints: opts.touchPoints ?? 0 },
|
|
142
|
+
localStorage: {
|
|
143
|
+
getItem: (key: string) => storage.get(key) ?? null,
|
|
144
|
+
setItem: (key: string, value: string) => { storage.set(key, value) },
|
|
145
|
+
},
|
|
146
|
+
document: doc,
|
|
147
|
+
}
|
|
148
|
+
createContext(sandbox)
|
|
149
|
+
new Script(PWA_INSTALL_SOURCE).runInContext(sandbox)
|
|
150
|
+
const textOf = (el: StubElement): string => el.textContent + el.children.map(textOf).join('')
|
|
151
|
+
return {
|
|
152
|
+
body,
|
|
153
|
+
storage,
|
|
154
|
+
fire: (name: string, event: unknown) => { for (const fn of windowListeners.get(name) ?? []) fn(event) },
|
|
155
|
+
domReady: () => { doc.body = body; for (const fn of docListeners.get('DOMContentLoaded') ?? []) fn() },
|
|
156
|
+
findBtn: () => body.children.find(child => child.id === 'dsh-pwa-install-btn'),
|
|
157
|
+
findGuide: () => body.children.find(child => child.id === 'dsh-pwa-guide'),
|
|
158
|
+
guideText: () => { const guide = body.children.find(child => child.id === 'dsh-pwa-guide'); return guide ? textOf(guide) : '' },
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const flush = () => new Promise(resolve => setTimeout(resolve, 0))
|
|
163
|
+
|
|
164
|
+
describe('PWA install entry behavior (vm)', () => {
|
|
165
|
+
it('shows the floating button on load without any beforeinstallprompt', () => {
|
|
166
|
+
const guide = runInstallGuide({ ua: UA.androidChrome })
|
|
167
|
+
const btn = guide.findBtn()
|
|
168
|
+
expect(btn).toBeDefined()
|
|
169
|
+
expect(btn?.textContent).toBe('安装到主屏幕')
|
|
170
|
+
expect(btn?.style.cssText).toContain('position:fixed')
|
|
171
|
+
expect(guide.findGuide()).toBeUndefined()
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('defers the button to DOMContentLoaded when injected into <head> before body exists', () => {
|
|
175
|
+
const guide = runInstallGuide({ ua: UA.androidChrome, bodyReady: false })
|
|
176
|
+
expect(guide.findBtn()).toBeUndefined()
|
|
177
|
+
guide.domReady()
|
|
178
|
+
expect(guide.findBtn()).toBeDefined()
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('click without a deferred prompt opens the iOS Safari share-sheet steps', () => {
|
|
182
|
+
const guide = runInstallGuide({ ua: UA.iphoneSafari })
|
|
183
|
+
guide.findBtn()?.onclick?.()
|
|
184
|
+
const text = guide.guideText()
|
|
185
|
+
expect(text).toContain('添加到主屏幕')
|
|
186
|
+
expect(text).toContain('分享')
|
|
187
|
+
expect(text).not.toContain('请先在 Safari 中打开本页')
|
|
188
|
+
expect(guide.findGuide()?.style.cssText).toContain('position:fixed')
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('treats iPadOS masquerading as Macintosh as iOS', () => {
|
|
192
|
+
const guide = runInstallGuide({ ua: UA.ipadMac, touchPoints: 5 })
|
|
193
|
+
guide.findBtn()?.onclick?.()
|
|
194
|
+
expect(guide.guideText()).toContain('分享')
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('tells iOS non-Safari browsers to open Safari first', () => {
|
|
198
|
+
const guide = runInstallGuide({ ua: UA.iphoneChrome })
|
|
199
|
+
guide.findBtn()?.onclick?.()
|
|
200
|
+
expect(guide.guideText()).toContain('请先在 Safari 中打开本页')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
it('click without a deferred prompt opens the Android Chrome ⋮ menu steps', () => {
|
|
204
|
+
const guide = runInstallGuide({ ua: UA.androidChrome })
|
|
205
|
+
guide.findBtn()?.onclick?.()
|
|
206
|
+
const text = guide.guideText()
|
|
207
|
+
expect(text).toContain('安装应用')
|
|
208
|
+
expect(text).toContain('⋮')
|
|
209
|
+
expect(text).toContain('添加到主屏幕')
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
it('other browsers get the switch-to-Chrome/Safari guidance', () => {
|
|
213
|
+
const guide = runInstallGuide({ ua: UA.desktopFirefox })
|
|
214
|
+
guide.findBtn()?.onclick?.()
|
|
215
|
+
const text = guide.guideText()
|
|
216
|
+
expect(text).toContain('Chrome')
|
|
217
|
+
expect(text).toContain('Safari')
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
it('知道了 closes the guidance overlay', () => {
|
|
221
|
+
const guide = runInstallGuide({ ua: UA.desktopFirefox })
|
|
222
|
+
guide.findBtn()?.onclick?.()
|
|
223
|
+
const overlay = guide.findGuide()
|
|
224
|
+
expect(overlay).toBeDefined()
|
|
225
|
+
const ok = overlay?.children[0]?.children.find(child => child.tagName === 'button')
|
|
226
|
+
ok?.onclick?.()
|
|
227
|
+
expect(guide.findGuide()).toBeUndefined()
|
|
228
|
+
expect(guide.findBtn()).toBeDefined()
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('standalone display mode hides the entry entirely', () => {
|
|
232
|
+
const guide = runInstallGuide({ ua: UA.androidChrome, standalone: true })
|
|
233
|
+
expect(guide.findBtn()).toBeUndefined()
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('iOS navigator.standalone hides the entry entirely', () => {
|
|
237
|
+
const guide = runInstallGuide({ ua: UA.iphoneSafari, iosStandalone: true })
|
|
238
|
+
expect(guide.findBtn()).toBeUndefined()
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
it('a persisted installed flag hides the entry on later loads', () => {
|
|
242
|
+
const guide = runInstallGuide({ ua: UA.androidChrome, installed: true })
|
|
243
|
+
expect(guide.findBtn()).toBeUndefined()
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
it('a captured beforeinstallprompt installs in one tap without the overlay', async () => {
|
|
247
|
+
const guide = runInstallGuide({ ua: UA.androidChrome })
|
|
248
|
+
let prompted = 0
|
|
249
|
+
let prevented = 0
|
|
250
|
+
guide.fire('beforeinstallprompt', {
|
|
251
|
+
preventDefault: () => { prevented += 1 },
|
|
252
|
+
prompt: () => { prompted += 1 },
|
|
253
|
+
userChoice: Promise.resolve({ outcome: 'accepted' }),
|
|
254
|
+
})
|
|
255
|
+
expect(prevented).toBe(1)
|
|
256
|
+
guide.findBtn()?.onclick?.()
|
|
257
|
+
expect(prompted).toBe(1)
|
|
258
|
+
expect(guide.findGuide()).toBeUndefined()
|
|
259
|
+
await flush()
|
|
260
|
+
expect(guide.findBtn()).toBeUndefined()
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
it('a dismissed prompt keeps the button for a retry', async () => {
|
|
264
|
+
const guide = runInstallGuide({ ua: UA.androidChrome })
|
|
265
|
+
guide.fire('beforeinstallprompt', {
|
|
266
|
+
preventDefault: () => {},
|
|
267
|
+
prompt: () => {},
|
|
268
|
+
userChoice: Promise.resolve({ outcome: 'dismissed' }),
|
|
269
|
+
})
|
|
270
|
+
guide.findBtn()?.onclick?.()
|
|
271
|
+
await flush()
|
|
272
|
+
expect(guide.findBtn()).toBeDefined()
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
it('appinstalled hides the button and persists the installed flag', () => {
|
|
276
|
+
const guide = runInstallGuide({ ua: UA.androidChrome })
|
|
277
|
+
expect(guide.findBtn()).toBeDefined()
|
|
278
|
+
guide.fire('appinstalled', undefined)
|
|
279
|
+
expect(guide.findBtn()).toBeUndefined()
|
|
280
|
+
expect(guide.storage.get('dsh-bridge-pwa-installed')).toBe('1')
|
|
281
|
+
const reload = runInstallGuide({ ua: UA.androidChrome, installed: true })
|
|
282
|
+
expect(reload.findBtn()).toBeUndefined()
|
|
283
|
+
})
|
|
284
|
+
})
|