forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-ports.mjs
More file actions
139 lines (126 loc) · 4.47 KB
/
Copy pathdev-ports.mjs
File metadata and controls
139 lines (126 loc) · 4.47 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
// Plain-Node mirror of the dev-instance PORT MATH in src/main/devInstance.ts, so
// the helper scripts (seed-auth.mjs, bootstrap) can resolve an instance's ports
// without a TypeScript loader. The app itself never imports this file.
//
// KEEP IN SYNC with src/main/devInstance.ts — the arithmetic below is mirrored,
// and devInstance.parity.test.ts fails if the two ever diverge.
import { existsSync, statSync } from 'node:fs'
import { basename, dirname, join, relative, isAbsolute } from 'node:path'
export const PRIMARY_RENDERER_PORT = 5179
export const PRIMARY_CDP_PORT = 9222
export const DEV_RENDERER_BASE = 5180
export const DEV_RENDERER_SPAN = 100
export const DEV_CDP_BASE = 9230
export const DEV_CDP_SPAN = 100
/** FNV-1a 32-bit hash. */
export function fnv1a(input) {
let hash = 0x811c9dc5
for (let i = 0; i < input.length; i++) {
hash ^= input.charCodeAt(i)
hash = Math.imul(hash, 0x01000193) >>> 0
}
return hash >>> 0
}
/** Murmur3-style finalizer. */
export function avalanche(h) {
h ^= h >>> 16
h = Math.imul(h, 0x85ebca6b)
h ^= h >>> 13
h = Math.imul(h, 0xc2b2ae35)
h ^= h >>> 16
return h >>> 0
}
function hashToRange(key, base, span) {
return base + (avalanche(fnv1a(key)) % span)
}
export function deriveRendererPort(name) {
return hashToRange(name, DEV_RENDERER_BASE, DEV_RENDERER_SPAN)
}
export function deriveCdpPort(name) {
return hashToRange('cdp:' + name, DEV_CDP_BASE, DEV_CDP_SPAN)
}
export function sanitizeInstanceName(raw) {
const slug = raw
.trim()
.toLowerCase()
.replace(/[^a-z0-9._-]+/g, '-')
.replace(/^-+|-+$/g, '')
return slug || 'wt'
}
/** Classify a checkout: a linked worktree has a `.git` FILE, the primary a `.git` DIR. */
export function findWorktreeContext(startDir) {
let dir = startDir
for (let i = 0; i < 40; i++) {
const gitPath = join(dir, '.git')
if (existsSync(gitPath)) {
let isDir = false
try {
isDir = statSync(gitPath).isDirectory()
} catch {
/* treat as linked */
}
if (!isDir) return { name: basename(dir), isPrimary: false }
// Mirror of devInstance.ts: recover a linked worktree whose `.git` pointer
// file was deleted (Windows worktree bug) instead of misdetecting as primary.
const recovered = linkedNameUnderWorktrees(dir, startDir)
return recovered
? { name: recovered, isPrimary: false }
: { name: basename(dir), isPrimary: true }
}
const parent = dirname(dir)
if (parent === dir) break
dir = parent
}
return { name: 'primary', isPrimary: true }
}
/** If `startDir` lives under `<primaryRoot>/.worktrees/<name>/…`, return `<name>`; else null. */
function linkedNameUnderWorktrees(primaryRoot, startDir) {
const rel = relative(join(primaryRoot, '.worktrees'), startDir)
if (!rel || rel.startsWith('..') || isAbsolute(rel)) return null
const first = rel.split(/[\\/]+/)[0]
return first || null
}
/** Mirror of devInstance.ts intEnv: parse a positive port, else the fallback. */
function intEnv(value, fallback) {
const n = value ? Number.parseInt(value, 10) : NaN
return Number.isInteger(n) && n > 0 && n < 65536 ? n : fallback
}
/** CDP port precedence: OMI_DEV_REMOTE_DEBUG > OMI_DEV_CDP_PORT > fallback. */
function cdpFromEnv(env, fallback) {
return intEnv(env.OMI_DEV_REMOTE_DEBUG, intEnv(env.OMI_DEV_CDP_PORT, fallback))
}
/**
* Mirror of devInstance.ts computeDevInstance (minus titleSuffix): resolve the
* instance ports honoring the SAME env overrides the app honors, so seed-auth /
* dev:instance never target a port the running app didn't bind.
*/
export function computeInstance(rawName, isPrimary, env = process.env) {
const forced = env.OMI_INSTANCE && env.OMI_INSTANCE.trim()
if (forced) {
if (forced.toLowerCase() === 'primary') isPrimary = true
else {
isPrimary = false
rawName = forced
}
}
if (isPrimary) {
return {
name: 'primary',
isPrimary: true,
rendererPort: intEnv(env.OMI_DEV_PORT, PRIMARY_RENDERER_PORT),
cdpPort: cdpFromEnv(env, PRIMARY_CDP_PORT)
}
}
const slug = sanitizeInstanceName(rawName)
return {
name: slug,
isPrimary: false,
rendererPort: intEnv(env.OMI_DEV_PORT, deriveRendererPort(slug)),
cdpPort: cdpFromEnv(env, deriveCdpPort(slug))
}
}
/** Resolve { name, isPrimary, rendererPort, cdpPort } for a checkout dir + env. */
export function resolveInstance(cwd, env = process.env) {
const { name, isPrimary } = findWorktreeContext(cwd)
return computeInstance(name, isPrimary, env)
}