w-converhp 2.0.4 → 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 +6 -6
- 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 +1 -1
- package/docs/WConverhpClient.mjs.html +4 -4
- package/docs/WConverhpServer.html +3 -3
- package/docs/WConverhpServer.mjs.html +33 -85
- package/docs/index.html +1 -1
- package/package.json +2 -2
- package/scla.mjs +8 -3
- package/sclb.mjs +9 -3
- package/sclc.mjs +10 -4
- package/src/WConverhpClient.mjs +3 -3
- package/src/WConverhpServer.mjs +31 -83
- package/src/mergeFiles.mjs +96 -0
- package/src/mergeFiles.wk.umd.js +1 -0
- package/srv.mjs +18 -13
- 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/toolg/gDistRollup.mjs +56 -47
- package/test/all.test.mjs +0 -10
|
@@ -0,0 +1,185 @@
|
|
|
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('executeWithU8a', function() {
|
|
10
|
+
|
|
11
|
+
let msAll = []
|
|
12
|
+
|
|
13
|
+
let runServer = () => {
|
|
14
|
+
|
|
15
|
+
let ms = []
|
|
16
|
+
|
|
17
|
+
let opt = {
|
|
18
|
+
port: 8080,
|
|
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('execute', (func, input, pm) => {
|
|
36
|
+
// console.log(`Server[port:${opt.port}]: execute`, func, input)
|
|
37
|
+
// console.log(`Server[port:${opt.port}]: execute`, func)
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
|
|
41
|
+
if (func === 'add') {
|
|
42
|
+
|
|
43
|
+
if (_.get(input, 'p.d.u8a', null)) {
|
|
44
|
+
// console.log('input.p.d.u8a', input.p.d.u8a)
|
|
45
|
+
ms.push({ 'input.p.d.u8a': input.p.d.u8a })
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let r = {
|
|
49
|
+
_add: input.p.a + input.p.b,
|
|
50
|
+
_data: [11, 22.22, 'abc', { x: '21', y: 65.43, z: 'test中文' }],
|
|
51
|
+
_bin: {
|
|
52
|
+
name: 'zdata.b2',
|
|
53
|
+
u8a: new Uint8Array([52, 66, 97, 115]),
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
pm.resolve(r)
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// console.log('invalid func')
|
|
62
|
+
pm.reject('invalid func')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
// console.log('execute error', err)
|
|
68
|
+
pm.reject('execute error')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
})
|
|
72
|
+
wo.on('error', () => {
|
|
73
|
+
// console.log(`Server[port:${opt.port}]: error`, err)
|
|
74
|
+
})
|
|
75
|
+
wo.on('handler', (data) => {
|
|
76
|
+
// console.log(`Server[port:${opt.port}]: handler`, data)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
setTimeout(() => {
|
|
80
|
+
// console.log('ms', JSON.stringify(ms))
|
|
81
|
+
msAll.push({ server: ms })
|
|
82
|
+
wo.stop()
|
|
83
|
+
}, 2000)
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let runClient = () => {
|
|
88
|
+
|
|
89
|
+
let ms = []
|
|
90
|
+
|
|
91
|
+
let opt = {
|
|
92
|
+
FormData,
|
|
93
|
+
url: 'http://localhost:8080',
|
|
94
|
+
apiName: 'api',
|
|
95
|
+
getToken: () => {
|
|
96
|
+
return 'token-for-test'
|
|
97
|
+
},
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//new
|
|
101
|
+
let wo = new WConverhpClient(opt)
|
|
102
|
+
|
|
103
|
+
wo.on('error', () => {
|
|
104
|
+
// console.log(`error`, err)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
async function execute(name, u8a) {
|
|
108
|
+
|
|
109
|
+
//p
|
|
110
|
+
let p = {
|
|
111
|
+
a: 12,
|
|
112
|
+
b: 34.56,
|
|
113
|
+
c: 'test中文',
|
|
114
|
+
d: {
|
|
115
|
+
name,
|
|
116
|
+
u8a,
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
// console.log('p', p)
|
|
120
|
+
ms.push({ 'execute input': p })
|
|
121
|
+
|
|
122
|
+
//execute
|
|
123
|
+
await wo.execute('add', { p },
|
|
124
|
+
function ({ prog, p, m }) {
|
|
125
|
+
if (m === 'upload') {
|
|
126
|
+
// console.log('client web: execute: prog', prog * 0.5, p, m)
|
|
127
|
+
}
|
|
128
|
+
if (m === 'download') {
|
|
129
|
+
// console.log('client web: execute: prog', 50 + prog * 0.5, p, m)
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
.then(function(r) {
|
|
133
|
+
// console.log('client web: execute: add', r)
|
|
134
|
+
// console.log('r._bin.name', r._bin.name, 'r._bin.u8a', r._bin.u8a)
|
|
135
|
+
// w.downloadFileFromU8Arr(r._bin.name, r._bin.u8a)
|
|
136
|
+
ms.push({ 'execute output': r })
|
|
137
|
+
})
|
|
138
|
+
.catch(function () {
|
|
139
|
+
// console.log('client web: execute: catch', err)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function executeWithU8a() {
|
|
145
|
+
let core = async() => {
|
|
146
|
+
|
|
147
|
+
//u8a
|
|
148
|
+
let u8a = new Uint8Array([66, 97, 115])
|
|
149
|
+
// console.log('executeWithU8a u8a', u8a)
|
|
150
|
+
|
|
151
|
+
//execute
|
|
152
|
+
await execute('zdata.b1', u8a)
|
|
153
|
+
|
|
154
|
+
// console.log('ms', JSON.stringify(ms))
|
|
155
|
+
msAll.push({ client: ms })
|
|
156
|
+
|
|
157
|
+
}
|
|
158
|
+
core()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
executeWithU8a()
|
|
162
|
+
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let run = () => {
|
|
166
|
+
let pm = w.genPm()
|
|
167
|
+
runServer()
|
|
168
|
+
runClient()
|
|
169
|
+
setTimeout(() => {
|
|
170
|
+
// console.log('msAll', JSON.stringify(msAll))
|
|
171
|
+
// fs.writeFileSync('./test_executeWithU8a.json', JSON.stringify(msAll), 'utf8')
|
|
172
|
+
pm.resolve(msAll)
|
|
173
|
+
}, 4000)
|
|
174
|
+
return pm
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let res = `[{"client":[{"execute input":{"a":12,"b":34.56,"c":"test中文","d":{"name":"zdata.b1","u8a":{"0":66,"1":97,"2":115}}}},{"execute output":{"_add":46.56,"_data":[11,22.22,"abc",{"x":"21","y":65.43,"z":"test中文"}],"_bin":{"name":"zdata.b2","u8a":{"0":52,"1":66,"2":97,"3":115}}}}]},{"server":[{"input.p.d.u8a":{"0":66,"1":97,"2":115}}]}]`
|
|
178
|
+
it(`should return ${res} when test`, async function() {
|
|
179
|
+
let r = await run()
|
|
180
|
+
r = JSON.stringify(r)
|
|
181
|
+
let rr = res
|
|
182
|
+
assert.strict.deepEqual(r, rr)
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
})
|
|
@@ -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
|
+
})
|
package/toolg/gDistRollup.mjs
CHANGED
|
@@ -1,54 +1,63 @@
|
|
|
1
|
+
import path from 'path'
|
|
1
2
|
import rollupFiles from 'w-package-tools/src/rollupFiles.mjs'
|
|
2
|
-
import
|
|
3
|
-
import w from 'wsemi'
|
|
3
|
+
import rollupWorker from 'w-package-tools/src/rollupWorker.mjs'
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
let fdSrc = './src'
|
|
7
7
|
let fdTar = './dist'
|
|
8
|
-
let fns = getFiles(fdSrc)
|
|
9
|
-
fns = fns.filter((v) => {
|
|
10
|
-
return w.strleft(v, 1) === 'W'
|
|
11
|
-
})
|
|
12
8
|
|
|
13
|
-
rollupFiles({
|
|
14
|
-
fns,
|
|
15
|
-
fdSrc,
|
|
16
|
-
fdTar,
|
|
17
|
-
nameDistType: 'kebabCase',
|
|
18
|
-
globals: {
|
|
19
|
-
'@hapi/hapi': '@hapi/hapi',
|
|
20
|
-
'@hapi/inert': '@hapi/inert',
|
|
21
|
-
'events': 'events',
|
|
22
|
-
'stream': 'stream',
|
|
23
|
-
// 'form-data': 'FormData',
|
|
24
|
-
'crypto': 'crypto', //因crypto-js修改使用內建crypto方式, 會偵測nodejs並使用require內建的crypto, 故需剔除
|
|
25
|
-
// 'combined-stream': 'combined-stream',
|
|
26
|
-
// 'util': 'util',
|
|
27
|
-
// 'fs': 'fs',
|
|
28
|
-
// 'path': 'path',
|
|
29
|
-
// 'http': 'http',
|
|
30
|
-
// 'https': 'https',
|
|
31
|
-
// 'url': 'url',
|
|
32
|
-
// 'mime-types': 'mime-types',
|
|
33
|
-
// 'asynckit': 'asynckit',
|
|
34
|
-
},
|
|
35
|
-
external: [
|
|
36
|
-
'@hapi/hapi',
|
|
37
|
-
'@hapi/inert',
|
|
38
|
-
'events',
|
|
39
|
-
'stream',
|
|
40
|
-
// 'form-data',
|
|
41
|
-
'crypto',
|
|
42
|
-
// 'combined-stream',
|
|
43
|
-
// 'util',
|
|
44
|
-
// 'path',
|
|
45
|
-
// 'http',
|
|
46
|
-
// 'https',
|
|
47
|
-
// 'url',
|
|
48
|
-
// 'fs',
|
|
49
|
-
// 'stream',
|
|
50
|
-
// 'mime-types',
|
|
51
|
-
// 'asynckit',
|
|
52
|
-
],
|
|
53
|
-
})
|
|
54
9
|
|
|
10
|
+
async function core() {
|
|
11
|
+
|
|
12
|
+
//因為WConverhpServer會import mergeFiles.wk.umd.js故得先編譯
|
|
13
|
+
await rollupWorker({
|
|
14
|
+
name: 'mergeFiles', //原模組名稱, 將來會掛於winodw下或於node引入使用
|
|
15
|
+
type: 'function', //原模組輸出為函數, 可傳入參數初始化
|
|
16
|
+
// execFunctionByInstance: true, //default, 原模組為計算函數回傳結果
|
|
17
|
+
fpSrc: path.resolve(fdSrc, 'mergeFiles.mjs'), //原始檔案路徑
|
|
18
|
+
fpTar: path.resolve(fdSrc, 'mergeFiles.wk.umd.js'), //檔案輸出路徑
|
|
19
|
+
formatOut: 'umd',
|
|
20
|
+
// bMinify: false,
|
|
21
|
+
globals: {
|
|
22
|
+
'path': 'path',
|
|
23
|
+
'fs': 'fs',
|
|
24
|
+
},
|
|
25
|
+
external: [
|
|
26
|
+
'path',
|
|
27
|
+
'fs',
|
|
28
|
+
],
|
|
29
|
+
})
|
|
30
|
+
.catch((err) => {
|
|
31
|
+
console.log(err)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
await rollupFiles({
|
|
35
|
+
fns: ['WConverhpServer.mjs', 'WConverhpClient.mjs'],
|
|
36
|
+
fdSrc,
|
|
37
|
+
fdTar,
|
|
38
|
+
nameDistType: 'kebabCase',
|
|
39
|
+
globals: {
|
|
40
|
+
'path': 'path',
|
|
41
|
+
'fs': 'fs',
|
|
42
|
+
'stream': 'stream',
|
|
43
|
+
'crypto': 'crypto', //因crypto-js修改使用內建crypto方式, 會偵測nodejs並使用require內建的crypto, 故需剔除
|
|
44
|
+
'@hapi/hapi': '@hapi/hapi',
|
|
45
|
+
'@hapi/inert': '@hapi/inert',
|
|
46
|
+
// 'form-data': 'FormData',
|
|
47
|
+
},
|
|
48
|
+
external: [
|
|
49
|
+
'path',
|
|
50
|
+
'fs',
|
|
51
|
+
'stream',
|
|
52
|
+
'crypto',
|
|
53
|
+
'@hapi/hapi',
|
|
54
|
+
'@hapi/inert',
|
|
55
|
+
// 'form-data',
|
|
56
|
+
],
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
core()
|
|
61
|
+
.catch((err) => {
|
|
62
|
+
console.log(err)
|
|
63
|
+
})
|