Skip to content

Commit 042300a

Browse files
author
Ray Schamp
committed
feat: replace "sources" with "stores"
Sources are only for fetching, but stores allow fetching/creating/updating. Add deprecation warnings when attempting to access "sources". addStore accepts two additional functions, for creation and updates. Additionally, the functions can return strings or an object if additional configuration to the request to the URL is necessary. The attributes of the object are passed as options to `nets`. BREAKING CHANGE: WebHelper.sources has been replaced by WebHelper.stores. WebHelper.addSource has been replaced by WebHelper.addStore. Storage.addWebSource has been replaced by Storage.addWebStore. BuiltinHelper.cache has been normalized to BuiltinHelper.store.
1 parent c4a4a3d commit 042300a

File tree

3 files changed

+83
-24
lines changed

3 files changed

+83
-24
lines changed

src/BuiltinHelper.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const md5 = require('js-md5');
22

3+
const log = require('./log');
4+
35
const Asset = require('./Asset');
46
const AssetType = require('./AssetType');
57
const DataFormat = require('./DataFormat');
@@ -93,14 +95,28 @@ class BuiltinHelper extends Helper {
9395
}
9496

9597
/**
96-
* Cache an asset for future lookups by ID.
98+
* Alias for store (old name of store)
99+
* @deprecated Use BuiltinHelper.store
97100
* @param {AssetType} assetType - The type of the asset to cache.
98101
* @param {DataFormat} dataFormat - The dataFormat of the data for the cached asset.
99102
* @param {Buffer} data - The data for the cached asset.
100103
* @param {string} id - The id for the cached asset.
101104
* @returns {string} The calculated id of the cached asset, or the supplied id if the asset is mutable.
102105
*/
103106
cache (assetType, dataFormat, data, id) {
107+
log.warn('Deprecation: BuiltinHelper.cache has been replaced with BuiltinHelper.store.');
108+
return this.store(assetType, dataFormat, data, id);
109+
}
110+
111+
/**
112+
* Cache an asset for future lookups by ID.
113+
* @param {AssetType} assetType - The type of the asset to cache.
114+
* @param {DataFormat} dataFormat - The dataFormat of the data for the cached asset.
115+
* @param {Buffer} data - The data for the cached asset.
116+
* @param {string} id - The id for the cached asset.
117+
* @returns {string} The calculated id of the cached asset, or the supplied id if the asset is mutable.
118+
*/
119+
store (assetType, dataFormat, data, id) {
104120
if (!dataFormat) throw new Error('Data cached without specifying its format');
105121
if (id !== null && typeof id !== 'undefined') {
106122
if (this.assets.hasOwnProperty(id) && assetType.immutable) return id;

src/ScratchStorage.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const log = require('./log');
2+
13
const BuiltinHelper = require('./BuiltinHelper');
24
const WebHelper = require('./WebHelper');
35

@@ -74,16 +76,29 @@ class ScratchStorage {
7476
* @returns {string} The calculated id of the cached asset, or the supplied id if the asset is mutable.
7577
*/
7678
cache (assetType, dataFormat, data, id) {
77-
return this.builtinHelper.cache(assetType, dataFormat, data, id);
79+
return this.builtinHelper.store(assetType, dataFormat, data, id);
80+
}
81+
82+
/**
83+
* Register a web-based source for assets. Sources will be checked in order of registration.
84+
* @param {Array.<AssetType>} types - The types of asset provided by this source.
85+
* @param {UrlFunction} getFunction - A function which computes a GET URL from an Asset.
86+
* @param {UrlFunction} createFunction - A function which computes a POST URL for asset data.
87+
* @param {UrlFunction} updateFunction - A function which computes a PUT URL for asset data.
88+
*/
89+
addWebStore (types, getFunction, createFunction, updateFunction) {
90+
this.webHelper.addStore(types, getFunction, createFunction, updateFunction);
7891
}
7992

8093
/**
8194
* Register a web-based source for assets. Sources will be checked in order of registration.
95+
* @deprecated Please use addWebStore
8296
* @param {Array.<AssetType>} types - The types of asset provided by this source.
83-
* @param {UrlFunction} urlFunction - A function which computes a URL from an Asset.
97+
* @param {UrlFunction} urlFunction - A function which computes a GET URL from an Asset.
8498
*/
8599
addWebSource (types, urlFunction) {
86-
this.webHelper.addSource(types, urlFunction);
100+
log.warn('Deprecation: Storage.addWebSource has been replaced by addWebStore.');
101+
this.addWebStore(types, urlFunction);
87102
}
88103

89104
/**

src/WebHelper.js

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
11
const nets = require('nets');
22

3+
const log = require('./log');
4+
35
const Asset = require('./Asset');
46
const Helper = require('./Helper');
57

68
/**
79
* @typedef {function} UrlFunction - A function which computes a URL from asset information.
810
* @param {Asset} - The asset for which the URL should be computed.
9-
* @returns {string} - The URL for the asset.
11+
* @returns {(string|object)} - A string representing the URL for the asset request OR an object with configuration for
12+
* the underlying `nets` call (necessary for configuring e.g. authentication)
1013
*/
1114

1215
class WebHelper extends Helper {
1316
constructor (parent) {
1417
super(parent);
1518

1619
/**
17-
* @type {Array.<SourceRecord>}
18-
* @typedef {object} SourceRecord
19-
* @property {Array.<string>} types - The types of asset provided by this source, from AssetType's name field.
20-
* @property {UrlFunction} urlFunction - A function which computes a URL from an Asset.
20+
* @type {Array.<StoreRecord>}
21+
* @typedef {object} StoreRecord
22+
* @property {Array.<string>} types - The types of asset provided by this store, from AssetType's name field.
23+
* @property {UrlFunction} getFunction - A function which computes a URL from an Asset.
24+
* @property {UrlFunction} createFunction - A function which computes a URL from an Asset.
25+
* @property {UrlFunction} updateFunction - A function which computes a URL from an Asset.
2126
*/
22-
this.sources = [];
27+
this.stores = [];
2328
}
2429

2530
/**
2631
* Register a web-based source for assets. Sources will be checked in order of registration.
32+
* @deprecated Please use addStore
2733
* @param {Array.<AssetType>} types - The types of asset provided by this source.
2834
* @param {UrlFunction} urlFunction - A function which computes a URL from an Asset.
2935
*/
3036
addSource (types, urlFunction) {
31-
this.sources.push({
37+
log.warn('Deprecation: WebHelper.addSource has been replaced with WebHelper.addStore.');
38+
this.addStore(types, urlFunction);
39+
}
40+
41+
/**
42+
* Register a web-based source for assets. Sources will be checked in order of registration.
43+
* @param {Array.<AssetType>} types - The types of asset provided by this source.
44+
* @param {UrlFunction} getFunction - A function which computes a GET URL for an Asset
45+
* @param {UrlFunction} createFunction - A function which computes a POST URL for an Asset
46+
* @param {UrlFunction} updateFunction - A function which computes a PUT URL for an Asset
47+
*/
48+
addStore (types, getFunction, createFunction, updateFunction) {
49+
this.stores.push({
3250
types: types.map(assetType => assetType.name),
33-
urlFunction: urlFunction
51+
get: getFunction,
52+
create: createFunction,
53+
update: updateFunction
3454
});
3555
}
3656

@@ -45,34 +65,42 @@ class WebHelper extends Helper {
4565

4666
/** @type {Array.<{url:string, result:*}>} List of URLs attempted & errors encountered. */
4767
const errors = [];
48-
const sources = this.sources.slice();
68+
const stores = this.stores.slice();
4969
const asset = new Asset(assetType, assetId, dataFormat);
50-
let sourceIndex = 0;
70+
let storeIndex = 0;
5171

5272
return new Promise((fulfill, reject) => {
5373

5474
const tryNextSource = () => {
5575

5676
/** @type {UrlFunction} */
57-
let urlFunction;
77+
let reqConfigFunction;
5878

59-
while (sourceIndex < sources.length) {
60-
const source = sources[sourceIndex];
61-
++sourceIndex;
62-
if (source.types.indexOf(assetType.name) >= 0) {
63-
urlFunction = source.urlFunction;
79+
while (storeIndex < stores.length) {
80+
const store = stores[storeIndex];
81+
++storeIndex;
82+
if (store.types.indexOf(assetType.name) >= 0) {
83+
reqConfigFunction = store.get;
6484
break;
6585
}
6686
}
6787

68-
if (urlFunction) {
69-
const url = urlFunction(asset);
70-
if (url === false) {
88+
if (reqConfigFunction) {
89+
let reqConfig = reqConfigFunction(asset);
90+
if (reqConfig === false) {
7191
tryNextSource();
7292
return;
7393
}
94+
if (typeof reqConfig === 'string') {
95+
reqConfig = {
96+
url: reqConfig
97+
};
98+
}
7499

75-
nets({url: url}, (err, resp, body) => {
100+
nets({
101+
method: 'get',
102+
...reqConfig
103+
}, (err, resp, body) => {
76104
// body is a Buffer
77105
if (err || Math.floor(resp.statusCode / 100) !== 2) {
78106
tryNextSource();

0 commit comments

Comments
 (0)