@remoteshell/device 0.1.0-beta.5 → 0.1.0-beta.6

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.
@@ -1,6 +1,7 @@
1
1
  // #327 S1: direct public-npx runtime. Never copies or installs the package.
2
2
  import {existsSync,mkdirSync,openSync,closeSync,readFileSync,rmSync,writeFileSync,lstatSync} from 'node:fs';
3
3
  import {join,resolve} from 'node:path';
4
+ import {randomUUID} from 'node:crypto';
4
5
  import {spawnSync} from 'node:child_process';
5
6
  import {homedir,platform} from 'node:os';
6
7
  import {devOfficialRegistration} from './dev-agent-client.mjs';
@@ -52,22 +53,63 @@ function privateLockDir(p){
52
53
  }
53
54
  return p.storageDir;
54
55
  }
55
- export function acquireDirectLock(p,{pid=process.pid,alive=id=>{try{process.kill(id,0);return true;}catch(e){return e?.code==='EPERM';}}}={}){
56
- const dir=privateLockDir(p),lock=join(dir,'direct-run.lock');
57
- let fd;
58
- for(let attempt=0;attempt<1;attempt++){
59
- try{fd=openSync(lock,'wx',0o600);writeFileSync(fd,JSON.stringify({pid,created:Date.now()}));closeSync(fd);fd=undefined;
60
- return ()=>{try{const info=JSON.parse(readFileSync(lock,'utf8'));if(info.pid===pid)rmSync(lock);}catch{}};
61
- }catch(e){
62
- if(fd!==undefined)closeSync(fd);
63
- if(e.code!=='EEXIST')FAIL('DEVICE_DIRECT_LOCK_DENIED');
64
- // Fail closed even for stale locks: explicit owner cleanup required.
65
- let old;try{old=JSON.parse(readFileSync(lock,'utf8'));}catch{}
66
- if(Number.isSafeInteger(old?.pid)&&alive(old.pid))FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
56
+ export function acquireDirectLock(p,{pid=process.pid,alive=id=>{
57
+ try{process.kill(id,0);return true;}catch(e){return e?.code==='EPERM';}
58
+ }}={}){
59
+ const dir=privateLockDir(p),lock=join(dir,'direct-run.lock'),reap=join(dir,'direct-run.reap.lock');
60
+ const nonce=randomUUID();
61
+ const create=()=>{
62
+ let fd;
63
+ try{
64
+ fd=openSync(lock,'wx',0o600);
65
+ writeFileSync(fd,JSON.stringify({pid,created:Date.now(),nonce}));
66
+ closeSync(fd);fd=undefined;
67
+ }catch(e){if(fd!==undefined)closeSync(fd);throw e;}
68
+ return ()=>{
69
+ try{
70
+ const info=JSON.parse(readFileSync(lock,'utf8'));
71
+ if(info.pid===pid&&info.nonce===nonce)rmSync(lock);
72
+ }catch{}
73
+ };
74
+ };
75
+ try{return create();}catch(e){
76
+ if(e.code!=='EEXIST')FAIL('DEVICE_DIRECT_LOCK_DENIED');
77
+ }
78
+ // Linux may be killed by systemd before an npm-wrapper child signals exit.
79
+ // Reclaim only a clearly dead, owner-private PID lock; never replace a live
80
+ // worker or trust a malformed/foreign lock.
81
+ if(p.os!=='linux')FAIL('DEVICE_DIRECT_STALE_LOCK');
82
+ let snapshot,prior,bytes;
83
+ try{
84
+ snapshot=lstatSync(lock);
85
+ if(!snapshot.isFile()||snapshot.isSymbolicLink()||snapshot.uid!==process.getuid?.()||
86
+ (snapshot.mode&0o777)!==0o600||snapshot.size<1||snapshot.size>256)
87
+ FAIL('DEVICE_DIRECT_STALE_LOCK');
88
+ bytes=readFileSync(lock,'utf8');prior=JSON.parse(bytes);
89
+ if(!Number.isSafeInteger(prior?.pid)||prior.pid<=1)
67
90
  FAIL('DEVICE_DIRECT_STALE_LOCK');
68
- }
91
+ }catch{FAIL('DEVICE_DIRECT_STALE_LOCK');}
92
+ if(alive(prior.pid))FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
93
+ let mutex;
94
+ try{mutex=openSync(reap,'wx',0o600);}catch{FAIL('DEVICE_DIRECT_STALE_LOCK');}
95
+ try{
96
+ const current=lstatSync(lock);
97
+ if(current.dev!==snapshot.dev||current.ino!==snapshot.ino||
98
+ !current.isFile()||readFileSync(lock,'utf8')!==bytes)
99
+ FAIL('DEVICE_DIRECT_STALE_LOCK');
100
+ if(alive(prior.pid))FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
101
+ rmSync(lock);
102
+ }catch(e){
103
+ if(e.message==='DEVICE_DIRECT_ALREADY_RUNNING')throw e;
104
+ FAIL('DEVICE_DIRECT_STALE_LOCK');
105
+ }finally{
106
+ closeSync(mutex);
107
+ rmSync(reap,{force:true});
108
+ }
109
+ try{return create();}catch(e){
110
+ if(e.code==='EEXIST')FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
111
+ FAIL('DEVICE_DIRECT_LOCK_DENIED');
69
112
  }
70
- FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
71
113
  }
72
114
  export async function directRegister(p,{pair=devPairDevice,onApprove=url=>console.log(JSON.stringify({next:'approve-device-in-browser',approval_url:url,ownerApprovalRequired:true}))}={}){
73
115
  requireNoLegacyInstall(p);
@@ -84,6 +126,8 @@ export async function directRun(p,{packageRoot,runWorker=runDevAgentWorker,guard
84
126
  const mainFile=join(packageRoot,'src/device-agent/main.mjs');
85
127
  if(!existsSync(mainFile))FAIL('DEVICE_DIRECT_PACKAGE_INVALID');
86
128
  const release=acquireDirectLock(p);
129
+ // Also clean on normal process exit when the npm parent receives SIGTERM.
130
+ process.once('exit',release);
87
131
  try{
88
132
  const child=runWorker({gatewayUrl:GATEWAY,credentialFile:join(p.storageDir,'device.json'),
89
133
  rootId:registration.deviceId,rootPath:p.home,home:p.home,mainFile});
@@ -94,5 +138,5 @@ export async function directRun(p,{packageRoot,runWorker=runDevAgentWorker,guard
94
138
  child.once('exit',(code,signal)=>{cleanup();ok({exitCode:signal?1:(code??1),signal:signal||null});});
95
139
  process.once('SIGINT',onInt);process.once('SIGTERM',onTerm);
96
140
  });
97
- }finally{release();}
141
+ }finally{process.removeListener('exit',release);release();}
98
142
  }
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@remoteshell/device",
3
- "version": "0.1.0-beta.5",
3
+ "version": "0.1.0-beta.6",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@remoteshell/device",
9
- "version": "0.1.0-beta.5",
9
+ "version": "0.1.0-beta.6",
10
10
  "dependencies": {
11
11
  "@modelcontextprotocol/sdk": "1.32.1",
12
12
  "@wonderwhy-er/desktop-commander": "0.3.1"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remoteshell/device",
3
- "version": "0.1.0-beta.5",
3
+ "version": "0.1.0-beta.6",
4
4
  "description": "RemoteShell public beta device agent",
5
5
  "type": "module",
6
6
  "bin": {