Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): avoid crash on logging circular objects #5186

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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