@mailwoman/core 10.8.0 → 11.0.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/lib/cloudflare/tokens.ts +141 -0
- package/lib/cloudflare/wrangler.ts +47 -0
- package/lib/cloudflare.ts +10 -0
- package/lib/geocode/result.ts +6 -0
- package/lib/network/connectivity.ts +112 -0
- package/lib/network/reachability.ts +90 -0
- package/lib/pipeline/client-result.ts +0 -9
- package/lib/resolver/types.ts +76 -47
- package/lib/resolver.ts +11 -3
- package/lib/scripting/arguments.ts +1 -1
- package/lib/scripting/operation.ts +9 -0
- package/out/cloudflare/tokens.d.ts +69 -0
- package/out/cloudflare/tokens.d.ts.map +1 -0
- package/out/cloudflare/tokens.js +80 -0
- package/out/cloudflare/tokens.js.map +1 -0
- package/out/cloudflare/wrangler.d.ts +24 -0
- package/out/cloudflare/wrangler.d.ts.map +1 -0
- package/out/cloudflare/wrangler.js +31 -0
- package/out/cloudflare/wrangler.js.map +1 -0
- package/out/cloudflare.d.ts +10 -0
- package/out/cloudflare.d.ts.map +1 -0
- package/out/cloudflare.js +10 -0
- package/out/cloudflare.js.map +1 -0
- package/out/geocode/result.d.ts +2 -0
- package/out/geocode/result.d.ts.map +1 -1
- package/out/geocode/result.js +6 -0
- package/out/geocode/result.js.map +1 -1
- package/out/network/connectivity.d.ts +67 -0
- package/out/network/connectivity.d.ts.map +1 -0
- package/out/network/connectivity.js +71 -0
- package/out/network/connectivity.js.map +1 -0
- package/out/network/reachability.d.ts +48 -0
- package/out/network/reachability.d.ts.map +1 -0
- package/out/network/reachability.js +80 -0
- package/out/network/reachability.js.map +1 -0
- package/out/pipeline/client-result.d.ts +0 -8
- package/out/pipeline/client-result.d.ts.map +1 -1
- package/out/resolver/types.d.ts +67 -46
- package/out/resolver/types.d.ts.map +1 -1
- package/out/resolver/types.js +13 -1
- package/out/resolver/types.js.map +1 -1
- package/out/resolver.d.ts +2 -2
- package/out/resolver.d.ts.map +1 -1
- package/out/resolver.js +1 -1
- package/out/resolver.js.map +1 -1
- package/out/scripting/arguments.d.ts +1 -1
- package/out/scripting/arguments.d.ts.map +1 -1
- package/out/scripting/operation.d.ts +8 -0
- package/out/scripting/operation.d.ts.map +1 -1
- package/out/scripting/operation.js.map +1 -1
- package/package.json +64 -36
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file Builds Cloudflare API token requests and the dashboard links that create the same tokens by hand.
|
|
6
|
+
* A permission is declared once with both spellings Cloudflare uses for it: the permission group name the
|
|
7
|
+
* API lists, and the short key a dashboard template link takes.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { stringifyJSON } from "#json"
|
|
11
|
+
|
|
12
|
+
const DASHBOARD = "https://dash.cloudflare.com/"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* One permission as the API names it and as a dashboard template link keys it.
|
|
16
|
+
*/
|
|
17
|
+
export interface CloudflarePermission {
|
|
18
|
+
/**
|
|
19
|
+
* The permission group's name in `GET /accounts/:id/tokens/permission_groups`.
|
|
20
|
+
*
|
|
21
|
+
* The API spells write access "Write" and the dashboard spells it "Edit".
|
|
22
|
+
* {@linkcode resolvePermissionGroups} matches either.
|
|
23
|
+
*/
|
|
24
|
+
group: string
|
|
25
|
+
/**
|
|
26
|
+
* The `permissionGroupKeys` entry of a dashboard template link.
|
|
27
|
+
*/
|
|
28
|
+
key: string
|
|
29
|
+
type: "read" | "edit" | "purge"
|
|
30
|
+
scope: "account" | "zone"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A permission group as `GET /accounts/:id/tokens/permission_groups` returns it.
|
|
35
|
+
*/
|
|
36
|
+
export interface PermissionGroup {
|
|
37
|
+
id: string
|
|
38
|
+
name: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TokenPolicy {
|
|
42
|
+
effect: "allow"
|
|
43
|
+
resources: Record<string, string>
|
|
44
|
+
permission_groups: PermissionGroup[]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface TokenRequest {
|
|
48
|
+
name: string
|
|
49
|
+
policies: TokenPolicy[]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A dashboard link that opens the account-owned token form with `permissions` and `name` filled in.
|
|
54
|
+
*
|
|
55
|
+
* The form asks which account when the viewer belongs to several.
|
|
56
|
+
* A template link cannot preset a zone, so a token with a zone permission asks the viewer to pick the zone.
|
|
57
|
+
*/
|
|
58
|
+
export function tokenTemplateLink(name: string, permissions: readonly CloudflarePermission[]): string {
|
|
59
|
+
const keys = permissions.map((permission) => ({ key: permission.key, type: permission.type }))
|
|
60
|
+
const query = new URLSearchParams({ permissionGroupKeys: stringifyJSON(keys), name })
|
|
61
|
+
|
|
62
|
+
// The dashboard reads `to` unencoded, as Cloudflare's own template links write it.
|
|
63
|
+
return `${DASHBOARD}?to=/:account/api-tokens&${query.toString().replaceAll("+", "%20")}`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function groupNameKey(name: string): string {
|
|
67
|
+
return name.replace(/ (Write|Edit)$/u, " write").toLowerCase()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The permission group of each of `permissions`, matched by name.
|
|
72
|
+
*
|
|
73
|
+
* Throws naming every permission without a group, so a renamed group stops a mint before any token exists.
|
|
74
|
+
*/
|
|
75
|
+
export function resolvePermissionGroups(
|
|
76
|
+
permissions: readonly CloudflarePermission[],
|
|
77
|
+
groups: readonly PermissionGroup[]
|
|
78
|
+
): Map<CloudflarePermission, PermissionGroup> {
|
|
79
|
+
const byName = new Map(groups.map((group) => [groupNameKey(group.name), group]))
|
|
80
|
+
const resolved = new Map<CloudflarePermission, PermissionGroup>()
|
|
81
|
+
const missing: string[] = []
|
|
82
|
+
|
|
83
|
+
for (const permission of permissions) {
|
|
84
|
+
const group = byName.get(groupNameKey(permission.group))
|
|
85
|
+
|
|
86
|
+
if (group) {
|
|
87
|
+
resolved.set(permission, group)
|
|
88
|
+
} else {
|
|
89
|
+
missing.push(permission.group)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (missing.length) {
|
|
94
|
+
throw new Error(`Cloudflare lists no permission group named ${missing.map((name) => `"${name}"`).join(", ")}`)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return resolved
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The request body that creates a token holding `permissions`.
|
|
102
|
+
*
|
|
103
|
+
* Account permissions apply to `accountID` and zone permissions to `zoneID`, one policy each.
|
|
104
|
+
* A token with no permission of a scope gets no policy for it.
|
|
105
|
+
*/
|
|
106
|
+
export function tokenRequestBody(options: {
|
|
107
|
+
name: string
|
|
108
|
+
permissions: readonly CloudflarePermission[]
|
|
109
|
+
groups: Map<CloudflarePermission, PermissionGroup>
|
|
110
|
+
accountID: string
|
|
111
|
+
zoneID?: string
|
|
112
|
+
}): TokenRequest {
|
|
113
|
+
const { name, permissions, groups, accountID, zoneID } = options
|
|
114
|
+
|
|
115
|
+
if (!zoneID && permissions.some((permission) => permission.scope === "zone")) {
|
|
116
|
+
throw new Error(`token "${name}" holds a zone permission and no zone was given`)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const resources = {
|
|
120
|
+
account: { [`com.cloudflare.api.account.${accountID}`]: "*" },
|
|
121
|
+
zone: { [`com.cloudflare.api.account.zone.${zoneID}`]: "*" },
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const policies = (["account", "zone"] as const)
|
|
125
|
+
.map((scope) => ({
|
|
126
|
+
effect: "allow" as const,
|
|
127
|
+
resources: resources[scope],
|
|
128
|
+
permission_groups: permissions
|
|
129
|
+
.filter((permission) => permission.scope === scope)
|
|
130
|
+
.map((permission) => {
|
|
131
|
+
const group = groups.get(permission)
|
|
132
|
+
|
|
133
|
+
if (!group) throw new Error(`no permission group resolved for "${permission.group}"`)
|
|
134
|
+
|
|
135
|
+
return { id: group.id, name: group.name }
|
|
136
|
+
}),
|
|
137
|
+
}))
|
|
138
|
+
.filter((policy) => policy.permission_groups.length)
|
|
139
|
+
|
|
140
|
+
return { name, policies }
|
|
141
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* R2 uploads through `wrangler r2 object put`. Wrangler reads `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID`
|
|
7
|
+
* from the environment. `wrangler` is an optional peer dependency of this package, so a caller installs it.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { resolvePath } from "path-ts"
|
|
11
|
+
|
|
12
|
+
import { statPath } from "#fs/readers/stat"
|
|
13
|
+
import { resolvePackageDirectory } from "#module/resolvers"
|
|
14
|
+
import { runFile } from "#process"
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The largest object `wrangler r2 object put` accepts.
|
|
18
|
+
* A larger file goes through rclone.
|
|
19
|
+
*/
|
|
20
|
+
export const WRANGLER_OBJECT_CAP_BYTES = 300 * 1024 * 1024
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Put one local file at `bucket/key` on R2.
|
|
24
|
+
*
|
|
25
|
+
* Throws before running wrangler when the file exceeds {@linkcode WRANGLER_OBJECT_CAP_BYTES}.
|
|
26
|
+
*/
|
|
27
|
+
export async function putR2ObjectWithWrangler({
|
|
28
|
+
file,
|
|
29
|
+
bucket,
|
|
30
|
+
key,
|
|
31
|
+
}: {
|
|
32
|
+
file: string
|
|
33
|
+
bucket: string
|
|
34
|
+
key: string
|
|
35
|
+
}): Promise<void> {
|
|
36
|
+
const { size } = await statPath(file)
|
|
37
|
+
|
|
38
|
+
if (size > WRANGLER_OBJECT_CAP_BYTES) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`${file} is ${size.toLocaleString()} bytes; wrangler puts objects up to ${WRANGLER_OBJECT_CAP_BYTES.toLocaleString()}, so publish it through rclone`
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const bin = resolvePath(resolvePackageDirectory("wrangler"), "bin", "wrangler.js")
|
|
45
|
+
|
|
46
|
+
await runFile("node", [bin, "r2", "object", "put", `${bucket}/${key}`, "--file", file, "--remote"])
|
|
47
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Cloudflare token request builders and the wrangler R2 upload. API calls go through the `cloudflare` SDK.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export * from "#cloudflare/tokens"
|
|
10
|
+
export * from "#cloudflare/wrangler"
|
package/lib/geocode/result.ts
CHANGED
|
@@ -69,6 +69,12 @@ export const GeocodeCandidateSchema = z
|
|
|
69
69
|
* The field is null when the resolver recorded no score for the candidate.
|
|
70
70
|
*/
|
|
71
71
|
score: z.number().nullable(),
|
|
72
|
+
/**
|
|
73
|
+
* True when the input matched an alias of this place, such as a former name, and differs from `name`.
|
|
74
|
+
*
|
|
75
|
+
* The field is absent when the input matched the primary name.
|
|
76
|
+
*/
|
|
77
|
+
matchedAlias: z.literal(true).optional(),
|
|
72
78
|
})
|
|
73
79
|
.meta({ id: "GeocodeCandidate", description: "A ranked candidate place." })
|
|
74
80
|
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file The two connectivity signals a browser client reads before it sends a request, and the error a client raises
|
|
6
|
+
* when they say the request cannot reach its service.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { StatusCodes } from "http-status-codes"
|
|
10
|
+
|
|
11
|
+
import { ResourceError } from "#errors/schema"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* What a client knows about its connection, from two independent sources.
|
|
15
|
+
*
|
|
16
|
+
* `null` means the source is unavailable or has not reported yet.
|
|
17
|
+
* A reading of `false` means the source reported a failure.
|
|
18
|
+
*/
|
|
19
|
+
export interface ConnectivityReading {
|
|
20
|
+
/**
|
|
21
|
+
* `navigator.onLine`: `false` when the device has no network interface up.
|
|
22
|
+
*
|
|
23
|
+
* `true` reports only that an interface is up.
|
|
24
|
+
* A captive portal or a network with no route out still reads `true`.
|
|
25
|
+
*/
|
|
26
|
+
online: boolean | null
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Whether the latest reachability probe of the service succeeded.
|
|
30
|
+
*/
|
|
31
|
+
reachable: boolean | null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The reading a client assumes before either source reports: both signals unknown.
|
|
36
|
+
*/
|
|
37
|
+
export const UNKNOWN_CONNECTIVITY: ConnectivityReading = { online: null, reachable: null }
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* `navigator.onLine`, or `null` on a runtime whose `navigator` lacks it.
|
|
41
|
+
*
|
|
42
|
+
* Node 21 and later define `navigator` without `onLine`, and workerd's `navigator` lacks
|
|
43
|
+
* it too, so the absent property reads as unknown there rather than as offline.
|
|
44
|
+
*/
|
|
45
|
+
export function readNavigatorOnline(): boolean | null {
|
|
46
|
+
const navigatorLike = (globalThis as { navigator?: { onLine?: unknown } }).navigator
|
|
47
|
+
const online = navigatorLike?.onLine
|
|
48
|
+
|
|
49
|
+
return typeof online === "boolean" ? online : null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Why a reading rules out a request to its service.
|
|
54
|
+
*/
|
|
55
|
+
export const OfflineReason = {
|
|
56
|
+
/**
|
|
57
|
+
* The device reports no network interface up.
|
|
58
|
+
*/
|
|
59
|
+
Offline: "offline",
|
|
60
|
+
/**
|
|
61
|
+
* The device has a network, and the service's reachability probe failed.
|
|
62
|
+
*/
|
|
63
|
+
Unreachable: "service-unreachable",
|
|
64
|
+
} as const
|
|
65
|
+
|
|
66
|
+
export type OfflineReason = (typeof OfflineReason)[keyof typeof OfflineReason]
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The reason a reading rules out a request, or `null` when the reading allows one or is unknown.
|
|
70
|
+
*/
|
|
71
|
+
export function offlineReason(reading: ConnectivityReading): OfflineReason | null {
|
|
72
|
+
if (reading.online === false) return OfflineReason.Offline
|
|
73
|
+
|
|
74
|
+
if (reading.reachable === false) return OfflineReason.Unreachable
|
|
75
|
+
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The URN source segment of an error {@linkcode offlineResourceError} builds.
|
|
81
|
+
*/
|
|
82
|
+
const CONNECTIVITY_ERROR_SOURCE = "connectivity"
|
|
83
|
+
|
|
84
|
+
const OFFLINE_MESSAGES: Record<OfflineReason, string> = {
|
|
85
|
+
[OfflineReason.Offline]: "This device is offline.",
|
|
86
|
+
[OfflineReason.Unreachable]: "This device has a network connection, but the service did not answer.",
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A 503 {@linkcode ResourceError} of kind `network` for a request that a connectivity reading ruled out.
|
|
91
|
+
*
|
|
92
|
+
* The `network` kind segment matches `ResourceErrorKind.Network`, the kind `APIClient`
|
|
93
|
+
* assigns a responseless failure, so `isTransientResourceError` reports it as transient
|
|
94
|
+
* and a caller that requeues network failures requeues this one.
|
|
95
|
+
* The URN is `connectivity:network:<reason>`, and `cause` holds the failure
|
|
96
|
+
* that prompted the check, if one did.
|
|
97
|
+
*/
|
|
98
|
+
export function offlineResourceError(reason: OfflineReason, cause?: unknown): ResourceError {
|
|
99
|
+
const error = ResourceError.from(
|
|
100
|
+
503 as StatusCodes,
|
|
101
|
+
OFFLINE_MESSAGES[reason],
|
|
102
|
+
CONNECTIVITY_ERROR_SOURCE,
|
|
103
|
+
"network",
|
|
104
|
+
reason
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
if (cause != null) {
|
|
108
|
+
error.cause = cause
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return error
|
|
112
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file Tests whether a service answers by requesting one of its routes.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The milliseconds {@linkcode ReachabilityProbe.test} waits for a response
|
|
10
|
+
* before it reports the service unreachable.
|
|
11
|
+
*/
|
|
12
|
+
export const DEFAULT_REACHABILITY_TIMEOUT_MS = 5000
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A probe that tests whether a service answers, by requesting a route on it.
|
|
16
|
+
*
|
|
17
|
+
* Probe the service the caller depends on, such as the geocoder API's `/health` route,
|
|
18
|
+
* rather than a third party's connectivity endpoint.
|
|
19
|
+
* A third-party endpoint reports whether the internet is reachable,
|
|
20
|
+
* and the service can be down while it answers.
|
|
21
|
+
*
|
|
22
|
+
* Each probe of a third-party endpoint also sends the user's IP address to that party.
|
|
23
|
+
*
|
|
24
|
+
* The request runs in `cors` mode, so the status is readable.
|
|
25
|
+
* A captive portal's interception of an HTTPS request fails the TLS handshake,
|
|
26
|
+
* and the probe reports that as unreachable.
|
|
27
|
+
*/
|
|
28
|
+
export class ReachabilityProbe implements Disposable {
|
|
29
|
+
#abortController: AbortController | null = null
|
|
30
|
+
protected readonly url: string
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param url The route to request.
|
|
34
|
+
* It must answer CORS requests from the page's origin.
|
|
35
|
+
*/
|
|
36
|
+
constructor(url: string | URL) {
|
|
37
|
+
this.url = String(url)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public [Symbol.dispose] = (): void => this.abort()
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Cancels the test in flight.
|
|
44
|
+
* The cancelled test resolves `false`.
|
|
45
|
+
*/
|
|
46
|
+
public abort = (): void => {
|
|
47
|
+
this.#abortController?.abort()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Resolves `true` when the route answers with a 2xx status, and `false` on any
|
|
52
|
+
* other status, a network failure or a timeout.
|
|
53
|
+
*
|
|
54
|
+
* A new test cancels the previous one.
|
|
55
|
+
*/
|
|
56
|
+
public test = (requestTimeout = DEFAULT_REACHABILITY_TIMEOUT_MS, requestInit?: RequestInit): Promise<boolean> => {
|
|
57
|
+
this.abort()
|
|
58
|
+
|
|
59
|
+
const controller = new AbortController()
|
|
60
|
+
this.#abortController = controller
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
// `AbortSignal.any` and `fetch` can throw synchronously, and the chain turns that into `false`.
|
|
64
|
+
Promise.resolve()
|
|
65
|
+
.then(() => {
|
|
66
|
+
const signals = [controller.signal, AbortSignal.timeout(requestTimeout)]
|
|
67
|
+
|
|
68
|
+
if (requestInit?.signal) {
|
|
69
|
+
signals.push(requestInit.signal)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return fetch(this.url, {
|
|
73
|
+
method: "GET",
|
|
74
|
+
credentials: "omit",
|
|
75
|
+
mode: "cors",
|
|
76
|
+
cache: "no-store",
|
|
77
|
+
...requestInit,
|
|
78
|
+
signal: AbortSignal.any(signals),
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
.then((response) => response.ok)
|
|
82
|
+
.catch(() => false)
|
|
83
|
+
.finally(() => {
|
|
84
|
+
if (this.#abortController === controller) {
|
|
85
|
+
this.#abortController = null
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -39,14 +39,6 @@ export interface ResolvedPlaceView {
|
|
|
39
39
|
uncertaintyM: number | null
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
export interface DualRoleView {
|
|
43
|
-
id: number
|
|
44
|
-
name: string
|
|
45
|
-
placetype: string
|
|
46
|
-
relationshipType: string
|
|
47
|
-
role: string
|
|
48
|
-
}
|
|
49
|
-
|
|
50
42
|
export interface StageTiming {
|
|
51
43
|
shape: number
|
|
52
44
|
classify: number
|
|
@@ -87,5 +79,4 @@ export interface ParseResult {
|
|
|
87
79
|
candidates: ResolvedPlaceView[]
|
|
88
80
|
fstActive: boolean
|
|
89
81
|
fstProvenance: FSTProvenance | null
|
|
90
|
-
dualRoles: DualRoleView[] | null
|
|
91
82
|
}
|
package/lib/resolver/types.ts
CHANGED
|
@@ -92,6 +92,10 @@ export interface ResolvedPlace {
|
|
|
92
92
|
* Optional marker for variant-alias exemption.
|
|
93
93
|
*/
|
|
94
94
|
variantAliasExempted?: true | null
|
|
95
|
+
/**
|
|
96
|
+
* True when the input matched an alias of this place and differs from its primary name.
|
|
97
|
+
*/
|
|
98
|
+
matchedAlias?: true | null
|
|
95
99
|
/**
|
|
96
100
|
* Optional stored extent.
|
|
97
101
|
* A point-only row stores its coordinate as both corners.
|
|
@@ -156,10 +160,6 @@ export interface ResolverBackend {
|
|
|
156
160
|
regionQualifier?: string
|
|
157
161
|
limit?: number
|
|
158
162
|
}): Promise<ResolvedPlace[]>
|
|
159
|
-
/**
|
|
160
|
-
* Optional coincident localities lookup.
|
|
161
|
-
*/
|
|
162
|
-
coincidentLocalitiesFor?(adminID: number | string): CoincidentLocality[]
|
|
163
163
|
/**
|
|
164
164
|
* Optional ancestor lineage lookup.
|
|
165
165
|
*/
|
|
@@ -170,54 +170,75 @@ export interface ResolverBackend {
|
|
|
170
170
|
artifactCoverage?: GazetteerArtifactCoverage | null
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
export interface
|
|
173
|
+
export interface Ancestor {
|
|
174
|
+
id: number | string
|
|
175
|
+
placetype: string
|
|
176
|
+
name: string
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* A reading used for weak-resolution span-rescore behavior.
|
|
181
|
+
*/
|
|
182
|
+
export type WeakResolutionReading = "score" | "containment" | "either"
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* What a landmark row is: a place the layer answers, or a settlement the admin ladder answers.
|
|
186
|
+
*/
|
|
187
|
+
export type LandmarkKind = "venue" | "settlement"
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* One place a landmark name belongs to.
|
|
191
|
+
*/
|
|
192
|
+
export interface LandmarkHit {
|
|
174
193
|
/**
|
|
175
|
-
*
|
|
194
|
+
* The Wikidata item.
|
|
176
195
|
*/
|
|
177
|
-
|
|
196
|
+
qid: string
|
|
197
|
+
name: string
|
|
198
|
+
kind: LandmarkKind
|
|
199
|
+
country: string | null
|
|
200
|
+
lat: number
|
|
201
|
+
lon: number
|
|
178
202
|
/**
|
|
179
|
-
*
|
|
203
|
+
* The number of Wikipedia language editions that describe the place.
|
|
180
204
|
*/
|
|
181
|
-
|
|
205
|
+
sitelinks: number
|
|
182
206
|
/**
|
|
183
|
-
* Whether the
|
|
207
|
+
* Whether the name is the place's own English or local name, rather than a translation or alias.
|
|
184
208
|
*/
|
|
185
|
-
|
|
209
|
+
primary: boolean
|
|
186
210
|
/**
|
|
187
|
-
*
|
|
211
|
+
* Whether the input wrote an identifier code of the place, such as `DTW`, in its own case.
|
|
188
212
|
*/
|
|
189
|
-
|
|
213
|
+
code: boolean
|
|
190
214
|
/**
|
|
191
|
-
*
|
|
215
|
+
* The importance Photon's index holds for an OpenStreetMap place, in place of a sitelink count.
|
|
192
216
|
*/
|
|
193
|
-
|
|
217
|
+
importance?: number | null
|
|
194
218
|
}
|
|
195
219
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
220
|
+
/**
|
|
221
|
+
* The stable ID prefix of a landmark that came from OpenStreetMap, as in `osm:W427818536`.
|
|
222
|
+
*/
|
|
223
|
+
export const OSM_LANDMARK_PREFIX = "osm:"
|
|
201
224
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Centroid distance in km.
|
|
213
|
-
*/
|
|
214
|
-
distanceKm: number
|
|
225
|
+
/**
|
|
226
|
+
* Returns a landmark's place ID: `openstreetmap:W427818536` or `wikidata:Q160409`.
|
|
227
|
+
*/
|
|
228
|
+
export function landmarkPlaceID(id: string | number): string {
|
|
229
|
+
const value = String(id)
|
|
230
|
+
|
|
231
|
+
return value.startsWith(OSM_LANDMARK_PREFIX)
|
|
232
|
+
? `openstreetmap:${value.slice(OSM_LANDMARK_PREFIX.length)}`
|
|
233
|
+
: `wikidata:${value}`
|
|
215
234
|
}
|
|
216
235
|
|
|
217
236
|
/**
|
|
218
|
-
*
|
|
237
|
+
* Finds the places a name belongs to, primary names first, then most sitelinks first.
|
|
219
238
|
*/
|
|
220
|
-
export
|
|
239
|
+
export interface LandmarkLookup {
|
|
240
|
+
find(name: string): LandmarkHit[] | Promise<LandmarkHit[]>
|
|
241
|
+
}
|
|
221
242
|
|
|
222
243
|
/**
|
|
223
244
|
* Who chose a default country scope.
|
|
@@ -257,7 +278,6 @@ export interface ResolveSwitches {
|
|
|
257
278
|
postcodeContainmentCoherence: boolean
|
|
258
279
|
postcodePrefixPrior: boolean
|
|
259
280
|
adminCoherence: boolean
|
|
260
|
-
hierarchyCompletion: boolean
|
|
261
281
|
includeAncestors: boolean
|
|
262
282
|
adminContainmentRerank: boolean
|
|
263
283
|
dependentLocalityContainment: boolean
|
|
@@ -279,7 +299,6 @@ export const RESOLVE_SWITCH_DEFAULTS: Readonly<ResolveSwitches> = {
|
|
|
279
299
|
postcodeContainmentCoherence: false,
|
|
280
300
|
postcodePrefixPrior: false,
|
|
281
301
|
adminCoherence: true,
|
|
282
|
-
hierarchyCompletion: true,
|
|
283
302
|
includeAncestors: false,
|
|
284
303
|
adminContainmentRerank: true,
|
|
285
304
|
dependentLocalityContainment: true,
|
|
@@ -403,6 +422,26 @@ export interface ResolveOpts {
|
|
|
403
422
|
* @defaultValue {@linkcode RESOLVE_SWITCH_DEFAULTS}
|
|
404
423
|
*/
|
|
405
424
|
spanRescoreRequireContextRemainder?: boolean
|
|
425
|
+
/**
|
|
426
|
+
* The landmark layer.
|
|
427
|
+
*
|
|
428
|
+
* It answers an input that is a place's name with no street or postcode.
|
|
429
|
+
*/
|
|
430
|
+
landmarks?: LandmarkLookup
|
|
431
|
+
/**
|
|
432
|
+
* The OpenStreetMap place layer, read after span rescore with the same rules as the landmark layer.
|
|
433
|
+
*/
|
|
434
|
+
osmPlaces?: LandmarkLookup
|
|
435
|
+
/**
|
|
436
|
+
* The input as the caller wrote it, because the parse may fold its case
|
|
437
|
+
* and a code such as `DTW` needs that case.
|
|
438
|
+
*/
|
|
439
|
+
writtenInput?: string
|
|
440
|
+
/**
|
|
441
|
+
* Returns whether a word is a POI category, so span-rescore does not re-read
|
|
442
|
+
* a venue's last word as a locality.
|
|
443
|
+
*/
|
|
444
|
+
isVenueCategoryWord?: (word: string) => boolean
|
|
406
445
|
/**
|
|
407
446
|
* Weak-resolution reading used by span-rescore.
|
|
408
447
|
*/
|
|
@@ -469,12 +508,6 @@ export interface ResolveOpts {
|
|
|
469
508
|
* @defaultValue {@linkcode RESOLVE_SWITCH_DEFAULTS}
|
|
470
509
|
*/
|
|
471
510
|
adminCoherence?: boolean
|
|
472
|
-
/**
|
|
473
|
-
* Enables dual-role hierarchy completion.
|
|
474
|
-
*
|
|
475
|
-
* @defaultValue {@linkcode RESOLVE_SWITCH_DEFAULTS}
|
|
476
|
-
*/
|
|
477
|
-
hierarchyCompletion?: boolean
|
|
478
511
|
/**
|
|
479
512
|
* Attach ancestor lineage to resolved nodes.
|
|
480
513
|
*
|
|
@@ -589,8 +622,4 @@ export interface Resolver {
|
|
|
589
622
|
* Optional artifact coverage facts passthrough.
|
|
590
623
|
*/
|
|
591
624
|
artifactCoverage: GazetteerArtifactCoverage | null
|
|
592
|
-
/**
|
|
593
|
-
* Optional list of missing backend capabilities.
|
|
594
|
-
*/
|
|
595
|
-
capabilityGaps: readonly BackendCapabilityGap[] | null
|
|
596
625
|
}
|
package/lib/resolver.ts
CHANGED
|
@@ -34,13 +34,18 @@ export type {
|
|
|
34
34
|
|
|
35
35
|
export type { RegionDatabaseProvider, RegionDatabases } from "#resolver/region-database-provider"
|
|
36
36
|
|
|
37
|
-
export {
|
|
37
|
+
export {
|
|
38
|
+
EMPTY_PLACE_FIELDS,
|
|
39
|
+
landmarkPlaceID,
|
|
40
|
+
OSM_LANDMARK_PREFIX,
|
|
41
|
+
RESOLVE_SWITCH_DEFAULTS,
|
|
42
|
+
resolveSwitches,
|
|
43
|
+
} from "#resolver/types"
|
|
44
|
+
|
|
38
45
|
export { hardCountrySafelistFromCoverage } from "#resolver/coverage-facts"
|
|
39
46
|
|
|
40
47
|
export type {
|
|
41
48
|
Ancestor,
|
|
42
|
-
BackendCapabilityGap,
|
|
43
|
-
CoincidentLocality,
|
|
44
49
|
ResolveCandidateTrace,
|
|
45
50
|
ResolveNodeTrace,
|
|
46
51
|
ResolveOpts,
|
|
@@ -51,6 +56,9 @@ export type {
|
|
|
51
56
|
Resolver,
|
|
52
57
|
ResolverBackend,
|
|
53
58
|
WeakResolutionReading,
|
|
59
|
+
LandmarkHit,
|
|
60
|
+
LandmarkKind,
|
|
61
|
+
LandmarkLookup,
|
|
54
62
|
} from "#resolver/types"
|
|
55
63
|
|
|
56
64
|
export type {
|
|
@@ -13,7 +13,7 @@ import { parseArgs, type ParseArgsConfig } from "node:util"
|
|
|
13
13
|
|
|
14
14
|
import { TextSpliterator } from "spliterator"
|
|
15
15
|
|
|
16
|
-
export type { ParseArgsConfig } from "node:util"
|
|
16
|
+
export type { ParseArgsConfig, ParseArgsOptionsConfig } from "node:util"
|
|
17
17
|
|
|
18
18
|
export type UnsafeCLIArgument = string & { __unsafeCLIArgumentBrand: never }
|
|
19
19
|
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
|
|
20
20
|
import type { ZodType } from "zod"
|
|
21
21
|
|
|
22
|
+
import type { ParseArgsOptionsConfig } from "#scripting/arguments"
|
|
23
|
+
|
|
22
24
|
/**
|
|
23
25
|
* What every operation receives beside its input, whatever its family.
|
|
24
26
|
*
|
|
@@ -52,6 +54,13 @@ export interface Operation<
|
|
|
52
54
|
* What the operation does to the world, declared rather than inferred.
|
|
53
55
|
*/
|
|
54
56
|
effect: TEffect
|
|
57
|
+
/**
|
|
58
|
+
* The command-line flags a CLI adapter passes to `parseArgs` from `node:util`, one per input key.
|
|
59
|
+
*
|
|
60
|
+
* A boolean flag is turned off with its `--no-` form.
|
|
61
|
+
* The parsed values go to `inputSchema`, which decides each value's final type.
|
|
62
|
+
*/
|
|
63
|
+
options: ParseArgsOptionsConfig
|
|
55
64
|
inputSchema: ZodType<In>
|
|
56
65
|
outputSchema: ZodType<Out>
|
|
57
66
|
run(input: In, context: TContext): Promise<Out>
|