dsh-multi-device-config-sync 0.0.0-stage → 1.6.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/README.md +105 -2
- package/assets/icon-256.png +0 -0
- package/client.js +1658 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +19 -0
- package/index.js +52 -0
- package/lib/backup.js +1087 -0
- package/lib/errors.js +67 -0
- package/lib/gist.js +343 -0
- package/lib/plugins.js +490 -0
- package/lib/routes.js +507 -0
- package/lib/store.js +73 -0
- package/lib/sync-install.js +231 -0
- package/lib/tool.js +191 -0
- package/locale/en.json +24 -0
- package/locale/zh.json +24 -0
- package/package.json +66 -4
- package/test/activation.test.mjs +141 -0
- package/test/client-decisions.test.mjs +55 -0
- package/test/client-registration.test.mjs +98 -0
- package/test/config-preview.test.mjs +454 -0
- package/test/first-run.test.mjs +104 -0
- package/test/harness.mjs +176 -0
- package/test/library.test.mjs +169 -0
- package/test/package.test.mjs +165 -0
- package/test/plugins.test.mjs +543 -0
- package/test/progress-client.test.mjs +225 -0
- package/test/progress-engine.test.mjs +235 -0
- package/test/progress-http.test.mjs +340 -0
- package/test/rename-compat.test.mjs +255 -0
- package/test/run.mjs +32 -0
- package/test/sync-install.test.mjs +338 -0
package/lib/errors.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One error type for every config-sync failure, carrying a machine-readable
|
|
3
|
+
* `code` so the browser half can show a localized message instead of raw
|
|
4
|
+
* Node/DOM error noise.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Machine-readable failure codes shared by the Host routes and the client. */
|
|
8
|
+
export const ERROR_CODES = [
|
|
9
|
+
'auth',
|
|
10
|
+
'notfound',
|
|
11
|
+
'rate-limit',
|
|
12
|
+
'invalid',
|
|
13
|
+
'too-large',
|
|
14
|
+
'timeout',
|
|
15
|
+
'network',
|
|
16
|
+
'no-profile',
|
|
17
|
+
'no-cloud',
|
|
18
|
+
'unsafe-path',
|
|
19
|
+
'nothing-to-undo',
|
|
20
|
+
'stale-plan',
|
|
21
|
+
'busy',
|
|
22
|
+
'other',
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
/** HTTP status the route answers with for each code. */
|
|
26
|
+
const STATUS_BY_CODE = {
|
|
27
|
+
auth: 401,
|
|
28
|
+
notfound: 404,
|
|
29
|
+
'rate-limit': 429,
|
|
30
|
+
invalid: 400,
|
|
31
|
+
'too-large': 413,
|
|
32
|
+
timeout: 504,
|
|
33
|
+
network: 502,
|
|
34
|
+
'no-profile': 500,
|
|
35
|
+
'no-cloud': 400,
|
|
36
|
+
'unsafe-path': 400,
|
|
37
|
+
'nothing-to-undo': 404,
|
|
38
|
+
'stale-plan': 409,
|
|
39
|
+
busy: 409,
|
|
40
|
+
other: 500,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A failure with a stable code; `message` stays human-readable. */
|
|
44
|
+
export class ConfigSyncError extends Error {
|
|
45
|
+
/**
|
|
46
|
+
* @param {string} message - human-readable reason, safe to show.
|
|
47
|
+
* @param {string} code - one of {@link ERROR_CODES}.
|
|
48
|
+
*/
|
|
49
|
+
constructor(message, code = 'other') {
|
|
50
|
+
super(message)
|
|
51
|
+
this.name = 'ConfigSyncError'
|
|
52
|
+
this.code = ERROR_CODES.includes(code) ? code : 'other'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** HTTP status for a thrown value. */
|
|
57
|
+
export function statusOf(error) {
|
|
58
|
+
if (error instanceof ConfigSyncError) return STATUS_BY_CODE[error.code] ?? 500
|
|
59
|
+
return 500
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Normalize any thrown value into `{ error, code }` for a JSON response. */
|
|
63
|
+
export function describeError(error) {
|
|
64
|
+
if (error instanceof ConfigSyncError) return { error: error.message, code: error.code }
|
|
65
|
+
if (error instanceof Error) return { error: error.message, code: 'other' }
|
|
66
|
+
return { error: String(error), code: 'other' }
|
|
67
|
+
}
|
package/lib/gist.js
ADDED
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub Gist transport for profile backups.
|
|
3
|
+
*
|
|
4
|
+
* Security posture:
|
|
5
|
+
* - The API host is hard-coded to api.github.com, so there is no SSRF surface.
|
|
6
|
+
* - A Gist id (or a gist.github.com URL) is validated against a strict
|
|
7
|
+
* character allowlist before it is interpolated into the request path.
|
|
8
|
+
* - Tokens are never written to disk by this plugin. The browser may send one
|
|
9
|
+
* per request (session memory only); otherwise the host reads
|
|
10
|
+
* `DSH_GITHUB_TOKEN` / `GITHUB_TOKEN`, or asks an already-signed-in `gh` CLI
|
|
11
|
+
* for its token.
|
|
12
|
+
* - Content read from a Gist goes through `validateBackup` before anything is
|
|
13
|
+
* written into the profile.
|
|
14
|
+
*
|
|
15
|
+
* Note the honest limit of a Gist: GitHub has no private Gist. A "secret" Gist
|
|
16
|
+
* is unlisted, not unreadable — anyone holding the link can read it.
|
|
17
|
+
*/
|
|
18
|
+
import { spawn } from 'node:child_process'
|
|
19
|
+
import { homedir } from 'node:os'
|
|
20
|
+
import { join } from 'node:path'
|
|
21
|
+
import { BACKUP_FILENAME, GIST_MAX_BYTES, validateBackup } from './backup.js'
|
|
22
|
+
import { ConfigSyncError } from './errors.js'
|
|
23
|
+
|
|
24
|
+
const API_ORIGIN = 'https://api.github.com'
|
|
25
|
+
const GIST_ID_RE = /^[A-Za-z0-9_-]{1,64}$/
|
|
26
|
+
const REQUEST_TIMEOUT_MS = 30_000
|
|
27
|
+
const MAX_RESPONSE_BYTES = GIST_MAX_BYTES + 64 * 1024
|
|
28
|
+
const TOKEN_ENV_NAMES = ['DSH_GITHUB_TOKEN', 'GITHUB_TOKEN']
|
|
29
|
+
const NETWORK_ERROR_CODES = new Set([
|
|
30
|
+
'ENOTFOUND', 'EAI_AGAIN', 'ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT',
|
|
31
|
+
'EPIPE', 'EHOSTUNREACH', 'ENETUNREACH', 'ECONNABORTED', 'UND_ERR_SOCKET',
|
|
32
|
+
])
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Normalize a Gist id or gist.github.com URL to a bare id.
|
|
36
|
+
* @param {string} input - the raw user input.
|
|
37
|
+
* @returns {string} the validated id.
|
|
38
|
+
*/
|
|
39
|
+
export function parseGistId(input) {
|
|
40
|
+
const trimmed = String(input ?? '').trim()
|
|
41
|
+
if (trimmed === '') throw new ConfigSyncError('a Gist id or link is required', 'invalid')
|
|
42
|
+
let candidate = trimmed
|
|
43
|
+
try {
|
|
44
|
+
const url = new URL(trimmed)
|
|
45
|
+
if (url.protocol === 'https:' && (url.hostname === 'gist.github.com' || url.hostname.endsWith('.gist.github.com'))) {
|
|
46
|
+
const parts = url.pathname.split('/').filter(Boolean)
|
|
47
|
+
candidate = parts[parts.length - 1] ?? ''
|
|
48
|
+
}
|
|
49
|
+
} catch {
|
|
50
|
+
// Not a URL: the allowlist below decides.
|
|
51
|
+
}
|
|
52
|
+
if (!GIST_ID_RE.test(candidate)) {
|
|
53
|
+
throw new ConfigSyncError(`not a Gist id or link: ${trimmed}`, 'invalid')
|
|
54
|
+
}
|
|
55
|
+
return candidate
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Which token source answered, so the UI can say where it came from. */
|
|
59
|
+
export function tokenSourceNames() {
|
|
60
|
+
const found = []
|
|
61
|
+
for (const name of TOKEN_ENV_NAMES) {
|
|
62
|
+
const value = process.env[name]
|
|
63
|
+
if (typeof value === 'string' && value.trim() !== '') found.push(name)
|
|
64
|
+
}
|
|
65
|
+
return found
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Resolve the token for one request: an explicitly supplied one, then the
|
|
70
|
+
* host's environment, then an already-signed-in `gh` CLI.
|
|
71
|
+
*
|
|
72
|
+
* The failure is the first thing a new machine hits, so it carries the three
|
|
73
|
+
* ways out and the scope the token needs, in that order. Vague wording here
|
|
74
|
+
* ("auth failed") sends the reader to the GitHub docs without telling them which
|
|
75
|
+
* of the three sources to fix.
|
|
76
|
+
* @param {unknown} bodyToken - token supplied with the request, if any.
|
|
77
|
+
* @returns {Promise<{ token: string, source: 'request' | 'env' | 'gh' }>}
|
|
78
|
+
*/
|
|
79
|
+
export async function resolveTokenSource(bodyToken) {
|
|
80
|
+
if (typeof bodyToken === 'string' && bodyToken.trim() !== '') {
|
|
81
|
+
return { token: bodyToken.trim(), source: 'request' }
|
|
82
|
+
}
|
|
83
|
+
for (const name of TOKEN_ENV_NAMES) {
|
|
84
|
+
const value = process.env[name]
|
|
85
|
+
if (typeof value === 'string' && value.trim() !== '') return { token: value.trim(), source: 'env' }
|
|
86
|
+
}
|
|
87
|
+
const fromGh = await ghAuthToken()
|
|
88
|
+
if (fromGh !== null) return { token: fromGh, source: 'gh' }
|
|
89
|
+
throw new ConfigSyncError(`No GitHub token is available.\n${TOKEN_HELP}`, 'auth')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let ghTokenCache = null
|
|
93
|
+
|
|
94
|
+
/** Ask an already-authenticated GitHub CLI for its token, if it has one. */
|
|
95
|
+
async function ghAuthToken() {
|
|
96
|
+
if (ghTokenCache !== null && Date.now() < ghTokenCache.expires) return ghTokenCache.token
|
|
97
|
+
const token = await runGhAuthToken()
|
|
98
|
+
ghTokenCache = { token, expires: Date.now() + (token !== null ? 10 * 60_000 : 30_000) }
|
|
99
|
+
return token
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Run `gh auth token` in a detached child and give up after 8 s. The parent
|
|
104
|
+
* never waits on the child, so a wedged gh cannot block the host's event loop.
|
|
105
|
+
* @returns {Promise<string | null>}
|
|
106
|
+
*/
|
|
107
|
+
function runGhAuthToken() {
|
|
108
|
+
return new Promise((resolveToken) => {
|
|
109
|
+
const candidates = ['gh', join(homedir(), '.local', 'bin', 'gh')]
|
|
110
|
+
let settled = false
|
|
111
|
+
const finish = (value) => {
|
|
112
|
+
if (settled) return
|
|
113
|
+
settled = true
|
|
114
|
+
resolveToken(value)
|
|
115
|
+
}
|
|
116
|
+
const tryNext = (index) => {
|
|
117
|
+
if (index >= candidates.length) {
|
|
118
|
+
finish(null)
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
let child = null
|
|
122
|
+
try {
|
|
123
|
+
child = spawn(candidates[index], ['auth', 'token'], {
|
|
124
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
125
|
+
detached: true,
|
|
126
|
+
windowsHide: true,
|
|
127
|
+
})
|
|
128
|
+
} catch {
|
|
129
|
+
tryNext(index + 1)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
if (child == null) {
|
|
133
|
+
tryNext(index + 1)
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
let out = ''
|
|
137
|
+
child.stdout?.on('data', (chunk) => { out += chunk.toString() })
|
|
138
|
+
const timer = setTimeout(() => {
|
|
139
|
+
try { child?.kill('SIGKILL') } catch { /* already gone */ }
|
|
140
|
+
finish(out.trim() !== '' ? out.trim() : null)
|
|
141
|
+
}, 8_000)
|
|
142
|
+
let errored = false
|
|
143
|
+
child.on('error', () => {
|
|
144
|
+
if (errored) return
|
|
145
|
+
errored = true
|
|
146
|
+
clearTimeout(timer)
|
|
147
|
+
if (!settled) tryNext(index + 1)
|
|
148
|
+
})
|
|
149
|
+
child.on('close', () => {
|
|
150
|
+
if (errored) return
|
|
151
|
+
clearTimeout(timer)
|
|
152
|
+
finish(out.trim() !== '' ? out.trim() : null)
|
|
153
|
+
})
|
|
154
|
+
child.unref()
|
|
155
|
+
}
|
|
156
|
+
tryNext(0)
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Map a transport-level failure (abort or network error) to a coded error. */
|
|
161
|
+
function classifyRequestError(error) {
|
|
162
|
+
if (error instanceof ConfigSyncError) return error
|
|
163
|
+
if (error instanceof Error && (error.name === 'TimeoutError' || error.name === 'AbortError')) {
|
|
164
|
+
return new ConfigSyncError('the GitHub request timed out', 'timeout')
|
|
165
|
+
}
|
|
166
|
+
const code = error?.code ?? error?.cause?.code
|
|
167
|
+
if (typeof code === 'string' && NETWORK_ERROR_CODES.has(code)) {
|
|
168
|
+
return new ConfigSyncError(`GitHub is unreachable (${code})`, 'network')
|
|
169
|
+
}
|
|
170
|
+
return new ConfigSyncError(error instanceof Error ? error.message : String(error), 'network')
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** One GitHub API call. */
|
|
174
|
+
async function githubRequest(token, method, path, body, signal) {
|
|
175
|
+
const headers = {
|
|
176
|
+
authorization: `Bearer ${token}`,
|
|
177
|
+
accept: 'application/vnd.github+json',
|
|
178
|
+
'user-agent': 'dsh-multi-device-config-sync',
|
|
179
|
+
'x-github-api-version': '2022-11-28',
|
|
180
|
+
}
|
|
181
|
+
if (body !== undefined) headers['content-type'] = 'application/json'
|
|
182
|
+
const ceiling = AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
183
|
+
const effective = signal === undefined
|
|
184
|
+
? ceiling
|
|
185
|
+
: typeof AbortSignal.any === 'function' ? AbortSignal.any([signal, ceiling]) : signal
|
|
186
|
+
let response
|
|
187
|
+
try {
|
|
188
|
+
response = await fetch(`${API_ORIGIN}${path}`, {
|
|
189
|
+
method,
|
|
190
|
+
headers,
|
|
191
|
+
body,
|
|
192
|
+
signal: effective,
|
|
193
|
+
redirect: 'error',
|
|
194
|
+
})
|
|
195
|
+
} catch (error) {
|
|
196
|
+
throw classifyRequestError(error)
|
|
197
|
+
}
|
|
198
|
+
const text = await response.text().catch(() => '')
|
|
199
|
+
if (text.length > MAX_RESPONSE_BYTES) {
|
|
200
|
+
throw new ConfigSyncError('the GitHub response is too large', 'invalid')
|
|
201
|
+
}
|
|
202
|
+
return { status: response.status, ok: response.ok, text }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* How to get a token that can reach a Gist, in the three ways this plugin
|
|
207
|
+
* resolves one. Kept in the error itself rather than only in the page's locale
|
|
208
|
+
* dictionary, because the agent tool and any other caller of this module see the
|
|
209
|
+
* same text and neither of them has the page's UI copy.
|
|
210
|
+
*/
|
|
211
|
+
const TOKEN_HELP = [
|
|
212
|
+
'Provide a GitHub token in one of these three ways:',
|
|
213
|
+
' 1. use the GitHub token field shown after an access error on the Multi-device config sync page (sent with that request, never saved by this plugin);',
|
|
214
|
+
' 2. set DSH_GITHUB_TOKEN (or GITHUB_TOKEN) in the environment DSH starts with, then restart DSH;',
|
|
215
|
+
' 3. sign in once with the GitHub CLI — `gh auth login` — and this plugin reads `gh auth token`.',
|
|
216
|
+
'The token needs the `gist` scope: GitHub → Settings → Developer settings →',
|
|
217
|
+
'Personal access tokens → Tokens (classic) → Generate new token → tick `gist`.',
|
|
218
|
+
'See: https://github.com/settings/tokens/new?scopes=gist&description=DSH%20Multi-device%20config%20sync',
|
|
219
|
+
].join('\n')
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Turn a non-2xx GitHub answer into a coded error.
|
|
223
|
+
*
|
|
224
|
+
* Distinguish an invalid token from an inaccessible or mistyped Gist. A secret
|
|
225
|
+
* Gist is unlisted, not access-controlled by its link; writing still requires
|
|
226
|
+
* the owner's authorization.
|
|
227
|
+
*/
|
|
228
|
+
function parseGithubError(status, text, action) {
|
|
229
|
+
let message = text
|
|
230
|
+
try {
|
|
231
|
+
const parsed = JSON.parse(text)
|
|
232
|
+
if (typeof parsed?.message === 'string' && parsed.message !== '') message = parsed.message
|
|
233
|
+
} catch {
|
|
234
|
+
// Keep the raw body.
|
|
235
|
+
}
|
|
236
|
+
if (status === 401) {
|
|
237
|
+
return new ConfigSyncError(`GitHub rejected this token for the ${action}: check whether it is invalid, expired, or revoked.\n${TOKEN_HELP}`, 'auth')
|
|
238
|
+
}
|
|
239
|
+
if (status === 403) return new ConfigSyncError(`GitHub refused the ${action} (rate limit or missing scope): ${message}\n${TOKEN_HELP}`, 'rate-limit')
|
|
240
|
+
if (status === 404) {
|
|
241
|
+
return new ConfigSyncError(
|
|
242
|
+
`Gist not found — check the id or link and whether the token is permitted to access that Gist. Updating an existing Gist requires its owner’s authorization.\n${TOKEN_HELP}`,
|
|
243
|
+
'notfound',
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
if (status === 422) return new ConfigSyncError(`GitHub rejected the ${action}: ${message}`, 'invalid')
|
|
247
|
+
return new ConfigSyncError(`GitHub ${action} failed: HTTP ${status} ${message}`, 'other')
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Create a new secret Gist carrying one backup file. */
|
|
251
|
+
export async function createGist(token, content, signal) {
|
|
252
|
+
if (Buffer.byteLength(content) > GIST_MAX_BYTES) {
|
|
253
|
+
throw new ConfigSyncError(
|
|
254
|
+
`this backup is ${(Buffer.byteLength(content) / 1024).toFixed(0)} KB, over GitHub's 1 MB per-file limit`,
|
|
255
|
+
'too-large',
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
const body = JSON.stringify({
|
|
259
|
+
description: 'DSH profile configuration backup (dsh-multi-device-config-sync)',
|
|
260
|
+
public: false,
|
|
261
|
+
files: { [BACKUP_FILENAME]: { content } },
|
|
262
|
+
})
|
|
263
|
+
const response = await githubRequest(token, 'POST', '/gists', body, signal)
|
|
264
|
+
if (response.status !== 201) throw parseGithubError(response.status, response.text, 'Gist creation')
|
|
265
|
+
return gistRef(response.text)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Overwrite the backup file inside an existing Gist, keeping its other files. */
|
|
269
|
+
export async function updateGist(token, gistId, content, signal) {
|
|
270
|
+
if (Buffer.byteLength(content) > GIST_MAX_BYTES) {
|
|
271
|
+
throw new ConfigSyncError(
|
|
272
|
+
`this backup is ${(Buffer.byteLength(content) / 1024).toFixed(0)} KB, over GitHub's 1 MB per-file limit`,
|
|
273
|
+
'too-large',
|
|
274
|
+
)
|
|
275
|
+
}
|
|
276
|
+
const body = JSON.stringify({ files: { [BACKUP_FILENAME]: { content } } })
|
|
277
|
+
const response = await githubRequest(token, 'PATCH', `/gists/${gistId}`, body, signal)
|
|
278
|
+
if (response.status !== 200) throw parseGithubError(response.status, response.text, 'Gist update')
|
|
279
|
+
return gistRef(response.text)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Read one Gist's fields. */
|
|
283
|
+
function gistRef(text) {
|
|
284
|
+
let data
|
|
285
|
+
try {
|
|
286
|
+
data = JSON.parse(text)
|
|
287
|
+
} catch {
|
|
288
|
+
throw new ConfigSyncError('GitHub returned an unreadable Gist payload', 'invalid')
|
|
289
|
+
}
|
|
290
|
+
const id = typeof data?.id === 'string' && data.id !== '' ? data.id : null
|
|
291
|
+
if (id === null) throw new ConfigSyncError('GitHub returned an invalid Gist', 'invalid')
|
|
292
|
+
return {
|
|
293
|
+
id,
|
|
294
|
+
htmlUrl: typeof data?.html_url === 'string' ? data.html_url : `https://gist.github.com/${id}`,
|
|
295
|
+
updatedAt: typeof data?.updated_at === 'string' ? data.updated_at : null,
|
|
296
|
+
files: data?.files !== null && typeof data?.files === 'object' && !Array.isArray(data.files)
|
|
297
|
+
? Object.keys(data.files)
|
|
298
|
+
: [],
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Download and strictly validate the backup file inside a Gist.
|
|
304
|
+
* @returns {Promise<{ ref: object, backup: object }>}
|
|
305
|
+
*/
|
|
306
|
+
export async function readGist(token, gistId, signal) {
|
|
307
|
+
const response = await githubRequest(token, 'GET', `/gists/${gistId}`, undefined, signal)
|
|
308
|
+
if (response.status !== 200) throw parseGithubError(response.status, response.text, 'Gist read')
|
|
309
|
+
let data
|
|
310
|
+
try {
|
|
311
|
+
data = JSON.parse(response.text)
|
|
312
|
+
} catch {
|
|
313
|
+
throw new ConfigSyncError('GitHub returned an unreadable Gist payload', 'invalid')
|
|
314
|
+
}
|
|
315
|
+
const file = data?.files?.[BACKUP_FILENAME]
|
|
316
|
+
const content = file !== null && typeof file === 'object' && !Array.isArray(file) ? file.content : undefined
|
|
317
|
+
if (typeof content !== 'string') {
|
|
318
|
+
const names = data?.files !== null && typeof data?.files === 'object' ? Object.keys(data.files) : []
|
|
319
|
+
throw new ConfigSyncError(
|
|
320
|
+
`that Gist has no ${BACKUP_FILENAME} file (it holds: ${names.join(', ') || 'nothing'})`,
|
|
321
|
+
'invalid',
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
let parsed
|
|
325
|
+
try {
|
|
326
|
+
parsed = JSON.parse(content)
|
|
327
|
+
} catch {
|
|
328
|
+
throw new ConfigSyncError('the Gist backup is not valid JSON', 'invalid')
|
|
329
|
+
}
|
|
330
|
+
return { ref: gistRef(response.text), backup: validateBackup(parsed) }
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Confirm a token is usable, and report which account it belongs to. */
|
|
334
|
+
export async function verifyToken(token, signal) {
|
|
335
|
+
const response = await githubRequest(token, 'GET', '/user', undefined, signal)
|
|
336
|
+
if (response.status !== 200) throw parseGithubError(response.status, response.text, 'token check')
|
|
337
|
+
try {
|
|
338
|
+
const data = JSON.parse(response.text)
|
|
339
|
+
return typeof data?.login === 'string' ? data.login : null
|
|
340
|
+
} catch {
|
|
341
|
+
return null
|
|
342
|
+
}
|
|
343
|
+
}
|