Skip to content

Commit 397ab31

Browse files
committed
fix: handle missing values in overlay processing to prevent invalid URLs
1 parent 2d95621 commit 397ab31

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

src/url/builder.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,23 @@ function processOverlay(overlay: Transformation["overlay"]): string | undefined
8686
}
8787

8888
switch (type) {
89-
case "text":
90-
entries.push("l-text");
91-
{
92-
const textOverlay = overlay as TextOverlay;
93-
if (textOverlay.text) {
94-
entries.push(`ie-${encodeURIComponent(safeBtoa(textOverlay.text))}`);
95-
}
89+
case "text": {
90+
const textOverlay = overlay as TextOverlay;
91+
if (!textOverlay.text) {
92+
return;
9693
}
94+
entries.push("l-text");
95+
entries.push(`ie-${encodeURIComponent(safeBtoa(textOverlay.text))}`);
96+
}
9797
break;
9898
case "image":
9999
entries.push("l-image");
100100
{
101101
const imageOverlay = overlay as ImageOverlay;
102102
if (imageOverlay.input) {
103103
entries.push(`i-${imageOverlay.input}`);
104+
} else {
105+
return;
104106
}
105107
}
106108
break;
@@ -110,6 +112,8 @@ function processOverlay(overlay: Transformation["overlay"]): string | undefined
110112
const videoOverlay = overlay as VideoOverlay;
111113
if (videoOverlay.input) {
112114
entries.push(`i-${videoOverlay.input}`);
115+
} else {
116+
return;
113117
}
114118
}
115119
break;
@@ -119,6 +123,8 @@ function processOverlay(overlay: Transformation["overlay"]): string | undefined
119123
const subtitleOverlay = overlay as SubtitleOverlay;
120124
if (subtitleOverlay.input) {
121125
entries.push(`i-${subtitleOverlay.input}`);
126+
} else {
127+
return;
122128
}
123129
}
124130
break;
@@ -129,6 +135,8 @@ function processOverlay(overlay: Transformation["overlay"]): string | undefined
129135
const solidColorOverlay = overlay as SolidColorOverlay;
130136
if (solidColorOverlay.color) {
131137
entries.push(`bg-${solidColorOverlay.color}`);
138+
} else {
139+
return;
132140
}
133141
}
134142
break;
@@ -182,10 +190,10 @@ function constructTransformationString(transformation: Transformation[] | undefi
182190

183191
if (key === "overlay" && typeof value === "object") {
184192
var rawString = processOverlay(value as Transformation["overlay"]);
185-
if (rawString) {
193+
if (rawString && rawString.trim() !== "") {
186194
parsedTransformStep.push(rawString);
187-
continue;
188195
}
196+
continue; // Always continue as overlay is processed.
189197
}
190198

191199
var transformKey = transformationUtils.getTransformKey(key);

test/url-generation/overlay.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,66 @@ import { safeBtoa } from "../../src/utils/transformation";
66
describe("Overlay Transformation Test Cases", function () {
77
const imagekit = new ImageKit(initializationParams);
88

9+
it('Ignore invalid values if text is missing', function () {
10+
const url = imagekit.url({
11+
path: "/base-image.jpg",
12+
transformation: [{
13+
overlay: {
14+
type: "text"
15+
}
16+
}]
17+
});
18+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/base-image.jpg`);
19+
});
20+
21+
it('Ignore invalid values if input', function () {
22+
const url = imagekit.url({
23+
path: "/base-image.jpg",
24+
transformation: [{
25+
overlay: {
26+
type: "image"
27+
}
28+
}]
29+
});
30+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/base-image.jpg`);
31+
});
32+
33+
it('Ignore invalid values if input', function () {
34+
const url = imagekit.url({
35+
path: "/base-image.jpg",
36+
transformation: [{
37+
overlay: {
38+
type: "video"
39+
}
40+
}]
41+
});
42+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/base-image.jpg`);
43+
});
44+
45+
it('Ignore invalid values if input', function () {
46+
const url = imagekit.url({
47+
path: "/base-image.jpg",
48+
transformation: [{
49+
overlay: {
50+
type: "subtitle"
51+
}
52+
}]
53+
});
54+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/base-image.jpg`);
55+
});
56+
57+
it('Ignore invalid values if color is missing', function () {
58+
const url = imagekit.url({
59+
path: "/base-image.jpg",
60+
transformation: [{
61+
overlay: {
62+
type: "solidColor"
63+
}
64+
}]
65+
});
66+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/base-image.jpg`);
67+
});
68+
969
it('Text overlay generates correct URL with encoded overlay text', function () {
1070
const url = imagekit.url({
1171
path: "/base-image.jpg",

0 commit comments

Comments
 (0)