Add node download example

This commit is contained in:
Arnaud 2025-05-26 12:54:34 +02:00
parent 496aae84e7
commit b50c0b7107
No known key found for this signature in database
GPG Key ID: B8FBC178F10CA7AE
11 changed files with 131 additions and 2 deletions

View File

@ -1,5 +1,5 @@
{
"name": "@codex-storage/sdk-js-download-example",
"name": "@codex-storage/sdk-js-download-browser-example",
"version": "1.0.0",
"main": "index.js",
"scripts": {
@ -15,4 +15,4 @@
"esbuild": "^0.25.1",
"prettier": "^3.5.3"
}
}
}

1
examples/download-node/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
index.bundle.js

View File

@ -0,0 +1,19 @@
# Download example
Small example to show how to download a file in node with Codex.
## Install dependencies
```bash
npm install
```
## Run node
```bash
CODEX_CID=REPLACE_BY_YOUR_CID node index.js
```
The response will be displayed as text, so it is better to test with .txt files.
Note: You can define `CODEX_NODE_URL`, default value is "http://localhost:8080".

View File

@ -0,0 +1,17 @@
import { Codex } from "@codex-storage/sdk-js";
async function main() {
const codex = new Codex(
process.env.CODEX_NODE_URL || "http://localhost:8080"
);
const data = codex.data;
const cid = process.env.CODEX_CID;
const result = await data.networkDownloadStream(cid);
console.info(await result.data.text());
}
main();

74
examples/download-node/package-lock.json generated Normal file
View File

@ -0,0 +1,74 @@
{
"name": "@codex-storage/sdk-js-download-node-example",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@codex-storage/sdk-js-download-node-example",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"@codex-storage/sdk-js": "../..",
"undici": "^7.7.0"
},
"devDependencies": {
"prettier": "^3.5.3"
}
},
"..": {
"extraneous": true
},
"../..": {
"name": "@codex-storage/sdk-js",
"version": "0.1.2",
"license": "MIT",
"dependencies": {
"valibot": "^1.0.0"
},
"devDependencies": {
"@tsconfig/strictest": "^2.0.5",
"@types/node": "^22.13.17",
"oas-normalize": "^13.1.2",
"openapi-typescript": "^7.6.1",
"prettier": "^3.5.3",
"tsup": "^8.3.6",
"typescript": "^5.8.2",
"vitest": "^3.1.1"
},
"engines": {
"node": ">=20.18.1"
},
"peerDependencies": {
"undici": "^7.7.0"
}
},
"node_modules/@codex-storage/sdk-js": {
"resolved": "../..",
"link": true
},
"node_modules/prettier": {
"version": "3.5.3",
"dev": true,
"license": "MIT",
"bin": {
"prettier": "bin/prettier.cjs"
},
"engines": {
"node": ">=14"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
},
"node_modules/undici": {
"version": "7.10.0",
"resolved": "https://registry.npmjs.org/undici/-/undici-7.10.0.tgz",
"integrity": "sha512-u5otvFBOBZvmdjWLVW+5DAc9Nkq8f24g0O9oY7qw2JVIF1VocIFoyz9JFkuVOS2j41AufeO0xnlweJ2RLT8nGw==",
"license": "MIT",
"engines": {
"node": ">=20.18.1"
}
}
}
}

View File

@ -0,0 +1,18 @@
{
"name": "@codex-storage/sdk-js-download-node-example",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"build": "node esbuild.js"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@codex-storage/sdk-js": "../..",
"undici": "^7.7.0"
},
"devDependencies": {
"prettier": "^3.5.3"
}
}