Skip to content

dcmjs-org/dicer

Repository files navigation

@dcmjs/dicer

A small, dependency-light streaming multipart parser for Node.js, written in TypeScript. It is a maintained fork of dicer by Brian White, modernized for ESM/TypeScript and focused on the multipart/related payloads used by DICOMweb (e.g. STOW-RS) — while remaining a general-purpose multipart parser usable in OHIF, dcmjs, and any Node service.

Install

npm install @dcmjs/dicer

The only runtime dependency is streamsearch.

Usage

Dicer is a Writable stream. Pipe a multipart body into it and listen for part events; each part is a Readable stream that emits a header event with the parsed headers.

import Dicer from '@dcmjs/dicer';

const dicer = new Dicer({ boundary: 'myboundary' });

dicer.on('part', (part) => {
  part.on('header', (headers) => {
    // headers: Record<string, string[]>, e.g. { 'content-type': ['application/dicom'] }
  });

  const chunks: Buffer[] = [];
  part.on('data', (chunk) => chunks.push(chunk));
  part.on('end', () => {
    const body = Buffer.concat(chunks);
    // ...handle the completed part
  });
});

dicer.on('finish', () => {
  // all parts parsed
});

dicer.on('error', (err) => {
  // malformed multipart data, oversized headers, early termination, etc.
});

request.pipe(dicer); // e.g. an incoming HTTP request stream

The boundary passed to Dicer is the raw boundary token (without the leading --). When parsing an HTTP request, extract it from the Content-Type header, for example with the content-type package.

Configuration

new Dicer({
  boundary,            // string: the multipart boundary token (required unless headerFirst)
  headerFirst,         // boolean: parse a leading header block before the first boundary
  partHwm,             // number: highWaterMark for each part Readable
  maxHeaderPairs,      // number: max header pairs per part (default 2000)
  // ...plus any Node WritableOptions
});

Part headers are capped at 64 KB; exceeding that emits an error.

Exports

import Dicer, { Dicer as DicerNamed, HeaderParser, PartStream } from '@dcmjs/dicer';
import type { DicerConfig, HeaderParserConfig, MultipartHeaders } from '@dcmjs/dicer';

Development

pnpm install
pnpm run build      # tsc → dist/ (ESM + .d.ts)
pnpm test           # vitest
pnpm run lint       # eslint
pnpm run typecheck  # tsc --noEmit

Credits

This package is a maintained fork of the original dicer by Brian White. The core parsing logic derives from that project; this fork modernizes it for TypeScript/ESM and the DICOMweb use case. Many thanks to the upstream author and contributors.

License

MIT — see LICENSE. The MIT terms of the original dicer project are preserved.

About

Dicer library for streaming parsing of Multipart data

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors