@mailwoman/core 7.2.0 → 7.2.1
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/api/APIClient.ts +175 -0
- package/api/headless.ts +23 -0
- package/api/index.ts +11 -0
- package/api/responses.ts +135 -0
- package/coarse-placer/coarse-placer.ts +300 -0
- package/coarse-placer/featurize.ts +171 -0
- package/coarse-placer/tools/build-dataset.ts +223 -0
- package/coarse-placer/tools/build-outlier-exposure.ts +172 -0
- package/coarse-placer/tools/build-outlier-latin.ts +200 -0
- package/coarse-placer/tools/build-outlier-oa.ts +231 -0
- package/coarse-placer/tools/eval-latin-offmap.ts +140 -0
- package/coarse-placer/tools/eval-openset.ts +519 -0
- package/coarse-placer/tools/eval-quant-compare.ts +151 -0
- package/coarse-placer/tools/eval.ts +187 -0
- package/coarse-placer/tools/fnv-hash.ts +24 -0
- package/coarse-placer/tools/index.ts +23 -0
- package/coarse-placer/tools/probe-frontier.ts +216 -0
- package/coarse-placer/tools/quantize.ts +116 -0
- package/coarse-placer/tools/train.ts +244 -0
- package/collections.ts +134 -0
- package/db/schema.ts +21 -0
- package/decoder/build-tree.ts +228 -0
- package/decoder/calibration.ts +101 -0
- package/decoder/containment.ts +87 -0
- package/decoder/index.ts +17 -0
- package/decoder/proposals-to-tree.ts +83 -0
- package/decoder/resolve-proposal-overlaps.ts +79 -0
- package/decoder/serialize-json.ts +70 -0
- package/decoder/serialize-tuples.ts +61 -0
- package/decoder/serialize-xml.ts +209 -0
- package/decoder/types.ts +159 -0
- package/decoder/unknown-spans.ts +103 -0
- package/decoder/validate-tree.ts +100 -0
- package/env/index.ts +42 -0
- package/env/load.ts +14 -0
- package/env/paths.ts +40 -0
- package/env/schema.ts +125 -0
- package/errors/index.ts +9 -0
- package/errors/schema.ts +155 -0
- package/fs/node.ts +38 -0
- package/identifiers.ts +58 -0
- package/index.ts +13 -0
- package/kysley/adapter.ts +29 -0
- package/kysley/client.ts +30 -0
- package/kysley/dialect-config.ts +30 -0
- package/kysley/dialect.ts +67 -0
- package/kysley/driver.ts +175 -0
- package/layers/index.ts +12 -0
- package/layers/manifest.ts +154 -0
- package/layers/schema.ts +105 -0
- package/lifecycle/ServiceSymbol.ts +95 -0
- package/lifecycle/index.ts +11 -0
- package/lifecycle/lru-cache.ts +44 -0
- package/lifecycle/services.ts +322 -0
- package/locale/index.ts +9 -0
- package/locale/locale.ts +49 -0
- package/locale/profiles/en-us.ts +69 -0
- package/locale/profiles/fr-fr.ts +75 -0
- package/locale/profiles/index.ts +9 -0
- package/locale/profiles/ja-jp.ts +33 -0
- package/locale/registry.ts +68 -0
- package/logging/index.ts +8 -0
- package/logging/shared.ts +145 -0
- package/logging/utils.ts +46 -0
- package/objects.ts +211 -0
- package/package.json +195 -104
- package/pipeline/index.ts +46 -0
- package/pipeline/reconcile-lookups.ts +108 -0
- package/pipeline/reconcile.ts +542 -0
- package/pipeline/runtime-pipeline.ts +732 -0
- package/pipeline/span-logit-aggregation.ts +151 -0
- package/pipeline/span-proposer.ts +587 -0
- package/pipeline/types.ts +348 -0
- package/policy/defaults.ts +35 -0
- package/policy/index.ts +10 -0
- package/policy/policy.ts +60 -0
- package/policy/proposal-collection.ts +64 -0
- package/policy/registry.ts +149 -0
- package/resolver/index.ts +29 -0
- package/resolver/types.ts +584 -0
- package/resources/LocaleIndex.ts +124 -0
- package/resources/ResourceMapCache.ts +126 -0
- package/resources/collections.ts +93 -0
- package/resources/debugging.ts +19 -0
- package/resources/git.ts +54 -0
- package/resources/index.ts +17 -0
- package/resources/languages/index.ts +49 -0
- package/resources/languages/types.gen.ts +1368 -0
- package/resources/libaddressinput.ts +105 -0
- package/resources/libpostal.ts +182 -0
- package/resources/locale.ts +23 -0
- package/resources/set.ts +179 -0
- package/resources/whosonfirst/DataSourceCache.ts +72 -0
- package/resources/whosonfirst/PlacetypeDataSource.ts +199 -0
- package/resources/whosonfirst/index.ts +10 -0
- package/resources/whosonfirst/loader.ts +157 -0
- package/resources/whosonfirst/placetypes/Placetype.ts +438 -0
- package/resources/whosonfirst/placetypes/admin.ts +173 -0
- package/resources/whosonfirst/placetypes/definition.ts +135 -0
- package/resources/whosonfirst/placetypes/graph.ts +0 -0
- package/resources/whosonfirst/placetypes/index.ts +12 -0
- package/resources/whosonfirst/placetypes/mermaid.ts +168 -0
- package/resources/whosonfirst/placetypes/tree.ts +46 -0
- package/routing/index.ts +208 -0
- package/scripting/index.ts +40 -0
- package/scripting/utils/index.ts +106 -0
- package/tokenization/Graph.ts +50 -0
- package/tokenization/Span.ts +297 -0
- package/tokenization/index.ts +9 -0
- package/tokenization/normalizer.ts +123 -0
- package/tools/download-libpostal-resources.ts +98 -0
- package/tools/download-ssl-address.ts +99 -0
- package/tools/generate-language-types.ts +194 -0
- package/tools/index.ts +13 -0
- package/types/Classification.ts +211 -0
- package/types/classifier.ts +133 -0
- package/types/component.ts +75 -0
- package/types/index.ts +10 -0
- package/types/mapping.ts +81 -0
- package/utils/data-root.ts +56 -0
- package/utils/hash.ts +27 -0
- package/utils/index.ts +14 -0
- package/utils/jsonl.ts +43 -0
- package/utils/python-json.ts +105 -0
- package/utils/python-random.ts +90 -0
- package/utils/repo.ts +118 -0
- package/utils/sealed-db.ts +85 -0
- package/utils/stats.ts +32 -0
- package/vitest.config.ts +53 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Placetype roles in a hierarchy.
|
|
9
|
+
*/
|
|
10
|
+
export const PlacetypeRoles = ["optional", "common", "common_optional"] as const
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A placetype's role in representing a full hierarchy.
|
|
14
|
+
*/
|
|
15
|
+
export type PlacetypeRole = (typeof PlacetypeRoles)[number]
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The order of placetype roles in a hierarchy.
|
|
19
|
+
*/
|
|
20
|
+
export const PlacetypeRoleOrder = Object.fromEntries(PlacetypeRoles.map((role, index) => [role, index])) as Record<
|
|
21
|
+
PlacetypeRole,
|
|
22
|
+
number
|
|
23
|
+
>
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* WhosOnFirst placetypes are a hierarchical classification system for geographic features.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://github.com/whosonfirst/whosonfirst-placetypes/blob/main/data/placetypes-spec-latest.json | WhosOnFirst Placetypes}
|
|
29
|
+
*/
|
|
30
|
+
export type WhosOnFirstPlacetype =
|
|
31
|
+
| "address"
|
|
32
|
+
| "arcade"
|
|
33
|
+
| "borough"
|
|
34
|
+
| "building"
|
|
35
|
+
| "campus"
|
|
36
|
+
| "concourse"
|
|
37
|
+
| "continent"
|
|
38
|
+
| "country"
|
|
39
|
+
| "county"
|
|
40
|
+
| "custom"
|
|
41
|
+
| "dependency"
|
|
42
|
+
| "disputed"
|
|
43
|
+
| "empire"
|
|
44
|
+
| "enclosure"
|
|
45
|
+
| "installation"
|
|
46
|
+
| "intersection"
|
|
47
|
+
| "localadmin"
|
|
48
|
+
| "locality"
|
|
49
|
+
| "macrocounty"
|
|
50
|
+
| "macrohood"
|
|
51
|
+
| "macroregion"
|
|
52
|
+
| "marinearea"
|
|
53
|
+
| "marketarea"
|
|
54
|
+
| "metroarea"
|
|
55
|
+
| "microhood"
|
|
56
|
+
| "nation"
|
|
57
|
+
| "neighbourhood"
|
|
58
|
+
| "ocean"
|
|
59
|
+
| "planet"
|
|
60
|
+
| "postalcode"
|
|
61
|
+
| "postalregion"
|
|
62
|
+
| "region"
|
|
63
|
+
| "timezone"
|
|
64
|
+
| "venue"
|
|
65
|
+
| "wing"
|
|
66
|
+
|
|
67
|
+
export type PlacetypeLocalizationRecord = {
|
|
68
|
+
[languageCode: string]: string | undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface PlacetypeDefinition {
|
|
72
|
+
/**
|
|
73
|
+
* The id of the placetype, typically generated by Brooklyn Integers.
|
|
74
|
+
*
|
|
75
|
+
* @see {@link https://brooklynintegers.com/ Brooklyn Integers}
|
|
76
|
+
*/
|
|
77
|
+
id: number
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The name of the placetype, e.g. "country", "city", "neighborhood"
|
|
81
|
+
*/
|
|
82
|
+
name: string
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The localized names of the placetype.
|
|
86
|
+
*/
|
|
87
|
+
names: PlacetypeLocalizationRecord
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The role of the placetype.
|
|
91
|
+
*/
|
|
92
|
+
role: PlacetypeRole
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The possible parent placetypes of this placetype.
|
|
96
|
+
*
|
|
97
|
+
* The parent placetypes are typically Brooklyn Integers.
|
|
98
|
+
*/
|
|
99
|
+
parent?: string[]
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const WOFPrefix = "wof:"
|
|
103
|
+
export type WOFPrefix = typeof WOFPrefix
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* A raw JSON representation of a placetype definition.
|
|
107
|
+
*
|
|
108
|
+
* This identical to the {@linkcode PlacetypeDefinition} however, each key is prefixed with "wof:"
|
|
109
|
+
*/
|
|
110
|
+
export type PlacetypeDefinitionInit = {
|
|
111
|
+
[K in keyof PlacetypeDefinition as `${WOFPrefix}${K}`]: PlacetypeDefinition[K]
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Convert a prefixed placetype definition to a standard placetype definition.
|
|
116
|
+
*/
|
|
117
|
+
export function normalizePlacetypeDefinition(
|
|
118
|
+
initLike: PlacetypeDefinition | PlacetypeDefinitionInit
|
|
119
|
+
): PlacetypeDefinition {
|
|
120
|
+
const spec: Record<string, unknown> = {}
|
|
121
|
+
|
|
122
|
+
for (const [key, value] of Object.entries(initLike)) {
|
|
123
|
+
const normalizedKey = key.startsWith(WOFPrefix) ? key.slice(WOFPrefix.length) : key
|
|
124
|
+
spec[normalizedKey] = value
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return spec as unknown as PlacetypeDefinition
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A record of placetype definitions, keyed by Placetype id.
|
|
132
|
+
*/
|
|
133
|
+
export type PlacetypeDefinitionRecord = {
|
|
134
|
+
[id: number]: Omit<PlacetypeDefinition, "id"> | undefined
|
|
135
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export * from "./admin.ts"
|
|
8
|
+
export * from "./definition.ts"
|
|
9
|
+
export * from "./graph.ts"
|
|
10
|
+
export * from "./mermaid.ts"
|
|
11
|
+
export * from "./Placetype.ts"
|
|
12
|
+
export * from "./tree.ts"
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { rgb } from "d3-color"
|
|
8
|
+
import { interpolateViridis } from "d3-scale-chromatic"
|
|
9
|
+
|
|
10
|
+
import { type PlacetypeRole, PlacetypeRoles } from "./definition.ts"
|
|
11
|
+
import { Placetype } from "./Placetype.ts"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Mermaid's `classDef` parser uses commas to separate style properties, so an `rgb(r, g, b)` value (which
|
|
15
|
+
* d3-scale-chromatic emits for several interpolators, e.g. `interpolateRainbow`, `interpolateTurbo`,
|
|
16
|
+
* `interpolateSinebow`) breaks the parse. Convert any d3-color-recognised input to hex before embedding. `d3-color`
|
|
17
|
+
* already handles hex/rgb/rgba/hsl/named inputs.
|
|
18
|
+
*/
|
|
19
|
+
function toMermaidColor(input: string): string {
|
|
20
|
+
const parsed = rgb(input)
|
|
21
|
+
|
|
22
|
+
return Number.isNaN(parsed.r) ? input : parsed.formatHex()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Hand-tuned default colors for placetype roles. Used when no `interpolator` is passed to
|
|
27
|
+
* {@linkcode generateMermaidMarkup}. Each entry pairs a fill with a darker stroke and a text color chosen for contrast
|
|
28
|
+
* against the fill.
|
|
29
|
+
*/
|
|
30
|
+
export const PlacetypeRoleColor = {
|
|
31
|
+
common: "#0066cc",
|
|
32
|
+
common_optional: "#00cc66",
|
|
33
|
+
optional: "#ffcc00",
|
|
34
|
+
} as const satisfies Record<PlacetypeRole, string>
|
|
35
|
+
|
|
36
|
+
const PlacetypeRoleStroke = {
|
|
37
|
+
common: "#004d99",
|
|
38
|
+
common_optional: "#009933",
|
|
39
|
+
optional: "#cca300",
|
|
40
|
+
} as const satisfies Record<PlacetypeRole, string>
|
|
41
|
+
|
|
42
|
+
const PlacetypeRoleText = {
|
|
43
|
+
common: "white",
|
|
44
|
+
common_optional: "white",
|
|
45
|
+
optional: "black",
|
|
46
|
+
} as const satisfies Record<PlacetypeRole, string>
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A color interpolator — compatible with d3-scale-chromatic's `interpolate*` functions (e.g. `interpolateViridis`,
|
|
50
|
+
* `interpolateTurbo`). Receives `t ∈ [0, 1]` and returns a CSS color string.
|
|
51
|
+
*/
|
|
52
|
+
export type InterpolateColorCallback = (t: number) => string
|
|
53
|
+
|
|
54
|
+
export interface GenerateMermaidMarkupOptions {
|
|
55
|
+
/** Restrict descendants to the given roles. Default: all roles. */
|
|
56
|
+
roles?: Iterable<PlacetypeRole>
|
|
57
|
+
/**
|
|
58
|
+
* Edge color interpolator. Each edge is colored by its child node's depth from the root: `t = (childDepth - 1) /
|
|
59
|
+
* (maxDepth - 1)`. This traces a smooth gradient along any lineage path (e.g. `planet → continent → country → …`) and
|
|
60
|
+
* gives a visual cue for how deep an edge sits in the tree.
|
|
61
|
+
*
|
|
62
|
+
* Defaults to d3-scale-chromatic's `interpolateViridis` — perceptually uniform and colorblind-friendly. Node
|
|
63
|
+
* fills/strokes are _not_ affected; they always use the hand-tuned {@linkcode PlacetypeRoleColor} palette, which
|
|
64
|
+
* carries more semantic weight than a sampled gradient for only three categorical role values.
|
|
65
|
+
*/
|
|
66
|
+
edgeInterpolator?: InterpolateColorCallback
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface RolePalette {
|
|
70
|
+
fill: string
|
|
71
|
+
stroke: string
|
|
72
|
+
text: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const HAND_TUNED_PALETTE: Record<PlacetypeRole, RolePalette> = Object.fromEntries(
|
|
76
|
+
PlacetypeRoles.map((role) => [
|
|
77
|
+
role,
|
|
78
|
+
{ fill: PlacetypeRoleColor[role], stroke: PlacetypeRoleStroke[role], text: PlacetypeRoleText[role] },
|
|
79
|
+
])
|
|
80
|
+
) as Record<PlacetypeRole, RolePalette>
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Walk the (filtered) subtree once to determine the deepest reachable descendant. Mirrors the structure of the
|
|
84
|
+
* emit-walk in {@linkcode generateMermaidMarkup} so the depths it computes line up with the edges that will be emitted.
|
|
85
|
+
* Cycles in the DAG are guarded by the `visited` set.
|
|
86
|
+
*/
|
|
87
|
+
function measureMaxDepth(root: Placetype, roles: Iterable<PlacetypeRole> | undefined): number {
|
|
88
|
+
let maxDepth = 0
|
|
89
|
+
const visited = new Set<string>()
|
|
90
|
+
|
|
91
|
+
const walk = (node: Placetype, depth: number): void => {
|
|
92
|
+
for (const child of node.findChildren(roles)) {
|
|
93
|
+
const childDepth = depth + 1
|
|
94
|
+
|
|
95
|
+
if (childDepth > maxDepth) {
|
|
96
|
+
maxDepth = childDepth
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (visited.has(child.name)) continue
|
|
100
|
+
visited.add(child.name)
|
|
101
|
+
walk(child, childDepth)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
walk(root, 0)
|
|
106
|
+
|
|
107
|
+
return maxDepth
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Generate a Mermaid flowchart markup for a placetype and its descendants.
|
|
112
|
+
*
|
|
113
|
+
* The walk is a recursive `findChildren` traversal — every emitted edge is a real direct-parent → direct-child
|
|
114
|
+
* relationship. WOF placetypes form a DAG (e.g. `borough` has both `country` and `macroregion` as parents), so a child
|
|
115
|
+
* can legitimately appear on multiple edges; the `visited` set prevents the subtree below it from being re-emitted.
|
|
116
|
+
*
|
|
117
|
+
* Edges are colored by depth from the root via {@linkcode GenerateMermaidMarkupOptions.edgeInterpolator} (default:
|
|
118
|
+
* viridis), so any lineage path traces a smooth gradient down the chart. Node fills always use the hand-tuned role
|
|
119
|
+
* palette.
|
|
120
|
+
*/
|
|
121
|
+
export function generateMermaidMarkup(placetype: Placetype, options: GenerateMermaidMarkupOptions = {}): string {
|
|
122
|
+
const { roles, edgeInterpolator = interpolateViridis } = options
|
|
123
|
+
const palette = HAND_TUNED_PALETTE
|
|
124
|
+
|
|
125
|
+
const lines: string[] = [
|
|
126
|
+
"---",
|
|
127
|
+
"config:",
|
|
128
|
+
" flowchart:",
|
|
129
|
+
" defaultRenderer: elk",
|
|
130
|
+
"---",
|
|
131
|
+
"graph TD",
|
|
132
|
+
" linkStyle default stroke-width: 5",
|
|
133
|
+
...PlacetypeRoles.map(
|
|
134
|
+
(role) =>
|
|
135
|
+
` classDef ${role} fill:${palette[role].fill},stroke:${palette[role].stroke},color:${palette[role].text},font-weight:bold`
|
|
136
|
+
),
|
|
137
|
+
// The root is never the target of an emitted edge, so declare it standalone so it picks
|
|
138
|
+
// up the role classDef and renders alongside the others.
|
|
139
|
+
` ${placetype.name}:::${placetype.role}`,
|
|
140
|
+
]
|
|
141
|
+
|
|
142
|
+
// Pre-compute max depth once so every edge's `t` is consistent across the second pass.
|
|
143
|
+
const maxDepth = measureMaxDepth(placetype, roles)
|
|
144
|
+
|
|
145
|
+
const visited = new Set<string>()
|
|
146
|
+
let edgeIdx = 0
|
|
147
|
+
|
|
148
|
+
const walk = (node: Placetype, depth: number): void => {
|
|
149
|
+
for (const child of node.findChildren(roles)) {
|
|
150
|
+
const childDepth = depth + 1
|
|
151
|
+
// Single-level case: nothing to interpolate across, sample mid-gradient.
|
|
152
|
+
const t = maxDepth > 1 ? (childDepth - 1) / (maxDepth - 1) : 0.5
|
|
153
|
+
const edgeColor = toMermaidColor(edgeInterpolator(t))
|
|
154
|
+
|
|
155
|
+
lines.push(` ${node.name} --> ${child.name}:::${child.role}`)
|
|
156
|
+
lines.push(` linkStyle ${edgeIdx} stroke:${edgeColor}`)
|
|
157
|
+
edgeIdx++
|
|
158
|
+
|
|
159
|
+
if (visited.has(child.name)) continue
|
|
160
|
+
visited.add(child.name)
|
|
161
|
+
walk(child, childDepth)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
walk(placetype, 0)
|
|
166
|
+
|
|
167
|
+
return lines.join("\n")
|
|
168
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { PlacetypeRole } from "./definition.ts"
|
|
8
|
+
import { Placetype } from "./Placetype.ts"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A node in the nested placetype tree produced by {@linkcode generatePlacetypeTree}.
|
|
12
|
+
*
|
|
13
|
+
* WOF placetypes form a DAG (a placetype can have multiple parents — `borough`, for example, is a direct child of both
|
|
14
|
+
* `country` and `macroregion`), so a tree projection rooted at a single placetype can list the same descendant under
|
|
15
|
+
* multiple branches. That repetition is expected.
|
|
16
|
+
*/
|
|
17
|
+
export interface PlacetypeTreeNode {
|
|
18
|
+
name: string
|
|
19
|
+
id: number
|
|
20
|
+
role: PlacetypeRole
|
|
21
|
+
children: PlacetypeTreeNode[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Build a nested tree of a placetype and its descendants, optionally filtered by role.
|
|
26
|
+
*
|
|
27
|
+
* The traversal mirrors {@linkcode Placetype.findChildren} — direct children only at each level, recursively. The
|
|
28
|
+
* DAG-to-tree projection may repeat descendants under multiple parents; if you need each placetype to appear exactly
|
|
29
|
+
* once, use {@linkcode Placetype.findDescendants} for a flat de-duplicated set instead.
|
|
30
|
+
*/
|
|
31
|
+
export function generatePlacetypeTree(placetype: Placetype, roles?: Iterable<PlacetypeRole> | null): PlacetypeTreeNode {
|
|
32
|
+
const roleSet = roles ? new Set(roles) : null
|
|
33
|
+
|
|
34
|
+
return buildNode(placetype, roleSet)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function buildNode(placetype: Placetype, roles: Set<PlacetypeRole> | null): PlacetypeTreeNode {
|
|
38
|
+
const children = placetype.findChildren(roles).map((child) => buildNode(child, roles))
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
name: placetype.name,
|
|
42
|
+
id: placetype.id,
|
|
43
|
+
role: placetype.role,
|
|
44
|
+
children,
|
|
45
|
+
}
|
|
46
|
+
}
|
package/routing/index.ts
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { AxiosRequestConfig } from "axios"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Derived from React Router, this type helper is used to extract the parameters from a path pattern.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/URLPattern | MDN: URLPattern}
|
|
14
|
+
*/
|
|
15
|
+
export type ParamPattern<Path extends string> = Path extends "*" | "/*"
|
|
16
|
+
? "*"
|
|
17
|
+
: Path extends `${infer Rest}/*`
|
|
18
|
+
? "*" | _$PathParam<Rest>
|
|
19
|
+
: _$PathParam<Path>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Type helper to extract the parameters from a path pattern.
|
|
23
|
+
*
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
type _$PathParam<Path extends string> = Path extends `${infer L}${"/" | "."}${infer R}`
|
|
27
|
+
? _$PathParam<L> | _$PathParam<R>
|
|
28
|
+
: Path extends `:${infer Param}`
|
|
29
|
+
? Param extends `${infer Optional}?`
|
|
30
|
+
? Optional
|
|
31
|
+
: Param extends `${infer ParamName}(${infer _ParamPattern})`
|
|
32
|
+
? ParamName
|
|
33
|
+
: Param
|
|
34
|
+
: never
|
|
35
|
+
|
|
36
|
+
export type URLPatternPathnameInit = Pick<Required<URLPatternInit>, "pathname">
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Either a URLPattern or a pathname pattern string.
|
|
40
|
+
*/
|
|
41
|
+
export type URLRouteInit<I extends URLPatternPathnameInit | string> = I extends string ? { pathname: I } : I
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Type helper to extract the parameters from a URL Pattern pathname.
|
|
45
|
+
*/
|
|
46
|
+
export type ExtractURLPatternPathnameParams<I extends URLPatternInit | string> = I extends string
|
|
47
|
+
? ParamPattern<I>
|
|
48
|
+
: I extends URLPatternPathnameInit
|
|
49
|
+
? ParamPattern<I["pathname"]>
|
|
50
|
+
: never
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Type helper to extract a URL Pattern pathname.
|
|
54
|
+
*/
|
|
55
|
+
export type ExtractURLPatternPathname<I extends URLPatternPathnameInit | string> = I extends string
|
|
56
|
+
? I
|
|
57
|
+
: I extends URLPatternPathnameInit
|
|
58
|
+
? I["pathname"]
|
|
59
|
+
: never
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A record of path parameter names to their raw values.
|
|
63
|
+
*/
|
|
64
|
+
export type URLPatternPathParameters<I extends URLPatternPathnameInit | string, V extends string | number = string> = {
|
|
65
|
+
[key in ExtractURLPatternPathnameParams<I>]: V
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const URLPatternComponents = [
|
|
69
|
+
"protocol",
|
|
70
|
+
"username",
|
|
71
|
+
"hostname",
|
|
72
|
+
"port",
|
|
73
|
+
"pathname",
|
|
74
|
+
"password",
|
|
75
|
+
"search",
|
|
76
|
+
"hash",
|
|
77
|
+
] as const satisfies readonly (keyof URLPatternInit)[]
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A URL pattern with path parameters.
|
|
81
|
+
*/
|
|
82
|
+
// @ts-ignore: Property 'URLPattern' does not exist
|
|
83
|
+
export class URLRoutePattern<I extends URLPatternPathnameInit | string = string> extends URLPattern {
|
|
84
|
+
public override toString(): string {
|
|
85
|
+
return JSON.stringify(this.toJSON(), null, "\t")
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
constructor(init: I) {
|
|
89
|
+
const normalizedInit = typeof init === "string" && !init.startsWith("http") ? { pathname: init } : init
|
|
90
|
+
super(normalizedInit)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static from<I extends URLPatternPathnameInit | string>(init: I): URLRoutePattern<I> {
|
|
94
|
+
return new URLRoutePattern(init)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Given a URL, attempts to match the path parameters.
|
|
99
|
+
*/
|
|
100
|
+
public matchParams(input: URLPatternInit | string, baseURL?: string): null | URLPatternPathParameters<I> {
|
|
101
|
+
return super.exec(input, baseURL)?.pathname.groups as URLPatternPathParameters<I>
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Given a set of parameters, returns an Axios configuration object.
|
|
106
|
+
*/
|
|
107
|
+
public toAxiosConfig(params: Partial<URLPatternPathParameters<I, string | number>> = {}): {
|
|
108
|
+
url: string
|
|
109
|
+
params: Partial<URLPatternPathParameters<I, string | number>>
|
|
110
|
+
} {
|
|
111
|
+
// First, we start with our uncompiled pathname pattern.
|
|
112
|
+
const components = this.toJSON()
|
|
113
|
+
|
|
114
|
+
const url = new URL(
|
|
115
|
+
components.pathname || "/",
|
|
116
|
+
components.baseURL || `${components.protocol || "https"}://${components.hostname || "localhost"}`
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
url.username = components.username || ""
|
|
120
|
+
url.password = components.password || ""
|
|
121
|
+
url.port = components.port || ""
|
|
122
|
+
|
|
123
|
+
let { pathname } = this
|
|
124
|
+
const paramPairs = Object.entries(params) as Array<[keyof URLPatternPathParameters<I>, string | number]>
|
|
125
|
+
const remainingParams = { ...params }
|
|
126
|
+
|
|
127
|
+
// ...Iterating over each parameter in the params object...
|
|
128
|
+
for (const [paramName, paramValue] of paramPairs) {
|
|
129
|
+
// Next, we check if the pathname pattern includes the param name.
|
|
130
|
+
const paramPattern = `:${paramName}`
|
|
131
|
+
|
|
132
|
+
const paramPatternIndex = pathname.indexOf(paramPattern)
|
|
133
|
+
|
|
134
|
+
if (paramPatternIndex === -1) continue
|
|
135
|
+
|
|
136
|
+
// Is the param present?
|
|
137
|
+
if (!paramValue) continue
|
|
138
|
+
|
|
139
|
+
let normalizedParamValue: string
|
|
140
|
+
|
|
141
|
+
switch (typeof paramValue) {
|
|
142
|
+
case "string":
|
|
143
|
+
normalizedParamValue = encodeURIComponent(paramValue)
|
|
144
|
+
break
|
|
145
|
+
case "number":
|
|
146
|
+
normalizedParamValue = encodeURIComponent(paramValue.toString())
|
|
147
|
+
break
|
|
148
|
+
default:
|
|
149
|
+
console.warn(`Unexpected parameter value type: ${typeof paramValue}`)
|
|
150
|
+
continue
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// We replace the param pattern with the normalized param value.
|
|
154
|
+
pathname =
|
|
155
|
+
pathname.slice(0, paramPatternIndex) +
|
|
156
|
+
normalizedParamValue +
|
|
157
|
+
pathname.slice(paramPatternIndex + paramPattern.length)
|
|
158
|
+
|
|
159
|
+
delete remainingParams[paramName]
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
url.pathname = pathname
|
|
163
|
+
const href = url.href
|
|
164
|
+
|
|
165
|
+
// We have what appears to be complete URL configuration, so let's test it.
|
|
166
|
+
const spec = new URLPattern(components)
|
|
167
|
+
|
|
168
|
+
if (!spec.test(href)) {
|
|
169
|
+
throw new Error(
|
|
170
|
+
`Insufficient parameters to compile URL route: ${this.pathname} (${JSON.stringify(params)}) -> ${pathname}`
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const config = {
|
|
175
|
+
url: components.hostname ? href : url.pathname,
|
|
176
|
+
params: remainingParams,
|
|
177
|
+
} satisfies Pick<AxiosRequestConfig, "url" | "params">
|
|
178
|
+
|
|
179
|
+
return config
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compiles the URL route with the given parameters.
|
|
184
|
+
*/
|
|
185
|
+
public compile(params: URLPatternPathParameters<I, string | number>): string {
|
|
186
|
+
return this.toAxiosConfig(params).url
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public toURL(params: URLPatternPathParameters<I>, baseURL?: string): URL {
|
|
190
|
+
return new URL(this.compile(params), baseURL)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public toJSON(): URLPatternInit {
|
|
194
|
+
const result: URLPatternInit = {}
|
|
195
|
+
|
|
196
|
+
for (const component of URLPatternComponents) {
|
|
197
|
+
const value = this[component]
|
|
198
|
+
|
|
199
|
+
if (!value) continue
|
|
200
|
+
|
|
201
|
+
if (value === "*") continue
|
|
202
|
+
|
|
203
|
+
result[component] = value
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return result
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ScriptCallback } from "./utils/index.ts"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Given the calling module's import.meta object, runs the callback if that module is the entry script.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* runIfScript(import.meta, async () => {...})
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* The caller's meta is required: `import.meta.main` is per-module, so checking our own would always be false. This is
|
|
11
|
+
* useful for conditionally running scripts without too much boilerplate.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export async function runIfScript(meta: ImportMeta, scriptCallback: ScriptCallback): Promise<void> {
|
|
16
|
+
if (typeof meta.main !== "boolean") {
|
|
17
|
+
// Vite/vitest module graphs define import.meta.env but never import.meta.main — a module
|
|
18
|
+
// imported there is not the entry script, so importing it must stay side-effect-free.
|
|
19
|
+
if ((meta as { env?: unknown }).env) return
|
|
20
|
+
|
|
21
|
+
throw new Error("Expected import.meta.main to be a boolean. Are we on Node.js 24+?")
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!meta.main) return
|
|
25
|
+
|
|
26
|
+
const [{ $public }, { ConsoleLogger, stringifyLoggedObject }, { runScript }] = await Promise.all([
|
|
27
|
+
import("@mailwoman/core/env"),
|
|
28
|
+
import("@mailwoman/core/logging"),
|
|
29
|
+
import("@mailwoman/core/scripting/utils"),
|
|
30
|
+
])
|
|
31
|
+
|
|
32
|
+
ConsoleLogger.info(
|
|
33
|
+
stringifyLoggedObject($public, {
|
|
34
|
+
description: "Public Environment",
|
|
35
|
+
showValues: true,
|
|
36
|
+
})
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
return runScript(scriptCallback)
|
|
40
|
+
}
|