File size: 4,209 Bytes
68465df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * @fileoverview The one source of truth for "where is the dev laptop?".

 *

 * In a Metro-served dev build the phone already knows the dev machine's

 * address — it fetched this very bundle from it. That address is, by

 * construction, reachable on the CURRENT network: it cannot go stale the way a

 * hardcoded IP (in .env or a persisted setting) does, because if it were wrong

 * the bundle would not have loaded.

 *

 * Sources, most to least authoritative (none is reliably populated across

 * every dev-client/Expo version, so we try all three):

 *   1. `SourceCode.scriptURL` — the literal URL this bundle was fetched from.

 *   2. `Constants.expoConfig.hostUri` — Metro's host as recorded in the

 *      manifest.

 *   3. `Constants.expoGoConfig.debuggerHost` — the legacy manifest field.

 * A localhost answer (USB + `adb reverse`) is kept only if no LAN candidate

 * exists, since other devices/services cannot dial the phone's own localhost.

 *

 * Everything that needs the dev machine (the Cloud V2 local endpoints, the

 * local-miniapp dev-server URL rewrite) derives it from here instead of

 * carrying its own copy of someone's LAN IP. Personal IPs must never be

 * committed in .env or code — they strand the next dev (or the same dev on a

 * different network) on yesterday's address.

 */
import Constants from "expo-constants"
import {NativeModules, TurboModuleRegistry} from "react-native"

/**

 * Sentinel stored in the cloud endpoint settings to mean "my dev laptop,

 * resolved live from Metro". Persisting the sentinel (not the resolved URL)

 * is what makes the setting survive network changes: the laptop's IP can

 * change daily, but "the machine serving this bundle" is always current.

 */
export const METRO_AUTO = "metro-auto"

function hostOf(value: string | undefined | null): string | undefined {
  if (!value) return undefined
  try {
    // scriptURL is a full URL; hostUri/debuggerHost are bare host:port. URL()
    // needs a scheme, so give the bare forms one.
    const url = new URL(value.includes("://") ? value : `http://${value}`)
    return url.hostname || undefined
  } catch {
    return undefined
  }
}

function isLoopback(host: string): boolean {
  return host === "localhost" || host === "127.0.0.1" || host === "10.0.2.2"
}

function scriptURL(): string | undefined {
  // New architecture (bridgeless): SourceCode is a TurboModule and is NOT
  // mirrored onto NativeModules (verified on-device: NativeModules.SourceCode
  // is undefined there). Old architecture: NativeModules carries it. Try both.
  try {
    const tm = TurboModuleRegistry?.get?.("SourceCode") as {getConstants?: () => {scriptURL?: string}} | null
    const fromTurbo = tm?.getConstants?.()?.scriptURL
    if (fromTurbo) return fromTurbo
  } catch {
    /* fall through to the legacy module */
  }
  return (NativeModules as {SourceCode?: {scriptURL?: string}}).SourceCode?.scriptURL
}

/**

 * The dev machine's LAN host (no port), or undefined when no non-loopback

 * candidate is available.

 *

 * Over Wi-Fi the scriptURL carries the laptop's LAN IP, which is what we want:

 * other consumers (the dev-miniapp WebView) may not ride the `adb reverse`

 * tunnel, so a loopback address would be useless to them. Over USB the bundle

 * is served via localhost, but we deliberately do NOT fall back to it here —

 * in order to use the loopback address with adb reverse should set the environment variable instead to force it

 */
export function devServerHost(): string | undefined {
  const candidates = [
    hostOf(scriptURL()),
    hostOf(Constants.expoConfig?.hostUri),
    hostOf((Constants as {expoGoConfig?: {debuggerHost?: string}}).expoGoConfig?.debuggerHost),
  ].filter((h): h is string => !!h)

  return candidates.find((h) => !isLoopback(h))
}

/**

 * The dev machine as this phone reaches it: its LAN address, else the loopback a USB `adb reverse` publishes it on

 * (only this phone can use that one).

 */
export function devMachineHost(): string | undefined {
  return devServerHost() ?? [hostOf(scriptURL()), hostOf(Constants.expoConfig?.hostUri)].find((h) => !!h)
}