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 CHANGED
@@ -21,16 +21,28 @@ To view documentation or get support, visit [docs](https://yuda-lyu.github.io/w-
21
21
  ```alias
22
22
  npm i w-converhp
23
23
  ```
24
- #### Example for w-converhp-server:
24
+
25
+ #### Example for w-converhp-server in nodejs:
25
26
  > **Link:** [[dev source code](https://github.com/yuda-lyu/w-converhp/blob/master/srv.mjs)]
26
27
  ```alias
27
- import WConverhpServer from 'w-converhp/dist/w-converhp-server.umd.js'
28
+ import fs from 'fs'
29
+ import _ from 'lodash-es'
30
+ import w from 'wsemi'
31
+ import WConverhpServer from './src/WConverhpServer.mjs'
32
+
33
+ let ms = []
28
34
 
29
35
  let opt = {
30
36
  port: 8080,
31
37
  apiName: 'api',
32
38
  pathStaticFiles: '.', //要存取專案資料夾下web.html, 故不能給dist
33
- verifyConn: () => {
39
+ verifyConn: async ({ apiType, authorization, headers, query }) => {
40
+ console.log('verifyConn', `apiType[${apiType}]`, `authorization[${authorization}]`)
41
+ let token = w.strdelleft(authorization, 7) //刪除Bearer
42
+ if (!w.isestr(token)) {
43
+ return false
44
+ }
45
+ // await w.delay(3000)
34
46
  return true
35
47
  },
36
48
  }
@@ -48,6 +60,7 @@ wo.on('execute', (func, input, pm) => {
48
60
 
49
61
  if (_.get(input, 'p.d.u8a', null)) {
50
62
  console.log('input.p.d.u8a', input.p.d.u8a)
63
+ ms.push({ 'input.p.d.u8a': input.p.d.u8a })
51
64
  }
52
65
 
53
66
  let r = {
@@ -55,13 +68,7 @@ wo.on('execute', (func, input, pm) => {
55
68
  _data: [11, 22.22, 'abc', { x: '21', y: 65.43, z: 'test中文' }],
56
69
  _bin: {
57
70
  name: 'zdata.b2',
58
- u8a: new Uint8Array([66, 97, 115]),
59
- // name: '100mb.7z',
60
- // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\100mb.7z')),
61
- // name: '500mb.7z',
62
- // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\500mb.7z')),
63
- // name: '1000mb.7z',
64
- // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\1000mb.7z')),
71
+ u8a: new Uint8Array([52, 66, 97, 115]),
65
72
  },
66
73
  }
67
74
 
@@ -84,6 +91,7 @@ wo.on('upload', (input, pm) => {
84
91
  console.log(`Server[port:${opt.port}]: upload`, input)
85
92
 
86
93
  try {
94
+ ms.push({ 'receive and return': input })
87
95
  let output = input
88
96
  pm.resolve(output)
89
97
  }
@@ -96,29 +104,32 @@ wo.on('upload', (input, pm) => {
96
104
  wo.on('download', (input, pm) => {
97
105
  console.log(`Server[port:${opt.port}]: download`, input)
98
106
 
107
+ let streamRead = null
99
108
  try {
100
109
  ms.push({ 'download': input })
101
110
 
102
111
  //fp
103
112
  let fp = `./test/1mb.7z`
104
113
 
105
- //streamRead
106
- let streamRead = fs.createReadStream(fp)
107
-
108
- //fileName
109
- let fileName = `1mb.7z`
114
+ //check, 檔案存在才往下
110
115
 
111
116
  //fileSize
112
117
  let stats = fs.statSync(fp)
113
118
  let fileSize = stats.size
114
119
 
120
+ //streamRead
121
+ streamRead = fs.createReadStream(fp)
122
+
123
+ //filename
124
+ let filename = `1mb中文.7z` //測試支援中文
125
+
115
126
  //fileType
116
127
  let fileType = 'application/x-7z-compressed'
117
128
 
118
129
  //output
119
130
  let output = {
120
131
  streamRead,
121
- fileName,
132
+ filename,
122
133
  fileSize,
123
134
  fileType,
124
135
  }
@@ -127,112 +138,52 @@ wo.on('download', (input, pm) => {
127
138
  }
128
139
  catch (err) {
129
140
  console.log('download error', err)
141
+ // try {
142
+ // streamRead.destroy() //若fs.createReadStream早於fs.statSync執行, 但fs.statSync發生錯誤時, stream得要destroy
143
+ // }
144
+ // catch (err) {}
130
145
  pm.reject('download error')
131
146
  }
132
147
 
133
148
  })
149
+ wo.on('error', (err) => {
150
+ console.log(`Server[port:${opt.port}]: error`, err)
151
+ })
134
152
  wo.on('handler', (data) => {
135
153
  // console.log(`Server[port:${opt.port}]: handler`, data)
136
154
  })
137
155
 
156
+ setTimeout(() => {
157
+ console.log('ms', ms)
158
+ // console.log('ms', JSON.stringify(ms))
159
+ wo.stop()
160
+ }, 3000)
161
+
138
162
  ```
139
- #### Example for w-converhp-client:
163
+
164
+ #### Example for w-converhp-client in nodejs:
140
165
  > **Link:** [[dev source code](https://github.com/yuda-lyu/w-converhp/blob/master/scla.mjs)]
141
166
  ```alias
142
- import WConverhpClient from 'w-converhp/dist/w-converhp-client.umd.js'
167
+ import FormData from 'form-data'
168
+ import WConverhpClient from './src/WConverhpClient.mjs'
169
+
170
+ let ms = []
143
171
 
144
172
  let opt = {
145
173
  FormData,
146
174
  url: 'http://localhost:8080',
147
175
  apiName: 'api',
176
+ getToken: () => {
177
+ return 'token-for-test'
178
+ },
148
179
  }
149
180
 
150
181
  //new
151
182
  let wo = new WConverhpClient(opt)
152
183
 
153
- async function execute(name, u8a) {
154
-
155
- //p
156
- let p = {
157
- a: 12,
158
- b: 34.56,
159
- c: 'test中文',
160
- d: {
161
- name,
162
- u8a,
163
- },
164
- }
165
- console.log('p', p)
166
-
167
- //execute
168
- await wo.execute('add', { p },
169
- function ({ prog, p, m }) {
170
- console.log('client web: execute: prog', prog, p, m)
171
- })
172
- .then(function(r) {
173
- console.log('client web: execute: add', r)
174
- console.log('r._bin.name', r._bin.name, 'r._bin.u8a', r._bin.u8a)
175
- // w.downloadFileFromU8Arr(r._bin.name, r._bin.u8a)
176
- })
177
- .catch(function (err) {
178
- console.log('client web: execute: catch', err)
179
- })
180
-
181
- }
182
-
183
- function executeWithU8a() {
184
- let core = async() => {
185
-
186
- //u8a
187
- let u8a = new Uint8Array([66, 97, 115])
188
- console.log('executeWithU8a u8a', u8a)
189
-
190
- //execute
191
- await execute('zdata.b1', u8a)
192
-
193
- }
194
- core()
195
- }
196
- executeWithU8a()
197
-
198
- function executeWithFile() {
199
- let core = async() => {
200
-
201
- //u8a
202
- let u8a = new Uint8Array(fs.readFileSync('../_data/10mb.7z'))
203
- console.log('executeWithFile u8a', u8a)
204
-
205
- //execute
206
- await execute('10mb.7z', u8a)
207
-
208
- }
209
- core()
210
- }
211
- executeWithFile()
212
-
213
- function uploadLargeFile() {
214
- let core = async() => {
215
-
216
- //u8a
217
- let u8a = new Uint8Array(fs.readFileSync('../_data/1000mb.7z'))
218
- console.log('u8a.length', u8a.length)
219
- console.log('uploadLargeFile u8a', u8a)
220
-
221
- await wo.upload('1000mb.7z', u8a,
222
- function ({ prog, p, m }) {
223
- console.log('client web: upload: prog', prog, p, m)
224
- })
225
- .then(function(res) {
226
- console.log('client web: upload: then', res)
227
- })
228
- .catch(function (err) {
229
- console.log('client web: upload: catch', err)
230
- })
231
-
232
- }
233
- core()
234
- }
235
- uploadLargeFile()
184
+ wo.on('error', (err) => {
185
+ console.log(`error`, err)
186
+ })
236
187
 
237
188
  function downloadLargeFile() {
238
189
  let core = async() => {
@@ -245,7 +196,7 @@ function downloadLargeFile() {
245
196
  }
246
197
  },
247
198
  {
248
- fdDownload: './',
199
+ fdDownload: './', //於後端nodejs環境才能提供
249
200
  })
250
201
  .then(function(res) {
251
202
  console.log('client web: download: then', res)
@@ -260,7 +211,9 @@ function downloadLargeFile() {
260
211
  }
261
212
  core()
262
213
  }
214
+
263
215
  downloadLargeFile()
216
+
264
217
  ```
265
218
 
266
219
  ### In a browser(UMD module):
@@ -268,9 +221,10 @@ downloadLargeFile()
268
221
 
269
222
  [Necessary] Add script for w-converhp-client.
270
223
  ```alias
271
- <script src="https://cdn.jsdelivr.net/npm/w-converhp@2.0.14/dist/w-converhp-client.umd.js"></script>
224
+ <script src="https://cdn.jsdelivr.net/npm/w-converhp@2.0.16/dist/w-converhp-client.umd.js"></script>
272
225
  ```
273
- #### Example for w-converhp-client:
226
+
227
+ #### Example for w-converhp-client in browser:
274
228
  > **Link:** [[dev source code](https://github.com/yuda-lyu/w-converhp/blob/master/web.html)]
275
229
  ```alias
276
230