Skip to content

Commit

Permalink
Fixed types
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed May 17, 2023
1 parent aa12fda commit 2958fa9
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 39 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ A regular expression that can be used to validate a boundary string.
A regular expression that can be used to extract a boundary string from a
`Content-Type` header.

#### `encodeMultipartMessage(boundary: string, msg: TDecodedMultipartMessage[]): ReadableStream<ArrayBuffer>`
#### `encodeMultipartMessage(boundary: string, msg: AsyncIterable<TDecodedMultipartMessage>): ReadableStream<ArrayBuffer>`

This function takes a boundary string and an array of messages as arguments and returns a `ReadableStream` that can be read to obtain a multipart message.

Expand All @@ -65,9 +65,9 @@ This function takes a boundary string and an array of messages as arguments and
* `headers`: a `Headers` object containing the headers of the current part
* `body` (optional): The body of the current part, or `null` if the part is
empty. It can be any of the following types: `ArrayBuffer`, `Blob`, `ReadableStream` or any typed array, such as `Uint8Array`.
* `parts` (optional): An array of one element or more of the same type
(`TDecodedMultipartMessage`), for nested messages. If both `body` and
`parts` are specified, `body` takes precedence.
* `parts` (optional): An async or sync iterable of one element or more of
the same type (`TDecodedMultipartMessage`), for nested messages. If both
`body` and `parts` are specified, `body` takes precedence.

### Example

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exact-realty/multipart-parser",
"version": "1.0.4",
"version": "1.0.6",
"description": "TypeScript streaming parser for MIME multipart messages",
"main": "dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -29,6 +29,7 @@
},
"author": "Exact Realty Limited",
"license": "ISC",
"keywords": ["form-data", "formdata", "mime", "mime", "multipart", "multipart/form-data", "multipart/mixed", "multipart/related", "parser", "rfc2046", "rfc2388", "rfc7568"],
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.0.0",
Expand Down
25 changes: 18 additions & 7 deletions src/encodeMultipartMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { boundaryMatchRegex } from './lib/boundaryRegex';
import createBufferStream from './lib/createBufferStream';
import { boundaryMatchRegex } from './lib/boundaryRegex.js';
import createBufferStream from './lib/createBufferStream.js';
import type { TTypedArray } from './types/index.js';

type TIterable<T> = AsyncIterable<T> | Iterable<T>;

export type TDecodedMultipartMessage = {
headers: Headers;
body?: TTypedArray | ArrayBuffer | Blob | ReadableStream | null;
parts?: TDecodedMultipartMessage[];
parts?: TIterable<TDecodedMultipartMessage>;
};

const textEncoder = new TextEncoder();
Expand Down Expand Up @@ -49,17 +52,20 @@ const pipeToOptions = {

async function* asyncEncoderGenerator(
boundary: string,
msg: TDecodedMultipartMessage[],
msg: TIterable<TDecodedMultipartMessage>,
ws: WritableStream,
): AsyncGenerator<void> {
const encodedBoundary = textEncoder.encode(`\r\n--${boundary}`);

if (!Array.isArray(msg) || msg.length < 1) {
if (Array.isArray(msg) && msg.length < 1) {
await ws.abort(Error('At least one part is required'));
return;
}

for (const part of msg) {
let count = 0;

for await (const part of msg) {
count++;
let subBoundary: string | undefined;
let partContentType: string | null | undefined;

Expand Down Expand Up @@ -166,13 +172,18 @@ async function* asyncEncoderGenerator(
}
}

if (!count) {
await ws.abort(Error('At least one part is required'));
return;
}

const encodedEndBoundary = textEncoder.encode(`\r\n--${boundary}--`);
await createBufferStream(encodedEndBoundary).pipeTo(ws, pipeToOptions);
}

const encodeMultipartMessage = (
boundary: string,
msg: TDecodedMultipartMessage[],
msg: TIterable<TDecodedMultipartMessage>,
): ReadableStream<ArrayBuffer> => {
const transformStream = new TransformStream<ArrayBuffer>();

Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

export { default as encodeMultipartMessage } from './encodeMultipartMessage';
export type { TDecodedMultipartMessage } from './encodeMultipartMessage';
export { boundaryMatchRegex, boundaryRegex } from './lib/boundaryRegex';
export { default as parseMessage } from './parseMessage';
export type { TMessage } from './parseMessage';
export * from './parseMultipartMessage';
export { default } from './parseMultipartMessage';
export { default as encodeMultipartMessage } from './encodeMultipartMessage.js';
export type { TDecodedMultipartMessage } from './encodeMultipartMessage.js';
export { boundaryMatchRegex, boundaryRegex } from './lib/boundaryRegex.js';
export { default as parseMessage } from './parseMessage.js';
export type { TMessage } from './parseMessage.js';
export * from './parseMultipartMessage.js';
export { default } from './parseMultipartMessage.js';
2 changes: 2 additions & 0 deletions src/lib/createBufferStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import type { TTypedArray } from '../types/index.js';

const createBufferStream = <T extends TTypedArray | ArrayBuffer>(buffer: T) => {
const readableStream = new ReadableStream<T>({
pull(controller) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/findIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import type { TTypedArray } from '../types/index.js';

// Helper function to find the index of a Uint8Array within another Uint8Array
const findIndex = <T extends TTypedArray>(buffer: T, delimiter: T): number => {
outerLoop: for (let i = 0; i <= buffer.length - delimiter.length; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/mergeTypedArrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import type { TTypedArray } from '../types/index.js';

const mergeTypedArrays = <T extends TTypedArray>(
input0: T,
...input: T[]
Expand Down
2 changes: 1 addition & 1 deletion src/parseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import findIndex from './lib/findIndex';
import findIndex from './lib/findIndex.js';

const textDecoder = new TextDecoder();
const textEncoder = new TextEncoder();
Expand Down
11 changes: 6 additions & 5 deletions src/parseMultipartMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { boundaryMatchRegex, boundaryRegex } from './lib/boundaryRegex';
import createBufferStream from './lib/createBufferStream';
import findIndex from './lib/findIndex';
import mergeTypedArrays from './lib/mergeTypedArrays';
import parseMessage from './parseMessage';
import { boundaryMatchRegex, boundaryRegex } from './lib/boundaryRegex.js';
import createBufferStream from './lib/createBufferStream.js';
import findIndex from './lib/findIndex.js';
import mergeTypedArrays from './lib/mergeTypedArrays.js';
import parseMessage from './parseMessage.js';
import type { TTypedArray } from './types/index.js';

enum EState {
PREAMBLE,
Expand Down
25 changes: 25 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright © 2023 Exact Realty Limited.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

export type TTypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array;
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"allowJs": true,
"checkJs": true,
"lib": ["ES2019.object", "ES2019.array", "dom"],
"typeRoots": ["./node_modules/@types", "./types"]
"typeRoots": ["./node_modules/@types", "./@types"]
},
"ts-node": {
"experimentalResolver": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"]
Expand Down
11 changes: 0 additions & 11 deletions types/global/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,3 @@
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

declare type TTypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array;

0 comments on commit 2958fa9

Please sign in to comment.