export class AppError extends Error {
public readonly statusCode: number;
public readonly code: string;
public readonly isOperational: boolean;
constructor(message: string, statusCode = 500, code = 'INTERNAL_ERROR', isOperational = true) {
const crypto = require('crypto');
function computeSignature(secret, timestamp, payload) {
return crypto
.createHmac('sha256', secret)
.update(`${timestamp}.${payload}`, 'utf8')
const express = require('express');
const { toCsv } = require('./csvSerializer');
const { fetchOrders } = require('./orderRepository');
const router = express.Router();
import { Response } from 'express';
type Client = { id: number; res: Response };
const clients = new Map<string, Set<Client>>();
let nextId = 1;
const { Pool } = require('pg');
const Cursor = require('pg-cursor');
const pool = new Pool();
async function* streamUsers({ since, batchSize = 500 }) {
const sharp = require('sharp');
const DEFAULTS = {
maxWidth: 1600,
maxHeight: 1600,
format: 'webp',
-- KEYS[1] = current window key, KEYS[2] = previous window key
-- ARGV: limit, window_ms, elapsed_ms
local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local elapsed = tonumber(ARGV[3])
CREATE TABLE idempotency_keys (
id BIGSERIAL PRIMARY KEY,
idempotency_key TEXT NOT NULL,
request_path TEXT NOT NULL,
request_fingerprint TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'in_progress',
const rawOrigins = process.env.ALLOWED_ORIGINS ?? "";
export const allowedOrigins = new Set(
rawOrigins
.split(",")
.map((o) => o.trim())
class TokenBucket {
constructor(capacity, refillPerSecond) {
this.capacity = capacity;
this.refillPerMs = refillPerSecond / 1000;
this.tokens = capacity;
this.lastRefill = Date.now();
import sharp from 'sharp';
export interface Variant {
name: string;
width: number;
quality: number;
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';