orez-sync-native 0.1.17 → 0.1.19

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.
@@ -0,0 +1,59 @@
1
+ 'use strict'
2
+
3
+ const { execFileSync } = require('node:child_process')
4
+ const { createServer } = require('node:net')
5
+
6
+ function portHolder(port) {
7
+ if (process.platform === 'win32') {
8
+ const rows = execFileSync('netstat', ['-ano', '-p', 'tcp'], { encoding: 'utf8' })
9
+ const row = rows.split('\n').find((line) => {
10
+ const fields = line.trim().split(/\s+/)
11
+ return fields[1]?.endsWith(`:${port}`) && fields[3] === 'LISTENING'
12
+ })
13
+ if (!row) return 'holder exited before inspection'
14
+ const pid = row.trim().split(/\s+/).at(-1)
15
+ const name = execFileSync('tasklist', ['/FI', `PID eq ${pid}`, '/FO', 'CSV', '/NH'], {
16
+ encoding: 'utf8',
17
+ }).trim()
18
+ return `PID ${pid} (${name})`
19
+ }
20
+ const rows = execFileSync('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-Fp'], {
21
+ encoding: 'utf8',
22
+ })
23
+ return rows
24
+ .trim()
25
+ .split('\n')
26
+ .filter((line) => line.startsWith('p'))
27
+ .map((line) => {
28
+ const pid = line.slice(1)
29
+ // argv names the executable even when Linux's comm is a thread name.
30
+ const command = execFileSync('ps', ['-p', pid, '-o', 'args='], {
31
+ encoding: 'utf8',
32
+ }).trim()
33
+ return `PID ${pid} (${command})`
34
+ })
35
+ .join(', ')
36
+ }
37
+
38
+ async function assertPortAvailable(port, host = '127.0.0.1') {
39
+ const server = createServer()
40
+ await new Promise((resolve, reject) => {
41
+ server.once('error', (error) => {
42
+ if (error.code !== 'EADDRINUSE') return reject(error)
43
+ let holder
44
+ try {
45
+ holder = portHolder(port)
46
+ } catch (inspectionError) {
47
+ holder = `could not inspect holder: ${inspectionError.message}`
48
+ }
49
+ reject(
50
+ new Error(
51
+ `native sync port ${host}:${port} is already in use by ${holder}. Stop that process or choose a different port offset.`
52
+ )
53
+ )
54
+ })
55
+ server.listen(port, host, () => server.close(resolve))
56
+ })
57
+ }
58
+
59
+ module.exports = { assertPortAvailable }
@@ -1,8 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict'
3
3
 
4
+ // generated by renderSyncNativeShim() in scripts/sync-native-platforms.ts.
5
+
4
6
  const { createRequire } = require('node:module')
5
7
  const { spawn } = require('node:child_process')
8
+ const { assertPortAvailable } = require('./port-check.cjs')
6
9
 
7
10
  const PACKAGES = {
8
11
  'darwin-arm64': {
@@ -74,18 +77,76 @@ try {
74
77
  process.exit(1)
75
78
  }
76
79
 
77
- const child = spawn(binary, process.argv.slice(2), { stdio: 'inherit' })
78
- for (const signal of ['SIGINT', 'SIGTERM']) {
79
- process.on(signal, () => child.kill(signal))
80
- }
81
- child.on('error', (error) => {
82
- throw error
83
- })
84
- child.on('exit', (code, signal) => {
85
- if (!signal) {
86
- process.exitCode = code ?? 1
87
- return
80
+ async function main() {
81
+ const args = process.argv.slice(2)
82
+ const parentPid = Number(process.env.OREZ_SYNC_NATIVE_PARENT_PID ?? process.ppid)
83
+ const parentAlive = () => {
84
+ if (process.ppid !== parentPid) return false
85
+ try {
86
+ process.kill(parentPid, 0)
87
+ return true
88
+ } catch (error) {
89
+ if (error.code === 'ESRCH') return false
90
+ if (error.code === 'EPERM') return true
91
+ throw error
92
+ }
93
+ }
94
+ let child
95
+ let stopping = false
96
+ let forceExit
97
+ const stop = (signal = 'SIGTERM') => {
98
+ if (stopping) return
99
+ stopping = true
100
+ if (!child) process.exit(1)
101
+ child.kill(signal)
102
+ forceExit = setTimeout(() => child.kill('SIGKILL'), 2000)
103
+ }
104
+ for (const signal of ['SIGINT', 'SIGTERM']) {
105
+ process.on(signal, () => stop(signal))
88
106
  }
89
- process.removeAllListeners(signal)
90
- process.kill(process.pid, signal)
91
- })
107
+ // reparenting also catches SIGKILL, which cannot run the parent's cleanup.
108
+ const parentWatch = setInterval(() => {
109
+ if (!parentAlive()) stop()
110
+ }, 100)
111
+ try {
112
+ if (args[0] === 'serve') {
113
+ const portIndex = args.indexOf('--port')
114
+ const hostIndex = args.indexOf('--host')
115
+ if (portIndex >= 0) {
116
+ await assertPortAvailable(
117
+ Number(args[portIndex + 1]),
118
+ hostIndex >= 0 ? args[hostIndex + 1] : undefined
119
+ )
120
+ }
121
+ }
122
+ if (!parentAlive()) return stop()
123
+ child = spawn(binary, args, {
124
+ // the pipe is owned only by this launcher. EOF kills the native host
125
+ // even if the launcher itself is killed before it can forward a signal.
126
+ stdio: ['pipe', 'inherit', 'inherit'],
127
+ env: { ...process.env, OREZ_SYNC_NATIVE_PARENT_PIPE: '1' },
128
+ })
129
+ child.on('error', (error) => {
130
+ console.error(`sync-native could not start: ${error.message}`)
131
+ clearInterval(parentWatch)
132
+ clearTimeout(forceExit)
133
+ process.exitCode = 1
134
+ })
135
+ child.on('exit', (code, signal) => {
136
+ clearInterval(parentWatch)
137
+ clearTimeout(forceExit)
138
+ if (!signal) {
139
+ process.exitCode = code ?? 1
140
+ return
141
+ }
142
+ process.removeAllListeners(signal)
143
+ process.kill(process.pid, signal)
144
+ })
145
+ } catch (error) {
146
+ clearInterval(parentWatch)
147
+ console.error(`sync-native: ${error.message}`)
148
+ process.exitCode = 1
149
+ }
150
+ }
151
+
152
+ void main()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orez-sync-native",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "description": "Prebuilt generic Orez sync host for application-owned callbacks",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -11,24 +11,25 @@
11
11
  "sync-native": "bin/sync-native.cjs"
12
12
  },
13
13
  "exports": {
14
- "./launcher": "./bin/sync-native.cjs"
14
+ "./launcher": "./bin/sync-native.cjs",
15
+ "./port-check": "./bin/port-check.cjs"
15
16
  },
16
17
  "files": [
17
18
  "bin"
18
19
  ],
19
20
  "optionalDependencies": {
20
- "orez-sync-native-darwin-arm64": "0.1.17",
21
- "orez-sync-native-darwin-x64": "0.1.17",
22
- "orez-sync-native-linux-arm64-gnu": "0.1.17",
23
- "orez-sync-native-linux-arm64-musl": "0.1.17",
24
- "orez-sync-native-linux-x64-gnu": "0.1.17",
25
- "orez-sync-native-linux-x64-musl": "0.1.17",
26
- "orez-sync-native-win32-arm64": "0.1.17",
27
- "orez-sync-native-win32-x64": "0.1.17"
21
+ "orez-sync-native-darwin-arm64": "0.1.19",
22
+ "orez-sync-native-darwin-x64": "0.1.19",
23
+ "orez-sync-native-linux-arm64-gnu": "0.1.19",
24
+ "orez-sync-native-linux-arm64-musl": "0.1.19",
25
+ "orez-sync-native-linux-x64-gnu": "0.1.19",
26
+ "orez-sync-native-linux-x64-musl": "0.1.19",
27
+ "orez-sync-native-win32-arm64": "0.1.19",
28
+ "orez-sync-native-win32-x64": "0.1.19"
28
29
  },
29
30
  "publishConfig": {
30
31
  "access": "public"
31
32
  },
32
- "orezSourceCommit": "a791f1e19ede469846f9830acb99821bc29b44d2",
33
- "orezNativeSourceRevision": "sha256:a922e2c302fe502813e178cd4e8a25306b30542c4b4ab4e2845bd268d9b08ace"
33
+ "orezSourceCommit": "24663bced0bb6bc3efb27b5f28cc12ed56a94fdd",
34
+ "orezNativeSourceRevision": "sha256:83fed4b2dff6bbc852699810e8756bffe5b1ba4532964f3674fdd46bf39569e4"
34
35
  }