Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
feat: add method to calc. bundle urls from hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalsadhu committed Mar 8, 2018
1 parent 26b313f commit 45570a5
Show file tree
Hide file tree
Showing 7 changed files with 1,836 additions and 88 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,35 @@ await client.publishInstructions('layout', 'js', ['podlet1', 'podlet2'], {
});
```

### bundleURL(feedHashes, options)

Calculates a bundle url string based on a given array of hashes which are asset feed content hashes. Each time an asset feed is published using `client.publishAssets` the resolved object will contain an `id` property which is the hash of the feed content and can be used with this method.

Calculation is done by sha256 hashing together the given `hashes` and dropping the resulting hash into a url template.

As such, this method does not perform any requests to the server and therefore cannot guarantee that the bundle exists on the server.

* `hashes` - `string[]` - array of asset feed content hashes as returned by `client.publishAssets`
* `options` - `object`
* `options.prefix` - `string` url prefix to use when building bundle url. Defaults to `${client.buildServerUri}/bundle/` which is the location on the asset server that a bundle can be located. Overwrite this if you use a CDN and need to point to that.
* `options.type` - `string` (`js`|`css`) - file type. Defaults to `js`

`return` - `Promise<string>` - url for asset bundle on asset server.

**Example**

```js
// publish instructions
await client.publishInstructions('layout', 'js', ['podlet1', 'podlet2']);

// publish necessary assets
const { uri, id1 } = await client.publishAssets('podlet1', ['/path/to/file.js']);
const { uri, id2 } = await client.publishAssets('podlet2', ['/path/to/file.js']);

// calculate the url of the finished bundle
const url = await client.bundleURL([id1, id2]);
```

## Transpilers

Since [asset-pipe][asset-pipe] is built on [browserify][browserify] under the
Expand Down
20 changes: 20 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { extname } = require('path');
const isStream = require('is-stream');
const Joi = require('joi');
const Boom = require('boom');
const { hashArray } = require('@asset-pipe/common');
const schemas = require('./schemas');
const { buildURL } = require('../lib/utils');

Expand Down Expand Up @@ -359,4 +360,23 @@ module.exports = class Client {
}
);
}

bundleHash(feedHashes) {
return hashArray(feedHashes);
}

bundleFilename(hash, type) {
return `${hash}.${type}`;
}

async bundleURL(feedHashes, options = {}) {
const { type, prefix } = {
prefix: url.resolve(this.buildServerUri, '/bundle/'),
type: 'js',
...options,
};
const hash = await this.bundleHash(feedHashes);
const filename = this.bundleFilename(hash, type);
return url.resolve(prefix, filename);
}
};
12 changes: 12 additions & 0 deletions lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,23 @@ const options = Joi.object()
.label('options object')
.optional();

const hash = Joi.string()
.label('hash')
.regex(/^[0-9A-Fa-f]+$/)
.required();

const hashArray = Joi.array()
.label('hash array')
.items(hash)
.required();

module.exports = {
file,
files,
tag,
type,
bundleInstruction,
options,
hash,
hashArray,
};
Loading

0 comments on commit 45570a5

Please sign in to comment.