@dorvyn/server 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class d extends Error{status;constructor(e,r){super(e),this.name="DorvynServerError",this.status=r}}const b=/^dvn_sk_(live|test)_[0-9A-HJKMNP-TV-Z]{59}$/,m=/^sha384-[A-Za-z0-9+/]{64}$/,T=/^[A-Za-z0-9._-]{1,64}$/,g={us:"https://export.dorvyn.dev",eu:"https://eu.export.dorvyn.dev"},k="application/vnd.openxmlformats-officedocument.wordprocessingml.document",u=t=>globalThis.process?.env?.[t];function p(t){const e=t.secretKey??u("DORVYN_SECRET_KEY");if(!e||!b.test(e))throw new d("Set DORVYN_SECRET_KEY on the server to your secret key (dvn_sk_…).",500);return{secretKey:e,api:t.api??u("DORVYN_API")??"https://api.dorvyn.dev",exportUrl:t.exportUrl??u("DORVYN_EXPORT_URL"),fetch:t.fetch??fetch}}async function l(t,e,r,a=!0){let o;try{o=await t.fetch(e,{method:"POST",headers:{"content-type":"application/json",...a&&{authorization:`Bearer ${t.secretKey}`}},body:JSON.stringify(r)})}catch{throw new d("Dorvyn couldn't be reached.",502)}if(!o.ok){const s=await o.text().catch(()=>"");let n=s;try{n=JSON.parse(s).message??s}catch{}throw new d(n||`Dorvyn refused (${o.status}).`,o.status)}return o}function y(t){if(typeof t!="string"||t.length>8192)return null;try{return JSON.parse(atob(t.split(".")[1].replace(/-/g,"+").replace(/_/g,"/")))}catch{return null}}async function x(t){const e=p(t),r=t.request??{},a=n=>typeof n=="string"&&T.test(n)?n:void 0,s=await(await l(e,`${e.api}/v1/grants/server`,{sdk:typeof r.sdk=="string"&&/^\d{1,4}\.\d{1,4}\.\d{1,4}$/.test(r.sdk)?r.sdk:"0.2.0",build:a(r.build),session:a(r.session),device:a(r.device),nonce:O(),origin:t.origin,subject:t.subject})).json().catch(()=>null);if(typeof s?.grant!="string"||typeof s.exp!="number")throw new d("Dorvyn sent an answer this package doesn't understand.",502);return{grant:s.grant,exp:s.exp}}async function E(t){const e=p(t),r=await(await l(e,`${e.api}/v1/exports/ticket`,{grant:t.grant})).json();if(typeof r?.ticket!="string")throw new d("Dorvyn sent an answer this package doesn't understand.",502);const a=y(t.grant)?.region,o=e.exportUrl??(a==="eu"?g.eu:g.us),s=new TextEncoder().encode(JSON.stringify(t.input));let n;if(s.length+t.grant.length+r.ticket.length+96<=R)n=await l(e,`${o}/v1/export`,{grant:t.grant,ticket:r.ticket,input:t.input},!1);else{const c=await(await l(e,`${o}/v1/export/upload`,{grant:t.grant,ticket:r.ticket,size:s.length},!1)).json();if(typeof c?.key!="string"||typeof c.url!="string")throw new d("Dorvyn sent an answer this package doesn't understand.",502);let i;try{i=await e.fetch(c.url,{method:"PUT",headers:{"content-type":"application/json"},body:s})}catch{throw new d("The document couldn't be uploaded to Dorvyn.",502)}if(!i.ok)throw new d("The document couldn't be uploaded to Dorvyn.",502);n=await l(e,`${o}/v1/export`,{grant:t.grant,ticket:r.ticket,upload:c.key},!1)}return new Uint8Array(await n.arrayBuffer())}const R=5*1024*1024;function _(t){const e=t.engine?.url??u("DORVYN_ENGINE_URL"),r=t.engine?.integrity??u("DORVYN_ENGINE_INTEGRITY");if(r&&!m.test(r))throw new d("The engine's integrity must be sha384-… (64 base64 characters).",500);const a=e||r?{...e&&{url:e},...r&&{integrity:r}}:void 0,o=(s,n)=>Response.json({message:n},{status:s,headers:{"cache-control":"no-store"}});return async s=>{try{const n=new URL(s.url).pathname.replace(/\/+$/,"").split("/").pop();if(s.method!=="POST")return o(405,"Method not allowed.");if(n!=="grant"&&n!=="export")return o(404,"Not found.");const c=s.headers.get("origin");if(!c)return o(400,"No Origin header.");if(t.origins&&!t.origins.includes(c))return o(403,"Not from an allowed page.");const i=await t.user(s);if(!i)return o(401,"Sign in to edit.");const f=await s.json().catch(()=>({}));if(n==="grant"){const v=await x({...t,origin:c,subject:i,request:f});return Response.json({...v,...a&&{engine:a}},{headers:{"cache-control":"no-store"}})}const h=y(f.grant);if(!h||h.sub!==i||h.origin!==c)return o(403,"That license is for another user or page.");const w=await E({...t,grant:f.grant,input:f.input});return new Response(w,{headers:{"content-type":k,"cache-control":"no-store"}})}catch(n){const c=n instanceof d?n.status:500;return c>=500&&console.error("Dorvyn route:",n instanceof Error?n.name:typeof n,n instanceof d?n.status:""),o(c,c>=500?"The editor's license isn't available right now.":String(n.message))}}}function D(t){return async(e,r)=>{let a;if(e.body!==void 0&&typeof e.body=="object"&&!(e.body instanceof Uint8Array))a=JSON.stringify(e.body);else{const c=[];for await(const i of e)c.push(typeof i=="string"?new TextEncoder().encode(i):i);a=new TextDecoder().decode(N(c))}const o=new Headers;for(const[c,i]of Object.entries(e.headers))typeof i=="string"&&o.set(c,i);const s=new URL(e.originalUrl??e.url??"/",`http://${e.headers.host??"localhost"}`),n=await t(new Request(s,{method:e.method??"POST",headers:o,body:e.method==="GET"?void 0:a}));r.writeHead(n.status,Object.fromEntries(n.headers)),r.end(new Uint8Array(await n.arrayBuffer()))}}function N(t){const e=new Uint8Array(t.reduce((a,o)=>a+o.length,0));let r=0;for(const a of t)e.set(a,r),r+=a.length;return e}function O(){const t=new Uint8Array(16);return globalThis.crypto.getRandomValues(t),[...t].map(e=>e.toString(16).padStart(2,"0")).join("")}export{d as DorvynServerError,_ as dorvynRoutes,D as nodeHandler,E as requestExport,x as requestGrant};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dorvyn/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Dorvyn on your server: grants and exports for your signed-in users, with your secret key from the environment.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Dorvyn Pvt Ltd <support@dorvyn.dev>",
|