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.
- package/dist/node/scratch-storage.js +46 -39
- package/dist/node/scratch-storage.js.map +1 -1
- package/dist/types/WebHelper.d.ts +11 -5
- package/dist/web/scratch-storage.js +46 -39
- package/dist/web/scratch-storage.js.map +1 -1
- package/dist/web/scratch-storage.min.js +1 -1
- package/package.json +7 -7
- package/src/WebHelper.ts +89 -65
- package/test/integration/download-known-assets.test.js +47 -0
|
@@ -4006,7 +4006,12 @@ function WebHelper_toPrimitive(t, r) { if ("object" != typeof t || !t) return t;
|
|
|
4006
4006
|
|
|
4007
4007
|
|
|
4008
4008
|
|
|
4009
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
4098
|
-
|
|
4099
|
-
|
|
4100
|
-
|
|
4101
|
-
|
|
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
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
return Promise.reject(errors);
|
|
4106
|
+
} catch (err) {
|
|
4107
|
+
errors.push(err);
|
|
4108
|
+
}
|
|
4110
4109
|
}
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
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
|
-
|
|
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))
|
|
4134
|
+
create && s.create || s.update));
|
|
4135
4135
|
const method = create ? 'post' : 'put';
|
|
4136
|
-
if (
|
|
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
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
|
|
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
|