@tern-secure/shared 1.3.0-canary.v20251108045933 → 1.3.0-canary.v20251125170702
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/dist/chunk-B2SN66XE.mjs +25 -0
- package/dist/chunk-B2SN66XE.mjs.map +1 -0
- package/dist/{chunk-33U3M4YY.mjs → chunk-IGYBIQYE.mjs} +14 -7
- package/dist/chunk-IGYBIQYE.mjs.map +1 -0
- package/dist/chunk-N2V3PKFE.mjs +61 -0
- package/dist/chunk-N2V3PKFE.mjs.map +1 -0
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/errors.mjs +1 -1
- package/dist/errors.mjs.map +1 -1
- package/dist/loadScript.js +68 -6
- package/dist/loadScript.js.map +1 -1
- package/dist/loadScript.mjs +2 -1
- package/dist/loadTernUIScript.d.mts +14 -6
- package/dist/loadTernUIScript.d.ts +14 -6
- package/dist/loadTernUIScript.js +122 -29
- package/dist/loadTernUIScript.js.map +1 -1
- package/dist/loadTernUIScript.mjs +43 -23
- package/dist/loadTernUIScript.mjs.map +1 -1
- package/dist/react/index.d.mts +19 -1
- package/dist/react/index.d.ts +19 -1
- package/dist/react/index.js +26 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +23 -1
- package/dist/react/index.mjs.map +1 -1
- package/dist/resolveVersion.d.mts +2 -1
- package/dist/resolveVersion.d.ts +2 -1
- package/dist/resolveVersion.js +16 -2
- package/dist/resolveVersion.js.map +1 -1
- package/dist/resolveVersion.mjs +3 -1
- package/dist/retry.mjs +3 -56
- package/dist/retry.mjs.map +1 -1
- package/dist/url.d.mts +2 -1
- package/dist/url.d.ts +2 -1
- package/dist/url.js +4 -0
- package/dist/url.js.map +1 -1
- package/dist/url.mjs +3 -0
- package/dist/url.mjs.map +1 -1
- package/package.json +3 -2
- package/dist/chunk-33U3M4YY.mjs.map +0 -1
- package/dist/chunk-PHCVLVZY.mjs +0 -12
- package/dist/chunk-PHCVLVZY.mjs.map +0 -1
package/dist/loadTernUIScript.js
CHANGED
|
@@ -20,12 +20,71 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/loadTernUIScript.ts
|
|
21
21
|
var loadTernUIScript_exports = {};
|
|
22
22
|
__export(loadTernUIScript_exports, {
|
|
23
|
+
applyLoadWithOptions: () => applyLoadWithOptions,
|
|
23
24
|
constructScriptAttributes: () => constructScriptAttributes,
|
|
25
|
+
constructTernUIScriptAttributes: () => constructTernUIScriptAttributes,
|
|
24
26
|
loadTernUIScript: () => loadTernUIScript,
|
|
25
27
|
ternUIgetScriptUrl: () => ternUIgetScriptUrl
|
|
26
28
|
});
|
|
27
29
|
module.exports = __toCommonJS(loadTernUIScript_exports);
|
|
28
30
|
|
|
31
|
+
// src/retry.ts
|
|
32
|
+
var defaultOptions = {
|
|
33
|
+
initialDelay: 125,
|
|
34
|
+
maxDelayBetweenRetries: 0,
|
|
35
|
+
factor: 2,
|
|
36
|
+
shouldRetry: (_, iteration) => iteration < 5,
|
|
37
|
+
retryImmediately: false,
|
|
38
|
+
jitter: true
|
|
39
|
+
};
|
|
40
|
+
var RETRY_IMMEDIATELY_DELAY = 100;
|
|
41
|
+
var sleep = async (ms) => new Promise((s) => setTimeout(s, ms));
|
|
42
|
+
var applyJitter = (delay, jitter) => {
|
|
43
|
+
return jitter ? delay * (1 + Math.random()) : delay;
|
|
44
|
+
};
|
|
45
|
+
var createExponentialDelayAsyncFn = (opts) => {
|
|
46
|
+
let timesCalled = 0;
|
|
47
|
+
const calculateDelayInMs = () => {
|
|
48
|
+
const constant = opts.initialDelay;
|
|
49
|
+
const base = opts.factor;
|
|
50
|
+
let delay = constant * Math.pow(base, timesCalled);
|
|
51
|
+
delay = applyJitter(delay, opts.jitter);
|
|
52
|
+
return Math.min(opts.maxDelayBetweenRetries || delay, delay);
|
|
53
|
+
};
|
|
54
|
+
return async () => {
|
|
55
|
+
await sleep(calculateDelayInMs());
|
|
56
|
+
timesCalled++;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
var retry = async (callback, options = {}) => {
|
|
60
|
+
let iterations = 0;
|
|
61
|
+
const { shouldRetry, initialDelay, maxDelayBetweenRetries, factor, retryImmediately, jitter } = {
|
|
62
|
+
...defaultOptions,
|
|
63
|
+
...options
|
|
64
|
+
};
|
|
65
|
+
const delay = createExponentialDelayAsyncFn({
|
|
66
|
+
initialDelay,
|
|
67
|
+
maxDelayBetweenRetries,
|
|
68
|
+
factor,
|
|
69
|
+
jitter
|
|
70
|
+
});
|
|
71
|
+
while (true) {
|
|
72
|
+
try {
|
|
73
|
+
return await callback();
|
|
74
|
+
} catch (e) {
|
|
75
|
+
iterations++;
|
|
76
|
+
if (!shouldRetry(e, iterations)) {
|
|
77
|
+
throw e;
|
|
78
|
+
}
|
|
79
|
+
if (retryImmediately && iterations === 1) {
|
|
80
|
+
await sleep(applyJitter(RETRY_IMMEDIATELY_DELAY, jitter));
|
|
81
|
+
} else {
|
|
82
|
+
await delay();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
29
88
|
// src/loadScript.ts
|
|
30
89
|
async function loadScript(src = "", options) {
|
|
31
90
|
const { async, defer, crossOrigin, nonce, beforeLoad } = options;
|
|
@@ -53,7 +112,6 @@ async function loadScript(src = "", options) {
|
|
|
53
112
|
};
|
|
54
113
|
const handleLoad = () => {
|
|
55
114
|
if (resolved) return;
|
|
56
|
-
console.log(`[loadScript] Script loaded successfully: ${src}`);
|
|
57
115
|
resolved = true;
|
|
58
116
|
cleanup();
|
|
59
117
|
resolve(script);
|
|
@@ -63,44 +121,60 @@ async function loadScript(src = "", options) {
|
|
|
63
121
|
resolved = true;
|
|
64
122
|
cleanup();
|
|
65
123
|
script.remove();
|
|
66
|
-
console.error(`[loadScript] Failed to load script: ${src}`, error);
|
|
67
124
|
reject(new Error(`Failed to load script: ${src}, Error: ${error.message}`));
|
|
68
125
|
};
|
|
69
|
-
script.addEventListener("load",
|
|
70
|
-
|
|
126
|
+
script.addEventListener("load", () => {
|
|
127
|
+
script.remove();
|
|
128
|
+
resolve(script);
|
|
129
|
+
});
|
|
130
|
+
script.addEventListener("error", () => {
|
|
131
|
+
script.remove();
|
|
132
|
+
reject();
|
|
133
|
+
});
|
|
71
134
|
script.src = src;
|
|
72
135
|
script.nonce = nonce;
|
|
73
136
|
beforeLoad == null ? void 0 : beforeLoad(script);
|
|
74
|
-
console.log(`[loadScript] Appending script to document body: ${src}`);
|
|
75
137
|
document.body.appendChild(script);
|
|
76
138
|
});
|
|
77
139
|
};
|
|
78
|
-
return load();
|
|
140
|
+
return retry(load, { shouldRetry: (_, iterations) => iterations <= 5 });
|
|
79
141
|
}
|
|
80
142
|
|
|
81
143
|
// src/resolveVersion.ts
|
|
82
|
-
var resolveVersion = (ternUIVersion, packageVersion =
|
|
144
|
+
var resolveVersion = (ternUIVersion, packageVersion = "1.1.0-canary.v20251125170702") => {
|
|
83
145
|
if (ternUIVersion) {
|
|
84
146
|
return ternUIVersion;
|
|
85
147
|
}
|
|
86
|
-
|
|
148
|
+
const prereleaseTag = getPrereleaseTag(packageVersion);
|
|
149
|
+
if (prereleaseTag) {
|
|
150
|
+
if (prereleaseTag === "snapshot") {
|
|
151
|
+
return "1.1.0-canary.v20251125170702";
|
|
152
|
+
}
|
|
153
|
+
return prereleaseTag;
|
|
154
|
+
}
|
|
155
|
+
return getMajorVersion(packageVersion);
|
|
156
|
+
};
|
|
157
|
+
var getPrereleaseTag = (packageVersion) => {
|
|
158
|
+
var _a;
|
|
159
|
+
return (_a = packageVersion.trim().replace(/^v/, "").match(/-(.+?)(\.|$)/)) == null ? void 0 : _a[1];
|
|
87
160
|
};
|
|
161
|
+
var getMajorVersion = (packageVersion) => packageVersion.trim().replace(/^v/, "").split(".")[0];
|
|
88
162
|
|
|
89
163
|
// src/loadTernUIScript.ts
|
|
164
|
+
var FAILED_TO_LOAD_ERROR = "TernUI: Failed to load TernSecure";
|
|
90
165
|
var loadTernUIScript = async (options) => {
|
|
91
166
|
const existingScript = document.querySelector("script[data-ternui-script]");
|
|
92
|
-
console.log("[TernSecure-shared] Existing script:", existingScript);
|
|
93
167
|
if (existingScript) {
|
|
94
168
|
return new Promise((resolve, reject) => {
|
|
95
169
|
existingScript.addEventListener("load", () => {
|
|
96
170
|
resolve(existingScript);
|
|
97
171
|
});
|
|
98
172
|
existingScript.addEventListener("error", (error) => {
|
|
99
|
-
reject(
|
|
173
|
+
reject(FAILED_TO_LOAD_ERROR);
|
|
100
174
|
});
|
|
101
175
|
});
|
|
102
176
|
}
|
|
103
|
-
if (!(options == null ? void 0 : options.
|
|
177
|
+
if (!(options == null ? void 0 : options.authDomain)) {
|
|
104
178
|
throw new Error(
|
|
105
179
|
"TernUI script requires a custom domain or proxy URL to be specified in options."
|
|
106
180
|
);
|
|
@@ -108,40 +182,59 @@ var loadTernUIScript = async (options) => {
|
|
|
108
182
|
return loadScript(ternUIgetScriptUrl(options), {
|
|
109
183
|
async: true,
|
|
110
184
|
//crossOrigin: undefined,
|
|
111
|
-
beforeLoad:
|
|
112
|
-
}).catch((
|
|
113
|
-
console.error("[TernSecure] Failed to load TernUI script:", error);
|
|
185
|
+
beforeLoad: applyLoadWithOptions(options)
|
|
186
|
+
}).catch(() => {
|
|
114
187
|
throw new Error("Failed to load TernUI script");
|
|
115
188
|
});
|
|
116
189
|
};
|
|
117
190
|
var ternUIgetScriptUrl = (options) => {
|
|
118
|
-
const {
|
|
191
|
+
const { ternUIUrl, ternUIVersion } = options;
|
|
119
192
|
const version = resolveVersion(ternUIVersion);
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
const localPort = (options == null ? void 0 : options.localPort) || process.env.TERN_UI_PORT || "4000";
|
|
123
|
-
return `http://${localHost}:${localPort}/ternsecure.browser.js`;
|
|
193
|
+
if (ternUIUrl) {
|
|
194
|
+
return ternUIUrl;
|
|
124
195
|
}
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
var beforeLoadWithOptions = (options) => (script) => {
|
|
128
|
-
const attributes = constructScriptAttributes(options);
|
|
129
|
-
Object.entries(attributes).forEach(([key, value]) => {
|
|
130
|
-
if (value) script.setAttribute(key, String(value));
|
|
131
|
-
});
|
|
132
|
-
console.log("[TernSecure-shared] Script attributes set:", attributes);
|
|
196
|
+
const ternsecureCDN = (options == null ? void 0 : options.authDomain) || (options == null ? void 0 : options.proxyUrl) && new URL(options.proxyUrl).host;
|
|
197
|
+
return `${ternsecureCDN}/${version}/ternsecure.browser.js`;
|
|
133
198
|
};
|
|
134
199
|
var constructScriptAttributes = (options) => {
|
|
135
200
|
return {
|
|
136
|
-
"data-domain": (options == null ? void 0 : options.
|
|
201
|
+
"data-auth-domain": (options == null ? void 0 : options.authDomain) || "",
|
|
137
202
|
"data-apikey": (options == null ? void 0 : options.apiKey) || "",
|
|
138
|
-
"data-
|
|
203
|
+
"data-api-url": (options == null ? void 0 : options.apiUrl) || "",
|
|
204
|
+
"data-proxy-url": (options == null ? void 0 : options.proxyUrl) || "",
|
|
139
205
|
...(options == null ? void 0 : options.nonce) ? { nonce: options.nonce } : {}
|
|
140
206
|
};
|
|
141
207
|
};
|
|
208
|
+
var constructTernUIScriptAttributes = (options) => {
|
|
209
|
+
const obj = {};
|
|
210
|
+
if (options.authDomain) {
|
|
211
|
+
obj["data-auth-domain"] = options.authDomain;
|
|
212
|
+
}
|
|
213
|
+
if (options.apiKey) {
|
|
214
|
+
obj["data-apikey"] = options.apiKey;
|
|
215
|
+
}
|
|
216
|
+
if (options.apiUrl) {
|
|
217
|
+
obj["data-api-url"] = options.apiUrl;
|
|
218
|
+
}
|
|
219
|
+
if (options.proxyUrl) {
|
|
220
|
+
obj["data-proxy-url"] = options.proxyUrl;
|
|
221
|
+
}
|
|
222
|
+
if (options.nonce) {
|
|
223
|
+
obj.nonce = options.nonce;
|
|
224
|
+
}
|
|
225
|
+
return obj;
|
|
226
|
+
};
|
|
227
|
+
var applyLoadWithOptions = (options) => (script) => {
|
|
228
|
+
const attributes = constructTernUIScriptAttributes(options);
|
|
229
|
+
for (const attribute in attributes) {
|
|
230
|
+
script.setAttribute(attribute, attributes[attribute]);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
142
233
|
// Annotate the CommonJS export names for ESM import in node:
|
|
143
234
|
0 && (module.exports = {
|
|
235
|
+
applyLoadWithOptions,
|
|
144
236
|
constructScriptAttributes,
|
|
237
|
+
constructTernUIScriptAttributes,
|
|
145
238
|
loadTernUIScript,
|
|
146
239
|
ternUIgetScriptUrl
|
|
147
240
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/loadTernUIScript.ts","../src/loadScript.ts","../src/resolveVersion.ts"],"sourcesContent":["import type { TernSecureInstanceTreeOptions, TernSecureSDK } from '@tern-secure/types';\n\nimport { loadScript } from './loadScript';\nimport { resolveVersion } from './resolveVersion';\n\nexport type LoadTernUISCriptOptions = TernSecureInstanceTreeOptions & {\n apiKey?: string;\n customDomain?: string;\n proxyUrl?: string;\n ternUIVersion?: string;\n sdkMetadata?: TernSecureSDK;\n scriptHost?: string;\n localPort?: string;\n nonce?: string;\n};\n\nexport const loadTernUIScript = async (options?: LoadTernUISCriptOptions) => {\n const existingScript = document.querySelector<HTMLScriptElement>('script[data-ternui-script]');\n console.log('[TernSecure-shared] Existing script:', existingScript);\n\n if (existingScript) {\n return new Promise((resolve, reject) => {\n existingScript.addEventListener('load', () => {\n resolve(existingScript);\n });\n\n existingScript.addEventListener('error', error => {\n reject(error);\n });\n });\n }\n\n if (!options?.customDomain) {\n throw new Error(\n 'TernUI script requires a custom domain or proxy URL to be specified in options.',\n );\n }\n\n return loadScript(ternUIgetScriptUrl(options), {\n async: true,\n //crossOrigin: undefined,\n beforeLoad: beforeLoadWithOptions(options),\n }).catch(error => {\n console.error('[TernSecure] Failed to load TernUI script:', error);\n throw new Error('Failed to load TernUI script');\n });\n};\n\nexport const ternUIgetScriptUrl = (options: LoadTernUISCriptOptions) => {\n const { ternUIVersion, isTernSecureDev } = options;\n const version = resolveVersion(ternUIVersion);\n\n if (isTernSecureDev) {\n const localHost = process.env.TERN_UI_HOST || 'localhost';\n const localPort = options?.localPort || process.env.TERN_UI_PORT || '4000';\n return `http://${localHost}:${localPort}/ternsecure.browser.js`;\n //return `http://cdn.lifesprintcare.ca/dist/ternsecure.browser.js`\n }\n //return `https://cdn.lifesprintcare.ca/dist/ternsecure.browser.js`\n return `https://cdn.jsdelivr.net/npm/@tern-secure/ui@${version}/dist/ternsecure.browser.js`;\n\n //const ternsecureCDN = options?.customDomain ||\n //(options?.proxyUrl && new URL(options.proxyUrl).host) || 'cdn.tern-secure.com';\n //return `${ternsecureCDN}/ternsecure.browser.js`;\n //return `https://${ternsecureCDN}/npm/@ternsecure/tern-ui@${version}/dist/ternsecure.browser.js`;\n};\n\nconst beforeLoadWithOptions =\n (options?: LoadTernUISCriptOptions) => (script: HTMLScriptElement) => {\n const attributes = constructScriptAttributes(options);\n Object.entries(attributes).forEach(([key, value]) => {\n if (value) script.setAttribute(key, String(value));\n });\n console.log('[TernSecure-shared] Script attributes set:', attributes);\n };\n\nexport const constructScriptAttributes = (options?: LoadTernUISCriptOptions) => {\n return {\n 'data-domain': options?.customDomain || '',\n 'data-apikey': options?.apiKey || '',\n 'data-proxyUrl': options?.proxyUrl || '',\n ...(options?.nonce ? { nonce: options.nonce } : {}),\n };\n};\n","type LoadScriptOptions = {\n async?: boolean;\n defer?: boolean;\n crossOrigin?: 'anonymous' | 'use-credentials';\n nonce?: string;\n beforeLoad?: (script: HTMLScriptElement) => void;\n };\n\n export async function loadScript(src ='', options: LoadScriptOptions): Promise<HTMLScriptElement> {\n const { async, defer, crossOrigin, nonce, beforeLoad } = options;\n\n const load = () => {\n return new Promise<HTMLScriptElement>((resolve, reject) => {\n if (!src) {\n reject(new Error('Script src is required'));\n }\n\n if (!document || !document.body) {\n reject(new Error('Document body is not available'));\n }\n\n const script = document.createElement('script');\n\n if (crossOrigin) script.setAttribute('crossorigin', crossOrigin);\n script.async = async || false;\n script.defer = defer || false;\n\n let resolved = false;\n let timeoutId: NodeJS.Timeout | null = null;\n\n const cleanup = () => {\n script.removeEventListener('load', handleLoad);\n script.removeEventListener('error', handleError);\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = null;\n }\n };\n \n const handleLoad = () => {\n if (resolved) return;\n console.log(`[loadScript] Script loaded successfully: ${src}`);\n\n resolved = true;\n cleanup();\n resolve(script);\n };\n \n const handleError = (error: ErrorEvent) => {\n if (resolved) return;\n resolved = true;\n cleanup();\n script.remove();\n console.error(`[loadScript] Failed to load script: ${src}`, error);\n reject(new Error(`Failed to load script: ${src}, Error: ${error.message}`));\n };\n \n script.addEventListener('load', handleLoad);\n script.addEventListener('error', handleError);\n\n script.src = src;\n script.nonce = nonce;\n beforeLoad?.(script);\n\n console.log(`[loadScript] Appending script to document body: ${src}`);\n document.body.appendChild(script)\n });\n };\n\n return load()\n\n //return retry(load, { shouldRetry: (_, iterations) => iterations <=5 });\n }","\nexport const resolveVersion = (ternUIVersion: string | undefined, packageVersion = TERN_UI_VERSION) => {\n if (ternUIVersion) {\n return ternUIVersion\n }\n\n return packageVersion;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACQE,eAAsB,WAAW,MAAK,IAAI,SAAwD;AAChG,QAAM,EAAE,OAAO,OAAO,aAAa,OAAO,WAAW,IAAI;AAEzD,QAAM,OAAO,MAAM;AACjB,WAAO,IAAI,QAA2B,CAAC,SAAS,WAAW;AACzD,UAAI,CAAC,KAAK;AACR,eAAO,IAAI,MAAM,wBAAwB,CAAC;AAAA,MAC5C;AAEA,UAAI,CAAC,YAAY,CAAC,SAAS,MAAM;AAC/B,eAAO,IAAI,MAAM,gCAAgC,CAAC;AAAA,MACpD;AAEA,YAAM,SAAS,SAAS,cAAc,QAAQ;AAE9C,UAAI,YAAa,QAAO,aA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TernUIScript.ts","../src/retry.ts","../src/loadScript.ts","../src/resolveVersion.ts"],"sourcesContent":["import type { TernSecureAuthOptions, TernSecureSDK } from '@tern-secure/types';\n\nimport { loadScript } from './loadScript';\nimport { resolveVersion } from './resolveVersion';\n\nconst FAILED_TO_LOAD_ERROR = 'TernUI: Failed to load TernSecure';\n\nexport type LoadTernUISCriptOptions = TernSecureAuthOptions & {\n apiKey?: string;\n apiUrl?: string;\n authDomain?: string;\n proxyUrl?: string;\n ternUIUrl?: string;\n ternUIVersion?: string;\n sdkMetadata?: TernSecureSDK;\n scriptHost?: string;\n localPort?: string;\n nonce?: string;\n};\n\nexport const loadTernUIScript = async (options?: LoadTernUISCriptOptions) => {\n const existingScript = document.querySelector<HTMLScriptElement>('script[data-ternui-script]');\n\n if (existingScript) {\n return new Promise((resolve, reject) => {\n existingScript.addEventListener('load', () => {\n resolve(existingScript);\n });\n\n existingScript.addEventListener('error', error => {\n reject(FAILED_TO_LOAD_ERROR);\n });\n });\n }\n\n if (!options?.authDomain) {\n throw new Error(\n 'TernUI script requires a custom domain or proxy URL to be specified in options.',\n );\n }\n\n return loadScript(ternUIgetScriptUrl(options), {\n async: true,\n //crossOrigin: undefined,\n beforeLoad: applyLoadWithOptions(options),\n }).catch(() => {\n throw new Error('Failed to load TernUI script');\n });\n};\n\nexport const ternUIgetScriptUrl = (options: LoadTernUISCriptOptions) => {\n const { ternUIUrl, ternUIVersion } = options;\n const version = resolveVersion(ternUIVersion);\n\n if (ternUIUrl) {\n return ternUIUrl;\n }\n\n //return `https://cdn.lifesprintcare.ca/dist/ternsecure.browser.js`\n //return `https://cdn.jsdelivr.net/npm/@tern-secure/ui@${version}/dist/ternsecure.browser.js`;\n\n const ternsecureCDN = options?.authDomain ||\n (options?.proxyUrl && new URL(options.proxyUrl).host);\n return `${ternsecureCDN}/${version}/ternsecure.browser.js`;\n //return `https://${ternsecureCDN}/npm/@ternsecure/tern-ui@${version}/dist/ternsecure.browser.js`;\n};\n\n/**\n * @deprecated Use applyLoadWithOptions instead\n */\nconst beforeLoadWithOptions =\n (options?: LoadTernUISCriptOptions) => (script: HTMLScriptElement) => {\n const attributes = constructScriptAttributes(options);\n Object.entries(attributes).forEach(([key, value]) => {\n if (value) script.setAttribute(key, String(value));\n });\n console.log('[TernSecure-shared] Script attributes set:', attributes);\n };\n\n/**\n * @deprecated Use constructTernUIScriptAttributes instead\n */\nexport const constructScriptAttributes = (options?: LoadTernUISCriptOptions) => {\n return {\n 'data-auth-domain': options?.authDomain || '',\n 'data-apikey': options?.apiKey || '',\n 'data-api-url': options?.apiUrl || '',\n 'data-proxy-url': options?.proxyUrl || '',\n ...(options?.nonce ? { nonce: options.nonce } : {}),\n };\n};\n\n\nconst constructTernUIScriptAttributes = (options: LoadTernUISCriptOptions) => {\n const obj: Record<string, string> = {};\n\n if (options.authDomain) {\n obj['data-auth-domain'] = options.authDomain;\n }\n if (options.apiKey) {\n obj['data-apikey'] = options.apiKey;\n }\n if (options.apiUrl) {\n obj['data-api-url'] = options.apiUrl;\n }\n if (options.proxyUrl) {\n obj['data-proxy-url'] = options.proxyUrl;\n }\n\n if (options.nonce) {\n obj.nonce = options.nonce;\n }\n\n return obj;\n}\n\nconst applyLoadWithOptions =\n (options: LoadTernUISCriptOptions) => (script: HTMLScriptElement) => {\n const attributes = constructTernUIScriptAttributes(options);\n for (const attribute in attributes) {\n script.setAttribute(attribute, attributes[attribute]);\n }\n };\n\n\nexport { constructTernUIScriptAttributes, applyLoadWithOptions };\n\n","type Milliseconds = number;\n\ntype RetryOptions = Partial<{\n /**\n * The initial delay before the first retry.\n * @default 125\n */\n initialDelay: Milliseconds;\n /**\n * The maximum delay between retries.\n * The delay between retries will never exceed this value.\n * If set to 0, the delay will increase indefinitely.\n * @default 0\n */\n maxDelayBetweenRetries: Milliseconds;\n /**\n * The multiplier for the exponential backoff.\n * @default 2\n */\n factor: number;\n /**\n * A function to determine if the operation should be retried.\n * The callback accepts the error that was thrown and the number of iterations.\n * The iterations variable references the number of retries AFTER attempt\n * that caused the error and starts at 1 (as in, this is the 1st, 2nd, nth retry).\n * @default (error, iterations) => iterations < 5\n */\n shouldRetry: (error: unknown, iterations: number) => boolean;\n /**\n * Controls whether the helper should retry the operation immediately once before applying exponential backoff.\n * The delay for the immediate retry is 100ms.\n * @default false\n */\n retryImmediately: boolean;\n /**\n * If true, the intervals will be multiplied by a factor in the range of [1,2].\n * @default true\n */\n jitter: boolean;\n}>;\n\nconst defaultOptions: Required<RetryOptions> = {\n initialDelay: 125,\n maxDelayBetweenRetries: 0,\n factor: 2,\n shouldRetry: (_: unknown, iteration: number) => iteration < 5,\n retryImmediately: false,\n jitter: true,\n};\n\nconst RETRY_IMMEDIATELY_DELAY = 100;\n\nconst sleep = async (ms: Milliseconds) => new Promise(s => setTimeout(s, ms));\n\nconst applyJitter = (delay: Milliseconds, jitter: boolean) => {\n return jitter ? delay * (1 + Math.random()) : delay;\n};\n\nconst createExponentialDelayAsyncFn = (\n opts: Required<Pick<RetryOptions, 'initialDelay' | 'maxDelayBetweenRetries' | 'factor' | 'jitter'>>,\n) => {\n let timesCalled = 0;\n\n const calculateDelayInMs = () => {\n const constant = opts.initialDelay;\n const base = opts.factor;\n let delay = constant * Math.pow(base, timesCalled);\n delay = applyJitter(delay, opts.jitter);\n return Math.min(opts.maxDelayBetweenRetries || delay, delay);\n };\n\n return async (): Promise<void> => {\n await sleep(calculateDelayInMs());\n timesCalled++;\n };\n};\n\n/**\n * Retries a callback until it succeeds or the shouldRetry function returns false.\n * See {@link RetryOptions} for the available options.\n */\nexport const retry = async <T>(callback: () => T | Promise<T>, options: RetryOptions = {}): Promise<T> => {\n let iterations = 0;\n const { shouldRetry, initialDelay, maxDelayBetweenRetries, factor, retryImmediately, jitter } = {\n ...defaultOptions,\n ...options,\n };\n\n const delay = createExponentialDelayAsyncFn({\n initialDelay,\n maxDelayBetweenRetries,\n factor,\n jitter,\n });\n\n while (true) {\n try {\n return await callback();\n } catch (e) {\n iterations++;\n if (!shouldRetry(e, iterations)) {\n throw e;\n }\n if (retryImmediately && iterations === 1) {\n await sleep(applyJitter(RETRY_IMMEDIATELY_DELAY, jitter));\n } else {\n await delay();\n }\n }\n }\n};\n","import { retry } from './retry';\n\ntype LoadScriptOptions = {\n async?: boolean;\n defer?: boolean;\n crossOrigin?: 'anonymous' | 'use-credentials';\n nonce?: string;\n beforeLoad?: (script: HTMLScriptElement) => void;\n};\n\nexport async function loadScript(src = '', options: LoadScriptOptions): Promise<HTMLScriptElement> {\n const { async, defer, crossOrigin, nonce, beforeLoad } = options;\n\n const load = () => {\n return new Promise<HTMLScriptElement>((resolve, reject) => {\n if (!src) {\n reject(new Error('Script src is required'));\n }\n\n if (!document || !document.body) {\n reject(new Error('Document body is not available'));\n }\n\n const script = document.createElement('script');\n\n if (crossOrigin) script.setAttribute('crossorigin', crossOrigin);\n script.async = async || false;\n script.defer = defer || false;\n\n let resolved = false;\n let timeoutId: NodeJS.Timeout | null = null;\n\n /**\n * @deprecated\n */\n const cleanup = () => {\n script.removeEventListener('load', handleLoad);\n script.removeEventListener('error', handleError);\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = null;\n }\n };\n\n /**\n * @deprecated\n */\n const handleLoad = () => {\n if (resolved) return;\n\n resolved = true;\n cleanup();\n resolve(script);\n };\n /**\n * @deprecated\n */\n const handleError = (error: ErrorEvent) => {\n if (resolved) return;\n resolved = true;\n cleanup();\n script.remove();\n reject(new Error(`Failed to load script: ${src}, Error: ${error.message}`));\n };\n\n script.addEventListener('load', () => {\n script.remove()\n resolve(script);\n });\n\n script.addEventListener('error', () => {\n script.remove()\n reject()\n });\n\n script.src = src;\n script.nonce = nonce;\n beforeLoad?.(script);\n document.body.appendChild(script)\n });\n };\n\n //return load()\n\n return retry(load, { shouldRetry: (_, iterations) => iterations <= 5 });\n}","\nexport const resolveVersion = (ternUIVersion: string | undefined, packageVersion = TERN_UI_VERSION) => {\n if (ternUIVersion) {\n return ternUIVersion\n }\n\n const prereleaseTag = getPrereleaseTag(packageVersion);\n if (prereleaseTag) {\n if (prereleaseTag === 'snapshot') {\n return TERN_UI_VERSION;\n }\n\n return prereleaseTag;\n }\n\n return getMajorVersion(packageVersion);\n}\n\nconst getPrereleaseTag = (packageVersion: string) =>\n packageVersion\n .trim()\n .replace(/^v/, '')\n .match(/-(.+?)(\\.|$)/)?.[1];\n\nexport const getMajorVersion = (packageVersion: string) => packageVersion.trim().replace(/^v/, '').split('.')[0];"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACyCA,IAAM,iBAAyC;AAAA,EAC7C,cAAc;AAAA,EACd,wBAAwB;AAAA,EACxB,QAAQ;AAAA,EACR,aAAa,CAAC,GAAY,cAAsB,YAAY;AAAA,EAC5D,kBAAkB;AAAA,EAClB,QAAQ;AACV;AAEA,IAAM,0BAA0B;AAEhC,IAAM,QAAQ,OAAO,OAAqB,IAAI,QAAQ,OAAK,WAAW,GAAG,EAAE,CAAC;AAE5E,IAAM,cAAc,CAAC,OAAqB,WAAoB;AAC5D,SAAO,SAAS,SAAS,IAAI,KAAK,OAAO,KAAK;AAChD;AAEA,IAAM,gCAAgC,CACpC,SACG;AACH,MAAI,cAAc;AAElB,QAAM,qBAAqB,MAAM;AAC/B,UAAM,WAAW,KAAK;AACtB,UAAM,OAAO,KAAK;AAClB,QAAI,QAAQ,WAAW,KAAK,IAAI,MAAM,WAAW;AACjD,YAAQ,YAAY,OAAO,KAAK,MAAM;AACtC,WAAO,KAAK,IAAI,KAAK,0BAA0B,OAAO,KAAK;AAAA,EAC7D;AAEA,SAAO,YAA2B;AAChC,UAAM,MAAM,mBAAmB,CAAC;AAChC;AAAA,EACF;AACF;AAMO,IAAM,QAAQ,OAAU,UAAgC,UAAwB,CAAC,MAAkB;AACxG,MAAI,aAAa;AACjB,QAAM,EAAE,aAAa,cAAc,wBAAwB,QAAQ,kBAAkB,OAAO,IAAI;AAAA,IAC9F,GAA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jB,QAAI,QAAQ,QAAQ;AAAA,EACtB;AAEA,SAAO;AACT;AAEA,IAAM,uBACJ,CAAC,YAAqC,CAAC,WAA8B;AACnE,QAAM,aAAa,gCAAgC,OAAO;AAC1D,aAAW,aAAa,YAAY;AAClC,WAAO,aAAa,WAAW,WAAW,SAAS,CAAC;AAAA,EACtD;AACF;","names":[]}
|
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
import {
|
|
2
2
|
resolveVersion
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-B2SN66XE.mjs";
|
|
4
4
|
import {
|
|
5
5
|
loadScript
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-IGYBIQYE.mjs";
|
|
7
|
+
import "./chunk-N2V3PKFE.mjs";
|
|
7
8
|
|
|
8
9
|
// src/loadTernUIScript.ts
|
|
10
|
+
var FAILED_TO_LOAD_ERROR = "TernUI: Failed to load TernSecure";
|
|
9
11
|
var loadTernUIScript = async (options) => {
|
|
10
12
|
const existingScript = document.querySelector("script[data-ternui-script]");
|
|
11
|
-
console.log("[TernSecure-shared] Existing script:", existingScript);
|
|
12
13
|
if (existingScript) {
|
|
13
14
|
return new Promise((resolve, reject) => {
|
|
14
15
|
existingScript.addEventListener("load", () => {
|
|
15
16
|
resolve(existingScript);
|
|
16
17
|
});
|
|
17
18
|
existingScript.addEventListener("error", (error) => {
|
|
18
|
-
reject(
|
|
19
|
+
reject(FAILED_TO_LOAD_ERROR);
|
|
19
20
|
});
|
|
20
21
|
});
|
|
21
22
|
}
|
|
22
|
-
if (!(options == null ? void 0 : options.
|
|
23
|
+
if (!(options == null ? void 0 : options.authDomain)) {
|
|
23
24
|
throw new Error(
|
|
24
25
|
"TernUI script requires a custom domain or proxy URL to be specified in options."
|
|
25
26
|
);
|
|
@@ -27,39 +28,58 @@ var loadTernUIScript = async (options) => {
|
|
|
27
28
|
return loadScript(ternUIgetScriptUrl(options), {
|
|
28
29
|
async: true,
|
|
29
30
|
//crossOrigin: undefined,
|
|
30
|
-
beforeLoad:
|
|
31
|
-
}).catch((
|
|
32
|
-
console.error("[TernSecure] Failed to load TernUI script:", error);
|
|
31
|
+
beforeLoad: applyLoadWithOptions(options)
|
|
32
|
+
}).catch(() => {
|
|
33
33
|
throw new Error("Failed to load TernUI script");
|
|
34
34
|
});
|
|
35
35
|
};
|
|
36
36
|
var ternUIgetScriptUrl = (options) => {
|
|
37
|
-
const {
|
|
37
|
+
const { ternUIUrl, ternUIVersion } = options;
|
|
38
38
|
const version = resolveVersion(ternUIVersion);
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
const localPort = (options == null ? void 0 : options.localPort) || process.env.TERN_UI_PORT || "4000";
|
|
42
|
-
return `http://${localHost}:${localPort}/ternsecure.browser.js`;
|
|
39
|
+
if (ternUIUrl) {
|
|
40
|
+
return ternUIUrl;
|
|
43
41
|
}
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
var beforeLoadWithOptions = (options) => (script) => {
|
|
47
|
-
const attributes = constructScriptAttributes(options);
|
|
48
|
-
Object.entries(attributes).forEach(([key, value]) => {
|
|
49
|
-
if (value) script.setAttribute(key, String(value));
|
|
50
|
-
});
|
|
51
|
-
console.log("[TernSecure-shared] Script attributes set:", attributes);
|
|
42
|
+
const ternsecureCDN = (options == null ? void 0 : options.authDomain) || (options == null ? void 0 : options.proxyUrl) && new URL(options.proxyUrl).host;
|
|
43
|
+
return `${ternsecureCDN}/${version}/ternsecure.browser.js`;
|
|
52
44
|
};
|
|
53
45
|
var constructScriptAttributes = (options) => {
|
|
54
46
|
return {
|
|
55
|
-
"data-domain": (options == null ? void 0 : options.
|
|
47
|
+
"data-auth-domain": (options == null ? void 0 : options.authDomain) || "",
|
|
56
48
|
"data-apikey": (options == null ? void 0 : options.apiKey) || "",
|
|
57
|
-
"data-
|
|
49
|
+
"data-api-url": (options == null ? void 0 : options.apiUrl) || "",
|
|
50
|
+
"data-proxy-url": (options == null ? void 0 : options.proxyUrl) || "",
|
|
58
51
|
...(options == null ? void 0 : options.nonce) ? { nonce: options.nonce } : {}
|
|
59
52
|
};
|
|
60
53
|
};
|
|
54
|
+
var constructTernUIScriptAttributes = (options) => {
|
|
55
|
+
const obj = {};
|
|
56
|
+
if (options.authDomain) {
|
|
57
|
+
obj["data-auth-domain"] = options.authDomain;
|
|
58
|
+
}
|
|
59
|
+
if (options.apiKey) {
|
|
60
|
+
obj["data-apikey"] = options.apiKey;
|
|
61
|
+
}
|
|
62
|
+
if (options.apiUrl) {
|
|
63
|
+
obj["data-api-url"] = options.apiUrl;
|
|
64
|
+
}
|
|
65
|
+
if (options.proxyUrl) {
|
|
66
|
+
obj["data-proxy-url"] = options.proxyUrl;
|
|
67
|
+
}
|
|
68
|
+
if (options.nonce) {
|
|
69
|
+
obj.nonce = options.nonce;
|
|
70
|
+
}
|
|
71
|
+
return obj;
|
|
72
|
+
};
|
|
73
|
+
var applyLoadWithOptions = (options) => (script) => {
|
|
74
|
+
const attributes = constructTernUIScriptAttributes(options);
|
|
75
|
+
for (const attribute in attributes) {
|
|
76
|
+
script.setAttribute(attribute, attributes[attribute]);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
61
79
|
export {
|
|
80
|
+
applyLoadWithOptions,
|
|
62
81
|
constructScriptAttributes,
|
|
82
|
+
constructTernUIScriptAttributes,
|
|
63
83
|
loadTernUIScript,
|
|
64
84
|
ternUIgetScriptUrl
|
|
65
85
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/loadTernUIScript.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../src/loadTernUIScript.ts"],"sourcesContent":["import type { TernSecureAuthOptions, TernSecureSDK } from '@tern-secure/types';\n\nimport { loadScript } from './loadScript';\nimport { resolveVersion } from './resolveVersion';\n\nconst FAILED_TO_LOAD_ERROR = 'TernUI: Failed to load TernSecure';\n\nexport type LoadTernUISCriptOptions = TernSecureAuthOptions & {\n apiKey?: string;\n apiUrl?: string;\n authDomain?: string;\n proxyUrl?: string;\n ternUIUrl?: string;\n ternUIVersion?: string;\n sdkMetadata?: TernSecureSDK;\n scriptHost?: string;\n localPort?: string;\n nonce?: string;\n};\n\nexport const loadTernUIScript = async (options?: LoadTernUISCriptOptions) => {\n const existingScript = document.querySelector<HTMLScriptElement>('script[data-ternui-script]');\n\n if (existingScript) {\n return new Promise((resolve, reject) => {\n existingScript.addEventListener('load', () => {\n resolve(existingScript);\n });\n\n existingScript.addEventListener('error', error => {\n reject(FAILED_TO_LOAD_ERROR);\n });\n });\n }\n\n if (!options?.authDomain) {\n throw new Error(\n 'TernUI script requires a custom domain or proxy URL to be specified in options.',\n );\n }\n\n return loadScript(ternUIgetScriptUrl(options), {\n async: true,\n //crossOrigin: undefined,\n beforeLoad: applyLoadWithOptions(options),\n }).catch(() => {\n throw new Error('Failed to load TernUI script');\n });\n};\n\nexport const ternUIgetScriptUrl = (options: LoadTernUISCriptOptions) => {\n const { ternUIUrl, ternUIVersion } = options;\n const version = resolveVersion(ternUIVersion);\n\n if (ternUIUrl) {\n return ternUIUrl;\n }\n\n //return `https://cdn.lifesprintcare.ca/dist/ternsecure.browser.js`\n //return `https://cdn.jsdelivr.net/npm/@tern-secure/ui@${version}/dist/ternsecure.browser.js`;\n\n const ternsecureCDN = options?.authDomain ||\n (options?.proxyUrl && new URL(options.proxyUrl).host);\n return `${ternsecureCDN}/${version}/ternsecure.browser.js`;\n //return `https://${ternsecureCDN}/npm/@ternsecure/tern-ui@${version}/dist/ternsecure.browser.js`;\n};\n\n/**\n * @deprecated Use applyLoadWithOptions instead\n */\nconst beforeLoadWithOptions =\n (options?: LoadTernUISCriptOptions) => (script: HTMLScriptElement) => {\n const attributes = constructScriptAttributes(options);\n Object.entries(attributes).forEach(([key, value]) => {\n if (value) script.setAttribute(key, String(value));\n });\n console.log('[TernSecure-shared] Script attributes set:', attributes);\n };\n\n/**\n * @deprecated Use constructTernUIScriptAttributes instead\n */\nexport const constructScriptAttributes = (options?: LoadTernUISCriptOptions) => {\n return {\n 'data-auth-domain': options?.authDomain || '',\n 'data-apikey': options?.apiKey || '',\n 'data-api-url': options?.apiUrl || '',\n 'data-proxy-url': options?.proxyUrl || '',\n ...(options?.nonce ? { nonce: options.nonce } : {}),\n };\n};\n\n\nconst constructTernUIScriptAttributes = (options: LoadTernUISCriptOptions) => {\n const obj: Record<string, string> = {};\n\n if (options.authDomain) {\n obj['data-auth-domain'] = options.authDomain;\n }\n if (options.apiKey) {\n obj['data-apikey'] = options.apiKey;\n }\n if (options.apiUrl) {\n obj['data-api-url'] = options.apiUrl;\n }\n if (options.proxyUrl) {\n obj['data-proxy-url'] = options.proxyUrl;\n }\n\n if (options.nonce) {\n obj.nonce = options.nonce;\n }\n\n return obj;\n}\n\nconst applyLoadWithOptions =\n (options: LoadTernUISCriptOptions) => (script: HTMLScriptElement) => {\n const attributes = constructTernUIScriptAttributes(options);\n for (const attribute in attributes) {\n script.setAttribute(attribute, attributes[attribute]);\n }\n };\n\n\nexport { constructTernUIScriptAttributes, applyLoadWithOptions };\n\n"],"mappings":";;;;;;;;;AAKA,IAAM,uBAAuB;AAetB,IAAM,mBAAmB,OAAO,YAAsC;AAC3E,QAAM,iBAAiB,SAAS,cAAiC,4BAA4B;AAE7F,MAAI,gBAAgB;AAClB,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,qBAAe,iBAAiB,QAAQ,MAAM;AAC5C,gBAAQ,cAAc;AAAA,MACxB,CAAC;AAED,qBAAe,iBAAiB,SAAS,WAAS;AAChD,eAAO,oBAAoB;AAAA,MAC7B,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAEA,MAAI,EAAC,mCAAS,aAAY;AACxB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,SAAO,WAAW,mBAAmB,OAAO,GAAG;AAAA,IAC7C,OAAO;AAAA;AAAA,IAEP,YAAY,qBAAqB,OAAO;AAAA,EAC1C,CAAC,EAAE,MAAM,MAAM;AACb,UAAM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import * as _tern_secure_types from '@tern-secure/types';
|
|
2
2
|
import { TernSecureAuth, TernSecureInstanceTree, TernSecureAuthProvider, TernSecureState } from '@tern-secure/types';
|
|
3
|
+
import { dequal } from 'dequal';
|
|
3
4
|
import * as react from 'react';
|
|
4
5
|
import react__default from 'react';
|
|
5
6
|
|
|
@@ -8,6 +9,23 @@ import react__default from 'react';
|
|
|
8
9
|
*/
|
|
9
10
|
declare const useTernSecure: () => TernSecureAuth;
|
|
10
11
|
|
|
12
|
+
type UseMemoFactory<T> = () => T;
|
|
13
|
+
type UseMemoDependencyArray = Exclude<Parameters<typeof react__default.useMemo>[1], 'undefined'>;
|
|
14
|
+
type UseDeepEqualMemo = <T>(factory: UseMemoFactory<T>, dependencyArray: UseMemoDependencyArray) => T;
|
|
15
|
+
/**
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
declare const useDeepEqualMemo: UseDeepEqualMemo;
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
declare const isDeeplyEqual: typeof dequal;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
declare const useSafeLayoutEffect: typeof react__default.useLayoutEffect;
|
|
28
|
+
|
|
11
29
|
type TernSecureAuthContextType = {
|
|
12
30
|
authProvider: TernSecureAuthProvider | null | undefined;
|
|
13
31
|
authState: TernSecureState;
|
|
@@ -60,4 +78,4 @@ declare const TernSecureAuthCtx: react.Context<{
|
|
|
60
78
|
declare const useTernSecureAuthCtx: () => TernSecureAuth;
|
|
61
79
|
declare function useAssertWrappedByTernSecureAuthProvider(displayNameOrFn: string | (() => void)): void;
|
|
62
80
|
|
|
63
|
-
export { SessionContext, TernSecureAuthContext, TernSecureAuthCtx, TernSecureInstanceContext, UserContext, assertContextExists, createContextAndHook, useAssertWrappedByTernSecureAuthProvider, useAssertWrappedByTernSecureProvider, useSessionContext, useTernSecure, useTernSecureAuthContext, useTernSecureAuthCtx, useTernSecureInstanceContext, useUserContext };
|
|
81
|
+
export { SessionContext, TernSecureAuthContext, TernSecureAuthCtx, TernSecureInstanceContext, UserContext, assertContextExists, createContextAndHook, isDeeplyEqual, useAssertWrappedByTernSecureAuthProvider, useAssertWrappedByTernSecureProvider, useDeepEqualMemo, useSafeLayoutEffect, useSessionContext, useTernSecure, useTernSecureAuthContext, useTernSecureAuthCtx, useTernSecureInstanceContext, useUserContext };
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as _tern_secure_types from '@tern-secure/types';
|
|
2
2
|
import { TernSecureAuth, TernSecureInstanceTree, TernSecureAuthProvider, TernSecureState } from '@tern-secure/types';
|
|
3
|
+
import { dequal } from 'dequal';
|
|
3
4
|
import * as react from 'react';
|
|
4
5
|
import react__default from 'react';
|
|
5
6
|
|
|
@@ -8,6 +9,23 @@ import react__default from 'react';
|
|
|
8
9
|
*/
|
|
9
10
|
declare const useTernSecure: () => TernSecureAuth;
|
|
10
11
|
|
|
12
|
+
type UseMemoFactory<T> = () => T;
|
|
13
|
+
type UseMemoDependencyArray = Exclude<Parameters<typeof react__default.useMemo>[1], 'undefined'>;
|
|
14
|
+
type UseDeepEqualMemo = <T>(factory: UseMemoFactory<T>, dependencyArray: UseMemoDependencyArray) => T;
|
|
15
|
+
/**
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
declare const useDeepEqualMemo: UseDeepEqualMemo;
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
declare const isDeeplyEqual: typeof dequal;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
declare const useSafeLayoutEffect: typeof react__default.useLayoutEffect;
|
|
28
|
+
|
|
11
29
|
type TernSecureAuthContextType = {
|
|
12
30
|
authProvider: TernSecureAuthProvider | null | undefined;
|
|
13
31
|
authState: TernSecureState;
|
|
@@ -60,4 +78,4 @@ declare const TernSecureAuthCtx: react.Context<{
|
|
|
60
78
|
declare const useTernSecureAuthCtx: () => TernSecureAuth;
|
|
61
79
|
declare function useAssertWrappedByTernSecureAuthProvider(displayNameOrFn: string | (() => void)): void;
|
|
62
80
|
|
|
63
|
-
export { SessionContext, TernSecureAuthContext, TernSecureAuthCtx, TernSecureInstanceContext, UserContext, assertContextExists, createContextAndHook, useAssertWrappedByTernSecureAuthProvider, useAssertWrappedByTernSecureProvider, useSessionContext, useTernSecure, useTernSecureAuthContext, useTernSecureAuthCtx, useTernSecureInstanceContext, useUserContext };
|
|
81
|
+
export { SessionContext, TernSecureAuthContext, TernSecureAuthCtx, TernSecureInstanceContext, UserContext, assertContextExists, createContextAndHook, isDeeplyEqual, useAssertWrappedByTernSecureAuthProvider, useAssertWrappedByTernSecureProvider, useDeepEqualMemo, useSafeLayoutEffect, useSessionContext, useTernSecure, useTernSecureAuthContext, useTernSecureAuthCtx, useTernSecureInstanceContext, useUserContext };
|
package/dist/react/index.js
CHANGED
|
@@ -37,8 +37,11 @@ __export(react_exports, {
|
|
|
37
37
|
UserContext: () => UserContext,
|
|
38
38
|
assertContextExists: () => assertContextExists,
|
|
39
39
|
createContextAndHook: () => createContextAndHook,
|
|
40
|
+
isDeeplyEqual: () => isDeeplyEqual,
|
|
40
41
|
useAssertWrappedByTernSecureAuthProvider: () => useAssertWrappedByTernSecureAuthProvider,
|
|
41
42
|
useAssertWrappedByTernSecureProvider: () => useAssertWrappedByTernSecureProvider,
|
|
43
|
+
useDeepEqualMemo: () => useDeepEqualMemo,
|
|
44
|
+
useSafeLayoutEffect: () => useSafeLayoutEffect,
|
|
42
45
|
useSessionContext: () => useSessionContext,
|
|
43
46
|
useTernSecure: () => useTernSecure,
|
|
44
47
|
useTernSecureAuthContext: () => useTernSecureAuthContext,
|
|
@@ -93,7 +96,7 @@ Possible fixes:
|
|
|
93
96
|
}
|
|
94
97
|
}
|
|
95
98
|
|
|
96
|
-
// src/react/
|
|
99
|
+
// src/react/ternsecureProvider.tsx
|
|
97
100
|
var import_react3 = require("react");
|
|
98
101
|
var [TernSecureInstanceContext, useTernSecureInstanceContext] = createContextAndHook("TernSecureInstanceContext");
|
|
99
102
|
var [TernSecureAuthContext, useTernSecureAuthContext] = createContextAndHook("TernSecureAuthContext");
|
|
@@ -121,6 +124,25 @@ var useTernSecure = () => {
|
|
|
121
124
|
useAssertWrappedByTernSecureAuthProvider("useTernSecure");
|
|
122
125
|
return useTernSecureAuthCtx();
|
|
123
126
|
};
|
|
127
|
+
|
|
128
|
+
// src/react/hooks/useDeepEqualMemo.ts
|
|
129
|
+
var import_dequal = require("dequal");
|
|
130
|
+
var import_react4 = __toESM(require("react"));
|
|
131
|
+
var useDeepEqualMemoize = (value) => {
|
|
132
|
+
const ref = import_react4.default.useRef(value);
|
|
133
|
+
if (!(0, import_dequal.dequal)(value, ref.current)) {
|
|
134
|
+
ref.current = value;
|
|
135
|
+
}
|
|
136
|
+
return import_react4.default.useMemo(() => ref.current, [ref.current]);
|
|
137
|
+
};
|
|
138
|
+
var useDeepEqualMemo = (factory, dependencyArray) => {
|
|
139
|
+
return import_react4.default.useMemo(factory, useDeepEqualMemoize(dependencyArray));
|
|
140
|
+
};
|
|
141
|
+
var isDeeplyEqual = import_dequal.dequal;
|
|
142
|
+
|
|
143
|
+
// src/react/hooks/useSafeLayoutEffect.tsx
|
|
144
|
+
var import_react5 = __toESM(require("react"));
|
|
145
|
+
var useSafeLayoutEffect = typeof window !== "undefined" ? import_react5.default.useLayoutEffect : import_react5.default.useEffect;
|
|
124
146
|
// Annotate the CommonJS export names for ESM import in node:
|
|
125
147
|
0 && (module.exports = {
|
|
126
148
|
SessionContext,
|
|
@@ -130,8 +152,11 @@ var useTernSecure = () => {
|
|
|
130
152
|
UserContext,
|
|
131
153
|
assertContextExists,
|
|
132
154
|
createContextAndHook,
|
|
155
|
+
isDeeplyEqual,
|
|
133
156
|
useAssertWrappedByTernSecureAuthProvider,
|
|
134
157
|
useAssertWrappedByTernSecureProvider,
|
|
158
|
+
useDeepEqualMemo,
|
|
159
|
+
useSafeLayoutEffect,
|
|
135
160
|
useSessionContext,
|
|
136
161
|
useTernSecure,
|
|
137
162
|
useTernSecureAuthContext,
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/react/index.ts","../../src/react/ternSecureAuthProvider.tsx","../../src/react/ternsecureCtx.ts","../../src/react/ternSecureProvider.tsx","../../src/react/hooks/useTernSecure.ts"],"sourcesContent":["export * from './hooks'\n\nexport {\n useAssertWrappedByTernSecureProvider,\n useTernSecureInstanceContext,\n useTernSecureAuthContext,\n useSessionContext,\n useUserContext,\n SessionContext,\n UserContext,\n TernSecureAuthContext,\n TernSecureInstanceContext\n} from './ternSecureProvider'\n\nexport {\n assertContextExists,\n createContextAndHook\n} from './ternsecureCtx'\n\nexport {\n TernSecureAuthCtx,\n useTernSecureAuthCtx,\n useAssertWrappedByTernSecureAuthProvider\n} from './ternSecureAuthProvider'","'use client'\n\nimport type { \n TernSecureAuth,\n} from '@tern-secure/types';\nimport { useContext } from 'react';\n\nimport { createContextAndHook } from './ternsecureCtx';\n\n\nconst [TernSecureAuthCtx, useTernSecureAuthCtx] =\n createContextAndHook<TernSecureAuth>('TernSecureAuthCtx');\n\nfunction useAssertWrappedByTernSecureAuthProvider(displayNameOrFn: string | (() => void)): void {\n //const ctx = useTernSecureInstanceContext();\n const ctx = useContext(TernSecureAuthCtx);\n \n if (!ctx) {\n if (typeof displayNameOrFn === 'function') {\n displayNameOrFn();\n return;\n }\n \n throw new Error(\n `${displayNameOrFn} can only be used within the <TernSecureProvider /> component.\n \nPossible fixes:\n1. Ensure that the <TernSecureProvider /> is correctly wrapping your application\n2. Check for multiple versions of @tern-secure packages in your project`\n );\n }\n}\n\n\n\nexport {\n TernSecureAuthCtx,\n useTernSecureAuthCtx,\n useAssertWrappedByTernSecureAuthProvider,\n};","'use client'\n\nimport React from 'react';\n\n/**\n * Assert that the context value exists, otherwise throw an error.\n *\n * @internal\n */\nexport function assertContextExists(contextVal: unknown, msgOrCtx: string | React.Context<any>): asserts contextVal {\n if (!contextVal) {\n throw typeof msgOrCtx === 'string' ? new Error(msgOrCtx) : new Error(`${msgOrCtx.displayName} not found`);\n }\n}\ntype Options = { assertCtxFn?: (v: unknown, msg: string) => void };\ntype ContextAndHook<T> = React.Context<{ value: T } | undefined>;\ntype UseCtxFn<T> = () => T;\n\n/**\n * Create and return a Context and two hooks that return the context value.\n * The Context type is derived from the type passed in by the user.\n *\n * The first hook returned guarantees that the context exists so the returned value is always `CtxValue`\n * The second hook makes no guarantees, so the returned value can be `CtxValue | undefined`\n *\n * @internal\n */\n\nexport const createContextAndHook = <CtxValue>(\n displayName: string,\n options?: Options,\n): [ContextAndHook<CtxValue>, UseCtxFn<CtxValue>, UseCtxFn<CtxValue | Partial<CtxValue>>] => {\n const { assertCtxFn = assertContextExists } = options || {};\n const Ctx = React.createContext<{ value: CtxValue } | undefined >(undefined);\n Ctx.displayName = displayName;\n\n const useCtx = () => {\n const ctx = React.useContext(Ctx);\n assertCtxFn(ctx, `${displayName} not found`);\n return (ctx as any).value as CtxValue;\n };\n\n const useCtxWithoutGuarantee = () => {\n const ctx = React.useContext(Ctx);\n return ctx ? ctx.value : {}\n };\n\n /**\n * Assert that the context value exists, otherwise throw an error.\n if (ctx === undefined) {\n throw new Error(`use${name} must be used within a ${name}Provider`);\n }\n return ctx.value;\n */\n\n return [Ctx, useCtx, useCtxWithoutGuarantee];\n}","'use client'\n\nimport type { \n TernSecureAuthProvider,\n TernSecureInstanceTree,\n TernSecureState,\n} from '@tern-secure/types';\nimport { useContext } from 'react';\n\nimport { createContextAndHook } from './ternsecureCtx';\n\nexport type TernSecureAuthContextType = {\n authProvider: TernSecureAuthProvider | null | undefined;\n authState: TernSecureState;\n}\n\n\n// Create TernSecure instance context\nconst [TernSecureInstanceContext, useTernSecureInstanceContext] = \n createContextAndHook<TernSecureInstanceTree>('TernSecureInstanceContext');\n\nconst [TernSecureAuthContext, useTernSecureAuthContext] =\n createContextAndHook<TernSecureAuthContextType>('TernSecureAuthContext');\n\nconst [SessionContext, useSessionContext] = \ncreateContextAndHook<TernSecureInstanceTree['auth']['session']>('SessionContext');\n\nconst [UserContext, useUserContext] = \ncreateContextAndHook<TernSecureInstanceTree['auth']['user']>('UserContext');\n\n// Assert helper\nfunction useAssertWrappedByTernSecureProvider(displayNameOrFn: string | (() => void)): void {\n //const ctx = useTernSecureInstanceContext();\n const ctx = useContext(TernSecureInstanceContext);\n \n if (!ctx) {\n if (typeof displayNameOrFn === 'function') {\n displayNameOrFn();\n return;\n }\n\n throw new Error(\n `${displayNameOrFn} can only be used within the <TernSecureProvider /> component.\n \nPossible fixes:\n1. Ensure that the <TernSecureProvider /> is correctly wrapping your application\n2. Check for multiple versions of @tern-secure packages in your project`\n );\n }\n}\n\n\nexport {\n TernSecureInstanceContext,\n TernSecureAuthContext,\n SessionContext,\n UserContext,\n useTernSecureAuthContext,\n useSessionContext,\n useUserContext,\n useTernSecureInstanceContext,\n useAssertWrappedByTernSecureProvider\n};","import type { TernSecureAuth, TernSecureInstanceTree } from '@tern-secure/types';\n\nimport {\n useAssertWrappedByTernSecureAuthProvider,\n useTernSecureAuthCtx,\n} from '../ternSecureAuthProvider';\nimport {\n useAssertWrappedByTernSecureProvider,\n useTernSecureInstanceContext,\n} from '../ternSecureProvider';\n\n/**\n * @deprecated this was a previous version with cdn. now since in this package we dont use cdn, create a new hook that uuses TernSecureAuth and rename this to useTernSecure_Deprecated\n *\n */\nexport const useTernSecure_Deprecated = (): TernSecureInstanceTree => {\n /**\n * if no assertion is needed, you can use the following:\n * const instance = useTernSecureInstanceContext();\n * if (!instance) {\n * throw new Error('useTernSecure must be used within a TernSecureProvider');\n * }\n * return instance;\n */\n\n useAssertWrappedByTernSecureProvider('useTernSecure');\n return useTernSecureInstanceContext();\n};\n\n\n/**\n * New hook that uses TernSecureAuth\n */\nexport const useTernSecure = (): TernSecureAuth => {\n /**\n * if no assertion is needed, you can use the following:\n * const instance = useTernSecureInstanceContext();\n * if (!instance) {\n * throw new Error('useTernSecure must be used within a TernSecureProvider');\n * }\n * return instance;\n */\n\n useAssertWrappedByTernSecureAuthProvider('useTernSecure');\n return useTernSecureAuthCtx();\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAAA,gBAA2B;;;ACH3B,mBAAkB;AAOX,SAAS,oBAAoB,YAAqB,UAA2D;AAClH,MAAI,CAAC,YAAY;AACf,UAAM,OAAO,aAAa,WAAW,IAAI,MAAM,QAAQ,IAAI,IAAI,MAAM,GAAG,SAAS,WAAW,YAAY;AAAA,EAC1G;AACF;AAeO,IAAM,uBAAuB,CAClC,aACA,YAC2F;AAC3F,QAAM,EAAE,cAAc,oBAAoB,IAAI,WAAW,CAAC;AAC1D,QAAM,MAAM,aAAAC,QAAM,cAAgD,MAAS;AAC3E,MAAI,cAAc;AAElB,QAAM,SAAS,MAAM;AACnB,UAAM,MAAM,aAAAA,QAAM,WAAW,GAAG;AAChC,gBAAY,KAAK,GAAG,WAAW,YAAY;AAC3C,WAAQ,IAAY;AAAA,EACtB;AAEA,QAAM,yBAAyB,MAAM;AACnC,UAAM,MAAM,aAAAA,QAAM,WAAW,GAAG;AAChC,WAAO,MAAM,IAAI,QAAQ,CAAC;AAAA,EAC5B;AAUA,SAAO,CAAC,KAAK,QAAQ,sBAAsB;AAC7C;;;AD9CA,IAAM,CAAC,mBAAmB,oBAAoB,IAC5C,qBAAqC,mBAAmB;AAE1D,SAAS,yCAAyC,iBAA8C;AAE9F,QAAM,UAAM,0BAAW,iBAAiB;AAExC,MAAI,CAAC,KAAK;AACR,QAAI,OAAO,oBAAoB,YAAY;AACzC,sBAAgB;AAChB;AAAA,IACF;AAEA,UAAM,IAAI;AAAA,MACR,GAAG,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA,IAKpB;AAAA,EACF;AACF;;;AExBA,IAAAC,gBAA2B;AAW3B,IAAM,CAAC,2BAA2B,4BAA4B,IAC5D,qBAA6C,2BAA2B;AAE1E,IAAM,CAAC,uBAAuB,wBAAwB,IACpD,qBAAgD,uBAAuB;AAEzE,IAAM,CAAC,gBAAgB,iBAAiB,IACxC,qBAAgE,gBAAgB;AAEhF,IAAM,CAAC,aAAa,cAAc,IAClC,qBAA6D,aAAa;AAG1E,SAAS,qCAAqC,iBAA8C;AAE1F,QAAM,UAAM,0BAAW,yBAAyB;AAEhD,MAAI,CAAC,KAAK;AACR,QAAI,OAAO,oBAAoB,YAAY;AACzC,sBAAgB;AAChB;AAAA,IACF;AAEA,UAAM,IAAI;AAAA,MACR,GAAG,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA,IAKpB;AAAA,EACF;AACF;;;AChBO,IAAM,gBAAgB,MAAsB;AAUjD,2CAAyC,eAAe;AACxD,SAAO,qBAAqB;AAC9B;","names":["import_react","React","import_react"]}
|
|
1
|
+
{"version":3,"sources":["../../src/react/index.ts","../../src/react/ternSecureAuthProvider.tsx","../../src/react/ternsecureCtx.ts","../../src/react/ternsecureProvider.tsx","../../src/react/hooks/useTernSecure.ts","../../src/react/hooks/useDeepEqualMemo.ts","../../src/react/hooks/useSafeLayoutEffect.tsx"],"sourcesContent":["export * from './hooks'\n\nexport {\n useAssertWrappedByTernSecureProvider,\n useTernSecureInstanceContext,\n useTernSecureAuthContext,\n useSessionContext,\n useUserContext,\n SessionContext,\n UserContext,\n TernSecureAuthContext,\n TernSecureInstanceContext\n} from './ternsecureProvider'\n\nexport {\n assertContextExists,\n createContextAndHook\n} from './ternsecureCtx'\n\nexport {\n TernSecureAuthCtx,\n useTernSecureAuthCtx,\n useAssertWrappedByTernSecureAuthProvider\n} from './ternSecureAuthProvider'","'use client';\n\nimport type { TernSecureAuth } from '@tern-secure/types';\nimport { useContext } from 'react';\n\nimport { createContextAndHook } from './ternsecureCtx';\n\nconst [TernSecureAuthCtx, useTernSecureAuthCtx] =\n createContextAndHook<TernSecureAuth>('TernSecureAuthCtx');\n\nfunction useAssertWrappedByTernSecureAuthProvider(displayNameOrFn: string | (() => void)): void {\n //const ctx = useTernSecureInstanceContext();\n const ctx = useContext(TernSecureAuthCtx);\n\n if (!ctx) {\n if (typeof displayNameOrFn === 'function') {\n displayNameOrFn();\n return;\n }\n\n throw new Error(\n `${displayNameOrFn} can only be used within the <TernSecureProvider /> component.\n \nPossible fixes:\n1. Ensure that the <TernSecureProvider /> is correctly wrapping your application\n2. Check for multiple versions of @tern-secure packages in your project`,\n );\n }\n}\n\nexport { TernSecureAuthCtx, useTernSecureAuthCtx, useAssertWrappedByTernSecureAuthProvider };\n","'use client'\n\nimport React from 'react';\n\n/**\n * Assert that the context value exists, otherwise throw an error.\n *\n * @internal\n */\nexport function assertContextExists(contextVal: unknown, msgOrCtx: string | React.Context<any>): asserts contextVal {\n if (!contextVal) {\n throw typeof msgOrCtx === 'string' ? new Error(msgOrCtx) : new Error(`${msgOrCtx.displayName} not found`);\n }\n}\ntype Options = { assertCtxFn?: (v: unknown, msg: string) => void };\ntype ContextAndHook<T> = React.Context<{ value: T } | undefined>;\ntype UseCtxFn<T> = () => T;\n\n/**\n * Create and return a Context and two hooks that return the context value.\n * The Context type is derived from the type passed in by the user.\n *\n * The first hook returned guarantees that the context exists so the returned value is always `CtxValue`\n * The second hook makes no guarantees, so the returned value can be `CtxValue | undefined`\n *\n * @internal\n */\n\nexport const createContextAndHook = <CtxValue>(\n displayName: string,\n options?: Options,\n): [ContextAndHook<CtxValue>, UseCtxFn<CtxValue>, UseCtxFn<CtxValue | Partial<CtxValue>>] => {\n const { assertCtxFn = assertContextExists } = options || {};\n const Ctx = React.createContext<{ value: CtxValue } | undefined >(undefined);\n Ctx.displayName = displayName;\n\n const useCtx = () => {\n const ctx = React.useContext(Ctx);\n assertCtxFn(ctx, `${displayName} not found`);\n return (ctx as any).value as CtxValue;\n };\n\n const useCtxWithoutGuarantee = () => {\n const ctx = React.useContext(Ctx);\n return ctx ? ctx.value : {}\n };\n\n /**\n * Assert that the context value exists, otherwise throw an error.\n if (ctx === undefined) {\n throw new Error(`use${name} must be used within a ${name}Provider`);\n }\n return ctx.value;\n */\n\n return [Ctx, useCtx, useCtxWithoutGuarantee];\n}","'use client'\n\nimport type { \n TernSecureAuthProvider,\n TernSecureInstanceTree,\n TernSecureState,\n} from '@tern-secure/types';\nimport { useContext } from 'react';\n\nimport { createContextAndHook } from './ternsecureCtx';\n\nexport type TernSecureAuthContextType = {\n authProvider: TernSecureAuthProvider | null | undefined;\n authState: TernSecureState;\n}\n\n\n// Create TernSecure instance context\nconst [TernSecureInstanceContext, useTernSecureInstanceContext] = \n createContextAndHook<TernSecureInstanceTree>('TernSecureInstanceContext');\n\nconst [TernSecureAuthContext, useTernSecureAuthContext] =\n createContextAndHook<TernSecureAuthContextType>('TernSecureAuthContext');\n\nconst [SessionContext, useSessionContext] = \ncreateContextAndHook<TernSecureInstanceTree['auth']['session']>('SessionContext');\n\nconst [UserContext, useUserContext] = \ncreateContextAndHook<TernSecureInstanceTree['auth']['user']>('UserContext');\n\n// Assert helper\nfunction useAssertWrappedByTernSecureProvider(displayNameOrFn: string | (() => void)): void {\n //const ctx = useTernSecureInstanceContext();\n const ctx = useContext(TernSecureInstanceContext);\n \n if (!ctx) {\n if (typeof displayNameOrFn === 'function') {\n displayNameOrFn();\n return;\n }\n\n throw new Error(\n `${displayNameOrFn} can only be used within the <TernSecureProvider /> component.\n \nPossible fixes:\n1. Ensure that the <TernSecureProvider /> is correctly wrapping your application\n2. Check for multiple versions of @tern-secure packages in your project`\n );\n }\n}\n\n\nexport {\n TernSecureInstanceContext,\n TernSecureAuthContext,\n SessionContext,\n UserContext,\n useTernSecureAuthContext,\n useSessionContext,\n useUserContext,\n useTernSecureInstanceContext,\n useAssertWrappedByTernSecureProvider\n};","import type { TernSecureAuth, TernSecureInstanceTree } from '@tern-secure/types';\n\nimport {\n useAssertWrappedByTernSecureAuthProvider,\n useTernSecureAuthCtx,\n} from '../ternSecureAuthProvider';\nimport {\n useAssertWrappedByTernSecureProvider,\n useTernSecureInstanceContext,\n} from '../ternsecureProvider';\n\n/**\n * @deprecated this was a previous version with cdn. now since in this package we dont use cdn, create a new hook that uuses TernSecureAuth and rename this to useTernSecure_Deprecated\n *\n */\nexport const useTernSecure_Deprecated = (): TernSecureInstanceTree => {\n /**\n * if no assertion is needed, you can use the following:\n * const instance = useTernSecureInstanceContext();\n * if (!instance) {\n * throw new Error('useTernSecure must be used within a TernSecureProvider');\n * }\n * return instance;\n */\n\n useAssertWrappedByTernSecureProvider('useTernSecure');\n return useTernSecureInstanceContext();\n};\n\n\n/**\n * New hook that uses TernSecureAuth\n */\nexport const useTernSecure = (): TernSecureAuth => {\n /**\n * if no assertion is needed, you can use the following:\n * const instance = useTernSecureInstanceContext();\n * if (!instance) {\n * throw new Error('useTernSecure must be used within a TernSecureProvider');\n * }\n * return instance;\n */\n\n useAssertWrappedByTernSecureAuthProvider('useTernSecure');\n return useTernSecureAuthCtx();\n};\n","import { dequal as deepEqual } from 'dequal';\nimport React from 'react';\n\ntype UseMemoFactory<T> = () => T;\ntype UseMemoDependencyArray = Exclude<Parameters<typeof React.useMemo>[1], 'undefined'>;\ntype UseDeepEqualMemo = <T>(factory: UseMemoFactory<T>, dependencyArray: UseMemoDependencyArray) => T;\n\nconst useDeepEqualMemoize = <T>(value: T) => {\n const ref = React.useRef<T>(value);\n if (!deepEqual(value, ref.current)) {\n ref.current = value;\n }\n return React.useMemo(() => ref.current, [ref.current]);\n};\n\n/**\n * @internal\n */\nexport const useDeepEqualMemo: UseDeepEqualMemo = (factory, dependencyArray) => {\n return React.useMemo(factory, useDeepEqualMemoize(dependencyArray));\n};\n\n/**\n * @internal\n */\nexport const isDeeplyEqual = deepEqual;\n","import React from 'react';\n\n/**\n * @internal\n */\n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