@kubun/plugin-rpc 0.8.1 → 0.9.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/lib/handlers.js +1 -1
- package/lib/index.d.ts +6 -3
- package/lib/index.js +1 -1
- package/package.json +16 -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:
|
|
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})}}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ServerTransportOf } from '@enkaku/protocol';
|
|
2
|
-
import { type Server } from '@enkaku/server';
|
|
2
|
+
import { type AccessRules, type Server } from '@enkaku/server';
|
|
3
3
|
import type { KubunPlugin, PluginFactoryParams } from '@kubun/engine';
|
|
4
4
|
import type { GraphProtocol } from '@kubun/protocol';
|
|
5
5
|
import { type TransactionManagerOptions } from './transaction-manager.js';
|
|
@@ -9,8 +9,11 @@ export type ServerTransport = ServerTransportOf<GraphProtocol>;
|
|
|
9
9
|
* Options for the RPC plugin.
|
|
10
10
|
*/
|
|
11
11
|
export type RPCPluginOptions = {
|
|
12
|
-
/**
|
|
13
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Enkaku access rules. `false` disables auth entirely. Default: `{}`
|
|
14
|
+
* (auth required, no procedures publicly accessible — server-only access).
|
|
15
|
+
*/
|
|
16
|
+
accessRules?: false | AccessRules;
|
|
14
17
|
/** Allow the engine to sign mutations on behalf of clients (delegated mode). Default: false. */
|
|
15
18
|
allowDelegatedMutations?: boolean;
|
|
16
19
|
/** Configuration for the transaction manager. */
|
package/lib/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{serve as e}from"@enkaku/server";import{isSigningIdentity as t}from"@enkaku/token";import{createGraphHandlers as n}from"./handlers.js";import{TransactionManager as i}from"./transaction-manager.js";export function createRPCPlugin(r){return
|
|
1
|
+
import{serve as e}from"@enkaku/server";import{isSigningIdentity as t}from"@enkaku/token";import{createGraphHandlers as n}from"./handlers.js";import{TransactionManager as i}from"./transaction-manager.js";export function createRPCPlugin(r){return a=>{let o=[],s=t(a.identity)?a.identity:null,g=new i({getRandomID:a.runtime.getRandomID,...r?.transactionConfig}),l=n({allowDelegatedMutations:r?.allowDelegatedMutations??!1,defaultDID:a.identity.id,engine:a.engine,getRandomValues:a.runtime.getRandomValues,graph:a.graph,signingIdentity:s,hlc:a.hlc,transactionManager:g});return{name:"rpc",api:{serve:(t,n)=>{let i=r?.accessRules,s={getRandomID:a.runtime.getRandomID,handlers:l,logger:a.getLogger("rpc-server"),signal:n,transport:t},g=e(!1===i?s:{...s,identity:a.identity,accessRules:i??{}});return o.push(g),g}},dispose:async()=>{for(let e of(g.dispose(),o))await e.dispose()}}}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/plugin-rpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -14,24 +14,24 @@
|
|
|
14
14
|
],
|
|
15
15
|
"sideEffects": false,
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@enkaku/generator": "^0.
|
|
18
|
-
"@enkaku/runtime": "^0.
|
|
19
|
-
"@enkaku/server": "^0.
|
|
20
|
-
"@enkaku/token": "0.
|
|
17
|
+
"@enkaku/generator": "^0.15.0",
|
|
18
|
+
"@enkaku/runtime": "^0.15.0",
|
|
19
|
+
"@enkaku/server": "^0.15.0",
|
|
20
|
+
"@enkaku/token": "^0.15.0",
|
|
21
21
|
"graphql": "^16.13.2",
|
|
22
|
-
"@kubun/db": "^0.
|
|
23
|
-
"@kubun/engine": "^0.
|
|
24
|
-
"@kubun/graphql": "^0.
|
|
25
|
-
"@kubun/hlc": "^0.
|
|
26
|
-
"@kubun/
|
|
27
|
-
"@kubun/
|
|
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"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@enkaku/transport": "0.
|
|
31
|
-
"@kubun/client": "^0.
|
|
32
|
-
"@kubun/
|
|
33
|
-
"@kubun/store-graph": "^0.
|
|
34
|
-
"@kubun/
|
|
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"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build:clean": "del lib",
|