or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdjson-parser.mdraw-parser.mdtext-parser.mdurlencoded-parser.md
tile.json

raw-parser.mddocs/

Raw Parser

Middleware for parsing raw request bodies as Buffer objects. Parses all bodies as binary data and only processes requests where the Content-Type header matches the type option.

Capabilities

Raw Parser Factory

Creates middleware that parses raw bodies and populates req.body with a Buffer containing the raw request data.

/**
 * Create middleware to parse raw bodies as Buffer
 * @param {object} [options] - Configuration options
 * @returns {function} Express middleware function
 */
function raw(options?: {
  inflate?: boolean;
  limit?: number | string;
  type?: string | string[] | ((req: IncomingMessage) => boolean);
  verify?: (req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string) => void;
}): (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;

Options

inflate

  • Type: boolean
  • Default: true
  • Description: When true, deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected

limit

  • Type: number | string
  • Default: '100kb'
  • Description: Controls the maximum request body size. If a number, specifies bytes; if a string, passed to the bytes library for parsing

type

  • Type: string | string[] | function
  • Default: 'application/octet-stream'
  • Description: Determines what media type the middleware will parse. Can be an extension name, MIME type, MIME type with wildcard, or function

verify

  • Type: function
  • Default: undefined
  • Description: Called as verify(req, res, buf, encoding) where buf is the raw request body Buffer. Can abort parsing by throwing an error

Usage Examples

Basic raw parsing:

const bodyParser = require('body-parser');
const express = require('express');

const app = express();

// Parse raw bodies with default options
app.use(bodyParser.raw());

app.post('/upload', (req, res) => {
  console.log(req.body); // <Buffer 89 50 4e 47 0d 0a 1a 0a ...>
  console.log(req.body.length); // Size in bytes
  res.send(`Received ${req.body.length} bytes`);
});

File upload handling:

app.use(bodyParser.raw({
  limit: '50mb',
  type: 'image/*'
}));

app.post('/image', (req, res) => {
  const imageBuffer = req.body;
  
  // Save or process the image buffer
  require('fs').writeFile('uploaded-image.png', imageBuffer, (err) => {
    if (err) throw err;
    res.send('Image saved');
  });
});

Binary data processing:

app.use(bodyParser.raw({
  type: 'application/octet-stream',
  limit: '10mb'
}));

app.post('/binary', (req, res) => {
  const buffer = req.body;
  
  // Process binary data
  const header = buffer.slice(0, 4);
  const data = buffer.slice(4);
  
  console.log('Header:', header.toString('hex'));
  console.log('Data length:', data.length);
  
  res.json({
    headerHex: header.toString('hex'),
    dataLength: data.length
  });
});

Custom type matching:

app.use(bodyParser.raw({
  type: (req) => {
    // Accept any binary content type
    return req.headers['content-type'] && 
           (req.headers['content-type'].startsWith('application/') ||
            req.headers['content-type'].startsWith('image/') ||
            req.headers['content-type'].startsWith('video/'));
  }
}));

With verification:

app.use(bodyParser.raw({
  verify: (req, res, buf, encoding) => {
    // Verify the buffer contains expected data
    if (buf.length === 0) {
      throw new Error('Empty buffer not allowed');
    }
    
    // Check for specific file signatures
    const pngSignature = Buffer.from([0x89, 0x50, 0x4E, 0x47]);
    if (!buf.slice(0, 4).equals(pngSignature)) {
      throw new Error('Invalid PNG signature');
    }
  },
  type: 'image/png'
}));

Processing with streams:

const crypto = require('crypto');

app.use(bodyParser.raw({
  limit: '100mb',
  verify: (req, res, buf, encoding) => {
    // Calculate hash while verifying
    const hash = crypto.createHash('sha256');
    hash.update(buf);
    req.fileHash = hash.digest('hex');
  }
}));

app.post('/secure-upload', (req, res) => {
  const buffer = req.body;
  const hash = req.fileHash;
  
  console.log(`Received ${buffer.length} bytes with hash: ${hash}`);
  res.json({ size: buffer.length, hash });
});

Use Cases

File Uploads

  • Handle binary file uploads when not using multipart forms
  • Process image, video, audio, or document uploads
  • Work with raw file data for custom processing

Binary Protocols

  • Handle custom binary protocol data
  • Process protocol buffers or other binary formats
  • Work with embedded system communications

Webhook Data

  • Receive binary payloads from webhooks
  • Process signed or encrypted binary data
  • Handle custom data formats

Error Handling

The raw parser can throw several types of errors:

  • 400 Bad Request: General parsing error
  • 413 Payload Too Large: Body exceeds size limit
  • 415 Unsupported Media Type: Unsupported content encoding
app.use(bodyParser.raw());

app.use((err, req, res, next) => {
  if (err.type === 'entity.too.large') {
    return res.status(413).send('File too large');
  }
  if (err.type === 'encoding.unsupported') {
    return res.status(415).send('Unsupported content encoding');
  }
  next(err);
});