-
Notifications
You must be signed in to change notification settings - Fork 1
/
A3sRemoteServer.ts
159 lines (138 loc) · 3.79 KB
/
A3sRemoteServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { URL } from 'url';
import { gunzipSync } from 'zlib';
import got, { CancelableRequest } from 'got';
// ArmA3Sync & java.io interfaces stolen from: https://github.com/gruppe-adler/node-arma3sync-lib
import { InputObjectStream } from 'java.io';
export interface A3sEventDto {
name: string;
description: string;
addonNames: {
[addonName: string]: boolean;
};
userconfigFolderNames: {
[folderName: string]: boolean;
};
}
export interface A3sEventsDto {
list: A3sEventDto[];
}
export interface A3sChangelogDto {
revision: number
buildDate: Date
contentUpdated: boolean
newAddons: string[]
updatedAddons: string[]
deletedAddons: string[]
addons: string[]
}
export interface A3sChangelogsDto {
list: A3sChangelogDto[],
}
export interface A3sServerInfoDto {
revision: number
buildDate: Date
numberOfFiles: number
totalFilesSize: number
hiddenFolderPaths: string[]
numberOfConnections: number
noPartialFileTransfer: boolean
repositoryContentUpdated: boolean
compressedPboFilesOnly: boolean
}
export interface A3sAutoconfigDto {
favoriteServers: A3sFavoriteServerDto[];
protocole: A3sRepositoryProtocolDto;
repositoryName: string;
}
export interface A3sRepositoryProtocolDto {
connectionTimeOut: string
encryptionMode?: A3sEncryptionMode
protocolType?: A3sProtocolType
login: string
password: string
port: string
readTimeOut: string
url: string
}
export interface A3sProtocolType {
description: string
prompt: string
defaultPort: string
protocol: A3sProtocol
}
export interface A3sFavoriteServerDto {
name: string
ipAddress: string
port: number
password: string
selected: boolean
modSetName: string
repositoryName: string
}
export interface A3sEncryptionMode {
description: string
encryption: A3sEncryption
}
export enum A3sProtocol {
FTP,
HTTP,
HTTPS,
A3S,
FTP_BITTORRENT,
HTTP_BITTORRENT,
HTTPS_BITTORRENT,
SOCKS4,
SOCKS5
}
export enum A3sEncryption {
NO_ENCRYPTION,
EXPLICIT_SSL,
IMPLICIT_SSL
}
// --
export enum A3sDataTypes {
autoconfig = 'autoconfig',
serverinfo = 'serverinfo',
events = 'events',
changelogs = 'changelogs'
}
export default class A3sRemoteServer {
url: string;
autoconfig?: A3sAutoconfigDto;
serverinfo?: A3sServerInfoDto;
events?: A3sEventsDto;
changelogs?: A3sChangelogsDto;
/**
*
* @param url the ArmA3Sync server url or autoconfig url
*/
constructor(url: string) {
if (url.slice(-11) === '/autoconfig') {
url = url.slice(0, -10);
}
if (url.slice(-1) !== '/') {
url += '/';
}
const reqUrl = new URL(url);
if (reqUrl.protocol !== 'http:' && reqUrl.protocol !== 'https:') {
throw new Error('TODO: support protocols other than HTTP(S)');
}
this.url = reqUrl.href;
}
public async loadData(types: Array<keyof typeof A3sDataTypes> = ['autoconfig', 'serverinfo', 'events', 'changelogs']): Promise<void> {
const req_promises: Array<Promise<CancelableRequest | void>> = [];
for (const t of types) {
req_promises.push(this.loadSingleData(t));
}
return Promise.all(req_promises).then(() => Promise.resolve());
}
private async loadSingleData(t: keyof typeof A3sDataTypes): Promise<CancelableRequest | void> {
return got(this.url + t, { decompress: false, timeout: 3000 })
.buffer()
.then(buff => {
this[t] = new InputObjectStream(gunzipSync(buff), false).readObject();
// console.log(JSON.stringify(this[t], null, 2)); // debug
})
.catch((e: any) => console.error(t, e?.message));
}
}