@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
74 lines (73 loc) • 2.46 kB
JavaScript
;
/* Socket Lib - Built with esbuild */
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);
var context_exports = {};
__export(context_exports, {
getTheme: () => getTheme,
onThemeChange: () => onThemeChange,
setTheme: () => setTheme,
withTheme: () => withTheme,
withThemeSync: () => withThemeSync
});
module.exports = __toCommonJS(context_exports);
var import_node_async_hooks = require("node:async_hooks");
var import_themes = require("./themes");
const themeStorage = new import_node_async_hooks.AsyncLocalStorage();
let fallbackTheme = import_themes.SOCKET_THEME;
const listeners = /* @__PURE__ */ new Set();
function setTheme(theme) {
fallbackTheme = typeof theme === "string" ? import_themes.THEMES[theme] : theme;
emitThemeChange(fallbackTheme);
}
function getTheme() {
return themeStorage.getStore() ?? fallbackTheme;
}
async function withTheme(theme, fn) {
const resolvedTheme = typeof theme === "string" ? import_themes.THEMES[theme] : theme;
return await themeStorage.run(resolvedTheme, async () => {
emitThemeChange(resolvedTheme);
return await fn();
});
}
function withThemeSync(theme, fn) {
const resolvedTheme = typeof theme === "string" ? import_themes.THEMES[theme] : theme;
return themeStorage.run(resolvedTheme, () => {
emitThemeChange(resolvedTheme);
return fn();
});
}
function onThemeChange(listener) {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
function emitThemeChange(theme) {
for (const listener of listeners) {
listener(theme);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getTheme,
onThemeChange,
setTheme,
withTheme,
withThemeSync
});