node

javascript
const jwt = require('jsonwebtoken');

const ACCESS_SECRET = process.env.JWT_ACCESS_SECRET;
const REFRESH_SECRET = process.env.JWT_REFRESH_SECRET;
const ISSUER = 'api.example.com';
const AUDIENCE = 'example-web';

Sign and Verify JWT Access Tokens in Express Auth Middleware

express jwt authentication
by codesnips 3 tabs
typescript
import { Queue } from "bullmq";
import { createHash } from "crypto";

export const connection = { host: "127.0.0.1", port: 6379 };

export interface ChargePayload {

BullMQ job idempotency via dedupe id

node redis background-jobs
by codesnips 3 tabs
yaml
name: CI

on:
  push:
    branches: [main]
  pull_request:

GitHub Actions: cache + tests + build

ci github-actions node
by codesnips 3 tabs
typescript
import { S3Client } from "@aws-sdk/client-s3";
import { createPresignedPost, PresignedPost } from "@aws-sdk/s3-presigned-post";
import { randomUUID } from "crypto";

const s3 = new S3Client({ region: process.env.AWS_REGION });
const BUCKET = process.env.UPLOAD_BUCKET!;

Pre-signed S3 upload from the browser

s3 security aws-sdk
by codesnips 3 tabs
typescript
import { WebSocketServer } from 'ws';
import type WebSocket from 'ws';

type ClientState = { topics: Set<string> };
const state = new WeakMap<WebSocket, ClientState>();

WebSocket server with topic subscriptions (ws)

node realtime websockets
by Mateo Rodriguez 1 tab
javascript
const SHUTDOWN_TIMEOUT_MS = 10_000;

function registerFatalHandlers({ logger, onFatal, exitCode = 1 }) {
  let shuttingDown = false;

  async function handleFatal(kind, error) {

Graceful Node.js Shutdown on uncaughtException and unhandledRejection

node reliability graceful-shutdown
by codesnips 3 tabs
typescript
import { Knex } from 'knex';

export interface RawDailyRow {
  day: string;
  order_count: string;
  gross_cents: string;

Aggregate Daily Order Totals Into a Report With Knex and a Formatter

knex postgres reporting
by codesnips 3 tabs
typescript
import { Request, Response } from 'express';
import { authenticate } from './auth.service';

export async function login(req: Request, res: Response): Promise<void> {
  const { email, password } = req.body ?? {};

Password hashing with Argon2

security node argon2
by codesnips 3 tabs
typescript
import { Pool } from "pg";

async function keyFor(pool: Pool, name: string): Promise<string> {
  const { rows } = await pool.query<{ key: string }>(
    "SELECT hashtextextended($1, 0) AS key",
    [name]

Postgres advisory lock for one-at-a-time work

postgres concurrency reliability
by codesnips 3 tabs
typescript
import { z } from "zod";

export const rowSchema = z.object({
  email: z.string().email(),
  name: z.string().min(1, "name is required"),
  age: z.coerce.number().int().min(0, "age must be >= 0"),

Parse a CSV Upload into Typed Rows with Per-Row Validation Errors

typescript csv validation
by codesnips 3 tabs
typescript
import { Prisma, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

type TxClient = Prisma.TransactionClient;

Prisma transaction with retries for serialization errors

prisma postgres concurrency
by codesnips 3 tabs
typescript
import jwt, { JwtPayload, SignOptions } from 'jsonwebtoken';

const SECRET = process.env.JWT_SECRET as string;
const ISSUER = 'auth.example.com';
const AUDIENCE = 'api.example.com';

Signing and Verifying JWT Access Tokens with Express Middleware

jwt express authentication
by codesnips 3 tabs