node

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
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 { 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
sql
CREATE TABLE password_reset_tokens (
  id          BIGSERIAL PRIMARY KEY,
  user_id     BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  token_hash  TEXT NOT NULL,
  expires_at  TIMESTAMPTZ NOT NULL,
  used_at     TIMESTAMPTZ,

Password reset tokens: hash + expiry

security express authentication
by codesnips 3 tabs
typescript
import { QueryResultRow } from 'pg';
import { pool, Queryable } from './db';

export abstract class BaseRepository<T extends QueryResultRow> {
  protected abstract readonly table: string;

Repository pattern for DB access (small, pragmatic)

node postgres architecture
by codesnips 4 tabs