scratch-storage 2.2.1 → 2.3.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 (39) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/dist/node/scratch-storage.js +45 -36
  3. package/dist/node/scratch-storage.js.map +1 -1
  4. package/dist/web/scratch-storage.js +45 -36
  5. package/dist/web/scratch-storage.js.map +1 -1
  6. package/dist/web/scratch-storage.min.js +45 -36
  7. package/dist/web/scratch-storage.min.js.map +1 -1
  8. package/jest.config.js +5 -0
  9. package/package.json +10 -8
  10. package/src/BuiltinHelper.js +3 -3
  11. package/src/scratchFetch.js +13 -0
  12. package/test/.eslintrc.js +5 -1
  13. package/test/{mocks/mock-fetch.js → __mocks__/cross-fetch.js} +25 -16
  14. package/test/fixtures/.gitattributes +3 -0
  15. package/test/fixtures/assets/117504922.json +196 -0
  16. package/test/fixtures/assets/66895930177178ea01d9e610917f8acf.png +0 -0
  17. package/test/fixtures/assets/6e8bd9ae68fdb02b7e1e3df656a75635.svg +37 -0
  18. package/test/fixtures/assets/7e24c99c1b853e52f8e7f9004416fa34.png +0 -0
  19. package/test/fixtures/assets/83c36d806dc92327b9e7049a565c6bff.wav +0 -0
  20. package/test/fixtures/assets/f88bf1935daea28f8ca098462a31dbb0.svg +35 -0
  21. package/test/fixtures/assets/fe5e3566965f9de793beeffce377d054.jpg +0 -0
  22. package/test/fixtures/known-assets.js +60 -0
  23. package/test/integration/download-known-assets.test.js +141 -0
  24. package/test/transformers/.eslintrc.js +8 -0
  25. package/test/transformers/arraybuffer-loader.js +11 -0
  26. package/test/unit/{add-helper.js → add-helper.test.js} +16 -20
  27. package/test/unit/fetch-tool.test.js +56 -0
  28. package/test/unit/load-default-assets.test.js +59 -0
  29. package/test/unit/metadata.test.js +129 -0
  30. package/webpack.config.js +4 -0
  31. package/.nyc_output/f1105466-7461-4f73-8b18-ee824eb652b1.json +0 -1
  32. package/.nyc_output/fd8f05df-43a0-4695-b45d-f4877b37f387.json +0 -1
  33. package/.nyc_output/processinfo/f1105466-7461-4f73-8b18-ee824eb652b1.json +0 -1
  34. package/.nyc_output/processinfo/fd8f05df-43a0-4695-b45d-f4877b37f387.json +0 -1
  35. package/.nyc_output/processinfo/index.json +0 -1
  36. package/test/integration/download-known-assets.js +0 -111
  37. package/test/unit/fetch-tool.js +0 -57
  38. package/test/unit/load-default-assets.js +0 -48
  39. package/test/unit/metadata.js +0 -147
@@ -1,48 +0,0 @@
1
- const md5 = require('js-md5');
2
- const test = require('tap').test;
3
-
4
- const ScratchStorage = require('../../dist/node/scratch-storage');
5
-
6
- let storage;
7
- test('constructor', t => {
8
- storage = new ScratchStorage();
9
- t.type(storage, ScratchStorage);
10
- t.end();
11
- });
12
-
13
- const defaultAssetTypes = [storage.AssetType.ImageBitmap, storage.AssetType.ImageVector, storage.AssetType.Sound];
14
- const defaultIds = {};
15
-
16
- test('getDefaultAssetId', t => {
17
- for (let i = 0; i < defaultAssetTypes.length; ++i) {
18
- const assetType = defaultAssetTypes[i];
19
- const id = storage.getDefaultAssetId(assetType);
20
- t.type(id, 'string');
21
- defaultIds[assetType.name] = id;
22
- }
23
- t.end();
24
- });
25
-
26
- test('load', t => {
27
- const promises = [];
28
- const checkAsset = (assetType, id, asset) => {
29
- t.type(asset, storage.Asset);
30
- t.strictEqual(asset.assetId, id);
31
- t.strictEqual(asset.assetType, assetType);
32
- t.ok(asset.data.length);
33
- t.strictEqual(md5(asset.data), id);
34
- };
35
- for (let i = 0; i < defaultAssetTypes.length; ++i) {
36
- const assetType = defaultAssetTypes[i];
37
- const id = defaultIds[assetType.name];
38
-
39
- const promise = storage.load(assetType, id);
40
- t.type(promise, 'Promise');
41
-
42
- promises.push(promise);
43
-
44
- promise.then(asset => checkAsset(assetType, id, asset));
45
- }
46
-
47
- return Promise.all(promises);
48
- });
@@ -1,147 +0,0 @@
1
- const tap = require('tap');
2
-
3
- const mockFetchModule = require('../mocks/mock-fetch.js');
4
-
5
-
6
- // Call this separately from each test to ensure that metadata gets reset.
7
- // This is especially important when parallelizing tests!
8
- const setupModules = () => {
9
- /**
10
- * This instance of scratchFetch will be shared between this file and FetchTool.
11
- * By sharing the same instance, the test can affect the metadata that FetchTool will use.
12
- */
13
- const scratchFetchModule = tap.mock('../../src/scratchFetch', {
14
- 'cross-fetch': mockFetchModule
15
- });
16
-
17
- /**
18
- * This is the real FetchTool, but the 'cross-fetch' module has been replaced with the mockFetch function.
19
- * @type {typeof import('../../src/FetchTool')}
20
- */
21
- const FetchTool = tap.mock('../../src/FetchTool', {
22
- 'cross-fetch': mockFetchModule,
23
- // Make sure FetchTool uses the same scratchFetch instance
24
- '../../src/scratchFetch': scratchFetchModule
25
- });
26
-
27
- return {scratchFetchModule, FetchTool};
28
- };
29
-
30
- tap.test('get without metadata', async t => {
31
- const {FetchTool} = setupModules();
32
-
33
- const tool = new FetchTool();
34
-
35
- /** @type import('../mocks/mock-fetch.js').MockFetchTestData */
36
- const mockFetchTestData = {};
37
- const result = await tool.get({url: '200', mockFetchTestData});
38
-
39
- t.type(result, Uint8Array);
40
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
41
- t.equal(mockFetchTestData.headersCount, 0);
42
- });
43
-
44
- tap.test('get with metadata', async t => {
45
- const {scratchFetchModule, FetchTool} = setupModules();
46
- const {RequestMetadata, setMetadata} = scratchFetchModule;
47
-
48
- const tool = new FetchTool();
49
-
50
- setMetadata(RequestMetadata.ProjectId, 1234);
51
- setMetadata(RequestMetadata.RunId, 5678);
52
-
53
- /** @type import('../mocks/mock-fetch.js').MockFetchTestData */
54
- const mockFetchTestData = {};
55
- const result = await tool.get({url: '200', mockFetchTestData});
56
-
57
- t.type(result, Uint8Array);
58
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
59
- t.equal(mockFetchTestData.headersCount, 2);
60
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.ProjectId), '1234');
61
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.RunId), '5678');
62
- });
63
-
64
- tap.test('send without metadata', async t => {
65
- const {FetchTool} = setupModules();
66
-
67
- const tool = new FetchTool();
68
-
69
- /** @type import('../mocks/mock-fetch.js').MockFetchTestData */
70
- const mockFetchTestData = {};
71
- const result = await tool.send({url: '200', mockFetchTestData});
72
-
73
- t.type(result, 'string');
74
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
75
- t.equal(mockFetchTestData.headersCount, 0);
76
- });
77
-
78
- tap.test('send with metadata', async t => {
79
- const {scratchFetchModule, FetchTool} = setupModules();
80
- const {RequestMetadata, setMetadata} = scratchFetchModule;
81
-
82
- const tool = new FetchTool();
83
-
84
- setMetadata(RequestMetadata.ProjectId, 4321);
85
- setMetadata(RequestMetadata.RunId, 8765);
86
-
87
- /** @type import('../mocks/mock-fetch.js').MockFetchTestData */
88
- const mockFetchTestData = {};
89
- const result = await tool.send({url: '200', mockFetchTestData});
90
-
91
- t.type(result, 'string');
92
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
93
- t.equal(mockFetchTestData.headersCount, 2);
94
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.ProjectId), '4321');
95
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.RunId), '8765');
96
- });
97
-
98
- tap.test('selectively delete metadata', async t => {
99
- const {scratchFetchModule, FetchTool} = setupModules();
100
- const {RequestMetadata, setMetadata, unsetMetadata} = scratchFetchModule;
101
-
102
- // verify that these special values are preserved and not interpreted as "delete"
103
- setMetadata(RequestMetadata.ProjectId, null);
104
- setMetadata(RequestMetadata.RunId, void 0); // void 0 = undefined
105
-
106
- const tool = new FetchTool();
107
-
108
- /** @type import('../mocks/mock-fetch.js').MockFetchTestData */
109
- const mockFetchTestData = {};
110
-
111
- const result1 = await tool.send({url: '200', mockFetchTestData});
112
- t.type(result1, 'string');
113
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
114
-
115
- t.equal(mockFetchTestData.headersCount, 2);
116
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.ProjectId), 'null'); // string "null" means it's present
117
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.RunId), 'undefined');
118
-
119
- // remove the Project ID from metadata
120
- unsetMetadata(RequestMetadata.ProjectId);
121
-
122
- const result2 = await tool.send({url: '200', mockFetchTestData});
123
- t.type(result2, 'string');
124
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
125
-
126
- t.equal(mockFetchTestData.headersCount, 1);
127
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.ProjectId), null); // value `null` means it's missing
128
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.RunId), 'undefined');
129
- });
130
-
131
- tap.test('metadata has case-insensitive keys', async t => {
132
- const {scratchFetchModule, FetchTool} = setupModules();
133
- const {setMetadata} = scratchFetchModule;
134
-
135
- setMetadata('foo', 1);
136
- setMetadata('FOO', 2);
137
-
138
- const tool = new FetchTool();
139
-
140
- /** @type import('../mocks/mock-fetch.js').MockFetchTestData */
141
- const mockFetchTestData = {};
142
- await tool.get({url: '200', mockFetchTestData});
143
-
144
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
145
- t.equal(mockFetchTestData.headersCount, 1);
146
- t.equal(mockFetchTestData.headers?.get('foo'), '2');
147
- });