Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Upgrade deps and switch from commonjs -> module (#59)
Browse files Browse the repository at this point in the history
* Upgrade deps and swithch from commonjs -> module

Upgrades deps to latest. node-fetch no longer supports commonjs giving us some impetous to switch to module syntax. A few notes from the upgrade:

* to import the package version from package.json we have to use new import assert syntax. Whilst nodejs supports this eslint requires a babel parser. Hence the inclusion of @babel/eslint-parser and @babel/plugin-syntax-import-assertions
* node-fetch no longer supports form-data. Switched to formdata-node
* base-x now returns a Uint8Array so we need to do some additional wrapping in a Buffer
* mocharc.js moved to mocharc.cjs as mocha does not support import syntax for this

* Bump @digicatapult/dscp-node

* Add depcheck
  • Loading branch information
mattdean-digicatapult authored Oct 18, 2022
1 parent 90b177f commit c69d306
Show file tree
Hide file tree
Showing 35 changed files with 2,616 additions and 1,377 deletions.
1 change: 1 addition & 0 deletions .depcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignores: ["@babel/plugin-syntax-import-assertions"]
9 changes: 7 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
"es6": true,
"node": true
},
"parser": "@babel/eslint-parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
"requireConfigFile": false,
"babelOptions": {
"plugins": [
"@babel/plugin-syntax-import-assertions"
]
}
},
"rules": {
"prettier/prettier": "error"
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,29 @@ jobs:
env:
AUTH_TYPE: ${{ matrix.auth }}

dependency-check:
name: Run dependency check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
node-version: 16.x
- name: Use npm v8
run: npm install -g npm@8
- name: Cache Node.js modules
uses: actions/cache@v1
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: Install Packages
run: npm ci
- name: Dependency Check
run: npm run depcheck

check-version:
name: 'Check version'
runs-on: ubuntu-latest
Expand Down Expand Up @@ -190,6 +213,7 @@ jobs:
- helm-lint
- helm-test
- lint
- dependency-check
- tests
- check-version
runs-on: ubuntu-latest
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ jobs:
env:
AUTH_TYPE: ${{ matrix.auth }}

dependency-check:
name: Run dependency check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
node-version: 16.x
- name: Use npm v8
run: npm install -g npm@8
- name: Cache Node.js modules
uses: actions/cache@v1
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: Install Packages
run: npm ci
- name: Dependency Check
run: npm run depcheck


check-version:
name: 'Check version'
Expand Down
6 changes: 4 additions & 2 deletions app/api-v3/api-doc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { PORT, API_VERSION, API_MAJOR_VERSION, EXTERNAL_ORIGIN, EXTERNAL_PATH_PREFIX } = require('../env')
import env from '../env.js'

const { PORT, API_VERSION, API_MAJOR_VERSION, EXTERNAL_ORIGIN, EXTERNAL_PATH_PREFIX } = env

let url = EXTERNAL_ORIGIN || `http://localhost:${PORT}`
if (EXTERNAL_PATH_PREFIX) {
Expand Down Expand Up @@ -218,4 +220,4 @@ const apiDoc = {
paths: {},
}

module.exports = apiDoc
export default apiDoc
10 changes: 5 additions & 5 deletions app/api-v3/routes/item/{id}.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { validateTokenId } = require('../../../util/appUtil')
const logger = require('../../../logger')
const { getReadableMetadataKeys } = require('../../../util/appUtil')
const { getDefaultSecurity } = require('../../../util/auth')
import { validateTokenId } from '../../../util/appUtil.js'
import logger from '../../../logger.js'
import { getReadableMetadataKeys } from '../../../util/appUtil.js'
import { getDefaultSecurity } from '../../../util/auth.js'

module.exports = function (apiService) {
export default function (apiService) {
const doc = {
GET: async function (req, res) {
const id = validateTokenId(req.params.id)
Expand Down
6 changes: 3 additions & 3 deletions app/api-v3/routes/item/{id}/metadata/{metadataKey}.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { getMetadataResponse } = require('../../../../../util/appUtil')
const { getDefaultSecurity } = require('../../../../../util/auth')
import { getMetadataResponse } from '../../../../../util/appUtil.js'
import { getDefaultSecurity } from '../../../../../util/auth.js'

module.exports = function () {
export default function () {
const doc = {
GET: async function (req, res) {
return getMetadataResponse(req.params.id, req.params.metadataKey, res)
Expand Down
6 changes: 3 additions & 3 deletions app/api-v3/routes/last-token.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const logger = require('../../logger')
const { getDefaultSecurity } = require('../../util/auth')
import logger from '../../logger.js'
import { getDefaultSecurity } from '../../util/auth.js'

module.exports = function (apiService) {
export default function (apiService) {
const doc = {
GET: async function (req, res) {
try {
Expand Down
8 changes: 4 additions & 4 deletions app/api-v3/routes/members.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const logger = require('../../logger')
const { membershipReducer } = require('../../util/appUtil')
const { getDefaultSecurity } = require('../../util/auth')
import logger from '../../logger.js'
import { membershipReducer } from '../../util/appUtil.js'
import { getDefaultSecurity } from '../../util/auth.js'

module.exports = function (apiService) {
export default function (apiService) {
const doc = {
GET: async function (req, res) {
try {
Expand Down
12 changes: 7 additions & 5 deletions app/api-v3/routes/run-process.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const logger = require('../../logger')
const { validateInputIds, processRoles, processMetadata, validateProcess } = require('../../util/appUtil')
const { getDefaultSecurity } = require('../../util/auth')
const { PROCESS_IDENTIFIER_LENGTH } = require('../../env')
import logger from '../../logger.js'
import { validateInputIds, processRoles, processMetadata, validateProcess } from '../../util/appUtil.js'
import { getDefaultSecurity } from '../../util/auth.js'
import env from '../../env.js'

module.exports = function (apiService) {
const { PROCESS_IDENTIFIER_LENGTH } = env

export default function (apiService) {
const doc = {
POST: async function (req, res) {
let request = null
Expand Down
20 changes: 10 additions & 10 deletions app/api-v3/services/apiService.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const {
import {
getItem,
runProcess: runProcessUtil,
getMembers: getMembersUtil,
getLastTokenId: getLastTokenIdUtil,
} = require('../../util/appUtil')
runProcess as runProcessUtil,
getMembers as getMembersUtil,
getLastTokenId as getLastTokenIdUtil,
} from '../../util/appUtil.js'

async function findItemById(tokenId) {
export async function findItemById(tokenId) {
return getItem(tokenId)
}

async function findLastTokenId() {
export async function findLastTokenId() {
return getLastTokenIdUtil()
}

async function findMembers() {
export async function findMembers() {
return getMembersUtil()
}

async function runProcess(process, inputs, outputs) {
export async function runProcess(process, inputs, outputs) {
return runProcessUtil(process, inputs, outputs)
}

module.exports = {
export default {
findItemById,
findLastTokenId,
findMembers,
Expand Down
8 changes: 4 additions & 4 deletions app/env.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const envalid = require('envalid')
const dotenv = require('dotenv')
import envalid from 'envalid'
import dotenv from 'dotenv'

const { version } = require('../package.json')
import version from './version.js'

if (process.env.NODE_ENV === 'test') {
dotenv.config({ path: 'test/test.env' })
Expand Down Expand Up @@ -45,7 +45,7 @@ const vars = envalid.cleanEnv(process.env, {
EXTERNAL_PATH_PREFIX: envalid.str({ default: '' }),
})

module.exports = {
export default {
...vars,
AUTH_TYPE,
}
2 changes: 1 addition & 1 deletion app/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { startServer } = require('./server')
import { startServer } from './server.js'

startServer()
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 @@ -21,4 +21,4 @@ const logger = pino(

logger.debug('Env: %j', env)

module.exports = logger
export default logger
47 changes: 26 additions & 21 deletions app/server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
const express = require('express')
const cors = require('cors')
const pinoHttp = require('pino-http')
const { initialize } = require('express-openapi')
const swaggerUi = require('swagger-ui-express')
const multer = require('multer')
const path = require('path')
const bodyParser = require('body-parser')
const compression = require('compression')
const { PORT, API_VERSION, API_MAJOR_VERSION, AUTH_TYPE, EXTERNAL_PATH_PREFIX } = require('./env')
const logger = require('./logger')
const apiDoc = require('./api-v3/api-doc')
const apiService = require('./api-v3/services/apiService')
const { startStatusHandlers } = require('./serviceStatus')
const { serviceState } = require('./util/statusPoll')
const { verifyJwks } = require('./util/auth')

async function createHttpServer() {
import express from 'express'
import cors from 'cors'
import pinoHttp from 'pino-http'
import { initialize } from 'express-openapi'
import swaggerUi from 'swagger-ui-express'
import multer from 'multer'
import path from 'path'
import bodyParser from 'body-parser'
import compression from 'compression'

import env from './env.js'
import logger from './logger.js'
import apiDoc from './api-v3/api-doc.js'
import apiService from './api-v3/services/apiService.js'
import { startStatusHandlers } from './serviceStatus/index.js'
import { serviceState } from './util/statusPoll.js'
import { verifyJwks } from './util/auth.js'

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

const { PORT, API_VERSION, API_MAJOR_VERSION, AUTH_TYPE, EXTERNAL_PATH_PREFIX } = env

export async function createHttpServer() {
const requestLogger = pinoHttp({ logger })
const app = express()
const statusHandler = await startStatusHandlers()
Expand Down Expand Up @@ -124,7 +131,7 @@ async function createHttpServer() {
return { app, statusHandler }
}

async function startServer() {
export async function startServer() {
try {
const { app, statusHandler } = await createHttpServer()

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

module.exports = { startServer, createHttpServer }
10 changes: 6 additions & 4 deletions app/serviceStatus/apiStatus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { startStatusHandler, serviceState } = require('../util/statusPoll')
const { substrateApi } = require('../util/substrateApi')
const { SUBSTRATE_STATUS_POLL_PERIOD_MS, SUBSTRATE_STATUS_TIMEOUT_MS } = require('../env')
import { startStatusHandler, serviceState } from '../util/statusPoll.js'
import { substrateApi } from '../util/substrateApi.js'
import env from '../env.js'

const { SUBSTRATE_STATUS_POLL_PERIOD_MS, SUBSTRATE_STATUS_TIMEOUT_MS } = env

const getStatus = async () => {
await substrateApi.isReadyOrError.catch(() => {})
Expand Down Expand Up @@ -37,4 +39,4 @@ const startApiStatus = () =>
serviceTimeoutMs: SUBSTRATE_STATUS_TIMEOUT_MS,
})

module.exports = startApiStatus
export default startApiStatus
12 changes: 4 additions & 8 deletions app/serviceStatus/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
const startApiStatus = require('./apiStatus')
const startIpfsStatus = require('./ipfsStatus')
const { buildCombinedHandler } = require('../util/statusPoll')
import startApiStatus from './apiStatus.js'
import startIpfsStatus from './ipfsStatus.js'
import { buildCombinedHandler } from '../util/statusPoll.js'

const startStatusHandlers = async () => {
export const startStatusHandlers = async () => {
const handlers = new Map()
const [apiStatus, ipfsStatus] = await Promise.all([startApiStatus(), startIpfsStatus()])
handlers.set('api', apiStatus)
handlers.set('ipfs', ipfsStatus)

return buildCombinedHandler(handlers)
}

module.exports = {
startStatusHandlers,
}
11 changes: 7 additions & 4 deletions app/serviceStatus/ipfsStatus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { startStatusHandler, serviceState } = require('../util/statusPoll')
const fetch = require('node-fetch')
const { IPFS_STATUS_POLL_PERIOD_MS, IPFS_STATUS_TIMEOUT_MS, IPFS_HOST, IPFS_PORT } = require('../env')
import fetch from 'node-fetch'

import { startStatusHandler, serviceState } from '../util/statusPoll.js'
import env from '../env.js'

const { IPFS_STATUS_POLL_PERIOD_MS, IPFS_STATUS_TIMEOUT_MS, IPFS_HOST, IPFS_PORT } = env

const versionURL = `http://${IPFS_HOST}:${IPFS_PORT}/api/v0/version`
const peersURL = `http://${IPFS_HOST}:${IPFS_PORT}/api/v0/swarm/peers`
Expand Down Expand Up @@ -43,4 +46,4 @@ const startIpfsStatus = () =>
serviceTimeoutMs: IPFS_STATUS_TIMEOUT_MS,
})

module.exports = startIpfsStatus
export default startIpfsStatus
Loading

0 comments on commit c69d306

Please sign in to comment.