@lionweb/delta-protocol-low-level-client-ws 0.7.0-beta.21
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 +6 -0
- package/README.md +22 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/low-level-client.d.ts +10 -0
- package/dist/low-level-client.d.ts.map +1 -0
- package/dist/low-level-client.js +90 -0
- package/dist/low-level-client.js.map +1 -0
- package/package.json +38 -0
- package/src/index.ts +19 -0
- package/src/low-level-client.ts +104 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# The `delta-protocol-low-level-client-ws` package
|
|
2
|
+
|
|
3
|
+
[
|
|
4
|
+
](./LICENSE)
|
|
5
|
+
|
|
6
|
+
An NPM package that can be added to a Node.js/NPM codebase as follows:
|
|
7
|
+
|
|
8
|
+
```shell
|
|
9
|
+
$ npm add @lionweb/delta-protocol-low-level-client-ws
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
***TODO***
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Development
|
|
16
|
+
|
|
17
|
+
Build this package from source as follows:
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
npm run build
|
|
21
|
+
```
|
|
22
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
export { createWSLowLevelClient } from "./low-level-client.js";
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,EAAE;AACF,iEAAiE;AACjE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,EAAE;AACF,sEAAsE;AACtE,sCAAsC;AAEtC,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LowLevelClient, LowLevelClientLogger, LowLevelClientParameters } from "@lionweb/delta-protocol-client";
|
|
2
|
+
/**
|
|
3
|
+
* @return a WebSocket-driven implementation of a {@link LowLevelClient low-level client},
|
|
4
|
+
* based on the `ws` package,
|
|
5
|
+
* as a {@link Promise} that resolves as soon the client was able to connect to the WebSocket server.
|
|
6
|
+
* @param lowLevelClientParameters Parameters required to create/instantiate the low-level client.
|
|
7
|
+
* @param optionalLoggingParameters Optional parameters regarding logging.
|
|
8
|
+
*/
|
|
9
|
+
export declare const createWSLowLevelClient: <TMessageForClient, TMessageToServer>({ url, clientId, receiveMessageOnClient }: LowLevelClientParameters<TMessageForClient>, optionalLogger?: LowLevelClientLogger<TMessageForClient, TMessageToServer>) => Promise<LowLevelClient<TMessageToServer>>;
|
|
10
|
+
//# sourceMappingURL=low-level-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"low-level-client.d.ts","sourceRoot":"","sources":["../src/low-level-client.ts"],"names":[],"mappings":"AAiBA,OAAO,EACH,cAAc,EACd,oBAAoB,EACpB,wBAAwB,EAE3B,MAAM,gCAAgC,CAAA;AAQvC;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,GAAU,iBAAiB,EAAE,gBAAgB,EAC5E,2CAA2C,wBAAwB,CAAC,iBAAiB,CAAC,EACtF,iBAAiB,oBAAoB,CAAC,iBAAiB,EAAE,gBAAgB,CAAC,KAC3E,OAAO,CAAC,cAAc,CAAC,gBAAgB,CAAC,CAuD1C,CAAA"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
import { noOpLowLevelClientLogger } from "@lionweb/delta-protocol-client";
|
|
18
|
+
import { tryParseJson, wrappedAsPromise } from "@lionweb/delta-protocol-common";
|
|
19
|
+
import { asMinimalJsonString } from "@lionweb/ts-utils";
|
|
20
|
+
import { WebSocket } from "ws";
|
|
21
|
+
/**
|
|
22
|
+
* @return a WebSocket-driven implementation of a {@link LowLevelClient low-level client},
|
|
23
|
+
* based on the `ws` package,
|
|
24
|
+
* as a {@link Promise} that resolves as soon the client was able to connect to the WebSocket server.
|
|
25
|
+
* @param lowLevelClientParameters Parameters required to create/instantiate the low-level client.
|
|
26
|
+
* @param optionalLoggingParameters Optional parameters regarding logging.
|
|
27
|
+
*/
|
|
28
|
+
export const createWSLowLevelClient = async ({ url, clientId, receiveMessageOnClient }, optionalLogger) => {
|
|
29
|
+
const webSocket = new WebSocket(url);
|
|
30
|
+
const log = optionalLogger ?? noOpLowLevelClientLogger;
|
|
31
|
+
log({ message: `client ${clientId} started` });
|
|
32
|
+
let state = "connecting";
|
|
33
|
+
return new Promise((resolveClientStart, rejectClientStart) => {
|
|
34
|
+
const lowLevelWebSocketClient = {
|
|
35
|
+
sendMessage: (message) => {
|
|
36
|
+
if (state === "connected") {
|
|
37
|
+
log({ sentToServer: message });
|
|
38
|
+
return wrappedAsPromise((callback) => {
|
|
39
|
+
webSocket.send(asMinimalJsonString(message), callback);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const messageText = `can't send message to server when client's state=${state}`;
|
|
44
|
+
log({ message: messageText });
|
|
45
|
+
return Promise.reject(new Error(messageText));
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
disconnect: () => new Promise((resolveDisconnect, rejectDisconnect) => {
|
|
49
|
+
if (state === "connected") {
|
|
50
|
+
webSocket.close();
|
|
51
|
+
state = "disconnected";
|
|
52
|
+
resolveDisconnect();
|
|
53
|
+
}
|
|
54
|
+
rejectDisconnect(new Error(state));
|
|
55
|
+
})
|
|
56
|
+
};
|
|
57
|
+
webSocket
|
|
58
|
+
.on("open", () => {
|
|
59
|
+
state = "connected";
|
|
60
|
+
log({ message: `connected to server` });
|
|
61
|
+
resolveClientStart(lowLevelWebSocketClient);
|
|
62
|
+
})
|
|
63
|
+
.on("message", (messageText) => {
|
|
64
|
+
const message = tryParseJson(messageText, (message) => log({ message }));
|
|
65
|
+
log({ receivedOnClient: message });
|
|
66
|
+
receiveMessageOnClient(message);
|
|
67
|
+
})
|
|
68
|
+
.on("error", (error) => {
|
|
69
|
+
log({ message: `error occurred: ${error}`, error: true });
|
|
70
|
+
if (isConnectionRefusedError(firstRealError(error))) {
|
|
71
|
+
if (state === "connecting") {
|
|
72
|
+
rejectClientStart(new Error(`could not connect to WebSocket server at ${url}`));
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
// ignore
|
|
76
|
+
// TODO is that OK?
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
.on("close", () => {
|
|
81
|
+
state = "disconnected";
|
|
82
|
+
log({ message: `disconnected from server` });
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
const firstRealError = (error) => error instanceof AggregateError ? firstRealError(error.errors[0]) : error;
|
|
87
|
+
const isConnectionRefusedError = (error) =>
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
+
error.code === "ECONNREFUSED";
|
|
90
|
+
//# sourceMappingURL=low-level-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"low-level-client.js","sourceRoot":"","sources":["../src/low-level-client.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,EAAE;AACF,iEAAiE;AACjE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,EAAE;AACF,sEAAsE;AACtE,sCAAsC;AAEtC,OAAO,EAIH,wBAAwB,EAC3B,MAAM,gCAAgC,CAAA;AACvC,OAAO,EAAE,Y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,OAAO,EAAE,GAAG,EAAE;YACd,KAAK,GAAG,cAAc,CAAA;YACtB,GAAG,CAAC,EAAE,OAAO,EAAE,0BAA0B,EAAE,CAAC,CAAA;QAChD,CAAC,CAAC,CAAA;IACV,CAAC,CAAC,CAAA;AACN,CAAC,CAAA;AAED,MAAM,cAAc,GAAG,CAAC,KAAY,EAAS,EAAE,CAC3C,KAAK,YAAY,cAAc,CAAC,CAAC,CAAC,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;AAE7E,MAAM,wBAAwB,GAAG,CAAC,KAAY,EAAE,EAAE;AAC9C,8DAA8D;AAC7D,KAAa,CAAC,IAAI,KAAK,cAAc,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lionweb/delta-protocol-low-level-client-ws",
|
|
3
|
+
"version": "0.7.0-beta.21",
|
|
4
|
+
"description": "Implementation of a low-level client for a delta protocol client using the WebSocket protocol, relying on the 'ws' package",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"typings": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/LionWeb-io/lionweb-typescript.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/LionWeb-io/lionweb-typescript/issues"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"clean": "npx rimraf dist node_modules",
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"lint": "eslint src",
|
|
21
|
+
"prep:pre-release": "npm run clean && npm install && npm run build",
|
|
22
|
+
"prerelease-alpha": "npm run prep:pre-release",
|
|
23
|
+
"release-alpha": "npm publish --tag alpha",
|
|
24
|
+
"prerelease-beta": "npm run prep:pre-release",
|
|
25
|
+
"release-beta": "npm publish --tag beta",
|
|
26
|
+
"prerelease": "npm run prep:pre-release",
|
|
27
|
+
"release": "npm publish"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@lionweb/delta-protocol-common": "0.7.0-beta.21",
|
|
31
|
+
"@lionweb/delta-protocol-client": "0.7.0-beta.21",
|
|
32
|
+
"@lionweb/ts-utils": "0.7.0-beta.21",
|
|
33
|
+
"ws": "8.18.2"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/ws": "8.18.1"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
export { createWSLowLevelClient } from "./low-level-client.js"
|
|
19
|
+
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
LowLevelClient,
|
|
20
|
+
LowLevelClientLogger,
|
|
21
|
+
LowLevelClientParameters,
|
|
22
|
+
noOpLowLevelClientLogger
|
|
23
|
+
} from "@lionweb/delta-protocol-client"
|
|
24
|
+
import { tryParseJson, wrappedAsPromise } from "@lionweb/delta-protocol-common"
|
|
25
|
+
import { asMinimalJsonString } from "@lionweb/ts-utils"
|
|
26
|
+
import { WebSocket } from "ws"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
type ClientState = "connecting" | "connected" | "disconnected"
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @return a WebSocket-driven implementation of a {@link LowLevelClient low-level client},
|
|
33
|
+
* based on the `ws` package,
|
|
34
|
+
* as a {@link Promise} that resolves as soon the client was able to connect to the WebSocket server.
|
|
35
|
+
* @param lowLevelClientParameters Parameters required to create/instantiate the low-level client.
|
|
36
|
+
* @param optionalLoggingParameters Optional parameters regarding logging.
|
|
37
|
+
*/
|
|
38
|
+
export const createWSLowLevelClient = async <TMessageForClient, TMessageToServer>(
|
|
39
|
+
{ url, clientId, receiveMessageOnClient }: LowLevelClientParameters<TMessageForClient>,
|
|
40
|
+
optionalLogger?: LowLevelClientLogger<TMessageForClient, TMessageToServer>
|
|
41
|
+
): Promise<LowLevelClient<TMessageToServer>> => {
|
|
42
|
+
const webSocket = new WebSocket(url)
|
|
43
|
+
const log = optionalLogger ?? noOpLowLevelClientLogger
|
|
44
|
+
log({ message: `client ${clientId} started` })
|
|
45
|
+
let state: ClientState = "connecting"
|
|
46
|
+
return new Promise((resolveClientStart, rejectClientStart) => {
|
|
47
|
+
const lowLevelWebSocketClient: LowLevelClient<TMessageToServer> = {
|
|
48
|
+
sendMessage: (message) => {
|
|
49
|
+
if (state === "connected") {
|
|
50
|
+
log({ sentToServer: message })
|
|
51
|
+
return wrappedAsPromise((callback) => {
|
|
52
|
+
webSocket.send(asMinimalJsonString(message), callback)
|
|
53
|
+
})
|
|
54
|
+
} else {
|
|
55
|
+
const messageText = `can't send message to server when client's state=${state}`
|
|
56
|
+
log({ message: messageText })
|
|
57
|
+
return Promise.reject(new Error(messageText))
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
disconnect: () => new Promise((resolveDisconnect, rejectDisconnect) => {
|
|
61
|
+
if (state === "connected") {
|
|
62
|
+
webSocket.close()
|
|
63
|
+
state = "disconnected"
|
|
64
|
+
resolveDisconnect()
|
|
65
|
+
}
|
|
66
|
+
rejectDisconnect(new Error(state))
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
webSocket
|
|
70
|
+
.on("open", () => {
|
|
71
|
+
state = "connected"
|
|
72
|
+
log({ message: `connected to server` })
|
|
73
|
+
resolveClientStart(lowLevelWebSocketClient)
|
|
74
|
+
})
|
|
75
|
+
.on("message", (messageText: string) => {
|
|
76
|
+
const message = tryParseJson(messageText, (message) => log({ message })) as TMessageForClient
|
|
77
|
+
log({ receivedOnClient: message })
|
|
78
|
+
receiveMessageOnClient(message)
|
|
79
|
+
})
|
|
80
|
+
.on("error", (error) => {
|
|
81
|
+
log({ message: `error occurred: ${error}`, error: true })
|
|
82
|
+
if (isConnectionRefusedError(firstRealError(error))) {
|
|
83
|
+
if (state === "connecting") {
|
|
84
|
+
rejectClientStart(new Error(`could not connect to WebSocket server at ${url}`))
|
|
85
|
+
} else {
|
|
86
|
+
// ignore
|
|
87
|
+
// TODO is that OK?
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
.on("close", () => {
|
|
92
|
+
state = "disconnected"
|
|
93
|
+
log({ message: `disconnected from server` })
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const firstRealError = (error: Error): Error =>
|
|
99
|
+
error instanceof AggregateError ? firstRealError(error.errors[0]) : error
|
|
100
|
+
|
|
101
|
+
const isConnectionRefusedError = (error: Error) =>
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
103
|
+
(error as any).code === "ECONNREFUSED"
|
|
104
|
+
|