@kubun/mutation 0.5.0 → 0.6.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/apply.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { type Validator } from '@enkaku/schema';
2
2
  import type { KubunDB } from '@kubun/db';
3
3
  import type { ChangeDocumentMutation, DocumentData, DocumentMutation, DocumentNode, SetDocumentMutation } from '@kubun/protocol';
4
+ import { HLC } from './hlc.js';
4
5
  export type DocumentValidator = Validator<DocumentData>;
5
6
  export type ValidatorsRecord = Record<string, Promise<DocumentValidator>>;
6
7
  export type AccessChecker = (doc: {
@@ -13,6 +14,8 @@ export type MutationContext = {
13
14
  db: KubunDB;
14
15
  validators: ValidatorsRecord;
15
16
  accessChecker?: AccessChecker;
17
+ hlc?: HLC;
18
+ maxDriftMS?: number;
16
19
  };
17
20
  export declare function getDocumentValidator(ctx: MutationContext, id: string): Promise<DocumentValidator>;
18
21
  export declare function applyChangeMutation(ctx: MutationContext, mutation: ChangeDocumentMutation): Promise<DocumentNode>;
package/lib/apply.js CHANGED
@@ -1 +1 @@
1
- import*as t from"@automerge/automerge/slim";import{fromB64 as e}from"@enkaku/codec";import{asType as a,createValidator as r}from"@enkaku/schema";import{DocumentID as o}from"@kubun/id";import{automergeReady as n,automergeToData as i}from"./automerge.js";export function getDocumentValidator(t,e){return null==t.validators[e]&&(t.validators[e]=t.db.getDocumentModel(e).then(t=>r({...t.schema,$id:e}))),t.validators[e]}export async function applyChangeMutation(r,u){let s=o.fromString(u.sub),l=s.toString(),c=await r.db.getDocument(s);if(null==c)throw Error(`Document not found: ${l}`);if(u.iss!==c.owner)throw Error("Invalid mutation issuer");if(r.accessChecker&&!await r.accessChecker(c,"write"))throw Error(`Access denied: cannot write document ${l}`);if(null===u.data)return await r.db.saveDocument({id:s,existing:c,data:null,state:null});if(null===c.data)throw Error(`Cannot apply changes to empty document: ${l}`);let[m,d]=await Promise.all([r.db.getDocumentStates([l]),getDocumentValidator(r,s.model.toString()),n]),w=m[l]?t.load(m[l]):t.from(c.data),g=e(u.data),p=u.inc?t.loadIncremental(w,g):t.merge(w,t.load(g)),f=a(d,i(p));return await r.db.saveDocument({id:s,existing:c,data:f,state:t.save(p)})}export async function applySetMutation(r,u){let s=u.aud??u.iss;if(u.iss!==s)throw Error("Invalid mutation issuer");let l=o.fromString(u.sub),[c,m]=await Promise.all([r.db.getDocument(l),getDocumentValidator(r,l.model.toString()),n]),d=null===u.data?null:t.load(e(u.data)),w=d?a(m,i(d)):null;if(null===c)return await r.db.createDocument({id:l,owner:s,data:w,state:d?t.save(d):null,unique:e(u.unq)});if(c.owner!==s)throw Error(`Cannot change owner from ${c.owner} to ${s} in document: ${l.toString()}`);if(r.accessChecker&&!await r.accessChecker(c,"write"))throw Error(`Access denied: cannot write document ${l.toString()}`);return await r.db.saveDocument({id:l,existing:c,data:w,state:d?t.save(d):null})}export async function applyMutation(t,e){switch(e.typ){case"change":return await applyChangeMutation(t,e);case"set":return await applySetMutation(t,e);default:throw Error("Unsupported mutation type")}}
1
+ import{fromB64 as t}from"@enkaku/codec";import{asType as e,createValidator as a}from"@enkaku/schema";import{DocumentID as r}from"@kubun/id";import{HLC as i}from"./hlc.js";export function getDocumentValidator(t,e){return null==t.validators[e]&&(t.validators[e]=t.db.getDocumentModel(e).then(t=>a({...t.schema,$id:e}))),t.validators[e]}function n(t,e,a){let r=i.parse(t),n=Date.now(),o=r.wallTime-n;if(o>e)throw Error(`HLC timestamp too far in the future: ${o}ms drift exceeds ${e}ms limit`);a&&a.receive(r)}export async function applyChangeMutation(t,a){let i=r.fromString(a.sub),o=i.toString(),l=await t.db.getDocument(i);if(null==l)throw Error(`Document not found: ${o}`);if(a.iss!==l.owner)throw Error("Invalid mutation issuer");if(t.accessChecker&&!await t.accessChecker(l,"write"))throw Error(`Access denied: cannot write document ${o}`);null!=t.maxDriftMS&&n(a.hlc,t.maxDriftMS,t.hlc);let c=i.model.toString(),u=await t.db.getFieldHLCs(o)??{};if(1===a.patch.length&&"replace"===a.patch[0].op&&"/"===a.patch[0].path&&null===a.patch[0].value){let e={...u,"/":a.hlc},r=Object.entries(e).every(([t,e])=>"/"===t||a.hlc>=e);return(await t.db.updateFieldHLCs(i,e),r)?await t.db.saveDocument({id:i,existing:l,data:null}):l}if(null!=u["/"]&&null===l.data)return l;let s=await getDocumentValidator(t,c),d=a.patch.filter(t=>"value"in t),h={...l.data??{}},m={...u},f=!1;for(let t of d){let e=u[t.path];(null==e||a.hlc>e)&&(h[t.path.slice(1)]=t.value,m[t.path]=a.hlc,f=!0)}if(!f)return l;await t.db.updateFieldHLCs(i,m);let p=e(s,h);return await t.db.saveDocument({id:i,existing:l,data:p})}export async function applySetMutation(a,i){let o=i.aud??i.iss;if(i.iss!==o)throw Error("Invalid mutation issuer");let l=r.fromString(i.sub),c=l.toString(),u=l.model.toString();null!=a.maxDriftMS&&n(i.hlc,a.maxDriftMS,a.hlc);let[s,d]=await Promise.all([a.db.getDocument(l),getDocumentValidator(a,u)]),h=e(d,i.data),m={};for(let t of Object.keys(h))m[`/${t}`]=i.hlc;if(null===s){let e=await a.db.createDocument({id:l,owner:o,data:h,unique:t(i.unq)});return await a.db.updateFieldHLCs(l,m),e}if(s.owner!==o)throw Error(`Cannot change owner from ${s.owner} to ${o} in document: ${l.toString()}`);if(a.accessChecker&&!await a.accessChecker(s,"write"))throw Error(`Access denied: cannot write document ${l.toString()}`);let f=await a.db.getFieldHLCs(c)??{},p={...s.data??{}},w={...f},g=!1;for(let[t,e]of Object.entries(h)){let a=`/${t}`,r=f[a];(null==r||i.hlc>r)&&(p[t]=e,w[a]=i.hlc,g=!0)}if(!g)return s;await a.db.updateFieldHLCs(l,w);let b=e(d,p);return await a.db.saveDocument({id:l,existing:s,data:b})}export async function applyMutation(t,e){switch(e.typ){case"change":return await applyChangeMutation(t,e);case"set":return await applySetMutation(t,e);default:throw Error("Unsupported mutation type")}}
package/lib/create.d.ts CHANGED
@@ -1,24 +1,26 @@
1
1
  import type { PatchOperation } from '@kubun/graphql';
2
2
  import { DocumentID, type DocumentModelID } from '@kubun/id';
3
3
  import type { ChangeDocumentMutation, DocumentData, SetDocumentMutation } from '@kubun/protocol';
4
+ import { HLC } from './hlc.js';
4
5
  export type CreateSetMutationParams<Data extends DocumentData = DocumentData> = {
5
- data: Data | null;
6
+ data: Data;
7
+ hlc: HLC;
6
8
  issuer: string;
7
9
  modelID: DocumentModelID | string;
8
10
  owner?: string;
9
11
  unique: Uint8Array;
10
12
  };
11
13
  export declare function createSetMutation<Data extends DocumentData = DocumentData>(params: CreateSetMutationParams<Data>): Promise<SetDocumentMutation>;
12
- export type CreateChangeMutationParams<Data extends DocumentData = DocumentData> = {
14
+ export type CreateChangeMutationParams = {
13
15
  docID: DocumentID | string;
14
- from?: Partial<Data>;
16
+ hlc: HLC;
15
17
  issuer: string;
16
- loadState: (id: string) => Promise<Uint8Array | null>;
17
18
  patch: Array<PatchOperation>;
18
19
  };
19
- export declare function createChangeMutation<Data extends DocumentData = DocumentData>(params: CreateChangeMutationParams<Data>): Promise<ChangeDocumentMutation>;
20
+ export declare function createChangeMutation(params: CreateChangeMutationParams): ChangeDocumentMutation;
20
21
  export type CreateRemoveMutationParams = {
21
22
  docID: string;
23
+ hlc: HLC;
22
24
  issuer: string;
23
25
  };
24
26
  export declare function createRemoveMutation(params: CreateRemoveMutationParams): ChangeDocumentMutation;
package/lib/create.js CHANGED
@@ -1 +1 @@
1
- import*as t from"@automerge/automerge/slim";import{toB64 as e}from"@enkaku/codec";import{applyPatches as a}from"@enkaku/patch";import{DocumentID as o}from"@kubun/id";import{automergeReady as r}from"./automerge.js";export async function createSetMutation(a){let n=a.issuer,u=a.owner??n,[i]=await Promise.all([o.create(a.modelID,u,a.unique),r]);return{typ:"set",iss:n,aud:u,sub:i.toString(),data:null==a.data?null:e(t.save(t.from(a.data))),unq:e(a.unique)}}export async function createChangeMutation(n){let u=o.from(n.docID).toString(),[i]=await Promise.all([n.loadState(u),r]),s=i?t.load(i):null,m=t.change(s??t.from(n.from??{}),t=>{a(t,n.patch)}),c=s?t.saveSince(m,t.getHeads(s)):t.save(m);return{typ:"change",iss:n.issuer,sub:u,data:e(c),inc:null!=s}}export function createRemoveMutation(t){return{typ:"change",iss:t.issuer,sub:t.docID,data:null,inc:!1}}
1
+ import{toB64 as e}from"@enkaku/codec";import{DocumentID as t}from"@kubun/id";import{HLC as r}from"./hlc.js";export async function createSetMutation(a){let n=a.issuer,o=a.owner??n,i=await t.create(a.modelID,o,a.unique);return{typ:"set",iss:n,aud:o,sub:i.toString(),data:a.data,hlc:r.serialize(a.hlc.now()),unq:e(a.unique)}}export function createChangeMutation(e){return{typ:"change",iss:e.issuer,sub:t.from(e.docID).toString(),patch:e.patch,hlc:r.serialize(e.hlc.now())}}export function createRemoveMutation(e){return{typ:"change",iss:e.issuer,sub:e.docID,patch:[{op:"replace",path:"/",value:null}],hlc:r.serialize(e.hlc.now())}}
package/lib/hlc.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ export type HLCTimestamp = {
2
+ wallTime: number;
3
+ counter: number;
4
+ nodeID: string;
5
+ };
6
+ type HLCParams = {
7
+ nodeID: string;
8
+ };
9
+ export declare class HLC {
10
+ #private;
11
+ constructor(params: HLCParams);
12
+ get nodeID(): string;
13
+ now(): HLCTimestamp;
14
+ receive(remote: HLCTimestamp): HLCTimestamp;
15
+ static serialize(ts: HLCTimestamp): string;
16
+ static parse(s: string): HLCTimestamp;
17
+ static compare(a: HLCTimestamp, b: HLCTimestamp): number;
18
+ }
19
+ export {};
package/lib/hlc.js ADDED
@@ -0,0 +1 @@
1
+ export class HLC{#t;#e=0;#l=0;constructor(t){this.#t=t.nodeID}get nodeID(){return this.#t}now(){let t=Date.now();return t>this.#e?(this.#e=t,this.#l=0):this.#l++,{wallTime:this.#e,counter:this.#l,nodeID:this.#t}}receive(t){let e=Date.now();return e>this.#e&&e>t.wallTime?(this.#e=e,this.#l=0):t.wallTime>this.#e?(this.#e=t.wallTime,this.#l=t.counter+1):this.#e>t.wallTime?this.#l++:this.#l=Math.max(this.#l,t.counter)+1,{wallTime:this.#e,counter:this.#l,nodeID:this.#t}}static serialize(t){let e=new Date(t.wallTime).toISOString(),l=t.counter.toString().padStart(4,"0");return`${e}:${l}:${t.nodeID}`}static parse(t){let e=t.indexOf("Z"),l=new Date(t.slice(0,e+1)).getTime(),i=t.slice(e+2),a=i.indexOf(":");return{wallTime:l,counter:Number.parseInt(i.slice(0,a),10),nodeID:i.slice(a+1)}}static compare(t,e){return t.wallTime!==e.wallTime?t.wallTime-e.wallTime:t.counter!==e.counter?t.counter-e.counter:t.nodeID<e.nodeID?-1:+(t.nodeID>e.nodeID)}}
package/lib/index.d.ts CHANGED
@@ -2,3 +2,6 @@ export type { DocumentValidator, MutationContext, ValidatorsRecord } from './app
2
2
  export { applyChangeMutation, applyMutation, applySetMutation } from './apply.js';
3
3
  export type { CreateChangeMutationParams, CreateRemoveMutationParams, CreateSetMutationParams, } from './create.js';
4
4
  export { createChangeMutation, createRemoveMutation, createSetMutation } from './create.js';
5
+ export { HLC, type HLCTimestamp } from './hlc.js';
6
+ export type { CreateMutationOperationsParams, GetRandomValues, MutationOperations, } from './operations.js';
7
+ export { convertPatchInput, createMutationOperations } from './operations.js';
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- export{applyChangeMutation,applyMutation,applySetMutation}from"./apply.js";export{createChangeMutation,createRemoveMutation,createSetMutation}from"./create.js";
1
+ export{applyChangeMutation,applyMutation,applySetMutation}from"./apply.js";export{createChangeMutation,createRemoveMutation,createSetMutation}from"./create.js";export{HLC}from"./hlc.js";export{convertPatchInput,createMutationOperations}from"./operations.js";
@@ -0,0 +1,19 @@
1
+ import type { PatchOperation } from '@kubun/graphql';
2
+ import type { ChangeDocumentMutation, DocumentData, SetDocumentMutation } from '@kubun/protocol';
3
+ import type { HLC } from './hlc.js';
4
+ export type GetRandomValues = <T extends ArrayBufferView>(array: T) => T;
5
+ export type CreateMutationOperationsParams<T> = {
6
+ issuer: string;
7
+ hlc: HLC;
8
+ getRandomValues?: GetRandomValues;
9
+ processSetMutation(mutation: SetDocumentMutation): Promise<T>;
10
+ processChangeMutation(mutation: ChangeDocumentMutation): Promise<T>;
11
+ };
12
+ export type MutationOperations<T> = {
13
+ createDocument(modelID: string, data: DocumentData): Promise<T>;
14
+ setDocument(modelID: string, unique: Uint8Array, data: DocumentData): Promise<T>;
15
+ updateDocument(docID: string, patch: Array<PatchOperation>): Promise<T>;
16
+ removeDocument(docID: string): Promise<T>;
17
+ };
18
+ export declare function createMutationOperations<T>(params: CreateMutationOperationsParams<T>): MutationOperations<T>;
19
+ export declare function convertPatchInput(patch: Array<Record<string, unknown>>): Array<PatchOperation>;
@@ -0,0 +1 @@
1
+ import{createChangeMutation as t,createRemoveMutation as e,createSetMutation as a}from"./create.js";export function createMutationOperations(n){let{issuer:r,hlc:o,processSetMutation:c,processChangeMutation:u}=n,i=n.getRandomValues??globalThis.crypto.getRandomValues.bind(globalThis.crypto);return{async createDocument(t,e){let n=i(new Uint8Array(12)),u=await a({issuer:r,hlc:o,modelID:t,data:e,unique:n});return await c(u)},async setDocument(t,e,n){let u=await a({issuer:r,hlc:o,modelID:t,data:n,unique:e});return await c(u)},async updateDocument(e,a){let n=t({issuer:r,hlc:o,docID:e,patch:a});return await u(n)},async removeDocument(t){let a=e({docID:t,issuer:r,hlc:o});return await u(a)}}}export function convertPatchInput(t){return t.map(t=>{let[e,a]=Object.entries(t)[0];return{...a,op:e}})}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/mutation",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "license": "see LICENSE.md",
5
5
  "keywords": [],
6
6
  "type": "module",
@@ -15,17 +15,14 @@
15
15
  ],
16
16
  "sideEffects": false,
17
17
  "dependencies": {
18
- "@automerge/automerge": "^3.2.3",
19
- "@enkaku/async": "^0.13.0",
20
18
  "@enkaku/codec": "^0.13.0",
21
- "@enkaku/patch": "^0.13.0",
22
19
  "@enkaku/schema": "^0.13.0",
23
- "@kubun/id": "^0.5.0"
20
+ "@kubun/id": "^0.6.0"
24
21
  },
25
22
  "devDependencies": {
26
- "@kubun/graphql": "^0.5.0",
27
- "@kubun/db": "^0.5.0",
28
- "@kubun/protocol": "^0.5.0"
23
+ "@kubun/db": "^0.6.0",
24
+ "@kubun/graphql": "^0.6.0",
25
+ "@kubun/protocol": "^0.6.0"
29
26
  },
30
27
  "scripts": {
31
28
  "build:clean": "del lib",
@@ -1,4 +0,0 @@
1
- import * as A from '@automerge/automerge/slim';
2
- import type { DocumentData } from '@kubun/protocol';
3
- export declare const automergeReady: import("@enkaku/async").LazyPromise<void>;
4
- export declare function automergeToData(doc: A.Doc<DocumentData>): DocumentData;
package/lib/automerge.js DELETED
@@ -1 +0,0 @@
1
- import{automergeWasmBase64 as e}from"@automerge/automerge/automerge.wasm.base64";import*as a from"@automerge/automerge/slim";import{lazy as r}from"@enkaku/async";export const automergeReady=r(()=>a.initializeBase64Wasm(e));export function automergeToData(e){return JSON.parse(JSON.stringify(e))}