From 0713216cd3213ce7e1405da101d85555a65fd446 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 23 Jul 2022 23:16:34 -0400 Subject: [PATCH] *Restructure to transpile to commonjs since vercel pkg does not support ESM --- package.json | 5 +++-- src/config.ts | 6 ++++-- src/dht.ts | 4 ++-- src/dns.ts | 3 +-- src/index.ts | 6 ++++-- src/relay.ts | 34 +++++++++++++++++++++++++++------- src/rpc.ts | 6 +++--- src/rpc/dns.ts | 5 +++-- src/rpc/handshake.ts | 23 +++++++++++++---------- src/rpc/solana.ts | 2 +- src/util.ts | 2 +- tsconfig.json | 4 ++-- 12 files changed, 64 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 86b930b..cb4599a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lumeweb/relay", - "type": "module", + "type": "commonjs", "version": "0.1.0", "description": "", "main": "build/index.js", @@ -50,6 +50,7 @@ "@types/sprintf-js": "^1.1.2", "esbuild": "^0.14.49", "hyper-typings": "^1.0.0", - "prettier": "^2.7.1" + "prettier": "^2.7.1", + "rollup": "^2.77.0" } } diff --git a/src/config.ts b/src/config.ts index cd69527..89abbfc 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,11 +1,13 @@ +//const require = createRequire(import.meta.url); +//import { createRequire } from "module"; + // @ts-ignore import BConfig from "bcfg"; import * as os from "os"; -import { createRequire } from "module"; + import path from "path"; import { errorExit } from "./error.js"; -const require = createRequire(import.meta.url); const config = new BConfig("lumeweb-relay"); let configLocation; diff --git a/src/dht.ts b/src/dht.ts index cf69835..f57734f 100644 --- a/src/dht.ts +++ b/src/dht.ts @@ -1,6 +1,6 @@ -import { createRequire } from "module"; +//const require = createRequire(import.meta.url); +//import { createRequire } from "module"; -const require = createRequire(import.meta.url); const DHT = require("@hyperswarm/dht"); import { deriveMyskyRootKeypair, diff --git a/src/dns.ts b/src/dns.ts index 945ccc2..707e387 100644 --- a/src/dns.ts +++ b/src/dns.ts @@ -10,8 +10,7 @@ import config from "./config.js"; import { hashDataKey } from "@lumeweb/kernel-utils"; import { errorExit } from "./error.js"; import log from "loglevel"; - -const { createHash } = await import("crypto"); +import { createHash } from "crypto"; let activeIp: string; diff --git a/src/index.ts b/src/index.ts index 1dae0e2..338d6fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,8 +5,10 @@ import config from "./config"; log.setDefaultLevel(config.str("log-level")); -await startRpc(); -await startRelay(); +async function boot() { + await startRpc(); + await startRelay(); +} process.on("uncaughtException", function (err) { console.log("Caught exception: " + err); diff --git a/src/relay.ts b/src/relay.ts index 8d63584..863052b 100644 --- a/src/relay.ts +++ b/src/relay.ts @@ -30,19 +30,14 @@ import { sprintf } from "sprintf-js"; let sslCtx: tls.SecureContext = tls.createSecureContext(); const sslParams: tls.SecureContextOptions = { cert: "", key: "" }; -const sslPrivateKey = await acme.forge.createPrivateKey(); -const acmeClient = new acme.Client({ - accountKey: sslPrivateKey, - directoryUrl: isSSlStaging() - ? acme.directory.letsencrypt.staging - : acme.directory.letsencrypt.production, -}); +let acmeClient: acme.Client; let app: Express; let router = express.Router(); const FILE_CERT_NAME = "/lumeweb/relay/ssl.crt"; const FILE_KEY_NAME = "/lumeweb/relay/ssl.key"; +const FILE_ACCOUNT_KEY_NAME = "/lumeweb/relay/account.key"; type SslData = { crt: IndependentFileSmall; key: IndependentFileSmall }; @@ -155,6 +150,30 @@ async function createOrRenewSSl( ) ); + let accountKey: boolean | IndependentFileSmall | Buffer = await getSslFile( + FILE_ACCOUNT_KEY_NAME + ); + + if (accountKey) { + accountKey = Buffer.from((accountKey as IndependentFileSmall).fileData); + } + + if (!accountKey) { + accountKey = await acme.forge.createPrivateKey(); + await createIndependentFileSmall( + getSeed(), + FILE_ACCOUNT_KEY_NAME, + accountKey + ); + } + + acmeClient = new acme.Client({ + accountKey: accountKey as Buffer, + directoryUrl: isSSlStaging() + ? acme.directory.letsencrypt.staging + : acme.directory.letsencrypt.production, + }); + const [certificateKey, certificateRequest] = await acme.forge.createCsr({ commonName: config.str("domain"), }); @@ -228,6 +247,7 @@ async function getCertInfo() { async function getSslCert(): Promise { return getSslFile(FILE_CERT_NAME); } + async function getSslKey(): Promise { return getSslFile(FILE_KEY_NAME); } diff --git a/src/rpc.ts b/src/rpc.ts index acd1e9d..4da7b23 100644 --- a/src/rpc.ts +++ b/src/rpc.ts @@ -1,8 +1,10 @@ +//const require = createRequire(import.meta.url); +//import { createRequire } from "module"; + import crypto from "crypto"; import jayson from "jayson/promise/index.js"; import { pack, unpack } from "msgpackr"; import { Mutex } from "async-mutex"; -import { createRequire } from "module"; import NodeCache from "node-cache"; import { get as getDHT } from "./dht.js"; import { rpcMethods } from "./rpc/index.js"; @@ -19,8 +21,6 @@ import { import config, { updateUsePocketGateway, usePocketGateway } from "./config.js"; import { ERR_NOT_READY, errorExit } from "./error.js"; -const require = createRequire(import.meta.url); - const stringify = require("json-stable-stringify"); const pendingRequests = new NodeCache(); const processedRequests = new NodeCache({ diff --git a/src/rpc/dns.ts b/src/rpc/dns.ts index 3c6b331..0a59254 100644 --- a/src/rpc/dns.ts +++ b/src/rpc/dns.ts @@ -1,8 +1,9 @@ +//import { createRequire } from "module"; +//const require = createRequire(import.meta.url); + import { isIp } from "../util.js"; import { RpcMethodList } from "./index.js"; -import { createRequire } from "module"; -const require = createRequire(import.meta.url); const bns = require("bns"); const { StubResolver, RecursiveResolver } = bns; diff --git a/src/rpc/handshake.ts b/src/rpc/handshake.ts index cd5c57a..0ee01f3 100644 --- a/src/rpc/handshake.ts +++ b/src/rpc/handshake.ts @@ -1,13 +1,14 @@ +//const require = createRequire(import.meta.url); +//import { createRequire } from "module"; + import { RpcMethodList } from "./index.js"; // @ts-ignore import rand from "random-key"; // @ts-ignore import SPVNode from "hsd/lib/node/spvnode.js"; import config from "../config.js"; -import { createRequire } from "module"; import { ERR_NOT_READY } from "../error.js"; -const require = createRequire(import.meta.url); const { NodeClient } = require("hs-client"); let hsdServer: SPVNode; @@ -54,15 +55,17 @@ if (!config.bool("hsd-use-external-node")) { } }); - try { - await hsdServer.ensure(); - await hsdServer.open(); - await hsdServer.connect(); + (async () => { + try { + await hsdServer.ensure(); + await hsdServer.open(); + await hsdServer.connect(); - hsdServer.startSync(); - } catch (e: any) { - console.error((e as Error).stack); - } + hsdServer.startSync(); + } catch (e: any) { + console.error((e as Error).stack); + } + })(); } else { clientArgs = { network: config.str("hsd-network-type"), diff --git a/src/rpc/solana.ts b/src/rpc/solana.ts index 5cd5996..b438cb6 100644 --- a/src/rpc/solana.ts +++ b/src/rpc/solana.ts @@ -1,6 +1,6 @@ import { proxyRpcMethod } from "./common.js"; import { RpcMethodList } from "./index.js"; -import * as chainNetworks from "../networks.json" assert { type: "json" }; +import * as chainNetworks from "../networks.json"; export default { getAccountInfo: proxyRpcMethod("getAccountInfo", [ diff --git a/src/util.ts b/src/util.ts index a354afc..45c8697 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,4 @@ -import * as chainNetworks from "./networks.json" assert { type: "json" }; +import * as chainNetworks from "./networks.json"; type networks = { [net: string]: string }; diff --git a/tsconfig.json b/tsconfig.json index b742cda..6c2a53a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { - "target": "esnext", - "module": "esnext", + "target": "es2020", + "module": "commonjs", "moduleResolution": "node", "rootDir": "src", "outDir": "build",