Download Game! Currently 71 players and visitors. Last logged in:DealRauskisPereKlarence

Blitzer's Blog >> 72070

Back to blogs index
Posted: 05 Sep 2026 15:04 [ permalink ]
Act as an Expert Node.js Tooling Developer.

The Goal: Write a headless Node.js daemon (host_agent.js) that synchronizes a
local physical directory with a remote WebSocket DMZ server.

Technical Requirements:

Use chokidar to monitor a local directory (./gamelib/mesh/) for file
additions, changes, and deletions.

Connect to a WebSocket server at ws://localhost:1777.

When chokidar detects a local change, read the file and broadcast: { type:
'VFS_WRITE', path: 'mesh/filename.ext', data, timestamp }.

Listen for incoming messages from the WebSocket. When a VFS_WRITE payload
arrives from the DMZ, write the data payload directly to the physical disk
inside ./gamelib/mesh/.

Crucial: Implement a debounce or lock mechanism to prevent infinite feedback
loops (e.g., the script writing to disk shouldn't trigger chokidar to
immediately broadcast that same file back to the server).

Please provide the complete script.

Thank you

==

Here is the complete script for your `host_agent.js` daemon.

To make this robust for game development (where mesh files like `.glb`,
`.fbx`, or `.obj` can be binary), I have implemented **Base64 encoding** for
the payload data. I've also included an auto-reconnect mechanism for the
WebSocket and a strict time-based Mutex (lock) to handle the echo cancellation
perfectly.

### Prerequisites

You will need to install the required dependencies:

```bash
npm install ws chokidar

```

### The Script (`host_agent.js`)

```javascript

```

### Architecture Notes & Best Practices Used:

1. **Echo Cancellation (The Lock Mechanism):**
When a `VFS_WRITE` comes in over the WebSocket, the script logs the absolute
file path into a `Map` with an expiration timestamp (`Date.now() + 1500ms`)
*before* it starts touching the filesystem. When Chokidar inevitably detects
this file write, it checks the `Map`. Because the timestamp hasn't expired, it
ignores the event and stops the infinite loop dead in its tracks.
2. **`awaitWriteFinish` Configuration:**
Chokidar is configured with `awaitWriteFinish`. When saving large mesh files
locally (like a 50MB `.glb`), the OS might trigger multiple `change` events as
chunks are written. This setting tells Chokidar to wait until the file hasn't
been modified for 300ms before firing, ensuring you only broadcast fully
constructed files.
3. **Directory Traversal Protection:**
The `handleIncomingWrite` method maps the requested `path` back to an absolute
directory, and then uses `startsWith(SYNC_DIR)` to ensure a malicious or
glitched server cannot overwrite arbitrary files on the host machine (e.g.,
`path: "mesh/../../windows/system32/..."`).
4. **Binary Safety:**
Files are read using `{ encoding: 'base64' }` and written using
`Buffer.from(msg.data, 'base64')`. Because `JSON.stringify()` cannot handle
raw binary data safely, casting your meshes to Base64 guarantees they will
perfectly survive the WebSocket transmission.