This repository was archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (140 loc) · 4.26 KB
/
index.js
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
const net = require("net");
const tls = require("tls");
function checkArg(arg, type){
if(arg !== undefined && arg !== null){
if(!(arg.constructor == type))
throw new TypeError(`Arg ${type} != ${arg}.constructor`);
}else throw new TypeError(`Arg ${arg} === undefined`);
}
const wait = ms => new Promise(r => setTimeout(r,ms));
class GPCOptions{
/**
* Will use TLS over TCP.
* @type {Boolean}
*/
tls = true
}
class InfoPackage{
/**
* The package name
* @type {string}
*/
name
/**
* The version of the package
* @type {string}
*/
version
/**
* The platform
* @type {string}
*/
platform
}
class Package extends InfoPackage{
/**
* The size in bytes of the packagetttf
* @type {int}
*/
size
/**
* @type {InfoPackage[]}
*/
dependencies
}
class GPCClient{
/**
* @type {net.Socket | tls.TLSSocket}
*/
#tcpConnection
/**
*
* @param {net.NetConnectOpts | GPCOptions} options
*/
constructor(options){
if(options.tls === undefined) options.tls = true;
if(options.tls)
this.#tcpConnection = tls.connect(options).unref();
else
this.#tcpConnection = net.createConnection(options).unref();
this.#tcpConnection.pause(); // block data event because ondata event will annoy
}
/**
* @returns {Promise<int>}
*/
async #read1byte(){
let buf;
while(!buf){
buf = this.#tcpConnection.read(1);
await wait(0);
}
return buf[0];
}
async #ondata(size){
let buf;
while(!buf){buf = this.#tcpConnection.read(size);await wait(0);}
return buf;
}
async #readMessageUntilNull(){
let buf = "";
while(true){
const value = this.#tcpConnection.read(1);
if(value){
const char = value[0];
if(char === 0) break;
buf += String.fromCharCode(char);
}else await wait(0);
}
return buf;
}
/**
*
* @param {String} package Package name
* @param {String} platform Package platform
* @param {String | undefined} version
* @returns {Promise<Package>}
*/
async getPackageInformation(packageName, platform, version){
checkArg(packageName, String);
checkArg(platform, String);
if(version) checkArg(version, String);
this.#tcpConnection.write(`\x00${packageName}\x00${platform}\x00${version ? version+"\x00" : ""}`);
const nosuccess = await this.#read1byte();
if(nosuccess)
throw new Error(await this.#readMessageUntilNull());
const packageNameInServer = await this.#readMessageUntilNull();
const platformInServer = await this.#readMessageUntilNull();
const versionInServer = await this.#readMessageUntilNull();
const size = (await this.#ondata(4)).readUintBE(0,4);
const dependenciesSize = await this.#read1byte();
const dependencies = [];
for(let i=0;i<dependenciesSize;i++){
const dep = {};
dep.name = await this.#readMessageUntilNull();
dep.platform = await this.#readMessageUntilNull();
dep.version = await this.#readMessageUntilNull();
dependencies.push(dep);
}
return {name: packageNameInServer, platform: platformInServer, version: versionInServer, size, dependencies};
}
/**
*
* @param {String} packageName Package name
* @param {String} platform Package platform
* @param {String} version Package version
* @returns {Promise<{checksum: Buffer, content: Buffer}>}
*/
async download(packageName, platform, version, size){
checkArg(packageName, String);
checkArg(platform, String);
checkArg(version, String);
this.#tcpConnection.write(`\x01${packageName}\x00${platform}\x00${version}\x00`);
const nosuccess = await this.#read1byte();
if(nosuccess)
throw new Error(await this.#readMessageUntilNull());
const checksum = await this.#ondata(32);
const content = await this.#ondata(size);
return {checksum, content};
}
}
module.exports = GPCClient;