@tak-ps/node-tak 8.2.1 → 8.4.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/.github/workflows/doc.yml +6 -6
- package/.github/workflows/release.yml +3 -3
- package/.github/workflows/test.yml +14 -9
- package/CHANGELOG.md +9 -0
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/lib/api/contacts.js +23 -0
- package/dist/lib/api/contacts.js.map +1 -0
- package/dist/lib/api/credentials.js +62 -0
- package/dist/lib/api/credentials.js.map +1 -0
- package/dist/lib/api/export.js +36 -0
- package/dist/lib/api/export.js.map +1 -0
- package/dist/lib/api/files.js +112 -0
- package/dist/lib/api/files.js.map +1 -0
- package/dist/lib/api/groups.js +47 -0
- package/dist/lib/api/groups.js.map +1 -0
- package/dist/lib/api/mission-layer.js +245 -0
- package/dist/lib/api/mission-layer.js.map +1 -0
- package/dist/lib/api/mission-log.js +108 -0
- package/dist/lib/api/mission-log.js.map +1 -0
- package/dist/lib/api/mission.js +583 -0
- package/dist/lib/api/mission.js.map +1 -0
- package/dist/lib/api/oauth.js +54 -0
- package/dist/lib/api/oauth.js.map +1 -0
- package/dist/lib/api/package.js +42 -0
- package/dist/lib/api/package.js.map +1 -0
- package/dist/lib/api/query.js +60 -0
- package/dist/lib/api/query.js.map +1 -0
- package/dist/lib/api/subscriptions.js +73 -0
- package/dist/lib/api/subscriptions.js.map +1 -0
- package/dist/lib/api/types.js +42 -0
- package/dist/lib/api/types.js.map +1 -0
- package/dist/lib/api/video.js +123 -0
- package/dist/lib/api/video.js.map +1 -0
- package/dist/lib/api.js +123 -0
- package/dist/lib/api.js.map +1 -0
- package/dist/lib/auth.js +92 -0
- package/dist/lib/auth.js.map +1 -0
- package/dist/lib/fetch.js +26 -0
- package/dist/lib/fetch.js.map +1 -0
- package/dist/lib/stream.js +9 -0
- package/dist/lib/stream.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/index.ts +8 -3
- package/lib/api/contacts.ts +27 -0
- package/lib/api/credentials.ts +74 -0
- package/lib/api/export.ts +44 -0
- package/lib/api/files.ts +151 -0
- package/lib/api/groups.ts +63 -0
- package/lib/api/mission-layer.ts +312 -0
- package/lib/api/mission-log.ts +140 -0
- package/lib/api/mission.ts +741 -0
- package/lib/api/oauth.ts +68 -0
- package/lib/api/package.ts +56 -0
- package/lib/api/query.ts +79 -0
- package/lib/api/subscriptions.ts +84 -0
- package/lib/api/types.ts +43 -0
- package/lib/api/video.ts +155 -0
- package/lib/api.ts +136 -0
- package/lib/auth.ts +117 -0
- package/lib/fetch.ts +38 -0
- package/lib/stream.ts +10 -0
- package/package.json +17 -4
package/lib/auth.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fetch from './fetch.js';
|
|
2
|
+
import { CookieJar, Cookie } from 'tough-cookie';
|
|
3
|
+
import { CookieAgent } from 'http-cookie-agent/undici';
|
|
4
|
+
import { Client } from 'undici';
|
|
5
|
+
import TAKAPI from './api.js';
|
|
6
|
+
import stream2buffer from './stream.js';
|
|
7
|
+
|
|
8
|
+
export class APIAuth {
|
|
9
|
+
async init(api: TAKAPI) { // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async fetch(api: TAKAPI, url: URL, opts: any): Promise<any> {
|
|
14
|
+
return await fetch(url, opts);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class APIAuthPassword extends APIAuth {
|
|
19
|
+
username: string;
|
|
20
|
+
password: string;
|
|
21
|
+
jwt: string;
|
|
22
|
+
|
|
23
|
+
constructor(username: string, password: string) {
|
|
24
|
+
super();
|
|
25
|
+
this.username = username;
|
|
26
|
+
this.password = password;
|
|
27
|
+
this.jwt = '';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async init(api: TAKAPI) {
|
|
31
|
+
const { token } = await api.OAuth.login({
|
|
32
|
+
username: this.username,
|
|
33
|
+
password: this.password
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
this.jwt = token;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async fetch(api: TAKAPI, url: URL, opts: any): Promise<any> {
|
|
40
|
+
const jar = new CookieJar();
|
|
41
|
+
|
|
42
|
+
await jar.setCookie(new Cookie({ key: 'access_token', value: this.jwt }), String(api.url));
|
|
43
|
+
|
|
44
|
+
opts.credentials = 'include';
|
|
45
|
+
|
|
46
|
+
if (!opts.nocookies) {
|
|
47
|
+
const agent = new CookieAgent({ cookies: { jar } });
|
|
48
|
+
opts.dispatcher = agent;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return await fetch(url, opts);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export class APIAuthToken extends APIAuth {
|
|
56
|
+
jwt?: string;
|
|
57
|
+
|
|
58
|
+
constructor(jwt: string) {
|
|
59
|
+
super();
|
|
60
|
+
this.jwt = jwt;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async fetch(api: TAKAPI, url: URL, opts: any): Promise<any> {
|
|
64
|
+
const jar = new CookieJar();
|
|
65
|
+
|
|
66
|
+
await jar.setCookie(new Cookie({ key: 'access_token', value: this.jwt }), String(api.url));
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
opts.credentials = 'include';
|
|
70
|
+
if (!opts.nocookies) {
|
|
71
|
+
const agent = new CookieAgent({ cookies: { jar } });
|
|
72
|
+
opts.dispatcher = agent;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return await fetch(url, opts);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export class APIAuthCertificate extends APIAuth {
|
|
80
|
+
cert: string;
|
|
81
|
+
key: string;
|
|
82
|
+
|
|
83
|
+
constructor(cert: string, key: string) {
|
|
84
|
+
super();
|
|
85
|
+
this.cert = cert;
|
|
86
|
+
this.key = key;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async fetch(api: TAKAPI, url: URL, opts: any): Promise<any> {
|
|
90
|
+
const client = new Client(api.url.origin, {
|
|
91
|
+
connect: {
|
|
92
|
+
key: this.key,
|
|
93
|
+
cert: this.cert,
|
|
94
|
+
rejectUnauthorized: false,
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const res = await client.request({
|
|
99
|
+
path: String(url).replace(api.url.origin, ''),
|
|
100
|
+
...opts
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
status: res.statusCode,
|
|
105
|
+
body: res.body,
|
|
106
|
+
// Make this similiar to the fetch standard
|
|
107
|
+
headers: new Map(Object.entries(res.headers)),
|
|
108
|
+
text: async () => {
|
|
109
|
+
return String(await stream2buffer(res.body));
|
|
110
|
+
},
|
|
111
|
+
json: async () => {
|
|
112
|
+
return JSON.parse(String(await stream2buffer(res.body)));
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
package/lib/fetch.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import Err from '@openaddresses/batch-error';
|
|
2
|
+
import { Static, TSchema, TUnknown } from "@sinclair/typebox";
|
|
3
|
+
import { TypeCompiler } from "@sinclair/typebox/compiler";
|
|
4
|
+
import { fetch, Response } from 'undici';
|
|
5
|
+
import type { RequestInfo, RequestInit } from 'undici';
|
|
6
|
+
|
|
7
|
+
export class TypedResponse extends Response {
|
|
8
|
+
constructor(response: Response) {
|
|
9
|
+
super(response.body, {
|
|
10
|
+
status: response.status,
|
|
11
|
+
statusText: response.statusText,
|
|
12
|
+
headers: response.headers,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
typed<T extends TSchema>(type: T): Promise<Static<T>>;
|
|
17
|
+
|
|
18
|
+
async typed<T extends TSchema = TUnknown>(type: T): Promise<Static<T>> {
|
|
19
|
+
const body = await this.json();
|
|
20
|
+
|
|
21
|
+
const typeChecker = TypeCompiler.Compile(type)
|
|
22
|
+
const result = typeChecker.Check(body);
|
|
23
|
+
|
|
24
|
+
if (result) return body;
|
|
25
|
+
|
|
26
|
+
const errors = typeChecker.Errors(body);
|
|
27
|
+
const firstError = errors.First();
|
|
28
|
+
|
|
29
|
+
throw new Err(500, null, `Internal Validation Error: ${JSON.stringify(firstError)}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default async function(
|
|
34
|
+
input: RequestInfo,
|
|
35
|
+
init?: RequestInit
|
|
36
|
+
): Promise<TypedResponse> {
|
|
37
|
+
return new TypedResponse(await fetch(input, init));
|
|
38
|
+
}
|
package/lib/stream.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Stream } from 'node:stream';
|
|
2
|
+
|
|
3
|
+
export default async function stream2buffer(stream: Stream): Promise<Buffer> {
|
|
4
|
+
return new Promise<Buffer>((resolve, reject) => {
|
|
5
|
+
const _buf = Array<Buffer>();
|
|
6
|
+
stream.on("data", chunk => _buf.push(chunk));
|
|
7
|
+
stream.on("end", () => resolve(Buffer.concat(_buf)));
|
|
8
|
+
stream.on("error", (err: Error) => reject(`error converting stream - ${err}`));
|
|
9
|
+
});
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,27 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tak-ps/node-tak",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "8.
|
|
4
|
+
"version": "8.4.0",
|
|
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",
|
|
8
8
|
"types": "index.ts",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">= 22"
|
|
11
|
+
},
|
|
9
12
|
"scripts": {
|
|
10
|
-
"test": "ts-node-test test/",
|
|
11
|
-
"lint": "eslint *.ts test
|
|
13
|
+
"test": "c8 --reporter=lcov ts-node-test test/",
|
|
14
|
+
"lint": "eslint *.ts test/ lib/",
|
|
12
15
|
"doc": "typedoc index.ts",
|
|
13
16
|
"build": "tsc --build",
|
|
14
17
|
"pretest": "npm run lint"
|
|
15
18
|
},
|
|
16
19
|
"dependencies": {
|
|
17
|
-
"ajv": "^8.12.0"
|
|
20
|
+
"ajv": "^8.12.0",
|
|
21
|
+
"form-data": "^4.0.2",
|
|
22
|
+
"http-cookie-agent": "^7.0.1",
|
|
23
|
+
"mime": "^4.0.7",
|
|
24
|
+
"pem": "^1.14.8",
|
|
25
|
+
"tough-cookie": "^5.1.2",
|
|
26
|
+
"undici": "^7.8.0",
|
|
27
|
+
"xml2js": "^0.6.2"
|
|
18
28
|
},
|
|
19
29
|
"peerDependencies": {
|
|
20
30
|
"@tak-ps/node-cot": "^12.0.0"
|
|
21
31
|
},
|
|
22
32
|
"devDependencies": {
|
|
23
33
|
"@types/node": "^22.0.0",
|
|
34
|
+
"@types/pem": "^1.14.4",
|
|
24
35
|
"@types/tape": "^5.6.0",
|
|
36
|
+
"@types/xml2js": "^0.4.14",
|
|
37
|
+
"c8": "^10.1.3",
|
|
25
38
|
"eslint": "^9.0.0",
|
|
26
39
|
"tape": "^5.6.1",
|
|
27
40
|
"ts-node": "^10.9.1",
|