Skip to content

Commit

Permalink
CommonJS (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
vderunov authored Mar 13, 2024
1 parent 3d50cdc commit 978e321
Show file tree
Hide file tree
Showing 30 changed files with 247 additions and 158 deletions.
5 changes: 3 additions & 2 deletions src/const.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const SYNTHETIX_IPNS = 'k2k4r8k0exi9e16awbpoy3y7gzfg37no0yceoz1b4dmt8mcohkf9kz4a';
module.exports.SYNTHETIX_IPNS = 'k2k4r8k0exi9e16awbpoy3y7gzfg37no0yceoz1b4dmt8mcohkf9kz4a';

export const SYNTHETIX_NODE_APP_CONFIG = 'k2k4r8jhn5r54sf708bnlxun971q0pteab82iw98544sj1e0hi3hp9q6';
module.exports.SYNTHETIX_NODE_APP_CONFIG =
'k2k4r8jhn5r54sf708bnlxun971q0pteab82iw98544sj1e0hi3hp9q6';
28 changes: 14 additions & 14 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import fs from 'node:fs';
import http from 'node:http';
import path from 'node:path';
import { BrowserWindow, Menu, Tray, app, ipcMain, session, shell } from 'electron';
import logger from 'electron-log';
import { SYNTHETIX_NODE_APP_CONFIG } from './const';
import { DAPPS, resolveDapp } from './main/dapps';
import {
const fs = require('node:fs');
const http = require('node:http');
const path = require('node:path');
const { BrowserWindow, Menu, Tray, app, ipcMain, session, shell } = require('electron');
const logger = require('electron-log');
const { SYNTHETIX_NODE_APP_CONFIG } = require('./const');
const { DAPPS, resolveDapp } = require('./main/dapps');
const {
configureFollower,
downloadFollower,
follower,
followerDaemon,
followerId,
followerIsInstalled,
followerTeardown,
} from './main/follower';
import {
} = require('./main/follower');
const {
configureIpfs,
downloadIpfs,
ipfsDaemon,
Expand All @@ -23,10 +23,10 @@ import {
ipfsTeardown,
rpcRequest,
waitForIpfs,
} from './main/ipfs';
import { fetchPeers } from './main/peers';
import * as settings from './main/settings';
import { ROOT } from './main/settings';
} = require('./main/ipfs');
const { fetchPeers } = require('./main/peers');
const settings = require('./main/settings');
const { ROOT } = require('./main/settings');

logger.transports.file.level = 'info';

Expand Down
30 changes: 20 additions & 10 deletions src/main/dapps.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import logger from 'electron-log';
import { EnsResolver, getDefaultProvider } from 'ethers';
import { rpcRequest } from './ipfs';
const logger = require('electron-log');
const { EnsResolver, getDefaultProvider } = require('ethers');
const { rpcRequest } = require('./ipfs');

export const DAPPS = [];
const DAPPS = [];

const provider = getDefaultProvider();

export async function resolveEns(dapp) {
async function resolveEns(dapp) {
if (dapp.ipns) {
return {
codec: 'ipns-ns',
Expand All @@ -32,7 +32,7 @@ export async function resolveEns(dapp) {
return { codec, hash };
}

export async function resolveQm(ipns) {
async function resolveQm(ipns) {
try {
const ipfsPath = await rpcRequest('name/resolve', [ipns]);
const qm = ipfsPath.Path.slice(6); // remove /ipfs/
Expand All @@ -43,7 +43,7 @@ export async function resolveQm(ipns) {
}
}

export async function convertCid(qm) {
async function convertCid(qm) {
try {
const res = await rpcRequest('cid/base32', [qm]);
return res.CidStr;
Expand All @@ -53,7 +53,7 @@ export async function convertCid(qm) {
}
}

export async function isPinned(qm) {
async function isPinned(qm) {
try {
const result = await rpcRequest('pin/ls', [qm], { type: 'recursive' });
return result.Keys[qm];
Expand All @@ -64,7 +64,7 @@ export async function isPinned(qm) {

const activePinningRequests = new Set();

export async function resolveDapp(dapp) {
async function resolveDapp(dapp) {
try {
const { codec, hash } = await resolveEns(dapp);
logger.log(dapp.id, 'resolved', codec, hash);
Expand Down Expand Up @@ -110,7 +110,7 @@ export async function resolveDapp(dapp) {
}
}

export async function cleanupOldDapps() {
async function cleanupOldDapps() {
const hashes = DAPPS.map((dapp) => dapp.qm);
logger.log('Current DAPPs hashes', hashes);
if (hashes.length < 1 || hashes.some((hash) => !hash)) {
Expand All @@ -137,3 +137,13 @@ export async function cleanupOldDapps() {
// do nothing
}
}

module.exports = {
DAPPS,
resolveEns,
resolveQm,
convertCid,
isPinned,
resolveDapp,
cleanupOldDapps,
};
59 changes: 36 additions & 23 deletions src/main/follower.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { exec, spawn } from 'node:child_process';
import { promises as fs, createReadStream, createWriteStream, readFileSync, rmSync } from 'node:fs';
import https from 'node:https';
import os from 'node:os';
import path from 'node:path';
import { pipeline } from 'node:stream/promises';
import zlib from 'node:zlib';
import AdmZip from 'adm-zip';
import logger from 'electron-log';
import tar from 'tar';
import { SYNTHETIX_IPNS } from '../const';
import { ROOT } from './settings';
import { getPlatformDetails } from './util';
const { exec, spawn } = require('node:child_process');
const { promises: fs, createReadStream, createWriteStream, rmSync } = require('node:fs');
const https = require('node:https');
const os = require('node:os');
const path = require('node:path');
const { pipeline } = require('node:stream/promises');
const zlib = require('node:zlib');
const AdmZip = require('adm-zip');
const logger = require('electron-log');
const tar = require('tar');
const { SYNTHETIX_IPNS } = require('../const');
const { ROOT } = require('./settings');
const { getPlatformDetails } = require('./util');

// Change if we ever want to store all follower info in a custom folder
const HOME = os.homedir();
const IPFS_FOLLOW_PATH = path.join(HOME, '.ipfs-cluster-follow');

export function followerTeardown() {
function followerTeardown() {
rmSync(path.join(ROOT, 'ipfs-cluster-follow.pid'), { force: true });
}

export async function followerIsInstalled() {
async function followerIsInstalled() {
try {
await fs.access(
path.join(
Expand All @@ -37,7 +37,7 @@ export async function followerIsInstalled() {
}
}

export async function followerDaemon() {
async function followerDaemon() {
const isInstalled = await followerIsInstalled();
if (!isInstalled) {
return;
Expand Down Expand Up @@ -82,7 +82,7 @@ export async function followerDaemon() {
}
}

export async function follower(arg) {
async function follower(arg) {
return new Promise((resolve, reject) => {
exec(
`${path.join(ROOT, 'ipfs-cluster-follow/ipfs-cluster-follow')} ${arg}`,
Expand All @@ -99,7 +99,7 @@ export async function follower(arg) {
});
}

export async function getLatestVersion() {
async function getLatestVersion() {
return new Promise((resolve, reject) => {
https
.get('https://dist.ipfs.tech/ipfs-cluster-follow/versions', (res) => {
Expand All @@ -117,7 +117,7 @@ export async function getLatestVersion() {
});
}

export async function getInstalledVersion() {
async function getInstalledVersion() {
try {
const version = await follower('--version');
const [, , installedVersion] = version.split(' ');
Expand All @@ -127,7 +127,7 @@ export async function getInstalledVersion() {
}
}

export async function downloadFollower(_e, { log = logger.log } = {}) {
async function downloadFollower(_e, { log = logger.log } = {}) {
const { osPlatform, fileExt, targetArch } = getPlatformDetails();

log('Checking for existing ipfs-cluster-follow installation...');
Expand Down Expand Up @@ -179,7 +179,7 @@ export async function downloadFollower(_e, { log = logger.log } = {}) {
return installedVersionCheck;
}

export async function isConfigured() {
async function isConfigured() {
try {
const service = await fs.readFile(
path.join(IPFS_FOLLOW_PATH, 'synthetix/service.json'),
Expand All @@ -191,7 +191,7 @@ export async function isConfigured() {
}
}

export async function followerId() {
async function followerId() {
try {
const identity = JSON.parse(
await fs.readFile(path.join(IPFS_FOLLOW_PATH, 'synthetix/identity.json'), 'utf8')
Expand All @@ -202,7 +202,7 @@ export async function followerId() {
}
}

export async function configureFollower({ log = logger.log } = {}) {
async function configureFollower({ log = logger.log } = {}) {
if (await isConfigured()) {
return;
}
Expand All @@ -214,3 +214,16 @@ export async function configureFollower({ log = logger.log } = {}) {
// whatever
}
}

module.exports = {
followerTeardown,
followerIsInstalled,
followerDaemon,
follower,
getLatestVersion,
getInstalledVersion,
downloadFollower,
isConfigured,
followerId,
configureFollower,
};
Loading

0 comments on commit 978e321

Please sign in to comment.