Skip to content

Commit

Permalink
fix: remove deprecated domain sharding functionality (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
sherwinski authored Jun 6, 2019
1 parent ca889dc commit 66d5cd9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 282 deletions.
57 changes: 4 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Depending on your module system, using imgix-core-js is done a few different way
var ImgixClient = require('imgix-core-js');

var client = new ImgixClient({
domains: "my-social-network.imgix.net",
domain: "my-social-network.imgix.net",
secureURLToken: "<SECURE TOKEN>"
});
var url = client.buildURL("/path/to/image.png", {
Expand All @@ -48,7 +48,7 @@ console.log(url); // => "https://my-social-network.imgix.net/users/1.png?w=400&h
import ImgixClient from 'imgix-core-js'

let client = new ImgixClient({
domains: 'my-social-network.imgix.net',
domain: 'my-social-network.imgix.net',
secureURLToken: '<SECURE TOKEN>'
});

Expand All @@ -60,7 +60,7 @@ console.log(url); // => 'https://my-social-network.imgix.net/users/1.png?w=400&h

``` javascript
var client = new ImgixClient({
domains: 'my-social-network.imgix.net'
domain: 'my-social-network.imgix.net'
// Do not use signed URLs with `secureURLToken` on the client side,
// as this would leak your token to the world. Signed URLs should
// be generated on the server.
Expand All @@ -70,55 +70,6 @@ var url = client.buildURL('/path/to/image.png', { w: 400, h: 300 });
console.log(url); // => "https://my-social-network.imgix.net/users/1.png?w=400&h=300"
```

## Domain Sharded URLs
**Warning: Domain Sharding has been deprecated and will be removed in the next major release. As a result, the `domains` argument will be deprecated in favor of `domain` instead.**<br>
To find out more, see our [blog post](https://blog.imgix.com/2019/05/03/deprecating-domain-sharding) explaining the decision to remove this feature.

Domain sharding enables you to spread image requests across multiple domains.
This allows you to bypass the requests-per-host limits of browsers. We
recommend 2-3 domain shards maximum if you are going to use domain sharding.

In order to use domain sharding, you need to add multiple domains to your
source. You then provide a list of these domains to `ImgixClient`.

``` javascript
var ImgixClient = require('imgix-core-js');

var client = new ImgixClient({
domains: ["demos-1.imgix.net", "demos-2.imgix.net", "demos-3.imgix.net"]
});

var url = client.buildURL("/bridge.png", {w: 100, h: 100});
console.log(url); // => "http://demos-2.imgix.net/bridge.png?h=100&w=100"

var url = client.buildURL("/flower.png", {w: 100, h: 100});
console.log(url); // => "http://demos-3.imgix.net/flower.png?h=100&w=100"
```

By default, shards are calculated using a checksum so that the image path
always resolves to the same domain. This improves caching in the browser.
However, you can supply a different strategy that cycles through domains
instead. For example:

``` javascript
var ImgixClient = require('imgix-core-js');

var client = new ImgixClient({
domains: ["demos-1.imgix.net", "demos-2.imgix.net", "demos-3.imgix.net"],
shard_strategy: ImgixClient.SHARD_STRATEGY_CYCLE
});

for(i=0; i<4; i++)
console.log(client.buildURL("/bridge.png", {w: 100, h: 100}));

// Prints out:
// http://demos-1.imgix.net/bridge.png?h=100&w=100
// http://demos-2.imgix.net/bridge.png?h=100&w=100
// http://demos-3.imgix.net/bridge.png?h=100&w=100
// http://demos-1.imgix.net/bridge.png?h=100&w=100
```


## What is the `ixlib` param on every request?

For security and diagnostic purposes, we sign all requests with the language and version of library used to generate the URL.
Expand All @@ -127,7 +78,7 @@ This can be disabled by passing a falsy value for the `includeLibraryParam` opti

``` javascript
new ImgixClient({
domains: 'my-source.imgix.net',
domain: 'my-source.imgix.net',
includeLibraryParam: false
});
```
Expand Down
3 changes: 1 addition & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
],
"dependencies": {
"md5": "~0.3.0",
"js-base64": "~2.1.9",
"crc": "~3.5.0"
"js-base64": "~2.1.9"
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"main": "dist/imgix-core-js.js",
"dependencies": {
"js-base64": "^2.1.9",
"md5": "^2.2.1",
"crc": "^3.5.0"
"md5": "^2.2.1"
},
"license": "BSD-2-Clause",
"devDependencies": {
Expand Down
78 changes: 17 additions & 61 deletions src/imgix-core-js.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define('Imgix', ['exports', 'md5', 'js-base64', 'crc'], factory);
define('Imgix', ['exports', 'md5', 'js-base64'], factory);
} else if (typeof exports !== 'undefined') {
module.exports = factory(exports, require('md5'), require('js-base64').Base64, require('crc'));
module.exports = factory(exports, require('md5'), require('js-base64').Base64);
} else {
var mod = {
exports: {}
};
global.ImgixClient = factory(mod.exports, global.md5, global.Base64, global.crc);
global.ImgixClient = factory(mod.exports, global.md5, global.Base64);
}
})(this, function (exports, _md5, _jsBase64, _crc) {
})(this, function (exports, _md5, _jsBase64) {
var md5 = _md5;
var Base64 = _jsBase64.Base64 || _jsBase64;
var crc = _crc;

var VERSION = '1.4.0';
var SHARD_STRATEGY_CRC = 'crc';
var SHARD_STRATEGY_CYCLE = 'cycle';
var DOMAIN_REGEX = /^(?:[a-z\d\-_]{1,62}\.){0,125}(?:[a-z\d](?:\-(?=\-*[a-z\d])|[a-z]|\d){0,62}\.)[a-z\d]{1,63}$/i;
var DEFAULTS = {
host: null,
domain: null,
domains: [],
useHTTPS: true,
includeLibraryParam: true,
shard_strategy: SHARD_STRATEGY_CRC
includeLibraryParam: true
};

var ImgixClient = (function() {
function ImgixClient(opts) {
var key, val;

this.settings = {};
this._shard_next_index = 0;

for (key in DEFAULTS) {
val = DEFAULTS[key];
Expand All @@ -44,48 +38,23 @@
this.settings[key] = val;
}

if (Array.isArray(this.settings.domains)) {
if (this.settings.domains.length > 1) {
console.warn("Warning: Domain sharding has been deprecated and will be removed in the next major version.\nAs a result, the 'domains' argument will be deprecated in favor of 'domain' instead.");
}
else if (this.settings.domains.length == 0) {
if (typeof(this.settings.domain) != "string" && this.settings.domain != null) {
throw new Error('ImgixClient.settings.domain only accepts a string argument');
}
else {
this.settings.domains = [this.settings.domain];
}
}
}
else {
this.settings.domains = [this.settings.domains];
}

if (!this.settings.host && this.settings.domains == 0) {
throw new Error('ImgixClient must be passed valid domain(s)');
if (this.settings.host) {
console.warn("'host' argument is deprecated; use 'domain' instead.");
if (this.settings.domain.length == 0)
this.settings.domain = this.settings.host;
}

if (this.settings.shard_strategy !== SHARD_STRATEGY_CRC
&& this.settings.shard_strategy !== SHARD_STRATEGY_CYCLE) {
throw new Error('Shard strategy must be one of ' +
SHARD_STRATEGY_CRC + ' or ' + SHARD_STRATEGY_CYCLE);
if (typeof this.settings.domain != "string") {
throw new Error('ImgixClient must be passed a valid string domain');
}

if (this.settings.host) {
console.warn("'host' argument is deprecated; either use 'domain' or 'domains' instead.");
if (this.settings.domains.length == 0)
this.settings.domains[0] = this.settings.host;
if (DOMAIN_REGEX.exec(this.settings.domain) == null) {
throw new Error(
'Domain must be passed in as fully-qualified ' +
'domain name and should not include a protocol or any path ' +
'element, i.e. "example.imgix.net".');
}

this.settings.domains.forEach(function(domain) {
if (DOMAIN_REGEX.exec(domain) == null) {
throw new Error(
'Domains must be passed in as fully-qualified ' +
'domain names and should not include a protocol or any path ' +
'element, i.e. "example.imgix.net".');
}
});

if (this.settings.includeLibraryParam) {
this.settings.libraryParam = "js-" + VERSION;
}
Expand All @@ -105,20 +74,9 @@
queryParams = this._signParams(path, queryParams);
}

return this.settings.urlPrefix + this._getDomain(path) + path + queryParams;
return this.settings.urlPrefix + this.settings.domain + path + queryParams;
};

ImgixClient.prototype._getDomain = function(path) {
if (this.settings.shard_strategy === SHARD_STRATEGY_CYCLE) {
var domain = this.settings.domains[this._shard_next_index];
this._shard_next_index = (this._shard_next_index + 1) % this.settings.domains.length;
return domain;
}
else if (this.settings.shard_strategy === SHARD_STRATEGY_CRC) {
return this.settings.domains[crc.crc32(path) % this.settings.domains.length];
}
}

ImgixClient.prototype._sanitizePath = function(path) {
// Strip leading slash first (we'll re-add after encoding)
path = path.replace(/^\//, '');
Expand Down Expand Up @@ -174,8 +132,6 @@
};

ImgixClient.VERSION = VERSION;
ImgixClient.SHARD_STRATEGY_CRC = SHARD_STRATEGY_CRC;
ImgixClient.SHARD_STRATEGY_CYCLE = SHARD_STRATEGY_CYCLE;

return ImgixClient;
})();
Expand Down
Loading

0 comments on commit 66d5cd9

Please sign in to comment.