Compare commits

...

3 Commits

6 changed files with 14 additions and 101 deletions

View File

@ -1,3 +1,5 @@
# [0.2.0-develop.27](https://git.lumeweb.com/LumeWeb/libportal/compare/v0.2.0-develop.26...v0.2.0-develop.27) (2023-09-07)
# [0.2.0-develop.26](https://git.lumeweb.com/LumeWeb/libportal/compare/v0.2.0-develop.25...v0.2.0-develop.26) (2023-09-07) # [0.2.0-develop.26](https://git.lumeweb.com/LumeWeb/libportal/compare/v0.2.0-develop.25...v0.2.0-develop.26) (2023-09-07)
# [0.2.0-develop.25](https://git.lumeweb.com/LumeWeb/libportal/compare/v0.2.0-develop.24...v0.2.0-develop.25) (2023-09-04) # [0.2.0-develop.25](https://git.lumeweb.com/LumeWeb/libportal/compare/v0.2.0-develop.24...v0.2.0-develop.25) (2023-09-04)

12
npm-shrinkwrap.json generated
View File

@ -1,14 +1,14 @@
{ {
"name": "@lumeweb/libportal", "name": "@lumeweb/libportal",
"version": "0.2.0-develop.26", "version": "0.2.0-develop.27",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@lumeweb/libportal", "name": "@lumeweb/libportal",
"version": "0.2.0-develop.26", "version": "0.2.0-develop.27",
"dependencies": { "dependencies": {
"@lumeweb/libs5": "^0.1.0-develop.48", "@lumeweb/libs5": "^0.1.0-develop.50",
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1", "@noble/hashes": "^1.3.1",
"detect-node": "^2.1.0", "detect-node": "^2.1.0",
@ -1928,9 +1928,9 @@
"peer": true "peer": true
}, },
"node_modules/@lumeweb/libs5": { "node_modules/@lumeweb/libs5": {
"version": "0.1.0-develop.48", "version": "0.1.0-develop.50",
"resolved": "https://registry.npmjs.org/@lumeweb/libs5/-/libs5-0.1.0-develop.48.tgz", "resolved": "https://registry.npmjs.org/@lumeweb/libs5/-/libs5-0.1.0-develop.50.tgz",
"integrity": "sha512-XO7btWOWMTrYD47uPtdVNLpni0PGOn4XDEsFO94WNO77WJ5E9wSDEFvpPjYjEi/51A2k4g3ku54TVPFCPDCHew==", "integrity": "sha512-mPywIV3n7YS5GdJHM0Ge9ODYeaHcdY6WJJMxlIr9OT2SudjCJJ426BynxGWSvJZR0O8u/SPyxNR6g+Uh1iC/1g==",
"dependencies": { "dependencies": {
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1", "@noble/hashes": "^1.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@lumeweb/libportal", "name": "@lumeweb/libportal",
"version": "0.2.0-develop.26", "version": "0.2.0-develop.27",
"main": "lib/index.js", "main": "lib/index.js",
"type": "module", "type": "module",
"repository": { "repository": {
@ -34,7 +34,7 @@
"semantic-release": "semantic-release" "semantic-release": "semantic-release"
}, },
"dependencies": { "dependencies": {
"@lumeweb/libs5": "^0.1.0-develop.48", "@lumeweb/libs5": "^0.1.0-develop.50",
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1", "@noble/hashes": "^1.3.1",
"detect-node": "^2.1.0", "detect-node": "^2.1.0",

View File

@ -1,88 +0,0 @@
import { base58btc } from "multiformats/bases/base58";
import * as edUtils from "@noble/curves/abstract/utils";
import { CID_HASH_TYPES, CID_TYPES } from "@lumeweb/libs5";
export interface CID {
hash: Uint8Array;
size: bigint;
type: number;
hashType: number;
}
export function encodeCid(
hash: Uint8Array,
size: bigint,
type?: number,
hashType?: number,
raw?: boolean,
);
export function encodeCid(
hash: string,
size: bigint,
type?: number,
hashType?: number,
raw?: boolean,
);
export function encodeCid(
hash: any,
size: bigint,
type = CID_TYPES.RAW,
hashType = CID_HASH_TYPES.BLAKE3,
raw: boolean = false,
) {
if (typeof hash === "string") {
hash = edUtils.hexToBytes(hash);
}
if (!(hash instanceof Uint8Array)) {
throw new Error();
}
if (size === undefined || size === null) {
throw new Error("size required");
}
size = BigInt(size);
const sizeBytes = new Uint8Array(8);
const sizeView = new DataView(sizeBytes.buffer);
sizeView.setBigInt64(0, size, true);
const prefixedHash = Uint8Array.from([type, hashType, ...hash, ...sizeBytes]);
if (raw) {
return prefixedHash;
}
return base58btc.encode(prefixedHash).toString();
}
export function decodeCid(cid: string | Uint8Array): CID {
let bytes = cid;
if (typeof bytes === "string") {
bytes = base58btc.decode(bytes);
}
if (!Object.values(CID_TYPES).includes(bytes[0])) {
throw new Error("Invalid cid type");
}
if (!Object.values(CID_HASH_TYPES).includes(bytes[1])) {
throw new Error("Invalid cid hash type");
}
const type = bytes[0];
const hashType = bytes[1];
bytes = bytes.slice(2);
let cidHash = bytes.slice(0, 32);
let size = bytes.slice(32);
const sizeView = new DataView(size.buffer);
return {
hash: cidHash,
size: sizeView.getBigInt64(0, true),
type,
hashType,
};
}

View File

@ -19,7 +19,6 @@ import streamToBlob from "stream-to-blob";
import defer from "p-defer"; import defer from "p-defer";
import { blake3 } from "@noble/hashes/blake3"; import { blake3 } from "@noble/hashes/blake3";
import { encodeCid } from "./cid.js";
import { import {
AuthStatusResponse, AuthStatusResponse,
LoginResponse, LoginResponse,
@ -27,6 +26,7 @@ import {
} from "./responses/auth.js"; } from "./responses/auth.js";
import isNode from "detect-node"; import isNode from "detect-node";
import { utf8ToBytes } from "@noble/curves/abstract/utils"; import { utf8ToBytes } from "@noble/curves/abstract/utils";
import { CID, CID_TYPES } from "@lumeweb/libs5";
type NodeReadableStreamType = typeof import("stream").Readable; type NodeReadableStreamType = typeof import("stream").Readable;
type NodePassThroughStreamType = typeof import("stream").PassThrough; type NodePassThroughStreamType = typeof import("stream").PassThrough;
@ -469,10 +469,10 @@ export class Client {
await ret.promise; await ret.promise;
const cid = encodeCid(hash, size as bigint); const cid = CID.fromHash(hash, Number(size));
while (true) { while (true) {
const status = await this.getUploadStatus(cid as string); const status = await this.getUploadStatus(cid.toString());
if (status.status === "uploaded") { if (status.status === "uploaded") {
break; break;
@ -483,7 +483,7 @@ export class Client {
}); });
} }
return cid as string; return cid.toString();
} }
async getUploadStatus(cid: string) { async getUploadStatus(cid: string) {

View File

@ -1,3 +1,2 @@
export * from "./client.js"; export * from "./client.js";
export * from "./cid.js";
export * from "./verify.js"; export * from "./verify.js";