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

isatec-jwt

v1.0.0

Published

Lib JWT minimalista, zero dependências, focada em performance e DX

Readme

Isatec-jwt

Lib JWT minimalista para Node.js. Zero dependências, ES Module, focada em performance e DX.

Instalação

npm install isatec-jwt

Requer Node.js >= 18.

Uso

import jwt from 'isatec-jwt'

// Gerar token
const token = jwt.encode({ id: 1, nome: 'Carlos' }, 'meu-secret', '30m')

// Decodificar e validar
try {
  const user = jwt.decode(token, 'meu-secret')
  console.log(user) // { id: 1, nome: 'Carlos' }
} catch (e) {
  console.error(e.message)
}

API

jwt.encode(payload, secret, exp?)

Gera um token JWT assinado com HMAC-SHA256.

| Parâmetro | Tipo | Padrão | Descrição | |---|---|---|---| | payload | object | — | Dados a serem codificados no token | | secret | string | — | Chave secreta para assinatura | | exp | string | "1h" | Tempo de expiração do token |

Formatos de expiração:

| Valor | Significado | |---|---| | "15s" | 15 segundos | | "30m" | 30 minutos | | "1h" | 1 hora (padrão) | | "7d" | 7 dias |

O encode adiciona automaticamente iat (data de criação) e exp (data de expiração) ao payload. Esses campos são removidos no decode.

Retorna: string — token JWT.

const token = jwt.encode({ id: 42, cargo: 'admin' }, 'secret', '2h')

jwt.decode(token, secret)

Decodifica um token JWT, valida a assinatura e checa a expiração.

| Parâmetro | Tipo | Descrição | |---|---|---| | token | string | Token JWT gerado pelo encode | | secret | string | Mesma chave secreta usada no encode |

Retorna: object — payload original (sem iat e exp).

Lança erros com mensagens em português:

| Mensagem | Quando | |---|---| | "Token mal formatado" | Token não tem 3 partes separadas por ponto | | "Algoritmo de assinatura não suportado" | Header com alg diferente de HS256 | | "Assinatura inválida" | Secret incorreto ou token alterado | | "Token sem campo de expiração" | Payload não contém exp | | "Token expirado" | Tempo de expiração já passou | | Formato de expiração inválido: "..." | exp com formato desconhecido no encode |

try {
  const user = jwt.decode(token, 'secret')
} catch (e) {
  if (e.message === 'Token expirado') {
    // redirecionar para login
  }
  if (e.message === 'Assinatura inválida') {
    // token adulterado
  }
}

Exemplo com Express

import express from 'express'
import jwt from 'isatec-jwt'

const app = express()
const SECRET = process.env.JWT_SECRET

app.post('/login', (req, res) => {
  const user = authenticate(req.body)
  if (!user) return res.status(401).json({ erro: 'Credenciais inválidas' })

  const token = jwt.encode({ id: user.id, email: user.email }, SECRET, '1d')
  res.json({ token })
})

app.get('/perfil', (req, res) => {
  try {
    const user = jwt.decode(req.headers.authorization?.slice(7), SECRET)
    res.json(user)
  } catch (e) {
    res.status(401).json({ erro: e.message })
  }
})

app.listen(3000)

Características

  • Zero dependências — usa apenas node:crypto
  • HS256 — HMAC com SHA-256
  • timingSafeEqual — proteção contra timing attacks na comparação de assinatura
  • Header cacheado — codificado uma única vez
  • ES Moduleimport/export nativo
  • JSDoc em português — autocomplete no editor sem build step

Testes

npm test