Skip to content

Commit

Permalink
Initial implementation NIP-04, disable drivers cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Apr 21, 2024
1 parent 8fffd5f commit 21b7ade
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/HyperdrivePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,25 @@ export class SharedDrive {


async put(path: string, data: string|Uint8Array) {
this.lastAccess = Date.now();
for (const drive of this.drives) {
if (drive.writable) {
await drive.put(path, data);
}
}

}

async get(path: string) : Promise<Buffer> {
// TODO: Pick newer
this.lastAccess = Date.now();
for (const drive of this.drives) {
if (await drive.exists(path)) {
return drive.get(path);
}
}
throw "File not found";

}


Expand Down Expand Up @@ -115,28 +119,29 @@ export class SharedDrive {
}

async exists(path: string) {
this.lastAccess = Date.now();
for (const drive of this.drives) {
if (await drive.exists(path)) {
return true;
}
}
this.lastAccess = Date.now();
return false;
}

async list(path: string) {
this.lastAccess = Date.now();
const files = [];
for (const drive of this.drives) {
for await (const entry of drive.list(path)) {
const path = entry.key;
if (!files.includes(path)) files.push(path);
}
}
this.lastAccess = Date.now();
return files;
}

async inputStream(path: string): Promise<ReadableGreedy> {
this.lastAccess = Date.now();
let newestEntryInDrive;
let newestEntryTimestamp = 0;
for (const drive of this.drives) {
Expand All @@ -149,7 +154,6 @@ export class SharedDrive {
}
}
if (!newestEntryInDrive) throw "File not found";
this.lastAccess = Date.now();
const rs = newestEntryInDrive.createReadStream(path);
rs.readAll = async () => {
const buffers = [];
Expand All @@ -162,6 +166,7 @@ export class SharedDrive {
}

async del(key: string) {
this.lastAccess = Date.now();
for (const drive of this.drives) {
if (drive.writable) {
await drive.del(key);
Expand All @@ -171,6 +176,7 @@ export class SharedDrive {
}

async close() {
this.lastAccess = Date.now();
for (const drive of this.drives) {
await drive.close();
}
Expand Down Expand Up @@ -233,15 +239,15 @@ export default class HyperdrivePool {

_cleanup(){
if(this.isClosed) return;
const now = Date.now();
for (const key in this.drives) {
const sharedDrive = this.drives[key];
if (sharedDrive.isClosed || now-sharedDrive.lastAccess>this.driverTimeout) {
sharedDrive.close();
delete this.drives[key];
}
}
setTimeout(()=>this._cleanup(),1000*60*5);
// const now = Date.now();
// for (const key in this.drives) {
// const sharedDrive = this.drives[key];
// if (sharedDrive.isClosed || now-sharedDrive.lastAccess>this.driverTimeout) {
// sharedDrive.close();
// delete this.drives[key];
// }
// }
// setTimeout(()=>this._cleanup(),1000*60*5);
}

createHyperUrl(diskName: string, diskKey: string, encryptionKey?: string) {
Expand Down
14 changes: 13 additions & 1 deletion src/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from "nostr-tools";
import { Event, nip04 } from "nostr-tools";
import { uuid as uuidv4 } from "uuidv4";
import crypto from "crypto";

Expand All @@ -7,6 +7,18 @@ export default class Utils {
return uuidv4();
}

static async encryptNostr(
text: string,
ourPrivateKey: string | Uint8Array,
theirPublicKey: string
): Promise<string> {
return await nip04.encrypt(ourPrivateKey, theirPublicKey, text);
}

static async decryptNostr(text: string, ourPrivateKey: string, theirPublicKey: string): Promise<string> {
return await nip04.decrypt(ourPrivateKey, theirPublicKey, text);
}

static encrypt(v: string, secret: string): string {
const key = crypto.createHash("sha256").update(String(secret)).digest();
const cipher = crypto.createCipheriv("aes-256-ecb", key, null);
Expand Down

0 comments on commit 21b7ade

Please sign in to comment.