@kubun/client 0.3.1 → 0.3.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/graphql.d.ts.map +1 -1
- package/lib/graphql.js +5 -5
- package/lib/mutations.d.ts +7 -17
- package/lib/mutations.d.ts.map +1 -1
- package/lib/mutations.js +19 -116
- package/package.json +6 -7
package/lib/graphql.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAkB,MAAM,eAAe,CAAA;AAChE,OAAO,KAAK,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAkB,MAAM,eAAe,CAAA;AAChE,OAAO,KAAK,EAAE,OAAO,EAAwD,MAAM,gBAAgB,CAAA;AAEnG,OAAO,KAAK,EAAgB,oBAAoB,EAAgB,MAAM,iBAAiB,CAAA;AACvF,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAsB,aAAa,EAAE,MAAM,SAAS,CAAA;AAK/F,qBAAa,qBAAsB,SAAQ,KAAK;;gBAGlC,MAAM,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,OAAO,SAA6B;IAMrF,IAAI,MAAM,IAAI,aAAa,CAAC,YAAY,CAAC,CAExC;CACF;AAID,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAA;IACrD,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjC,MAAM,EAAE,WAAW,CAAA;CACpB,CAAA;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAwD/D;AAED,KAAK,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAA;AAE3E,KAAK,oBAAoB,GAAG;IAC1B,OAAO,CAAC,EAAE,aAAa,CAAA;CACxB,CAAA;AAED,KAAK,qBAAqB,GAAG;IAC3B,OAAO,EAAE,gBAAgB,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC,CAAA;AAED,qBAAa,eAAe;;gBAGd,MAAM,GAAE,oBAAyB;IAI7C,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI9B,SAAS,CACP,EAAE,EAAE,MAAM,EACV,eAAe,EAAE,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC,GACpE,IAAI;IAID,OAAO,CAAC,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAAC,eAAe,CAAC;CAavE"}
|
package/lib/graphql.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { stringifyToken } from '@enkaku/token';
|
|
2
2
|
import { createSchema } from '@kubun/graphql';
|
|
3
3
|
import { graphql } from 'graphql';
|
|
4
|
-
import {
|
|
4
|
+
import { signChangeMutation, signRemoveMutation, signSetMutation } from './mutations.js';
|
|
5
5
|
export class GraphQLExecutionError extends Error {
|
|
6
6
|
#errors;
|
|
7
7
|
constructor(errors, message = 'GraphQL execution failed'){
|
|
@@ -19,7 +19,7 @@ export function createContext(client) {
|
|
|
19
19
|
async executeCreateMutation (modelID, data, info) {
|
|
20
20
|
// Create the mutation token
|
|
21
21
|
const unique = globalThis.crypto.getRandomValues(new Uint8Array(12));
|
|
22
|
-
const mutation = await
|
|
22
|
+
const mutation = await signSetMutation({
|
|
23
23
|
signer: client.signer,
|
|
24
24
|
modelID,
|
|
25
25
|
data,
|
|
@@ -32,7 +32,7 @@ export function createContext(client) {
|
|
|
32
32
|
},
|
|
33
33
|
async executeSetMutation (modelID, unique, data, info) {
|
|
34
34
|
// Create the mutation
|
|
35
|
-
const mutation = await
|
|
35
|
+
const mutation = await signSetMutation({
|
|
36
36
|
signer: client.signer,
|
|
37
37
|
modelID,
|
|
38
38
|
data,
|
|
@@ -44,7 +44,7 @@ export function createContext(client) {
|
|
|
44
44
|
return noDocument;
|
|
45
45
|
},
|
|
46
46
|
async executeUpdateMutation (input, info) {
|
|
47
|
-
const mutation = await
|
|
47
|
+
const mutation = await signChangeMutation({
|
|
48
48
|
loadState: client.loadState,
|
|
49
49
|
signer: client.signer,
|
|
50
50
|
docID: input.id,
|
|
@@ -65,7 +65,7 @@ export function createContext(client) {
|
|
|
65
65
|
return noDocument;
|
|
66
66
|
},
|
|
67
67
|
async executeRemoveMutation (id, info) {
|
|
68
|
-
const mutation = await
|
|
68
|
+
const mutation = await signRemoveMutation({
|
|
69
69
|
docID: id,
|
|
70
70
|
signer: client.signer
|
|
71
71
|
});
|
package/lib/mutations.d.ts
CHANGED
|
@@ -1,26 +1,16 @@
|
|
|
1
1
|
import type { SignedToken, TokenSigner } from '@enkaku/token';
|
|
2
|
-
import type {
|
|
3
|
-
import { DocumentID, type DocumentModelID } from '@kubun/id';
|
|
2
|
+
import type { CreateChangeMutationParams, CreateRemoveMutationParams, CreateSetMutationParams } from '@kubun/mutation';
|
|
4
3
|
import type { ChangeDocumentMutation, DocumentData, SetDocumentMutation } from '@kubun/protocol';
|
|
5
|
-
export type SetMutationParams<Data extends DocumentData = DocumentData> = {
|
|
6
|
-
data: Data | null;
|
|
7
|
-
modelID: DocumentModelID | string;
|
|
8
|
-
owner?: string;
|
|
4
|
+
export type SetMutationParams<Data extends DocumentData = DocumentData> = Omit<CreateSetMutationParams<Data>, 'issuer'> & {
|
|
9
5
|
signer: TokenSigner;
|
|
10
|
-
unique: Uint8Array;
|
|
11
6
|
};
|
|
12
|
-
export declare function
|
|
13
|
-
export type
|
|
14
|
-
docID: DocumentID | string;
|
|
15
|
-
from?: Partial<Data>;
|
|
16
|
-
loadState: (id: string) => Promise<Uint8Array | null>;
|
|
17
|
-
patch: Array<PatchOperation>;
|
|
7
|
+
export declare function signSetMutation<Data extends DocumentData = DocumentData>(params: SetMutationParams<Data>): Promise<SignedToken<SetDocumentMutation>>;
|
|
8
|
+
export type ChangeMutationParams<Data extends DocumentData = DocumentData> = Omit<CreateChangeMutationParams<Data>, 'issuer'> & {
|
|
18
9
|
signer: TokenSigner;
|
|
19
10
|
};
|
|
20
|
-
export declare function
|
|
21
|
-
export type RemoveMutationParams = {
|
|
22
|
-
docID: string;
|
|
11
|
+
export declare function signChangeMutation<Data extends DocumentData = DocumentData>(params: ChangeMutationParams<Data>): Promise<SignedToken<ChangeDocumentMutation>>;
|
|
12
|
+
export type RemoveMutationParams = Omit<CreateRemoveMutationParams, 'issuer'> & {
|
|
23
13
|
signer: TokenSigner;
|
|
24
14
|
};
|
|
25
|
-
export declare function
|
|
15
|
+
export declare function signRemoveMutation(params: RemoveMutationParams): Promise<SignedToken<ChangeDocumentMutation>>;
|
|
26
16
|
//# sourceMappingURL=mutations.d.ts.map
|
package/lib/mutations.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../src/mutations.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../src/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC7D,OAAO,KAAK,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,uBAAuB,EACxB,MAAM,iBAAiB,CAAA;AAExB,OAAO,KAAK,EAAE,sBAAsB,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAEhG,MAAM,MAAM,iBAAiB,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,IAAI,IAAI,CAC5E,uBAAuB,CAAC,IAAI,CAAC,EAC7B,QAAQ,CACT,GAAG;IACF,MAAM,EAAE,WAAW,CAAA;CACpB,CAAA;AAED,wBAAsB,eAAe,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,EAC5E,MAAM,EAAE,iBAAiB,CAAC,IAAI,CAAC,GAC9B,OAAO,CAAC,WAAW,CAAC,mBAAmB,CAAC,CAAC,CAI3C;AAED,MAAM,MAAM,oBAAoB,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,IAAI,IAAI,CAC/E,0BAA0B,CAAC,IAAI,CAAC,EAChC,QAAQ,CACT,GAAG;IACF,MAAM,EAAE,WAAW,CAAA;CACpB,CAAA;AAED,wBAAsB,kBAAkB,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,EAC/E,MAAM,EAAE,oBAAoB,CAAC,IAAI,CAAC,GACjC,OAAO,CAAC,WAAW,CAAC,sBAAsB,CAAC,CAAC,CAI9C;AAED,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,0BAA0B,EAAE,QAAQ,CAAC,GAAG;IAC9E,MAAM,EAAE,WAAW,CAAA;CACpB,CAAA;AAED,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,oBAAoB,GAC3B,OAAO,CAAC,WAAW,CAAC,sBAAsB,CAAC,CAAC,CAI9C"}
|
package/lib/mutations.js
CHANGED
|
@@ -1,122 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const automergeReady = lazy(()=>A.initializeBase64Wasm(automergeWasmBase64));
|
|
8
|
-
function parsePath(path) {
|
|
9
|
-
return path.slice(1).split('/').map((key)=>{
|
|
10
|
-
// Convert array indices to numbers
|
|
11
|
-
const index = Number(key);
|
|
12
|
-
return Number.isNaN(index) ? key : index;
|
|
1
|
+
import { createChangeMutation, createRemoveMutation, createSetMutation } from '@kubun/mutation';
|
|
2
|
+
export async function signSetMutation(params) {
|
|
3
|
+
const { signer, ...rest } = params;
|
|
4
|
+
const mutation = await createSetMutation({
|
|
5
|
+
...rest,
|
|
6
|
+
issuer: signer.id
|
|
13
7
|
});
|
|
8
|
+
return await signer.createToken(mutation);
|
|
14
9
|
}
|
|
15
|
-
function
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
function setPath(obj, path, value) {
|
|
21
|
-
const keys = parsePath(path);
|
|
22
|
-
const lastKey = keys.pop();
|
|
23
|
-
if (lastKey !== undefined) {
|
|
24
|
-
// @ts-ignore unknown object
|
|
25
|
-
const target = keys.reduce((acc, key)=>acc[key], obj);
|
|
26
|
-
if (Array.isArray(target) && typeof lastKey === 'number' && lastKey === target.length) {
|
|
27
|
-
target.push(value) // Append to array if index is equal to array length
|
|
28
|
-
;
|
|
29
|
-
} else {
|
|
30
|
-
;
|
|
31
|
-
target[lastKey] = value;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function deletePath(obj, path) {
|
|
36
|
-
const keys = parsePath(path);
|
|
37
|
-
const lastKey = keys.pop();
|
|
38
|
-
if (lastKey !== undefined) {
|
|
39
|
-
// @ts-ignore unknown object
|
|
40
|
-
const target = keys.reduce((acc, key)=>acc[key], obj);
|
|
41
|
-
if (Array.isArray(target) && typeof lastKey === 'number') {
|
|
42
|
-
target.splice(lastKey, 1) // Remove from array if lastKey is an index
|
|
43
|
-
;
|
|
44
|
-
} else {
|
|
45
|
-
delete target[lastKey];
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function applyPatches(data, patches) {
|
|
50
|
-
for (const patch of patches){
|
|
51
|
-
switch(patch.op){
|
|
52
|
-
case 'add':
|
|
53
|
-
case 'replace':
|
|
54
|
-
setPath(data, patch.path, patch.value);
|
|
55
|
-
break;
|
|
56
|
-
case 'remove':
|
|
57
|
-
deletePath(data, patch.path);
|
|
58
|
-
break;
|
|
59
|
-
case 'copy':
|
|
60
|
-
{
|
|
61
|
-
const value = getPath(data, patch.from);
|
|
62
|
-
setPath(data, patch.path, value);
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
case 'move':
|
|
66
|
-
{
|
|
67
|
-
const value = getPath(data, patch.from);
|
|
68
|
-
deletePath(data, patch.from);
|
|
69
|
-
setPath(data, patch.path, value);
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
default:
|
|
73
|
-
// @ts-ignore never type
|
|
74
|
-
throw new Error(`Unknown operation: ${patch.op}`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
export async function createSetMutation(params) {
|
|
79
|
-
const issuer = params.signer.id;
|
|
80
|
-
const owner = params.owner ?? issuer;
|
|
81
|
-
await automergeReady;
|
|
82
|
-
const docID = await DocumentID.create(params.modelID, owner, params.unique);
|
|
83
|
-
return await params.signer.createToken({
|
|
84
|
-
typ: 'set',
|
|
85
|
-
iss: issuer,
|
|
86
|
-
aud: owner,
|
|
87
|
-
sub: docID.toString(),
|
|
88
|
-
data: params.data == null ? null : toB64(A.save(A.from(params.data))),
|
|
89
|
-
unq: toB64(params.unique)
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
export async function createChangeMutation(params) {
|
|
93
|
-
const docID = DocumentID.from(params.docID).toString();
|
|
94
|
-
// Load current state of document from DB
|
|
95
|
-
const [state] = await Promise.all([
|
|
96
|
-
params.loadState(docID),
|
|
97
|
-
automergeReady
|
|
98
|
-
]);
|
|
99
|
-
const loadedDoc = state ? A.load(state) : null;
|
|
100
|
-
// Apply patches to loaded doc or locally created one
|
|
101
|
-
const newDoc = A.change(loadedDoc ?? A.from(params.from ?? {}), (proxy)=>{
|
|
102
|
-
applyPatches(proxy, params.patch);
|
|
103
|
-
});
|
|
104
|
-
// Save incremental or full data state
|
|
105
|
-
const data = loadedDoc ? A.saveSince(newDoc, A.getHeads(loadedDoc)) : A.save(newDoc);
|
|
106
|
-
return await params.signer.createToken({
|
|
107
|
-
typ: 'change',
|
|
108
|
-
iss: params.signer.id,
|
|
109
|
-
sub: docID,
|
|
110
|
-
data: toB64(data),
|
|
111
|
-
inc: loadedDoc != null
|
|
10
|
+
export async function signChangeMutation(params) {
|
|
11
|
+
const { signer, ...rest } = params;
|
|
12
|
+
const mutation = await createChangeMutation({
|
|
13
|
+
...rest,
|
|
14
|
+
issuer: signer.id
|
|
112
15
|
});
|
|
16
|
+
return await signer.createToken(mutation);
|
|
113
17
|
}
|
|
114
|
-
export async function
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
data: null,
|
|
120
|
-
inc: false
|
|
18
|
+
export async function signRemoveMutation(params) {
|
|
19
|
+
const { signer, ...rest } = params;
|
|
20
|
+
const mutation = createRemoveMutation({
|
|
21
|
+
...rest,
|
|
22
|
+
issuer: signer.id
|
|
121
23
|
});
|
|
24
|
+
return await signer.createToken(mutation);
|
|
122
25
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/client",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -15,18 +15,17 @@
|
|
|
15
15
|
],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@automerge/automerge": "^2.2.8",
|
|
19
|
-
"@enkaku/async": "^0.12.0",
|
|
20
18
|
"@enkaku/client": "^0.12.0",
|
|
21
19
|
"@enkaku/codec": "^0.12.0",
|
|
22
20
|
"@enkaku/token": "^0.12.0",
|
|
23
|
-
"graphql": "^16.
|
|
21
|
+
"graphql": "^16.11.0",
|
|
24
22
|
"multiformats": "^13.3.2",
|
|
25
|
-
"@kubun/
|
|
26
|
-
"@kubun/
|
|
23
|
+
"@kubun/id": "^0.3.0",
|
|
24
|
+
"@kubun/mutation": "^0.3.0"
|
|
27
25
|
},
|
|
28
26
|
"devDependencies": {
|
|
29
|
-
"@kubun/protocol": "^0.3.
|
|
27
|
+
"@kubun/protocol": "^0.3.4",
|
|
28
|
+
"@kubun/graphql": "^0.3.6"
|
|
30
29
|
},
|
|
31
30
|
"scripts": {
|
|
32
31
|
"build:clean": "del lib",
|