w-converhp 2.0.5 → 2.0.6
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 -1
- package/dist/w-converhp-client.umd.js +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 +1 -1
- package/docs/WConverhpClient.mjs.html +1 -1
- package/docs/WConverhpServer.html +2 -2
- package/docs/WConverhpServer.mjs.html +11 -1
- package/docs/index.html +1 -1
- package/package.json +1 -1
- package/scla.mjs +7 -2
- package/sclb.mjs +8 -2
- package/sclc.mjs +9 -3
- package/src/WConverhpServer.mjs +10 -0
- package/src/mergeFiles.mjs +37 -14
- package/src/mergeFiles.wk.umd.js +1 -1
- package/srv.mjs +12 -7
- package/test/1mb.7z +0 -0
- package/test/executeWithFile.test.mjs +191 -0
- package/test/executeWithU8a.test.mjs +185 -0
- package/test/uploadLargeFile.test.mjs +149 -0
- package/test/all.test.mjs +0 -10
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import assert from 'assert'
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
// import _ from 'lodash-es'
|
|
4
|
+
import w from 'wsemi'
|
|
5
|
+
import WConverhpServer from '../src/WConverhpServer.mjs'
|
|
6
|
+
import WConverhpClient from '../src/WConverhpClient.mjs'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
describe('uploadLargeFile', function() {
|
|
10
|
+
|
|
11
|
+
let msAll = []
|
|
12
|
+
|
|
13
|
+
let runServer = () => {
|
|
14
|
+
|
|
15
|
+
let ms = []
|
|
16
|
+
|
|
17
|
+
let opt = {
|
|
18
|
+
port: 8082, //8080, //同時test故得要不同port
|
|
19
|
+
apiName: 'api',
|
|
20
|
+
pathStaticFiles: '.', //要存取專案資料夾下web.html, 故不能給dist
|
|
21
|
+
funCheck: async ({ apiType, authorization, headers, query }) => {
|
|
22
|
+
// console.log('funCheck', `apiType[${apiType}]`, `authorization[${authorization}]`)
|
|
23
|
+
let token = w.strdelleft(authorization, 7) //刪除Bearer
|
|
24
|
+
if (!w.isestr(token)) {
|
|
25
|
+
return false
|
|
26
|
+
}
|
|
27
|
+
// await w.delay(3000)
|
|
28
|
+
return true
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//new
|
|
33
|
+
let wo = new WConverhpServer(opt)
|
|
34
|
+
|
|
35
|
+
wo.on('upload', (input, pm) => {
|
|
36
|
+
// console.log(`Server[port:${opt.port}]: upload`, input)
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
|
|
40
|
+
let b = fs.readFileSync(`./${input.path}`)
|
|
41
|
+
let u8a = new Uint8Array(b)
|
|
42
|
+
let t = u8a
|
|
43
|
+
let ts = [t[0], t[1], t[2]]
|
|
44
|
+
ms.push({ 'receive and return': ts })
|
|
45
|
+
|
|
46
|
+
fs.unlinkSync(`./${input.path}`) //測試完刪除暫存檔
|
|
47
|
+
|
|
48
|
+
let output = input
|
|
49
|
+
pm.resolve(output)
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
// console.log('upload error', err)
|
|
53
|
+
pm.reject('upload error')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
})
|
|
57
|
+
wo.on('error', () => {
|
|
58
|
+
// console.log(`Server[port:${opt.port}]: error`, err)
|
|
59
|
+
})
|
|
60
|
+
wo.on('handler', (data) => {
|
|
61
|
+
// console.log(`Server[port:${opt.port}]: handler`, data)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
setTimeout(() => {
|
|
65
|
+
// console.log('ms', ms)
|
|
66
|
+
msAll.push({ server: ms })
|
|
67
|
+
wo.stop()
|
|
68
|
+
}, 2000)
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let runClient = () => {
|
|
73
|
+
|
|
74
|
+
let ms = []
|
|
75
|
+
|
|
76
|
+
let opt = {
|
|
77
|
+
FormData,
|
|
78
|
+
url: 'http://localhost:8082', //同時test故得要不同port
|
|
79
|
+
apiName: 'api',
|
|
80
|
+
getToken: () => {
|
|
81
|
+
return 'token-for-test'
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//new
|
|
86
|
+
let wo = new WConverhpClient(opt)
|
|
87
|
+
|
|
88
|
+
wo.on('error', () => {
|
|
89
|
+
// console.log(`error`, err)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
function uploadLargeFile() {
|
|
93
|
+
let core = async() => {
|
|
94
|
+
|
|
95
|
+
//u8a
|
|
96
|
+
let u8a = new Uint8Array(fs.readFileSync('./test/1mb.7z')) //使用test內檔案
|
|
97
|
+
// console.log('u8a.length', u8a.length)
|
|
98
|
+
// console.log('uploadLargeFile u8a', u8a)
|
|
99
|
+
let t = u8a
|
|
100
|
+
let ts = [t[0], t[1], t[2]]
|
|
101
|
+
ms.push({ 'upload u8a.length': ts })
|
|
102
|
+
|
|
103
|
+
await wo.upload('1mb.7z', u8a,
|
|
104
|
+
function ({ prog, p, m }) {
|
|
105
|
+
// console.log('client web: upload: prog', prog, p, m)
|
|
106
|
+
if (m === 'upload') {
|
|
107
|
+
// console.log('client web: upload: prog', prog)
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
.then(function(res) {
|
|
111
|
+
// console.log('client web: upload: then', res)
|
|
112
|
+
ms.push({ 'upload output': { filename: res.filename } })
|
|
113
|
+
})
|
|
114
|
+
.catch(function () {
|
|
115
|
+
// console.log('client web: upload: catch', err)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
// console.log('ms', ms)
|
|
119
|
+
msAll.push({ client: ms })
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
core()
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
uploadLargeFile()
|
|
126
|
+
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let run = () => {
|
|
130
|
+
let pm = w.genPm()
|
|
131
|
+
runServer()
|
|
132
|
+
runClient()
|
|
133
|
+
setTimeout(() => {
|
|
134
|
+
// console.log('msAll', msAll)
|
|
135
|
+
// fs.writeFileSync('./test_uploadLargeFile.json', JSON.stringify(msAll), 'utf8')
|
|
136
|
+
pm.resolve(msAll)
|
|
137
|
+
}, 4000)
|
|
138
|
+
return pm
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let res = `[{"client":[{"upload u8a.length":[55,122,188]},{"upload output":{"filename":"1mb.7z"}}]},{"server":[{"receive and return":[55,122,188]}]}]`
|
|
142
|
+
it(`should return ${res} when test`, async function() {
|
|
143
|
+
let r = await run()
|
|
144
|
+
r = JSON.stringify(r)
|
|
145
|
+
let rr = res
|
|
146
|
+
assert.strict.deepEqual(r, rr)
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
})
|