Skip to content
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
2 changes: 2 additions & 0 deletions src/index.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("encodeUrl", () => {
"unpaired surrogates": "https://example.com/\uD800foo\uDC00",
"invalid escapes": "https://example.com/%foo%bar%zap%",
"long plain URL": `https://example.com/${"foo/bar/".repeat(500)}`,
"long percent run": "%".repeat(1_000_000),
"long mixed run": "% 日".repeat(333_333),
};

for (const [name, url] of Object.entries(cases)) {
Expand Down
47 changes: 47 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,53 @@ describe("encodeUrl(url)", function () {
"http://localhost/%25foo%bar%25zap%25",
);
});

it('should encode a "%" at the end of a truncated sequence', function () {
expect(encodeUrl("http://localhost/%A")).toBe("http://localhost/%25A");
expect(encodeUrl("http://localhost/a%2")).toBe("http://localhost/a%252");
});

it("should not encode a valid sequence after an invalid one", function () {
expect(encodeUrl("http://localhost/%%41")).toBe(
"http://localhost/%25%41",
);
expect(encodeUrl("http://localhost/%2%20snow.html")).toBe(
"http://localhost/%252%20snow.html",
);
});
});

describe("when url contains large inputs", () => {
const length = 10_000_000;

it.each([
["plain characters", "a", "a"],
["spaces", " ", "%20"],
["percent signs", "%", "%25"],
["Unicode", "日", "%E6%97%A5"],
["surrogate pairs", "👻", "%F0%9F%91%BB"],
["unpaired surrogates", "\uD800", "%EF%BF%BD"],
["valid escapes", "%20", "%20"],
["mixed characters", "% 日", "%25%20%E6%97%A5"],
["percent signs and surrogate pairs", "%👻", "%25%F0%9F%91%BB"],
])("should handle a long run of %s", (_name, input, expected) => {
const count = Math.floor(length / input.length);
expect(encodeUrl(input.repeat(count))).toBe(expected.repeat(count));
});

it("should preserve a valid escape after a long percent run", () => {
expect(encodeUrl(`${"%".repeat(length)}41`)).toBe(
`${"%25".repeat(length - 1)}%41`,
);
});

it("should preserve escapes and surrogate pairs across grouped runs", () => {
const input = "%👻".repeat(1025);
const expected = "%25%F0%9F%91%BB".repeat(1025);
expect(encodeUrl(`${input}%%41%2%20%A`)).toBe(
`${expected}%25%41%252%20%25A`,
);
});
});

describe("when url contains raw surrogate pairs", function () {
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
/**
* RegExp to match non-URL code points, *after* encoding (i.e. not including "%")
* and including invalid escape sequences.
* Group up to 1024 character and percent runs per replacement to reduce calls
* to encodeURI while bounding RegExp stack use. Character runs stay intact so
* surrogate pairs are never split between replacements.
* @private
*/
const ENCODE_CHARS_REGEXP =
/(?:[\x00-\x20"<>`{}\x7F-\uFFFF]|%(?:[0-9A-Fa-f]?[^0-9A-Fa-f]|$))+/g;
/(?:[\x00-\x20"<>`{}\x7F-\uFFFF]+|%+(?![0-9A-Fa-f]{2})){1,1024}/g;

/**
* Encode a URL to a percent-encoded form, excluding already-encoded sequences.
Expand Down
Loading