scratch-storage 6.1.10 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4006,7 +4006,12 @@ function WebHelper_toPrimitive(t, r) { if ("object" != typeof t || !t) return t;
4006
4006
 
4007
4007
 
4008
4008
 
4009
- const ensureRequestConfig = reqConfig => {
4009
+ /**
4010
+ * Ensure that the provided request configuration is in object form, converting from
4011
+ * string if necessary.
4012
+ */
4013
+ const ensureRequestConfig = async reqConfig => {
4014
+ reqConfig = await reqConfig;
4010
4015
  if (typeof reqConfig === 'string') {
4011
4016
  return {
4012
4017
  url: reqConfig
@@ -4075,8 +4080,8 @@ class WebHelper extends Helper {
4075
4080
  * @param {DataFormat} dataFormat - The file format / file extension of the asset to fetch: PNG, JPG, etc.
4076
4081
  * @returns {Promise.<Asset>} A promise for the contents of the asset.
4077
4082
  */
4078
- load(assetType, assetId, dataFormat) {
4079
- /** @type {Array.<{url:string, result:*}>} List of URLs attempted & errors encountered. */
4083
+ async load(assetType, assetId, dataFormat) {
4084
+ /** @type {unknown[]} List of errors encountered while attempting to load the asset. */
4080
4085
  const errors = [];
4081
4086
  const stores = this.stores.slice().filter(store => store.types.indexOf(assetType.name) >= 0);
4082
4087
  // New empty asset but it doesn't have data yet
@@ -4085,33 +4090,29 @@ class WebHelper extends Helper {
4085
4090
  if (assetType.name === 'Project') {
4086
4091
  tool = this.projectTool;
4087
4092
  }
4088
- let storeIndex = 0;
4089
- const tryNextSource = err => {
4090
- if (err) {
4091
- errors.push(err);
4092
- }
4093
- const store = stores[storeIndex++];
4094
- /** @type {UrlFunction} */
4093
+ for (const store of stores) {
4095
4094
  const reqConfigFunction = store && store.get;
4096
4095
  if (reqConfigFunction) {
4097
- const reqConfig = ensureRequestConfig(reqConfigFunction(asset));
4098
- if (reqConfig === false) {
4099
- return tryNextSource();
4100
- }
4101
- return tool.get(reqConfig).then(body => {
4096
+ try {
4097
+ const reqConfig = await ensureRequestConfig(reqConfigFunction(asset));
4098
+ if (!reqConfig) {
4099
+ continue;
4100
+ }
4101
+ const body = await tool.get(reqConfig);
4102
4102
  if (body) {
4103
4103
  asset.setData(body, dataFormat);
4104
4104
  return asset;
4105
4105
  }
4106
- return tryNextSource();
4107
- }).catch(tryNextSource);
4108
- } else if (errors.length > 0) {
4109
- return Promise.reject(errors);
4106
+ } catch (err) {
4107
+ errors.push(err);
4108
+ }
4110
4109
  }
4111
- // no stores matching asset
4112
- return Promise.resolve(null);
4113
- };
4114
- return tryNextSource();
4110
+ }
4111
+ if (errors.length > 0) {
4112
+ return Promise.reject(errors);
4113
+ }
4114
+ // no stores matching asset
4115
+ return Promise.resolve(null);
4115
4116
  }
4116
4117
  /**
4117
4118
  * Create or update an asset with provided data. The create function is called if no asset id is provided
@@ -4121,33 +4122,38 @@ class WebHelper extends Helper {
4121
4122
  * @param {?string} assetId - The ID of the asset to fetch: a project ID, MD5, etc.
4122
4123
  * @returns {Promise.<object>} A promise for the response from the create or update request
4123
4124
  */
4124
- store(assetType, dataFormat, data, assetId) {
4125
+ async store(assetType, dataFormat, data, assetId) {
4125
4126
  const asset = new Asset(assetType, assetId, dataFormat);
4126
4127
  // If we have an asset id, we should update, otherwise create to get an id
4127
4128
  const create = assetId === '' || assetId === null || typeof assetId === 'undefined';
4128
- // Use the first store with the appropriate asset type and url function
4129
- const store = this.stores.filter(s =>
4129
+ const candidateStores = this.stores.filter(s =>
4130
4130
  // Only use stores for the incoming asset type
4131
4131
  s.types.indexOf(assetType.name) !== -1 && (
4132
4132
  // Only use stores that have a create function if this is a create request
4133
4133
  // or an update function if this is an update request
4134
- create && s.create || s.update))[0];
4134
+ create && s.create || s.update));
4135
4135
  const method = create ? 'post' : 'put';
4136
- if (!store) return Promise.reject(new Error('No appropriate stores'));
4136
+ if (candidateStores.length === 0) {
4137
+ return Promise.reject(new Error('No appropriate stores'));
4138
+ }
4137
4139
  let tool = this.assetTool;
4138
4140
  if (assetType.name === 'Project') {
4139
4141
  tool = this.projectTool;
4140
4142
  }
4141
- const reqConfig = ensureRequestConfig(
4142
- // The non-nullability of this gets checked above while looking up the store.
4143
- // Making TS understand that is going to require code refactoring which we currently don't
4144
- // feel safe to do.
4145
- create ? store.create(asset) : store.update(asset));
4146
- const reqBodyConfig = Object.assign({
4147
- body: data,
4148
- method
4149
- }, reqConfig);
4150
- return tool.send(reqBodyConfig).then(body => {
4143
+ for (const store of candidateStores) {
4144
+ const reqConfig = await ensureRequestConfig(
4145
+ // The non-nullability of this gets checked above while looking up the store.
4146
+ // Making TS understand that is going to require code refactoring which we currently don't
4147
+ // feel safe to do.
4148
+ create ? store.create(asset) : store.update(asset));
4149
+ if (!reqConfig) {
4150
+ continue;
4151
+ }
4152
+ const reqBodyConfig = Object.assign({
4153
+ body: data,
4154
+ method
4155
+ }, reqConfig);
4156
+ let body = await tool.send(reqBodyConfig);
4151
4157
  // xhr makes it difficult to both send FormData and
4152
4158
  // automatically parse a JSON response. So try to parse
4153
4159
  // everything as JSON.
@@ -4164,7 +4170,8 @@ class WebHelper extends Helper {
4164
4170
  return Object.assign({
4165
4171
  id: body['content-name'] || assetId
4166
4172
  }, body);
4167
- });
4173
+ }
4174
+ return Promise.reject(new Error('No store could handle the request'));
4168
4175
  }
4169
4176
  }
4170
4177
  ;// ./src/ScratchStorage.ts