Compare commits

...

3 Commits

Author SHA1 Message Date
semantic-release-bot 85cbf2afe5 chore(release): 0.2.0-develop.30 [skip ci]
# [0.2.0-develop.30](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.29...v0.2.0-develop.30) (2023-08-10)

### Features

* add new downloadSmallObject function that only hashes the data and compares it and does not verify it in real time ([747aeb7](747aeb7d2e))
2023-08-10 06:49:14 +00:00
Derrick Hammer 872fab5455
Merge remote-tracking branch 'origin/develop' into develop 2023-08-10 02:48:02 -04:00
Derrick Hammer 747aeb7d2e
feat: add new downloadSmallObject function that only hashes the data and compares it and does not verify it in real time 2023-08-10 02:47:57 -04:00
4 changed files with 62 additions and 5 deletions

View File

@ -1,3 +1,10 @@
# [0.2.0-develop.30](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.29...v0.2.0-develop.30) (2023-08-10)
### Features
* add new downloadSmallObject function that only hashes the data and compares it and does not verify it in real time ([747aeb7](https://git.lumeweb.com/LumeWeb/libweb/commit/747aeb7d2e34b702b764eae367f90b062aabc1f3))
# [0.2.0-develop.29](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.28...v0.2.0-develop.29) (2023-08-10)

12
npm-shrinkwrap.json generated
View File

@ -1,18 +1,19 @@
{
"name": "@lumeweb/libweb",
"version": "0.2.0-develop.29",
"version": "0.2.0-develop.30",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@lumeweb/libweb",
"version": "0.2.0-develop.29",
"version": "0.2.0-develop.30",
"dependencies": {
"@lumeweb/community-portals": "^0.1.0-develop.6",
"@lumeweb/libportal": "0.2.0-develop.17",
"@lumeweb/node-library-preset": "0.2.7",
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1"
"@noble/hashes": "^1.3.1",
"binconv": "^0.2.0"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.3",
@ -3814,6 +3815,11 @@
"node": ">=8"
}
},
"node_modules/binconv": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/binconv/-/binconv-0.2.0.tgz",
"integrity": "sha512-FAVbv8Fe1lsk1XQeWaYzfIQ9EnBgLJxjlqVA5XzbusP1YCzFgXUfIfmuanNmfM7i0XsNCNnYOnnq7NRLVKRgdQ=="
},
"node_modules/bottleneck": {
"version": "2.19.5",
"resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "@lumeweb/libweb",
"version": "0.2.0-develop.29",
"version": "0.2.0-develop.30",
"main": "lib/index.js",
"type": "module",
"repository": {
@ -25,7 +25,8 @@
"@lumeweb/libportal": "0.2.0-develop.17",
"@lumeweb/node-library-preset": "0.2.7",
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1"
"@noble/hashes": "^1.3.1",
"binconv": "^0.2.0"
},
"publishConfig": {
"access": "public"

View File

@ -1,6 +1,12 @@
import { getActivePortals } from "#portal.js";
import { ErrTuple } from "#types.js";
import { decodeCid, getVerifiableStream } from "@lumeweb/libportal";
import {
readableStreamToUint8Array,
uint8ArrayToReadableStream,
} from "binconv";
import { equalBytes } from "@noble/curves/abstract/utils";
import { blake3 } from "@noble/hashes/blake3";
const NO_PORTALS_ERROR = [null, "no active portals"] as ErrTuple;
@ -37,3 +43,40 @@ export async function downloadObject(cid: string): Promise<ErrTuple> {
return NO_PORTALS_ERROR;
}
export async function downloadSmallObject(cid: string): Promise<ErrTuple> {
const activePortals = getActivePortals();
if (!activePortals.length) {
return NO_PORTALS_ERROR;
}
for (const portal of activePortals) {
if (!(await portal.isLoggedIn())) {
try {
await portal.register();
} catch {}
await portal.login();
}
let stream;
try {
stream = await portal.downloadFile(cid);
} catch {
continue;
}
const CID = decodeCid(cid);
const data = await readableStreamToUint8Array(stream);
if (!equalBytes(blake3(data), CID.hash)) {
return [null, "cid verification failed"];
}
return [uint8ArrayToReadableStream(data), null];
}
return NO_PORTALS_ERROR;
}