Fix European trip return heuristic for weekend location tracking

Adjust European short trip heuristic from >3 days to >1 day to correctly
detect when user has returned home from European trips. This fixes the
April 29-30, 2023 case where the location incorrectly showed "Sankt Georg, Hamburg"
instead of "Bristol" when the user was free (no events scheduled) after
the foss-north trip ended on April 27.

The previous logic required more than 3 days to pass before assuming
return home from European countries, but for short European trips by
rail/ferry, users typically return within 1-2 days.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2025-07-16 06:38:37 +02:00
parent 663dc479c2
commit ea4980a5d7
6407 changed files with 1072847 additions and 18 deletions

44
node_modules/@sindresorhus/merge-streams/index.d.ts generated vendored Normal file
View file

@ -0,0 +1,44 @@
import {type Readable} from 'node:stream';
/**
Merges an array of [readable streams](https://nodejs.org/api/stream.html#readable-streams) and returns a new readable stream that emits data from the individual streams as it arrives.
If you provide an empty array, it returns an already-ended stream.
@example
```
import mergeStreams from '@sindresorhus/merge-streams';
const stream = mergeStreams([streamA, streamB]);
for await (const chunk of stream) {
console.log(chunk);
//=> 'A1'
//=> 'B1'
//=> 'A2'
//=> 'B2'
}
```
*/
export default function mergeStreams(streams: Readable[]): MergedStream;
/**
A single stream combining the output of multiple streams.
*/
export class MergedStream extends Readable {
/**
Pipe a new readable stream.
Throws if `MergedStream` has already ended.
*/
add(stream: Readable): void;
/**
Unpipe a stream previously added using either `mergeStreams(streams)` or `MergedStream.add(stream)`.
Returns `false` if the stream was not previously added, or if it was already removed by `MergedStream.remove(stream)`.
The removed stream is not automatically ended.
*/
remove(stream: Readable): boolean;
}

223
node_modules/@sindresorhus/merge-streams/index.js generated vendored Normal file
View file

@ -0,0 +1,223 @@
import {on, once} from 'node:events';
import {PassThrough as PassThroughStream} from 'node:stream';
import {finished} from 'node:stream/promises';
export default function mergeStreams(streams) {
if (!Array.isArray(streams)) {
throw new TypeError(`Expected an array, got \`${typeof streams}\`.`);
}
for (const stream of streams) {
validateStream(stream);
}
const objectMode = streams.some(({readableObjectMode}) => readableObjectMode);
const highWaterMark = getHighWaterMark(streams, objectMode);
const passThroughStream = new MergedStream({
objectMode,
writableHighWaterMark: highWaterMark,
readableHighWaterMark: highWaterMark,
});
for (const stream of streams) {
passThroughStream.add(stream);
}
if (streams.length === 0) {
endStream(passThroughStream);
}
return passThroughStream;
}
const getHighWaterMark = (streams, objectMode) => {
if (streams.length === 0) {
// @todo Use `node:stream` `getDefaultHighWaterMark(objectMode)` in next major release
return 16_384;
}
const highWaterMarks = streams
.filter(({readableObjectMode}) => readableObjectMode === objectMode)
.map(({readableHighWaterMark}) => readableHighWaterMark);
return Math.max(...highWaterMarks);
};
class MergedStream extends PassThroughStream {
#streams = new Set([]);
#ended = new Set([]);
#aborted = new Set([]);
#onFinished;
add(stream) {
validateStream(stream);
if (this.#streams.has(stream)) {
return;
}
this.#streams.add(stream);
this.#onFinished ??= onMergedStreamFinished(this, this.#streams);
endWhenStreamsDone({
passThroughStream: this,
stream,
streams: this.#streams,
ended: this.#ended,
aborted: this.#aborted,
onFinished: this.#onFinished,
});
stream.pipe(this, {end: false});
}
remove(stream) {
validateStream(stream);
if (!this.#streams.has(stream)) {
return false;
}
stream.unpipe(this);
return true;
}
}
const onMergedStreamFinished = async (passThroughStream, streams) => {
updateMaxListeners(passThroughStream, PASSTHROUGH_LISTENERS_COUNT);
const controller = new AbortController();
try {
await Promise.race([
onMergedStreamEnd(passThroughStream, controller),
onInputStreamsUnpipe(passThroughStream, streams, controller),
]);
} finally {
controller.abort();
updateMaxListeners(passThroughStream, -PASSTHROUGH_LISTENERS_COUNT);
}
};
const onMergedStreamEnd = async (passThroughStream, {signal}) => {
await finished(passThroughStream, {signal, cleanup: true});
};
const onInputStreamsUnpipe = async (passThroughStream, streams, {signal}) => {
for await (const [unpipedStream] of on(passThroughStream, 'unpipe', {signal})) {
if (streams.has(unpipedStream)) {
unpipedStream.emit(unpipeEvent);
}
}
};
const validateStream = stream => {
if (typeof stream?.pipe !== 'function') {
throw new TypeError(`Expected a readable stream, got: \`${typeof stream}\`.`);
}
};
const endWhenStreamsDone = async ({passThroughStream, stream, streams, ended, aborted, onFinished}) => {
updateMaxListeners(passThroughStream, PASSTHROUGH_LISTENERS_PER_STREAM);
const controller = new AbortController();
try {
await Promise.race([
afterMergedStreamFinished(onFinished, stream),
onInputStreamEnd({passThroughStream, stream, streams, ended, aborted, controller}),
onInputStreamUnpipe({stream, streams, ended, aborted, controller}),
]);
} finally {
controller.abort();
updateMaxListeners(passThroughStream, -PASSTHROUGH_LISTENERS_PER_STREAM);
}
if (streams.size === ended.size + aborted.size) {
if (ended.size === 0 && aborted.size > 0) {
abortStream(passThroughStream);
} else {
endStream(passThroughStream);
}
}
};
// This is the error thrown by `finished()` on `stream.destroy()`
const isAbortError = error => error?.code === 'ERR_STREAM_PREMATURE_CLOSE';
const afterMergedStreamFinished = async (onFinished, stream) => {
try {
await onFinished;
abortStream(stream);
} catch (error) {
if (isAbortError(error)) {
abortStream(stream);
} else {
errorStream(stream, error);
}
}
};
const onInputStreamEnd = async ({passThroughStream, stream, streams, ended, aborted, controller: {signal}}) => {
try {
await finished(stream, {signal, cleanup: true, readable: true, writable: false});
if (streams.has(stream)) {
ended.add(stream);
}
} catch (error) {
if (signal.aborted || !streams.has(stream)) {
return;
}
if (isAbortError(error)) {
aborted.add(stream);
} else {
errorStream(passThroughStream, error);
}
}
};
const onInputStreamUnpipe = async ({stream, streams, ended, aborted, controller: {signal}}) => {
await once(stream, unpipeEvent, {signal});
streams.delete(stream);
ended.delete(stream);
aborted.delete(stream);
};
const unpipeEvent = Symbol('unpipe');
const endStream = stream => {
if (stream.writable) {
stream.end();
}
};
const abortStream = stream => {
if (stream.readable || stream.writable) {
stream.destroy();
}
};
// `stream.destroy(error)` crashes the process with `uncaughtException` if no `error` event listener exists on `stream`.
// We take care of error handling on user behalf, so we do not want this to happen.
const errorStream = (stream, error) => {
if (!stream.destroyed) {
stream.once('error', noop);
stream.destroy(error);
}
};
const noop = () => {};
const updateMaxListeners = (passThroughStream, increment) => {
const maxListeners = passThroughStream.getMaxListeners();
if (maxListeners !== 0 && maxListeners !== Number.POSITIVE_INFINITY) {
passThroughStream.setMaxListeners(maxListeners + increment);
}
};
// Number of times `passThroughStream.on()` is called regardless of streams:
// - once due to `finished(passThroughStream)`
// - once due to `on(passThroughStream)`
const PASSTHROUGH_LISTENERS_COUNT = 2;
// Number of times `passThroughStream.on()` is called per stream:
// - once due to `stream.pipe(passThroughStream)`
const PASSTHROUGH_LISTENERS_PER_STREAM = 1;

9
node_modules/@sindresorhus/merge-streams/license generated vendored Normal file
View file

@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

49
node_modules/@sindresorhus/merge-streams/package.json generated vendored Normal file
View file

@ -0,0 +1,49 @@
{
"name": "@sindresorhus/merge-streams",
"version": "2.3.0",
"description": "Merge multiple streams into a unified stream",
"license": "MIT",
"repository": "sindresorhus/merge-streams",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=18"
},
"scripts": {
"test": "xo && c8 ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"merge",
"stream",
"streams",
"readable",
"passthrough",
"interleave",
"interleaved",
"unify",
"unified"
],
"devDependencies": {
"@types/node": "^20.8.9",
"ava": "^6.1.0",
"c8": "^9.1.0",
"tempfile": "^5.0.0",
"tsd": "^0.30.4",
"typescript": "^5.2.2",
"xo": "^0.56.0"
}
}

53
node_modules/@sindresorhus/merge-streams/readme.md generated vendored Normal file
View file

@ -0,0 +1,53 @@
# merge-streams
> Merge multiple streams into a unified stream
## Install
```sh
npm install @sindresorhus/merge-streams
```
## Usage
```js
import mergeStreams from '@sindresorhus/merge-streams';
const stream = mergeStreams([streamA, streamB]);
for await (const chunk of stream) {
console.log(chunk);
//=> 'A1'
//=> 'B1'
//=> 'A2'
//=> 'B2'
}
```
## API
### `mergeStreams(streams: stream.Readable[]): MergedStream`
Merges an array of [readable streams](https://nodejs.org/api/stream.html#readable-streams) and returns a new readable stream that emits data from the individual streams as it arrives.
If you provide an empty array, it returns an already-ended stream.
#### `MergedStream`
_Type_: `stream.Readable`
A single stream combining the output of multiple streams.
##### `MergedStream.add(stream: stream.Readable): void`
Pipe a new readable stream.
Throws if `MergedStream` has already ended.
##### `MergedStream.remove(stream: stream.Readable): boolean`
Unpipe a stream previously added using either [`mergeStreams(streams)`](#mergestreamsstreams-streamreadable-mergedstream) or [`MergedStream.add(stream)`](#mergedstreamaddstream-streamreadable-void).
Returns `false` if the stream was not previously added, or if it was already removed by `MergedStream.remove(stream)`.
The removed stream is not automatically ended.