Forward JSON from a server#
Schedule a client's request body on the server and send the result back without parsing the JSON on either side.
The raw exports take JSON text and return JSON text. If the request body is already ProjectData, it can go straight into schedule, and the returned envelope can go straight into the response. The engine does all the validation.
Prerequisites#
- The Node build of the module (
pkg-node/). See Quick start. - Clients that send
ProjectDataas the request body and read the envelope from the response.
Steps#
-
Load the module and fix the solve options on the server. The client sends only the data:
const = ; const = ; -
Read the request body as a string. Don't call
JSON.parseon it: -
Pass the body to
scheduleand write the returned string to the response:const = ; ; ;Bad input doesn't throw. Malformed JSON, an empty body or an invalid field comes back as an
ok: falseenvelope, just like a successful result. -
Catch host-level throws around the call. Only an internal engine failure (a
WebAssembly.RuntimeError) gets pastschedulethis way. Answer it with a 500:let ;
The complete server:
const = ;
const = ;
const = ;
http
..;
Choose the HTTP status#
The server above always answers 200 and leaves the ok check to the client. To send a 4xx for an error envelope instead, parse the envelope on the server. That's the only reason to parse it:
const = . ? 200 : 422;
;
;
Don't test the text with something like envelope.startsWith('{"ok":true'). The key order in the envelope isn't part of the wire format.
Keep a project per document#
schedule builds a project, solves it and drops it on every request. For an editor that sends small edits, keep one JsonProject per open document and forward each body to the matching call:
| Request body | Call |
|---|---|
ProjectData, on load or resync |
solve(body, options) |
SchedulingChanges |
apply(body), then solve(undefined, options) |
SchedulingChanges, as a preview |
preview(body, options) |
const = ;
const = new; // document id → JsonProject
A body that fails solve with a parse or validation error leaves the project as it was. The next solve(undefined, …) returns the previous result. Free each project when its document closes. See Freeing handles.
Keep the event loop free#
Every export is synchronous. While schedule or solve runs on a large project, the server handles no other request. Run the module in worker threads and pass the body and envelope strings to and from them. See Run in a worker.
Result#
Start the server with node server.js, then post a project:
The response body is the envelope, exactly as schedule returned it:
Post a date that doesn't exist, such as "start_date":"2026-13-01", and the response is still a 200 with an error envelope:
See Error handling for every error kind the client can get back.