Skip to content

Commit

Permalink
fix: treat application/octet-stream as a binary encoding (#1587)
Browse files Browse the repository at this point in the history
* fix: treat application/octet-stream as a binary encoding

* chore: prettier fix

---------

Co-authored-by: Dorian <[email protected]>
Co-authored-by: MazurDorian <[email protected]>
  • Loading branch information
3 people committed May 20, 2024
1 parent 823e770 commit 14d2bff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ export { generateAlbHapiPath } from "./generateHapiPath.js"
const { isArray } = Array
const { keys } = Object

const possibleBinaryContentTypes = [
"application/octet-stream",
"multipart/form-data",
]

// Detect the toString encoding from the request headers content-type
// enhance if further content types need to be non utf8 encoded.
export function detectEncoding(request) {
const contentType = request.headers["content-type"]

return typeof contentType === "string" &&
contentType.includes("multipart/form-data")
possibleBinaryContentTypes.some((possibleBinaryContentType) =>
contentType.includes(possibleBinaryContentType),
)
? "binary"
: "utf8"
}
Expand Down
11 changes: 11 additions & 0 deletions tests/old-unit/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ describe("utils", () => {
})
})

describe("with application/octet-stream content-type", () => {
it("should return binary", () => {
const request = {
headers: {
"content-type": "application/octet-stream",
},
}
assert.strictEqual(detectEncoding(request), "binary")
})
})

describe("with multipart/form-data content-type", () => {
it("should return binary", () => {
const request = {
Expand Down

0 comments on commit 14d2bff

Please sign in to comment.