@tak-ps/node-tak 0.3.0 → 0.3.2
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 +8 -0
- package/dist/index.js +173 -0
- package/dist/index.js.map +1 -0
- package/dist/test/default.test.js +8 -0
- package/dist/test/default.test.js.map +1 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/dist/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
import tls from 'node:tls';
|
|
3
|
+
import CoT from '@tak-ps/node-cot';
|
|
4
|
+
export default class TAK extends EventEmitter {
|
|
5
|
+
id;
|
|
6
|
+
type;
|
|
7
|
+
url;
|
|
8
|
+
auth;
|
|
9
|
+
open;
|
|
10
|
+
destroyed;
|
|
11
|
+
queue;
|
|
12
|
+
writing;
|
|
13
|
+
client;
|
|
14
|
+
version;
|
|
15
|
+
constructor(id, type, url, auth) {
|
|
16
|
+
super();
|
|
17
|
+
this.id = id;
|
|
18
|
+
this.type = type;
|
|
19
|
+
this.url = url;
|
|
20
|
+
this.auth = auth;
|
|
21
|
+
this.writing = false;
|
|
22
|
+
this.open = false;
|
|
23
|
+
this.destroyed = false;
|
|
24
|
+
this.queue = [];
|
|
25
|
+
}
|
|
26
|
+
static async connect(id, url, auth) {
|
|
27
|
+
const tak = new TAK(id, 'ssl', url, auth);
|
|
28
|
+
if (url.protocol === 'ssl:') {
|
|
29
|
+
if (!tak.auth.cert)
|
|
30
|
+
throw new Error('auth.cert required');
|
|
31
|
+
if (!tak.auth.key)
|
|
32
|
+
throw new Error('auth.key required');
|
|
33
|
+
return await tak.connect_ssl();
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
throw new Error('Unknown TAK Server Protocol');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
connect_ssl() {
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
this.client = tls.connect({
|
|
42
|
+
host: this.url.hostname,
|
|
43
|
+
port: parseInt(this.url.port),
|
|
44
|
+
rejectUnauthorized: false,
|
|
45
|
+
cert: this.auth.cert,
|
|
46
|
+
key: this.auth.key
|
|
47
|
+
});
|
|
48
|
+
this.client.setNoDelay();
|
|
49
|
+
this.client.on('connect', () => {
|
|
50
|
+
console.error(`ok - ${this.id} @ connect:${this.client ? this.client.authorized : 'NO CLIENT'} - ${this.client ? this.client.authorizationError : 'NO CLIENT'}`);
|
|
51
|
+
});
|
|
52
|
+
this.client.on('secureConnect', () => {
|
|
53
|
+
console.error(`ok - ${this.id} @ secure:${this.client ? this.client.authorized : 'NO CLIENT'} - ${this.client ? this.client.authorizationError : 'NO CLIENT'}`);
|
|
54
|
+
});
|
|
55
|
+
let buff = '';
|
|
56
|
+
this.client.on('data', (data) => {
|
|
57
|
+
// Eventually Parse ProtoBuf
|
|
58
|
+
buff = buff + data.toString();
|
|
59
|
+
let result = TAK.findCoT(buff);
|
|
60
|
+
while (result && result.event) {
|
|
61
|
+
const cot = new CoT(result.event);
|
|
62
|
+
try {
|
|
63
|
+
// @ts-ignore: Remove once we have JSONSchema => TS Defs for CoT Messages
|
|
64
|
+
if (cot.raw.event._attributes.type === 't-x-c-t-r') {
|
|
65
|
+
this.open = true;
|
|
66
|
+
this.emit('ping');
|
|
67
|
+
// @ts-ignore: Remove once we have JSONSchema => TS Defs for CoT Messages
|
|
68
|
+
}
|
|
69
|
+
else if (cot.raw.event._attributes.type === 't-x-takp-v') {
|
|
70
|
+
// @ts-ignore: Remove once we have JSONSchema => TS Defs for CoT Messages
|
|
71
|
+
this.version = cot.raw.event.detail.TakControl.TakServerVersionInfo._attributes.serverVersion;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
this.emit('cot', cot);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
console.error('Error parsing', e, data.toString());
|
|
79
|
+
}
|
|
80
|
+
buff = result.remainder;
|
|
81
|
+
result = TAK.findCoT(buff);
|
|
82
|
+
}
|
|
83
|
+
}).on('timeout', () => {
|
|
84
|
+
if (!this.destroyed)
|
|
85
|
+
this.emit('timeout');
|
|
86
|
+
}).on('error', (err) => {
|
|
87
|
+
this.emit('error', err);
|
|
88
|
+
}).on('end', () => {
|
|
89
|
+
this.open = false;
|
|
90
|
+
if (!this.destroyed)
|
|
91
|
+
this.emit('end');
|
|
92
|
+
});
|
|
93
|
+
setInterval(() => {
|
|
94
|
+
this.ping();
|
|
95
|
+
}, 5000);
|
|
96
|
+
return resolve(this);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async reconnect() {
|
|
100
|
+
return await this.connect_ssl();
|
|
101
|
+
}
|
|
102
|
+
destroy() {
|
|
103
|
+
this.destroyed = true;
|
|
104
|
+
if (this.client) {
|
|
105
|
+
this.client.destroy();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async ping() {
|
|
109
|
+
this.write([CoT.ping()]);
|
|
110
|
+
}
|
|
111
|
+
writer(body) {
|
|
112
|
+
return new Promise((resolve, reject) => {
|
|
113
|
+
if (!this.client)
|
|
114
|
+
return reject(new Error('A Connection Client must first be created before it can be written'));
|
|
115
|
+
const res = this.client.write(body + '\n', () => {
|
|
116
|
+
return resolve(res);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
async process() {
|
|
121
|
+
this.writing = true;
|
|
122
|
+
while (this.queue.length) {
|
|
123
|
+
const body = this.queue.shift();
|
|
124
|
+
if (!body)
|
|
125
|
+
continue;
|
|
126
|
+
await this.writer(body);
|
|
127
|
+
}
|
|
128
|
+
await this.writer('');
|
|
129
|
+
if (this.queue.length) {
|
|
130
|
+
process.nextTick(() => {
|
|
131
|
+
this.process();
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
this.writing = false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Write a CoT to the TAK Connection
|
|
140
|
+
*
|
|
141
|
+
* @param {CoT} cot CoT Object
|
|
142
|
+
*/
|
|
143
|
+
write(cots) {
|
|
144
|
+
for (const cot of cots) {
|
|
145
|
+
this.queue.push(cot.to_xml());
|
|
146
|
+
}
|
|
147
|
+
if (this.queue.length && !this.writing)
|
|
148
|
+
this.process();
|
|
149
|
+
}
|
|
150
|
+
write_xml(body) {
|
|
151
|
+
this.queue.push(body);
|
|
152
|
+
if (this.queue.length && !this.writing)
|
|
153
|
+
this.process();
|
|
154
|
+
}
|
|
155
|
+
// https://github.com/vidterra/multitak/blob/main/app/lib/helper.js#L4
|
|
156
|
+
static findCoT(str) {
|
|
157
|
+
/* eslint-disable no-control-regex */
|
|
158
|
+
str = str.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");
|
|
159
|
+
let match = str.match(/(<event.*?<\/event>)(.*)/); // find first CoT
|
|
160
|
+
if (!match) {
|
|
161
|
+
match = str.match(/(<event[^>]*\/>)(.*)/); // find first CoT
|
|
162
|
+
if (!match)
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
event: match[1],
|
|
167
|
+
remainder: match[2],
|
|
168
|
+
discard: match[0]
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
export { CoT };
|
|
173
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AACvC,OAAO,GAAG,MAAM,UAAU,CAAC;AAC3B,OAAO,GAAG,MAAM,kBAAkB,CAAC;AAcnC,MAAM,CAAC,OAAO,OAAO,GAAI,SAAQ,YAAY;IACzC,EAAE,CAAS;IACX,IAAI,CAAS;IACb,GAAG,CAAM;IACT,IAAI,CAAU;IACd,IAAI,CAAU;IACd,SAAS,CAAU;IACnB,KAAK,CAAW;IAChB,OAAO,C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,IAAI,CAAC;SAC3B;QAED,OAAO;YACH,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;YACf,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;YACnB,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;SACpB,CAAC;IACN,CAAC;CACJ;AAED,OAAO,EACH,GAAG,EACN,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default.test.js","sourceRoot":"","sources":["../../test/default.test.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,IAAI,CAAC,eAAe,EAAE,CAAC,CAAC,EAAE,EAAE;IACxB,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IACV,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IACV,CAAC,CAAC,GAAG,EAAE,CAAC;AACZ,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tak-ps/node-tak",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.2",
|
|
5
5
|
"description": "Lightweight JavaScript library for communicating with TAK Server",
|
|
6
6
|
"author": "Nick Ingalls <nick@ingalls.ca>",
|
|
7
7
|
"main": "dist/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"pretest": "npm run lint"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@tak-ps/node-cot": "^4.2.
|
|
17
|
+
"@tak-ps/node-cot": "^4.2.1",
|
|
18
18
|
"ajv": "^8.12.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|