@feathersdev/automerge 0.11.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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/esm/index.d.ts +32 -0
- package/esm/index.js +58 -0
- package/package.json +54 -0
- package/src/index.ts +80 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [0.11.0](https://github.com/feathersdev/app/compare/v0.10.1...v0.11.0) (2025-05-25)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Basic Automerge feature integration ([#275](https://github.com/feathersdev/app/issues/275)) ([3ccc80c](https://github.com/feathersdev/app/commit/3ccc80cdd02db52fc32d7e1566b844ef211c2c0f))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Feathers Cloud Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { AnyDocumentId, StorageAdapterInterface } from '@automerge/automerge-repo';
|
|
2
|
+
import type { FeathersAuthClient } from '@feathersdev/auth';
|
|
3
|
+
import { Repo } from '@automerge/automerge-repo';
|
|
4
|
+
export * from '@automerge/automerge-repo';
|
|
5
|
+
export interface AutomergeClientOptions {
|
|
6
|
+
storage: StorageAdapterInterface;
|
|
7
|
+
syncUrl: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const syncUrl = "https://sync.feathers.dev";
|
|
10
|
+
export declare class AutomergeClient {
|
|
11
|
+
client: FeathersAuthClient;
|
|
12
|
+
options: AutomergeClientOptions;
|
|
13
|
+
private repo?;
|
|
14
|
+
constructor(client: FeathersAuthClient, options: AutomergeClientOptions);
|
|
15
|
+
private getDocFromToken;
|
|
16
|
+
connect(): Promise<Repo>;
|
|
17
|
+
/**
|
|
18
|
+
* Retrieve an Automerge document handle. Will use the auth application
|
|
19
|
+
* document if no id is passed.
|
|
20
|
+
*
|
|
21
|
+
* @param _id The Automerge document id or nothing when using the application document
|
|
22
|
+
* @returns The document handle
|
|
23
|
+
*/
|
|
24
|
+
find<T = unknown>(_id?: AnyDocumentId): Promise<import("@automerge/automerge-repo").DocHandle<T>>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create an Automerge client for Feathers
|
|
28
|
+
*
|
|
29
|
+
* @param client The feathers auth client
|
|
30
|
+
* @returns A new Automerge client instance
|
|
31
|
+
*/
|
|
32
|
+
export declare function createAutomerge(client: FeathersAuthClient, options?: Partial<AutomergeClientOptions>): AutomergeClient;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Repo } from '@automerge/automerge-repo';
|
|
2
|
+
import { BrowserWebSocketClientAdapter } from '@automerge/automerge-repo-network-websocket';
|
|
3
|
+
import { IndexedDBStorageAdapter } from '@automerge/automerge-repo-storage-indexeddb';
|
|
4
|
+
import { verifyToken } from '@feathersdev/auth/internals';
|
|
5
|
+
export * from '@automerge/automerge-repo';
|
|
6
|
+
export const syncUrl = 'https://sync.feathers.dev';
|
|
7
|
+
export class AutomergeClient {
|
|
8
|
+
constructor(client, options) {
|
|
9
|
+
this.client = client;
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
async getDocFromToken() {
|
|
13
|
+
const getAccessToken = await this.client.getAccessToken();
|
|
14
|
+
const { payload } = await verifyToken(getAccessToken);
|
|
15
|
+
return payload.doc;
|
|
16
|
+
}
|
|
17
|
+
async connect() {
|
|
18
|
+
if (!this.repo) {
|
|
19
|
+
const { syncUrl, storage } = this.options;
|
|
20
|
+
const token = await this.client.getAccessToken();
|
|
21
|
+
const wsUrl = `${syncUrl}?authorization=${token}`;
|
|
22
|
+
this.repo = new Repo({
|
|
23
|
+
network: [new BrowserWebSocketClientAdapter(wsUrl)],
|
|
24
|
+
storage,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return this.repo;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Retrieve an Automerge document handle. Will use the auth application
|
|
31
|
+
* document if no id is passed.
|
|
32
|
+
*
|
|
33
|
+
* @param _id The Automerge document id or nothing when using the application document
|
|
34
|
+
* @returns The document handle
|
|
35
|
+
*/
|
|
36
|
+
async find(_id) {
|
|
37
|
+
const id = _id ?? (await this.getDocFromToken());
|
|
38
|
+
if (!id) {
|
|
39
|
+
throw new Error('Invalid document id');
|
|
40
|
+
}
|
|
41
|
+
const repo = await this.connect();
|
|
42
|
+
return repo.find(id);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Create an Automerge client for Feathers
|
|
47
|
+
*
|
|
48
|
+
* @param client The feathers auth client
|
|
49
|
+
* @returns A new Automerge client instance
|
|
50
|
+
*/
|
|
51
|
+
export function createAutomerge(client, options) {
|
|
52
|
+
const storage = options?.storage ?? new IndexedDBStorageAdapter();
|
|
53
|
+
return new AutomergeClient(client, {
|
|
54
|
+
storage,
|
|
55
|
+
syncUrl,
|
|
56
|
+
...options,
|
|
57
|
+
});
|
|
58
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@feathersdev/automerge",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.11.0",
|
|
5
|
+
"description": "Feathers local-first data synchronization using Automerge",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Feathers Cloud Inc.",
|
|
8
|
+
"email": "hello@feathers.dev",
|
|
9
|
+
"url": "https://feathers.dev"
|
|
10
|
+
},
|
|
11
|
+
"contributors": [],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"homepage": "https://feathers.dev",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git://github.com/feathersdev/app.git",
|
|
17
|
+
"directory": "packages/automerge"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"feathersdev"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"main": "./esm/index.js",
|
|
26
|
+
"files": [
|
|
27
|
+
"CHANGELOG.md",
|
|
28
|
+
"LICENSE",
|
|
29
|
+
"README.md",
|
|
30
|
+
"esm/**",
|
|
31
|
+
"lib/**",
|
|
32
|
+
"src/**"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">= 18"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"prepublish": "pnpm compile",
|
|
39
|
+
"compile": "shx rm -rf esm/ && npx tsc --outDir esm/"
|
|
40
|
+
},
|
|
41
|
+
"directories": {
|
|
42
|
+
"lib": "lib"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@automerge/automerge-repo": "^1.2.1",
|
|
46
|
+
"@automerge/automerge-repo-network-websocket": "^1.2.1",
|
|
47
|
+
"@automerge/automerge-repo-storage-indexeddb": "^1.2.1",
|
|
48
|
+
"@feathersdev/auth": "^0.11.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@vitest/browser": "^3.1.2"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "c39ce0034f664037faf0e4be4ca209309835bfa4"
|
|
54
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { AnyDocumentId, StorageAdapterInterface } from '@automerge/automerge-repo'
|
|
2
|
+
import type { FeathersAuthClient } from '@feathersdev/auth'
|
|
3
|
+
import { Repo } from '@automerge/automerge-repo'
|
|
4
|
+
import { BrowserWebSocketClientAdapter } from '@automerge/automerge-repo-network-websocket'
|
|
5
|
+
import { IndexedDBStorageAdapter } from '@automerge/automerge-repo-storage-indexeddb'
|
|
6
|
+
import { verifyToken } from '@feathersdev/auth/internals'
|
|
7
|
+
|
|
8
|
+
export * from '@automerge/automerge-repo'
|
|
9
|
+
|
|
10
|
+
export interface AutomergeClientOptions {
|
|
11
|
+
storage: StorageAdapterInterface
|
|
12
|
+
syncUrl: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const syncUrl = 'https://sync.feathers.dev'
|
|
16
|
+
|
|
17
|
+
export class AutomergeClient {
|
|
18
|
+
private repo?: Repo
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
public client: FeathersAuthClient,
|
|
22
|
+
public options: AutomergeClientOptions,
|
|
23
|
+
) {}
|
|
24
|
+
|
|
25
|
+
private async getDocFromToken() {
|
|
26
|
+
const getAccessToken = await this.client.getAccessToken()
|
|
27
|
+
const { payload } = await verifyToken<{ doc?: string }>(getAccessToken)
|
|
28
|
+
return payload.doc
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async connect() {
|
|
32
|
+
if (!this.repo) {
|
|
33
|
+
const { syncUrl, storage } = this.options
|
|
34
|
+
const token = await this.client.getAccessToken()
|
|
35
|
+
const wsUrl = `${syncUrl}?authorization=${token}`
|
|
36
|
+
|
|
37
|
+
this.repo = new Repo({
|
|
38
|
+
network: [new BrowserWebSocketClientAdapter(wsUrl)],
|
|
39
|
+
storage,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return this.repo
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Retrieve an Automerge document handle. Will use the auth application
|
|
48
|
+
* document if no id is passed.
|
|
49
|
+
*
|
|
50
|
+
* @param _id The Automerge document id or nothing when using the application document
|
|
51
|
+
* @returns The document handle
|
|
52
|
+
*/
|
|
53
|
+
async find<T = unknown>(_id?: AnyDocumentId) {
|
|
54
|
+
const id = _id ?? (await this.getDocFromToken())
|
|
55
|
+
|
|
56
|
+
if (!id) {
|
|
57
|
+
throw new Error('Invalid document id')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const repo = await this.connect()
|
|
61
|
+
|
|
62
|
+
return repo.find<T>(id as AnyDocumentId)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create an Automerge client for Feathers
|
|
68
|
+
*
|
|
69
|
+
* @param client The feathers auth client
|
|
70
|
+
* @returns A new Automerge client instance
|
|
71
|
+
*/
|
|
72
|
+
export function createAutomerge(client: FeathersAuthClient, options?: Partial<AutomergeClientOptions>) {
|
|
73
|
+
const storage = options?.storage ?? new IndexedDBStorageAdapter()
|
|
74
|
+
|
|
75
|
+
return new AutomergeClient(client, {
|
|
76
|
+
storage,
|
|
77
|
+
syncUrl,
|
|
78
|
+
...options,
|
|
79
|
+
})
|
|
80
|
+
}
|