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
94 changes: 74 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@
"dist/"
],
"scripts": {
"bench": "vitest bench",
"build": "ts-scripts build",
"format": "ts-scripts format",
"lint": "ts-scripts check",
"prepare": "ts-scripts install && npm run build",
"size": "size-limit",
"specs": "ts-scripts specs",
"test": "ts-scripts test"
"test": "ts-scripts test && npm run size"
},
"devDependencies": {
"@borderless/ts-scripts": "^0.15.0",
"@size-limit/preset-small-lib": "^14.3.0",
"@types/node": "^26.6.5",
"@vitest/coverage-v8": "^5.0.5",
"size-limit": "^14.3.0",
"typescript": "^7.0.2",
"vitest": "^5.0.5"
},
Expand All @@ -44,6 +48,12 @@
"publishConfig": {
"access": "public"
},
"size-limit": [
{
"path": "dist/index.js",
"limit": "250 B"
}
],
"ts-scripts": {
"dist": [
"dist"
Expand Down
23 changes: 23 additions & 0 deletions src/index.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, test } from "vitest";
import { encodeUrl } from "./index.js";

const _encodeUrl = encodeUrl;

describe("encodeUrl", () => {
const cases = {
"plain URL": "https://example.com/foo/bar?hello=world#section",
"already encoded": "https://example.com/foo%20bar?q=caf%C3%A9",
spaces: "https://example.com/foo bar?q=hello world",
Unicode: "https://example.com/café/日本語",
emoji: "https://example.com/👻😀🚀",
"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)}`,
};

for (const [name, url] of Object.entries(cases)) {
test(name, async ({ bench }) => {
await bench(name, () => _encodeUrl(url)).run();
});
}
});
16 changes: 4 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
* @private
*/
const ENCODE_CHARS_REGEXP =
/(?:[^\x21\x23-\x3B\x3D\x3F-\x5F\x61-\x7A\x7C\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g;

/**
* RegExp to match surrogate pairs and unpaired surrogates.
* @private
*/
const SURROGATE_REGEXP = /([\uD800-\uDBFF][\uDC00-\uDFFF])|[\uD800-\uDFFF]/g;
/(?:[\x00-\x20"<>`{}\x7F-\uFFFF]|%(?:[0-9A-Fa-f]?[^0-9A-Fa-f]|$))+/g;

/**
* Encode a URL to a percent-encoded form, excluding already-encoded sequences.
Expand All @@ -36,9 +30,7 @@ const SURROGATE_REGEXP = /([\uD800-\uDBFF][\uDC00-\uDFFF])|[\uD800-\uDFFF]/g;
* @public
*/
export function encodeUrl(url: string): string {
return url
.replace(SURROGATE_REGEXP, function (_: string, pair: string | undefined) {
return pair || "\uFFFD";
})
.replace(ENCODE_CHARS_REGEXP, encodeURI);
const index = url.search(ENCODE_CHARS_REGEXP);
if (index === -1) return url;
return url.toWellFormed().replace(ENCODE_CHARS_REGEXP, encodeURI);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "@borderless/ts-scripts/configs/tsconfig.json",
"compilerOptions": {
"target": "ES2023",
"lib": ["ES2023"],
"lib": ["ES2023", "ES2024.String"],
"rootDir": "src",
"outDir": "dist",
"module": "NodeNext",
Expand Down
Loading