@mailwoman/photon 9.1.0 → 9.3.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/{app.ts → lib/app.ts} +28 -7
- package/{cli.ts → lib/cli.ts} +61 -36
- package/{index.ts → lib/index.ts} +4 -4
- package/lib/locality-postcode.ts +69 -0
- package/{projection.ts → lib/projection.ts} +6 -1
- package/{routes.ts → lib/routes.ts} +13 -21
- package/{schema.ts → lib/schema.ts} +2 -2
- package/out/app.d.ts +8 -1
- package/out/app.d.ts.map +1 -1
- package/out/app.js +15 -6
- package/out/app.js.map +1 -1
- package/out/cli.d.ts.map +1 -1
- package/out/cli.js +50 -33
- package/out/cli.js.map +1 -1
- package/out/engine.d.ts.map +1 -1
- package/out/engine.js.map +1 -1
- package/out/index.d.ts +4 -4
- package/out/index.d.ts.map +1 -1
- package/out/index.js +4 -4
- package/out/index.js.map +1 -1
- package/out/locality-postcode.d.ts +17 -0
- package/out/locality-postcode.d.ts.map +1 -0
- package/out/locality-postcode.js +55 -0
- package/out/locality-postcode.js.map +1 -0
- package/out/projection.d.ts +1 -1
- package/out/projection.d.ts.map +1 -1
- package/out/projection.js +6 -1
- package/out/projection.js.map +1 -1
- package/out/routes.d.ts +3 -2
- package/out/routes.d.ts.map +1 -1
- package/out/routes.js +9 -16
- package/out/routes.js.map +1 -1
- package/out/schema.d.ts +10 -2
- package/out/schema.d.ts.map +1 -1
- package/out/schema.js +2 -2
- package/out/schema.js.map +1 -1
- package/package.json +44 -15
- /package/{engine.ts → lib/engine.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @mailwoman/photon
|
|
2
2
|
|
|
3
|
-
A **Photon-compatible autocomplete geocoding API** over the [Mailwoman](https://mailwoman.
|
|
3
|
+
A **Photon-compatible autocomplete geocoding API** over the [Mailwoman](https://mailwoman.ai) engine — search-as-you-type, returning GeoJSON `FeatureCollection`s. Where [`@mailwoman/nominatim`](../nominatim) is structured lookup, this is the type-ahead front door. No Elasticsearch.
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
6
|
# One-time data fetch (worldwide candidate gazetteer, population-first ranking, ~1.65 GB):
|
|
@@ -13,7 +13,7 @@ MAILWOMAN_DATA_ROOT="${MAILWOMAN_DATA_ROOT:-/tmp/mailwoman-data}" \
|
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Prefer to try before self-hosting? A hosted trial endpoint runs at
|
|
16
|
-
**https://photon.
|
|
16
|
+
**https://photon.mailwoman.ai** (`/api` + `/reverse`, conservative rate limits).
|
|
17
17
|
|
|
18
18
|
```bash
|
|
19
19
|
curl "http://localhost:2322/api?q=1600 penn&limit=5&lat=38.9&lon=-77"
|
package/{app.ts → lib/app.ts}
RENAMED
|
@@ -8,12 +8,22 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { OpenAPIHono } from "@hono/zod-openapi"
|
|
11
|
-
import { attachOpenAPIDocs, type OpenAPIDocInfo } from "@mailwoman/api-kit"
|
|
12
|
-
import
|
|
11
|
+
import { attachOpenAPIDocs, engineHeaders, type OpenAPIDocInfo } from "@mailwoman/api-kit"
|
|
12
|
+
import { readLocalJSONFile } from "@mailwoman/core/fs/readers"
|
|
13
|
+
import type { EngineStamp } from "@mailwoman/core/license"
|
|
14
|
+
import { resolvePackagePath } from "@mailwoman/core/module/resolvers"
|
|
13
15
|
import { cors } from "hono/cors"
|
|
14
16
|
|
|
15
|
-
import type { PhotonEngine } from "
|
|
16
|
-
import { registerPhotonRoutes } from "
|
|
17
|
+
import type { PhotonEngine } from "#engine"
|
|
18
|
+
import { registerPhotonRoutes } from "#routes"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* This package's own manifest, read at load rather than imported as a module: a JSON import makes `tsc` copy the file
|
|
22
|
+
* into `out/`, where it becomes the package scope for the compiled tree and breaks every `#` import in it.
|
|
23
|
+
*/
|
|
24
|
+
const packageJson = await readLocalJSONFile<{ name: string; version: string; description: string }>(
|
|
25
|
+
resolvePackagePath("@mailwoman/photon", "package.json")
|
|
26
|
+
)
|
|
17
27
|
|
|
18
28
|
/**
|
|
19
29
|
* Options for {@link createPhotonApp}.
|
|
@@ -26,6 +36,13 @@ export interface PhotonAppOptions {
|
|
|
26
36
|
* without it (#1017). Set `false` when a reverse proxy already owns the CORS headers.
|
|
27
37
|
*/
|
|
28
38
|
cors?: boolean
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The engine stamp to carry on every response: `engine` as a foreign member of each FeatureCollection, and the
|
|
42
|
+
* `Server` + `Link: rel="license"` headers everywhere. Absent when an embedding application builds the app without
|
|
43
|
+
* the `mailwoman` package; the `photon` bin always passes one.
|
|
44
|
+
*/
|
|
45
|
+
engine?: EngineStamp
|
|
29
46
|
}
|
|
30
47
|
|
|
31
48
|
/**
|
|
@@ -38,10 +55,10 @@ export const PHOTON_DOC_INFO: OpenAPIDocInfo = {
|
|
|
38
55
|
version: packageJson.version,
|
|
39
56
|
description: packageJson.description,
|
|
40
57
|
license: { name: "AGPL-3.0-only OR LicenseRef-Commercial", identifier: "AGPL-3.0-only" },
|
|
41
|
-
contact: { name: "Sister Software", url: "https://mailwoman.
|
|
58
|
+
contact: { name: "Sister Software", url: "https://mailwoman.ai" },
|
|
42
59
|
externalDocs: {
|
|
43
60
|
description: "What Mailwoman is",
|
|
44
|
-
url: "https://mailwoman.
|
|
61
|
+
url: "https://mailwoman.ai/docs/developers/get-started/what-mailwoman-is",
|
|
45
62
|
},
|
|
46
63
|
servers: [
|
|
47
64
|
{
|
|
@@ -68,11 +85,15 @@ export function createPhotonApp(engine: PhotonEngine, options: PhotonAppOptions
|
|
|
68
85
|
app.use(cors({ origin: "*", allowMethods: ["GET", "OPTIONS"], allowHeaders: ["*"], maxAge: 86_400 }))
|
|
69
86
|
}
|
|
70
87
|
|
|
88
|
+
if (options.engine) {
|
|
89
|
+
app.use(engineHeaders(options.engine))
|
|
90
|
+
}
|
|
91
|
+
|
|
71
92
|
// Safety net: malformed input or an engine fault returns an empty FeatureCollection, never a crash (photon's
|
|
72
93
|
// envelope — NOT `{error}`, which is the libpostal/nominatim shape).
|
|
73
94
|
app.onError((_error, c) => c.json({ type: "FeatureCollection", features: [], message: "internal error" }, 500))
|
|
74
95
|
|
|
75
|
-
registerPhotonRoutes(app, engine)
|
|
96
|
+
registerPhotonRoutes(app, engine, options.engine)
|
|
76
97
|
attachOpenAPIDocs(app, PHOTON_DOC_INFO)
|
|
77
98
|
|
|
78
99
|
return app
|
package/{cli.ts → lib/cli.ts}
RENAMED
|
@@ -14,20 +14,21 @@
|
|
|
14
14
|
* autocomplete tier is the eventual front for `/api`; geocode resolution is the MVP path.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
import { printOpenAPIDocument, serveNode } from "@mailwoman/api-kit"
|
|
17
|
+
import { serveNode } from "@mailwoman/api-kit"
|
|
20
18
|
import { matchCountry } from "@mailwoman/codex/country"
|
|
19
|
+
import { pyTitle } from "@mailwoman/core"
|
|
20
|
+
import { parseArguments } from "@mailwoman/core/scripting/arguments"
|
|
21
21
|
import { createWOFResolver } from "@mailwoman/resolver"
|
|
22
22
|
import {
|
|
23
23
|
corsBannerLine,
|
|
24
24
|
gazetteerBannerLines,
|
|
25
25
|
loadClassifierOrExit,
|
|
26
|
-
|
|
26
|
+
openAPICommand,
|
|
27
27
|
resolveGazetteerOrExit,
|
|
28
28
|
runDropInCLI,
|
|
29
29
|
} from "mailwoman/cli-kit/dropin"
|
|
30
|
-
import {
|
|
30
|
+
import type { ResolvedEngineStamp } from "mailwoman/cli-kit/engine-stamp"
|
|
31
|
+
import { geocodeAddress, RegionDatabaseProvider } from "mailwoman/geocode"
|
|
31
32
|
import { createResolverBackend, mailwomanDataRoot } from "mailwoman/resolver-backend"
|
|
32
33
|
|
|
33
34
|
import {
|
|
@@ -40,7 +41,8 @@ import {
|
|
|
40
41
|
photonOSMTags,
|
|
41
42
|
type PhotonEngine,
|
|
42
43
|
type PhotonProperties,
|
|
43
|
-
} from "
|
|
44
|
+
} from "#index"
|
|
45
|
+
import { createLocalityPostcodeLookup } from "#locality-postcode"
|
|
44
46
|
|
|
45
47
|
/**
|
|
46
48
|
* WOF placetype → Photon property key.
|
|
@@ -61,8 +63,8 @@ const MAX_QUERY_LEN = 512
|
|
|
61
63
|
|
|
62
64
|
const BINARY_NAME = "mailwoman-photon"
|
|
63
65
|
|
|
64
|
-
async function serve(): Promise<void> {
|
|
65
|
-
const { values } =
|
|
66
|
+
async function serve(engineStamp: ResolvedEngineStamp): Promise<void> {
|
|
67
|
+
const { values } = parseArguments({
|
|
66
68
|
options: {
|
|
67
69
|
port: { type: "string", default: "2322" },
|
|
68
70
|
host: { type: "string", default: "0.0.0.0" },
|
|
@@ -79,17 +81,18 @@ async function serve(): Promise<void> {
|
|
|
79
81
|
const host = values.host ?? "0.0.0.0"
|
|
80
82
|
|
|
81
83
|
const resolverMod = await import("@mailwoman/resolver-wof-sqlite")
|
|
82
|
-
const gazetteer = resolveGazetteerOrExit(values["candidate-db"])
|
|
83
|
-
const { adminDBPath,
|
|
84
|
+
const gazetteer = await resolveGazetteerOrExit(values["candidate-db"])
|
|
85
|
+
const { adminDBPath, candidateDB, wofPaths } = gazetteer
|
|
84
86
|
const classifier = await loadClassifierOrExit()
|
|
85
87
|
|
|
86
|
-
const backend = createResolverBackend(resolverMod, { wofPaths,
|
|
88
|
+
const backend = await createResolverBackend(resolverMod, { wofPaths, candidateDB })
|
|
87
89
|
const resolver = createWOFResolver(backend)
|
|
88
|
-
const
|
|
90
|
+
const extracts = await RegionDatabaseProvider.create(resolverMod, mailwomanDataRoot())
|
|
91
|
+
const postcodeOfLocality = await createLocalityPostcodeLookup()
|
|
89
92
|
// National open-register rooftop tier (#1012): BAN-FR ahead of the OSM tier for a non-US parse. A no-op
|
|
90
|
-
// when the
|
|
91
|
-
const {
|
|
92
|
-
const
|
|
93
|
+
// when the extract isn't on disk (conditioned on existsSync inside the provider), so the endpoint degrades cleanly.
|
|
94
|
+
const { BANRegionDatabaseProvider } = await import("@mailwoman/ban/sdk")
|
|
95
|
+
const banExtracts = await BANRegionDatabaseProvider.create(mailwomanDataRoot())
|
|
93
96
|
const reverseGeo = adminDBPath ? new resolverMod.WOFReverseGeocoder({ adminDBPath }) : undefined
|
|
94
97
|
|
|
95
98
|
const engine: PhotonEngine = {
|
|
@@ -108,8 +111,8 @@ async function serve(): Promise<void> {
|
|
|
108
111
|
const result = await geocodeAddress(query, {
|
|
109
112
|
classifier,
|
|
110
113
|
resolver,
|
|
111
|
-
|
|
112
|
-
|
|
114
|
+
databases: extracts.for,
|
|
115
|
+
nationalDatabases: banExtracts.for,
|
|
113
116
|
bias,
|
|
114
117
|
// Decision A endpoint default: Photon is an autocomplete front — a human typing fragments.
|
|
115
118
|
inputMode: "fragmented",
|
|
@@ -121,20 +124,55 @@ async function serve(): Promise<void> {
|
|
|
121
124
|
// Photon clients don't TypeError. The candidate backend fills only the locality (no ancestors() table),
|
|
122
125
|
// so state/county come through only on an ancestry-capable backend — country still lands from the code.
|
|
123
126
|
const country = matchCountry(result.countryCode)
|
|
127
|
+
|
|
124
128
|
// #1041: a rooftop (`address_point`) or house-number-estimate (`interpolated`) tier is HOUSE-GRADE — carry the
|
|
125
129
|
// parsed housenumber + street so photonForwardProperties decorates it `type: house` (matching upstream Photon)
|
|
126
130
|
// instead of inheriting the admin locality's `type: city`. The admin tier (a locality centroid) never does.
|
|
127
|
-
const houseGrade =
|
|
131
|
+
const houseGrade =
|
|
132
|
+
result.resolution_tier === "address_point" ||
|
|
133
|
+
result.resolution_tier === "interpolated" ||
|
|
134
|
+
result.resolution_tier === "plus_code"
|
|
135
|
+
|
|
128
136
|
// #1050: the street-centroid tier is STREET-GRADE — full assembled street name in `name`,
|
|
129
137
|
// highway/street osm tags (the parallel of the #1041 house treatment).
|
|
130
138
|
const streetGrade = result.resolution_tier === "street"
|
|
131
139
|
|
|
140
|
+
// The register row's own scope tags (result.rooftop) decorate a house-grade answer whose
|
|
141
|
+
// hierarchy carries no locality/postcode — the register ATTESTS the rooftop's commune and
|
|
142
|
+
// postcode even when the query never named them, and #1014's decorate-from-the-resolved-place
|
|
143
|
+
// doctrine covers register attestations exactly as it covers gazetteer rows. The key form is
|
|
144
|
+
// normalized; title-case it for display (the extracts store no display-cased locality).
|
|
145
|
+
const places = result.hierarchy.map((h) => ({ tag: h.tag, name: h.name }))
|
|
146
|
+
|
|
147
|
+
if (result.rooftop?.localityNorm && !places.some((p) => p.tag === "locality")) {
|
|
148
|
+
// The key form is normalized lowercase (the extracts store no display-cased locality);
|
|
149
|
+
// pyTitle display-cases it particle-and-apostrophe-aware.
|
|
150
|
+
places.push({ tag: "locality", name: pyTitle(result.rooftop.localityNorm) })
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Locality→postcode enrichment: an admin answer for a place whose CONTAINING postcode is
|
|
154
|
+
// unambiguous (exactly one) carries that postcode — the register/WOF attests it, the query
|
|
155
|
+
// simply never said it. Multi-postcode cities (Paris) get NOTHING: the exactly-one rule is
|
|
156
|
+
// the abstention, per the registry doctrine. Keyed by the resolved place's WOF id, so no
|
|
157
|
+
// name matching is involved.
|
|
158
|
+
let enrichedPostcode: string | undefined
|
|
159
|
+
|
|
160
|
+
if (!result.postcode && !result.rooftop?.postcode) {
|
|
161
|
+
const localityID = result.hierarchy.find(
|
|
162
|
+
(h) => (h.tag === "locality" || h.tag === "localadmin") && h.placeID?.startsWith("wof:")
|
|
163
|
+
)?.placeID
|
|
164
|
+
|
|
165
|
+
if (localityID) {
|
|
166
|
+
enrichedPostcode = postcodeOfLocality(Number(localityID.slice(4)), result.countryCode)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
132
170
|
const primary: PhotonForwardInput = {
|
|
133
171
|
lat: result.lat,
|
|
134
172
|
lon: result.lon,
|
|
135
|
-
postcode: result.postcode,
|
|
173
|
+
postcode: result.postcode ?? result.rooftop?.postcode ?? enrichedPostcode,
|
|
136
174
|
country: country ? { name: country.canonical, code: country.iso2 } : undefined,
|
|
137
|
-
places
|
|
175
|
+
places,
|
|
138
176
|
...(houseGrade ? { house: { number: result.house_number, street: result.street } } : {}),
|
|
139
177
|
...(streetGrade ? { street: { name: result.street } } : {}),
|
|
140
178
|
}
|
|
@@ -182,9 +220,9 @@ async function serve(): Promise<void> {
|
|
|
182
220
|
},
|
|
183
221
|
}
|
|
184
222
|
|
|
185
|
-
const app = createPhotonApp(engine, { cors: values.cors })
|
|
223
|
+
const app = createPhotonApp(engine, { cors: values.cors, engine: engineStamp.stamp })
|
|
186
224
|
|
|
187
|
-
serveNode({
|
|
225
|
+
await serveNode({
|
|
188
226
|
fetch: app.fetch,
|
|
189
227
|
port,
|
|
190
228
|
hostname: host,
|
|
@@ -201,22 +239,9 @@ async function serve(): Promise<void> {
|
|
|
201
239
|
})
|
|
202
240
|
}
|
|
203
241
|
|
|
204
|
-
/**
|
|
205
|
-
* `openapi` — print (or `--out`-write) the emitted OpenAPI document for this surface. Builds the app around an
|
|
206
|
-
* all-optional stub {@link PhotonEngine} (`{}` — every route answers 501 under this stub, but the document itself only
|
|
207
|
-
* reflects the ROUTE TABLE, not handler behavior) so this NEVER boots the neural classifier or opens a gazetteer DB:
|
|
208
|
-
* pure route-table introspection, fast regardless of data-root state. `--flavor 3.0` prints the 3.0.3 diet instead of
|
|
209
|
-
* the default 3.1.0.
|
|
210
|
-
*/
|
|
211
|
-
function openapi(): void {
|
|
212
|
-
const app = createPhotonApp({})
|
|
213
|
-
|
|
214
|
-
printOpenAPIDocument(app, PHOTON_DOC_INFO, parseOpenAPIFlags(BINARY_NAME))
|
|
215
|
-
}
|
|
216
|
-
|
|
217
242
|
await runDropInCLI({
|
|
218
243
|
binaryName: BINARY_NAME,
|
|
219
|
-
openapi,
|
|
244
|
+
openapi: openAPICommand(BINARY_NAME, createPhotonApp, PHOTON_DOC_INFO, {}),
|
|
220
245
|
serve,
|
|
221
246
|
usage: [
|
|
222
247
|
" serve [--port 2322] [--host 0.0.0.0] [--candidate-db <path>] [--no-cors]",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* `schema.ts`.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
export * from "
|
|
23
|
-
export * from "
|
|
24
|
-
export * from "
|
|
25
|
-
export * from "
|
|
22
|
+
export * from "#app"
|
|
23
|
+
export * from "#engine"
|
|
24
|
+
export * from "#projection"
|
|
25
|
+
export * from "#schema"
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Reverse lookup over the `postcode-locality-*.db` artifacts: the CONTAINING postcode of a
|
|
7
|
+
* resolved locality, keyed by its WOF id — no name matching. Serves the drop-ins' answer
|
|
8
|
+
* enrichment: a village answer carries the postcode the gazetteer attests for it.
|
|
9
|
+
*
|
|
10
|
+
* The exactly-one rule is the abstention: a locality contained by several postcodes (any real
|
|
11
|
+
* city) gets NOTHING — emitting one of many would state a precision the evidence doesn't hold.
|
|
12
|
+
* Tolerate-and-degrade like every optional artifact: a machine without the DBs answers undefined
|
|
13
|
+
* everywhere and the consumer simply doesn't decorate.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { dataRootPath } from "@mailwoman/core/data-root"
|
|
17
|
+
import { pathExists } from "@mailwoman/core/fs/readers"
|
|
18
|
+
import { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
19
|
+
import type { PostcodeLocalityDatabase } from "mailwoman/gazetteer-pipeline/postcode-locality/schema"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The per-country artifacts, probed in caller-country order with `intl` as the shared fallback.
|
|
23
|
+
*/
|
|
24
|
+
const POSTCODE_LOCALITY_SUFFIXES = ["fr", "de", "jp", "intl"] as const
|
|
25
|
+
|
|
26
|
+
export type LocalityPostcodeLookup = (wofID: number, countryCode: string | null) => string | undefined
|
|
27
|
+
|
|
28
|
+
export async function createLocalityPostcodeLookup(): Promise<LocalityPostcodeLookup> {
|
|
29
|
+
const statements = new Map<string, ReturnType<DatabaseClient["prepare"]>>()
|
|
30
|
+
|
|
31
|
+
for (const suffix of POSTCODE_LOCALITY_SUFFIXES) {
|
|
32
|
+
const path = String(dataRootPath("wof", `postcode-locality-${suffix}.db`))
|
|
33
|
+
|
|
34
|
+
if (!(await pathExists(path))) continue
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
const db = new DatabaseClient<PostcodeLocalityDatabase>(path, { readOnly: true })
|
|
38
|
+
|
|
39
|
+
// No `is_containing` filter: villages routinely carry 0 (the builder's containment test is
|
|
40
|
+
// distance-classified, and a village near its postcode centroid still has exactly one code).
|
|
41
|
+
// The exactly-one DISTINCT rule below is the entire ambiguity guard.
|
|
42
|
+
statements.set(
|
|
43
|
+
suffix,
|
|
44
|
+
db.prepare(`SELECT DISTINCT postcode FROM postcode_locality WHERE locality_id = ? LIMIT 2`)
|
|
45
|
+
)
|
|
46
|
+
} catch {
|
|
47
|
+
// A torn or foreign file degrades to no enrichment, never a crash at serve start.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return (wofID, countryCode) => {
|
|
52
|
+
const cc = countryCode?.toLowerCase()
|
|
53
|
+
const order = cc && statements.has(cc) ? [cc, "intl"] : ["intl"]
|
|
54
|
+
|
|
55
|
+
for (const suffix of order) {
|
|
56
|
+
const stmt = statements.get(suffix)
|
|
57
|
+
|
|
58
|
+
if (!stmt) continue
|
|
59
|
+
|
|
60
|
+
const rows = stmt.all(wofID) as Array<{ postcode: string }>
|
|
61
|
+
|
|
62
|
+
if (rows.length === 1) return rows[0]!.postcode
|
|
63
|
+
|
|
64
|
+
if (rows.length > 1) return undefined // ambiguous — abstain, never guess
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return undefined
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
photonCollection,
|
|
18
18
|
photonFeature,
|
|
19
19
|
type PhotonProperties,
|
|
20
|
-
} from "
|
|
20
|
+
} from "#engine"
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* The resolved-place info a forward `/api` result carries: the admin ladder (MOST-SPECIFIC first) with GAZETTEER names,
|
|
@@ -76,6 +76,11 @@ const FORWARD_TAG_PROJECTION: Record<
|
|
|
76
76
|
dependent_locality: { key: "district", osmKey: "place", osmValue: "suburb", type: "district" },
|
|
77
77
|
borough: { key: "district", osmKey: "place", osmValue: "borough", type: "district" },
|
|
78
78
|
locality: { key: "city", osmKey: "place", osmValue: "city", type: "city" },
|
|
79
|
+
// The JP tiers the character-path model emits (#2164): a municipality is Photon's city, a prefecture its state, a
|
|
80
|
+
// district (大字 / 町名) its district — the same keys Nominatim assigns those ranks.
|
|
81
|
+
municipality: { key: "city", osmKey: "place", osmValue: "city", type: "city" },
|
|
82
|
+
prefecture: { key: "state", osmKey: "place", osmValue: "state", type: "state" },
|
|
83
|
+
district: { key: "district", osmKey: "place", osmValue: "suburb", type: "district" },
|
|
79
84
|
// WOF placetypes the /reverse hierarchy uses (so both endpoints derive osm tags from ONE table).
|
|
80
85
|
localadmin: { key: "city", osmKey: "place", osmValue: "city", type: "city" },
|
|
81
86
|
subregion: { key: "county", osmKey: "place", osmValue: "county", type: "county" },
|
|
@@ -9,12 +9,12 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { createRoute, type OpenAPIHono, z } from "@hono/zod-openapi"
|
|
12
|
-
import { legacyQuery } from "@mailwoman/api-kit"
|
|
13
|
-
import type {
|
|
12
|
+
import { asString, legacyQuery, withEngineStamp } from "@mailwoman/api-kit"
|
|
13
|
+
import type { EngineStamp } from "@mailwoman/core/license"
|
|
14
14
|
|
|
15
|
-
import type { PhotonEngine, PhotonFeatureCollection, PhotonReverseParams, PhotonSearchParams } from "
|
|
16
|
-
import { photonToSchemaOrg } from "
|
|
17
|
-
import { PhotonMessageCollectionSchema, PhotonResponseSchema, reverseQueryParams, searchQueryParams } from "
|
|
15
|
+
import type { PhotonEngine, PhotonFeatureCollection, PhotonReverseParams, PhotonSearchParams } from "#engine"
|
|
16
|
+
import { photonToSchemaOrg } from "#projection"
|
|
17
|
+
import { PhotonMessageCollectionSchema, PhotonResponseSchema, reverseQueryParams, searchQueryParams } from "#schema"
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Southern limit of latitude in WGS-84 degrees.
|
|
@@ -40,10 +40,6 @@ const DEFAULT_LIMIT = 15
|
|
|
40
40
|
|
|
41
41
|
const EMPTY: PhotonFeatureCollection = { type: "FeatureCollection", features: [] }
|
|
42
42
|
|
|
43
|
-
function asString(raw: unknown): string | undefined {
|
|
44
|
-
return typeof raw === "string" && raw.length ? raw : undefined
|
|
45
|
-
}
|
|
46
|
-
|
|
47
43
|
function asStringArray(raw: unknown): string[] | undefined {
|
|
48
44
|
if (Array.isArray(raw)) return raw.filter((v): v is string => typeof v === "string")
|
|
49
45
|
const s = asString(raw)
|
|
@@ -84,7 +80,7 @@ footer { margin-top: 2rem; font-size: .9rem; opacity: .8 }
|
|
|
84
80
|
<li><a class="q" href="/api?q=1600+pennsylvania+ave&limit=1">/api?q=1600+pennsylvania+ave&limit=1</a></li>
|
|
85
81
|
<li><a class="q" href="/reverse?lat=52.52&lon=13.405">/reverse?lat=52.52&lon=13.405</a></li>
|
|
86
82
|
</ul>
|
|
87
|
-
<footer><a href="https://mailwoman.
|
|
83
|
+
<footer><a href="https://mailwoman.ai/docs/developers/get-started/what-mailwoman-is">What Mailwoman is</a> · <a href="https://mailwoman.ai/demo">Live demo</a></footer>
|
|
88
84
|
</body>
|
|
89
85
|
</html>
|
|
90
86
|
`
|
|
@@ -136,7 +132,7 @@ const reverseRoute = createRoute({
|
|
|
136
132
|
/**
|
|
137
133
|
* Register the Photon-compatible routes against an injected engine.
|
|
138
134
|
*/
|
|
139
|
-
export function registerPhotonRoutes(app: OpenAPIHono, engine: PhotonEngine): void {
|
|
135
|
+
export function registerPhotonRoutes(app: OpenAPIHono, engine: PhotonEngine, stamp?: EngineStamp): void {
|
|
140
136
|
app.openapi(rootRoute, (c) => c.html(ROOT_HTML))
|
|
141
137
|
|
|
142
138
|
app.openapi(searchRoute, async (c) => {
|
|
@@ -158,15 +154,12 @@ export function registerPhotonRoutes(app: OpenAPIHono, engine: PhotonEngine): vo
|
|
|
158
154
|
|
|
159
155
|
const collection = await engine.search(params)
|
|
160
156
|
|
|
161
|
-
// #1052: `format=jsonld` re-serializes the SAME FeatureCollection as schema.org `Place[]` JSON-LD.
|
|
162
|
-
// declared 200 schema documents only the FeatureCollection shape (matching the legacy yaml); the
|
|
163
|
-
// jsonld branch's differing runtime shape is a local cast at the boundary,
|
|
164
|
-
// never a wire-behavior change (api-kit's `openapi.ts` sets the precedent for this idiom).
|
|
157
|
+
// #1052: `format=jsonld` re-serializes the SAME FeatureCollection as schema.org `Place[]` JSON-LD.
|
|
165
158
|
if (asString(q["format"]) === "jsonld") {
|
|
166
|
-
return c.json(photonToSchemaOrg(collection)
|
|
159
|
+
return c.json(photonToSchemaOrg(collection), 200)
|
|
167
160
|
}
|
|
168
161
|
|
|
169
|
-
return c.json(collection, 200)
|
|
162
|
+
return c.json(withEngineStamp(collection, stamp), 200)
|
|
170
163
|
})
|
|
171
164
|
|
|
172
165
|
app.openapi(reverseRoute, async (c) => {
|
|
@@ -193,12 +186,11 @@ export function registerPhotonRoutes(app: OpenAPIHono, engine: PhotonEngine): vo
|
|
|
193
186
|
|
|
194
187
|
const collection = await engine.reverse(params)
|
|
195
188
|
|
|
196
|
-
// #1052: `format=jsonld` re-serializes the reverse FeatureCollection as schema.org `Place[]` JSON-LD.
|
|
197
|
-
// the search handler's comment above for why this is a local cast, not a wire-behavior change.
|
|
189
|
+
// #1052: `format=jsonld` re-serializes the reverse FeatureCollection as schema.org `Place[]` JSON-LD.
|
|
198
190
|
if (asString(q["format"]) === "jsonld") {
|
|
199
|
-
return c.json(photonToSchemaOrg(collection)
|
|
191
|
+
return c.json(photonToSchemaOrg(collection), 200)
|
|
200
192
|
}
|
|
201
193
|
|
|
202
|
-
return c.json(collection, 200)
|
|
194
|
+
return c.json(withEngineStamp(collection, stamp), 200)
|
|
203
195
|
})
|
|
204
196
|
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { z } from "@hono/zod-openapi"
|
|
13
|
-
import { featureCollectionSchema, featureSchema } from "@mailwoman/api-kit"
|
|
13
|
+
import { featureCollectionSchema, featureSchema, stampedResponseSchema } from "@mailwoman/api-kit"
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Photon feature properties — OSM-derived keys; tolerant of extras (`[key: string]: unknown` on the wire type).
|
|
@@ -108,7 +108,7 @@ export const SchemaOrgPlaceSchema = z
|
|
|
108
108
|
* Doc-only; the wire behavior is unchanged.
|
|
109
109
|
*/
|
|
110
110
|
export const PhotonResponseSchema = z
|
|
111
|
-
.union([PhotonFeatureCollectionSchema, z.array(SchemaOrgPlaceSchema)])
|
|
111
|
+
.union([stampedResponseSchema(PhotonFeatureCollectionSchema), z.array(SchemaOrgPlaceSchema)])
|
|
112
112
|
.openapi("PhotonResponse")
|
|
113
113
|
|
|
114
114
|
/**
|
package/out/app.d.ts
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
10
10
|
import { type OpenAPIDocInfo } from "@mailwoman/api-kit";
|
|
11
|
-
import type {
|
|
11
|
+
import type { EngineStamp } from "@mailwoman/core/license";
|
|
12
|
+
import type { PhotonEngine } from "#engine";
|
|
12
13
|
/**
|
|
13
14
|
* Options for {@link createPhotonApp}.
|
|
14
15
|
*/
|
|
@@ -20,6 +21,12 @@ export interface PhotonAppOptions {
|
|
|
20
21
|
* without it (#1017). Set `false` when a reverse proxy already owns the CORS headers.
|
|
21
22
|
*/
|
|
22
23
|
cors?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* The engine stamp to carry on every response: `engine` as a foreign member of each FeatureCollection, and the
|
|
26
|
+
* `Server` + `Link: rel="license"` headers everywhere. Absent when an embedding application builds the app without
|
|
27
|
+
* the `mailwoman` package; the `photon` bin always passes one.
|
|
28
|
+
*/
|
|
29
|
+
engine?: EngineStamp;
|
|
23
30
|
}
|
|
24
31
|
/**
|
|
25
32
|
* The document info stamped into the emitted OpenAPI document. Exported (not inlined) so the CLI's `openapi` subcommand
|
package/out/app.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../app.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,
|
|
1
|
+
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../lib/app.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAoC,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAE1F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAI1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAW3C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC;;;;;OAKG;IACH,IAAI,CAAC,EAAE,OAAO,CAAA;IAEd;;;;OAIG;IACH,MAAM,CAAC,EAAE,WAAW,CAAA;CACpB;AAED;;;;GAIG;AACH,eAAO,MAAM,eAAe,EAAE,cAqB7B,CAAA;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,GAAE,gBAAqB,GAAG,WAAW,CAqBjG"}
|
package/out/app.js
CHANGED
|
@@ -7,10 +7,16 @@
|
|
|
7
7
|
* document. Engine-agnostic — the CLI wires the real engine; tests inject fixtures.
|
|
8
8
|
*/
|
|
9
9
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
10
|
-
import { attachOpenAPIDocs } from "@mailwoman/api-kit";
|
|
11
|
-
import
|
|
10
|
+
import { attachOpenAPIDocs, engineHeaders } from "@mailwoman/api-kit";
|
|
11
|
+
import { readLocalJSONFile } from "@mailwoman/core/fs/readers";
|
|
12
|
+
import { resolvePackagePath } from "@mailwoman/core/module/resolvers";
|
|
12
13
|
import { cors } from "hono/cors";
|
|
13
|
-
import { registerPhotonRoutes } from "
|
|
14
|
+
import { registerPhotonRoutes } from "#routes";
|
|
15
|
+
/**
|
|
16
|
+
* This package's own manifest, read at load rather than imported as a module: a JSON import makes `tsc` copy the file
|
|
17
|
+
* into `out/`, where it becomes the package scope for the compiled tree and breaks every `#` import in it.
|
|
18
|
+
*/
|
|
19
|
+
const packageJson = await readLocalJSONFile(resolvePackagePath("@mailwoman/photon", "package.json"));
|
|
14
20
|
/**
|
|
15
21
|
* The document info stamped into the emitted OpenAPI document. Exported (not inlined) so the CLI's `openapi` subcommand
|
|
16
22
|
* can call `emitOpenAPIDocuments` with the SAME info the mounted `/openapi.json` route (below, via
|
|
@@ -21,10 +27,10 @@ export const PHOTON_DOC_INFO = {
|
|
|
21
27
|
version: packageJson.version,
|
|
22
28
|
description: packageJson.description,
|
|
23
29
|
license: { name: "AGPL-3.0-only OR LicenseRef-Commercial", identifier: "AGPL-3.0-only" },
|
|
24
|
-
contact: { name: "Sister Software", url: "https://mailwoman.
|
|
30
|
+
contact: { name: "Sister Software", url: "https://mailwoman.ai" },
|
|
25
31
|
externalDocs: {
|
|
26
32
|
description: "What Mailwoman is",
|
|
27
|
-
url: "https://mailwoman.
|
|
33
|
+
url: "https://mailwoman.ai/docs/developers/get-started/what-mailwoman-is",
|
|
28
34
|
},
|
|
29
35
|
servers: [
|
|
30
36
|
{
|
|
@@ -48,10 +54,13 @@ export function createPhotonApp(engine, options = {}) {
|
|
|
48
54
|
if (options.cors !== false) {
|
|
49
55
|
app.use(cors({ origin: "*", allowMethods: ["GET", "OPTIONS"], allowHeaders: ["*"], maxAge: 86_400 }));
|
|
50
56
|
}
|
|
57
|
+
if (options.engine) {
|
|
58
|
+
app.use(engineHeaders(options.engine));
|
|
59
|
+
}
|
|
51
60
|
// Safety net: malformed input or an engine fault returns an empty FeatureCollection, never a crash (photon's
|
|
52
61
|
// envelope — NOT `{error}`, which is the libpostal/nominatim shape).
|
|
53
62
|
app.onError((_error, c) => c.json({ type: "FeatureCollection", features: [], message: "internal error" }, 500));
|
|
54
|
-
registerPhotonRoutes(app, engine);
|
|
63
|
+
registerPhotonRoutes(app, engine, options.engine);
|
|
55
64
|
attachOpenAPIDocs(app, PHOTON_DOC_INFO);
|
|
56
65
|
return app;
|
|
57
66
|
}
|
package/out/app.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.js","sourceRoot":"","sources":["../app.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAuB,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../lib/app.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAuB,MAAM,oBAAoB,CAAA;AAC1F,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAE9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAA;AACrE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAGhC,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAE9C;;;GAGG;AACH,MAAM,WAAW,GAAG,MAAM,iBAAiB,CAC1C,kBAAkB,CAAC,mBAAmB,EAAE,cAAc,CAAC,CACvD,CAAA;AAsBD;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAmB;IAC9C,KAAK,EAAE,WAAW,CAAC,IAAI;IACvB,OAAO,EAAE,WAAW,CAAC,OAAO;IAC5B,WAAW,EAAE,WAAW,CAAC,WAAW;IACpC,OAAO,EAAE,EAAE,IAAI,EAAE,wCAAwC,EAAE,UAAU,EAAE,eAAe,EAAE;IACxF,OAAO,EAAE,EAAE,IAAI,EAAE,iBAAiB,EAAE,GAAG,EAAE,sBAAsB,EAAE;IACjE,YAAY,EAAE;QACb,WAAW,EAAE,mBAAmB;QAChC,GAAG,EAAE,oEAAoE;KACzE;IACD,OAAO,EAAE;QACR;YACC,GAAG,EAAE,sBAAsB;YAC3B,SAAS,EAAE,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,WAAW,EAAE,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE;SACxE;KACD;IACD,QAAQ,EAAE,EAAE;IACZ,IAAI,EAAE;QACL,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,6CAA6C,EAAE;QACjF,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,oCAAoC,EAAE;KACnE;CACD,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,MAAoB,EAAE,UAA4B,EAAE;IACnF,MAAM,GAAG,GAAG,IAAI,WAAW,EAAE,CAAA;IAE7B,gHAAgH;IAChH,6GAA6G;IAC7G,IAAI,OAAO,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;QAC5B,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAA;IACtG,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QACpB,GAAG,CAAC,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAA;IACvC,CAAC;IAED,6GAA6G;IAC7G,qEAAqE;IACrE,GAAG,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,QAAQ,EAAE,EAAE,EAAE,OAAO,EAAE,gBAAgB,EAAE,EAAE,GAAG,CAAC,CAAC,CAAA;IAE/G,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;IACjD,iBAAiB,CAAC,GAAG,EAAE,eAAe,CAAC,CAAA;IAEvC,OAAO,GAAG,CAAA;AACX,CAAC"}
|
package/out/cli.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../cli.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;GAaG"}
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../lib/cli.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;GAaG"}
|