diff --git a/src/search/placeSuggestions.js b/src/search/placeSuggestions.js index 99a68b62ce..6fa20b11b5 100644 --- a/src/search/placeSuggestions.js +++ b/src/search/placeSuggestions.js @@ -87,6 +87,12 @@ function foldLine(text) { .replace(/(?![0-9])\p{Nd}/gu, '0'); } +/** A US ZIP+4 after a state's code or name, as an address writes it. */ +const US_ZIP4 = new RegExp( + String.raw`(?<=(?:^|\s)(?:[a-z]{2}|[a-z]\.[a-z]|Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Mass|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming|Puerto Rico)\.?,?\s)\d{5}-\d{4}\b`, + 'giu', +); + /** * Why a line may be private: 'number' when only a long run of digits makes * it so, 'other' for anything else, or null. @@ -138,8 +144,9 @@ function privacyReason(text) { ? line : line .replace( - // A street word before ("030 12345" is a phone number). - /(?<=\p{L}\.?\s)\d{1,4}[a-z]?,?\s+\d{5}(?:-\d{3})?(?=\s+\p{L}|$)/giu, + // After a street word, or before the city ("12, 10115 Berlin"); + // "030 12345" alone is a phone number. + /(?<=\p{L}\.?\s)\d{1,4}[a-z]?,?\s+\d{5}(?:-\d{3})?(?=\s+\p{L}|$)|(?<=^|\s)\d{1,4}[a-z]?,?\s+\d{5}(?:-\d{3})?(?=\s+\p{L})/giu, ' ', ) // Other postcodes after a street's house number and before the city (a @@ -151,9 +158,10 @@ function privacyReason(text) { /(?<=\p{L}\.?\s)\d{1,4}[a-z]?(?:,\s*\d{4}(?:-\d{3})?|,?\s+(?:\d{2}-\d{3}|\d{3} \d{2}|\d{6}))(?=\s+\p{L})/giu, ' x ', ) - // ZIP+4 after a state's two letters ("DC 20500-0003"), not after - // any word ("Alice 12345-6789"). - .replace(/(?<=(?:^|\s)\p{L}{2},?\s)\d{5}-\d{4}\b/gu, ' x ') + // ZIP+4 after a state ("DC 20500-0003", "MA. 02139-4307", + // "California 90210-1234"), not after any word ("Alice + // 12345-6789"). + .replace(US_ZIP4, ' x ') .replace(/\b\d{5}-\d{3}\b/g, ' x ') .replace( /〒\s?\d{3}-\d{4}|\b\d{3}-\d{4}(?=\s*[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}])/gu, @@ -176,7 +184,9 @@ function privacyReason(text) { /(? Date.now()) { - let timer; - const pause = new Promise((resolve) => { - timer = setTimeout(resolve, shared.nextRequestAt - Date.now()); - }); + // The same text already on its way is that request's answer, unless + // its caller cancels it: then this one asks for itself. + const inFlight = shared.pending.get(key); + if (inFlight) try { - await waitForSignal(pause, signal); - } finally { - clearTimeout(timer); + return await waitForSignal(inFlight, signal); + } catch { + signal?.throwIfAborted(); + } + const ask = async () => { + // Concurrent callers recheck the gap after waking before sending. + while (shared.nextRequestAt > Date.now()) { + let timer; + const pause = new Promise((resolve) => { + timer = setTimeout(resolve, shared.nextRequestAt - Date.now()); + }); + try { + await waitForSignal(pause, signal); + } finally { + clearTimeout(timer); + } } - } - signal?.throwIfAborted(); - // The same text asked again while this one waited is answered now. - if (cache.has(key)) return cache.get(key); - if (Date.now() < shared.pausedUntil) return []; - const url = new URL(photonSearchUrl(text, { endpoint, limit })); - if (['en', 'de', 'fr', 'it'].includes(language)) - url.searchParams.set('lang', language); - - shared.nextRequestAt = Date.now() + MIN_GAP_MS; - let features; - try { - features = await requestWithDeadline( - async (requestSignal) => { - const response = await fetchImpl(url, { signal: requestSignal }); - requestSignal.throwIfAborted(); - if (!response.ok) throw new Error(`HTTP ${response.status}`); - const data = await response.json(); - return data?.features; - }, - { signal, timeoutMs: TIMEOUT_MS }, - ); - } catch (error) { signal?.throwIfAborted(); - // A refusal, a timeout or a failed request (a refusal sent without - // CORS headers arrives as one) all hold suggestions off a while. - void error; - shared.pausedUntil = Date.now() + PAUSE_AFTER_REFUSAL_MS; - return []; - } - signal?.throwIfAborted(); - if (!Array.isArray(features)) return []; + // The same text asked again while this one waited is answered now. + if (cache.has(key)) return cache.get(key); + if (Date.now() < shared.pausedUntil) return []; + const url = new URL(photonSearchUrl(text, { endpoint, limit })); + if (['en', 'de', 'fr', 'it'].includes(language)) + url.searchParams.set('lang', language); - const seen = new Set(); - const places = []; - for (const feature of features) { - const normalized = normalizePhotonFeature(feature); - if (!normalized) continue; - const { - city, - state, - country, - countrycode: countryCode, - osm_value: osmType, - type, - street, - housenumber: houseNumber, - } = feature.properties ?? {}; - // A house has no name of its own: its street and number are its - // name, so two houses on one street stay two places. - const address = street - ? [houseNumber, street].filter(Boolean).join(' ') - : ''; - const name = normalized.name || address || city || state || country; - if (!name) continue; - const detail = [...new Set([city, state, country])] - .filter((part) => part && part !== name) - .join(', '); - const id = `${name}|${detail}`; - if (seen.has(id)) continue; - seen.add(id); - const { southwest, northeast } = normalized.viewport ?? {}; - places.push({ - name, - detail, - latitude: normalized.lat, - longitude: normalized.lng, - type: (osmType === 'administrative' ? type : osmType || type) || '', - state: state || null, - country: country || null, - countryCode: /^[a-z]{2}$/i.test(countryCode ?? '') - ? countryCode.toUpperCase() - : null, - extent: southwest - ? [southwest.lng, northeast.lat, northeast.lng, southwest.lat] - : null, - }); - if (places.length >= limit) break; - } + shared.nextRequestAt = Date.now() + MIN_GAP_MS; + let features; + try { + features = await requestWithDeadline( + async (requestSignal) => { + const response = await fetchImpl(url, { signal: requestSignal }); + requestSignal.throwIfAborted(); + if (!response.ok) throw new Error(`HTTP ${response.status}`); + const data = await response.json(); + return data?.features; + }, + { signal, timeoutMs: TIMEOUT_MS }, + ); + } catch (error) { + signal?.throwIfAborted(); + // A refusal, a timeout or a failed request (a refusal sent without + // CORS headers arrives as one) all hold suggestions off a while. + void error; + shared.pausedUntil = Date.now() + PAUSE_AFTER_REFUSAL_MS; + return []; + } + signal?.throwIfAborted(); + if (!Array.isArray(features)) return []; - cache.set(key, places); - if (cache.size > CACHE_LIMIT) cache.delete(cache.keys().next().value); - return places; + const seen = new Set(); + const places = []; + for (const feature of features) { + const normalized = normalizePhotonFeature(feature); + if (!normalized) continue; + const { + city, + state, + country, + countrycode: countryCode, + osm_value: osmType, + type, + street, + housenumber: houseNumber, + } = feature.properties ?? {}; + // A house has no name of its own: its street and number are its + // name, so two houses on one street stay two places. + const address = + street && houseNumber ? `${houseNumber} ${street}` : ''; + const name = normalized.name || address || city || state || country; + if (!name) continue; + const detail = [...new Set([city, state, country])] + .filter((part) => part && part !== name) + .join(', '); + const id = `${name}|${detail}`; + if (seen.has(id)) continue; + seen.add(id); + const { southwest, northeast } = normalized.viewport ?? {}; + places.push({ + name, + detail, + latitude: normalized.lat, + longitude: normalized.lng, + type: (osmType === 'administrative' ? type : osmType || type) || '', + state: state || null, + country: country || null, + countryCode: /^[a-z]{2}$/i.test(countryCode ?? '') + ? countryCode.toUpperCase() + : null, + extent: southwest + ? [southwest.lng, northeast.lat, northeast.lng, southwest.lat] + : null, + }); + if (places.length >= limit) break; + } + + cache.set(key, places); + if (cache.size > CACHE_LIMIT) cache.delete(cache.keys().next().value); + return places; + }; + const asking = ask(); + shared.pending.set(key, asking); + try { + return await asking; + } finally { + if (shared.pending.get(key) === asking) shared.pending.delete(key); + } }, }; } diff --git a/src/search/placeSuggestions.test.mjs b/src/search/placeSuggestions.test.mjs index ca08fe6744..7063008116 100644 --- a/src/search/placeSuggestions.test.mjs +++ b/src/search/placeSuggestions.test.mjs @@ -385,3 +385,45 @@ test('two houses on one street are two suggestions, asked once', async () => { assert.deepEqual(second, first); assert.equal(sent, 1); }); + +test('streets named for keys, tokens and codes, and ZIP+4 after a state, are places', () => { + for (const line of [ + 'Secret Key Lake', + 'Access Token Road', + 'Api Key Lane', + 'GATE CODE LANE', + 'SECURITY CODE PLAZA', + 'OTP Bank', + 'MFA BOSTON', + 'Auth Code ABCD Street', + 'California 90210-1234', + 'Austin Texas 78701-1234', + 'Cambridge MA. 02139-4307', + '12, 10115 Berlin', + ]) + assert.equal(mayBePrivate(line), false, line); + for (const line of [ + 'gate code: abcd', + 'PIN A7B2', + 'PIN is A7B2', + 'OTP-ABCDEF', + 'OTP – ABCDEF', + 'login code ab-12-cd', + 'auth_token=abc', + ]) + assert.equal(mayBePrivate(line), true, line); +}); + +test('the same text asked again while its request is out is sent once', async () => { + let sent = 0; + const service = createPlaceSuggester({ + endpoint: 'https://fixture.test/api/', + fetchImpl: async () => { + sent++; + await new Promise((resolve) => setTimeout(resolve, 700)); + return Response.json({ features: [] }); + }, + }); + await Promise.all([service.suggest('Paris'), service.suggest('Paris')]); + assert.equal(sent, 1); +});