w-converhp 2.0.0 → 2.0.2
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/README.md +1 -5
- package/dist/w-converhp-client.umd.js +2 -2
- package/dist/w-converhp-client.umd.js.map +1 -1
- package/dist/w-converhp-server.umd.js +2 -2
- package/dist/w-converhp-server.umd.js.map +1 -1
- package/docs/WConverhpClient.html +43 -3
- package/docs/WConverhpClient.mjs.html +108 -46
- package/docs/WConverhpServer.html +2 -2
- package/docs/WConverhpServer.mjs.html +55 -21
- package/docs/index.html +1 -1
- package/package.json +4 -4
- package/scla.mjs +13 -1
- package/sclb.mjs +13 -1
- package/sclc.mjs +11 -1
- package/src/WConverhpClient.mjs +107 -45
- package/src/WConverhpServer.mjs +54 -20
- package/srv.mjs +5 -1
- package/toolg/gDistRollup.mjs +1 -1
package/sclb.mjs
CHANGED
|
@@ -8,11 +8,18 @@ let opt = {
|
|
|
8
8
|
FormData,
|
|
9
9
|
url: 'http://localhost:8080',
|
|
10
10
|
apiName: 'api',
|
|
11
|
+
getToken: () => {
|
|
12
|
+
return 'token-for-test'
|
|
13
|
+
},
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
//new
|
|
14
17
|
let wo = new WConverhpClient(opt)
|
|
15
18
|
|
|
19
|
+
wo.on('error', function(err) {
|
|
20
|
+
console.log(`error`, err)
|
|
21
|
+
})
|
|
22
|
+
|
|
16
23
|
async function execute(name, u8a) {
|
|
17
24
|
|
|
18
25
|
//p
|
|
@@ -30,7 +37,12 @@ async function execute(name, u8a) {
|
|
|
30
37
|
//execute
|
|
31
38
|
await wo.execute('add', { p },
|
|
32
39
|
function (prog, p, m) {
|
|
33
|
-
|
|
40
|
+
if (m === 'upload') {
|
|
41
|
+
console.log('client web: execute: prog', prog * 0.5, p, m)
|
|
42
|
+
}
|
|
43
|
+
if (m === 'download') {
|
|
44
|
+
console.log('client web: execute: prog', 50 + prog * 0.5, p, m)
|
|
45
|
+
}
|
|
34
46
|
})
|
|
35
47
|
.then(function(r) {
|
|
36
48
|
console.log('client web: execute: add', r)
|
package/sclc.mjs
CHANGED
|
@@ -8,11 +8,18 @@ let opt = {
|
|
|
8
8
|
FormData,
|
|
9
9
|
url: 'http://localhost:8080',
|
|
10
10
|
apiName: 'api',
|
|
11
|
+
getToken: () => {
|
|
12
|
+
return 'token-for-test'
|
|
13
|
+
},
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
//new
|
|
14
17
|
let wo = new WConverhpClient(opt)
|
|
15
18
|
|
|
19
|
+
wo.on('error', function(err) {
|
|
20
|
+
console.log(`error`, err)
|
|
21
|
+
})
|
|
22
|
+
|
|
16
23
|
function uploadLargeFile() {
|
|
17
24
|
let core = async() => {
|
|
18
25
|
|
|
@@ -23,7 +30,10 @@ function uploadLargeFile() {
|
|
|
23
30
|
|
|
24
31
|
await wo.upload('1000mb.7z', u8a,
|
|
25
32
|
function (prog, p, m) {
|
|
26
|
-
console.log('client web: upload: prog', prog, p, m)
|
|
33
|
+
// console.log('client web: upload: prog', prog, p, m)
|
|
34
|
+
if (m === 'upload') {
|
|
35
|
+
console.log('client web: upload: prog', prog)
|
|
36
|
+
}
|
|
27
37
|
})
|
|
28
38
|
.then(function(res) {
|
|
29
39
|
console.log('client web: upload: then', res)
|
package/src/WConverhpClient.mjs
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import axios from 'axios'
|
|
2
|
-
// import * as FormData from 'form-data/lib/form_data.js'
|
|
3
|
-
// import FormData from 'form-data'
|
|
4
2
|
import get from 'lodash-es/get.js'
|
|
5
3
|
import isWindow from 'wsemi/src/isWindow.mjs'
|
|
6
4
|
import genPm from 'wsemi/src/genPm.mjs'
|
|
@@ -12,8 +10,10 @@ import isp0int from 'wsemi/src/isp0int.mjs'
|
|
|
12
10
|
import isestr from 'wsemi/src/isestr.mjs'
|
|
13
11
|
import isobj from 'wsemi/src/isobj.mjs'
|
|
14
12
|
import iseobj from 'wsemi/src/iseobj.mjs'
|
|
13
|
+
import ispm from 'wsemi/src/ispm.mjs'
|
|
15
14
|
import cint from 'wsemi/src/cint.mjs'
|
|
16
15
|
import strright from 'wsemi/src/strright.mjs'
|
|
16
|
+
import evem from 'wsemi/src/evem.mjs'
|
|
17
17
|
import blob2u8arr from 'wsemi/src/blob2u8arr.mjs'
|
|
18
18
|
import obj2u8arr from 'wsemi/src/obj2u8arr.mjs'
|
|
19
19
|
import u8arr2obj from 'wsemi/src/u8arr2obj.mjs'
|
|
@@ -28,9 +28,10 @@ import now2strp from 'wsemi/src/now2strp.mjs'
|
|
|
28
28
|
* @param {Object} opt 輸入設定參數物件
|
|
29
29
|
* @param {String} [opt.url='http://localhost:8080'] 輸入Hapi伺服器網址,預設為'http://localhost:8080'
|
|
30
30
|
* @param {String} [opt.apiName='api'] 輸入API名稱字串,預設'api'
|
|
31
|
+
* @param {Function} [opt.getToken=()=>''] 輸入取得使用者token的回調函數,預設()=>''
|
|
31
32
|
* @param {Integer} [opt.sizeSlice=1024*1024] 輸入切片上傳檔案之切片檔案大小整數,單位為Byte,預設為1024*1024
|
|
32
33
|
* @param {Integer} [opt.retry=3] 輸入傳輸失敗重試次數整數,預設為3
|
|
33
|
-
* @returns {Object}
|
|
34
|
+
* @returns {Object} 回傳事件物件,可使用函數execute、upload
|
|
34
35
|
* @example
|
|
35
36
|
*
|
|
36
37
|
* import WConverhpClient from 'w-converhp/dist/w-converhp-client.umd.js'
|
|
@@ -152,6 +153,14 @@ function WConverhpClient(opt) {
|
|
|
152
153
|
url = _url + '/' + apiName
|
|
153
154
|
}
|
|
154
155
|
|
|
156
|
+
//getToken
|
|
157
|
+
let getToken = get(opt, 'getToken', null)
|
|
158
|
+
if (!isfun(getToken)) {
|
|
159
|
+
getToken = () => {
|
|
160
|
+
return ''
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
155
164
|
//sizeSlice
|
|
156
165
|
let sizeSlice = get(opt, 'sizeSlice')
|
|
157
166
|
if (!ispint(sizeSlice)) {
|
|
@@ -168,6 +177,16 @@ function WConverhpClient(opt) {
|
|
|
168
177
|
let env = isWindow() ? 'browser' : 'nodejs'
|
|
169
178
|
// console.log('env', env)
|
|
170
179
|
|
|
180
|
+
//ee
|
|
181
|
+
let ee = evem() //new events.EventEmitter()
|
|
182
|
+
|
|
183
|
+
//eeEmit
|
|
184
|
+
let eeEmit = (name, ...args) => {
|
|
185
|
+
setTimeout(() => {
|
|
186
|
+
ee.emit(name, ...args)
|
|
187
|
+
}, 1)
|
|
188
|
+
}
|
|
189
|
+
|
|
171
190
|
//res2u8arr
|
|
172
191
|
async function res2u8arr(bb) {
|
|
173
192
|
//blob(in browser) or buffer(in nodejs) to u8a
|
|
@@ -195,7 +214,7 @@ function WConverhpClient(opt) {
|
|
|
195
214
|
}
|
|
196
215
|
|
|
197
216
|
//send
|
|
198
|
-
function send(type, pkg, opt = {}) {
|
|
217
|
+
async function send(type, pkg, opt = {}) {
|
|
199
218
|
|
|
200
219
|
//headers
|
|
201
220
|
let headers = get(opt, 'headers')
|
|
@@ -206,13 +225,16 @@ function WConverhpClient(opt) {
|
|
|
206
225
|
|
|
207
226
|
//dataType
|
|
208
227
|
let dataType = get(opt, 'dataType', '')
|
|
209
|
-
if (dataType !== 'blob' && dataType !== '
|
|
228
|
+
if (dataType !== 'blob' && dataType !== 'fmd' && dataType !== 'json') {
|
|
210
229
|
dataType = 'blob'
|
|
211
230
|
}
|
|
212
231
|
// console.log('dataType', dataType)
|
|
213
232
|
|
|
214
233
|
//cbProgress
|
|
215
234
|
let cbProgress = get(opt, 'cbProgress')
|
|
235
|
+
if (!isfun(cbProgress)) {
|
|
236
|
+
cbProgress = () => {}
|
|
237
|
+
}
|
|
216
238
|
|
|
217
239
|
//urlUse
|
|
218
240
|
let urlUse = ''
|
|
@@ -228,7 +250,6 @@ function WConverhpClient(opt) {
|
|
|
228
250
|
else {
|
|
229
251
|
throw new Error(`invalid type[${type}]`)
|
|
230
252
|
}
|
|
231
|
-
// console.log('urlUse', urlUse)
|
|
232
253
|
|
|
233
254
|
//pm
|
|
234
255
|
let pm = genPm()
|
|
@@ -259,7 +280,8 @@ function WConverhpClient(opt) {
|
|
|
259
280
|
fmd = new opt.FormData({ maxDataSize: 1024 * 1024 * 1024 * 1024 }) //nodejs, 使用套件form-data設定資料量最大為1tb
|
|
260
281
|
}
|
|
261
282
|
else {
|
|
262
|
-
console.log(`invalid opt.FormData, need [npm i form-data] and [import FormData from 'form-data'] to set opt.FormData = FormData`)
|
|
283
|
+
// console.log(`invalid opt.FormData, need [npm i form-data] and [import FormData from 'form-data'] to set opt.FormData = FormData`)
|
|
284
|
+
eeEmit('error', `invalid opt.FormData, need [npm i form-data] and [import FormData from 'form-data'] to set opt.FormData = FormData`)
|
|
263
285
|
throw new Error('invalid opt.FormData')
|
|
264
286
|
}
|
|
265
287
|
}
|
|
@@ -275,17 +297,24 @@ function WConverhpClient(opt) {
|
|
|
275
297
|
}
|
|
276
298
|
// console.log('ct', ct)
|
|
277
299
|
}
|
|
300
|
+
else {
|
|
301
|
+
//browser會自動根據fmd的邊界boundary來設定
|
|
302
|
+
}
|
|
278
303
|
|
|
279
304
|
//set dd
|
|
280
305
|
dd = fmd
|
|
281
306
|
|
|
282
307
|
}
|
|
283
308
|
else if (dataType === 'json') {
|
|
309
|
+
//axios預設會將物件自動轉換為JSON, 此處指定Content-Type且強制轉, 避免可能問題
|
|
310
|
+
|
|
311
|
+
//set ct
|
|
312
|
+
ct = {
|
|
313
|
+
'Content-Type': 'application/json',
|
|
314
|
+
}
|
|
315
|
+
|
|
284
316
|
dd = JSON.stringify(pkg)
|
|
285
317
|
}
|
|
286
|
-
else if (dataType === 'obj') {
|
|
287
|
-
dd = pkg
|
|
288
|
-
}
|
|
289
318
|
// console.log('dd', dd)
|
|
290
319
|
|
|
291
320
|
//rt
|
|
@@ -295,12 +324,19 @@ function WConverhpClient(opt) {
|
|
|
295
324
|
}
|
|
296
325
|
// console.log('rt', rt)
|
|
297
326
|
|
|
327
|
+
//token
|
|
328
|
+
let token = getToken()
|
|
329
|
+
if (ispm(token)) {
|
|
330
|
+
token = await token
|
|
331
|
+
}
|
|
332
|
+
|
|
298
333
|
//s
|
|
299
334
|
let s = {
|
|
300
335
|
method: 'POST',
|
|
301
336
|
url: urlUse,
|
|
302
337
|
data: dd,
|
|
303
338
|
headers: {
|
|
339
|
+
Authorization: `Bearer ${token}`,
|
|
304
340
|
...ct,
|
|
305
341
|
...headers,
|
|
306
342
|
},
|
|
@@ -320,9 +356,7 @@ function WConverhpClient(opt) {
|
|
|
320
356
|
}
|
|
321
357
|
|
|
322
358
|
//cbProgress
|
|
323
|
-
|
|
324
|
-
cbProgress(Math.floor(r), loaded, 'upload')
|
|
325
|
-
}
|
|
359
|
+
cbProgress(Math.floor(r), loaded, 'upload')
|
|
326
360
|
|
|
327
361
|
},
|
|
328
362
|
onDownloadProgress: function (ev) {
|
|
@@ -338,9 +372,7 @@ function WConverhpClient(opt) {
|
|
|
338
372
|
}
|
|
339
373
|
|
|
340
374
|
//cbProgress
|
|
341
|
-
|
|
342
|
-
cbProgress(Math.floor(r), loaded, 'donwload')
|
|
343
|
-
}
|
|
375
|
+
cbProgress(Math.floor(r), loaded, 'download')
|
|
344
376
|
|
|
345
377
|
},
|
|
346
378
|
}
|
|
@@ -365,7 +397,8 @@ function WConverhpClient(opt) {
|
|
|
365
397
|
|
|
366
398
|
//check
|
|
367
399
|
if (!iseobj(data)) {
|
|
368
|
-
console.log('data is not an effective object', data)
|
|
400
|
+
// console.log('data is not an effective object', data)
|
|
401
|
+
eeEmit('error', `data is not an effective object`)
|
|
369
402
|
pm.reject(`data is not an effective object`)
|
|
370
403
|
return
|
|
371
404
|
}
|
|
@@ -375,10 +408,12 @@ function WConverhpClient(opt) {
|
|
|
375
408
|
pm.resolve(data.success)
|
|
376
409
|
}
|
|
377
410
|
else if (haskey(data, 'error')) {
|
|
378
|
-
|
|
411
|
+
eeEmit('error', data.error)
|
|
412
|
+
pm.reject(data.error)
|
|
379
413
|
}
|
|
380
414
|
else {
|
|
381
|
-
console.log('invalid data', data)
|
|
415
|
+
// console.log('invalid data', data)
|
|
416
|
+
eeEmit('error', `invalid data`)
|
|
382
417
|
pm.reject(`invalid data`)
|
|
383
418
|
}
|
|
384
419
|
|
|
@@ -393,6 +428,10 @@ function WConverhpClient(opt) {
|
|
|
393
428
|
//statusText, err
|
|
394
429
|
let statusText = get(res, 'response.statusText') || get(res, 'message')
|
|
395
430
|
let err = get(res, 'response.data') || get(res, 'stack')
|
|
431
|
+
// console.log(`get(res, 'response.statusText')`, get(res, 'response.statusText'))
|
|
432
|
+
// console.log(`get(res, 'message')`, get(res, 'message'))
|
|
433
|
+
// console.log(`get(res, 'response.data')`, get(res, 'response.data'))
|
|
434
|
+
// console.log(`get(res, 'stack')`, get(res, 'stack'))
|
|
396
435
|
|
|
397
436
|
if (statusText) {
|
|
398
437
|
// console.log('statusText', statusText)
|
|
@@ -407,7 +446,8 @@ function WConverhpClient(opt) {
|
|
|
407
446
|
res = res.toJSON()
|
|
408
447
|
}
|
|
409
448
|
catch (err) {}
|
|
410
|
-
console.log('err', res)
|
|
449
|
+
// console.log('err', res)
|
|
450
|
+
eeEmit('error', res)
|
|
411
451
|
data = 'Can not connect to server.'
|
|
412
452
|
}
|
|
413
453
|
if (data === 'Network Error') {
|
|
@@ -420,7 +460,6 @@ function WConverhpClient(opt) {
|
|
|
420
460
|
return pm
|
|
421
461
|
}
|
|
422
462
|
|
|
423
|
-
|
|
424
463
|
//sendPkg
|
|
425
464
|
async function sendPkg(type, data, cbProgress) {
|
|
426
465
|
|
|
@@ -449,7 +488,6 @@ function WConverhpClient(opt) {
|
|
|
449
488
|
return res
|
|
450
489
|
}
|
|
451
490
|
|
|
452
|
-
|
|
453
491
|
//sendData
|
|
454
492
|
async function sendData(type, data, cbProgress) {
|
|
455
493
|
|
|
@@ -466,7 +504,7 @@ function WConverhpClient(opt) {
|
|
|
466
504
|
break
|
|
467
505
|
}
|
|
468
506
|
console.log(`retry n=${n}`)
|
|
469
|
-
r = await fun(data, cbProgress)
|
|
507
|
+
r = await fun(type, data, cbProgress)
|
|
470
508
|
}
|
|
471
509
|
|
|
472
510
|
if (r.state === 'success') {
|
|
@@ -477,7 +515,6 @@ function WConverhpClient(opt) {
|
|
|
477
515
|
}
|
|
478
516
|
}
|
|
479
517
|
|
|
480
|
-
|
|
481
518
|
//sendDataSlice
|
|
482
519
|
async function sendDataSlice(tempId, bb, cbProgress) {
|
|
483
520
|
|
|
@@ -487,7 +524,6 @@ function WConverhpClient(opt) {
|
|
|
487
524
|
try {
|
|
488
525
|
n = bb.size //for Blob
|
|
489
526
|
n = cint(n)
|
|
490
|
-
console.log('size n', n)
|
|
491
527
|
}
|
|
492
528
|
catch (err) {}
|
|
493
529
|
}
|
|
@@ -499,6 +535,7 @@ function WConverhpClient(opt) {
|
|
|
499
535
|
catch (err) {}
|
|
500
536
|
}
|
|
501
537
|
if (n === 0) {
|
|
538
|
+
eeEmit('error', `Can not get size of bb`)
|
|
502
539
|
return Promise.reject(`Can not get size of bb`)
|
|
503
540
|
}
|
|
504
541
|
// console.log('n', n)
|
|
@@ -511,6 +548,29 @@ function WConverhpClient(opt) {
|
|
|
511
548
|
let packageId = `${now2strp()}-${genID()}`
|
|
512
549
|
// console.log('packageId', packageId)
|
|
513
550
|
|
|
551
|
+
//progCount, progWeightSlice
|
|
552
|
+
let progCount = 0
|
|
553
|
+
let progWeightSlice = 0.99 //上傳階段進度使用99%
|
|
554
|
+
// let progWeightMerge = 0.01 //合併階段進度使用1%
|
|
555
|
+
|
|
556
|
+
//cbProgressSlice
|
|
557
|
+
let cbProgressSlice = (perc, _psiz, dir) => {
|
|
558
|
+
if (dir === 'upload' && perc === 100) {
|
|
559
|
+
progCount++
|
|
560
|
+
let r = progCount / chunkTotal
|
|
561
|
+
let prog = r * progWeightSlice * 100
|
|
562
|
+
let psiz = r * n
|
|
563
|
+
cbProgress(prog, psiz, 'upload')
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
//cbProgressMerge
|
|
568
|
+
let cbProgressMerge = (perc, _psiz, dir) => {
|
|
569
|
+
if (dir === 'download' && perc === 100) {
|
|
570
|
+
cbProgress(100, n, 'upload')
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
514
574
|
//upload slice
|
|
515
575
|
for (let i = 0; i < chunkTotal; i++) {
|
|
516
576
|
|
|
@@ -532,19 +592,19 @@ function WConverhpClient(opt) {
|
|
|
532
592
|
|
|
533
593
|
//send slice
|
|
534
594
|
// console.log(`uploading chunk[${i + 1}/${chunkTotal}] of packageId[${packageId}]...`)
|
|
535
|
-
await send('slice', chunk, { headers: hd, dataType: 'blob', cbProgress })
|
|
595
|
+
await send('slice', chunk, { headers: hd, dataType: 'blob', cbProgress: cbProgressSlice })
|
|
536
596
|
// console.log(`upload chunk[${i + 1}/${chunkTotal}] of packageId[${packageId}] done`, res)
|
|
537
597
|
|
|
538
598
|
}
|
|
539
599
|
|
|
540
600
|
//send merge
|
|
541
|
-
|
|
601
|
+
console.log(`merging tempId[${tempId}]...`)
|
|
542
602
|
let msg = {
|
|
543
603
|
'filename': tempId,
|
|
544
604
|
'chunk-total': chunkTotal,
|
|
545
605
|
'package-id': packageId,
|
|
546
606
|
}
|
|
547
|
-
let resMg = await send('slicemerge', msg, { dataType: '
|
|
607
|
+
let resMg = await send('slicemerge', msg, { dataType: 'json', cbProgress: cbProgressMerge })
|
|
548
608
|
// console.log(`merge tempId[${tempId}] done`, resMg)
|
|
549
609
|
|
|
550
610
|
return resMg
|
|
@@ -568,7 +628,14 @@ function WConverhpClient(opt) {
|
|
|
568
628
|
.then((msg) => {
|
|
569
629
|
// console.log('msg', msg)
|
|
570
630
|
|
|
571
|
-
//check
|
|
631
|
+
//check, 若為字串為錯誤訊息
|
|
632
|
+
if (isestr(msg)) {
|
|
633
|
+
state = 'error'
|
|
634
|
+
res = msg
|
|
635
|
+
return
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
//check, 若為非物件為非預期錯誤
|
|
572
639
|
if (!iseobj(msg)) {
|
|
573
640
|
console.log('msg is not an effective object', msg)
|
|
574
641
|
state = 'error'
|
|
@@ -576,7 +643,7 @@ function WConverhpClient(opt) {
|
|
|
576
643
|
return
|
|
577
644
|
}
|
|
578
645
|
|
|
579
|
-
//check,
|
|
646
|
+
//check, 若不存在output為非預期錯誤, msg格式為{func,input,output}但input會刪除
|
|
580
647
|
if (!haskey(msg, 'output')) {
|
|
581
648
|
console.log('invalid msg.output', msg)
|
|
582
649
|
state = 'error'
|
|
@@ -592,25 +659,21 @@ function WConverhpClient(opt) {
|
|
|
592
659
|
res = msg
|
|
593
660
|
})
|
|
594
661
|
|
|
595
|
-
//r
|
|
596
|
-
let r = {
|
|
597
|
-
state,
|
|
598
|
-
msg: res,
|
|
599
|
-
}
|
|
600
|
-
// console.log('sendData r', r)
|
|
601
|
-
|
|
602
662
|
//check
|
|
603
663
|
if (state === '') {
|
|
604
|
-
console.log('invalid state',
|
|
664
|
+
// console.log('invalid state', r)
|
|
665
|
+
eeEmit('error', `invalid state`)
|
|
605
666
|
return Promise.reject('invalid state')
|
|
606
667
|
}
|
|
607
668
|
|
|
608
669
|
//check
|
|
609
670
|
if (state === 'error') {
|
|
610
|
-
|
|
671
|
+
// console.log('send data error', r)
|
|
672
|
+
// eeEmit('error', res) //一般錯誤會嘗試n次, 每次也都會emit, 故此處不再基於已知state='error'時再emit
|
|
673
|
+
return Promise.reject(res)
|
|
611
674
|
}
|
|
612
675
|
|
|
613
|
-
return
|
|
676
|
+
return res
|
|
614
677
|
}
|
|
615
678
|
|
|
616
679
|
//upload
|
|
@@ -622,12 +685,11 @@ function WConverhpClient(opt) {
|
|
|
622
685
|
return sendDataSlice(tempId, bb, cbProgress)
|
|
623
686
|
}
|
|
624
687
|
|
|
625
|
-
//
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
r.upload = upload
|
|
688
|
+
//save
|
|
689
|
+
ee.execute = execute
|
|
690
|
+
ee.upload = upload
|
|
629
691
|
|
|
630
|
-
return
|
|
692
|
+
return ee
|
|
631
693
|
}
|
|
632
694
|
|
|
633
695
|
|
package/src/WConverhpServer.mjs
CHANGED
|
@@ -38,7 +38,7 @@ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
|
|
|
38
38
|
* @param {Integer} [opt.delayForBasic=0] 輸入基本API用延遲響應時間,單位ms,預設0
|
|
39
39
|
* @param {Integer} [opt.delayForSlice=100] 輸入切片上傳檔案API用延遲響應時間,單位ms,預設100
|
|
40
40
|
* @param {Boolean} [opt.serverHapi=null] 輸入外部提供Hapi伺服器物件,預設null
|
|
41
|
-
* @returns {Object}
|
|
41
|
+
* @returns {Object} 回傳事件物件,可監聽事件execute、upload、handler
|
|
42
42
|
* @example
|
|
43
43
|
*
|
|
44
44
|
* import _ from 'lodash-es'
|
|
@@ -194,8 +194,8 @@ function WConverhpServer(opt = {}) {
|
|
|
194
194
|
|
|
195
195
|
//create server
|
|
196
196
|
server = Hapi.server({
|
|
197
|
-
port,
|
|
198
197
|
//host: 'localhost',
|
|
198
|
+
port,
|
|
199
199
|
routes: {
|
|
200
200
|
cors: {
|
|
201
201
|
origin: corsOrigins, //Access-Control-Allow-Origin
|
|
@@ -210,7 +210,7 @@ function WConverhpServer(opt = {}) {
|
|
|
210
210
|
let ee = evem() //new events.EventEmitter()
|
|
211
211
|
|
|
212
212
|
//eeEmit
|
|
213
|
-
|
|
213
|
+
let eeEmit = (name, ...args) => {
|
|
214
214
|
setTimeout(() => {
|
|
215
215
|
ee.emit(name, ...args)
|
|
216
216
|
}, 1)
|
|
@@ -309,7 +309,7 @@ function WConverhpServer(opt = {}) {
|
|
|
309
309
|
maxBytes: 1024 * 1024 * 1024 * 1024, //預設為1mb, 調整至1tb, 也就是給予3次方
|
|
310
310
|
maxParts: 1000 * 1000 * 1000, //預設為1000, 給予3次方
|
|
311
311
|
timeout: false, //避免請求未完成時中斷
|
|
312
|
-
output: 'stream',
|
|
312
|
+
output: 'stream', //前端用blob與application/octet-stream傳
|
|
313
313
|
parse: false,
|
|
314
314
|
},
|
|
315
315
|
timeout: {
|
|
@@ -332,20 +332,31 @@ function WConverhpServer(opt = {}) {
|
|
|
332
332
|
// console.log('query', query)
|
|
333
333
|
|
|
334
334
|
//token
|
|
335
|
-
let token = get(query, 'token', '')
|
|
335
|
+
// let token = get(query, 'token', '')
|
|
336
|
+
let token = get(headers, 'authorization', '')
|
|
337
|
+
token = isestr(token) ? token : ''
|
|
336
338
|
|
|
337
339
|
//check
|
|
338
340
|
if (true) {
|
|
339
341
|
|
|
340
342
|
//funCheck
|
|
341
|
-
let m = funCheck({ token, headers, query })
|
|
343
|
+
let m = funCheck({ apiType: 'main', token, headers, query })
|
|
342
344
|
if (ispm(m)) {
|
|
343
345
|
m = await m
|
|
344
346
|
}
|
|
345
347
|
|
|
346
348
|
//check
|
|
347
349
|
if (m !== true) {
|
|
348
|
-
|
|
350
|
+
|
|
351
|
+
//u8aOut
|
|
352
|
+
let out = {
|
|
353
|
+
error: 'permission denied',
|
|
354
|
+
}
|
|
355
|
+
let u8aOut = obj2u8arr(out)
|
|
356
|
+
// console.log('main u8aOut', u8aOut)
|
|
357
|
+
|
|
358
|
+
// console.log('main return permission denied')
|
|
359
|
+
return responseU8aStream(res, u8aOut)
|
|
349
360
|
}
|
|
350
361
|
|
|
351
362
|
}
|
|
@@ -408,8 +419,8 @@ function WConverhpServer(opt = {}) {
|
|
|
408
419
|
|
|
409
420
|
//error
|
|
410
421
|
req.payload.on('error', (err) => {
|
|
411
|
-
console.log(`receive payload err`, err)
|
|
412
|
-
|
|
422
|
+
// console.log(`receive payload err`, err)
|
|
423
|
+
eeEmit('error', `receive payload err`)
|
|
413
424
|
pm.reject(err.message)
|
|
414
425
|
})
|
|
415
426
|
|
|
@@ -456,7 +467,7 @@ function WConverhpServer(opt = {}) {
|
|
|
456
467
|
maxBytes: 1024 * 1024 * 1024 * 1024, //預設為1mb, 調整至1tb, 也就是給予3次方
|
|
457
468
|
maxParts: 1000 * 1000 * 1000, //預設為1000, 給予3次方
|
|
458
469
|
timeout: false, //避免請求未完成時中斷
|
|
459
|
-
output: 'stream',
|
|
470
|
+
output: 'stream', //前端用blob與application/octet-stream傳
|
|
460
471
|
parse: false,
|
|
461
472
|
},
|
|
462
473
|
timeout: {
|
|
@@ -479,20 +490,31 @@ function WConverhpServer(opt = {}) {
|
|
|
479
490
|
// console.log('query', query)
|
|
480
491
|
|
|
481
492
|
//token
|
|
482
|
-
let token = get(query, 'token', '')
|
|
493
|
+
// let token = get(query, 'token', '')
|
|
494
|
+
let token = get(headers, 'authorization', '')
|
|
495
|
+
token = isestr(token) ? token : ''
|
|
483
496
|
|
|
484
497
|
//check
|
|
485
498
|
if (true) {
|
|
486
499
|
|
|
487
500
|
//funCheck
|
|
488
|
-
let m = funCheck({ token, headers, query })
|
|
501
|
+
let m = funCheck({ apiType: 'slice', token, headers, query })
|
|
489
502
|
if (ispm(m)) {
|
|
490
503
|
m = await m
|
|
491
504
|
}
|
|
492
505
|
|
|
493
506
|
//check
|
|
494
507
|
if (m !== true) {
|
|
495
|
-
|
|
508
|
+
|
|
509
|
+
//u8aOut
|
|
510
|
+
let out = {
|
|
511
|
+
error: 'permission denied',
|
|
512
|
+
}
|
|
513
|
+
let u8aOut = obj2u8arr(out)
|
|
514
|
+
// console.log('slice u8aOut', u8aOut)
|
|
515
|
+
|
|
516
|
+
// console.log('slice return permission denied')
|
|
517
|
+
return responseU8aStream(res, u8aOut)
|
|
496
518
|
}
|
|
497
519
|
|
|
498
520
|
}
|
|
@@ -554,8 +576,9 @@ function WConverhpServer(opt = {}) {
|
|
|
554
576
|
})
|
|
555
577
|
|
|
556
578
|
//error
|
|
557
|
-
req.payload.on('error', (
|
|
558
|
-
console.log(`receive chunk[${chunkIndex + 1}/${chunkTotal}] err`, err)
|
|
579
|
+
req.payload.on('error', () => {
|
|
580
|
+
// console.log(`receive chunk[${chunkIndex + 1}/${chunkTotal}] err`, err)
|
|
581
|
+
eeEmit('error', `receive chunk[${chunkIndex + 1}/${chunkTotal}] err`)
|
|
559
582
|
pm.reject(`chunk[${chunkIndex}/${chunkTotal}] of packageId[${packageId}] err`)
|
|
560
583
|
})
|
|
561
584
|
|
|
@@ -590,7 +613,8 @@ function WConverhpServer(opt = {}) {
|
|
|
590
613
|
maxBytes: 1024 * 1024 * 1024 * 1024, //預設為1mb, 調整至1tb, 也就是給予3次方
|
|
591
614
|
maxParts: 1000 * 1000 * 1000, //預設為1000, 給予3次方
|
|
592
615
|
timeout: false, //避免請求未完成時中斷
|
|
593
|
-
|
|
616
|
+
// output: 'stream',
|
|
617
|
+
parse: true, //前端送obj過來
|
|
594
618
|
},
|
|
595
619
|
timeout: {
|
|
596
620
|
socket: false, //避免socket自動關閉
|
|
@@ -612,20 +636,31 @@ function WConverhpServer(opt = {}) {
|
|
|
612
636
|
// console.log('query', query)
|
|
613
637
|
|
|
614
638
|
//token
|
|
615
|
-
let token = get(query, 'token', '')
|
|
639
|
+
// let token = get(query, 'token', '')
|
|
640
|
+
let token = get(headers, 'authorization', '')
|
|
641
|
+
token = isestr(token) ? token : ''
|
|
616
642
|
|
|
617
643
|
//check
|
|
618
644
|
if (true) {
|
|
619
645
|
|
|
620
646
|
//funCheck
|
|
621
|
-
let m = funCheck({ token, headers, query })
|
|
647
|
+
let m = funCheck({ apiType: 'merge', token, headers, query })
|
|
622
648
|
if (ispm(m)) {
|
|
623
649
|
m = await m
|
|
624
650
|
}
|
|
625
651
|
|
|
626
652
|
//check
|
|
627
653
|
if (m !== true) {
|
|
628
|
-
|
|
654
|
+
|
|
655
|
+
//u8aOut
|
|
656
|
+
let out = {
|
|
657
|
+
error: 'permission denied',
|
|
658
|
+
}
|
|
659
|
+
let u8aOut = obj2u8arr(out)
|
|
660
|
+
// console.log('mergeu8aOut', u8aOut)
|
|
661
|
+
|
|
662
|
+
// console.log('merge return permission denied')
|
|
663
|
+
return responseU8aStream(res, u8aOut)
|
|
629
664
|
}
|
|
630
665
|
|
|
631
666
|
}
|
|
@@ -683,7 +718,6 @@ function WConverhpServer(opt = {}) {
|
|
|
683
718
|
//check
|
|
684
719
|
if (!fsIsFile(pathFileChunk)) {
|
|
685
720
|
// console.log(`pathFileChunk[${pathFileChunk}] is not a file`)
|
|
686
|
-
// return res.response({ error: `Missing chunk ${i} of filename[${filename}]` }).code(400)
|
|
687
721
|
throw new Error(`Missing chunk ${i} of filename[${filename}]`)
|
|
688
722
|
}
|
|
689
723
|
|
package/srv.mjs
CHANGED
|
@@ -6,7 +6,8 @@ let opt = {
|
|
|
6
6
|
port: 8080,
|
|
7
7
|
apiName: 'api',
|
|
8
8
|
pathStaticFiles: '.', //要存取專案資料夾下web.html, 故不能給dist
|
|
9
|
-
funCheck: () => {
|
|
9
|
+
funCheck: ({ apiType, token, headers, query }) => {
|
|
10
|
+
console.log('funCheck', apiType, token)
|
|
10
11
|
return true
|
|
11
12
|
},
|
|
12
13
|
}
|
|
@@ -69,6 +70,9 @@ wo.on('upload', function(input, pm) {
|
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
})
|
|
73
|
+
wo.on('error', function(err) {
|
|
74
|
+
console.log(`Server[port:${opt.port}]: error`, err)
|
|
75
|
+
})
|
|
72
76
|
wo.on('handler', function(data) {
|
|
73
77
|
// console.log(`Server[port:${opt.port}]: handler`, data)
|
|
74
78
|
})
|