antongolub/npm-registry-firewall

View on GitHub
src/main/js/app.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { createServer } from './http/server.js'
import { createRouter } from './http/router.js'
import { runInCtx, mixCtx } from './als.js'
import {
  ctx,
  healthcheck,
  errorBoundary,
  notFound,
  trace,
  proxy,
  timeout,
  firewall,
  advisory,
  metrics,
} from './mwares/index.js'
import { loadConfig } from './config.js'
import { getCache, stopCache } from './cache.js'

export { getCache, stopCache }

export const _createApp = (cfg, {
  cache,
  logger
} = {}) => {
  const config = loadConfig(cfg)
  getCache(cache || config.cache) // init cache

  mixCtx({
    logger,
    config,
  })

  const firewalls = createRoutes(config)

  const router = createRouter([
    ctx({config, logger}),
    timeout,
    trace,
    ['GET', config.server.healthcheck, healthcheck],
    ['GET', config.server.metrics, metrics],
    ...firewalls,
    notFound,
    errorBoundary,
  ], config.server.base)

  const server = createServer({...config.server, router})

  return {
    server,
    config,
    start() { return this.server.start() },
    stop() {
      stopCache()
      return this.server.stop()
    },
  }
}

export const createRoutes = (config) =>
  config.firewall.map(({base, entrypoint, registry, token, rules}) => {
    const f = firewall({registry, rules, entrypoint, token})
    const a = advisory({registry, rules, entrypoint, token})

    return createRouter([
      // tarball
      [
        'GET',
        [
          /^\/((?:(@[a-z0-9\-._]+)(?:%2[fF]|\/))?[a-z0-9\-._]+)\/-\/[a-z0-9\-._]+-(\d+\.\d+\.\d+(?:-[+\-.a-z0-9_]+)?)\.tgz$/,
          ['name', 'org', 'version']
        ],
        f
      ],
      // packument
      [
        '*',
        [
          /^\/((?:(@[a-z0-9\-._]+)(?:%2[fF]|\/))?[a-z0-9\-._]+)\/?$/,
          ['name', 'org']
        ],
        f
      ],
      // advisory
      [
        'GET',
        [
          /^\/_check\/((?:(@[a-z0-9\-._]+)(?:%2[fF]|\/))?[a-z0-9\-._]+)\/(\d+\.\d+\.\d+(?:-[+\-.a-z0-9_]+)?)\/?$/,
          ['name', 'org', 'version']
        ],
        a
      ],
      [
        'POST',
        [
          /^\/_check\/bulk\/?$/,
          []
        ],
        a
      ],
      proxy(registry),
      errorBoundary,
    ], base)
  })

export const createApp = (...args) => {
  let app
  runInCtx({}, () => app = _createApp(...args))
  return app
}