Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dep update + commonjs -> module #39

Merged
merged 5 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
},
"parser": "@babel/eslint-parser",
"parserOptions": {
"sourceType": "module",
"requireConfigFile": false
},
"ignorePatterns": ["**/test/__fixtures__/**/*"],
"rules": {
"prettier/prettier": "error",
"no-console": 2
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1.3-labs

ARG IPFS_BUILD_IMAGE_VERSION=1.17-alpine3.15
ARG IPFS_BUILD_IMAGE_VERSION=1.19.2-alpine3.15
ARG NODE_RUNTIME_IMAGE_VERSION=16-alpine
FROM golang:$IPFS_BUILD_IMAGE_VERSION AS ipfs_build

Expand All @@ -10,7 +10,7 @@ RUN apk add --no-cache git make bash gcc musl-dev

WORKDIR /target

ARG IPFS_TAG="v0.13.1"
ARG IPFS_TAG="v0.16.0"

RUN <<EOF
set -ex
Expand Down
19 changes: 13 additions & 6 deletions app/env.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const path = require('path')
const envalid = require('envalid')
const dotenv = require('dotenv')
import path from 'path'
import envalid from 'envalid'
import dotenv from 'dotenv'
import { fileURLToPath } from 'url'
import os from 'os'
import fsWithSync from 'fs'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

/* istanbul ignore else */
if (process.env.NODE_ENV === 'test') {
dotenv.config({ path: 'test/test.env' })
const ipfsDir = fsWithSync.mkdtempSync(path.join(os.tmpdir(), 'dscp-ipfs-'))
process.env.IPFS_PATH = ipfsDir
mattdean-digicatapult marked this conversation as resolved.
Show resolved Hide resolved
}

const validateArgs = envalid.makeValidator((input) => {
Expand All @@ -28,7 +36,7 @@ const vars = envalid.cleanEnv(
default: 'ipfs',
devDefault: path.resolve(__dirname, '..', `node_modules`, '.bin', 'ipfs'),
}),
IPFS_ARGS: validateArgs({ default: '["daemon", "--migrate"]' }),
IPFS_ARGS: validateArgs({ default: ['daemon', '--migrate'] }),
jonmattgray marked this conversation as resolved.
Show resolved Hide resolved
IPFS_LOG_LEVEL: envalid.str({ default: 'info', devDefault: 'debug' }),
HEALTHCHECK_POLL_PERIOD_MS: envalid.num({ default: 30 * 1000, devDefault: 1000 }),
HEALTHCHECK_TIMEOUT_MS: envalid.num({ default: 2 * 1000, devDefault: 1000 }),
Expand All @@ -37,5 +45,4 @@ const vars = envalid.cleanEnv(
strict: true,
}
)

module.exports = vars
export default vars
2 changes: 1 addition & 1 deletion app/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

const { startServer } = require('./server')
import { startServer } from './server.js'

startServer()
22 changes: 9 additions & 13 deletions app/ipfs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { spawn } = require('child_process')
const EventEmitter = require('events')
import { spawn } from 'child_process'
import EventEmitter from 'events'

const { ConnectionError } = require('./utils/Errors')
const { IPFS_PATH, IPFS_EXECUTABLE, IPFS_ARGS, IPFS_LOG_LEVEL } = require('./env')
const logger = require('./logger')
import { ConnectionError } from './utils/Errors.js'
import env from './env.js'
import logger from './logger.js'
import { createSwarmKeyFile, removeSwarmKeyFile } from './swarmKey.js'

const { createSwarmKeyFile, removeSwarmKeyFile } = require('./swarmKey')
const { IPFS_PATH, IPFS_EXECUTABLE, IPFS_ARGS, IPFS_LOG_LEVEL } = env

async function setupIpfs() {
export async function setupIpfs() {
let ipfs = null
let ipfsLogger = logger.child({ module: 'ipfs' }, { level: IPFS_LOG_LEVEL })

Expand Down Expand Up @@ -84,7 +85,7 @@ async function setupIpfs() {
return that
}

async function ipfsHealthCheack(api = {}, name = 'ipfs') {
export async function ipfsHealthCheck(api = {}, name = 'ipfs') {
try {
if (!api.ipfs || !api.ipfs.pid) throw new ConnectionError({ name })
const { spawnfile, pid, killed } = api.ipfs
Expand All @@ -102,8 +103,3 @@ async function ipfsHealthCheack(api = {}, name = 'ipfs') {
return { name, status: 'error', error }
}
}

module.exports = {
setupIpfs,
ipfsHealthCheack,
}
12 changes: 5 additions & 7 deletions app/keyWatcher/api.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { buildApi } = require('@digicatapult/dscp-node')
import { buildApi } from '@digicatapult/dscp-node'

const { NODE_HOST, NODE_PORT } = require('../env')
import env from '../env.js'

const createNodeApi = async () => {
export const createNodeApi = async () => {
const { api, keyring } = buildApi({
options: {
apiHost: NODE_HOST,
apiPort: NODE_PORT,
apiHost: env.NODE_HOST,
apiPort: env.NODE_PORT,
},
})

Expand All @@ -20,5 +20,3 @@ const createNodeApi = async () => {
setupEventProcessor: (eventProcessor) => api.query.system.events(eventProcessor),
}
}

module.exports = { createNodeApi }
57 changes: 28 additions & 29 deletions app/keyWatcher/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
const { createNodeApi } = require('./api')
const { setupKeyWatcher } = require('./keyWatcher')
const { ConnectionError } = require('../utils/Errors')
import { setupKeyWatcher as setup } from './keyWatcher.js'
import { ConnectionError } from '../utils/Errors.js'

module.exports = {
jonmattgray marked this conversation as resolved.
Show resolved Hide resolved
createNodeApi,
setupKeyWatcher: async ({ api, onUpdate }) => {
await setupKeyWatcher(api)({ onUpdate })
},
nodeHealthCheck: async (api, name = 'substrate') => {
try {
if (!(await api.isConnected)) throw new ConnectionError({ name })
const [chain, runtime] = await Promise.all([api.runtimeChain, api.runtimeVersion])
export { createNodeApi } from './api.js'

return {
name,
status: 'up',
details: {
chain,
runtime: {
name: runtime.specName,
versions: {
spec: runtime.specVersion.toNumber(),
impl: runtime.implVersion.toNumber(),
authoring: runtime.authoringVersion.toNumber(),
transaction: runtime.transactionVersion.toNumber(),
},
export const setupKeyWatcher = async ({ api, onUpdate }) => {
await setup(api)({ onUpdate })
}

export const nodeHealthCheck = async (api, name = 'substrate') => {
try {
if (!(await api.isConnected)) throw new ConnectionError({ name })
const [chain, runtime] = await Promise.all([api.runtimeChain, api.runtimeVersion])

return {
name,
status: 'up',
details: {
chain,
runtime: {
name: runtime.specName,
versions: {
spec: runtime.specVersion.toNumber(),
impl: runtime.implVersion.toNumber(),
authoring: runtime.authoringVersion.toNumber(),
transaction: runtime.transactionVersion.toNumber(),
},
},
}
} catch (error) {
return { name, status: 'error', error }
},
}
},
} catch (error) {
return { name, status: 'error', error }
}
}
8 changes: 2 additions & 6 deletions app/keyWatcher/keyWatcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const logger = require('../logger')
import logger from '../logger.js'

const setupKeyWatcher =
export const setupKeyWatcher =
(api) =>
async ({ onUpdate }) => {
const onKeyUpdate = (keyU8Array) => {
Expand All @@ -23,7 +23,3 @@ const setupKeyWatcher =
const key = await api.getCurrentKey()
onKeyUpdate(key)
}

module.exports = {
setupKeyWatcher,
}
6 changes: 3 additions & 3 deletions app/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const pino = require('pino')
const env = require('./env')
import pino from 'pino'
import env from './env.js'

const logger = pino(
{
Expand All @@ -11,4 +11,4 @@ const logger = pino(

logger.debug('Environment variables: %j', { ...env })

module.exports = logger
export default logger
27 changes: 12 additions & 15 deletions app/server.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const express = require('express')
const pinoHttp = require('pino-http')
import express from 'express'
import pinoHttp from 'pino-http'

const { PORT } = require('./env')
const logger = require('./logger')
const { createNodeApi, setupKeyWatcher, nodeHealthCheck } = require('./keyWatcher')
const { ipfsHealthCheack } = require('./ipfs')
const { setupIpfs } = require('./ipfs')
const ServiceWatcher = require('./utils/ServiceWatcher')
import env from './env.js'
import logger from './logger.js'
import { createNodeApi, setupKeyWatcher, nodeHealthCheck } from './keyWatcher/index.js'
import { ipfsHealthCheck, setupIpfs } from './ipfs.js'
import ServiceWatcher from './utils/ServiceWatcher.js'

async function createHttpServer() {
export async function createHttpServer() {
const app = express()
const requestLogger = pinoHttp({ logger })
const ipfs = await setupIpfs()
const api = await createNodeApi()

const sw = new ServiceWatcher({
substrate: { healthCheck: () => nodeHealthCheck(api._api) },
ipfs: { healthCheck: () => ipfsHealthCheack(ipfs) },
ipfs: { healthCheck: () => ipfsHealthCheck(ipfs) },
})
await sw.start()

Expand Down Expand Up @@ -54,13 +53,13 @@ async function createHttpServer() {
}

/* istanbul ignore next */
async function startServer() {
export async function startServer() {
try {
const { app, ipfs, sw } = await createHttpServer()
const server = await new Promise((resolve, reject) => {
const server = app.listen(PORT, (err) => {
const server = app.listen(env.PORT, (err) => {
if (err) return reject(err)
logger.info(`Listening on port ${PORT} `)
logger.info(`Listening on port ${env.PORT} `)
resolve(server)
})

Expand Down Expand Up @@ -90,5 +89,3 @@ async function startServer() {
process.exit(1)
}
}

module.exports = { startServer, createHttpServer }
20 changes: 8 additions & 12 deletions app/swarmKey.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const path = require('path')
const fs = require('fs/promises')
const { IPFS_PATH } = require('./env')
const logger = require('./logger')
import path from 'path'
import fs from 'fs/promises'

const swarmKeyPath = path.join(IPFS_PATH, 'swarm.key')
import env from './env.js'
import logger from './logger.js'

async function removeSwarmKeyFile() {
const swarmKeyPath = path.join(env.IPFS_PATH, 'swarm.key')

export async function removeSwarmKeyFile() {
try {
await fs.rm(swarmKeyPath)
} catch (err) {
Expand All @@ -15,7 +16,7 @@ async function removeSwarmKeyFile() {
}
}

async function createSwarmKeyFile({ swarmKey }) {
export async function createSwarmKeyFile({ swarmKey }) {
logger.info('Writing IPFS swarm key to: %s', swarmKeyPath)
logger.trace('Writing IPFS swarm key with: %s', `0x${swarmKey.toString('hex')}`)

Expand All @@ -27,8 +28,3 @@ async function createSwarmKeyFile({ swarmKey }) {
{ mode: 0o400 }
)
}

module.exports = {
removeSwarmKeyFile,
createSwarmKeyFile,
}
9 changes: 2 additions & 7 deletions app/utils/Errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class TimeoutError extends Error {
export class TimeoutError extends Error {
constructor(service) {
super()
this.type = this.constructor.name
Expand All @@ -7,15 +7,10 @@ class TimeoutError extends Error {
}
}

class ConnectionError extends Error {
export class ConnectionError extends Error {
constructor(service) {
super()
this.service = service.name
this.message = 'Connection is not established, will retry during next polling cycle'
}
}

module.exports = {
TimeoutError,
ConnectionError,
}
10 changes: 5 additions & 5 deletions app/utils/ServiceWatcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { TimeoutError } = require('./Errors')
const { HEALTHCHECK_POLL_PERIOD_MS, HEALTHCHECK_TIMEOUT_MS } = require('../env')
import { TimeoutError } from './Errors.js'
import env from '../env.js'

class ServiceWatcher {
#pollPeriod
Expand All @@ -8,8 +8,8 @@ class ServiceWatcher {
// TODO add a method for updating this.services
constructor(apis) {
this.report = {}
this.#pollPeriod = HEALTHCHECK_POLL_PERIOD_MS
this.#timeout = HEALTHCHECK_TIMEOUT_MS
this.#pollPeriod = env.HEALTHCHECK_POLL_PERIOD_MS
this.#timeout = env.HEALTHCHECK_TIMEOUT_MS
this.services = this.#init(apis)
}

Expand Down Expand Up @@ -84,4 +84,4 @@ class ServiceWatcher {
}
}

module.exports = ServiceWatcher
export default ServiceWatcher
6 changes: 3 additions & 3 deletions helm/dscp-ipfs/Chart.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
- name: dscp-node
repository: https://digicatapult.github.io/dscp-node/
version: 4.2.1
digest: sha256:8b359b958a03de828d26a882411ed569157b898842fad62c683bdbef2b2ede4b
generated: "2022-07-18T17:08:40.939659+01:00"
version: 4.3.1
digest: sha256:3e3a6afc97ea2181208c13a3f50028d3c55e481d32fe35bcac301cf5221f4e11
generated: "2022-10-19T16:37:44.200925+01:00"
6 changes: 3 additions & 3 deletions helm/dscp-ipfs/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apiVersion: v2
name: dscp-ipfs
appVersion: '2.6.1'
appVersion: '2.6.2'
description: A Helm chart for dscp-ipfs
version: '2.6.1'
version: '2.6.2'
type: application
dependencies:
- name: dscp-node
version: '4.2.1'
version: '4.3.1'
repository: https://digicatapult.github.io/dscp-node/
condition: dscpNode.enabled
alias: dscpNode
Expand Down
Loading