Skip to content

Commit 8166889

Browse files
authored
Merge pull request #92 from imagekit-developer/SDK-74
added encoding logic
2 parents d069c56 + 7195b31 commit 8166889

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

libs/url/builder.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ const SIGNATURE_PARAMETER: string = "ik-s";
2424
const TIMESTAMP_PARAMETER: string = "ik-t";
2525
const DEFAULT_TIMESTAMP: string = "9999999999";
2626

27+
//used to check if special char is present in string (you'll need to encode it to utf-8 if it does)
28+
const hasMoreThanAscii = (str: string) => {
29+
return str.split('').some((char) => char.charCodeAt(0) > 127);
30+
}
31+
32+
const customEncodeURI = (str: string) => {
33+
return str.includes("?") ? `${encodeURI(str.split("?")[0])}?${str.split("?")[1]}` : encodeURI(str);
34+
};
35+
36+
export const encodeStringIfRequired = (str: string) => {
37+
return hasMoreThanAscii(str) ? customEncodeURI(str) : str;
38+
}
39+
2740
const buildURL = function (opts: FinalUrlOptions): string {
2841
//Create correct query parameters
2942
var parsedURL: UrlWithStringQuery;
@@ -160,9 +173,10 @@ function getSignatureTimestamp(seconds: number): string {
160173
return String(currentTimestamp + sec);
161174
}
162175

163-
function getSignature(opts: any) {
176+
export function getSignature(opts: any) {
164177
if (!opts.privateKey || !opts.url || !opts.urlEndpoint) return "";
165178
var stringToSign = opts.url.replace(urlFormatter.addTrailingSlash(opts.urlEndpoint), "") + opts.expiryTimestamp;
179+
stringToSign = encodeStringIfRequired(stringToSign);
166180
return crypto.createHmac("sha1", opts.privateKey).update(stringToSign).digest("hex");
167181
}
168182

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"description": "Offical NodeJS SDK for ImageKit.io integration",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

tests/url-generation.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const pkg = require("../package.json");
33
const expect = chai.expect;
44
const initializationParams = require("./data").initializationParams
55
import ImageKit from "../index";
6+
import { encodeStringIfRequired, getSignature } from "../libs/url/builder";
67
var imagekit = new ImageKit(initializationParams);
78

89
describe("URL generation", function () {
@@ -59,6 +60,84 @@ describe("URL generation", function () {
5960
expect(url).includes(`ik-s=`);
6061
});
6162

63+
it("Signed URL with é in filename", function () {
64+
const testURL = "https://ik.imagekit.io/test_url_endpoint/test_é_path_alt.jpg";
65+
const encodedUrl = encodeStringIfRequired(testURL);
66+
expect(encodedUrl).equal("https://ik.imagekit.io/test_url_endpoint/test_%C3%A9_path_alt.jpg");
67+
const signature = getSignature({
68+
privateKey: "test_private_key",
69+
url: testURL,
70+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
71+
expiryTimestamp: "9999999999",
72+
});
73+
const url = imagekit.url({
74+
path: "/test_é_path_alt.jpg",
75+
signed: true,
76+
});
77+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_é_path_alt.jpg?ik-s=${signature}`);
78+
});
79+
80+
it("Signed URL with é in filename and path", function () {
81+
const testURL = "https://ik.imagekit.io/test_url_endpoint/aéb/test_é_path_alt.jpg";
82+
const encodedUrl = encodeStringIfRequired(testURL);
83+
expect(encodedUrl).equal("https://ik.imagekit.io/test_url_endpoint/a%C3%A9b/test_%C3%A9_path_alt.jpg");
84+
const signature = getSignature({
85+
privateKey: "test_private_key",
86+
url: testURL,
87+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
88+
expiryTimestamp: "9999999999",
89+
});
90+
const url = imagekit.url({
91+
path: "/aéb/test_é_path_alt.jpg",
92+
signed: true,
93+
});
94+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/aéb/test_é_path_alt.jpg?ik-s=${signature}`);
95+
});
96+
97+
it("Signed URL with é in filename, path and transformation as path", function () {
98+
const testURL = "https://ik.imagekit.io/test_url_endpoint/tr:l-text,i-Imagekité,fs-50,l-end/aéb/test_é_path_alt.jpg";
99+
const encodedUrl = encodeStringIfRequired(testURL);
100+
expect(encodedUrl).equal("https://ik.imagekit.io/test_url_endpoint/tr:l-text,i-Imagekit%C3%A9,fs-50,l-end/a%C3%A9b/test_%C3%A9_path_alt.jpg");
101+
const signature = getSignature({
102+
privateKey: "test_private_key",
103+
url: testURL,
104+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
105+
expiryTimestamp: "9999999999",
106+
});
107+
108+
const url = imagekit.url({
109+
path: "/aéb/test_é_path_alt.jpg",
110+
signed: true,
111+
transformation: [{ raw: "l-text,i-Imagekité,fs-50,l-end" }],
112+
transformationPosition: "path",
113+
});
114+
expect(url).equal(
115+
`https://ik.imagekit.io/test_url_endpoint/tr:l-text,i-Imagekité,fs-50,l-end/aéb/test_é_path_alt.jpg?ik-s=${signature}`
116+
);
117+
});
118+
119+
it("Signed URL with é in filename, path and transformation as query", function () {
120+
const testURL = "https://ik.imagekit.io/test_url_endpoint/aéb/test_é_path_alt.jpg?tr=l-text%2Ci-Imagekit%C3%A9%2Cfs-50%2Cl-end";
121+
const encodedUrl = encodeStringIfRequired(testURL);
122+
expect(encodedUrl).equal("https://ik.imagekit.io/test_url_endpoint/a%C3%A9b/test_%C3%A9_path_alt.jpg?tr=l-text%2Ci-Imagekit%C3%A9%2Cfs-50%2Cl-end");
123+
const signature = getSignature({
124+
privateKey: "test_private_key",
125+
url: testURL,
126+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
127+
expiryTimestamp: "9999999999",
128+
});
129+
const url = imagekit.url({
130+
path: "/aéb/test_é_path_alt.jpg",
131+
signed: true,
132+
transformation: [{ raw: "l-text,i-Imagekité,fs-50,l-end" }],
133+
transformationPosition: "query",
134+
});
135+
expect(url).equal(
136+
`https://ik.imagekit.io/test_url_endpoint/aéb/test_é_path_alt.jpg?tr=l-text%2Ci-Imagekit%C3%A9%2Cfs-50%2Cl-end&ik-s=${signature}`
137+
);
138+
});
139+
140+
62141
it('should generate the correct url with path param', function () {
63142
const url = imagekit.url({
64143
path: "/test_path.jpg",

0 commit comments

Comments
 (0)