ws-process 0.3.35 → 0.3.37

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.
Files changed (2) hide show
  1. package/index.js +50 -16
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -232,7 +232,32 @@ function b64DecodeUnicode(str) {
232
232
  );
233
233
  }
234
234
 
235
- function convertResponseData(inData) {
235
+ function b64decode(str) {
236
+ const binaryString = window.atob(str);
237
+ const len = binaryString.length;
238
+ const bytes = new Uint8Array(new ArrayBuffer(len));
239
+ for (let i = 0; i < len; i += 1) {
240
+ bytes[i] = binaryString.charCodeAt(i);
241
+ }
242
+ return bytes;
243
+ }
244
+
245
+ async function decompress(decodedRes) {
246
+ // base64 encoding to Blob
247
+ const stream = new Blob([decodedRes], {
248
+ type: 'application/json',
249
+ }).stream();
250
+ const compressedReadableStream = stream.pipeThrough(
251
+ // eslint-disable-next-line no-undef
252
+ new DecompressionStream('gzip'),
253
+ );
254
+ const resp = await new Response(compressedReadableStream);
255
+ const blob = await resp.blob();
256
+ const uncompressedStrRes = await blob.text();
257
+ return JSON.parse(uncompressedStrRes);
258
+ }
259
+
260
+ async function convertResponseData(inData) {
236
261
  let jsonData;
237
262
 
238
263
  if (inData.splitted) {
@@ -278,7 +303,13 @@ function convertResponseData(inData) {
278
303
  };
279
304
  }
280
305
  if (inData.splitted.isBase64Encoded) {
281
- jsonData = b64DecodeUnicode(itemLocal.dataSplitted.join(''));
306
+ try {
307
+ jsonData = b64DecodeUnicode(itemLocal.dataSplitted.join(''));
308
+ } catch (ex) {
309
+ jsonData = b64decode(itemLocal.dataSplitted.join(''));
310
+ jsonData = await decompress(jsonData);
311
+ console.log('compression ratio:', inData.splitted.compressionRatio, '=', inData.splitted.compressedSize, '/', inData.splitted.uncompressedSize);
312
+ }
282
313
  } else {
283
314
  jsonData = itemLocal.dataSplitted.join('');
284
315
  }
@@ -288,7 +319,13 @@ function convertResponseData(inData) {
288
319
  return null;
289
320
  }
290
321
  } else if (inData.isBase64Encoded) {
291
- jsonData = b64DecodeUnicode(inData.response);
322
+ try {
323
+ jsonData = b64DecodeUnicode(inData.response);
324
+ } catch (ex) {
325
+ jsonData = b64decode(inData.response);
326
+ jsonData = await decompress(jsonData);
327
+ console.log('compression ratio:', inData.compressionRatio, '=', inData.compressedSize, '/', inData.uncompressedSize);
328
+ }
292
329
  } else {
293
330
  jsonData = inData;
294
331
  }
@@ -428,9 +465,12 @@ function handleResponseHandler(res, getState, isLast = true, isFirst = true) {
428
465
  }
429
466
  if (isValid === null) {
430
467
  local.afterEachProcess(prc, local.wsProcess.processes);
431
- prc.reject(
432
- 'Request is determined to be failed, and the process is terminated as your wish',
433
- );
468
+ const errMsg = ['Request is determined to be failed', 'and the process is terminated as your wish'].join(', ');
469
+ if (tryit(() => prc.options.doNotRejectIfTerminate) === true) {
470
+ console.warn(errMsg);
471
+ } else {
472
+ prc.reject(errMsg);
473
+ }
434
474
  wrapClearWSProcess(res.processID);
435
475
  return { terminate: true, cbData };
436
476
  }
@@ -711,15 +751,9 @@ function asyncSend(msg) {
711
751
  onCloseWebSocket(event, dispatch);
712
752
  };
713
753
 
714
- local.wsProcess.wSocket.onmessage = (event) => {
715
- // console.log('%conmessage:', 'color:red', event.data);
716
- const jsonData = tryit(() => {
717
- const parsedData = JSON.parse(event.data);
718
- if (parsedData.isBase64Encoded === true) {
719
- return JSON.parse(b64DecodeUnicode(parsedData.response));
720
- }
721
- return parsedData;
722
- }, event.data);
754
+ local.wsProcess.wSocket.onmessage = async (event) => {
755
+ // console.log('%conmessage:', 'color:red', event.data);
756
+ const jsonData = tryit(() => JSON.parse(event.data), event.data);
723
757
 
724
758
  // console.debug("%conmessage:", "color:red", jsonData);
725
759
  /* started has following attributes
@@ -750,7 +784,7 @@ function asyncSend(msg) {
750
784
  return;
751
785
  }
752
786
 
753
- const jsonResponse = convertResponseData(jsonData);
787
+ const jsonResponse = await convertResponseData(jsonData);
754
788
  console.log('%conmessage(converted):', 'color:red', jsonResponse);
755
789
 
756
790
  if (!jsonResponse) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ws-process",
3
- "version": "0.3.35",
3
+ "version": "0.3.37",
4
4
  "description": "Process Management for Web Socket Client",
5
5
  "main": "index.js",
6
6
  "scripts": {