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

Vue 3 infinite log warning with enumerating ownKeys #4573

Closed
wants to merge 14 commits into from
54 changes: 51 additions & 3 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,54 @@ const nativeBridge = (function (exports) {
win.Capacitor = cap;
win.Ionic.WebView = IonicWebView;
};
const shouldBeCloneable = (o) => {
const type = typeof o;
return (type === "undefined" ||
o === null ||
type === "boolean" ||
type === "number" ||
type === "string" ||
o instanceof Date ||
o instanceof RegExp ||
o instanceof Blob ||
o instanceof File ||
o instanceof FileList ||
o instanceof ArrayBuffer ||
o instanceof Array ||
o instanceof Map ||
o instanceof Set);
};
const isCloneable = (obj) => {
try {
postMessage(obj, "*");
}
catch (error) {
if ((error === null || error === void 0 ? void 0 : error.code) === 25)
return false; // DATA_CLONE_ERR
}
return true;
};
// https://stackoverflow.com/a/62544968
const isProxy = (obj) => {
const _shouldBeCloneable = shouldBeCloneable(obj);
const _isCloneable = isCloneable(obj);
if (_isCloneable) {
return false;
}
if (!_shouldBeCloneable) {
return "maybe";
}
return _shouldBeCloneable && !_isCloneable;
};
const safeStringify = (value) => {
if (isProxy(value)) {
return "proxy";
}
const seen = new Set();
return JSON.stringify(value, (_k, v) => {
if (isProxy(v)) {
return "proxy";
}
if (seen.has(v)) {
return '...';
}
Expand Down Expand Up @@ -242,6 +287,9 @@ const nativeBridge = (function (exports) {
typeof c.dir === 'function');
};
const serializeConsoleMessage = (msg) => {
if (isProxy(msg)) {
return "proxy";
}
if (typeof msg === 'object') {
try {
msg = safeStringify(msg);
Expand All @@ -258,12 +306,12 @@ const nativeBridge = (function (exports) {
if (win.console && isIos) {
for (const logfn of BRIDGED_CONSOLE_METHODS) {
win.console[logfn] = (...args) => {
const msgs = [...args];
originalConsole[logfn](...msgs);
// eslint-disable-next-line prefer-spread
originalConsole[logfn].apply(originalConsole, args);
try {
cap.toNative('Console', 'log', {
level: logfn,
message: msgs.map(serializeConsoleMessage).join(' '),
message: args.map(serializeConsoleMessage).join(' '),
});
}
catch (e) {
Expand Down
59 changes: 55 additions & 4 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,59 @@ const initBridge = (w: any): void => {
win.Ionic.WebView = IonicWebView;
};

const shouldBeCloneable = (o: any): boolean => {
const type = typeof o;
return (
type === "undefined" ||
o === null ||
type === "boolean" ||
type === "number" ||
type === "string" ||
o instanceof Date ||
o instanceof RegExp ||
o instanceof Blob ||
o instanceof File ||
o instanceof FileList ||
o instanceof ArrayBuffer ||
o instanceof Array ||
o instanceof Map ||
o instanceof Set
);
}

const isCloneable = (obj: any): boolean => {
try {
postMessage(obj, "*");
} catch (error) {
if (error?.code === 25) return false; // DATA_CLONE_ERR
}

return true;
}

// https://stackoverflow.com/a/62544968
const isProxy = (obj: any): string | boolean => {
const _shouldBeCloneable = shouldBeCloneable(obj);
const _isCloneable = isCloneable(obj);

if(_isCloneable) {
return false;
}

if(!_shouldBeCloneable) {
return "maybe";
}

return _shouldBeCloneable && !_isCloneable;
}

const safeStringify = (value: any): string => {
if (isProxy(value)) { return "proxy" }
const seen = new Set();
return JSON.stringify(value, (_k, v) => {
if (isProxy(v)) {
return "proxy"
}
if (seen.has(v)) {
return '...';
}
Expand Down Expand Up @@ -286,8 +336,9 @@ const initBridge = (w: any): void => {
typeof c.dir === 'function'
);
};

const serializeConsoleMessage = (msg: any): string => {
if (isProxy(msg)) { return "proxy" }
if (typeof msg === 'object') {
try {
msg = safeStringify(msg);
Expand All @@ -306,14 +357,14 @@ const initBridge = (w: any): void => {
if (win.console && isIos) {
for (const logfn of BRIDGED_CONSOLE_METHODS) {
win.console[logfn] = (...args: any[]) => {
const msgs = [...args];

originalConsole[logfn](...msgs);
// eslint-disable-next-line prefer-spread
originalConsole[logfn].apply(originalConsole, args);

try {
cap.toNative('Console', 'log', {
level: logfn,
message: msgs.map(serializeConsoleMessage).join(' '),
message: args.map(serializeConsoleMessage).join(' '),
});
} catch (e) {
// error converting/posting console messages
Expand Down
54 changes: 51 additions & 3 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,54 @@ const nativeBridge = (function (exports) {
win.Capacitor = cap;
win.Ionic.WebView = IonicWebView;
};
const shouldBeCloneable = (o) => {
const type = typeof o;
return (type === "undefined" ||
o === null ||
type === "boolean" ||
type === "number" ||
type === "string" ||
o instanceof Date ||
o instanceof RegExp ||
o instanceof Blob ||
o instanceof File ||
o instanceof FileList ||
o instanceof ArrayBuffer ||
o instanceof Array ||
o instanceof Map ||
o instanceof Set);
};
const isCloneable = (obj) => {
try {
postMessage(obj, "*");
}
catch (error) {
if ((error === null || error === void 0 ? void 0 : error.code) === 25)
return false; // DATA_CLONE_ERR
}
return true;
};
// https://stackoverflow.com/a/62544968
const isProxy = (obj) => {
const _shouldBeCloneable = shouldBeCloneable(obj);
const _isCloneable = isCloneable(obj);
if (_isCloneable) {
return false;
}
if (!_shouldBeCloneable) {
return "maybe";
}
return _shouldBeCloneable && !_isCloneable;
};
const safeStringify = (value) => {
if (isProxy(value)) {
return "proxy";
}
const seen = new Set();
return JSON.stringify(value, (_k, v) => {
if (isProxy(v)) {
return "proxy";
}
if (seen.has(v)) {
return '...';
}
Expand Down Expand Up @@ -242,6 +287,9 @@ const nativeBridge = (function (exports) {
typeof c.dir === 'function');
};
const serializeConsoleMessage = (msg) => {
if (isProxy(msg)) {
return "proxy";
}
if (typeof msg === 'object') {
try {
msg = safeStringify(msg);
Expand All @@ -258,12 +306,12 @@ const nativeBridge = (function (exports) {
if (win.console && isIos) {
for (const logfn of BRIDGED_CONSOLE_METHODS) {
win.console[logfn] = (...args) => {
const msgs = [...args];
originalConsole[logfn](...msgs);
// eslint-disable-next-line prefer-spread
originalConsole[logfn].apply(originalConsole, args);
try {
cap.toNative('Console', 'log', {
level: logfn,
message: msgs.map(serializeConsoleMessage).join(' '),
message: args.map(serializeConsoleMessage).join(' '),
});
}
catch (e) {
Expand Down