Skip to content

Commit

Permalink
feat: assert method is valid before attempting to read body
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 1, 2021
1 parent 6236fc2 commit 92f67f0
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/utils/body.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { IncomingMessage } from 'http'
import destr from 'destr'
import type { Encoding } from '../types/node'
import { HTTPMethod, assertMethod } from './request'

const RawBodySymbol = Symbol('h3RawBody')
const ParsedBodySymbol = Symbol('h3RawBody')

const PayloadMethods = ['PATCH', 'POST', 'PUT'] as HTTPMethod[]

/**
* Reads body of the request and returns encoded raw string (default) or `Buffer` if encoding if falsy.
* @param req {IncomingMessage} An IncomingMessage object is created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
Expand All @@ -13,6 +16,9 @@ const ParsedBodySymbol = Symbol('h3RawBody')
* @return {String|Buffer} Encoded raw string or raw Buffer of the body
*/
export function useRawBody (req: IncomingMessage, encoding: Encoding = 'utf-8'): Encoding extends false ? Buffer : Promise<string> {
// Ensure using correct HTTP method before attempt to read payload
assertMethod(req, PayloadMethods)

// @ts-ignore
if (RawBodySymbol in req) {
// @ts-ignore
Expand Down

0 comments on commit 92f67f0

Please sign in to comment.