rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
37 lines (36 loc) • 1.16 kB
JavaScript
// Simple debug function with DEBUG environment variable filtering
// Supports patterns like: DEBUG=passkey:* or DEBUG=passkey:database,passkey:functions
function isEnabled(namespace) {
const debug = process.env.DEBUG;
if (!debug)
return false;
const patterns = debug.split(",").map((p) => p.trim());
for (const pattern of patterns) {
if (pattern.startsWith("-")) {
const excludePattern = pattern.slice(1);
if (matchesPattern(namespace, excludePattern)) {
return false;
}
continue;
}
if (matchesPattern(namespace, pattern)) {
return true;
}
}
return false;
}
function matchesPattern(namespace, pattern) {
// Convert pattern to regex (handle * wildcards)
const regex = pattern
.replace(/\*/g, ".*") // * becomes .*
.replace(/:/g, "\\:"); // escape colons
return new RegExp(`^${regex}$`).test(namespace);
}
const debug = (namespace) => {
return (...args) => {
if (isEnabled(namespace)) {
console.log(`[${namespace}]`, ...args);
}
};
};
export default debug;