@clerk/shared
Version:
Internal package utils used by the Clerk SDKs
121 lines (118 loc) • 4.12 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/keys.ts
var keys_exports = {};
__export(keys_exports, {
buildPublishableKey: () => buildPublishableKey,
createDevOrStagingUrlCache: () => createDevOrStagingUrlCache,
isDevelopmentFromApiKey: () => isDevelopmentFromApiKey,
isLegacyFrontendApiKey: () => isLegacyFrontendApiKey,
isProductionFromApiKey: () => isProductionFromApiKey,
isPublishableKey: () => isPublishableKey,
parsePublishableKey: () => parsePublishableKey
});
module.exports = __toCommonJS(keys_exports);
// src/isomorphicAtob.ts
var isomorphicAtob = (data) => {
if (typeof atob !== "undefined" && typeof atob === "function") {
return atob(data);
} else if (typeof global !== "undefined" && global.Buffer) {
return new global.Buffer(data, "base64").toString();
}
return data;
};
// src/keys.ts
var PUBLISHABLE_KEY_LIVE_PREFIX = "pk_live_";
var PUBLISHABLE_KEY_TEST_PREFIX = "pk_test_";
var PUBLISHABLE_FRONTEND_API_DEV_REGEX = /^(([a-z]+)-){2}([0-9]{1,2})\.clerk\.accounts([a-z.]*)(dev|com)$/i;
function buildPublishableKey(frontendApi) {
const keyPrefix = PUBLISHABLE_FRONTEND_API_DEV_REGEX.test(frontendApi) ? PUBLISHABLE_KEY_TEST_PREFIX : PUBLISHABLE_KEY_LIVE_PREFIX;
return `${keyPrefix}${btoa(`${frontendApi}$`)}`;
}
function parsePublishableKey(key) {
key = key || "";
if (!isPublishableKey(key)) {
return null;
}
const instanceType = key.startsWith(PUBLISHABLE_KEY_LIVE_PREFIX) ? "production" : "development";
let frontendApi = isomorphicAtob(key.split("_")[2]);
if (!frontendApi.endsWith("$")) {
return null;
}
frontendApi = frontendApi.slice(0, -1);
return {
instanceType,
frontendApi
};
}
function isPublishableKey(key) {
key = key || "";
const hasValidPrefix = key.startsWith(PUBLISHABLE_KEY_LIVE_PREFIX) || key.startsWith(PUBLISHABLE_KEY_TEST_PREFIX);
const hasValidFrontendApiPostfix = isomorphicAtob(key.split("_")[2] || "").endsWith("$");
return hasValidPrefix && hasValidFrontendApiPostfix;
}
function isLegacyFrontendApiKey(key) {
key = key || "";
return key.startsWith("clerk.");
}
function createDevOrStagingUrlCache() {
const DEV_OR_STAGING_SUFFIXES = [
".lcl.dev",
".stg.dev",
".lclstage.dev",
".stgstage.dev",
".dev.lclclerk.com",
".stg.lclclerk.com",
".accounts.lclclerk.com",
"accountsstage.dev",
"accounts.dev"
];
const devOrStagingUrlCache = /* @__PURE__ */ new Map();
return {
isDevOrStagingUrl: (url) => {
if (!url) {
return false;
}
const hostname = typeof url === "string" ? url : url.hostname;
let res = devOrStagingUrlCache.get(hostname);
if (res === void 0) {
res = DEV_OR_STAGING_SUFFIXES.some((s) => hostname.endsWith(s));
devOrStagingUrlCache.set(hostname, res);
}
return res;
}
};
}
function isDevelopmentFromApiKey(apiKey) {
return apiKey.startsWith("test_") || apiKey.startsWith("sk_test_");
}
function isProductionFromApiKey(apiKey) {
return apiKey.startsWith("live_") || apiKey.startsWith("sk_live_");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
buildPublishableKey,
createDevOrStagingUrlCache,
isDevelopmentFromApiKey,
isLegacyFrontendApiKey,
isProductionFromApiKey,
isPublishableKey,
parsePublishableKey
});
//# sourceMappingURL=keys.js.map