@peers-app/peers-sdk 0.1.4
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 +1 -0
- package/dist/context/data-context.d.ts +31 -0
- package/dist/context/data-context.js +56 -0
- package/dist/context/index.d.ts +3 -0
- package/dist/context/index.js +19 -0
- package/dist/context/user-context-singleton.d.ts +11 -0
- package/dist/context/user-context-singleton.js +121 -0
- package/dist/context/user-context.d.ts +55 -0
- package/dist/context/user-context.js +205 -0
- package/dist/data/assistants.d.ts +68 -0
- package/dist/data/assistants.js +64 -0
- package/dist/data/change-tracking.d.ts +219 -0
- package/dist/data/change-tracking.js +119 -0
- package/dist/data/channels.d.ts +29 -0
- package/dist/data/channels.js +25 -0
- package/dist/data/data-locks.d.ts +37 -0
- package/dist/data/data-locks.js +180 -0
- package/dist/data/data-locks.test.d.ts +1 -0
- package/dist/data/data-locks.test.js +456 -0
- package/dist/data/device-sync-info.d.ts +19 -0
- package/dist/data/device-sync-info.js +24 -0
- package/dist/data/devices.d.ts +51 -0
- package/dist/data/devices.js +36 -0
- package/dist/data/embeddings.d.ts +47 -0
- package/dist/data/embeddings.js +36 -0
- package/dist/data/files/file-read-stream.d.ts +27 -0
- package/dist/data/files/file-read-stream.js +195 -0
- package/dist/data/files/file-write-stream.d.ts +20 -0
- package/dist/data/files/file-write-stream.js +113 -0
- package/dist/data/files/file.types.d.ts +47 -0
- package/dist/data/files/file.types.js +55 -0
- package/dist/data/files/files.d.ts +28 -0
- package/dist/data/files/files.js +127 -0
- package/dist/data/files/files.test.d.ts +1 -0
- package/dist/data/files/files.test.js +728 -0
- package/dist/data/files/index.d.ts +4 -0
- package/dist/data/files/index.js +23 -0
- package/dist/data/group-member-roles.d.ts +9 -0
- package/dist/data/group-member-roles.js +25 -0
- package/dist/data/group-members.d.ts +39 -0
- package/dist/data/group-members.js +68 -0
- package/dist/data/group-members.test.d.ts +1 -0
- package/dist/data/group-members.test.js +287 -0
- package/dist/data/group-permissions.d.ts +8 -0
- package/dist/data/group-permissions.js +73 -0
- package/dist/data/group-share.d.ts +50 -0
- package/dist/data/group-share.js +196 -0
- package/dist/data/groups.d.ts +50 -0
- package/dist/data/groups.js +73 -0
- package/dist/data/groups.test.d.ts +1 -0
- package/dist/data/groups.test.js +153 -0
- package/dist/data/index.d.ts +31 -0
- package/dist/data/index.js +47 -0
- package/dist/data/knowledge/knowledge-frames.d.ts +34 -0
- package/dist/data/knowledge/knowledge-frames.js +34 -0
- package/dist/data/knowledge/knowledge-links.d.ts +30 -0
- package/dist/data/knowledge/knowledge-links.js +25 -0
- package/dist/data/knowledge/knowledge-values.d.ts +35 -0
- package/dist/data/knowledge/knowledge-values.js +35 -0
- package/dist/data/knowledge/peer-types.d.ts +112 -0
- package/dist/data/knowledge/peer-types.js +27 -0
- package/dist/data/knowledge/predicates.d.ts +34 -0
- package/dist/data/knowledge/predicates.js +27 -0
- package/dist/data/messages.d.ts +57 -0
- package/dist/data/messages.js +97 -0
- package/dist/data/orm/client-proxy.data-source.d.ts +27 -0
- package/dist/data/orm/client-proxy.data-source.js +65 -0
- package/dist/data/orm/cursor.d.ts +25 -0
- package/dist/data/orm/cursor.js +47 -0
- package/dist/data/orm/cursor.test.d.ts +1 -0
- package/dist/data/orm/cursor.test.js +315 -0
- package/dist/data/orm/data-query.d.ts +96 -0
- package/dist/data/orm/data-query.js +208 -0
- package/dist/data/orm/data-query.mongo.d.ts +17 -0
- package/dist/data/orm/data-query.mongo.js +267 -0
- package/dist/data/orm/data-query.mongo.test.d.ts +1 -0
- package/dist/data/orm/data-query.mongo.test.js +398 -0
- package/dist/data/orm/data-query.sqlite.d.ts +14 -0
- package/dist/data/orm/data-query.sqlite.js +297 -0
- package/dist/data/orm/data-query.sqlite.test.d.ts +1 -0
- package/dist/data/orm/data-query.sqlite.test.js +377 -0
- package/dist/data/orm/data-query.test.d.ts +1 -0
- package/dist/data/orm/data-query.test.js +553 -0
- package/dist/data/orm/decorators.d.ts +6 -0
- package/dist/data/orm/decorators.js +21 -0
- package/dist/data/orm/dependency-injection.test.d.ts +1 -0
- package/dist/data/orm/dependency-injection.test.js +171 -0
- package/dist/data/orm/doc.d.ts +26 -0
- package/dist/data/orm/doc.js +124 -0
- package/dist/data/orm/event-registry.d.ts +24 -0
- package/dist/data/orm/event-registry.js +40 -0
- package/dist/data/orm/event-registry.test.d.ts +1 -0
- package/dist/data/orm/event-registry.test.js +44 -0
- package/dist/data/orm/factory.d.ts +8 -0
- package/dist/data/orm/factory.js +147 -0
- package/dist/data/orm/index.d.ts +16 -0
- package/dist/data/orm/index.js +32 -0
- package/dist/data/orm/multi-cursors.d.ts +11 -0
- package/dist/data/orm/multi-cursors.js +146 -0
- package/dist/data/orm/multi-cursors.test.d.ts +1 -0
- package/dist/data/orm/multi-cursors.test.js +455 -0
- package/dist/data/orm/sql-db.d.ts +6 -0
- package/dist/data/orm/sql-db.js +2 -0
- package/dist/data/orm/sql.data-source.d.ts +38 -0
- package/dist/data/orm/sql.data-source.js +379 -0
- package/dist/data/orm/sql.data-source.test.d.ts +1 -0
- package/dist/data/orm/sql.data-source.test.js +406 -0
- package/dist/data/orm/subscribable.data-source.d.ts +25 -0
- package/dist/data/orm/subscribable.data-source.js +72 -0
- package/dist/data/orm/table-container-events.test.d.ts +1 -0
- package/dist/data/orm/table-container-events.test.js +93 -0
- package/dist/data/orm/table-container.d.ts +39 -0
- package/dist/data/orm/table-container.js +96 -0
- package/dist/data/orm/table-definitions.system.d.ts +9 -0
- package/dist/data/orm/table-definitions.system.js +29 -0
- package/dist/data/orm/table-definitions.type.d.ts +19 -0
- package/dist/data/orm/table-definitions.type.js +2 -0
- package/dist/data/orm/table-dependencies.d.ts +32 -0
- package/dist/data/orm/table-dependencies.js +2 -0
- package/dist/data/orm/table.d.ts +42 -0
- package/dist/data/orm/table.event-source.test.d.ts +1 -0
- package/dist/data/orm/table.event-source.test.js +341 -0
- package/dist/data/orm/table.js +244 -0
- package/dist/data/orm/types.d.ts +20 -0
- package/dist/data/orm/types.js +115 -0
- package/dist/data/orm/types.test.d.ts +1 -0
- package/dist/data/orm/types.test.js +71 -0
- package/dist/data/package-permissions.d.ts +7 -0
- package/dist/data/package-permissions.js +18 -0
- package/dist/data/packages.d.ts +92 -0
- package/dist/data/packages.js +90 -0
- package/dist/data/peer-events/peer-event-handlers.d.ts +21 -0
- package/dist/data/peer-events/peer-event-handlers.js +28 -0
- package/dist/data/peer-events/peer-event-types.d.ts +119 -0
- package/dist/data/peer-events/peer-event-types.js +29 -0
- package/dist/data/peer-events/peer-events.d.ts +41 -0
- package/dist/data/peer-events/peer-events.js +102 -0
- package/dist/data/persistent-vars.d.ts +87 -0
- package/dist/data/persistent-vars.js +230 -0
- package/dist/data/tool-tests.d.ts +37 -0
- package/dist/data/tool-tests.js +27 -0
- package/dist/data/tools.d.ts +358 -0
- package/dist/data/tools.js +48 -0
- package/dist/data/user-permissions.d.ts +15 -0
- package/dist/data/user-permissions.js +39 -0
- package/dist/data/user-permissions.test.d.ts +1 -0
- package/dist/data/user-permissions.test.js +252 -0
- package/dist/data/users.d.ts +38 -0
- package/dist/data/users.js +73 -0
- package/dist/data/workflow-logs.d.ts +106 -0
- package/dist/data/workflow-logs.js +67 -0
- package/dist/data/workflow-runs.d.ts +103 -0
- package/dist/data/workflow-runs.js +313 -0
- package/dist/data/workflows.d.ts +16 -0
- package/dist/data/workflows.js +21 -0
- package/dist/device/connection.d.ts +41 -0
- package/dist/device/connection.js +249 -0
- package/dist/device/connection.test.d.ts +1 -0
- package/dist/device/connection.test.js +292 -0
- package/dist/device/device-election.d.ts +36 -0
- package/dist/device/device-election.js +137 -0
- package/dist/device/device.d.ts +22 -0
- package/dist/device/device.js +110 -0
- package/dist/device/device.test.d.ts +1 -0
- package/dist/device/device.test.js +203 -0
- package/dist/device/get-trust-level.d.ts +3 -0
- package/dist/device/get-trust-level.js +87 -0
- package/dist/device/socket.type.d.ts +20 -0
- package/dist/device/socket.type.js +15 -0
- package/dist/device/streamed-socket.d.ts +27 -0
- package/dist/device/streamed-socket.js +154 -0
- package/dist/device/streamed-socket.test.d.ts +1 -0
- package/dist/device/streamed-socket.test.js +44 -0
- package/dist/events.d.ts +35 -0
- package/dist/events.js +128 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +50 -0
- package/dist/keys.d.ts +51 -0
- package/dist/keys.js +234 -0
- package/dist/keys.test.d.ts +1 -0
- package/dist/keys.test.js +215 -0
- package/dist/mentions.d.ts +9 -0
- package/dist/mentions.js +46 -0
- package/dist/observable.d.ts +19 -0
- package/dist/observable.js +112 -0
- package/dist/observable.test.d.ts +1 -0
- package/dist/observable.test.js +183 -0
- package/dist/package-loader/get-require.d.ts +10 -0
- package/dist/package-loader/get-require.js +31 -0
- package/dist/package-loader/index.d.ts +1 -0
- package/dist/package-loader/index.js +17 -0
- package/dist/package-loader/package-loader.d.ts +16 -0
- package/dist/package-loader/package-loader.js +102 -0
- package/dist/peers-ui/peers-ui.d.ts +15 -0
- package/dist/peers-ui/peers-ui.js +23 -0
- package/dist/peers-ui/peers-ui.types.d.ts +35 -0
- package/dist/peers-ui/peers-ui.types.js +3 -0
- package/dist/rpc-types.d.ts +45 -0
- package/dist/rpc-types.js +47 -0
- package/dist/serial-json.d.ts +5 -0
- package/dist/serial-json.js +186 -0
- package/dist/serial-json.test.d.ts +1 -0
- package/dist/serial-json.test.js +86 -0
- package/dist/system-ids.d.ts +6 -0
- package/dist/system-ids.js +10 -0
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +17 -0
- package/dist/tools/tools-factory.d.ts +5 -0
- package/dist/tools/tools-factory.js +34 -0
- package/dist/types/app-nav.d.ts +18 -0
- package/dist/types/app-nav.js +10 -0
- package/dist/types/assistant-runner-args.d.ts +9 -0
- package/dist/types/assistant-runner-args.js +2 -0
- package/dist/types/field-type.d.ts +37 -0
- package/dist/types/field-type.js +26 -0
- package/dist/types/peer-device.d.ts +40 -0
- package/dist/types/peer-device.js +14 -0
- package/dist/types/peers-package.d.ts +23 -0
- package/dist/types/peers-package.js +2 -0
- package/dist/types/workflow-logger.d.ts +2 -0
- package/dist/types/workflow-logger.js +2 -0
- package/dist/types/workflow-run-context.d.ts +12 -0
- package/dist/types/workflow-run-context.js +2 -0
- package/dist/types/workflow.d.ts +72 -0
- package/dist/types/workflow.js +24 -0
- package/dist/types/zod-types.d.ts +7 -0
- package/dist/types/zod-types.js +12 -0
- package/dist/users.query.d.ts +13 -0
- package/dist/users.query.js +134 -0
- package/dist/utils.d.ts +39 -0
- package/dist/utils.js +240 -0
- package/dist/utils.test.d.ts +1 -0
- package/dist/utils.test.js +140 -0
- package/package.json +50 -0
package/dist/events.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Emitter = exports.Event = void 0;
|
|
4
|
+
exports.subscribe = subscribe;
|
|
5
|
+
exports.subscribeDebounce = subscribeDebounce;
|
|
6
|
+
exports.emit = emit;
|
|
7
|
+
exports.unionEvents = unionEvents;
|
|
8
|
+
const rpc_types_1 = require("./rpc-types");
|
|
9
|
+
const unitTests = process.env.NODE_ENV === 'test';
|
|
10
|
+
const subscriptions = [];
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
+
function subscribe(nameOrFilter, handler) {
|
|
13
|
+
if (typeof nameOrFilter === "string") {
|
|
14
|
+
const name = nameOrFilter;
|
|
15
|
+
nameOrFilter = (evt) => evt.name === name;
|
|
16
|
+
}
|
|
17
|
+
const filter = nameOrFilter;
|
|
18
|
+
const subscription = {
|
|
19
|
+
filter,
|
|
20
|
+
handler,
|
|
21
|
+
};
|
|
22
|
+
subscriptions.push(subscription);
|
|
23
|
+
return {
|
|
24
|
+
unsubscribe: () => {
|
|
25
|
+
const iSub = subscriptions.indexOf(subscription);
|
|
26
|
+
if (iSub >= 0) {
|
|
27
|
+
subscriptions.splice(iSub, 1);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
function subscribeDebounce(nameOrFilter, handler, debounceMs) {
|
|
36
|
+
let pid = 0;
|
|
37
|
+
const handlerDebounced = (evt) => {
|
|
38
|
+
if (pid) {
|
|
39
|
+
clearTimeout(pid);
|
|
40
|
+
}
|
|
41
|
+
pid = setTimeout(() => {
|
|
42
|
+
pid = 0;
|
|
43
|
+
handler(evt);
|
|
44
|
+
}, debounceMs);
|
|
45
|
+
};
|
|
46
|
+
return subscribe(nameOrFilter, handlerDebounced);
|
|
47
|
+
}
|
|
48
|
+
async function emit(event, dontPropagate) {
|
|
49
|
+
const matchedHandlerPromises = subscriptions
|
|
50
|
+
.filter((subscription) => subscription.filter(event))
|
|
51
|
+
.map(async (subscription) => {
|
|
52
|
+
try {
|
|
53
|
+
return await subscription.handler(event);
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
console.error(`An unhandled error occurred in a handler while processing event: ${JSON.stringify({ event, subscription })}`, err);
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
// if (!(dontPropagate || unitTests)) {
|
|
61
|
+
// matchedHandlerPromises.push(
|
|
62
|
+
// isClient
|
|
63
|
+
// ? rpcServerCalls.emitEvent(event).catch((err) => { console.error(`Error while propagating event to server`, err); return false; })
|
|
64
|
+
// : rpcClientCalls.emitEvent(event).catch((err) => { console.error(`Error while propagating event to client`, err); return false; })
|
|
65
|
+
// );
|
|
66
|
+
// }
|
|
67
|
+
if (!(dontPropagate || unitTests)) {
|
|
68
|
+
matchedHandlerPromises.push(rpc_types_1.rpcClientCalls.emitEvent(event).catch((err) => { console.error(`Error while propagating event to client`, err); return false; }));
|
|
69
|
+
}
|
|
70
|
+
const results = await Promise.all(matchedHandlerPromises);
|
|
71
|
+
// if any handlers returned false (or errored), return false, otherwise return true
|
|
72
|
+
return !results.some((r) => r === false);
|
|
73
|
+
}
|
|
74
|
+
if (rpc_types_1.isClient) {
|
|
75
|
+
rpc_types_1.rpcClientCalls.emitEvent = (event) => emit(event, true);
|
|
76
|
+
}
|
|
77
|
+
class Event {
|
|
78
|
+
_eventName;
|
|
79
|
+
constructor(_eventName) {
|
|
80
|
+
this._eventName = _eventName;
|
|
81
|
+
}
|
|
82
|
+
eventName = () => this._eventName;
|
|
83
|
+
subscribe = (handler, debounceMs) => {
|
|
84
|
+
const rawHandler = (evt) => handler(evt.data);
|
|
85
|
+
if (typeof debounceMs !== "number") {
|
|
86
|
+
return subscribe(this._eventName, rawHandler);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
return subscribeDebounce(this._eventName, rawHandler, debounceMs);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
next = () => new Promise((resolve) => {
|
|
93
|
+
const sub = this.subscribe((evt) => {
|
|
94
|
+
sub.unsubscribe();
|
|
95
|
+
resolve(evt);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
union = (event) => unionEvents(this, event);
|
|
99
|
+
}
|
|
100
|
+
exports.Event = Event;
|
|
101
|
+
class Emitter {
|
|
102
|
+
eventName;
|
|
103
|
+
constructor(eventName) {
|
|
104
|
+
this.eventName = eventName;
|
|
105
|
+
if (eventName.includes("|")) {
|
|
106
|
+
throw new Error(`Do not use pipes in event names, they are reserved for union events`);
|
|
107
|
+
}
|
|
108
|
+
this.event = new Event(eventName);
|
|
109
|
+
}
|
|
110
|
+
event;
|
|
111
|
+
emit(data) {
|
|
112
|
+
return emit({
|
|
113
|
+
name: this.eventName,
|
|
114
|
+
data,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.Emitter = Emitter;
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
120
|
+
function unionEvents(...events) {
|
|
121
|
+
const eventName = events.map((s) => s.eventName()).join("|");
|
|
122
|
+
return {
|
|
123
|
+
eventName: () => eventName,
|
|
124
|
+
next: () => Promise.race(events.map((s) => s.next())),
|
|
125
|
+
subscribe: (handler) => subscribe((evt) => events.flatMap((s) => s.eventName().split("|")).includes(evt.name), (evt) => handler(evt)),
|
|
126
|
+
union: unionEvents,
|
|
127
|
+
};
|
|
128
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export * from "./context";
|
|
2
|
+
export * from "./peers-ui/peers-ui";
|
|
3
|
+
export * from "./peers-ui/peers-ui.types";
|
|
4
|
+
export * from "./users.query";
|
|
5
|
+
export * from "./system-ids";
|
|
6
|
+
export * from "./types/app-nav";
|
|
7
|
+
export * from "./types/assistant-runner-args";
|
|
8
|
+
export * from "./types/field-type";
|
|
9
|
+
export * from "./types/peer-device";
|
|
10
|
+
export * from "./types/peers-package";
|
|
11
|
+
export * from "./types/workflow";
|
|
12
|
+
export * from "./types/workflow-logger";
|
|
13
|
+
export * from "./types/workflow-run-context";
|
|
14
|
+
export * from "./types/zod-types";
|
|
15
|
+
export * from "./device/connection";
|
|
16
|
+
export * from "./device/device";
|
|
17
|
+
export * from "./device/device-election";
|
|
18
|
+
export * from "./device/get-trust-level";
|
|
19
|
+
export * from "./device/socket.type";
|
|
20
|
+
export * from "./package-loader";
|
|
21
|
+
export * from "./events";
|
|
22
|
+
export * from "./data";
|
|
23
|
+
export * as data from "./data";
|
|
24
|
+
export * from "./data/orm";
|
|
25
|
+
export * as orm from "./data/orm";
|
|
26
|
+
export * from "./tools";
|
|
27
|
+
export * as tools from "./tools";
|
|
28
|
+
export * from "./keys";
|
|
29
|
+
export * from "./mentions";
|
|
30
|
+
export * from "./observable";
|
|
31
|
+
export * from "./rpc-types";
|
|
32
|
+
export * from "./serial-json";
|
|
33
|
+
export * from "./utils";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.tools = exports.orm = exports.data = void 0;
|
|
18
|
+
__exportStar(require("./context"), exports);
|
|
19
|
+
__exportStar(require("./peers-ui/peers-ui"), exports);
|
|
20
|
+
__exportStar(require("./peers-ui/peers-ui.types"), exports);
|
|
21
|
+
__exportStar(require("./users.query"), exports);
|
|
22
|
+
__exportStar(require("./system-ids"), exports);
|
|
23
|
+
__exportStar(require("./types/app-nav"), exports);
|
|
24
|
+
__exportStar(require("./types/assistant-runner-args"), exports);
|
|
25
|
+
__exportStar(require("./types/field-type"), exports);
|
|
26
|
+
__exportStar(require("./types/peer-device"), exports);
|
|
27
|
+
__exportStar(require("./types/peers-package"), exports);
|
|
28
|
+
__exportStar(require("./types/workflow"), exports);
|
|
29
|
+
__exportStar(require("./types/workflow-logger"), exports);
|
|
30
|
+
__exportStar(require("./types/workflow-run-context"), exports);
|
|
31
|
+
__exportStar(require("./types/zod-types"), exports);
|
|
32
|
+
__exportStar(require("./device/connection"), exports);
|
|
33
|
+
__exportStar(require("./device/device"), exports);
|
|
34
|
+
__exportStar(require("./device/device-election"), exports);
|
|
35
|
+
__exportStar(require("./device/get-trust-level"), exports);
|
|
36
|
+
__exportStar(require("./device/socket.type"), exports);
|
|
37
|
+
__exportStar(require("./package-loader"), exports);
|
|
38
|
+
__exportStar(require("./events"), exports);
|
|
39
|
+
__exportStar(require("./data"), exports);
|
|
40
|
+
exports.data = require("./data");
|
|
41
|
+
__exportStar(require("./data/orm"), exports);
|
|
42
|
+
exports.orm = require("./data/orm");
|
|
43
|
+
__exportStar(require("./tools"), exports);
|
|
44
|
+
exports.tools = require("./tools");
|
|
45
|
+
__exportStar(require("./keys"), exports);
|
|
46
|
+
__exportStar(require("./mentions"), exports);
|
|
47
|
+
__exportStar(require("./observable"), exports);
|
|
48
|
+
__exportStar(require("./rpc-types"), exports);
|
|
49
|
+
__exportStar(require("./serial-json"), exports);
|
|
50
|
+
__exportStar(require("./utils"), exports);
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export declare function setPRNG(fn: (x: Uint8Array, n: number) => void): void;
|
|
2
|
+
export declare function hashValue(str: string): string;
|
|
3
|
+
export declare function hashBytes(buffer: Uint8Array): string;
|
|
4
|
+
export declare function stableStringify(obj: any): string;
|
|
5
|
+
export declare function hashObject(obj: ({
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
})): string;
|
|
8
|
+
export declare function encodeBase64(data: Uint8Array): string;
|
|
9
|
+
export declare function decodeBase64(data: string): Uint8Array;
|
|
10
|
+
export declare function newToken(size?: number): string;
|
|
11
|
+
export interface IPublicKeys {
|
|
12
|
+
publicKey: string;
|
|
13
|
+
publicBoxKey: string;
|
|
14
|
+
}
|
|
15
|
+
export interface IPublicPrivateKeys extends IPublicKeys {
|
|
16
|
+
secretKey: string;
|
|
17
|
+
}
|
|
18
|
+
export interface ISignedObject<T> {
|
|
19
|
+
contents: T;
|
|
20
|
+
signature: string;
|
|
21
|
+
publicKey: string;
|
|
22
|
+
}
|
|
23
|
+
export interface IDataBox {
|
|
24
|
+
contents: string;
|
|
25
|
+
nonce: string;
|
|
26
|
+
fromPublicKey: string;
|
|
27
|
+
}
|
|
28
|
+
export declare function newKeys(): IPublicPrivateKeys;
|
|
29
|
+
export declare function loadKeys(secretKey: string): IPublicPrivateKeys;
|
|
30
|
+
export declare function signMessageWithSecretKey(msg: string, secretKey: string): string;
|
|
31
|
+
export declare function openMessageWithPublicKey(msg: string, publicKey: string): string;
|
|
32
|
+
export declare function signObjectWithSecretKey<T>(obj: T, secretKey: string): ISignedObject<T>;
|
|
33
|
+
export declare function openSignedObject<T>(signedObj: ISignedObject<T>): T;
|
|
34
|
+
export declare function boxDataWithKeys(data: any, toPublicBoxKey: string, mySecretKey: string): IDataBox;
|
|
35
|
+
export declare function openBoxWithSecretKey(box: IDataBox, mySecretKey: string): any;
|
|
36
|
+
export declare function encryptData<T>(data: T, secretKey: string): string;
|
|
37
|
+
export declare function decryptData<T>(encryptedData: string, secretKey: string): (T | null);
|
|
38
|
+
export type IObjectWithSignature<T> = T & {
|
|
39
|
+
signature: string;
|
|
40
|
+
};
|
|
41
|
+
type IMaybeSignature = {
|
|
42
|
+
signature?: string;
|
|
43
|
+
};
|
|
44
|
+
export declare function addSignatureToObject<T extends Record<string, any>>(obj: T, secretKey: string): IObjectWithSignature<T>;
|
|
45
|
+
/**
|
|
46
|
+
* Will throw if the signature is invalid
|
|
47
|
+
*/
|
|
48
|
+
export declare function verifyObjectSignature<T extends IMaybeSignature>(objectWithSignature: T): void;
|
|
49
|
+
export declare function isObjectSignatureValid<T extends IMaybeSignature>(objectWithSignature: T): boolean;
|
|
50
|
+
export declare function getPublicKeyFromObjectSignature<T extends IMaybeSignature>(objectWithSignature: T): string | undefined;
|
|
51
|
+
export {};
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setPRNG = setPRNG;
|
|
4
|
+
exports.hashValue = hashValue;
|
|
5
|
+
exports.hashBytes = hashBytes;
|
|
6
|
+
exports.stableStringify = stableStringify;
|
|
7
|
+
exports.hashObject = hashObject;
|
|
8
|
+
exports.encodeBase64 = encodeBase64;
|
|
9
|
+
exports.decodeBase64 = decodeBase64;
|
|
10
|
+
exports.newToken = newToken;
|
|
11
|
+
exports.newKeys = newKeys;
|
|
12
|
+
exports.loadKeys = loadKeys;
|
|
13
|
+
exports.signMessageWithSecretKey = signMessageWithSecretKey;
|
|
14
|
+
exports.openMessageWithPublicKey = openMessageWithPublicKey;
|
|
15
|
+
exports.signObjectWithSecretKey = signObjectWithSecretKey;
|
|
16
|
+
exports.openSignedObject = openSignedObject;
|
|
17
|
+
exports.boxDataWithKeys = boxDataWithKeys;
|
|
18
|
+
exports.openBoxWithSecretKey = openBoxWithSecretKey;
|
|
19
|
+
exports.encryptData = encryptData;
|
|
20
|
+
exports.decryptData = decryptData;
|
|
21
|
+
exports.addSignatureToObject = addSignatureToObject;
|
|
22
|
+
exports.verifyObjectSignature = verifyObjectSignature;
|
|
23
|
+
exports.isObjectSignatureValid = isObjectSignatureValid;
|
|
24
|
+
exports.getPublicKeyFromObjectSignature = getPublicKeyFromObjectSignature;
|
|
25
|
+
const nacl = require("tweetnacl");
|
|
26
|
+
const utils = require("tweetnacl-util");
|
|
27
|
+
const tweetnacl_util_1 = require("tweetnacl-util");
|
|
28
|
+
const serial_json_1 = require("./serial-json");
|
|
29
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
30
|
+
const buffer_1 = require("buffer");
|
|
31
|
+
globalThis.Buffer = buffer_1.Buffer; // shim for browsers/RN
|
|
32
|
+
const _stableStringify = require('fast-json-stable-stringify');
|
|
33
|
+
function setPRNG(fn) {
|
|
34
|
+
nacl.setPRNG(fn);
|
|
35
|
+
}
|
|
36
|
+
function hashValue(str) {
|
|
37
|
+
// const full = nacl.hash(decodeUTF8(str));
|
|
38
|
+
// const half = full.slice(0, 32);
|
|
39
|
+
// return encodeBase64(half);
|
|
40
|
+
const hash = (0, sha2_1.sha256)(new TextEncoder().encode(str));
|
|
41
|
+
return encodeBase64(hash);
|
|
42
|
+
}
|
|
43
|
+
;
|
|
44
|
+
function hashBytes(buffer) {
|
|
45
|
+
const hash = (0, sha2_1.sha256)(buffer);
|
|
46
|
+
return encodeBase64(hash);
|
|
47
|
+
}
|
|
48
|
+
;
|
|
49
|
+
function stableStringify(obj) {
|
|
50
|
+
return _stableStringify(obj);
|
|
51
|
+
}
|
|
52
|
+
function hashObject(obj) {
|
|
53
|
+
const stableJson = stableStringify(obj);
|
|
54
|
+
const hash = hashValue(stableJson);
|
|
55
|
+
return hash;
|
|
56
|
+
}
|
|
57
|
+
function encodeBase64(data) {
|
|
58
|
+
// return Buffer.from(data).toString("base64url"); // no padding by default
|
|
59
|
+
const str = utils.encodeBase64(data);
|
|
60
|
+
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
61
|
+
}
|
|
62
|
+
function decodeBase64(data) {
|
|
63
|
+
data = data.replace(/-/g, '+').replace(/_/g, '/');
|
|
64
|
+
while (data.length % 4 !== 0) {
|
|
65
|
+
data += '=';
|
|
66
|
+
}
|
|
67
|
+
return utils.decodeBase64(data);
|
|
68
|
+
}
|
|
69
|
+
function newToken(size = 32) {
|
|
70
|
+
return encodeBase64(nacl.randomBytes(size));
|
|
71
|
+
}
|
|
72
|
+
function newKeys() {
|
|
73
|
+
const keyPair = nacl.sign.keyPair();
|
|
74
|
+
// the first 32 bytes of the secret key are the secret part. The second 32 bytes are the public part.
|
|
75
|
+
const secretKeyPart = keyPair.secretKey.slice(0, 32);
|
|
76
|
+
const boxKeyPair = nacl.box.keyPair.fromSecretKey(secretKeyPart);
|
|
77
|
+
return {
|
|
78
|
+
secretKey: encodeBase64(keyPair.secretKey),
|
|
79
|
+
publicKey: encodeBase64(keyPair.publicKey),
|
|
80
|
+
publicBoxKey: encodeBase64(boxKeyPair.publicKey),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function loadKeys(secretKey) {
|
|
84
|
+
let _secretKey = decodeBase64(secretKey);
|
|
85
|
+
const boxKeyPair = nacl.box.keyPair.fromSecretKey(_secretKey.slice(0, 32));
|
|
86
|
+
return {
|
|
87
|
+
secretKey: encodeBase64(_secretKey),
|
|
88
|
+
publicKey: encodeBase64(_secretKey.slice(32)),
|
|
89
|
+
publicBoxKey: encodeBase64(boxKeyPair.publicKey),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function signMessageWithSecretKey(msg, secretKey) {
|
|
93
|
+
let _secretKey = decodeBase64(secretKey);
|
|
94
|
+
const msgDecoded = (0, tweetnacl_util_1.decodeUTF8)(msg);
|
|
95
|
+
const msgSigned = nacl.sign(msgDecoded, _secretKey);
|
|
96
|
+
return encodeBase64(msgSigned);
|
|
97
|
+
}
|
|
98
|
+
function openMessageWithPublicKey(msg, publicKey) {
|
|
99
|
+
let _publicKey = decodeBase64(publicKey);
|
|
100
|
+
const msgDecoded = decodeBase64(msg);
|
|
101
|
+
const msgOpened = nacl.sign.open(msgDecoded, _publicKey);
|
|
102
|
+
if (!msgOpened) {
|
|
103
|
+
throw new Error('Failed to open message');
|
|
104
|
+
}
|
|
105
|
+
return (0, tweetnacl_util_1.encodeUTF8)(msgOpened);
|
|
106
|
+
}
|
|
107
|
+
function signObjectWithSecretKey(obj, secretKey) {
|
|
108
|
+
let _secretKey = decodeBase64(secretKey);
|
|
109
|
+
if (obj === undefined) {
|
|
110
|
+
obj = { __isUndefined: true };
|
|
111
|
+
}
|
|
112
|
+
if (obj === null) {
|
|
113
|
+
obj = { __isNull: true };
|
|
114
|
+
}
|
|
115
|
+
const objStr = (0, serial_json_1.toJSONString)(obj);
|
|
116
|
+
const objDecoded = (0, tweetnacl_util_1.decodeUTF8)(objStr);
|
|
117
|
+
const objSigned = nacl.sign.detached(objDecoded, _secretKey);
|
|
118
|
+
return {
|
|
119
|
+
contents: obj,
|
|
120
|
+
signature: encodeBase64(objSigned),
|
|
121
|
+
publicKey: encodeBase64(_secretKey.slice(32)),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function openSignedObject(signedObj) {
|
|
125
|
+
let _publicKey = decodeBase64(signedObj.publicKey);
|
|
126
|
+
const objStr = (0, serial_json_1.toJSONString)(signedObj.contents);
|
|
127
|
+
const objDecoded = (0, tweetnacl_util_1.decodeUTF8)(objStr);
|
|
128
|
+
const signature = decodeBase64(signedObj.signature);
|
|
129
|
+
const valid = nacl.sign.detached.verify(objDecoded, signature, _publicKey);
|
|
130
|
+
if (!valid) {
|
|
131
|
+
throw new Error('Signature verification failed');
|
|
132
|
+
}
|
|
133
|
+
const result = signedObj.contents;
|
|
134
|
+
if (result?.__isUndefined && Object.keys(result).length === 1) {
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
if (result?.__isNull && Object.keys(result).length === 1) {
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
return signedObj.contents;
|
|
143
|
+
}
|
|
144
|
+
function boxDataWithKeys(data, toPublicBoxKey, mySecretKey) {
|
|
145
|
+
let _secretKey = decodeBase64(mySecretKey);
|
|
146
|
+
const boxKeyPair = nacl.box.keyPair.fromSecretKey(_secretKey.slice(0, 32));
|
|
147
|
+
const _toPublicBoxKey = decodeBase64(toPublicBoxKey);
|
|
148
|
+
const nonce = nacl.randomBytes(24);
|
|
149
|
+
data = (0, serial_json_1.toJSONString)(data);
|
|
150
|
+
const dataDecoded = (0, tweetnacl_util_1.decodeUTF8)(data);
|
|
151
|
+
const dataBoxed = nacl.box(dataDecoded, nonce, _toPublicBoxKey, boxKeyPair.secretKey);
|
|
152
|
+
return {
|
|
153
|
+
contents: encodeBase64(dataBoxed),
|
|
154
|
+
nonce: encodeBase64(nonce),
|
|
155
|
+
fromPublicKey: encodeBase64(boxKeyPair.publicKey),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function openBoxWithSecretKey(box, mySecretKey) {
|
|
159
|
+
let _secretKey = decodeBase64(mySecretKey);
|
|
160
|
+
const boxKeyPair = nacl.box.keyPair.fromSecretKey(_secretKey.slice(0, 32));
|
|
161
|
+
const boxedData = decodeBase64(box.contents);
|
|
162
|
+
const nonce = decodeBase64(box.nonce);
|
|
163
|
+
const _fromPublicBoxKey = decodeBase64(box.fromPublicKey);
|
|
164
|
+
const dataAry = nacl.box.open(boxedData, nonce, _fromPublicBoxKey, boxKeyPair.secretKey);
|
|
165
|
+
if (dataAry === null) {
|
|
166
|
+
throw new Error('Message was null or verification failed');
|
|
167
|
+
}
|
|
168
|
+
const dataStr = (0, tweetnacl_util_1.encodeUTF8)(dataAry);
|
|
169
|
+
return (0, serial_json_1.fromJSONString)(dataStr);
|
|
170
|
+
}
|
|
171
|
+
function encryptString(data, secretKey) {
|
|
172
|
+
let _secretKey = decodeBase64(secretKey);
|
|
173
|
+
const _data = (0, tweetnacl_util_1.decodeUTF8)(data);
|
|
174
|
+
const nonce = nacl.randomBytes(24);
|
|
175
|
+
const encryptedData = nacl.secretbox(_data, nonce, _secretKey.slice(0, 32));
|
|
176
|
+
return `${encodeBase64(encryptedData)}~${encodeBase64(nonce)}`;
|
|
177
|
+
}
|
|
178
|
+
function decryptString(data, secretKey) {
|
|
179
|
+
let _secretKey = decodeBase64(secretKey);
|
|
180
|
+
const [encryptedData, nonce] = data.split('~');
|
|
181
|
+
const _data = decodeBase64(encryptedData);
|
|
182
|
+
const _nonce = decodeBase64(nonce);
|
|
183
|
+
const decryptedData = nacl.secretbox.open(_data, _nonce, _secretKey.slice(0, 32));
|
|
184
|
+
if (decryptedData === null) {
|
|
185
|
+
throw new Error('Failed to decrypt data');
|
|
186
|
+
}
|
|
187
|
+
return (0, tweetnacl_util_1.encodeUTF8)(decryptedData);
|
|
188
|
+
}
|
|
189
|
+
function encryptData(data, secretKey) {
|
|
190
|
+
const _data = (0, serial_json_1.toJSONString)(data);
|
|
191
|
+
return encryptString(_data, secretKey);
|
|
192
|
+
}
|
|
193
|
+
function decryptData(encryptedData, secretKey) {
|
|
194
|
+
const _data = decryptString(encryptedData, secretKey);
|
|
195
|
+
return (0, serial_json_1.fromJSONString)(_data);
|
|
196
|
+
}
|
|
197
|
+
function addSignatureToObject(obj, secretKey) {
|
|
198
|
+
obj = { ...obj };
|
|
199
|
+
delete obj.signature;
|
|
200
|
+
const signedObj = signObjectWithSecretKey(obj, secretKey);
|
|
201
|
+
const objSignature = `${signedObj.publicKey}:${signedObj.signature}`;
|
|
202
|
+
return { ...obj, signature: objSignature };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Will throw if the signature is invalid
|
|
206
|
+
*/
|
|
207
|
+
function verifyObjectSignature(objectWithSignature) {
|
|
208
|
+
const objWithoutSignature = { ...objectWithSignature };
|
|
209
|
+
delete objWithoutSignature.signature;
|
|
210
|
+
const [objPublicKey, objSignature] = objectWithSignature.signature?.split(':') || [];
|
|
211
|
+
const signedObject = {
|
|
212
|
+
contents: objWithoutSignature,
|
|
213
|
+
signature: objSignature,
|
|
214
|
+
publicKey: objPublicKey
|
|
215
|
+
};
|
|
216
|
+
openSignedObject(signedObject); // will throw if invalid
|
|
217
|
+
}
|
|
218
|
+
function isObjectSignatureValid(objectWithSignature) {
|
|
219
|
+
try {
|
|
220
|
+
verifyObjectSignature(objectWithSignature);
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
function getPublicKeyFromObjectSignature(objectWithSignature) {
|
|
228
|
+
const parts = objectWithSignature.signature?.split(':');
|
|
229
|
+
if (!parts || parts.length !== 2) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
const [publicKey] = parts;
|
|
233
|
+
return publicKey || undefined;
|
|
234
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|