-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
329 lines (311 loc) · 13.9 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const path = require('path');
const fs = require('fs');
const os = require('os');
const JSZip = require("jszip");
const crypto = require('crypto');
function onEnd(remote, appId, token) {
return fetch(`${remote}/encrypt/end`, {
method: 'post',
headers: {
'app-id': appId,
token,
},
});
}
const STEP = {
START: 0,
TOKEN: 1,
CREATE_HASH: 2,
UPLOADED: 3,
ENCRYPT_START: 4,
FINISH: 5,
DOWNLOADED: 6,
ERROR: 7,
};
const CACHE_ROOT = path.resolve(os.homedir(), '.js-encrypt-client');
if (!fs.existsSync(CACHE_ROOT)) fs.mkdirSync(CACHE_ROOT);
const chunkSize = 5242880;
const chunkMaxTry = 5;
const TYPE = {
BROWSER: 'browser',
NODE: 'node',
};
function isType(type) {
return function (obj) {
return Object.prototype.toString.call(obj) === '[object ' + type + ']';
}
}
const isArray = isType('Array');
function appendItem(itemMap, entry, filePath, type, recursive) {
if (!type) type = TYPE.BROWSER;
if (!fs.existsSync(filePath)) return;
let state = fs.statSync(filePath);
if (state.isFile()) {
if ('.js' === path.extname(filePath)) {
let name = path.relative(entry, filePath);
name = name.replace(/\\/g, '/');
itemMap[filePath] = {name, type};
}
} else if (state.isDirectory()) {
let files = fs.readdirSync(filePath);
files.forEach(file => {
const childPath = path.join(filePath, file);
if (!recursive) {
const state = fs.statSync(childPath);
if (state.isDirectory()) return;
}
appendItem(itemMap, entry, childPath, type, recursive);
});
}
}
function configWithDefault(config) {
if (!config.entry) throw new Error('config.entry is not defined!');
if (!config.remote) throw new Error('config.remote is not defined!');
if (!config.appId) throw new Error('config.appId is not defined!');
if (!config.appSecret) throw new Error('config.appSecret is not defined!');
if (!config.output) config.output = config.entry;
if ('boolean' !== typeof config.packageAll) config.packageAll = true;
if (!config.type) config.type = TYPE.BROWSER;
if (!isArray(config.dirs)) config.dirs = [];
if (!isArray(config.files)) config.files = [];
return config;
}
function getItemMap(config) {
config = configWithDefault(config);
const itemMap = {};
if (config.packageAll) {
appendItem(itemMap, config.entry, config.entry, config.type, true);
}
config.dirs.forEach(dir => {
appendItem(itemMap, config.entry, dir.name, dir.type, dir.recursive);
});
config.files.forEach(file => {
appendItem(itemMap, config.entry, file.name, file.type, false);
});
return itemMap
}
function encrypt(config) {
return new Promise((resolve, reject) => {
config = configWithDefault(config);
let cacheHash = crypto.createHash('sha256').update(config.entry).digest().toString('hex');
const cacheConfigFile = path.resolve(CACHE_ROOT, `${config.appId}-${cacheHash}`);
const itemMap = getItemMap(config);
let configJson = {};
configJson.items = Object.entries(itemMap).map(([k, v]) => v);
let zip = new JSZip();
zip.file('config.json', Buffer.from(JSON.stringify(configJson)));
let filesHash = {};
Object.entries(itemMap).forEach(([filepath, item]) => {
const content = fs.readFileSync(filepath);
zip.file(item.name, content);
filesHash[filepath] = crypto.createHash('sha1').update(content).digest().toString('hex');
});
let token;
zip.generateAsync({//设置压缩格式,开始打包
type: "nodebuffer",//nodejs用
compression: "DEFLATE",//压缩算法
compressionOptions: {//压缩级别
level: 9
}
})
.then(async function (content) {//Buffer
const fileHash = crypto.createHash('sha1').update(content).digest('hex');
let cacheConfig;
if (fs.existsSync(cacheConfigFile)) {
try {
cacheConfig = JSON.parse(fs.readFileSync(cacheConfigFile).toString('utf-8'));
} catch {
}
}
if (!cacheConfig) cacheConfig = {
step: STEP.START,
};
let isNew = true;
if (cacheConfig.step > STEP.START) {
const tmp = Object.entries(filesHash);
if ('object' === typeof cacheConfig.filesHash && Object.entries(cacheConfig.filesHash).length === tmp.length) {
isNew = tmp.filter(i => cacheConfig.filesHash[i[0]] !== i[1]).length > 0;
}
}
if (!cacheConfig.token) isNew = true;//no token must be new mission
if (!isNew) {
//send check token to verify if expired, if expired isNew=true and send end
const onState = await fetch(`${config.remote}/encrypt/token/state`, {
method: 'post',
headers: {
'app-id': config.appId,
token: cacheConfig.token,
},
});
const body = await onState.json()
if (200 !== onState.status || 0 !== body.code) {
await onEnd(config.remote, config.appId, cacheConfig.token);
isNew = true;
}
}
if (isNew) {
cacheConfig.step = STEP.START;
cacheConfig.hash = fileHash;
cacheConfig.size = content.length;
}
if (isNew) {
const getToken = await fetch(`${config.remote}/encrypt/getToken`, {
method: 'post',
headers: {
'app-id': config.appId,
'app-secret': config.appSecret,
},
});
if (200 !== getToken.status) throw new Error(`no token, with HTTP statusCode: ${getToken.status}`);
const body = await getToken.json()
if (0 !== body.code || !body.data || !body.data.token) throw new Error(`no token, code: ${body.code}`);
cacheConfig.token = body.data.token;
cacheConfig.step = STEP.TOKEN;
cacheConfig.filesHash = filesHash;
fs.writeFileSync(cacheConfigFile, JSON.stringify(cacheConfig));
}
//生成上传任务
token = cacheConfig.token;
const total = Math.ceil(content.length / chunkSize);
if (cacheConfig.step === STEP.TOKEN) {
const onCreate = await fetch(`${config.remote}/upload/create`, {
method: 'post',
headers: {
'app-id': config.appId,
token,
'content-type': 'application/json',
},
body: JSON.stringify({
hash: fileHash,
size: content.length,
chunkSize,
total,
}),
});
const body = await onCreate.json()
if (200 !== onCreate.status || 0 !== body.code) throw new Error('create failed');
cacheConfig.step = STEP.CREATE_HASH;
fs.writeFileSync(cacheConfigFile, JSON.stringify(cacheConfig));
}
//上传
if (cacheConfig.step === STEP.CREATE_HASH) {
let current = cacheConfig.current || 0;
let tries = 0;
while (current < total) {
const begin = current * chunkSize;
const end = Math.min(begin + chunkSize, content.length);
const chunk = content.slice(begin, end);
const onChunk = await fetch(`${config.remote}/upload/chunk`, {
method: 'post',
headers: {
'app-id': config.appId,
'chunk-id': current,
'hash': crypto.createHash('md5').update(chunk).digest('hex'),
token,
'content-type': 'application/octet-stream',
},
body: chunk,
});
const body = await onChunk.json()
if (200 !== onChunk.status || 0 !== body.code) {
if (tries > chunkMaxTry) {
if (200 !== onChunk.status) throw new Error(`chunk failed, with HTTP statusCode: ${onChunk.status}`);
throw new Error(`chunk failed, code: ${body.code}`);
}
if (2 === body.code) current = body.data.current;
tries++;
continue;
}
current = body.data.current;
cacheConfig.current = current;
fs.writeFileSync(cacheConfigFile, JSON.stringify(cacheConfig));
}
cacheConfig.step = STEP.UPLOADED;
fs.writeFileSync(cacheConfigFile, JSON.stringify(cacheConfig));
}
//打包
if (cacheConfig.step === STEP.UPLOADED) {
const onEncrypt = await fetch(`${config.remote}/encrypt/start`, {
method: 'post',
headers: {
'app-id': config.appId,
token,
},
});
const body = await onEncrypt.json()
if (200 !== onEncrypt.status || 0 !== body.code) throw new Error('encrypt failed on start');
cacheConfig.step = STEP.ENCRYPT_START;
fs.writeFileSync(cacheConfigFile, JSON.stringify(cacheConfig));
}
//检测
if (cacheConfig.step === STEP.ENCRYPT_START) {
let process = 0;
let finish = false;
let timeout = 2000;
while (!finish) {
await new Promise(r => setTimeout(r, timeout));
const onState = await fetch(`${config.remote}/encrypt/state`, {
method: 'post',
headers: {
'app-id': config.appId,
token,
},
});
const body = await onState.json()
if (200 !== onState.status || 0 !== body.code) throw new Error('encrypt failed on pending');
process = body.data.process;
finish = body.data.finish;
console.log(`process: ${process}%`);
}
cacheConfig.step = STEP.FINISH;
fs.writeFileSync(cacheConfigFile, JSON.stringify(cacheConfig));
}
//下载
if (cacheConfig.step === STEP.FINISH) {
console.log('downloading...');
const onDownload = await fetch(`${config.remote}/encrypt/download`, {
method: 'post',
headers: {
'app-id': config.appId,
token,
},
});
if (200 !== onDownload.status) throw new Error('download failed');
const body = await onDownload.arrayBuffer()
//解压
JSZip.loadAsync(body)
.then(zip => {
const files = zip.files;
const basepath = config.output;
if (!fs.existsSync(basepath)) fs.mkdirSync(basepath);
let jobs = [];
for (const filename of Object.keys(files)) {
const dest = path.join(basepath, filename);
if (files[filename].dir) fs.mkdirSync(dest, {recursive: true});
else jobs.push(files[filename].async('nodebuffer').then(content => {
const parentDir = path.dirname(dest);
if (!fs.existsSync(parentDir)) fs.mkdirSync(parentDir, {recursive: true});
fs.writeFileSync(dest, content)
}));
}
return Promise.all(jobs);
})
.then(() => {
console.log('finish.');
resolve();
});
cacheConfig.step = STEP.DOWNLOADED;
fs.writeFileSync(cacheConfigFile, JSON.stringify(cacheConfig));
}
})
.catch(e => {
console.error(e);
reject(e);
})
.finally(() => {
onEnd(config.remote, config.appId, token);
if (fs.existsSync(cacheConfigFile)) fs.unlinkSync(cacheConfigFile);
});
});
}
module.exports = {encrypt, getItemMap};