From 427d76f72129c0596eafc9f46f9a7d4bcc78d355 Mon Sep 17 00:00:00 2001 From: Bugra Date: Mon, 28 Mar 2022 22:56:05 +0300 Subject: [PATCH] Added paths parameter to config to be used if filenames are different from gdal3WebAssembly.(data|wasm) and gdal3.js. --- src/index.d.ts | 7 +++++++ src/index.js | 30 +++++++++++++++++++++++++----- src/workerSupport.js | 16 +++++++++++++--- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index bbe94f1..d5a3fdd 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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; } diff --git a/src/index.js b/src/index.js index 466e88b..22b5ae8 100644 --- a/src/index.js +++ b/src/index.js @@ -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} "Promise" returns Gdal namespace. @@ -70,7 +74,14 @@ 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; @@ -78,7 +89,9 @@ export default function initGdalJs( } 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) { @@ -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); }); diff --git a/src/workerSupport.js b/src/workerSupport.js index 7b11397..d179aa9 100644 --- a/src/workerSupport.js +++ b/src/workerSupport.js @@ -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)); @@ -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) => { @@ -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) {