@kubun/engine 0.8.1 → 0.8.3
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/engine.d.ts +4 -3
- package/lib/engine.js +1 -1
- package/package.json +7 -7
package/lib/engine.d.ts
CHANGED
|
@@ -82,9 +82,10 @@ export declare class KubunEngine implements Engine {
|
|
|
82
82
|
*/
|
|
83
83
|
getGraphQLSource(params: GraphQLSourceParams): GraphQLSource;
|
|
84
84
|
/**
|
|
85
|
-
* Register a context factory that contributes per-request fields to
|
|
86
|
-
* GraphQL execution context.
|
|
87
|
-
*
|
|
85
|
+
* Register a context factory that contributes per-request fields flatly to
|
|
86
|
+
* the GraphQL execution context. Escape hatch for test fixtures — not
|
|
87
|
+
* subject to the plugin-namespace collision check. Prefer `KubunPlugin`'s
|
|
88
|
+
* `createContextFactory` for plugin-scoped, namespaced contributions.
|
|
88
89
|
*/
|
|
89
90
|
registerContextFactory(factory: ContextFactory): void;
|
|
90
91
|
getGraphQLSchema(id: string): Promise<GraphQLSchema>;
|
package/lib/engine.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createRuntime as e}from"@enkaku/runtime";import{asType as t,createValidator as i}from"@enkaku/schema";import{isSigningIdentity as s,stringifyToken as
|
|
1
|
+
import{createRuntime as e}from"@enkaku/runtime";import{asType as t,createValidator as i}from"@enkaku/schema";import{isSigningIdentity as s,stringifyToken as a,verifyToken as r}from"@enkaku/token";import{KubunDB as n}from"@kubun/db";import{createReadContext as o,createSchema as l}from"@kubun/graphql";import{HLC as u}from"@kubun/hlc";import{getKubunLogger as h}from"@kubun/logger";import{applyMutation as c,convertPatchInput as d,createMutationOperations as p}from"@kubun/mutation";import{clusterToRecord as g,documentMutation as m,GraphModel as y}from"@kubun/protocol";import{GRAPH_STORE as v,getGraphStore as w,graphStoreDefinition as f}from"@kubun/store-graph";import{execute as x,GraphQLError as b,Kind as D,parse as I,subscribe as E}from"graphql";import{EngineEventBus as M}from"./events.js";import{computeMutationHash as G}from"./mutation-hash.js";import{runPolicies as C}from"./policies.js";import{createRegistry as S}from"./registry.js";let k=i(m);function F(){throw Error("Graph mutations require mutateGraph() — the core context does not sign or log writes. Use KubunEngine.mutateGraph() instead.")}function O(){throw Error("Access control mutations are not supported by the engine core — plugin override required.")}function j(){throw Error("Transaction mutations are not supported by the engine core — plugin override required (see plugin-rpc).")}let q={executeCreateMutation:F,executeSetMutation:F,executeUpdateMutation:F,executeRemoveMutation:F,executeSetModelAccessDefaults:O,executeRemoveModelAccessDefaults:O,executeSetDocumentAccessOverride:O,executeRemoveDocumentAccessOverride:O,beginTransaction:j,commitTransaction:j,rollbackTransaction:j};export class KubunEngine{#e=[];#t=[];#i;#s;#a={};#r;#n;#o;#l={};#u=new Map;#h=[];#c;#d;#p={};#g={};constructor(t){this.#i=t.db instanceof n?t.db:new n({adapter:t.db}),this.#i.register(f),this.#n=t.identity,this.#r=new u({nodeID:t.identity.id}),this.#o=t.logger??h("engine"),this.#s=t.eventBus??new M({logger:this.#o.getChild("events")}),this.#d=e(t.runtime),this.#c=S();let i=t.plugins??[],s={engine:this,graph:{execute:e=>this.#m(e),subscribe:e=>this.#y(e),applyVerifiedMutation:e=>this.#v(e),applyVerifiedMutations:e=>this.#w(e)},db:this.#i,runtime:this.#d,identity:this.#n,eventBus:this.#s,hlc:this.#r,getLogger:e=>this.#o.getChild(e)};for(let e of i){let t=e(s);this.#h.push(t),null!=t.api&&this.#c.registerPlugin(t.name,t.api)}this.#c.closeGate();let a=new Set;for(let e of this.#h){if(null!=e.schemaExtension&&this.#u.set(e.name,e.schemaExtension),null!=e.createContextFactory){if(a.has(e.name))throw Error(`Duplicate plugin namespace: ${e.name}`);a.add(e.name),this.#e.push({name:e.name,factory:e.createContextFactory()})}if(null!=e.policies)for(let[t,i]of Object.entries(e.policies)){null==this.#l[t]&&(this.#l[t]={sync:[],async:[]});let e=this.#l[t];null!=i.sync&&(e.sync=[...e.sync??[],...i.sync]),null!=i.async&&(e.async=[...e.async??[],...i.async])}}this.#o.info("engine initialized with {count} plugins",{count:this.#h.length})}get did(){return this.#n.id}get identity(){return this.#n}get eventBus(){return this.#s}async dispose(){await Promise.all(this.#h.filter(e=>null!=e.dispose).map(e=>e.dispose?.()))}getGraphQLSource(e){let{graphID:t}=e,i=e.getViewerDID??(()=>this.#n.id);return{query:async e=>await this.queryGraph({id:t,text:e.text,variables:e.variables,viewerDID:i()??void 0}),mutate:async e=>await this.mutateGraph({id:t,text:e.text,variables:e.variables,viewerDID:i()??void 0}),subscribe:e=>this.#y({graphID:t,text:e.text,variables:e.variables,viewerDID:i()??void 0})}}registerContextFactory(e){this.#t.push(e)}async #f(){return await w(this.#i)}async #x(e){var t;let i,s=e.viewerDID??this.#n.id,a=this.#b(s),r=e.stores??this.#i,n={};for(let{name:e,factory:t}of this.#e)n[e]=t(a,r);for(let e of this.#t)n={...n,...e(a,r)};null!=e.contextExtensions&&(n={...n,...e.contextExtensions});let l=e.graphID;return this.#g[l]??={},i={...o({store:(t={store:await w(r),viewerDID:s,eventBus:this.#s,contextExtensions:Object.keys(n).length>0?n:void 0}).store,viewerDID:t.viewerDID,events:t.eventBus}),...q},null!=t.contextExtensions?{...i,...t.contextExtensions}:i}#b(e){return{viewerDID:e}}async #D(e){return null==this.#a[e]&&(this.#a[e]=this.#f().then(async t=>{let i=await t.getGraph(e);if(null==i)throw this.#o.warn("graph {id} not found",{id:e}),delete this.#a[e],Error(`Graph not found: ${e}`);return this.#o.debug("cached model for graph {id}",{id:e}),{aliases:i.aliases,record:i.record,extensionSDL:i.extension_sdl??void 0,pluginConfig:i.plugin_config??void 0}})),await this.#a[e]}async getGraphQLSchema(e){return null==this.#p[e]&&(this.#p[e]=this.#D(e).then(t=>{let i;if(null!=t.pluginConfig){let e={},s={},a={},r={},n={};for(let[i,o]of Object.entries(t.pluginConfig)){let t=this.#u.get(i);if(null!=t){let i=t(o);if(Object.assign(e,i.resolvers.queryFields??{}),Object.assign(s,i.resolvers.mutationFields??{}),Object.assign(a,i.resolvers.subscriptionFields??{}),Object.assign(n,i.resolvers.nodeResolvers??{}),null!=i.resolvers.typeFields)for(let[e,t]of Object.entries(i.resolvers.typeFields))r[e]={...r[e],...t}}}i={queryFields:e,mutationFields:s,subscriptionFields:a,typeFields:r,nodeResolvers:n}}let s=l({...t,extensionResolvers:i});return this.#o.debug("cached schema for graph {id}",{id:e}),s}).catch(t=>{throw delete this.#p[e],t})),await this.#p[e]}async #I(e,t){let i=t??I(e.text);if(null==i.definitions[0])throw Error("Missing GraphQL document definition");return{schema:await this.getGraphQLSchema(e.graphID),document:i,contextValue:await this.#x(e),variableValues:e.variables}}#E(e){let t=e.definitions[0];return null!=t&&t.kind===D.OPERATION_DEFINITION?t.operation:"query"}async #M(e,t){let i=await C(this.#l,e,{viewerDID:t});return"issues"in i&&null!=i.issues&&i.issues.length>0?{errors:i.issues.map(e=>new b(e.message,{extensions:null!=e.path?{path:e.path}:void 0}))}:null}async listGraphs(){let e=await this.#f();return{graphs:(await e.listGraphs()).map(e=>({id:e.id,name:e.name}))}}async loadGraph(e){let t=await this.#D(e.id);return{aliases:t.aliases,record:t.record,extensionSDL:t.extensionSDL}}async deployGraph(e){let t,i,s={};for(let t of e.clusters)Object.assign(s,g(t));let a=y.fromClusters({clusters:s});if(null!=e.plugins){i=e.plugins;let s=[];for(let[e,t]of Object.entries(i)){let i=this.#u.get(e);if(null==i)throw Error(`Plugin "${e}" not found or does not provide schema extensions`);let a=i(t);s.push(a.sdl)}s.length>0&&(t=s.join("\n"))}let r=await this.#f(),n=await r.createGraph({id:e.id??this.#d.getRandomID(),name:e.name,record:a.record,extensionSDL:t,pluginConfig:i}),o=a.toJSON();return this.#a[n]=Promise.resolve({...o,extensionSDL:t,pluginConfig:i}),delete this.#p[n],delete this.#g[n],this.#o.info("deployed graph {id}",{id:n}),{id:n,...o}}async #G(e){for(let{document:t,mutation:i,authorDID:s}of e){let e="set"===i.typ?"create":"update",a={type:e,document:t,previous:{data:null},getCursor:()=>t.id};await this.#s.emit("document:saved",a),"create"===e?await this.#s.emit("engine:document:created",{documentID:t.id,modelID:t.model,viewerDID:s}):await this.#s.emit("engine:document:updated",{documentID:t.id,modelID:t.model,viewerDID:s}),await this.#s.emit("engine:mutation:applied",{type:e,documentID:t.id,viewerDID:s})}}async #v(e,i){let{token:s,graphID:a}=e,n=t(k,(await r(s)).payload);this.#g[a]??={};let o=null!=i?await w(i):await this.#f(),l={store:o,validators:this.#g[a],hlc:this.#r},u=await c(l,n),h=G(s),d=n.iss;await o.insertMutationLogEntry({mutation_hash:h,model_id:u.model,document_id:u.id,author_did:d,hlc:n.hlc,mutation_jwt:s,status:"applied"});let p={document:u,mutation:n,authorDID:d,hash:h};return null==i&&await this.#G([p]),p}async #w(e){let{tokens:i,graphID:s}=e;this.#g[s]??={};let a=await Promise.all(i.map(async e=>{let i=t(k,(await r(e)).payload);return{token:e,mutation:i}})),n=[];return await this.#i.withTransaction(async e=>{let t=await e.getStore(v);for(let{token:e,mutation:i}of a){let a={store:t,validators:this.#g[s],hlc:this.#r},r=await c(a,i),o=G(e);await t.insertMutationLogEntry({mutation_hash:o,model_id:r.model,document_id:r.id,author_did:i.iss,hlc:i.hlc,mutation_jwt:e,status:"applied"}),n.push({document:r,mutation:i,authorDID:i.iss,hash:o})}}),await this.#G(n),{results:n}}async #m(e){let t=I(e.text),i=this.#E(t),s=await this.#M(i,e.viewerDID??this.#n.id);if(null!=s)return s;let a=await this.#I(e,t);return await x(a)}async #y(e){let t=await this.#I(e);return await E(t)}async queryGraph(e){let t=I(e.text);if("mutation"===this.#E(t))throw Error("queryGraph() does not accept mutation operations. Use mutateGraph() instead.");return await this.#m({graphID:e.id,text:e.text,variables:e.variables??{},viewerDID:e.viewerDID})}async mutateGraph(e){if(!s(this.#n))throw Error("mutateGraph requires a SigningIdentity");let t=this.#n,i=e.id,r=[],n=await this.#i.withTransaction(async s=>{let n=async e=>{let n=a(await t.signToken(e)),o=await this.#v({token:n,graphID:i},s);return r.push(o),o.document},o=p({issuer:t.id,hlc:this.#r,getRandomValues:this.#d.getRandomValues,processSetMutation:e=>n(e),processChangeMutation:e=>n(e)});return await this.#m({graphID:i,text:e.text,variables:e.variables??{},viewerDID:e.viewerDID??t.id,stores:s,contextExtensions:{executeCreateMutation:async e=>await o.createDocument({modelID:e.modelID,data:e.data}),executeSetMutation:async e=>await o.setDocument({modelID:e.modelID,unique:e.unique,data:e.data}),executeUpdateMutation:async e=>await o.updateDocument({docID:e.input.id,patch:d(e.input.patch)}),executeRemoveMutation:async e=>{await o.removeDocument({docID:e.id})}}})});return await this.#G(r),n}async subscribeToGraph(e){return await this.#y({graphID:e.id,text:e.text,variables:e.variables??{},viewerDID:e.viewerDID})}async getAPI(e){return await this.#c.getAPI(e)}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/engine",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@enkaku/async": "^0.14.
|
|
18
|
+
"@enkaku/async": "^0.14.1",
|
|
19
19
|
"@enkaku/capability": "^0.14.0",
|
|
20
20
|
"@enkaku/event": "^0.14.1",
|
|
21
21
|
"@enkaku/runtime": "^0.14.0",
|
|
@@ -23,20 +23,20 @@
|
|
|
23
23
|
"@enkaku/token": "0.14.1",
|
|
24
24
|
"@noble/hashes": "^2.2.0",
|
|
25
25
|
"graphql": "^16.13.2",
|
|
26
|
+
"@kubun/hlc": "^0.8.0",
|
|
26
27
|
"@kubun/db": "^0.8.1",
|
|
27
28
|
"@kubun/db-adapter": "^0.8.1",
|
|
29
|
+
"@kubun/graphql": "^0.8.2",
|
|
28
30
|
"@kubun/logger": "^0.8.0",
|
|
29
31
|
"@kubun/mutation": "^0.8.1",
|
|
30
|
-
"@kubun/
|
|
31
|
-
"@kubun/
|
|
32
|
-
"@kubun/store-graph": "^0.8.0",
|
|
33
|
-
"@kubun/graphql": "^0.8.1"
|
|
32
|
+
"@kubun/protocol": "^0.8.1",
|
|
33
|
+
"@kubun/store-graph": "^0.8.1"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@testcontainers/postgresql": "^11.14.0",
|
|
37
37
|
"@kubun/db-postgres": "^0.8.1",
|
|
38
38
|
"@kubun/id": "^0.8.0",
|
|
39
|
-
"@kubun/store-
|
|
39
|
+
"@kubun/store-p2p": "^0.8.0",
|
|
40
40
|
"@kubun/test-utils": "^0.8.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|