![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
This library allows you to interact with the Swarm network from JavaScript.
Install
npm install swarm-js
Import
// Loads the Swarm API pointing to the official gateway
const swarm = require("swarm-js").at("http://swarm-gateways.net");
With JSON:
Raw data:
const file = "test file"; // could also be an Uint8Array of binary data
swarm.upload(file).then(hash => {
console.log("Uploaded file. Address:", hash);
})
Directory:
To upload a directory, just call swarm.upload(directory)
, where directory is an object mapping paths to entries, those containing a mime-type and the data (a buffer).
const dir = {
"/foo.txt": {type: "text/plain", data: "file 0"},
"/bar.txt": {type: "text/plain", data: "file 1"}
};
swarm.upload(dir).then(hash => {
console.log("Uploaded directory. Address:", hash);
});
From disk:
On Node.js:
swarm.upload({
path: "/path/to/thing", // path to data / file / directory
kind: "directory", // could also be "file" or "data"
defaultFile: "/index.html"}) // optional, and only for kind === "directory"
.then(console.log)
.catch(console.log);
On browsers:
// only works inside an event
document.onClick = function() {
swarm.upload({pick: "file"}) // could also be "directory" or "data"
.then(alert);
};
With JSON:
Raw data:
const fileHash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";
swarm.download(fileHash).then(buffer => {
console.log("Downloaded file:", buffer.toString());
});
Directory:
const dirHash = "7e980476df218c05ecfcb0a2ca73597193a34c5a9d6da84d54e295ecd8e0c641";
swarm.download(dirHash).then(dir => {
console.log("Downloaded directory:");
for (let path in dir) {
console.log("-", path, ":", dir[path].data.toString());
}
});
To disk:
On Node.js:
swarm.download("DAPP_HASH", "/target/dir")
.then(path => console.log(`Downloaded DApp to ${path}.`))
.catch(console.log);
On browser:
(Just link the Swarm URL.)
For more examples, check out examples.
FAQs
Swarm tools for JavaScript.
The npm package swarm-js receives a total of 229,102 weekly downloads. As such, swarm-js popularity was classified as popular.
We found that swarm-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.