Exports
Realtime, timing, safety, and resilience helpers.
GitHub Packages docs for v0.4.0
A dependency-free JavaScript toolkit for realtime state, events, polling, safer logs, guarded fetch calls, rate limiting, and circuit-breaker recovery.
Realtime, timing, safety, and resilience helpers.
Simple ESM. No runtime dependencies.
Secret scan, tests, and dry-run before release.
Install
The package is scoped to `@shnwazdeveloper`, so npm must use GitHub Packages for that scope.
@shnwazdeveloper:registry=https://npm.pkg.github.com
gh auth refresh -h github.com -s read:packages
$token = gh auth token
npm config set "//npm.pkg.github.com/:_authToken" "$token" --location=user
npm install @shnwazdeveloper/shnwazdev@0.4.0
Quick start
Combine state, events, safe logging, rate limiting, and guarded fetch calls in app code, bots, dashboards, workers, and APIs.
import {
createEventBus,
createRateLimiter,
createRealtimeStore,
createSafeLogger,
safeFetch
} from "@shnwazdeveloper/shnwazdev";
const store = createRealtimeStore({ online: false });
const bus = createEventBus();
const logger = createSafeLogger(console);
const limiter = createRateLimiter({ limit: 10, interval: 60000 });
limiter.assert("user:shnwazdeveloper");
store.setState({ online: true });
bus.emit("status", store.getState());
const result = await safeFetch("https://api.github.com", {
allowedOrigins: ["https://api.github.com"],
timeout: 3000
});
logger.info("request finished", result.safeRequest);
API reference
Filter by area to scan the package quickly. Every API is dependency-free and exported from the package root.
Local publish/subscribe events with `on`, `once`, `off`, `emit`, and `emitAsync`.
Small state container with subscriptions, selectors, updates, and `waitFor`.
Repeated async work with `onData`, `onError`, `runOnce`, `until`, and backoff.
Live beat signals for bots, dashboards, workers, and connection monitors.
Abortable async delay that cleans up abort listeners on resolve and abort.
Wrap a promise and reject with `TimeoutError` if it takes too long.
Retry unstable async work with delay, backoff factor, and custom retry logic.
Delay frequent calls until input settles. Useful for search and resize events.
Limit high-frequency calls from scroll, pointer, and progress events.
Replace sensitive keys and token-like strings with `[REDACTED]`.
Find sensitive values and return masked previews without exposing the raw value.
Throw `SensitiveDataError` when a payload contains sensitive data.
Stringify circular data safely while redacting secrets.
Console-style logger that redacts every argument before writing.
Create masked secret previews with no raw characters shown by default.
Generate crypto-safe request IDs, trace IDs, and session-safe identifiers.
Compare strings without returning early at the first different character.
Prepare request or response headers for logs by redacting private fields.
Redact query params, credentials, and token-like values before logging URLs.
In-memory rate limiting by key with `consume`, `assert`, `reset`, and snapshots.
Open after repeated failures and recover through a half-open test call.
Origin-guarded fetch with timeout support and redacted request metadata.
Realtime
The realtime helpers are meant for dashboards, bots, small services, worker loops, browser apps, and local tools that need live updates without a heavy framework.
Timing
Timing helpers keep small apps predictable: abort delays, cap slow work, retry unstable operations, and reduce noisy UI event handlers.
await sleep(500);
await timeout(fetch("https://api.github.com"), 3000);
await retry(loadData, { retries: 3, delay: 500, factor: 2 });
Safety
Safety helpers are built around one rule: keep structure, remove secrets. They protect common keys like `token`, `password`, `authorization`, `cookie`, `secret`, `apiKey`, and private key fields.
const logger = createSafeLogger(console);
logger.info("request", {
user: "shnwazdeveloper",
token: "private-token"
});
Resilience
These helpers reduce repeated abuse, stop retry storms, and keep request logs useful without exposing tokens or private headers.
const result = await safeFetch("https://api.github.com", {
allowedOrigins: ["https://api.github.com"],
timeout: 3000
});
logger.info("request", result.safeRequest);
const limiter = createRateLimiter({ limit: 5, interval: 60000 });
limiter.assert("user:shnwazdeveloper");
const breaker = createCircuitBreaker(loadData, {
failureThreshold: 3,
recoveryTime: 30000
});
await breaker.execute();
Publishing
New versions publish when a version tag is pushed. The workflow installs, checks for secrets, runs tests, and publishes with GitHub's package token.
Troubleshooting
npm is using the wrong registry. Set the GitHub Packages scope registry.
npm config set "@shnwazdeveloper:registry" "https://npm.pkg.github.com" --location=user
npm needs a GitHub token with `read:packages`.
gh auth refresh -h github.com -s read:packages
$token = gh auth token
npm config set "//npm.pkg.github.com/:_authToken" "$token" --location=user
GitHub Packages does not republish the same version. Increase `package.json` and push a matching new tag.
Remove the secret from source/docs. Use placeholders that do not look like real tokens, then run `npm run safe:check` again.