@enbox/dwn-server 0.0.2 → 0.0.3
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 +13 -13
- package/dist/esm/src/config.d.ts +2 -6
- package/dist/esm/src/config.d.ts.map +1 -1
- package/dist/esm/src/config.js +4 -8
- package/dist/esm/src/config.js.map +1 -1
- package/dist/esm/src/connection/connection-manager.d.ts +9 -9
- package/dist/esm/src/connection/connection-manager.d.ts.map +1 -1
- package/dist/esm/src/connection/connection-manager.js +5 -3
- package/dist/esm/src/connection/connection-manager.js.map +1 -1
- package/dist/esm/src/connection/socket-connection.d.ts +18 -17
- package/dist/esm/src/connection/socket-connection.d.ts.map +1 -1
- package/dist/esm/src/connection/socket-connection.js +26 -35
- package/dist/esm/src/connection/socket-connection.js.map +1 -1
- package/dist/esm/src/dwn-error.js.map +1 -1
- package/dist/esm/src/dwn-server.d.ts +5 -6
- package/dist/esm/src/dwn-server.d.ts.map +1 -1
- package/dist/esm/src/dwn-server.js +3 -14
- package/dist/esm/src/dwn-server.js.map +1 -1
- package/dist/esm/src/http-api.d.ts +9 -11
- package/dist/esm/src/http-api.d.ts.map +1 -1
- package/dist/esm/src/http-api.js +450 -375
- package/dist/esm/src/http-api.js.map +1 -1
- package/dist/esm/src/json-rpc-handlers/dwn/process-message.d.ts.map +1 -1
- package/dist/esm/src/json-rpc-handlers/dwn/process-message.js +3 -3
- package/dist/esm/src/json-rpc-handlers/dwn/process-message.js.map +1 -1
- package/dist/esm/src/json-rpc-handlers/subscription/close.d.ts.map +1 -1
- package/dist/esm/src/json-rpc-handlers/subscription/close.js.map +1 -1
- package/dist/esm/src/json-rpc-socket.d.ts +1 -1
- package/dist/esm/src/json-rpc-socket.d.ts.map +1 -1
- package/dist/esm/src/json-rpc-socket.js +1 -1
- package/dist/esm/src/json-rpc-socket.js.map +1 -1
- package/dist/esm/src/lib/json-rpc-router.d.ts +3 -4
- package/dist/esm/src/lib/json-rpc-router.d.ts.map +1 -1
- package/dist/esm/src/lib/json-rpc-router.js.map +1 -1
- package/dist/esm/src/lib/json-rpc.d.ts.map +1 -1
- package/dist/esm/src/lib/json-rpc.js.map +1 -1
- package/dist/esm/src/main.js +0 -0
- package/dist/esm/src/metrics.d.ts +1 -1
- package/dist/esm/src/metrics.js.map +1 -1
- package/dist/esm/src/registration/proof-of-work-manager.d.ts +1 -1
- package/dist/esm/src/registration/proof-of-work-manager.d.ts.map +1 -1
- package/dist/esm/src/registration/proof-of-work-manager.js +3 -3
- package/dist/esm/src/registration/proof-of-work-manager.js.map +1 -1
- package/dist/esm/src/registration/registration-manager.d.ts +3 -3
- package/dist/esm/src/registration/registration-manager.d.ts.map +1 -1
- package/dist/esm/src/registration/registration-manager.js +6 -6
- package/dist/esm/src/registration/registration-manager.js.map +1 -1
- package/dist/esm/src/registration/registration-store.d.ts +1 -1
- package/dist/esm/src/registration/registration-store.d.ts.map +1 -1
- package/dist/esm/src/registration/registration-store.js.map +1 -1
- package/dist/esm/src/storage.d.ts +2 -2
- package/dist/esm/src/storage.d.ts.map +1 -1
- package/dist/esm/src/storage.js +5 -4
- package/dist/esm/src/storage.js.map +1 -1
- package/dist/esm/src/web5-connect/sql-ttl-cache.d.ts.map +1 -1
- package/dist/esm/src/web5-connect/sql-ttl-cache.js +3 -2
- package/dist/esm/src/web5-connect/sql-ttl-cache.js.map +1 -1
- package/dist/esm/src/web5-connect/web5-connect-server.d.ts.map +1 -1
- package/dist/esm/src/web5-connect/web5-connect-server.js +2 -2
- package/dist/esm/src/web5-connect/web5-connect-server.js.map +1 -1
- package/dist/esm/src/ws-api.d.ts +2 -4
- package/dist/esm/src/ws-api.d.ts.map +1 -1
- package/dist/esm/src/ws-api.js +6 -17
- package/dist/esm/src/ws-api.js.map +1 -1
- package/dist/esm/tests/common-scenario-validator.d.ts.map +1 -1
- package/dist/esm/tests/common-scenario-validator.js +2 -3
- package/dist/esm/tests/common-scenario-validator.js.map +1 -1
- package/dist/esm/tests/connection/connection-manager.spec.js +11 -9
- package/dist/esm/tests/connection/connection-manager.spec.js.map +1 -1
- package/dist/esm/tests/connection/socket-connection.spec.js +40 -18
- package/dist/esm/tests/connection/socket-connection.spec.js.map +1 -1
- package/dist/esm/tests/cors/http-api.browser.js +1 -1
- package/dist/esm/tests/cors/http-api.browser.js.map +1 -1
- package/dist/esm/tests/dwn-process-message.spec.js +4 -4
- package/dist/esm/tests/dwn-process-message.spec.js.map +1 -1
- package/dist/esm/tests/dwn-server.spec.js +8 -9
- package/dist/esm/tests/dwn-server.spec.js.map +1 -1
- package/dist/esm/tests/http-api.spec.js +92 -85
- package/dist/esm/tests/http-api.spec.js.map +1 -1
- package/dist/esm/tests/json-rpc-socket.spec.js +11 -9
- package/dist/esm/tests/json-rpc-socket.spec.js.map +1 -1
- package/dist/esm/tests/plugins/data-store-sqlite.d.ts +2 -2
- package/dist/esm/tests/plugins/data-store-sqlite.js +2 -2
- package/dist/esm/tests/plugins/event-log-sqlite.d.ts +2 -2
- package/dist/esm/tests/plugins/event-log-sqlite.js +2 -2
- package/dist/esm/tests/plugins/event-stream-in-memory.d.ts +2 -2
- package/dist/esm/tests/plugins/event-stream-in-memory.d.ts.map +1 -1
- package/dist/esm/tests/plugins/event-stream-in-memory.js +1 -1
- package/dist/esm/tests/plugins/event-stream-in-memory.js.map +1 -1
- package/dist/esm/tests/plugins/message-store-sqlite.d.ts +2 -2
- package/dist/esm/tests/plugins/message-store-sqlite.d.ts.map +1 -1
- package/dist/esm/tests/plugins/message-store-sqlite.js +2 -2
- package/dist/esm/tests/plugins/message-store-sqlite.js.map +1 -1
- package/dist/esm/tests/plugins/resumable-task-store-sqlite.d.ts +2 -2
- package/dist/esm/tests/plugins/resumable-task-store-sqlite.d.ts.map +1 -1
- package/dist/esm/tests/plugins/resumable-task-store-sqlite.js +2 -2
- package/dist/esm/tests/plugins/resumable-task-store-sqlite.js.map +1 -1
- package/dist/esm/tests/process-handler.spec.js +6 -6
- package/dist/esm/tests/process-handler.spec.js.map +1 -1
- package/dist/esm/tests/registration/proof-of-work-manager.spec.js +3 -4
- package/dist/esm/tests/registration/proof-of-work-manager.spec.js.map +1 -1
- package/dist/esm/tests/rpc-subscribe-close.spec.js +1 -1
- package/dist/esm/tests/rpc-subscribe-close.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/dynamic-plugin-loading.spec.js +11 -10
- package/dist/esm/tests/scenarios/dynamic-plugin-loading.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/registration.spec.js +16 -12
- package/dist/esm/tests/scenarios/registration.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/web5-connect.spec.js +12 -8
- package/dist/esm/tests/scenarios/web5-connect.spec.js.map +1 -1
- package/dist/esm/tests/test-dwn.d.ts.map +1 -1
- package/dist/esm/tests/test-dwn.js +9 -15
- package/dist/esm/tests/test-dwn.js.map +1 -1
- package/dist/esm/tests/utils.d.ts +3 -6
- package/dist/esm/tests/utils.d.ts.map +1 -1
- package/dist/esm/tests/utils.js +9 -18
- package/dist/esm/tests/utils.js.map +1 -1
- package/dist/esm/tests/ws-api.spec.js +28 -23
- package/dist/esm/tests/ws-api.spec.js.map +1 -1
- package/package.json +25 -44
- package/src/config.ts +15 -19
- package/src/connection/connection-manager.ts +18 -12
- package/src/connection/socket-connection.ts +52 -57
- package/src/dwn-error.ts +2 -2
- package/src/dwn-server.ts +17 -30
- package/src/http-api.ts +499 -396
- package/src/json-rpc-handlers/dwn/process-message.ts +9 -10
- package/src/json-rpc-handlers/subscription/close.ts +4 -4
- package/src/json-rpc-socket.ts +3 -2
- package/src/lib/json-rpc-router.ts +5 -6
- package/src/lib/json-rpc.ts +6 -6
- package/src/metrics.ts +7 -7
- package/src/process-handlers.ts +5 -5
- package/src/registration/proof-of-work-manager.ts +11 -10
- package/src/registration/registration-manager.ts +23 -21
- package/src/registration/registration-store.ts +8 -7
- package/src/storage.ts +15 -13
- package/src/web5-connect/sql-ttl-cache.ts +5 -4
- package/src/web5-connect/web5-connect-server.ts +9 -8
- package/src/ws-api.ts +11 -26
- package/dist/cjs/index.js +0 -6811
- package/dist/cjs/package.json +0 -1
- package/dist/esm/src/lib/http-server-shutdown-handler.d.ts +0 -10
- package/dist/esm/src/lib/http-server-shutdown-handler.d.ts.map +0 -1
- package/dist/esm/src/lib/http-server-shutdown-handler.js +0 -65
- package/dist/esm/src/lib/http-server-shutdown-handler.js.map +0 -1
- package/src/lib/http-server-shutdown-handler.ts +0 -79
|
@@ -1,54 +1,63 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { DwnMethodName } from '@enbox/dwn-sdk-js';
|
|
2
|
+
import type { RequestContext } from '../lib/json-rpc-router.js';
|
|
3
|
+
import type { ServerWebSocket } from 'bun';
|
|
4
|
+
import type { Dwn, GenericMessage, MessageEvent } from '@enbox/dwn-sdk-js';
|
|
5
|
+
import type { JsonRpcErrorResponse, JsonRpcId, JsonRpcRequest, JsonRpcResponse, JsonRpcSubscription } from '../lib/json-rpc.js';
|
|
6
|
+
|
|
7
|
+
import type { WsData } from '../http-api.js';
|
|
3
8
|
|
|
4
|
-
import type { WebSocket } from "ws";
|
|
5
9
|
import log from 'loglevel';
|
|
6
10
|
import { v4 as uuidv4 } from 'uuid';
|
|
7
11
|
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
import { jsonRpcRouter } from '../json-rpc-api.js';
|
|
13
|
+
import { requestCounter } from '../metrics.js';
|
|
14
|
+
import {
|
|
15
|
+
createJsonRpcErrorResponse,
|
|
16
|
+
createJsonRpcSuccessResponse,
|
|
17
|
+
JsonRpcErrorCodes,
|
|
18
|
+
} from '../lib/json-rpc.js';
|
|
19
|
+
import { DwnServerError, DwnServerErrorCode } from '../dwn-error.js';
|
|
15
20
|
|
|
16
21
|
const HEARTBEAT_INTERVAL = 30_000;
|
|
17
22
|
|
|
18
23
|
/**
|
|
19
24
|
* SocketConnection handles a WebSocket connection to a DWN using JSON RPC.
|
|
20
25
|
* It also manages references to the long running RPC subscriptions for the connection.
|
|
26
|
+
*
|
|
27
|
+
* With Bun's native WebSocket, the message/close/error events are dispatched by the
|
|
28
|
+
* Bun.serve() websocket handlers in http-api.ts, which delegate to the public `message()`
|
|
29
|
+
* and `close()` methods on this class.
|
|
21
30
|
*/
|
|
22
31
|
export class SocketConnection {
|
|
23
|
-
private heartbeatInterval:
|
|
32
|
+
private heartbeatInterval: ReturnType<typeof setInterval>;
|
|
24
33
|
private subscriptions: Map<JsonRpcId, JsonRpcSubscription> = new Map();
|
|
25
34
|
private isAlive: boolean;
|
|
26
35
|
|
|
27
36
|
constructor(
|
|
28
|
-
private socket:
|
|
37
|
+
private socket: ServerWebSocket<WsData>,
|
|
29
38
|
private dwn: Dwn,
|
|
30
|
-
private
|
|
39
|
+
private onCloseCallback?: () => void
|
|
31
40
|
){
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
socket.on('error', this.error.bind(this));
|
|
35
|
-
socket.on('pong', this.pong.bind(this));
|
|
36
|
-
|
|
37
|
-
// Sometimes connections between client <-> server can get borked in such a way that
|
|
38
|
-
// leaves both unaware of the borkage. ping messages can be used as a means to verify
|
|
39
|
-
// that the remote endpoint is still responsive. Server will ping each socket every 30s
|
|
40
|
-
// if a pong hasn't received from a socket by the next ping, the server will terminate
|
|
41
|
-
// the socket connection
|
|
41
|
+
// Bun handles ping/pong automatically at the protocol level, but we still
|
|
42
|
+
// want an application-level heartbeat to detect dead connections.
|
|
42
43
|
this.isAlive = true;
|
|
43
44
|
this.heartbeatInterval = setInterval(() => {
|
|
44
45
|
if (this.isAlive === false) {
|
|
45
46
|
this.close();
|
|
47
|
+
return;
|
|
46
48
|
}
|
|
47
49
|
this.isAlive = false;
|
|
48
50
|
this.socket.ping();
|
|
49
51
|
}, HEARTBEAT_INTERVAL);
|
|
50
52
|
}
|
|
51
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Called when a pong is received (triggered by Bun's built-in ping/pong handling).
|
|
56
|
+
*/
|
|
57
|
+
pong(): void {
|
|
58
|
+
this.isAlive = true;
|
|
59
|
+
}
|
|
60
|
+
|
|
52
61
|
/**
|
|
53
62
|
* Checks to see if the incoming `JsonRpcId` is already in use for a subscription.
|
|
54
63
|
*/
|
|
@@ -65,7 +74,7 @@ export class SocketConnection {
|
|
|
65
74
|
throw new DwnServerError(
|
|
66
75
|
DwnServerErrorCode.ConnectionSubscriptionJsonRpcIdExists,
|
|
67
76
|
`the subscription with id ${subscription.id} already exists`
|
|
68
|
-
)
|
|
77
|
+
);
|
|
69
78
|
}
|
|
70
79
|
|
|
71
80
|
this.subscriptions.set(subscription.id, subscription);
|
|
@@ -81,7 +90,7 @@ export class SocketConnection {
|
|
|
81
90
|
throw new DwnServerError(
|
|
82
91
|
DwnServerErrorCode.ConnectionSubscriptionJsonRpcIdNotFound,
|
|
83
92
|
`the subscription with id ${id} was not found`
|
|
84
|
-
)
|
|
93
|
+
);
|
|
85
94
|
}
|
|
86
95
|
|
|
87
96
|
const connection = this.subscriptions.get(id);
|
|
@@ -95,10 +104,7 @@ export class SocketConnection {
|
|
|
95
104
|
async close(): Promise<void> {
|
|
96
105
|
clearInterval(this.heartbeatInterval);
|
|
97
106
|
|
|
98
|
-
|
|
99
|
-
this.socket.removeAllListeners();
|
|
100
|
-
|
|
101
|
-
const closePromises = [];
|
|
107
|
+
const closePromises: Promise<void>[] = [];
|
|
102
108
|
for (const [id, subscription] of this.subscriptions) {
|
|
103
109
|
closePromises.push(subscription.close());
|
|
104
110
|
this.subscriptions.delete(id);
|
|
@@ -111,52 +117,45 @@ export class SocketConnection {
|
|
|
111
117
|
this.socket.close();
|
|
112
118
|
|
|
113
119
|
// if there was a close handler passed call it after the connection has been closed
|
|
114
|
-
if (this.
|
|
115
|
-
this.
|
|
120
|
+
if (this.onCloseCallback !== undefined) {
|
|
121
|
+
this.onCloseCallback();
|
|
116
122
|
}
|
|
117
123
|
}
|
|
118
124
|
|
|
119
|
-
/**
|
|
120
|
-
* Pong messages are automatically sent in response to ping messages as required by
|
|
121
|
-
* the websocket spec. So, no need to send explicit pongs.
|
|
122
|
-
*/
|
|
123
|
-
private pong(): void {
|
|
124
|
-
this.isAlive = true;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
125
|
/**
|
|
128
126
|
* Log the error and close the connection.
|
|
129
127
|
*/
|
|
130
|
-
|
|
128
|
+
async error(error: Error): Promise<void> {
|
|
131
129
|
log.error(`SocketConnection error, terminating connection`, error);
|
|
132
|
-
this.socket.
|
|
130
|
+
this.socket.close();
|
|
133
131
|
await this.close();
|
|
134
132
|
}
|
|
135
133
|
|
|
136
134
|
/**
|
|
137
135
|
* Handles a `JSON RPC 2.0` encoded message.
|
|
136
|
+
* This is called by Bun's websocket message handler via http-api.ts.
|
|
138
137
|
*/
|
|
139
|
-
|
|
138
|
+
async message(dataBuffer: Buffer): Promise<void> {
|
|
140
139
|
const requestData = dataBuffer.toString();
|
|
141
140
|
if (!requestData) {
|
|
142
141
|
return this.send(createJsonRpcErrorResponse(
|
|
143
142
|
uuidv4(),
|
|
144
143
|
JsonRpcErrorCodes.BadRequest,
|
|
145
144
|
'request payload required.'
|
|
146
|
-
))
|
|
145
|
+
));
|
|
147
146
|
}
|
|
148
147
|
|
|
149
148
|
let jsonRequest: JsonRpcRequest;
|
|
150
149
|
try {
|
|
151
150
|
jsonRequest = JSON.parse(requestData);
|
|
152
|
-
} catch(error) {
|
|
151
|
+
} catch (error) {
|
|
153
152
|
const errorResponse = createJsonRpcErrorResponse(
|
|
154
153
|
uuidv4(),
|
|
155
154
|
JsonRpcErrorCodes.BadRequest,
|
|
156
155
|
(error as Error).message
|
|
157
156
|
);
|
|
158
157
|
return this.send(errorResponse);
|
|
159
|
-
}
|
|
158
|
+
}
|
|
160
159
|
|
|
161
160
|
const requestContext = await this.buildRequestContext(jsonRequest);
|
|
162
161
|
const { jsonRpcResponse } = await jsonRpcRouter.handle(jsonRequest, requestContext);
|
|
@@ -164,15 +163,15 @@ export class SocketConnection {
|
|
|
164
163
|
requestCounter.inc({ method: jsonRequest.method, error: 1 });
|
|
165
164
|
} else {
|
|
166
165
|
requestCounter.inc({
|
|
167
|
-
method: jsonRequest.method,
|
|
168
|
-
status: jsonRpcResponse?.result?.reply?.status?.code || 0,
|
|
166
|
+
method : jsonRequest.method,
|
|
167
|
+
status : jsonRpcResponse?.result?.reply?.status?.code || 0,
|
|
169
168
|
});
|
|
170
169
|
}
|
|
171
170
|
this.send(jsonRpcResponse);
|
|
172
171
|
}
|
|
173
172
|
|
|
174
173
|
/**
|
|
175
|
-
* Sends a JSON encoded
|
|
174
|
+
* Sends a JSON encoded string through the WebSocket.
|
|
176
175
|
*/
|
|
177
176
|
private send(response: JsonRpcResponse | JsonRpcErrorResponse): void {
|
|
178
177
|
this.socket.send(JSON.stringify(response));
|
|
@@ -180,20 +179,16 @@ export class SocketConnection {
|
|
|
180
179
|
|
|
181
180
|
/**
|
|
182
181
|
* Creates a subscription handler to send messages matching the subscription requested.
|
|
183
|
-
*
|
|
184
|
-
* Wraps the incoming `message` in a `JSON RPC Success Response` using the original subscription`JSON RPC Id` to send through the WebSocket.
|
|
185
182
|
*/
|
|
186
183
|
private createSubscriptionHandler(id: JsonRpcId): (message: MessageEvent) => void {
|
|
187
184
|
return (event) => {
|
|
188
185
|
const response = createJsonRpcSuccessResponse(id, { event });
|
|
189
186
|
this.send(response);
|
|
190
|
-
}
|
|
187
|
+
};
|
|
191
188
|
}
|
|
192
189
|
|
|
193
190
|
/**
|
|
194
191
|
* Builds a `RequestContext` object to use with the `JSON RPC API`.
|
|
195
|
-
*
|
|
196
|
-
* Adds a `subscriptionHandler` for `Subscribe` messages.
|
|
197
192
|
*/
|
|
198
193
|
private async buildRequestContext(request: JsonRpcRequest): Promise<RequestContext> {
|
|
199
194
|
const { params, method, subscription } = request;
|
|
@@ -202,7 +197,7 @@ export class SocketConnection {
|
|
|
202
197
|
transport : 'ws',
|
|
203
198
|
dwn : this.dwn,
|
|
204
199
|
socketConnection : this,
|
|
205
|
-
}
|
|
200
|
+
};
|
|
206
201
|
|
|
207
202
|
// methods that expect a long-running subscription begin with `rpc.subscribe.`
|
|
208
203
|
if (method.startsWith('rpc.subscribe.') && subscription) {
|
|
@@ -210,9 +205,9 @@ export class SocketConnection {
|
|
|
210
205
|
if (message?.descriptor.method === DwnMethodName.Subscribe) {
|
|
211
206
|
const handlerFunc = this.createSubscriptionHandler(subscription.id);
|
|
212
207
|
requestContext.subscriptionRequest = {
|
|
213
|
-
id: subscription.id,
|
|
214
|
-
subscriptionHandler: (message): void => handlerFunc(message),
|
|
215
|
-
}
|
|
208
|
+
id : subscription.id,
|
|
209
|
+
subscriptionHandler : (message): void => handlerFunc(message),
|
|
210
|
+
};
|
|
216
211
|
}
|
|
217
212
|
}
|
|
218
213
|
|
package/src/dwn-error.ts
CHANGED
package/src/dwn-server.ts
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import type { DidResolver } from '@enbox/dids';
|
|
2
|
+
import type { DwnServerConfig } from './config.js';
|
|
2
3
|
import type { EventStream } from '@enbox/dwn-sdk-js';
|
|
3
4
|
import type { ProcessHandlers } from './process-handlers.js';
|
|
4
|
-
import type { Server } from '
|
|
5
|
-
|
|
6
|
-
import type {
|
|
5
|
+
import type { Server } from 'bun';
|
|
6
|
+
|
|
7
|
+
import type { WsData } from './http-api.js';
|
|
7
8
|
|
|
8
9
|
import log from 'loglevel';
|
|
9
10
|
import prefix from 'loglevel-plugin-prefix';
|
|
11
|
+
import { Dwn, EventEmitterStream } from '@enbox/dwn-sdk-js';
|
|
12
|
+
|
|
10
13
|
import { config as defaultConfig } from './config.js';
|
|
11
14
|
import { getDwnConfig } from './storage.js';
|
|
12
|
-
import { HttpServerShutdownHandler } from './lib/http-server-shutdown-handler.js';
|
|
13
15
|
import { HttpApi } from './http-api.js';
|
|
14
16
|
import { PluginLoader } from './plugin-loader.js';
|
|
15
17
|
import { RegistrationManager } from './registration/registration-manager.js';
|
|
16
18
|
import { WsApi } from './ws-api.js';
|
|
17
|
-
import { Dwn, EventEmitterStream } from '@enbox/dwn-sdk-js';
|
|
18
19
|
import { removeProcessHandlers, setProcessHandlers } from './process-handlers.js';
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -43,7 +44,7 @@ enum DwnServerState {
|
|
|
43
44
|
export class DwnServer {
|
|
44
45
|
serverState = DwnServerState.Stopped;
|
|
45
46
|
processHandlers: ProcessHandlers;
|
|
46
|
-
|
|
47
|
+
|
|
47
48
|
/**
|
|
48
49
|
* A custom DID resolver to use in the DWN.
|
|
49
50
|
* Mainly for testing purposes. Ignored if `dwn` is provided.
|
|
@@ -51,7 +52,6 @@ export class DwnServer {
|
|
|
51
52
|
didResolver?: DidResolver;
|
|
52
53
|
dwn?: Dwn;
|
|
53
54
|
config: DwnServerConfig;
|
|
54
|
-
#httpServerShutdownHandler: HttpServerShutdownHandler;
|
|
55
55
|
#httpApi: HttpApi;
|
|
56
56
|
#wsApi: WsApi;
|
|
57
57
|
|
|
@@ -93,10 +93,10 @@ export class DwnServer {
|
|
|
93
93
|
if (!this.dwn) {
|
|
94
94
|
// undefined registrationStoreUrl is used as a signal that there is no need for tenant registration, DWN is open for all.
|
|
95
95
|
registrationManager = await RegistrationManager.create({
|
|
96
|
-
registrationStoreUrl: this.config.registrationStoreUrl,
|
|
97
|
-
termsOfServiceFilePath: this.config.termsOfServiceFilePath,
|
|
98
|
-
proofOfWorkChallengeNonceSeed: this.config.registrationProofOfWorkSeed,
|
|
99
|
-
proofOfWorkInitialMaximumAllowedHash: this.config.registrationProofOfWorkInitialMaxHash,
|
|
96
|
+
registrationStoreUrl : this.config.registrationStoreUrl,
|
|
97
|
+
termsOfServiceFilePath : this.config.termsOfServiceFilePath,
|
|
98
|
+
proofOfWorkChallengeNonceSeed : this.config.registrationProofOfWorkSeed,
|
|
99
|
+
proofOfWorkInitialMaximumAllowedHash : this.config.registrationProofOfWorkInitialMaxHash,
|
|
100
100
|
});
|
|
101
101
|
|
|
102
102
|
let eventStream: EventStream | undefined;
|
|
@@ -111,10 +111,10 @@ export class DwnServer {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
const dwnConfig = await getDwnConfig(this.config, {
|
|
114
|
-
didResolver: this.didResolver,
|
|
115
|
-
tenantGate: registrationManager,
|
|
114
|
+
didResolver : this.didResolver,
|
|
115
|
+
tenantGate : registrationManager,
|
|
116
116
|
eventStream,
|
|
117
|
-
})
|
|
117
|
+
});
|
|
118
118
|
this.dwn = await Dwn.create(dwnConfig);
|
|
119
119
|
}
|
|
120
120
|
|
|
@@ -123,12 +123,8 @@ export class DwnServer {
|
|
|
123
123
|
await this.#httpApi.start(this.config.port);
|
|
124
124
|
log.info(`HttpServer listening on port ${this.config.port}`);
|
|
125
125
|
|
|
126
|
-
this.#httpServerShutdownHandler = new HttpServerShutdownHandler(
|
|
127
|
-
this.#httpApi.server,
|
|
128
|
-
);
|
|
129
|
-
|
|
130
126
|
if (this.config.webSocketSupport) {
|
|
131
|
-
this.#wsApi = new WsApi(this.#httpApi
|
|
127
|
+
this.#wsApi = new WsApi(this.#httpApi, this.dwn);
|
|
132
128
|
this.#wsApi.start();
|
|
133
129
|
log.info('WebSocketServer ready...');
|
|
134
130
|
}
|
|
@@ -143,32 +139,23 @@ export class DwnServer {
|
|
|
143
139
|
}
|
|
144
140
|
|
|
145
141
|
await this.dwn.close();
|
|
146
|
-
await this.#httpApi.close();
|
|
147
142
|
|
|
148
143
|
// close WebSocket server if it was initialized
|
|
149
144
|
if (this.#wsApi !== undefined) {
|
|
150
145
|
await this.#wsApi.close();
|
|
151
146
|
}
|
|
152
147
|
|
|
153
|
-
await
|
|
154
|
-
this.#httpServerShutdownHandler.stop(() => {
|
|
155
|
-
resolve();
|
|
156
|
-
});
|
|
157
|
-
});
|
|
148
|
+
await this.#httpApi.close();
|
|
158
149
|
|
|
159
150
|
removeProcessHandlers(this.processHandlers);
|
|
160
151
|
|
|
161
152
|
this.serverState = DwnServerState.Stopped;
|
|
162
153
|
}
|
|
163
154
|
|
|
164
|
-
get httpServer(): Server {
|
|
155
|
+
get httpServer(): Server<WsData> {
|
|
165
156
|
return this.#httpApi.server;
|
|
166
157
|
}
|
|
167
158
|
|
|
168
|
-
get wsServer(): WebSocketServer | undefined {
|
|
169
|
-
return this.#wsApi?.server;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
159
|
/**
|
|
173
160
|
* Gets the RegistrationManager for testing purposes.
|
|
174
161
|
*/
|