Remove sync parseGpx, use parseGpxAsync everywhere
parseGpx (sync) needs browser DOMParser which doesn't exist on the server — it silently failed in every server-side caller. Removed the export and migrated all callers to parseGpxAsync. Updated tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
9279ffffbb
commit
9da5c1fb53
5 changed files with 29 additions and 21 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import { redirect, data } from "react-router";
|
import { redirect, data } from "react-router";
|
||||||
import type { Route } from "./+types/new";
|
import type { Route } from "./+types/new";
|
||||||
import { createSession, initializeSessionWithWaypoints } from "~/lib/sessions";
|
import { createSession, initializeSessionWithWaypoints } from "~/lib/sessions";
|
||||||
import { parseGpx } from "@trails-cool/gpx";
|
import { parseGpxAsync } from "@trails-cool/gpx";
|
||||||
import { checkRateLimit } from "~/lib/rate-limit";
|
import { checkRateLimit } from "~/lib/rate-limit";
|
||||||
|
|
||||||
export async function loader({ request }: Route.LoaderArgs) {
|
export async function loader({ request }: Route.LoaderArgs) {
|
||||||
|
|
@ -35,7 +35,7 @@ export async function loader({ request }: Route.LoaderArgs) {
|
||||||
if (gpxEncoded) {
|
if (gpxEncoded) {
|
||||||
try {
|
try {
|
||||||
const gpx = decodeURIComponent(gpxEncoded);
|
const gpx = decodeURIComponent(gpxEncoded);
|
||||||
const gpxData = parseGpx(gpx);
|
const gpxData = await parseGpxAsync(gpx);
|
||||||
initializeSessionWithWaypoints(session.id, gpxData.waypoints);
|
initializeSessionWithWaypoints(session.id, gpxData.waypoints);
|
||||||
} catch {
|
} catch {
|
||||||
// Continue with empty session if GPX is invalid
|
// Continue with empty session if GPX is invalid
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { describe, it, expect } from "vitest";
|
import { describe, it, expect } from "vitest";
|
||||||
import { generateGpx } from "./generate.ts";
|
import { generateGpx } from "./generate.ts";
|
||||||
import { parseGpx } from "./parse.ts";
|
import { parseGpxAsync } from "./parse.ts";
|
||||||
|
|
||||||
describe("generateGpx", () => {
|
describe("generateGpx", () => {
|
||||||
it("generates valid GPX with name", () => {
|
it("generates valid GPX with name", () => {
|
||||||
|
|
@ -36,13 +36,13 @@ describe("generateGpx", () => {
|
||||||
expect(gpx).toContain("Route <A> & B");
|
expect(gpx).toContain("Route <A> & B");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("produces round-trippable GPX", () => {
|
it("produces round-trippable GPX", async () => {
|
||||||
const original = generateGpx({
|
const original = generateGpx({
|
||||||
name: "Round Trip",
|
name: "Round Trip",
|
||||||
waypoints: [{ lat: 52.52, lon: 13.405, name: "Start" }],
|
waypoints: [{ lat: 52.52, lon: 13.405, name: "Start" }],
|
||||||
tracks: [[{ lat: 52.52, lon: 13.405, ele: 34 }, { lat: 48.137, lon: 11.576, ele: 519 }]],
|
tracks: [[{ lat: 52.52, lon: 13.405, ele: 34 }, { lat: 48.137, lon: 11.576, ele: 519 }]],
|
||||||
});
|
});
|
||||||
const parsed = parseGpx(original);
|
const parsed = await parseGpxAsync(original);
|
||||||
expect(parsed.name).toBe("Round Trip");
|
expect(parsed.name).toBe("Round Trip");
|
||||||
expect(parsed.waypoints).toHaveLength(1);
|
expect(parsed.waypoints).toHaveLength(1);
|
||||||
expect(parsed.waypoints[0]!.name).toBe("Start");
|
expect(parsed.waypoints[0]!.name).toBe("Start");
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
export { parseGpx, parseGpxAsync } from "./parse.ts";
|
export { parseGpxAsync } from "./parse.ts";
|
||||||
export { generateGpx } from "./generate.ts";
|
export { generateGpx } from "./generate.ts";
|
||||||
export type { GpxData, TrackPoint, ElevationProfile } from "./types.ts";
|
export type { GpxData, TrackPoint, ElevationProfile } from "./types.ts";
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { describe, it, expect } from "vitest";
|
import { describe, it, expect } from "vitest";
|
||||||
import { parseGpx } from "./parse.ts";
|
import { parseGpxAsync } from "./parse.ts";
|
||||||
|
|
||||||
const sampleGpx = `<?xml version="1.0" encoding="UTF-8"?>
|
const sampleGpx = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<gpx version="1.1" creator="test" xmlns="http://www.topografix.com/GPX/1/1">
|
<gpx version="1.1" creator="test" xmlns="http://www.topografix.com/GPX/1/1">
|
||||||
|
|
@ -15,42 +15,50 @@ const sampleGpx = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
</trk>
|
</trk>
|
||||||
</gpx>`;
|
</gpx>`;
|
||||||
|
|
||||||
describe("parseGpx", () => {
|
describe("parseGpxAsync", () => {
|
||||||
it("parses route name", () => {
|
it("parses route name", async () => {
|
||||||
const result = parseGpx(sampleGpx);
|
const result = await parseGpxAsync(sampleGpx);
|
||||||
expect(result.name).toBe("Test Route");
|
expect(result.name).toBe("Test Route");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("parses waypoints with lat, lon, and name", () => {
|
it("parses waypoints with lat, lon, and name", async () => {
|
||||||
const result = parseGpx(sampleGpx);
|
const result = await parseGpxAsync(sampleGpx);
|
||||||
expect(result.waypoints).toHaveLength(2);
|
expect(result.waypoints).toHaveLength(2);
|
||||||
expect(result.waypoints[0]).toEqual({ lat: 52.52, lon: 13.405, name: "Berlin" });
|
expect(result.waypoints[0]).toEqual({ lat: 52.52, lon: 13.405, name: "Berlin" });
|
||||||
expect(result.waypoints[1]).toEqual({ lat: 48.137, lon: 11.576, name: "Munich" });
|
expect(result.waypoints[1]).toEqual({ lat: 48.137, lon: 11.576, name: "Munich" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("parses track points with elevation", () => {
|
it("parses track points with elevation", async () => {
|
||||||
const result = parseGpx(sampleGpx);
|
const result = await parseGpxAsync(sampleGpx);
|
||||||
expect(result.tracks).toHaveLength(1);
|
expect(result.tracks).toHaveLength(1);
|
||||||
expect(result.tracks[0]).toHaveLength(3);
|
expect(result.tracks[0]).toHaveLength(3);
|
||||||
expect(result.tracks[0]![0]).toEqual({ lat: 52.52, lon: 13.405, ele: 34, time: undefined });
|
expect(result.tracks[0]![0]).toEqual({ lat: 52.52, lon: 13.405, ele: 34, time: undefined });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("computes elevation gain and loss", () => {
|
it("computes elevation gain and loss", async () => {
|
||||||
const result = parseGpx(sampleGpx);
|
const result = await parseGpxAsync(sampleGpx);
|
||||||
expect(result.elevation.gain).toBeGreaterThan(0);
|
expect(result.elevation.gain).toBeGreaterThan(0);
|
||||||
expect(result.elevation.loss).toBe(0); // monotonically increasing elevation
|
expect(result.elevation.loss).toBe(0); // monotonically increasing elevation
|
||||||
expect(result.elevation.gain).toBe(485); // 113-34 + 519-113
|
expect(result.elevation.gain).toBe(485); // 113-34 + 519-113
|
||||||
});
|
});
|
||||||
|
|
||||||
it("builds elevation profile", () => {
|
it("builds elevation profile", async () => {
|
||||||
const result = parseGpx(sampleGpx);
|
const result = await parseGpxAsync(sampleGpx);
|
||||||
expect(result.elevation.profile).toHaveLength(3);
|
expect(result.elevation.profile).toHaveLength(3);
|
||||||
expect(result.elevation.profile[0]!.distance).toBe(0);
|
expect(result.elevation.profile[0]!.distance).toBe(0);
|
||||||
expect(result.elevation.profile[0]!.elevation).toBe(34);
|
expect(result.elevation.profile[0]!.elevation).toBe(34);
|
||||||
expect(result.elevation.profile[2]!.distance).toBeGreaterThan(0);
|
expect(result.elevation.profile[2]!.distance).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws on invalid XML", () => {
|
it("computes total distance independently of elevation", async () => {
|
||||||
expect(() => parseGpx("not xml at all <<<<")).toThrow("Invalid GPX XML");
|
const result = await parseGpxAsync(sampleGpx);
|
||||||
|
expect(result.distance).toBeGreaterThan(0);
|
||||||
|
// Berlin to Munich is ~500km, our 3-point track should be in that range
|
||||||
|
expect(result.distance).toBeGreaterThan(400_000);
|
||||||
|
expect(result.distance).toBeLessThan(600_000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws on invalid XML", async () => {
|
||||||
|
await expect(parseGpxAsync("not xml at all <<<<")).rejects.toThrow();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ async function getDOMParser(): Promise<typeof DOMParser> {
|
||||||
return _LinkedDOMParser;
|
return _LinkedDOMParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseGpx(xml: string): GpxData {
|
function parseGpx(xml: string): GpxData {
|
||||||
// Synchronous path for browser
|
// Synchronous path for browser
|
||||||
if (typeof DOMParser !== "undefined") {
|
if (typeof DOMParser !== "undefined") {
|
||||||
return parseGpxWithParser(new DOMParser(), xml);
|
return parseGpxWithParser(new DOMParser(), xml);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue