mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 10:51:07 +00:00
js: avoid use of BigInt operations for older safari browsers [#103]
This commit is contained in:
28
js/index.ts
28
js/index.ts
@@ -330,6 +330,12 @@ export class FetchSource implements Source {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getUint64(v: DataView, offset: number): number {
|
||||||
|
const wh = v.getUint32(offset + 4, true);
|
||||||
|
const wl = v.getUint32(offset + 0, true);
|
||||||
|
return wh * Math.pow(2, 32) + wl;
|
||||||
|
}
|
||||||
|
|
||||||
export function bytesToHeader(bytes: ArrayBuffer, etag?: string): Header {
|
export function bytesToHeader(bytes: ArrayBuffer, etag?: string): Header {
|
||||||
const v = new DataView(bytes);
|
const v = new DataView(bytes);
|
||||||
const spec_version = v.getUint8(7);
|
const spec_version = v.getUint8(7);
|
||||||
@@ -341,17 +347,17 @@ export function bytesToHeader(bytes: ArrayBuffer, etag?: string): Header {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
specVersion: spec_version,
|
specVersion: spec_version,
|
||||||
rootDirectoryOffset: Number(v.getBigUint64(8, true)),
|
rootDirectoryOffset: getUint64(v, 8),
|
||||||
rootDirectoryLength: Number(v.getBigUint64(16, true)),
|
rootDirectoryLength: getUint64(v, 16),
|
||||||
jsonMetadataOffset: Number(v.getBigUint64(24, true)),
|
jsonMetadataOffset: getUint64(v, 24),
|
||||||
jsonMetadataLength: Number(v.getBigUint64(32, true)),
|
jsonMetadataLength: getUint64(v, 32),
|
||||||
leafDirectoryOffset: Number(v.getBigUint64(40, true)),
|
leafDirectoryOffset: getUint64(v, 40),
|
||||||
leafDirectoryLength: Number(v.getBigUint64(48, true)),
|
leafDirectoryLength: getUint64(v, 48),
|
||||||
tileDataOffset: Number(v.getBigUint64(56, true)),
|
tileDataOffset: getUint64(v, 56),
|
||||||
tileDataLength: Number(v.getBigUint64(64, true)),
|
tileDataLength: getUint64(v, 64),
|
||||||
numAddressedTiles: Number(v.getBigUint64(72, true)),
|
numAddressedTiles: getUint64(v, 72),
|
||||||
numTileEntries: Number(v.getBigUint64(80, true)),
|
numTileEntries: getUint64(v, 80),
|
||||||
numTileContents: Number(v.getBigUint64(88, true)),
|
numTileContents: getUint64(v, 88),
|
||||||
clustered: v.getUint8(96) === 1,
|
clustered: v.getUint8(96) === 1,
|
||||||
internalCompression: v.getUint8(97),
|
internalCompression: v.getUint8(97),
|
||||||
tileCompression: v.getUint8(98),
|
tileCompression: v.getUint8(98),
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
RangeResponse,
|
RangeResponse,
|
||||||
EtagMismatch,
|
EtagMismatch,
|
||||||
PMTiles,
|
PMTiles,
|
||||||
|
getUint64,
|
||||||
} from "../index";
|
} from "../index";
|
||||||
|
|
||||||
test("varint", () => {
|
test("varint", () => {
|
||||||
@@ -171,6 +172,16 @@ test("cache getHeader", async () => {
|
|||||||
assert.strictEqual(header.maxLat, 1);
|
assert.strictEqual(header.maxLat, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("getUint64", async () => {
|
||||||
|
const view = new DataView(new ArrayBuffer(8));
|
||||||
|
view.setBigUint64(0, 0n, true);
|
||||||
|
assert.strictEqual(getUint64(view, 0), 0);
|
||||||
|
view.setBigUint64(0, 1n, true);
|
||||||
|
assert.strictEqual(getUint64(view, 0), 1);
|
||||||
|
view.setBigUint64(0, 9007199254740991n, true);
|
||||||
|
assert.strictEqual(getUint64(view, 0), 9007199254740991);
|
||||||
|
});
|
||||||
|
|
||||||
test("cache check against empty", async () => {
|
test("cache check against empty", async () => {
|
||||||
const source = new TestNodeFileSource("test/data/empty.pmtiles", "1");
|
const source = new TestNodeFileSource("test/data/empty.pmtiles", "1");
|
||||||
const cache = new SharedPromiseCache();
|
const cache = new SharedPromiseCache();
|
||||||
|
|||||||
Reference in New Issue
Block a user