npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@swissjs/security

v0.1.13

Published

Security primitives for SwissJS

Readme

@swissjs/security

Security engine for SwissJS. Implements the SecurityGateway interface consumed by @swissjs/core, plus rate limiting, JSON schema validation, and security middleware.


Installation

pnpm add @swissjs/security

Wire it at app startup before any components mount:

import { setSecurityGateway } from '@swissjs/core';
import { InMemorySecurityEngine } from '@swissjs/security';

setSecurityGateway(new InMemorySecurityEngine());

InMemorySecurityEngine

Reference implementation of SecurityGateway. Stores policies, audit log, and rate limit state in memory.

import { InMemorySecurityEngine } from '@swissjs/security';

const engine = new InMemorySecurityEngine({
  windowMs: 60_000,    // rate limit window in ms
  maxRequests: 100,    // max requests per window per key
});

// Register a policy
engine.registerPolicy({
  id: 'allow-network',
  target: 'network',
  effect: 'allow',
  conditions: [],
});

// Evaluate a capability check
const allowed = engine.evaluate('network', { layer: 'component', componentName: 'UserList' });

// Read the audit log
const log = engine.getAuditLog();

SecurityValidator

JSON Schema validation for payloads.

import { SecurityValidator } from '@swissjs/security';

const validator = new SecurityValidator();

validator.addSchema('CreateUser', {
  type: 'object',
  required: ['name', 'email'],
  properties: {
    name: { type: 'string', minLength: 1 },
    email: { type: 'string', format: 'email' },
  },
  additionalProperties: false,
});

const result = validator.validate('CreateUser', body);
if (!result.ok) {
  console.error('Validation failed:', result.reasons);
}

RateLimiter

import { RateLimiter } from '@swissjs/security';

const limiter = new RateLimiter({ windowMs: 60_000, maxRequests: 50 });

const result = limiter.check('user-123');
if (!result.allowed) {
  console.warn('Rate limit exceeded, retry after:', result.retryAfter);
}

Middleware (Express-compatible)

import {
  createSecurityHeadersMiddleware,
  createRateLimitMiddleware,
  createValidationMiddleware,
  createPolicyValidationMiddleware,
} from '@swissjs/security';

// Security headers (HSTS, CSP, X-Frame-Options, etc.)
app.use(createSecurityHeadersMiddleware({
  contentSecurityPolicy: "default-src 'self'",
}));

// Rate limiting per route
app.use('/api', createRateLimitMiddleware({ maxRequests: 100, windowMs: 60_000 }));

// Request body validation
app.post('/api/users',
  createValidationMiddleware({ schema: 'CreateUser', validator }),
  handler,
);

// Policy enforcement
app.use('/api/admin',
  createPolicyValidationMiddleware({ capability: 'admin', engine }),
  adminRouter,
);

Types

import type {
  SecurityGateway,
  SecurityContext,
  SecurityPolicy,
  ValidationResult,
  AuditEntry,
  AuditFilter,
  AuditConfig,
  JSONSchema,
  SecurityError,
  SecurityEvent,
  SecurityMiddlewareOptions,
  RateLimitConfig,
  RateLimitResult,
  ValidationRule,
  CompiledSchema,
} from '@swissjs/security';

Custom gateway

For production, implement SecurityGateway to integrate with your auth system:

import { setSecurityGateway } from '@swissjs/core';
import type { SecurityGateway, SecurityContext, ValidationResult } from '@swissjs/security';

class ProductionGateway implements SecurityGateway {
  evaluate(target: string, ctx: SecurityContext): boolean {
    return authService.hasPermission(ctx.userId!, target);
  }

  audit(entry: { action: string; target?: string; success?: boolean }) {
    auditService.log(entry);
  }

  auditPlugin(plugin: { name: string; requiredCapabilities?: string[] }): ValidationResult {
    return { ok: true };
  }
}

setSecurityGateway(new ProductionGateway());