Repository navigation
Expand file tree
/
Copy pathserver.mjs
More file actions
252 lines (227 loc) · 10.2 KB
/
Copy pathserver.mjs
File metadata and controls
252 lines (227 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import express from 'express'
import cityCatalog from 'cities.json' with { type: 'json' }
import countryCodeLookup from 'country-code-lookup'
import { createHash } from 'node:crypto'
import { access, copyFile, mkdir, readFile, rename, writeFile } from 'node:fs/promises'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { feature } from 'topojson-client'
import atlas from 'world-atlas/countries-110m.json' with { type: 'json' }
const root = dirname(fileURLToPath(import.meta.url))
const dataFile = resolve(process.env.ATLAS_DATA_FILE ?? resolve(root, 'data/trips.json'))
const seedFile = resolve(root, 'data/trips.example.json')
const port = Number(process.env.PORT ?? 3001)
const app = express()
const atlasCountries = feature(atlas, atlas.objects.countries).features
const countryNamesByCode = new Map(atlasCountries.flatMap((country) => {
const match = countryCodeLookup.byIso(Number(country.id))
const name = country.properties?.name
return match && name ? [[match.iso2, name]] : []
}))
const cityIndex = new Map()
const usStatesByPlace = new Map()
const normalizeCitySearch = (value) => value.normalize('NFD').replace(/[\u0300-\u036f]/g, '').trim().toLowerCase()
for (const city of cityCatalog) {
const name = city.name?.trim()
const country = countryNamesByCode.get(city.country) ?? countryCodeLookup.byIso(city.country)?.country
if (!name || !country || !Number.isFinite(Number(city.lat)) || !Number.isFinite(Number(city.lng))) continue
const state = city.country === 'US' ? city.admin1 : undefined
const entry = { name, country, countryCode: city.country, ...(state ? { state } : {}), lat: Number(city.lat), lng: Number(city.lng) }
if (state) usStatesByPlace.set(`${name.toLowerCase()}|${entry.lat}|${entry.lng}`, state)
const searchTerms = new Set([name, country, city.country, state ?? '']
.flatMap((field) => normalizeCitySearch(field).split(/\s+/)))
for (const term of searchTerms) {
const prefix = term.slice(0, 2)
if (prefix.length < 2) continue
const entries = cityIndex.get(prefix) ?? new Set()
entries.add(entry)
cityIndex.set(prefix, entries)
}
}
app.use(express.json({ limit: '256kb' }))
await mkdir(dirname(dataFile), { recursive: true })
try {
await access(dataFile)
} catch (error) {
if (error?.code !== 'ENOENT') throw error
if (dataFile !== seedFile) await copyFile(seedFile, dataFile)
}
function normalizeStore(saved) {
const normalizePlace = (place) => {
const normalized = { ...place }
delete normalized.year
if (!normalized.state && typeof normalized.city === 'string') {
const key = `${normalized.city.toLowerCase()}|${Number(normalized.lat)}|${Number(normalized.lng)}`
const state = usStatesByPlace.get(key)
if (state) normalized.state = state
}
return normalized
}
if (Array.isArray(saved?.homes)) {
return {
name: typeof saved.name === 'string' ? saved.name : '',
homes: saved.homes.map((home) => ({
...normalizePlace(home),
visits: (Array.isArray(home.visits) ? home.visits : []).map(normalizePlace),
})),
starredCountries: Array.isArray(saved.starredCountries) ? saved.starredCountries : [],
}
}
const trips = Array.isArray(saved) ? saved : Array.isArray(saved?.trips) ? saved.trips : []
const homes = trips.filter((trip) => trip.starred).map((trip) => {
const place = normalizePlace(trip)
delete place.starred
delete place.sources
return { ...place, visits: [] }
})
const homesById = new Map(homes.map((home) => [home.id, home]))
for (const trip of trips.filter((item) => !item.starred)) {
const place = normalizePlace(trip)
const sources = Array.isArray(place.sources) ? place.sources : []
delete place.starred
delete place.sources
const parents = [...new Set(sources)]
.filter((source) => homesById.has(source))
if (!parents.length) throw new Error(`Legacy place ${trip.city} has no home assignment.`)
for (const parent of parents) {
const home = homesById.get(parent)
home.visits.push({ ...place, id: parents.length > 1 ? `${trip.id}-${parent}` : trip.id })
}
}
return {
name: typeof saved.name === 'string' ? saved.name : '',
homes,
starredCountries: Array.isArray(saved.starredCountries) ? saved.starredCountries : [],
}
}
function revisionFor(contents) {
return `"${createHash('sha256').update(contents).digest('hex')}"`
}
async function readStore() {
const contents = await readFile(dataFile, 'utf8')
return { store: normalizeStore(JSON.parse(contents)), revision: revisionFor(contents) }
}
function validateStore(body) {
if (!body || !Array.isArray(body.homes) || !Array.isArray(body.starredCountries)) {
throw new Error('Expected homes and starredCountries arrays.')
}
if (body.name !== undefined && (typeof body.name !== 'string' || body.name.length > 80)) {
throw new Error('Name must be a string of 80 characters or fewer.')
}
const ids = new Set()
const validatePlace = (place) => {
const lat = place?.lat
const lng = place?.lng
const state = typeof place?.state === 'string' ? place.state.trim() : ''
if (!place || typeof place.id !== 'string' || !place.id || ids.has(place.id) || typeof place.city !== 'string' || !place.city.trim()
|| typeof place.country !== 'string' || !place.country.trim()
|| (place.state !== undefined && (typeof place.state !== 'string' || place.state.length > 80))
|| typeof lat !== 'number' || !Number.isFinite(lat) || lat < -90 || lat > 90
|| typeof lng !== 'number' || !Number.isFinite(lng) || lng < -180 || lng > 180) {
throw new Error('Each place needs a valid id, city, country, and coordinates.')
}
ids.add(place.id)
return {
id: place.id,
city: place.city.trim(),
country: place.country.trim(),
...(state ? { state } : {}),
lat,
lng,
}
}
const homes = body.homes.map((home) => {
if (!Array.isArray(home?.visits)) throw new Error('Each home must contain a visits array.')
return { ...validatePlace(home), visits: home.visits.map(validatePlace) }
})
const visitedCountries = new Set(homes.flatMap((home) => [home.country, ...home.visits.map((visit) => visit.country)]))
const starredCountries = [...new Set(body.starredCountries)]
.filter((country) => typeof country === 'string' && visitedCountries.has(country))
return { name: typeof body.name === 'string' ? body.name.trim() : '', homes, starredCountries }
}
let writeQueue = Promise.resolve()
function writeStore(store, expectedRevision) {
const write = writeQueue.then(async () => {
const currentContents = await readFile(dataFile, 'utf8')
const currentRevision = revisionFor(currentContents)
if (currentRevision !== expectedRevision) {
return { conflict: true, store: normalizeStore(JSON.parse(currentContents)), revision: currentRevision }
}
const contents = `${JSON.stringify(store, null, 2)}\n`
const temporaryFile = `${dataFile}.${process.pid}.tmp`
await writeFile(temporaryFile, contents)
await rename(temporaryFile, dataFile)
return { conflict: false, revision: revisionFor(contents) }
})
writeQueue = write.catch(() => {})
return write
}
app.get('/api/trips', async (_request, response) => {
try {
response.set('Cache-Control', 'no-store')
const { store, revision } = await readStore()
response.set('ETag', revision)
response.json(store)
} catch (error) {
console.error('Could not read atlas data:', error)
response.status(500).json({ error: 'Atlas data is unavailable.' })
}
})
app.get('/api/cities', (request, response) => {
const query = normalizeCitySearch(String(request.query.q ?? '')).replace(/[,.]/g, ' ').replace(/\s+/g, ' ').trim()
const queryTerms = query.split(' ').filter(Boolean)
if (query.length < 2) return response.json([])
const matches = [...(cityIndex.get(queryTerms[0].slice(0, 2)) ?? [])]
.filter((city) => {
const searchWords = [city.name, city.state, city.country, city.countryCode]
.filter(Boolean)
.flatMap((field) => normalizeCitySearch(field).split(/\s+/))
return queryTerms.every((term) => searchWords.some((word) => word.startsWith(term)))
})
.sort((left, right) => {
const leftName = normalizeCitySearch(left.name)
const rightName = normalizeCitySearch(right.name)
const leftNameMatches = queryTerms.filter((term) => leftName.split(/\s+/).some((word) => word.startsWith(term))).length
const rightNameMatches = queryTerms.filter((term) => rightName.split(/\s+/).some((word) => word.startsWith(term))).length
return Number(leftName !== query) - Number(rightName !== query)
|| rightNameMatches - leftNameMatches
|| Number(!left.state) - Number(!right.state)
|| left.name.localeCompare(right.name)
|| left.country.localeCompare(right.country)
|| (left.state ?? '').localeCompare(right.state ?? '')
})
.slice(0, 12)
response.set('Cache-Control', 'public, max-age=3600')
response.json(matches.map((city) => ({
name: city.name,
country: city.country,
...(city.state ? { state: city.state } : {}),
lat: city.lat,
lng: city.lng,
})))
})
app.put('/api/trips', async (request, response) => {
try {
const expectedRevision = request.get('If-Match')
if (!expectedRevision) return response.status(428).json({ error: 'A file revision is required to save atlas data.' })
const store = validateStore(request.body)
const result = await writeStore(store, expectedRevision)
response.set('ETag', result.revision)
if (result.conflict) return response.status(409).json({ error: 'Atlas data changed since it was loaded.', store: result.store })
return response.json(store)
} catch (error) {
const message = error instanceof Error ? error.message : 'Could not save atlas data.'
response.status(400).json({ error: message })
}
})
const dist = resolve(root, 'dist')
app.use(express.static(dist))
app.get('*path', (request, response, next) => {
if (request.path === '/api' || request.path.startsWith('/api/')) return next()
response.sendFile(resolve(dist, 'index.html'), (error) => {
if (error) next(error)
})
})
app.listen(port, '127.0.0.1', () => {
console.log(`Travel log API listening on http://127.0.0.1:${port}`)
})