Skip to content

About

A Pastebin clone with expiration features

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Pastebin Lite

A simple pastebin application for sharing text with optional expiration constraints.

Deployment

Live URL: https://pastebin-lite-woad.vercel.app

Repository: https://github.com/mindmonkey2/pastebin-lite

Features

  • Create text pastes with shareable URLs
  • Optional time-based expiration (TTL)
  • Optional view-count limits
  • Automatic paste deletion when constraints are met
  • Clean, simple UI for creating and viewing pastes

Tech Stack

  • Framework: Next.js 16 (Pages Router)
  • Deployment: Vercel
  • Persistence: Redis (Upstash)
  • ID Generation: nanoid
  • Runtime: Node.js

Running Locally

Prerequisites

  • Node.js 18+ installed
  • Redis instance (or use environment variables from Vercel)

Setup

Clone the repository: git clone https://github.com/mindmonkey2/pastebin-lite.git cd pastebin-lite

Install dependencies:

bash npm install

Set up environment variables in .env.local:

text REDIS_URL=your_redis_connection_url TEST_MODE=0

Run the development server:

bash npm run dev

Open http://localhost:3000 in your browser

Persistence Layer

This application uses Redis (Upstash) for data persistence. Redis is a serverless, in-memory data store that:

Survives across serverless function invocations

Provides automatic scaling and low-latency access globally

Supports native key expiration for TTL functionality

Offers seamless integration with Vercel deployment

Why Redis?

Serverless-native: No infrastructure management required

Built-in TTL: Redis natively supports key expiration

Fast: In-memory storage provides sub-millisecond response times

Simple: Key-value operations perfect for paste storage

Reliable: Managed service with automatic backups

Design Decisions Time-based Expiration

Implemented using both application-level checks and Redis TTL. The Redis EXPIRE command ensures keys are automatically deleted after the specified time, while application checks handle deterministic testing with the x-test-now-ms header for automated test suites. View Counting

View counts increment only on API fetch (/api/pastes/:id). HTML page views (/p/:id) do not increment the counter to prevent accidental view consumption when users navigate to the page without fetching content programmatically. Deterministic Testing

When TEST_MODE=1 environment variable is set, the application respects the x-test-now-ms request header for expiry calculations, enabling automated testing with controlled time values. This allows test suites to verify time-based expiration without waiting for real time to pass. ID Generation

Uses nanoid with 10-character IDs providing collision-resistant unique identifiers. With 10 characters from nanoid's default alphabet (A-Za-z0-9_-), this provides ~3 million years to reach 1% collision probability at 1000 IDs per hour. Content Safety

Paste content is rendered safely using React's built-in XSS protection through automatic escaping. All user-submitted content is treated as plain text, preventing script execution and other injection attacks. Connection Management

Each API route creates a new Redis connection and properly closes it after use. This prevents connection leaks in serverless environments where function instances are ephemeral. API Routes Health Check

text GET /api/healthz

Returns server health status and database connectivity.

Response:

json { "ok": true }

Create Paste

text POST /api/pastes

Request Body:

json { "content": "string (required, non-empty)", "ttl_seconds": 60, "max_views": 5 }

Response:

json { "id": "T9u1D_ZSKc", "url": "https://pastebin-lite-woad.vercel.app/p/T9u1D_ZSKc" }

Validation:

content: Required, must be non-empty string

ttl_seconds: Optional, must be integer ≥ 1

max_views: Optional, must be integer ≥ 1

Fetch Paste (API)

text GET /api/pastes/:id

Response:

json { "content": "Hello from Pastebin!", "remaining_views": 4, "expires_at": "2026-01-02T10:47:14.570Z" }

Notes:

Each successful API fetch counts as a view

Returns 404 if paste not found, expired, or view limit exceeded

remaining_views is null if unlimited

expires_at is null if no TTL set

View Paste (HTML)

text GET /p/:id

Returns HTML page displaying the paste content. Returns 404 if unavailable. Request/Response Examples Create a paste with TTL

bash curl -X POST https://pastebin-lite-woad.vercel.app/api/pastes
-H "Content-Type: application/json"
-d '{"content":"Hello World","ttl_seconds":3600}'

Create a paste with view limit

bash curl -X POST https://pastebin-lite-woad.vercel.app/api/pastes
-H "Content-Type: application/json"
-d '{"content":"Secret message","max_views":1}'

Fetch paste

bash curl https://pastebin-lite-woad.vercel.app/api/pastes/T9u1D_ZSKc

Error response (invalid input)

bash curl -X POST https://pastebin-lite-woad.vercel.app/api/pastes
-H "Content-Type: application/json"
-d '{"content":"","ttl_seconds":60}'

Response: 400

{ "error": "content is required and must be a non-empty string" }

Testing

The application supports deterministic time testing for automated test suites:

bash

Set TEST_MODE environment variable

export TEST_MODE=1

Use x-test-now-ms header to control current time (milliseconds since epoch)

curl -H "x-test-now-ms: 1735812000000"
https://pastebin-lite-woad.vercel.app/api/pastes/test-id

This allows testing time-based expiration without waiting for real time to pass. Project Structure

text pastebin-lite/ ├── pages/ │ ├── api/ │ │ ├── healthz.js # Health check endpoint │ │ └── pastes/ │ │ ├── index.js # Create paste │ │ └── [id].js # Fetch paste │ ├── p/ │ │ └── [id].js # View paste HTML │ └── index.js # Home page ├── lib/ │ └── time.js # Time utilities for deterministic testing ├── .env.local # Environment variables (local) ├── package.json └── README.md

Environment Variables

REDIS_URL: Redis connection string (automatically set by Vercel)

TEST_MODE: Enable deterministic time testing (0 or 1)

Built as a take-home assignment demonstrating serverless application development with Next.js and Redis.

About

A Pastebin clone with expiration features

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages