Skip to content

Commit

Permalink
Add version to URLs for CDN cache
Browse files Browse the repository at this point in the history
  • Loading branch information
keichan34 committed Oct 4, 2024
1 parent a1e3efc commit 4c3afad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
14 changes: 11 additions & 3 deletions src/japanese-addresses-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ export function prefectureName(pref: SinglePrefecture): string {
return pref.pref
}

type Api<T> = {
meta: {
/// データ更新日(UNIX時間; 秒)
updated: number
}
data: T
}

/**
* 都道府県、市区町村一覧API
* 政令都市の場合は区で区切ります
* @file api/ja.json
*/
export type PrefectureApi = SinglePrefecture[]
export type PrefectureApi = Api<SinglePrefecture[]>

export type SingleCity = {
/// 全国地方公共団体コード
Expand All @@ -46,7 +54,7 @@ export function cityName(city: SingleCity): string {
* 市区町村一覧API
* @file api/ja/{都道府県名}.json
*/
export type CityApi = SingleCity[]
export type CityApi = Api<SingleCity[]>

export type SingleMachiAza = {
/// ABR上の「町字ID」
Expand Down Expand Up @@ -74,7 +82,7 @@ export function machiAzaName(m: SingleMachiAza): string {
* 町字一覧API
* @file api/ja/{都道府県名}/{市区町村名}.json
*/
export type MachiAzaApi = SingleMachiAza[]
export type MachiAzaApi = Api<SingleMachiAza[]>

export type SingleRsdt = {
/// 街区符号
Expand Down
26 changes: 19 additions & 7 deletions src/lib/cacheRegexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ export const cachePrefectures = (data: PrefectureList) => {
return (cachedPrefectures = data)
}

export const getPrefectureRegexPatterns = (data: PrefectureApi) => {
export const getPrefectureRegexPatterns = (api: PrefectureApi) => {
if (cachedPrefecturePatterns) {
return cachedPrefecturePatterns
}

const data = api.data
cachedPrefecturePatterns = data.map<[SinglePrefecture, string]>((pref) => {
const _pref = pref.pref.replace(/(|||)$/, '') // `東京` の様に末尾の `都府県` が抜けた住所に対応
const pattern = `^${_pref}(都|道|府|県)?`
Expand Down Expand Up @@ -117,6 +118,7 @@ export const getCityRegexPatterns = (pref: SinglePrefecture) => {
export const getTowns = async (
prefObj: SinglePrefecture,
cityObj: SingleCity,
apiVersion: number,
) => {
const pref = prefectureName(prefObj)
const city = cityName(cityObj)
Expand All @@ -128,7 +130,7 @@ export const getTowns = async (
}

const townsResp = await __internals.fetch(
['', encodeURI(pref), encodeURI(city) + '.json'].join('/'),
['', encodeURI(pref), encodeURI(city) + `.json?v=${apiVersion}`].join('/'),
{},
)
const towns = (await townsResp.json()) as MachiAzaApi
Expand All @@ -139,6 +141,7 @@ async function fetchMetadata(
kind: '地番' | '住居表示',
pref: SinglePrefecture,
city: SingleCity,
apiVersion: number,
) {
const prefN = prefectureName(pref)
const cityN = cityName(city)
Expand All @@ -150,7 +153,11 @@ async function fetchMetadata(
throw new Error('metadata fetch failure')
}
const resp = await __internals.fetch(
['', encodeURI(prefN), encodeURI(`${cityN}-${kind}.txt`)].join('/'),
[
'',
encodeURI(prefN),
encodeURI(`${cityN}-${kind}.txt?v=${apiVersion}`),
].join('/'),
{
offset: tries * 50_000,
length: 50_000,
Expand Down Expand Up @@ -259,11 +266,12 @@ export const getRsdt = async (
pref: SinglePrefecture,
city: SingleCity,
town: SingleTown,
apiVersion: number,
) => {
const metadataParsed = await fetchFromCache(
`住居表示-${pref.code}-${city.code}`,
async () => {
const metadata = await fetchMetadata('住居表示', pref, city)
const metadata = await fetchMetadata('住居表示', pref, city, apiVersion)
return parseMetadata(metadata)
},
)
Expand Down Expand Up @@ -296,11 +304,12 @@ export const getChiban = async (
pref: SinglePrefecture,
city: SingleCity,
town: SingleTown,
apiVersion: number,
) => {
const metadataParsed = await fetchFromCache(
`地番-${pref.code}-${city.code}`,
async () => {
const metadata = await fetchMetadata('地番', pref, city)
const metadata = await fetchMetadata('地番', pref, city, apiVersion)
return parseMetadata(metadata)
},
)
Expand Down Expand Up @@ -341,11 +350,13 @@ const isKanjiNumberFollewedByCho = (targetTownName: string) => {
export const getTownRegexPatterns = async (
pref: SinglePrefecture,
city: SingleCity,
apiVersion: number,
) =>
fetchFromCache<[SingleTown, string][]>(
`${pref.code}-${city.code}`,
async () => {
const pre_towns = await getTowns(pref, city)
const api = await getTowns(pref, city, apiVersion)
const pre_towns = api.data
const townSet = new Set(pre_towns.map((town) => machiAzaName(town)))
const towns: (
| SingleMachiAza
Expand Down Expand Up @@ -467,12 +478,13 @@ export const getTownRegexPatterns = async (
)

export const getSameNamedPrefectureCityRegexPatterns = (
prefList: PrefectureList,
prefApi: PrefectureApi,
) => {
if (typeof cachedSameNamedPrefectureCityRegexPatterns !== 'undefined') {
return cachedSameNamedPrefectureCityRegexPatterns
}

const prefList = prefApi.data
const _prefs = prefList.map((pref) => {
return pref.pref.replace(/[|||]$/, '')
})
Expand Down
24 changes: 15 additions & 9 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ const normalizeTownName = async (
input: string,
pref: SinglePrefecture,
city: SingleCity,
apiVersion: number,
) => {
input = input.trim().replace(/^/, '')
const townPatterns = await getTownRegexPatterns(pref, city)
const townPatterns = await getTownRegexPatterns(pref, city, apiVersion)

const regexPrefixes = ['^']
if (city.city === '京都市') {
Expand Down Expand Up @@ -136,6 +137,7 @@ async function normalizeAddrPart(
pref: SinglePrefecture,
city: SingleCity,
town: SingleMachiAza,
apiVersion: number,
): Promise<NormalizedAddrPart> {
const match = addr.match(
/^([1-9][0-9]*)(?:-([1-9][0-9]*))?(?:-([1-9][0-9]*))?/,
Expand All @@ -147,7 +149,7 @@ async function normalizeAddrPart(
}
// TODO: rsdtの場合はrsdtと地番を両方取得する
if (town.rsdt) {
const res = await getRsdt(pref, city, town)
const res = await getRsdt(pref, city, town, apiVersion)
for (const rsdt of res) {
const addrPart = rsdtToString(rsdt)
if (match[0] === addrPart) {
Expand All @@ -158,7 +160,7 @@ async function normalizeAddrPart(
}
}
} else {
const res = await getChiban(pref, city, town)
const res = await getChiban(pref, city, town, apiVersion)
for (const chiban of res) {
const addrPart = chibanToString(chiban)
if (match[0] === addrPart) {
Expand Down Expand Up @@ -195,6 +197,7 @@ export const normalize: Normalizer = async (
// 都道府県名の正規化

const prefectures = await getPrefectures()
const apiVersion = prefectures.meta.updated
const prefPatterns = getPrefectureRegexPatterns(prefectures)
const sameNamedPrefectureCityRegexPatterns =
getSameNamedPrefectureCityRegexPatterns(prefectures)
Expand Down Expand Up @@ -226,7 +229,7 @@ export const normalize: Normalizer = async (
city: SingleCity
other: string
}[] = []
for (const _pref of prefectures) {
for (const _pref of prefectures.data) {
const cityPatterns = getCityRegexPatterns(_pref)

other = other.trim()
Expand All @@ -247,7 +250,12 @@ export const normalize: Normalizer = async (
pref = matched[0].pref
} else {
for (const m of matched) {
const normalized = await normalizeTownName(m.other, m.pref, m.city)
const normalized = await normalizeTownName(
m.other,
m.pref,
m.city,
apiVersion,
)
if (normalized) {
pref = m.pref
city = m.city
Expand Down Expand Up @@ -276,7 +284,7 @@ export const normalize: Normalizer = async (

// 町丁目以降の正規化
if (pref && city && option.level >= 3) {
const normalized = await normalizeTownName(other, pref, city)
const normalized = await normalizeTownName(other, pref, city, apiVersion)
if (normalized) {
town = normalized.town
other = normalized.other
Expand Down Expand Up @@ -359,12 +367,10 @@ export const normalize: Normalizer = async (

const normalizedAddrPart = await normalizeAddrPart(
other,

pref!,

city!,

town!,
apiVersion,
)
// TODO: rsdtと地番を両方対応した時に両方返すけど、今はrsdtを優先する
if (normalizedAddrPart.rsdt) {
Expand Down

0 comments on commit 4c3afad

Please sign in to comment.