db-rest/lib/db-stations.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-12-19 13:23:13 +01:00
import {createRequire} from 'node:module'
const require = createRequire(import.meta.url)
2022-12-19 13:23:13 +01:00
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.
2022-12-19 13:23:13 +01:00
// 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
const pStations = new Promise((resolve, reject) => {
let raw = readRawStations()
raw.once('error', reject)
let data = Object.create(null)
raw.on('data', (station) => {
data[station.id] = station
if (Array.isArray(station.ril100Identifiers)) {
for (const ril100 of station.ril100Identifiers) {
data[ril100.rilIdentifier] = station
}
}
if (Array.isArray(station.additionalIds)) {
for (const addId of station.additionalIds) {
data[addId] = station
}
}
})
raw.once('end', () => {
raw = null
resolve({data, timeModified})
})
})
pStations.catch((err) => {
console.error(err)
process.exit(1) // todo: is this appropriate?
})
2022-12-19 13:23:13 +01:00
export {
pStations,
}