Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
async-preloader
Advanced tools
Assets preloader using ES2017 async/await and fetch.
$ npm install --save async-preloader
interface LoadItem {
id?: string;
src: string;
loader?: string;
options?: object;
body?: "arrayBuffer" | "blob" | "formData" | "json" | "text"
}
Key | Description |
---|---|
id | Optional id to retrieve the file using AsyncPreloader.items.get(id) |
src | Input for the Fetch API |
loader | Optional string from one of the LOADERS Map. It needs to be specified for Font and Audio (webm |
options | Optional object to pass to the fetch method. |
body | Optional string to define the Body method to handle the Response. Default to blob() for Image, Video and Audio. |
A Map object containing the loaded items (keys being LoadItem.id
if specified or LoadItem.src
).
items
(Array): Array of LoadItem
s
(Promise): A Promise that resolves with the loaded items.
import AsyncPreloader from "async-preloader";
const pItems = AsyncPreloader.loadItems([
{ "id": "myDefaultFile", "src": "assets/default" },
{ "id": "myTextFile", "src": "assets/text.txt" },
{ "id": "myJsonFile", "src": "assets/json.json" },
{ "id": "myImageFile", "src": "assets/image.jpg" },
{ "id": "myVideoFile", "src": "assets/video.mp4" },
{ "id": "myAudioFile", "src": "assets/audio.mp3" },
{ "id": "myXmlFile", "src": "assets/xml.xml" },
{ "id": "mySvgFile", "src": "assets/xml.svg" },
{ "id": "myHtmlFile", "src": "assets/xml.html" },
{ "id": "myFont", "src": "Open Sans Regular", "loader": "Font" }
]);
pItems
.then(items => {
const element = AsyncPreloader.items.get("myVideoFile");
document.body.appendChild(element);
})
.catch(error => console.error("Error loading items", error));
Note: Font loader is using FontFaceObserver
You can also load a manifest file. It works in a similar fashion as createjs's PreloadJS.
src
(String): Input for the Fetch API. It will load the file using the JsonLoader
and look for an "items"
key containing an array of LoadItem
s.
(Promise): A Promise that resolves with the loaded items.
import AsyncPreloader from "async-preloader";
const pItems = AsyncPreloader.loadItems("assets/manifest.json");
pItems
.then(items => useLoadedItemsFromManifest(items)) // or AsyncPreloader.items.get(pathOrId)
.catch(error => console.error("Error loading items", error));
It is also possible to use the LOADERS individually.
item
(LoadItem): a LoadItem
.
(Promise): A Promise that resolves with the loaded item.
import AsyncPreloader from "async-preloader";
const pItem = AsyncPreloader.loadJson({ "src": "assets/json.json" });
pItem
.then(item => useLoadedItem(item))
.catch(error => console.error("Error loading item", error));
Since fetch
doesn't support Progress events
yet, you might want to get a per file progress:
import AsyncPreloader from "async-preloader";
let loadedCount = 0;
async function preload() {
await Promise.all(
itemsToLoad.map(async item => {
const data = await AsyncPreloader.loadItem(item);
loadedCount++;
console.log(loadedCount / itemsToLoad.length);
})
);
}
await preload();
ArrayBuffer
instead of a blobYou can specify how the response is handle by using the body
key in a LoadItem
.
Typical use case: use with the WebAudio API to decode the data with baseAudioContext.decodeAudioData()
:
import AsyncPreloader from "async-preloader";
const pItem = AsyncPreloader.loadAudio({ src: "assets/audio.mp3", body: "arrayBuffer" });
pItem
.then(item => audioContext.decodeAudioData(item))
.catch(error => console.error("Error decoding audio", error));
MIT © Damien Seguin
FAQs
Assets preloader using async/await and fetch for usage both in the browser and Node.js.
The npm package async-preloader receives a total of 0 weekly downloads. As such, async-preloader popularity was classified as not popular.
We found that async-preloader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.