*refactor to use dynamic imports for ESM modules and do so via a helper that bypasses typescript parsing.
This commit is contained in:
parent
5a17b6befd
commit
01a32d8734
20
src/dht.ts
20
src/dht.ts
|
@ -3,14 +3,9 @@
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import DHT from "@hyperswarm/dht";
|
import DHT from "@hyperswarm/dht";
|
||||||
import {
|
|
||||||
deriveMyskyRootKeypair,
|
|
||||||
Ed25519Keypair,
|
|
||||||
seedPhraseToSeed,
|
|
||||||
validSeedPhrase,
|
|
||||||
} from "libskynet";
|
|
||||||
import config from "./config.js";
|
import config from "./config.js";
|
||||||
import { errorExit } from "./error.js";
|
import { errorExit } from "./error.js";
|
||||||
|
import { dynImport } from "./util.js";
|
||||||
|
|
||||||
let node: {
|
let node: {
|
||||||
ready: () => any;
|
ready: () => any;
|
||||||
|
@ -18,12 +13,17 @@ let node: {
|
||||||
defaultKeyPair: any;
|
defaultKeyPair: any;
|
||||||
on: any;
|
on: any;
|
||||||
};
|
};
|
||||||
let server: {
|
let server: any;
|
||||||
listen: (arg0: Ed25519Keypair) => any;
|
|
||||||
on: any;
|
|
||||||
};
|
|
||||||
|
|
||||||
async function start() {
|
async function start() {
|
||||||
|
const {
|
||||||
|
deriveMyskyRootKeypair,
|
||||||
|
// @ts-ignore
|
||||||
|
Ed25519Keypair,
|
||||||
|
seedPhraseToSeed,
|
||||||
|
validSeedPhrase,
|
||||||
|
} = await dynImport("libskynet");
|
||||||
|
|
||||||
const seed = config.str("seed");
|
const seed = config.str("seed");
|
||||||
|
|
||||||
let err = validSeedPhrase(seed);
|
let err = validSeedPhrase(seed);
|
||||||
|
|
12
src/dns.ts
12
src/dns.ts
|
@ -1,18 +1,19 @@
|
||||||
import cron from "node-cron";
|
import cron from "node-cron";
|
||||||
import fetch from "node-fetch";
|
|
||||||
import { get as getDHT } from "./dht.js";
|
import { get as getDHT } from "./dht.js";
|
||||||
import { overwriteRegistryEntry } from "libskynetnode";
|
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { Parser } from "xml2js";
|
import { Parser } from "xml2js";
|
||||||
import { URL } from "url";
|
import { URL } from "url";
|
||||||
import { pack } from "msgpackr";
|
import { pack } from "msgpackr";
|
||||||
import config from "./config.js";
|
import config from "./config.js";
|
||||||
import { hashDataKey } from "@lumeweb/kernel-utils";
|
|
||||||
import { errorExit } from "./error.js";
|
import { errorExit } from "./error.js";
|
||||||
import log from "loglevel";
|
import log from "loglevel";
|
||||||
import { createHash } from "crypto";
|
import { createHash } from "crypto";
|
||||||
|
import { dynImport } from "./util.js";
|
||||||
|
|
||||||
let activeIp: string;
|
let activeIp: string;
|
||||||
|
let fetch: typeof import("node-fetch").default;
|
||||||
|
let overwriteRegistryEntry: typeof import("libskynetnode").overwriteRegistryEntry;
|
||||||
|
let hashDataKey: typeof import("@lumeweb/kernel-utils").hashDataKey;
|
||||||
|
|
||||||
const REGISTRY_NODE_KEY = "lumeweb-dht-node";
|
const REGISTRY_NODE_KEY = "lumeweb-dht-node";
|
||||||
|
|
||||||
|
@ -33,6 +34,11 @@ async function ipUpdate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function start() {
|
export async function start() {
|
||||||
|
fetch = (await dynImport("node-fetch")).default;
|
||||||
|
overwriteRegistryEntry = (await dynImport("libskynetnode"))
|
||||||
|
.overwriteRegistryEntry;
|
||||||
|
hashDataKey = (await dynImport("@lumeweb/kernel-utils")).hashDataKey;
|
||||||
|
|
||||||
const dht = (await getDHT()) as any;
|
const dht = (await getDHT()) as any;
|
||||||
|
|
||||||
await ipUpdate();
|
await ipUpdate();
|
||||||
|
|
89
src/file.ts
89
src/file.ts
|
@ -1,30 +1,7 @@
|
||||||
import {
|
import type { Ed25519Keypair, Err, progressiveFetchResult } from "libskynet";
|
||||||
addContextToErr,
|
|
||||||
blake2b,
|
|
||||||
bufToHex,
|
|
||||||
Ed25519Keypair,
|
|
||||||
ed25519Sign,
|
|
||||||
encodePrefixedBytes,
|
|
||||||
encodeU64,
|
|
||||||
Err,
|
|
||||||
} from "libskynet";
|
|
||||||
import { progressiveFetch } from "libskynetnode/dist/progressivefetch.js";
|
|
||||||
import { defaultPortalList } from "libskynet/dist/defaultportals.js";
|
|
||||||
import { readRegistryEntry } from "libskynetnode/dist/registryread.js";
|
|
||||||
import {
|
|
||||||
bufToB64,
|
|
||||||
decryptFileSmall,
|
|
||||||
deriveChildSeed,
|
|
||||||
deriveRegistryEntryID,
|
|
||||||
encryptFileSmall,
|
|
||||||
entryIDToSkylink,
|
|
||||||
namespaceInode,
|
|
||||||
skylinkToResolverEntryData,
|
|
||||||
taggedRegistryEntryKeys,
|
|
||||||
} from "libskynet";
|
|
||||||
import { upload } from "libskynetnode";
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { SkynetClient } from "@skynetlabs/skynet-nodejs";
|
import { SkynetClient } from "@skynetlabs/skynet-nodejs";
|
||||||
|
import { dynImport } from "./util.js";
|
||||||
|
|
||||||
const ERR_EXISTS = "exists";
|
const ERR_EXISTS = "exists";
|
||||||
const ERR_NOT_EXISTS = "DNE";
|
const ERR_NOT_EXISTS = "DNE";
|
||||||
|
@ -63,12 +40,58 @@ interface IndependentFileSmallViewer {
|
||||||
readData: ReadDataFn;
|
readData: ReadDataFn;
|
||||||
}
|
}
|
||||||
|
|
||||||
function overwriteRegistryEntry(
|
let addContextToErr: typeof import("libskynet").addContextToErr,
|
||||||
|
blake2b: typeof import("libskynet").blake2b,
|
||||||
|
bufToHex: typeof import("libskynet").bufToHex,
|
||||||
|
ed25519Sign: typeof import("libskynet").ed25519Sign,
|
||||||
|
encodePrefixedBytes: typeof import("libskynet").encodePrefixedBytes,
|
||||||
|
encodeU64: typeof import("libskynet").encodeU64,
|
||||||
|
progressiveFetch: typeof import("libskynet").progressiveFetch,
|
||||||
|
defaultPortalList: typeof import("libskynet").defaultPortalList,
|
||||||
|
readRegistryEntry: typeof import("libskynetnode/dist/registryread.js").readRegistryEntry,
|
||||||
|
upload: typeof import("libskynetnode").upload,
|
||||||
|
skylinkToResolverEntryData: typeof import("libskynet").skylinkToResolverEntryData,
|
||||||
|
encryptFileSmall: typeof import("libskynet").encryptFileSmall,
|
||||||
|
deriveChildSeed: typeof import("libskynet").deriveChildSeed,
|
||||||
|
bufToB64: typeof import("libskynet").bufToB64,
|
||||||
|
decryptFileSmall: typeof import("libskynet").decryptFileSmall,
|
||||||
|
entryIDToSkylink: typeof import("libskynet").entryIDToSkylink,
|
||||||
|
deriveRegistryEntryID: typeof import("libskynet").deriveRegistryEntryID,
|
||||||
|
taggedRegistryEntryKeys: typeof import("libskynet").taggedRegistryEntryKeys,
|
||||||
|
namespaceInode: typeof import("libskynet").namespaceInode;
|
||||||
|
|
||||||
|
async function loadLibs() {
|
||||||
|
const libskynet = await dynImport("libskynet");
|
||||||
|
addContextToErr = libskynet.addContextToErr;
|
||||||
|
bufToHex = libskynet.bufToHex;
|
||||||
|
ed25519Sign = libskynet.ed25519Sign;
|
||||||
|
encodePrefixedBytes = libskynet.encodePrefixedBytes;
|
||||||
|
encodeU64 = libskynet.encodeU64;
|
||||||
|
defaultPortalList = libskynet.defaultPortalList;
|
||||||
|
skylinkToResolverEntryData = libskynet.skylinkToResolverEntryData;
|
||||||
|
encryptFileSmall = libskynet.encryptFileSmall;
|
||||||
|
deriveChildSeed = libskynet.deriveChildSeed;
|
||||||
|
bufToB64 = libskynet.bufToB64;
|
||||||
|
decryptFileSmall = libskynet.decryptFileSmall;
|
||||||
|
entryIDToSkylink = libskynet.entryIDToSkylink;
|
||||||
|
deriveRegistryEntryID = libskynet.deriveRegistryEntryID;
|
||||||
|
taggedRegistryEntryKeys = libskynet.taggedRegistryEntryKeys;
|
||||||
|
namespaceInode = libskynet.namespaceInode;
|
||||||
|
|
||||||
|
progressiveFetch = (await dynImport("libskynetnode/dist/progressivefetch.js"))
|
||||||
|
.progressiveFetch;
|
||||||
|
readRegistryEntry = (await dynImport("libskynetnode/dist/registryread.js"))
|
||||||
|
.readRegistryEntry;
|
||||||
|
upload = (await dynImport("libskynetnode")).upload;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function overwriteRegistryEntry(
|
||||||
keypair: any,
|
keypair: any,
|
||||||
datakey: Uint8Array,
|
datakey: Uint8Array,
|
||||||
data: Uint8Array,
|
data: Uint8Array,
|
||||||
revision: bigint
|
revision: bigint
|
||||||
): Promise<null> {
|
): Promise<null> {
|
||||||
|
await loadLibs();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (data.length > 86) {
|
if (data.length > 86) {
|
||||||
reject("provided data is too large to fit in a registry entry");
|
reject("provided data is too large to fit in a registry entry");
|
||||||
|
@ -119,7 +142,7 @@ function overwriteRegistryEntry(
|
||||||
fetchOpts,
|
fetchOpts,
|
||||||
defaultPortalList,
|
defaultPortalList,
|
||||||
verifyRegistryWrite
|
verifyRegistryWrite
|
||||||
).then((result) => {
|
).then((result: progressiveFetchResult) => {
|
||||||
if (result.success === true) {
|
if (result.success === true) {
|
||||||
resolve(null);
|
resolve(null);
|
||||||
return;
|
return;
|
||||||
|
@ -128,7 +151,8 @@ function overwriteRegistryEntry(
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function verifyRegistryWrite(response: Response): Promise<Err> {
|
async function verifyRegistryWrite(response: Response): Promise<Err> {
|
||||||
|
await loadLibs();
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
if (!("status" in response)) {
|
if (!("status" in response)) {
|
||||||
resolve("response did not contain a status");
|
resolve("response did not contain a status");
|
||||||
|
@ -142,11 +166,12 @@ function verifyRegistryWrite(response: Response): Promise<Err> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function createIndependentFileSmall(
|
async function createIndependentFileSmall(
|
||||||
seed: Uint8Array,
|
seed: Uint8Array,
|
||||||
userInode: string,
|
userInode: string,
|
||||||
fileData: Uint8Array
|
fileData: Uint8Array
|
||||||
): Promise<[IndependentFileSmall, Err]> {
|
): Promise<[IndependentFileSmall, Err]> {
|
||||||
|
await loadLibs();
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
let [inode, errNI] = namespaceInode("IndependentFileSmall", userInode);
|
let [inode, errNI] = namespaceInode("IndependentFileSmall", userInode);
|
||||||
if (errNI !== null) {
|
if (errNI !== null) {
|
||||||
|
@ -276,10 +301,11 @@ function createIndependentFileSmall(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function openIndependentFileSmall(
|
async function openIndependentFileSmall(
|
||||||
seed: Uint8Array,
|
seed: Uint8Array,
|
||||||
userInode: string
|
userInode: string
|
||||||
): Promise<[IndependentFileSmall, Err]> {
|
): Promise<[IndependentFileSmall, Err]> {
|
||||||
|
await loadLibs();
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
let [inode, errNI] = namespaceInode("IndependentFileSmall", userInode);
|
let [inode, errNI] = namespaceInode("IndependentFileSmall", userInode);
|
||||||
if (errNI !== null) {
|
if (errNI !== null) {
|
||||||
|
@ -378,10 +404,11 @@ function openIndependentFileSmall(
|
||||||
resolve([ifile, null]);
|
resolve([ifile, null]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function overwriteIndependentFileSmall(
|
async function overwriteIndependentFileSmall(
|
||||||
file: IndependentFileSmall,
|
file: IndependentFileSmall,
|
||||||
newData: Uint8Array
|
newData: Uint8Array
|
||||||
): Promise<Err> {
|
): Promise<Err> {
|
||||||
|
await loadLibs();
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
// Create a new metadata for the file based on the current file
|
// Create a new metadata for the file based on the current file
|
||||||
// metadata. Need to update the largest historic size.
|
// metadata. Need to update the largest historic size.
|
||||||
|
|
|
@ -22,10 +22,10 @@ import {
|
||||||
openIndependentFileSmall,
|
openIndependentFileSmall,
|
||||||
overwriteIndependentFileSmall,
|
overwriteIndependentFileSmall,
|
||||||
} from "./file.js";
|
} from "./file.js";
|
||||||
import { seedPhraseToSeed } from "libskynet";
|
|
||||||
import log from "loglevel";
|
import log from "loglevel";
|
||||||
import { AddressInfo } from "net";
|
import { AddressInfo } from "net";
|
||||||
import { sprintf } from "sprintf-js";
|
import { sprintf } from "sprintf-js";
|
||||||
|
import { dynImport } from "./util.js";
|
||||||
|
|
||||||
let sslCtx: tls.SecureContext = tls.createSecureContext();
|
let sslCtx: tls.SecureContext = tls.createSecureContext();
|
||||||
const sslParams: tls.SecureContextOptions = { cert: "", key: "" };
|
const sslParams: tls.SecureContextOptions = { cert: "", key: "" };
|
||||||
|
@ -35,6 +35,8 @@ let acmeClient: acme.Client;
|
||||||
let app: Express;
|
let app: Express;
|
||||||
let router = express.Router();
|
let router = express.Router();
|
||||||
|
|
||||||
|
let seedPhraseToSeed: typeof import("libskynet").seedPhraseToSeed;
|
||||||
|
|
||||||
const FILE_CERT_NAME = "/lumeweb/relay/ssl.crt";
|
const FILE_CERT_NAME = "/lumeweb/relay/ssl.crt";
|
||||||
const FILE_KEY_NAME = "/lumeweb/relay/ssl.key";
|
const FILE_KEY_NAME = "/lumeweb/relay/ssl.key";
|
||||||
const FILE_ACCOUNT_KEY_NAME = "/lumeweb/relay/account.key";
|
const FILE_ACCOUNT_KEY_NAME = "/lumeweb/relay/account.key";
|
||||||
|
@ -42,6 +44,8 @@ const FILE_ACCOUNT_KEY_NAME = "/lumeweb/relay/account.key";
|
||||||
type SslData = { crt: IndependentFileSmall; key: IndependentFileSmall };
|
type SslData = { crt: IndependentFileSmall; key: IndependentFileSmall };
|
||||||
|
|
||||||
export async function start() {
|
export async function start() {
|
||||||
|
seedPhraseToSeed = (await dynImport("libskynet")).seedPhraseToSeed;
|
||||||
|
|
||||||
const relayPort = config.uint("port");
|
const relayPort = config.uint("port");
|
||||||
app = express();
|
app = express();
|
||||||
app.use(function (req, res, next) {
|
app.use(function (req, res, next) {
|
||||||
|
|
|
@ -32,3 +32,7 @@ export function isIp(ip: string) {
|
||||||
ip
|
ip
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function dynImport(module: string) {
|
||||||
|
return Function(`return import("${module}")`)() as Promise<any>;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue