timing-provider-server 2.0.13

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Christoph Guttandin
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,44 @@
1
+ # timing-provider-server
2
+
3
+ **A command line tool to spin up a server which can be used with the timing-provider.**
4
+
5
+ [![dependencies](https://img.shields.io/david/chrisguttandin/timing-provider-server.svg?style=flat-square)](https://github.com/chrisguttandin/timing-provider-server/network/dependencies)
6
+ [![version](https://img.shields.io/npm/v/timing-provider-server.svg?style=flat-square)](https://www.npmjs.com/package/timing-provider-server)
7
+
8
+ This command line tool spins up a server which can be used as the signaling server for the [timing-provider](https://github.com/chrisguttandin/timing-provider).
9
+
10
+ ## Usage
11
+
12
+ The timing-provider-server package is published on [npm](https://www.npmjs.com/package/timing-provider-server). It can be installed like this:
13
+
14
+ ```shell
15
+ npm install timing-provider-server
16
+ ```
17
+
18
+ Afterwards the server can be started by executing the following command:
19
+
20
+ ```shell
21
+ timing-provider-server
22
+ ```
23
+
24
+ It is also possible to execute the command above without installing a local version of this package like this:
25
+
26
+ ```shell
27
+ npx timing-provider-server
28
+ ```
29
+
30
+ Once the server is up and running it can be used when creating a new `TimingProvider`.
31
+
32
+ ```js
33
+ const timingProvider = new TimingProvider('ws://localhost:2276');
34
+ ```
35
+
36
+ ## Arguments
37
+
38
+ ### --port
39
+
40
+ This option can be used to specify the port to which the server is listening. If this argument is not used 2276 will be used as the default port.
41
+
42
+ ```shell
43
+ timing-provider-server --port 4567
44
+ ```
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=app.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../../src/app.ts"],"names":[],"mappings":""}
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env node
2
+ import { generateUniqueNumber } from 'fast-unique-numbers';
3
+ import { Server } from 'ws';
4
+ import yargs from 'yargs';
5
+ if (require.main !== module) {
6
+ throw new Error('This script is meant to be executed from the command line.');
7
+ }
8
+ (async () => {
9
+ const commandLineArguments = yargs
10
+ .help()
11
+ .option('port', {
12
+ default: 2276,
13
+ describe: 'specify the port at which the server will be listening',
14
+ type: 'number'
15
+ })
16
+ .strict().argv;
17
+ if (commandLineArguments instanceof Promise) {
18
+ throw new Error('The command line arguments are expected to get parsed synchronously.');
19
+ }
20
+ const { port } = commandLineArguments;
21
+ const activeConnections = new Map();
22
+ const server = new Server({ port });
23
+ server.on('connection', (connection) => {
24
+ const id = generateUniqueNumber(activeConnections);
25
+ const sendMessage = (message) => connection.send(JSON.stringify(message));
26
+ activeConnections.set(id, sendMessage);
27
+ connection.on('message', (message) => {
28
+ const parsedMessage = JSON.parse(message.toString());
29
+ const sendMessageToActiveConnection = activeConnections.get(parsedMessage.client.id);
30
+ if (sendMessageToActiveConnection !== undefined) {
31
+ sendMessageToActiveConnection({ client: { id }, message: parsedMessage.message });
32
+ }
33
+ });
34
+ connection.on('close', () => activeConnections.delete(id));
35
+ for (const activeConnection of activeConnections) {
36
+ if (activeConnection[0] !== id) {
37
+ const label = [id, activeConnection[0]].sort().join('-');
38
+ const type = 'request';
39
+ sendMessage({ message: { isActive: true, label, mask: { client: { id: activeConnection[0] } } }, type });
40
+ activeConnection[1]({ message: { isActive: false, mask: { client: { id } } }, type });
41
+ }
42
+ }
43
+ });
44
+ })();
45
+ //# sourceMappingURL=app.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app.js","sourceRoot":"","sources":["../../src/app.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE;IACzB,MAAM,IAAI,KAAK,CAAC,4DAA4D,CAAC,CAAC;CACjF;AAED,CAAC,KAAK,IAAI,EAAE;IACR,MAAM,oBAAoB,GAAuC,KAAM;SAClE,IAAI,EAAE;SACN,MAAM,CAAC,MAAM,EAAE;QACZ,OAAO,EAAE,IAAI;QACb,QAAQ,EAAE,wDAAwD;QAClE,IAAI,EAAE,QAAQ;KACjB,CAAC;SACD,MAAM,EAAE,CAAC,IAAI,CAAC;IAEnB,IAAI,oBAAoB,YAAY,OAAO,EAAE;QACzC,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAC;KAC3F;IAED,MAAM,EAAE,IAAI,EAAE,GAAG,oBAAoB,CAAC;IACtC,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAqC,CAAC;IACvE,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IAEp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AE,EAAE,EAAE,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;gBACzG,gBAAgB,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;aACzF;SACJ;IACL,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,EAAE,CAAC"}
@@ -0,0 +1,4 @@
1
+ export interface ICommandLineArguments {
2
+ port: number;
3
+ }
4
+ //# sourceMappingURL=command-line-arguments.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-line-arguments.d.ts","sourceRoot":"","sources":["../../../src/interfaces/command-line-arguments.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IAClC,IAAI,EAAE,MAAM,CAAC;CAChB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=command-line-arguments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-line-arguments.js","sourceRoot":"","sources":["../../../src/interfaces/command-line-arguments.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export * from './command-line-arguments';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './command-line-arguments';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC"}
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ var _fastUniqueNumbers = require("fast-unique-numbers");
5
+
6
+ var _ws = require("ws");
7
+
8
+ var _yargs = _interopRequireDefault(require("yargs"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ if (require.main !== module) {
13
+ throw new Error('This script is meant to be executed from the command line.');
14
+ }
15
+
16
+ (async () => {
17
+ const commandLineArguments = _yargs.default.help().option('port', {
18
+ default: 2276,
19
+ describe: 'specify the port at which the server will be listening',
20
+ type: 'number'
21
+ }).strict().argv;
22
+
23
+ if (commandLineArguments instanceof Promise) {
24
+ throw new Error('The command line arguments are expected to get parsed synchronously.');
25
+ }
26
+
27
+ const {
28
+ port
29
+ } = commandLineArguments;
30
+ const activeConnections = new Map();
31
+ const server = new _ws.Server({
32
+ port
33
+ });
34
+ server.on('connection', connection => {
35
+ const id = (0, _fastUniqueNumbers.generateUniqueNumber)(activeConnections);
36
+
37
+ const sendMessage = message => connection.send(JSON.stringify(message));
38
+
39
+ activeConnections.set(id, sendMessage);
40
+ connection.on('message', message => {
41
+ const parsedMessage = JSON.parse(message.toString());
42
+ const sendMessageToActiveConnection = activeConnections.get(parsedMessage.client.id);
43
+
44
+ if (sendMessageToActiveConnection !== undefined) {
45
+ sendMessageToActiveConnection({
46
+ client: {
47
+ id
48
+ },
49
+ message: parsedMessage.message
50
+ });
51
+ }
52
+ });
53
+ connection.on('close', () => activeConnections.delete(id));
54
+
55
+ for (const activeConnection of activeConnections) {
56
+ if (activeConnection[0] !== id) {
57
+ const label = [id, activeConnection[0]].sort().join('-');
58
+ const type = 'request';
59
+ sendMessage({
60
+ message: {
61
+ isActive: true,
62
+ label,
63
+ mask: {
64
+ client: {
65
+ id: activeConnection[0]
66
+ }
67
+ }
68
+ },
69
+ type
70
+ });
71
+ activeConnection[1]({
72
+ message: {
73
+ isActive: false,
74
+ mask: {
75
+ client: {
76
+ id
77
+ }
78
+ }
79
+ },
80
+ type
81
+ });
82
+ }
83
+ }
84
+ });
85
+ })();
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _commandLineArguments = require("./command-line-arguments");
8
+
9
+ Object.keys(_commandLineArguments).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _commandLineArguments[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _commandLineArguments[key];
16
+ }
17
+ });
18
+ });
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "author": "Christoph Guttandin",
3
+ "bin": {
4
+ "timing-provider-server": "./build/node/app.js"
5
+ },
6
+ "bugs": {
7
+ "url": "https://github.com/chrisguttandin/timing-provider-server/issues"
8
+ },
9
+ "config": {
10
+ "commitizen": {
11
+ "path": "cz-conventional-changelog"
12
+ }
13
+ },
14
+ "dependencies": {
15
+ "fast-unique-numbers": "^6.0.6",
16
+ "tslib": "^2.3.1",
17
+ "typescript": "^4.4.3",
18
+ "ws": "^8.2.2",
19
+ "yargs": "^17.1.1"
20
+ },
21
+ "description": "A command line tool to spin up a server which can be used with the timing-provider.",
22
+ "devDependencies": {
23
+ "@babel/core": "^7.15.5",
24
+ "@babel/preset-env": "^7.15.6",
25
+ "@babel/register": "^7.15.3",
26
+ "@commitlint/cli": "^13.1.0",
27
+ "@commitlint/config-angular": "^13.1.0",
28
+ "@types/ws": "^7.4.7",
29
+ "@types/yargs": "^17.0.2",
30
+ "chai": "^4.3.4",
31
+ "commitizen": "^4.2.4",
32
+ "cz-conventional-changelog": "^3.3.0",
33
+ "eslint": "^7.32.0",
34
+ "eslint-config-holy-grail": "^50.1.8",
35
+ "grunt": "^1.4.1",
36
+ "grunt-babel": "^8.0.0",
37
+ "grunt-cli": "^1.4.3",
38
+ "grunt-contrib-clean": "^2.0.0",
39
+ "grunt-sh": "^0.2.0",
40
+ "husky": "^7.0.2",
41
+ "load-grunt-config": "^4.0.1",
42
+ "mocha": "^9.1.1",
43
+ "prettier": "^2.4.1",
44
+ "pretty-quick": "^3.1.1",
45
+ "sinon": "^11.1.2",
46
+ "sinon-chai": "^3.7.0",
47
+ "tsconfig-holy-grail": "^11.1.12",
48
+ "tslint": "^6.1.3",
49
+ "tslint-config-holy-grail": "^53.2.25"
50
+ },
51
+ "engines": {
52
+ "node": ">=12.20.1"
53
+ },
54
+ "files": [
55
+ "build/es2019/",
56
+ "build/node/",
57
+ "src/"
58
+ ],
59
+ "homepage": "https://github.com/chrisguttandin/timing-provider-server",
60
+ "keywords": [
61
+ "Timing Object",
62
+ "Timing Provider",
63
+ "synchronisation",
64
+ "timing",
65
+ "timingsrc",
66
+ "webtiming"
67
+ ],
68
+ "license": "MIT",
69
+ "name": "timing-provider-server",
70
+ "repository": {
71
+ "type": "git",
72
+ "url": "https://github.com/chrisguttandin/timing-provider-server.git"
73
+ },
74
+ "scripts": {
75
+ "prepare": "husky install",
76
+ "prepublishOnly": "grunt build",
77
+ "start": "node build/node/app.js",
78
+ "test": "grunt test"
79
+ },
80
+ "version": "2.0.13"
81
+ }
package/src/app.ts ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { generateUniqueNumber } from 'fast-unique-numbers';
4
+ import { Server } from 'ws';
5
+ import yargs from 'yargs';
6
+ import { ICommandLineArguments } from './interfaces';
7
+
8
+ if (require.main !== module) {
9
+ throw new Error('This script is meant to be executed from the command line.');
10
+ }
11
+
12
+ (async () => {
13
+ const commandLineArguments = (<yargs.Argv<ICommandLineArguments>>yargs)
14
+ .help()
15
+ .option('port', {
16
+ default: 2276,
17
+ describe: 'specify the port at which the server will be listening',
18
+ type: 'number'
19
+ })
20
+ .strict().argv;
21
+
22
+ if (commandLineArguments instanceof Promise) {
23
+ throw new Error('The command line arguments are expected to get parsed synchronously.');
24
+ }
25
+
26
+ const { port } = commandLineArguments;
27
+ const activeConnections = new Map<number, (message: object) => void>();
28
+ const server = new Server({ port });
29
+
30
+ server.on('connection', (connection) => {
31
+ const id = generateUniqueNumber(activeConnections);
32
+ const sendMessage = (message: object) => connection.send(JSON.stringify(message));
33
+
34
+ activeConnections.set(id, sendMessage);
35
+
36
+ connection.on('message', (message) => {
37
+ const parsedMessage = JSON.parse(message.toString());
38
+
39
+ const sendMessageToActiveConnection = activeConnections.get(parsedMessage.client.id);
40
+
41
+ if (sendMessageToActiveConnection !== undefined) {
42
+ sendMessageToActiveConnection({ client: { id }, message: parsedMessage.message });
43
+ }
44
+ });
45
+
46
+ connection.on('close', () => activeConnections.delete(id));
47
+
48
+ for (const activeConnection of activeConnections) {
49
+ if (activeConnection[0] !== id) {
50
+ const label = [id, activeConnection[0]].sort().join('-');
51
+ const type = 'request';
52
+
53
+ sendMessage({ message: { isActive: true, label, mask: { client: { id: activeConnection[0] } } }, type });
54
+ activeConnection[1]({ message: { isActive: false, mask: { client: { id } } }, type });
55
+ }
56
+ }
57
+ });
58
+ })();
@@ -0,0 +1,3 @@
1
+ export interface ICommandLineArguments {
2
+ port: number;
3
+ }
@@ -0,0 +1 @@
1
+ export * from './command-line-arguments';
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "tsconfig-holy-grail/src/tsconfig-node"
3
+ }