From 36255e2f901f21aad0786de5d9053f92aa971c3c Mon Sep 17 00:00:00 2001 From: MrJ0se <37480421+MrJ0se@users.noreply.github.com> Date: Fri, 2 Sep 2022 10:40:18 -0300 Subject: [PATCH] updating to std 154 + opine support + work around to accept OpineRequest ("deno.land/x/opine") --- deps.ts | 3 ++- lib/multiParserV2.ts | 27 +++++++++++++++++---------- mod.ts | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/deps.ts b/deps.ts index ebab5a6..50ebb46 100644 --- a/deps.ts +++ b/deps.ts @@ -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"; diff --git a/lib/multiParserV2.ts b/lib/multiParserV2.ts index ab0d5e7..098f77a 100644 --- a/lib/multiParserV2.ts +++ b/lib/multiParserV2.ts @@ -1,4 +1,4 @@ -import { bytes } from "../deps.ts"; +import { bytes, readAll } from "../deps.ts"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); @@ -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) { @@ -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) { @@ -123,7 +130,7 @@ function getHeaderNContentType( function getHeaderOnly(headerLineByte: Uint8Array) { let headers: Record = {}; - let filenameIndex = bytes.indexOf(headerLineByte, encode.filename); + let filenameIndex = bytes.indexOfNeedle(headerLineByte, encode.filename); if (filenameIndex < 0) { headers.name = getNameOnly(headerLineByte); } else { @@ -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 and get string inside double quote => "string" let nameByte = headerLineByte.slice( nameIndex + encode.name.byteLength , @@ -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); @@ -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); @@ -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( diff --git a/mod.ts b/mod.ts index 6ead28b..91dda1c 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,5 @@ export type { - Form , + Form, FormFile, } from "./lib/multiParserV2.ts";