mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 13:36:48 +00:00
1ad2e92142
The bulk of what SonarQube still had open on the reliability side, read site by
site rather than swept. Two clusters are worth calling out because they are
defects, not lint.
**Four regexes could be made to hang the process.** The worst is
`/.+@.+\..+/` in the forgot-password path: three unbounded `.+` that all
overlap, unanchored, on an endpoint that needs no login and whose schema is a
bare z.string() under a 100kb body cap. A thousand '@' characters had not
finished after 100 seconds — one request pins the event loop, and Node has one.
EMAIL_REGEX in auth.helpers is the same shape on register and profile update
(310ms for 8kb). The two tag strippers over an uploaded KML description and over
Commons metadata took 0.52s on a 16k run of '<'. Each rewrite was proven
equivalent by brute force against the original — millions of strings for the
coordinate patterns, 671,876 for the tag stripper — because a subtly different
regex on a parsing path is worse than the finding.
`<[^>]+>` could not be fixed in the pattern: `[^>]` matches `<` too, so the only
unambiguous form changes what happens to malformed markup. It is now a scan in
common/stripHtmlTags.ts, quirks included — `<>` is left alone, `<a<b>` is one
tag — with the equivalence pinned by a test that compares against the old regex
over every string up to length six.
**And roughly 500 places could only be reached with a mouse.** Clickable divs
became real buttons where the markup allowed (52 of them), took
role + tabIndex + Enter/Space where they had to stay divs, and modal backdrops
took role="presentation" — the honest role, since a backdrop is a mouse shortcut
for a Cancel control that is already in the tab order, and making it a button
plants a nameless full-screen tab stop around its own contents.
Alongside those: `.sort()` calls that now say the order they always had, `{n &&
<X/>}` guards that rendered a literal "0" for a zero-byte file and for a place at
latitude 0, an optimistic budget write that stored a boolean where the contract
and both transports use 0/1, and the Number statics.
sonar-project.properties documents the six that were deliberately left: two wiki
patterns with no overlapping quantifier pair whose "fix" would stop parsing
`[see [1]](url)`, four tar header fields bounded at 12-155 bytes by the format,
two djb2 hashes where the 32-bit wrap is the algorithm, two `| 0` that are also
coercing undefined to 0 on a path where the API sends no user_id, and the MCP
session maps Sonar reads as empty because they are filled from another module.
64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import type { Reservation } from '../../types'
|
|
|
|
/**
|
|
* Real-path geometry for transit journeys on the map (#1065). MOTIS delivers
|
|
* each leg's shape as a Google-encoded polyline; we store it on
|
|
* metadata.transit.legs[].geometry and decode it here so the map can draw the
|
|
* actual rail/bus alignment instead of a straight line.
|
|
*/
|
|
|
|
export interface TransitMapSegment {
|
|
coords: [number, number][]
|
|
color: string | null
|
|
walk: boolean
|
|
}
|
|
|
|
/** Google polyline decoding with a configurable precision (MOTIS uses 6). */
|
|
export function decodePolyline(encoded: string, precision = 6): [number, number][] {
|
|
const factor = Math.pow(10, precision)
|
|
const coords: [number, number][] = []
|
|
let index = 0
|
|
let lat = 0
|
|
let lng = 0
|
|
while (index < encoded.length) {
|
|
for (const which of [0, 1] as const) {
|
|
let result = 0
|
|
let shift = 0
|
|
let byte = 0x20
|
|
while (byte >= 0x20) {
|
|
if (index >= encoded.length) return coords
|
|
byte = encoded.codePointAt(index++) - 63
|
|
result |= (byte & 0x1f) << shift
|
|
shift += 5
|
|
}
|
|
const delta = result & 1 ? ~(result >> 1) : result >> 1
|
|
if (which === 0) lat += delta
|
|
else lng += delta
|
|
}
|
|
coords.push([lat / factor, lng / factor])
|
|
}
|
|
return coords
|
|
}
|
|
|
|
/**
|
|
* The decoded per-leg segments of a transit reservation, or [] when it has no
|
|
* stored geometry (pre-geometry entries fall back to the straight line).
|
|
*/
|
|
export function getTransitMapSegments(res: Reservation): TransitMapSegment[] {
|
|
if (res.type !== 'transit') return []
|
|
let meta: any = res.metadata
|
|
if (typeof meta === 'string') {
|
|
try { meta = JSON.parse(meta) } catch { return [] }
|
|
}
|
|
const legs = meta?.transit?.legs
|
|
if (!Array.isArray(legs)) return []
|
|
const out: TransitMapSegment[] = []
|
|
for (const leg of legs) {
|
|
if (!leg?.geometry || typeof leg.geometry !== 'string') continue
|
|
const coords = decodePolyline(leg.geometry, typeof leg.geometry_precision === 'number' ? leg.geometry_precision : 6)
|
|
if (coords.length < 2) continue
|
|
out.push({ coords, color: leg.line_color || null, walk: leg.mode === 'WALK' })
|
|
}
|
|
return out
|
|
}
|