Your server
@dorvyn/server reference
Everything @dorvyn/server exports. For a walkthrough, start with Strict mode. In Next.js, import the same from @dorvyn/next/server.
It has no dependencies and runs on Node 18 or later and on edge runtimes (Cloudflare Workers, Vercel Edge, Deno, Bun). It's for servers only: never import it in a page.
dorvynRoutes(options)#
Returns a handler, (request: Request) => Promise<Response>, that answers POST …/grant and POST …/export. The last part of the path picks which. Anything else gets 404, and other methods get 405.
import { dorvynRoutes } from "@dorvyn/server";
const handler = dorvynRoutes({
user: async (request) => (await getSession(request))?.userId,
});| Option | Type | Default | What it is |
|---|---|---|---|
user | (request: Request) => string | null | undefined | Promise<…> | (required) | The signed-in user's opaque id (1 to 128 of A–Z a–z 0–9 . _ : @ | -). Nothing means nobody is signed in, and the answer is 401 |
origins | string[] | Any of your application's domains | Only these page addresses may ask, for example ["https://app.example.com"]. Dorvyn checks the address against your application's domains either way |
secretKey | string | DORVYN_SECRET_KEY | Your secret key |
exportUrl | string | DORVYN_EXPORT_URL, or Dorvyn's for your region | Your own export service |
engine | { url?: string } | DORVYN_ENGINE_URL, or Dorvyn's CDN | Where pages load the editor's engine, if you serve it yourself (Options) |
fetch | typeof fetch | the global fetch | For a proxy, or tests |
The handler's answers:
| Status | Body | Why |
|---|---|---|
200 | the grant (for …/grant), or the .docx file (for …/export) | |
400 | { "message": "No Origin header." } | Not called by a browser |
401 | { "message": "Sign in to edit." } | user returned nothing |
403 | { "message": "Not from an allowed page." } | The page isn't in origins |
403 | { "message": "That license is for another user or page." } | An export with a grant for someone else |
4xx | { "message": "…" } | Dorvyn refused, with its reason (Messages) |
5xx | { "message": "The editor's license isn't available right now." } | Your server's setup, or Dorvyn couldn't be reached. The details are logged on your server, never sent to the page |
Every answer has cache-control: no-store.
nodeHandler(handler)#
Wraps a handler for Node's http and Express: (req, res) => Promise<void>. If Express has already parsed the JSON body, it uses that. Otherwise it reads the body itself.
The handler gets a web Request made from req, so user sees headers and cookies but not what your middleware added to req (req.user, req.session). Read those in a closure:
app.post("/api/dorvyn/:action", express.json({ limit: "250mb" }), (req, res) =>
nodeHandler(dorvynRoutes({ user: () => req.user?.id }))(req, res),
);requestGrant(options) and requestExport(options)#
The two calls dorvynRoutes makes, for wiring of your own: a queue, a different framework, extra checks.
import { requestExport, requestGrant } from "@dorvyn/server";
// A grant for this page and user. `request` is the body the editor sent.
const grant = await requestGrant({ origin: request.headers.get("origin")!, subject: user.id, request: body });
// Saves as .docx: the editor's own grant and its document. Returns the file's bytes.
const bytes = await requestExport({ grant: body.grant, input: body.input });| Function | Options (besides secretKey, exportUrl, fetch) | Returns |
|---|---|---|
requestGrant | origin (the page's origin), subject (the user's id), request (what the editor sent) | { grant, exp }: pass it to the editor as it is |
requestExport | grant (the grant the editor holds), input (the document the editor sent) | Uint8Array, the .docx |
Only export under the grant of the user who is asking, for the page that asked: dorvynRoutes checks both, so prefer it unless you need something it can't do.
With wiring of your own, the editor can still fetch its grants your way with the getGrant option (Options).
DorvynServerError#
Everything above throws DorvynServerError when Dorvyn refuses or can't be reached. It has message (Dorvyn's reason, in words you can show) and status (the HTTP status to answer with).
| Message | Status | What to do |
|---|---|---|
| Set DORVYN_SECRET_KEY on the server to your secret key (dvn_sk_…). | 500 | The variable is missing, or isn't a secret key. Check your server's environment, and restart it after changing it |
| Dorvyn couldn't be reached. | 502 | Your server's network, a firewall, or an outage. The editor keeps editing for up to 24 hours meanwhile |
| Dorvyn sent an answer this package doesn't understand. | 502 | Update @dorvyn/server |
| This key can't be used here. | 401 | See Messages |
| Too many requests. Try again in a minute. | 429 | A limit was reached. The editor tries again by itself |
Types#
interface DorvynServerOptions { secretKey?: string; exportUrl?: string; fetch?: typeof fetch }
interface DorvynRoutesOptions extends DorvynServerOptions {
user: (request: Request) => string | null | undefined | Promise<string | null | undefined>;
origins?: string[];
engine?: { url?: string };
}
interface IssuedGrant { grant: string; exp: number }