@supabase/auth-js 2.61.0
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/LICENSE +21 -0
- package/README.md +50 -0
- package/dist/main/AuthAdminApi.d.ts +4 -0
- package/dist/main/AuthAdminApi.d.ts.map +1 -0
- package/dist/main/AuthAdminApi.js +9 -0
- package/dist/main/AuthAdminApi.js.map +1 -0
- package/dist/main/AuthClient.d.ts +4 -0
- package/dist/main/AuthClient.d.ts.map +1 -0
- package/dist/main/AuthClient.js +9 -0
- package/dist/main/AuthClient.js.map +1 -0
- package/dist/main/GoTrueAdminApi.d.ts +99 -0
- package/dist/main/GoTrueAdminApi.d.ts.map +1 -0
- package/dist/main/GoTrueAdminApi.js +268 -0
- package/dist/main/GoTrueAdminApi.js.map +1 -0
- package/dist/main/GoTrueClient.d.ts +432 -0
- package/dist/main/GoTrueClient.d.ts.map +1 -0
- package/dist/main/GoTrueClient.js +1889 -0
- package/dist/main/GoTrueClient.js.map +1 -0
- package/dist/main/index.d.ts +9 -0
- package/dist/main/index.d.ts.map +1 -0
- package/dist/main/index.js +35 -0
- package/dist/main/index.js.map +1 -0
- package/dist/main/lib/constants.d.ts +12 -0
- package/dist/main/lib/constants.d.ts.map +1 -0
- package/dist/main/lib/constants.js +14 -0
- package/dist/main/lib/constants.js.map +1 -0
- package/dist/main/lib/errors.d.ts +96 -0
- package/dist/main/lib/errors.d.ts.map +1 -0
- package/dist/main/lib/errors.js +135 -0
- package/dist/main/lib/errors.js.map +1 -0
- package/dist/main/lib/fetch.d.ts +33 -0
- package/dist/main/lib/fetch.d.ts.map +1 -0
- package/dist/main/lib/fetch.js +162 -0
- package/dist/main/lib/fetch.js.map +1 -0
- package/dist/main/lib/helpers.d.ts +48 -0
- package/dist/main/lib/helpers.d.ts.map +1 -0
- package/dist/main/lib/helpers.js +292 -0
- package/dist/main/lib/helpers.js.map +1 -0
- package/dist/main/lib/local-storage.d.ts +13 -0
- package/dist/main/lib/local-storage.d.ts.map +1 -0
- package/dist/main/lib/local-storage.js +46 -0
- package/dist/main/lib/local-storage.js.map +1 -0
- package/dist/main/lib/locks.d.ts +44 -0
- package/dist/main/lib/locks.d.ts.map +1 -0
- package/dist/main/lib/locks.js +118 -0
- package/dist/main/lib/locks.js.map +1 -0
- package/dist/main/lib/polyfills.d.ts +5 -0
- package/dist/main/lib/polyfills.d.ts.map +1 -0
- package/dist/main/lib/polyfills.js +30 -0
- package/dist/main/lib/polyfills.js.map +1 -0
- package/dist/main/lib/types.d.ts +948 -0
- package/dist/main/lib/types.d.ts.map +1 -0
- package/dist/main/lib/types.js +3 -0
- package/dist/main/lib/types.js.map +1 -0
- package/dist/main/lib/version.d.ts +2 -0
- package/dist/main/lib/version.d.ts.map +1 -0
- package/dist/main/lib/version.js +6 -0
- package/dist/main/lib/version.js.map +1 -0
- package/dist/module/AuthAdminApi.d.ts +4 -0
- package/dist/module/AuthAdminApi.d.ts.map +1 -0
- package/dist/module/AuthAdminApi.js +4 -0
- package/dist/module/AuthAdminApi.js.map +1 -0
- package/dist/module/AuthClient.d.ts +4 -0
- package/dist/module/AuthClient.d.ts.map +1 -0
- package/dist/module/AuthClient.js +4 -0
- package/dist/module/AuthClient.js.map +1 -0
- package/dist/module/GoTrueAdminApi.d.ts +99 -0
- package/dist/module/GoTrueAdminApi.d.ts.map +1 -0
- package/dist/module/GoTrueAdminApi.js +265 -0
- package/dist/module/GoTrueAdminApi.js.map +1 -0
- package/dist/module/GoTrueClient.d.ts +432 -0
- package/dist/module/GoTrueClient.d.ts.map +1 -0
- package/dist/module/GoTrueClient.js +1883 -0
- package/dist/module/GoTrueClient.js.map +1 -0
- package/dist/module/index.d.ts +9 -0
- package/dist/module/index.d.ts.map +1 -0
- package/dist/module/index.js +9 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/lib/constants.d.ts +12 -0
- package/dist/module/lib/constants.d.ts.map +1 -0
- package/dist/module/lib/constants.js +11 -0
- package/dist/module/lib/constants.js.map +1 -0
- package/dist/module/lib/errors.d.ts +96 -0
- package/dist/module/lib/errors.d.ts.map +1 -0
- package/dist/module/lib/errors.js +117 -0
- package/dist/module/lib/errors.js.map +1 -0
- package/dist/module/lib/fetch.d.ts +33 -0
- package/dist/module/lib/fetch.d.ts.map +1 -0
- package/dist/module/lib/fetch.js +152 -0
- package/dist/module/lib/fetch.js.map +1 -0
- package/dist/module/lib/helpers.d.ts +48 -0
- package/dist/module/lib/helpers.d.ts.map +1 -0
- package/dist/module/lib/helpers.js +249 -0
- package/dist/module/lib/helpers.js.map +1 -0
- package/dist/module/lib/local-storage.d.ts +13 -0
- package/dist/module/lib/local-storage.d.ts.map +1 -0
- package/dist/module/lib/local-storage.js +42 -0
- package/dist/module/lib/local-storage.js.map +1 -0
- package/dist/module/lib/locks.d.ts +44 -0
- package/dist/module/lib/locks.d.ts.map +1 -0
- package/dist/module/lib/locks.js +112 -0
- package/dist/module/lib/locks.js.map +1 -0
- package/dist/module/lib/polyfills.d.ts +5 -0
- package/dist/module/lib/polyfills.d.ts.map +1 -0
- package/dist/module/lib/polyfills.js +26 -0
- package/dist/module/lib/polyfills.js.map +1 -0
- package/dist/module/lib/types.d.ts +948 -0
- package/dist/module/lib/types.d.ts.map +1 -0
- package/dist/module/lib/types.js +2 -0
- package/dist/module/lib/types.js.map +1 -0
- package/dist/module/lib/version.d.ts +2 -0
- package/dist/module/lib/version.d.ts.map +1 -0
- package/dist/module/lib/version.js +3 -0
- package/dist/module/lib/version.js.map +1 -0
- package/package.json +69 -0
- package/src/AuthAdminApi.ts +5 -0
- package/src/AuthClient.ts +5 -0
- package/src/GoTrueAdminApi.ts +333 -0
- package/src/GoTrueClient.ts +2470 -0
- package/src/index.ts +12 -0
- package/src/lib/constants.ts +10 -0
- package/src/lib/errors.ts +150 -0
- package/src/lib/fetch.ts +238 -0
- package/src/lib/helpers.ts +306 -0
- package/src/lib/local-storage.ts +49 -0
- package/src/lib/locks.ts +140 -0
- package/src/lib/polyfills.ts +23 -0
- package/src/lib/types.ts +1120 -0
- package/src/lib/version.ts +2 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { SupportedStorage } from './types';
|
|
2
|
+
export declare function expiresAt(expiresIn: number): number;
|
|
3
|
+
export declare function uuid(): string;
|
|
4
|
+
export declare const isBrowser: () => boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Checks whether localStorage is supported on this browser.
|
|
7
|
+
*/
|
|
8
|
+
export declare const supportsLocalStorage: () => boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Extracts parameters encoded in the URL both in the query and fragment.
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseParametersFromURL(href: string): {
|
|
13
|
+
[parameter: string]: string;
|
|
14
|
+
};
|
|
15
|
+
declare type Fetch = typeof fetch;
|
|
16
|
+
export declare const resolveFetch: (customFetch?: Fetch) => Fetch;
|
|
17
|
+
export declare const looksLikeFetchResponse: (maybeResponse: unknown) => maybeResponse is Response;
|
|
18
|
+
export declare const setItemAsync: (storage: SupportedStorage, key: string, data: any) => Promise<void>;
|
|
19
|
+
export declare const getItemAsync: (storage: SupportedStorage, key: string) => Promise<unknown>;
|
|
20
|
+
export declare const removeItemAsync: (storage: SupportedStorage, key: string) => Promise<void>;
|
|
21
|
+
export declare function decodeBase64URL(value: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* A deferred represents some asynchronous work that is not yet finished, which
|
|
24
|
+
* may or may not culminate in a value.
|
|
25
|
+
* Taken from: https://github.com/mike-north/types/blob/master/src/async.ts
|
|
26
|
+
*/
|
|
27
|
+
export declare class Deferred<T = any> {
|
|
28
|
+
static promiseConstructor: PromiseConstructor;
|
|
29
|
+
readonly promise: PromiseLike<T>;
|
|
30
|
+
readonly resolve: (value?: T | PromiseLike<T>) => void;
|
|
31
|
+
readonly reject: (reason?: any) => any;
|
|
32
|
+
constructor();
|
|
33
|
+
}
|
|
34
|
+
export declare function decodeJWTPayload(token: string): any;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a promise that resolves to null after some time.
|
|
37
|
+
*/
|
|
38
|
+
export declare function sleep(time: number): Promise<null>;
|
|
39
|
+
/**
|
|
40
|
+
* Converts the provided async function into a retryable function. Each result
|
|
41
|
+
* or thrown error is sent to the isRetryable function which should return true
|
|
42
|
+
* if the function should run again.
|
|
43
|
+
*/
|
|
44
|
+
export declare function retryable<T>(fn: (attempt: number) => Promise<T>, isRetryable: (attempt: number, error: any | null, result?: T) => boolean): Promise<T>;
|
|
45
|
+
export declare function generatePKCEVerifier(): string;
|
|
46
|
+
export declare function generatePKCEChallenge(verifier: string): Promise<string>;
|
|
47
|
+
export {};
|
|
48
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/lib/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAC1C,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,UAG1C;AAED,wBAAgB,IAAI,WAMnB;AAED,eAAO,MAAM,SAAS,eAAwC,CAAA;AAO9D;;GAEG;AACH,eAAO,MAAM,oBAAoB,eAmChC,CAAA;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM;;EAsBlD;AAED,aAAK,KAAK,GAAG,OAAO,KAAK,CAAA;AAEzB,eAAO,MAAM,YAAY,iBAAkB,KAAK,KAAG,KAWlD,CAAA;AAED,eAAO,MAAM,sBAAsB,kBAAmB,OAAO,8BAS5D,CAAA;AAGD,eAAO,MAAM,YAAY,YACd,gBAAgB,OACpB,MAAM,QACL,GAAG,KACR,QAAQ,IAAI,CAEd,CAAA;AAED,eAAO,MAAM,YAAY,YAAmB,gBAAgB,OAAO,MAAM,KAAG,QAAQ,OAAO,CAY1F,CAAA;AAED,eAAO,MAAM,eAAe,YAAmB,gBAAgB,OAAO,MAAM,KAAG,QAAQ,IAAI,CAE1F,CAAA;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CA0BrD;AAED;;;;GAIG;AACH,qBAAa,QAAQ,CAAC,CAAC,GAAG,GAAG;IAC3B,OAAc,kBAAkB,EAAE,kBAAkB,CAAU;IAE9D,SAAgB,OAAO,EAAG,WAAW,CAAC,CAAC,CAAC,CAAA;IAExC,SAAgB,OAAO,EAAG,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAE9D,SAAgB,MAAM,EAAG,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,GAAG,CAAA;;CAW/C;AAGD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,OAgB7C;AAED;;GAEG;AACH,wBAAsB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIvD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,EAAE,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,EACnC,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC,KAAK,OAAO,GACvE,OAAO,CAAC,CAAC,CAAC,CAuBZ;AAOD,wBAAgB,oBAAoB,WAcnC;AAiBD,wBAAsB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,mBAc3D"}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
export function expiresAt(expiresIn) {
|
|
2
|
+
const timeNow = Math.round(Date.now() / 1000);
|
|
3
|
+
return timeNow + expiresIn;
|
|
4
|
+
}
|
|
5
|
+
export function uuid() {
|
|
6
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
7
|
+
const r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
8
|
+
return v.toString(16);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export const isBrowser = () => typeof document !== 'undefined';
|
|
12
|
+
const localStorageWriteTests = {
|
|
13
|
+
tested: false,
|
|
14
|
+
writable: false,
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether localStorage is supported on this browser.
|
|
18
|
+
*/
|
|
19
|
+
export const supportsLocalStorage = () => {
|
|
20
|
+
if (!isBrowser()) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
if (typeof globalThis.localStorage !== 'object') {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
// DOM exception when accessing `localStorage`
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (localStorageWriteTests.tested) {
|
|
33
|
+
return localStorageWriteTests.writable;
|
|
34
|
+
}
|
|
35
|
+
const randomKey = `lswt-${Math.random()}${Math.random()}`;
|
|
36
|
+
try {
|
|
37
|
+
globalThis.localStorage.setItem(randomKey, randomKey);
|
|
38
|
+
globalThis.localStorage.removeItem(randomKey);
|
|
39
|
+
localStorageWriteTests.tested = true;
|
|
40
|
+
localStorageWriteTests.writable = true;
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
// localStorage can't be written to
|
|
44
|
+
// https://www.chromium.org/for-testers/bug-reporting-guidelines/uncaught-securityerror-failed-to-read-the-localstorage-property-from-window-access-is-denied-for-this-document
|
|
45
|
+
localStorageWriteTests.tested = true;
|
|
46
|
+
localStorageWriteTests.writable = false;
|
|
47
|
+
}
|
|
48
|
+
return localStorageWriteTests.writable;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Extracts parameters encoded in the URL both in the query and fragment.
|
|
52
|
+
*/
|
|
53
|
+
export function parseParametersFromURL(href) {
|
|
54
|
+
const result = {};
|
|
55
|
+
const url = new URL(href);
|
|
56
|
+
if (url.hash && url.hash[0] === '#') {
|
|
57
|
+
try {
|
|
58
|
+
const hashSearchParams = new URLSearchParams(url.hash.substring(1));
|
|
59
|
+
hashSearchParams.forEach((value, key) => {
|
|
60
|
+
result[key] = value;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
// hash is not a query string
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// search parameters take precedence over hash parameters
|
|
68
|
+
url.searchParams.forEach((value, key) => {
|
|
69
|
+
result[key] = value;
|
|
70
|
+
});
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
export const resolveFetch = (customFetch) => {
|
|
74
|
+
let _fetch;
|
|
75
|
+
if (customFetch) {
|
|
76
|
+
_fetch = customFetch;
|
|
77
|
+
}
|
|
78
|
+
else if (typeof fetch === 'undefined') {
|
|
79
|
+
_fetch = (...args) => import('@supabase/node-fetch').then(({ default: fetch }) => fetch(...args));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
_fetch = fetch;
|
|
83
|
+
}
|
|
84
|
+
return (...args) => _fetch(...args);
|
|
85
|
+
};
|
|
86
|
+
export const looksLikeFetchResponse = (maybeResponse) => {
|
|
87
|
+
return (typeof maybeResponse === 'object' &&
|
|
88
|
+
maybeResponse !== null &&
|
|
89
|
+
'status' in maybeResponse &&
|
|
90
|
+
'ok' in maybeResponse &&
|
|
91
|
+
'json' in maybeResponse &&
|
|
92
|
+
typeof maybeResponse.json === 'function');
|
|
93
|
+
};
|
|
94
|
+
// Storage helpers
|
|
95
|
+
export const setItemAsync = async (storage, key, data) => {
|
|
96
|
+
await storage.setItem(key, JSON.stringify(data));
|
|
97
|
+
};
|
|
98
|
+
export const getItemAsync = async (storage, key) => {
|
|
99
|
+
const value = await storage.getItem(key);
|
|
100
|
+
if (!value) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
return JSON.parse(value);
|
|
105
|
+
}
|
|
106
|
+
catch (_a) {
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
export const removeItemAsync = async (storage, key) => {
|
|
111
|
+
await storage.removeItem(key);
|
|
112
|
+
};
|
|
113
|
+
export function decodeBase64URL(value) {
|
|
114
|
+
const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
115
|
+
let base64 = '';
|
|
116
|
+
let chr1, chr2, chr3;
|
|
117
|
+
let enc1, enc2, enc3, enc4;
|
|
118
|
+
let i = 0;
|
|
119
|
+
value = value.replace('-', '+').replace('_', '/');
|
|
120
|
+
while (i < value.length) {
|
|
121
|
+
enc1 = key.indexOf(value.charAt(i++));
|
|
122
|
+
enc2 = key.indexOf(value.charAt(i++));
|
|
123
|
+
enc3 = key.indexOf(value.charAt(i++));
|
|
124
|
+
enc4 = key.indexOf(value.charAt(i++));
|
|
125
|
+
chr1 = (enc1 << 2) | (enc2 >> 4);
|
|
126
|
+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
|
127
|
+
chr3 = ((enc3 & 3) << 6) | enc4;
|
|
128
|
+
base64 = base64 + String.fromCharCode(chr1);
|
|
129
|
+
if (enc3 != 64 && chr2 != 0) {
|
|
130
|
+
base64 = base64 + String.fromCharCode(chr2);
|
|
131
|
+
}
|
|
132
|
+
if (enc4 != 64 && chr3 != 0) {
|
|
133
|
+
base64 = base64 + String.fromCharCode(chr3);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return base64;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A deferred represents some asynchronous work that is not yet finished, which
|
|
140
|
+
* may or may not culminate in a value.
|
|
141
|
+
* Taken from: https://github.com/mike-north/types/blob/master/src/async.ts
|
|
142
|
+
*/
|
|
143
|
+
export class Deferred {
|
|
144
|
+
constructor() {
|
|
145
|
+
// eslint-disable-next-line @typescript-eslint/no-extra-semi
|
|
146
|
+
;
|
|
147
|
+
this.promise = new Deferred.promiseConstructor((res, rej) => {
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/no-extra-semi
|
|
149
|
+
;
|
|
150
|
+
this.resolve = res;
|
|
151
|
+
this.reject = rej;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
Deferred.promiseConstructor = Promise;
|
|
156
|
+
// Taken from: https://stackoverflow.com/questions/38552003/how-to-decode-jwt-token-in-javascript-without-using-a-library
|
|
157
|
+
export function decodeJWTPayload(token) {
|
|
158
|
+
// Regex checks for base64url format
|
|
159
|
+
const base64UrlRegex = /^([a-z0-9_-]{4})*($|[a-z0-9_-]{3}=?$|[a-z0-9_-]{2}(==)?$)$/i;
|
|
160
|
+
const parts = token.split('.');
|
|
161
|
+
if (parts.length !== 3) {
|
|
162
|
+
throw new Error('JWT is not valid: not a JWT structure');
|
|
163
|
+
}
|
|
164
|
+
if (!base64UrlRegex.test(parts[1])) {
|
|
165
|
+
throw new Error('JWT is not valid: payload is not in base64url format');
|
|
166
|
+
}
|
|
167
|
+
const base64Url = parts[1];
|
|
168
|
+
return JSON.parse(decodeBase64URL(base64Url));
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Creates a promise that resolves to null after some time.
|
|
172
|
+
*/
|
|
173
|
+
export async function sleep(time) {
|
|
174
|
+
return await new Promise((accept) => {
|
|
175
|
+
setTimeout(() => accept(null), time);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Converts the provided async function into a retryable function. Each result
|
|
180
|
+
* or thrown error is sent to the isRetryable function which should return true
|
|
181
|
+
* if the function should run again.
|
|
182
|
+
*/
|
|
183
|
+
export function retryable(fn, isRetryable) {
|
|
184
|
+
const promise = new Promise((accept, reject) => {
|
|
185
|
+
// eslint-disable-next-line @typescript-eslint/no-extra-semi
|
|
186
|
+
;
|
|
187
|
+
(async () => {
|
|
188
|
+
for (let attempt = 0; attempt < Infinity; attempt++) {
|
|
189
|
+
try {
|
|
190
|
+
const result = await fn(attempt);
|
|
191
|
+
if (!isRetryable(attempt, null, result)) {
|
|
192
|
+
accept(result);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch (e) {
|
|
197
|
+
if (!isRetryable(attempt, e)) {
|
|
198
|
+
reject(e);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
})();
|
|
204
|
+
});
|
|
205
|
+
return promise;
|
|
206
|
+
}
|
|
207
|
+
function dec2hex(dec) {
|
|
208
|
+
return ('0' + dec.toString(16)).substr(-2);
|
|
209
|
+
}
|
|
210
|
+
// Functions below taken from: https://stackoverflow.com/questions/63309409/creating-a-code-verifier-and-challenge-for-pkce-auth-on-spotify-api-in-reactjs
|
|
211
|
+
export function generatePKCEVerifier() {
|
|
212
|
+
const verifierLength = 56;
|
|
213
|
+
const array = new Uint32Array(verifierLength);
|
|
214
|
+
if (typeof crypto === 'undefined') {
|
|
215
|
+
const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
|
|
216
|
+
const charSetLen = charSet.length;
|
|
217
|
+
let verifier = '';
|
|
218
|
+
for (let i = 0; i < verifierLength; i++) {
|
|
219
|
+
verifier += charSet.charAt(Math.floor(Math.random() * charSetLen));
|
|
220
|
+
}
|
|
221
|
+
return verifier;
|
|
222
|
+
}
|
|
223
|
+
crypto.getRandomValues(array);
|
|
224
|
+
return Array.from(array, dec2hex).join('');
|
|
225
|
+
}
|
|
226
|
+
async function sha256(randomString) {
|
|
227
|
+
const encoder = new TextEncoder();
|
|
228
|
+
const encodedData = encoder.encode(randomString);
|
|
229
|
+
const hash = await crypto.subtle.digest('SHA-256', encodedData);
|
|
230
|
+
const bytes = new Uint8Array(hash);
|
|
231
|
+
return Array.from(bytes)
|
|
232
|
+
.map((c) => String.fromCharCode(c))
|
|
233
|
+
.join('');
|
|
234
|
+
}
|
|
235
|
+
function base64urlencode(str) {
|
|
236
|
+
return btoa(str).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
237
|
+
}
|
|
238
|
+
export async function generatePKCEChallenge(verifier) {
|
|
239
|
+
const hasCryptoSupport = typeof crypto !== 'undefined' &&
|
|
240
|
+
typeof crypto.subtle !== 'undefined' &&
|
|
241
|
+
typeof TextEncoder !== 'undefined';
|
|
242
|
+
if (!hasCryptoSupport) {
|
|
243
|
+
console.warn('WebCrypto API is not supported. Code challenge method will default to use plain instead of sha256.');
|
|
244
|
+
return verifier;
|
|
245
|
+
}
|
|
246
|
+
const hashed = await sha256(verifier);
|
|
247
|
+
return base64urlencode(hashed);
|
|
248
|
+
}
|
|
249
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/lib/helpers.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,SAAS,CAAC,SAAiB;IACzC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAA;IAC7C,OAAO,OAAO,GAAG,SAAS,CAAA;AAC5B,CAAC;AAED,MAAM,UAAU,IAAI;IAClB,OAAO,sCAAsC,CAAC,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC;QACxE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,EAChC,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAA;QACpC,OAAO,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;IACvB,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,OAAO,QAAQ,KAAK,WAAW,CAAA;AAE9D,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|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SupportedStorage } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Provides safe access to the globalThis.localStorage property.
|
|
4
|
+
*/
|
|
5
|
+
export declare const localStorageAdapter: SupportedStorage;
|
|
6
|
+
/**
|
|
7
|
+
* Returns a localStorage-like object that stores the key-value pairs in
|
|
8
|
+
* memory.
|
|
9
|
+
*/
|
|
10
|
+
export declare function memoryLocalStorageAdapter(store?: {
|
|
11
|
+
[key: string]: string;
|
|
12
|
+
}): SupportedStorage;
|
|
13
|
+
//# sourceMappingURL=local-storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-storage.d.ts","sourceRoot":"","sources":["../../../src/lib/local-storage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE1C;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,gBAsBjC,CAAA;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,GAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAO,GAAG,gBAAgB,CAcjG"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { supportsLocalStorage } from './helpers';
|
|
2
|
+
/**
|
|
3
|
+
* Provides safe access to the globalThis.localStorage property.
|
|
4
|
+
*/
|
|
5
|
+
export const localStorageAdapter = {
|
|
6
|
+
getItem: (key) => {
|
|
7
|
+
if (!supportsLocalStorage()) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return globalThis.localStorage.getItem(key);
|
|
11
|
+
},
|
|
12
|
+
setItem: (key, value) => {
|
|
13
|
+
if (!supportsLocalStorage()) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
globalThis.localStorage.setItem(key, value);
|
|
17
|
+
},
|
|
18
|
+
removeItem: (key) => {
|
|
19
|
+
if (!supportsLocalStorage()) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
globalThis.localStorage.removeItem(key);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Returns a localStorage-like object that stores the key-value pairs in
|
|
27
|
+
* memory.
|
|
28
|
+
*/
|
|
29
|
+
export function memoryLocalStorageAdapter(store = {}) {
|
|
30
|
+
return {
|
|
31
|
+
getItem: (key) => {
|
|
32
|
+
return store[key] || null;
|
|
33
|
+
},
|
|
34
|
+
setItem: (key, value) => {
|
|
35
|
+
store[key] = value;
|
|
36
|
+
},
|
|
37
|
+
removeItem: (key) => {
|
|
38
|
+
delete store[key];
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=local-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-storage.js","sourceRoot":"","sources":["../../../src/lib/local-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAGhD;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAqB;IACnD,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;QACf,IAAI,CAAC,oBAAoB,EAAE,EAAE;YAC3B,OAAO,IAAI,CAAA;SACZ;QAED,OAAO,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IAC7C,CAAC;IACD,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE;QACtB,IAAI,CAAC,oBAAoB,EAAE,EAAE;YAC3B,OAAM;SACP;QAED,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IAC7C,CAAC;IACD,UAAU,EAAE,CAAC,GAAG,EAAE,EAAE;QAClB,IAAI,CAAC,oBAAoB,EAAE,EAAE;YAC3B,OAAM;SACP;QAED,UAAU,CAAC,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;IACzC,CAAC;CACF,CAAA;AAED;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CAAC,QAAmC,EAAE;IAC7E,OAAO;QACL,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;YACf,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,IAAI,CAAA;QAC3B,CAAC;QAED,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE;YACtB,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QACpB,CAAC;QAED,UAAU,EAAE,CAAC,GAAG,EAAE,EAAE;YAClB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAA;QACnB,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @experimental
|
|
3
|
+
*/
|
|
4
|
+
export declare const internals: {
|
|
5
|
+
/**
|
|
6
|
+
* @experimental
|
|
7
|
+
*/
|
|
8
|
+
debug: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare abstract class LockAcquireTimeoutError extends Error {
|
|
11
|
+
readonly isAcquireTimeout = true;
|
|
12
|
+
constructor(message: string);
|
|
13
|
+
}
|
|
14
|
+
export declare class NavigatorLockAcquireTimeoutError extends LockAcquireTimeoutError {
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Implements a global exclusive lock using the Navigator LockManager API. It
|
|
18
|
+
* is available on all browsers released after 2022-03-15 with Safari being the
|
|
19
|
+
* last one to release support. If the API is not available, this function will
|
|
20
|
+
* throw. Make sure you check availablility before configuring {@link
|
|
21
|
+
* GoTrueClient}.
|
|
22
|
+
*
|
|
23
|
+
* You can turn on debugging by setting the `supabase.gotrue-js.locks.debug`
|
|
24
|
+
* local storage item to `true`.
|
|
25
|
+
*
|
|
26
|
+
* Internals:
|
|
27
|
+
*
|
|
28
|
+
* Since the LockManager API does not preserve stack traces for the async
|
|
29
|
+
* function passed in the `request` method, a trick is used where acquiring the
|
|
30
|
+
* lock releases a previously started promise to run the operation in the `fn`
|
|
31
|
+
* function. The lock waits for that promise to finish (with or without error),
|
|
32
|
+
* while the function will finally wait for the result anyway.
|
|
33
|
+
*
|
|
34
|
+
* @experimental
|
|
35
|
+
*
|
|
36
|
+
* @param name Name of the lock to be acquired.
|
|
37
|
+
* @param acquireTimeout If negative, no timeout. If 0 an error is thrown if
|
|
38
|
+
* the lock can't be acquired without waiting. If positive, the lock acquire
|
|
39
|
+
* will time out after so many milliseconds. An error is
|
|
40
|
+
* a timeout if it has `isAcquireTimeout` set to true.
|
|
41
|
+
* @param fn The operation to run once the lock is acquired.
|
|
42
|
+
*/
|
|
43
|
+
export declare function navigatorLock<R>(name: string, acquireTimeout: number, fn: () => Promise<R>): Promise<R>;
|
|
44
|
+
//# sourceMappingURL=locks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locks.d.ts","sourceRoot":"","sources":["../../../src/lib/locks.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,SAAS;IACpB;;OAEG;;CAOJ,CAAA;AAED,8BAAsB,uBAAwB,SAAQ,KAAK;IACzD,SAAgB,gBAAgB,QAAO;gBAE3B,OAAO,EAAE,MAAM;CAG5B;AAED,qBAAa,gCAAiC,SAAQ,uBAAuB;CAAG;AAEhF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,aAAa,CAAC,CAAC,EACnC,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,MAAM,EACtB,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACnB,OAAO,CAAC,CAAC,CAAC,CAiFZ"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { supportsLocalStorage } from './helpers';
|
|
2
|
+
/**
|
|
3
|
+
* @experimental
|
|
4
|
+
*/
|
|
5
|
+
export const internals = {
|
|
6
|
+
/**
|
|
7
|
+
* @experimental
|
|
8
|
+
*/
|
|
9
|
+
debug: !!(globalThis &&
|
|
10
|
+
supportsLocalStorage() &&
|
|
11
|
+
globalThis.localStorage &&
|
|
12
|
+
globalThis.localStorage.getItem('supabase.gotrue-js.locks.debug') === 'true'),
|
|
13
|
+
};
|
|
14
|
+
export class LockAcquireTimeoutError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.isAcquireTimeout = true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class NavigatorLockAcquireTimeoutError extends LockAcquireTimeoutError {
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Implements a global exclusive lock using the Navigator LockManager API. It
|
|
24
|
+
* is available on all browsers released after 2022-03-15 with Safari being the
|
|
25
|
+
* last one to release support. If the API is not available, this function will
|
|
26
|
+
* throw. Make sure you check availablility before configuring {@link
|
|
27
|
+
* GoTrueClient}.
|
|
28
|
+
*
|
|
29
|
+
* You can turn on debugging by setting the `supabase.gotrue-js.locks.debug`
|
|
30
|
+
* local storage item to `true`.
|
|
31
|
+
*
|
|
32
|
+
* Internals:
|
|
33
|
+
*
|
|
34
|
+
* Since the LockManager API does not preserve stack traces for the async
|
|
35
|
+
* function passed in the `request` method, a trick is used where acquiring the
|
|
36
|
+
* lock releases a previously started promise to run the operation in the `fn`
|
|
37
|
+
* function. The lock waits for that promise to finish (with or without error),
|
|
38
|
+
* while the function will finally wait for the result anyway.
|
|
39
|
+
*
|
|
40
|
+
* @experimental
|
|
41
|
+
*
|
|
42
|
+
* @param name Name of the lock to be acquired.
|
|
43
|
+
* @param acquireTimeout If negative, no timeout. If 0 an error is thrown if
|
|
44
|
+
* the lock can't be acquired without waiting. If positive, the lock acquire
|
|
45
|
+
* will time out after so many milliseconds. An error is
|
|
46
|
+
* a timeout if it has `isAcquireTimeout` set to true.
|
|
47
|
+
* @param fn The operation to run once the lock is acquired.
|
|
48
|
+
*/
|
|
49
|
+
export async function navigatorLock(name, acquireTimeout, fn) {
|
|
50
|
+
if (internals.debug) {
|
|
51
|
+
console.log('@supabase/gotrue-js: navigatorLock: acquire lock', name, acquireTimeout);
|
|
52
|
+
}
|
|
53
|
+
const abortController = new globalThis.AbortController();
|
|
54
|
+
if (acquireTimeout > 0) {
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
abortController.abort();
|
|
57
|
+
if (internals.debug) {
|
|
58
|
+
console.log('@supabase/gotrue-js: navigatorLock acquire timed out', name);
|
|
59
|
+
}
|
|
60
|
+
}, acquireTimeout);
|
|
61
|
+
}
|
|
62
|
+
// MDN article: https://developer.mozilla.org/en-US/docs/Web/API/LockManager/request
|
|
63
|
+
return await globalThis.navigator.locks.request(name, acquireTimeout === 0
|
|
64
|
+
? {
|
|
65
|
+
mode: 'exclusive',
|
|
66
|
+
ifAvailable: true,
|
|
67
|
+
}
|
|
68
|
+
: {
|
|
69
|
+
mode: 'exclusive',
|
|
70
|
+
signal: abortController.signal,
|
|
71
|
+
}, async (lock) => {
|
|
72
|
+
if (lock) {
|
|
73
|
+
if (internals.debug) {
|
|
74
|
+
console.log('@supabase/gotrue-js: navigatorLock: acquired', name, lock.name);
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
return await fn();
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
if (internals.debug) {
|
|
81
|
+
console.log('@supabase/gotrue-js: navigatorLock: released', name, lock.name);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
if (acquireTimeout === 0) {
|
|
87
|
+
if (internals.debug) {
|
|
88
|
+
console.log('@supabase/gotrue-js: navigatorLock: not immediately available', name);
|
|
89
|
+
}
|
|
90
|
+
throw new NavigatorLockAcquireTimeoutError(`Acquiring an exclusive Navigator LockManager lock "${name}" immediately failed`);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
if (internals.debug) {
|
|
94
|
+
try {
|
|
95
|
+
const result = await globalThis.navigator.locks.query();
|
|
96
|
+
console.log('@supabase/gotrue-js: Navigator LockManager state', JSON.stringify(result, null, ' '));
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
console.warn('@supabase/gotrue-js: Error when querying Navigator LockManager state', e);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Browser is not following the Navigator LockManager spec, it
|
|
103
|
+
// returned a null lock when we didn't use ifAvailable. So we can
|
|
104
|
+
// pretend the lock is acquired in the name of backward compatibility
|
|
105
|
+
// and user experience and just run the function.
|
|
106
|
+
console.warn('@supabase/gotrue-js: Navigator LockManager returned a null lock when using #request without ifAvailable set to true, it appears this browser is not following the LockManager spec https://developer.mozilla.org/en-US/docs/Web/API/LockManager/request');
|
|
107
|
+
return await fn();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=locks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locks.js","sourceRoot":"","sources":["../../../src/lib/locks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAEhD;;GAEG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB;;OAEG;IACH,KAAK,EAAE,CAAC,CAAC,CACP,UAAU;QACV,oBAAoB,EAAE;QACtB,UAAU,CAAC,YAAY;QACvB,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,gCAAgC,CAAC,KAAK,MAAM,CAC7E;CACF,CAAA;AAED,MAAM,OAAgB,uBAAwB,SAAQ,KAAK;IAGzD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QAHA,qBAAgB,GAAG,IAAI,CAAA;IAIvC,CAAC;CACF;AAED,MAAM,OAAO,gCAAiC,SAAQ,uBAAuB;CAAG;AAEhF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,IAAY,EACZ,cAAsB,EACtB,EAAoB;IAEpB,IAAI,SAAS,CAAC,KAAK,EAAE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sD,IAAI,sBAAsB,CACjF,CAAA;aACF;iBAAM;gBACL,IAAI,SAAS,CAAC,KAAK,EAAE;oBACnB,IAAI;wBACF,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;wBAEvD,OAAO,CAAC,GAAG,CACT,kDAAkD,EAClD,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CACnC,CAAA;qBACF;oBAAC,OAAO,CAAM,EAAE;wBACf,OAAO,CAAC,IAAI,CACV,sEAAsE,EACtE,CAAC,CACF,CAAA;qBACF;iBACF;gBAED,8DAA8D;gBAC9D,iEAAiE;gBACjE,qEAAqE;gBACrE,iDAAiD;gBACjD,OAAO,CAAC,IAAI,CACV,yPAAyP,CAC1P,CAAA;gBAED,OAAO,MAAM,EAAE,EAAE,CAAA;aAClB;SACF;IACH,CAAC,CACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polyfills.d.ts","sourceRoot":"","sources":["../../../src/lib/polyfills.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,kBAAkB,SAmBjC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* https://mathiasbynens.be/notes/globalthis
|
|
3
|
+
*/
|
|
4
|
+
export function polyfillGlobalThis() {
|
|
5
|
+
if (typeof globalThis === 'object')
|
|
6
|
+
return;
|
|
7
|
+
try {
|
|
8
|
+
Object.defineProperty(Object.prototype, '__magic__', {
|
|
9
|
+
get: function () {
|
|
10
|
+
return this;
|
|
11
|
+
},
|
|
12
|
+
configurable: true,
|
|
13
|
+
});
|
|
14
|
+
// @ts-expect-error 'Allow access to magic'
|
|
15
|
+
__magic__.globalThis = __magic__;
|
|
16
|
+
// @ts-expect-error 'Allow access to magic'
|
|
17
|
+
delete Object.prototype.__magic__;
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
if (typeof self !== 'undefined') {
|
|
21
|
+
// @ts-expect-error 'Allow access to globals'
|
|
22
|
+
self.globalThis = self;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=polyfills.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polyfills.js","sourceRoot":"","sources":["../../../src/lib/polyfills.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,kBAAkB;IAChC,IAAI,OAAO,UAAU,KAAK,QAAQ;QAAE,OAAM;IAC1C,IAAI;QACF,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,EAAE;YACnD,GAAG,EAAE;gBACH,OAAO,IAAI,CAAA;YACb,CAAC;YACD,YAAY,EAAE,IAAI;SACnB,CAAC,CAAA;QACF,2CAA2C;QAC3C,SAAS,CAAC,UAAU,GAAG,SAAS,CAAA;QAChC,2CAA2C;QAC3C,OAAO,MAAM,CAAC,SAAS,CAAC,SAAS,CAAA;KAClC;IAAC,OAAO,CAAC,EAAE;QACV,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;YAC/B,6CAA6C;YAC7C,IAAI,CAAC,UAAU,GAAG,IAAI,CAAA;SACvB;KACF;AACH,CAAC"}
|