Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ requires one whenever NATS is reachable off-loopback.
await client.get(uri, ref) // fetch (ref = branch/commit hash to pin a version)
await client.diff(uri, newTemplate) // semantic diff: stored version vs. your draft
await client.publish(uri, template, slots) // publish a new version + notify subscribers
client.subscribe(uri, onChange) // be notified the moment the prompt changes
client.subscribe(uri, onChange, { onError }) // be notified the moment the prompt changes;
// onChange errors go to onError (default console.error)
```

## Live updates
Expand Down
17 changes: 11 additions & 6 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ class PromptClient {
// (structural | localized tweak | minor edit | new | ""), so an agent can
// auto-reload a tweak but hold a structural change. Needs the optional
// `@nats-io/transport-node` dependency and natsUrl set. Returns the connection.
async subscribe(uri, onChange) {
// A throw or rejection from onChange goes to onError (default: console.error)
// instead of becoming an unhandled rejection that exits the host process.
async subscribe(uri, onChange, { onError = (err) => console.error(`priompt subscribe ${uri}:`, err) } = {}) {
if (!this._natsUrl) throw new Error("set natsUrl on PromptClient to subscribe");
const { connect } = require("@nats-io/transport-node");
await checkNatsAuth(this._natsUrl, this._natsToken);
Expand All @@ -199,12 +201,15 @@ class PromptClient {
let ev;
try {
ev = JSON.parse(m.string());
} catch {
ev = { version: m.string() }; // pre-0.7 bare-hash body
}
onChange(ev.version || "", ev.classification || "");
} catch {}
// pre-0.7 bare-hash body, or anything that isn't an event object
// (an all-digit hash parses as a number, "null" as null).
if (!ev || typeof ev !== "object") ev = { version: m.string() };
Promise.resolve()
.then(() => onChange(String(ev.version || ""), ev.classification || ""))
.catch(onError);
}
})();
})().catch(onError);
return nc;
}

Expand Down
32 changes: 32 additions & 0 deletions test/subscribe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,35 @@ test("subscribe delivers version and classification", async (t) => {
});
assert.deepStrictEqual(await got, { version: "abc123", classification: "minor edit" });
});

test("a throwing onChange or a malformed body does not crash the process", async (t) => {
const broker = await startFakeNats({ token: "s3cret" });
t.after(() => broker.close());
const client = new PromptClient({ host: "localhost:1", natsUrl: broker.url.replace("nats://", "nats://s3cret@") });
t.after(() => client.close());

const errors = [];
const seen = [];
const done = new Promise((resolve) => {
const onChange = async (version) => {
seen.push(version);
if (seen.length === 1) throw new Error("sync-ish handler bug");
if (seen.length === 2) return Promise.reject(new Error("async handler bug"));
if (seen.length === 4) resolve();
};
client.subscribe("priompt://acme/a", onChange, { onError: (err) => errors.push(err.message) })
.then((nc) => {
t.after(() => nc.close());
return nc.flush();
})
.then(() => {
broker.publish("priompt.acme.a", JSON.stringify({ version: "v1" }));
broker.publish("priompt.acme.a", JSON.stringify({ version: "v2" }));
broker.publish("priompt.acme.a", "null");
broker.publish("priompt.acme.a", "12345678");
});
});
await done;
assert.deepStrictEqual(seen, ["v1", "v2", "null", "12345678"]);
assert.deepStrictEqual(errors, ["sync-ish handler bug", "async handler bug"]);
});
Loading