@peerbit/canonical-client 0.0.0-e209d2e
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/LICENSE +202 -0
- package/dist/src/auto.d.ts +41 -0
- package/dist/src/auto.d.ts.map +1 -0
- package/dist/src/auto.js +104 -0
- package/dist/src/auto.js.map +1 -0
- package/dist/src/client.d.ts +76 -0
- package/dist/src/client.d.ts.map +1 -0
- package/dist/src/client.js +709 -0
- package/dist/src/client.js.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/peerbit.d.ts +51 -0
- package/dist/src/peerbit.d.ts.map +1 -0
- package/dist/src/peerbit.js +253 -0
- package/dist/src/peerbit.js.map +1 -0
- package/dist/src/service-worker.d.ts +19 -0
- package/dist/src/service-worker.d.ts.map +1 -0
- package/dist/src/service-worker.js +89 -0
- package/dist/src/service-worker.js.map +1 -0
- package/dist/src/window.d.ts +17 -0
- package/dist/src/window.d.ts.map +1 -0
- package/dist/src/window.js +80 -0
- package/dist/src/window.js.map +1 -0
- package/package.json +90 -0
- package/src/auto.ts +166 -0
- package/src/client.ts +722 -0
- package/src/index.ts +9 -0
- package/src/peerbit.ts +354 -0
- package/src/service-worker.ts +118 -0
- package/src/window.ts +112 -0
package/package.json
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peerbit/canonical-client",
|
|
3
|
+
"version": "0.0.0-e209d2e",
|
|
4
|
+
"description": "Client helpers for connecting to a Peerbit canonical runtime",
|
|
5
|
+
"author": "dao.xyz",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"types": "./dist/src/index.d.ts",
|
|
10
|
+
"typesVersions": {
|
|
11
|
+
"*": {
|
|
12
|
+
"*": [
|
|
13
|
+
"*",
|
|
14
|
+
"dist/*",
|
|
15
|
+
"dist/src/*",
|
|
16
|
+
"dist/src/*/index"
|
|
17
|
+
],
|
|
18
|
+
"src/*": [
|
|
19
|
+
"*",
|
|
20
|
+
"dist/*",
|
|
21
|
+
"dist/src/*",
|
|
22
|
+
"dist/src/*/index"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"dist",
|
|
29
|
+
"!dist/test",
|
|
30
|
+
"!**/*.tsbuildinfo"
|
|
31
|
+
],
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/src/index.d.ts",
|
|
35
|
+
"import": "./dist/src/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./window": {
|
|
38
|
+
"types": "./dist/src/window.d.ts",
|
|
39
|
+
"import": "./dist/src/window.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"eslintConfig": {
|
|
43
|
+
"extends": "peerbit",
|
|
44
|
+
"parserOptions": {
|
|
45
|
+
"project": true,
|
|
46
|
+
"sourceType": "module"
|
|
47
|
+
},
|
|
48
|
+
"ignorePatterns": [
|
|
49
|
+
"!.aegir.js",
|
|
50
|
+
"test/ts-use",
|
|
51
|
+
"*.d.ts"
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "https://github.com/dao-xyz/peerbit"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=18"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@dao-xyz/borsh": "^6.0.0",
|
|
66
|
+
"@libp2p/interface": "^3.1.0",
|
|
67
|
+
"@libp2p/peer-id": "^6.0.1",
|
|
68
|
+
"@multiformats/multiaddr": "^13.0.1",
|
|
69
|
+
"@peerbit/canonical-transport": "0.0.0-e209d2e",
|
|
70
|
+
"@peerbit/crypto": "2.4.1-e209d2e",
|
|
71
|
+
"@peerbit/program": "5.5.2-e209d2e"
|
|
72
|
+
},
|
|
73
|
+
"devDependencies": {},
|
|
74
|
+
"localMaintainers": [
|
|
75
|
+
"dao.xyz"
|
|
76
|
+
],
|
|
77
|
+
"keywords": [
|
|
78
|
+
"peerbit",
|
|
79
|
+
"rpc",
|
|
80
|
+
"canonical",
|
|
81
|
+
"worker",
|
|
82
|
+
"sharedworker"
|
|
83
|
+
],
|
|
84
|
+
"scripts": {
|
|
85
|
+
"clean": "aegir clean",
|
|
86
|
+
"build": "aegir build --no-bundle",
|
|
87
|
+
"test": "aegir test --target node",
|
|
88
|
+
"lint": "aegir lint"
|
|
89
|
+
}
|
|
90
|
+
}
|
package/src/auto.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Address,
|
|
3
|
+
OpenOptions,
|
|
4
|
+
Program,
|
|
5
|
+
ProgramClient,
|
|
6
|
+
} from "@peerbit/program";
|
|
7
|
+
import type { CanonicalClient } from "./client.js";
|
|
8
|
+
|
|
9
|
+
export type CanonicalOpenMode = "auto" | "local" | "canonical";
|
|
10
|
+
|
|
11
|
+
export type CanonicalOpenResult<T> = {
|
|
12
|
+
proxy: T;
|
|
13
|
+
address?: Address;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type CanonicalOpenAdapter<
|
|
17
|
+
S extends Program<any> = Program<any>,
|
|
18
|
+
T = unknown,
|
|
19
|
+
> = {
|
|
20
|
+
name: string;
|
|
21
|
+
canOpen(program: Program<any>): program is S;
|
|
22
|
+
getKey?(program: S, options?: OpenOptions<S>): string | undefined;
|
|
23
|
+
open(ctx: {
|
|
24
|
+
program: S;
|
|
25
|
+
options: OpenOptions<S>;
|
|
26
|
+
peer: ProgramClient;
|
|
27
|
+
client: CanonicalClient;
|
|
28
|
+
}): Promise<CanonicalOpenResult<T>>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type ManagedProxy = {
|
|
32
|
+
parents: any[];
|
|
33
|
+
children: any[];
|
|
34
|
+
closed: boolean;
|
|
35
|
+
close: (from?: any) => Promise<boolean>;
|
|
36
|
+
drop: (from?: any) => Promise<boolean>;
|
|
37
|
+
emitEvent?: (event: CustomEvent) => void;
|
|
38
|
+
address?: Address;
|
|
39
|
+
rootAddress?: Address;
|
|
40
|
+
isRoot?: boolean;
|
|
41
|
+
node?: ProgramClient;
|
|
42
|
+
save?: () => Promise<Address>;
|
|
43
|
+
delete?: () => Promise<void>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const createManagedProxy = <T extends { close: () => Promise<void> }>(
|
|
47
|
+
proxy: T,
|
|
48
|
+
options: {
|
|
49
|
+
address?: Address;
|
|
50
|
+
node?: ProgramClient;
|
|
51
|
+
onClose?: () => void;
|
|
52
|
+
} = {},
|
|
53
|
+
): T & ManagedProxy => {
|
|
54
|
+
const parents: any[] = [];
|
|
55
|
+
const children: any[] = [];
|
|
56
|
+
let closed = false;
|
|
57
|
+
const rawClose = proxy.close.bind(proxy);
|
|
58
|
+
|
|
59
|
+
const close = async (from?: any): Promise<boolean> => {
|
|
60
|
+
if (closed) return true;
|
|
61
|
+
if (from != null) {
|
|
62
|
+
const idx = parents.findIndex((p) => p === from);
|
|
63
|
+
if (idx === -1) {
|
|
64
|
+
throw new Error("Could not find parent");
|
|
65
|
+
}
|
|
66
|
+
if (parents.length > 1) {
|
|
67
|
+
parents.splice(idx, 1);
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const pending = children.splice(0);
|
|
73
|
+
await Promise.all(
|
|
74
|
+
pending.map((child) =>
|
|
75
|
+
typeof child?.close === "function"
|
|
76
|
+
? child.close(proxy as any)
|
|
77
|
+
: undefined,
|
|
78
|
+
),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
await rawClose();
|
|
82
|
+
closed = true;
|
|
83
|
+
(proxy as any).closed = true;
|
|
84
|
+
options.onClose?.();
|
|
85
|
+
return true;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const drop = async (from?: any): Promise<boolean> => {
|
|
89
|
+
return close(from);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const emitEvent = (event: CustomEvent) => {
|
|
93
|
+
const events = (proxy as any).events;
|
|
94
|
+
if (events && typeof events.dispatchEvent === "function") {
|
|
95
|
+
events.dispatchEvent(event);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const isRoot = () => {
|
|
100
|
+
if (!parents || parents.length === 0) return true;
|
|
101
|
+
return parents.filter((parent) => !!parent).length === 0;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const rootAddress = () => {
|
|
105
|
+
let root: any = proxy;
|
|
106
|
+
while (root?.parents && root.parents.length > 0) {
|
|
107
|
+
if (root.parents.length > 1) {
|
|
108
|
+
throw new Error("Multiple parents not supported");
|
|
109
|
+
}
|
|
110
|
+
const parent = root.parents[0];
|
|
111
|
+
if (!parent) break;
|
|
112
|
+
root = parent;
|
|
113
|
+
}
|
|
114
|
+
const address = root?.address;
|
|
115
|
+
if (!address) {
|
|
116
|
+
throw new Error("Proxy has no address");
|
|
117
|
+
}
|
|
118
|
+
return address as Address;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const save = async () => {
|
|
122
|
+
if (options.address) return options.address;
|
|
123
|
+
if ((proxy as any).address) return (proxy as any).address as Address;
|
|
124
|
+
throw new Error("Proxy has no address");
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const del = async () => {
|
|
128
|
+
await close();
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
Object.assign(proxy as any, {
|
|
132
|
+
parents,
|
|
133
|
+
children,
|
|
134
|
+
close,
|
|
135
|
+
drop,
|
|
136
|
+
emitEvent,
|
|
137
|
+
});
|
|
138
|
+
(proxy as any).closed = closed;
|
|
139
|
+
if (!("isRoot" in (proxy as any))) {
|
|
140
|
+
Object.defineProperty(proxy as any, "isRoot", {
|
|
141
|
+
get: isRoot,
|
|
142
|
+
enumerable: true,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
if (!("rootAddress" in (proxy as any))) {
|
|
146
|
+
Object.defineProperty(proxy as any, "rootAddress", {
|
|
147
|
+
get: rootAddress,
|
|
148
|
+
enumerable: true,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (options.address) {
|
|
153
|
+
(proxy as any).address = options.address;
|
|
154
|
+
}
|
|
155
|
+
if (options.node) {
|
|
156
|
+
(proxy as any).node = options.node;
|
|
157
|
+
}
|
|
158
|
+
if (typeof (proxy as any).save !== "function") {
|
|
159
|
+
(proxy as any).save = save;
|
|
160
|
+
}
|
|
161
|
+
if (typeof (proxy as any).delete !== "function") {
|
|
162
|
+
(proxy as any).delete = del;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return proxy as T & ManagedProxy;
|
|
166
|
+
};
|