From c742a74a5217b086801666f36810ccd37cdf3cbb Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Thu, 20 Apr 2017 16:13:40 -0700 Subject: [PATCH] Add `Asset` and `AssetType` to storage instance Actually, to the prototype. This allows consumers of this module to access `Asset` and `AssetType` without having to `require` the storage module itself. --- src/ScratchStorage.js | 7 +++++ src/index.js | 9 +++++++ test/integration/download-known-assets.js | 32 +++++++++++------------ test/unit/load-default-assets.js | 10 +++---- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/ScratchStorage.js b/src/ScratchStorage.js index 8836d33..3aa7a9e 100644 --- a/src/ScratchStorage.js +++ b/src/ScratchStorage.js @@ -1,3 +1,5 @@ +const Asset = require('./Asset'); +const AssetType = require('./AssetType'); const BuiltinHelper = require('./BuiltinHelper'); const LocalHelper = require('./LocalHelper'); const WebHelper = require('./WebHelper'); @@ -95,4 +97,9 @@ class ScratchStorage { } } +Object.assign(ScratchStorage.prototype, { + Asset, + AssetType +}); + module.exports = ScratchStorage; diff --git a/src/index.js b/src/index.js index 51ca841..b8edfe7 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,15 @@ const ScratchStorage = require('./ScratchStorage'); * @type {ScratchStorage} */ module.exports = Object.assign(ScratchStorage, { + /** + * Please use the `Asset` member of a storage instance instead. + * @deprecated + */ Asset: Asset, + + /** + * Please use the `AssetType` member of a storage instance instead. + * @deprecated + */ AssetType: AssetType }); diff --git a/test/integration/download-known-assets.js b/test/integration/download-known-assets.js index e23e110..ab17135 100644 --- a/test/integration/download-known-assets.js +++ b/test/integration/download-known-assets.js @@ -2,8 +2,13 @@ const crypto = require('crypto'); const test = require('tap').test; const ScratchStorage = require('../../dist/node/scratch-storage'); -const Asset = ScratchStorage.Asset; -const AssetType = ScratchStorage.AssetType; + +var storage; +test('constructor', t => { + storage = new ScratchStorage(); + t.type(storage, ScratchStorage); + t.end(); +}); /** * @@ -14,43 +19,36 @@ const AssetType = ScratchStorage.AssetType; */ const testAssets = [ { - type: AssetType.Project, + type: storage.AssetType.Project, id: '117504922', md5: null // don't check MD5 for project without revision ID }, { - type: AssetType.Project, + type: storage.AssetType.Project, id: '117504922.d6ae1ffb76f2bc83421cd3f40fc4fd57', md5: '1225460702e149727de28bff4cfd9e23' }, { - type: AssetType.ImageVector, + type: storage.AssetType.ImageVector, id: 'f88bf1935daea28f8ca098462a31dbb0', // cat1-a md5: 'f88bf1935daea28f8ca098462a31dbb0' }, { - type: AssetType.ImageBitmap, + type: storage.AssetType.ImageBitmap, id: '7e24c99c1b853e52f8e7f9004416fa34', // squirrel md5: '7e24c99c1b853e52f8e7f9004416fa34' }, { - type: AssetType.Sound, + type: storage.AssetType.Sound, id: '83c36d806dc92327b9e7049a565c6bff', // meow md5: '83c36d806dc92327b9e7049a565c6bff' // wat } ]; -var storage; -test('constructor', t => { - storage = new ScratchStorage(); - t.type(storage, ScratchStorage); - t.end(); -}); - test('addWebSource', t => { t.doesNotThrow(() => { storage.addWebSource( - [AssetType.Project], + [storage.AssetType.Project], asset => { const idParts = asset.assetId.split('.'); return idParts[1] ? @@ -60,7 +58,7 @@ test('addWebSource', t => { }); t.doesNotThrow(() => { storage.addWebSource( - [AssetType.ImageVector, AssetType.ImageBitmap, AssetType.Sound], + [storage.AssetType.ImageVector, storage.AssetType.ImageBitmap, storage.AssetType.Sound], asset => `https://cdn.assets.scratch.mit.edu/internalapi/asset/${asset.assetId}.${asset.assetType.runtimeFormat}/get/` ); }); @@ -78,7 +76,7 @@ test('load', t => { promises.push(promise); promise.then(asset => { - t.type(asset, Asset); + t.type(asset, storage.Asset); t.strictEqual(asset.assetId, assetInfo.id); t.strictEqual(asset.assetType, assetInfo.type); t.ok(asset.data.length); diff --git a/test/unit/load-default-assets.js b/test/unit/load-default-assets.js index 5088710..89a94b7 100644 --- a/test/unit/load-default-assets.js +++ b/test/unit/load-default-assets.js @@ -2,11 +2,6 @@ const crypto = require('crypto'); const test = require('tap').test; const ScratchStorage = require('../../dist/node/scratch-storage'); -const Asset = ScratchStorage.Asset; -const AssetType = ScratchStorage.AssetType; - -const defaultAssetTypes = [AssetType.ImageBitmap, AssetType.ImageVector, AssetType.Sound]; -const defaultIds = {}; var storage; test('constructor', t => { @@ -15,6 +10,9 @@ test('constructor', t => { t.end(); }); +const defaultAssetTypes = [storage.AssetType.ImageBitmap, storage.AssetType.ImageVector, storage.AssetType.Sound]; +const defaultIds = {}; + test('getDefaultAssetId', t => { for (var i = 0; i < defaultAssetTypes.length; ++i) { const assetType = defaultAssetTypes[i]; @@ -37,7 +35,7 @@ test('load', t => { promises.push(promise); promise.then(asset => { - t.type(asset, Asset); + t.type(asset, storage.Asset); t.strictEqual(asset.assetId, id); t.strictEqual(asset.assetType, assetType); t.ok(asset.data.length);