@farmslot/capabilities 0.7.11 → 0.7.12
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 +4 -0
- package/dist/fs-watch.d.ts +18 -0
- package/dist/fs-watch.d.ts.map +1 -0
- package/dist/fs-watch.js +125 -0
- package/dist/fs-watch.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/screen-frame.d.ts +14 -0
- package/dist/screen-frame.d.ts.map +1 -0
- package/dist/screen-frame.js +40 -0
- package/dist/screen-frame.js.map +1 -0
- package/dist/screen-h264.d.ts +7 -0
- package/dist/screen-h264.d.ts.map +1 -0
- package/dist/screen-h264.js +91 -0
- package/dist/screen-h264.js.map +1 -0
- package/dist/workspace-watch.d.ts +3 -0
- package/dist/workspace-watch.d.ts.map +1 -0
- package/dist/workspace-watch.js +90 -0
- package/dist/workspace-watch.js.map +1 -0
- package/package.json +31 -10
- package/src/fs-watch.ts +0 -133
- package/src/index.ts +0 -15
- package/src/screen-frame.ts +0 -60
- package/src/screen-h264.ts +0 -101
- package/src/workspace-watch.ts +0 -100
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,10 @@ All notable changes to `@farmslot/capabilities` are tracked here.
|
|
|
6
6
|
|
|
7
7
|
- Active-development baseline; add user-facing changes here before release or package publication.
|
|
8
8
|
|
|
9
|
+
## 0.7.12 - 2026-11-30
|
|
10
|
+
|
|
11
|
+
- Ship compiled `dist/` output (`build` script, `main`/`exports` point at `dist/`), so the compiled gateway and node load it with plain `node`.
|
|
12
|
+
|
|
9
13
|
## 0.7.11 - 2026-11-25
|
|
10
14
|
|
|
11
15
|
- Republished in the 25 Nov cohort release with the other @farmslot packages, so an install resolves one copy of each.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FileWatchUnavailable } from '@farmslot/protocol';
|
|
2
|
+
export declare function expandTilde(p: string): string;
|
|
3
|
+
export interface FileWatchHandle {
|
|
4
|
+
stop(): void;
|
|
5
|
+
}
|
|
6
|
+
export interface FileWatchOptions {
|
|
7
|
+
/** Wake an append reader without reading or transporting the file body. */
|
|
8
|
+
notificationsOnly?: boolean;
|
|
9
|
+
onUnavailable?: (observation: FileWatchUnavailable) => void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Watch a single file for content changes. `onChange` receives the file's UTF-8
|
|
13
|
+
* content on every write. The parent directory remains watched so atomic file
|
|
14
|
+
* replacement does not detach the watcher from subsequent updates. Returns a
|
|
15
|
+
* handle the caller stops when done.
|
|
16
|
+
*/
|
|
17
|
+
export declare function watchFile(filePath: string, onChange: (content: string) => void, options?: FileWatchOptions): FileWatchHandle;
|
|
18
|
+
//# sourceMappingURL=fs-watch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fs-watch.d.ts","sourceRoot":"","sources":["../src/fs-watch.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAoB,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAIjF,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAI7C;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,IAAI,IAAI,CAAC;CACd;AAED,MAAM,WAAW,gBAAgB;IAC/B,2EAA2E;IAC3E,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,aAAa,CAAC,EAAE,CAAC,WAAW,EAAE,oBAAoB,KAAK,IAAI,CAAC;CAC7D;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CACvB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,EACnC,OAAO,GAAE,gBAAqB,GAC7B,eAAe,CA4FjB"}
|
package/dist/fs-watch.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Shared file-watch primitive (ADR-046). Native parent-directory `fs.watch`
|
|
2
|
+
// survives atomic file replacement and expands tilde paths. Used by the node
|
|
3
|
+
// (primary owner of machine-local monitoring) and by the gateway as a local
|
|
4
|
+
// fallback when a machine has no connected node — one implementation, two callers.
|
|
5
|
+
import { existsSync, watch } from 'node:fs';
|
|
6
|
+
import { readFile } from 'node:fs/promises';
|
|
7
|
+
import { homedir } from 'node:os';
|
|
8
|
+
import { basename, dirname } from 'node:path';
|
|
9
|
+
// Pool configs can declare repos as `~/...`. `~` is a shell token, not a
|
|
10
|
+
// filesystem prefix — node:fs treats it literally. Expand to $HOME first.
|
|
11
|
+
export function expandTilde(p) {
|
|
12
|
+
if (p === '~')
|
|
13
|
+
return homedir();
|
|
14
|
+
if (p.startsWith('~/'))
|
|
15
|
+
return `${homedir()}/${p.slice(2)}`;
|
|
16
|
+
return p;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Watch a single file for content changes. `onChange` receives the file's UTF-8
|
|
20
|
+
* content on every write. The parent directory remains watched so atomic file
|
|
21
|
+
* replacement does not detach the watcher from subsequent updates. Returns a
|
|
22
|
+
* handle the caller stops when done.
|
|
23
|
+
*/
|
|
24
|
+
export function watchFile(filePath, onChange, options = {}) {
|
|
25
|
+
const targetPath = expandTilde(filePath);
|
|
26
|
+
let watcher = null;
|
|
27
|
+
let stopped = false;
|
|
28
|
+
let lastObservation;
|
|
29
|
+
let generation = 0;
|
|
30
|
+
const unavailable = (error) => {
|
|
31
|
+
const code = error.code;
|
|
32
|
+
if (code === 'ENOENT')
|
|
33
|
+
return { kind: 'deleted' };
|
|
34
|
+
const failure = {
|
|
35
|
+
message: error instanceof Error ? error.message : String(error),
|
|
36
|
+
...(typeof code === 'string' ? { code } : {}),
|
|
37
|
+
};
|
|
38
|
+
return { kind: 'error', error: failure };
|
|
39
|
+
};
|
|
40
|
+
const observe = async () => {
|
|
41
|
+
if (stopped)
|
|
42
|
+
return;
|
|
43
|
+
const current = ++generation;
|
|
44
|
+
let observation;
|
|
45
|
+
try {
|
|
46
|
+
observation = { kind: 'updated', content: await readFile(targetPath, 'utf-8') };
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
observation = unavailable(error);
|
|
50
|
+
}
|
|
51
|
+
if (stopped || current !== generation)
|
|
52
|
+
return;
|
|
53
|
+
const fingerprint = JSON.stringify(observation);
|
|
54
|
+
if (fingerprint === lastObservation)
|
|
55
|
+
return;
|
|
56
|
+
lastObservation = fingerprint;
|
|
57
|
+
if (observation.kind === 'updated')
|
|
58
|
+
onChange(observation.content);
|
|
59
|
+
else if (options.onUnavailable)
|
|
60
|
+
options.onUnavailable(observation);
|
|
61
|
+
else if (observation.kind === 'error') {
|
|
62
|
+
// Content-only callers retain their last value; the parent watch stays armed
|
|
63
|
+
// so a subsequent permission change or replacement can restore delivery.
|
|
64
|
+
console.warn(`[fs.watch] read failed for ${targetPath}: ${observation.error.message}`);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const start = () => {
|
|
68
|
+
if (stopped)
|
|
69
|
+
return;
|
|
70
|
+
// Watch the parent directory even when the file already exists. Checklist markers,
|
|
71
|
+
// git, and many editors update files through atomic rename; watching the file inode
|
|
72
|
+
// sees the first replacement and then silently follows the orphaned inode.
|
|
73
|
+
const dir = dirname(targetPath);
|
|
74
|
+
if (!existsSync(dir)) {
|
|
75
|
+
if (options.onUnavailable) {
|
|
76
|
+
throw Object.assign(new Error(`Watch parent does not exist: ${dir}`), { code: 'ENOENT' });
|
|
77
|
+
}
|
|
78
|
+
// Legacy content-only consumers keep their existing inactive-watch behavior.
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const target = basename(targetPath);
|
|
82
|
+
watcher = watch(dir, { persistent: false }, (_, filename) => {
|
|
83
|
+
if (filename != null && filename.toString() !== target)
|
|
84
|
+
return;
|
|
85
|
+
if (options.notificationsOnly)
|
|
86
|
+
onChange('');
|
|
87
|
+
else
|
|
88
|
+
void observe();
|
|
89
|
+
});
|
|
90
|
+
watcher.on('error', (error) => {
|
|
91
|
+
if (stopped)
|
|
92
|
+
return;
|
|
93
|
+
// A failed native watch can no longer prove subsequent changes. Revoke
|
|
94
|
+
// admission explicitly; the caller must establish a new watch to retry.
|
|
95
|
+
generation++;
|
|
96
|
+
watcher?.close();
|
|
97
|
+
watcher = null;
|
|
98
|
+
stopped = true;
|
|
99
|
+
if (options.onUnavailable) {
|
|
100
|
+
options.onUnavailable({
|
|
101
|
+
kind: 'error',
|
|
102
|
+
error: {
|
|
103
|
+
message: error.message,
|
|
104
|
+
...('code' in error && typeof error.code === 'string' ? { code: error.code } : {}),
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
start();
|
|
114
|
+
return {
|
|
115
|
+
stop() {
|
|
116
|
+
stopped = true;
|
|
117
|
+
generation++;
|
|
118
|
+
if (watcher) {
|
|
119
|
+
watcher.close();
|
|
120
|
+
watcher = null;
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=fs-watch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fs-watch.js","sourceRoot":"","sources":["../src/fs-watch.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,4EAA4E;AAC5E,mFAAmF;AAEnF,OAAO,EAAE,UAAU,EAAkB,KAAK,EAAE,MAAM,SAAS,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAI9C,yEAAyE;AACzE,0EAA0E;AAC1E,MAAM,UAAU,WAAW,CAAC,CAAS;IACnC,IAAI,CAAC,KAAK,GAAG;QAAE,O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AC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { expandTilde, type FileWatchHandle, type FileWatchOptions, watchFile } from './fs-watch.js';
|
|
2
|
+
export { type DecodedNodeFrame, decodeNodeFrame, encodeNodeFrame, NODE_FRAME_MAGIC, } from './screen-frame.js';
|
|
3
|
+
export { createH264FrameSplitter, type H264FrameHandler, type H264FrameSplitter, } from './screen-h264.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,KAAK,eAAe,EAAE,KAAK,gBAAgB,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AACpG,OAAO,EACL,KAAK,gBAAgB,EACrB,eAAe,EACf,eAAe,EACf,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,uBAAuB,EACvB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,GACvB,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// @farmslot/capabilities — machine-local capability primitives shared by the node
|
|
2
|
+
// (primary owner) and the gateway (local fallback when no node). ADR-046.
|
|
3
|
+
export { expandTilde, watchFile } from './fs-watch.js';
|
|
4
|
+
export { decodeNodeFrame, encodeNodeFrame, NODE_FRAME_MAGIC, } from './screen-frame.js';
|
|
5
|
+
export { createH264FrameSplitter, } from './screen-h264.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,0EAA0E;AAE1E,OAAO,EAAE,WAAW,EAA+C,SAAS,EAAE,MAAM,eAAe,CAAC;AACpG,OAAO,EAEL,eAAe,EACf,eAAe,EACf,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,uBAAuB,GAGxB,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { NODE_FRAME_MAGIC } from '@farmslot/protocol';
|
|
2
|
+
export { NODE_FRAME_MAGIC };
|
|
3
|
+
export declare function encodeNodeFrame(slotId: string, payload: Uint8Array, keyFrame: boolean, width: number, height: number): Buffer;
|
|
4
|
+
export interface DecodedNodeFrame {
|
|
5
|
+
slotId: string;
|
|
6
|
+
payload: Uint8Array;
|
|
7
|
+
keyFrame: boolean;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
}
|
|
11
|
+
/** Decode a node capture frame. Returns null when the buffer is truncated. Callers
|
|
12
|
+
* should have already checked `raw[0] === NODE_FRAME_MAGIC`. */
|
|
13
|
+
export declare function decodeNodeFrame(raw: Buffer): DecodedNodeFrame | null;
|
|
14
|
+
//# sourceMappingURL=screen-frame.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"screen-frame.d.ts","sourceRoot":"","sources":["../src/screen-frame.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,OAAO,EAAE,gBAAgB,EAAE,CAAC;AAI5B,wBAAgB,eAAe,CAC7B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,UAAU,EACnB,QAAQ,EAAE,OAAO,EACjB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GACb,MAAM,CAYR;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,UAAU,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;iEACiE;AACjE,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAepE"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Shared node→gateway capture-frame codec (ADR-046). Single source of truth for the
|
|
2
|
+
// binary envelope so the node (encode) and the gateway (decode) never drift. The magic
|
|
3
|
+
// byte lives in @farmslot/protocol; the Buffer-based codec lives here (node-only) so it
|
|
4
|
+
// stays out of the UI-shared protocol bundle.
|
|
5
|
+
//
|
|
6
|
+
// Envelope: [0xAF magic][1B flags][2B width BE][2B height BE][1B slotIdLen][slotId…][payload…]
|
|
7
|
+
// flags bit0 = keyframe.
|
|
8
|
+
import { NODE_FRAME_MAGIC } from '@farmslot/protocol';
|
|
9
|
+
export { NODE_FRAME_MAGIC };
|
|
10
|
+
const HEADER_FIXED = 7; // magic + flags + width(2) + height(2) + slotIdLen
|
|
11
|
+
export function encodeNodeFrame(slotId, payload, keyFrame, width, height) {
|
|
12
|
+
const slotBytes = Buffer.from(slotId, 'utf-8');
|
|
13
|
+
const headerSize = HEADER_FIXED + slotBytes.length;
|
|
14
|
+
const buf = Buffer.alloc(headerSize + payload.length);
|
|
15
|
+
buf[0] = NODE_FRAME_MAGIC;
|
|
16
|
+
buf[1] = keyFrame ? 1 : 0;
|
|
17
|
+
buf.writeUInt16BE(width, 2);
|
|
18
|
+
buf.writeUInt16BE(height, 4);
|
|
19
|
+
buf[6] = slotBytes.length;
|
|
20
|
+
slotBytes.copy(buf, HEADER_FIXED);
|
|
21
|
+
buf.set(payload, headerSize);
|
|
22
|
+
return buf;
|
|
23
|
+
}
|
|
24
|
+
/** Decode a node capture frame. Returns null when the buffer is truncated. Callers
|
|
25
|
+
* should have already checked `raw[0] === NODE_FRAME_MAGIC`. */
|
|
26
|
+
export function decodeNodeFrame(raw) {
|
|
27
|
+
if (raw.length < HEADER_FIXED)
|
|
28
|
+
return null;
|
|
29
|
+
const flags = raw[1];
|
|
30
|
+
const keyFrame = (flags & 1) !== 0;
|
|
31
|
+
const width = raw.readUInt16BE(2);
|
|
32
|
+
const height = raw.readUInt16BE(4);
|
|
33
|
+
const slotIdLen = raw[6];
|
|
34
|
+
if (raw.length < HEADER_FIXED + slotIdLen)
|
|
35
|
+
return null;
|
|
36
|
+
const slotId = raw.subarray(HEADER_FIXED, HEADER_FIXED + slotIdLen).toString('utf-8');
|
|
37
|
+
const payload = new Uint8Array(raw.buffer, raw.byteOffset + HEADER_FIXED + slotIdLen, raw.length - HEADER_FIXED - slotIdLen);
|
|
38
|
+
return { slotId, payload, keyFrame, width, height };
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=screen-frame.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"screen-frame.js","sourceRoot":"","sources":["../src/screen-frame.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,uFAAuF;AACvF,wFAAwF;AACxF,8CAA8C;AAC9C,EAAE;AACF,+FAA+F;AAC/F,yBAAyB;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,OAAO,EAAE,gBAAgB,EAAE,CAAC;AAE5B,MAAM,YAAY,GAAG,CAAC,CAAC,CAAC,mDAAmD;AAE3E,MAAM,UAAU,eAAe,CAC7B,MAAc,EACd,OAAmB,EACnB,QAAiB,EACjB,KAAa,EACb,MAAc;IAEd,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/C,MAAM,UAAU,GAAG,YAAY,GAAG,SAAS,CAAC,MAAM,CAAC;IACnD,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD,GAAG,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC;IAC1B,GAAG,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1B,GAAG,CAAC,aAAa,CAAC,KAAK,E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
1
|
+
export type H264FrameHandler = (frame: Uint8Array, keyFrame: boolean) => void;
|
|
2
|
+
export interface H264FrameSplitter {
|
|
3
|
+
/** Feed a raw stdout chunk; emits complete video frames via the handler. */
|
|
4
|
+
push(chunk: Buffer): void;
|
|
5
|
+
}
|
|
6
|
+
export declare function createH264FrameSplitter(onFrame: H264FrameHandler): H264FrameSplitter;
|
|
7
|
+
//# sourceMappingURL=screen-h264.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"screen-h264.d.ts","sourceRoot":"","sources":["../src/screen-h264.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,KAAK,IAAI,CAAC;AAE9E,MAAM,WAAW,iBAAiB;IAChC,4EAA4E;IAC5E,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAaD,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,gBAAgB,GAAG,iBAAiB,CA4EpF"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Shared H.264 frame splitter. The node and the gateway both read the raw byte stream
|
|
2
|
+
// from `adb screenrecord --output-format=h264` and slice it into individual video frames
|
|
3
|
+
// the same way: split on the 00 00 00 01 NAL start codes and group the NAL units into
|
|
4
|
+
// frames (SPS + PPS + IDR = a keyframe, a lone slice = a delta frame). This logic was
|
|
5
|
+
// copied verbatim into both services; it now lives in one place.
|
|
6
|
+
function concatBuffers(parts) {
|
|
7
|
+
const total = parts.reduce((s, p) => s + p.length, 0);
|
|
8
|
+
const out = new Uint8Array(total);
|
|
9
|
+
let offset = 0;
|
|
10
|
+
for (const p of parts) {
|
|
11
|
+
out.set(p, offset);
|
|
12
|
+
offset += p.length;
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
export function createH264FrameSplitter(onFrame) {
|
|
17
|
+
let buffer = Buffer.alloc(0);
|
|
18
|
+
function drain() {
|
|
19
|
+
const buf = buffer;
|
|
20
|
+
const startCodes = [];
|
|
21
|
+
// Find all 4-byte start codes.
|
|
22
|
+
for (let i = 0; i <= buf.length - 4; i++) {
|
|
23
|
+
if (buf[i] === 0 && buf[i + 1] === 0 && buf[i + 2] === 0 && buf[i + 3] === 1) {
|
|
24
|
+
startCodes.push(i);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (startCodes.length < 2)
|
|
28
|
+
return; // need at least 2 start codes to extract a NAL
|
|
29
|
+
let consumed = 0;
|
|
30
|
+
let frameStart = -1;
|
|
31
|
+
let frameIsKey = false;
|
|
32
|
+
let frameData = [];
|
|
33
|
+
for (let i = 0; i < startCodes.length - 1; i++) {
|
|
34
|
+
const nalStart = startCodes[i];
|
|
35
|
+
const nalEnd = startCodes[i + 1];
|
|
36
|
+
const nalType = buf[nalStart + 4] & 0x1f;
|
|
37
|
+
if (nalType === 7 || nalType === 8) {
|
|
38
|
+
// SPS or PPS — start accumulating a keyframe.
|
|
39
|
+
if (frameStart === -1)
|
|
40
|
+
frameStart = nalStart;
|
|
41
|
+
frameIsKey = true;
|
|
42
|
+
frameData.push(buf.subarray(nalStart, nalEnd));
|
|
43
|
+
}
|
|
44
|
+
else if (nalType === 5) {
|
|
45
|
+
// IDR slice — completes the keyframe.
|
|
46
|
+
if (frameStart === -1) {
|
|
47
|
+
frameStart = nalStart;
|
|
48
|
+
frameIsKey = true;
|
|
49
|
+
}
|
|
50
|
+
frameData.push(buf.subarray(nalStart, nalEnd));
|
|
51
|
+
onFrame(concatBuffers(frameData), true);
|
|
52
|
+
consumed = nalEnd;
|
|
53
|
+
frameStart = -1;
|
|
54
|
+
frameIsKey = false;
|
|
55
|
+
frameData = [];
|
|
56
|
+
}
|
|
57
|
+
else if (nalType === 1) {
|
|
58
|
+
// P-slice — standalone frame.
|
|
59
|
+
if (frameData.length > 0) {
|
|
60
|
+
// Flush accumulated SPS/PPS without IDR (shouldn't happen, but safe).
|
|
61
|
+
onFrame(concatBuffers(frameData), frameIsKey);
|
|
62
|
+
frameData = [];
|
|
63
|
+
frameIsKey = false;
|
|
64
|
+
frameStart = -1;
|
|
65
|
+
}
|
|
66
|
+
onFrame(new Uint8Array(buf.buffer, buf.byteOffset + nalStart, nalEnd - nalStart), false);
|
|
67
|
+
consumed = nalEnd;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
// Skip other NAL types (SEI=6, AUD=9, etc.).
|
|
71
|
+
if (frameStart !== -1) {
|
|
72
|
+
frameData.push(buf.subarray(nalStart, nalEnd));
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
consumed = nalEnd;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Keep unconsumed data (last incomplete NAL + any accumulated frame data).
|
|
80
|
+
if (consumed > 0) {
|
|
81
|
+
buffer = Buffer.from(buf.subarray(frameData.length > 0 ? frameStart : consumed));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
push(chunk) {
|
|
86
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
87
|
+
drain();
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=screen-h264.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"screen-h264.js","sourceRoot":"","sources":["../src/screen-h264.ts"],"names":[],"mappings":"AAAA,sFAAsF;AACtF,yFAAyF;AACzF,sFAAsF;AACtF,sFAAsF;AACtF,iEAAiE;AASjE,SAAS,aAAa,CAAC,KAAe;IACpC,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IACtD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,MAAM,GAAG,CAA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D,OAAO,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,GAAG,QAAQ,EAAE,MAAM,GAAG,QAAQ,CAAC,EAAE,KAAK,CAAC,CAAC;gBACzF,QAAQ,GAAG,MAAM,CAAC;YACpB,CAAC;iBAAM,CAAC;gBACN,6CAA6C;gBAC7C,IAAI,UAAU,KAAK,CAAC,CAAC,EAAE,CAAC;oBACtB,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;gBACjD,CAAC;qBAAM,CAAC;oBACN,QAAQ,GAAG,MAAM,CAAC;gBACpB,CAAC;YACH,CAAC;QACH,CAAC;QAED,2EAA2E;QAC3E,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;YACjB,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,UAAW,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;QACpF,CAAC;IACH,CAAC;IAED,OAAO;QACL,IAAI,CAAC,KAAa;YAChB,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;YACxC,KAAK,EAAE,CAAC;QACV,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Arm all workspace bytes, names, index and refs. No periodic rereads or following symlinks. */
|
|
2
|
+
export declare function armLocalWorkspace(root: string, changed: () => void, failed: (error: Error) => void): Promise<() => void>;
|
|
3
|
+
//# sourceMappingURL=workspace-watch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace-watch.d.ts","sourceRoot":"","sources":["../src/workspace-watch.ts"],"names":[],"mappings":"AAkCA,iGAAiG;AACjG,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,IAAI,EACnB,MAAM,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAC7B,OAAO,CAAC,MAAM,IAAI,CAAC,CA4DrB"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { execFile as execFileCallback } from 'node:child_process';
|
|
2
|
+
import { watch } from 'node:fs';
|
|
3
|
+
import { lstat, realpath } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { expandTilde } from './fs-watch.js';
|
|
6
|
+
function discoverGit(cwd) {
|
|
7
|
+
return new Promise((resolve, reject) => execFileCallback('git', ['rev-parse', '--path-format=absolute', '--git-dir', '--git-common-dir'], {
|
|
8
|
+
cwd,
|
|
9
|
+
timeout: 10_000,
|
|
10
|
+
maxBuffer: 64 * 1024,
|
|
11
|
+
env: Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_'))),
|
|
12
|
+
}, (error, stdout, stderr) => {
|
|
13
|
+
// Numeric exit is Git-domain status. Spawn, timeout and signal failures are not plain directories.
|
|
14
|
+
if (error && (typeof error.code !== 'number' || error.killed || error.signal))
|
|
15
|
+
reject(error);
|
|
16
|
+
else
|
|
17
|
+
resolve({ code: error?.code ?? 0, stdout, stderr });
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
/** Arm all workspace bytes, names, index and refs. No periodic rereads or following symlinks. */
|
|
21
|
+
export async function armLocalWorkspace(root, changed, failed) {
|
|
22
|
+
const watchers = [];
|
|
23
|
+
let stopped = false;
|
|
24
|
+
const close = () => {
|
|
25
|
+
stopped = true;
|
|
26
|
+
for (const watcher of watchers)
|
|
27
|
+
watcher.close();
|
|
28
|
+
};
|
|
29
|
+
const fail = (error) => {
|
|
30
|
+
if (!stopped) {
|
|
31
|
+
close();
|
|
32
|
+
failed(error);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const actualRoot = await realpath(expandTilde(root));
|
|
36
|
+
// Root watch is armed before discovering git metadata so a concurrent checkout is fenced.
|
|
37
|
+
const add = async (target) => {
|
|
38
|
+
const initial = await lstat(target);
|
|
39
|
+
const watcher = watch(target, { recursive: true, persistent: false }, (_, filename) => {
|
|
40
|
+
if (stopped)
|
|
41
|
+
return;
|
|
42
|
+
if (target === actualRoot && filename?.toString() === '.git') {
|
|
43
|
+
fail(new Error('Workspace git binding changed; retry to rearm'));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
changed();
|
|
47
|
+
});
|
|
48
|
+
watcher.on('error', fail);
|
|
49
|
+
watchers.push(watcher);
|
|
50
|
+
// Roots and linked metadata can be atomically removed/replaced. Watching an old inode is insufficient.
|
|
51
|
+
const parent = watch(path.dirname(target), { persistent: false }, async (_, filename) => {
|
|
52
|
+
if (filename != null && filename.toString() !== path.basename(target))
|
|
53
|
+
return;
|
|
54
|
+
try {
|
|
55
|
+
const current = await lstat(target);
|
|
56
|
+
if (current.ino !== initial.ino || current.dev !== initial.dev)
|
|
57
|
+
fail(new Error('Workspace source root replaced'));
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
fail(error instanceof Error ? error : new Error(String(error)));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
parent.on('error', fail);
|
|
64
|
+
watchers.push(parent);
|
|
65
|
+
};
|
|
66
|
+
try {
|
|
67
|
+
await add(actualRoot);
|
|
68
|
+
const discovery = await discoverGit(actualRoot);
|
|
69
|
+
if (discovery.code === 0) {
|
|
70
|
+
for (const metadata of new Set(discovery.stdout.trim().split('\n'))) {
|
|
71
|
+
const target = await realpath(metadata);
|
|
72
|
+
const relative = path.relative(actualRoot, target);
|
|
73
|
+
if (relative.startsWith('..') || path.isAbsolute(relative))
|
|
74
|
+
await add(target);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else if (discovery.code !== 128 || !discovery.stderr.includes('not a git repository')) {
|
|
78
|
+
throw new Error(`Git metadata discovery failed (${discovery.code}): ${discovery.stderr}`);
|
|
79
|
+
}
|
|
80
|
+
// Exit 128 with Git's specific diagnostic admits a plain directory, never runner state.
|
|
81
|
+
if (stopped)
|
|
82
|
+
throw new Error('Workspace source failed while arming');
|
|
83
|
+
return close;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
close();
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=workspace-watch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace-watch.js","sourceRoot":"","sources":["../src/workspace-watch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAkB,KAAK,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAM5C,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,CACrC,gBAAgB,CACd,KAAK,EACL,CAAC,WAAW,EAAE,wBAAwB,EAAE,WAAW,EAAE,kBAAkB,CAAC,EACxE;QACE,GAAG;QACH,OAAO,EAAE,MAAM;QACf,SAAS,EAAE,EAAE,GAAG,IAAI;QACpB,GAAG,EAAE,MAAM,CAAC,WAAW,CACrB,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CACvE;KACF,EACD,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;QACxB,mGAAmG;QACnG,IAAI,KAAK,IAAI,CAAC,OAAO,KAAK,CAAC,I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AC;QAChD,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;YACzB,KAAK,MAAM,QAAQ,IAAI,IAAI,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;gBACpE,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAC,CAAC;gBACxC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;gBACnD,IAAI,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;oBAAE,MAAM,GAAG,CAAC,MAAM,CAAC,CAAC;YAChF,CAAC;QACH,CAAC;aAAM,IAAI,SAAS,CAAC,IAAI,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,sBAAsB,CAAC,EAAE,CAAC;YACxF,MAAM,IAAI,KAAK,CAAC,kCAAkC,SAAS,CAAC,IAAI,MAAM,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;QAC5F,CAAC;QACD,wFAAwF;QACxF,IAAI,OAAO;YAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QACrE,OAAO,KAAK,CAAC;IACf,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,KAAK,EAAE,CAAC;QACR,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,33 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farmslot/capabilities",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.12",
|
|
4
4
|
"description": "Machine-local capability primitives (fs-watch, screen capture) shared by the node (primary owner) and the gateway (local fallback when no node) — ADR-046.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"main": "
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
|
-
".":
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./fs-watch": {
|
|
16
|
+
"types": "./dist/fs-watch.d.ts",
|
|
17
|
+
"import": "./dist/fs-watch.js",
|
|
18
|
+
"default": "./dist/fs-watch.js"
|
|
19
|
+
},
|
|
20
|
+
"./workspace-watch": {
|
|
21
|
+
"types": "./dist/workspace-watch.d.ts",
|
|
22
|
+
"import": "./dist/workspace-watch.js",
|
|
23
|
+
"default": "./dist/workspace-watch.js"
|
|
24
|
+
},
|
|
25
|
+
"./screen-frame": {
|
|
26
|
+
"types": "./dist/screen-frame.d.ts",
|
|
27
|
+
"import": "./dist/screen-frame.js",
|
|
28
|
+
"default": "./dist/screen-frame.js"
|
|
29
|
+
},
|
|
30
|
+
"./screen-h264": {
|
|
31
|
+
"types": "./dist/screen-h264.d.ts",
|
|
32
|
+
"import": "./dist/screen-h264.js",
|
|
33
|
+
"default": "./dist/screen-h264.js"
|
|
34
|
+
}
|
|
14
35
|
},
|
|
15
36
|
"files": [
|
|
16
37
|
"README.md",
|
|
17
38
|
"LICENSE",
|
|
18
|
-
"
|
|
19
|
-
"!src/**/*.test.ts"
|
|
39
|
+
"dist"
|
|
20
40
|
],
|
|
21
41
|
"publishConfig": {
|
|
22
42
|
"access": "public"
|
|
23
43
|
},
|
|
24
44
|
"scripts": {
|
|
45
|
+
"build": "rm -rf dist && tsc --project tsconfig.build.json && node ../../scripts/finish-dist-build.mjs",
|
|
25
46
|
"test": "node ../../scripts/quality/run-tsx-tests.mjs --cwd . --tsconfig tsconfig.json src",
|
|
26
47
|
"typecheck": "tsc --noEmit --project tsconfig.json",
|
|
27
48
|
"quality": "yarn test && yarn typecheck"
|
|
28
49
|
},
|
|
29
50
|
"dependencies": {
|
|
30
|
-
"@farmslot/protocol": "0.92.
|
|
51
|
+
"@farmslot/protocol": "0.92.6"
|
|
31
52
|
},
|
|
32
53
|
"devDependencies": {
|
|
33
54
|
"@types/node": "^22.0.0",
|
package/src/fs-watch.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
// Shared file-watch primitive (ADR-046). Native parent-directory `fs.watch`
|
|
2
|
-
// survives atomic file replacement and expands tilde paths. Used by the node
|
|
3
|
-
// (primary owner of machine-local monitoring) and by the gateway as a local
|
|
4
|
-
// fallback when a machine has no connected node — one implementation, two callers.
|
|
5
|
-
|
|
6
|
-
import { existsSync, type FSWatcher, watch } from 'node:fs';
|
|
7
|
-
import { readFile } from 'node:fs/promises';
|
|
8
|
-
import { homedir } from 'node:os';
|
|
9
|
-
import { basename, dirname } from 'node:path';
|
|
10
|
-
|
|
11
|
-
import type { FileWatchFailure, FileWatchUnavailable } from '@farmslot/protocol';
|
|
12
|
-
|
|
13
|
-
// Pool configs can declare repos as `~/...`. `~` is a shell token, not a
|
|
14
|
-
// filesystem prefix — node:fs treats it literally. Expand to $HOME first.
|
|
15
|
-
export function expandTilde(p: string): string {
|
|
16
|
-
if (p === '~') return homedir();
|
|
17
|
-
if (p.startsWith('~/')) return `${homedir()}/${p.slice(2)}`;
|
|
18
|
-
return p;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface FileWatchHandle {
|
|
22
|
-
stop(): void;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface FileWatchOptions {
|
|
26
|
-
/** Wake an append reader without reading or transporting the file body. */
|
|
27
|
-
notificationsOnly?: boolean;
|
|
28
|
-
onUnavailable?: (observation: FileWatchUnavailable) => void;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Watch a single file for content changes. `onChange` receives the file's UTF-8
|
|
33
|
-
* content on every write. The parent directory remains watched so atomic file
|
|
34
|
-
* replacement does not detach the watcher from subsequent updates. Returns a
|
|
35
|
-
* handle the caller stops when done.
|
|
36
|
-
*/
|
|
37
|
-
export function watchFile(
|
|
38
|
-
filePath: string,
|
|
39
|
-
onChange: (content: string) => void,
|
|
40
|
-
options: FileWatchOptions = {},
|
|
41
|
-
): FileWatchHandle {
|
|
42
|
-
const targetPath = expandTilde(filePath);
|
|
43
|
-
let watcher: FSWatcher | null = null;
|
|
44
|
-
let stopped = false;
|
|
45
|
-
let lastObservation: string | undefined;
|
|
46
|
-
let generation = 0;
|
|
47
|
-
|
|
48
|
-
const unavailable = (error: unknown): FileWatchUnavailable => {
|
|
49
|
-
const code = (error as NodeJS.ErrnoException).code;
|
|
50
|
-
if (code === 'ENOENT') return { kind: 'deleted' };
|
|
51
|
-
const failure: FileWatchFailure = {
|
|
52
|
-
message: error instanceof Error ? error.message : String(error),
|
|
53
|
-
...(typeof code === 'string' ? { code } : {}),
|
|
54
|
-
};
|
|
55
|
-
return { kind: 'error', error: failure };
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const observe = async (): Promise<void> => {
|
|
59
|
-
if (stopped) return;
|
|
60
|
-
const current = ++generation;
|
|
61
|
-
let observation: FileWatchUnavailable | { kind: 'updated'; content: string };
|
|
62
|
-
try {
|
|
63
|
-
observation = { kind: 'updated', content: await readFile(targetPath, 'utf-8') };
|
|
64
|
-
} catch (error) {
|
|
65
|
-
observation = unavailable(error);
|
|
66
|
-
}
|
|
67
|
-
if (stopped || current !== generation) return;
|
|
68
|
-
const fingerprint = JSON.stringify(observation);
|
|
69
|
-
if (fingerprint === lastObservation) return;
|
|
70
|
-
lastObservation = fingerprint;
|
|
71
|
-
if (observation.kind === 'updated') onChange(observation.content);
|
|
72
|
-
else if (options.onUnavailable) options.onUnavailable(observation);
|
|
73
|
-
else if (observation.kind === 'error') {
|
|
74
|
-
// Content-only callers retain their last value; the parent watch stays armed
|
|
75
|
-
// so a subsequent permission change or replacement can restore delivery.
|
|
76
|
-
console.warn(`[fs.watch] read failed for ${targetPath}: ${observation.error.message}`);
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const start = (): void => {
|
|
81
|
-
if (stopped) return;
|
|
82
|
-
// Watch the parent directory even when the file already exists. Checklist markers,
|
|
83
|
-
// git, and many editors update files through atomic rename; watching the file inode
|
|
84
|
-
// sees the first replacement and then silently follows the orphaned inode.
|
|
85
|
-
const dir = dirname(targetPath);
|
|
86
|
-
if (!existsSync(dir)) {
|
|
87
|
-
if (options.onUnavailable) {
|
|
88
|
-
throw Object.assign(new Error(`Watch parent does not exist: ${dir}`), { code: 'ENOENT' });
|
|
89
|
-
}
|
|
90
|
-
// Legacy content-only consumers keep their existing inactive-watch behavior.
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
const target = basename(targetPath);
|
|
94
|
-
watcher = watch(dir, { persistent: false }, (_, filename) => {
|
|
95
|
-
if (filename != null && filename.toString() !== target) return;
|
|
96
|
-
if (options.notificationsOnly) onChange('');
|
|
97
|
-
else void observe();
|
|
98
|
-
});
|
|
99
|
-
watcher.on('error', (error) => {
|
|
100
|
-
if (stopped) return;
|
|
101
|
-
// A failed native watch can no longer prove subsequent changes. Revoke
|
|
102
|
-
// admission explicitly; the caller must establish a new watch to retry.
|
|
103
|
-
generation++;
|
|
104
|
-
watcher?.close();
|
|
105
|
-
watcher = null;
|
|
106
|
-
stopped = true;
|
|
107
|
-
if (options.onUnavailable) {
|
|
108
|
-
options.onUnavailable({
|
|
109
|
-
kind: 'error',
|
|
110
|
-
error: {
|
|
111
|
-
message: error.message,
|
|
112
|
-
...('code' in error && typeof error.code === 'string' ? { code: error.code } : {}),
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
} else {
|
|
116
|
-
throw error;
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
start();
|
|
122
|
-
|
|
123
|
-
return {
|
|
124
|
-
stop(): void {
|
|
125
|
-
stopped = true;
|
|
126
|
-
generation++;
|
|
127
|
-
if (watcher) {
|
|
128
|
-
watcher.close();
|
|
129
|
-
watcher = null;
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
};
|
|
133
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// @farmslot/capabilities — machine-local capability primitives shared by the node
|
|
2
|
-
// (primary owner) and the gateway (local fallback when no node). ADR-046.
|
|
3
|
-
|
|
4
|
-
export { expandTilde, type FileWatchHandle, type FileWatchOptions, watchFile } from './fs-watch.js';
|
|
5
|
-
export {
|
|
6
|
-
type DecodedNodeFrame,
|
|
7
|
-
decodeNodeFrame,
|
|
8
|
-
encodeNodeFrame,
|
|
9
|
-
NODE_FRAME_MAGIC,
|
|
10
|
-
} from './screen-frame.js';
|
|
11
|
-
export {
|
|
12
|
-
createH264FrameSplitter,
|
|
13
|
-
type H264FrameHandler,
|
|
14
|
-
type H264FrameSplitter,
|
|
15
|
-
} from './screen-h264.js';
|
package/src/screen-frame.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
// Shared node→gateway capture-frame codec (ADR-046). Single source of truth for the
|
|
2
|
-
// binary envelope so the node (encode) and the gateway (decode) never drift. The magic
|
|
3
|
-
// byte lives in @farmslot/protocol; the Buffer-based codec lives here (node-only) so it
|
|
4
|
-
// stays out of the UI-shared protocol bundle.
|
|
5
|
-
//
|
|
6
|
-
// Envelope: [0xAF magic][1B flags][2B width BE][2B height BE][1B slotIdLen][slotId…][payload…]
|
|
7
|
-
// flags bit0 = keyframe.
|
|
8
|
-
|
|
9
|
-
import { NODE_FRAME_MAGIC } from '@farmslot/protocol';
|
|
10
|
-
|
|
11
|
-
export { NODE_FRAME_MAGIC };
|
|
12
|
-
|
|
13
|
-
const HEADER_FIXED = 7; // magic + flags + width(2) + height(2) + slotIdLen
|
|
14
|
-
|
|
15
|
-
export function encodeNodeFrame(
|
|
16
|
-
slotId: string,
|
|
17
|
-
payload: Uint8Array,
|
|
18
|
-
keyFrame: boolean,
|
|
19
|
-
width: number,
|
|
20
|
-
height: number,
|
|
21
|
-
): Buffer {
|
|
22
|
-
const slotBytes = Buffer.from(slotId, 'utf-8');
|
|
23
|
-
const headerSize = HEADER_FIXED + slotBytes.length;
|
|
24
|
-
const buf = Buffer.alloc(headerSize + payload.length);
|
|
25
|
-
buf[0] = NODE_FRAME_MAGIC;
|
|
26
|
-
buf[1] = keyFrame ? 1 : 0;
|
|
27
|
-
buf.writeUInt16BE(width, 2);
|
|
28
|
-
buf.writeUInt16BE(height, 4);
|
|
29
|
-
buf[6] = slotBytes.length;
|
|
30
|
-
slotBytes.copy(buf, HEADER_FIXED);
|
|
31
|
-
buf.set(payload, headerSize);
|
|
32
|
-
return buf;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface DecodedNodeFrame {
|
|
36
|
-
slotId: string;
|
|
37
|
-
payload: Uint8Array;
|
|
38
|
-
keyFrame: boolean;
|
|
39
|
-
width: number;
|
|
40
|
-
height: number;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Decode a node capture frame. Returns null when the buffer is truncated. Callers
|
|
44
|
-
* should have already checked `raw[0] === NODE_FRAME_MAGIC`. */
|
|
45
|
-
export function decodeNodeFrame(raw: Buffer): DecodedNodeFrame | null {
|
|
46
|
-
if (raw.length < HEADER_FIXED) return null;
|
|
47
|
-
const flags = raw[1];
|
|
48
|
-
const keyFrame = (flags & 1) !== 0;
|
|
49
|
-
const width = raw.readUInt16BE(2);
|
|
50
|
-
const height = raw.readUInt16BE(4);
|
|
51
|
-
const slotIdLen = raw[6];
|
|
52
|
-
if (raw.length < HEADER_FIXED + slotIdLen) return null;
|
|
53
|
-
const slotId = raw.subarray(HEADER_FIXED, HEADER_FIXED + slotIdLen).toString('utf-8');
|
|
54
|
-
const payload = new Uint8Array(
|
|
55
|
-
raw.buffer,
|
|
56
|
-
raw.byteOffset + HEADER_FIXED + slotIdLen,
|
|
57
|
-
raw.length - HEADER_FIXED - slotIdLen,
|
|
58
|
-
);
|
|
59
|
-
return { slotId, payload, keyFrame, width, height };
|
|
60
|
-
}
|
package/src/screen-h264.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
// Shared H.264 frame splitter. The node and the gateway both read the raw byte stream
|
|
2
|
-
// from `adb screenrecord --output-format=h264` and slice it into individual video frames
|
|
3
|
-
// the same way: split on the 00 00 00 01 NAL start codes and group the NAL units into
|
|
4
|
-
// frames (SPS + PPS + IDR = a keyframe, a lone slice = a delta frame). This logic was
|
|
5
|
-
// copied verbatim into both services; it now lives in one place.
|
|
6
|
-
|
|
7
|
-
export type H264FrameHandler = (frame: Uint8Array, keyFrame: boolean) => void;
|
|
8
|
-
|
|
9
|
-
export interface H264FrameSplitter {
|
|
10
|
-
/** Feed a raw stdout chunk; emits complete video frames via the handler. */
|
|
11
|
-
push(chunk: Buffer): void;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function concatBuffers(parts: Buffer[]): Uint8Array {
|
|
15
|
-
const total = parts.reduce((s, p) => s + p.length, 0);
|
|
16
|
-
const out = new Uint8Array(total);
|
|
17
|
-
let offset = 0;
|
|
18
|
-
for (const p of parts) {
|
|
19
|
-
out.set(p, offset);
|
|
20
|
-
offset += p.length;
|
|
21
|
-
}
|
|
22
|
-
return out;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function createH264FrameSplitter(onFrame: H264FrameHandler): H264FrameSplitter {
|
|
26
|
-
let buffer = Buffer.alloc(0);
|
|
27
|
-
|
|
28
|
-
function drain(): void {
|
|
29
|
-
const buf = buffer;
|
|
30
|
-
const startCodes: number[] = [];
|
|
31
|
-
|
|
32
|
-
// Find all 4-byte start codes.
|
|
33
|
-
for (let i = 0; i <= buf.length - 4; i++) {
|
|
34
|
-
if (buf[i] === 0 && buf[i + 1] === 0 && buf[i + 2] === 0 && buf[i + 3] === 1) {
|
|
35
|
-
startCodes.push(i);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (startCodes.length < 2) return; // need at least 2 start codes to extract a NAL
|
|
40
|
-
|
|
41
|
-
let consumed = 0;
|
|
42
|
-
let frameStart = -1;
|
|
43
|
-
let frameIsKey = false;
|
|
44
|
-
let frameData: Buffer[] = [];
|
|
45
|
-
|
|
46
|
-
for (let i = 0; i < startCodes.length - 1; i++) {
|
|
47
|
-
const nalStart = startCodes[i];
|
|
48
|
-
const nalEnd = startCodes[i + 1];
|
|
49
|
-
const nalType = buf[nalStart + 4] & 0x1f;
|
|
50
|
-
|
|
51
|
-
if (nalType === 7 || nalType === 8) {
|
|
52
|
-
// SPS or PPS — start accumulating a keyframe.
|
|
53
|
-
if (frameStart === -1) frameStart = nalStart;
|
|
54
|
-
frameIsKey = true;
|
|
55
|
-
frameData.push(buf.subarray(nalStart, nalEnd));
|
|
56
|
-
} else if (nalType === 5) {
|
|
57
|
-
// IDR slice — completes the keyframe.
|
|
58
|
-
if (frameStart === -1) {
|
|
59
|
-
frameStart = nalStart;
|
|
60
|
-
frameIsKey = true;
|
|
61
|
-
}
|
|
62
|
-
frameData.push(buf.subarray(nalStart, nalEnd));
|
|
63
|
-
onFrame(concatBuffers(frameData), true);
|
|
64
|
-
consumed = nalEnd;
|
|
65
|
-
frameStart = -1;
|
|
66
|
-
frameIsKey = false;
|
|
67
|
-
frameData = [];
|
|
68
|
-
} else if (nalType === 1) {
|
|
69
|
-
// P-slice — standalone frame.
|
|
70
|
-
if (frameData.length > 0) {
|
|
71
|
-
// Flush accumulated SPS/PPS without IDR (shouldn't happen, but safe).
|
|
72
|
-
onFrame(concatBuffers(frameData), frameIsKey);
|
|
73
|
-
frameData = [];
|
|
74
|
-
frameIsKey = false;
|
|
75
|
-
frameStart = -1;
|
|
76
|
-
}
|
|
77
|
-
onFrame(new Uint8Array(buf.buffer, buf.byteOffset + nalStart, nalEnd - nalStart), false);
|
|
78
|
-
consumed = nalEnd;
|
|
79
|
-
} else {
|
|
80
|
-
// Skip other NAL types (SEI=6, AUD=9, etc.).
|
|
81
|
-
if (frameStart !== -1) {
|
|
82
|
-
frameData.push(buf.subarray(nalStart, nalEnd));
|
|
83
|
-
} else {
|
|
84
|
-
consumed = nalEnd;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Keep unconsumed data (last incomplete NAL + any accumulated frame data).
|
|
90
|
-
if (consumed > 0) {
|
|
91
|
-
buffer = Buffer.from(buf.subarray(frameData.length > 0 ? frameStart! : consumed));
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
push(chunk: Buffer): void {
|
|
97
|
-
buffer = Buffer.concat([buffer, chunk]);
|
|
98
|
-
drain();
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
}
|
package/src/workspace-watch.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { execFile as execFileCallback } from 'node:child_process';
|
|
2
|
-
import { type FSWatcher, watch } from 'node:fs';
|
|
3
|
-
import { lstat, realpath } from 'node:fs/promises';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
|
|
6
|
-
import { expandTilde } from './fs-watch.js';
|
|
7
|
-
interface GitDiscovery {
|
|
8
|
-
code: number;
|
|
9
|
-
stdout: string;
|
|
10
|
-
stderr: string;
|
|
11
|
-
}
|
|
12
|
-
function discoverGit(cwd: string): Promise<GitDiscovery> {
|
|
13
|
-
return new Promise((resolve, reject) =>
|
|
14
|
-
execFileCallback(
|
|
15
|
-
'git',
|
|
16
|
-
['rev-parse', '--path-format=absolute', '--git-dir', '--git-common-dir'],
|
|
17
|
-
{
|
|
18
|
-
cwd,
|
|
19
|
-
timeout: 10_000,
|
|
20
|
-
maxBuffer: 64 * 1024,
|
|
21
|
-
env: Object.fromEntries(
|
|
22
|
-
Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')),
|
|
23
|
-
),
|
|
24
|
-
},
|
|
25
|
-
(error, stdout, stderr) => {
|
|
26
|
-
// Numeric exit is Git-domain status. Spawn, timeout and signal failures are not plain directories.
|
|
27
|
-
if (error && (typeof error.code !== 'number' || error.killed || error.signal))
|
|
28
|
-
reject(error);
|
|
29
|
-
else resolve({ code: (error?.code as number) ?? 0, stdout, stderr });
|
|
30
|
-
},
|
|
31
|
-
),
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Arm all workspace bytes, names, index and refs. No periodic rereads or following symlinks. */
|
|
36
|
-
export async function armLocalWorkspace(
|
|
37
|
-
root: string,
|
|
38
|
-
changed: () => void,
|
|
39
|
-
failed: (error: Error) => void,
|
|
40
|
-
): Promise<() => void> {
|
|
41
|
-
const watchers: FSWatcher[] = [];
|
|
42
|
-
let stopped = false;
|
|
43
|
-
const close = () => {
|
|
44
|
-
stopped = true;
|
|
45
|
-
for (const watcher of watchers) watcher.close();
|
|
46
|
-
};
|
|
47
|
-
const fail = (error: Error) => {
|
|
48
|
-
if (!stopped) {
|
|
49
|
-
close();
|
|
50
|
-
failed(error);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
const actualRoot = await realpath(expandTilde(root));
|
|
54
|
-
// Root watch is armed before discovering git metadata so a concurrent checkout is fenced.
|
|
55
|
-
const add = async (target: string) => {
|
|
56
|
-
const initial = await lstat(target);
|
|
57
|
-
const watcher = watch(target, { recursive: true, persistent: false }, (_, filename) => {
|
|
58
|
-
if (stopped) return;
|
|
59
|
-
if (target === actualRoot && filename?.toString() === '.git') {
|
|
60
|
-
fail(new Error('Workspace git binding changed; retry to rearm'));
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
changed();
|
|
64
|
-
});
|
|
65
|
-
watcher.on('error', fail);
|
|
66
|
-
watchers.push(watcher);
|
|
67
|
-
// Roots and linked metadata can be atomically removed/replaced. Watching an old inode is insufficient.
|
|
68
|
-
const parent = watch(path.dirname(target), { persistent: false }, async (_, filename) => {
|
|
69
|
-
if (filename != null && filename.toString() !== path.basename(target)) return;
|
|
70
|
-
try {
|
|
71
|
-
const current = await lstat(target);
|
|
72
|
-
if (current.ino !== initial.ino || current.dev !== initial.dev)
|
|
73
|
-
fail(new Error('Workspace source root replaced'));
|
|
74
|
-
} catch (error) {
|
|
75
|
-
fail(error instanceof Error ? error : new Error(String(error)));
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
parent.on('error', fail);
|
|
79
|
-
watchers.push(parent);
|
|
80
|
-
};
|
|
81
|
-
try {
|
|
82
|
-
await add(actualRoot);
|
|
83
|
-
const discovery = await discoverGit(actualRoot);
|
|
84
|
-
if (discovery.code === 0) {
|
|
85
|
-
for (const metadata of new Set(discovery.stdout.trim().split('\n'))) {
|
|
86
|
-
const target = await realpath(metadata);
|
|
87
|
-
const relative = path.relative(actualRoot, target);
|
|
88
|
-
if (relative.startsWith('..') || path.isAbsolute(relative)) await add(target);
|
|
89
|
-
}
|
|
90
|
-
} else if (discovery.code !== 128 || !discovery.stderr.includes('not a git repository')) {
|
|
91
|
-
throw new Error(`Git metadata discovery failed (${discovery.code}): ${discovery.stderr}`);
|
|
92
|
-
}
|
|
93
|
-
// Exit 128 with Git's specific diagnostic admits a plain directory, never runner state.
|
|
94
|
-
if (stopped) throw new Error('Workspace source failed while arming');
|
|
95
|
-
return close;
|
|
96
|
-
} catch (error) {
|
|
97
|
-
close();
|
|
98
|
-
throw error;
|
|
99
|
-
}
|
|
100
|
-
}
|