@mailwoman/photon 10.0.0 → 10.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/{lib/cli.ts → cli/main.ts} +42 -56
- package/lib/app.ts +12 -34
- package/lib/engine.ts +21 -13
- package/lib/index.ts +6 -11
- package/lib/locality-postcode.ts +13 -17
- package/lib/projection.ts +61 -56
- package/lib/routes.ts +14 -26
- package/lib/schema.ts +31 -24
- package/out/app.d.ts +6 -28
- package/out/app.d.ts.map +1 -1
- package/out/app.js +5 -12
- package/out/app.js.map +1 -1
- package/out/cli/main.d.ts +12 -0
- package/out/cli/main.d.ts.map +1 -0
- package/out/{cli.js → cli/main.js} +42 -56
- package/out/cli/main.js.map +1 -0
- package/out/engine.d.ts +21 -13
- package/out/engine.d.ts.map +1 -1
- package/out/engine.js +2 -2
- package/out/engine.js.map +1 -1
- package/out/index.d.ts +6 -11
- package/out/index.d.ts.map +1 -1
- package/out/index.js +6 -11
- package/out/index.js.map +1 -1
- package/out/locality-postcode.d.ts +5 -8
- package/out/locality-postcode.d.ts.map +1 -1
- package/out/locality-postcode.js +11 -15
- package/out/locality-postcode.js.map +1 -1
- package/out/projection.d.ts +48 -38
- package/out/projection.d.ts.map +1 -1
- package/out/projection.js +38 -36
- package/out/projection.js.map +1 -1
- package/out/routes.d.ts +2 -2
- package/out/routes.d.ts.map +1 -1
- package/out/routes.js +13 -25
- package/out/routes.js.map +1 -1
- package/out/schema.d.ts +31 -24
- package/out/schema.d.ts.map +1 -1
- package/out/schema.js +31 -24
- package/out/schema.js.map +1 -1
- package/package.json +49 -33
- package/out/cli.d.ts +0 -17
- package/out/cli.d.ts.map +0 -1
- package/out/cli.js.map +0 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ MAILWOMAN_DATA_ROOT="${MAILWOMAN_DATA_ROOT:-/tmp/mailwoman-data}" npx mailwoman
|
|
|
8
8
|
|
|
9
9
|
MAILWOMAN_DATA_ROOT="${MAILWOMAN_DATA_ROOT:-/tmp/mailwoman-data}" \
|
|
10
10
|
npx @mailwoman/photon serve --port 2322
|
|
11
|
-
# The pulled candidate.db is auto-detected at $MAILWOMAN_DATA_ROOT/wof/candidate.db — no export needed.
|
|
11
|
+
# The pulled candidate.db is auto-detected at $MAILWOMAN_DATA_ROOT/db/wof/candidate.db — no export needed.
|
|
12
12
|
# Or point at your own data: --candidate-db <path> / $MAILWOMAN_CANDIDATE_DB
|
|
13
13
|
```
|
|
14
14
|
|
|
@@ -22,7 +22,7 @@ curl "http://localhost:2322/reverse?lat=38.8977&lon=-77.0365"
|
|
|
22
22
|
|
|
23
23
|
## Endpoints
|
|
24
24
|
|
|
25
|
-
| Endpoint | Photon
|
|
25
|
+
| Endpoint | Photon interface |
|
|
26
26
|
| --------------- | ---------------------------------------------------- |
|
|
27
27
|
| `/` | HTML landing page (endpoint index + example queries) |
|
|
28
28
|
| `/api` | forward / autocomplete → GeoJSON FeatureCollection |
|
|
@@ -4,21 +4,15 @@
|
|
|
4
4
|
* @license AGPL-3.0
|
|
5
5
|
* @author Teffen Ellis, et al.
|
|
6
6
|
*
|
|
7
|
-
* `mailwoman-photon` — boot a Photon-compatible autocomplete endpoint via the `serve` command
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* - Examples live in the package README.
|
|
11
|
-
*
|
|
12
|
-
* Wires the real engine: `/api` over `geocodeAddress` (parse → resolve), `/reverse` over
|
|
13
|
-
* `WOFReverseGeocoder`, projecting results into Photon's GeoJSON FeatureCollection. The FST
|
|
14
|
-
* autocomplete tier is the eventual front for `/api`; geocode resolution is the MVP path.
|
|
7
|
+
* `mailwoman-photon` — boot a Photon-compatible autocomplete endpoint via the `serve` command, wiring
|
|
8
|
+
* `/api` over `geocodeAddress` (parse → resolve) and `/reverse` over `WOFReverseGeocoder` into Photon's
|
|
9
|
+
* GeoJSON FeatureCollection.
|
|
15
10
|
*/
|
|
16
11
|
|
|
17
12
|
import { serveNode } from "@mailwoman/api-kit"
|
|
18
13
|
import { matchCountry } from "@mailwoman/codex/country"
|
|
19
|
-
import {
|
|
14
|
+
import { dataRootPath } from "@mailwoman/core/data-root"
|
|
20
15
|
import { parseArguments } from "@mailwoman/core/scripting/arguments"
|
|
21
|
-
import { mailwomanDataRoot } from "@mailwoman/core/utils"
|
|
22
16
|
import { createWOFResolver } from "@mailwoman/resolver"
|
|
23
17
|
import {
|
|
24
18
|
corsBannerLine,
|
|
@@ -27,10 +21,11 @@ import {
|
|
|
27
21
|
openAPICommand,
|
|
28
22
|
resolveGazetteerOrExit,
|
|
29
23
|
runDropInCLI,
|
|
30
|
-
} from "mailwoman/cli
|
|
31
|
-
import type { ResolvedEngineStamp } from "mailwoman/cli
|
|
32
|
-
import { geocodeAddress,
|
|
24
|
+
} from "mailwoman/cli/kit/dropin"
|
|
25
|
+
import type { ResolvedEngineStamp } from "mailwoman/cli/kit/engine-stamp"
|
|
26
|
+
import { geocodeAddress, USStateDatabaseProvider } from "mailwoman/geocode"
|
|
33
27
|
import { createResolverBackend } from "mailwoman/resolver-backend"
|
|
28
|
+
import { titleCase } from "spliterator"
|
|
34
29
|
|
|
35
30
|
import {
|
|
36
31
|
createPhotonApp,
|
|
@@ -58,7 +53,9 @@ const PLACETYPE_TO_KEY: Record<string, keyof PhotonProperties> = {
|
|
|
58
53
|
}
|
|
59
54
|
|
|
60
55
|
/**
|
|
61
|
-
* A real address fits comfortably
|
|
56
|
+
* A real address fits comfortably.
|
|
57
|
+
*
|
|
58
|
+
* Longer input is malformed and would exceed the model's window.
|
|
62
59
|
*/
|
|
63
60
|
const MAX_QUERY_LEN = 512
|
|
64
61
|
|
|
@@ -70,8 +67,8 @@ async function serve(engineStamp: ResolvedEngineStamp): Promise<void> {
|
|
|
70
67
|
port: { type: "string", default: "2322" },
|
|
71
68
|
host: { type: "string", default: "0.0.0.0" },
|
|
72
69
|
"candidate-db": { type: "string" },
|
|
73
|
-
// Permissive CORS is on by default
|
|
74
|
-
//
|
|
70
|
+
// Permissive CORS is on by default for upstream Photon parity; `--no-cors` turns it off
|
|
71
|
+
// where a reverse proxy already sets the headers.
|
|
75
72
|
cors: { type: "boolean", default: true },
|
|
76
73
|
},
|
|
77
74
|
allowNegative: true,
|
|
@@ -82,81 +79,71 @@ async function serve(engineStamp: ResolvedEngineStamp): Promise<void> {
|
|
|
82
79
|
const host = values.host ?? "0.0.0.0"
|
|
83
80
|
|
|
84
81
|
const resolverMod = await import("@mailwoman/resolver-wof-sqlite")
|
|
85
|
-
const gazetteer = await resolveGazetteerOrExit(values["candidate-db"])
|
|
82
|
+
const gazetteer = await resolveGazetteerOrExit(values["candidate-db"] ?? null)
|
|
86
83
|
const { adminDBPath, candidateDB, wofPaths } = gazetteer
|
|
87
84
|
const classifier = await loadClassifierOrExit()
|
|
88
85
|
|
|
89
86
|
const backend = await createResolverBackend(resolverMod, { wofPaths, candidateDB })
|
|
90
87
|
const resolver = createWOFResolver(backend)
|
|
91
|
-
const extracts = await
|
|
88
|
+
const extracts = await USStateDatabaseProvider.create(resolverMod, dataRootPath())
|
|
92
89
|
const postcodeOfLocality = await createLocalityPostcodeLookup()
|
|
93
|
-
// National open-register rooftop tier
|
|
94
|
-
// when the extract
|
|
95
|
-
const { BANRegionDatabaseProvider } = await import("@mailwoman/ban/
|
|
96
|
-
const banExtracts = await BANRegionDatabaseProvider.create(
|
|
90
|
+
// National open-register rooftop tier: BAN-FR ahead of the OSM tier for a non-US parse,
|
|
91
|
+
// a no-op when the extract is absent.
|
|
92
|
+
const { BANRegionDatabaseProvider } = await import("@mailwoman/ban/region-database-provider")
|
|
93
|
+
const banExtracts = await BANRegionDatabaseProvider.create(dataRootPath())
|
|
97
94
|
const reverseGeo = adminDBPath ? new resolverMod.WOFReverseGeocoder({ adminDBPath }) : undefined
|
|
98
95
|
|
|
99
96
|
const engine: PhotonEngine = {
|
|
100
97
|
async search(params) {
|
|
101
|
-
// Empty/whitespace → no query; absurdly long → not an address (and would blow the model's input).
|
|
102
98
|
const query = params.q?.trim()
|
|
103
99
|
|
|
104
100
|
if (!query || query.length > MAX_QUERY_LEN) return photonCollection([])
|
|
105
|
-
//
|
|
106
|
-
// folds into candidate scoring
|
|
101
|
+
// Forward the client's viewport as a proximity bias, a soft re-rank the resolver
|
|
102
|
+
// folds into candidate scoring, only when both coords are present.
|
|
107
103
|
const bias = params.lat != null && params.lon != null ? [{ lat: params.lat, lon: params.lon }] : undefined
|
|
108
104
|
|
|
109
|
-
// No country constraint
|
|
110
|
-
//
|
|
111
|
-
// non-US query to its US namesake — wrong for a global autocomplete front.
|
|
105
|
+
// No country constraint lets the placer route the query's own country.
|
|
106
|
+
// A hard-coded `US` value here would resolve every non-US query to its US namesake.
|
|
112
107
|
const result = await geocodeAddress(query, {
|
|
113
108
|
classifier,
|
|
114
109
|
resolver,
|
|
115
110
|
databases: extracts.for,
|
|
116
111
|
nationalDatabases: banExtracts.for,
|
|
117
112
|
bias,
|
|
118
|
-
//
|
|
113
|
+
// Photon is an autocomplete front where a human types fragments.
|
|
119
114
|
inputMode: "fragmented",
|
|
120
115
|
})
|
|
121
116
|
|
|
122
117
|
if (result.lat == null || result.lon == null) return photonCollection([])
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
//
|
|
126
|
-
// so state/county come through only on an ancestry-capable backend — country still lands from the code.
|
|
118
|
+
// Decorate from the resolved place using proper-cased ancestry names,
|
|
119
|
+
// the resolved country and osm_key/value/type.
|
|
120
|
+
// Include state/county only on an ancestry-capable backend.
|
|
127
121
|
const country = matchCountry(result.countryCode)
|
|
128
122
|
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
//
|
|
123
|
+
// A rooftop or interpolated tier is house-grade: include the parsed housenumber
|
|
124
|
+
// and street so photonForwardProperties decorates it `type: house`
|
|
125
|
+
// rather than the admin locality's `type: city`.
|
|
132
126
|
const houseGrade =
|
|
133
127
|
result.resolution_tier === "address_point" ||
|
|
134
128
|
result.resolution_tier === "interpolated" ||
|
|
135
129
|
result.resolution_tier === "plus_code"
|
|
136
130
|
|
|
137
|
-
//
|
|
138
|
-
// highway/street osm tags
|
|
131
|
+
// The street-centroid tier is street-grade: the full assembled street name
|
|
132
|
+
// in `name` plus highway/street osm tags.
|
|
139
133
|
const streetGrade = result.resolution_tier === "street"
|
|
140
134
|
|
|
141
|
-
// The register row's own
|
|
142
|
-
//
|
|
143
|
-
// postcode even when the query never named them, and #1014's decorate-from-the-resolved-place
|
|
144
|
-
// doctrine covers register attestations exactly as it covers gazetteer rows. The key form is
|
|
145
|
-
// normalized; title-case it for display (the extracts store no display-cased locality).
|
|
135
|
+
// The register row's own locality decorates a house-grade answer whose hierarchy
|
|
136
|
+
// has no locality, title-cased because extracts store no display-cased locality.
|
|
146
137
|
const places = result.hierarchy.map((h) => ({ tag: h.tag, name: h.name }))
|
|
147
138
|
|
|
148
139
|
if (result.rooftop?.localityNorm && !places.some((p) => p.tag === "locality")) {
|
|
149
|
-
|
|
150
|
-
// pyTitle display-cases it particle-and-apostrophe-aware.
|
|
151
|
-
places.push({ tag: "locality", name: pyTitle(result.rooftop.localityNorm) })
|
|
140
|
+
places.push({ tag: "locality", name: titleCase(result.rooftop.localityNorm) })
|
|
152
141
|
}
|
|
153
142
|
|
|
154
|
-
// Locality→postcode enrichment: an admin answer
|
|
155
|
-
// unambiguous (exactly one
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
// name matching is involved.
|
|
159
|
-
let enrichedPostcode: string | undefined
|
|
143
|
+
// Locality→postcode enrichment: an admin answer whose containing postcode is
|
|
144
|
+
// unambiguous (exactly one, keyed by the resolved place's WOF ID) includes it.
|
|
145
|
+
// A multi-postcode city gets no postcode.
|
|
146
|
+
let enrichedPostcode: string | null = null
|
|
160
147
|
|
|
161
148
|
if (!result.postcode && !result.rooftop?.postcode) {
|
|
162
149
|
const localityID = result.hierarchy.find(
|
|
@@ -178,8 +165,8 @@ async function serve(engineStamp: ResolvedEngineStamp): Promise<void> {
|
|
|
178
165
|
...(streetGrade ? { street: { name: result.street } } : {}),
|
|
179
166
|
}
|
|
180
167
|
|
|
181
|
-
//
|
|
182
|
-
// extra features
|
|
168
|
+
// candidates[0] is the primary.
|
|
169
|
+
// Its ranked alternatives become extra features up to the requested `limit`.
|
|
183
170
|
const alternatives = result.candidates.slice(1).map((c) => {
|
|
184
171
|
const cc = matchCountry(c.countryCode)
|
|
185
172
|
|
|
@@ -201,8 +188,7 @@ async function serve(engineStamp: ResolvedEngineStamp): Promise<void> {
|
|
|
201
188
|
if (!hierarchy.length) return photonCollection([])
|
|
202
189
|
const deepest = hierarchy[0]!
|
|
203
190
|
|
|
204
|
-
//
|
|
205
|
-
// no Photon client should dereference an undefined osm_key on a reverse result either.
|
|
191
|
+
// Include osm_key/osm_value/type from the deepest placetype so `/reverse` matches `/api`'s schema.
|
|
206
192
|
const properties: PhotonProperties = {
|
|
207
193
|
name: deepest.name,
|
|
208
194
|
countrycode: deepest.country?.toLowerCase(),
|
package/lib/app.ts
CHANGED
|
@@ -2,43 +2,25 @@
|
|
|
2
2
|
* @copyright Sister Software
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* The Photon-compatible Hono app: CORS + error safety net + routes + the emitted OpenAPI
|
|
7
|
-
* document. Engine-agnostic — the CLI wires the real engine; tests inject fixtures.
|
|
8
5
|
*/
|
|
9
6
|
|
|
10
7
|
import { OpenAPIHono } from "@hono/zod-openapi"
|
|
11
|
-
import {
|
|
12
|
-
|
|
8
|
+
import {
|
|
9
|
+
attachOpenAPIDocs,
|
|
10
|
+
type CompatibilityAppOptions,
|
|
11
|
+
engineHeaders,
|
|
12
|
+
type OpenAPIDocInfo,
|
|
13
|
+
readServedDocumentInfo,
|
|
14
|
+
} from "@mailwoman/api-kit"
|
|
13
15
|
import { cors } from "hono/cors"
|
|
14
16
|
|
|
15
17
|
import type { PhotonEngine } from "#engine"
|
|
16
18
|
import { registerPhotonRoutes } from "#routes"
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Emit permissive CORS headers (`Access-Control-Allow-Origin: *`) on every response and answer preflight `OPTIONS`
|
|
24
|
-
* with `204`. Default `true` — upstream komoot/photon serves permissive CORS, and the map-widget use case
|
|
25
|
-
* (leaflet-control-geocoder, @openrunner/photon-geocoder, …) needs it: a browser's cross-origin XHR is blocked
|
|
26
|
-
* without it (#1017). Set `false` when a reverse proxy already owns the CORS headers.
|
|
27
|
-
*/
|
|
28
|
-
cors?: boolean
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The engine stamp to carry on every response: `engine` as a foreign member of each FeatureCollection, and the
|
|
32
|
-
* `Server` + `Link: rel="license"` headers everywhere. Absent when an embedding application builds the app without
|
|
33
|
-
* the `mailwoman` package; the `photon` bin always passes one.
|
|
34
|
-
*/
|
|
35
|
-
engine?: EngineStamp
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* The document info stamped into the emitted OpenAPI document. Exported (not inlined) so the CLI's `openapi` subcommand
|
|
40
|
-
* can call `emitOpenAPIDocuments` with the SAME info the mounted `/openapi.json` route (below, via
|
|
41
|
-
* {@link attachOpenAPIDocs}) uses — one source of truth, no risk of the two drifting.
|
|
21
|
+
* The OpenAPI document info for the Photon API.
|
|
22
|
+
*
|
|
23
|
+
* The served `/openapi.json` route and the CLI's `openapi` subcommand both use it.
|
|
42
24
|
*/
|
|
43
25
|
export const PHOTON_DOC_INFO: OpenAPIDocInfo = {
|
|
44
26
|
...(await readServedDocumentInfo(import.meta.url, "@mailwoman/photon")),
|
|
@@ -62,13 +44,11 @@ export const PHOTON_DOC_INFO: OpenAPIDocInfo = {
|
|
|
62
44
|
}
|
|
63
45
|
|
|
64
46
|
/**
|
|
65
|
-
*
|
|
47
|
+
* Builds the Photon-compatible app around the given {@link PhotonEngine}.
|
|
66
48
|
*/
|
|
67
|
-
export function createPhotonApp(engine: PhotonEngine, options:
|
|
49
|
+
export function createPhotonApp(engine: PhotonEngine, options: CompatibilityAppOptions = {}): OpenAPIHono {
|
|
68
50
|
const app = new OpenAPIHono()
|
|
69
51
|
|
|
70
|
-
// Browser-embedded widgets need CORS or their cross-origin XHR is blocked before the request completes (#1017).
|
|
71
|
-
// GET-only — photon has no mutating routes, so unlike libpostal's CORS there is no POST in the methods list.
|
|
72
52
|
if (options.cors !== false) {
|
|
73
53
|
app.use(cors({ origin: "*", allowMethods: ["GET", "OPTIONS"], allowHeaders: ["*"], maxAge: 86_400 }))
|
|
74
54
|
}
|
|
@@ -77,8 +57,6 @@ export function createPhotonApp(engine: PhotonEngine, options: PhotonAppOptions
|
|
|
77
57
|
app.use(engineHeaders(options.engine))
|
|
78
58
|
}
|
|
79
59
|
|
|
80
|
-
// Safety net: malformed input or an engine fault returns an empty FeatureCollection, never a crash (photon's
|
|
81
|
-
// envelope — NOT `{error}`, which is the libpostal/nominatim shape).
|
|
82
60
|
app.onError((_error, c) => c.json({ type: "FeatureCollection", features: [], message: "internal error" }, 500))
|
|
83
61
|
|
|
84
62
|
registerPhotonRoutes(app, engine, options.engine)
|
package/lib/engine.ts
CHANGED
|
@@ -3,14 +3,17 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
* The Photon engine
|
|
6
|
+
* The Photon engine interface + wire types the router delegates to. Feature/collection
|
|
7
7
|
* construction lives here too (`photonFeature`, `photonCollection`) since every projection
|
|
8
|
-
* builds on them
|
|
8
|
+
* builds on them. the resolved-place → {@link PhotonProperties} projection itself lives in
|
|
9
9
|
* `projection.ts`.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Photon feature properties
|
|
13
|
+
* Photon feature properties.
|
|
14
|
+
*
|
|
15
|
+
* OSM-derived tag names, populated from Mailwoman's `ComponentTag` / resolved place.
|
|
16
|
+
*
|
|
14
17
|
* `extent` is `[minLon, maxLat, maxLon, minLat]` per Photon's convention.
|
|
15
18
|
*/
|
|
16
19
|
export interface PhotonProperties {
|
|
@@ -59,15 +62,15 @@ export interface PhotonFeatureCollection {
|
|
|
59
62
|
export interface PhotonSearchParams {
|
|
60
63
|
q: string
|
|
61
64
|
limit: number
|
|
62
|
-
lang?: string
|
|
65
|
+
lang?: string | null
|
|
63
66
|
/**
|
|
64
67
|
* Location bias.
|
|
65
68
|
*/
|
|
66
|
-
lat?: number
|
|
67
|
-
lon?: number
|
|
68
|
-
bbox?: [number, number, number, number]
|
|
69
|
-
osmTag?: string[]
|
|
70
|
-
layer?: string[]
|
|
69
|
+
lat?: number | null
|
|
70
|
+
lon?: number | null
|
|
71
|
+
bbox?: [number, number, number, number] | null
|
|
72
|
+
osmTag?: string[] | null
|
|
73
|
+
layer?: string[] | null
|
|
71
74
|
}
|
|
72
75
|
|
|
73
76
|
/**
|
|
@@ -77,13 +80,18 @@ export interface PhotonReverseParams {
|
|
|
77
80
|
lat: number
|
|
78
81
|
lon: number
|
|
79
82
|
limit: number
|
|
80
|
-
lang?: string
|
|
81
|
-
radius?: number
|
|
83
|
+
lang?: string | null
|
|
84
|
+
radius?: number | null
|
|
82
85
|
}
|
|
83
86
|
|
|
84
87
|
/**
|
|
85
|
-
* The engine the router delegates to.
|
|
86
|
-
*
|
|
88
|
+
* The engine the router delegates to.
|
|
89
|
+
*
|
|
90
|
+
* Each method is optional.
|
|
91
|
+
* A missing one answers `501`.
|
|
92
|
+
*
|
|
93
|
+
* The real implementation backs `/api` with the FST autocomplete tier + parse→resolve.
|
|
94
|
+
* and `/reverse` with the `WOFReverseGeocoder`.
|
|
87
95
|
*/
|
|
88
96
|
export interface PhotonEngine {
|
|
89
97
|
search?(params: PhotonSearchParams): Promise<PhotonFeatureCollection>
|
package/lib/index.ts
CHANGED
|
@@ -3,20 +3,15 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
* `@mailwoman/photon`
|
|
6
|
+
* `@mailwoman/photon` is a Photon-compatible autocomplete / type-ahead geocoding API over the
|
|
7
7
|
* Mailwoman engine. Where `@mailwoman/nominatim` is structured lookup, Photon is
|
|
8
8
|
* search-as-you-type: a GeoJSON `FeatureCollection` per query, biased by location, ranked for
|
|
9
|
-
* prefixes. It maps onto Mailwoman's shipped FST autocomplete tier
|
|
10
|
-
*
|
|
9
|
+
* prefixes. It maps onto Mailwoman's shipped FST autocomplete tier and the parse then resolve
|
|
10
|
+
* path.
|
|
11
11
|
*
|
|
12
|
-
* Like its siblings, the package is engine-agnostic
|
|
13
|
-
* {@link PhotonEngine}
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* The Hono app (CORS + error envelope + the emitted OpenAPI document) lives in `app.ts`; route
|
|
17
|
-
* definitions + handlers in `routes.ts`; wire types + the engine contract in `engine.ts`; the
|
|
18
|
-
* resolved-place → Photon-schema projection in `projection.ts`; the zod wire schemas in
|
|
19
|
-
* `schema.ts`.
|
|
12
|
+
* Like its siblings, the package is engine-agnostic. {@link createPhotonApp} takes a
|
|
13
|
+
* {@link PhotonEngine}, and the CLI wires the real engine. Routes whose engine method is absent
|
|
14
|
+
* answer `501`.
|
|
20
15
|
*/
|
|
21
16
|
|
|
22
17
|
export * from "#app"
|
package/lib/locality-postcode.ts
CHANGED
|
@@ -3,42 +3,38 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
* Reverse lookup over the `postcode-locality-*.db` artifacts: the
|
|
7
|
-
* resolved locality, keyed by its WOF id
|
|
8
|
-
* enrichment: a village answer carries the postcode the gazetteer attests for it.
|
|
6
|
+
* Reverse lookup over the `postcode-locality-*.db` artifacts: the containing postcode of a
|
|
7
|
+
* resolved locality, keyed by its WOF id.
|
|
9
8
|
*
|
|
10
|
-
* The exactly-one rule is the abstention: a locality contained by several postcodes (any real
|
|
11
|
-
*
|
|
12
|
-
* Tolerate-and-degrade like every optional artifact: a machine without the DBs answers undefined
|
|
13
|
-
* everywhere and the consumer simply doesn't decorate.
|
|
9
|
+
* The exactly-one rule is the abstention: a locality contained by several postcodes (any real city) gets
|
|
10
|
+
* no postcode. A machine without the DBs degrades to no enrichment.
|
|
14
11
|
*/
|
|
15
12
|
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
13
|
+
import { pathExists } from "@mailwoman/core/fs/readers/stat"
|
|
14
|
+
import { wofDatabasePath } from "@mailwoman/resolver-wof-sqlite/paths"
|
|
18
15
|
import { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
19
|
-
import type { PostcodeLocalityDatabase } from "mailwoman/
|
|
16
|
+
import type { PostcodeLocalityDatabase } from "mailwoman/locality-postcode-schema"
|
|
20
17
|
|
|
21
18
|
/**
|
|
22
19
|
* The per-country artifacts, probed in caller-country order with `intl` as the shared fallback.
|
|
23
20
|
*/
|
|
24
21
|
const POSTCODE_LOCALITY_SUFFIXES = ["fr", "de", "jp", "intl"] as const
|
|
25
22
|
|
|
26
|
-
export type LocalityPostcodeLookup = (wofID: number, countryCode: string | null) => string |
|
|
23
|
+
export type LocalityPostcodeLookup = (wofID: number, countryCode: string | null) => string | null
|
|
27
24
|
|
|
28
25
|
export async function createLocalityPostcodeLookup(): Promise<LocalityPostcodeLookup> {
|
|
29
26
|
const statements = new Map<string, ReturnType<DatabaseClient["prepare"]>>()
|
|
30
27
|
|
|
31
28
|
for (const suffix of POSTCODE_LOCALITY_SUFFIXES) {
|
|
32
|
-
const path =
|
|
29
|
+
const path = wofDatabasePath(`postcode-locality-${suffix}.db`)
|
|
33
30
|
|
|
34
31
|
if (!(await pathExists(path))) continue
|
|
35
32
|
|
|
36
33
|
try {
|
|
37
34
|
const db = new DatabaseClient<PostcodeLocalityDatabase>(path, { readOnly: true })
|
|
38
35
|
|
|
39
|
-
// No `is_containing` filter: villages routinely
|
|
40
|
-
//
|
|
41
|
-
// The exactly-one DISTINCT rule below is the entire ambiguity guard.
|
|
36
|
+
// No `is_containing` filter: villages routinely have 0, so the exactly-one
|
|
37
|
+
// distinct rule below is the entire ambiguity guard.
|
|
42
38
|
statements.set(
|
|
43
39
|
suffix,
|
|
44
40
|
db.prepare(`SELECT DISTINCT postcode FROM postcode_locality WHERE locality_id = ? LIMIT 2`)
|
|
@@ -61,9 +57,9 @@ export async function createLocalityPostcodeLookup(): Promise<LocalityPostcodeLo
|
|
|
61
57
|
|
|
62
58
|
if (rows.length === 1) return rows[0]!.postcode
|
|
63
59
|
|
|
64
|
-
if (rows.length > 1) return
|
|
60
|
+
if (rows.length > 1) return null // ambiguous — abstain, never guess
|
|
65
61
|
}
|
|
66
62
|
|
|
67
|
-
return
|
|
63
|
+
return null
|
|
68
64
|
}
|
|
69
65
|
}
|