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

Pass number instead of string into Buffer.indexOf #88

Merged
merged 1 commit into from
Oct 28, 2020
Merged
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
5 changes: 4 additions & 1 deletion src/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Time } from "./types";
// reads through a buffer and extracts { [key: string]: value: string }
// pairs - the buffer is expected to have length prefixed utf8 strings
// with a '=' separating the key and value
const EQUALS_CHARCODE = "=".charCodeAt(0);
export function extractFields(buffer: Buffer) {
if (buffer.length < 4) {
throw new Error("Header fields are truncated.");
Expand All @@ -27,8 +28,10 @@ export function extractFields(buffer: Buffer) {
throw new Error("Header fields are corrupt.");
}

// Passing a number into "indexOf" explicitly to avoid Buffer polyfill
// slow path. See issue #87.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 great comment to help remember why the code is the way it is.

const field = buffer.slice(i, i + length);
const index = field.indexOf("=");
const index = field.indexOf(EQUALS_CHARCODE);
if (index === -1) {
throw new Error("Header field is missing equals sign.");
}
Expand Down