@sparkelf/dsh-plugin-mobile-gateway 0.8.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/LICENSE +21 -0
- package/PROTOCOL.md +1180 -0
- package/README.md +281 -0
- package/bin/setup-ip.mjs +474 -0
- package/cordis.patch.yml +44 -0
- package/docs/assets/mobile-device-management.png +0 -0
- package/docs/assets/public-access-ui.png +0 -0
- package/docs/assets/whale-girl-ios-app-promo-16x9.png +0 -0
- package/docs/blog-mobile-gateway.md +542 -0
- package/docs/dsh-0.1.5-rc.2-compatibility-audit.md +255 -0
- package/docs/dsh-0.1.6-alpha.1-compatibility-audit.md +122 -0
- package/docs/dsh-rc2-mobile-integration.md +155 -0
- package/docs/multi-gateway-app-integration.md +124 -0
- package/docs/multi-gateway-phase1-acceptance.md +179 -0
- package/docs/multi-gateway-todo.md +137 -0
- package/docs/remote-gateway-refactor-plan.md +52 -0
- package/docs/runtime-architecture.architecture.json +264 -0
- package/docs/runtime-architecture.html +15001 -0
- package/docs/runtime-architecture.visual-check.html +32 -0
- package/docs/runtime-architecture.visual-check.json +548 -0
- package/docs/session-agent-preset-app-integration.md +93 -0
- package/docs/typert-remote-gateway-feature-checklist.md +294 -0
- package/helper/dsh_mobile_gateway_helper.py +227 -0
- package/lib/client.js +520 -0
- package/lib/devices.js +209 -0
- package/lib/dsh-host-adapter.mjs +466 -0
- package/lib/gateway-state.mjs +57 -0
- package/lib/index.mjs +3197 -0
- package/lib/session-follower.mjs +136 -0
- package/lib/wire-json.mjs +7 -0
- package/package.json +50 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import crypto from 'node:crypto'
|
|
4
|
+
|
|
5
|
+
export const GATEWAY_MODES = ['disabled', 'temporary', 'persistent']
|
|
6
|
+
|
|
7
|
+
// A dedicated file keeps installation identity independent of device revocation.
|
|
8
|
+
// A malformed file must fail startup rather than silently replace that identity.
|
|
9
|
+
export function createGatewayState(file) {
|
|
10
|
+
const validate = (value) => {
|
|
11
|
+
if (!value || value.version !== 1 ||
|
|
12
|
+
typeof value.gatewayId !== 'string' ||
|
|
13
|
+
!/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value.gatewayId) ||
|
|
14
|
+
(value.mode !== null && !GATEWAY_MODES.includes(value.mode))) {
|
|
15
|
+
throw new Error('invalid gateway identity or mode')
|
|
16
|
+
}
|
|
17
|
+
return value
|
|
18
|
+
}
|
|
19
|
+
const read = () => validate(JSON.parse(fs.readFileSync(file, 'utf8')))
|
|
20
|
+
const write = (value, initial = false) => {
|
|
21
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
|
|
22
|
+
const tmp = `${file}.${process.pid}.${crypto.randomUUID()}.tmp`
|
|
23
|
+
try {
|
|
24
|
+
fs.writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600, flag: 'wx' })
|
|
25
|
+
if (initial) fs.linkSync(tmp, file) // Never overwrite a concurrently created identity.
|
|
26
|
+
else fs.renameSync(tmp, file)
|
|
27
|
+
} finally {
|
|
28
|
+
fs.rmSync(tmp, { force: true })
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
let state
|
|
32
|
+
try {
|
|
33
|
+
try {
|
|
34
|
+
state = read()
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (error.code !== 'ENOENT') throw error
|
|
37
|
+
state = { version: 1, gatewayId: crypto.randomUUID(), mode: null }
|
|
38
|
+
try { write(state, true) } catch (cause) {
|
|
39
|
+
if (cause.code !== 'EEXIST') throw cause
|
|
40
|
+
state = read()
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
fs.chmodSync(file, 0o600)
|
|
44
|
+
} catch (error) {
|
|
45
|
+
throw new Error(`failed to load gateway state ${file}: ${error.message}`, { cause: error })
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
get gatewayId() { return state.gatewayId },
|
|
49
|
+
get mode() { return state.mode },
|
|
50
|
+
setMode(mode) {
|
|
51
|
+
if (!GATEWAY_MODES.includes(mode)) throw new TypeError('invalid gateway mode')
|
|
52
|
+
const next = { ...state, mode }
|
|
53
|
+
write(next)
|
|
54
|
+
state = next
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
}
|