@abide/bridge 0.5.2 → 0.6.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/CHANGELOG.md +12 -0
- package/bin/abideBridge.ts +26 -7
- package/package.json +1 -1
- package/src/allowedSites.ts +83 -0
- package/src/bridge.ts +40 -24
- package/src/claudeTurn.ts +28 -58
- package/src/desktop.ts +164 -0
- package/src/grant.ts +210 -0
- package/src/install.ts +204 -54
- package/src/opened.ts +162 -56
- package/src/pairingLink.ts +11 -5
- package/src/readLines.ts +2 -2
- package/src/siteTls.ts +17 -0
package/src/grant.ts
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// WHAT A SITE IS GIVEN OF THIS MACHINE (44.29, 44.58): the tools it asked for that the visitor granted —
|
|
2
|
+
// built-in ones by name, an MCP server's as Claude Code names them, `mcp__<server>__<tool>`, or all of
|
|
3
|
+
// a server's as `mcp__<server>` — and of those, the MCP tools whose server the visitor's own Claude Code
|
|
4
|
+
// configuration holds. A site names tools and never a server: a server is a command this machine runs.
|
|
5
|
+
|
|
6
|
+
/** What Claude Code names the one MCP server the bridge hands it, and so the prefix on each of its tools. */
|
|
7
|
+
export const SERVER_NAME = 'app'
|
|
8
|
+
|
|
9
|
+
/** What `asked` is given, and why each tool it is not given is not. */
|
|
10
|
+
export type Grant = {
|
|
11
|
+
/** Built-in tools, as Claude Code's `--tools` names them. */
|
|
12
|
+
builtIn: string[]
|
|
13
|
+
/** MCP tools, as `--allowedTools` names them. */
|
|
14
|
+
mcp: string[]
|
|
15
|
+
/** The visitor's servers those MCP tools are of, as an MCP config's `mcpServers` holds them. */
|
|
16
|
+
servers: Record<string, unknown>
|
|
17
|
+
/** Asked for and not granted. */
|
|
18
|
+
ungranted: string[]
|
|
19
|
+
/** Granted, of a server the visitor's configuration does not hold. */
|
|
20
|
+
unserved: string[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The values the visitor gave a flag by any of its `spellings` — `--flag a b`, `--flag a,b` or
|
|
25
|
+
* `--flag=a` — taken out of `args`, or `undefined` where they never gave it.
|
|
26
|
+
*/
|
|
27
|
+
export function takeFlag(
|
|
28
|
+
args: string[],
|
|
29
|
+
spellings: string[],
|
|
30
|
+
): string[] | undefined {
|
|
31
|
+
let values: string[] | undefined
|
|
32
|
+
for (let index = 0; index < args.length;) {
|
|
33
|
+
const arg = args[index] as string
|
|
34
|
+
const [flag, inline] = arg.split(/=(.*)/s) as [string, string?]
|
|
35
|
+
if (!spellings.includes(flag)) {
|
|
36
|
+
index++
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
values ??= []
|
|
40
|
+
if (inline !== undefined) {
|
|
41
|
+
values.push(inline)
|
|
42
|
+
args.splice(index, 1)
|
|
43
|
+
} else {
|
|
44
|
+
let end = index + 1
|
|
45
|
+
while (end < args.length && !(args[end] as string).startsWith('-'))
|
|
46
|
+
end++
|
|
47
|
+
values.push(...args.slice(index + 1, end))
|
|
48
|
+
args.splice(index, end - index)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return values
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The tools the visitor granted, each `--tools` value split on commas and spaces. */
|
|
55
|
+
export function grantedOf(values: string[] | undefined): Set<string> {
|
|
56
|
+
const granted = new Set<string>()
|
|
57
|
+
for (const value of values ?? [])
|
|
58
|
+
for (const tool of value.split(/[\s,]+/))
|
|
59
|
+
if (tool !== '') granted.add(tool)
|
|
60
|
+
return granted
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The server an MCP tool name is of — `calendar` for `mcp__calendar__meetings` or `mcp__calendar` — or `undefined` for a built-in. */
|
|
64
|
+
export function serverOf(tool: string): string | undefined {
|
|
65
|
+
if (!tool.startsWith('mcp__')) return undefined
|
|
66
|
+
const rest = tool.slice('mcp__'.length)
|
|
67
|
+
const end = rest.indexOf('__')
|
|
68
|
+
return end === -1 ? rest : rest.slice(0, end)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A server's name as Claude Code writes it into its tools' names. */
|
|
72
|
+
function toolNameOf(server: string): string {
|
|
73
|
+
return server.replace(/[^A-Za-z0-9_-]/g, '_')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The Claude Code configuration file whose user scope holds the servers `claude mcp add --scope user` adds. */
|
|
77
|
+
function userConfiguration(): string {
|
|
78
|
+
const directory = Bun.env.CLAUDE_CONFIG_DIR
|
|
79
|
+
return directory === undefined || directory === ''
|
|
80
|
+
? `${Bun.env.HOME ?? Bun.env.USERPROFILE ?? ''}/.claude.json`
|
|
81
|
+
: `${directory}/.claude.json`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The MCP servers the visitor's Claude Code is configured with, by the name its tools carry: those of
|
|
86
|
+
* each `--mcp-config` they passed — a file, or JSON written out — or, where they passed none, the user
|
|
87
|
+
* scope of their Claude Code configuration. Read each time, so a server added since is found. 44.58.
|
|
88
|
+
*
|
|
89
|
+
* Throws where a source they named cannot be read.
|
|
90
|
+
*/
|
|
91
|
+
export async function visitorServers(
|
|
92
|
+
sources: string[] | undefined,
|
|
93
|
+
): Promise<Map<string, unknown>> {
|
|
94
|
+
const servers = new Map<string, unknown>()
|
|
95
|
+
const read: { mcpServers?: Record<string, unknown> }[] = []
|
|
96
|
+
if (sources === undefined) {
|
|
97
|
+
// Claude Code rewrites this file while it runs, so a read caught mid-write is no servers rather
|
|
98
|
+
// than a failed turn — the visitor named no source to be told about.
|
|
99
|
+
try {
|
|
100
|
+
read.push(await Bun.file(userConfiguration()).json())
|
|
101
|
+
} catch {}
|
|
102
|
+
} else
|
|
103
|
+
for (const source of sources) {
|
|
104
|
+
const written = source.trim()
|
|
105
|
+
try {
|
|
106
|
+
read.push(
|
|
107
|
+
written.startsWith('{')
|
|
108
|
+
? JSON.parse(written)
|
|
109
|
+
: await Bun.file(written).json(),
|
|
110
|
+
)
|
|
111
|
+
} catch (thrown) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
`The --mcp-config ${source} could not be read: ${thrown instanceof Error ? thrown.message : String(thrown)}`,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const configuration of read)
|
|
118
|
+
for (const [name, server] of Object.entries(
|
|
119
|
+
configuration.mcpServers ?? {},
|
|
120
|
+
))
|
|
121
|
+
servers.set(toolNameOf(name), server)
|
|
122
|
+
// The bridge's own server is the site's, never one of the visitor's.
|
|
123
|
+
servers.delete(SERVER_NAME)
|
|
124
|
+
return servers
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** What a site asking for `asked` is given by a visitor who granted `granted` and is configured with `configured`. 44.29. */
|
|
128
|
+
export function grantOf(
|
|
129
|
+
asked: readonly string[],
|
|
130
|
+
granted: ReadonlySet<string>,
|
|
131
|
+
configured: ReadonlyMap<string, unknown>,
|
|
132
|
+
): Grant {
|
|
133
|
+
const grant: Grant = {
|
|
134
|
+
builtIn: [],
|
|
135
|
+
mcp: [],
|
|
136
|
+
servers: {},
|
|
137
|
+
ungranted: [],
|
|
138
|
+
unserved: [],
|
|
139
|
+
}
|
|
140
|
+
for (const tool of asked) {
|
|
141
|
+
const server = serverOf(tool)
|
|
142
|
+
if (server === undefined) {
|
|
143
|
+
;(granted.has(tool) ? grant.builtIn : grant.ungranted).push(tool)
|
|
144
|
+
continue
|
|
145
|
+
}
|
|
146
|
+
if (!granted.has(tool) && !granted.has(`mcp__${server}`)) {
|
|
147
|
+
grant.ungranted.push(tool)
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
const configuration = configured.get(server)
|
|
151
|
+
if (configuration === undefined) {
|
|
152
|
+
grant.unserved.push(tool)
|
|
153
|
+
continue
|
|
154
|
+
}
|
|
155
|
+
grant.mcp.push(tool)
|
|
156
|
+
grant.servers[server] = configuration
|
|
157
|
+
}
|
|
158
|
+
return grant
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* What a site asking for `asked` is given by a visitor who passed `claudeArgs`, beside those args with
|
|
163
|
+
* `--tools` and `--mcp-config` taken out. The visitor's servers are read only where an MCP tool is asked.
|
|
164
|
+
*
|
|
165
|
+
* Throws where an `--mcp-config` they named cannot be read, unless `unreadable` is given: then that is
|
|
166
|
+
* said to it, and the grant is of no server.
|
|
167
|
+
*/
|
|
168
|
+
export async function grantFor(
|
|
169
|
+
asked: readonly string[],
|
|
170
|
+
claudeArgs: readonly string[],
|
|
171
|
+
unreadable?: (message: string) => void,
|
|
172
|
+
): Promise<{ grant: Grant; args: string[] }> {
|
|
173
|
+
const args = [...claudeArgs]
|
|
174
|
+
const granted = grantedOf(takeFlag(args, ['--tools']))
|
|
175
|
+
const sources = takeFlag(args, ['--mcp-config'])
|
|
176
|
+
let asksMcp = false
|
|
177
|
+
for (const tool of asked) if (serverOf(tool) !== undefined) asksMcp = true
|
|
178
|
+
let configured = new Map<string, unknown>()
|
|
179
|
+
if (asksMcp)
|
|
180
|
+
try {
|
|
181
|
+
configured = await visitorServers(sources)
|
|
182
|
+
} catch (thrown) {
|
|
183
|
+
if (unreadable === undefined) throw thrown
|
|
184
|
+
unreadable(
|
|
185
|
+
thrown instanceof Error ? thrown.message : String(thrown),
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
return { grant: grantOf(asked, granted, configured), args }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The servers `unserved` tools are of, as one list to name in a sentence. */
|
|
192
|
+
export function serversNamed(unserved: readonly string[]): string {
|
|
193
|
+
const servers = new Set<string>()
|
|
194
|
+
for (const tool of unserved) servers.add(serverOf(tool) as string)
|
|
195
|
+
return [...servers].join(', ')
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** What the bridge says of each tool `grant` withholds, a line per reason. */
|
|
199
|
+
export function withheldNotices(grant: Grant): string[] {
|
|
200
|
+
const notices: string[] = []
|
|
201
|
+
if (grant.ungranted.length > 0)
|
|
202
|
+
notices.push(
|
|
203
|
+
`The site asked for ${grant.ungranted.join(', ')}, which this bridge wasn’t given. Add \`-- --tools ${grant.ungranted.join(',')}\` to allow it.`,
|
|
204
|
+
)
|
|
205
|
+
if (grant.unserved.length > 0)
|
|
206
|
+
notices.push(
|
|
207
|
+
`The site asked for ${grant.unserved.join(', ')}, but your Claude Code has no ${serversNamed(grant.unserved)} MCP server. Add it with \`claude mcp add --scope user\`.`,
|
|
208
|
+
)
|
|
209
|
+
return notices
|
|
210
|
+
}
|
package/src/install.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
// `abide-bridge install`:
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// file so nothing it imports can be left behind) with the link. Built here, it carries no quarantine and Gatekeeper never reads it; ad-hoc signed whole after
|
|
5
|
-
// its `Info.plist` is changed, as `abide bundle` signs its own, since an edited bundle whose signature
|
|
6
|
-
// no longer matches is one macOS reads as damaged.
|
|
1
|
+
// `abide-bridge install`: WHAT OPENS PAIRING LINKS ON THIS COMPUTER, built here rather than downloaded,
|
|
2
|
+
// with the handler it starts (`opened.ts`, bundled into one file so nothing it imports can be left
|
|
3
|
+
// behind) and what install found beside it. Each desktop registers a scheme its own way (44.43):
|
|
7
4
|
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
5
|
+
// MACOS hands a URL to an app as an Apple Event, never as an argument, so the app is an AppleScript
|
|
6
|
+
// applet (`osacompile`, on every Mac) whose `open location` starts the handler with the link. Built
|
|
7
|
+
// here, it carries no quarantine and Gatekeeper never reads it; ad-hoc signed whole after its
|
|
8
|
+
// `Info.plist` is changed, as `abide bundle` signs its own, since an edited bundle whose signature no
|
|
9
|
+
// longer matches is one macOS reads as damaged. In Application Support, not an Applications folder: it
|
|
10
|
+
// is nothing to open by hand, and Launch Services finds a handler there only once it is registered.
|
|
11
|
+
//
|
|
12
|
+
// LINUX reads a desktop entry claiming `x-scheme-handler/abide-bridge`, made the default with
|
|
13
|
+
// `xdg-mime`. WINDOWS reads the scheme from the visitor's own registry classes.
|
|
11
14
|
|
|
12
15
|
import { rm } from 'node:fs/promises' // bun has no recursive remove of its own
|
|
13
16
|
|
|
@@ -16,19 +19,76 @@ import { PAIRING_SCHEME } from './pairingLink.ts'
|
|
|
16
19
|
|
|
17
20
|
const APP_NAME = 'abide bridge'
|
|
18
21
|
export const IDENTIFIER = 'abide.bridge'
|
|
22
|
+
const DESKTOP_ENTRY = 'abide-bridge.desktop'
|
|
19
23
|
const LAUNCH_SERVICES =
|
|
20
24
|
'/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister'
|
|
21
|
-
|
|
25
|
+
const REGISTRY_KEY = `HKCU\\Software\\Classes\\${PAIRING_SCHEME}`
|
|
26
|
+
|
|
27
|
+
/** The handler install bundles into `directory`, which the desktop starts. */
|
|
22
28
|
export function handlerIn(directory: string): string {
|
|
23
|
-
return `${directory}/
|
|
29
|
+
return `${handlerDirectory(directory)}/opened.js`
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The directory the handler and what it keeps — its settings, the sites it always allows — are in. */
|
|
33
|
+
export function handlerDirectory(directory: string): string {
|
|
34
|
+
return `${directory}/handler`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Where `XDG_DATA_HOME` points, or its default under the visitor's home. */
|
|
38
|
+
function dataHome(): string {
|
|
39
|
+
const declared = Bun.env.XDG_DATA_HOME
|
|
40
|
+
return declared === undefined || declared === ''
|
|
41
|
+
? `${Bun.env.HOME ?? ''}/.local/share`
|
|
42
|
+
: declared
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* `mimeapps.list` as `list` reads, with the desktop entry no longer the scheme's default — `xdg-mime`
|
|
47
|
+
* sets a default and has no way to unset one, and a default naming a removed entry opens nothing.
|
|
48
|
+
*/
|
|
49
|
+
export function withoutDefault(list: string): string {
|
|
50
|
+
const key = `x-scheme-handler/${PAIRING_SCHEME}=`
|
|
51
|
+
const lines: string[] = []
|
|
52
|
+
for (const line of list.split('\n')) {
|
|
53
|
+
const entries = line.startsWith(key)
|
|
54
|
+
? line.slice(key.length).split(';')
|
|
55
|
+
: []
|
|
56
|
+
if (!entries.includes(DESKTOP_ENTRY)) {
|
|
57
|
+
lines.push(line)
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
const kept: string[] = []
|
|
61
|
+
for (const entry of entries)
|
|
62
|
+
if (entry !== '' && entry !== DESKTOP_ENTRY) kept.push(entry)
|
|
63
|
+
if (kept.length > 0) lines.push(`${key}${kept.join(';')};`)
|
|
64
|
+
}
|
|
65
|
+
return lines.join('\n')
|
|
24
66
|
}
|
|
25
67
|
|
|
26
|
-
/**
|
|
68
|
+
/** Unregisters the Linux desktop entry: no longer the scheme's default, and gone. */
|
|
69
|
+
async function uninstallLinux(directory: string): Promise<void> {
|
|
70
|
+
const declared = Bun.env.XDG_CONFIG_HOME
|
|
71
|
+
const list = Bun.file(
|
|
72
|
+
`${declared === undefined || declared === '' ? `${Bun.env.HOME ?? ''}/.config` : declared}/mimeapps.list`,
|
|
73
|
+
)
|
|
74
|
+
if (await list.exists()) {
|
|
75
|
+
const written = await list.text()
|
|
76
|
+
const unregistered = withoutDefault(written)
|
|
77
|
+
if (unregistered !== written) await Bun.write(list, unregistered)
|
|
78
|
+
}
|
|
79
|
+
await rm(entryPath(directory, true), { force: true })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Where install writes, under the visitor's own home on each desktop. */
|
|
27
83
|
export function installDirectory(): string {
|
|
84
|
+
const platform = process.platform
|
|
85
|
+
if (platform === 'win32')
|
|
86
|
+
return `${Bun.env.LOCALAPPDATA ?? `${Bun.env.USERPROFILE ?? ''}\\AppData\\Local`}\\abide-bridge`
|
|
87
|
+
if (platform === 'linux') return `${dataHome()}/abide-bridge`
|
|
28
88
|
return `${Bun.env.HOME ?? ''}/Library/Application Support/abide-bridge`
|
|
29
89
|
}
|
|
30
90
|
|
|
31
|
-
/** The app install writes into `directory
|
|
91
|
+
/** The app install writes into `directory`, on macOS. */
|
|
32
92
|
export function appIn(directory: string): string {
|
|
33
93
|
return `${directory}/${APP_NAME}.app`
|
|
34
94
|
}
|
|
@@ -38,12 +98,11 @@ function appleScriptString(text: string): string {
|
|
|
38
98
|
return `"${text.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`
|
|
39
99
|
}
|
|
40
100
|
|
|
41
|
-
/** The applet's source: a link opened starts the handler with it, in the background
|
|
101
|
+
/** The applet's source: a link opened starts the handler with it, in the background. */
|
|
42
102
|
function appletSource(bun: string, directory: string): string {
|
|
43
103
|
const handler = appleScriptString(handlerIn(directory))
|
|
44
|
-
const log = appleScriptString(`${directory}/bridge.log`)
|
|
45
104
|
return `on open location theURL
|
|
46
|
-
\tdo shell script quoted form of ${appleScriptString(bun)} & " " & quoted form of ${handler} & " " & quoted form of theURL & "
|
|
105
|
+
\tdo shell script quoted form of ${appleScriptString(bun)} & " " & quoted form of ${handler} & " " & quoted form of theURL & " > /dev/null 2>&1 &"
|
|
47
106
|
end open location
|
|
48
107
|
|
|
49
108
|
on run
|
|
@@ -52,7 +111,46 @@ end run
|
|
|
52
111
|
`
|
|
53
112
|
}
|
|
54
113
|
|
|
55
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* `argument` quoted as a desktop entry's `Exec` reads one: its quoting escapes first, then the string
|
|
116
|
+
* escape that doubles every backslash again, and `%` doubled so it is never read as a field code.
|
|
117
|
+
*/
|
|
118
|
+
function execArgument(argument: string): string {
|
|
119
|
+
const quoted = argument.replace(/(["`$\\])/g, '\\$1')
|
|
120
|
+
return `"${quoted.replaceAll('\\', '\\\\').replaceAll('%', '%%')}"`
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The desktop entry that opens the scheme on Linux, starting the handler with the link. */
|
|
124
|
+
export function desktopEntry(bun: string, directory: string): string {
|
|
125
|
+
return `[Desktop Entry]
|
|
126
|
+
Type=Application
|
|
127
|
+
Name=${APP_NAME}
|
|
128
|
+
Exec=${execArgument(bun)} ${execArgument(handlerIn(directory))} %u
|
|
129
|
+
NoDisplay=true
|
|
130
|
+
Terminal=false
|
|
131
|
+
MimeType=x-scheme-handler/${PAIRING_SCHEME};
|
|
132
|
+
`
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The commands that register the scheme in the visitor's own registry classes on Windows. */
|
|
136
|
+
export function registryCommands(bun: string, directory: string): string[][] {
|
|
137
|
+
const handler = handlerIn(directory).replaceAll('/', '\\')
|
|
138
|
+
return [
|
|
139
|
+
['reg', 'add', REGISTRY_KEY, '/ve', '/d', `URL:${APP_NAME}`, '/f'],
|
|
140
|
+
['reg', 'add', REGISTRY_KEY, '/v', 'URL Protocol', '/d', '', '/f'],
|
|
141
|
+
[
|
|
142
|
+
'reg',
|
|
143
|
+
'add',
|
|
144
|
+
`${REGISTRY_KEY}\\shell\\open\\command`,
|
|
145
|
+
'/ve',
|
|
146
|
+
'/d',
|
|
147
|
+
`"${bun}" "${handler}" "%1"`,
|
|
148
|
+
'/f',
|
|
149
|
+
],
|
|
150
|
+
]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Runs one of the desktop's own tools, throwing with what it wrote where it fails. */
|
|
56
154
|
async function tool(command: string[]): Promise<void> {
|
|
57
155
|
const child = Bun.spawn(command, { stdout: 'ignore', stderr: 'pipe' })
|
|
58
156
|
const [code, said] = await Promise.all([
|
|
@@ -62,42 +160,29 @@ async function tool(command: string[]): Promise<void> {
|
|
|
62
160
|
if (code !== 0) throw new Error(`${command[0]} failed: ${said.trim()}`)
|
|
63
161
|
}
|
|
64
162
|
|
|
65
|
-
/** Throws where this is
|
|
66
|
-
function
|
|
67
|
-
if (
|
|
163
|
+
/** Throws where this is a desktop no pairing link opens on. */
|
|
164
|
+
function supported(): void {
|
|
165
|
+
if (
|
|
166
|
+
process.platform !== 'darwin' &&
|
|
167
|
+
process.platform !== 'linux' &&
|
|
168
|
+
process.platform !== 'win32'
|
|
169
|
+
)
|
|
68
170
|
throw new Error(
|
|
69
|
-
`pairing links open on macOS, and this is ${process.platform}. The command a site shows pairs from any terminal.`,
|
|
171
|
+
`pairing links open on macOS, Linux and Windows, and this is ${process.platform}. The command a site shows pairs from any terminal.`,
|
|
70
172
|
)
|
|
71
173
|
}
|
|
72
174
|
|
|
73
|
-
/** Where install and uninstall work, and whether they
|
|
175
|
+
/** Where install and uninstall work, and whether they register with the desktop — a test's own directory, unregistered. */
|
|
74
176
|
type Placement = { directory?: string; register?: boolean }
|
|
75
177
|
|
|
76
|
-
/**
|
|
77
|
-
|
|
78
|
-
* registers it with Launch Services unless `register` is false.
|
|
79
|
-
*/
|
|
80
|
-
export async function install(
|
|
178
|
+
/** Builds the macOS applet into `directory`, answering where it is. */
|
|
179
|
+
async function installMac(
|
|
81
180
|
settings: HandlerSettings,
|
|
82
|
-
|
|
181
|
+
directory: string,
|
|
182
|
+
register: boolean,
|
|
83
183
|
): Promise<string> {
|
|
84
|
-
onMac()
|
|
85
|
-
const directory = options.directory ?? installDirectory()
|
|
86
184
|
const app = appIn(directory)
|
|
87
|
-
const
|
|
88
|
-
entrypoints: [`${import.meta.dir}/opened.ts`],
|
|
89
|
-
outdir: `${directory}/handler`,
|
|
90
|
-
target: 'bun',
|
|
91
|
-
})
|
|
92
|
-
if (!bundled.success)
|
|
93
|
-
throw new Error(
|
|
94
|
-
`the link handler did not bundle: ${bundled.logs.join('\n')}`,
|
|
95
|
-
)
|
|
96
|
-
await Bun.write(
|
|
97
|
-
`${directory}/handler/settings.json`,
|
|
98
|
-
JSON.stringify(settings, null, 4),
|
|
99
|
-
)
|
|
100
|
-
const source = `${directory}/handler/applet.applescript`
|
|
185
|
+
const source = `${handlerDirectory(directory)}/applet.applescript`
|
|
101
186
|
await Bun.write(source, appletSource(settings.bun, directory))
|
|
102
187
|
await rm(app, { recursive: true, force: true })
|
|
103
188
|
await tool(['osacompile', '-o', app, source])
|
|
@@ -118,24 +203,89 @@ export async function install(
|
|
|
118
203
|
] as const)
|
|
119
204
|
await tool(['plutil', '-replace', key, type, value, plist])
|
|
120
205
|
await tool(['codesign', '--force', '--sign', '-', app])
|
|
121
|
-
if (
|
|
206
|
+
if (register) await tool([LAUNCH_SERVICES, '-f', app])
|
|
122
207
|
return app
|
|
123
208
|
}
|
|
124
209
|
|
|
210
|
+
/** The desktop entry's path: in `directory` where install is not registering, else the visitor's applications. */
|
|
211
|
+
function entryPath(directory: string, register: boolean): string {
|
|
212
|
+
return register
|
|
213
|
+
? `${dataHome()}/applications/${DESKTOP_ENTRY}`
|
|
214
|
+
: `${directory}/${DESKTOP_ENTRY}`
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Writes the Linux desktop entry, and makes it the scheme's default, answering where it is. */
|
|
218
|
+
async function installLinux(
|
|
219
|
+
settings: HandlerSettings,
|
|
220
|
+
directory: string,
|
|
221
|
+
register: boolean,
|
|
222
|
+
): Promise<string> {
|
|
223
|
+
const entry = entryPath(directory, register)
|
|
224
|
+
await Bun.write(entry, desktopEntry(settings.bun, directory))
|
|
225
|
+
if (register)
|
|
226
|
+
await tool([
|
|
227
|
+
'xdg-mime',
|
|
228
|
+
'default',
|
|
229
|
+
DESKTOP_ENTRY,
|
|
230
|
+
`x-scheme-handler/${PAIRING_SCHEME}`,
|
|
231
|
+
])
|
|
232
|
+
return entry
|
|
233
|
+
}
|
|
234
|
+
|
|
125
235
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
236
|
+
* Builds what opens pairing links into `directory`, with the handler it starts beside it, and
|
|
237
|
+
* registers it with the desktop unless `register` is false. Answers what was registered.
|
|
238
|
+
*/
|
|
239
|
+
export async function install(
|
|
240
|
+
settings: HandlerSettings,
|
|
241
|
+
options: Placement = {},
|
|
242
|
+
): Promise<string> {
|
|
243
|
+
supported()
|
|
244
|
+
const directory = options.directory ?? installDirectory()
|
|
245
|
+
const register = options.register !== false
|
|
246
|
+
const bundled = await Bun.build({
|
|
247
|
+
entrypoints: [`${import.meta.dir}/opened.ts`],
|
|
248
|
+
outdir: handlerDirectory(directory),
|
|
249
|
+
target: 'bun',
|
|
250
|
+
})
|
|
251
|
+
if (!bundled.success)
|
|
252
|
+
throw new Error(
|
|
253
|
+
`the link handler did not bundle: ${bundled.logs.join('\n')}`,
|
|
254
|
+
)
|
|
255
|
+
await Bun.write(
|
|
256
|
+
`${handlerDirectory(directory)}/settings.json`,
|
|
257
|
+
JSON.stringify(settings, null, 4),
|
|
258
|
+
)
|
|
259
|
+
if (process.platform === 'darwin')
|
|
260
|
+
return installMac(settings, directory, register)
|
|
261
|
+
if (process.platform === 'linux')
|
|
262
|
+
return installLinux(settings, directory, register)
|
|
263
|
+
if (register)
|
|
264
|
+
for (const command of registryCommands(settings.bun, directory))
|
|
265
|
+
await tool(command)
|
|
266
|
+
return REGISTRY_KEY
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Unregisters what install registered from `directory` and removes everything install wrote there — the
|
|
271
|
+
* removal runs even where the desktop refuses, and the refusal is thrown after it.
|
|
128
272
|
*/
|
|
129
273
|
export async function uninstall(options: Placement = {}): Promise<void> {
|
|
130
|
-
|
|
274
|
+
supported()
|
|
131
275
|
const directory = options.directory ?? installDirectory()
|
|
132
|
-
const
|
|
276
|
+
const register = options.register !== false
|
|
133
277
|
try {
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
278
|
+
if (process.platform === 'darwin') {
|
|
279
|
+
const app = appIn(directory)
|
|
280
|
+
if (
|
|
281
|
+
register &&
|
|
282
|
+
(await Bun.file(`${app}/Contents/Info.plist`).exists())
|
|
283
|
+
)
|
|
284
|
+
await tool([LAUNCH_SERVICES, '-u', app])
|
|
285
|
+
} else if (process.platform === 'linux') {
|
|
286
|
+
// Unregistered, the entry is in `directory`, which goes below.
|
|
287
|
+
if (register) await uninstallLinux(directory)
|
|
288
|
+
} else if (register) await tool(['reg', 'delete', REGISTRY_KEY, '/f'])
|
|
139
289
|
} finally {
|
|
140
290
|
await rm(directory, { recursive: true, force: true })
|
|
141
291
|
}
|