@kubun/plugin-rpc 0.9.0 → 0.10.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.
Files changed (2) hide show
  1. package/lib/handlers.js +1 -1
  2. package/package.json +18 -16
package/lib/handlers.js CHANGED
@@ -1 +1 @@
1
- import{consume as a}from"@enkaku/generator";import{stringifyToken as t}from"@enkaku/token";import{DocumentID as e}from"@kubun/id";import{convertPatchInput as r,createMutationOperations as n}from"@kubun/mutation";function i(a){let t={data:a.data};return null!=a.errors&&(t.errors=a.errors.map(a=>a.toJSON())),null!=a.extensions&&(t.extensions=a.extensions),t}function o(a,t){let e=a.message?.payload,r=e?.sub??e?.iss??t;return{id:a.param.id,text:a.param.text,variables:a.param.variables??{},viewerDID:r}}export function createGraphHandlers(s){let{allowDelegatedMutations:u,engine:l,getRandomValues:p,graph:c,defaultDID:d,hlc:m,signingIdentity:g,transactionManager:h}=s;return{"graph/deploy":async a=>await l.deployGraph({clusters:a.param.clusters,id:a.param.id,name:a.param.name,plugins:a.param.plugins}),"graph/list":async()=>await l.listGraphs(),"graph/load":async a=>await l.loadGraph({id:a.param.id}),"graph/mutate":async a=>{let{viewerDID:s}=o(a,d);if(null!=a.param.mutations){let t=a.param.mutations,e=Object.keys(t),r=e.map(a=>t[a]),{results:n}=await c.applyVerifiedMutations({tokens:r}),o={};for(let a=0;a<e.length;a++){let t=n[a].document;if(null==t)throw Error("Unexpected dropped mutation in pre-signed apply (no gate configured)");o[e[a]]=t}return i(await c.execute({graphID:a.param.id,text:a.param.text,variables:a.param.variables??{},viewerDID:s,contextExtensions:{executeCreateMutation:async a=>o[a.info.path.key],executeSetMutation:async a=>o[a.info.path.key],executeUpdateMutation:async a=>o[a.info.path.key],executeRemoveMutation:async()=>{}}}))}if(!u)throw Error("Delegated mutations not enabled. Set allowDelegatedMutations: true or provide pre-signed mutations.");if(null==g)throw Error("Delegated mutations require a SigningIdentity on the engine");let y=a.param.transactionID??null;if(null==y)return i(await l.mutateGraph({id:a.param.id,text:a.param.text,variables:a.param.variables??{},viewerDID:s}));let w=async a=>{let r=t(await g.signToken(a)),n=h.getTransaction(y);if(null==n)throw Error(`Transaction not found or expired: ${y}`);let i=e.fromString(a.sub),o="change"===a.typ,s={id:a.sub,model:i.model.toString(),owner:a.aud??a.iss,data:o?null:a.data,createdAt:new Date,updatedAt:o?new Date:null};return n.mutations.push({mutation:a,jwt:r,authorDID:a.iss,documentID:s.id,result:s}),s},f=n({issuer:g.id,hlc:m,getRandomValues:p,processSetMutation:async a=>await w(a),processChangeMutation:async a=>await w(a)});return i(await c.execute({graphID:a.param.id,text:a.param.text,variables:a.param.variables??{},viewerDID:s,contextExtensions:{executeCreateMutation:async a=>await f.createDocument({modelID:a.modelID,data:a.data}),executeSetMutation:async a=>await f.setDocument({modelID:a.modelID,unique:a.unique,data:a.data}),executeUpdateMutation:async a=>await f.updateDocument({docID:a.input.id,patch:r(a.input.patch)}),executeRemoveMutation:async a=>{await f.removeDocument({docID:a.id})}}}))},"graph/query":async a=>i(await l.queryGraph(o(a,d))),"graph/subscribe":async t=>{let e=await l.subscribeToGraph(o(t,d));if(t.signal.aborted)return null;if("errors"in e)return i(e);let r=t.writable.getWriter();try{await a(e,async a=>{await r.write(i(a))},t.signal)}catch(a){if(!t.signal.aborted)throw a}finally{try{await r.close()}catch{}}return null},"graph/beginTransaction":async a=>{let t=a.message?.payload,e=t?.iss??d;return await l.loadGraph({id:a.param.id}),{transactionID:h.beginTransaction(e,a.param.id).id}},"graph/commitTransaction":async a=>{let t=h.getTransaction(a.param.transactionID);if(null==t)throw Error(`Transaction not found or expired: ${a.param.transactionID}`);if(t.mutations.length>0){let a=t.mutations.map(a=>a.jwt);await c.applyVerifiedMutations({tokens:a})}return h.markCommitted(a.param.transactionID),{success:!0}},"graph/rollbackTransaction":async a=>(h.rollbackTransaction(a.param.transactionID),{success:!0})}}
1
+ import{consume as a}from"@enkaku/generator";import{stringifyToken as t}from"@enkaku/token";import{DocumentID as e}from"@kubun/id";import{convertPatchInput as r,createMutationOperations as n,WriteAccessDeniedError as i}from"@kubun/mutation";import{GraphQLError as o}from"graphql";function s(a){let t={data:a.data};return null!=a.errors&&(t.errors=a.errors.map(a=>a.toJSON())),null!=a.extensions&&(t.extensions=a.extensions),t}function u(a,t){let e=a.message?.payload,r=e?.sub??e?.iss??t;return{id:a.param.id,text:a.param.text,variables:a.param.variables??{},viewerDID:r}}export function createGraphHandlers(c){let{allowDelegatedMutations:l,engine:p,getRandomValues:d,graph:m,defaultDID:g,hlc:h,signingIdentity:y,transactionManager:w}=c;return{"graph/deploy":async a=>await p.deployGraph({clusters:a.param.clusters,id:a.param.id,name:a.param.name,plugins:a.param.plugins}),"graph/list":async()=>await p.listGraphs(),"graph/load":async a=>await p.loadGraph({id:a.param.id}),"graph/mutate":async a=>{let{viewerDID:c}=u(a,g);if(null!=a.param.mutations){let t,e=a.param.mutations,r=Object.keys(e),n=r.map(a=>e[a]);try{t=await m.applyVerifiedMutations({tokens:n})}catch(a){if(a instanceof i)return s({data:null,errors:[new o(a.message,{extensions:{code:a.code,docID:a.docID}})]});throw a}let{results:u}=t,l={};for(let a=0;a<r.length;a++){let t=u[a].document;if(null==t)throw Error("Unexpected dropped mutation in pre-signed apply (no gate configured)");l[r[a]]=t}return s(await m.execute({graphID:a.param.id,text:a.param.text,variables:a.param.variables??{},viewerDID:c,contextExtensions:{executeCreateMutation:async a=>l[a.info.path.key],executeSetMutation:async a=>l[a.info.path.key],executeUpdateMutation:async a=>l[a.info.path.key],executeRemoveMutation:async()=>{}}}))}if(!l)throw Error("Delegated mutations not enabled. Set allowDelegatedMutations: true or provide pre-signed mutations.");if(null==y)throw Error("Delegated mutations require a SigningIdentity on the engine");let f=a.param.transactionID??null;if(null==f)return s(await p.mutateGraph({id:a.param.id,text:a.param.text,variables:a.param.variables??{},viewerDID:c}));let D=async a=>{let r=t(await y.signToken(a)),n=w.getTransaction(f);if(null==n)throw Error(`Transaction not found or expired: ${f}`);let i=e.fromString(a.sub),o="change"===a.typ,s={id:a.sub,model:i.model.toString(),owner:a.aud??a.iss,data:o?null:a.data,createdAt:new Date,updatedAt:o?new Date:null};return n.mutations.push({mutation:a,jwt:r,authorDID:a.iss,documentID:s.id,result:s}),s},x=n({issuer:y.id,hlc:h,getRandomValues:d,processSetMutation:async a=>await D(a),processChangeMutation:async a=>await D(a)});return s(await m.execute({graphID:a.param.id,text:a.param.text,variables:a.param.variables??{},viewerDID:c,contextExtensions:{executeCreateMutation:async a=>await x.createDocument({modelID:a.modelID,data:a.data}),executeSetMutation:async a=>await x.setDocument({modelID:a.modelID,unique:a.unique,data:a.data}),executeUpdateMutation:async a=>await x.updateDocument({docID:a.input.id,patch:r(a.input.patch)}),executeRemoveMutation:async a=>{await x.removeDocument({docID:a.id})}}}))},"graph/query":async a=>s(await p.queryGraph(u(a,g))),"graph/subscribe":async t=>{let e=await p.subscribeToGraph(u(t,g));if(t.signal.aborted)return null;if("errors"in e)return s(e);let r=t.writable.getWriter();try{await a(e,async a=>{await r.write(s(a))},t.signal)}catch(a){if(!t.signal.aborted)throw a}finally{try{await r.close()}catch{}}return null},"graph/beginTransaction":async a=>{let t=a.message?.payload,e=t?.iss??g;return await p.loadGraph({id:a.param.id}),{transactionID:w.beginTransaction(e,a.param.id).id}},"graph/commitTransaction":async a=>{let t=w.getTransaction(a.param.transactionID);if(null==t)throw Error(`Transaction not found or expired: ${a.param.transactionID}`);if(t.mutations.length>0){let e=t.mutations.map(a=>a.jwt);try{await m.applyVerifiedMutations({tokens:e})}catch(t){if(t instanceof i)return w.rollbackTransaction(a.param.transactionID),{success:!1,error:{code:t.code,message:t.message,docID:t.docID}};throw t}}return w.markCommitted(a.param.transactionID),{success:!0}},"graph/rollbackTransaction":async a=>(w.rollbackTransaction(a.param.transactionID),{success:!0})}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/plugin-rpc",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "license": "see LICENSE.md",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -14,24 +14,26 @@
14
14
  ],
15
15
  "sideEffects": false,
16
16
  "dependencies": {
17
- "@enkaku/generator": "^0.15.0",
18
- "@enkaku/runtime": "^0.15.0",
19
- "@enkaku/server": "^0.15.0",
20
- "@enkaku/token": "^0.15.0",
17
+ "@enkaku/generator": "^0.16.0",
18
+ "@enkaku/runtime": "^0.16.0",
19
+ "@enkaku/server": "^0.16.0",
20
+ "@enkaku/token": "^0.16.0",
21
21
  "graphql": "^16.13.2",
22
- "@kubun/db": "^0.9.0",
23
- "@kubun/engine": "^0.9.0",
24
- "@kubun/graphql": "^0.9.0",
25
- "@kubun/hlc": "^0.9.0",
26
- "@kubun/mutation": "^0.9.0",
27
- "@kubun/protocol": "^0.9.0"
22
+ "@kubun/db": "^0.10.0",
23
+ "@kubun/hlc": "^0.10.0",
24
+ "@kubun/graphql": "^0.10.0",
25
+ "@kubun/mutation": "^0.10.0",
26
+ "@kubun/protocol": "^0.10.0",
27
+ "@kubun/engine": "^0.10.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@enkaku/transport": "^0.15.0",
31
- "@kubun/client": "^0.9.0",
32
- "@kubun/test-utils": "^0.9.0",
33
- "@kubun/store-graph": "^0.9.0",
34
- "@kubun/id": "^0.9.0"
30
+ "@enkaku/capability": "^0.16.0",
31
+ "@enkaku/transport": "^0.16.0",
32
+ "@kubun/client": "^0.10.0",
33
+ "@kubun/id": "^0.10.0",
34
+ "@kubun/store-graph": "^0.10.0",
35
+ "@kubun/store-p2p": "^0.10.0",
36
+ "@kubun/test-utils": "^0.10.0"
35
37
  },
36
38
  "scripts": {
37
39
  "build:clean": "del lib",