@dbs-portal/core-module-registry
Version:
Core module registry system for automatic module discovery and registration
382 lines (380 loc) • 11.6 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
function detectEnvironment() {
const isBrowser2 = typeof window !== "undefined" && typeof window.document !== "undefined" && typeof navigator !== "undefined";
const isNode2 = typeof process !== "undefined" && process.versions != null && process.versions.node != null && !isBrowser2;
const isWebWorker = typeof importScripts === "function" && typeof navigator !== "undefined" && !isBrowser2;
const isTest2 = typeof globalThis !== "undefined" && globalThis.__TEST__ || typeof jest !== "undefined" || typeof vitest !== "undefined";
const isDevelopment2 = isBrowser2 && window.location?.hostname === "localhost";
const isProduction2 = typeof process !== "undefined" && true || !isDevelopment2 && !isTest2;
return {
isBrowser: isBrowser2,
isNode: isNode2,
isWebWorker,
isTest: isTest2,
isDevelopment: isDevelopment2,
isProduction: isProduction2
};
}
const ENV = detectEnvironment();
async function loadConditionalModule(nodeModule, browserModule) {
if (ENV.isNode) {
return await nodeModule();
} else {
return await browserModule();
}
}
function loadConditionalModuleSync(nodeModule, browserModule) {
if (ENV.isNode) {
return nodeModule();
} else {
return browserModule();
}
}
function getPathSeparator() {
return ENV.isNode && process.platform === "win32" ? "\\" : "/";
}
function getCurrentDirectory() {
if (ENV.isNode && typeof process !== "undefined") {
return process.cwd();
} else if (ENV.isBrowser && typeof window !== "undefined") {
return window.location.pathname;
} else {
return "/";
}
}
function isDevelopment() {
return ENV.isDevelopment;
}
function isProduction() {
return ENV.isProduction;
}
function isTest() {
return ENV.isTest;
}
function isBrowser() {
return ENV.isBrowser;
}
function isNode() {
return ENV.isNode;
}
function getEnvironmentName() {
if (ENV.isTest) return "test";
if (ENV.isDevelopment) return "development";
if (ENV.isProduction) return "production";
return "unknown";
}
function getPlatformName() {
if (ENV.isNode) return "node";
if (ENV.isBrowser) return "browser";
if (ENV.isWebWorker) return "webworker";
return "unknown";
}
function createLogger(prefix = "") {
const logPrefix = `[${getPlatformName()}${prefix ? `:${prefix}` : ""}]`;
return {
debug: (...args) => {
if (ENV.isDevelopment) {
console.debug(logPrefix, ...args);
}
},
info: (...args) => {
console.info(logPrefix, ...args);
},
warn: (...args) => {
console.warn(logPrefix, ...args);
},
error: (...args) => {
console.error(logPrefix, ...args);
}
};
}
const FEATURES = {
// File system operations
canReadFiles: ENV.isNode,
canWriteFiles: ENV.isNode,
canAccessFileSystem: ENV.isNode,
// Network operations
canFetch: ENV.isBrowser || ENV.isNode,
canMakeHttpRequests: true,
// Module operations
canDynamicImport: true,
canRequire: ENV.isNode,
// Discovery features
canScanPackages: ENV.isNode,
canScanFileSystem: ENV.isNode,
canUseGlob: ENV.isNode,
// Caching
canUseLocalStorage: ENV.isBrowser,
canUseSessionStorage: ENV.isBrowser,
canUseMemoryCache: true,
// Development features
canHotReload: ENV.isDevelopment,
canShowDevTools: ENV.isDevelopment && ENV.isBrowser
};
function hasFeature(feature) {
return FEATURES[feature];
}
function assertFeature(feature, message) {
if (!hasFeature(feature)) {
throw new Error(message || `Feature '${feature}' is not available in ${getPlatformName()} environment`);
}
}
function warnMissingFeature(feature, fallback) {
if (!hasFeature(feature)) {
const logger2 = createLogger("feature-check");
logger2.warn(`Feature '${feature}' is not available in ${getPlatformName()} environment${fallback ? `, using fallback: ${fallback}` : ""}`);
}
}
const logger = createLogger("platform-modules");
async function loadPath() {
return loadConditionalModule(
// Node.js version
async () => {
try {
const path = await import("path");
logger.debug("Loaded Node.js path module");
return path;
} catch (error) {
logger.warn("Failed to load Node.js path module, falling back to browser version");
const browserPath = await import(
/* webpackChunkName: "browser-path" */
"./browser-compat-AuLlE_ij.js"
).then((n) => n.b);
return browserPath.default;
}
},
// Browser version
async () => {
const browserPath = await import(
/* webpackChunkName: "browser-path" */
"./browser-compat-AuLlE_ij.js"
).then((n) => n.b);
logger.debug("Loaded browser path module");
return browserPath.default;
}
);
}
async function loadFs() {
return loadConditionalModule(
// Node.js version
async () => {
try {
const fs = await import("fs/promises");
logger.debug("Loaded Node.js fs module");
return {
...fs,
exists: async (path) => {
try {
await fs.access(path);
return true;
} catch {
return false;
}
}
};
} catch (error) {
logger.warn("Failed to load Node.js fs module, falling back to browser version");
const browserFs = await import(
/* webpackChunkName: "browser-fs" */
"./browser-compat-AuLlE_ij.js"
).then((n) => n.a);
return browserFs.default;
}
},
// Browser version
async () => {
const browserFs = await import(
/* webpackChunkName: "browser-fs" */
"./browser-compat-AuLlE_ij.js"
).then((n) => n.a);
logger.debug("Loaded browser fs module");
return browserFs.default;
}
);
}
async function loadGlob() {
return loadConditionalModule(
// Node.js version
async () => {
try {
const { glob, globSync } = await import("glob");
logger.debug("Loaded Node.js glob module");
return {
glob: async (pattern, options = {}) => {
if (Array.isArray(pattern)) {
const results = await Promise.all(
pattern.map((p) => glob(p, options))
);
return results.flat();
}
return await glob(pattern, options);
},
globSync: (pattern, options = {}) => {
if (Array.isArray(pattern)) {
return pattern.flatMap((p) => globSync(p, options));
}
return globSync(pattern, options);
}
};
} catch (error) {
logger.warn("Failed to load Node.js glob module, falling back to browser version");
const browserGlob = await import(
/* webpackChunkName: "browser-glob" */
"./browser-compat-AuLlE_ij.js"
).then((n) => n.c);
return browserGlob.default;
}
},
// Browser version
async () => {
const browserGlob = await import(
/* webpackChunkName: "browser-glob" */
"./browser-compat-AuLlE_ij.js"
).then((n) => n.c);
logger.debug("Loaded browser glob module");
return browserGlob.default;
}
);
}
class PlatformModuleCache {
constructor() {
__publicField(this, "pathModule", null);
__publicField(this, "fsModule", null);
__publicField(this, "globModule", null);
}
async getPath() {
if (!this.pathModule) {
this.pathModule = await loadPath();
}
return this.pathModule;
}
async getFs() {
if (!this.fsModule) {
this.fsModule = await loadFs();
}
return this.fsModule;
}
async getGlob() {
if (!this.globModule) {
this.globModule = await loadGlob();
}
return this.globModule;
}
clear() {
this.pathModule = null;
this.fsModule = null;
this.globModule = null;
}
}
const moduleCache = new PlatformModuleCache();
const getPath = () => moduleCache.getPath();
const getFs = () => moduleCache.getFs();
const getGlob = () => moduleCache.getGlob();
const clearModuleCache = () => moduleCache.clear();
async function initializePlatformModules() {
logger.info(`Initializing platform modules for ${ENV.isNode ? "Node.js" : "browser"} environment`);
const [path, fs, glob] = await Promise.all([
getPath(),
getFs(),
getGlob()
]);
logger.info("Platform modules initialized successfully");
return { path, fs, glob };
}
async function preloadBrowserModules(mockData) {
if (!ENV.isBrowser) {
logger.warn("preloadBrowserModules called in non-browser environment");
return;
}
logger.info("Preloading browser modules with mock data");
try {
if (mockData.files || mockData.directories) {
const fs = await getFs();
const browserFs = fs;
if (mockData.files && browserFs.preloadFile) {
for (const [path, content] of Object.entries(mockData.files)) {
browserFs.preloadFile(path, content);
}
}
if (mockData.directories && browserFs.preloadDirectory) {
for (const [path, files] of Object.entries(mockData.directories)) {
browserFs.preloadDirectory(path, files);
}
}
}
if (mockData.structure) {
const glob = await getGlob();
const browserGlob = glob;
if (browserGlob.setMockStructure) {
browserGlob.setMockStructure(mockData.structure);
}
}
logger.info("Browser modules preloaded successfully");
} catch (error) {
logger.error("Failed to preload browser modules:", error);
throw error;
}
}
function getPlatformInfo() {
return {
platform: ENV.isNode ? "node" : "browser",
canReadFiles: ENV.isNode,
canScanDirectories: ENV.isNode,
canUseGlob: ENV.isNode,
requiresPreloading: ENV.isBrowser,
features: {
fileSystem: ENV.isNode,
glob: ENV.isNode,
dynamicImport: true,
fetch: true
}
};
}
function createPlatformError(operation, originalError, suggestions) {
const platform = ENV.isNode ? "Node.js" : "browser";
const info = getPlatformInfo();
let message = `Platform operation '${operation}' failed in ${platform} environment: ${originalError.message}`;
if (suggestions?.length) {
message += `
Suggestions:
${suggestions.map((s) => ` - ${s}`).join("\n")}`;
}
if (!info.canReadFiles && operation.includes("file")) {
message += "\n\nNote: File system operations are not available in browser environment. Consider using preloadBrowserModules() to provide mock data.";
}
const error = new Error(message);
error.name = "PlatformError";
error.cause = originalError;
return error;
}
export {
ENV as E,
FEATURES as F,
getFs as a,
getGlob as b,
getPlatformInfo as c,
createPlatformError as d,
clearModuleCache as e,
detectEnvironment as f,
getPath as g,
loadConditionalModuleSync as h,
initializePlatformModules as i,
getPathSeparator as j,
getCurrentDirectory as k,
loadConditionalModule as l,
isDevelopment as m,
isProduction as n,
isTest as o,
preloadBrowserModules as p,
isBrowser as q,
isNode as r,
getEnvironmentName as s,
getPlatformName as t,
createLogger as u,
hasFeature as v,
assertFeature as w,
warnMissingFeature as x
};
//# sourceMappingURL=platform-utils-Cix3Fefx.js.map