Compare commits

...

5 Commits

6 changed files with 165 additions and 1154 deletions

View File

@ -1,3 +1,10 @@
# [0.2.0-develop.16](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.15...v0.2.0-develop.16) (2023-06-26)
### Features
* add initial upload method ([7126203](https://git.lumeweb.com/LumeWeb/libweb/commit/7126203cd3c97485de422e8759442a04074a8f64))
# [0.2.0-develop.15](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.14...v0.2.0-develop.15) (2023-06-25)
# [0.2.0-develop.14](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.13...v0.2.0-develop.14) (2023-06-24)

1263
npm-shrinkwrap.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@lumeweb/libweb",
"version": "0.2.0-develop.15",
"version": "0.2.0-develop.16",
"main": "lib/index.js",
"type": "module",
"repository": {
@ -22,8 +22,8 @@
},
"dependencies": {
"@lumeweb/community-portals": "^0.1.0-develop.2",
"@lumeweb/libportal": "0.2.0-develop.8",
"@lumeweb/node-library-preset": "https://git.lumeweb.com/LumeWeb/node-library-preset/archive/v0.1.1-develop.11.tar.gz",
"@lumeweb/libportal": "^0.2.0-develop.10",
"@lumeweb/node-library-preset": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git",
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1"
},

View File

@ -1,4 +1,5 @@
import { objAsString } from "./objAsString.js";
import { ErrTuple } from "#types.js";
// addContextToErr is a helper function that standardizes the formatting of
// adding context to an error.
@ -14,3 +15,5 @@ function addContextToErr(err: any, context: string): string {
}
export { addContextToErr };
export const NO_PORTALS_ERROR = [null, "no active portals"] as ErrTuple;

View File

@ -11,5 +11,6 @@ export * from "./cid.js";
export * from "./encoding.js";
export * from "./keys.js";
export * from "./download.js";
export * from "./upload.js";
export * from "./portal.js";
export { ed25519, sha512 };

39
src/upload.ts Normal file
View File

@ -0,0 +1,39 @@
import { ErrTuple } from "#types.js";
import { getActivePortals } from "#portal.js";
import { NO_PORTALS_ERROR } from "#err.js";
export async function uploadObject(
file:
| ReadableStream<Uint8Array>
| import("stream").Readable
| Uint8Array
| Blob,
size?: bigint,
): 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 upload;
try {
upload = await portal.uploadFile(file as any, size);
} catch {
continue;
}
return [upload, null];
}
return NO_PORTALS_ERROR;
}