@tak-ps/node-cot 10.5.0 → 10.6.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.
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tak-ps/node-cot",
3
3
  "type": "module",
4
- "version": "10.5.0",
4
+ "version": "10.6.0",
5
5
  "description": "Lightweight JavaScript library for parsing and manipulating TAK messages",
6
6
  "author": "Nick Ingalls <nick@ingalls.ca>",
7
7
  "main": "dist/index.js",
@@ -10,7 +10,7 @@
10
10
  "test": "ts-node-test test/",
11
11
  "lint": "eslint *.ts lib/ test/",
12
12
  "doc": "typedoc index.ts",
13
- "build": "tsc --build && cp package.json dist/ && mkdir -p ./dist/lib/proto/ && cp lib/proto/* ./dist/lib/proto/",
13
+ "build": "tsc --build && cp package.json dist/ && cp ./lib/*.xml ./dist/lib/ && mkdir -p ./dist/lib/proto/ && cp lib/proto/* ./dist/lib/proto/",
14
14
  "pretest": "npm run lint"
15
15
  },
16
16
  "dependencies": {
@@ -0,0 +1,7 @@
1
+ import { CoTTypes } from '../index.js';
2
+ import test from 'tape';
3
+ test(`CoTTypes Parsing`, async (t) => {
4
+ await CoTTypes.CoTTypes.load();
5
+ t.end();
6
+ });
7
+ //# sourceMappingURL=cot-types.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cot-types.test.js","sourceRoot":"","sources":["../../test/cot-types.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,IAAI,CAAC,kBAAkB,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IACjC,MAAM,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;IAE/B,CAAC,CAAC,GAAG,EAAE,CAAC;AACZ,CAAC,CAAC,CAAC"}
package/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import CoT from './lib/cot.js';
2
2
  export * as Feature from './lib/feature.js'
3
3
  export * as Types from './lib/types.js'
4
+ export * as CoTTypes from './lib/cot-types.js'
4
5
  export * from './lib/chat.js'
5
6
  export * from './lib/fileshare.js'
6
7
  export * from './lib/data-package.js'
@@ -0,0 +1,115 @@
1
+ import Err from '@openaddresses/batch-error';
2
+ import fsp from 'node:fs/promises';
3
+ import xmljs from 'xml-js';
4
+ import { Static, Type } from '@sinclair/typebox'
5
+ import AJV from 'ajv';
6
+
7
+ export const TypeFormat_COT = Type.Object({
8
+ cot: Type.String(),
9
+ full: Type.Optional(Type.String()),
10
+ desc: Type.String()
11
+ })
12
+
13
+ export const TypeFormat_Weapon = Type.Object({
14
+ cot: Type.String(),
15
+ desc: Type.String()
16
+ })
17
+
18
+ export const TypeFormat_Relation = Type.Object({
19
+ cot: Type.String(),
20
+ desc: Type.String()
21
+ })
22
+
23
+ export const TypeFormat_Is = Type.Object({
24
+ what: Type.String(),
25
+ match: Type.String()
26
+ });
27
+
28
+ export const TypeFormat_How = Type.Object({
29
+ what: Type.String(),
30
+ value: Type.String()
31
+ })
32
+
33
+ export const TypeFormat = Type.Object({
34
+ types: Type.Object({
35
+ cot: Type.Array(Type.Object({
36
+ _attributes: TypeFormat_COT
37
+ })),
38
+ weapon: Type.Array(Type.Object({
39
+ _attributes: TypeFormat_Weapon
40
+ })),
41
+ relation: Type.Array(Type.Object({
42
+ _attributes: TypeFormat_Relation
43
+ })),
44
+ is: Type.Array(Type.Object({
45
+ _attributes: TypeFormat_Is
46
+ })),
47
+ how: Type.Array(Type.Object({
48
+ _attributes: TypeFormat_How
49
+ }))
50
+ })
51
+ });
52
+
53
+ const checkTypes = (new AJV({
54
+ allErrors: true,
55
+ coerceTypes: true,
56
+ allowUnionTypes: true
57
+ }))
58
+ .compile(TypeFormat);
59
+
60
+ export class CoTTypes {
61
+ cots: Map<string, Static<typeof TypeFormat_COT>>
62
+ weapons: Map<string, Static<typeof TypeFormat_Weapon>>
63
+ relations: Map<string, Static<typeof TypeFormat_Relation>>
64
+ is: Map<string, Static<typeof TypeFormat_Is>>
65
+ how: Map<string, Static<typeof TypeFormat_How>>
66
+
67
+ constructor(
68
+ cots: Map<string, Static<typeof TypeFormat_COT>>,
69
+ weapons: Map<string, Static<typeof TypeFormat_Weapon>>,
70
+ relations: Map<string, Static<typeof TypeFormat_Relation>>,
71
+ is: Map<string, Static<typeof TypeFormat_Is>>,
72
+ how: Map<string, Static<typeof TypeFormat_How>>
73
+ ) {
74
+ this.cots = cots;
75
+ this.weapons = weapons;
76
+ this.relations = relations;
77
+ this.is = is;
78
+ this.how = how;
79
+ }
80
+
81
+ static async load(): Promise<CoTTypes> {
82
+ const xml = xmljs.xml2js(String(await fsp.readFile(new URL('cot-types.xml', import.meta.url))), { compact: true })
83
+
84
+ checkTypes(xml);
85
+ if (checkTypes.errors) throw new Err(400, null, `${checkTypes.errors[0].message} (${checkTypes.errors[0].instancePath})`);
86
+ const types = xml as Static<typeof TypeFormat>;
87
+
88
+ const cots: Map<string, Static<typeof TypeFormat_COT>> = new Map();
89
+ for (const cot of types.types.cot) {
90
+ cots.set(cot._attributes.cot, cot._attributes);
91
+ }
92
+
93
+ const weapons: Map<string, Static<typeof TypeFormat_Weapon>> = new Map();
94
+ for (const weapon of types.types.weapon) {
95
+ weapons.set(weapon._attributes.cot, weapon._attributes);
96
+ }
97
+
98
+ const relations: Map<string, Static<typeof TypeFormat_Relation>> = new Map();
99
+ for (const relation of types.types.relation) {
100
+ relations.set(relation._attributes.cot, relation._attributes);
101
+ }
102
+
103
+ const is: Map<string, Static<typeof TypeFormat_Is>> = new Map();
104
+ for (const i of types.types.is) {
105
+ is.set(i._attributes.what, i._attributes);
106
+ }
107
+
108
+ const how: Map<string, Static<typeof TypeFormat_How>> = new Map();
109
+ for (const h of types.types.how) {
110
+ how.set(h._attributes.value, h._attributes);
111
+ }
112
+
113
+ return new CoTTypes(cots, weapons, relations, is, how);
114
+ }
115
+ }