Skip to content

Commit

Permalink
Merge pull request #92 from imagekit-developer/SDK-74
Browse files Browse the repository at this point in the history
added encoding logic
  • Loading branch information
imagekitio committed Apr 2, 2024
2 parents d069c56 + 7195b31 commit 8166889
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
16 changes: 15 additions & 1 deletion libs/url/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ const SIGNATURE_PARAMETER: string = "ik-s";
const TIMESTAMP_PARAMETER: string = "ik-t";
const DEFAULT_TIMESTAMP: string = "9999999999";

//used to check if special char is present in string (you'll need to encode it to utf-8 if it does)
const hasMoreThanAscii = (str: string) => {
return str.split('').some((char) => char.charCodeAt(0) > 127);
}

const customEncodeURI = (str: string) => {
return str.includes("?") ? `${encodeURI(str.split("?")[0])}?${str.split("?")[1]}` : encodeURI(str);
};

export const encodeStringIfRequired = (str: string) => {
return hasMoreThanAscii(str) ? customEncodeURI(str) : str;
}

const buildURL = function (opts: FinalUrlOptions): string {
//Create correct query parameters
var parsedURL: UrlWithStringQuery;
Expand Down Expand Up @@ -160,9 +173,10 @@ function getSignatureTimestamp(seconds: number): string {
return String(currentTimestamp + sec);
}

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imagekit",
"version": "5.0.0",
"version": "5.0.1",
"description": "Offical NodeJS SDK for ImageKit.io integration",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
79 changes: 79 additions & 0 deletions tests/url-generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const pkg = require("../package.json");
const expect = chai.expect;
const initializationParams = require("./data").initializationParams
import ImageKit from "../index";
import { encodeStringIfRequired, getSignature } from "../libs/url/builder";
var imagekit = new ImageKit(initializationParams);

describe("URL generation", function () {
Expand Down Expand Up @@ -59,6 +60,84 @@ describe("URL generation", function () {
expect(url).includes(`ik-s=`);
});

it("Signed URL with é in filename", function () {
const testURL = "https://ik.imagekit.io/test_url_endpoint/test_é_path_alt.jpg";
const encodedUrl = encodeStringIfRequired(testURL);
expect(encodedUrl).equal("https://ik.imagekit.io/test_url_endpoint/test_%C3%A9_path_alt.jpg");
const signature = getSignature({
privateKey: "test_private_key",
url: testURL,
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
expiryTimestamp: "9999999999",
});
const url = imagekit.url({
path: "/test_é_path_alt.jpg",
signed: true,
});
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_é_path_alt.jpg?ik-s=${signature}`);
});

it("Signed URL with é in filename and path", function () {
const testURL = "https://ik.imagekit.io/test_url_endpoint/aéb/test_é_path_alt.jpg";
const encodedUrl = encodeStringIfRequired(testURL);
expect(encodedUrl).equal("https://ik.imagekit.io/test_url_endpoint/a%C3%A9b/test_%C3%A9_path_alt.jpg");
const signature = getSignature({
privateKey: "test_private_key",
url: testURL,
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
expiryTimestamp: "9999999999",
});
const url = imagekit.url({
path: "/aéb/test_é_path_alt.jpg",
signed: true,
});
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/aéb/test_é_path_alt.jpg?ik-s=${signature}`);
});

it("Signed URL with é in filename, path and transformation as path", function () {
const testURL = "https://ik.imagekit.io/test_url_endpoint/tr:l-text,i-Imagekité,fs-50,l-end/aéb/test_é_path_alt.jpg";
const encodedUrl = encodeStringIfRequired(testURL);
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");
const signature = getSignature({
privateKey: "test_private_key",
url: testURL,
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
expiryTimestamp: "9999999999",
});

const url = imagekit.url({
path: "/aéb/test_é_path_alt.jpg",
signed: true,
transformation: [{ raw: "l-text,i-Imagekité,fs-50,l-end" }],
transformationPosition: "path",
});
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=${signature}`
);
});

it("Signed URL with é in filename, path and transformation as query", function () {
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";
const encodedUrl = encodeStringIfRequired(testURL);
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");
const signature = getSignature({
privateKey: "test_private_key",
url: testURL,
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
expiryTimestamp: "9999999999",
});
const url = imagekit.url({
path: "/aéb/test_é_path_alt.jpg",
signed: true,
transformation: [{ raw: "l-text,i-Imagekité,fs-50,l-end" }],
transformationPosition: "query",
});
expect(url).equal(
`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}`
);
});


it('should generate the correct url with path param', function () {
const url = imagekit.url({
path: "/test_path.jpg",
Expand Down

0 comments on commit 8166889

Please sign in to comment.