Skip to content

Commit

Permalink
fix(core): avoid crash on logging circular objects (#5186)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Nov 2, 2021
1 parent c495bed commit 1451ec8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 53 deletions.
21 changes: 3 additions & 18 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,6 @@ const nativeBridge = (function (exports) {
win.Capacitor = cap;
win.Ionic.WebView = IonicWebView;
};
const safeStringify = (value) => {
const seen = new Set();
return JSON.stringify(value, (_k, v) => {
if (seen.has(v)) {
if (v === null)
return null;
else
return '...';
}
if (typeof v === 'object') {
seen.add(v);
}
return v;
});
};
const initLogger = (win, cap) => {
const BRIDGED_CONSOLE_METHODS = [
'debug',
Expand Down Expand Up @@ -248,7 +233,7 @@ const nativeBridge = (function (exports) {
const serializeConsoleMessage = (msg) => {
if (typeof msg === 'object') {
try {
msg = safeStringify(msg);
msg = JSON.stringify(msg);
}
catch (e) {
// ignore
Expand Down Expand Up @@ -317,7 +302,7 @@ const nativeBridge = (function (exports) {
postToNative = data => {
var _a;
try {
win.androidBridge.postMessage(safeStringify(data));
win.androidBridge.postMessage(JSON.stringify(data));
}
catch (e) {
(_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.error(e);
Expand Down Expand Up @@ -348,7 +333,7 @@ const nativeBridge = (function (exports) {
url: url,
line: lineNo,
col: columnNo,
errorObject: safeStringify(err),
errorObject: JSON.stringify(err),
},
};
if (err !== null) {
Expand Down
20 changes: 3 additions & 17 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,6 @@ const initBridge = (w: any): void => {
win.Ionic.WebView = IonicWebView;
};

const safeStringify = (value: any): string => {
const seen = new Set();
return JSON.stringify(value, (_k, v) => {
if (seen.has(v)) {
if (v === null) return null;
else return '...';
}
if (typeof v === 'object') {
seen.add(v);
}
return v;
});
};

const initLogger = (win: WindowCapacitor, cap: CapacitorInstance) => {
const BRIDGED_CONSOLE_METHODS: (keyof Console)[] = [
'debug',
Expand Down Expand Up @@ -291,7 +277,7 @@ const initBridge = (w: any): void => {
const serializeConsoleMessage = (msg: any): string => {
if (typeof msg === 'object') {
try {
msg = safeStringify(msg);
msg = JSON.stringify(msg);
} catch (e) {
// ignore
}
Expand Down Expand Up @@ -378,7 +364,7 @@ const initBridge = (w: any): void => {
// android platform
postToNative = data => {
try {
win.androidBridge.postMessage(safeStringify(data));
win.androidBridge.postMessage(JSON.stringify(data));
} catch (e) {
win?.console?.error(e);
}
Expand Down Expand Up @@ -408,7 +394,7 @@ const initBridge = (w: any): void => {
url: url,
line: lineNo,
col: columnNo,
errorObject: safeStringify(err),
errorObject: JSON.stringify(err),
},
};

Expand Down
21 changes: 3 additions & 18 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,6 @@ const nativeBridge = (function (exports) {
win.Capacitor = cap;
win.Ionic.WebView = IonicWebView;
};
const safeStringify = (value) => {
const seen = new Set();
return JSON.stringify(value, (_k, v) => {
if (seen.has(v)) {
if (v === null)
return null;
else
return '...';
}
if (typeof v === 'object') {
seen.add(v);
}
return v;
});
};
const initLogger = (win, cap) => {
const BRIDGED_CONSOLE_METHODS = [
'debug',
Expand Down Expand Up @@ -248,7 +233,7 @@ const nativeBridge = (function (exports) {
const serializeConsoleMessage = (msg) => {
if (typeof msg === 'object') {
try {
msg = safeStringify(msg);
msg = JSON.stringify(msg);
}
catch (e) {
// ignore
Expand Down Expand Up @@ -317,7 +302,7 @@ const nativeBridge = (function (exports) {
postToNative = data => {
var _a;
try {
win.androidBridge.postMessage(safeStringify(data));
win.androidBridge.postMessage(JSON.stringify(data));
}
catch (e) {
(_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.error(e);
Expand Down Expand Up @@ -348,7 +333,7 @@ const nativeBridge = (function (exports) {
url: url,
line: lineNo,
col: columnNo,
errorObject: safeStringify(err),
errorObject: JSON.stringify(err),
},
};
if (err !== null) {
Expand Down

0 comments on commit 1451ec8

Please sign in to comment.