libshv-js 3.0.0

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.
@@ -0,0 +1,33 @@
1
+ # vim: sw=2
2
+ name: Lint
3
+
4
+ concurrency:
5
+ group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
6
+ cancel-in-progress: true
7
+
8
+ on:
9
+ push:
10
+ branches: [ "main" ]
11
+ pull_request:
12
+ branches: [ "main" ]
13
+
14
+ jobs:
15
+ type-check:
16
+ name: Type check
17
+ runs-on: ubuntu-22.04
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+ - name: Install tsc
21
+ run: npm install
22
+ - name: Run tsc
23
+ run: npx tsc --noEmit
24
+
25
+ xo:
26
+ name: xo
27
+ runs-on: ubuntu-22.04
28
+ steps:
29
+ - uses: actions/checkout@v3
30
+ - name: Install tsc
31
+ run: npm install
32
+ - name: Run xo
33
+ run: npx xo --reporter unix src/* test.ts
@@ -0,0 +1,25 @@
1
+ # vim: sw=2
2
+ name: Test
3
+
4
+ concurrency:
5
+ group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
6
+ cancel-in-progress: true
7
+
8
+ on:
9
+ push:
10
+ branches: [ "main" ]
11
+ pull_request:
12
+ branches: [ "main" ]
13
+
14
+ jobs:
15
+ test:
16
+ name: CPON/Chainpack conversions
17
+ runs-on: ubuntu-22.04
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+ - uses: denoland/setup-deno@v1
21
+ with:
22
+ deno-version: vx.x.x
23
+
24
+ - name: Run tests
25
+ run: deno run test.ts
@@ -0,0 +1,33 @@
1
+ {
2
+ "space": 4,
3
+ "rules": {
4
+ "@typescript-eslint/no-unsafe-argument": "off",
5
+ "@typescript-eslint/no-unsafe-assignment": "off",
6
+ "@typescript-eslint/no-unsafe-call": "off",
7
+ "@typescript-eslint/no-unsafe-return": "off",
8
+
9
+ "@typescript-eslint/naming-convention": "off",
10
+ "@typescript-eslint/prefer-promise-reject-errors": "off",
11
+ "@typescript-eslint/promise-function-async": "off",
12
+ "capitalized-comments": "off",
13
+ "max-depth": "off",
14
+ "@typescript-eslint/padding-line-between-statements": "off",
15
+ "no-constant-condition": ["error", {
16
+ "checkLoops": false
17
+ }],
18
+ "complexity": "off",
19
+ "no-bitwise": "off",
20
+ "object-curly-newline": ["error", {
21
+ "ImportDeclaration": "never"
22
+ }],
23
+ "n/file-extension-in-import": "off",
24
+ "promise-function-async": "off",
25
+ "default-case": "off",
26
+ "prefer-promise-reject-errors": "off",
27
+ "eslint-disable-next-line prefer-promise-reject-errors": "off",
28
+ "unicorn/explicit-length-check": "off",
29
+ "unicorn/no-negated-condition": "off",
30
+ "unicorn/switch-case-braces": "off",
31
+ "unicorn/prevent-abbreviations": "off"
32
+ }
33
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 silicon-heaven
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # libshv-js
2
+ JavaScript implementation of SHV RPC
package/deno.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": ["dom", "dom.iterable", "deno.window"]
4
+ },
5
+ "tasks": {
6
+ "lint": "deno run --allow-read --allow-write --allow-net --allow-sys --allow-env npm:xo --reporter unix src/*"
7
+ }
8
+ }
@@ -0,0 +1,44 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>WebSocketoSaurus</title>
5
+ <meta charset="utf-8">
6
+ <script type="text/javascript" src="../rpcvalue.js"></script>
7
+ <script type="text/javascript" src="../cpcontext.js"></script>
8
+ <script type="text/javascript" src="../chainpack.js"></script>
9
+ <script type="text/javascript" src="../cpon.js"></script>
10
+ <script type="text/javascript" src="../bint.js"></script>
11
+ <script type="text/javascript" src="../rpcmessage.js"></script>
12
+ <script type="text/javascript" src="../ws_client.js"></script>
13
+ </head>
14
+ <body>
15
+ <h1>WebSocketoSaurus</h1>
16
+ <h2>WebSocket Shv Broker test Client</h1>
17
+ <p>
18
+ Uri <input type="text" id="edUri" style="width:50em;"/>
19
+ </p>
20
+ <p>
21
+ user <input type="text" id="edUser"/>
22
+ password <input type="password" id="edPassword"/>
23
+ </p>
24
+ <p>
25
+ <button onClick="initWebSocket(true);">Connect ChainPack</button>
26
+ <button onClick="initWebSocket(false);">Connect Cpon</button>
27
+ <button onClick="stopWebSocket();">Disconnect</button>
28
+ </p>
29
+ <p>
30
+ <input type="text" id="edShvPath" onkeydown="if(event.keyCode==13)sendMessage();"/>
31
+ <input type="text" id="edMethod" onkeydown="if(event.keyCode==13)sendMessage();"/>
32
+ <input type="text" id="edParams" onkeydown="if(event.keyCode==13)sendMessage();"/>
33
+ <button onClick="sendMessage();">Send</button>
34
+ </p>
35
+ <p>
36
+ <textarea id="txtResult" style="width:50em;height:100px;"></textarea>
37
+ </p>
38
+ <p>
39
+ <textarea id="txtLog" style="width:50em;height:200px;"></textarea>
40
+ </p>
41
+
42
+ <script type="text/javascript" src="websocketosaurus.js" ></script>
43
+ </body>
44
+ </html>
@@ -0,0 +1,94 @@
1
+ //document.getElementById("edUri").value = "wss://nirvana.elektroline.cz:3778";
2
+ document.getElementById("edUri").value = "wss://localhost:3778";
3
+ document.getElementById("edUser").value = "user";
4
+ document.getElementById("edPassword").value = "pwd";
5
+
6
+ document.getElementById("edShvPath").value = ".broker/app";
7
+ document.getElementById("edMethod").value = "echo";
8
+ document.getElementById("edParams").value = "42";
9
+
10
+ let txtLog = document.getElementById("txtLog");
11
+
12
+ const debug = (...args) => {
13
+ let line = "";
14
+ for (let i = 0; i < args.length; i++) {
15
+ if (i > 0) {
16
+ line += " ";
17
+ }
18
+ line += args[i];
19
+ }
20
+ txtLog.value += line + "\n";
21
+ txtLog.scrollTop = txtLog.scrollHeight;
22
+ };
23
+
24
+ let ws_client;
25
+
26
+ const sendMessage = () => {
27
+ if (ws_client && ws_client.websocket.readyState == 1 ) {
28
+ const shv_path = document.getElementById("edShvPath").value;
29
+ const method = document.getElementById("edMethod").value;
30
+ const params = document.getElementById("edParams").value;
31
+
32
+ ws_client.callRpcMethod(shv_path, method, params).then((rpc_msg) => {
33
+ const txtResult = document.getElementById("txtResult");
34
+ // result == 2
35
+ // error == 3
36
+ const err = rpc_msg.error();
37
+ if (err) {
38
+ txtResult.value = err.toString();
39
+ txtResult.style.background = "salmon";
40
+ } else {
41
+ const result = rpc_msg.result();
42
+ txtResult.value = result.toString();
43
+ txtResult.style.background = "";
44
+ }
45
+ });
46
+ }
47
+ }
48
+
49
+ const initWebSocket = (use_chainpack) => {
50
+ const wsUri = document.getElementById("edUri").value;
51
+ const user = document.getElementById("edUser").value;
52
+ const password = document.getElementById("edPassword").value;
53
+
54
+
55
+ const isUseCpon = !use_chainpack;
56
+ const txtLog = document.getElementById("txtLog");
57
+ txtLog.value = `Connection to shvbroker using ${isUseCpon? "Cpon": "ChainPack"}serialization.`;
58
+ try {
59
+ ws_client = new WsClient({
60
+ user,
61
+ password,
62
+ wsUri,
63
+ logDebug: debug,
64
+ onRequest: (rpc_msg) => {
65
+ const method = rpc_msg.method().asString();
66
+ const resp = new RpcMessage();
67
+ if(method == "dir") {
68
+ resp.setResult(["ls", "dir", "appName"]);
69
+ }
70
+ else if(method == "ls") {
71
+ resp.setResult([]);
72
+ }
73
+ else if(method == "appName") {
74
+ resp.setResult("websocketosaurus");
75
+ }
76
+ else {
77
+ debug(`ERROR: Method: ${method} is not defined.`);
78
+ resp.setError(`Method: ${method} is not defined.`);
79
+ }
80
+ resp.setRequestId(rpc_msg.requestId());
81
+ resp.setCallerIds(rpc_msg.callerIds());
82
+ ws_client.sendRpcMessage(resp);
83
+ }
84
+ });
85
+ } catch (exception) {
86
+ debug(`EXCEPTION: ${exception}`);
87
+ }
88
+ };
89
+
90
+ const stopWebSocket = () => {
91
+ if (websocket) {
92
+ websocket.close();
93
+ }
94
+ };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "libshv-js",
3
+ "version": "3.0.0",
4
+ "description": "Typescript implementation of libshv",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "author": "Elektroline",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/silicon-heaven/libshv-js.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/silicon-heaven/libshv-js/issues"
15
+ },
16
+ "homepage": "https://github.com/silicon-heaven/libshv-js",
17
+ "exports": {
18
+ "./bint.ts": "./src/bint.ts",
19
+ "./chainpack.ts": "./src/chainpack.ts",
20
+ "./cpcontext.ts": "./src/cpcontext.ts",
21
+ "./cpon.ts": "./src/cpon.ts",
22
+ "./rpcmessage.ts": "./src/rpcmessage.ts",
23
+ "./rpcvalue.ts": "./src/rpcvalue.ts",
24
+ "./ws-client.ts": "./src/ws-client.ts"
25
+ },
26
+ "devDependencies": {
27
+ "typescript": "^5.5.4",
28
+ "xo": "^0.59.3"
29
+ }
30
+ }