-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.js
More file actions
163 lines (153 loc) · 4.21 KB
/
Copy pathendpoint.js
File metadata and controls
163 lines (153 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const fs = require('fs')
const os = require('os')
const path = require('path')
const axios = require('axios')
const DEFAULT_PROTOCOL = 'http'
const DEFAULT_HOST = '127.0.0.1'
const DEFAULT_PORT = 42000
const DEFAULT_BASE_URL = `${DEFAULT_PROTOCOL}://${DEFAULT_HOST}:${DEFAULT_PORT}`
const CONFIG_PATH = path.resolve(os.homedir(), '.pinokio', 'config.json')
let resolvedHttpBaseUrlPromise = null
function formatHostForUrl(host) {
if (!host) {
return ''
}
const normalized = String(host).trim().replace(/^\[|\]$/g, '')
if (!normalized) {
return ''
}
return normalized.includes(':') ? `[${normalized}]` : normalized
}
function normalizeBaseUrl(value) {
if (typeof value !== 'string') {
return null
}
const trimmed = value.trim()
if (!trimmed) {
return null
}
try {
const url = new URL(trimmed)
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
return null
}
return `${url.protocol}//${url.host}`
} catch (error) {
return null
}
}
function buildExplicitHttpBaseUrl(target) {
if (!target) {
return null
}
if (typeof target === 'string') {
const normalized = normalizeBaseUrl(target)
if (normalized) {
return normalized
}
const host = formatHostForUrl(target)
if (!host) {
return null
}
return normalizeBaseUrl(`${DEFAULT_PROTOCOL}://${host}:${DEFAULT_PORT}`)
}
if (typeof target !== 'object') {
return null
}
const protocol = target.protocol === 'https' ? 'https' : DEFAULT_PROTOCOL
const host = formatHostForUrl(target.host)
if (!host) {
return null
}
const rawPort = Number.parseInt(String(target.port), 10)
const port = Number.isFinite(rawPort) && rawPort > 0 ? rawPort : DEFAULT_PORT
return normalizeBaseUrl(`${protocol}://${host}:${port}`)
}
function readStoredAccessBaseUrl() {
try {
const raw = fs.readFileSync(CONFIG_PATH, 'utf8')
const parsed = JSON.parse(raw)
const access = parsed && parsed.access
if (!access) {
return null
}
if (typeof access === 'string') {
const value = access.trim()
if (!value) {
return null
}
if (/^https?:\/\//i.test(value)) {
return normalizeBaseUrl(value)
}
if (/^\[.*\](?::\d+)?$/.test(value) || /:\d+$/.test(value)) {
return normalizeBaseUrl(`${DEFAULT_PROTOCOL}://${value}`)
}
return normalizeBaseUrl(`${DEFAULT_PROTOCOL}://${formatHostForUrl(value)}:${DEFAULT_PORT}`)
}
if (typeof access !== 'object') {
return null
}
const protocol = access.protocol === 'https' ? 'https' : DEFAULT_PROTOCOL
const host = formatHostForUrl(access.host)
if (!host) {
return null
}
const rawPort = Number.parseInt(String(access.port), 10)
const port = Number.isFinite(rawPort) && rawPort > 0 ? rawPort : DEFAULT_PORT
return normalizeBaseUrl(`${protocol}://${host}:${port}`)
} catch (error) {
return null
}
}
async function canReachControlPlane(baseUrl) {
try {
await axios.get(`${baseUrl}/pinokio/version`, {
timeout: 1000,
headers: {
'x-pinokio-client': 'pterm'
},
validateStatus: () => true
})
return true
} catch (error) {
return false
}
}
async function resolveHttpBaseUrl(target) {
const explicitBaseUrl = buildExplicitHttpBaseUrl(target)
if (explicitBaseUrl) {
return explicitBaseUrl
}
if (!resolvedHttpBaseUrlPromise) {
resolvedHttpBaseUrlPromise = (async () => {
if (await canReachControlPlane(DEFAULT_BASE_URL)) {
return DEFAULT_BASE_URL
}
return readStoredAccessBaseUrl() || DEFAULT_BASE_URL
})()
}
try {
return await resolvedHttpBaseUrlPromise
} catch (error) {
resolvedHttpBaseUrlPromise = null
throw error
}
}
async function resolveWsBaseUrl(target) {
const httpBaseUrl = await resolveHttpBaseUrl(target)
const normalized = normalizeBaseUrl(httpBaseUrl)
if (!normalized) {
return `ws://${DEFAULT_HOST}:${DEFAULT_PORT}`
}
const url = new URL(normalized)
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'
return `${url.protocol}//${url.host}`
}
module.exports = {
DEFAULT_BASE_URL,
DEFAULT_PORT,
buildExplicitHttpBaseUrl,
readStoredAccessBaseUrl,
resolveHttpBaseUrl,
resolveWsBaseUrl,
}