diff --git a/js/index.ts b/js/index.ts index 4d8bc03..7f8ffa9 100644 --- a/js/index.ts +++ b/js/index.ts @@ -309,8 +309,15 @@ export class FetchSource implements Source { export function bytesToHeader(bytes: ArrayBuffer, etag?: string): Header { const v = new DataView(bytes); + const spec_version = v.getUint8(7); + if (spec_version > 3) { + throw Error( + `Archive is spec version ${spec_version} but this library supports up to spec version 3` + ); + } + return { - specVersion: 3, + specVersion: spec_version, rootDirectoryOffset: Number(v.getBigUint64(8, true)), rootDirectoryLength: Number(v.getBigUint64(16, true)), jsonMetadataOffset: Number(v.getBigUint64(24, true)), diff --git a/js/test/data/invalid_v4.pmtiles b/js/test/data/invalid_v4.pmtiles new file mode 100644 index 0000000..1871cb2 Binary files /dev/null and b/js/test/data/invalid_v4.pmtiles differ diff --git a/js/test/v3.test.ts b/js/test/v3.test.ts index bec25b7..4238a95 100644 --- a/js/test/v3.test.ts +++ b/js/test/v3.test.ts @@ -192,7 +192,17 @@ test("cache check magic number", async (assertion) => { await cache.getHeader(source); assertion.fail("Should have thrown"); } catch (e) { - console.log(e); + assertion.ok(e instanceof Error); + } +}); + +test("cache check future spec version", async (assertion) => { + const source = new TestNodeFileSource("test/data/invalid_v4.pmtiles", "1"); + const cache = new SharedPromiseCache(); + try { + await cache.getHeader(source); + assertion.fail("Should have thrown"); + } catch (e) { assertion.ok(e instanceof Error); } });