ws-process 0.3.39 → 0.3.40

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/index.js CHANGED
@@ -258,6 +258,36 @@ async function decompress(decodedRes) {
258
258
  return JSON.parse(uncompressedStrRes);
259
259
  }
260
260
 
261
+ async function compress(jsonString) {
262
+ // Convert JSON to Stream
263
+ const stream = new Blob([jsonString], {
264
+ type: 'application/json',
265
+ }).stream();
266
+
267
+ // gzip stream
268
+ const compressedReadableStream = stream.pipeThrough(
269
+ // eslint-disable-next-line no-undef
270
+ new CompressionStream('gzip'),
271
+ );
272
+
273
+ // create Response
274
+ const compressedResponse = await new Response(compressedReadableStream);
275
+
276
+ // Get response Blob
277
+ const blob = await compressedResponse.blob();
278
+ // Get the ArrayBuffer
279
+ const buffer = await blob.arrayBuffer();
280
+
281
+ // convert ArrayBuffer to base64 encoded string
282
+ const compressedBase64 = btoa(
283
+ String.fromCharCode(
284
+ ...new Uint8Array(buffer),
285
+ ),
286
+ );
287
+
288
+ return compressedBase64;
289
+ }
290
+
261
291
  async function convertResponseData(inData) {
262
292
  let jsonData;
263
293
 
@@ -309,7 +339,7 @@ async function convertResponseData(inData) {
309
339
  } catch (ex) {
310
340
  jsonData = b64decode(itemLocal.dataSplitted.join(''));
311
341
  jsonData = await decompress(jsonData);
312
- console.log('compression ratio:', inData.splitted.compressionRatio, '=', inData.splitted.compressedSize, '/', inData.splitted.uncompressedSize);
342
+ console.log('res compression ratio:', inData.splitted.compressionRatio, '=', inData.splitted.compressedSize, '/', inData.splitted.uncompressedSize);
313
343
  }
314
344
  } else {
315
345
  jsonData = itemLocal.dataSplitted.join('');
@@ -325,7 +355,7 @@ async function convertResponseData(inData) {
325
355
  } catch (ex) {
326
356
  jsonData = b64decode(inData.response);
327
357
  jsonData = await decompress(jsonData);
328
- console.log('compression ratio:', inData.compressionRatio, '=', inData.compressedSize, '/', inData.uncompressedSize);
358
+ console.log('res compression ratio:', inData.compressionRatio, '=', inData.compressedSize, '/', inData.uncompressedSize);
329
359
  }
330
360
  } else {
331
361
  jsonData = inData;
@@ -654,7 +684,7 @@ function asyncSend(msg) {
654
684
  )
655
685
  : false;
656
686
  if (hasError) {
657
- // reject the promise from reqHandler in runProcess
687
+ // reject the promise from reqHandler in runProcess
658
688
 
659
689
  const { callback } = cProcessItem;
660
690
  if (callback) {
@@ -689,7 +719,7 @@ function asyncSend(msg) {
689
719
  }
690
720
 
691
721
  local.store.dispatch(getWebSocket()).then(
692
- () => {
722
+ async () => {
693
723
  const jsonMSG = tryit(
694
724
  () => (typeof msg === 'string' ? JSON.parse(msg) : msg),
695
725
  msg,
@@ -731,13 +761,20 @@ function asyncSend(msg) {
731
761
  }
732
762
 
733
763
  console.log('%csending message:', 'color:green', jsonMSG);
764
+
734
765
  const wsMSG = JSON.stringify(jsonMSG);
766
+ const encodedMsg = b64EncodeUnicode(wsMSG);
767
+ const compressedMsg = await compress(wsMSG);
768
+ console.log('req compression ratio: ', (compressedMsg.length / encodedMsg.length).toFixed(2), '=', compressedMsg.length, '/', encodedMsg.length);
735
769
 
736
770
  const frameSize = Math.floor(1024 * 32 * 0.9);
737
- const refinedMSG = cutStringByLength(wsMSG, frameSize);
771
+ const refinedMSG = cutStringByLength(compressedMsg, frameSize);
738
772
 
739
773
  if (refinedMSG.length === 1) {
740
- local.wsProcess.wSocket.send(wsMSG);
774
+ local.wsProcess.wSocket.send(JSON.stringify({
775
+ action: jsonMSG.action,
776
+ body: compressedMsg,
777
+ }));
741
778
  } else {
742
779
  try {
743
780
  const transaction = Sentry.startInactiveSpan({
@@ -786,10 +823,10 @@ function asyncSend(msg) {
786
823
 
787
824
  const started = tryit(() => jsonData.started);
788
825
  if (started) {
789
- /*
790
- The web socket has run the target lambda function asynchronously,
791
- so we just wait for next message from lambda function
792
- */
826
+ /*
827
+ The web socket has run the target lambda function asynchronously,
828
+ so we just wait for next message from lambda function
829
+ */
793
830
 
794
831
  const cProcess = getCurrentProcess(getState, started.processID);
795
832
  if (cProcess) {
@@ -808,7 +845,7 @@ function asyncSend(msg) {
808
845
  console.log('%conmessage(converted):', 'color:red', jsonResponse);
809
846
 
810
847
  if (!jsonResponse) {
811
- // It might be splitted
848
+ // It might be splitted
812
849
 
813
850
  return;
814
851
  }
@@ -833,11 +870,11 @@ function asyncSend(msg) {
833
870
  if (
834
871
  hasKeys(jsonResponse, ['message', 'connectionId', 'requestId'])
835
872
  ) {
836
- /*
837
- It's an error when lambda failed by
838
- uncaught exceptions,
839
- like "forbidden", "internal server error".
840
- */
873
+ /*
874
+ It's an error when lambda failed by
875
+ uncaught exceptions,
876
+ like "forbidden", "internal server error".
877
+ */
841
878
 
842
879
  if (local.validateResponse) {
843
880
  local.store.dispatch(local.validateResponse(jsonResponse));
@@ -854,7 +891,7 @@ function asyncSend(msg) {
854
891
  jsonResponse.processID,
855
892
  );
856
893
  if (!getNextProcessItem(getState, jsonResponse.processID)) {
857
- // It is last, then append
894
+ // It is last, then append
858
895
 
859
896
  local.dispatch(
860
897
  addWSRequest({
@@ -1111,9 +1148,7 @@ function runProcess(processID, data) {
1111
1148
  if (reqParameters && prcItem.reqParameters) {
1112
1149
  // If this process item is re-added by ws-process
1113
1150
 
1114
- reqParameters.subAction = `${
1115
- reqParameters.subAction
1116
- }_${getRandomID()}`;
1151
+ reqParameters.subAction = `${reqParameters.subAction}_${getRandomID()}`;
1117
1152
  }
1118
1153
 
1119
1154
  if (reqParameters && reqParameters.url) {
@@ -1352,10 +1387,10 @@ function init({
1352
1387
  local.stage = stage;
1353
1388
  local.wsURL = wsURL;
1354
1389
 
1355
- local.beforeEachProcess = beforeEachProcess || (() => {});
1356
- local.afterEachProcess = afterEachProcess || (() => {});
1357
- local.beforeAllProcesses = beforeAllProcesses || (() => {});
1358
- local.afterAllProcesses = afterAllProcesses || (() => {});
1390
+ local.beforeEachProcess = beforeEachProcess || (() => { });
1391
+ local.afterEachProcess = afterEachProcess || (() => { });
1392
+ local.beforeAllProcesses = beforeAllProcesses || (() => { });
1393
+ local.afterAllProcesses = afterAllProcesses || (() => { });
1359
1394
  local.validateResponse = validateResponse;
1360
1395
  }
1361
1396
 
@@ -1,4 +1,5 @@
1
- import { isArray, isNumber, isString } from '@bsgp/lib-core/types';
1
+ // eslint-disable-next-line import/no-unresolved
2
+ import { isArray, isNumber, isString } from '@bsgp/lib-core';
2
3
 
3
4
  /**
4
5
  * This function checks string byte size.
@@ -7,8 +8,8 @@ import { isArray, isNumber, isString } from '@bsgp/lib-core/types';
7
8
  * @param {string} string string payload which will be cut into pieces
8
9
  * @param {number} size length of each cut pieces
9
10
  */
10
- function cutStringByLength(string, size = 30000) {
11
- if (!isString(string)) {
11
+ function cutStringByLength(encodedString, size = 30000) {
12
+ if (!isString(encodedString)) {
12
13
  throw new Error('first parameter should be string on function cutStringByLength');
13
14
  }
14
15
 
@@ -16,10 +17,6 @@ function cutStringByLength(string, size = 30000) {
16
17
  throw new Error('second parameter should be number on function cutStringByLength');
17
18
  }
18
19
 
19
- const encodedString = btoa(
20
- encodeURIComponent(string).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(`0x${p1}`)),
21
- );
22
-
23
20
  const payloadPieces = [];
24
21
  const payloadLength = encodedString.length;
25
22
  for (let idx = 0; idx < payloadLength; idx += size) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ws-process",
3
- "version": "0.3.39",
3
+ "version": "0.3.40",
4
4
  "description": "Process Management for Web Socket Client",
5
5
  "main": "index.js",
6
6
  "scripts": {