🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

obug

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

obug - npm Package Compare versions

Comparing version
2.1.1
to
2.1.2
+1
-1
dist/browser.d.ts

@@ -11,2 +11,2 @@ import { a as Debugger, i as DebugOptions, n as enabled, o as Formatters, r as namespaces, s as InspectOptions, t as disable } from "./core.js";

//#endregion
export { DebugOptions, Debugger, Formatters, InspectOptions, createDebug, disable, enable, enabled, namespaces };
export { type DebugOptions, type Debugger, type Formatters, type InspectOptions, createDebug, disable, enable, enabled, namespaces };
import { a as namespaces, i as enabled, n as disable, o as humanize, r as enable$1, s as selectColor, t as createDebug$1 } from "./core.js";
//#region src/browser.ts
const colors = [

@@ -80,2 +81,5 @@ "#0000CC",

];
/**
* Colorize log arguments if enabled.
*/
function formatArgs(diff, args) {

@@ -96,2 +100,8 @@ const { useColors } = this;

}
/**
* Invokes `console.debug()` when available.
* No-op when `console.debug` is not a "function".
* If `console.debug` is not available, falls back
* to `console.log`.
*/
const log = console.debug || console.log || (() => {});

@@ -102,3 +112,7 @@ const storage = localstorage();

formatArgs,
formatters: { j(v) {
formatters: {
/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/
j(v) {
try {

@@ -119,2 +133,9 @@ return JSON.stringify(v);

}
/**
* Localstorage attempts to return the localstorage.
*
* This is necessary because safari throws
* when a user disables cookies/localstorage
* and you attempt to access it.
*/
function localstorage() {

@@ -133,13 +154,18 @@ try {

}
function save(namespaces$1) {
function save(namespaces) {
try {
if (namespaces$1) storage.setItem("debug", namespaces$1);
if (namespaces) storage.setItem("debug", namespaces);
else storage.removeItem("debug");
} catch (_unused3) {}
}
function enable(namespaces$1) {
save(namespaces$1);
enable$1(namespaces$1);
/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
*/
function enable(namespaces) {
save(namespaces);
enable$1(namespaces);
}
enable$1(load());
//#endregion
export { createDebug, disable, enable, enabled, namespaces };

@@ -1,1 +0,1 @@

function e(e){return e instanceof Error?e.stack||e.message:e}function t(e,t){let n=0;for(let e=0;e<t.length;e++)n=(n<<5)-n+t.charCodeAt(e),n|=0;return e[Math.abs(n)%e.length]}function n(e,t){let n=0,r=0,i=-1,a=0;for(;n<e.length;)if(r<t.length&&(t[r]===e[n]||t[r]===`*`))t[r]===`*`?(i=r,a=n,r++):(n++,r++);else if(i!==-1)r=i+1,a++,n=a;else return!1;for(;r<t.length&&t[r]===`*`;)r++;return r===t.length}function r(e){return e>=1e3?`${(e/1e3).toFixed(1)}s`:`${e}ms`}let i=``;function a(){return i}function o(t,n){let r,a,s,c,l=(...t)=>{if(!l.enabled)return;let i=Date.now(),a=i-(r||i);r=i,t[0]=e(t[0]),typeof t[0]!=`string`&&t.unshift(`%O`);let o=0;t[0]=t[0].replace(/%([a-z%])/gi,(e,r)=>{if(e===`%%`)return`%`;o++;let i=n.formatters[r];if(typeof i==`function`){let n=t[o];e=i.call(l,n),t.splice(o,1),o--}return e}),n.formatArgs.call(l,a,t),l.log(...t)};return l.extend=function(e,t=`:`){return o(this.namespace+t+e,{useColors:this.useColors,color:this.color,formatArgs:this.formatArgs,formatters:this.formatters,inspectOpts:this.inspectOpts,log:this.log,humanize:this.humanize})},Object.assign(l,n),l.namespace=t,Object.defineProperty(l,`enabled`,{enumerable:!0,configurable:!1,get:()=>a==null?(s!==i&&(s=i,c=d(t)),c):a,set:e=>{a=e}}),l}let s=[],c=[];function l(e){i=e,s=[],c=[];let t=i.trim().replace(/\s+/g,`,`).split(`,`).filter(Boolean);for(let e of t)e[0]===`-`?c.push(e.slice(1)):s.push(e)}function u(){let e=[...s,...c.map(e=>`-${e}`)].join(`,`);return l(``),e}function d(e){for(let t of c)if(n(e,t))return!1;for(let t of s)if(n(e,t))return!0;return!1}const f=`#0000CC.#0000FF.#0033CC.#0033FF.#0066CC.#0066FF.#0099CC.#0099FF.#00CC00.#00CC33.#00CC66.#00CC99.#00CCCC.#00CCFF.#3300CC.#3300FF.#3333CC.#3333FF.#3366CC.#3366FF.#3399CC.#3399FF.#33CC00.#33CC33.#33CC66.#33CC99.#33CCCC.#33CCFF.#6600CC.#6600FF.#6633CC.#6633FF.#66CC00.#66CC33.#9900CC.#9900FF.#9933CC.#9933FF.#99CC00.#99CC33.#CC0000.#CC0033.#CC0066.#CC0099.#CC00CC.#CC00FF.#CC3300.#CC3333.#CC3366.#CC3399.#CC33CC.#CC33FF.#CC6600.#CC6633.#CC9900.#CC9933.#CCCC00.#CCCC33.#FF0000.#FF0033.#FF0066.#FF0099.#FF00CC.#FF00FF.#FF3300.#FF3333.#FF3366.#FF3399.#FF33CC.#FF33FF.#FF6600.#FF6633.#FF9900.#FF9933.#FFCC00.#FFCC33`.split(`.`);function p(e,t){let{useColors:n}=this;if(t[0]=`${(n?`%c`:``)+this.namespace+(n?` %c`:` `)+t[0]+(n?`%c `:` `)}+${this.humanize(e)}`,!n)return;let r=`color: ${this.color}`;t.splice(1,0,r,`color: inherit`);let i=0,a=0;t[0].replace(/%[a-z%]/gi,e=>{e!==`%%`&&(i++,e===`%c`&&(a=i))}),t.splice(a,0,r)}const m=console.debug||console.log||(()=>{}),h=v(),g={useColors:!0,formatArgs:p,formatters:{j(e){try{return JSON.stringify(e)}catch(e){return`[UnexpectedJSONParseError]: ${e.message}`}}},inspectOpts:{},humanize:r,log:m};function _(e,n){var r;let i=(r=n&&n.color)==null?t(f,e):r;return o(e,Object.assign(g,{color:i},n))}function v(){try{return localStorage}catch(e){}}function y(){let e;try{e=h.getItem(`debug`)||h.getItem(`DEBUG`)}catch(e){}return!e&&typeof process<`u`&&`env`in process&&(e=process.env.DEBUG),e||``}function b(e){try{e?h.setItem(`debug`,e):h.removeItem(`debug`)}catch(e){}}function x(e){b(e),l(e)}l(y());export{_ as createDebug,u as disable,x as enable,d as enabled,a as namespaces};
function e(e){return e instanceof Error?e.stack||e.message:e}function t(e,t){let n=0;for(let e=0;e<t.length;e++)n=(n<<5)-n+t.charCodeAt(e),n|=0;return e[Math.abs(n)%e.length]}function n(e,t){let n=0,r=0,i=-1,a=0;for(;n<e.length;)if(r<t.length&&(t[r]===e[n]||t[r]===`*`))t[r]===`*`?(i=r,a=n,r++):(n++,r++);else if(i!==-1)r=i+1,a++,n=a;else return!1;for(;r<t.length&&t[r]===`*`;)r++;return r===t.length}function r(e){return e>=1e3?`${(e/1e3).toFixed(1)}s`:`${e}ms`}let i=``;function a(){return i}function o(t,n){let r,a,s,c,l=(...t)=>{if(!l.enabled)return;let i=Date.now(),a=i-(r||i);r=i,t[0]=e(t[0]),typeof t[0]!=`string`&&t.unshift(`%O`);let o=0;t[0]=t[0].replace(/%([a-z%])/gi,(e,r)=>{if(e===`%%`)return`%`;o++;let i=n.formatters[r];if(typeof i==`function`){let n=t[o];e=i.call(l,n),t.splice(o,1),o--}return e}),n.formatArgs.call(l,a,t),l.log(...t)};return l.extend=function(e,t=`:`){return o(this.namespace+t+e,{useColors:this.useColors,color:this.color,formatArgs:this.formatArgs,formatters:this.formatters,inspectOpts:this.inspectOpts,log:this.log,humanize:this.humanize})},Object.assign(l,n),l.namespace=t,Object.defineProperty(l,"enabled",{enumerable:!0,configurable:!1,get:()=>a==null?(s!==i&&(s=i,c=d(t)),c):a,set:e=>{a=e}}),l}let s=[],c=[];function l(e){i=e,s=[],c=[];let t=i.trim().replace(/\s+/g,`,`).split(`,`).filter(Boolean);for(let e of t)e[0]===`-`?c.push(e.slice(1)):s.push(e)}function u(){let e=[...s,...c.map(e=>`-${e}`)].join(`,`);return l(``),e}function d(e){for(let t of c)if(n(e,t))return!1;for(let t of s)if(n(e,t))return!0;return!1}const f=`#0000CC.#0000FF.#0033CC.#0033FF.#0066CC.#0066FF.#0099CC.#0099FF.#00CC00.#00CC33.#00CC66.#00CC99.#00CCCC.#00CCFF.#3300CC.#3300FF.#3333CC.#3333FF.#3366CC.#3366FF.#3399CC.#3399FF.#33CC00.#33CC33.#33CC66.#33CC99.#33CCCC.#33CCFF.#6600CC.#6600FF.#6633CC.#6633FF.#66CC00.#66CC33.#9900CC.#9900FF.#9933CC.#9933FF.#99CC00.#99CC33.#CC0000.#CC0033.#CC0066.#CC0099.#CC00CC.#CC00FF.#CC3300.#CC3333.#CC3366.#CC3399.#CC33CC.#CC33FF.#CC6600.#CC6633.#CC9900.#CC9933.#CCCC00.#CCCC33.#FF0000.#FF0033.#FF0066.#FF0099.#FF00CC.#FF00FF.#FF3300.#FF3333.#FF3366.#FF3399.#FF33CC.#FF33FF.#FF6600.#FF6633.#FF9900.#FF9933.#FFCC00.#FFCC33`.split(`.`);function p(e,t){let{useColors:n}=this;if(t[0]=`${(n?`%c`:``)+this.namespace+(n?` %c`:` `)+t[0]+(n?`%c `:` `)}+${this.humanize(e)}`,!n)return;let r=`color: ${this.color}`;t.splice(1,0,r,`color: inherit`);let i=0,a=0;t[0].replace(/%[a-z%]/gi,e=>{e!==`%%`&&(i++,e===`%c`&&(a=i))}),t.splice(a,0,r)}const m=console.debug||console.log||(()=>{}),h=v(),g={useColors:!0,formatArgs:p,formatters:{j(e){try{return JSON.stringify(e)}catch(e){return`[UnexpectedJSONParseError]: ${e.message}`}}},inspectOpts:{},humanize:r,log:m};function _(e,n){var r;let i=(r=n&&n.color)==null?t(f,e):r;return o(e,Object.assign(g,{color:i},n))}function v(){try{return localStorage}catch(e){}}function y(){let e;try{e=h.getItem(`debug`)||h.getItem(`DEBUG`)}catch(e){}return!e&&typeof process<`u`&&`env`in process&&(e=process.env.DEBUG),e||``}function b(e){try{e?h.setItem(`debug`,e):h.removeItem(`debug`)}catch(e){}}function x(e){b(e),l(e)}l(y());export{_ as createDebug,u as disable,x as enable,d as enabled,a as namespaces};

@@ -0,1 +1,5 @@

//#region src/utils.ts
/**
* Coerce `value`.
*/
function coerce(value) {

@@ -5,2 +9,6 @@ if (value instanceof Error) return value.stack || value.message;

}
/**
* Selects a color for a debug namespace
* @return An ANSI color code for the given namespace
*/
function selectColor(colors, namespace) {

@@ -14,2 +22,6 @@ let hash = 0;

}
/**
* Checks if the given string matches a namespace template, honoring
* asterisks as wildcards.
*/
function matchesTemplate(search, template) {

@@ -40,3 +52,8 @@ let searchIndex = 0;

}
//#endregion
//#region src/core.ts
let globalNamespaces = "";
/**
* Returns a string of the currently enabled debug namespaces.
*/
function namespaces() {

@@ -73,4 +90,4 @@ return globalNamespaces;

};
debug.extend = function(namespace$1, delimiter = ":") {
return createDebug(this.namespace + delimiter + namespace$1, {
debug.extend = function(namespace, delimiter = ":") {
return createDebug(this.namespace + delimiter + namespace, {
useColors: this.useColors,

@@ -106,4 +123,4 @@ color: this.color,

let skips = [];
function enable(namespaces$1) {
globalNamespaces = namespaces$1;
function enable(namespaces) {
globalNamespaces = namespaces;
names = [];

@@ -115,7 +132,13 @@ skips = [];

}
/**
* Disable debug output.
*/
function disable() {
const namespaces$1 = [...names, ...skips.map((namespace) => `-${namespace}`)].join(",");
const namespaces = [...names, ...skips.map((namespace) => `-${namespace}`)].join(",");
enable("");
return namespaces$1;
return namespaces;
}
/**
* Returns true if the given mode name is enabled, false otherwise.
*/
function enabled(name) {

@@ -126,2 +149,3 @@ for (const skip of skips) if (matchesTemplate(name, skip)) return false;

}
//#endregion
export { namespaces as a, enabled as i, disable as n, humanize as o, enable as r, selectColor as s, createDebug as t };

@@ -11,2 +11,2 @@ import { a as Debugger, i as DebugOptions, n as enabled, o as Formatters, r as namespaces, s as InspectOptions, t as disable } from "./core.js";

//#endregion
export { DebugOptions, Debugger, Formatters, InspectOptions, createDebug, disable, enable, enabled, namespaces };
export { type DebugOptions, type Debugger, type Formatters, type InspectOptions, createDebug, disable, enable, enabled, namespaces };
import { a as namespaces, i as enabled, n as disable, o as humanize, r as enable$1, s as selectColor, t as createDebug$1 } from "./core.js";
import { isatty } from "node:tty";
import { formatWithOptions, inspect } from "node:util";
//#region src/node.ts
const colors = process.stderr.getColorDepth && process.stderr.getColorDepth() > 2 ? [

@@ -99,3 +100,6 @@ 20,

return obj;
}, {});
}, Object.create(null));
/**
* Is stdout a TTY? Colored output is enabled when `true`.
*/
function useColors() {

@@ -108,5 +112,8 @@ return "colors" in inspectOpts ? Boolean(inspectOpts.colors) : isatty(process.stderr.fd);

}
/**
* Adds ANSI color escape codes if enabled.
*/
function formatArgs(diff, args) {
const { namespace: name, useColors: useColors$1 } = this;
if (useColors$1) {
const { namespace: name, useColors } = this;
if (useColors) {
const c = this.color;

@@ -126,2 +133,5 @@ const colorCode = `\u001B[3${c < 8 ? c : `8;5;${c}`}`;

formatters: {
/**
* Map %o to `util.inspect()`, all on a single line.
*/
o(v) {

@@ -131,2 +141,5 @@ this.inspectOpts.colors = this.useColors;

},
/**
* Map %O to `util.inspect()`, allowing multiple lines if needed.
*/
O(v) {

@@ -146,11 +159,16 @@ this.inspectOpts.colors = this.useColors;

}
function save(namespaces$1) {
if (namespaces$1) process.env.DEBUG = namespaces$1;
function save(namespaces) {
if (namespaces) process.env.DEBUG = namespaces;
else delete process.env.DEBUG;
}
function enable(namespaces$1) {
save(namespaces$1);
enable$1(namespaces$1);
/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
*/
function enable(namespaces) {
save(namespaces);
enable$1(namespaces);
}
enable$1(process.env.DEBUG || "");
//#endregion
export { createDebug, disable, enable, enabled, namespaces };
{
"name": "obug",
"version": "2.1.1",
"type": "module",
"version": "2.1.2",
"description": "A lightweight JavaScript debugging utility, forked from debug, featuring TypeScript and ESM support.",
"type": "module",
"author": "Kevin Deng <sxzz@sxzz.moe>",
"license": "MIT",
"funding": [
"https://github.com/sponsors/sxzz",
"https://opencollective.com/debug"
],
"homepage": "https://github.com/sxzz/obug#readme",
"bugs": {
"url": "https://github.com/sxzz/obug/issues"
},
"repository": {

@@ -15,13 +17,5 @@ "type": "git",

},
"author": "Kevin Deng <sxzz@sxzz.moe>",
"funding": [
"https://github.com/sponsors/sxzz",
"https://opencollective.com/debug"
],
"files": [
"dist"
],
"main": "./dist/node.js",
"module": "./dist/node.js",
"types": "./dist/browser.d.ts",
"bugs": {
"url": "https://github.com/sxzz/obug/issues"
},
"exports": {

@@ -34,23 +28,33 @@ ".": {

},
"main": "./dist/node.js",
"module": "./dist/node.js",
"unpkg": "./dist/browser.min.js",
"jsdelivr": "./dist/browser.min.js",
"types": "./dist/browser.d.ts",
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=12.20.0"
},
"devDependencies": {
"@sxzz/eslint-config": "^7.3.0",
"@sxzz/prettier-config": "^2.2.5",
"@types/debug": "^4.1.12",
"@types/node": "^24.10.1",
"@vitest/browser-playwright": "^4.0.10",
"@vitest/coverage-v8": "^4.0.10",
"bumpp": "^10.3.1",
"@sxzz/eslint-config": "^8.1.0",
"@sxzz/prettier-config": "^2.3.1",
"@types/debug": "^4.1.13",
"@types/node": "^25.9.1",
"@typescript/native-preview": "7.0.0-dev.20260603.1",
"@vitest/browser-playwright": "^4.1.8",
"@vitest/coverage-v8": "^4.1.8",
"bumpp": "^11.1.0",
"debug": "^4.4.3",
"eslint": "^9.39.1",
"playwright": "^1.56.1",
"prettier": "^3.6.2",
"tsdown": "^0.16.5",
"typescript": "^5.9.3",
"vite": "^7.2.2",
"vitest": "^4.0.10"
"eslint": "^10.4.1",
"playwright": "^1.60.0",
"prettier": "^3.8.3",
"tsdown": "^0.22.1",
"typescript": "^6.0.3",
"vite": "^8.0.16",
"vitest": "^4.1.8"
},

@@ -66,3 +70,3 @@ "prettier": "@sxzz/prettier-config",

"play": "vite playground",
"typecheck": "tsc --noEmit",
"typecheck": "tsgo --noEmit",
"format": "prettier --cache --write .",

@@ -69,0 +73,0 @@ "release": "bumpp"

+145
-0

@@ -5,2 +5,3 @@ # obug

[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![JSR][jsr-badge-src]][jsr-badge-href]
[![Unit Test][unit-test-src]][unit-test-href]

@@ -75,2 +76,144 @@

## How to use
### Enabling debug output
obug picks up enabled namespaces from the `DEBUG` environment variable in Node.js, or from `localStorage.debug` in browsers.
In Node.js:
```bash
DEBUG=app:* node app.js
```
On Windows (CMD):
```cmd
set DEBUG=app:* & node app.js
```
On Windows (PowerShell):
```powershell
$env:DEBUG='app:*'; node app.js
```
In the browser, set the value in DevTools and refresh the page:
```js
localStorage.debug = 'app:*'
```
### Wildcards and exclusion
The `*` character is a wildcard. Suppose your library has debuggers named `connect:bodyParser`, `connect:compress`, and `connect:session` — instead of listing each one, use `DEBUG=connect:*`. Use `DEBUG=*` to enable everything.
Exclude namespaces by prefixing them with `-`:
```bash
DEBUG=*,-connect:* node app.js
```
### Namespace conventions
If you're using obug in a library, prefix your namespaces with the library name and use `:` to separate features (e.g. `connect:bodyParser`). This lets users opt into the parts they care about without guessing names.
### Extending a namespace
Use `.extend()` to create a sub-namespace that inherits options from its parent:
```ts
const log = createDebug('auth')
const logSign = log.extend('sign') // 'auth:sign'
const logLogin = log.extend('login') // 'auth:login'
log('hello') // auth hello
logSign('hello') // auth:sign hello
logLogin('hello') // auth:login hello
```
### Formatters
obug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting. Built-in formatters:
| Formatter | Representation |
| --------- | -------------------------------------------------- |
| `%O` | Pretty-print an Object on multiple lines. |
| `%o` | Pretty-print an Object all on a single line. |
| `%%` | Single percent sign. Does not consume an argument. |
Other format specifiers supported by Node's `util.format` (`%s`, `%d`, `%j`, …) and the browser console pass through to the underlying logger.
#### Custom formatters
Register custom formatters via the `formatters` option. For example, to render a `Buffer` as hex with `%h`:
```ts
const debug = createDebug('foo', {
formatters: {
h: (v) => v.toString('hex'),
},
})
debug('this is hex: %h', Buffer.from('hello world'))
// foo this is hex: 68656c6c6f20776f726c64 +0ms
```
### Dynamic enable / disable
You can toggle namespaces at runtime:
```ts
import { disable, enable, enabled, namespaces } from 'obug'
enable('app:*')
console.log(enabled('app:server')) // true
const previous = disable()
console.log(enabled('app:server')) // false
// Restore later
enable(previous)
```
`namespaces()` returns the string of currently enabled namespaces. Note that calling `enable()` overrides the value initially read from `DEBUG`.
### Checking whether a debugger is enabled
Guard expensive work behind the `enabled` property:
```ts
const debug = createDebug('http')
if (debug.enabled) {
// expensive computation only when enabled
}
```
You can also force the state by assigning to `debug.enabled` directly.
### Custom output
By default, obug writes to `stderr` in Node.js and to the console in browsers. Override the `log` option to redirect output per-namespace:
```ts
const log = createDebug('app:log', {
log: console.log,
})
const error = createDebug('app:error') // still goes to stderr
```
### Environment variables (Node.js)
| Name | Purpose |
| ------------------- | ----------------------------------------------- |
| `DEBUG` | Enables/disables specific debugging namespaces. |
| `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY). |
| `DEBUG_COLORS` | Whether to use colors in the debug output. |
| `DEBUG_DEPTH` | Object inspection depth. |
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
Variables prefixed with `DEBUG_` are converted to camelCase keys on the options object passed to Node's [`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options) for the `%o` / `%O` formatters.
## Original Authors

@@ -109,1 +252,3 @@

[unit-test-href]: https://github.com/sxzz/obug/actions/workflows/unit-test.yml
[jsr-badge-src]: https://jsr.io/badges/@sxzz/obug
[jsr-badge-href]: https://jsr.io/@sxzz/obug