Skip to content

Commit 5bfb758

Browse files
lext-7HazAT
authored andcommitted
fix: add setPrototypeOf polyfill (getsentry#2127)
1 parent 8dfcdd3 commit 5bfb758

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/utils/src/error.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { setPrototypeOf } from './polyfill';
2+
13
/** An error emitted by Sentry SDKs and related utilities. */
24
export class SentryError extends Error {
35
/** Display name of this error instance. */
@@ -8,6 +10,6 @@ export class SentryError extends Error {
810

911
// tslint:disable:no-unsafe-any
1012
this.name = new.target.prototype.constructor.name;
11-
Object.setPrototypeOf(this, new.target.prototype);
13+
setPrototypeOf(this, new.target.prototype);
1214
}
1315
}

packages/utils/src/polyfill.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const setPrototypeOf =
2+
Object.setPrototypeOf || ({ __proto__: [] } instanceof Array ? setProtoOf : mixinProperties);
3+
4+
/**
5+
* setPrototypeOf polyfill using __proto__
6+
*/
7+
function setProtoOf<TTarget extends object, TProto>(obj: TTarget, proto: TProto): TTarget & TProto {
8+
// @ts-ignore
9+
obj.__proto__ = proto;
10+
return obj as TTarget & TProto;
11+
}
12+
13+
/**
14+
* setPrototypeOf polyfill using mixin
15+
*/
16+
function mixinProperties<TTarget extends object, TProto>(obj: TTarget, proto: TProto): TTarget & TProto {
17+
for (const prop in proto) {
18+
if (!obj.hasOwnProperty(prop)) {
19+
// @ts-ignore
20+
obj[prop] = proto[prop];
21+
}
22+
}
23+
24+
return obj as TTarget & TProto;
25+
}

0 commit comments

Comments
 (0)