feat: add initial upload method

This commit is contained in:
Derrick Hammer 2023-06-26 00:26:49 -04:00
parent b406ea60bb
commit 7126203cd3
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 153 additions and 1148 deletions

1256
npm-shrinkwrap.json generated

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,7 @@
}, },
"dependencies": { "dependencies": {
"@lumeweb/community-portals": "^0.1.0-develop.2", "@lumeweb/community-portals": "^0.1.0-develop.2",
"@lumeweb/libportal": "0.2.0-develop.8", "@lumeweb/libportal": "^0.2.0-develop.10",
"@lumeweb/node-library-preset": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git", "@lumeweb/node-library-preset": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git",
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1" "@noble/hashes": "^1.3.1"

View File

@ -1,4 +1,5 @@
import { objAsString } from "./objAsString.js"; import { objAsString } from "./objAsString.js";
import { ErrTuple } from "#types.js";
// addContextToErr is a helper function that standardizes the formatting of // addContextToErr is a helper function that standardizes the formatting of
// adding context to an error. // adding context to an error.
@ -14,3 +15,5 @@ function addContextToErr(err: any, context: string): string {
} }
export { addContextToErr }; 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 "./encoding.js";
export * from "./keys.js"; export * from "./keys.js";
export * from "./download.js"; export * from "./download.js";
export * from "./upload.js";
export * from "./portal.js"; export * from "./portal.js";
export { ed25519, sha512 }; 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;
}