diff --git a/.eslintrc.json b/.eslintrc.json index 1d44c69..8f435cc 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,12 +1,11 @@ { "extends": "eslint:recommended", "env": { - "commonjs": true, - "es6": true, + "es2022": true, "node": true }, "parserOptions": { - "ecmaVersion": 2018 + "sourceType": "module" }, "ignorePatterns": [ "node_modules" diff --git a/api-docs.js b/api-docs.js index 4743771..3cdf6c6 100644 --- a/api-docs.js +++ b/api-docs.js @@ -1,7 +1,5 @@ -'use strict' - -const generateApiDocs = require('hafas-rest-api/tools/generate-docs') -const {api} = require('./api') +import generateApiDocs from 'hafas-rest-api/tools/generate-docs.js' +import {api} from './api.js' const HEAD = `\ # \`v6.db.transport.rest\` API documentation diff --git a/api.js b/api.js index 19a51fe..ba954b4 100644 --- a/api.js +++ b/api.js @@ -1,19 +1,25 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'node:module' +const require = createRequire(import.meta.url) + +import {dirname, join as pathJoin} from 'node:path' +import {fileURLToPath} from 'node:url' +import createHafas from 'db-hafas' +import createApi from 'hafas-rest-api' +import createHealthCheck from 'hafas-client-health-check' +import Redis from 'ioredis' +import withCache from 'cached-hafas-client' +import redisStore from 'cached-hafas-client/stores/redis.js' +import serveStatic from 'serve-static' +import {parseBoolean} from 'hafas-rest-api/lib/parse.js' +import {loyaltyCardParser} from './lib/loyalty-cards.js' +import {route as stations} from './routes/stations.js' +import {route as station} from './routes/station.js' -const createHafas = require('db-hafas') -const createApi = require('hafas-rest-api') -const createHealthCheck = require('hafas-client-health-check') -const Redis = require('ioredis') -const withCache = require('cached-hafas-client') -const redisStore = require('cached-hafas-client/stores/redis') -const {join: pathJoin} = require('path') -const serveStatic = require('serve-static') -const {parseBoolean} = require('hafas-rest-api/lib/parse') const pkg = require('./package.json') -const {loyaltyCardParser} = require('./lib/loyalty-cards') -const stations = require('./routes/stations') -const station = require('./routes/station') +const __dirname = dirname(fileURLToPath(import.meta.url)) const docsRoot = pathJoin(__dirname, 'docs') const berlinHbf = '8011160' @@ -87,7 +93,7 @@ const api = createApi(hafas, config, (api) => { })) }) -module.exports = { +export { hafas, config, api, diff --git a/index.js b/index.js index f5bfd59..75cad8d 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,4 @@ -'use strict' - -const {api, config} = require('./api') +import {api, config} from './api.js' api.listen(config.port, (err) => { const {logger} = api.locals diff --git a/lib/db-stations.js b/lib/db-stations.js index 012f272..9773a6c 100644 --- a/lib/db-stations.js +++ b/lib/db-stations.js @@ -1,10 +1,13 @@ -'use strict' +import {createRequire} from 'node:module' +const require = createRequire(import.meta.url) -const {statSync} = require('fs') -const {full: readRawStations} = require('db-stations') +import {statSync} from 'node:fs' +import _dbStations from 'db-stations' +const {full: readRawStations} = _dbStations // We don't have access to the publish date+time of the npm package, // so we use the ctime of db-stations/full.ndjson as an approximation. +// Also require() doesn't make much sense in ES Modules. // todo: this is brittle, find a better way, e.g. a build script const timeModified = statSync(require.resolve('db-stations/full.ndjson')).ctime @@ -37,4 +40,6 @@ pStations.catch((err) => { process.exit(1) // todo: is this appropriate? }) -module.exports = pStations +export { + pStations, +} diff --git a/lib/loyalty-cards.js b/lib/loyalty-cards.js index 552566f..5532a8d 100644 --- a/lib/loyalty-cards.js +++ b/lib/loyalty-cards.js @@ -1,4 +1,5 @@ -const {data: cards} = require('hafas-client/p/db/loyalty-cards') +import _loyaltyCards from 'hafas-client/p/db/loyalty-cards.js' +const {data: cards} = _loyaltyCards const typesByName = new Map([ // https://github.com/public-transport/hafas-client/blob/68ecd7c5e976dd2f51c5c64a81600e7e181a8996/p/db/loyalty-cards.js#L6-L11 @@ -29,8 +30,8 @@ const loyaltyCardParser = { parse: parseLoyaltyCard, } -module.exports = { - loyaltyCards: cards, +export { + cards as loyaltyCards, parseLoyaltyCard, loyaltyCardParser, } diff --git a/package.json b/package.json index 1e1441e..f0916a0 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "name": "db-rest", "description": "A clean REST API wrapping around the Deutsche Bahn API.", "version": "5.0.2", + "type": "module", "main": "index.js", "author": "Jannis R ", "homepage": "https://github.com/derhuerst/db-rest/tree/6", diff --git a/routes/station.js b/routes/station.js index c2b24b3..24f5cb9 100644 --- a/routes/station.js +++ b/routes/station.js @@ -1,6 +1,4 @@ -'use strict' - -const pStations = require('../lib/db-stations') +import {pStations} from '../lib/db-stations.js' const err404 = (msg) => { const err = new Error(msg) @@ -58,4 +56,6 @@ Returns a stop/station from [\`db-stations@3\`](https://github.com/derhuerst/db- }, } -module.exports = stationRoute +export { + stationRoute as route, +} diff --git a/routes/stations.js b/routes/stations.js index c64a464..3a9eda7 100644 --- a/routes/stations.js +++ b/routes/stations.js @@ -1,11 +1,10 @@ -'use strict' - -const computeEtag = require('etag') -const serveBuffer = require('serve-buffer') -const autocomplete = require('db-stations-autocomplete') -const parse = require('cli-native').to -const createFilter = require('db-stations/create-filter') -let pAllStations = require('../lib/db-stations') +import computeEtag from 'etag' +import serveBuffer from 'serve-buffer' +import autocomplete from 'db-stations-autocomplete' +import _cliNative from 'cli-native' +const {to: parse} = _cliNative +import createFilter from 'db-stations/create-filter.js' +import {pStations} from '../lib/db-stations.js' const JSON_MIME = 'application/json' const NDJSON_MIME = 'application/x-ndjson' @@ -24,7 +23,7 @@ const toNdjsonBuf = (data) => { return Buffer.concat(chunks, bytes) } -pAllStations = pAllStations.then(({data, timeModified}) => { +const pAllStations = pStations.then(({data, timeModified}) => { const asJson = Buffer.from(JSON.stringify(data), 'utf8') const asNdjson = toNdjsonBuf(data) return { @@ -202,4 +201,6 @@ stationsRoute.queryParameters = { }, } -module.exports = stationsRoute +export { + stationsRoute as route, +} diff --git a/test/index.js b/test/index.js index 4ef1e52..b6da33c 100644 --- a/test/index.js +++ b/test/index.js @@ -1,10 +1,9 @@ -'use strict' - -const tape = require('tape') -const {parse: ndjsonParser} = require('ndjson') -const {loyaltyCards} = require('../lib/loyalty-cards') -const {fetchWithTestApi} = require('./util') -const pAllStations = require('../lib/db-stations') +import tape from 'tape' +import _ndjson from 'ndjson' +const {parse: ndjsonParser} = _ndjson +import {loyaltyCards} from '../lib/loyalty-cards.js' +import {fetchWithTestApi} from './util.js' +import {pStations as pAllStations} from '../lib/db-stations.js' const NO_JOURNEYS = { // todo? diff --git a/test/util.js b/test/util.js index 3498ee2..8916160 100644 --- a/test/util.js +++ b/test/util.js @@ -1,12 +1,9 @@ -'use strict' - -const createApi = require('hafas-rest-api') -const getPort = require('get-port') -const {createServer} = require('http') -const {promisify} = require('util') -const axios = require('axios') -const pkg = require('../package.json') -const {config, hafas: unmockedHafas} = require('../api') +import createApi from 'hafas-rest-api' +import getPort from 'get-port' +import {createServer} from 'node:http' +import {promisify} from 'node:util' +import axios from 'axios' +import {config, hafas as unmockedHafas} from '../api.js' // adapted from https://github.com/public-transport/hafas-rest-api/blob/60335eacd8332d7f448da875a7498dd97934e360/test/util.js#L40-L77 const createTestApi = async (mocks, cfg) => { @@ -50,7 +47,7 @@ const fetchWithTestApi = async (mocks, cfg, path, opt = {}) => { return res } -module.exports = { +export { createTestApi, fetchWithTestApi, }