Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

export * as bytes from "https://deno.land/std@0.114.0/bytes/mod.ts";
export * as bytes from "https://deno.land/std@0.154.0/bytes/mod.ts";
export { readAll } from "https://deno.land/std@0.154.0/streams/conversion.ts";
27 changes: 17 additions & 10 deletions lib/multiParserV2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bytes } from "../deps.ts";
import { bytes, readAll } from "../deps.ts";

const encoder = new TextEncoder();
const decoder = new TextDecoder();
Expand Down Expand Up @@ -27,14 +27,21 @@ export interface Form {
}

// TODO: provide options
export async function multiParser(req: Request, option?: any) {
export async function multiParser(
req: Request | { headers: Headers, raw: Deno.Reader}/*to accept opine request*/,
option?: any
) {
if (
req.headers.has("content-type") &&
req.headers.get("content-type")?.startsWith("multipart/form-data")
) {
const arrayBuf = await req.arrayBuffer()
let buf:Uint8Array;

let buf = new Uint8Array(arrayBuf);
//@ts-ignore Opine work around - for some rason if(req.raw) not work properly
if (req.app) buf = await readAll(req.raw);
else
// for Request
buf = new Uint8Array(await (req as Request).arrayBuffer()):

let boundaryByte = getBoundary(req.headers.get("content-type") as string);
if (!boundaryByte) {
Expand Down Expand Up @@ -91,7 +98,7 @@ function getForm(pieces: Uint8Array[]) {
}

function getHeaders(headerByte: Uint8Array) {
let contentTypeIndex = bytes.indexOf(headerByte, encode.contentType);
let contentTypeIndex = bytes.indexOfNeedle(headerByte, encode.contentType);

// no contentType, it may be a string field, return name only
if (contentTypeIndex < 0) {
Expand Down Expand Up @@ -123,7 +130,7 @@ function getHeaderNContentType(
function getHeaderOnly(headerLineByte: Uint8Array) {
let headers: Record<string, string> = {};

let filenameIndex = bytes.indexOf(headerLineByte, encode.filename);
let filenameIndex = bytes.indexOfNeedle(headerLineByte, encode.filename);
if (filenameIndex < 0) {
headers.name = getNameOnly(headerLineByte);
} else {
Expand All @@ -146,7 +153,7 @@ function getNameNFilename(headerLineByte: Uint8Array, filenameIndex: number) {
}

function getNameOnly(headerLineByte: Uint8Array) {
let nameIndex = bytes.indexOf(headerLineByte, encode.name);
let nameIndex = bytes.indexOfNeedle(headerLineByte, encode.name);
// jump <name="> and get string inside double quote => "string"
let nameByte = headerLineByte.slice(
nameIndex + encode.name.byteLength ,
Expand All @@ -156,7 +163,7 @@ function getNameOnly(headerLineByte: Uint8Array) {
}

function splitPiece(piece: Uint8Array) {
const contentIndex = bytes.indexOf(piece, encode.returnNewline2);
const contentIndex = bytes.indexOfNeedle(piece, encode.returnNewline2);
const headerByte = piece.slice(0, contentIndex);
const contentByte = piece.slice(contentIndex + 4);

Expand All @@ -172,7 +179,7 @@ function getFieldPieces(buf: Uint8Array, boundaryByte: Uint8Array) {
while (!bytes.startsWith(buf, endBoundaryByte)) {
// jump over boundary + '\r\n'
buf = buf.slice(startBoundaryByte.byteLength + 2);
let boundaryIndex = bytes.indexOf(buf, startBoundaryByte);
let boundaryIndex = bytes.indexOfNeedle(buf, startBoundaryByte);
// get field content piece
pieces.push(buf.slice(0, boundaryIndex - 2)); // -2 means remove /r/n
buf = buf.slice(boundaryIndex);
Expand All @@ -183,7 +190,7 @@ function getFieldPieces(buf: Uint8Array, boundaryByte: Uint8Array) {

function getBoundary(contentType: string): Uint8Array | undefined {
let contentTypeByte = encoder.encode(contentType);
let boundaryIndex = bytes.indexOf(contentTypeByte, encode.boundaryEqual);
let boundaryIndex = bytes.indexOfNeedle(contentTypeByte, encode.boundaryEqual);
if (boundaryIndex >= 0) {
// jump over 'boundary=' to get the real boundary
let boundary = contentTypeByte.slice(
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type {
Form ,
Form,
FormFile,
} from "./lib/multiParserV2.ts";

Expand Down