Skip to content

Commit ca7f97c

Browse files
committed
fix: copy encodeurl into utils
1 parent c596768 commit ca7f97c

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
"deepmerge": "^4.3.1",
9797
"dotenv": "17.2.2",
9898
"emoji-regex": "^10.5.0",
99-
"encodeurl": "^2.0.0",
10099
"eventsource": "^2.0.2",
101100
"fast-json-stringify": "^6.0.1",
102101
"fastify": "^5.6.0",
@@ -149,7 +148,6 @@
149148
"@faker-js/faker": "^9.8.0",
150149
"@fastify/static": "^8.2.0",
151150
"@swc/core": "^1.13.5",
152-
"@types/encodeurl": "^1.0.3",
153151
"@types/express": "^5.0.3",
154152
"@types/humanize-duration": "^3.27.4",
155153
"@types/jest": "^29.5.14",

pnpm-lock.yaml

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/encodeurl.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*!
2+
* encodeurl
3+
* Copyright(c) 2016 Douglas Christopher Wilson
4+
* MIT Licensed
5+
*/
6+
7+
/**
8+
* RegExp to match non-URL code points, *after* encoding (i.e. not including "%")
9+
* and including invalid escape sequences.
10+
* @private
11+
*/
12+
13+
const ENCODE_CHARS_REGEXP =
14+
/(?:[^\x21\x23-\x3B\x3D\x3F-\x5F\x61-\x7A\x7C\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g;
15+
16+
/**
17+
* RegExp to match unmatched surrogate pair.
18+
* @private
19+
*/
20+
21+
const UNMATCHED_SURROGATE_PAIR_REGEXP =
22+
/(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g;
23+
24+
/**
25+
* String to replace unmatched surrogate pair with.
26+
* @private
27+
*/
28+
29+
const UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2';
30+
31+
/**
32+
* Encode a URL to a percent-encoded form, excluding already-encoded sequences.
33+
*
34+
* This function will take an already-encoded URL and encode all the non-URL
35+
* code points. This function will not encode the "%" character unless it is
36+
* not part of a valid sequence (`%20` will be left as-is, but `%foo` will
37+
* be encoded as `%25foo`).
38+
*
39+
* This encode is meant to be "safe" and does not throw errors. It will try as
40+
* hard as it can to properly encode the given URL, including replacing any raw,
41+
* unpaired surrogate pairs with the Unicode replacement character prior to
42+
* encoding.
43+
*
44+
* @param {string} url
45+
* @return {string}
46+
* @public
47+
*/
48+
49+
export const encodeUrl = (url: string): string =>
50+
String(url)
51+
.replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
52+
.replace(ENCODE_CHARS_REGEXP, encodeURI);

src/routes/redirector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { URL } from 'url';
22
import { FastifyInstance } from 'fastify';
3-
import encodeurl from 'encodeurl';
43
import { ArticlePost, Post } from '../entity';
54
import { getDiscussionLink, notifyView } from '../common';
65
import createOrGetConnection from '../db';
6+
import { encodeUrl } from '../common/encodeurl';
77

88
export default async function (fastify: FastifyInstance): Promise<void> {
99
fastify.get<{ Params: { postId: string }; Querystring: { a?: string } }>(
@@ -36,7 +36,7 @@ export default async function (fastify: FastifyInstance): Promise<void> {
3636
}
3737
const url = new URL(post.url);
3838
url.searchParams.append('ref', 'dailydev');
39-
const encodedUri = encodeurl(url.href);
39+
const encodedUri = encodeUrl(url.href);
4040
if (req.isBot) {
4141
return res.status(302).redirect(encodedUri);
4242
}

0 commit comments

Comments
 (0)