@tak-ps/node-cot 2.3.0 → 2.5.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,29 @@
1
+ name: NPM Release
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - '*'
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v3
13
+
14
+ - name: Get tag
15
+ id: tag
16
+ uses: dawidd6/action-get-tag@v1
17
+
18
+ - uses: actions/setup-node@v3
19
+ with:
20
+ node-version: 18
21
+ registry-url: https://registry.npmjs.org/
22
+
23
+ - name: npm install
24
+ run: npm install
25
+
26
+ - name: npm publish
27
+ run: npm publish
28
+ env:
29
+ NPM_TOKEN: ${{ secrets.NPM_SECRET }}
@@ -1,6 +1,9 @@
1
1
  name: Test
2
2
 
3
3
  on:
4
+ push:
5
+ branches:
6
+ - main
4
7
  pull_request:
5
8
  types:
6
9
  - opened
@@ -25,8 +28,8 @@ jobs:
25
28
  - name: Install
26
29
  run: npm install
27
30
 
28
- - name: Test
29
- run: npm test
30
-
31
31
  - name: Lint
32
32
  run: npm run lint
33
+
34
+ - name: Test
35
+ run: npm test
package/CHANGELOG.md CHANGED
@@ -10,6 +10,14 @@
10
10
 
11
11
  ## Version History
12
12
 
13
+ ### v2.5.0
14
+
15
+ - :tada: Automatically perform basic schema validation on CoT Creation
16
+
17
+ ### v2.4.0
18
+
19
+ - :tada: `from_geojson(Feature.LineString)` Support
20
+
13
21
  ### v2.3.0
14
22
 
15
23
  - :tada: `from_geojson(Feature.Polygon)` Support
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import XML from './src/xml.js';
1
+ import XML from './lib/xml.js';
2
2
 
3
3
  export { XML };
@@ -0,0 +1,83 @@
1
+ {
2
+ "type": "object",
3
+ "required": [
4
+ "event"
5
+ ],
6
+ "properties": {
7
+ "event": {
8
+ "type": "object",
9
+ "required": [
10
+ "_attributes",
11
+ "point"
12
+ ],
13
+ "properties": {
14
+ "_attributes": {
15
+ "type": "object",
16
+ "required": [
17
+ "version",
18
+ "uid",
19
+ "type",
20
+ "time",
21
+ "start",
22
+ "stale"
23
+ ],
24
+ "properties": {
25
+ "version": {
26
+ "type": "string"
27
+ },
28
+ "uid": {
29
+ "type": "string"
30
+ },
31
+ "type": {
32
+ "type": "string"
33
+ },
34
+ "time": {
35
+ "type": "string"
36
+ },
37
+ "start": {
38
+ "type": "string"
39
+ },
40
+ "stale": {
41
+ "type": "string"
42
+ }
43
+ }
44
+ },
45
+ "point": {
46
+ "type": "object",
47
+ "required": [
48
+ "_attributes"
49
+ ],
50
+ "properties": {
51
+ "_attributes": {
52
+ "type": "object",
53
+ "required": [
54
+ "lat",
55
+ "lon",
56
+ "hae",
57
+ "ce",
58
+ "le"
59
+ ],
60
+ "properties": {
61
+ "lat": {
62
+ "type": "number"
63
+ },
64
+ "lon": {
65
+ "type": "number"
66
+ },
67
+ "hae": {
68
+ "type": "number"
69
+ },
70
+ "ce": {
71
+ "type": "number"
72
+ },
73
+ "le": {
74
+ "type": "number"
75
+ }
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
File without changes
@@ -1,6 +1,11 @@
1
1
  import xmljs from 'xml-js';
2
2
  import Util from './util.js';
3
3
  import PointOnFeature from '@turf/point-on-feature';
4
+ import AJV from 'ajv';
5
+ import fs from 'fs';
6
+
7
+ const ajv = (new AJV({ allErrors: true })).compile(JSON.parse(fs.readFileSync(new URL('./schema.json', import.meta.url))));
8
+
4
9
 
5
10
  /**
6
11
  * Convert to and from an XML CoT message
@@ -26,6 +31,11 @@ export default class XMLCot {
26
31
  this.raw.event.point._attributes[key] = parseFloat(this.raw.event.point._attributes[key]);
27
32
  }
28
33
  }
34
+
35
+ if (!this.raw.event._attributes.uid) this.raw.event._attributes.uuid = Util.cot_uuid().uid;
36
+
37
+ ajv(this.raw);
38
+ if (ajv.errors) throw new Error(ajv.errors[0].message);
29
39
  }
30
40
 
31
41
  /**
@@ -55,22 +65,34 @@ export default class XMLCot {
55
65
  }
56
66
 
57
67
  if (!feature.geometry) throw new Error('Must have Geometry');
58
- if (!['Point', 'Polygon'].includes(feature.geometry.type)) throw new Error('Unsupported Geoemtry Type');
68
+ if (!['Point', 'Polygon', 'LineString'].includes(feature.geometry.type)) throw new Error('Unsupported Geoemtry Type');
59
69
 
60
70
  if (feature.geometry.type === 'Point') {
61
71
  cot.event.point._attributes.lon = feature.geometry.coordinates[0];
62
72
  cot.event.point._attributes.lat = feature.geometry.coordinates[1];
63
- } else if (feature.geometry.type === 'Polygon') {
64
- // Inner rings are not yet supported
65
-
66
- cot.event.detail.link = [];
67
- feature.geometry.coordinates[0].pop(); // Dont' Close Loop in COT
68
- for (const coord of feature.geometry.coordinates[0]) {
69
- cot.event.detail.link.push({
70
- _attributes: {
71
- point: `${coord[1]},${coord[0]}`
72
- }
73
- });
73
+ } else if (['Polygon', 'LineString'].includes(feature.geometry.type)) {
74
+ cot.event._attributes.type = 'u-d-f';
75
+
76
+ if (feature.geometry.type === 'Polygon') {
77
+ cot.event._attributes.type = 'u-d-r';
78
+
79
+ // Inner rings are not yet supported
80
+ cot.event.detail.link = [];
81
+ feature.geometry.coordinates[0].pop(); // Dont' Close Loop in COT
82
+ for (const coord of feature.geometry.coordinates[0]) {
83
+ cot.event.detail.link.push({
84
+ _attributes: { point: `${coord[1]},${coord[0]}` }
85
+ });
86
+ }
87
+ } else if (feature.geometry.type === 'LineString') {
88
+ cot.event._attributes.type = 'u-d-f';
89
+
90
+ cot.event.detail.link = [];
91
+ for (const coord of feature.geometry.coordinates) {
92
+ cot.event.detail.link.push({
93
+ _attributes: { point: `${coord[1]},${coord[0]}` }
94
+ });
95
+ }
74
96
  }
75
97
 
76
98
  cot.event.detail.labels_on = { _attributes: { value: 'false' } };
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "@tak-ps/node-cot",
3
3
  "type": "module",
4
- "version": "2.3.0",
4
+ "version": "2.5.0",
5
5
  "description": "Lightweight JavaScript library for parsing and manipulating TAK messages",
6
6
  "main": "index.js",
7
7
  "scripts": {
8
8
  "test": "tape test/**.test.js",
9
- "lint": "eslint *.js src/*.js test/*.js"
9
+ "lint": "eslint *.js lib/*.js test/*.js"
10
10
  },
11
11
  "dependencies": {
12
12
  "@turf/point-on-feature": "^6.5.0",
13
+ "ajv": "^8.11.2",
13
14
  "protobufjs": "^7.1.2",
14
15
  "uuid": "^9.0.0",
15
16
  "xml-js": "^1.6.11"
@@ -1,7 +1,7 @@
1
1
  import test from 'tape';
2
2
  import { XML } from '../index.js';
3
3
 
4
- test('XML.from_geojson - point', (t) => {
4
+ test('XML.from_geojson - Point', (t) => {
5
5
  const geo = XML.from_geojson({
6
6
  type: 'Feature',
7
7
  properties: {},
@@ -30,7 +30,7 @@ test('XML.from_geojson - point', (t) => {
30
30
  t.end();
31
31
  });
32
32
 
33
- test('XML.from_geojson - polygon', (t) => {
33
+ test('XML.from_geojson - Polygon', (t) => {
34
34
  const geo = XML.from_geojson({
35
35
  type: 'Feature',
36
36
  properties: {},
@@ -47,7 +47,7 @@ test('XML.from_geojson - polygon', (t) => {
47
47
  });
48
48
 
49
49
  t.equals(geo.raw.event._attributes.version, '2.0');
50
- t.equals(geo.raw.event._attributes.type, 'a-f-G');
50
+ t.equals(geo.raw.event._attributes.type, 'u-d-r');
51
51
  t.equals(geo.raw.event._attributes.how, 'm-g');
52
52
  t.equals(geo.raw.event._attributes.uid.length, 36);
53
53
  t.equals(geo.raw.event._attributes.time.length, 24);
@@ -76,3 +76,51 @@ test('XML.from_geojson - polygon', (t) => {
76
76
 
77
77
  t.end();
78
78
  });
79
+
80
+ test('XML.from_geojson - LineString', (t) => {
81
+ const geo = XML.from_geojson({
82
+ type: 'Feature',
83
+ properties: {},
84
+ geometry: {
85
+ type: 'LineString',
86
+ coordinates: [
87
+ [-108.587, 39.098],
88
+ [-108.587, 39.032],
89
+ [-108.505, 39.032],
90
+ [-108.505, 39.098],
91
+ [-108.587, 39.098]
92
+ ]
93
+ }
94
+ });
95
+
96
+ t.equals(geo.raw.event._attributes.version, '2.0');
97
+ t.equals(geo.raw.event._attributes.type, 'u-d-f');
98
+ t.equals(geo.raw.event._attributes.how, 'm-g');
99
+ t.equals(geo.raw.event._attributes.uid.length, 36);
100
+ t.equals(geo.raw.event._attributes.time.length, 24);
101
+ t.equals(geo.raw.event._attributes.start.length, 24);
102
+ t.equals(geo.raw.event._attributes.stale.length, 24);
103
+
104
+ t.deepEquals(geo.raw.event.point, {
105
+ _attributes: { lat: 39.098, lon: -108.505, hae: 0, ce: 9999999, le: 9999999 }
106
+ });
107
+
108
+ t.deepEquals(geo.raw.event.detail, {
109
+ contact: { _attributes: { callsign: 'UNKNOWN' } },
110
+ link: [
111
+ { _attributes: { point: '39.098,-108.587' } },
112
+ { _attributes: { point: '39.032,-108.587' } },
113
+ { _attributes: { point: '39.032,-108.505' } },
114
+ { _attributes: { point: '39.098,-108.505' } },
115
+ { _attributes: { point: '39.098,-108.587' } }
116
+ ],
117
+ labels_on: { _attributes: { value: 'false' } },
118
+ tog: { _attributes: { enabled: '0' } },
119
+ strokeColor: { _attributes: { value: '-256' } },
120
+ strokeWeight: { _attributes: { value: '3.0' } },
121
+ strokeStyle: { _attributes: { value: 'solid' } },
122
+ fillColor: { _attributes: { value: '-1761607936' } }
123
+ });
124
+
125
+ t.end();
126
+ });