Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Apr 21, 2024
1 parent c309497 commit 4d2fdde
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ENV GRPC_SERVER_KEY=""
# NOSTR_CONNECT_SECRET_KEY empty = generate random key
ENV NOSTR_SECRET_KEY=""
ENV NOSTR_RELAYS=""
ENV BLOB_STORAGE_PATH="/blobs"

EXPOSE 5000

Expand Down
8 changes: 7 additions & 1 deletion src/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import * as GRPC from "@grpc/grpc-js";
import Utils from "./Utils";
import { generateSecretKey, getPublicKey } from "nostr-tools";

export default class Auth {


static isAuthorized(methodName: string, nodeId: string): boolean {
return true;
}
static adaptService(data: [GRPC.ServiceDefinition, GRPC.UntypedServiceImplementation]): [GRPC.ServiceDefinition, GRPC.UntypedServiceImplementation]{

static adaptService(
poolSecretKey: string,
data: [GRPC.ServiceDefinition, GRPC.UntypedServiceImplementation]): [GRPC.ServiceDefinition, GRPC.UntypedServiceImplementation]{
let [def, impl] = data;

impl = Object.fromEntries(
Expand Down
6 changes: 5 additions & 1 deletion src/HyperdrivePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export default class HyperdrivePool {
async create(
owner: string,
encryptionKey?: string,
includeEncryptionKeyInUrl: boolean = false
includeEncryptionKeyInUrl: boolean = false,
): Promise<string> {
await this.discovery.flushed();

Expand Down Expand Up @@ -320,6 +320,10 @@ export default class HyperdrivePool {
}

async open(owner: string, bundleUrl: string, encryptionKey?: string): Promise<string> {
if (!bundleUrl.startsWith("hyperdrive+bundle://")) {
bundleUrl = "hyperdrive+bundle://" + bundleUrl;
}

await this.discovery.flushed();
const bundleData = this.parseHyperUrl(bundleUrl, encryptionKey);

Expand Down
55 changes: 29 additions & 26 deletions src/RPCServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class RpcConnector implements IPoolConnector {
url,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -97,7 +97,7 @@ class RpcConnector implements IPoolConnector {
diskId: disk,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -110,7 +110,7 @@ class RpcConnector implements IPoolConnector {
success: true,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -126,7 +126,7 @@ class RpcConnector implements IPoolConnector {
success: true,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -143,7 +143,7 @@ class RpcConnector implements IPoolConnector {
files,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -162,7 +162,7 @@ class RpcConnector implements IPoolConnector {
}
await responses.complete();
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -187,7 +187,7 @@ class RpcConnector implements IPoolConnector {
}
await outputStream.write(request.data);
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -196,7 +196,7 @@ class RpcConnector implements IPoolConnector {
success: true,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -213,7 +213,7 @@ class RpcConnector implements IPoolConnector {
success: true,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -230,7 +230,7 @@ class RpcConnector implements IPoolConnector {
data,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -246,7 +246,7 @@ class RpcConnector implements IPoolConnector {
const job = await this.conn.getJob(nodeId, id);
return job;
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -273,7 +273,7 @@ class RpcConnector implements IPoolConnector {
};
return pendingJobs;
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -292,7 +292,7 @@ class RpcConnector implements IPoolConnector {
};
}
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -302,7 +302,7 @@ class RpcConnector implements IPoolConnector {
const nodeId = this.getNodeId(context);
return this.conn.acceptJob(nodeId, request.jobId);
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -312,7 +312,7 @@ class RpcConnector implements IPoolConnector {
const nodeId = this.getNodeId(context);
return this.conn.cancelJob(nodeId, request.jobId, request.reason);
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -322,7 +322,7 @@ class RpcConnector implements IPoolConnector {
const nodeId = this.getNodeId(context);
return this.conn.outputForJob(nodeId, request.jobId, request.output);
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -332,7 +332,7 @@ class RpcConnector implements IPoolConnector {
const nodeId = this.getNodeId(context);
return this.conn.completeJob(nodeId, request.jobId, request.output);
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -342,7 +342,7 @@ class RpcConnector implements IPoolConnector {
const nodeId = this.getNodeId(context);
return this.conn.logForJob(nodeId, request.jobId, request.log);
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -361,7 +361,7 @@ class RpcConnector implements IPoolConnector {
request.outputFormat
);
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -378,7 +378,7 @@ class RpcConnector implements IPoolConnector {
success: true,
} as RpcSendSignedEventResponse;
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -395,7 +395,7 @@ class RpcConnector implements IPoolConnector {
subscriptionId: subId,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -415,7 +415,7 @@ class RpcConnector implements IPoolConnector {
events,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -431,7 +431,7 @@ class RpcConnector implements IPoolConnector {
success: true,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -453,7 +453,7 @@ class RpcConnector implements IPoolConnector {
refreshInterval: timeout,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -472,7 +472,7 @@ class RpcConnector implements IPoolConnector {
refreshInterval: timeout,
};
} catch (e) {
console.error(e);
console.log(e);
throw e;
}
}
Expand All @@ -488,7 +488,9 @@ export default class RPCServer {
serverCrt: Buffer | undefined;
serverKey: Buffer | undefined;
hyperdrivePool: HyperdrivePool;
poolSecretKey: string;
constructor(
poolSecretKey: string,
addr: string,
port: number,
descriptorPath: string,
Expand Down Expand Up @@ -516,6 +518,7 @@ export default class RPCServer {

server.addService(
...Auth.adaptService(
this.poolSecretKey,
GPRCBackend.adaptService(
PoolConnector,
new RpcConnector(this.nostrConnector, this.hyperdrivePool)
Expand Down Expand Up @@ -546,7 +549,7 @@ export default class RPCServer {
(err: Error | null, port: number) => {
if (err) {
reject(err);
console.error(`Server error: ${err.message}`);
console.log(`Server error: ${err.message}`);
} else {
resolve(true);
console.log(`Server bound on port: ${port}`);
Expand Down
12 changes: 11 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ async function main(){
const nostr = new NostrConnector(SECRET_KEY, RELAYS, undefined);
nostr.setWebHooks(webhooks);
const hyp = new HyperdrivePool(BLOB_STORAGE_PATH, nostr);
const server = new RPCServer(IP, PORT, DESCRIPTOR_PATH, nostr, hyp, CA_CRT, SERVER_CRT, SERVER_KEY);
const server = new RPCServer(
SECRET_KEY,
IP,
PORT,
DESCRIPTOR_PATH,
nostr,
hyp,
CA_CRT,
SERVER_CRT,
SERVER_KEY
);
await server.start();

}
Expand Down

0 comments on commit 4d2fdde

Please sign in to comment.