ws-process 0.3.39 → 0.3.41
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 +60 -27
- package/lib/split-payload.js +4 -7
- package/package.json +1 -1
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
|
-
|
|
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,
|
|
@@ -726,18 +756,23 @@ function asyncSend(msg) {
|
|
|
726
756
|
}
|
|
727
757
|
}
|
|
728
758
|
|
|
729
|
-
|
|
730
|
-
jsonMSG.compressAsGzip = true;
|
|
731
|
-
}
|
|
759
|
+
jsonMSG.compressAsGzip = true;
|
|
732
760
|
|
|
733
761
|
console.log('%csending message:', 'color:green', jsonMSG);
|
|
762
|
+
|
|
734
763
|
const wsMSG = JSON.stringify(jsonMSG);
|
|
764
|
+
const encodedMsg = b64EncodeUnicode(wsMSG);
|
|
765
|
+
const compressedMsg = await compress(wsMSG);
|
|
766
|
+
console.log('req compression ratio: ', (compressedMsg.length / encodedMsg.length).toFixed(2), '=', compressedMsg.length, '/', encodedMsg.length);
|
|
735
767
|
|
|
736
768
|
const frameSize = Math.floor(1024 * 32 * 0.9);
|
|
737
|
-
const refinedMSG = cutStringByLength(
|
|
769
|
+
const refinedMSG = cutStringByLength(compressedMsg, frameSize);
|
|
738
770
|
|
|
739
771
|
if (refinedMSG.length === 1) {
|
|
740
|
-
local.wsProcess.wSocket.send(
|
|
772
|
+
local.wsProcess.wSocket.send(JSON.stringify({
|
|
773
|
+
action: jsonMSG.action,
|
|
774
|
+
body: compressedMsg,
|
|
775
|
+
}));
|
|
741
776
|
} else {
|
|
742
777
|
try {
|
|
743
778
|
const transaction = Sentry.startInactiveSpan({
|
|
@@ -786,10 +821,10 @@ function asyncSend(msg) {
|
|
|
786
821
|
|
|
787
822
|
const started = tryit(() => jsonData.started);
|
|
788
823
|
if (started) {
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
824
|
+
/*
|
|
825
|
+
The web socket has run the target lambda function asynchronously,
|
|
826
|
+
so we just wait for next message from lambda function
|
|
827
|
+
*/
|
|
793
828
|
|
|
794
829
|
const cProcess = getCurrentProcess(getState, started.processID);
|
|
795
830
|
if (cProcess) {
|
|
@@ -808,7 +843,7 @@ function asyncSend(msg) {
|
|
|
808
843
|
console.log('%conmessage(converted):', 'color:red', jsonResponse);
|
|
809
844
|
|
|
810
845
|
if (!jsonResponse) {
|
|
811
|
-
|
|
846
|
+
// It might be splitted
|
|
812
847
|
|
|
813
848
|
return;
|
|
814
849
|
}
|
|
@@ -833,11 +868,11 @@ function asyncSend(msg) {
|
|
|
833
868
|
if (
|
|
834
869
|
hasKeys(jsonResponse, ['message', 'connectionId', 'requestId'])
|
|
835
870
|
) {
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
871
|
+
/*
|
|
872
|
+
It's an error when lambda failed by
|
|
873
|
+
uncaught exceptions,
|
|
874
|
+
like "forbidden", "internal server error".
|
|
875
|
+
*/
|
|
841
876
|
|
|
842
877
|
if (local.validateResponse) {
|
|
843
878
|
local.store.dispatch(local.validateResponse(jsonResponse));
|
|
@@ -854,7 +889,7 @@ function asyncSend(msg) {
|
|
|
854
889
|
jsonResponse.processID,
|
|
855
890
|
);
|
|
856
891
|
if (!getNextProcessItem(getState, jsonResponse.processID)) {
|
|
857
|
-
|
|
892
|
+
// It is last, then append
|
|
858
893
|
|
|
859
894
|
local.dispatch(
|
|
860
895
|
addWSRequest({
|
|
@@ -1111,9 +1146,7 @@ function runProcess(processID, data) {
|
|
|
1111
1146
|
if (reqParameters && prcItem.reqParameters) {
|
|
1112
1147
|
// If this process item is re-added by ws-process
|
|
1113
1148
|
|
|
1114
|
-
reqParameters.subAction = `${
|
|
1115
|
-
reqParameters.subAction
|
|
1116
|
-
}_${getRandomID()}`;
|
|
1149
|
+
reqParameters.subAction = `${reqParameters.subAction}_${getRandomID()}`;
|
|
1117
1150
|
}
|
|
1118
1151
|
|
|
1119
1152
|
if (reqParameters && reqParameters.url) {
|
|
@@ -1352,10 +1385,10 @@ function init({
|
|
|
1352
1385
|
local.stage = stage;
|
|
1353
1386
|
local.wsURL = wsURL;
|
|
1354
1387
|
|
|
1355
|
-
local.beforeEachProcess = beforeEachProcess || (() => {});
|
|
1356
|
-
local.afterEachProcess = afterEachProcess || (() => {});
|
|
1357
|
-
local.beforeAllProcesses = beforeAllProcesses || (() => {});
|
|
1358
|
-
local.afterAllProcesses = afterAllProcesses || (() => {});
|
|
1388
|
+
local.beforeEachProcess = beforeEachProcess || (() => { });
|
|
1389
|
+
local.afterEachProcess = afterEachProcess || (() => { });
|
|
1390
|
+
local.beforeAllProcesses = beforeAllProcesses || (() => { });
|
|
1391
|
+
local.afterAllProcesses = afterAllProcesses || (() => { });
|
|
1359
1392
|
local.validateResponse = validateResponse;
|
|
1360
1393
|
}
|
|
1361
1394
|
|
package/lib/split-payload.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
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(
|
|
11
|
-
if (!isString(
|
|
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) {
|