@btps/sdk 0.0.0-stage → 1.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/CHANGELOG.md +171 -0
- package/LICENSE +190 -0
- package/NOTICE +25 -0
- package/README.md +261 -2
- package/dist/client/btpsAgent.d.ts +20 -0
- package/dist/client/btpsAgent.js +172 -0
- package/dist/client/btpsClient.d.ts +202 -0
- package/dist/client/btpsClient.js +602 -0
- package/dist/client/btpsTransporter.d.ts +52 -0
- package/dist/client/btpsTransporter.js +249 -0
- package/dist/client/constants/index.d.ts +11 -0
- package/dist/client/constants/index.js +20 -0
- package/dist/client/index.d.ts +12 -0
- package/dist/client/index.js +27 -0
- package/dist/client/libs/schema.js +56 -0
- package/dist/client/libs/timeouts.d.ts +22 -0
- package/dist/client/libs/timeouts.js +19 -0
- package/dist/client/types/index.d.ts +155 -0
- package/dist/client/types/index.js +7 -0
- package/dist/core/authentication/BtpsAuthentication.d.ts +128 -0
- package/dist/core/authentication/BtpsAuthentication.js +293 -0
- package/dist/core/authentication/index.d.ts +11 -0
- package/dist/core/authentication/index.js +12 -0
- package/dist/core/authentication/keyProof.d.ts +18 -0
- package/dist/core/authentication/keyProof.js +16 -0
- package/dist/core/authentication/storage/InMemoryAuthSessionStore.d.ts +55 -0
- package/dist/core/authentication/storage/InMemoryAuthSessionStore.js +62 -0
- package/dist/core/authentication/storage/InMemoryTokenStore.d.ts +63 -0
- package/dist/core/authentication/storage/InMemoryTokenStore.js +63 -0
- package/dist/core/authentication/types.d.ts +135 -0
- package/dist/core/authentication/types.js +7 -0
- package/dist/core/crypto/decryptVerify.d.ts +9 -0
- package/dist/core/crypto/decryptVerify.js +45 -0
- package/dist/core/crypto/decrypter-interface.d.ts +25 -0
- package/dist/core/crypto/decrypter-interface.js +7 -0
- package/dist/core/crypto/index.d.ts +77 -0
- package/dist/core/crypto/index.js +210 -0
- package/dist/core/crypto/jcs.d.ts +28 -0
- package/dist/core/crypto/jcs.js +60 -0
- package/dist/core/crypto/keygen.d.ts +9 -0
- package/dist/core/crypto/keygen.js +35 -0
- package/dist/core/crypto/pem-decrypter.d.ts +26 -0
- package/dist/core/crypto/pem-decrypter.js +52 -0
- package/dist/core/crypto/pem-signer.d.ts +21 -0
- package/dist/core/crypto/pem-signer.js +34 -0
- package/dist/core/crypto/signEncrypt.d.ts +34 -0
- package/dist/core/crypto/signEncrypt.js +75 -0
- package/dist/core/crypto/signer-interface.d.ts +22 -0
- package/dist/core/crypto/signer-interface.js +7 -0
- package/dist/core/crypto/types.d.ts +79 -0
- package/dist/core/crypto/types.js +7 -0
- package/dist/core/delegation/BtpsDelegator.d.ts +102 -0
- package/dist/core/delegation/BtpsDelegator.js +118 -0
- package/dist/core/delegation/index.d.ts +7 -0
- package/dist/core/delegation/index.js +8 -0
- package/dist/core/error/constant.d.ts +66 -0
- package/dist/core/error/constant.js +194 -0
- package/dist/core/error/index.d.ts +35 -0
- package/dist/core/error/index.js +52 -0
- package/dist/core/error/types.d.ts +16 -0
- package/dist/core/error/types.js +7 -0
- package/dist/core/server/constants/btps-protocol.d.ts +17 -0
- package/dist/core/server/constants/btps-protocol.js +25 -0
- package/dist/core/server/constants/control.d.ts +7 -0
- package/dist/core/server/constants/control.js +11 -0
- package/dist/core/server/constants/currency.d.ts +7 -0
- package/dist/core/server/constants/currency.js +332 -0
- package/dist/core/server/constants/index.d.ts +26 -0
- package/dist/core/server/constants/index.js +61 -0
- package/dist/core/server/constants/limits.d.ts +50 -0
- package/dist/core/server/constants/limits.js +50 -0
- package/dist/core/server/helpers/BtpsMessageQueue.d.ts +13 -0
- package/dist/core/server/helpers/BtpsMessageQueue.js +12 -0
- package/dist/core/server/helpers/InMemoryQueue.d.ts +16 -0
- package/dist/core/server/helpers/InMemoryQueue.js +35 -0
- package/dist/core/server/helpers/index.d.ts +8 -0
- package/dist/core/server/helpers/index.js +9 -0
- package/dist/core/server/schemas/artifacts/agentSchema.js +51 -0
- package/dist/core/server/schemas/artifacts/artifacts.js +21 -0
- package/dist/core/server/schemas/artifacts/controlSchema.js +18 -0
- package/dist/core/server/schemas/artifacts/identityLookupSchema.js +21 -0
- package/dist/core/server/schemas/artifacts/transporterSchema.js +60 -0
- package/dist/core/server/schemas/btpsDocsSchema.d.ts +4519 -0
- package/dist/core/server/schemas/btpsDocsSchema.js +272 -0
- package/dist/core/server/schemas/helpers.js +87 -0
- package/dist/core/server/schemas/responseSchema.js +86 -0
- package/dist/core/server/schemas/schema.js +70 -0
- package/dist/core/server/schemas/shared.js +18 -0
- package/dist/core/server/types.d.ts +172 -0
- package/dist/core/server/types.js +7 -0
- package/dist/core/storage/AbstractIdentityStore.d.ts +17 -0
- package/dist/core/storage/AbstractIdentityStore.js +13 -0
- package/dist/core/storage/AbstractStorageStore.d.ts +16 -0
- package/dist/core/storage/AbstractStorageStore.js +17 -0
- package/dist/core/storage/JsonIdentityStore.d.ts +29 -0
- package/dist/core/storage/JsonIdentityStore.js +39 -0
- package/dist/core/storage/JsonStorageStore.d.ts +59 -0
- package/dist/core/storage/JsonStorageStore.js +110 -0
- package/dist/core/storage/index.d.ts +11 -0
- package/dist/core/storage/index.js +18 -0
- package/dist/core/storage/types.d.ts +28 -0
- package/dist/core/storage/types.js +7 -0
- package/dist/core/trust/index.d.ts +10 -0
- package/dist/core/trust/index.js +15 -0
- package/dist/core/trust/storage/AbstractTrustStore.d.ts +23 -0
- package/dist/core/trust/storage/AbstractTrustStore.js +13 -0
- package/dist/core/trust/storage/JsonTrustStore.d.ts +33 -0
- package/dist/core/trust/storage/JsonTrustStore.js +42 -0
- package/dist/core/trust/trustUtils.d.ts +78 -0
- package/dist/core/trust/trustUtils.js +41 -0
- package/dist/core/trust/types.d.ts +53 -0
- package/dist/core/trust/types.js +7 -0
- package/dist/core/utils/identity.d.ts +20 -0
- package/dist/core/utils/identity.js +22 -0
- package/dist/core/utils/index.d.ts +70 -0
- package/dist/core/utils/index.js +124 -0
- package/dist/core/utils/types.d.ts +10 -0
- package/dist/core/utils/types.js +7 -0
- package/dist/core/utils/validation.js +13 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/server/btpsServer.d.ts +292 -0
- package/dist/server/btpsServer.js +827 -0
- package/dist/server/index.d.ts +11 -0
- package/dist/server/index.js +15 -0
- package/dist/server/libs/abstractRateLimiter.d.ts +10 -0
- package/dist/server/libs/abstractRateLimiter.js +15 -0
- package/dist/server/libs/artifactSizeGuard.d.ts +27 -0
- package/dist/server/libs/artifactSizeGuard.js +44 -0
- package/dist/server/libs/artifactTimeWindow.d.ts +24 -0
- package/dist/server/libs/artifactTimeWindow.js +30 -0
- package/dist/server/libs/btpsFactory.d.ts +29 -0
- package/dist/server/libs/btpsFactory.js +49 -0
- package/dist/server/libs/btpsLightweightFactory.d.ts +35 -0
- package/dist/server/libs/btpsLightweightFactory.js +37 -0
- package/dist/server/libs/btpsRegistry.d.ts +14 -0
- package/dist/server/libs/btpsRegistry.js +16 -0
- package/dist/server/libs/btpsSimpleMetricsTracker.d.ts +13 -0
- package/dist/server/libs/btpsSimpleMetricsTracker.js +21 -0
- package/dist/server/libs/btpsSimpleRateLimiter.d.ts +28 -0
- package/dist/server/libs/btpsSimpleRateLimiter.js +46 -0
- package/dist/server/libs/connectionTimeout.d.ts +16 -0
- package/dist/server/libs/connectionTimeout.js +14 -0
- package/dist/server/libs/defaultMiddleware.d.ts +8 -0
- package/dist/server/libs/defaultMiddleware.js +128 -0
- package/dist/server/libs/inFlightLimit.d.ts +18 -0
- package/dist/server/libs/inFlightLimit.js +17 -0
- package/dist/server/libs/index.d.ts +18 -0
- package/dist/server/libs/index.js +34 -0
- package/dist/server/libs/middlewareManager.js +126 -0
- package/dist/server/libs/type.d.ts +22 -0
- package/dist/server/libs/type.js +7 -0
- package/dist/server/types.d.ts +236 -0
- package/dist/server/types.js +7 -0
- package/package.json +126 -4
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { BTP_ERROR_IDENTITY, BTPErrorException } from "../core/error/index.js";
|
|
9
|
+
import { BtpsClient } from "./btpsClient.js";
|
|
10
|
+
import { isValidIdentity } from "../core/utils/index.js";
|
|
11
|
+
import { BTP_PROTOCOL_VERSION } from "../core/server/constants/index.js";
|
|
12
|
+
import {
|
|
13
|
+
BTP_TRANSPORTER_DEFAULT_CONNECTION_TTL_SECONDS,
|
|
14
|
+
BTP_TRANSPORTER_DEFAULT_MAX_CONNECTIONS,
|
|
15
|
+
BTP_TRANSPORTER_ERROR_CONNECTION_ALREADY_EXISTS,
|
|
16
|
+
BTP_TRANSPORTER_ERROR_MAX_CONNECTIONS_REACHED
|
|
17
|
+
} from "./constants/index.js";
|
|
18
|
+
import { BtpTransporterArtifactSchema } from "../core/server/schemas/artifacts/transporterSchema.js";
|
|
19
|
+
import { validate } from "../core/utils/validation.js";
|
|
20
|
+
import { EventEmitter } from "events";
|
|
21
|
+
class BtpsTransporter {
|
|
22
|
+
connections = new Map();
|
|
23
|
+
connectionLock = new Map();
|
|
24
|
+
clientOptions;
|
|
25
|
+
maxConnections;
|
|
26
|
+
connectionTTLSeconds;
|
|
27
|
+
emitter = new EventEmitter();
|
|
28
|
+
constructor(options) {
|
|
29
|
+
const { maxConnections, connectionTTLSeconds, ...clientOptions } = options;
|
|
30
|
+
this.maxConnections = maxConnections ?? BTP_TRANSPORTER_DEFAULT_MAX_CONNECTIONS, this.connectionTTLSeconds = connectionTTLSeconds ?? BTP_TRANSPORTER_DEFAULT_CONNECTION_TTL_SECONDS, this.clientOptions = {
|
|
31
|
+
...clientOptions
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
createClient(to, clientOptions) {
|
|
35
|
+
try {
|
|
36
|
+
return {
|
|
37
|
+
client: new BtpsClient({
|
|
38
|
+
to,
|
|
39
|
+
...clientOptions ?? this.clientOptions
|
|
40
|
+
}),
|
|
41
|
+
error: void 0
|
|
42
|
+
};
|
|
43
|
+
} catch (error) {
|
|
44
|
+
return { client: void 0, error };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
attachListeners(connection) {
|
|
48
|
+
connection.listeners = {
|
|
49
|
+
connected: () => {
|
|
50
|
+
connection.isActive = !0, this.emitter.emit("connectionConnected", connection.id);
|
|
51
|
+
},
|
|
52
|
+
end: (endInfo) => {
|
|
53
|
+
connection.isActive = !1, this.emitter.emit("connectionEnd", connection.id, endInfo);
|
|
54
|
+
},
|
|
55
|
+
error: (errorInfo) => {
|
|
56
|
+
connection.isActive = !1, this.emitter.emit("connectionError", connection.id, errorInfo);
|
|
57
|
+
},
|
|
58
|
+
message: (msg) => {
|
|
59
|
+
connection.isActive = !0, this.emitter.emit("connectionMessage", connection.id, msg);
|
|
60
|
+
},
|
|
61
|
+
close: (closeInfo) => {
|
|
62
|
+
connection.isActive = !1, this.emitter.emit("connectionClose", connection.id, closeInfo);
|
|
63
|
+
}
|
|
64
|
+
}, Object.keys(connection.listeners).forEach((event) => {
|
|
65
|
+
connection.client.on(event, connection.listeners[event]);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
detachListeners(connection) {
|
|
69
|
+
connection.listeners && (Object.keys(connection.listeners).forEach((event) => {
|
|
70
|
+
connection.client.off(event, connection.listeners[event]);
|
|
71
|
+
}), delete connection.listeners);
|
|
72
|
+
}
|
|
73
|
+
async performConnectionRegistration(to, clientOptions, override = !1) {
|
|
74
|
+
if (!isValidIdentity(to))
|
|
75
|
+
return {
|
|
76
|
+
connection: void 0,
|
|
77
|
+
error: new BTPErrorException(BTP_ERROR_IDENTITY, {
|
|
78
|
+
cause: "Invalid identity",
|
|
79
|
+
meta: { to }
|
|
80
|
+
})
|
|
81
|
+
};
|
|
82
|
+
if (this.connections.get(to))
|
|
83
|
+
if (override)
|
|
84
|
+
this.deregisterConnection(to);
|
|
85
|
+
else
|
|
86
|
+
return {
|
|
87
|
+
connection: void 0,
|
|
88
|
+
error: new BTPErrorException(BTP_TRANSPORTER_ERROR_CONNECTION_ALREADY_EXISTS, {
|
|
89
|
+
cause: "Connection already exists",
|
|
90
|
+
meta: { to }
|
|
91
|
+
})
|
|
92
|
+
};
|
|
93
|
+
if (this.connections.size >= this.maxConnections) {
|
|
94
|
+
const idlest = [...this.connections.values()].filter((c) => c.inFlight === 0).sort((a, b) => a.lastUsedAt.localeCompare(b.lastUsedAt))[0];
|
|
95
|
+
idlest && this.deregisterConnection(idlest.id);
|
|
96
|
+
}
|
|
97
|
+
if (this.connections.size >= this.maxConnections)
|
|
98
|
+
return {
|
|
99
|
+
connection: void 0,
|
|
100
|
+
error: new BTPErrorException(BTP_TRANSPORTER_ERROR_MAX_CONNECTIONS_REACHED, {
|
|
101
|
+
cause: "Max connections reached",
|
|
102
|
+
meta: { maxConnections: this.maxConnections }
|
|
103
|
+
})
|
|
104
|
+
};
|
|
105
|
+
const dateNow = ( new Date()).toISOString(), { client, error } = this.createClient(to, clientOptions);
|
|
106
|
+
if (error)
|
|
107
|
+
return { connection: void 0, error };
|
|
108
|
+
const newConnection = {
|
|
109
|
+
id: to,
|
|
110
|
+
client,
|
|
111
|
+
createdAt: dateNow,
|
|
112
|
+
updatedAt: dateNow,
|
|
113
|
+
lastUsedAt: dateNow,
|
|
114
|
+
isActive: !1,
|
|
115
|
+
clientOptions: clientOptions ?? { to, ...this.clientOptions },
|
|
116
|
+
getStatus: () => client.getConnectionStates(),
|
|
117
|
+
timeout: this.createConnectionTimeout(to),
|
|
118
|
+
inFlight: 0
|
|
119
|
+
};
|
|
120
|
+
return this.connections.set(to, newConnection), this.attachListeners(newConnection), this.emitter.emit("connectionCreated", to), { connection: newConnection, error: void 0 };
|
|
121
|
+
}
|
|
122
|
+
buildClientErrorResponse(error) {
|
|
123
|
+
return Promise.resolve({
|
|
124
|
+
error
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
createConnectionTimeout(connectionId, ttlSeconds) {
|
|
128
|
+
return setTimeout(
|
|
129
|
+
() => {
|
|
130
|
+
const connection = this.connections.get(connectionId);
|
|
131
|
+
if (connection && connection.inFlight > 0) {
|
|
132
|
+
connection.timeout = this.createConnectionTimeout(connectionId, connection.ttlSeconds);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this.deregisterConnection(connectionId);
|
|
136
|
+
},
|
|
137
|
+
(ttlSeconds ?? this.connectionTTLSeconds) * 1e3
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
touch(connection) {
|
|
141
|
+
clearTimeout(connection.timeout), connection.timeout = this.createConnectionTimeout(connection.id, connection.ttlSeconds), connection.lastUsedAt = ( new Date()).toISOString();
|
|
142
|
+
}
|
|
143
|
+
async registerConnection(to, clientOptions, override = !1) {
|
|
144
|
+
const existingLock = this.connectionLock.get(to);
|
|
145
|
+
existingLock && await existingLock;
|
|
146
|
+
const registrationPromise = this.performConnectionRegistration(to, clientOptions, override), lockPromise = registrationPromise.then(() => {
|
|
147
|
+
});
|
|
148
|
+
this.connectionLock.set(to, lockPromise);
|
|
149
|
+
const { connection, error } = await registrationPromise;
|
|
150
|
+
if (this.connectionLock.delete(to), error)
|
|
151
|
+
throw error;
|
|
152
|
+
return connection;
|
|
153
|
+
}
|
|
154
|
+
deregisterConnection(to) {
|
|
155
|
+
const connection = this.connections.get(to);
|
|
156
|
+
if (!connection) return;
|
|
157
|
+
const { client, timeout } = connection;
|
|
158
|
+
this.detachListeners(connection), client.destroy(), clearTimeout(timeout), this.connections.delete(to), this.emitter.emit("connectionDestroyed", to);
|
|
159
|
+
}
|
|
160
|
+
async transport(to, artifact, clientOptions) {
|
|
161
|
+
const validationResult = validate(BtpTransporterArtifactSchema, artifact);
|
|
162
|
+
if (!validationResult.success)
|
|
163
|
+
return this.buildClientErrorResponse(
|
|
164
|
+
new BTPErrorException(
|
|
165
|
+
{ message: "Invalid artifact" },
|
|
166
|
+
{ cause: { validationZodError: validationResult.error }, meta: { artifact } }
|
|
167
|
+
)
|
|
168
|
+
);
|
|
169
|
+
if (!this.connections.has(to))
|
|
170
|
+
try {
|
|
171
|
+
await this.registerConnection(to, clientOptions);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
return this.buildClientErrorResponse(error);
|
|
174
|
+
}
|
|
175
|
+
const connection = this.connections.get(to);
|
|
176
|
+
connection.inFlight++, this.touch(connection);
|
|
177
|
+
try {
|
|
178
|
+
return await connection.client.send(artifact, clientOptions?.replyTimeoutMs);
|
|
179
|
+
} finally {
|
|
180
|
+
connection.inFlight--, this.connections.get(to) === connection && this.touch(connection);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async transportBatch(to, artifacts, clientOptions) {
|
|
184
|
+
return Promise.all(artifacts.map((artifact) => this.transport(to, artifact, clientOptions)));
|
|
185
|
+
}
|
|
186
|
+
getMaxConnections() {
|
|
187
|
+
return this.maxConnections;
|
|
188
|
+
}
|
|
189
|
+
setMaxConnections(maxConnections) {
|
|
190
|
+
this.maxConnections = maxConnections;
|
|
191
|
+
}
|
|
192
|
+
getConnectionTTLSeconds() {
|
|
193
|
+
return this.connectionTTLSeconds;
|
|
194
|
+
}
|
|
195
|
+
updateConnectionTTLSeconds(connectionTTLSeconds, updateExistingConnections = !1) {
|
|
196
|
+
this.connectionTTLSeconds = connectionTTLSeconds, updateExistingConnections && this.connections.forEach((connection) => {
|
|
197
|
+
this.updateConnection(connection.id, void 0, connectionTTLSeconds);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
getClientOptions() {
|
|
201
|
+
return this.clientOptions;
|
|
202
|
+
}
|
|
203
|
+
getProtocolVersion() {
|
|
204
|
+
return BTP_PROTOCOL_VERSION;
|
|
205
|
+
}
|
|
206
|
+
getConnections() {
|
|
207
|
+
return Array.from(this.connections.values()).map((connection) => {
|
|
208
|
+
const { timeout, ...rest } = connection;
|
|
209
|
+
return rest;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
getConnection(id) {
|
|
213
|
+
const connection = this.connections.get(id);
|
|
214
|
+
if (!connection)
|
|
215
|
+
return;
|
|
216
|
+
const { timeout, ...rest } = connection;
|
|
217
|
+
return rest;
|
|
218
|
+
}
|
|
219
|
+
updateConnection(id, clientOptions, ttlSeconds) {
|
|
220
|
+
const connection = this.connections.get(id);
|
|
221
|
+
if (connection)
|
|
222
|
+
return clientOptions && connection.client.update(clientOptions), ttlSeconds && (connection.ttlSeconds = ttlSeconds, clearTimeout(connection.timeout), connection.timeout = this.createConnectionTimeout(id, ttlSeconds)), connection.updatedAt = ( new Date()).toISOString(), this.emitter.emit("connectionUpdated", id), connection;
|
|
223
|
+
}
|
|
224
|
+
getActiveConnections() {
|
|
225
|
+
return Array.from(this.connections.values()).filter((connection) => connection.isActive);
|
|
226
|
+
}
|
|
227
|
+
getTotalConnections() {
|
|
228
|
+
return this.connections.size;
|
|
229
|
+
}
|
|
230
|
+
getTotalActiveConnections() {
|
|
231
|
+
return this.getActiveConnections().length;
|
|
232
|
+
}
|
|
233
|
+
getMetrics() {
|
|
234
|
+
return {
|
|
235
|
+
totalConnections: this.getTotalConnections(),
|
|
236
|
+
activeConnections: this.getTotalActiveConnections()
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
on = (event, listener) => (this.emitter.on(event, listener), this);
|
|
240
|
+
off = (event, listener) => (this.emitter.off(event, listener), this);
|
|
241
|
+
destroy() {
|
|
242
|
+
this.connections.forEach((connection) => {
|
|
243
|
+
this.deregisterConnection(connection.id);
|
|
244
|
+
}), this.connectionLock.clear(), this.emitter.removeAllListeners(), this.connections.clear();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
export {
|
|
248
|
+
BtpsTransporter
|
|
249
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
import type { BTPError } from '../../core/error/types.js';
|
|
8
|
+
export declare const BTP_TRANSPORTER_DEFAULT_MAX_CONNECTIONS = 10;
|
|
9
|
+
export declare const BTP_TRANSPORTER_DEFAULT_CONNECTION_TTL_SECONDS = 300;
|
|
10
|
+
export declare const BTP_TRANSPORTER_ERROR_CONNECTION_ALREADY_EXISTS: BTPError;
|
|
11
|
+
export declare const BTP_TRANSPORTER_ERROR_MAX_CONNECTIONS_REACHED: BTPError;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const BTP_TRANSPORTER_DEFAULT_MAX_CONNECTIONS = 10, BTP_TRANSPORTER_DEFAULT_CONNECTION_TTL_SECONDS = 300, BTP_TRANSPORTER_ERROR_CONNECTION_ALREADY_EXISTS = {
|
|
9
|
+
code: "BTP_TRANSPORTER_ERROR_CONNECTION_ALREADY_EXISTS",
|
|
10
|
+
message: "Connection already exists"
|
|
11
|
+
}, BTP_TRANSPORTER_ERROR_MAX_CONNECTIONS_REACHED = {
|
|
12
|
+
code: "BTP_TRANSPORTER_ERROR_MAX_CONNECTIONS_REACHED",
|
|
13
|
+
message: "Max connections reached"
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
BTP_TRANSPORTER_DEFAULT_CONNECTION_TTL_SECONDS,
|
|
17
|
+
BTP_TRANSPORTER_DEFAULT_MAX_CONNECTIONS,
|
|
18
|
+
BTP_TRANSPORTER_ERROR_CONNECTION_ALREADY_EXISTS,
|
|
19
|
+
BTP_TRANSPORTER_ERROR_MAX_CONNECTIONS_REACHED
|
|
20
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
export * from './types/index.js';
|
|
8
|
+
export { BtpsAgent } from './btpsAgent.js';
|
|
9
|
+
export { BtpsClient } from './btpsClient.js';
|
|
10
|
+
export { BtpsTransporter } from './btpsTransporter.js';
|
|
11
|
+
export * from './constants/index.js';
|
|
12
|
+
export { MIN_CONNECT_TIMEOUT_MS, DEFAULT_CONNECT_TIMEOUT_MS, MIN_REPLY_TIMEOUT_MS, DEFAULT_REPLY_TIMEOUT_MS, } from './libs/timeouts.js';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from "./types/index.js";
|
|
9
|
+
import { BtpsAgent } from "./btpsAgent.js";
|
|
10
|
+
import { BtpsClient } from "./btpsClient.js";
|
|
11
|
+
import { BtpsTransporter } from "./btpsTransporter.js";
|
|
12
|
+
export * from "./constants/index.js";
|
|
13
|
+
import {
|
|
14
|
+
MIN_CONNECT_TIMEOUT_MS,
|
|
15
|
+
DEFAULT_CONNECT_TIMEOUT_MS,
|
|
16
|
+
MIN_REPLY_TIMEOUT_MS,
|
|
17
|
+
DEFAULT_REPLY_TIMEOUT_MS
|
|
18
|
+
} from "./libs/timeouts.js";
|
|
19
|
+
export {
|
|
20
|
+
BtpsAgent,
|
|
21
|
+
BtpsClient,
|
|
22
|
+
BtpsTransporter,
|
|
23
|
+
DEFAULT_CONNECT_TIMEOUT_MS,
|
|
24
|
+
DEFAULT_REPLY_TIMEOUT_MS,
|
|
25
|
+
MIN_CONNECT_TIMEOUT_MS,
|
|
26
|
+
MIN_REPLY_TIMEOUT_MS
|
|
27
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import {
|
|
10
|
+
BtpAgentQuerySchema,
|
|
11
|
+
BtpAgentMutationSchema,
|
|
12
|
+
BtpIdsPayloadSchema,
|
|
13
|
+
BtpAgentCreateSchema,
|
|
14
|
+
BtpCryptoOptionsSchema,
|
|
15
|
+
BtpsAgentActionTypeSchema
|
|
16
|
+
} from "../../core/server/schemas/schema.js";
|
|
17
|
+
import { identitySchema } from "../../core/server/schemas/shared.js";
|
|
18
|
+
import { validateAgentDocument } from "../../core/server/schemas/helpers.js";
|
|
19
|
+
import {
|
|
20
|
+
BtpAuthReqDocSchema,
|
|
21
|
+
BtpDocumentSchema,
|
|
22
|
+
BtpLifecycleEventSchema,
|
|
23
|
+
BtpTrustReqDocSchema,
|
|
24
|
+
BtpTrustResDocSchema
|
|
25
|
+
} from "../../core/server/schemas/btpsDocsSchema.js";
|
|
26
|
+
const BtpsAgentOptionsSchema = BtpCryptoOptionsSchema.optional(), BtpsAgentCommandDocumentSchema = z.union([
|
|
27
|
+
BtpTrustReqDocSchema,
|
|
28
|
+
BtpTrustResDocSchema,
|
|
29
|
+
BtpDocumentSchema,
|
|
30
|
+
BtpLifecycleEventSchema,
|
|
31
|
+
BtpAuthReqDocSchema,
|
|
32
|
+
BtpAgentQuerySchema,
|
|
33
|
+
BtpAgentMutationSchema,
|
|
34
|
+
BtpIdsPayloadSchema,
|
|
35
|
+
BtpAgentCreateSchema
|
|
36
|
+
]).optional(), BtpsAgentCommandSchema = z.object({
|
|
37
|
+
actionType: BtpsAgentActionTypeSchema,
|
|
38
|
+
to: identitySchema,
|
|
39
|
+
document: BtpsAgentCommandDocumentSchema.optional(),
|
|
40
|
+
options: BtpCryptoOptionsSchema.extend({ relatesTo: z.string().min(1).optional() }).optional()
|
|
41
|
+
}), BtpsAgentCommandCallSchema = BtpsAgentCommandSchema.refine(
|
|
42
|
+
(data) => validateAgentDocument(data.actionType, null, data.document),
|
|
43
|
+
{
|
|
44
|
+
message: "Document is required for this action type or document format is invalid",
|
|
45
|
+
path: ["document"]
|
|
46
|
+
}
|
|
47
|
+
).refine((data) => data.actionType === "event.send" == (data.options?.relatesTo !== void 0), {
|
|
48
|
+
message: "options.relatesTo is required on event.send and only there",
|
|
49
|
+
path: ["options", "relatesTo"]
|
|
50
|
+
});
|
|
51
|
+
export {
|
|
52
|
+
BtpsAgentCommandCallSchema,
|
|
53
|
+
BtpsAgentCommandDocumentSchema,
|
|
54
|
+
BtpsAgentCommandSchema,
|
|
55
|
+
BtpsAgentOptionsSchema
|
|
56
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
/** How long a sender waits to connect to a receiver, at least and by default: 5 seconds. */
|
|
8
|
+
export declare const MIN_CONNECT_TIMEOUT_MS = 5000;
|
|
9
|
+
export declare const DEFAULT_CONNECT_TIMEOUT_MS = 5000;
|
|
10
|
+
/**
|
|
11
|
+
* How long a sender waits for the answer once it has sent: at least 10 seconds,
|
|
12
|
+
* 20 by default. Every receiver can count on that long to answer normally,
|
|
13
|
+
* rather than answer "try again" during a slow moment.
|
|
14
|
+
*/
|
|
15
|
+
export declare const MIN_REPLY_TIMEOUT_MS = 10000;
|
|
16
|
+
export declare const DEFAULT_REPLY_TIMEOUT_MS = 20000;
|
|
17
|
+
/**
|
|
18
|
+
* A wait the client uses, in milliseconds. One it cannot use as given never
|
|
19
|
+
* stops the client: it is warned about and replaced by the minimum (when too
|
|
20
|
+
* short) or the default (when not a number of milliseconds at all).
|
|
21
|
+
*/
|
|
22
|
+
export declare const resolveTimeoutMs: (name: "connectTimeoutMs" | "replyTimeoutMs", configured: number | undefined, minimum: number, fallback: number) => number;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const MIN_CONNECT_TIMEOUT_MS = 5e3, DEFAULT_CONNECT_TIMEOUT_MS = 5e3, MIN_REPLY_TIMEOUT_MS = 1e4, DEFAULT_REPLY_TIMEOUT_MS = 2e4, resolveTimeoutMs = (name, configured, minimum, fallback) => configured === void 0 ? fallback : !Number.isFinite(configured) || configured <= 0 ? (console.warn(
|
|
9
|
+
`[BtpsClient] ${name} ${configured} is not a usable time; using the default ${fallback} ms`
|
|
10
|
+
), fallback) : configured < minimum ? (console.warn(
|
|
11
|
+
`[BtpsClient] ${name} ${configured} is below the ${minimum} ms minimum; using ${minimum} ms`
|
|
12
|
+
), minimum) : configured;
|
|
13
|
+
export {
|
|
14
|
+
DEFAULT_CONNECT_TIMEOUT_MS,
|
|
15
|
+
DEFAULT_REPLY_TIMEOUT_MS,
|
|
16
|
+
MIN_CONNECT_TIMEOUT_MS,
|
|
17
|
+
MIN_REPLY_TIMEOUT_MS,
|
|
18
|
+
resolveTimeoutMs
|
|
19
|
+
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
import type { BTPCryptoOptions } from '../../core/crypto/types.js';
|
|
8
|
+
import type { Signer } from '../../core/crypto/signer-interface.js';
|
|
9
|
+
import type { Decrypter } from '../../core/crypto/decrypter-interface.js';
|
|
10
|
+
import type { BTPErrorException } from '../../core/error/index.js';
|
|
11
|
+
import type { BtpsClient } from '../../client/btpsClient.js';
|
|
12
|
+
import type { BTPAgentMutation, BTPAgentQuery, BTPDocType, BTPIdsPayload, BTPAuthReqDoc, BTPServerResponse, AgentAction } from '../../server/index.js';
|
|
13
|
+
import type { ConnectionOptions, TLSSocket } from 'tls';
|
|
14
|
+
export interface BTPTransporterMetrics {
|
|
15
|
+
totalConnections: number;
|
|
16
|
+
activeConnections: number;
|
|
17
|
+
}
|
|
18
|
+
export interface BTPClientOptions {
|
|
19
|
+
to: string;
|
|
20
|
+
maxRetries?: number;
|
|
21
|
+
retryDelayMs?: number;
|
|
22
|
+
/** How long to wait to connect, in ms: at least 5000, 5000 by default */
|
|
23
|
+
connectTimeoutMs?: number;
|
|
24
|
+
/** How long `send()` waits for the answer, in ms: at least 10000, 20000 by default */
|
|
25
|
+
replyTimeoutMs?: number;
|
|
26
|
+
/** Closes the connection after this long with no artifact waiting for an answer, in ms; reconnects on the next send. Off when not set */
|
|
27
|
+
connectionTimeoutMs?: number;
|
|
28
|
+
btpMtsOptions?: Omit<ConnectionOptions, 'port' | 'host'>;
|
|
29
|
+
host?: string;
|
|
30
|
+
hostSelector?: string;
|
|
31
|
+
/** With `host`: the identity operating that host, which signs its lookup answers. Otherwise read from DNS (`op`). */
|
|
32
|
+
hostOperator?: string;
|
|
33
|
+
version?: string;
|
|
34
|
+
port?: number;
|
|
35
|
+
maxQueue?: number;
|
|
36
|
+
}
|
|
37
|
+
/** An agent and its device key: a PEM key pair, or a Signer (and Decrypter) holding it elsewhere, such as the device's own keystore. */
|
|
38
|
+
export type BTPAgent = {
|
|
39
|
+
id: string;
|
|
40
|
+
identityKey: string;
|
|
41
|
+
identityCert: string;
|
|
42
|
+
} | {
|
|
43
|
+
id: string;
|
|
44
|
+
signer: Signer;
|
|
45
|
+
decrypter?: Decrypter;
|
|
46
|
+
};
|
|
47
|
+
export interface BTPAgentOptions extends Omit<BTPClientOptions, 'to'> {
|
|
48
|
+
agent: BTPAgent;
|
|
49
|
+
btpIdentity: string;
|
|
50
|
+
}
|
|
51
|
+
export interface BTPTransporterOptions extends Omit<BTPClientOptions, 'to'> {
|
|
52
|
+
maxConnections?: number;
|
|
53
|
+
connectionTTLSeconds?: number;
|
|
54
|
+
}
|
|
55
|
+
export type BtpsAgentDoc = BTPDocType | BTPAuthReqDoc | BTPAgentMutation | BTPIdsPayload | BTPAgentQuery;
|
|
56
|
+
export type BtpsAgentCommandOptions = BTPCryptoOptions & {
|
|
57
|
+
/** Required on `event.send`, refused elsewhere: the id of the `BTPS_DOC` artifact that delivered the invoice. */
|
|
58
|
+
relatesTo?: string;
|
|
59
|
+
};
|
|
60
|
+
export type BtpsAgentCommandParams = {
|
|
61
|
+
actionType: AgentAction;
|
|
62
|
+
to: string;
|
|
63
|
+
document?: BtpsAgentDoc;
|
|
64
|
+
options?: BtpsAgentCommandOptions;
|
|
65
|
+
};
|
|
66
|
+
export interface ConnectionStates {
|
|
67
|
+
isConnecting: boolean;
|
|
68
|
+
isConnected: boolean;
|
|
69
|
+
isDraining: boolean;
|
|
70
|
+
isDestroyed: boolean;
|
|
71
|
+
shouldRetry: boolean;
|
|
72
|
+
}
|
|
73
|
+
export interface BTPSRetryInfo {
|
|
74
|
+
willRetry: boolean;
|
|
75
|
+
retriesLeft: number;
|
|
76
|
+
attempt: number;
|
|
77
|
+
}
|
|
78
|
+
export type BtpsClientErrorInfo = BTPSRetryInfo & {
|
|
79
|
+
error: BTPErrorException;
|
|
80
|
+
};
|
|
81
|
+
export interface UpdateBTPClientOptions {
|
|
82
|
+
maxRetries?: number;
|
|
83
|
+
retryDelayMs?: number;
|
|
84
|
+
connectTimeoutMs?: number;
|
|
85
|
+
replyTimeoutMs?: number;
|
|
86
|
+
connectionTimeoutMs?: number;
|
|
87
|
+
maxQueue?: number;
|
|
88
|
+
}
|
|
89
|
+
/** What the client emits on `connected`. */
|
|
90
|
+
export type BtpsClientConnectedInfo = {
|
|
91
|
+
identity: string;
|
|
92
|
+
retries: number;
|
|
93
|
+
states: ConnectionStates;
|
|
94
|
+
socket: TLSSocket;
|
|
95
|
+
};
|
|
96
|
+
export type BtpsClientEvents = {
|
|
97
|
+
connected: (connectedInfo: BtpsClientConnectedInfo) => void;
|
|
98
|
+
end: (endInfo: BtpsClientErrorInfo) => void;
|
|
99
|
+
error: (errorInfo: BtpsClientErrorInfo) => void;
|
|
100
|
+
message: (msg: BTPClientResponse & {
|
|
101
|
+
validSignature: boolean;
|
|
102
|
+
}) => void;
|
|
103
|
+
close: (closeInfo: BtpsClientErrorInfo) => void;
|
|
104
|
+
};
|
|
105
|
+
export type BTPConnectionInternal = BTPConnection & {
|
|
106
|
+
timeout: NodeJS.Timeout;
|
|
107
|
+
listeners?: BtpsClientEvents;
|
|
108
|
+
/** Sends waiting for their answer: the connection is never closed while any are */
|
|
109
|
+
inFlight: number;
|
|
110
|
+
/** Set by updateConnection; the transporter's own TTL otherwise */
|
|
111
|
+
ttlSeconds?: number;
|
|
112
|
+
};
|
|
113
|
+
export type BTPConnection = {
|
|
114
|
+
id: string;
|
|
115
|
+
client: BtpsClient;
|
|
116
|
+
createdAt: string;
|
|
117
|
+
updatedAt: string;
|
|
118
|
+
lastUsedAt: string;
|
|
119
|
+
isActive: boolean;
|
|
120
|
+
clientOptions: BTPClientOptions;
|
|
121
|
+
getStatus: () => ConnectionStates;
|
|
122
|
+
};
|
|
123
|
+
export type BTPConnectionUpdate = {
|
|
124
|
+
clientOptions?: BTPClientOptions;
|
|
125
|
+
ttl?: number;
|
|
126
|
+
maxQueue?: number;
|
|
127
|
+
};
|
|
128
|
+
export type BtpsTransporterEvents = {
|
|
129
|
+
connectionCreated: (connectionId: string) => void;
|
|
130
|
+
connectionUpdated: (connectionId: string) => void;
|
|
131
|
+
connectionDestroyed: (connectionId: string) => void;
|
|
132
|
+
connectionError: (connectionId: string, errorInfo: BtpsClientErrorInfo) => void;
|
|
133
|
+
connectionEnd: (connectionId: string, endInfo: BtpsClientErrorInfo) => void;
|
|
134
|
+
connectionConnected: (connectionId: string) => void;
|
|
135
|
+
connectionClose: (connectionId: string, closeInfo: BtpsClientErrorInfo) => void;
|
|
136
|
+
connectionMessage: (connectionId: string, message: BTPClientResponse & {
|
|
137
|
+
validSignature: boolean;
|
|
138
|
+
}) => void;
|
|
139
|
+
};
|
|
140
|
+
export type TypedEventEmitter<T = BtpsClientEvents> = {
|
|
141
|
+
on<K extends keyof T>(event: K, listener: T[K]): void;
|
|
142
|
+
off<K extends keyof T>(event: K, listener: T[K]): void;
|
|
143
|
+
};
|
|
144
|
+
export interface BTPClientResponse {
|
|
145
|
+
response?: BTPServerResponse;
|
|
146
|
+
error?: BTPErrorException;
|
|
147
|
+
}
|
|
148
|
+
export type BtpsHostDnsTxt = {
|
|
149
|
+
version: string;
|
|
150
|
+
hostname: string;
|
|
151
|
+
selector: string;
|
|
152
|
+
port: number;
|
|
153
|
+
/** The identity operating the host (`op`), when declared */
|
|
154
|
+
operator?: string;
|
|
155
|
+
};
|