Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added encoding logic #92

Merged
merged 12 commits into from
Apr 2, 2024
Merged
Changes from 4 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
11 changes: 10 additions & 1 deletion libs/url/builder.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,15 @@ 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 encodeStringIfRequired = (str: string) => {
return hasMoreThanAscii(str) ? encodeURI(str) : str;
}

const buildURL = function (opts: FinalUrlOptions): string {
//Create correct query parameters
var parsedURL: UrlWithStringQuery;
@@ -98,7 +107,7 @@ const buildURL = function (opts: FinalUrlOptions): string {

var urlSignature = getSignature({
privateKey: opts.privateKey,
url: intermediateURL,
url: encodeStringIfRequired(intermediateURL),
urlEndpoint: opts.urlEndpoint,
expiryTimestamp: expiryTimestamp,
});
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",
12 changes: 12 additions & 0 deletions tests/url-generation.js
Original file line number Diff line number Diff line change
@@ -59,6 +59,18 @@ describe("URL generation", function () {
expect(url).includes(`ik-s=`);
});

it('Signed URL with expireSeconds and é in url', function () {
const url = imagekit.url({
path: "/test_é_path_alt.jpg",
signed: true,
expireSeconds: 100
});

expect(url).includes(`https://ik.imagekit.io/test_url_endpoint/test_é_path_alt.jpg`);
expect(url).includes(`ik-s=`);
expect(url).includes(`ik-t=`);
});

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