w-converhp 2.0.14 → 2.0.16
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 +58 -104
- 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 +102 -119
- package/docs/WConverhpServer.html +43 -3
- package/docs/WConverhpServer.mjs.html +330 -35
- package/docs/index.html +1 -1
- package/package.json +2 -2
- package/scld.mjs +4 -4
- package/src/WConverhpClient.mjs +101 -118
- package/src/WConverhpServer.mjs +329 -34
- package/srv.mjs +15 -7
- package/test/downloadLargeFile.test.mjs +24 -19
- 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
|
@@ -36,100 +36,30 @@ import now2strp from 'wsemi/src/now2strp.mjs'
|
|
|
36
36
|
* @returns {Object} 回傳事件物件,可使用函數execute、upload
|
|
37
37
|
* @example
|
|
38
38
|
*
|
|
39
|
-
* 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 = []
|
|
40
47
|
*
|
|
41
48
|
* let opt = {
|
|
42
49
|
* FormData,
|
|
43
50
|
* url: 'http://localhost:8080',
|
|
44
51
|
* apiName: 'api',
|
|
52
|
+
* getToken: () => {
|
|
53
|
+
* return 'token-for-test'
|
|
54
|
+
* },
|
|
45
55
|
* }
|
|
46
56
|
*
|
|
47
57
|
* //new
|
|
48
58
|
* let wo = new WConverhpClient(opt)
|
|
49
59
|
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* let p = {
|
|
54
|
-
* a: 12,
|
|
55
|
-
* b: 34.56,
|
|
56
|
-
* c: 'test中文',
|
|
57
|
-
* d: {
|
|
58
|
-
* name,
|
|
59
|
-
* u8a,
|
|
60
|
-
* },
|
|
61
|
-
* }
|
|
62
|
-
* console.log('p', p)
|
|
63
|
-
*
|
|
64
|
-
* //execute
|
|
65
|
-
* await wo.execute('add', { p },
|
|
66
|
-
* function ({ prog, p, m }) {
|
|
67
|
-
* console.log('client web: execute: prog', prog, p, m)
|
|
68
|
-
* })
|
|
69
|
-
* .then(function(r) {
|
|
70
|
-
* console.log('client web: execute: add', r)
|
|
71
|
-
* console.log('r._bin.name', r._bin.name, 'r._bin.u8a', r._bin.u8a)
|
|
72
|
-
* // w.downloadFileFromU8Arr(r._bin.name, r._bin.u8a)
|
|
73
|
-
* })
|
|
74
|
-
* .catch(function (err) {
|
|
75
|
-
* console.log('client web: execute: catch', err)
|
|
76
|
-
* })
|
|
77
|
-
*
|
|
78
|
-
* }
|
|
79
|
-
*
|
|
80
|
-
* function executeWithU8a() {
|
|
81
|
-
* let core = async() => {
|
|
82
|
-
*
|
|
83
|
-
* //u8a
|
|
84
|
-
* let u8a = new Uint8Array([66, 97, 115])
|
|
85
|
-
* console.log('executeWithU8a u8a', u8a)
|
|
86
|
-
*
|
|
87
|
-
* //execute
|
|
88
|
-
* await execute('zdata.b1', u8a)
|
|
89
|
-
*
|
|
90
|
-
* }
|
|
91
|
-
* core()
|
|
92
|
-
* }
|
|
93
|
-
* executeWithU8a()
|
|
94
|
-
*
|
|
95
|
-
* function executeWithFile() {
|
|
96
|
-
* let core = async() => {
|
|
97
|
-
*
|
|
98
|
-
* //u8a
|
|
99
|
-
* let u8a = new Uint8Array(fs.readFileSync('../_data/10mb.7z'))
|
|
100
|
-
* console.log('executeWithFile u8a', u8a)
|
|
101
|
-
*
|
|
102
|
-
* //execute
|
|
103
|
-
* await execute('10mb.7z', u8a)
|
|
104
|
-
*
|
|
105
|
-
* }
|
|
106
|
-
* core()
|
|
107
|
-
* }
|
|
108
|
-
* executeWithFile()
|
|
109
|
-
*
|
|
110
|
-
* function uploadLargeFile() {
|
|
111
|
-
* let core = async() => {
|
|
112
|
-
*
|
|
113
|
-
* //u8a
|
|
114
|
-
* let u8a = new Uint8Array(fs.readFileSync('../_data/1000mb.7z'))
|
|
115
|
-
* console.log('u8a.length', u8a.length)
|
|
116
|
-
* console.log('uploadLargeFile u8a', u8a)
|
|
117
|
-
*
|
|
118
|
-
* await wo.upload('1000mb.7z', u8a,
|
|
119
|
-
* function ({ prog, p, m }) {
|
|
120
|
-
* console.log('client web: upload: prog', prog, p, m)
|
|
121
|
-
* })
|
|
122
|
-
* .then(function(res) {
|
|
123
|
-
* console.log('client web: upload: then', res)
|
|
124
|
-
* })
|
|
125
|
-
* .catch(function (err) {
|
|
126
|
-
* console.log('client web: upload: catch', err)
|
|
127
|
-
* })
|
|
128
|
-
*
|
|
129
|
-
* }
|
|
130
|
-
* core()
|
|
131
|
-
* }
|
|
132
|
-
* uploadLargeFile()
|
|
60
|
+
* wo.on('error', (err) => {
|
|
61
|
+
* console.log(`error`, err)
|
|
62
|
+
* })
|
|
133
63
|
*
|
|
134
64
|
* function downloadLargeFile() {
|
|
135
65
|
* let core = async() => {
|
|
@@ -142,7 +72,7 @@ import now2strp from 'wsemi/src/now2strp.mjs'
|
|
|
142
72
|
* }
|
|
143
73
|
* },
|
|
144
74
|
* {
|
|
145
|
-
* fdDownload: './',
|
|
75
|
+
* fdDownload: './', //於後端nodejs環境才能提供
|
|
146
76
|
* })
|
|
147
77
|
* .then(function(res) {
|
|
148
78
|
* console.log('client web: download: then', res)
|
|
@@ -224,6 +154,36 @@ function WConverhpClient(opt) {
|
|
|
224
154
|
}, 1)
|
|
225
155
|
}
|
|
226
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
|
+
|
|
227
187
|
//res2u8arr
|
|
228
188
|
async function res2u8arr(bb) {
|
|
229
189
|
//blob(in browser) or buffer(in nodejs) to u8a
|
|
@@ -274,22 +234,7 @@ function WConverhpClient(opt) {
|
|
|
274
234
|
}
|
|
275
235
|
|
|
276
236
|
//urlUse
|
|
277
|
-
let urlUse =
|
|
278
|
-
if (type === 'basic') {
|
|
279
|
-
urlUse = url
|
|
280
|
-
}
|
|
281
|
-
else if (type === 'slice') {
|
|
282
|
-
urlUse = `${url}slc`
|
|
283
|
-
}
|
|
284
|
-
else if (type === 'slicemerge') {
|
|
285
|
-
urlUse = `${url}slcm`
|
|
286
|
-
}
|
|
287
|
-
else if (type === 'download') {
|
|
288
|
-
urlUse = `${url}dw`
|
|
289
|
-
}
|
|
290
|
-
else {
|
|
291
|
-
throw new Error(`invalid type[${type}]`)
|
|
292
|
-
}
|
|
237
|
+
let urlUse = getUrlUse(type)
|
|
293
238
|
|
|
294
239
|
//pm
|
|
295
240
|
let pm = genPm()
|
|
@@ -468,7 +413,7 @@ function WConverhpClient(opt) {
|
|
|
468
413
|
|
|
469
414
|
if (env === 'browser') {
|
|
470
415
|
|
|
471
|
-
|
|
416
|
+
//browser通過createObjectURL接收stream與a.href+a.click()接收檔案
|
|
472
417
|
try {
|
|
473
418
|
let url = URL.createObjectURL(streamRecv)
|
|
474
419
|
let a = document.createElement('a')
|
|
@@ -488,6 +433,7 @@ function WConverhpClient(opt) {
|
|
|
488
433
|
}
|
|
489
434
|
else {
|
|
490
435
|
|
|
436
|
+
//nodejs通過fs與stream接收檔案, stream出錯只會觸發error事件, 此處try catch為攔截其他非stream程式碼錯誤
|
|
491
437
|
try {
|
|
492
438
|
|
|
493
439
|
//path, fs
|
|
@@ -645,9 +591,7 @@ function WConverhpClient(opt) {
|
|
|
645
591
|
}
|
|
646
592
|
|
|
647
593
|
//send
|
|
648
|
-
// console.log('send bb...')
|
|
649
594
|
let res = await send(type, bb, { dataType: 'blob', cbProgress })
|
|
650
|
-
// console.log('send done', res)
|
|
651
595
|
|
|
652
596
|
return res
|
|
653
597
|
}
|
|
@@ -752,29 +696,23 @@ function WConverhpClient(opt) {
|
|
|
752
696
|
//chunk
|
|
753
697
|
let chunk = bb.slice(start, end)
|
|
754
698
|
|
|
755
|
-
//
|
|
699
|
+
//send slice
|
|
756
700
|
let hd = {
|
|
757
701
|
'chunk-index': i,
|
|
758
702
|
'chunk-total': chunkTotal,
|
|
759
703
|
'package-id': packageId,
|
|
760
704
|
}
|
|
761
|
-
|
|
762
|
-
//send slice
|
|
763
|
-
// console.log(`uploading chunk[${i + 1}/${chunkTotal}] of packageId[${packageId}]...`)
|
|
764
705
|
await send('slice', chunk, { headers: hd, dataType: 'blob', cbProgress: cbProgressSlice })
|
|
765
|
-
// console.log(`upload chunk[${i + 1}/${chunkTotal}] of packageId[${packageId}] done`, res)
|
|
766
706
|
|
|
767
707
|
}
|
|
768
708
|
|
|
769
|
-
//send merge
|
|
770
|
-
// console.log(`merging tempId[${tempId}]...`)
|
|
709
|
+
//send slice-merge
|
|
771
710
|
let msg = {
|
|
772
711
|
'filename': tempId,
|
|
773
712
|
'chunk-total': chunkTotal,
|
|
774
713
|
'package-id': packageId,
|
|
775
714
|
}
|
|
776
|
-
let resMg = await send('
|
|
777
|
-
// console.log(`merge tempId[${tempId}] done`, resMg)
|
|
715
|
+
let resMg = await send('slice-merge', msg, { dataType: 'json', cbProgress: cbProgressMerge })
|
|
778
716
|
|
|
779
717
|
return resMg
|
|
780
718
|
}
|
|
@@ -854,19 +792,64 @@ function WConverhpClient(opt) {
|
|
|
854
792
|
return sendDataSlice(tempId, bb, cbProgress)
|
|
855
793
|
}
|
|
856
794
|
|
|
857
|
-
//
|
|
858
|
-
async function
|
|
859
|
-
|
|
860
|
-
//msg
|
|
861
|
-
let msg = { fileId }
|
|
795
|
+
//downloadNodejs
|
|
796
|
+
async function downloadNodejs(fileId, cbProgress, opt = {}) {
|
|
862
797
|
|
|
863
798
|
//send download
|
|
799
|
+
let msg = { fileId }
|
|
864
800
|
let resMg = await send('download', msg, { ...opt, dataType: 'json', cbProgress })
|
|
865
801
|
// console.log('resMg', resMg)
|
|
866
802
|
|
|
867
803
|
return resMg
|
|
868
804
|
}
|
|
869
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
|
+
console.log('token', token)
|
|
816
|
+
|
|
817
|
+
//send download-get-filename
|
|
818
|
+
let msg = { fileId }
|
|
819
|
+
let resMg = await send('download-get-filename', msg, { dataType: 'json' })
|
|
820
|
+
console.log('resMg', resMg)
|
|
821
|
+
|
|
822
|
+
//filename
|
|
823
|
+
let filename = get(resMg, 'filename', '')
|
|
824
|
+
console.log('filename', filename)
|
|
825
|
+
|
|
826
|
+
//urlUse
|
|
827
|
+
let urlUse = getUrlUse('download-get')
|
|
828
|
+
console.log('urlUse', urlUse)
|
|
829
|
+
|
|
830
|
+
//url
|
|
831
|
+
let url = `${urlUse}?fileId=${fileId}&token=${token}`
|
|
832
|
+
console.log('url', url)
|
|
833
|
+
|
|
834
|
+
//透過a元素打url下載, 讓瀏覽器認定為直接下載模式, 由瀏覽器展示下載進度與排入正在下載清單
|
|
835
|
+
let a = document.createElement('a')
|
|
836
|
+
a.href = url
|
|
837
|
+
a.download = filename
|
|
838
|
+
a.click()
|
|
839
|
+
|
|
840
|
+
return filename
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
//download
|
|
844
|
+
async function download(fileId, cbProgress, opt = {}) {
|
|
845
|
+
if (env === 'browser') {
|
|
846
|
+
return downloadBrowser(fileId, cbProgress, opt)
|
|
847
|
+
}
|
|
848
|
+
else {
|
|
849
|
+
return downloadNodejs(fileId, cbProgress, opt)
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
870
853
|
//save
|
|
871
854
|
ee.execute = execute
|
|
872
855
|
ee.upload = upload
|