Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: treat application/octet-stream as a binary encoding #1587

Merged
merged 3 commits into from
May 20, 2024
Merged
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
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
Loading