scratch-storage 2.3.283 → 3.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.
Files changed (61) hide show
  1. package/.browserslistrc +7 -0
  2. package/CHANGELOG.md +18 -0
  3. package/dist/{web/f6240eab828e6d415177.worker.js → node/chunks/fetch-worker.eefe62e3c8ee125d3436.js} +105 -180
  4. package/dist/node/chunks/fetch-worker.eefe62e3c8ee125d3436.js.map +1 -0
  5. package/dist/node/scratch-storage.js +3898 -6304
  6. package/dist/node/scratch-storage.js.map +1 -1
  7. package/dist/types/Asset.d.ts +38 -0
  8. package/dist/types/AssetType.d.ts +49 -0
  9. package/dist/types/BuiltinHelper.d.ts +69 -0
  10. package/dist/types/DataFormat.d.ts +15 -0
  11. package/dist/types/FetchTool.d.ts +33 -0
  12. package/dist/types/FetchWorkerTool.d.ts +29 -0
  13. package/dist/types/Helper.d.ts +20 -0
  14. package/dist/types/ProxyTool.d.ts +53 -0
  15. package/dist/types/ScratchStorage.d.ts +202 -0
  16. package/dist/types/Tool.d.ts +13 -0
  17. package/dist/types/WebHelper.d.ts +59 -0
  18. package/dist/types/index.d.ts +11 -0
  19. package/dist/types/log.d.ts +2 -0
  20. package/dist/types/memoizedToString.d.ts +5 -0
  21. package/dist/web/chunks/fetch-worker.85da71862ab8ee15f1cd.js +2 -0
  22. package/dist/web/chunks/fetch-worker.85da71862ab8ee15f1cd.js.map +1 -0
  23. package/dist/web/chunks/fetch-worker.ba5eddd48c8ae259073b.js +676 -0
  24. package/dist/web/chunks/fetch-worker.ba5eddd48c8ae259073b.js.map +1 -0
  25. package/dist/web/scratch-storage.js +2002 -2758
  26. package/dist/web/scratch-storage.js.map +1 -1
  27. package/dist/web/scratch-storage.min.js +2 -6398
  28. package/dist/web/scratch-storage.min.js.LICENSE.txt +18 -0
  29. package/dist/web/scratch-storage.min.js.map +1 -1
  30. package/jest.config.js +32 -0
  31. package/package.json +18 -13
  32. package/src/.eslintrc.js +13 -1
  33. package/src/Asset.ts +100 -0
  34. package/src/{AssetType.js → AssetType.ts} +10 -5
  35. package/src/{BuiltinHelper.js → BuiltinHelper.ts} +44 -35
  36. package/src/{DataFormat.js → DataFormat.ts} +3 -3
  37. package/src/{FetchTool.js → FetchTool.ts} +8 -9
  38. package/src/{FetchWorkerTool.js → FetchWorkerTool.ts} +47 -22
  39. package/src/{Helper.js → Helper.ts} +10 -5
  40. package/src/{ProxyTool.js → ProxyTool.ts} +32 -29
  41. package/src/{ScratchStorage.js → ScratchStorage.ts} +57 -34
  42. package/src/Tool.ts +10 -0
  43. package/src/{WebHelper.js → WebHelper.ts} +42 -15
  44. package/src/index.ts +19 -0
  45. package/src/log.ts +4 -0
  46. package/src/{Asset.js → memoizedToString.ts} +16 -76
  47. package/src/scratchFetch.js +0 -2
  48. package/src/types.d.ts +3 -0
  49. package/test/integration/download-known-assets.test.js +1 -1
  50. package/test/unit/add-helper.test.js +1 -1
  51. package/test/unit/fetch-tool.test.js +1 -1
  52. package/test/unit/load-default-assets.test.js +1 -1
  53. package/test/unit/metadata.test.js +6 -6
  54. package/tsconfig.json +29 -0
  55. package/tsconfig.test.json +11 -0
  56. package/webpack.config.js +58 -65
  57. package/dist/node/ce8a01e629587e4f90e4.worker.js +0 -4274
  58. package/dist/node/ce8a01e629587e4f90e4.worker.js.map +0 -1
  59. package/dist/web/f6240eab828e6d415177.worker.js.map +0 -1
  60. package/src/index.js +0 -7
  61. package/src/log.js +0 -4
@@ -1,7 +1,18 @@
1
+ import {AssetId} from './Asset';
2
+
3
+ declare function require(name: 'fastestsmallesttextencoderdecoder'): {
4
+ TextEncoder: typeof TextEncoder,
5
+ TextDecoder: typeof TextDecoder
6
+ };
7
+
8
+ declare function require(name: 'base64-js'): {
9
+ fromByteArray: (data: Uint8Array) => string;
10
+ };
11
+
1
12
  // Use JS implemented TextDecoder and TextEncoder if it is not provided by the
2
13
  // browser.
3
- let _TextDecoder;
4
- let _TextEncoder;
14
+ let _TextDecoder: typeof TextDecoder;
15
+ let _TextEncoder: typeof TextEncoder;
5
16
  if (typeof TextDecoder === 'undefined' || typeof TextEncoder === 'undefined') {
6
17
  // Wait to require the text encoding polyfill until we know it's needed.
7
18
  // eslint-disable-next-line global-require
@@ -13,8 +24,6 @@ if (typeof TextDecoder === 'undefined' || typeof TextEncoder === 'undefined') {
13
24
  _TextEncoder = TextEncoder;
14
25
  }
15
26
 
16
- const md5 = require('js-md5');
17
-
18
27
  const memoizedToString = (function () {
19
28
  /**
20
29
  * The maximum length of a chunk before encoding it into base64.
@@ -30,10 +39,10 @@ const memoizedToString = (function () {
30
39
  * An array cache of bytes to characters.
31
40
  * @const {?Array.<string>}
32
41
  */
33
- let fromCharCode = null;
42
+ let fromCharCode: string[] | null = null;
34
43
 
35
44
  const strings = {};
36
- return (assetId, data) => {
45
+ return (assetId: AssetId, data: Uint8Array) => {
37
46
  if (!Object.prototype.hasOwnProperty.call(strings, assetId)) {
38
47
  if (typeof btoa === 'undefined') {
39
48
  // Use a library that does not need btoa to run.
@@ -74,73 +83,4 @@ const memoizedToString = (function () {
74
83
  };
75
84
  }());
76
85
 
77
- class Asset {
78
- /**
79
- * Construct an Asset.
80
- * @param {AssetType} assetType - The type of this asset (sound, image, etc.)
81
- * @param {string} assetId - The ID of this asset.
82
- * @param {DataFormat} [dataFormat] - The format of the data (WAV, PNG, etc.); required iff `data` is present.
83
- * @param {Buffer} [data] - The in-memory data for this asset; optional.
84
- * @param {bool} [generateId] - Whether to create id from an md5 hash of data
85
- */
86
- constructor (assetType, assetId, dataFormat, data, generateId) {
87
- /** @type {AssetType} */
88
- this.assetType = assetType;
89
-
90
- /** @type {string} */
91
- this.assetId = assetId;
92
-
93
- this.setData(data, dataFormat || assetType.runtimeFormat, generateId);
94
-
95
- /** @type {Asset[]} */
96
- this.dependencies = [];
97
- }
98
-
99
- setData (data, dataFormat, generateId) {
100
- if (data && !dataFormat) {
101
- throw new Error('Data provided without specifying its format');
102
- }
103
-
104
- /** @type {DataFormat} */
105
- this.dataFormat = dataFormat;
106
-
107
- /** @type {Buffer} */
108
- this.data = data;
109
-
110
- if (generateId) this.assetId = md5(data);
111
-
112
- // Mark as clean only if set is being called without generateId
113
- // If a new id is being generated, mark this asset as not clean
114
- this.clean = !generateId;
115
- }
116
-
117
- /**
118
- * @returns {string} - This asset's data, decoded as text.
119
- */
120
- decodeText () {
121
- const decoder = new _TextDecoder();
122
- return decoder.decode(this.data);
123
- }
124
-
125
- /**
126
- * Same as `setData` but encodes text first.
127
- * @param {string} data - the text data to encode and store.
128
- * @param {DataFormat} dataFormat - the format of the data (DataFormat.SVG for example).
129
- * @param {bool} generateId - after setting data, set the id to an md5 of the data?
130
- */
131
- encodeTextData (data, dataFormat, generateId) {
132
- const encoder = new _TextEncoder();
133
- this.setData(encoder.encode(data), dataFormat, generateId);
134
- }
135
-
136
- /**
137
- * @param {string} [contentType] - Optionally override the content type to be included in the data URI.
138
- * @returns {string} - A data URI representing the asset's data.
139
- */
140
- encodeDataURI (contentType) {
141
- contentType = contentType || this.assetType.contentType;
142
- return `data:${contentType};base64,${memoizedToString(this.assetId, this.data)}`;
143
- }
144
- }
145
-
146
- module.exports = Asset;
86
+ export {memoizedToString, _TextEncoder, _TextDecoder};
@@ -103,8 +103,6 @@ const unsetMetadata = name => {
103
103
  };
104
104
 
105
105
  module.exports = {
106
- default: scratchFetch,
107
-
108
106
  Headers: crossFetch.Headers,
109
107
  RequestMetadata,
110
108
  applyMetadata,
package/src/types.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare module '*.png?arrayBuffer';
2
+ declare module '*.wav?arrayBuffer';
3
+ declare module '*.svg?arrayBuffer';
@@ -1,6 +1,6 @@
1
1
  const md5 = require('js-md5');
2
2
 
3
- const ScratchStorage = require('../../src/index.js');
3
+ const ScratchStorage = require('../../src/index').ScratchStorage;
4
4
 
5
5
  test('constructor', () => {
6
6
  const storage = new ScratchStorage();
@@ -1,4 +1,4 @@
1
- const ScratchStorage = require('../../src');
1
+ const {ScratchStorage} = require('../../src/ScratchStorage');
2
2
 
3
3
  /**
4
4
  * Simulate a storage helper, adding log messages when "load" is called rather than actually loading anything.
@@ -2,7 +2,7 @@ const TextDecoder = require('util').TextDecoder;
2
2
 
3
3
  jest.mock('cross-fetch');
4
4
  const mockFetch = require('cross-fetch');
5
- const FetchTool = require('../../src/FetchTool.js');
5
+ const {FetchTool} = require('../../src/FetchTool');
6
6
 
7
7
  test('send success returns response.text()', async () => {
8
8
  const tool = new FetchTool();
@@ -1,6 +1,6 @@
1
1
  const md5 = require('js-md5');
2
2
 
3
- const ScratchStorage = require('../../dist/node/scratch-storage');
3
+ const {ScratchStorage} = require('../../src/ScratchStorage');
4
4
 
5
5
  // Hash and file size of each default asset
6
6
  const knownSizes = {
@@ -14,7 +14,7 @@ beforeEach(() => {
14
14
  });
15
15
 
16
16
  test('get without metadata', async () => {
17
- const FetchTool = require('../../src/FetchTool.js');
17
+ const {FetchTool} = require('../../src/FetchTool');
18
18
  const tool = new FetchTool();
19
19
 
20
20
  const mockFetchTestData = {};
@@ -26,7 +26,7 @@ test('get without metadata', async () => {
26
26
  });
27
27
 
28
28
  test('get with metadata', async () => {
29
- const FetchTool = require('../../src/FetchTool.js');
29
+ const {FetchTool} = require('../../src/FetchTool');
30
30
  const ScratchFetch = require('../../src/scratchFetch');
31
31
  const {RequestMetadata, setMetadata} = ScratchFetch;
32
32
 
@@ -46,7 +46,7 @@ test('get with metadata', async () => {
46
46
  });
47
47
 
48
48
  test('send without metadata', async () => {
49
- const FetchTool = require('../../src/FetchTool.js');
49
+ const {FetchTool} = require('../../src/FetchTool');
50
50
  const tool = new FetchTool();
51
51
 
52
52
  const mockFetchTestData = {};
@@ -58,7 +58,7 @@ test('send without metadata', async () => {
58
58
  });
59
59
 
60
60
  test('send with metadata', async () => {
61
- const FetchTool = require('../../src/FetchTool.js');
61
+ const {FetchTool} = require('../../src/FetchTool');
62
62
  const ScratchFetch = require('../../src/scratchFetch');
63
63
  const {RequestMetadata, setMetadata} = ScratchFetch;
64
64
 
@@ -78,7 +78,7 @@ test('send with metadata', async () => {
78
78
  });
79
79
 
80
80
  test('selectively delete metadata', async () => {
81
- const FetchTool = require('../../src/FetchTool.js');
81
+ const {FetchTool} = require('../../src/FetchTool');
82
82
  const ScratchFetch = require('../../src/scratchFetch');
83
83
  const {RequestMetadata, setMetadata, unsetMetadata} = ScratchFetch;
84
84
 
@@ -111,7 +111,7 @@ test('selectively delete metadata', async () => {
111
111
  });
112
112
 
113
113
  test('metadata has case-insensitive keys', async () => {
114
- const FetchTool = require('../../src/FetchTool.js');
114
+ const {FetchTool} = require('../../src/FetchTool');
115
115
  const ScratchFetch = require('../../src/scratchFetch');
116
116
  const {setMetadata} = ScratchFetch;
117
117
 
package/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "include": ["src"],
3
+ "compilerOptions": {
4
+ /* Visit https://aka.ms/tsconfig to read more about this file */
5
+
6
+ /* Language and Environment */
7
+ "target": "ESNext",
8
+
9
+ /* Modules */
10
+ "module": "Preserve",
11
+ "types": ["./src/types.d.ts"],
12
+
13
+ /* Emit */
14
+ "declaration": true,
15
+ "sourceMap": true,
16
+ "outDir": "./dist/types/",
17
+
18
+ /* Interop Constraints */
19
+ "esModuleInterop": true,
20
+ "forceConsistentCasingInFileNames": true,
21
+
22
+ /* Type Checking */
23
+ "strict": true,
24
+ "noImplicitAny": false,
25
+
26
+ /* Completeness */
27
+ "skipLibCheck": true
28
+ }
29
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "include": ["test"],
3
+ "extends": ["./tsconfig.json"],
4
+ "compilerOptions": {
5
+ /* Visit https://aka.ms/tsconfig to read more about this file */
6
+
7
+ /* Modules */
8
+ "module": "CommonJS",
9
+ "types": ["jest", "./src/types.d.ts"],
10
+ }
11
+ }
package/webpack.config.js CHANGED
@@ -1,80 +1,73 @@
1
1
  const path = require('path');
2
- const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
2
+ const webpack = require('webpack');
3
3
 
4
- const base = {
5
- mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
6
- devtool: 'cheap-module-source-map',
7
- module: {
8
- rules: [
9
- {
10
- include: [
11
- path.resolve('src')
12
- ],
13
- test: /\.js$/,
14
- loader: 'babel-loader',
15
- options: {
16
- plugins: [
17
- '@babel/plugin-transform-runtime'
18
- ],
19
- presets: [
20
- ['@babel/preset-env', {targets: {browsers: ['last 3 versions', 'Safari >= 8', 'iOS >= 8']}}]
21
- ],
22
- // Consider a file a "module" if import/export statements are present, or else consider it a
23
- // "script". Fixes "Cannot assign to read only property 'exports'" when using
24
- // @babel/plugin-transform-runtime with CommonJS files.
25
- sourceType: 'unambiguous'
26
- }
27
- },
28
- {
29
- test: /\.(png|svg|wav)$/,
30
- loader: 'arraybuffer-loader'
4
+ const ScratchWebpackConfigBuilder = require('scratch-webpack-configuration');
5
+
6
+ const baseConfig = new ScratchWebpackConfigBuilder(
7
+ {
8
+ rootPath: path.resolve(__dirname),
9
+ enableReact: false,
10
+ enableTs: true,
11
+ shouldSplitChunks: false
12
+ })
13
+ .setTarget('browserslist')
14
+ .merge({
15
+ resolve: {
16
+ fallback: {
17
+ Buffer: require.resolve('buffer/')
31
18
  }
32
- ]
33
- },
34
- optimization: {
35
- minimizer: [
36
- new UglifyJsPlugin({
37
- include: /\.min\.js$/,
38
- sourceMap: true
39
- })
40
- ]
41
- },
42
- plugins: []
43
- };
19
+ }
20
+ });
44
21
 
45
- module.exports = [
46
- // Web-compatible
47
- Object.assign({}, base, {
48
- target: 'web',
49
- entry: {
50
- 'scratch-storage': './src/index.js',
51
- 'scratch-storage.min': './src/index.js'
52
- },
22
+ if (!process.env.CI) {
23
+ baseConfig.addPlugin(new webpack.ProgressPlugin());
24
+ }
25
+
26
+ // Web-compatible
27
+ const webConfig = baseConfig.clone()
28
+ .merge({
53
29
  output: {
54
30
  library: 'ScratchStorage',
55
31
  libraryTarget: 'umd',
56
- path: path.resolve('dist', 'web'),
57
- filename: '[name].js'
32
+ path: path.resolve(__dirname, 'dist', 'web'),
33
+ filename: '[name].js',
34
+ clean: false
58
35
  }
59
- }),
36
+ });
60
37
 
61
- // Node-compatible
62
- Object.assign({}, base, {
63
- target: 'node',
38
+ const webNonMinConfig = webConfig.clone()
39
+ .merge({
64
40
  entry: {
65
- 'scratch-storage': './src/index.js'
41
+ 'scratch-storage': path.join(__dirname, './src/index.ts')
42
+ },
43
+ optimization: {
44
+ minimize: false
45
+ }
46
+ });
47
+
48
+ const webMinConfig = webConfig.clone()
49
+ .merge({
50
+ entry: {
51
+ 'scratch-storage.min': path.join(__dirname, './src/index.ts')
52
+ },
53
+ optimization: {
54
+ minimize: true
55
+ }
56
+ });
57
+
58
+ // Node-compatible
59
+ const nodeConfig = baseConfig.clone()
60
+ .merge({
61
+ entry: {
62
+ 'scratch-storage': path.join(__dirname, './src/index.ts')
66
63
  },
67
64
  output: {
68
- library: 'ScratchStorage',
69
65
  libraryTarget: 'commonjs2',
70
- path: path.resolve('dist', 'node'),
71
- filename: '[name].js'
72
- },
73
- externals: {
74
- 'base64-js': true,
75
- 'js-md5': true,
76
- 'localforage': true,
77
- 'text-encoding': true
66
+ path: path.resolve(__dirname, 'dist', 'node'),
67
+ filename: '[name].js',
68
+ clean: false
78
69
  }
79
70
  })
80
- ];
71
+ .addExternals(['base64-js', 'js-md5', 'localforage', 'text-encoding']);
72
+
73
+ module.exports = [webNonMinConfig.get(), webMinConfig.get(), nodeConfig.get()];