Load and store project and asset files for Scratch 3.0
Find a file
renovate[bot] aea3f7c4a4
Some checks are pending
CI/CD / ci-cd (push) Waiting to run
Merge pull request #682 from scratchfoundation/renovate/ts-jest-mock-import-meta-1.x
chore(deps): update dependency ts-jest-mock-import-meta to v1.2.1
2024-11-14 22:09:52 +00:00
.github chore(deps): pin dependencies 2024-02-16 00:30:59 +00:00
.husky chore: add husky hook for commitlint 2023-12-20 18:51:25 -08:00
src fix: fix web worker usage in scratch-vm integration tests 2024-10-23 15:04:57 +03:00
test chore: fix a test referencing the old default export 2024-10-23 12:56:25 +03:00
.browserslistrc feat!: upgrade webpack to 5 and add TS support 2024-10-10 11:45:50 +03:00
.editorconfig chore(deps): use scratch-renovate-config:conservative 2021-07-20 16:51:51 -07:00
.eslintignore Initial structure 2016-10-21 18:27:47 -07:00
.eslintrc.js Set root: true in .eslintrc.js 2016-12-08 21:17:07 -08:00
.gitattributes chore(deps): use scratch-renovate-config:conservative 2021-07-20 16:51:51 -07:00
.gitignore Initial structure 2016-10-21 18:27:47 -07:00
.npmignore Initial commit 2016-10-20 16:03:45 -07:00
.nvmrc ci: initial GHA boilerplate. added .nvmrc. commented out cci config 2023-10-10 15:00:13 -04:00
CHANGELOG.md chore(release): 3.0.26 [skip ci] 2024-11-13 06:57:11 +00:00
commitlint.config.js chore: update commitlint and its config 2023-12-20 18:51:19 -08:00
jest.config.js feat!: upgrade webpack to 5 and add TS support 2024-10-10 11:45:50 +03:00
LICENSE Initial commit 2016-10-20 16:03:45 -07:00
package-lock.json chore(deps): update dependency ts-jest-mock-import-meta to v1.2.1 2024-11-14 20:15:46 +00:00
package.json chore(deps): update dependency ts-jest-mock-import-meta to v1.2.1 2024-11-14 20:15:46 +00:00
README.md docs: replace LLK URL in README.md 2024-02-23 13:38:06 -08:00
release.config.js ci: update semantic-release config 2023-12-20 19:45:26 -08:00
renovate.json5 chore(deps): use js-lib-bundled Renovate config 2024-02-21 09:34:36 -08:00
TRADEMARK Update TRADEMARK 2018-06-18 13:17:31 -04:00
tsconfig.json feat!: upgrade webpack to 5 and add TS support 2024-10-10 11:45:50 +03:00
tsconfig.test.json feat!: upgrade webpack to 5 and add TS support 2024-10-10 11:45:50 +03:00
webpack.config.js chore: use the preferred webpack config format 2024-10-23 15:45:27 +03:00

scratch-storage

Scratch Storage is a library for loading and storing project and asset files for Scratch 3.0

CircleCI

Coverage Status Greenkeeper badge

Installation

This requires you to have Node.js installed.

In your own Node.js environment/application:

npm install https://github.com/scratchfoundation/scratch-storage.git

If you want to edit/play yourself (requires Git):

git clone https://github.com/scratchfoundation/scratch-storage.git
cd scratch-storage
npm install

Using scratch-storage

From HTML

<script src="scratch-storage/dist/web/scratch-storage.js"></script>
<script>
    var storage = new Scratch.Storage();
    // continue to "Storage API Quick Start" section below
</script>

From Node.js / Webpack

var storage = require('scratch-storage');
// continue to "Storage API Quick Start" section below

Storage API Quick Start

Once you have an instance of scratch-storage, add some web sources. For each source you'll need to provide a function to generate a URL for a supported type of asset:

/**
 * @param {Asset} asset - calculate a URL for this asset.
 * @returns {string} a URL to download a project asset (PNG, WAV, etc.)
 */
var getAssetUrl = function (asset) {
    var assetUrlParts = [
        'https://assets.example.com/path/to/assets/',
        asset.assetId,
        '.',
        asset.dataFormat,
        '/get/'
    ];
    return assetUrlParts.join('');
};

Then, let the storage module know about your source:

storage.addWebStore(
    [AssetType.ImageVector, AssetType.ImageBitmap, AssetType.Sound],
    getAssetUrl);

If you're using ES6 you may be able to simplify all of the above quite a bit:

storage.addWebStore(
    [AssetType.ImageVector, AssetType.ImageBitmap, AssetType.Sound],
    asset => `https://assets.example.com/path/to/assets/${asset.assetId}.${asset.dataFormat}/get/`);

Once the storage module is aware of the sources you need, you can start loading assets:

storage.load(AssetType.Sound, soundId).then(function (soundAsset) {
    // `soundAsset` is an `Asset` object. File contents are stored in `soundAsset.data`.
});

If you'd like to use scratch-storage with scratch-vm you must "attach" the storage module to the VM:

vm.attachStorage(storage);

Testing

To run all tests:

npm test

To show test coverage:

npm run coverage

Committing

This project uses semantic release to ensure version bumps follow semver so that projects using the config don't break unexpectedly.

In order to automatically determine the type of version bump necessary, semantic release expects commit messages to be formatted following conventional-changelog.

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

subject and body are your familiar commit subject and body. footer is where you would include BREAKING CHANGE and ISSUES FIXED sections if applicable.

type is one of:

  • fix: A bug fix Causes a patch release (0.0.x)
  • feat: A new feature Causes a minor release (0.x.0)
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  • refactor: A code change that neither fixes a bug nor adds a feature
  • perf: A code change that improves performance May or may not cause a minor release. It's not clear.
  • test: Adding missing tests or correcting existing tests
  • ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit

Use the commitizen CLI to make commits formatted in this way:

npm install -g commitizen
npm install

Now you're ready to make commits using git cz.

Breaking changes

If you're committing a change that makes an API change, or will otherwise require changes to existing code, ensure your commit specifies a breaking change. In your commit body, prefix the changes with "BREAKING CHANGE: " This will cause a major version bump so downstream projects must choose to upgrade and will not break the build unexpectedly.

Donate

We provide Scratch free of charge, and want to keep it that way! Please consider making a donation to support our continued engineering, design, community, and resource development efforts. Donations of any size are appreciated. Thank you!