Skip to content

Commit

Permalink
Added paths parameter to config to be used if filenames are different…
Browse files Browse the repository at this point in the history
… from gdal3WebAssembly.(data|wasm) and gdal3.js.
  • Loading branch information
bugra9 committed Mar 28, 2022
1 parent 492c5d0 commit 427d76f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,15 @@ interface Gdal {
drivers: Drivers;
}

interface GdalFilePaths {
wasm: string;
data: string;
js?: string;
}

interface Config {
path?: string;
paths?: GdalFilePaths
dest?: string;
useWorker?: boolean;
}
Expand Down
30 changes: 25 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ let gdalJsPromise;
* @async
* @function initGdalJs
* @param {Object} config Configuration Object.
* @param {string} config.path Path of wasm and data files.
* @param {string} config.path Parent path of wasm and data files.
* @param {Object} config.paths Use if filenames differ from gdal3WebAssembly.(data|wasm) and gdal3.js.
* @param {string} config.paths.wasm Wasm file path. (Default: gdal3WebAssembly.wasm)
* @param {string} config.paths.data Data file path. (Default: gdal3WebAssembly.data)
* @param {string} config.paths.js Js file path for web worker. (Default: gdal3.js)
* @param {string} config.dest Destination path where the created files will be saved. (Node.js only)
* @param {boolean} config.useWorker=true Using [Web Workers]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers} on the browser. It doesn't work on Node.js.
* @return {Promise<Gdal>} "Promise" returns Gdal namespace.
Expand Down Expand Up @@ -70,15 +74,24 @@ export default function initGdalJs(
setDrivers();
};

Module.locateFile = function locateFile(path) {
Module.locateFile = function locateFile(fileName) {
let path = fileName;
if (config.paths && config.paths.wasm && fileName.split('.').pop() === 'wasm') {
path = config.paths.wasm;
} else if (config.paths && config.paths.data && fileName.split('.').pop() === 'data') {
path = config.paths.data;
}

let prefix = '';
if (config.path) {
prefix = config.path;
if (prefix.slice(-1) !== '/') prefix += '/';
} else if (isNode) {
prefix = 'node_modules/gdal3.js/dist/package/';
}
return prefix + path;
let output = prefix + path;
if (!isNode && output.substring(0, 4) !== 'http' && output[0] !== '/') output = `/${output}`;
return output;
};

if (isNode) {
Expand All @@ -93,9 +106,16 @@ export default function initGdalJs(
});
});
} else {
const workerJsName = config.workerJsName || 'gdal3.js';
const workerJsName = (config.paths && config.paths.js) || 'gdal3.js';

let prefix = '';
if (config.path) {
prefix = config.path;
if (prefix.slice(-1) !== '/') prefix += '/';
}

gdalJsPromise = new Promise((resolve) => {
workerOutsideSupport.variables.gdalWorkerWrapper = new workerOutsideSupport.WorkerWrapper(`${config.path || ''}/${workerJsName}`, (d) => {
workerOutsideSupport.variables.gdalWorkerWrapper = new workerOutsideSupport.WorkerWrapper(`${prefix}${workerJsName}`, config, (d) => {
workerOutsideSupport.variables.drivers = d;
resolve(workerOutsideSupport.gdalProxy);
});
Expand Down
16 changes: 13 additions & 3 deletions src/workerSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ function onError(err) {
}

export default function workerInsideSupport(initGdalJs) {
const moduleReady = initGdalJs({ useWorker: false });
moduleReady.then(({ drivers }) => postMessage({ success: true, id: 'onload', data: drivers })).catch((e) => postMessage({ success: false, id: 'onload', data: e }));
let moduleReady;
onmessage = function onmessage(event) {
if (event.data && event.data.func === 'constructor') {
let config = { useWorker: false };
if (event.data.params && event.data.params.config) {
config = { ...event.data.params.config, ...config };
}
moduleReady = initGdalJs(config);
moduleReady.then(({ drivers }) => postMessage({ success: true, id: 'onload', data: drivers })).catch((e) => postMessage({ success: false, id: 'onload', data: e }));
return null;
}

return moduleReady
.then(onModuleReady.bind(event))
.catch(onError.bind(event));
Expand All @@ -31,7 +40,7 @@ const variables = {
drivers: null,
};
class WorkerWrapper {
constructor(file, onload) {
constructor(file, config, onload) {
this.promises = { onload: { resolve: onload, reject: console.error } };
this.gdalWorker = new Worker(file);
this.gdalWorker.onmessage = (evt) => {
Expand All @@ -40,6 +49,7 @@ class WorkerWrapper {
else this.promises[evt.data.id].reject(evt.data.data);
}
};
this.gdalWorker.postMessage({ func: 'constructor', params: { config } });
}

call(i) {
Expand Down

0 comments on commit 427d76f

Please sign in to comment.