ws-process 0.3.36 → 0.3.38
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 +48 -13
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -232,7 +232,32 @@ function b64DecodeUnicode(str) {
|
|
|
232
232
|
);
|
|
233
233
|
}
|
|
234
234
|
|
|
235
|
-
function
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|
|
@@ -688,6 +725,10 @@ function asyncSend(msg) {
|
|
|
688
725
|
}
|
|
689
726
|
}
|
|
690
727
|
|
|
728
|
+
if (['requestodata', 'requestsoap'].includes(jsonMSG.action)) {
|
|
729
|
+
jsonMSG.compressAsGzip = true;
|
|
730
|
+
}
|
|
731
|
+
|
|
691
732
|
console.log('%csending message:', 'color:green', jsonMSG);
|
|
692
733
|
const wsMSG = JSON.stringify(jsonMSG);
|
|
693
734
|
|
|
@@ -714,15 +755,9 @@ function asyncSend(msg) {
|
|
|
714
755
|
onCloseWebSocket(event, dispatch);
|
|
715
756
|
};
|
|
716
757
|
|
|
717
|
-
local.wsProcess.wSocket.onmessage = (event) => {
|
|
718
|
-
|
|
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);
|
|
758
|
+
local.wsProcess.wSocket.onmessage = async (event) => {
|
|
759
|
+
// console.log('%conmessage:', 'color:red', event.data);
|
|
760
|
+
const jsonData = tryit(() => JSON.parse(event.data), event.data);
|
|
726
761
|
|
|
727
762
|
// console.debug("%conmessage:", "color:red", jsonData);
|
|
728
763
|
/* started has following attributes
|
|
@@ -753,7 +788,7 @@ function asyncSend(msg) {
|
|
|
753
788
|
return;
|
|
754
789
|
}
|
|
755
790
|
|
|
756
|
-
const jsonResponse = convertResponseData(jsonData);
|
|
791
|
+
const jsonResponse = await convertResponseData(jsonData);
|
|
757
792
|
console.log('%conmessage(converted):', 'color:red', jsonResponse);
|
|
758
793
|
|
|
759
794
|
if (!jsonResponse) {
|