Skip to content

Commit bca9300

Browse files
author
imgix-git-robot
committed
chore(release): 3.5.0
# [3.5.0](v3.4.0...v3.5.0) (2022-02-24) ### Bug Fixes * ensure slash always between domain and path when sanitizing ([929e878](929e878)) ### Features * allow path encoding to be disabled ([1413800](1413800)) [skip ci]
1 parent f9dca28 commit bca9300

File tree

6 files changed

+138
-103
lines changed

6 files changed

+138
-103
lines changed

dist/imgix-js-core.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cjs.js

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,22 @@ function ownKeys(object, enumerableOnly) {
1212

1313
if (Object.getOwnPropertySymbols) {
1414
var symbols = Object.getOwnPropertySymbols(object);
15-
16-
if (enumerableOnly) {
17-
symbols = symbols.filter(function (sym) {
18-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
19-
});
20-
}
21-
22-
keys.push.apply(keys, symbols);
15+
enumerableOnly && (symbols = symbols.filter(function (sym) {
16+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
17+
})), keys.push.apply(keys, symbols);
2318
}
2419

2520
return keys;
2621
}
2722

2823
function _objectSpread2(target) {
2924
for (var i = 1; i < arguments.length; i++) {
30-
var source = arguments[i] != null ? arguments[i] : {};
31-
32-
if (i % 2) {
33-
ownKeys(Object(source), true).forEach(function (key) {
34-
_defineProperty(target, key, source[key]);
35-
});
36-
} else if (Object.getOwnPropertyDescriptors) {
37-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
38-
} else {
39-
ownKeys(Object(source)).forEach(function (key) {
40-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
41-
});
42-
}
25+
var source = null != arguments[i] ? arguments[i] : {};
26+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
27+
_defineProperty(target, key, source[key]);
28+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
29+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
30+
});
4331
}
4432

4533
return target;
@@ -48,17 +36,11 @@ function _objectSpread2(target) {
4836
function _typeof(obj) {
4937
"@babel/helpers - typeof";
5038

51-
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
52-
_typeof = function (obj) {
53-
return typeof obj;
54-
};
55-
} else {
56-
_typeof = function (obj) {
57-
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
58-
};
59-
}
60-
61-
return _typeof(obj);
39+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
40+
return typeof obj;
41+
} : function (obj) {
42+
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
43+
}, _typeof(obj);
6244
}
6345

6446
function _classCallCheck(instance, Constructor) {
@@ -80,6 +62,9 @@ function _defineProperties(target, props) {
8062
function _createClass(Constructor, protoProps, staticProps) {
8163
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
8264
if (staticProps) _defineProperties(Constructor, staticProps);
65+
Object.defineProperty(Constructor, "prototype", {
66+
writable: false
67+
});
8368
return Constructor;
8469
}
8570

@@ -294,18 +279,21 @@ var ImgixClient = /*#__PURE__*/function () {
294279
_createClass(ImgixClient, [{
295280
key: "buildURL",
296281
value: function buildURL() {
297-
var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
282+
var rawPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
298283
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
284+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
299285

300-
var sanitizedPath = this._sanitizePath(path);
286+
var path = this._sanitizePath(rawPath, {
287+
encode: !options.disablePathEncoding
288+
});
301289

302290
var finalParams = this._buildParams(params);
303291

304292
if (!!this.settings.secureURLToken) {
305-
finalParams = this._signParams(sanitizedPath, finalParams);
293+
finalParams = this._signParams(path, finalParams);
306294
}
307295

308-
return this.settings.urlPrefix + this.settings.domain + sanitizedPath + finalParams;
296+
return this.settings.urlPrefix + this.settings.domain + path + finalParams;
309297
}
310298
}, {
311299
key: "_buildParams",
@@ -331,23 +319,38 @@ var ImgixClient = /*#__PURE__*/function () {
331319
key: "_signParams",
332320
value: function _signParams(path, queryParams) {
333321
var signatureBase = this.settings.secureURLToken + path + queryParams;
334-
var signature = md5__default['default'](signatureBase);
322+
var signature = md5__default["default"](signatureBase);
335323
return queryParams.length > 0 ? queryParams + '&s=' + signature : '?s=' + signature;
336324
}
325+
/**
326+
* "Sanitize" the path of the image URL.
327+
* Ensures that the path has a leading slash, and that the path is correctly
328+
* encoded. If it's a proxy path (begins with http/https), then encode the
329+
* whole path as a URI component, otherwise only encode specific characters.
330+
* @param {string} path The URL path of the image
331+
* @param {Object} options Sanitization options
332+
* @param {boolean} options.encode Whether to encode the path, default true
333+
* @returns {string} The sanitized path
334+
*/
335+
337336
}, {
338337
key: "_sanitizePath",
339338
value: function _sanitizePath(path) {
339+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
340+
340341
// Strip leading slash first (we'll re-add after encoding)
341342
var _path = path.replace(/^\//, '');
342343

343-
if (/^https?:\/\//.test(_path)) {
344-
// Use de/encodeURIComponent to ensure *all* characters are handled,
345-
// since it's being used as a path
346-
_path = encodeURIComponent(_path);
347-
} else {
348-
// Use de/encodeURI if we think the path is just a path,
349-
// so it leaves legal characters like '/' and '@' alone
350-
_path = encodeURI(_path).replace(/[#?:+]/g, encodeURIComponent);
344+
if (!(options.encode === false)) {
345+
if (/^https?:\/\//.test(_path)) {
346+
// Use de/encodeURIComponent to ensure *all* characters are handled,
347+
// since it's being used as a path
348+
_path = encodeURIComponent(_path);
349+
} else {
350+
// Use de/encodeURI if we think the path is just a path,
351+
// so it leaves legal characters like '/' and '@' alone
352+
_path = encodeURI(_path).replace(/[#?:+]/g, encodeURIComponent);
353+
}
351354
}
352355

353356
return '/' + _path;
@@ -369,9 +372,12 @@ var ImgixClient = /*#__PURE__*/function () {
369372

370373
}, {
371374
key: "_buildSrcSetPairs",
372-
value: function _buildSrcSetPairs(path, params, options) {
375+
value: function _buildSrcSetPairs(path) {
373376
var _this = this;
374377

378+
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
379+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
380+
375381
var _validateAndDestructu = validateAndDestructureOptions(options),
376382
_validateAndDestructu2 = _slicedToArray(_validateAndDestructu, 3),
377383
widthTolerance = _validateAndDestructu2[0],
@@ -390,15 +396,20 @@ var ImgixClient = /*#__PURE__*/function () {
390396
var srcset = targetWidthValues.map(function (w) {
391397
return "".concat(_this.buildURL(path, _objectSpread2(_objectSpread2({}, params), {}, {
392398
w: w
393-
})), " ").concat(w, "w");
399+
}), {
400+
disablePathEncoding: options.disablePathEncoding
401+
}), " ").concat(w, "w");
394402
});
395403
return srcset.join(',\n');
396404
}
397405
}, {
398406
key: "_buildDPRSrcSet",
399-
value: function _buildDPRSrcSet(path, params, options) {
407+
value: function _buildDPRSrcSet(path) {
400408
var _this2 = this;
401409

410+
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
411+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
412+
402413
if (options.devicePixelRatios) {
403414
validateDevicePixelRatios(options.devicePixelRatios);
404415
}
@@ -420,13 +431,17 @@ var ImgixClient = /*#__PURE__*/function () {
420431
return "".concat(_this2.buildURL(path, _objectSpread2(_objectSpread2({}, params), {}, {
421432
dpr: dpr,
422433
q: params.q || qualities[dpr] || qualities[Math.floor(dpr)]
423-
})), " ").concat(dpr, "x");
434+
}), {
435+
disablePathEncoding: options.disablePathEncoding
436+
}), " ").concat(dpr, "x");
424437
};
425438

426439
var srcset = disableVariableQuality ? targetRatios.map(function (dpr) {
427440
return "".concat(_this2.buildURL(path, _objectSpread2(_objectSpread2({}, params), {}, {
428441
dpr: dpr
429-
})), " ").concat(dpr, "x");
442+
}), {
443+
disablePathEncoding: options.disablePathEncoding
444+
}), " ").concat(dpr, "x");
430445
}) : targetRatios.map(function (dpr) {
431446
return withQuality(path, params, dpr);
432447
});

dist/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ declare class ImgixClient {
1111
includeLibraryParam?: boolean;
1212
});
1313

14-
buildURL(path: string, params?: {}): string;
14+
buildURL(
15+
path: string,
16+
params?: {},
17+
options?: { disablePathEncoding?: boolean },
18+
): string;
1519
_sanitizePath(path: string): string;
1620
_buildParams(params: {}): string;
1721
_signParams(path: string, queryParams?: {}): string;
@@ -38,6 +42,7 @@ export interface SrcSetOptions {
3842
disableVariableQuality?: boolean;
3943
devicePixelRatios?: DevicePixelRatio[];
4044
variableQualities?: VariableQualities;
45+
disablePathEncoding?: boolean;
4146
}
4247

4348
export default ImgixClient;

0 commit comments

Comments
 (0)