js decoder 1.1.0

adds fflate dependency; maplibre decoder can read gzipped tiles [#41]
This commit is contained in:
Brandon Liu
2022-07-07 12:36:16 +08:00
parent abf1dd9372
commit 3811ff9b1b
5 changed files with 31 additions and 9 deletions

View File

@@ -93,6 +93,7 @@ function MaplibreMap(props: { file: PMTiles }) {
useEffect(() => { useEffect(() => {
let cache = new ProtocolCache(); let cache = new ProtocolCache();
maplibregl.addProtocol("pmtiles", cache.protocol); maplibregl.addProtocol("pmtiles", cache.protocol);
cache.add(props.file); // this is necessary for non-HTTP sources
map = new maplibregl.Map({ map = new maplibregl.Map({
container: mapContainerRef.current!, container: mapContainerRef.current!,

View File

@@ -1,5 +1,7 @@
declare const L: any; declare const L: any;
import { decompressSync } from "fflate";
export const shift = (n: number, shift: number) => { export const shift = (n: number, shift: number) => {
return n * Math.pow(2, shift); return n * Math.pow(2, shift);
}; };
@@ -536,7 +538,11 @@ export class ProtocolCache {
instance!.source instance!.source
.getBytes(val.offset, val.length) .getBytes(val.offset, val.length)
.then((arr) => { .then((arr) => {
callback(null, new Uint8Array(arr.buffer), null, null); let data = new Uint8Array(arr.buffer);
if (data[0] == 0x1f && data[1] == 0x8b) {
data = decompressSync(data);
}
callback(null, data, null, null);
}) })
.catch((e) => { .catch((e) => {
callback(new Error("Canceled"), null, null, null); callback(new Error("Canceled"), null, null, null);

17
js/package-lock.json generated
View File

@@ -1,13 +1,16 @@
{ {
"name": "pmtiles", "name": "pmtiles",
"version": "1.0.0", "version": "1.1.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "pmtiles", "name": "pmtiles",
"version": "1.0.0", "version": "1.1.0",
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"dependencies": {
"fflate": "^0.7.3"
},
"devDependencies": { "devDependencies": {
"esbuild": "^0.11.14", "esbuild": "^0.11.14",
"esbuild-runner": "^2.2.1", "esbuild-runner": "^2.2.1",
@@ -47,6 +50,11 @@
"esbuild": "*" "esbuild": "*"
} }
}, },
"node_modules/fflate": {
"version": "0.7.3",
"resolved": "https://registry.npmjs.org/fflate/-/fflate-0.7.3.tgz",
"integrity": "sha512-0Zz1jOzJWERhyhsimS54VTqOteCNwRtIlh8isdL0AXLo0g7xNTfTL7oWrkmCnPhZGocKIkWHBistBrrpoNH3aw=="
},
"node_modules/source-map": { "node_modules/source-map": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
@@ -115,6 +123,11 @@
"tslib": "2.3.1" "tslib": "2.3.1"
} }
}, },
"fflate": {
"version": "0.7.3",
"resolved": "https://registry.npmjs.org/fflate/-/fflate-0.7.3.tgz",
"integrity": "sha512-0Zz1jOzJWERhyhsimS54VTqOteCNwRtIlh8isdL0AXLo0g7xNTfTL7oWrkmCnPhZGocKIkWHBistBrrpoNH3aw=="
},
"source-map": { "source-map": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",

View File

@@ -1,19 +1,19 @@
{ {
"name": "pmtiles", "name": "pmtiles",
"version": "1.0.4", "version": "1.1.0",
"description": "PMTiles archive decoder for browsers", "description": "PMTiles archive decoder for browsers",
"main": "dist/index.cjs", "main": "dist/index.cjs",
"module": "dist/index.mjs", "module": "dist/index.mjs",
"types":"dist/index.d.ts", "types": "dist/index.d.ts",
"source": "index.ts", "source": "index.ts",
"files": [ "files": [
"dist", "dist",
"index.ts" "index.ts"
], ],
"scripts": { "scripts": {
"build-iife": "esbuild index.ts --outfile=dist/index.js --target=es6 --global-name=pmtiles --format=iife", "build-iife": "esbuild index.ts --outfile=dist/index.js --target=es6 --global-name=pmtiles --bundle --format=iife",
"build-esm": "esbuild index.ts --outfile=dist/index.mjs --target=es6 --global-name=pmtiles --format=esm", "build-esm": "esbuild index.ts --outfile=dist/index.mjs --target=es6 --global-name=pmtiles --bundle --format=esm",
"build-cjs": "esbuild index.ts --outfile=dist/index.cjs --target=es6 --global-name=pmtiles --format=cjs", "build-cjs": "esbuild index.ts --outfile=dist/index.cjs --target=es6 --global-name=pmtiles --bundle --format=cjs",
"build-tsc": "tsc --declaration --emitDeclarationOnly --outdir dist", "build-tsc": "tsc --declaration --emitDeclarationOnly --outdir dist",
"build": "npm run build-iife && npm run build-esm && npm run build-cjs && npm run build-tsc", "build": "npm run build-iife && npm run build-esm && npm run build-cjs && npm run build-tsc",
"test": "node -r esbuild-runner/register index.test.ts", "test": "node -r esbuild-runner/register index.test.ts",
@@ -28,5 +28,8 @@
"esbuild-runner": "^2.2.1", "esbuild-runner": "^2.2.1",
"typescript": "^4.5.5", "typescript": "^4.5.5",
"zora": "^5.0.2" "zora": "^5.0.2"
},
"dependencies": {
"fflate": "^0.7.3"
} }
} }

View File

@@ -18,7 +18,6 @@ def MemorySource(buf):
return get_bytes return get_bytes
class Reader: class Reader:
def __init__(self, get_bytes): def __init__(self, get_bytes):
self.get_bytes = get_bytes self.get_bytes = get_bytes