Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dynamic imports for non-browser-compatible modules #422

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ module.exports = {
curly: ['error', 'all'],
'brace-style': ['error', '1tbs', { allowSingleLine: false }],
'no-else-return': 0,
'no-return-await': 0,
},
};
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"parse-headers": "^2.0.2",
"quick-lru": "^6.1.1",
"web-worker": "^1.2.0",
"xml-utils": "^1.0.2",
"xml-utils": "^1.7.2-0",
"zstddec": "^0.1.0"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default {
{
modules: false,
targets: 'last 2 versions, not dead',
exclude: [
"@babel/plugin-transform-typeof-symbol"
]
},
],
],
Expand Down
10 changes: 6 additions & 4 deletions src/geotiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Pool from './pool.js';
import { makeRemoteSource, makeCustomSource } from './source/remote.js';
import { makeBufferSource } from './source/arraybuffer.js';
import { makeFileReaderSource } from './source/filereader.js';
import { makeFileSource } from './source/file.js';
import { BaseClient, BaseResponse } from './source/client/base.js';

import { fieldTypes, fieldTagNames, arrayFields, geoKeyNames } from './globals.js';
Expand Down Expand Up @@ -682,7 +681,7 @@ export { MultiGeoTIFF };
* @returns {Promise<GeoTIFF>} The resulting GeoTIFF file.
*/
export async function fromUrl(url, options = {}, signal) {
return GeoTIFF.fromSource(makeRemoteSource(url, options), signal);
return GeoTIFF.fromSource(await makeRemoteSource(url, options), signal);
}

/**
Expand Down Expand Up @@ -723,6 +722,7 @@ export async function fromArrayBuffer(arrayBuffer, signal) {
* @returns {Promise<GeoTIFF>} The resulting GeoTIFF file.
*/
export async function fromFile(path, signal) {
const makeFileSource = await import('./source/file.js');
return GeoTIFF.fromSource(makeFileSource(path), signal);
}

Expand Down Expand Up @@ -752,9 +752,11 @@ export async function fromBlob(blob, signal) {
* @returns {Promise<MultiGeoTIFF>} The resulting MultiGeoTIFF file.
*/
export async function fromUrls(mainUrl, overviewUrls = [], options = {}, signal) {
const mainFile = await GeoTIFF.fromSource(makeRemoteSource(mainUrl, options), signal);
const mainSource = await makeRemoteSource(mainUrl, options);
const mainFile = await GeoTIFF.fromSource(mainSource, signal);
const overviewFiles = await Promise.all(
overviewUrls.map((url) => GeoTIFF.fromSource(makeRemoteSource(url, options))),
overviewUrls.map((url) => makeRemoteSource(url, options).then((src) => GeoTIFF.fromSource(src)),
),
);

return new MultiGeoTIFF(mainFile, overviewFiles);
Expand Down
4 changes: 2 additions & 2 deletions src/geotiffimage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @module geotiffimage */
import { getFloat16 } from '@petamoriken/float16';
import getAttribute from 'xml-utils/get-attribute.js';
import findTagsByName from 'xml-utils/find-tags-by-name.js';
import getAttribute from 'xml-utils/get-attribute.mjs';
import findTagsByName from 'xml-utils/find-tags-by-name.mjs';

import { photometricInterpretations, ExtraSamplesValues } from './globals.js';
import { fromWhiteIsZero, fromBlackIsZero, fromPalette, fromCMYK, fromYCbCr, fromCIELab } from './rgb.js';
Expand Down
27 changes: 16 additions & 11 deletions src/source/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { parseByteRanges, parseContentRange, parseContentType } from './httputil
import { BaseSource } from './basesource.js';
import { BlockedSource } from './blockedsource.js';

import { FetchClient } from './client/fetch.js';
import { XHRClient } from './client/xhr.js';
import { HttpClient } from './client/http.js';

class RemoteSource extends BaseSource {
/**
*
Expand Down Expand Up @@ -157,19 +153,28 @@ function maybeWrapInBlockedSource(source, { blockSize, cacheSize }) {
return new BlockedSource(source, { blockSize, cacheSize });
}

export function makeFetchSource(url, { headers = {}, credentials, maxRanges = 0, allowFullFile = false, ...blockOptions } = {}) {
export async function makeFetchSource(url, {
headers = {},
credentials,
maxRanges = 0,
allowFullFile = false,
...blockOptions
} = {}) {
const { FetchClient } = await import('./client/fetch.js');
const client = new FetchClient(url, credentials);
const source = new RemoteSource(client, headers, maxRanges, allowFullFile);
return maybeWrapInBlockedSource(source, blockOptions);
}

export function makeXHRSource(url, { headers = {}, maxRanges = 0, allowFullFile = false, ...blockOptions } = {}) {
export async function makeXHRSource(url, { headers = {}, maxRanges = 0, allowFullFile = false, ...blockOptions } = {}) {
const { XHRClient } = await import('./client/xhr.js');
const client = new XHRClient(url);
const source = new RemoteSource(client, headers, maxRanges, allowFullFile);
return maybeWrapInBlockedSource(source, blockOptions);
}

export function makeHttpSource(url, { headers = {}, maxRanges = 0, allowFullFile = false, ...blockOptions } = {}) {
export async function makeHttpSource(url, { headers = {}, maxRanges = 0, allowFullFile = false, ...blockOptions } = {}) {
const { HttpClient } = await import('./client/http.js');
const client = new HttpClient(url);
const source = new RemoteSource(client, headers, maxRanges, allowFullFile);
return maybeWrapInBlockedSource(source, blockOptions);
Expand All @@ -185,12 +190,12 @@ export function makeCustomSource(client, { headers = {}, maxRanges = 0, allowFul
* @param {string} url
* @param {object} options
*/
export function makeRemoteSource(url, { forceXHR = false, ...clientOptions } = {}) {
export async function makeRemoteSource(url, { forceXHR = false, ...clientOptions } = {}) {
if (typeof fetch === 'function' && !forceXHR) {
return makeFetchSource(url, clientOptions);
return await makeFetchSource(url, clientOptions);
}
if (typeof XMLHttpRequest !== 'undefined') {
return makeXHRSource(url, clientOptions);
return await makeXHRSource(url, clientOptions);
}
return makeHttpSource(url, clientOptions);
return await makeHttpSource(url, clientOptions);
}
Loading