scratch-storage
Version:
Load and store project and asset files for Scratch 3.0
204 lines (203 loc) • 9.43 kB
TypeScript
import BuiltinHelper from './BuiltinHelper';
import WebHelper, { UrlFunction } from './WebHelper';
import _Asset, { AssetData, AssetId } from './Asset';
import { AssetType } from './AssetType';
import { DataFormat } from './DataFormat';
import * as _scratchFetch from './scratchFetch';
import Helper from './Helper';
export declare class ScratchStorage {
defaultAssetId: Record<AssetType['name'], AssetId>;
builtinHelper: BuiltinHelper;
webHelper: WebHelper;
private _helpers;
constructor();
/**
* @returns {Asset} - the `Asset` class constructor.
* @class
*/
get Asset(): typeof _Asset;
/**
* @returns {AssetType} - the list of supported asset types.
* @class
*/
get AssetType(): {
readonly ImageBitmap: {
readonly contentType: "image/png";
readonly name: "ImageBitmap";
readonly runtimeFormat: "png";
readonly immutable: true;
};
readonly ImageVector: {
readonly contentType: "image/svg+xml";
readonly name: "ImageVector";
readonly runtimeFormat: "svg";
readonly immutable: true;
};
readonly Project: {
readonly contentType: "application/json";
readonly name: "Project";
readonly runtimeFormat: "json";
readonly immutable: false;
};
readonly Sound: {
readonly contentType: "audio/x-wav";
readonly name: "Sound";
readonly runtimeFormat: "wav";
readonly immutable: true;
};
readonly Sprite: {
readonly contentType: "application/json";
readonly name: "Sprite";
readonly runtimeFormat: "json";
readonly immutable: true;
};
};
/**
* @returns {DataFormat} - the list of supported data formats.
* @class
*/
get DataFormat(): {
readonly JPG: "jpg";
readonly JSON: "json";
readonly MP3: "mp3";
readonly PNG: "png";
readonly SB2: "sb2";
readonly SB3: "sb3";
readonly SVG: "svg";
readonly WAV: "wav";
};
/**
* Access the `scratchFetch` module within this library.
* @returns {module} the scratchFetch module, with properties for `scratchFetch`, `setMetadata`, etc.
*/
get scratchFetch(): typeof _scratchFetch;
/**
* @deprecated Please use the `Asset` member of a storage instance instead.
* @returns {Asset} - the `Asset` class constructor.
* @class
*/
static get Asset(): typeof _Asset;
/**
* @deprecated Please use the `AssetType` member of a storage instance instead.
* @returns {AssetType} - the list of supported asset types.
* @class
*/
static get AssetType(): {
readonly ImageBitmap: {
readonly contentType: "image/png";
readonly name: "ImageBitmap";
readonly runtimeFormat: "png";
readonly immutable: true;
};
readonly ImageVector: {
readonly contentType: "image/svg+xml";
readonly name: "ImageVector";
readonly runtimeFormat: "svg";
readonly immutable: true;
};
readonly Project: {
readonly contentType: "application/json";
readonly name: "Project";
readonly runtimeFormat: "json";
readonly immutable: false;
};
readonly Sound: {
readonly contentType: "audio/x-wav";
readonly name: "Sound";
readonly runtimeFormat: "wav";
readonly immutable: true;
};
readonly Sprite: {
readonly contentType: "application/json";
readonly name: "Sprite";
readonly runtimeFormat: "json";
readonly immutable: true;
};
};
/**
* Add a storage helper to this manager. Helpers with a higher priority number will be checked first when loading
* or storing assets. For comparison, the helper for built-in assets has `priority=100` and the default web helper
* has `priority=-100`. The relative order of helpers with equal priorities is undefined.
* @param {Helper} helper - the helper to be added.
* @param {number} [priority] - the priority for this new helper (default: 0).
*/
addHelper(helper: Helper, priority?: number): void;
/**
* Synchronously fetch a cached asset from built-in storage. Assets are cached when they are loaded.
* @param {string} assetId - The id of the asset to fetch.
* @returns {?Asset} The asset, if it exists.
*/
get(assetId: string): _Asset | null;
/**
* Deprecated API for caching built-in assets. Use createAsset.
* @param {AssetType} assetType - The type of the asset to cache.
* @param {DataFormat} dataFormat - The dataFormat of the data for the cached asset.
* @param {Buffer} data - The data for the cached asset.
* @param {string} id - The id for the cached asset.
* @returns {string} The calculated id of the cached asset, or the supplied id if the asset is mutable.
*/
cache(assetType: AssetType, dataFormat: DataFormat, data: AssetData, id: AssetId): AssetId;
/**
* Construct an Asset, and optionally generate an md5 hash of its data to create an id
* @param {AssetType} assetType - The type of the asset to cache.
* @param {DataFormat} dataFormat - The dataFormat of the data for the cached asset.
* @param {Buffer} data - The data for the cached asset.
* @param {string} [id] - The id for the cached asset.
* @param {bool} [generateId] - flag to set id to an md5 hash of data if `id` isn't supplied
* @returns {Asset} generated Asset with `id` attribute set if not supplied
*/
createAsset(assetType: AssetType, dataFormat: DataFormat, data: AssetData, id: AssetId, generateId: boolean): _Asset;
/**
* Register a web-based source for assets. Sources will be checked in order of registration.
* @param {Array.<AssetType>} types - The types of asset provided by this source.
* @param {UrlFunction} getFunction - A function which computes a GET URL from an Asset.
* @param {UrlFunction} createFunction - A function which computes a POST URL for asset data.
* @param {UrlFunction} updateFunction - A function which computes a PUT URL for asset data.
*/
addWebStore(types: AssetType[], getFunction: UrlFunction, createFunction?: UrlFunction, updateFunction?: UrlFunction): void;
/**
* Register a web-based source for assets. Sources will be checked in order of registration.
* @deprecated Please use addWebStore
* @param {Array.<AssetType>} types - The types of asset provided by this source.
* @param {UrlFunction} urlFunction - A function which computes a GET URL from an Asset.
*/
addWebSource(types: AssetType[], urlFunction: UrlFunction): void;
/**
* TODO: Should this be removed in favor of requesting an asset with `null` as the ID?
* @param {AssetType} type - Get the default ID for assets of this type.
* @returns {?string} The ID of the default asset of the given type, if any.
*/
getDefaultAssetId(type: AssetType): AssetId | undefined;
/**
* Set the default ID for a particular type of asset. This default asset will be used if a requested asset cannot
* be found and automatic fallback is enabled. Ideally this should be an asset that is available locally or even
* one built into this module.
* TODO: Should this be removed in favor of requesting an asset with `null` as the ID?
* @param {AssetType} type - The type of asset for which the default will be set.
* @param {string} id - The default ID to use for this type of asset.
*/
setDefaultAssetId(type: AssetType, id: AssetId): void;
/**
* Fetch an asset by type & ID.
* @param {AssetType} assetType - The type of asset to fetch. This also determines which asset store to use.
* @param {string} assetId - The ID of the asset to fetch: a project ID, MD5, etc.
* @param {DataFormat} [dataFormat] - Optional: load this format instead of the AssetType's default.
* @returns {Promise.<Asset>} A promise for the requested Asset.
* If the promise is resolved with non-null, the value is the requested asset.
* If the promise is resolved with null, the desired asset could not be found with the current asset sources.
* If the promise is rejected, there was an error on at least one asset source. HTTP 404 does not count as an
* error here, but (for example) HTTP 403 does.
*/
load(assetType: AssetType, assetId: AssetId, dataFormat: DataFormat): Promise<_Asset | null>;
/**
* Store an asset by type & ID.
* @param {AssetType} assetType - The type of asset to fetch. This also determines which asset store to use.
* @param {?DataFormat} [dataFormat] - Optional: load this format instead of the AssetType's default.
* @param {Buffer} data - Data to store for the asset
* @param {?string} [assetId] - The ID of the asset to fetch: a project ID, MD5, etc.
* @returns {Promise.<object>} A promise for asset metadata
*/
store(assetType: AssetType, dataFormat: DataFormat | null | undefined, data: AssetData, assetId?: AssetId): Promise<string | {
id: string;
}>;
}