Allow fetch in db library vm contexts

Db library scripts are basically a JS module scoped to the db, and
handlers already get fetch — but runLibrary's own vm context only
bound sql/console, so any library calling fetch (e.g. the chat db's
OpenAI call) threw ReferenceError. Since library calls are often
fire-and-forget inside setTimeout (no rejection handler), that
crashed the whole process. Bind fetch in all three places a library
script runs: request-time (runLibrary), the library-editor save eval,
and repl.

Also add `bliss link` to bliss-cli for jumping straight to a
structure/route/template/db's workshop editor URL, including
resolving a db by its require('db')(alias) name.
This commit is contained in:
Your Name 2026-08-02 23:19:24 -04:00
parent 44f345fff0
commit 03a9d2143d
4 changed files with 62 additions and 4 deletions

View file

@ -103,10 +103,11 @@ function makeConsole(structureId, routeId) {
};
}
// Evaluate a db's "library" script with `sql` and `console` bound, returning
// its exports. Each library runs in its own context so nothing leaks between dbs.
// Evaluate a db's "library" script with `sql`, `console`, and `fetch` bound,
// returning its exports. Each library runs in its own context so nothing
// leaks between dbs.
function runLibrary(sql, librarySource, console) {
const libContext = vm.createContext({ sql, console, module: { exports: null } });
const libContext = vm.createContext({ sql, console, fetch, module: { exports: null } });
vm.runInContext(librarySource, libContext);
return libContext.module.exports;
}
@ -548,6 +549,7 @@ app.put("/workshop/:structure_id/db/:db_id/library", (req, res) => {
let context = vm.createContext({
module: { exports: null },
sql: dbInstance,
fetch,
console: {
log: (...args) => capturedOutput.push(inspectArgs(args)),
},
@ -569,6 +571,7 @@ app.post("/workshop/:structure_id/db/:db_id/repl", (req, res) => {
let context = vm.createContext({
module: { exports: null },
sql: dbInstance,
fetch,
console: {
log: (...args) => capturedOutput.push(inspectArgs(args)),
},