Skip to content

Commit

Permalink
Re-export everything from multipart-parser/node
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Dec 7, 2024
1 parent 61a96d2 commit 70261fd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions packages/multipart-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This is the changelog for [`multipart-parser`](https://github.com/mjackson/remix-the-web/tree/main/packages/multipart-parser). It follows [semantic versioning](https://semver.org/).

## HEAD

- Re-export everything from `multipart-parser/node` because we have a separate bundle for Node.js now, so everything should be imported from there when using it.

## v0.7.0 (2024-11-14)

- Added CommonJS build
Expand Down
3 changes: 1 addition & 2 deletions packages/multipart-parser/examples/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as fs from 'node:fs';
import * as http from 'node:http';
import tmp from 'tmp';

import { MultipartParseError } from '@mjackson/multipart-parser';
import { parseMultipartRequest } from '@mjackson/multipart-parser/node';
import { MultipartParseError, parseMultipartRequest } from '@mjackson/multipart-parser/node';

const PORT = 3000;

Expand Down
12 changes: 6 additions & 6 deletions packages/multipart-parser/src/lib/multipart.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ import {
/**
* Returns true if the given request is a multipart request.
*/
export function isMultipartRequest(request: http.IncomingMessage): boolean {
let contentType = request.headers['content-type'];
export function isMultipartRequest(req: http.IncomingMessage): boolean {
let contentType = req.headers['content-type'];
return contentType != null && /^multipart\//i.test(contentType);
}

/**
* Parse a multipart Node.js request and yield each part as a `MultipartPart` object.
*/
export async function* parseMultipartRequest(
request: http.IncomingMessage,
req: http.IncomingMessage,
options?: MultipartParserOptions,
): AsyncGenerator<MultipartPart> {
if (!isMultipartRequest(request)) {
if (!isMultipartRequest(req)) {
throw new MultipartParseError('Request is not a multipart request');
}

let boundary = getMultipartBoundary(request.headers['content-type']!);
let boundary = getMultipartBoundary(req.headers['content-type']!);
if (!boundary) {
throw new MultipartParseError('Invalid Content-Type header: missing boundary');
}

yield* parseMultipart(request, boundary, options);
yield* parseMultipart(req, boundary, options);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions packages/multipart-parser/src/multipart-parser.node.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
// Re-export all core functionality
export {
getMultipartBoundary,
MultipartParseError,
type MultipartParserOptions,
MultipartParser,
MultipartPart,
} from './lib/multipart.ts';

// Export Node.js-specific functionality
export { isMultipartRequest, parseMultipartRequest, parseMultipart } from './lib/multipart.node.ts';

0 comments on commit 70261fd

Please sign in to comment.