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
|
@@ -46,8 +46,6 @@
|
|
|
46
46
|
<section>
|
|
47
47
|
<article>
|
|
48
48
|
<pre class="prettyprint source linenums"><code>import axios from 'axios'
|
|
49
|
-
// import * as FormData from 'form-data/lib/form_data.js'
|
|
50
|
-
// import FormData from 'form-data'
|
|
51
49
|
import get from 'lodash-es/get.js'
|
|
52
50
|
import isWindow from 'wsemi/src/isWindow.mjs'
|
|
53
51
|
import genPm from 'wsemi/src/genPm.mjs'
|
|
@@ -59,8 +57,10 @@ import isp0int from 'wsemi/src/isp0int.mjs'
|
|
|
59
57
|
import isestr from 'wsemi/src/isestr.mjs'
|
|
60
58
|
import isobj from 'wsemi/src/isobj.mjs'
|
|
61
59
|
import iseobj from 'wsemi/src/iseobj.mjs'
|
|
60
|
+
import ispm from 'wsemi/src/ispm.mjs'
|
|
62
61
|
import cint from 'wsemi/src/cint.mjs'
|
|
63
62
|
import strright from 'wsemi/src/strright.mjs'
|
|
63
|
+
import evem from 'wsemi/src/evem.mjs'
|
|
64
64
|
import blob2u8arr from 'wsemi/src/blob2u8arr.mjs'
|
|
65
65
|
import obj2u8arr from 'wsemi/src/obj2u8arr.mjs'
|
|
66
66
|
import u8arr2obj from 'wsemi/src/u8arr2obj.mjs'
|
|
@@ -75,9 +75,10 @@ import now2strp from 'wsemi/src/now2strp.mjs'
|
|
|
75
75
|
* @param {Object} opt 輸入設定參數物件
|
|
76
76
|
* @param {String} [opt.url='http://localhost:8080'] 輸入Hapi伺服器網址,預設為'http://localhost:8080'
|
|
77
77
|
* @param {String} [opt.apiName='api'] 輸入API名稱字串,預設'api'
|
|
78
|
+
* @param {Function} [opt.getToken=()=>''] 輸入取得使用者token的回調函數,預設()=>''
|
|
78
79
|
* @param {Integer} [opt.sizeSlice=1024*1024] 輸入切片上傳檔案之切片檔案大小整數,單位為Byte,預設為1024*1024
|
|
79
80
|
* @param {Integer} [opt.retry=3] 輸入傳輸失敗重試次數整數,預設為3
|
|
80
|
-
* @returns {Object}
|
|
81
|
+
* @returns {Object} 回傳事件物件,可使用函數execute、upload
|
|
81
82
|
* @example
|
|
82
83
|
*
|
|
83
84
|
* import WConverhpClient from 'w-converhp/dist/w-converhp-client.umd.js'
|
|
@@ -199,6 +200,14 @@ function WConverhpClient(opt) {
|
|
|
199
200
|
url = _url + '/' + apiName
|
|
200
201
|
}
|
|
201
202
|
|
|
203
|
+
//getToken
|
|
204
|
+
let getToken = get(opt, 'getToken', null)
|
|
205
|
+
if (!isfun(getToken)) {
|
|
206
|
+
getToken = () => {
|
|
207
|
+
return ''
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
202
211
|
//sizeSlice
|
|
203
212
|
let sizeSlice = get(opt, 'sizeSlice')
|
|
204
213
|
if (!ispint(sizeSlice)) {
|
|
@@ -215,6 +224,16 @@ function WConverhpClient(opt) {
|
|
|
215
224
|
let env = isWindow() ? 'browser' : 'nodejs'
|
|
216
225
|
// console.log('env', env)
|
|
217
226
|
|
|
227
|
+
//ee
|
|
228
|
+
let ee = evem() //new events.EventEmitter()
|
|
229
|
+
|
|
230
|
+
//eeEmit
|
|
231
|
+
let eeEmit = (name, ...args) => {
|
|
232
|
+
setTimeout(() => {
|
|
233
|
+
ee.emit(name, ...args)
|
|
234
|
+
}, 1)
|
|
235
|
+
}
|
|
236
|
+
|
|
218
237
|
//res2u8arr
|
|
219
238
|
async function res2u8arr(bb) {
|
|
220
239
|
//blob(in browser) or buffer(in nodejs) to u8a
|
|
@@ -242,7 +261,7 @@ function WConverhpClient(opt) {
|
|
|
242
261
|
}
|
|
243
262
|
|
|
244
263
|
//send
|
|
245
|
-
function send(type, pkg, opt = {}) {
|
|
264
|
+
async function send(type, pkg, opt = {}) {
|
|
246
265
|
|
|
247
266
|
//headers
|
|
248
267
|
let headers = get(opt, 'headers')
|
|
@@ -253,13 +272,16 @@ function WConverhpClient(opt) {
|
|
|
253
272
|
|
|
254
273
|
//dataType
|
|
255
274
|
let dataType = get(opt, 'dataType', '')
|
|
256
|
-
if (dataType !== 'blob' && dataType !== '
|
|
275
|
+
if (dataType !== 'blob' && dataType !== 'fmd' && dataType !== 'json') {
|
|
257
276
|
dataType = 'blob'
|
|
258
277
|
}
|
|
259
278
|
// console.log('dataType', dataType)
|
|
260
279
|
|
|
261
280
|
//cbProgress
|
|
262
281
|
let cbProgress = get(opt, 'cbProgress')
|
|
282
|
+
if (!isfun(cbProgress)) {
|
|
283
|
+
cbProgress = () => {}
|
|
284
|
+
}
|
|
263
285
|
|
|
264
286
|
//urlUse
|
|
265
287
|
let urlUse = ''
|
|
@@ -275,7 +297,6 @@ function WConverhpClient(opt) {
|
|
|
275
297
|
else {
|
|
276
298
|
throw new Error(`invalid type[${type}]`)
|
|
277
299
|
}
|
|
278
|
-
// console.log('urlUse', urlUse)
|
|
279
300
|
|
|
280
301
|
//pm
|
|
281
302
|
let pm = genPm()
|
|
@@ -306,7 +327,8 @@ function WConverhpClient(opt) {
|
|
|
306
327
|
fmd = new opt.FormData({ maxDataSize: 1024 * 1024 * 1024 * 1024 }) //nodejs, 使用套件form-data設定資料量最大為1tb
|
|
307
328
|
}
|
|
308
329
|
else {
|
|
309
|
-
console.log(`invalid opt.FormData, need [npm i form-data] and [import FormData from 'form-data'] to set opt.FormData = FormData`)
|
|
330
|
+
// console.log(`invalid opt.FormData, need [npm i form-data] and [import FormData from 'form-data'] to set opt.FormData = FormData`)
|
|
331
|
+
eeEmit('error', `invalid opt.FormData, need [npm i form-data] and [import FormData from 'form-data'] to set opt.FormData = FormData`)
|
|
310
332
|
throw new Error('invalid opt.FormData')
|
|
311
333
|
}
|
|
312
334
|
}
|
|
@@ -322,17 +344,24 @@ function WConverhpClient(opt) {
|
|
|
322
344
|
}
|
|
323
345
|
// console.log('ct', ct)
|
|
324
346
|
}
|
|
347
|
+
else {
|
|
348
|
+
//browser會自動根據fmd的邊界boundary來設定
|
|
349
|
+
}
|
|
325
350
|
|
|
326
351
|
//set dd
|
|
327
352
|
dd = fmd
|
|
328
353
|
|
|
329
354
|
}
|
|
330
355
|
else if (dataType === 'json') {
|
|
356
|
+
//axios預設會將物件自動轉換為JSON, 此處指定Content-Type且強制轉, 避免可能問題
|
|
357
|
+
|
|
358
|
+
//set ct
|
|
359
|
+
ct = {
|
|
360
|
+
'Content-Type': 'application/json',
|
|
361
|
+
}
|
|
362
|
+
|
|
331
363
|
dd = JSON.stringify(pkg)
|
|
332
364
|
}
|
|
333
|
-
else if (dataType === 'obj') {
|
|
334
|
-
dd = pkg
|
|
335
|
-
}
|
|
336
365
|
// console.log('dd', dd)
|
|
337
366
|
|
|
338
367
|
//rt
|
|
@@ -342,12 +371,19 @@ function WConverhpClient(opt) {
|
|
|
342
371
|
}
|
|
343
372
|
// console.log('rt', rt)
|
|
344
373
|
|
|
374
|
+
//token
|
|
375
|
+
let token = getToken()
|
|
376
|
+
if (ispm(token)) {
|
|
377
|
+
token = await token
|
|
378
|
+
}
|
|
379
|
+
|
|
345
380
|
//s
|
|
346
381
|
let s = {
|
|
347
382
|
method: 'POST',
|
|
348
383
|
url: urlUse,
|
|
349
384
|
data: dd,
|
|
350
385
|
headers: {
|
|
386
|
+
Authorization: `Bearer ${token}`,
|
|
351
387
|
...ct,
|
|
352
388
|
...headers,
|
|
353
389
|
},
|
|
@@ -367,9 +403,7 @@ function WConverhpClient(opt) {
|
|
|
367
403
|
}
|
|
368
404
|
|
|
369
405
|
//cbProgress
|
|
370
|
-
|
|
371
|
-
cbProgress(Math.floor(r), loaded, 'upload')
|
|
372
|
-
}
|
|
406
|
+
cbProgress(Math.floor(r), loaded, 'upload')
|
|
373
407
|
|
|
374
408
|
},
|
|
375
409
|
onDownloadProgress: function (ev) {
|
|
@@ -385,9 +419,7 @@ function WConverhpClient(opt) {
|
|
|
385
419
|
}
|
|
386
420
|
|
|
387
421
|
//cbProgress
|
|
388
|
-
|
|
389
|
-
cbProgress(Math.floor(r), loaded, 'donwload')
|
|
390
|
-
}
|
|
422
|
+
cbProgress(Math.floor(r), loaded, 'download')
|
|
391
423
|
|
|
392
424
|
},
|
|
393
425
|
}
|
|
@@ -412,7 +444,8 @@ function WConverhpClient(opt) {
|
|
|
412
444
|
|
|
413
445
|
//check
|
|
414
446
|
if (!iseobj(data)) {
|
|
415
|
-
console.log('data is not an effective object', data)
|
|
447
|
+
// console.log('data is not an effective object', data)
|
|
448
|
+
eeEmit('error', `data is not an effective object`)
|
|
416
449
|
pm.reject(`data is not an effective object`)
|
|
417
450
|
return
|
|
418
451
|
}
|
|
@@ -422,10 +455,12 @@ function WConverhpClient(opt) {
|
|
|
422
455
|
pm.resolve(data.success)
|
|
423
456
|
}
|
|
424
457
|
else if (haskey(data, 'error')) {
|
|
425
|
-
|
|
458
|
+
eeEmit('error', data.error)
|
|
459
|
+
pm.reject(data.error)
|
|
426
460
|
}
|
|
427
461
|
else {
|
|
428
|
-
console.log('invalid data', data)
|
|
462
|
+
// console.log('invalid data', data)
|
|
463
|
+
eeEmit('error', `invalid data`)
|
|
429
464
|
pm.reject(`invalid data`)
|
|
430
465
|
}
|
|
431
466
|
|
|
@@ -440,6 +475,10 @@ function WConverhpClient(opt) {
|
|
|
440
475
|
//statusText, err
|
|
441
476
|
let statusText = get(res, 'response.statusText') || get(res, 'message')
|
|
442
477
|
let err = get(res, 'response.data') || get(res, 'stack')
|
|
478
|
+
// console.log(`get(res, 'response.statusText')`, get(res, 'response.statusText'))
|
|
479
|
+
// console.log(`get(res, 'message')`, get(res, 'message'))
|
|
480
|
+
// console.log(`get(res, 'response.data')`, get(res, 'response.data'))
|
|
481
|
+
// console.log(`get(res, 'stack')`, get(res, 'stack'))
|
|
443
482
|
|
|
444
483
|
if (statusText) {
|
|
445
484
|
// console.log('statusText', statusText)
|
|
@@ -454,7 +493,8 @@ function WConverhpClient(opt) {
|
|
|
454
493
|
res = res.toJSON()
|
|
455
494
|
}
|
|
456
495
|
catch (err) {}
|
|
457
|
-
console.log('err', res)
|
|
496
|
+
// console.log('err', res)
|
|
497
|
+
eeEmit('error', res)
|
|
458
498
|
data = 'Can not connect to server.'
|
|
459
499
|
}
|
|
460
500
|
if (data === 'Network Error') {
|
|
@@ -467,7 +507,6 @@ function WConverhpClient(opt) {
|
|
|
467
507
|
return pm
|
|
468
508
|
}
|
|
469
509
|
|
|
470
|
-
|
|
471
510
|
//sendPkg
|
|
472
511
|
async function sendPkg(type, data, cbProgress) {
|
|
473
512
|
|
|
@@ -496,7 +535,6 @@ function WConverhpClient(opt) {
|
|
|
496
535
|
return res
|
|
497
536
|
}
|
|
498
537
|
|
|
499
|
-
|
|
500
538
|
//sendData
|
|
501
539
|
async function sendData(type, data, cbProgress) {
|
|
502
540
|
|
|
@@ -513,7 +551,7 @@ function WConverhpClient(opt) {
|
|
|
513
551
|
break
|
|
514
552
|
}
|
|
515
553
|
console.log(`retry n=${n}`)
|
|
516
|
-
r = await fun(data, cbProgress)
|
|
554
|
+
r = await fun(type, data, cbProgress)
|
|
517
555
|
}
|
|
518
556
|
|
|
519
557
|
if (r.state === 'success') {
|
|
@@ -524,7 +562,6 @@ function WConverhpClient(opt) {
|
|
|
524
562
|
}
|
|
525
563
|
}
|
|
526
564
|
|
|
527
|
-
|
|
528
565
|
//sendDataSlice
|
|
529
566
|
async function sendDataSlice(tempId, bb, cbProgress) {
|
|
530
567
|
|
|
@@ -534,7 +571,6 @@ function WConverhpClient(opt) {
|
|
|
534
571
|
try {
|
|
535
572
|
n = bb.size //for Blob
|
|
536
573
|
n = cint(n)
|
|
537
|
-
console.log('size n', n)
|
|
538
574
|
}
|
|
539
575
|
catch (err) {}
|
|
540
576
|
}
|
|
@@ -546,6 +582,7 @@ function WConverhpClient(opt) {
|
|
|
546
582
|
catch (err) {}
|
|
547
583
|
}
|
|
548
584
|
if (n === 0) {
|
|
585
|
+
eeEmit('error', `Can not get size of bb`)
|
|
549
586
|
return Promise.reject(`Can not get size of bb`)
|
|
550
587
|
}
|
|
551
588
|
// console.log('n', n)
|
|
@@ -558,6 +595,29 @@ function WConverhpClient(opt) {
|
|
|
558
595
|
let packageId = `${now2strp()}-${genID()}`
|
|
559
596
|
// console.log('packageId', packageId)
|
|
560
597
|
|
|
598
|
+
//progCount, progWeightSlice
|
|
599
|
+
let progCount = 0
|
|
600
|
+
let progWeightSlice = 0.99 //上傳階段進度使用99%
|
|
601
|
+
// let progWeightMerge = 0.01 //合併階段進度使用1%
|
|
602
|
+
|
|
603
|
+
//cbProgressSlice
|
|
604
|
+
let cbProgressSlice = (perc, _psiz, dir) => {
|
|
605
|
+
if (dir === 'upload' && perc === 100) {
|
|
606
|
+
progCount++
|
|
607
|
+
let r = progCount / chunkTotal
|
|
608
|
+
let prog = r * progWeightSlice * 100
|
|
609
|
+
let psiz = r * n
|
|
610
|
+
cbProgress(prog, psiz, 'upload')
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
//cbProgressMerge
|
|
615
|
+
let cbProgressMerge = (perc, _psiz, dir) => {
|
|
616
|
+
if (dir === 'download' && perc === 100) {
|
|
617
|
+
cbProgress(100, n, 'upload')
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
561
621
|
//upload slice
|
|
562
622
|
for (let i = 0; i < chunkTotal; i++) {
|
|
563
623
|
|
|
@@ -579,19 +639,19 @@ function WConverhpClient(opt) {
|
|
|
579
639
|
|
|
580
640
|
//send slice
|
|
581
641
|
// console.log(`uploading chunk[${i + 1}/${chunkTotal}] of packageId[${packageId}]...`)
|
|
582
|
-
await send('slice', chunk, { headers: hd, dataType: 'blob', cbProgress })
|
|
642
|
+
await send('slice', chunk, { headers: hd, dataType: 'blob', cbProgress: cbProgressSlice })
|
|
583
643
|
// console.log(`upload chunk[${i + 1}/${chunkTotal}] of packageId[${packageId}] done`, res)
|
|
584
644
|
|
|
585
645
|
}
|
|
586
646
|
|
|
587
647
|
//send merge
|
|
588
|
-
|
|
648
|
+
console.log(`merging tempId[${tempId}]...`)
|
|
589
649
|
let msg = {
|
|
590
650
|
'filename': tempId,
|
|
591
651
|
'chunk-total': chunkTotal,
|
|
592
652
|
'package-id': packageId,
|
|
593
653
|
}
|
|
594
|
-
let resMg = await send('slicemerge', msg, { dataType: '
|
|
654
|
+
let resMg = await send('slicemerge', msg, { dataType: 'json', cbProgress: cbProgressMerge })
|
|
595
655
|
// console.log(`merge tempId[${tempId}] done`, resMg)
|
|
596
656
|
|
|
597
657
|
return resMg
|
|
@@ -615,7 +675,14 @@ function WConverhpClient(opt) {
|
|
|
615
675
|
.then((msg) => {
|
|
616
676
|
// console.log('msg', msg)
|
|
617
677
|
|
|
618
|
-
//check
|
|
678
|
+
//check, 若為字串為錯誤訊息
|
|
679
|
+
if (isestr(msg)) {
|
|
680
|
+
state = 'error'
|
|
681
|
+
res = msg
|
|
682
|
+
return
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
//check, 若為非物件為非預期錯誤
|
|
619
686
|
if (!iseobj(msg)) {
|
|
620
687
|
console.log('msg is not an effective object', msg)
|
|
621
688
|
state = 'error'
|
|
@@ -623,7 +690,7 @@ function WConverhpClient(opt) {
|
|
|
623
690
|
return
|
|
624
691
|
}
|
|
625
692
|
|
|
626
|
-
//check,
|
|
693
|
+
//check, 若不存在output為非預期錯誤, msg格式為{func,input,output}但input會刪除
|
|
627
694
|
if (!haskey(msg, 'output')) {
|
|
628
695
|
console.log('invalid msg.output', msg)
|
|
629
696
|
state = 'error'
|
|
@@ -639,25 +706,21 @@ function WConverhpClient(opt) {
|
|
|
639
706
|
res = msg
|
|
640
707
|
})
|
|
641
708
|
|
|
642
|
-
//r
|
|
643
|
-
let r = {
|
|
644
|
-
state,
|
|
645
|
-
msg: res,
|
|
646
|
-
}
|
|
647
|
-
// console.log('sendData r', r)
|
|
648
|
-
|
|
649
709
|
//check
|
|
650
710
|
if (state === '') {
|
|
651
|
-
console.log('invalid state',
|
|
711
|
+
// console.log('invalid state', r)
|
|
712
|
+
eeEmit('error', `invalid state`)
|
|
652
713
|
return Promise.reject('invalid state')
|
|
653
714
|
}
|
|
654
715
|
|
|
655
716
|
//check
|
|
656
717
|
if (state === 'error') {
|
|
657
|
-
|
|
718
|
+
// console.log('send data error', r)
|
|
719
|
+
// eeEmit('error', res) //一般錯誤會嘗試n次, 每次也都會emit, 故此處不再基於已知state='error'時再emit
|
|
720
|
+
return Promise.reject(res)
|
|
658
721
|
}
|
|
659
722
|
|
|
660
|
-
return
|
|
723
|
+
return res
|
|
661
724
|
}
|
|
662
725
|
|
|
663
726
|
//upload
|
|
@@ -669,12 +732,11 @@ function WConverhpClient(opt) {
|
|
|
669
732
|
return sendDataSlice(tempId, bb, cbProgress)
|
|
670
733
|
}
|
|
671
734
|
|
|
672
|
-
//
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
r.upload = upload
|
|
735
|
+
//save
|
|
736
|
+
ee.execute = execute
|
|
737
|
+
ee.upload = upload
|
|
676
738
|
|
|
677
|
-
return
|
|
739
|
+
return ee
|
|
678
740
|
}
|
|
679
741
|
|
|
680
742
|
|
|
@@ -693,7 +755,7 @@ export default WConverhpClient
|
|
|
693
755
|
<br class="clear">
|
|
694
756
|
|
|
695
757
|
<footer>
|
|
696
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on
|
|
758
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 30 2025 19:42:30 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
697
759
|
</footer>
|
|
698
760
|
|
|
699
761
|
<script>prettyPrint();</script>
|
|
@@ -650,7 +650,7 @@
|
|
|
650
650
|
|
|
651
651
|
|
|
652
652
|
<div class="param-desc">
|
|
653
|
-
<p
|
|
653
|
+
<p>回傳事件物件,可監聽事件execute、upload、handler</p>
|
|
654
654
|
</div>
|
|
655
655
|
|
|
656
656
|
|
|
@@ -709,7 +709,7 @@
|
|
|
709
709
|
<br class="clear">
|
|
710
710
|
|
|
711
711
|
<footer>
|
|
712
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on
|
|
712
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 30 2025 19:42:30 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
713
713
|
</footer>
|
|
714
714
|
|
|
715
715
|
<script>prettyPrint();</script>
|
|
@@ -85,7 +85,7 @@ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
|
|
|
85
85
|
* @param {Integer} [opt.delayForBasic=0] 輸入基本API用延遲響應時間,單位ms,預設0
|
|
86
86
|
* @param {Integer} [opt.delayForSlice=100] 輸入切片上傳檔案API用延遲響應時間,單位ms,預設100
|
|
87
87
|
* @param {Boolean} [opt.serverHapi=null] 輸入外部提供Hapi伺服器物件,預設null
|
|
88
|
-
* @returns {Object}
|
|
88
|
+
* @returns {Object} 回傳事件物件,可監聽事件execute、upload、handler
|
|
89
89
|
* @example
|
|
90
90
|
*
|
|
91
91
|
* import _ from 'lodash-es'
|
|
@@ -241,8 +241,8 @@ function WConverhpServer(opt = {}) {
|
|
|
241
241
|
|
|
242
242
|
//create server
|
|
243
243
|
server = Hapi.server({
|
|
244
|
-
port,
|
|
245
244
|
//host: 'localhost',
|
|
245
|
+
port,
|
|
246
246
|
routes: {
|
|
247
247
|
cors: {
|
|
248
248
|
origin: corsOrigins, //Access-Control-Allow-Origin
|
|
@@ -257,7 +257,7 @@ function WConverhpServer(opt = {}) {
|
|
|
257
257
|
let ee = evem() //new events.EventEmitter()
|
|
258
258
|
|
|
259
259
|
//eeEmit
|
|
260
|
-
|
|
260
|
+
let eeEmit = (name, ...args) => {
|
|
261
261
|
setTimeout(() => {
|
|
262
262
|
ee.emit(name, ...args)
|
|
263
263
|
}, 1)
|
|
@@ -356,7 +356,7 @@ function WConverhpServer(opt = {}) {
|
|
|
356
356
|
maxBytes: 1024 * 1024 * 1024 * 1024, //預設為1mb, 調整至1tb, 也就是給予3次方
|
|
357
357
|
maxParts: 1000 * 1000 * 1000, //預設為1000, 給予3次方
|
|
358
358
|
timeout: false, //避免請求未完成時中斷
|
|
359
|
-
output: 'stream',
|
|
359
|
+
output: 'stream', //前端用blob與application/octet-stream傳
|
|
360
360
|
parse: false,
|
|
361
361
|
},
|
|
362
362
|
timeout: {
|
|
@@ -379,20 +379,31 @@ function WConverhpServer(opt = {}) {
|
|
|
379
379
|
// console.log('query', query)
|
|
380
380
|
|
|
381
381
|
//token
|
|
382
|
-
let token = get(query, 'token', '')
|
|
382
|
+
// let token = get(query, 'token', '')
|
|
383
|
+
let token = get(headers, 'authorization', '')
|
|
384
|
+
token = isestr(token) ? token : ''
|
|
383
385
|
|
|
384
386
|
//check
|
|
385
387
|
if (true) {
|
|
386
388
|
|
|
387
389
|
//funCheck
|
|
388
|
-
let m = funCheck({ token, headers, query })
|
|
390
|
+
let m = funCheck({ apiType: 'main', token, headers, query })
|
|
389
391
|
if (ispm(m)) {
|
|
390
392
|
m = await m
|
|
391
393
|
}
|
|
392
394
|
|
|
393
395
|
//check
|
|
394
396
|
if (m !== true) {
|
|
395
|
-
|
|
397
|
+
|
|
398
|
+
//u8aOut
|
|
399
|
+
let out = {
|
|
400
|
+
error: 'permission denied',
|
|
401
|
+
}
|
|
402
|
+
let u8aOut = obj2u8arr(out)
|
|
403
|
+
// console.log('main u8aOut', u8aOut)
|
|
404
|
+
|
|
405
|
+
// console.log('main return permission denied')
|
|
406
|
+
return responseU8aStream(res, u8aOut)
|
|
396
407
|
}
|
|
397
408
|
|
|
398
409
|
}
|
|
@@ -455,8 +466,8 @@ function WConverhpServer(opt = {}) {
|
|
|
455
466
|
|
|
456
467
|
//error
|
|
457
468
|
req.payload.on('error', (err) => {
|
|
458
|
-
console.log(`receive payload err`, err)
|
|
459
|
-
|
|
469
|
+
// console.log(`receive payload err`, err)
|
|
470
|
+
eeEmit('error', `receive payload err`)
|
|
460
471
|
pm.reject(err.message)
|
|
461
472
|
})
|
|
462
473
|
|
|
@@ -503,7 +514,7 @@ function WConverhpServer(opt = {}) {
|
|
|
503
514
|
maxBytes: 1024 * 1024 * 1024 * 1024, //預設為1mb, 調整至1tb, 也就是給予3次方
|
|
504
515
|
maxParts: 1000 * 1000 * 1000, //預設為1000, 給予3次方
|
|
505
516
|
timeout: false, //避免請求未完成時中斷
|
|
506
|
-
output: 'stream',
|
|
517
|
+
output: 'stream', //前端用blob與application/octet-stream傳
|
|
507
518
|
parse: false,
|
|
508
519
|
},
|
|
509
520
|
timeout: {
|
|
@@ -526,20 +537,31 @@ function WConverhpServer(opt = {}) {
|
|
|
526
537
|
// console.log('query', query)
|
|
527
538
|
|
|
528
539
|
//token
|
|
529
|
-
let token = get(query, 'token', '')
|
|
540
|
+
// let token = get(query, 'token', '')
|
|
541
|
+
let token = get(headers, 'authorization', '')
|
|
542
|
+
token = isestr(token) ? token : ''
|
|
530
543
|
|
|
531
544
|
//check
|
|
532
545
|
if (true) {
|
|
533
546
|
|
|
534
547
|
//funCheck
|
|
535
|
-
let m = funCheck({ token, headers, query })
|
|
548
|
+
let m = funCheck({ apiType: 'slice', token, headers, query })
|
|
536
549
|
if (ispm(m)) {
|
|
537
550
|
m = await m
|
|
538
551
|
}
|
|
539
552
|
|
|
540
553
|
//check
|
|
541
554
|
if (m !== true) {
|
|
542
|
-
|
|
555
|
+
|
|
556
|
+
//u8aOut
|
|
557
|
+
let out = {
|
|
558
|
+
error: 'permission denied',
|
|
559
|
+
}
|
|
560
|
+
let u8aOut = obj2u8arr(out)
|
|
561
|
+
// console.log('slice u8aOut', u8aOut)
|
|
562
|
+
|
|
563
|
+
// console.log('slice return permission denied')
|
|
564
|
+
return responseU8aStream(res, u8aOut)
|
|
543
565
|
}
|
|
544
566
|
|
|
545
567
|
}
|
|
@@ -601,8 +623,9 @@ function WConverhpServer(opt = {}) {
|
|
|
601
623
|
})
|
|
602
624
|
|
|
603
625
|
//error
|
|
604
|
-
req.payload.on('error', (
|
|
605
|
-
console.log(`receive chunk[${chunkIndex + 1}/${chunkTotal}] err`, err)
|
|
626
|
+
req.payload.on('error', () => {
|
|
627
|
+
// console.log(`receive chunk[${chunkIndex + 1}/${chunkTotal}] err`, err)
|
|
628
|
+
eeEmit('error', `receive chunk[${chunkIndex + 1}/${chunkTotal}] err`)
|
|
606
629
|
pm.reject(`chunk[${chunkIndex}/${chunkTotal}] of packageId[${packageId}] err`)
|
|
607
630
|
})
|
|
608
631
|
|
|
@@ -637,7 +660,8 @@ function WConverhpServer(opt = {}) {
|
|
|
637
660
|
maxBytes: 1024 * 1024 * 1024 * 1024, //預設為1mb, 調整至1tb, 也就是給予3次方
|
|
638
661
|
maxParts: 1000 * 1000 * 1000, //預設為1000, 給予3次方
|
|
639
662
|
timeout: false, //避免請求未完成時中斷
|
|
640
|
-
|
|
663
|
+
// output: 'stream',
|
|
664
|
+
parse: true, //前端送obj過來
|
|
641
665
|
},
|
|
642
666
|
timeout: {
|
|
643
667
|
socket: false, //避免socket自動關閉
|
|
@@ -659,20 +683,31 @@ function WConverhpServer(opt = {}) {
|
|
|
659
683
|
// console.log('query', query)
|
|
660
684
|
|
|
661
685
|
//token
|
|
662
|
-
let token = get(query, 'token', '')
|
|
686
|
+
// let token = get(query, 'token', '')
|
|
687
|
+
let token = get(headers, 'authorization', '')
|
|
688
|
+
token = isestr(token) ? token : ''
|
|
663
689
|
|
|
664
690
|
//check
|
|
665
691
|
if (true) {
|
|
666
692
|
|
|
667
693
|
//funCheck
|
|
668
|
-
let m = funCheck({ token, headers, query })
|
|
694
|
+
let m = funCheck({ apiType: 'merge', token, headers, query })
|
|
669
695
|
if (ispm(m)) {
|
|
670
696
|
m = await m
|
|
671
697
|
}
|
|
672
698
|
|
|
673
699
|
//check
|
|
674
700
|
if (m !== true) {
|
|
675
|
-
|
|
701
|
+
|
|
702
|
+
//u8aOut
|
|
703
|
+
let out = {
|
|
704
|
+
error: 'permission denied',
|
|
705
|
+
}
|
|
706
|
+
let u8aOut = obj2u8arr(out)
|
|
707
|
+
// console.log('mergeu8aOut', u8aOut)
|
|
708
|
+
|
|
709
|
+
// console.log('merge return permission denied')
|
|
710
|
+
return responseU8aStream(res, u8aOut)
|
|
676
711
|
}
|
|
677
712
|
|
|
678
713
|
}
|
|
@@ -730,7 +765,6 @@ function WConverhpServer(opt = {}) {
|
|
|
730
765
|
//check
|
|
731
766
|
if (!fsIsFile(pathFileChunk)) {
|
|
732
767
|
// console.log(`pathFileChunk[${pathFileChunk}] is not a file`)
|
|
733
|
-
// return res.response({ error: `Missing chunk ${i} of filename[${filename}]` }).code(400)
|
|
734
768
|
throw new Error(`Missing chunk ${i} of filename[${filename}]`)
|
|
735
769
|
}
|
|
736
770
|
|
|
@@ -877,7 +911,7 @@ export default WConverhpServer
|
|
|
877
911
|
<br class="clear">
|
|
878
912
|
|
|
879
913
|
<footer>
|
|
880
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on
|
|
914
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 30 2025 19:42:30 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
881
915
|
</footer>
|
|
882
916
|
|
|
883
917
|
<script>prettyPrint();</script>
|
package/docs/index.html
CHANGED
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
<br class="clear">
|
|
72
72
|
|
|
73
73
|
<footer>
|
|
74
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on
|
|
74
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 30 2025 19:42:30 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
75
75
|
</footer>
|
|
76
76
|
|
|
77
77
|
<script>prettyPrint();</script>
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "w-converhp",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"main": "dist/w-converhp-server.umd.js",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@hapi/hapi": "^21.
|
|
6
|
+
"@hapi/hapi": "^21.4.0",
|
|
7
7
|
"@hapi/inert": "^7.1.0",
|
|
8
|
-
"axios": "^1.8.
|
|
8
|
+
"axios": "^1.8.4",
|
|
9
9
|
"lodash-es": "^4.17.21",
|
|
10
|
-
"wsemi": "^1.7.
|
|
10
|
+
"wsemi": "^1.7.74"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"form-data": "^4.0.2",
|
package/scla.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)
|