Skip to content

Commit 120b145

Browse files
authored
Merge branch 'master' into feat--add_zstd
2 parents b4887b6 + 12af432 commit 120b145

19 files changed

+620
-199
lines changed

.eslintrc.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module.exports = {
1313
ecmaVersion: 'latest',
1414
sourceType: 'module',
1515
},
16+
globals: {
17+
FinalizationRegistry: true,
18+
},
1619
rules: {
1720
'no-underscore-dangle': 0,
1821
'class-methods-use-this': 0,

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ YCbCr, and CIE L*a*b.
360360

361361
geotiff.js provides a method to automatically convert these images to RGB:
362362
`readRGB()`. This method is very similar to the `readRasters` method with
363-
distinction that the `interleave` option is now always `true` and the
363+
the distinction that the `interleave` option now defaults to `true` and the
364364
`samples` are automatically chosen.
365365

366366
```javascript

package-lock.json

Lines changed: 55 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@
8787
"pako": "^2.0.4",
8888
"parse-headers": "^2.0.2",
8989
"quick-lru": "^6.1.1",
90-
"web-worker": "^1.2.0",
91-
"xml-utils": "^1.0.2",
90+
"web-worker": "^1.5.0",
91+
"xml-utils": "^1.10.2",
9292
"zstddec": "^0.2.0-alpha.3"
9393
},
9494
"devDependencies": {
@@ -100,6 +100,7 @@
100100
"@rollup/plugin-commonjs": "^21.0.1",
101101
"@rollup/plugin-node-resolve": "^13.1.3",
102102
"chai": "^4.2.0",
103+
"chai-as-promised": "^8.0.1",
103104
"chokidar-cli": "^3.0.0",
104105
"detect-node": "^2.0.4",
105106
"eslint": "^7.32.0",
@@ -125,25 +126,25 @@
125126
"typescript": "^4.5.5"
126127
},
127128
"scripts": {
128-
"prebuild": "npm run build:clean && npm run build:module",
129-
"build": "run-p build:browser build:node build:types",
130-
"build:clean": "rimraf dist-node/ dist-browser/ dist-module/",
131-
"build:node": "tsc --project tsconfig.build.json && shx echo \"{\\\"type\\\":\\\"commonjs\\\"}\" > dist-node/package.json",
129+
"prebuild": "npm run build:clean",
130+
"build": "npm run build:module && run-p build:browser build:node build:types",
131+
"build:clean": "shx rm -rf dist-node/ dist-browser/ dist-module/",
132+
"build:module": "shx mkdir -p dist-module && shx cp -rf src/* dist-module/ && rollup -c rollup.worker.config.js",
132133
"build:browser": "rollup -c rollup.config.js",
133-
"build:module": "shx mkdir -p dist-module && shx cp -rf src/* dist-module/ && node scripts/serialize-workers.cjs",
134+
"build:node": "tsc --project tsconfig.build.json && shx echo \"{\\\"type\\\":\\\"commonjs\\\"}\" > dist-node/package.json",
134135
"build:types": "tsc --outdir dist-module/",
135-
"watch:browser": "chokidar \"dist-module/*.js\" -c \"npm run build:browser\"",
136136
"watch:module": "chokidar \"src/*.js\" -c \"npm run build:module\"",
137+
"watch:browser": "chokidar \"dist-module/*.js\" -c \"npm run build:browser\"",
137138
"predev": "npm run build",
138139
"dev": "run-p watch:module watch:browser dev:serve",
139140
"dev:serve": "serve --listen 8090",
140141
"docs": "rm -rf docs/; jsdoc -c .jsdoc.json -r src README.md -d docs",
141142
"prelint": "npm run build:module",
142-
"lint": "eslint src test scripts/*.cjs .eslintrc.cjs",
143+
"lint": "eslint src test .eslintrc.cjs",
143144
"lint:fix": "npm run lint -- --fix",
144145
"prepare": "npm run build",
145146
"pretest": "npm run lint",
146-
"test": "mocha --full-trace test/geotiff.spec.js"
147+
"test": "mocha --full-trace test/*.spec.js"
147148
},
148149
"author": "Fabian Schindler",
149150
"browser": {

rollup.worker.config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
3+
import baseConfig from './rollup.config.js';
4+
5+
export default {
6+
...baseConfig,
7+
input: 'dist-module/worker/decoder.js',
8+
output: {
9+
file: 'dist-module/worker/create.js',
10+
format: 'module',
11+
inlineDynamicImports: true,
12+
},
13+
plugins: [
14+
...baseConfig.plugins,
15+
{
16+
name: 'serialize worker and export create function',
17+
renderChunk(code) {
18+
return `\
19+
import Worker from 'web-worker';
20+
21+
export default function create() {
22+
const source = ${JSON.stringify(code)};
23+
return new Worker(typeof Buffer !== 'undefined'
24+
? 'data:application/javascript;base64,' + Buffer.from(source, 'binary').toString('base64')
25+
: URL.createObjectURL(new Blob([source], { type: 'application/javascript' })));
26+
}`;
27+
},
28+
},
29+
],
30+
};

scripts/serialize-workers.cjs

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/compression/index.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
const registry = new Map();
2+
const preferWorkerMap = new Map();
23

3-
export function addDecoder(cases, importFn) {
4+
/**
5+
* Either a number or undefined.
6+
* @typedef {(number|undefined)} NumberOrUndefined
7+
*/
8+
9+
/**
10+
* Register a decoder for a specific compression method or a range of compressions
11+
* @param {(NumberOrUndefined|(NumberOrUndefined[]))} cases ids of the compression methods to register for
12+
* @param {function():Promise} importFn the function to import the decoder
13+
* @param {boolean} preferWorker_ Whether to prefer running the decoder in a worker
14+
*/
15+
export function addDecoder(cases, importFn, preferWorker_ = true) {
416
if (!Array.isArray(cases)) {
517
cases = [cases]; // eslint-disable-line no-param-reassign
618
}
7-
cases.forEach((c) => registry.set(c, importFn));
19+
cases.forEach((c) => {
20+
registry.set(c, importFn);
21+
preferWorkerMap.set(c, preferWorker_);
22+
});
823
}
924

25+
/**
26+
* Get a decoder for a specific file directory
27+
* @param {object} fileDirectory the file directory of the image
28+
* @returns {Promise<Decoder>}
29+
*/
1030
export async function getDecoder(fileDirectory) {
1131
const importFn = registry.get(fileDirectory.Compression);
1232
if (!importFn) {
@@ -16,8 +36,17 @@ export async function getDecoder(fileDirectory) {
1636
return new Decoder(fileDirectory);
1737
}
1838

39+
/**
40+
* Whether to prefer running the decoder in a worker
41+
* @param {object} fileDirectory the file directory of the image
42+
* @returns {boolean}
43+
*/
44+
export function preferWorker(fileDirectory) {
45+
return preferWorkerMap.get(fileDirectory.Compression);
46+
}
47+
1948
// Add default decoders to registry (end-user may override with other implementations)
20-
addDecoder([undefined, 1], () => import('./raw.js').then((m) => m.default));
49+
addDecoder([undefined, 1], () => import('./raw.js').then((m) => m.default), false);
2150
addDecoder(5, () => import('./lzw.js').then((m) => m.default));
2251
addDecoder(6, () => {
2352
throw new Error('old style JPEG compression is not supported.');
@@ -39,4 +68,4 @@ addDecoder(50000, () => import('./zstd.js')
3968
})
4069
.then((m) => m.default),
4170
);
42-
addDecoder(50001, () => import('./webimage.js').then((m) => m.default));
71+
addDecoder(50001, () => import('./webimage.js').then((m) => m.default), false);

0 commit comments

Comments
 (0)