nodejs

javascript
function escapeCell(value) {
  if (value === null || value === undefined) return '';
  const str = String(value);
  if (/[",\n\r]/.test(str)) {
    return '"' + str.replace(/"/g, '""') + '"';
  }

Stream a Large CSV Export in Express with Backpressure and an Async Row Generator

express streaming csv
by codesnips 3 tabs
javascript
const MAX_LIMIT = 100;
const DEFAULT_LIMIT = 20;
const ALLOWED_ORDER = new Set(['asc', 'desc']);

function decodeCursor(raw) {
  const json = Buffer.from(raw, 'base64').toString('utf8');

Cursor-Based Pagination in Express With Query-Parsing Middleware

express pagination cursor-pagination
by codesnips 3 tabs
javascript
const multer = require('multer');

const ALLOWED_MIME = new Set(['image/jpeg', 'image/png', 'image/webp']);

function fileFilter(req, file, cb) {
  if (!ALLOWED_MIME.has(file.mimetype)) {

Upload and Resize User Avatars with Multer and Sharp in Express

express multer sharp
by codesnips 3 tabs
javascript
class ApiError extends Error {
  constructor(statusCode, message, code) {
    super(message);
    this.name = 'ApiError';
    this.statusCode = statusCode;
    this.code = code || null;

Centralized Async Error Handling in Express With asyncHandler and Error Middleware

express nodejs error-handling
by codesnips 4 tabs
javascript
class BatchLoader {
  constructor(batchFn, { cacheKeyFn = (k) => k } = {}) {
    this.batchFn = batchFn;
    this.cacheKeyFn = cacheKeyFn;
    this.cache = new Map();
    this.queue = [];

Coalescing Concurrent Reads with a DataLoader-Style Batch Loader in Node

dataloader batching graphql
by codesnips 3 tabs
javascript
const express = require('express');
const multer = require('multer');
const os = require('os');
const fs = require('fs/promises');
const { parseCsvStream } = require('./csvStreamParser');

Stream and Validate Large CSV Uploads Row-by-Row with Node Streams and Backpressure

nodejs streams csv
by codesnips 3 tabs
typescript
import { randomBytes } from 'crypto';
import { Request, Response, NextFunction } from 'express';

interface CspOptions {
  reportOnly?: boolean;
  reportUri?: string;

Content Security Policy headers (defense-in-depth)

security express csp
by codesnips 3 tabs
javascript
const fs = require('fs');
const readline = require('readline');

async function* streamCsvRows(filePath) {
  const stream = fs.createReadStream(filePath, { encoding: 'utf8' });
  const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });

Stream and Import a Large CSV File Line-by-Line with Node.js readline

nodejs streams readline
by codesnips 3 tabs