@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,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Address field codes.
|
|
9
|
+
*/
|
|
10
|
+
export const AddressFieldCode = {
|
|
11
|
+
Name: "N",
|
|
12
|
+
Organisation: "O",
|
|
13
|
+
StreetAddressLine: "A",
|
|
14
|
+
/**
|
|
15
|
+
* May be an inner-city district or a suburb
|
|
16
|
+
*/
|
|
17
|
+
DependentLocality: "D",
|
|
18
|
+
CityOrLocality: "C",
|
|
19
|
+
/**
|
|
20
|
+
* State, province, island etc.
|
|
21
|
+
*
|
|
22
|
+
* Indicates as to the data expected can be read from the `state_name_type` field.
|
|
23
|
+
*
|
|
24
|
+
* If this is missing, it is assumed to be "province" - but it may be overridden with values such as "state", "island"
|
|
25
|
+
* etc.
|
|
26
|
+
*/
|
|
27
|
+
AdministrativeArea: "S",
|
|
28
|
+
ZipOrPostalCode: "Z",
|
|
29
|
+
SortingCode: "X",
|
|
30
|
+
} as const satisfies Record<string, string>
|
|
31
|
+
|
|
32
|
+
export type AddressFieldCode = (typeof AddressFieldCode)[keyof typeof AddressFieldCode]
|
|
33
|
+
|
|
34
|
+
export interface PostcodeSpec {
|
|
35
|
+
/**
|
|
36
|
+
* Example postcodes, delimited by a comma.
|
|
37
|
+
*/
|
|
38
|
+
zipex: string
|
|
39
|
+
/**
|
|
40
|
+
* Required fields.
|
|
41
|
+
*
|
|
42
|
+
* @see {@linkcode AddressFieldCode}
|
|
43
|
+
*/
|
|
44
|
+
require: string
|
|
45
|
+
/**
|
|
46
|
+
* URL to lookup the postcode.
|
|
47
|
+
*/
|
|
48
|
+
posturl: string
|
|
49
|
+
/**
|
|
50
|
+
* - ISO 3166-1 alpha-2 country code.
|
|
51
|
+
*/
|
|
52
|
+
key: string
|
|
53
|
+
/**
|
|
54
|
+
* Country name.
|
|
55
|
+
*/
|
|
56
|
+
name: string
|
|
57
|
+
/**
|
|
58
|
+
* Format of the address.
|
|
59
|
+
*/
|
|
60
|
+
fmt: string
|
|
61
|
+
/**
|
|
62
|
+
* Type of the postcode.
|
|
63
|
+
*/
|
|
64
|
+
zip_name_type: string
|
|
65
|
+
/**
|
|
66
|
+
* Subdivision names.
|
|
67
|
+
*/
|
|
68
|
+
sub_names: string
|
|
69
|
+
/**
|
|
70
|
+
* Subdivision example postcodes.
|
|
71
|
+
*/
|
|
72
|
+
sub_zipexs: string
|
|
73
|
+
/**
|
|
74
|
+
* Language code.
|
|
75
|
+
*/
|
|
76
|
+
lang: string
|
|
77
|
+
/**
|
|
78
|
+
* Subdivision ISO codes.
|
|
79
|
+
*/
|
|
80
|
+
sub_isoids: string
|
|
81
|
+
/**
|
|
82
|
+
* Type of the state name.
|
|
83
|
+
*/
|
|
84
|
+
state_name_type: string
|
|
85
|
+
/**
|
|
86
|
+
* Regex pattern for the postcode.
|
|
87
|
+
*/
|
|
88
|
+
zip: string
|
|
89
|
+
/**
|
|
90
|
+
* Subdivision keys.
|
|
91
|
+
*/
|
|
92
|
+
sub_keys: string
|
|
93
|
+
/**
|
|
94
|
+
* Languages.
|
|
95
|
+
*/
|
|
96
|
+
languages: string
|
|
97
|
+
/**
|
|
98
|
+
* Uppercase fields.
|
|
99
|
+
*/
|
|
100
|
+
upper: string
|
|
101
|
+
/**
|
|
102
|
+
* Subdivision postcodes.
|
|
103
|
+
*/
|
|
104
|
+
sub_zips: string
|
|
105
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { readdir } from "node:fs/promises"
|
|
8
|
+
import { availableParallelism } from "node:os"
|
|
9
|
+
|
|
10
|
+
import { tryStat } from "@mailwoman/core/fs"
|
|
11
|
+
import { Alpha2LanguageCode } from "@mailwoman/core/resources/languages"
|
|
12
|
+
import { TextNormalizer, type TextNormalizerInit } from "@mailwoman/core/tokenization"
|
|
13
|
+
import { PathBuilder } from "path-ts"
|
|
14
|
+
import pluralize from "pluralize"
|
|
15
|
+
import { TextSpliterator } from "spliterator"
|
|
16
|
+
|
|
17
|
+
import { resourceDictionaryPathBuilder } from "../utils/repo.ts"
|
|
18
|
+
import { takeAsync } from "./collections.ts"
|
|
19
|
+
import { LocaleIndex } from "./LocaleIndex.ts"
|
|
20
|
+
|
|
21
|
+
const batchSize = availableParallelism()
|
|
22
|
+
const libPostalDataDirectory = resourceDictionaryPathBuilder("libpostal")
|
|
23
|
+
const libPostalInternalDataDirectory = resourceDictionaryPathBuilder("internal", "libpostal")
|
|
24
|
+
|
|
25
|
+
export type LibPostalLanguageCode = Alpha2LanguageCode | "all"
|
|
26
|
+
|
|
27
|
+
let _availableLanguages: Promise<LibPostalLanguageCode[]> | undefined
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Languages with a libpostal dictionary directory on disk. Lazily resolved and memoized — this was a top-level `await`
|
|
31
|
+
* until #481, which made any bare-import + subpath-import combination a TLA cycle under Vite's loader (base classes at
|
|
32
|
+
* the far side of the barrel evaluated as `undefined`). The promise is shared, so concurrent first callers do one
|
|
33
|
+
* `readdir`.
|
|
34
|
+
*/
|
|
35
|
+
export function getAvailableLanguages(): Promise<LibPostalLanguageCode[]> {
|
|
36
|
+
_availableLanguages ??= readdir(libPostalDataDirectory.toString()).then((directories) => {
|
|
37
|
+
const languageCodeDirectories = new Set<string>()
|
|
38
|
+
|
|
39
|
+
for (const directory of directories) {
|
|
40
|
+
if (directory.includes(".")) continue
|
|
41
|
+
|
|
42
|
+
languageCodeDirectories.add(directory)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return Array.from(languageCodeDirectories) as LibPostalLanguageCode[]
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
return _availableLanguages
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface LibPostalFileEntry {
|
|
52
|
+
filePath: string
|
|
53
|
+
languageCode: LibPostalLanguageCode
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Given a filename and a list of selected language codes, return all matching files.
|
|
58
|
+
*
|
|
59
|
+
* @param filename The name of the file to search for.
|
|
60
|
+
* @param dataDirectory The directory to search in.
|
|
61
|
+
* @param languageCodes An iterable of language codes to search for.
|
|
62
|
+
*
|
|
63
|
+
* @returns A list of file paths and their corresponding language codes.
|
|
64
|
+
*/
|
|
65
|
+
async function* findFilesMatchingLocale(
|
|
66
|
+
filename: string,
|
|
67
|
+
dataDirectory: PathBuilder,
|
|
68
|
+
languageCodes: LibPostalLanguageCode[]
|
|
69
|
+
) {
|
|
70
|
+
for (const languageCode of languageCodes) {
|
|
71
|
+
const filePath = dataDirectory(languageCode, filename).toString()
|
|
72
|
+
const stats = await tryStat(filePath)
|
|
73
|
+
|
|
74
|
+
if (!stats) continue
|
|
75
|
+
const entry: LibPostalFileEntry = { filePath, languageCode }
|
|
76
|
+
|
|
77
|
+
yield entry
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface LibPostalNormalizerInit extends TextNormalizerInit {
|
|
82
|
+
pluralize?: boolean
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Load a dictionary file.
|
|
87
|
+
*/
|
|
88
|
+
export async function prepareLocaleIndex(
|
|
89
|
+
/**
|
|
90
|
+
* An iterable of language codes
|
|
91
|
+
*/
|
|
92
|
+
languageCodes: LibPostalLanguageCode[] | undefined,
|
|
93
|
+
filename: string,
|
|
94
|
+
options?: LibPostalNormalizerInit
|
|
95
|
+
): Promise<LocaleIndex<LibPostalLanguageCode>> {
|
|
96
|
+
const normalizer = new TextNormalizer(options)
|
|
97
|
+
const index = new LocaleIndex<LibPostalLanguageCode>([], {
|
|
98
|
+
displayName: "libpostal",
|
|
99
|
+
normalizer,
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
let inserted = false
|
|
103
|
+
|
|
104
|
+
// Default resolves lazily (#481 — the former TLA value).
|
|
105
|
+
const codes = languageCodes ?? (await getAvailableLanguages())
|
|
106
|
+
const fileEntries = takeAsync(findFilesMatchingLocale(filename, libPostalDataDirectory, codes), batchSize)
|
|
107
|
+
|
|
108
|
+
for await (const batch of fileEntries) {
|
|
109
|
+
await Promise.all(
|
|
110
|
+
batch.map(async ({ filePath, languageCode }) => {
|
|
111
|
+
const lines = TextSpliterator.fromAsync(filePath)
|
|
112
|
+
|
|
113
|
+
for await (const line of lines) {
|
|
114
|
+
for (const entry of TextSpliterator.from(line, { delimiter: "|" })) {
|
|
115
|
+
index.add(entry, languageCode)
|
|
116
|
+
inserted = true
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const internalFileEntries = takeAsync(
|
|
124
|
+
findFilesMatchingLocale(filename, libPostalInternalDataDirectory, codes),
|
|
125
|
+
batchSize
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
for await (const batch of internalFileEntries) {
|
|
129
|
+
await Promise.all(
|
|
130
|
+
batch.map(async ({ filePath, languageCode }) => {
|
|
131
|
+
const lines = TextSpliterator.fromAsync(filePath)
|
|
132
|
+
|
|
133
|
+
for await (const line of lines) {
|
|
134
|
+
if (!line) continue
|
|
135
|
+
|
|
136
|
+
const firstCharacter = line[0]
|
|
137
|
+
|
|
138
|
+
// Skip comments.
|
|
139
|
+
if (firstCharacter === "#") continue
|
|
140
|
+
|
|
141
|
+
if (firstCharacter === "!") {
|
|
142
|
+
for (const entry of TextSpliterator.from(line.slice(1), { delimiter: "|" })) {
|
|
143
|
+
index.remove(entry)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
for (const entry of TextSpliterator.from(line, { delimiter: "|" })) {
|
|
150
|
+
index.add(entry, languageCode)
|
|
151
|
+
inserted = true
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (index.size === 0 && !inserted) {
|
|
159
|
+
throw new Error(
|
|
160
|
+
`No index matches found for ${filename} in ${libPostalDataDirectory} for languages ${languageCodes}`
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (options?.pluralize) {
|
|
165
|
+
generatePlurals(index)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return index
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Generate plurals for the index.
|
|
173
|
+
*/
|
|
174
|
+
export function generatePlurals(index: LocaleIndex<LibPostalLanguageCode>) {
|
|
175
|
+
for (const [singularCell, value] of index) {
|
|
176
|
+
if (!value.has("en")) continue
|
|
177
|
+
|
|
178
|
+
const plural = pluralize(singularCell)
|
|
179
|
+
|
|
180
|
+
index.add(plural, "en")
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Format a quantity for display.
|
|
9
|
+
*/
|
|
10
|
+
export const formatQuantity = (quantity: number) => {
|
|
11
|
+
return quantity.toLocaleString("en-US", {
|
|
12
|
+
maximumFractionDigits: 2,
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Format a duration for display.
|
|
18
|
+
*/
|
|
19
|
+
export const formatMinutes = (minutes: number) => {
|
|
20
|
+
return minutes.toLocaleString("en-US", {
|
|
21
|
+
maximumFractionDigits: 2,
|
|
22
|
+
})
|
|
23
|
+
}
|
package/resources/set.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A set-like collection that maintains the order of its elements.
|
|
9
|
+
*/
|
|
10
|
+
export class Sequence<T extends WeakKey> extends Set<T> {
|
|
11
|
+
#first: T | null
|
|
12
|
+
#last: T | null
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The first node in the sequence.
|
|
16
|
+
*/
|
|
17
|
+
public get first(): T | null {
|
|
18
|
+
return this.#first
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The last node in the sequence.
|
|
23
|
+
*/
|
|
24
|
+
public get last(): T | null {
|
|
25
|
+
return this.#last
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public constructor(nodes: T[] = []) {
|
|
29
|
+
super(nodes)
|
|
30
|
+
|
|
31
|
+
this.#first = nodes[0] ?? null
|
|
32
|
+
this.#last = nodes[nodes.length - 1] ?? null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Clear the sequence.
|
|
37
|
+
*/
|
|
38
|
+
public override clear() {
|
|
39
|
+
super.clear()
|
|
40
|
+
|
|
41
|
+
this.#first = null
|
|
42
|
+
this.#last = null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Add a node to the sequence.
|
|
47
|
+
*/
|
|
48
|
+
public override add(...nodes: T[]): this {
|
|
49
|
+
if (!nodes.length) return this
|
|
50
|
+
|
|
51
|
+
for (const node of nodes) {
|
|
52
|
+
if (typeof node === "undefined" || node === null) {
|
|
53
|
+
throw new Error("Attempted to add an undefined or null node to a sequence.")
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
super.add(node)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!this.#first) {
|
|
60
|
+
this.#first = nodes[0]!
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.#last = nodes[nodes.length - 1]!
|
|
64
|
+
|
|
65
|
+
return this
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Delete a node from the sequence.
|
|
70
|
+
*/
|
|
71
|
+
public override delete(node: T): boolean {
|
|
72
|
+
if (typeof node === "undefined" || node === null) {
|
|
73
|
+
throw new Error("Attempted to delete an undefined or null node from a sequence.")
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const deleted = super.delete(node)
|
|
77
|
+
|
|
78
|
+
if (!deleted) return false
|
|
79
|
+
|
|
80
|
+
if (this.size === 0) {
|
|
81
|
+
this.#first = null
|
|
82
|
+
this.#last = null
|
|
83
|
+
} else if (this.#first === node) {
|
|
84
|
+
this.#first = this.values().next().value ?? null
|
|
85
|
+
} else if (this.#last === node) {
|
|
86
|
+
this.#last = Array.from(this).pop() ?? null
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return true
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Sort the sequence in place.
|
|
94
|
+
*/
|
|
95
|
+
public sort(callback: (a: T, b: T) => number) {
|
|
96
|
+
const currentEntries = Array.from(this)
|
|
97
|
+
|
|
98
|
+
currentEntries.sort(callback)
|
|
99
|
+
|
|
100
|
+
this.clear()
|
|
101
|
+
|
|
102
|
+
for (const node of currentEntries) {
|
|
103
|
+
super.add(node)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.#first = currentEntries[0] ?? null
|
|
107
|
+
this.#last = currentEntries[currentEntries.length - 1] ?? null
|
|
108
|
+
|
|
109
|
+
return this
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Serialize the sequence to JSON.
|
|
114
|
+
*/
|
|
115
|
+
public toJSON(): T[] {
|
|
116
|
+
return Array.from(this)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Given a property key of the items in the sequence, return an iterator of the values of that property.
|
|
121
|
+
*
|
|
122
|
+
* This is useful when building nested maps of properties.
|
|
123
|
+
*/
|
|
124
|
+
public pluck<P extends keyof T>(property: P): IteratorObject<T[P], undefined, unknown> {
|
|
125
|
+
return Iterator.from(this).map((node) => node[property])
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public override toString() {
|
|
129
|
+
return `Sequence(${this.size})`
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public [Symbol.for("nodejs.util.inspect.custom")]() {
|
|
133
|
+
const { first } = this
|
|
134
|
+
|
|
135
|
+
if (!first) return "Sequence(0)"
|
|
136
|
+
|
|
137
|
+
const constructorName = first.constructor.name
|
|
138
|
+
|
|
139
|
+
return `${constructorName}Sequence(${this.size})`
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function disposeOf<T extends Disposable | PropertyKey>(set: Pick<Set<T>, "clear" | typeof Symbol.iterator>) {
|
|
144
|
+
for (const node of set) {
|
|
145
|
+
if (!node || typeof node !== "object") continue
|
|
146
|
+
|
|
147
|
+
node[Symbol.dispose]?.()
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
set.clear()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* A set that cleans up its elements when disposed.
|
|
155
|
+
*/
|
|
156
|
+
export class DisposableSet<T extends Disposable | PropertyKey> extends Set<T> implements Disposable {
|
|
157
|
+
public [Symbol.dispose]() {
|
|
158
|
+
for (const node of this) {
|
|
159
|
+
if (!node || typeof node !== "object") continue
|
|
160
|
+
|
|
161
|
+
node[Symbol.dispose]?.()
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
this.clear()
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A set that asynchronously cleans up its elements when disposed.
|
|
170
|
+
*/
|
|
171
|
+
export class AsyncDisposableSet<T extends AsyncDisposable> extends Set<T> implements AsyncDisposable {
|
|
172
|
+
public async [Symbol.asyncDispose]() {
|
|
173
|
+
for (const node of this) {
|
|
174
|
+
await node[Symbol.asyncDispose]?.()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this.clear()
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { mkdirSync } from "node:fs"
|
|
8
|
+
import { setTimeout } from "node:timers/promises"
|
|
9
|
+
|
|
10
|
+
import type { Alpha3bLanguageCode } from "@mailwoman/core/resources/languages"
|
|
11
|
+
import { dirname } from "path-ts"
|
|
12
|
+
|
|
13
|
+
import { PlacetypeDataSource, type PlacetypeDataSourceOptions } from "./PlacetypeDataSource.ts"
|
|
14
|
+
import type { WhosOnFirstPlacetype } from "./placetypes/definition.ts"
|
|
15
|
+
|
|
16
|
+
export class DataSourceCache extends DisposableStack {
|
|
17
|
+
#placetypeToLanguage = new Map<WhosOnFirstPlacetype, Map<Alpha3bLanguageCode, PlacetypeDataSource>>()
|
|
18
|
+
|
|
19
|
+
public override [Symbol.toStringTag] = "DataSourceCache"
|
|
20
|
+
|
|
21
|
+
public override [Symbol.dispose]() {
|
|
22
|
+
super[Symbol.dispose]()
|
|
23
|
+
|
|
24
|
+
this.#placetypeToLanguage.clear()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public open({ placetype, languageCode, dataDirectory }: PlacetypeDataSourceOptions): PlacetypeDataSource {
|
|
28
|
+
let languageToDataSource = this.#placetypeToLanguage.get(placetype)
|
|
29
|
+
|
|
30
|
+
if (!languageToDataSource) {
|
|
31
|
+
languageToDataSource = new Map()
|
|
32
|
+
this.#placetypeToLanguage.set(placetype, languageToDataSource)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let dataSource = languageToDataSource.get(languageCode)
|
|
36
|
+
|
|
37
|
+
if (dataSource) return dataSource
|
|
38
|
+
|
|
39
|
+
const databasePath = PlacetypeDataSource.createPath({ placetype, languageCode, dataDirectory }).toString()
|
|
40
|
+
|
|
41
|
+
mkdirSync(dirname(databasePath), { recursive: true })
|
|
42
|
+
|
|
43
|
+
dataSource = new PlacetypeDataSource(databasePath)
|
|
44
|
+
|
|
45
|
+
languageToDataSource.set(languageCode, dataSource)
|
|
46
|
+
|
|
47
|
+
return dataSource
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Given a callback, attempt to run it up to `attempts` times.
|
|
53
|
+
*/
|
|
54
|
+
export async function tryWithBackoff<T>(attempts: number, callback: () => T): Promise<T> {
|
|
55
|
+
let lastError: unknown
|
|
56
|
+
|
|
57
|
+
for (let i = 0; i < attempts; i++) {
|
|
58
|
+
try {
|
|
59
|
+
const result = await callback()
|
|
60
|
+
|
|
61
|
+
return result
|
|
62
|
+
} catch (error) {
|
|
63
|
+
lastError = error
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// We try to avoid contention by giving a pause between attempts.
|
|
67
|
+
const delay = Math.floor(Math.random() * 1000) + 1000 * attempts
|
|
68
|
+
await setTimeout(delay)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
throw lastError
|
|
72
|
+
}
|