scratch-storage 1.3.4 → 2.0.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/.editorconfig +1 -1
- package/.gitattributes +1 -0
- package/dist/node/50604be620465cc30dd8.worker.js +1906 -0
- package/dist/node/50604be620465cc30dd8.worker.js.map +1 -0
- package/dist/node/scratch-storage.js +1743 -21
- package/dist/node/scratch-storage.js.map +1 -1
- package/dist/{node/4816ce2aa9bf12df8b86.worker.js → web/3770307661f114ec2389.worker.js} +5 -3
- package/dist/{node/4816ce2aa9bf12df8b86.worker.js.map → web/3770307661f114ec2389.worker.js.map} +1 -1
- package/dist/web/scratch-storage.js +36 -20
- package/dist/web/scratch-storage.js.map +1 -1
- package/dist/web/scratch-storage.min.js +36 -20
- package/dist/web/scratch-storage.min.js.map +1 -1
- package/package.json +15 -15
- package/renovate.json5 +7 -0
- package/src/BuiltinHelper.js +3 -3
- package/src/FetchTool.js +5 -2
- package/src/FetchWorkerTool.worker.js +5 -1
- package/src/ScratchStorage.js +4 -4
- package/src/WebHelper.js +15 -4
- package/test/integration/download-known-assets.js +1 -2
- package/test/unit/fetch-tool.js +53 -5
- package/webpack.config.js +7 -1
- package/dist/web/3325b91e8645157f8dc6.worker.js +0 -188
- package/dist/web/3325b91e8645157f8dc6.worker.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "scratch-storage",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Load and store project and asset files for Scratch 3.0",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"homepage": "https://github.com/LLK/scratch-storage#readme",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "https://github.com/LLK/scratch-storage.git",
|
|
10
|
-
"sha": "
|
|
10
|
+
"sha": "6aa3ebea57319215450963b29455cbb344ee59fd"
|
|
11
11
|
},
|
|
12
12
|
"main": "./dist/node/scratch-storage.js",
|
|
13
13
|
"browser": "./src/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"semantic-release": "semantic-release"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"arraybuffer-loader": "^1.0.
|
|
28
|
+
"arraybuffer-loader": "^1.0.3",
|
|
29
29
|
"base64-js": "1.3.0",
|
|
30
30
|
"fastestsmallesttextencoderdecoder": "^1.0.7",
|
|
31
31
|
"js-md5": "0.7.3",
|
|
@@ -33,27 +33,27 @@
|
|
|
33
33
|
"worker-loader": "^2.0.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@babel/core": "
|
|
37
|
-
"@babel/polyfill": "7.
|
|
38
|
-
"@babel/preset-env": "
|
|
36
|
+
"@babel/core": "7.14.8",
|
|
37
|
+
"@babel/polyfill": "7.12.1",
|
|
38
|
+
"@babel/preset-env": "7.14.8",
|
|
39
39
|
"@commitlint/cli": "8.2.0",
|
|
40
40
|
"@commitlint/config-conventional": "8.2.0",
|
|
41
41
|
"@commitlint/travis-cli": "8.2.0",
|
|
42
42
|
"babel-eslint": "10.1.0",
|
|
43
|
-
"babel-loader": "
|
|
44
|
-
"cz-conventional-changelog": "
|
|
45
|
-
"eslint": "
|
|
46
|
-
"eslint-config-scratch": "
|
|
47
|
-
"eslint-plugin-react": "
|
|
43
|
+
"babel-loader": "8.0.6",
|
|
44
|
+
"cz-conventional-changelog": "3.3.0",
|
|
45
|
+
"eslint": "7.27.0",
|
|
46
|
+
"eslint-config-scratch": "6.0.0",
|
|
47
|
+
"eslint-plugin-react": "7.24.0",
|
|
48
48
|
"file-loader": "4.1.0",
|
|
49
49
|
"husky": "1.3.1",
|
|
50
50
|
"json": "^9.0.4",
|
|
51
|
-
"node-fetch": "
|
|
51
|
+
"node-fetch": "2.6.1",
|
|
52
52
|
"semantic-release": "^15.10.5",
|
|
53
53
|
"tap": "12.1.1",
|
|
54
|
-
"uglifyjs-webpack-plugin": "
|
|
55
|
-
"webpack": "
|
|
56
|
-
"webpack-cli": "
|
|
54
|
+
"uglifyjs-webpack-plugin": "2.2.0",
|
|
55
|
+
"webpack": "4.46.0",
|
|
56
|
+
"webpack-cli": "3.1.2"
|
|
57
57
|
},
|
|
58
58
|
"config": {
|
|
59
59
|
"commitizen": {
|
package/renovate.json5
ADDED
package/src/BuiltinHelper.js
CHANGED
|
@@ -23,7 +23,7 @@ const DefaultAssets = [
|
|
|
23
23
|
type: AssetType.ImageBitmap,
|
|
24
24
|
format: DataFormat.PNG,
|
|
25
25
|
id: null,
|
|
26
|
-
data:
|
|
26
|
+
data: Buffer.from(
|
|
27
27
|
require('!arraybuffer-loader!./builtins/defaultBitmap.png') // eslint-disable-line global-require
|
|
28
28
|
)
|
|
29
29
|
},
|
|
@@ -31,7 +31,7 @@ const DefaultAssets = [
|
|
|
31
31
|
type: AssetType.Sound,
|
|
32
32
|
format: DataFormat.WAV,
|
|
33
33
|
id: null,
|
|
34
|
-
data:
|
|
34
|
+
data: Buffer.from(
|
|
35
35
|
require('!arraybuffer-loader!./builtins/defaultSound.wav') // eslint-disable-line global-require
|
|
36
36
|
)
|
|
37
37
|
},
|
|
@@ -39,7 +39,7 @@ const DefaultAssets = [
|
|
|
39
39
|
type: AssetType.ImageVector,
|
|
40
40
|
format: DataFormat.SVG,
|
|
41
41
|
id: null,
|
|
42
|
-
data:
|
|
42
|
+
data: Buffer.from(
|
|
43
43
|
require('!arraybuffer-loader!./builtins/defaultVector.svg') // eslint-disable-line global-require
|
|
44
44
|
)
|
|
45
45
|
}
|
package/src/FetchTool.js
CHANGED
|
@@ -20,8 +20,11 @@ class FetchTool {
|
|
|
20
20
|
*/
|
|
21
21
|
get ({url, ...options}) {
|
|
22
22
|
return fetch(url, Object.assign({method: 'GET'}, options))
|
|
23
|
-
.then(result =>
|
|
24
|
-
|
|
23
|
+
.then(result => {
|
|
24
|
+
if (result.ok) return result.arrayBuffer().then(b => new Uint8Array(b));
|
|
25
|
+
if (result.status === 404) return null;
|
|
26
|
+
return Promise.reject(result.status);
|
|
27
|
+
});
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
/**
|
|
@@ -49,7 +49,11 @@ const onMessage = ({data: job}) => {
|
|
|
49
49
|
jobsActive++;
|
|
50
50
|
|
|
51
51
|
fetch(job.url, job.options)
|
|
52
|
-
.then(
|
|
52
|
+
.then(result => {
|
|
53
|
+
if (result.ok) return result.arrayBuffer();
|
|
54
|
+
if (result.status === 404) return null;
|
|
55
|
+
return Promise.reject(result.status);
|
|
56
|
+
})
|
|
53
57
|
.then(buffer => complete.push({id: job.id, buffer}))
|
|
54
58
|
.catch(error => complete.push({id: job.id, error}))
|
|
55
59
|
.then(() => jobsActive--);
|
package/src/ScratchStorage.js
CHANGED
|
@@ -168,7 +168,7 @@ class ScratchStorage {
|
|
|
168
168
|
* @param {string} assetId - The ID of the asset to fetch: a project ID, MD5, etc.
|
|
169
169
|
* @param {DataFormat} [dataFormat] - Optional: load this format instead of the AssetType's default.
|
|
170
170
|
* @return {Promise.<Asset>} A promise for the requested Asset.
|
|
171
|
-
* If the promise is resolved with non-null, the value is the requested asset
|
|
171
|
+
* If the promise is resolved with non-null, the value is the requested asset.
|
|
172
172
|
* If the promise is resolved with null, the desired asset could not be found with the current asset sources.
|
|
173
173
|
* If the promise is rejected, there was an error on at least one asset source. HTTP 404 does not count as an
|
|
174
174
|
* error here, but (for example) HTTP 403 does.
|
|
@@ -182,7 +182,7 @@ class ScratchStorage {
|
|
|
182
182
|
let helperIndex = 0;
|
|
183
183
|
let helper;
|
|
184
184
|
const tryNextHelper = err => {
|
|
185
|
-
if (err) {
|
|
185
|
+
if (err) { // Track the error, but continue looking
|
|
186
186
|
errors.push(err);
|
|
187
187
|
}
|
|
188
188
|
|
|
@@ -198,8 +198,8 @@ class ScratchStorage {
|
|
|
198
198
|
// TODO: maybe some types of error should prevent trying the next helper?
|
|
199
199
|
.catch(tryNextHelper);
|
|
200
200
|
} else if (errors.length > 0) {
|
|
201
|
-
//
|
|
202
|
-
//
|
|
201
|
+
// We looked through all the helpers and couldn't find the asset, AND
|
|
202
|
+
// at least one thing went wrong while we were looking.
|
|
203
203
|
return Promise.reject(errors);
|
|
204
204
|
}
|
|
205
205
|
|
package/src/WebHelper.js
CHANGED
|
@@ -90,6 +90,8 @@ class WebHelper extends Helper {
|
|
|
90
90
|
const errors = [];
|
|
91
91
|
const stores = this.stores.slice()
|
|
92
92
|
.filter(store => store.types.indexOf(assetType.name) >= 0);
|
|
93
|
+
|
|
94
|
+
// New empty asset but it doesn't have data yet
|
|
93
95
|
const asset = new Asset(assetType, assetId, dataFormat);
|
|
94
96
|
|
|
95
97
|
let tool = this.assetTool;
|
|
@@ -98,11 +100,14 @@ class WebHelper extends Helper {
|
|
|
98
100
|
}
|
|
99
101
|
|
|
100
102
|
let storeIndex = 0;
|
|
101
|
-
const tryNextSource =
|
|
103
|
+
const tryNextSource = err => {
|
|
104
|
+
if (err) {
|
|
105
|
+
errors.push(err);
|
|
106
|
+
}
|
|
102
107
|
const store = stores[storeIndex++];
|
|
103
108
|
|
|
104
109
|
/** @type {UrlFunction} */
|
|
105
|
-
const reqConfigFunction = store.get;
|
|
110
|
+
const reqConfigFunction = store && store.get;
|
|
106
111
|
|
|
107
112
|
if (reqConfigFunction) {
|
|
108
113
|
const reqConfig = ensureRequestConfig(reqConfigFunction(asset));
|
|
@@ -111,7 +116,13 @@ class WebHelper extends Helper {
|
|
|
111
116
|
}
|
|
112
117
|
|
|
113
118
|
return tool.get(reqConfig)
|
|
114
|
-
.then(body =>
|
|
119
|
+
.then(body => {
|
|
120
|
+
if (body) {
|
|
121
|
+
asset.setData(body, dataFormat);
|
|
122
|
+
return asset;
|
|
123
|
+
}
|
|
124
|
+
return tryNextSource();
|
|
125
|
+
})
|
|
115
126
|
.catch(tryNextSource);
|
|
116
127
|
} else if (errors.length > 0) {
|
|
117
128
|
return Promise.reject(errors);
|
|
@@ -121,7 +132,7 @@ class WebHelper extends Helper {
|
|
|
121
132
|
return Promise.resolve(null);
|
|
122
133
|
};
|
|
123
134
|
|
|
124
|
-
return tryNextSource()
|
|
135
|
+
return tryNextSource();
|
|
125
136
|
}
|
|
126
137
|
|
|
127
138
|
/**
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
const md5 = require('js-md5');
|
|
2
2
|
const test = require('tap').test;
|
|
3
|
-
// Polyfill the fetch API
|
|
4
|
-
global.fetch = require('node-fetch');
|
|
5
3
|
|
|
6
4
|
const ScratchStorage = require('../../dist/node/scratch-storage');
|
|
7
5
|
|
|
@@ -18,6 +16,7 @@ test('constructor', t => {
|
|
|
18
16
|
* @typedef {object} AssetTestInfo
|
|
19
17
|
* @property {AssetType} type - The type of the asset.
|
|
20
18
|
* @property {string} id - The asset's unique ID.
|
|
19
|
+
* @property {string} md5 - The asset's MD5 hash.
|
|
21
20
|
* @property {DataFormat} [ext] - Optional: the asset's data format / file extension.
|
|
22
21
|
*/
|
|
23
22
|
const testAssets = [
|
package/test/unit/fetch-tool.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
const test = require('tap').test;
|
|
2
|
+
const TextEncoder = require('util').TextEncoder;
|
|
3
|
+
const TextDecoder = require('util').TextDecoder;
|
|
2
4
|
|
|
3
5
|
const FetchTool = require('../../src/FetchTool');
|
|
4
6
|
|
|
@@ -10,9 +12,11 @@ test('send success returns response.text()', t => {
|
|
|
10
12
|
|
|
11
13
|
const tool = new FetchTool();
|
|
12
14
|
|
|
13
|
-
return
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
return t.resolves(
|
|
16
|
+
tool.send('url').then(result => {
|
|
17
|
+
t.equal(result, 'successful response');
|
|
18
|
+
})
|
|
19
|
+
);
|
|
16
20
|
});
|
|
17
21
|
|
|
18
22
|
test('send failure returns response.status', t => {
|
|
@@ -23,7 +27,51 @@ test('send failure returns response.status', t => {
|
|
|
23
27
|
|
|
24
28
|
const tool = new FetchTool();
|
|
25
29
|
|
|
26
|
-
return tool.send('url')
|
|
27
|
-
|
|
30
|
+
return t.rejects(tool.send('url'), 500);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('get success returns Uint8Array.body(response.arrayBuffer())', t => {
|
|
34
|
+
const text = 'successful response';
|
|
35
|
+
const encoding = 'utf-8';
|
|
36
|
+
const encoded = new TextEncoder().encode(text);
|
|
37
|
+
const decoder = new TextDecoder(encoding);
|
|
38
|
+
|
|
39
|
+
global.fetch = () => Promise.resolve({
|
|
40
|
+
ok: true,
|
|
41
|
+
arrayBuffer: () => Promise.resolve(encoded.buffer)
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const tool = new FetchTool();
|
|
45
|
+
|
|
46
|
+
return t.resolves(
|
|
47
|
+
tool.get({url: 'url'}).then(result => {
|
|
48
|
+
t.equal(decoder.decode(result), text);
|
|
49
|
+
})
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('get with 404 response returns null data', t => {
|
|
54
|
+
global.fetch = () => Promise.resolve({
|
|
55
|
+
ok: false,
|
|
56
|
+
status: 404
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const tool = new FetchTool();
|
|
60
|
+
|
|
61
|
+
return t.resolves(
|
|
62
|
+
tool.get('url').then(result => {
|
|
63
|
+
t.equal(result, null);
|
|
64
|
+
})
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('get failure returns response.status', t => {
|
|
69
|
+
global.fetch = () => Promise.resolve({
|
|
70
|
+
ok: false,
|
|
71
|
+
status: 500
|
|
28
72
|
});
|
|
73
|
+
|
|
74
|
+
const tool = new FetchTool();
|
|
75
|
+
|
|
76
|
+
return t.rejects(tool.get({url: 'url'}), 500);
|
|
29
77
|
});
|
package/webpack.config.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
|
+
const {ProvidePlugin} = require('webpack');
|
|
2
3
|
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
|
|
3
4
|
|
|
4
5
|
const base = {
|
|
@@ -64,6 +65,11 @@ module.exports = [
|
|
|
64
65
|
'js-md5': true,
|
|
65
66
|
'localforage': true,
|
|
66
67
|
'text-encoding': true
|
|
67
|
-
}
|
|
68
|
+
},
|
|
69
|
+
plugins: [
|
|
70
|
+
new ProvidePlugin({
|
|
71
|
+
fetch: ['node-fetch', 'default']
|
|
72
|
+
})
|
|
73
|
+
]
|
|
68
74
|
})
|
|
69
75
|
];
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
/******/ (function(modules) { // webpackBootstrap
|
|
2
|
-
/******/ // The module cache
|
|
3
|
-
/******/ var installedModules = {};
|
|
4
|
-
/******/
|
|
5
|
-
/******/ // The require function
|
|
6
|
-
/******/ function __webpack_require__(moduleId) {
|
|
7
|
-
/******/
|
|
8
|
-
/******/ // Check if module is in cache
|
|
9
|
-
/******/ if(installedModules[moduleId]) {
|
|
10
|
-
/******/ return installedModules[moduleId].exports;
|
|
11
|
-
/******/ }
|
|
12
|
-
/******/ // Create a new module (and put it into the cache)
|
|
13
|
-
/******/ var module = installedModules[moduleId] = {
|
|
14
|
-
/******/ i: moduleId,
|
|
15
|
-
/******/ l: false,
|
|
16
|
-
/******/ exports: {}
|
|
17
|
-
/******/ };
|
|
18
|
-
/******/
|
|
19
|
-
/******/ // Execute the module function
|
|
20
|
-
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
|
21
|
-
/******/
|
|
22
|
-
/******/ // Flag the module as loaded
|
|
23
|
-
/******/ module.l = true;
|
|
24
|
-
/******/
|
|
25
|
-
/******/ // Return the exports of the module
|
|
26
|
-
/******/ return module.exports;
|
|
27
|
-
/******/ }
|
|
28
|
-
/******/
|
|
29
|
-
/******/
|
|
30
|
-
/******/ // expose the modules object (__webpack_modules__)
|
|
31
|
-
/******/ __webpack_require__.m = modules;
|
|
32
|
-
/******/
|
|
33
|
-
/******/ // expose the module cache
|
|
34
|
-
/******/ __webpack_require__.c = installedModules;
|
|
35
|
-
/******/
|
|
36
|
-
/******/ // define getter function for harmony exports
|
|
37
|
-
/******/ __webpack_require__.d = function(exports, name, getter) {
|
|
38
|
-
/******/ if(!__webpack_require__.o(exports, name)) {
|
|
39
|
-
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
|
|
40
|
-
/******/ }
|
|
41
|
-
/******/ };
|
|
42
|
-
/******/
|
|
43
|
-
/******/ // define __esModule on exports
|
|
44
|
-
/******/ __webpack_require__.r = function(exports) {
|
|
45
|
-
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
46
|
-
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
47
|
-
/******/ }
|
|
48
|
-
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
49
|
-
/******/ };
|
|
50
|
-
/******/
|
|
51
|
-
/******/ // create a fake namespace object
|
|
52
|
-
/******/ // mode & 1: value is a module id, require it
|
|
53
|
-
/******/ // mode & 2: merge all properties of value into the ns
|
|
54
|
-
/******/ // mode & 4: return value when already ns object
|
|
55
|
-
/******/ // mode & 8|1: behave like require
|
|
56
|
-
/******/ __webpack_require__.t = function(value, mode) {
|
|
57
|
-
/******/ if(mode & 1) value = __webpack_require__(value);
|
|
58
|
-
/******/ if(mode & 8) return value;
|
|
59
|
-
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
|
|
60
|
-
/******/ var ns = Object.create(null);
|
|
61
|
-
/******/ __webpack_require__.r(ns);
|
|
62
|
-
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
|
|
63
|
-
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
|
|
64
|
-
/******/ return ns;
|
|
65
|
-
/******/ };
|
|
66
|
-
/******/
|
|
67
|
-
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
|
68
|
-
/******/ __webpack_require__.n = function(module) {
|
|
69
|
-
/******/ var getter = module && module.__esModule ?
|
|
70
|
-
/******/ function getDefault() { return module['default']; } :
|
|
71
|
-
/******/ function getModuleExports() { return module; };
|
|
72
|
-
/******/ __webpack_require__.d(getter, 'a', getter);
|
|
73
|
-
/******/ return getter;
|
|
74
|
-
/******/ };
|
|
75
|
-
/******/
|
|
76
|
-
/******/ // Object.prototype.hasOwnProperty.call
|
|
77
|
-
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
|
78
|
-
/******/
|
|
79
|
-
/******/ // __webpack_public_path__
|
|
80
|
-
/******/ __webpack_require__.p = "";
|
|
81
|
-
/******/
|
|
82
|
-
/******/
|
|
83
|
-
/******/ // Load entry module and return exports
|
|
84
|
-
/******/ return __webpack_require__(__webpack_require__.s = "./node_modules/babel-loader/lib/index.js?!./src/FetchWorkerTool.worker.js");
|
|
85
|
-
/******/ })
|
|
86
|
-
/************************************************************************/
|
|
87
|
-
/******/ ({
|
|
88
|
-
|
|
89
|
-
/***/ "./node_modules/babel-loader/lib/index.js?!./src/FetchWorkerTool.worker.js":
|
|
90
|
-
/*!*******************************************************************************!*\
|
|
91
|
-
!*** ./node_modules/babel-loader/lib??ref--4!./src/FetchWorkerTool.worker.js ***!
|
|
92
|
-
\*******************************************************************************/
|
|
93
|
-
/*! no static exports found */
|
|
94
|
-
/***/ (function(module, exports) {
|
|
95
|
-
|
|
96
|
-
/* eslint-env worker */
|
|
97
|
-
var jobsActive = 0;
|
|
98
|
-
var complete = [];
|
|
99
|
-
var intervalId = null;
|
|
100
|
-
/**
|
|
101
|
-
* Register a step function.
|
|
102
|
-
*
|
|
103
|
-
* Step checks if there are completed jobs and if there are sends them to the
|
|
104
|
-
* parent. Then it checks the jobs count. If there are no further jobs, clear
|
|
105
|
-
* the step.
|
|
106
|
-
*/
|
|
107
|
-
|
|
108
|
-
var registerStep = function registerStep() {
|
|
109
|
-
intervalId = setInterval(function () {
|
|
110
|
-
if (complete.length) {
|
|
111
|
-
// Send our chunk of completed requests and instruct postMessage to
|
|
112
|
-
// transfer the buffers instead of copying them.
|
|
113
|
-
postMessage(complete.slice(), // Instruct postMessage that these buffers in the sent message
|
|
114
|
-
// should use their Transferable trait. After the postMessage
|
|
115
|
-
// call the "buffers" will still be in complete if you looked,
|
|
116
|
-
// but they will all be length 0 as the data they reference has
|
|
117
|
-
// been sent to the window. This lets us send a lot of data
|
|
118
|
-
// without the normal postMessage behaviour of making a copy of
|
|
119
|
-
// all of the data for the window.
|
|
120
|
-
complete.map(function (response) {
|
|
121
|
-
return response.buffer;
|
|
122
|
-
}).filter(Boolean));
|
|
123
|
-
complete.length = 0;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (jobsActive === 0) {
|
|
127
|
-
clearInterval(intervalId);
|
|
128
|
-
intervalId = null;
|
|
129
|
-
}
|
|
130
|
-
}, 1);
|
|
131
|
-
};
|
|
132
|
-
/**
|
|
133
|
-
* Receive a job from the parent and fetch the requested data.
|
|
134
|
-
* @param {object} options.job A job id, url, and options descriptor to perform.
|
|
135
|
-
*/
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
var onMessage = function onMessage(_ref) {
|
|
139
|
-
var job = _ref.data;
|
|
140
|
-
|
|
141
|
-
if (jobsActive === 0 && !intervalId) {
|
|
142
|
-
registerStep();
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
jobsActive++;
|
|
146
|
-
fetch(job.url, job.options).then(function (response) {
|
|
147
|
-
return response.arrayBuffer();
|
|
148
|
-
}).then(function (buffer) {
|
|
149
|
-
return complete.push({
|
|
150
|
-
id: job.id,
|
|
151
|
-
buffer: buffer
|
|
152
|
-
});
|
|
153
|
-
}).catch(function (error) {
|
|
154
|
-
return complete.push({
|
|
155
|
-
id: job.id,
|
|
156
|
-
error: error
|
|
157
|
-
});
|
|
158
|
-
}).then(function () {
|
|
159
|
-
return jobsActive--;
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
if (self.fetch) {
|
|
164
|
-
postMessage({
|
|
165
|
-
support: {
|
|
166
|
-
fetch: true
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
self.addEventListener('message', onMessage);
|
|
170
|
-
} else {
|
|
171
|
-
postMessage({
|
|
172
|
-
support: {
|
|
173
|
-
fetch: false
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
self.addEventListener('message', function (_ref2) {
|
|
177
|
-
var job = _ref2.data;
|
|
178
|
-
postMessage([{
|
|
179
|
-
id: job.id,
|
|
180
|
-
error: new Error('fetch is unavailable')
|
|
181
|
-
}]);
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/***/ })
|
|
186
|
-
|
|
187
|
-
/******/ });
|
|
188
|
-
//# sourceMappingURL=3325b91e8645157f8dc6.worker.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"3325b91e8645157f8dc6.worker.js","sources":["webpack://ScratchStorage/webpack/bootstrap","webpack://ScratchStorage/./src/FetchWorkerTool.worker.js"],"sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = \"./node_modules/babel-loader/lib/index.js?!./src/FetchWorkerTool.worker.js\");\n","/* eslint-env worker */\n\nlet jobsActive = 0;\nconst complete = [];\n\nlet intervalId = null;\n\n/**\n * Register a step function.\n *\n * Step checks if there are completed jobs and if there are sends them to the\n * parent. Then it checks the jobs count. If there are no further jobs, clear\n * the step.\n */\nconst registerStep = function () {\n intervalId = setInterval(() => {\n if (complete.length) {\n // Send our chunk of completed requests and instruct postMessage to\n // transfer the buffers instead of copying them.\n postMessage(\n complete.slice(),\n // Instruct postMessage that these buffers in the sent message\n // should use their Transferable trait. After the postMessage\n // call the \"buffers\" will still be in complete if you looked,\n // but they will all be length 0 as the data they reference has\n // been sent to the window. This lets us send a lot of data\n // without the normal postMessage behaviour of making a copy of\n // all of the data for the window.\n complete.map(response => response.buffer).filter(Boolean)\n );\n complete.length = 0;\n }\n if (jobsActive === 0) {\n clearInterval(intervalId);\n intervalId = null;\n }\n }, 1);\n};\n\n/**\n * Receive a job from the parent and fetch the requested data.\n * @param {object} options.job A job id, url, and options descriptor to perform.\n */\nconst onMessage = ({data: job}) => {\n if (jobsActive === 0 && !intervalId) {\n registerStep();\n }\n\n jobsActive++;\n\n fetch(job.url, job.options)\n .then(response => response.arrayBuffer())\n .then(buffer => complete.push({id: job.id, buffer}))\n .catch(error => complete.push({id: job.id, error}))\n .then(() => jobsActive--);\n};\n\nif (self.fetch) {\n postMessage({support: {fetch: true}});\n self.addEventListener('message', onMessage);\n} else {\n postMessage({support: {fetch: false}});\n self.addEventListener('message', ({data: job}) => {\n postMessage([{id: job.id, error: new Error('fetch is unavailable')}]);\n });\n}\n"],"mappings":";AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;AClFA;AAEA;AACA;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;AACA;AACA;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AAAA;AAEA;AACA;AACA;AAAA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AADA;AAAA;AACA;AAAA;AACA;AACA;AACA;AACA;AAEA;AACA;AAAA;AACA;AAAA;AAAA;AAAA;AAAA;AACA;AAAA;AAAA;AAAA;AAAA;AACA;AAAA;AACA;AACA;AACA;AACA;AAAA;AAAA;AAAA;AAAA;AACA;AACA;AACA;AAAA;AAAA;AAAA;AAAA;AACA;AAAA;AACA;AAAA;AAAA;AAAA;AACA;AACA;;;;A","sourceRoot":""}
|