w-converhp 2.0.13 → 2.0.15
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 +68 -99
- 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 +3 -3
- package/docs/WConverhpClient.mjs.html +104 -120
- package/docs/WConverhpServer.html +43 -3
- package/docs/WConverhpServer.mjs.html +349 -32
- package/docs/index.html +1 -1
- package/package.json +1 -1
- package/scld.mjs +5 -5
- package/src/WConverhpClient.mjs +103 -119
- package/src/WConverhpServer.mjs +348 -31
- package/srv.mjs +25 -2
- package/test/downloadLargeFile.test.mjs +12 -13
- package/test/executeWithFile.test.mjs +10 -12
- package/test/executeWithU8a.test.mjs +11 -13
- package/test/uploadLargeFile.test.mjs +10 -14
package/src/WConverhpClient.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import axios from 'axios'
|
|
2
2
|
import get from 'lodash-es/get.js'
|
|
3
3
|
import isWindow from 'wsemi/src/isWindow.mjs'
|
|
4
|
+
import evem from 'wsemi/src/evem.mjs'
|
|
4
5
|
import genPm from 'wsemi/src/genPm.mjs'
|
|
5
6
|
import genID from 'wsemi/src/genID.mjs'
|
|
6
7
|
import haskey from 'wsemi/src/haskey.mjs'
|
|
@@ -13,7 +14,7 @@ import iseobj from 'wsemi/src/iseobj.mjs'
|
|
|
13
14
|
import ispm from 'wsemi/src/ispm.mjs'
|
|
14
15
|
import cint from 'wsemi/src/cint.mjs'
|
|
15
16
|
import strright from 'wsemi/src/strright.mjs'
|
|
16
|
-
import
|
|
17
|
+
import b642str from 'wsemi/src/b642str.mjs'
|
|
17
18
|
import blob2u8arr from 'wsemi/src/blob2u8arr.mjs'
|
|
18
19
|
import obj2u8arr from 'wsemi/src/obj2u8arr.mjs'
|
|
19
20
|
import u8arr2obj from 'wsemi/src/u8arr2obj.mjs'
|
|
@@ -35,100 +36,30 @@ import now2strp from 'wsemi/src/now2strp.mjs'
|
|
|
35
36
|
* @returns {Object} 回傳事件物件,可使用函數execute、upload
|
|
36
37
|
* @example
|
|
37
38
|
*
|
|
38
|
-
* import
|
|
39
|
+
* import path from 'path'
|
|
40
|
+
* import fs from 'fs'
|
|
41
|
+
* import _ from 'lodash-es'
|
|
42
|
+
* import w from 'wsemi'
|
|
43
|
+
* import FormData from 'form-data'
|
|
44
|
+
* import WConverhpClient from './src/WConverhpClient.mjs'
|
|
45
|
+
*
|
|
46
|
+
* let ms = []
|
|
39
47
|
*
|
|
40
48
|
* let opt = {
|
|
41
49
|
* FormData,
|
|
42
50
|
* url: 'http://localhost:8080',
|
|
43
51
|
* apiName: 'api',
|
|
52
|
+
* getToken: () => {
|
|
53
|
+
* return 'token-for-test'
|
|
54
|
+
* },
|
|
44
55
|
* }
|
|
45
56
|
*
|
|
46
57
|
* //new
|
|
47
58
|
* let wo = new WConverhpClient(opt)
|
|
48
59
|
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* let p = {
|
|
53
|
-
* a: 12,
|
|
54
|
-
* b: 34.56,
|
|
55
|
-
* c: 'test中文',
|
|
56
|
-
* d: {
|
|
57
|
-
* name,
|
|
58
|
-
* u8a,
|
|
59
|
-
* },
|
|
60
|
-
* }
|
|
61
|
-
* console.log('p', p)
|
|
62
|
-
*
|
|
63
|
-
* //execute
|
|
64
|
-
* await wo.execute('add', { p },
|
|
65
|
-
* function ({ prog, p, m }) {
|
|
66
|
-
* console.log('client web: execute: prog', prog, p, m)
|
|
67
|
-
* })
|
|
68
|
-
* .then(function(r) {
|
|
69
|
-
* console.log('client web: execute: add', r)
|
|
70
|
-
* console.log('r._bin.name', r._bin.name, 'r._bin.u8a', r._bin.u8a)
|
|
71
|
-
* // w.downloadFileFromU8Arr(r._bin.name, r._bin.u8a)
|
|
72
|
-
* })
|
|
73
|
-
* .catch(function (err) {
|
|
74
|
-
* console.log('client web: execute: catch', err)
|
|
75
|
-
* })
|
|
76
|
-
*
|
|
77
|
-
* }
|
|
78
|
-
*
|
|
79
|
-
* function executeWithU8a() {
|
|
80
|
-
* let core = async() => {
|
|
81
|
-
*
|
|
82
|
-
* //u8a
|
|
83
|
-
* let u8a = new Uint8Array([66, 97, 115])
|
|
84
|
-
* console.log('executeWithU8a u8a', u8a)
|
|
85
|
-
*
|
|
86
|
-
* //execute
|
|
87
|
-
* await execute('zdata.b1', u8a)
|
|
88
|
-
*
|
|
89
|
-
* }
|
|
90
|
-
* core()
|
|
91
|
-
* }
|
|
92
|
-
* executeWithU8a()
|
|
93
|
-
*
|
|
94
|
-
* function executeWithFile() {
|
|
95
|
-
* let core = async() => {
|
|
96
|
-
*
|
|
97
|
-
* //u8a
|
|
98
|
-
* let u8a = new Uint8Array(fs.readFileSync('../_data/10mb.7z'))
|
|
99
|
-
* console.log('executeWithFile u8a', u8a)
|
|
100
|
-
*
|
|
101
|
-
* //execute
|
|
102
|
-
* await execute('10mb.7z', u8a)
|
|
103
|
-
*
|
|
104
|
-
* }
|
|
105
|
-
* core()
|
|
106
|
-
* }
|
|
107
|
-
* executeWithFile()
|
|
108
|
-
*
|
|
109
|
-
* function uploadLargeFile() {
|
|
110
|
-
* let core = async() => {
|
|
111
|
-
*
|
|
112
|
-
* //u8a
|
|
113
|
-
* let u8a = new Uint8Array(fs.readFileSync('../_data/1000mb.7z'))
|
|
114
|
-
* console.log('u8a.length', u8a.length)
|
|
115
|
-
* console.log('uploadLargeFile u8a', u8a)
|
|
116
|
-
*
|
|
117
|
-
* await wo.upload('1000mb.7z', u8a,
|
|
118
|
-
* function ({ prog, p, m }) {
|
|
119
|
-
* console.log('client web: upload: prog', prog, p, m)
|
|
120
|
-
* })
|
|
121
|
-
* .then(function(res) {
|
|
122
|
-
* console.log('client web: upload: then', res)
|
|
123
|
-
* })
|
|
124
|
-
* .catch(function (err) {
|
|
125
|
-
* console.log('client web: upload: catch', err)
|
|
126
|
-
* })
|
|
127
|
-
*
|
|
128
|
-
* }
|
|
129
|
-
* core()
|
|
130
|
-
* }
|
|
131
|
-
* uploadLargeFile()
|
|
60
|
+
* wo.on('error', (err) => {
|
|
61
|
+
* console.log(`error`, err)
|
|
62
|
+
* })
|
|
132
63
|
*
|
|
133
64
|
* function downloadLargeFile() {
|
|
134
65
|
* let core = async() => {
|
|
@@ -141,7 +72,7 @@ import now2strp from 'wsemi/src/now2strp.mjs'
|
|
|
141
72
|
* }
|
|
142
73
|
* },
|
|
143
74
|
* {
|
|
144
|
-
* fdDownload: './',
|
|
75
|
+
* fdDownload: './', //於後端nodejs環境才能提供
|
|
145
76
|
* })
|
|
146
77
|
* .then(function(res) {
|
|
147
78
|
* console.log('client web: download: then', res)
|
|
@@ -223,6 +154,36 @@ function WConverhpClient(opt) {
|
|
|
223
154
|
}, 1)
|
|
224
155
|
}
|
|
225
156
|
|
|
157
|
+
//getUrlUse
|
|
158
|
+
let getUrlUse = (type) => {
|
|
159
|
+
|
|
160
|
+
//urlUse
|
|
161
|
+
let urlUse = ''
|
|
162
|
+
if (type === 'basic') {
|
|
163
|
+
urlUse = url
|
|
164
|
+
}
|
|
165
|
+
else if (type === 'slice') {
|
|
166
|
+
urlUse = `${url}slc`
|
|
167
|
+
}
|
|
168
|
+
else if (type === 'slice-merge') {
|
|
169
|
+
urlUse = `${url}slcm`
|
|
170
|
+
}
|
|
171
|
+
else if (type === 'download-get-filename') {
|
|
172
|
+
urlUse = `${url}dwgfn`
|
|
173
|
+
}
|
|
174
|
+
else if (type === 'download-get') {
|
|
175
|
+
urlUse = `${url}dwgf`
|
|
176
|
+
}
|
|
177
|
+
else if (type === 'download') {
|
|
178
|
+
urlUse = `${url}dw`
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
throw new Error(`invalid type[${type}]`)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return urlUse
|
|
185
|
+
}
|
|
186
|
+
|
|
226
187
|
//res2u8arr
|
|
227
188
|
async function res2u8arr(bb) {
|
|
228
189
|
//blob(in browser) or buffer(in nodejs) to u8a
|
|
@@ -273,22 +234,7 @@ function WConverhpClient(opt) {
|
|
|
273
234
|
}
|
|
274
235
|
|
|
275
236
|
//urlUse
|
|
276
|
-
let urlUse =
|
|
277
|
-
if (type === 'basic') {
|
|
278
|
-
urlUse = url
|
|
279
|
-
}
|
|
280
|
-
else if (type === 'slice') {
|
|
281
|
-
urlUse = `${url}slc`
|
|
282
|
-
}
|
|
283
|
-
else if (type === 'slicemerge') {
|
|
284
|
-
urlUse = `${url}slcm`
|
|
285
|
-
}
|
|
286
|
-
else if (type === 'download') {
|
|
287
|
-
urlUse = `${url}dw`
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
throw new Error(`invalid type[${type}]`)
|
|
291
|
-
}
|
|
237
|
+
let urlUse = getUrlUse(type)
|
|
292
238
|
|
|
293
239
|
//pm
|
|
294
240
|
let pm = genPm()
|
|
@@ -459,6 +405,7 @@ function WConverhpClient(opt) {
|
|
|
459
405
|
|
|
460
406
|
//filename
|
|
461
407
|
let filename = getFilenameByHeader(contentDisposition)
|
|
408
|
+
filename = b642str(filename) //headers內對中文支援度不佳須用base64傳, 此處解析提取後須反轉
|
|
462
409
|
// console.log('filename', filename)
|
|
463
410
|
|
|
464
411
|
//streamRecv
|
|
@@ -466,7 +413,7 @@ function WConverhpClient(opt) {
|
|
|
466
413
|
|
|
467
414
|
if (env === 'browser') {
|
|
468
415
|
|
|
469
|
-
|
|
416
|
+
//browser通過createObjectURL接收stream與a.href+a.click()接收檔案
|
|
470
417
|
try {
|
|
471
418
|
let url = URL.createObjectURL(streamRecv)
|
|
472
419
|
let a = document.createElement('a')
|
|
@@ -486,6 +433,7 @@ function WConverhpClient(opt) {
|
|
|
486
433
|
}
|
|
487
434
|
else {
|
|
488
435
|
|
|
436
|
+
//nodejs通過fs與stream接收檔案, stream出錯只會觸發error事件, 此處try catch為攔截其他非stream程式碼錯誤
|
|
489
437
|
try {
|
|
490
438
|
|
|
491
439
|
//path, fs
|
|
@@ -643,9 +591,7 @@ function WConverhpClient(opt) {
|
|
|
643
591
|
}
|
|
644
592
|
|
|
645
593
|
//send
|
|
646
|
-
// console.log('send bb...')
|
|
647
594
|
let res = await send(type, bb, { dataType: 'blob', cbProgress })
|
|
648
|
-
// console.log('send done', res)
|
|
649
595
|
|
|
650
596
|
return res
|
|
651
597
|
}
|
|
@@ -750,29 +696,23 @@ function WConverhpClient(opt) {
|
|
|
750
696
|
//chunk
|
|
751
697
|
let chunk = bb.slice(start, end)
|
|
752
698
|
|
|
753
|
-
//
|
|
699
|
+
//send slice
|
|
754
700
|
let hd = {
|
|
755
701
|
'chunk-index': i,
|
|
756
702
|
'chunk-total': chunkTotal,
|
|
757
703
|
'package-id': packageId,
|
|
758
704
|
}
|
|
759
|
-
|
|
760
|
-
//send slice
|
|
761
|
-
// console.log(`uploading chunk[${i + 1}/${chunkTotal}] of packageId[${packageId}]...`)
|
|
762
705
|
await send('slice', chunk, { headers: hd, dataType: 'blob', cbProgress: cbProgressSlice })
|
|
763
|
-
// console.log(`upload chunk[${i + 1}/${chunkTotal}] of packageId[${packageId}] done`, res)
|
|
764
706
|
|
|
765
707
|
}
|
|
766
708
|
|
|
767
|
-
//send merge
|
|
768
|
-
// console.log(`merging tempId[${tempId}]...`)
|
|
709
|
+
//send slice-merge
|
|
769
710
|
let msg = {
|
|
770
711
|
'filename': tempId,
|
|
771
712
|
'chunk-total': chunkTotal,
|
|
772
713
|
'package-id': packageId,
|
|
773
714
|
}
|
|
774
|
-
let resMg = await send('
|
|
775
|
-
// console.log(`merge tempId[${tempId}] done`, resMg)
|
|
715
|
+
let resMg = await send('slice-merge', msg, { dataType: 'json', cbProgress: cbProgressMerge })
|
|
776
716
|
|
|
777
717
|
return resMg
|
|
778
718
|
}
|
|
@@ -852,19 +792,63 @@ function WConverhpClient(opt) {
|
|
|
852
792
|
return sendDataSlice(tempId, bb, cbProgress)
|
|
853
793
|
}
|
|
854
794
|
|
|
855
|
-
//
|
|
856
|
-
async function
|
|
857
|
-
|
|
858
|
-
//msg
|
|
859
|
-
let msg = { fileId }
|
|
795
|
+
//downloadNodejs
|
|
796
|
+
async function downloadNodejs(fileId, cbProgress, opt = {}) {
|
|
860
797
|
|
|
861
798
|
//send download
|
|
799
|
+
let msg = { fileId }
|
|
862
800
|
let resMg = await send('download', msg, { ...opt, dataType: 'json', cbProgress })
|
|
863
801
|
// console.log('resMg', resMg)
|
|
864
802
|
|
|
865
803
|
return resMg
|
|
866
804
|
}
|
|
867
805
|
|
|
806
|
+
//downloadBrowser
|
|
807
|
+
async function downloadBrowser(fileId, cbProgress, opt = {}) {
|
|
808
|
+
//交由瀏覽器下載與管理故無法監聽進度, 不使用cbProgress
|
|
809
|
+
|
|
810
|
+
//token
|
|
811
|
+
let token = getToken()
|
|
812
|
+
if (ispm(token)) {
|
|
813
|
+
token = await token
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
//send download-get-filename
|
|
817
|
+
let msg = { fileId }
|
|
818
|
+
let resMg = await send('download-get-filename', msg, { dataType: 'json' })
|
|
819
|
+
// console.log('resMg', resMg)
|
|
820
|
+
|
|
821
|
+
//filename
|
|
822
|
+
let filename = get(resMg, 'filename', '')
|
|
823
|
+
// console.log('filename', filename)
|
|
824
|
+
|
|
825
|
+
//urlUse
|
|
826
|
+
let urlUse = getUrlUse('download-get')
|
|
827
|
+
// console.log('urlUse', urlUse)
|
|
828
|
+
|
|
829
|
+
//url
|
|
830
|
+
let url = `${urlUse}?fileId=${fileId}&token=${token}`
|
|
831
|
+
// console.log('url', url)
|
|
832
|
+
|
|
833
|
+
//透過a元素打url下載, 讓瀏覽器認定為直接下載模式, 由瀏覽器展示下載進度與排入正在下載清單
|
|
834
|
+
let a = document.createElement('a')
|
|
835
|
+
a.href = url
|
|
836
|
+
a.download = filename
|
|
837
|
+
a.click()
|
|
838
|
+
|
|
839
|
+
return filename
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
//download
|
|
843
|
+
async function download(fileId, cbProgress, opt = {}) {
|
|
844
|
+
if (env === 'browser') {
|
|
845
|
+
return downloadBrowser(fileId, cbProgress, opt)
|
|
846
|
+
}
|
|
847
|
+
else {
|
|
848
|
+
return downloadNodejs(fileId, cbProgress, opt)
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
|
|
868
852
|
//save
|
|
869
853
|
ee.execute = execute
|
|
870
854
|
ee.upload = upload
|