Skip to content

Commit 07785f4

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
added test case and updated logic
1 parent 547d112 commit 07785f4

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

libs/url/builder.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,22 @@ const hasMoreThanAscii = (str: string) => {
2929
return str.split('').some((char) => char.charCodeAt(0) > 127);
3030
}
3131

32+
const customEncodeURIComponent = (str: string) => {
33+
const parts = str.includes("?") ? str.split("?")[0] : str;
34+
const segments = parts.split("/");
35+
const encodedSegments = segments.map((segment) => {
36+
if (segment === "/") return "/";
37+
if(segment.includes('https:') || segment.includes('http:') || segment.includes('tr:'))
38+
return segment;
39+
return encodeURIComponent(segment);
40+
});
41+
return str.includes("?") ? `${encodedSegments.join("/")}?${str.split("?")[1]}` : encodedSegments.join("/");
42+
// return str.includes("?") ? `${encodeURI(parts)}?${str.split("?")[1]}` : encodeURI(parts);
43+
};
44+
3245
const encodeStringIfRequired = (str: string) => {
33-
return hasMoreThanAscii(str) ? encodeURI(str) : str;
46+
console.log({str})
47+
return hasMoreThanAscii(str) ? customEncodeURIComponent(str) : str;
3448
}
3549

3650
const buildURL = function (opts: FinalUrlOptions): string {
@@ -170,6 +184,7 @@ function getSignatureTimestamp(seconds: number): string {
170184
}
171185

172186
function getSignature(opts: any) {
187+
console.log({opts})
173188
if (!opts.privateKey || !opts.url || !opts.urlEndpoint) return "";
174189
var stringToSign = opts.url.replace(urlFormatter.addTrailingSlash(opts.urlEndpoint), "") + opts.expiryTimestamp;
175190
return crypto.createHmac("sha1", opts.privateKey).update(stringToSign).digest("hex");

tests/url-generation.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,33 @@ describe("URL generation", function () {
5959
expect(url).includes(`ik-s=`);
6060
});
6161

62-
it('Signed URL with expireSeconds and é in url', function () {
62+
it('Signed URL with é in filename', function () {
6363
const url = imagekit.url({
6464
path: "/test_é_path_alt.jpg",
6565
signed: true,
66-
expireSeconds: 100
6766
});
67+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_é_path_alt.jpg?ik-s=09a329f06a5106a8b9c43de8fb6a64948fff7c59`);
68+
});
6869

69-
expect(url).includes(`https://ik.imagekit.io/test_url_endpoint/test_é_path_alt.jpg`);
70-
expect(url).includes(`ik-s=`);
71-
expect(url).includes(`ik-t=`);
70+
it('Signed URL with é in filename and path', function () {
71+
const url = imagekit.url({
72+
path: "/aéb/test_é_path_alt.jpg",
73+
signed: true,
74+
});
75+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/aéb/test_é_path_alt.jpg?ik-s=fca91582138ac65694425d52f0710b7ae2c3d7cf`);
7276
});
7377

78+
it('Signed URL with é in filename, path and query', function () {
79+
const url = imagekit.url({
80+
path: "/aéb/test_é_path_alt.jpg",
81+
signed: true,
82+
transformation: [ { raw: "l-text,i-Imagekité,fs-50,l-end"}]
83+
});
84+
console.log({url})
85+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:l-text,i-Imagekité,fs-50,l-end/aéb/test_é_path_alt.jpg?ik-s=38539311889a0721b46ebe30b5f297773d01d960`);
86+
});
87+
88+
7489
it('should generate the correct url with path param', function () {
7590
const url = imagekit.url({
7691
path: "/test_path.jpg",

0 commit comments

Comments
 (0)