scratch-storage 2.2.0 → 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 (41) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/dist/node/scratch-storage.js +125 -57
  3. package/dist/node/scratch-storage.js.map +1 -1
  4. package/dist/web/scratch-storage.js +125 -57
  5. package/dist/web/scratch-storage.js.map +1 -1
  6. package/dist/web/scratch-storage.min.js +125 -57
  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/FetchWorkerTool.js +8 -1
  12. package/src/ScratchStorage.js +9 -0
  13. package/src/scratchFetch.js +51 -11
  14. package/test/.eslintrc.js +5 -1
  15. package/test/{mocks/mockFetch.js → __mocks__/cross-fetch.js} +30 -16
  16. package/test/fixtures/.gitattributes +3 -0
  17. package/test/fixtures/assets/117504922.json +196 -0
  18. package/test/fixtures/assets/66895930177178ea01d9e610917f8acf.png +0 -0
  19. package/test/fixtures/assets/6e8bd9ae68fdb02b7e1e3df656a75635.svg +37 -0
  20. package/test/fixtures/assets/7e24c99c1b853e52f8e7f9004416fa34.png +0 -0
  21. package/test/fixtures/assets/83c36d806dc92327b9e7049a565c6bff.wav +0 -0
  22. package/test/fixtures/assets/f88bf1935daea28f8ca098462a31dbb0.svg +35 -0
  23. package/test/fixtures/assets/fe5e3566965f9de793beeffce377d054.jpg +0 -0
  24. package/test/fixtures/known-assets.js +60 -0
  25. package/test/integration/download-known-assets.test.js +141 -0
  26. package/test/transformers/.eslintrc.js +8 -0
  27. package/test/transformers/arraybuffer-loader.js +11 -0
  28. package/test/unit/{add-helper.js → add-helper.test.js} +16 -20
  29. package/test/unit/fetch-tool.test.js +56 -0
  30. package/test/unit/load-default-assets.test.js +59 -0
  31. package/test/unit/metadata.test.js +129 -0
  32. package/webpack.config.js +4 -0
  33. package/.nyc_output/c826ae6b-4ae2-4d8e-bdb2-162df5761fa7.json +0 -1
  34. package/.nyc_output/ec2d52a1-5131-4dd0-9c23-59106f297082.json +0 -1
  35. package/.nyc_output/processinfo/c826ae6b-4ae2-4d8e-bdb2-162df5761fa7.json +0 -1
  36. package/.nyc_output/processinfo/ec2d52a1-5131-4dd0-9c23-59106f297082.json +0 -1
  37. package/.nyc_output/processinfo/index.json +0 -1
  38. package/test/integration/download-known-assets.js +0 -111
  39. package/test/unit/fetch-tool.js +0 -60
  40. package/test/unit/load-default-assets.js +0 -48
  41. package/test/unit/metadata.js +0 -136
@@ -1,60 +0,0 @@
1
- const tap = require('tap');
2
- const TextDecoder = require('util').TextDecoder;
3
-
4
- const {mockFetch, successText} = require('../mocks/mockFetch.js');
5
-
6
- /**
7
- * This is the real FetchTool, but the 'cross-fetch' module has been replaced with the mockFetch function.
8
- * @type {typeof import('../../src/FetchTool')}
9
- */
10
- const FetchTool = tap.mock('../../src/FetchTool', {
11
- 'cross-fetch': {
12
- default: mockFetch,
13
- fetch: mockFetch
14
- }
15
- });
16
-
17
- tap.test('send success returns response.text()', t => {
18
- const tool = new FetchTool();
19
-
20
- return t.resolves(
21
- tool.send({url: '200'}).then(result => {
22
- t.equal(result, successText);
23
- })
24
- );
25
- });
26
-
27
- tap.test('send failure returns response.status', t => {
28
- const tool = new FetchTool();
29
-
30
- return t.rejects(tool.send({url: '500'}), 500);
31
- });
32
-
33
- tap.test('get success returns Uint8Array.body(response.arrayBuffer())', t => {
34
- const encoding = 'utf-8';
35
- const decoder = new TextDecoder(encoding);
36
-
37
- const tool = new FetchTool();
38
-
39
- return t.resolves(
40
- tool.get({url: '200'}).then(result => {
41
- t.equal(decoder.decode(result), successText);
42
- })
43
- );
44
- });
45
-
46
- tap.test('get with 404 response returns null data', t => {
47
- const tool = new FetchTool();
48
-
49
- return t.resolves(
50
- tool.get({url: '404'}).then(result => {
51
- t.equal(result, null);
52
- })
53
- );
54
- });
55
-
56
- tap.test('get failure returns response.status', t => {
57
- const tool = new FetchTool();
58
-
59
- return t.rejects(tool.get({url: '500'}), 500);
60
- });
@@ -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,136 +0,0 @@
1
- const tap = require('tap');
2
-
3
- const crossFetch = require('cross-fetch');
4
-
5
- const {mockFetch} = require('../mocks/mockFetch.js');
6
-
7
- const mockFetchModule = {
8
- ...crossFetch, // Headers, Request, Response, etc.
9
- default: mockFetch,
10
- fetch: mockFetch
11
- };
12
-
13
- // Call this separately from each test to ensure that metadata gets reset.
14
- // This is especially important when parallelizing tests!
15
- const setupModules = () => {
16
- /**
17
- * This instance of scratchFetch will be shared between this file and FetchTool.
18
- * By sharing the same instance, the test can affect the metadata that FetchTool will use.
19
- */
20
- const scratchFetchModule = tap.mock('../../src/scratchFetch', {
21
- 'cross-fetch': mockFetchModule
22
- });
23
-
24
- /**
25
- * This is the real FetchTool, but the 'cross-fetch' module has been replaced with the mockFetch function.
26
- * @type {typeof import('../../src/FetchTool')}
27
- */
28
- const FetchTool = tap.mock('../../src/FetchTool', {
29
- 'cross-fetch': mockFetchModule,
30
- // Make sure FetchTool uses the same scratchFetch instance
31
- '../../src/scratchFetch': scratchFetchModule
32
- });
33
-
34
- return {scratchFetchModule, FetchTool};
35
- };
36
-
37
- tap.test('get without metadata', async t => {
38
- const {FetchTool} = setupModules();
39
-
40
- const tool = new FetchTool();
41
-
42
- /** @type import('../mocks/mockFetch.js').MockFetchTestData */
43
- const mockFetchTestData = {};
44
- const result = await tool.get({url: '200', mockFetchTestData});
45
-
46
- t.type(result, Uint8Array);
47
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
48
- t.equal(mockFetchTestData.headersCount, 0);
49
- });
50
-
51
- tap.test('get with metadata', async t => {
52
- const {scratchFetchModule, FetchTool} = setupModules();
53
- const {RequestMetadata, setMetadata} = scratchFetchModule;
54
-
55
- const tool = new FetchTool();
56
-
57
- setMetadata(RequestMetadata.ProjectId, 1234);
58
- setMetadata(RequestMetadata.RunId, 5678);
59
-
60
- /** @type import('../mocks/mockFetch.js').MockFetchTestData */
61
- const mockFetchTestData = {};
62
- const result = await tool.get({url: '200', mockFetchTestData});
63
-
64
- t.type(result, Uint8Array);
65
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
66
- t.equal(mockFetchTestData.headersCount, 2);
67
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.ProjectId), '1234');
68
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.RunId), '5678');
69
- });
70
-
71
- tap.test('send without metadata', async t => {
72
- const {FetchTool} = setupModules();
73
-
74
- const tool = new FetchTool();
75
-
76
- /** @type import('../mocks/mockFetch.js').MockFetchTestData */
77
- const mockFetchTestData = {};
78
- const result = await tool.send({url: '200', mockFetchTestData});
79
-
80
- t.type(result, 'string');
81
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
82
- t.equal(mockFetchTestData.headersCount, 0);
83
- });
84
-
85
- tap.test('send with metadata', async t => {
86
- const {scratchFetchModule, FetchTool} = setupModules();
87
- const {RequestMetadata, setMetadata} = scratchFetchModule;
88
-
89
- const tool = new FetchTool();
90
-
91
- setMetadata(RequestMetadata.ProjectId, 4321);
92
- setMetadata(RequestMetadata.RunId, 8765);
93
-
94
- /** @type import('../mocks/mockFetch.js').MockFetchTestData */
95
- const mockFetchTestData = {};
96
- const result = await tool.send({url: '200', mockFetchTestData});
97
-
98
- t.type(result, 'string');
99
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
100
- t.equal(mockFetchTestData.headersCount, 2);
101
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.ProjectId), '4321');
102
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.RunId), '8765');
103
- });
104
-
105
- tap.test('selectively delete metadata', async t => {
106
- const {scratchFetchModule, FetchTool} = setupModules();
107
- const {RequestMetadata, setMetadata, unsetMetadata} = scratchFetchModule;
108
-
109
- // verify that these special values are preserved and not interpreted as "delete"
110
- setMetadata(RequestMetadata.ProjectId, null);
111
- setMetadata(RequestMetadata.RunId, void 0); // void 0 = undefined
112
-
113
- const tool = new FetchTool();
114
-
115
- /** @type import('../mocks/mockFetch.js').MockFetchTestData */
116
- const mockFetchTestData = {};
117
-
118
- const result1 = await tool.send({url: '200', mockFetchTestData});
119
- t.type(result1, 'string');
120
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
121
-
122
- t.equal(mockFetchTestData.headersCount, 2);
123
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.ProjectId), 'null'); // string "null" means it's present
124
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.RunId), 'undefined');
125
-
126
- // remove the Project ID from metadata
127
- unsetMetadata(RequestMetadata.ProjectId);
128
-
129
- const result2 = await tool.send({url: '200', mockFetchTestData});
130
- t.type(result2, 'string');
131
- t.ok(mockFetchTestData.headers, 'mockFetch did not report headers');
132
-
133
- t.equal(mockFetchTestData.headersCount, 1);
134
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.ProjectId), null); // value `null` means it's missing
135
- t.equal(mockFetchTestData.headers?.get(RequestMetadata.RunId), 'undefined');
136
- });