@dabble/patches 0.1.1
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/README.md +632 -0
- package/dist/client/PatchDoc.d.ts +85 -0
- package/dist/client/PatchDoc.js +299 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +1 -0
- package/dist/event-signal.d.ts +31 -0
- package/dist/event-signal.js +40 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/json-patch/JSONPatch.d.ts +126 -0
- package/dist/json-patch/JSONPatch.js +221 -0
- package/dist/json-patch/applyPatch.d.ts +11 -0
- package/dist/json-patch/applyPatch.js +37 -0
- package/dist/json-patch/composePatch.d.ts +2 -0
- package/dist/json-patch/composePatch.js +38 -0
- package/dist/json-patch/createJSONPatch.d.ts +35 -0
- package/dist/json-patch/createJSONPatch.js +41 -0
- package/dist/json-patch/index.d.ts +9 -0
- package/dist/json-patch/index.js +8 -0
- package/dist/json-patch/invertPatch.d.ts +2 -0
- package/dist/json-patch/invertPatch.js +31 -0
- package/dist/json-patch/ops/add.d.ts +2 -0
- package/dist/json-patch/ops/add.js +52 -0
- package/dist/json-patch/ops/bitmask.d.ts +14 -0
- package/dist/json-patch/ops/bitmask.js +48 -0
- package/dist/json-patch/ops/copy.d.ts +2 -0
- package/dist/json-patch/ops/copy.js +34 -0
- package/dist/json-patch/ops/increment.d.ts +5 -0
- package/dist/json-patch/ops/increment.js +21 -0
- package/dist/json-patch/ops/index.d.ts +22 -0
- package/dist/json-patch/ops/index.js +25 -0
- package/dist/json-patch/ops/move.d.ts +2 -0
- package/dist/json-patch/ops/move.js +211 -0
- package/dist/json-patch/ops/remove.d.ts +2 -0
- package/dist/json-patch/ops/remove.js +31 -0
- package/dist/json-patch/ops/replace.d.ts +2 -0
- package/dist/json-patch/ops/replace.js +44 -0
- package/dist/json-patch/ops/test.d.ts +2 -0
- package/dist/json-patch/ops/test.js +22 -0
- package/dist/json-patch/ops/text.d.ts +2 -0
- package/dist/json-patch/ops/text.js +57 -0
- package/dist/json-patch/patchProxy.d.ts +41 -0
- package/dist/json-patch/patchProxy.js +125 -0
- package/dist/json-patch/state.d.ts +2 -0
- package/dist/json-patch/state.js +8 -0
- package/dist/json-patch/transformPatch.d.ts +19 -0
- package/dist/json-patch/transformPatch.js +37 -0
- package/dist/json-patch/types.d.ts +52 -0
- package/dist/json-patch/types.js +1 -0
- package/dist/json-patch/utils/deepEqual.d.ts +1 -0
- package/dist/json-patch/utils/deepEqual.js +33 -0
- package/dist/json-patch/utils/exit.d.ts +2 -0
- package/dist/json-patch/utils/exit.js +4 -0
- package/dist/json-patch/utils/get.d.ts +2 -0
- package/dist/json-patch/utils/get.js +6 -0
- package/dist/json-patch/utils/getOpData.d.ts +2 -0
- package/dist/json-patch/utils/getOpData.js +10 -0
- package/dist/json-patch/utils/getType.d.ts +3 -0
- package/dist/json-patch/utils/getType.js +6 -0
- package/dist/json-patch/utils/index.d.ts +14 -0
- package/dist/json-patch/utils/index.js +14 -0
- package/dist/json-patch/utils/log.d.ts +2 -0
- package/dist/json-patch/utils/log.js +7 -0
- package/dist/json-patch/utils/ops.d.ts +14 -0
- package/dist/json-patch/utils/ops.js +103 -0
- package/dist/json-patch/utils/paths.d.ts +9 -0
- package/dist/json-patch/utils/paths.js +53 -0
- package/dist/json-patch/utils/pluck.d.ts +5 -0
- package/dist/json-patch/utils/pluck.js +30 -0
- package/dist/json-patch/utils/shallowCopy.d.ts +1 -0
- package/dist/json-patch/utils/shallowCopy.js +20 -0
- package/dist/json-patch/utils/softWrites.d.ts +7 -0
- package/dist/json-patch/utils/softWrites.js +18 -0
- package/dist/json-patch/utils/toArrayIndex.d.ts +1 -0
- package/dist/json-patch/utils/toArrayIndex.js +12 -0
- package/dist/json-patch/utils/toKeys.d.ts +1 -0
- package/dist/json-patch/utils/toKeys.js +15 -0
- package/dist/json-patch/utils/updateArrayIndexes.d.ts +5 -0
- package/dist/json-patch/utils/updateArrayIndexes.js +38 -0
- package/dist/json-patch/utils/updateArrayPath.d.ts +5 -0
- package/dist/json-patch/utils/updateArrayPath.js +45 -0
- package/dist/net/AbstractTransport.d.ts +47 -0
- package/dist/net/AbstractTransport.js +37 -0
- package/dist/net/PatchesOfflineFirst.d.ts +3 -0
- package/dist/net/PatchesOfflineFirst.js +3 -0
- package/dist/net/PatchesRealtime.d.ts +90 -0
- package/dist/net/PatchesRealtime.js +257 -0
- package/dist/net/index.d.ts +9 -0
- package/dist/net/index.js +8 -0
- package/dist/net/protocol/JSONRPCClient.d.ts +55 -0
- package/dist/net/protocol/JSONRPCClient.js +106 -0
- package/dist/net/protocol/types.d.ts +142 -0
- package/dist/net/protocol/types.js +1 -0
- package/dist/net/webrtc/WebRTCAwareness.d.ts +81 -0
- package/dist/net/webrtc/WebRTCAwareness.js +119 -0
- package/dist/net/webrtc/WebRTCTransport.d.ts +80 -0
- package/dist/net/webrtc/WebRTCTransport.js +157 -0
- package/dist/net/websocket/PatchesWebSocket.d.ts +107 -0
- package/dist/net/websocket/PatchesWebSocket.js +144 -0
- package/dist/net/websocket/SignalingService.d.ts +91 -0
- package/dist/net/websocket/SignalingService.js +140 -0
- package/dist/net/websocket/WebSocketTransport.d.ts +47 -0
- package/dist/net/websocket/WebSocketTransport.js +138 -0
- package/dist/persist/IndexedDBStore.d.ts +72 -0
- package/dist/persist/IndexedDBStore.js +283 -0
- package/dist/persist/index.d.ts +2 -0
- package/dist/persist/index.js +1 -0
- package/dist/server/BranchManager.d.ts +40 -0
- package/dist/server/BranchManager.js +138 -0
- package/dist/server/HistoryManager.d.ts +63 -0
- package/dist/server/HistoryManager.js +92 -0
- package/dist/server/PatchServer.d.ts +129 -0
- package/dist/server/PatchServer.js +358 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.js +3 -0
- package/dist/types.d.ts +158 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +36 -0
- package/dist/utils.js +83 -0
- package/package.json +78 -0
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Change, DeletedChange } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Splits an array of changes into two arrays based on the presence of a baseRev.
|
|
4
|
+
* The first array contains changes before the first change with a baseRev,
|
|
5
|
+
* and the second array contains the change with baseRev and all subsequent changes.
|
|
6
|
+
*
|
|
7
|
+
* @param changes - Array of changes to split
|
|
8
|
+
* @returns A tuple containing [changes before baseRev, changes with and after baseRev]
|
|
9
|
+
*/
|
|
10
|
+
export declare function splitChanges<T>(changes: Change[]): [Change[], Change[]];
|
|
11
|
+
/**
|
|
12
|
+
* Applies a sequence of changes to a state object.
|
|
13
|
+
* Each change is applied in sequence using the applyPatch function.
|
|
14
|
+
*
|
|
15
|
+
* @param state - The initial state to apply changes to
|
|
16
|
+
* @param changes - Array of changes to apply
|
|
17
|
+
* @returns The state after all changes have been applied
|
|
18
|
+
*/
|
|
19
|
+
export declare function applyChanges<T>(state: T, changes: Change[]): T;
|
|
20
|
+
/**
|
|
21
|
+
* Rebases local changes against server changes using operational transformation.
|
|
22
|
+
* This function handles the transformation of local changes to be compatible with server changes
|
|
23
|
+
* that have been applied in the meantime.
|
|
24
|
+
*
|
|
25
|
+
* The process:
|
|
26
|
+
* 1. Filters out local changes that are already in server changes
|
|
27
|
+
* 2. Creates a patch from server changes that need to be transformed against
|
|
28
|
+
* 3. Transforms each remaining local change against the server patch
|
|
29
|
+
* 4. Updates revision numbers for the transformed changes
|
|
30
|
+
*
|
|
31
|
+
* @param serverChanges - Array of changes received from the server
|
|
32
|
+
* @param localChanges - Array of local changes that need to be rebased
|
|
33
|
+
* @returns Array of rebased local changes with updated revision numbers
|
|
34
|
+
*/
|
|
35
|
+
export declare function rebaseChanges(serverChanges: Change[], localChanges: Change[]): Change[];
|
|
36
|
+
export declare function isChange(change: Change | DeletedChange): change is Change;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { applyPatch } from './json-patch/applyPatch.js';
|
|
2
|
+
import { JSONPatch } from './json-patch/JSONPatch.js';
|
|
3
|
+
/**
|
|
4
|
+
* Splits an array of changes into two arrays based on the presence of a baseRev.
|
|
5
|
+
* The first array contains changes before the first change with a baseRev,
|
|
6
|
+
* and the second array contains the change with baseRev and all subsequent changes.
|
|
7
|
+
*
|
|
8
|
+
* @param changes - Array of changes to split
|
|
9
|
+
* @returns A tuple containing [changes before baseRev, changes with and after baseRev]
|
|
10
|
+
*/
|
|
11
|
+
export function splitChanges(changes) {
|
|
12
|
+
const index = changes.findIndex(c => c.baseRev);
|
|
13
|
+
return [changes.slice(0, index), changes.slice(index)];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Applies a sequence of changes to a state object.
|
|
17
|
+
* Each change is applied in sequence using the applyPatch function.
|
|
18
|
+
*
|
|
19
|
+
* @param state - The initial state to apply changes to
|
|
20
|
+
* @param changes - Array of changes to apply
|
|
21
|
+
* @returns The state after all changes have been applied
|
|
22
|
+
*/
|
|
23
|
+
export function applyChanges(state, changes) {
|
|
24
|
+
if (!changes.length)
|
|
25
|
+
return state;
|
|
26
|
+
for (const change of changes) {
|
|
27
|
+
state = applyPatch(state, change.ops, { strict: true });
|
|
28
|
+
}
|
|
29
|
+
return state;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Rebases local changes against server changes using operational transformation.
|
|
33
|
+
* This function handles the transformation of local changes to be compatible with server changes
|
|
34
|
+
* that have been applied in the meantime.
|
|
35
|
+
*
|
|
36
|
+
* The process:
|
|
37
|
+
* 1. Filters out local changes that are already in server changes
|
|
38
|
+
* 2. Creates a patch from server changes that need to be transformed against
|
|
39
|
+
* 3. Transforms each remaining local change against the server patch
|
|
40
|
+
* 4. Updates revision numbers for the transformed changes
|
|
41
|
+
*
|
|
42
|
+
* @param serverChanges - Array of changes received from the server
|
|
43
|
+
* @param localChanges - Array of local changes that need to be rebased
|
|
44
|
+
* @returns Array of rebased local changes with updated revision numbers
|
|
45
|
+
*/
|
|
46
|
+
export function rebaseChanges(serverChanges, localChanges) {
|
|
47
|
+
if (!serverChanges.length || !localChanges.length) {
|
|
48
|
+
return localChanges;
|
|
49
|
+
}
|
|
50
|
+
const lastChange = serverChanges[serverChanges.length - 1];
|
|
51
|
+
const receivedIds = new Set(serverChanges.map(change => change.id));
|
|
52
|
+
const transformAgainstIds = new Set(receivedIds);
|
|
53
|
+
// Filter out local changes that are already in server changes
|
|
54
|
+
const filteredLocalChanges = [];
|
|
55
|
+
for (const change of localChanges) {
|
|
56
|
+
if (receivedIds.has(change.id)) {
|
|
57
|
+
transformAgainstIds.delete(change.id);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
filteredLocalChanges.push(change);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Create a patch from server changes that need to be transformed against
|
|
64
|
+
const transformPatch = new JSONPatch(serverChanges
|
|
65
|
+
.filter(change => transformAgainstIds.has(change.id))
|
|
66
|
+
.map(change => change.ops)
|
|
67
|
+
.flat());
|
|
68
|
+
// Rebase local changes against server changes
|
|
69
|
+
const base = lastChange.rev;
|
|
70
|
+
let rev = lastChange.rev;
|
|
71
|
+
return filteredLocalChanges
|
|
72
|
+
.map(change => {
|
|
73
|
+
rev++;
|
|
74
|
+
const ops = transformPatch.transform(change.ops).ops;
|
|
75
|
+
if (!ops.length)
|
|
76
|
+
return null;
|
|
77
|
+
return { ...change, base, rev, ops };
|
|
78
|
+
})
|
|
79
|
+
.filter(Boolean);
|
|
80
|
+
}
|
|
81
|
+
export function isChange(change) {
|
|
82
|
+
return 'rev' in change;
|
|
83
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dabble/patches",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Immutable JSON Patch implementation based on RFC 6902 supporting operational transformation and last-writer-wins",
|
|
5
|
+
"author": "Jacob Wright <jacwright@gmail.com>",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/dabblewriter/patches/issues"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./client": {
|
|
18
|
+
"import": "./dist/client/index.js",
|
|
19
|
+
"types": "./dist/client/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./server": {
|
|
22
|
+
"import": "./dist/server/index.js",
|
|
23
|
+
"types": "./dist/server/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./net": {
|
|
26
|
+
"import": "./dist/net/index.js",
|
|
27
|
+
"types": "./dist/net/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./persist": {
|
|
30
|
+
"import": "./dist/persist/index.js",
|
|
31
|
+
"types": "./dist/persist/index.d.ts"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"package.json",
|
|
36
|
+
"README.md",
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"homepage": "https://github.com/dabblewriter/patches/",
|
|
40
|
+
"keywords": [
|
|
41
|
+
"immutable",
|
|
42
|
+
"json",
|
|
43
|
+
"patch",
|
|
44
|
+
"rfc6902",
|
|
45
|
+
"operational-transformation",
|
|
46
|
+
"collaborative-editing"
|
|
47
|
+
],
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "https://github.com/dabblewriter/patches.git"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"dev": "vite",
|
|
55
|
+
"build": "svelte-package -i src",
|
|
56
|
+
"prepare": "npm run build",
|
|
57
|
+
"test": "vitest run",
|
|
58
|
+
"tdd": "vitest"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"alphacounter": "^2.x",
|
|
62
|
+
"crypto-id": "^0.2.3",
|
|
63
|
+
"simple-peer": "^9.11.1"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"@dabble/delta": "^1.2.4"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@sveltejs/package": "^2.3.10",
|
|
70
|
+
"@types/simple-peer": "^9.11.8",
|
|
71
|
+
"fake-indexeddb": "^6.0.0",
|
|
72
|
+
"prettier": "^3.5.3",
|
|
73
|
+
"typescript": "^5.8.3",
|
|
74
|
+
"vite": "^6.2.6",
|
|
75
|
+
"vite-plugin-dts": "^4.5.3",
|
|
76
|
+
"vitest": "^3.1.1"
|
|
77
|
+
}
|
|
78
|
+
}
|