drone-path-engine 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +33 -2
- package/THIRD-PARTY-NOTICES.md +11 -0
- package/dist/assets.d.ts +34 -0
- package/dist/assets.js +60 -0
- package/dist/config.d.ts +208 -0
- package/dist/config.js +577 -0
- package/dist/configfile.d.ts +20 -0
- package/dist/configfile.js +82 -0
- package/dist/decimate.d.ts +2 -0
- package/dist/decimate.js +1 -0
- package/dist/ffmpeg-output.d.ts +9 -0
- package/dist/ffmpeg-output.js +40 -0
- package/dist/logging.d.ts +1 -0
- package/dist/logging.js +1 -0
- package/dist/paths.d.ts +4 -0
- package/dist/paths.js +21 -0
- package/dist/pipeline.d.ts +90 -0
- package/dist/pipeline.js +436 -0
- package/dist/plan.d.ts +47 -0
- package/dist/plan.js +113 -0
- package/dist/png.d.ts +1 -0
- package/dist/png.js +32 -0
- package/dist/range.d.ts +19 -0
- package/dist/range.js +50 -0
- package/dist/recordings.d.ts +5 -0
- package/dist/recordings.js +45 -0
- package/dist/render-page.d.ts +35 -0
- package/dist/render-page.js +85 -0
- package/dist/render.d.ts +57 -0
- package/dist/render.js +397 -0
- package/dist/renderer/boot.d.ts +4 -0
- package/dist/renderer/boot.js +28 -0
- package/dist/renderer/dom.d.ts +28 -0
- package/dist/renderer/dom.js +179 -0
- package/dist/renderer/framemath.d.ts +5 -0
- package/dist/renderer/framemath.js +30 -0
- package/dist/renderer/scene.d.ts +38 -0
- package/dist/renderer/scene.js +394 -0
- package/dist/renderer/theme.d.ts +13 -0
- package/dist/renderer/theme.js +82 -0
- package/dist/renderer/tilesources.d.ts +14 -0
- package/dist/renderer/tilesources.js +38 -0
- package/dist/renderer/trail.d.ts +18 -0
- package/dist/renderer/trail.js +42 -0
- package/dist/restyle.d.ts +13 -0
- package/dist/restyle.js +16 -0
- package/dist/serve.d.ts +6 -0
- package/dist/serve.js +27 -0
- package/dist/tilecache.d.ts +12 -0
- package/dist/tilecache.js +21 -0
- package/dist/tiles.d.ts +25 -0
- package/dist/tiles.js +215 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/package.json +37 -4
package/dist/serve.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { resolveAsset } from './assets.js';
|
|
6
|
+
const require_ = createRequire(import.meta.url);
|
|
7
|
+
const LEAFLET_DIR = path.dirname(require_.resolve('leaflet/dist/leaflet.js'));
|
|
8
|
+
export const RENDERER_DIR = fileURLToPath(new URL('./renderer/', import.meta.url));
|
|
9
|
+
export function serveAsset(url, res, routes) {
|
|
10
|
+
const asset = resolveAsset(url, { ...routes, leafletDir: LEAFLET_DIR });
|
|
11
|
+
if (asset.kind === 'unrouted')
|
|
12
|
+
return false;
|
|
13
|
+
if (asset.kind === 'file') {
|
|
14
|
+
let file = asset.filePath;
|
|
15
|
+
if (!fs.existsSync(file) && asset.fallbackPath && fs.existsSync(asset.fallbackPath)) {
|
|
16
|
+
file = asset.fallbackPath;
|
|
17
|
+
}
|
|
18
|
+
if (fs.existsSync(file) && fs.statSync(file).isFile()) {
|
|
19
|
+
res.writeHead(200, { 'Content-Type': asset.mime });
|
|
20
|
+
res.end(fs.readFileSync(file));
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
25
|
+
res.end('not found');
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MapStyle } from './config.js';
|
|
2
|
+
import type { TileCoord } from './plan.js';
|
|
3
|
+
interface TileRef {
|
|
4
|
+
style: MapStyle;
|
|
5
|
+
tile: TileCoord;
|
|
6
|
+
}
|
|
7
|
+
export declare function parseTilePathname(pathname: string): TileRef | null;
|
|
8
|
+
export declare function tilePaths(cacheDir: string, style: MapStyle, tile: TileCoord): {
|
|
9
|
+
file: string;
|
|
10
|
+
placeholder: string;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { TILE_SOURCES } from './renderer/tilesources.js';
|
|
3
|
+
function cacheKeyFor(style) {
|
|
4
|
+
return TILE_SOURCES[style].cacheKey;
|
|
5
|
+
}
|
|
6
|
+
export function parseTilePathname(pathname) {
|
|
7
|
+
const m = /^\/tiles\/([a-z0-9_-]+)\/(\d+)\/(\d+)\/(\d+)\.png$/.exec(pathname);
|
|
8
|
+
if (!m)
|
|
9
|
+
return null;
|
|
10
|
+
const style = Object.hasOwn(TILE_SOURCES, m[1]) ? m[1] : null;
|
|
11
|
+
return style === null
|
|
12
|
+
? null
|
|
13
|
+
: { style, tile: { z: Number(m[2]), x: Number(m[3]), y: Number(m[4]) } };
|
|
14
|
+
}
|
|
15
|
+
export function tilePaths(cacheDir, style, tile) {
|
|
16
|
+
const dir = path.join(cacheDir, cacheKeyFor(style), String(tile.z), String(tile.x));
|
|
17
|
+
return {
|
|
18
|
+
file: path.join(dir, `${tile.y}.png`),
|
|
19
|
+
placeholder: path.join(dir, `${tile.y}.placeholder.png`),
|
|
20
|
+
};
|
|
21
|
+
}
|
package/dist/tiles.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { MapStyle } from './config.js';
|
|
2
|
+
import type { TileCoord } from './plan.js';
|
|
3
|
+
type TileStatus = 'cached' | 'downloaded' | 'failed';
|
|
4
|
+
export interface PrefetchStats {
|
|
5
|
+
downloaded: number;
|
|
6
|
+
failed: number;
|
|
7
|
+
total: number;
|
|
8
|
+
}
|
|
9
|
+
export interface WarmOptions {
|
|
10
|
+
style: MapStyle;
|
|
11
|
+
tiles: TileCoord[];
|
|
12
|
+
log?: (msg: string) => void;
|
|
13
|
+
onTile?: (status: TileStatus) => void;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
interface PrefetchOptions extends WarmOptions {
|
|
17
|
+
cacheDir: string;
|
|
18
|
+
}
|
|
19
|
+
interface TileTraffic {
|
|
20
|
+
ensure(style: MapStyle, tile: TileCoord): Promise<string>;
|
|
21
|
+
stream(options: WarmOptions): Promise<PrefetchStats>;
|
|
22
|
+
}
|
|
23
|
+
export declare function createTileTraffic(cacheDir: string, version?: string): TileTraffic;
|
|
24
|
+
export declare function prefetchTiles(options: PrefetchOptions): Promise<PrefetchStats>;
|
|
25
|
+
export {};
|
package/dist/tiles.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import PQueue from 'p-queue';
|
|
4
|
+
import { errorMessage } from 'drone-path-core/errors';
|
|
5
|
+
import { solidPng } from './png.js';
|
|
6
|
+
import { tilePaths } from './tilecache.js';
|
|
7
|
+
import { TILE_SOURCES } from './renderer/tilesources.js';
|
|
8
|
+
import { VERSION } from './version.js';
|
|
9
|
+
function sleep(ms, signal) {
|
|
10
|
+
if (signal?.aborted)
|
|
11
|
+
return Promise.reject(canceledError(signal));
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const onAbort = () => {
|
|
14
|
+
clearTimeout(timer);
|
|
15
|
+
reject(canceledError(signal));
|
|
16
|
+
};
|
|
17
|
+
const timer = setTimeout(() => {
|
|
18
|
+
signal?.removeEventListener('abort', onAbort);
|
|
19
|
+
resolve();
|
|
20
|
+
}, ms);
|
|
21
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const DOWNLOAD_ATTEMPTS = 2;
|
|
25
|
+
const DOWNLOAD_WORKERS = 2;
|
|
26
|
+
const REQUEST_INTERVAL_MS = 500;
|
|
27
|
+
const RETRY_DELAY_MS = 500;
|
|
28
|
+
const FETCH_TIMEOUT_MS = 20000;
|
|
29
|
+
// p-queue starts higher priorities first, so 'now' jumps the queued pre-warm.
|
|
30
|
+
const TASK_PRIORITY = { now: 1, background: 0 };
|
|
31
|
+
function tileUrl(style, tile) {
|
|
32
|
+
const source = TILE_SOURCES[style];
|
|
33
|
+
const subdomain = source.subdomains.length
|
|
34
|
+
? source.subdomains[(tile.x + tile.y) % source.subdomains.length]
|
|
35
|
+
: '';
|
|
36
|
+
return source.url
|
|
37
|
+
.replace('{s}', subdomain)
|
|
38
|
+
.replace('{z}', String(tile.z))
|
|
39
|
+
.replace('{x}', String(tile.x))
|
|
40
|
+
.replace('{y}', String(tile.y));
|
|
41
|
+
}
|
|
42
|
+
function canceledError(signal) {
|
|
43
|
+
return new Error('tile download canceled', { cause: signal.reason });
|
|
44
|
+
}
|
|
45
|
+
async function fetchBody(url, userAgent, signal) {
|
|
46
|
+
const res = await fetch(url, {
|
|
47
|
+
headers: { 'User-Agent': userAgent },
|
|
48
|
+
signal: signal
|
|
49
|
+
? AbortSignal.any([AbortSignal.timeout(FETCH_TIMEOUT_MS), signal])
|
|
50
|
+
: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
51
|
+
});
|
|
52
|
+
return res.ok ? Buffer.from(await res.arrayBuffer()) : null;
|
|
53
|
+
}
|
|
54
|
+
async function downloadTile(ctx, url, priority, signal) {
|
|
55
|
+
for (let attempt = 0; attempt < DOWNLOAD_ATTEMPTS; attempt++) {
|
|
56
|
+
if (signal?.aborted)
|
|
57
|
+
throw canceledError(signal);
|
|
58
|
+
try {
|
|
59
|
+
const body = await ctx.queue.add(() => fetchBody(url, ctx.userAgent, signal), {
|
|
60
|
+
priority: TASK_PRIORITY[priority],
|
|
61
|
+
signal,
|
|
62
|
+
});
|
|
63
|
+
if (body)
|
|
64
|
+
return body;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
if (signal?.aborted)
|
|
68
|
+
throw canceledError(signal);
|
|
69
|
+
}
|
|
70
|
+
if (attempt + 1 < DOWNLOAD_ATTEMPTS) {
|
|
71
|
+
await sleep(RETRY_DELAY_MS, signal);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
let cacheWriteSeq = 0;
|
|
77
|
+
function writeCacheFile(cachePath, data) {
|
|
78
|
+
const dir = path.dirname(cachePath);
|
|
79
|
+
const tmpPath = path.join(dir, `${path.basename(cachePath)}.${process.pid}.${++cacheWriteSeq}.tmp`);
|
|
80
|
+
try {
|
|
81
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
82
|
+
fs.writeFileSync(tmpPath, data);
|
|
83
|
+
fs.renameSync(tmpPath, cachePath);
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
try {
|
|
87
|
+
fs.rmSync(tmpPath, { force: true });
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
// best-effort cleanup so the original cache write error propagates
|
|
91
|
+
}
|
|
92
|
+
throw new Error(`cache write failed: ${cachePath}: ${errorMessage(e)}`, { cause: e });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function createPlaceholderSource() {
|
|
96
|
+
const cache = new Map();
|
|
97
|
+
return (style) => {
|
|
98
|
+
let png = cache.get(style);
|
|
99
|
+
if (!png) {
|
|
100
|
+
png = solidPng(TILE_SOURCES[style].placeholderBg);
|
|
101
|
+
cache.set(style, png);
|
|
102
|
+
}
|
|
103
|
+
return png;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
async function fetchTileToCache(ctx, style, tile, priority, signal) {
|
|
107
|
+
if (signal?.aborted)
|
|
108
|
+
throw canceledError(signal);
|
|
109
|
+
const paths = tilePaths(ctx.cacheDir, style, tile);
|
|
110
|
+
if (fs.existsSync(paths.file))
|
|
111
|
+
return { file: paths.file, status: 'cached' };
|
|
112
|
+
const body = await downloadTile(ctx, tileUrl(style, tile), priority, signal);
|
|
113
|
+
if (body) {
|
|
114
|
+
writeCacheFile(paths.file, body);
|
|
115
|
+
return { file: paths.file, status: 'downloaded' };
|
|
116
|
+
}
|
|
117
|
+
if (signal?.aborted)
|
|
118
|
+
throw canceledError(signal);
|
|
119
|
+
writeCacheFile(paths.placeholder, ctx.placeholderPng(style));
|
|
120
|
+
return { file: paths.placeholder, status: 'failed' };
|
|
121
|
+
}
|
|
122
|
+
function withAbort(promise, signal) {
|
|
123
|
+
if (!signal)
|
|
124
|
+
return promise;
|
|
125
|
+
if (signal.aborted)
|
|
126
|
+
return Promise.reject(canceledError(signal));
|
|
127
|
+
return new Promise((resolve, reject) => {
|
|
128
|
+
const onAbort = () => reject(canceledError(signal));
|
|
129
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
130
|
+
promise.then((value) => {
|
|
131
|
+
signal.removeEventListener('abort', onAbort);
|
|
132
|
+
resolve(value);
|
|
133
|
+
}, (err) => {
|
|
134
|
+
signal.removeEventListener('abort', onAbort);
|
|
135
|
+
reject(err);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
function createDownloader(cacheDir, userAgent, placeholderPng) {
|
|
140
|
+
const queue = new PQueue({
|
|
141
|
+
concurrency: DOWNLOAD_WORKERS,
|
|
142
|
+
intervalCap: 1,
|
|
143
|
+
interval: REQUEST_INTERVAL_MS,
|
|
144
|
+
// Sliding window: no more than intervalCap starts in any interval window,
|
|
145
|
+
// matching the old request gate's nextFreeAt semantics.
|
|
146
|
+
strict: true,
|
|
147
|
+
});
|
|
148
|
+
const ctx = { cacheDir, queue, userAgent, placeholderPng };
|
|
149
|
+
const inFlight = new Map();
|
|
150
|
+
const enqueue = (style, tile, options = {}) => {
|
|
151
|
+
if (options.signal?.aborted) {
|
|
152
|
+
return Promise.reject(canceledError(options.signal));
|
|
153
|
+
}
|
|
154
|
+
const priority = options.priority ?? 'background';
|
|
155
|
+
const key = tilePaths(cacheDir, style, tile).file;
|
|
156
|
+
const shared = inFlight.get(key);
|
|
157
|
+
if (shared && !shared.signal?.aborted) {
|
|
158
|
+
return withAbort(shared.promise, options.signal).catch((e) => {
|
|
159
|
+
if (shared.signal?.aborted && !options.signal?.aborted) {
|
|
160
|
+
return enqueue(style, tile, options);
|
|
161
|
+
}
|
|
162
|
+
throw e;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
const task = fetchTileToCache(ctx, style, tile, priority, options.signal);
|
|
166
|
+
const entry = { promise: task, signal: options.signal };
|
|
167
|
+
const clear = () => {
|
|
168
|
+
options.signal?.removeEventListener('abort', clear);
|
|
169
|
+
if (inFlight.get(key) === entry)
|
|
170
|
+
inFlight.delete(key);
|
|
171
|
+
};
|
|
172
|
+
options.signal?.addEventListener('abort', clear, { once: true });
|
|
173
|
+
void task.then(clear, clear);
|
|
174
|
+
inFlight.set(key, entry);
|
|
175
|
+
return task;
|
|
176
|
+
};
|
|
177
|
+
return { enqueue };
|
|
178
|
+
}
|
|
179
|
+
export function createTileTraffic(cacheDir, version = VERSION) {
|
|
180
|
+
const userAgent = `drone-path/${version} (flight-path map renderer; https://github.com/MrManny/drone-path)`;
|
|
181
|
+
const downloader = createDownloader(cacheDir, userAgent, createPlaceholderSource());
|
|
182
|
+
return {
|
|
183
|
+
ensure(style, tile) {
|
|
184
|
+
return downloader.enqueue(style, tile, { priority: 'now' }).then((result) => result.file);
|
|
185
|
+
},
|
|
186
|
+
stream(options) {
|
|
187
|
+
const { style, tiles, log = () => { }, onTile = () => { }, signal } = options;
|
|
188
|
+
if (signal?.aborted)
|
|
189
|
+
return Promise.reject(canceledError(signal));
|
|
190
|
+
const missing = [];
|
|
191
|
+
for (const tile of tiles) {
|
|
192
|
+
if (fs.existsSync(tilePaths(cacheDir, style, tile).file))
|
|
193
|
+
onTile('cached');
|
|
194
|
+
else
|
|
195
|
+
missing.push(tile);
|
|
196
|
+
}
|
|
197
|
+
if (missing.length === 0) {
|
|
198
|
+
return Promise.resolve({ downloaded: 0, failed: 0, total: tiles.length });
|
|
199
|
+
}
|
|
200
|
+
log(`tiles: ${tiles.length} needed, ${missing.length} to download (${style})`);
|
|
201
|
+
let downloaded = 0;
|
|
202
|
+
let failed = 0;
|
|
203
|
+
return Promise.all(missing.map((tile) => downloader.enqueue(style, tile, { priority: 'background', signal }).then((result) => {
|
|
204
|
+
if (result.status === 'downloaded')
|
|
205
|
+
downloaded++;
|
|
206
|
+
else if (result.status === 'failed')
|
|
207
|
+
failed++;
|
|
208
|
+
onTile(result.status);
|
|
209
|
+
}))).then(() => ({ downloaded, failed, total: tiles.length }));
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
export function prefetchTiles(options) {
|
|
214
|
+
return createTileTraffic(options.cacheDir).stream(options);
|
|
215
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const VERSION: string;
|
package/dist/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "drone-path-engine",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Plan and render flight-path map overlays from drone telemetry.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./*": {
|
|
8
|
+
"types": "./dist/*.d.ts",
|
|
9
|
+
"default": "./dist/*.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/**/*.js",
|
|
14
|
+
"dist/**/*.d.ts",
|
|
15
|
+
"THIRD-PARTY-NOTICES.md"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=24"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "node ../../scripts/clean-package-dist.mjs && tsc -p tsconfig.json",
|
|
22
|
+
"prepack": "npm run build:shared --prefix ../.."
|
|
23
|
+
},
|
|
24
|
+
"author": "Manuel Warum",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/MrManny/drone-path.git",
|
|
29
|
+
"directory": "packages/engine"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@types/leaflet": "~1.9.22",
|
|
33
|
+
"drone-path-core": "~1.0.0",
|
|
34
|
+
"leaflet": "~1.9.4",
|
|
35
|
+
"p-queue": "~10.0.0",
|
|
36
|
+
"playwright-core": "~1.65.0",
|
|
37
|
+
"zod": "~4.6.7"
|
|
38
|
+
}
|
|
39
|
+
}
|