ws-process 0.3.36 → 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 +44 -13
  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
  }
@@ -714,15 +751,9 @@ function asyncSend(msg) {
714
751
  onCloseWebSocket(event, dispatch);
715
752
  };
716
753
 
717
- local.wsProcess.wSocket.onmessage = (event) => {
718
- // console.log('%conmessage:', 'color:red', event.data);
719
- const jsonData = tryit(() => {
720
- const parsedData = JSON.parse(event.data);
721
- if (parsedData.isBase64Encoded === true) {
722
- return JSON.parse(b64DecodeUnicode(parsedData.response));
723
- }
724
- return parsedData;
725
- }, 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);
726
757
 
727
758
  // console.debug("%conmessage:", "color:red", jsonData);
728
759
  /* started has following attributes
@@ -753,7 +784,7 @@ function asyncSend(msg) {
753
784
  return;
754
785
  }
755
786
 
756
- const jsonResponse = convertResponseData(jsonData);
787
+ const jsonResponse = await convertResponseData(jsonData);
757
788
  console.log('%conmessage(converted):', 'color:red', jsonResponse);
758
789
 
759
790
  if (!jsonResponse) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ws-process",
3
- "version": "0.3.36",
3
+ "version": "0.3.37",
4
4
  "description": "Process Management for Web Socket Client",
5
5
  "main": "index.js",
6
6
  "scripts": {