w-converhp 2.0.15 → 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.
@@ -80,7 +80,7 @@
80
80
 
81
81
  <dt class="tag-source">Source:</dt>
82
82
  <dd class="tag-source"><ul class="dummy"><li>
83
- <a href="WConverhpServer.mjs.html">WConverhpServer.mjs</a>, <a href="WConverhpServer.mjs.html#line203">line 203</a>
83
+ <a href="WConverhpServer.mjs.html">WConverhpServer.mjs</a>, <a href="WConverhpServer.mjs.html#line188">line 188</a>
84
84
  </li></ul></dd>
85
85
 
86
86
 
@@ -127,7 +127,7 @@
127
127
 
128
128
  <h5 class="h5-examples">Example</h5>
129
129
 
130
- <pre class="prettyprint"><code>import fs from 'fs'
131
130
  port: 8080,
132
131
  apiName: 'api',
133
132
  pathStaticFiles: '.', //要存取專案資料夾下web.html, 故不能給dist
134
133
  verifyConn: async ({ apiType, authorization, headers, query }) => {
135
134
  console.log('verifyConn', `apiType[${apiType}]`, `authorization[${authorization}]`)
136
135
  let token = w.strdelleft(authorization, 7) //刪除Bearer
137
136
  if (!w.isestr(token)) {
138
137
  return false
139
138
  }
140
139
  // await w.delay(3000)
141
140
  return true
142
141
  },
143
142
  // console.log(`Server[port:${opt.port}]: execute`, func, input)
144
143
  console.log(`Server[port:${opt.port}]: execute`, func)
145
144
  try {
146
145
  if (func === 'add') {
147
146
  if (_.get(input, 'p.d.u8a', null)) {
148
147
  console.log('input.p.d.u8a', input.p.d.u8a)
149
148
  ms.push({ 'input.p.d.u8a': input.p.d.u8a })
150
149
  }
151
150
  let r = {
152
151
  _add: input.p.a + input.p.b,
153
152
  _data: [11, 22.22, 'abc', { x: '21', y: 65.43, z: 'test中文' }],
154
153
  _bin: {
155
154
  name: 'zdata.b2',
156
155
  u8a: new Uint8Array([52, 66, 97, 115]),
157
156
  },
158
157
  }
159
158
  pm.resolve(r)
160
159
  }
161
160
  else {
162
161
  console.log('invalid func')
163
162
  pm.reject('invalid func')
164
163
  }
165
164
  }
166
165
  catch (err) {
167
166
  console.log('execute error', err)
168
167
  pm.reject('execute error')
169
168
  }
170
169
  console.log(`Server[port:${opt.port}]: upload`, input)
171
170
  try {
172
171
  ms.push({ 'receive and return': input })
173
172
  let output = input
174
173
  pm.resolve(output)
175
174
  }
176
175
  catch (err) {
177
176
  console.log('upload error', err)
178
177
  pm.reject('upload error')
179
178
  }
180
179
  console.log(`Server[port:${opt.port}]: download-get-filename`, input)
181
180
  try {
182
181
  ms.push({ 'download': input })
183
182
  //filename
184
183
  let filename = `1mb中文.7z` //測試支援中文
185
184
  //output
186
185
  let output = {
187
186
  filename
188
187
  }
189
188
  pm.resolve(output)
190
189
  }
191
190
  catch (err) {
192
191
  console.log('download error', err)
193
192
  pm.reject('download error')
194
193
  }
195
194
  console.log(`Server[port:${opt.port}]: download`, input)
196
195
  try {
197
196
  ms.push({ 'download': input })
198
197
  //fp
199
198
  let fp = `./test/1mb.7z`
200
199
  //streamRead
201
200
  let streamRead = fs.createReadStream(fp)
202
201
  //fileName
203
202
  let fileName = `1mb中文.7z` //測試支援中文
204
203
  //fileSize
205
204
  let stats = fs.statSync(fp)
206
205
  let fileSize = stats.size
207
206
  //fileType
208
207
  let fileType = 'application/x-7z-compressed'
209
208
  //output
210
209
  let output = {
211
210
  streamRead,
212
211
  fileName,
213
212
  fileSize,
214
213
  fileType,
215
214
  }
216
215
  pm.resolve(output)
217
216
  }
218
217
  catch (err) {
219
218
  console.log('download error', err)
220
219
  pm.reject('download error')
221
220
  }
222
221
  console.log(`Server[port:${opt.port}]: error`, err)
223
222
  // console.log(`Server[port:${opt.port}]: handler`, data)
224
223
  console.log('ms', ms)
225
224
  // console.log('ms', JSON.stringify(ms))
226
225
  wo.stop()
226
+ <pre class="prettyprint"><code>import fs from 'fs'
227
227
  port: 8080,
228
228
  apiName: 'api',
229
229
  pathStaticFiles: '.', //要存取專案資料夾下web.html, 故不能給dist
230
230
  verifyConn: async ({ apiType, authorization, headers, query }) => {
231
231
  console.log('verifyConn', `apiType[${apiType}]`, `authorization[${authorization}]`)
232
232
  let token = w.strdelleft(authorization, 7) //刪除Bearer
233
233
  if (!w.isestr(token)) {
234
234
  return false
235
235
  }
236
236
  // await w.delay(3000)
237
237
  return true
238
238
  },
239
239
  // console.log(`Server[port:${opt.port}]: execute`, func, input)
240
240
  console.log(`Server[port:${opt.port}]: execute`, func)
241
241
  try {
242
242
  if (func === 'add') {
243
243
  if (_.get(input, 'p.d.u8a', null)) {
244
244
  console.log('input.p.d.u8a', input.p.d.u8a)
245
245
  ms.push({ 'input.p.d.u8a': input.p.d.u8a })
246
246
  }
247
247
  let r = {
248
248
  _add: input.p.a + input.p.b,
249
249
  _data: [11, 22.22, 'abc', { x: '21', y: 65.43, z: 'test中文' }],
250
250
  _bin: {
251
251
  name: 'zdata.b2',
252
252
  u8a: new Uint8Array([52, 66, 97, 115]),
253
253
  },
254
254
  }
255
255
  pm.resolve(r)
256
256
  }
257
257
  else {
258
258
  console.log('invalid func')
259
259
  pm.reject('invalid func')
260
260
  }
261
261
  }
262
262
  catch (err) {
263
263
  console.log('execute error', err)
264
264
  pm.reject('execute error')
265
265
  }
266
266
  console.log(`Server[port:${opt.port}]: upload`, input)
267
267
  try {
268
268
  ms.push({ 'receive and return': input })
269
269
  let output = input
270
270
  pm.resolve(output)
271
271
  }
272
272
  catch (err) {
273
273
  console.log('upload error', err)
274
274
  pm.reject('upload error')
275
275
  }
276
276
  console.log(`Server[port:${opt.port}]: download`, input)
277
277
  let streamRead = null
278
278
  try {
279
279
  ms.push({ 'download': input })
280
280
  //fp
281
281
  let fp = `./test/1mb.7z`
282
282
  //check, 檔案存在才往下
283
283
  //fileSize
284
284
  let stats = fs.statSync(fp)
285
285
  let fileSize = stats.size
286
286
  //streamRead
287
287
  streamRead = fs.createReadStream(fp)
288
288
  //filename
289
289
  let filename = `1mb中文.7z` //測試支援中文
290
290
  //fileType
291
291
  let fileType = 'application/x-7z-compressed'
292
292
  //output
293
293
  let output = {
294
294
  streamRead,
295
295
  filename,
296
296
  fileSize,
297
297
  fileType,
298
298
  }
299
299
  pm.resolve(output)
300
300
  }
301
301
  catch (err) {
302
302
  console.log('download error', err)
303
303
  // try {
304
304
  // streamRead.destroy() //若fs.createReadStream早於fs.statSync執行, 但fs.statSync發生錯誤時, stream得要destroy
305
305
  // }
306
306
  // catch (err) {}
307
307
  pm.reject('download error')
308
308
  }
309
309
  console.log(`Server[port:${opt.port}]: error`, err)
310
310
  // console.log(`Server[port:${opt.port}]: handler`, data)
311
311
  console.log('ms', ms)
312
312
  // console.log('ms', JSON.stringify(ms))
313
313
  wo.stop()
314
314
 
315
315
 
316
316
 
@@ -749,7 +749,7 @@
749
749
  <br class="clear">
750
750
 
751
751
  <footer>
752
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Wed Apr 09 2025 13:07:53 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
752
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Wed Apr 09 2025 14:36:52 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
753
753
  </footer>
754
754
 
755
755
  <script>prettyPrint();</script>
@@ -173,54 +173,35 @@ import mergeFiles from './mergeFiles.wk.umd.js'
173
173
  * }
174
174
  *
175
175
  * })
176
- * wo.on('download-get-filename', (input, pm) => {
177
- * console.log(`Server[port:${opt.port}]: download-get-filename`, input)
178
- *
179
- * try {
180
- * ms.push({ 'download': input })
181
- *
182
- * //filename
183
- * let filename = `1mb中文.7z` //測試支援中文
184
- *
185
- * //output
186
- * let output = {
187
- * filename
188
- * }
189
- *
190
- * pm.resolve(output)
191
- * }
192
- * catch (err) {
193
- * console.log('download error', err)
194
- * pm.reject('download error')
195
- * }
196
- *
197
- * })
198
176
  * wo.on('download', (input, pm) => {
199
177
  * console.log(`Server[port:${opt.port}]: download`, input)
200
178
  *
179
+ * let streamRead = null
201
180
  * try {
202
181
  * ms.push({ 'download': input })
203
182
  *
204
183
  * //fp
205
184
  * let fp = `./test/1mb.7z`
206
185
  *
207
- * //streamRead
208
- * let streamRead = fs.createReadStream(fp)
209
- *
210
- * //fileName
211
- * let fileName = `1mb中文.7z` //測試支援中文
186
+ * //check, 檔案存在才往下
212
187
  *
213
188
  * //fileSize
214
189
  * let stats = fs.statSync(fp)
215
190
  * let fileSize = stats.size
216
191
  *
192
+ * //streamRead
193
+ * streamRead = fs.createReadStream(fp)
194
+ *
195
+ * //filename
196
+ * let filename = `1mb中文.7z` //測試支援中文
197
+ *
217
198
  * //fileType
218
199
  * let fileType = 'application/x-7z-compressed'
219
200
  *
220
201
  * //output
221
202
  * let output = {
222
203
  * streamRead,
223
- * fileName,
204
+ * filename,
224
205
  * fileSize,
225
206
  * fileType,
226
207
  * }
@@ -229,6 +210,10 @@ import mergeFiles from './mergeFiles.wk.umd.js'
229
210
  * }
230
211
  * catch (err) {
231
212
  * console.log('download error', err)
213
+ * // try {
214
+ * // streamRead.destroy() //若fs.createReadStream早於fs.statSync執行, 但fs.statSync發生錯誤時, stream得要destroy
215
+ * // }
216
+ * // catch (err) {}
232
217
  * pm.reject('download error')
233
218
  * }
234
219
  *
@@ -423,36 +408,6 @@ function WConverhpServer(opt = {}) {
423
408
  return pm
424
409
  }
425
410
 
426
- //procDownloadGetFilename
427
- async function procDownloadGetFilename(input) {
428
- // console.log('procDownloadGetFilename', input)
429
-
430
- //pm, pmm
431
- let pm = genPm()
432
- let pmm = genPm()
433
-
434
- //重新處理回傳結果
435
- pmm
436
- .then((output) => {
437
-
438
- //resolve
439
- pm.resolve(output)
440
-
441
- })
442
- .catch((err) => {
443
- pm.reject(err)
444
- })
445
-
446
- if (true) {
447
-
448
- //download, 下載檔案
449
- eeEmit('download-get-filename', input, pmm)
450
-
451
- }
452
-
453
- return pm
454
- }
455
-
456
411
  //procDownload
457
412
  async function procDownload(input) {
458
413
  // console.log('procDownload', input)
@@ -1013,7 +968,7 @@ function WConverhpServer(opt = {}) {
1013
968
  let out = {}
1014
969
  let returnType = ''
1015
970
  let returnMsg = ''
1016
- await procDownloadGetFilename(inp)
971
+ await procDownload(inp)
1017
972
  .then(function(msg) {
1018
973
  out.success = msg
1019
974
  returnType = 'success'
@@ -1026,6 +981,32 @@ function WConverhpServer(opt = {}) {
1026
981
  })
1027
982
  // console.log('out', out)
1028
983
 
984
+ //r
985
+ let r = get(out, 'success')
986
+
987
+ //streamRead
988
+ let streamRead = get(r, 'streamRead')
989
+
990
+ //destroy, 不提供stream故須預先destroy
991
+ try {
992
+ streamRead.destroy()
993
+ }
994
+ catch (err) {}
995
+
996
+ //filename
997
+ let filename = get(r, 'filename')
998
+ if (!isestr(filename)) {
999
+ //已於前面destroy
1000
+ return responseU8aStreamWithError(res, 'invalid filename')
1001
+ }
1002
+
1003
+ //重新提供out
1004
+ out = {
1005
+ success: {
1006
+ filename,
1007
+ },
1008
+ }
1009
+
1029
1010
  //u8aOut
1030
1011
  let u8aOut = obj2u8arr(out)
1031
1012
  // console.log('u8aOut', u8aOut)
@@ -1056,12 +1037,12 @@ function WConverhpServer(opt = {}) {
1056
1037
  //query
1057
1038
  let query = get(req, 'query')
1058
1039
  query = iseobj(query) ? query : ''
1059
- console.log('query', query)
1040
+ // console.log('query', query)
1060
1041
 
1061
1042
  //token
1062
1043
  let token = get(query, 'token', '')
1063
1044
  token = isestr(token) ? token : ''
1064
- console.log('token', token)
1045
+ // console.log('token', token)
1065
1046
 
1066
1047
  //authorization
1067
1048
  let authorization = ''
@@ -1095,7 +1076,7 @@ function WConverhpServer(opt = {}) {
1095
1076
  //fileId
1096
1077
  let fileId = get(query, 'fileId', '')
1097
1078
  fileId = isestr(fileId) ? fileId : ''
1098
- console.log('fileId', fileId)
1079
+ // console.log('fileId', fileId)
1099
1080
 
1100
1081
  //check
1101
1082
  if (!isestr(fileId)) {
@@ -1129,17 +1110,21 @@ function WConverhpServer(opt = {}) {
1129
1110
  //streamRead
1130
1111
  let streamRead = get(r, 'streamRead')
1131
1112
 
1132
- // //fileName
1133
- // let fileName = get(r, 'fileName')
1134
- // if (!isestr(fileName)) {
1135
- // return responseU8aStreamWithError(res, 'invalid fileName')
1113
+ // //filename
1114
+ // let filename = get(r, 'filename')
1115
+ // if (!isestr(filename)) {
1116
+ // return responseU8aStreamWithError(res, 'invalid filename')
1136
1117
  // }
1137
- // fileName = str2b64(fileName) //headers內對中文支援度不佳須用base64傳
1138
- // console.log('fileName', fileName)
1118
+ // filename = str2b64(filename) //headers內對中文支援度不佳須用base64傳
1119
+ // console.log('filename', filename)
1139
1120
 
1140
1121
  //fileSize
1141
1122
  let fileSize = get(r, 'fileSize')
1142
1123
  if (!isNumber(fileSize)) {
1124
+ try {
1125
+ streamRead.destroy() //提供stream前發生錯誤, 得強制destroy
1126
+ }
1127
+ catch (err) {}
1143
1128
  return responseU8aStreamWithError(res, 'invalid fileSize')
1144
1129
  }
1145
1130
  // fileSize = cstr(fileSize)
@@ -1147,13 +1132,17 @@ function WConverhpServer(opt = {}) {
1147
1132
  //fileType
1148
1133
  let fileType = get(r, 'fileType')
1149
1134
  if (!isestr(fileType)) {
1135
+ try {
1136
+ streamRead.destroy() //提供stream前發生錯誤, 得強制destroy
1137
+ }
1138
+ catch (err) {}
1150
1139
  return responseU8aStreamWithError(res, 'invalid fileType')
1151
1140
  }
1152
1141
  fileType = cstr(fileType)
1153
1142
 
1154
1143
  return res.response(streamRead)
1155
1144
  .type(fileType)
1156
- // .header('Content-Disposition', `attachment; filename="${fileName}"`) //chrome會優先使用header內filename, 但header內支援中文度很差須用base64, 此導致chrome下載檔名只能為base64, 故一律改由前端(browser)先取得真實檔名後直接給予下載檔名, 避免用header提供真實檔名
1145
+ // .header('Content-Disposition', `attachment; filename="${filename}"`) //chrome會優先使用header內filename, 但header內支援中文度很差須用base64, 此導致chrome下載檔名只能為base64, 故一律改由前端(browser)先取得真實檔名後直接給予下載檔名, 避免用header提供真實檔名
1157
1146
  .header('Content-Length', fileSize)
1158
1147
  },
1159
1148
  }
@@ -1252,16 +1241,24 @@ function WConverhpServer(opt = {}) {
1252
1241
  //streamRead
1253
1242
  let streamRead = get(r, 'streamRead')
1254
1243
 
1255
- //fileName
1256
- let fileName = get(r, 'fileName')
1257
- if (!isestr(fileName)) {
1258
- return responseU8aStreamWithError(res, 'invalid fileName')
1244
+ //filename
1245
+ let filename = get(r, 'filename')
1246
+ if (!isestr(filename)) {
1247
+ try {
1248
+ streamRead.destroy() //提供stream前發生錯誤, 得強制destroy
1249
+ }
1250
+ catch (err) {}
1251
+ return responseU8aStreamWithError(res, 'invalid filename')
1259
1252
  }
1260
- fileName = str2b64(fileName) //headers內對中文支援度不佳須用base64傳
1253
+ filename = str2b64(filename) //headers內對中文支援度不佳須用base64傳
1261
1254
 
1262
1255
  //fileSize
1263
1256
  let fileSize = get(r, 'fileSize')
1264
1257
  if (!isNumber(fileSize)) {
1258
+ try {
1259
+ streamRead.destroy() //提供stream前發生錯誤, 得強制destroy
1260
+ }
1261
+ catch (err) {}
1265
1262
  return responseU8aStreamWithError(res, 'invalid fileSize')
1266
1263
  }
1267
1264
  // fileSize = cstr(fileSize)
@@ -1269,13 +1266,17 @@ function WConverhpServer(opt = {}) {
1269
1266
  //fileType
1270
1267
  let fileType = get(r, 'fileType')
1271
1268
  if (!isestr(fileType)) {
1269
+ try {
1270
+ streamRead.destroy() //提供stream前發生錯誤, 得強制destroy
1271
+ }
1272
+ catch (err) {}
1272
1273
  return responseU8aStreamWithError(res, 'invalid fileType')
1273
1274
  }
1274
1275
  fileType = cstr(fileType)
1275
1276
 
1276
1277
  return res.response(streamRead)
1277
1278
  .type(fileType)
1278
- .header('Content-Disposition', `attachment; filename="${fileName}"`) //前端(nodejs)用POST下載代表, 可基於header內base64檔名解析出並直接給予檔名, 不用預先取得檔名
1279
+ .header('Content-Disposition', `attachment; filename="${filename}"`) //針對前端(nodejs)用POST下載, 可基於header內base64檔名解析出並直接給予檔名, 不用預先取得檔名
1279
1280
  .header('Content-Length', fileSize)
1280
1281
  },
1281
1282
  }
@@ -1362,7 +1363,7 @@ export default WConverhpServer
1362
1363
  <br class="clear">
1363
1364
 
1364
1365
  <footer>
1365
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Wed Apr 09 2025 13:07:53 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1366
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Wed Apr 09 2025 14:36:52 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1366
1367
  </footer>
1367
1368
 
1368
1369
  <script>prettyPrint();</script>
package/docs/index.html CHANGED
@@ -71,7 +71,7 @@
71
71
  <br class="clear">
72
72
 
73
73
  <footer>
74
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Wed Apr 09 2025 13:07:53 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
74
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Wed Apr 09 2025 14:36:52 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
75
75
  </footer>
76
76
 
77
77
  <script>prettyPrint();</script>
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "w-converhp",
3
- "version": "2.0.15",
3
+ "version": "2.0.16",
4
4
  "main": "dist/w-converhp-server.umd.js",
5
5
  "dependencies": {
6
6
  "@hapi/hapi": "^21.4.0",
7
7
  "@hapi/inert": "^7.1.0",
8
8
  "axios": "^1.8.4",
9
9
  "lodash-es": "^4.17.21",
10
- "wsemi": "^1.7.75"
10
+ "wsemi": "^1.7.77"
11
11
  },
12
12
  "devDependencies": {
13
13
  "form-data": "^4.0.2",
@@ -812,23 +812,24 @@ function WConverhpClient(opt) {
812
812
  if (ispm(token)) {
813
813
  token = await token
814
814
  }
815
+ console.log('token', token)
815
816
 
816
817
  //send download-get-filename
817
818
  let msg = { fileId }
818
819
  let resMg = await send('download-get-filename', msg, { dataType: 'json' })
819
- // console.log('resMg', resMg)
820
+ console.log('resMg', resMg)
820
821
 
821
822
  //filename
822
823
  let filename = get(resMg, 'filename', '')
823
- // console.log('filename', filename)
824
+ console.log('filename', filename)
824
825
 
825
826
  //urlUse
826
827
  let urlUse = getUrlUse('download-get')
827
- // console.log('urlUse', urlUse)
828
+ console.log('urlUse', urlUse)
828
829
 
829
830
  //url
830
831
  let url = `${urlUse}?fileId=${fileId}&token=${token}`
831
- // console.log('url', url)
832
+ console.log('url', url)
832
833
 
833
834
  //透過a元素打url下載, 讓瀏覽器認定為直接下載模式, 由瀏覽器展示下載進度與排入正在下載清單
834
835
  let a = document.createElement('a')