fix(wind): bound GFS acquisition and preserve canvas trails and lifecycle

This commit is contained in:
Bilawal Sidhu committed 2026-09-15 02:21:09 -05:00
1 parent 93d4224fdf
commit dcb3339070
17 files changed
+251 -101

No files matched your search

+5 -6
View File
@@ -1,7 +1,6 @@
import os from 'node:os';
import path from 'node:path';
import { promises as fs } from 'node:fs';
import { randomUUID } from 'node:crypto';
/**
* Lazily created ecCodes WASM instance. The `@meri-imperiumi/eccodes-wasm`
@@ -37,12 +36,12 @@ export async function decodeWindGribMessage(
{ eccodesModule, mountDir = os.tmpdir() } = {},
) {
const eccodes = eccodesModule ?? (await loadEccodes());
const name = `wind-${randomUUID()}.grib2`;
const file = path.join(mountDir, name);
await fs.writeFile(file, buffer);
const directory = await fs.mkdtemp(path.join(mountDir, 'gev-wind-'));
const file = path.join(directory, 'wind.grib2');
await fs.writeFile(file, buffer, { mode: 0o600 });
let handle = null;
try {
eccodes.mountFilesystem(mountDir);
eccodes.mountFilesystem(directory);
handle = eccodes.openGrib(file);
return {
ni: handle.getLong('Ni'),
@@ -58,6 +57,6 @@ export async function decodeWindGribMessage(
};
} finally {
if (handle) handle.delete();
await fs.unlink(file).catch(() => {});
await fs.rm(directory, { recursive: true, force: true }).catch(() => {});
}
}
+8 -7
View File
@@ -1,3 +1,4 @@
import { readWindBody } from '../../../src/sources/windBody.js';
/**
* Parse a GFS `.idx` inventory into ordered message records.
*
@@ -55,9 +56,9 @@ export function windMessageRanges(parsed, { level = '10 m above ground' } = {})
/** Fetch a bounded text response as a Buffer. */
export async function fetchText({ url, fetchImpl = fetch, signal }) {
const response = await fetchImpl(url, { signal });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return Buffer.from(await response.arrayBuffer());
const response = await fetchImpl(url, { signal, redirect: 'error' });
if (!response.ok) { await response.body?.cancel(); throw new Error('Wind upstream unavailable'); }
return Buffer.from(await readWindBody(response, 2 * 1024 * 1024, signal));
}
/**
@@ -72,17 +73,17 @@ export async function fetchRange({
signal,
maxBytes = 8 * 1024 * 1024,
}) {
if (end < start || end - start + 1 > maxBytes)
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 0 || end < start || end - start + 1 > maxBytes)
throw new Error('range too large');
const response = await fetchImpl(url, {
signal,
redirect: 'error',
headers: { Range: `bytes=${start}-${end}` },
});
const validStatus =
response.status === 206 || (response.status === 200 && start === 0);
if (!validStatus || !response.ok)
throw new Error(`invalid range response ${response.status}`);
const buffer = Buffer.from(await response.arrayBuffer());
if (!validStatus || !response.ok) { await response.body?.cancel(); throw new Error('Wind upstream unavailable'); }
const buffer = Buffer.from(await readWindBody(response, end - start + 1, signal));
if (buffer.length !== end - start + 1)
throw new Error('invalid range length');
return buffer;
+2
View File
@@ -21,6 +21,8 @@ export function resampleWindGrid({
dx = 1,
dy = 1,
}) {
if (!Number.isInteger(ni) || !Number.isInteger(nj) || ni < 1 || nj < 1 || ni * nj > 2_000_000 || u?.length !== ni * nj || v?.length !== ni * nj || ![lo1, la1, di, dj, dx, dy].every(Number.isFinite) || di <= 0 || dj <= 0 || dx < 0.25 || dy < 0.25 || dx > 180 || dy > 180)
throw new Error('Invalid wind grid geometry');
const nx = Math.round(360 / dx);
const ny = Math.round(180 / dy) + 1;
const outU = new Float32Array(nx * ny);