@loaders.gl/worker-utils
Advanced tools
Comparing version 4.2.0-alpha.4 to 4.2.0-alpha.5
@@ -1,21 +0,21 @@ | ||
import type { WorkerObject } from './types'; | ||
export type { WorkerObject, WorkerOptions, WorkerMessage, WorkerMessageType, WorkerMessageData, WorkerMessagePayload } from './types'; | ||
export { assert } from './lib/env-utils/assert'; | ||
export { isBrowser, isWorker } from './lib/env-utils/globals'; | ||
export { default as WorkerJob } from './lib/worker-farm/worker-job'; | ||
export { default as WorkerThread } from './lib/worker-farm/worker-thread'; | ||
export { default as WorkerFarm } from './lib/worker-farm/worker-farm'; | ||
export { default as WorkerPool } from './lib/worker-farm/worker-pool'; | ||
export { default as WorkerBody } from './lib/worker-farm/worker-body'; | ||
export type { ProcessOnWorkerOptions } from './lib/worker-api/process-on-worker'; | ||
export { processOnWorker, canProcessOnWorker } from './lib/worker-api/process-on-worker'; | ||
export { createWorker } from './lib/worker-api/create-worker'; | ||
export { getWorkerURL } from './lib/worker-api/get-worker-url'; | ||
export { validateWorkerVersion } from './lib/worker-api/validate-worker-version'; | ||
export { getTransferList, getTransferListForWriter } from './lib/worker-utils/get-transfer-list'; | ||
export { getLibraryUrl, loadLibrary } from './lib/library-utils/library-utils'; | ||
export { default as AsyncQueue } from './lib/async-queue/async-queue'; | ||
export { default as ChildProcessProxy } from './lib/process-utils/child-process-proxy'; | ||
import type { WorkerObject } from "./types.js"; | ||
export type { WorkerObject, WorkerOptions, WorkerMessage, WorkerMessageType, WorkerMessageData, WorkerMessagePayload } from "./types.js"; | ||
export { assert } from "./lib/env-utils/assert.js"; | ||
export { isBrowser, isWorker } from "./lib/env-utils/globals.js"; | ||
export { default as WorkerJob } from "./lib/worker-farm/worker-job.js"; | ||
export { default as WorkerThread } from "./lib/worker-farm/worker-thread.js"; | ||
export { default as WorkerFarm } from "./lib/worker-farm/worker-farm.js"; | ||
export { default as WorkerPool } from "./lib/worker-farm/worker-pool.js"; | ||
export { default as WorkerBody } from "./lib/worker-farm/worker-body.js"; | ||
export type { ProcessOnWorkerOptions } from "./lib/worker-api/process-on-worker.js"; | ||
export { processOnWorker, canProcessOnWorker } from "./lib/worker-api/process-on-worker.js"; | ||
export { createWorker } from "./lib/worker-api/create-worker.js"; | ||
export { getWorkerURL } from "./lib/worker-api/get-worker-url.js"; | ||
export { validateWorkerVersion } from "./lib/worker-api/validate-worker-version.js"; | ||
export { getTransferList, getTransferListForWriter } from "./lib/worker-utils/get-transfer-list.js"; | ||
export { getLibraryUrl, loadLibrary } from "./lib/library-utils/library-utils.js"; | ||
export { default as AsyncQueue } from "./lib/async-queue/async-queue.js"; | ||
export { default as ChildProcessProxy } from "./lib/process-utils/child-process-proxy.js"; | ||
/** A null worker to test that worker processing is functional */ | ||
export declare const NullWorker: WorkerObject; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -0,6 +1,12 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { VERSION } from "./lib/env-utils/version.js"; | ||
// GENERAL UTILS | ||
export { assert } from "./lib/env-utils/assert.js"; | ||
export { isBrowser, isWorker } from "./lib/env-utils/globals.js"; | ||
// WORKER UTILS - TYPES | ||
export { default as WorkerJob } from "./lib/worker-farm/worker-job.js"; | ||
export { default as WorkerThread } from "./lib/worker-farm/worker-thread.js"; | ||
// WORKER FARMS | ||
export { default as WorkerFarm } from "./lib/worker-farm/worker-farm.js"; | ||
@@ -11,17 +17,22 @@ export { default as WorkerPool } from "./lib/worker-farm/worker-pool.js"; | ||
export { createWorker } from "./lib/worker-api/create-worker.js"; | ||
// WORKER UTILS - EXPORTS | ||
export { getWorkerURL } from "./lib/worker-api/get-worker-url.js"; | ||
export { validateWorkerVersion } from "./lib/worker-api/validate-worker-version.js"; | ||
export { getTransferList, getTransferListForWriter } from "./lib/worker-utils/get-transfer-list.js"; | ||
// LIBRARY UTILS | ||
export { getLibraryUrl, loadLibrary } from "./lib/library-utils/library-utils.js"; | ||
// PARSER UTILS | ||
export { default as AsyncQueue } from "./lib/async-queue/async-queue.js"; | ||
// PROCESS UTILS | ||
export { default as ChildProcessProxy } from "./lib/process-utils/child-process-proxy.js"; | ||
// WORKER OBJECTS | ||
/** A null worker to test that worker processing is functional */ | ||
export const NullWorker = { | ||
id: 'null', | ||
name: 'null', | ||
module: 'worker-utils', | ||
version: VERSION, | ||
options: { | ||
null: {} | ||
} | ||
id: 'null', | ||
name: 'null', | ||
module: 'worker-utils', | ||
version: VERSION, | ||
options: { | ||
null: {} | ||
} | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,75 +0,87 @@ | ||
let _Symbol$asyncIterator; | ||
_Symbol$asyncIterator = Symbol.asyncIterator; | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
// From https://github.com/rauschma/async-iter-demo/tree/master/src under MIT license | ||
// http://2ality.com/2016/10/asynchronous-iteration.html | ||
/** | ||
* Async Queue | ||
* - AsyncIterable: An async iterator can be | ||
* - Values can be pushed onto the queue | ||
* @example | ||
* const asyncQueue = new AsyncQueue(); | ||
* setTimeout(() => asyncQueue.enqueue('tick'), 1000); | ||
* setTimeout(() => asyncQueue.enqueue(new Error('done')), 10000); | ||
* for await (const value of asyncQueue) { | ||
* console.log(value); // tick | ||
* } | ||
*/ | ||
export default class AsyncQueue { | ||
constructor() { | ||
this._values = void 0; | ||
this._settlers = void 0; | ||
this._closed = void 0; | ||
this._values = []; | ||
this._settlers = []; | ||
this._closed = false; | ||
} | ||
[_Symbol$asyncIterator]() { | ||
return this; | ||
} | ||
push(value) { | ||
return this.enqueue(value); | ||
} | ||
enqueue(value) { | ||
if (this._closed) { | ||
throw new Error('Closed'); | ||
constructor() { | ||
this._values = []; // enqueues > dequeues | ||
this._settlers = []; // dequeues > enqueues | ||
this._closed = false; | ||
} | ||
if (this._settlers.length > 0) { | ||
if (this._values.length > 0) { | ||
throw new Error('Illegal internal state'); | ||
} | ||
const settler = this._settlers.shift(); | ||
if (value instanceof Error) { | ||
settler.reject(value); | ||
} else { | ||
settler.resolve({ | ||
value | ||
}); | ||
} | ||
} else { | ||
this._values.push(value); | ||
/** Return an async iterator for this queue */ | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
} | ||
} | ||
close() { | ||
while (this._settlers.length > 0) { | ||
const settler = this._settlers.shift(); | ||
settler.resolve({ | ||
done: true | ||
}); | ||
/** Push a new value - the async iterator will yield a promise resolved to this value */ | ||
push(value) { | ||
return this.enqueue(value); | ||
} | ||
this._closed = true; | ||
} | ||
next() { | ||
if (this._values.length > 0) { | ||
const value = this._values.shift(); | ||
if (value instanceof Error) { | ||
return Promise.reject(value); | ||
} | ||
return Promise.resolve({ | ||
done: false, | ||
value | ||
}); | ||
/** | ||
* Push a new value - the async iterator will yield a promise resolved to this value | ||
* Add an error - the async iterator will yield a promise rejected with this value | ||
*/ | ||
enqueue(value) { | ||
if (this._closed) { | ||
throw new Error('Closed'); | ||
} | ||
if (this._settlers.length > 0) { | ||
if (this._values.length > 0) { | ||
throw new Error('Illegal internal state'); | ||
} | ||
const settler = this._settlers.shift(); | ||
if (value instanceof Error) { | ||
settler.reject(value); | ||
} | ||
else { | ||
settler.resolve({ value }); | ||
} | ||
} | ||
else { | ||
this._values.push(value); | ||
} | ||
} | ||
if (this._closed) { | ||
if (this._settlers.length > 0) { | ||
throw new Error('Illegal internal state'); | ||
} | ||
return Promise.resolve({ | ||
done: true, | ||
value: undefined | ||
}); | ||
/** Indicate that we not waiting for more values - The async iterator will be done */ | ||
close() { | ||
while (this._settlers.length > 0) { | ||
const settler = this._settlers.shift(); | ||
settler.resolve({ done: true }); | ||
} | ||
this._closed = true; | ||
} | ||
return new Promise((resolve, reject) => { | ||
this._settlers.push({ | ||
resolve, | ||
reject | ||
}); | ||
}); | ||
} | ||
// ITERATOR IMPLEMENTATION | ||
/** @returns a Promise for an IteratorResult */ | ||
next() { | ||
// If values in queue, yield the first value | ||
if (this._values.length > 0) { | ||
const value = this._values.shift(); | ||
if (value instanceof Error) { | ||
return Promise.reject(value); | ||
} | ||
return Promise.resolve({ done: false, value }); | ||
} | ||
// If queue is closed, the iterator is done | ||
if (this._closed) { | ||
if (this._settlers.length > 0) { | ||
throw new Error('Illegal internal state'); | ||
} | ||
return Promise.resolve({ done: true, value: undefined }); | ||
} | ||
// Yield a promise that waits for new values to be enqueued | ||
return new Promise((resolve, reject) => { | ||
this._settlers.push({ resolve, reject }); | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=async-queue.js.map |
@@ -0,6 +1,12 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
// Replacement for the external assert method to reduce bundle size | ||
// Note: We don't use the second "message" argument in calling code, | ||
// so no need to support it here | ||
/** Throws an `Error` with the optional `message` if `condition` is falsy */ | ||
export function assert(condition, message) { | ||
if (!condition) { | ||
throw new Error(message || 'loaders.gl assertion failed.'); | ||
} | ||
if (!condition) { | ||
throw new Error(message || 'loaders.gl assertion failed.'); | ||
} | ||
} | ||
//# sourceMappingURL=assert.js.map |
@@ -0,6 +1,12 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
// Purpose: include this in your module to avoids adding dependencies on | ||
// micro modules like 'global' and 'is-browser'; | ||
/* eslint-disable no-restricted-globals */ | ||
const globals = { | ||
self: typeof self !== 'undefined' && self, | ||
window: typeof window !== 'undefined' && window, | ||
global: typeof global !== 'undefined' && global, | ||
document: typeof document !== 'undefined' && document | ||
self: typeof self !== 'undefined' && self, | ||
window: typeof window !== 'undefined' && window, | ||
global: typeof global !== 'undefined' && global, | ||
document: typeof document !== 'undefined' && document | ||
}; | ||
@@ -12,7 +18,13 @@ const self_ = globals.self || globals.window || globals.global || {}; | ||
export { self_ as self, window_ as window, global_ as global, document_ as document }; | ||
export const isBrowser = typeof process !== 'object' || String(process) !== '[object process]' || process.browser; | ||
/** true if running in the browser, false if running in Node.js */ | ||
export const isBrowser = | ||
// @ts-ignore process.browser | ||
typeof process !== 'object' || String(process) !== '[object process]' || process.browser; | ||
/** true if running on a worker thread */ | ||
export const isWorker = typeof importScripts === 'function'; | ||
/** true if running on a mobile device */ | ||
export const isMobile = typeof window !== 'undefined' && typeof window.orientation !== 'undefined'; | ||
// Extract node major version | ||
const matches = typeof process !== 'undefined' && process.version && /v([0-9]*)/.exec(process.version); | ||
export const nodeVersion = matches && parseFloat(matches[1]) || 0; | ||
//# sourceMappingURL=globals.js.map | ||
/** Version of Node.js if running under Node, otherwise 0 */ | ||
export const nodeVersion = (matches && parseFloat(matches[1])) || 0; |
@@ -0,16 +1,25 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
// Version constant cannot be imported, it needs to correspond to the build version of **this** module. | ||
/** | ||
* TODO - unpkg.com doesn't seem to have a `latest` specifier for alpha releases... | ||
* 'beta' on beta branch, 'latest' on prod branch | ||
*/ | ||
export const NPM_TAG = 'latest'; | ||
function getVersion() { | ||
var _globalThis$_loadersg; | ||
if (!((_globalThis$_loadersg = globalThis._loadersgl_) !== null && _globalThis$_loadersg !== void 0 && _globalThis$_loadersg.version)) { | ||
globalThis._loadersgl_ = globalThis._loadersgl_ || {}; | ||
if (typeof "4.2.0-alpha.4" === 'undefined') { | ||
console.warn('loaders.gl: The __VERSION__ variable is not injected using babel plugin. Latest unstable workers would be fetched from the CDN.'); | ||
globalThis._loadersgl_.version = NPM_TAG; | ||
} else { | ||
globalThis._loadersgl_.version = "4.2.0-alpha.4"; | ||
if (!globalThis._loadersgl_?.version) { | ||
globalThis._loadersgl_ = globalThis._loadersgl_ || {}; | ||
// __VERSION__ is injected by babel-plugin-version-inline | ||
if (typeof "4.2.0-alpha.4" === 'undefined') { | ||
// eslint-disable-next-line | ||
console.warn('loaders.gl: The __VERSION__ variable is not injected using babel plugin. Latest unstable workers would be fetched from the CDN.'); | ||
globalThis._loadersgl_.version = NPM_TAG; | ||
} | ||
else { | ||
globalThis._loadersgl_.version = "4.2.0-alpha.4"; | ||
} | ||
} | ||
} | ||
return globalThis._loadersgl_.version; | ||
return globalThis._loadersgl_.version; | ||
} | ||
export const VERSION = getVersion(); | ||
//# sourceMappingURL=version.js.map |
@@ -0,1 +1,5 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
/* global importScripts */ | ||
import { isBrowser, isWorker } from "../env-utils/globals.js"; | ||
@@ -5,86 +9,157 @@ import * as node from "../node/require-utils.node.js"; | ||
import { VERSION } from "../env-utils/version.js"; | ||
const loadLibraryPromises = {}; | ||
export async function loadLibrary(libraryUrl) { | ||
let moduleName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
let libraryName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
if (moduleName) { | ||
libraryUrl = getLibraryUrl(libraryUrl, moduleName, options, libraryName); | ||
} | ||
loadLibraryPromises[libraryUrl] = loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl); | ||
return await loadLibraryPromises[libraryUrl]; | ||
const loadLibraryPromises = {}; // promises | ||
/** | ||
* Dynamically loads a library ("module") | ||
* | ||
* - wasm library: Array buffer is returned | ||
* - js library: Parse JS is returned | ||
* | ||
* Method depends on environment | ||
* - browser - script element is created and installed on document | ||
* - worker - eval is called on global context | ||
* - node - file is required | ||
* | ||
* @param libraryUrl | ||
* @param moduleName | ||
* @param options | ||
*/ | ||
export async function loadLibrary(libraryUrl, moduleName = null, options = {}, libraryName = null) { | ||
if (moduleName) { | ||
libraryUrl = getLibraryUrl(libraryUrl, moduleName, options, libraryName); | ||
} | ||
// Ensure libraries are only loaded once | ||
loadLibraryPromises[libraryUrl] = | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl); | ||
return await loadLibraryPromises[libraryUrl]; | ||
} | ||
export function getLibraryUrl(library, moduleName) { | ||
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
let libraryName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
if (!options.useLocalLibraries && library.startsWith('http')) { | ||
return library; | ||
} | ||
libraryName = libraryName || library; | ||
const modules = options.modules || {}; | ||
if (modules[libraryName]) { | ||
return modules[libraryName]; | ||
} | ||
if (!isBrowser) { | ||
return `modules/${moduleName}/dist/libs/${libraryName}`; | ||
} | ||
if (options.CDN) { | ||
assert(options.CDN.startsWith('http')); | ||
return `${options.CDN}/${moduleName}@${VERSION}/dist/libs/${libraryName}`; | ||
} | ||
if (isWorker) { | ||
return `../src/libs/${libraryName}`; | ||
} | ||
return `modules/${moduleName}/src/libs/${libraryName}`; | ||
// TODO - sort out how to resolve paths for main/worker and dev/prod | ||
export function getLibraryUrl(library, moduleName, options = {}, libraryName = null) { | ||
// Check if already a URL | ||
if (!options.useLocalLibraries && library.startsWith('http')) { | ||
return library; | ||
} | ||
libraryName = libraryName || library; | ||
// Allow application to import and supply libraries through `options.modules` | ||
const modules = options.modules || {}; | ||
if (modules[libraryName]) { | ||
return modules[libraryName]; | ||
} | ||
// Load from local files, not from CDN scripts in Node.js | ||
// TODO - needs to locate the modules directory when installed! | ||
if (!isBrowser) { | ||
return `modules/${moduleName}/dist/libs/${libraryName}`; | ||
} | ||
// In browser, load from external scripts | ||
if (options.CDN) { | ||
assert(options.CDN.startsWith('http')); | ||
return `${options.CDN}/${moduleName}@${VERSION}/dist/libs/${libraryName}`; | ||
} | ||
// TODO - loading inside workers requires paths relative to worker script location... | ||
if (isWorker) { | ||
return `../src/libs/${libraryName}`; | ||
} | ||
return `modules/${moduleName}/src/libs/${libraryName}`; | ||
} | ||
async function loadLibraryFromFile(libraryUrl) { | ||
if (libraryUrl.endsWith('wasm')) { | ||
return await loadAsArrayBuffer(libraryUrl); | ||
} | ||
if (!isBrowser) { | ||
try { | ||
return node && node.requireFromFile && (await node.requireFromFile(libraryUrl)); | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
if (libraryUrl.endsWith('wasm')) { | ||
return await loadAsArrayBuffer(libraryUrl); | ||
} | ||
} | ||
if (isWorker) { | ||
return importScripts(libraryUrl); | ||
} | ||
const scriptSource = await loadAsText(libraryUrl); | ||
return loadLibraryFromString(scriptSource, libraryUrl); | ||
if (!isBrowser) { | ||
// TODO - Node doesn't yet support dynamic import from https URLs | ||
// try { | ||
// return await import(libraryUrl); | ||
// } catch (error) { | ||
// console.error(error); | ||
// } | ||
try { | ||
return node && node.requireFromFile && (await node.requireFromFile(libraryUrl)); | ||
} | ||
catch (error) { | ||
console.error(error); // eslint-disable-line no-console | ||
return null; | ||
} | ||
} | ||
if (isWorker) { | ||
return importScripts(libraryUrl); | ||
} | ||
// TODO - fix - should be more secure than string parsing since observes CORS | ||
// if (isBrowser) { | ||
// return await loadScriptFromFile(libraryUrl); | ||
// } | ||
const scriptSource = await loadAsText(libraryUrl); | ||
return loadLibraryFromString(scriptSource, libraryUrl); | ||
} | ||
/* | ||
async function loadScriptFromFile(libraryUrl) { | ||
const script = document.createElement('script'); | ||
script.src = libraryUrl; | ||
return await new Promise((resolve, reject) => { | ||
script.onload = data => { | ||
resolve(data); | ||
}; | ||
script.onerror = reject; | ||
}); | ||
} | ||
*/ | ||
// TODO - Needs security audit... | ||
// - Raw eval call | ||
// - Potentially bypasses CORS | ||
// Upside is that this separates fetching and parsing | ||
// we could create a`LibraryLoader` or`ModuleLoader` | ||
function loadLibraryFromString(scriptSource, id) { | ||
if (!isBrowser) { | ||
return node.requireFromString && node.requireFromString(scriptSource, id); | ||
} | ||
if (isWorker) { | ||
eval.call(globalThis, scriptSource); | ||
if (!isBrowser) { | ||
return node.requireFromString && node.requireFromString(scriptSource, id); | ||
} | ||
if (isWorker) { | ||
// Use lvalue trick to make eval run in global scope | ||
eval.call(globalThis, scriptSource); // eslint-disable-line no-eval | ||
// https://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript | ||
// http://perfectionkills.com/global-eval-what-are-the-options/ | ||
return null; | ||
} | ||
const script = document.createElement('script'); | ||
script.id = id; | ||
// most browsers like a separate text node but some throw an error. The second method covers those. | ||
try { | ||
script.appendChild(document.createTextNode(scriptSource)); | ||
} | ||
catch (e) { | ||
script.text = scriptSource; | ||
} | ||
document.body.appendChild(script); | ||
return null; | ||
} | ||
const script = document.createElement('script'); | ||
script.id = id; | ||
try { | ||
script.appendChild(document.createTextNode(scriptSource)); | ||
} catch (e) { | ||
script.text = scriptSource; | ||
} | ||
document.body.appendChild(script); | ||
return null; | ||
} | ||
// TODO - technique for module injection into worker, from THREE.DracoLoader... | ||
/* | ||
function combineWorkerWithLibrary(worker, jsContent) { | ||
var fn = wWorker.toString(); | ||
var body = [ | ||
'// injected', | ||
jsContent, | ||
'', | ||
'// worker', | ||
fn.substring(fn.indexOf('{') + 1, fn.lastIndexOf('}')) | ||
].join('\n'); | ||
this.workerSourceURL = URL.createObjectURL(new Blob([body])); | ||
} | ||
*/ | ||
async function loadAsArrayBuffer(url) { | ||
if (isBrowser || !node.readFileAsArrayBuffer || url.startsWith('http')) { | ||
const response = await fetch(url); | ||
return await response.arrayBuffer(); | ||
} | ||
return await node.readFileAsArrayBuffer(url); | ||
if (isBrowser || !node.readFileAsArrayBuffer || url.startsWith('http')) { | ||
const response = await fetch(url); | ||
return await response.arrayBuffer(); | ||
} | ||
return await node.readFileAsArrayBuffer(url); | ||
} | ||
/** | ||
* Load a file from local file system | ||
* @param filename | ||
* @returns | ||
*/ | ||
async function loadAsText(url) { | ||
if (isBrowser || !node.readFileAsText || url.startsWith('http')) { | ||
const response = await fetch(url); | ||
return await response.text(); | ||
} | ||
return await node.readFileAsText(url); | ||
if (isBrowser || !node.readFileAsText || url.startsWith('http')) { | ||
const response = await fetch(url); | ||
return await response.text(); | ||
} | ||
return await node.readFileAsText(url); | ||
} | ||
//# sourceMappingURL=library-utils.js.map |
@@ -0,54 +1,83 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
// Fork of https://github.com/floatdrop/require-from-string/blob/master/index.js | ||
// Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com> (github.com/floatdrop) | ||
// MIT license | ||
// this file is not visible to webpack (it is excluded in the package.json "browser" field). | ||
import Module from 'module'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
/** | ||
* Load a file from local file system | ||
* @param filename | ||
* @returns | ||
*/ | ||
export async function readFileAsArrayBuffer(filename) { | ||
if (filename.startsWith('http')) { | ||
const response = await fetch(filename); | ||
return await response.arrayBuffer(); | ||
} | ||
const buffer = fs.readFileSync(filename); | ||
return buffer.buffer; | ||
if (filename.startsWith('http')) { | ||
const response = await fetch(filename); | ||
return await response.arrayBuffer(); | ||
} | ||
const buffer = fs.readFileSync(filename); | ||
return buffer.buffer; | ||
} | ||
/** | ||
* Load a file from local file system | ||
* @param filename | ||
* @returns | ||
*/ | ||
export async function readFileAsText(filename) { | ||
if (filename.startsWith('http')) { | ||
const response = await fetch(filename); | ||
return await response.text(); | ||
} | ||
const text = fs.readFileSync(filename, 'utf8'); | ||
return text; | ||
if (filename.startsWith('http')) { | ||
const response = await fetch(filename); | ||
return await response.text(); | ||
} | ||
const text = fs.readFileSync(filename, 'utf8'); | ||
return text; | ||
} | ||
// Node.js Dynamically require from file | ||
// Relative names are resolved relative to cwd | ||
// This indirect function is provided because webpack will try to bundle `module.require`. | ||
// this file is not visible to webpack (it is excluded in the package.json "browser" field). | ||
export async function requireFromFile(filename) { | ||
if (filename.startsWith('http')) { | ||
const response = await fetch(filename); | ||
const code = await response.text(); | ||
if (filename.startsWith('http')) { | ||
const response = await fetch(filename); | ||
const code = await response.text(); | ||
return requireFromString(code); | ||
} | ||
if (!filename.startsWith('/')) { | ||
filename = `${process.cwd()}/${filename}`; | ||
} | ||
const code = await fs.promises.readFile(filename, 'utf8'); | ||
return requireFromString(code); | ||
} | ||
if (!filename.startsWith('/')) { | ||
filename = `${process.cwd()}/${filename}`; | ||
} | ||
const code = await fs.promises.readFile(filename, 'utf8'); | ||
return requireFromString(code); | ||
} | ||
export function requireFromString(code) { | ||
var _module, _options, _options2; | ||
let filename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
let options = arguments.length > 2 ? arguments[2] : undefined; | ||
if (typeof filename === 'object') { | ||
options = filename; | ||
filename = ''; | ||
} | ||
if (typeof code !== 'string') { | ||
throw new Error(`code must be a string, not ${typeof code}`); | ||
} | ||
const paths = Module._nodeModulePaths(path.dirname(filename)); | ||
const parent = typeof module !== 'undefined' && ((_module = module) === null || _module === void 0 ? void 0 : _module.parent); | ||
const newModule = new Module(filename, parent); | ||
newModule.filename = filename; | ||
newModule.paths = [].concat(((_options = options) === null || _options === void 0 ? void 0 : _options.prependPaths) || []).concat(paths).concat(((_options2 = options) === null || _options2 === void 0 ? void 0 : _options2.appendPaths) || []); | ||
newModule._compile(code, filename); | ||
if (parent && parent.children) { | ||
parent.children.splice(parent.children.indexOf(newModule), 1); | ||
} | ||
return newModule.exports; | ||
// Dynamically require from string | ||
// - `code` - Required - Type: string - Module code. | ||
// - `filename` - Type: string - Default: '' - Optional filename. | ||
// - `options.appendPaths` Type: Array List of paths, that will be appended to module paths. | ||
// Useful, when you want to be able require modules from these paths. | ||
// - `options.prependPaths` Type: Array Same as appendPaths, but paths will be prepended. | ||
export function requireFromString(code, filename = '', options) { | ||
if (typeof filename === 'object') { | ||
options = filename; | ||
filename = ''; | ||
} | ||
if (typeof code !== 'string') { | ||
throw new Error(`code must be a string, not ${typeof code}`); | ||
} | ||
// @ts-ignore | ||
const paths = Module._nodeModulePaths(path.dirname(filename)); | ||
const parent = typeof module !== 'undefined' && module?.parent; | ||
// @ts-ignore | ||
const newModule = new Module(filename, parent); | ||
newModule.filename = filename; | ||
newModule.paths = [] | ||
.concat(options?.prependPaths || []) | ||
.concat(paths) | ||
.concat(options?.appendPaths || []); | ||
// @ts-ignore | ||
newModule._compile(code, filename); | ||
if (parent && parent.children) { | ||
parent.children.splice(parent.children.indexOf(newModule), 1); | ||
} | ||
return newModule.exports; | ||
} | ||
//# sourceMappingURL=require-utils.node.js.map |
@@ -0,5 +1,12 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
/** Browser polyfill for Node.js built-in `worker_threads` module. | ||
* These fills are non-functional, and just intended to ensure that | ||
* `import 'worker_threads` doesn't break browser builds. | ||
* The replacement is done in package.json browser field | ||
*/ | ||
export class NodeWorker { | ||
terminate() {} | ||
terminate() { } | ||
} | ||
export const parentPort = null; | ||
//# sourceMappingURL=worker_threads-browser.js.map |
@@ -0,5 +1,7 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import * as WorkerThreads from 'worker_threads'; | ||
export * from 'worker_threads'; | ||
export const parentPort = WorkerThreads === null || WorkerThreads === void 0 ? void 0 : WorkerThreads.parentPort; | ||
export const parentPort = WorkerThreads?.parentPort; | ||
export const NodeWorker = WorkerThreads.Worker; | ||
//# sourceMappingURL=worker_threads.js.map |
@@ -0,105 +1,113 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
/* eslint-disable no-console */ | ||
// Avoid using named imports for Node builtins to help with "empty" resolution | ||
// for bundlers targeting browser environments. Access imports & types | ||
// through the `ChildProcess` object (e.g. `ChildProcess.spawn`, `ChildProcess.ChildProcess`). | ||
import * as ChildProcess from 'child_process'; | ||
import { getAvailablePort } from "./process-utils.js"; | ||
const DEFAULT_PROPS = { | ||
command: '', | ||
arguments: [], | ||
port: 5000, | ||
autoPort: true, | ||
wait: 2000, | ||
onSuccess: processProxy => { | ||
console.log(`Started ${processProxy.props.command}`); | ||
} | ||
command: '', | ||
arguments: [], | ||
port: 5000, | ||
autoPort: true, | ||
wait: 2000, | ||
onSuccess: (processProxy) => { | ||
console.log(`Started ${processProxy.props.command}`); | ||
} | ||
}; | ||
/** | ||
* Manager for a Node.js child process | ||
* Prepares arguments, starts, stops and tracks output | ||
*/ | ||
export default class ChildProcessProxy { | ||
constructor() { | ||
let { | ||
id = 'browser-driver' | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
this.id = void 0; | ||
this.props = { | ||
...DEFAULT_PROPS | ||
}; | ||
this.childProcess = null; | ||
this.port = 0; | ||
this.successTimer = void 0; | ||
this.id = id; | ||
} | ||
async start(props) { | ||
props = { | ||
...DEFAULT_PROPS, | ||
...props | ||
}; | ||
this.props = props; | ||
const args = [...props.arguments]; | ||
this.port = Number(props.port); | ||
if (props.portArg) { | ||
if (props.autoPort) { | ||
this.port = await getAvailablePort(props.port); | ||
} | ||
args.push(props.portArg, String(this.port)); | ||
// constructor(props?: {id?: string}); | ||
constructor({ id = 'browser-driver' } = {}) { | ||
this.props = { ...DEFAULT_PROPS }; | ||
this.childProcess = null; | ||
this.port = 0; | ||
this.id = id; | ||
} | ||
return await new Promise((resolve, reject) => { | ||
try { | ||
this._setTimeout(() => { | ||
if (props.onSuccess) { | ||
props.onSuccess(this); | ||
} | ||
resolve({}); | ||
/** Starts a child process with the provided props */ | ||
async start(props) { | ||
props = { ...DEFAULT_PROPS, ...props }; | ||
this.props = props; | ||
const args = [...props.arguments]; | ||
// If portArg is set, we can look up an available port | ||
this.port = Number(props.port); | ||
if (props.portArg) { | ||
if (props.autoPort) { | ||
this.port = await getAvailablePort(props.port); | ||
} | ||
args.push(props.portArg, String(this.port)); | ||
} | ||
return await new Promise((resolve, reject) => { | ||
try { | ||
this._setTimeout(() => { | ||
if (props.onSuccess) { | ||
props.onSuccess(this); | ||
} | ||
resolve({}); | ||
}); | ||
console.log(`Spawning ${props.command} ${props.arguments.join(' ')}`); | ||
const childProcess = ChildProcess.spawn(props.command, args, props.spawn); | ||
this.childProcess = childProcess; | ||
childProcess.stdout.on('data', (data) => { | ||
console.log(data.toString()); | ||
}); | ||
childProcess.stderr.on('data', (data) => { | ||
console.log(`Child process wrote to stderr: "${data}".`); | ||
if (!props.ignoreStderr) { | ||
this._clearTimeout(); | ||
reject(new Error(data)); | ||
} | ||
}); | ||
childProcess.on('error', (error) => { | ||
console.log(`Child process errored with ${error}`); | ||
this._clearTimeout(); | ||
reject(error); | ||
}); | ||
childProcess.on('close', (code) => { | ||
console.log(`Child process exited with ${code}`); | ||
this.childProcess = null; | ||
this._clearTimeout(); | ||
resolve({}); | ||
}); | ||
} | ||
catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
console.log(`Spawning ${props.command} ${props.arguments.join(' ')}`); | ||
const childProcess = ChildProcess.spawn(props.command, args, props.spawn); | ||
this.childProcess = childProcess; | ||
childProcess.stdout.on('data', data => { | ||
console.log(data.toString()); | ||
}); | ||
childProcess.stderr.on('data', data => { | ||
console.log(`Child process wrote to stderr: "${data}".`); | ||
if (!props.ignoreStderr) { | ||
this._clearTimeout(); | ||
reject(new Error(data)); | ||
} | ||
}); | ||
childProcess.on('error', error => { | ||
console.log(`Child process errored with ${error}`); | ||
this._clearTimeout(); | ||
reject(error); | ||
}); | ||
childProcess.on('close', code => { | ||
console.log(`Child process exited with ${code}`); | ||
this.childProcess = null; | ||
this._clearTimeout(); | ||
resolve({}); | ||
}); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
} | ||
async stop() { | ||
if (this.childProcess) { | ||
this.childProcess.kill(); | ||
this.childProcess = null; | ||
} | ||
} | ||
async exit() { | ||
let statusCode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
try { | ||
await this.stop(); | ||
process.exit(statusCode); | ||
} catch (error) { | ||
console.error(error.message || error); | ||
process.exit(1); | ||
/** Stops a running child process */ | ||
async stop() { | ||
if (this.childProcess) { | ||
this.childProcess.kill(); | ||
this.childProcess = null; | ||
} | ||
} | ||
} | ||
_setTimeout(callback) { | ||
if (Number(this.props.wait) > 0) { | ||
this.successTimer = setTimeout(callback, this.props.wait); | ||
/** Exits this process */ | ||
async exit(statusCode = 0) { | ||
try { | ||
await this.stop(); | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(statusCode); | ||
} | ||
catch (error) { | ||
console.error(error.message || error); | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
_clearTimeout() { | ||
if (this.successTimer) { | ||
clearTimeout(this.successTimer); | ||
_setTimeout(callback) { | ||
if (Number(this.props.wait) > 0) { | ||
this.successTimer = setTimeout(callback, this.props.wait); | ||
} | ||
} | ||
} | ||
_clearTimeout() { | ||
if (this.successTimer) { | ||
clearTimeout(this.successTimer); | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=child-process-proxy.js.map |
@@ -0,26 +1,31 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import ChildProcess from 'child_process'; | ||
export function getAvailablePort() { | ||
let defaultPort = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3000; | ||
return new Promise(resolve => { | ||
ChildProcess.exec('lsof -i -P -n | grep LISTEN', (error, stdout) => { | ||
if (error) { | ||
resolve(defaultPort); | ||
return; | ||
} | ||
const portsInUse = []; | ||
const regex = /:(\d+) \(LISTEN\)/; | ||
stdout.split('\n').forEach(line => { | ||
const match = regex.exec(line); | ||
if (match) { | ||
portsInUse.push(Number(match[1])); | ||
} | ||
}); | ||
let port = defaultPort; | ||
while (portsInUse.includes(port)) { | ||
port++; | ||
} | ||
resolve(port); | ||
// Get an available port | ||
// Works on Unix systems | ||
export function getAvailablePort(defaultPort = 3000) { | ||
return new Promise((resolve) => { | ||
// Get a list of all ports in use | ||
ChildProcess.exec('lsof -i -P -n | grep LISTEN', (error, stdout) => { | ||
if (error) { | ||
// likely no permission, e.g. CI | ||
resolve(defaultPort); | ||
return; | ||
} | ||
const portsInUse = []; | ||
const regex = /:(\d+) \(LISTEN\)/; | ||
stdout.split('\n').forEach((line) => { | ||
const match = regex.exec(line); | ||
if (match) { | ||
portsInUse.push(Number(match[1])); | ||
} | ||
}); | ||
let port = defaultPort; | ||
while (portsInUse.includes(port)) { | ||
port++; | ||
} | ||
resolve(port); | ||
}); | ||
}); | ||
}); | ||
} | ||
//# sourceMappingURL=process-utils.js.map |
@@ -1,2 +0,2 @@ | ||
import type { WorkerContext, Process, ProcessInBatches } from '../../types'; | ||
import type { WorkerContext, Process, ProcessInBatches } from "../../types.js"; | ||
export type ProcessOnMainThread = (data: any, options?: { | ||
@@ -3,0 +3,0 @@ [key: string]: any; |
@@ -0,84 +1,87 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import AsyncQueue from "../async-queue/async-queue.js"; | ||
import WorkerBody from "../worker-farm/worker-body.js"; | ||
// import {validateWorkerVersion} from './validate-worker-version'; | ||
/** Counter for jobs */ | ||
let requestId = 0; | ||
let inputBatches; | ||
let options; | ||
/** | ||
* Set up a WebWorkerGlobalScope to talk with the main thread | ||
*/ | ||
export async function createWorker(process, processInBatches) { | ||
if (!(await WorkerBody.inWorkerThread())) { | ||
return; | ||
} | ||
const context = { | ||
process: processOnMainThread | ||
}; | ||
WorkerBody.onmessage = async (type, payload) => { | ||
try { | ||
switch (type) { | ||
case 'process': | ||
if (!process) { | ||
throw new Error('Worker does not support atomic processing'); | ||
} | ||
const result = await process(payload.input, payload.options || {}, context); | ||
WorkerBody.postMessage('done', { | ||
result | ||
}); | ||
break; | ||
case 'process-in-batches': | ||
if (!processInBatches) { | ||
throw new Error('Worker does not support batched processing'); | ||
} | ||
inputBatches = new AsyncQueue(); | ||
options = payload.options || {}; | ||
const resultIterator = processInBatches(inputBatches, options, context); | ||
for await (const batch of resultIterator) { | ||
WorkerBody.postMessage('output-batch', { | ||
result: batch | ||
}); | ||
} | ||
WorkerBody.postMessage('done', {}); | ||
break; | ||
case 'input-batch': | ||
inputBatches.push(payload.input); | ||
break; | ||
case 'input-done': | ||
inputBatches.close(); | ||
break; | ||
default: | ||
} | ||
} catch (error) { | ||
const message = error instanceof Error ? error.message : ''; | ||
WorkerBody.postMessage('error', { | ||
error: message | ||
}); | ||
if (!(await WorkerBody.inWorkerThread())) { | ||
return; | ||
} | ||
}; | ||
} | ||
function processOnMainThread(arrayBuffer) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return new Promise((resolve, reject) => { | ||
const id = requestId++; | ||
const onMessage = (type, payload) => { | ||
if (payload.id !== id) { | ||
return; | ||
} | ||
switch (type) { | ||
case 'done': | ||
WorkerBody.removeEventListener(onMessage); | ||
resolve(payload.result); | ||
break; | ||
case 'error': | ||
WorkerBody.removeEventListener(onMessage); | ||
reject(payload.error); | ||
break; | ||
default: | ||
} | ||
const context = { | ||
process: processOnMainThread | ||
}; | ||
WorkerBody.addEventListener(onMessage); | ||
const payload = { | ||
id, | ||
input: arrayBuffer, | ||
options | ||
// eslint-disable-next-line complexity | ||
WorkerBody.onmessage = async (type, payload) => { | ||
try { | ||
switch (type) { | ||
case 'process': | ||
if (!process) { | ||
throw new Error('Worker does not support atomic processing'); | ||
} | ||
const result = await process(payload.input, payload.options || {}, context); | ||
WorkerBody.postMessage('done', { result }); | ||
break; | ||
case 'process-in-batches': | ||
if (!processInBatches) { | ||
throw new Error('Worker does not support batched processing'); | ||
} | ||
inputBatches = new AsyncQueue(); | ||
options = payload.options || {}; | ||
const resultIterator = processInBatches(inputBatches, options, context); | ||
for await (const batch of resultIterator) { | ||
WorkerBody.postMessage('output-batch', { result: batch }); | ||
} | ||
WorkerBody.postMessage('done', {}); | ||
break; | ||
case 'input-batch': | ||
inputBatches.push(payload.input); | ||
break; | ||
case 'input-done': | ||
inputBatches.close(); | ||
break; | ||
default: | ||
} | ||
} | ||
catch (error) { | ||
const message = error instanceof Error ? error.message : ''; | ||
WorkerBody.postMessage('error', { error: message }); | ||
} | ||
}; | ||
WorkerBody.postMessage('process', payload); | ||
}); | ||
} | ||
//# sourceMappingURL=create-worker.js.map | ||
function processOnMainThread(arrayBuffer, options = {}) { | ||
return new Promise((resolve, reject) => { | ||
const id = requestId++; | ||
/** | ||
*/ | ||
const onMessage = (type, payload) => { | ||
if (payload.id !== id) { | ||
// not ours | ||
return; | ||
} | ||
switch (type) { | ||
case 'done': | ||
WorkerBody.removeEventListener(onMessage); | ||
resolve(payload.result); | ||
break; | ||
case 'error': | ||
WorkerBody.removeEventListener(onMessage); | ||
reject(payload.error); | ||
break; | ||
default: | ||
// ignore | ||
} | ||
}; | ||
WorkerBody.addEventListener(onMessage); | ||
// Ask the main thread to decode data | ||
const payload = { id, input: arrayBuffer, options }; | ||
WorkerBody.postMessage('process', payload); | ||
}); | ||
} |
@@ -1,2 +0,2 @@ | ||
import type { WorkerObject, WorkerOptions } from '../../types'; | ||
import type { WorkerObject, WorkerOptions } from "../../types.js"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Gets worker object's name (for debugging in Chrome thread inspector window) |
@@ -0,34 +1,60 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { assert } from "../env-utils/assert.js"; | ||
import { isBrowser } from "../env-utils/globals.js"; | ||
import { VERSION, NPM_TAG } from "../env-utils/version.js"; | ||
/** | ||
* Gets worker object's name (for debugging in Chrome thread inspector window) | ||
*/ | ||
export function getWorkerName(worker) { | ||
const warning = worker.version !== VERSION ? ` (worker-utils@${VERSION})` : ''; | ||
return `${worker.name}@${worker.version}${warning}`; | ||
const warning = worker.version !== VERSION ? ` (worker-utils@${VERSION})` : ''; | ||
return `${worker.name}@${worker.version}${warning}`; | ||
} | ||
export function getWorkerURL(worker) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
const workerOptions = options[worker.id] || {}; | ||
const workerFile = isBrowser ? `${worker.id}-worker.js` : `${worker.id}-worker-node.js`; | ||
let url = workerOptions.workerUrl; | ||
if (!url && worker.id === 'compression') { | ||
url = options.workerUrl; | ||
} | ||
if (options._workerType === 'test') { | ||
if (isBrowser) { | ||
url = `modules/${worker.module}/dist/${workerFile}`; | ||
} else { | ||
url = `modules/${worker.module}/src/workers/${worker.id}-worker-node.ts`; | ||
/** | ||
* Generate a worker URL based on worker object and options | ||
* @returns A URL to one of the following: | ||
* - a published worker on unpkg CDN | ||
* - a local test worker | ||
* - a URL provided by the user in options | ||
*/ | ||
export function getWorkerURL(worker, options = {}) { | ||
const workerOptions = options[worker.id] || {}; | ||
const workerFile = isBrowser ? `${worker.id}-worker.js` : `${worker.id}-worker-node.js`; | ||
let url = workerOptions.workerUrl; | ||
// HACK: Allow for non-nested workerUrl for the CompressionWorker. | ||
// For the compression worker, workerOptions is currently not nested correctly. For most loaders, | ||
// you'd have options within an object, i.e. `{mvt: {coordinates: ...}}` but the CompressionWorker | ||
// puts options at the top level, not within a `compression` key (its `id`). For this reason, the | ||
// above `workerOptions` will always be a string (i.e. `'gzip'`) for the CompressionWorker. To not | ||
// break backwards compatibility, we allow the CompressionWorker to have options at the top level. | ||
if (!url && worker.id === 'compression') { | ||
url = options.workerUrl; | ||
} | ||
} | ||
if (!url) { | ||
let version = worker.version; | ||
if (version === 'latest') { | ||
version = NPM_TAG; | ||
// If URL is test, generate local loaders.gl url | ||
// @ts-ignore _workerType | ||
if (options._workerType === 'test') { | ||
if (isBrowser) { | ||
url = `modules/${worker.module}/dist/${workerFile}`; | ||
} | ||
else { | ||
// In the test environment the ts-node loader requires TypeScript code | ||
url = `modules/${worker.module}/src/workers/${worker.id}-worker-node.ts`; | ||
} | ||
} | ||
const versionTag = version ? `@${version}` : ''; | ||
url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`; | ||
} | ||
assert(url); | ||
return url; | ||
// If url override is not provided, generate a URL to published version on npm CDN unpkg.com | ||
if (!url) { | ||
// GENERATE | ||
let version = worker.version; | ||
// On master we need to load npm alpha releases published with the `beta` tag | ||
if (version === 'latest') { | ||
// throw new Error('latest worker version specified'); | ||
version = NPM_TAG; | ||
} | ||
const versionTag = version ? `@${version}` : ''; | ||
url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`; | ||
} | ||
assert(url); | ||
// Allow user to override location | ||
return url; | ||
} | ||
//# sourceMappingURL=get-worker-url.js.map |
@@ -1,2 +0,2 @@ | ||
import type { WorkerObject, WorkerOptions, WorkerContext } from '../../types'; | ||
import type { WorkerObject, WorkerOptions, WorkerContext } from "../../types.js"; | ||
/** Options for worker processing */ | ||
@@ -3,0 +3,0 @@ export type ProcessOnWorkerOptions = WorkerOptions & { |
@@ -0,75 +1,78 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import WorkerFarm from "../worker-farm/worker-farm.js"; | ||
import { getWorkerURL, getWorkerName } from "./get-worker-url.js"; | ||
import { getTransferListForWriter } from "../worker-utils/get-transfer-list.js"; | ||
/** | ||
* Determines if we can parse with worker | ||
* @param loader | ||
* @param data | ||
* @param options | ||
*/ | ||
export function canProcessOnWorker(worker, options) { | ||
if (!WorkerFarm.isSupported()) { | ||
return false; | ||
} | ||
return worker.worker && (options === null || options === void 0 ? void 0 : options.worker); | ||
if (!WorkerFarm.isSupported()) { | ||
return false; | ||
} | ||
return worker.worker && options?.worker; | ||
} | ||
export async function processOnWorker(worker, data) { | ||
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
let context = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
const name = getWorkerName(worker); | ||
const workerFarm = WorkerFarm.getWorkerFarm(options); | ||
const { | ||
source | ||
} = options; | ||
const workerPoolProps = { | ||
name, | ||
source | ||
}; | ||
if (!source) { | ||
workerPoolProps.url = getWorkerURL(worker, options); | ||
} | ||
const workerPool = workerFarm.getWorkerPool(workerPoolProps); | ||
const jobName = options.jobName || worker.name; | ||
const job = await workerPool.startJob(jobName, onMessage.bind(null, context)); | ||
const transferableOptions = getTransferListForWriter(options); | ||
job.postMessage('process', { | ||
input: data, | ||
options: transferableOptions | ||
}); | ||
const result = await job.result; | ||
return result.result; | ||
/** | ||
* This function expects that the worker thread sends certain messages, | ||
* Creating such a worker can be automated if the worker is wrapper by a call to | ||
* createWorker in @loaders.gl/worker-utils. | ||
*/ | ||
export async function processOnWorker(worker, data, options = {}, context = {}) { | ||
const name = getWorkerName(worker); | ||
const workerFarm = WorkerFarm.getWorkerFarm(options); | ||
const { source } = options; | ||
const workerPoolProps = { name, source }; | ||
if (!source) { | ||
workerPoolProps.url = getWorkerURL(worker, options); | ||
} | ||
const workerPool = workerFarm.getWorkerPool(workerPoolProps); | ||
const jobName = options.jobName || worker.name; | ||
const job = await workerPool.startJob(jobName, | ||
// eslint-disable-next-line | ||
onMessage.bind(null, context)); | ||
// Kick off the processing in the worker | ||
const transferableOptions = getTransferListForWriter(options); | ||
job.postMessage('process', { input: data, options: transferableOptions }); | ||
const result = await job.result; | ||
return result.result; | ||
} | ||
/** | ||
* Job completes when we receive the result | ||
* @param job | ||
* @param message | ||
*/ | ||
async function onMessage(context, job, type, payload) { | ||
switch (type) { | ||
case 'done': | ||
job.done(payload); | ||
break; | ||
case 'error': | ||
job.error(new Error(payload.error)); | ||
break; | ||
case 'process': | ||
const { | ||
id, | ||
input, | ||
options | ||
} = payload; | ||
try { | ||
if (!context.process) { | ||
job.postMessage('error', { | ||
id, | ||
error: 'Worker not set up to process on main thread' | ||
}); | ||
return; | ||
} | ||
const result = await context.process(input, options); | ||
job.postMessage('done', { | ||
id, | ||
result | ||
}); | ||
} catch (error) { | ||
const message = error instanceof Error ? error.message : 'unknown error'; | ||
job.postMessage('error', { | ||
id, | ||
error: message | ||
}); | ||
} | ||
break; | ||
default: | ||
console.warn(`process-on-worker: unknown message ${type}`); | ||
} | ||
switch (type) { | ||
case 'done': | ||
// Worker is done | ||
job.done(payload); | ||
break; | ||
case 'error': | ||
// Worker encountered an error | ||
job.error(new Error(payload.error)); | ||
break; | ||
case 'process': | ||
// Worker is asking for us (main thread) to process something | ||
const { id, input, options } = payload; | ||
try { | ||
if (!context.process) { | ||
job.postMessage('error', { id, error: 'Worker not set up to process on main thread' }); | ||
return; | ||
} | ||
const result = await context.process(input, options); | ||
job.postMessage('done', { id, result }); | ||
} | ||
catch (error) { | ||
const message = error instanceof Error ? error.message : 'unknown error'; | ||
job.postMessage('error', { id, error: message }); | ||
} | ||
break; | ||
default: | ||
// eslint-disable-next-line | ||
console.warn(`process-on-worker: unknown message ${type}`); | ||
} | ||
} | ||
//# sourceMappingURL=process-on-worker.js.map |
@@ -1,2 +0,2 @@ | ||
import type { WorkerObject } from '../../types'; | ||
import type { WorkerObject } from "../../types.js"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Check if worker is compatible with this library version |
@@ -0,19 +1,34 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { assert } from "../env-utils/assert.js"; | ||
import { VERSION } from "../env-utils/version.js"; | ||
export function validateWorkerVersion(worker) { | ||
let coreVersion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : VERSION; | ||
assert(worker, 'no worker provided'); | ||
const workerVersion = worker.version; | ||
if (!coreVersion || !workerVersion) { | ||
return false; | ||
} | ||
return true; | ||
/** | ||
* Check if worker is compatible with this library version | ||
* @param worker | ||
* @param libVersion | ||
* @returns `true` if the two versions are compatible | ||
*/ | ||
export function validateWorkerVersion(worker, coreVersion = VERSION) { | ||
assert(worker, 'no worker provided'); | ||
const workerVersion = worker.version; | ||
if (!coreVersion || !workerVersion) { | ||
return false; | ||
} | ||
// TODO enable when fix the __version__ injection | ||
// const coreVersions = parseVersion(coreVersion); | ||
// const workerVersions = parseVersion(workerVersion); | ||
// assert( | ||
// coreVersion.major === workerVersion.major && coreVersion.minor <= workerVersion.minor, | ||
// `worker: ${worker.name} is not compatible. ${coreVersion.major}.${ | ||
// coreVersion.minor | ||
// }+ is required.` | ||
// ); | ||
return true; | ||
} | ||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
function parseVersion(version) { | ||
const parts = version.split('.').map(Number); | ||
return { | ||
major: parts[0], | ||
minor: parts[1] | ||
}; | ||
const parts = version.split('.').map(Number); | ||
return { major: parts[0], minor: parts[1] }; | ||
} | ||
//# sourceMappingURL=validate-worker-version.js.map |
@@ -1,2 +0,2 @@ | ||
import type { WorkerMessageType, WorkerMessagePayload } from '../../types'; | ||
import type { WorkerMessageType, WorkerMessagePayload } from "../../types.js"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Type safe wrapper for worker code |
@@ -0,83 +1,121 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { getTransferList } from "../worker-utils/get-transfer-list.js"; | ||
import { parentPort } from "../node/worker_threads.js"; | ||
/** Vile hack to defeat over-zealous bundlers from stripping out the require */ | ||
async function getParentPort() { | ||
return parentPort; | ||
// const isNode = globalThis.process; | ||
// let parentPort; | ||
// try { | ||
// // prettier-ignore | ||
// eval('globalThis.parentPort = require(\'worker_threads\').parentPort'); // eslint-disable-line no-eval | ||
// parentPort = globalThis.parentPort; | ||
// } catch { | ||
// try { | ||
// // prettier-ignore | ||
// eval('globalThis.workerThreadsPromise = import(\'worker_threads\')'); // eslint-disable-line no-eval | ||
// const workerThreads = await globalThis.workerThreadsPromise; | ||
// parentPort = workerThreads.parentPort; | ||
// } catch (error) { | ||
// console.error((error as Error).message); // eslint-disable-line no-console | ||
// } | ||
// } | ||
return parentPort; | ||
} | ||
const onMessageWrapperMap = new Map(); | ||
/** | ||
* Type safe wrapper for worker code | ||
*/ | ||
export default class WorkerBody { | ||
static async inWorkerThread() { | ||
return typeof self !== 'undefined' || Boolean(await getParentPort()); | ||
} | ||
static set onmessage(onMessage) { | ||
async function handleMessage(message) { | ||
const parentPort = await getParentPort(); | ||
const { | ||
type, | ||
payload | ||
} = parentPort ? message : message.data; | ||
onMessage(type, payload); | ||
/** Check that we are actually in a worker thread */ | ||
static async inWorkerThread() { | ||
return typeof self !== 'undefined' || Boolean(await getParentPort()); | ||
} | ||
getParentPort().then(parentPort => { | ||
if (parentPort) { | ||
parentPort.on('message', handleMessage); | ||
parentPort.on('exit', () => console.debug('Node worker closing')); | ||
} else { | ||
globalThis.onmessage = handleMessage; | ||
} | ||
}); | ||
} | ||
static async addEventListener(onMessage) { | ||
let onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
if (!onMessageWrapper) { | ||
onMessageWrapper = async message => { | ||
if (!isKnownMessage(message)) { | ||
return; | ||
/* | ||
* (type: WorkerMessageType, payload: WorkerMessagePayload) => any | ||
*/ | ||
static set onmessage(onMessage) { | ||
async function handleMessage(message) { | ||
const parentPort = await getParentPort(); | ||
// Confusingly the message itself also has a 'type' field which is always set to 'message' | ||
const { type, payload } = parentPort ? message : message.data; | ||
// if (!isKnownMessage(message)) { | ||
// return; | ||
// } | ||
onMessage(type, payload); | ||
} | ||
getParentPort().then((parentPort) => { | ||
if (parentPort) { | ||
parentPort.on('message', handleMessage); | ||
// if (message == 'exit') { parentPort.unref(); } | ||
// eslint-disable-next-line | ||
parentPort.on('exit', () => console.debug('Node worker closing')); | ||
} | ||
else { | ||
// eslint-disable-next-line no-restricted-globals | ||
globalThis.onmessage = handleMessage; | ||
} | ||
}); | ||
} | ||
static async addEventListener(onMessage) { | ||
let onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
if (!onMessageWrapper) { | ||
onMessageWrapper = async (message) => { | ||
if (!isKnownMessage(message)) { | ||
return; | ||
} | ||
const parentPort = await getParentPort(); | ||
// Confusingly in the browser, the message itself also has a 'type' field which is always set to 'message' | ||
const { type, payload } = parentPort ? message : message.data; | ||
onMessage(type, payload); | ||
}; | ||
} | ||
const parentPort = await getParentPort(); | ||
const { | ||
type, | ||
payload | ||
} = parentPort ? message : message.data; | ||
onMessage(type, payload); | ||
}; | ||
if (parentPort) { | ||
console.error('not implemented'); // eslint-disable-line | ||
} | ||
else { | ||
globalThis.addEventListener('message', onMessageWrapper); | ||
} | ||
} | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
console.error('not implemented'); | ||
} else { | ||
globalThis.addEventListener('message', onMessageWrapper); | ||
static async removeEventListener(onMessage) { | ||
const onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
onMessageWrapperMap.delete(onMessage); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
console.error('not implemented'); // eslint-disable-line | ||
} | ||
else { | ||
globalThis.removeEventListener('message', onMessageWrapper); | ||
} | ||
} | ||
} | ||
static async removeEventListener(onMessage) { | ||
const onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
onMessageWrapperMap.delete(onMessage); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
console.error('not implemented'); | ||
} else { | ||
globalThis.removeEventListener('message', onMessageWrapper); | ||
/** | ||
* Send a message from a worker to creating thread (main thread) | ||
* @param type | ||
* @param payload | ||
*/ | ||
static async postMessage(type, payload) { | ||
const data = { source: 'loaders.gl', type, payload }; | ||
// console.log('posting message', data); | ||
// Cast to Node compatible transfer list | ||
const transferList = getTransferList(payload); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
parentPort.postMessage(data, transferList); | ||
// console.log('posted message', data); | ||
} | ||
else { | ||
// @ts-expect-error Outside of worker scopes this call has a third parameter | ||
globalThis.postMessage(data, transferList); | ||
} | ||
} | ||
} | ||
static async postMessage(type, payload) { | ||
const data = { | ||
source: 'loaders.gl', | ||
type, | ||
payload | ||
}; | ||
const transferList = getTransferList(payload); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
parentPort.postMessage(data, transferList); | ||
} else { | ||
globalThis.postMessage(data, transferList); | ||
} | ||
} | ||
} | ||
// Filter out noise messages sent to workers | ||
function isKnownMessage(message) { | ||
const { | ||
type, | ||
data | ||
} = message; | ||
return type === 'message' && data && typeof data.source === 'string' && data.source.startsWith('loaders.gl'); | ||
const { type, data } = message; | ||
return (type === 'message' && | ||
data && | ||
typeof data.source === 'string' && | ||
data.source.startsWith('loaders.gl')); | ||
} | ||
//# sourceMappingURL=worker-body.js.map |
@@ -1,2 +0,2 @@ | ||
import WorkerPool from './worker-pool'; | ||
import WorkerPool from "./worker-pool.js"; | ||
/** | ||
@@ -3,0 +3,0 @@ * @param maxConcurrency - max count of workers |
@@ -0,71 +1,86 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import WorkerPool from "./worker-pool.js"; | ||
import WorkerThread from "./worker-thread.js"; | ||
const DEFAULT_PROPS = { | ||
maxConcurrency: 3, | ||
maxMobileConcurrency: 1, | ||
reuseWorkers: true, | ||
onDebug: () => {} | ||
maxConcurrency: 3, | ||
maxMobileConcurrency: 1, | ||
reuseWorkers: true, | ||
onDebug: () => { } | ||
}; | ||
/** | ||
* Process multiple jobs with a "farm" of different workers in worker pools. | ||
*/ | ||
export default class WorkerFarm { | ||
static isSupported() { | ||
return WorkerThread.isSupported(); | ||
} | ||
static getWorkerFarm() { | ||
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({}); | ||
WorkerFarm._workerFarm.setProps(props); | ||
return WorkerFarm._workerFarm; | ||
} | ||
constructor(props) { | ||
this.props = void 0; | ||
this.workerPools = new Map(); | ||
this.props = { | ||
...DEFAULT_PROPS | ||
}; | ||
this.setProps(props); | ||
this.workerPools = new Map(); | ||
} | ||
destroy() { | ||
for (const workerPool of this.workerPools.values()) { | ||
workerPool.destroy(); | ||
/** Checks if workers are supported on this platform */ | ||
static isSupported() { | ||
return WorkerThread.isSupported(); | ||
} | ||
this.workerPools = new Map(); | ||
} | ||
setProps(props) { | ||
this.props = { | ||
...this.props, | ||
...props | ||
}; | ||
for (const workerPool of this.workerPools.values()) { | ||
workerPool.setProps(this._getWorkerPoolProps()); | ||
/** Get the singleton instance of the global worker farm */ | ||
static getWorkerFarm(props = {}) { | ||
WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({}); | ||
WorkerFarm._workerFarm.setProps(props); | ||
return WorkerFarm._workerFarm; | ||
} | ||
} | ||
getWorkerPool(options) { | ||
const { | ||
name, | ||
source, | ||
url | ||
} = options; | ||
let workerPool = this.workerPools.get(name); | ||
if (!workerPool) { | ||
workerPool = new WorkerPool({ | ||
name, | ||
source, | ||
url | ||
}); | ||
workerPool.setProps(this._getWorkerPoolProps()); | ||
this.workerPools.set(name, workerPool); | ||
/** get global instance with WorkerFarm.getWorkerFarm() */ | ||
constructor(props) { | ||
this.workerPools = new Map(); | ||
this.props = { ...DEFAULT_PROPS }; | ||
this.setProps(props); | ||
/** @type Map<string, WorkerPool>} */ | ||
this.workerPools = new Map(); | ||
} | ||
return workerPool; | ||
} | ||
_getWorkerPoolProps() { | ||
return { | ||
maxConcurrency: this.props.maxConcurrency, | ||
maxMobileConcurrency: this.props.maxMobileConcurrency, | ||
reuseWorkers: this.props.reuseWorkers, | ||
onDebug: this.props.onDebug | ||
}; | ||
} | ||
/** | ||
* Terminate all workers in the farm | ||
* @note Can free up significant memory | ||
*/ | ||
destroy() { | ||
for (const workerPool of this.workerPools.values()) { | ||
workerPool.destroy(); | ||
} | ||
this.workerPools = new Map(); | ||
} | ||
/** | ||
* Set props used when initializing worker pools | ||
* @param props | ||
*/ | ||
setProps(props) { | ||
this.props = { ...this.props, ...props }; | ||
// Update worker pool props | ||
for (const workerPool of this.workerPools.values()) { | ||
workerPool.setProps(this._getWorkerPoolProps()); | ||
} | ||
} | ||
/** | ||
* Returns a worker pool for the specified worker | ||
* @param options - only used first time for a specific worker name | ||
* @param options.name - the name of the worker - used to identify worker pool | ||
* @param options.url - | ||
* @param options.source - | ||
* @example | ||
* const job = WorkerFarm.getWorkerFarm().getWorkerPool({name, url}).startJob(...); | ||
*/ | ||
getWorkerPool(options) { | ||
const { name, source, url } = options; | ||
let workerPool = this.workerPools.get(name); | ||
if (!workerPool) { | ||
workerPool = new WorkerPool({ | ||
name, | ||
source, | ||
url | ||
}); | ||
workerPool.setProps(this._getWorkerPoolProps()); | ||
this.workerPools.set(name, workerPool); | ||
} | ||
return workerPool; | ||
} | ||
_getWorkerPoolProps() { | ||
return { | ||
maxConcurrency: this.props.maxConcurrency, | ||
maxMobileConcurrency: this.props.maxMobileConcurrency, | ||
reuseWorkers: this.props.reuseWorkers, | ||
onDebug: this.props.onDebug | ||
}; | ||
} | ||
} | ||
WorkerFarm._workerFarm = void 0; | ||
//# sourceMappingURL=worker-farm.js.map |
@@ -1,3 +0,3 @@ | ||
import type { WorkerMessageType, WorkerMessagePayload } from '../../types'; | ||
import WorkerThread from './worker-thread'; | ||
import type { WorkerMessageType, WorkerMessagePayload } from "../../types.js"; | ||
import WorkerThread from "./worker-thread.js"; | ||
/** | ||
@@ -4,0 +4,0 @@ * Represents one Job handled by a WorkerPool or WorkerFarm |
@@ -0,35 +1,47 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { assert } from "../env-utils/assert.js"; | ||
/** | ||
* Represents one Job handled by a WorkerPool or WorkerFarm | ||
*/ | ||
export default class WorkerJob { | ||
constructor(jobName, workerThread) { | ||
this.name = void 0; | ||
this.workerThread = void 0; | ||
this.isRunning = true; | ||
this.result = void 0; | ||
this._resolve = () => {}; | ||
this._reject = () => {}; | ||
this.name = jobName; | ||
this.workerThread = workerThread; | ||
this.result = new Promise((resolve, reject) => { | ||
this._resolve = resolve; | ||
this._reject = reject; | ||
}); | ||
} | ||
postMessage(type, payload) { | ||
this.workerThread.postMessage({ | ||
source: 'loaders.gl', | ||
type, | ||
payload | ||
}); | ||
} | ||
done(value) { | ||
assert(this.isRunning); | ||
this.isRunning = false; | ||
this._resolve(value); | ||
} | ||
error(error) { | ||
assert(this.isRunning); | ||
this.isRunning = false; | ||
this._reject(error); | ||
} | ||
constructor(jobName, workerThread) { | ||
this.isRunning = true; | ||
this._resolve = () => { }; | ||
this._reject = () => { }; | ||
this.name = jobName; | ||
this.workerThread = workerThread; | ||
this.result = new Promise((resolve, reject) => { | ||
this._resolve = resolve; | ||
this._reject = reject; | ||
}); | ||
} | ||
/** | ||
* Send a message to the job's worker thread | ||
* @param data any data structure, ideally consisting mostly of transferrable objects | ||
*/ | ||
postMessage(type, payload) { | ||
this.workerThread.postMessage({ | ||
source: 'loaders.gl', // Lets worker ignore unrelated messages | ||
type, | ||
payload | ||
}); | ||
} | ||
/** | ||
* Call to resolve the `result` Promise with the supplied value | ||
*/ | ||
done(value) { | ||
assert(this.isRunning); | ||
this.isRunning = false; | ||
this._resolve(value); | ||
} | ||
/** | ||
* Call to reject the `result` Promise with the supplied error | ||
*/ | ||
error(error) { | ||
assert(this.isRunning); | ||
this.isRunning = false; | ||
this._reject(error); | ||
} | ||
} | ||
//# sourceMappingURL=worker-job.js.map |
@@ -1,4 +0,4 @@ | ||
import type { WorkerMessageType, WorkerMessagePayload } from '../../types'; | ||
import WorkerThread from './worker-thread'; | ||
import WorkerJob from './worker-job'; | ||
import type { WorkerMessageType, WorkerMessagePayload } from "../../types.js"; | ||
import WorkerThread from "./worker-thread.js"; | ||
import WorkerJob from "./worker-job.js"; | ||
/** WorkerPool onDebug Callback Parameters */ | ||
@@ -5,0 +5,0 @@ type OnDebugParameters = { |
@@ -0,125 +1,165 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { isMobile, isBrowser } from "../env-utils/globals.js"; | ||
import WorkerThread from "./worker-thread.js"; | ||
import WorkerJob from "./worker-job.js"; | ||
/** | ||
* Process multiple data messages with small pool of identical workers | ||
*/ | ||
export default class WorkerPool { | ||
static isSupported() { | ||
return WorkerThread.isSupported(); | ||
} | ||
constructor(props) { | ||
this.name = 'unnamed'; | ||
this.source = void 0; | ||
this.url = void 0; | ||
this.maxConcurrency = 1; | ||
this.maxMobileConcurrency = 1; | ||
this.onDebug = () => {}; | ||
this.reuseWorkers = true; | ||
this.props = {}; | ||
this.jobQueue = []; | ||
this.idleQueue = []; | ||
this.count = 0; | ||
this.isDestroyed = false; | ||
this.source = props.source; | ||
this.url = props.url; | ||
this.setProps(props); | ||
} | ||
destroy() { | ||
this.idleQueue.forEach(worker => worker.destroy()); | ||
this.isDestroyed = true; | ||
} | ||
setProps(props) { | ||
this.props = { | ||
...this.props, | ||
...props | ||
}; | ||
if (props.name !== undefined) { | ||
this.name = props.name; | ||
/** Checks if workers are supported on this platform */ | ||
static isSupported() { | ||
return WorkerThread.isSupported(); | ||
} | ||
if (props.maxConcurrency !== undefined) { | ||
this.maxConcurrency = props.maxConcurrency; | ||
/** | ||
* @param processor - worker function | ||
* @param maxConcurrency - max count of workers | ||
*/ | ||
constructor(props) { | ||
this.name = 'unnamed'; | ||
this.maxConcurrency = 1; | ||
this.maxMobileConcurrency = 1; | ||
this.onDebug = () => { }; | ||
this.reuseWorkers = true; | ||
this.props = {}; | ||
this.jobQueue = []; | ||
this.idleQueue = []; | ||
this.count = 0; | ||
this.isDestroyed = false; | ||
this.source = props.source; | ||
this.url = props.url; | ||
this.setProps(props); | ||
} | ||
if (props.maxMobileConcurrency !== undefined) { | ||
this.maxMobileConcurrency = props.maxMobileConcurrency; | ||
/** | ||
* Terminates all workers in the pool | ||
* @note Can free up significant memory | ||
*/ | ||
destroy() { | ||
// Destroy idle workers, active Workers will be destroyed on completion | ||
this.idleQueue.forEach((worker) => worker.destroy()); | ||
this.isDestroyed = true; | ||
} | ||
if (props.reuseWorkers !== undefined) { | ||
this.reuseWorkers = props.reuseWorkers; | ||
setProps(props) { | ||
this.props = { ...this.props, ...props }; | ||
if (props.name !== undefined) { | ||
this.name = props.name; | ||
} | ||
if (props.maxConcurrency !== undefined) { | ||
this.maxConcurrency = props.maxConcurrency; | ||
} | ||
if (props.maxMobileConcurrency !== undefined) { | ||
this.maxMobileConcurrency = props.maxMobileConcurrency; | ||
} | ||
if (props.reuseWorkers !== undefined) { | ||
this.reuseWorkers = props.reuseWorkers; | ||
} | ||
if (props.onDebug !== undefined) { | ||
this.onDebug = props.onDebug; | ||
} | ||
} | ||
if (props.onDebug !== undefined) { | ||
this.onDebug = props.onDebug; | ||
async startJob(name, onMessage = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) { | ||
// Promise resolves when thread starts working on this job | ||
const startPromise = new Promise((onStart) => { | ||
// Promise resolves when thread completes or fails working on this job | ||
this.jobQueue.push({ name, onMessage, onError, onStart }); | ||
return this; | ||
}); | ||
this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises | ||
return await startPromise; | ||
} | ||
} | ||
async startJob(name) { | ||
let onMessage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (job, type, data) => job.done(data); | ||
let onError = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : (job, error) => job.error(error); | ||
const startPromise = new Promise(onStart => { | ||
this.jobQueue.push({ | ||
name, | ||
onMessage, | ||
onError, | ||
onStart | ||
}); | ||
return this; | ||
}); | ||
this._startQueuedJob(); | ||
return await startPromise; | ||
} | ||
async _startQueuedJob() { | ||
if (!this.jobQueue.length) { | ||
return; | ||
// PRIVATE | ||
/** | ||
* Starts first queued job if worker is available or can be created | ||
* Called when job is started and whenever a worker returns to the idleQueue | ||
*/ | ||
async _startQueuedJob() { | ||
if (!this.jobQueue.length) { | ||
return; | ||
} | ||
const workerThread = this._getAvailableWorker(); | ||
if (!workerThread) { | ||
return; | ||
} | ||
// We have a worker, dequeue and start the job | ||
const queuedJob = this.jobQueue.shift(); | ||
if (queuedJob) { | ||
// Emit a debug event | ||
// @ts-ignore | ||
this.onDebug({ | ||
message: 'Starting job', | ||
name: queuedJob.name, | ||
workerThread, | ||
backlog: this.jobQueue.length | ||
}); | ||
// Create a worker job to let the app access thread and manage job completion | ||
const job = new WorkerJob(queuedJob.name, workerThread); | ||
// Set the worker thread's message handlers | ||
workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload); | ||
workerThread.onError = (error) => queuedJob.onError(job, error); | ||
// Resolve the start promise so that the app can start sending messages to worker | ||
queuedJob.onStart(job); | ||
// Wait for the app to signal that the job is complete, then return worker to queue | ||
try { | ||
await job.result; | ||
} | ||
catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Worker exception: ${error}`); | ||
} | ||
finally { | ||
this.returnWorkerToQueue(workerThread); | ||
} | ||
} | ||
} | ||
const workerThread = this._getAvailableWorker(); | ||
if (!workerThread) { | ||
return; | ||
/** | ||
* Returns a worker to the idle queue | ||
* Destroys the worker if | ||
* - pool is destroyed | ||
* - if this pool doesn't reuse workers | ||
* - if maxConcurrency has been lowered | ||
* @param worker | ||
*/ | ||
returnWorkerToQueue(worker) { | ||
const shouldDestroyWorker = | ||
// Workers on Node.js prevent the process from exiting. | ||
// Until we figure out how to close them before exit, we always destroy them | ||
!isBrowser || | ||
// If the pool is destroyed, there is no reason to keep the worker around | ||
this.isDestroyed || | ||
// If the app has disabled worker reuse, any completed workers should be destroyed | ||
!this.reuseWorkers || | ||
// If concurrency has been lowered, this worker might be surplus to requirements | ||
this.count > this._getMaxConcurrency(); | ||
if (shouldDestroyWorker) { | ||
worker.destroy(); | ||
this.count--; | ||
} | ||
else { | ||
this.idleQueue.push(worker); | ||
} | ||
if (!this.isDestroyed) { | ||
this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises | ||
} | ||
} | ||
const queuedJob = this.jobQueue.shift(); | ||
if (queuedJob) { | ||
this.onDebug({ | ||
message: 'Starting job', | ||
name: queuedJob.name, | ||
workerThread, | ||
backlog: this.jobQueue.length | ||
}); | ||
const job = new WorkerJob(queuedJob.name, workerThread); | ||
workerThread.onMessage = data => queuedJob.onMessage(job, data.type, data.payload); | ||
workerThread.onError = error => queuedJob.onError(job, error); | ||
queuedJob.onStart(job); | ||
try { | ||
await job.result; | ||
} catch (error) { | ||
console.error(`Worker exception: ${error}`); | ||
} finally { | ||
this.returnWorkerToQueue(workerThread); | ||
} | ||
/** | ||
* Returns idle worker or creates new worker if maxConcurrency has not been reached | ||
*/ | ||
_getAvailableWorker() { | ||
// If a worker has completed and returned to the queue, it can be used | ||
if (this.idleQueue.length > 0) { | ||
return this.idleQueue.shift() || null; | ||
} | ||
// Create fresh worker if we haven't yet created the max amount of worker threads for this worker source | ||
if (this.count < this._getMaxConcurrency()) { | ||
this.count++; | ||
const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`; | ||
return new WorkerThread({ name, source: this.source, url: this.url }); | ||
} | ||
// No worker available, have to wait | ||
return null; | ||
} | ||
} | ||
returnWorkerToQueue(worker) { | ||
const shouldDestroyWorker = !isBrowser || this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency(); | ||
if (shouldDestroyWorker) { | ||
worker.destroy(); | ||
this.count--; | ||
} else { | ||
this.idleQueue.push(worker); | ||
_getMaxConcurrency() { | ||
return isMobile ? this.maxMobileConcurrency : this.maxConcurrency; | ||
} | ||
if (!this.isDestroyed) { | ||
this._startQueuedJob(); | ||
} | ||
} | ||
_getAvailableWorker() { | ||
if (this.idleQueue.length > 0) { | ||
return this.idleQueue.shift() || null; | ||
} | ||
if (this.count < this._getMaxConcurrency()) { | ||
this.count++; | ||
const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`; | ||
return new WorkerThread({ | ||
name, | ||
source: this.source, | ||
url: this.url | ||
}); | ||
} | ||
return null; | ||
} | ||
_getMaxConcurrency() { | ||
return isMobile ? this.maxMobileConcurrency : this.maxConcurrency; | ||
} | ||
} | ||
//# sourceMappingURL=worker-pool.js.map |
@@ -1,2 +0,2 @@ | ||
import { NodeWorkerType } from '../node/worker_threads'; | ||
import { NodeWorkerType } from "../node/worker_threads.js"; | ||
export type WorkerThreadProps = { | ||
@@ -3,0 +3,0 @@ name: string; |
@@ -0,1 +1,4 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { NodeWorker } from "../node/worker_threads.js"; | ||
@@ -6,100 +9,123 @@ import { isBrowser } from "../env-utils/globals.js"; | ||
import { getTransferList } from "../worker-utils/get-transfer-list.js"; | ||
const NOOP = () => {}; | ||
const NOOP = () => { }; | ||
/** | ||
* Represents one worker thread | ||
*/ | ||
export default class WorkerThread { | ||
static isSupported() { | ||
return typeof Worker !== 'undefined' && isBrowser || typeof NodeWorker !== 'undefined' && !isBrowser; | ||
} | ||
constructor(props) { | ||
this.name = void 0; | ||
this.source = void 0; | ||
this.url = void 0; | ||
this.terminated = false; | ||
this.worker = void 0; | ||
this.onMessage = void 0; | ||
this.onError = void 0; | ||
this._loadableURL = ''; | ||
const { | ||
name, | ||
source, | ||
url | ||
} = props; | ||
assert(source || url); | ||
this.name = name; | ||
this.source = source; | ||
this.url = url; | ||
this.onMessage = NOOP; | ||
this.onError = error => console.log(error); | ||
this.worker = isBrowser ? this._createBrowserWorker() : this._createNodeWorker(); | ||
} | ||
destroy() { | ||
this.onMessage = NOOP; | ||
this.onError = NOOP; | ||
this.worker.terminate(); | ||
this.terminated = true; | ||
} | ||
get isRunning() { | ||
return Boolean(this.onMessage); | ||
} | ||
postMessage(data, transferList) { | ||
transferList = transferList || getTransferList(data); | ||
this.worker.postMessage(data, transferList); | ||
} | ||
_getErrorFromErrorEvent(event) { | ||
let message = 'Failed to load '; | ||
message += `worker ${this.name} from ${this.url}. `; | ||
if (event.message) { | ||
message += `${event.message} in `; | ||
/** Checks if workers are supported on this platform */ | ||
static isSupported() { | ||
return ((typeof Worker !== 'undefined' && isBrowser) || | ||
(typeof NodeWorker !== 'undefined' && !isBrowser)); | ||
} | ||
if (event.lineno) { | ||
message += `:${event.lineno}:${event.colno}`; | ||
constructor(props) { | ||
this.terminated = false; | ||
this._loadableURL = ''; | ||
const { name, source, url } = props; | ||
assert(source || url); // Either source or url must be defined | ||
this.name = name; | ||
this.source = source; | ||
this.url = url; | ||
this.onMessage = NOOP; | ||
this.onError = (error) => console.log(error); // eslint-disable-line | ||
this.worker = isBrowser ? this._createBrowserWorker() : this._createNodeWorker(); | ||
} | ||
return new Error(message); | ||
} | ||
_createBrowserWorker() { | ||
this._loadableURL = getLoadableWorkerURL({ | ||
source: this.source, | ||
url: this.url | ||
}); | ||
const worker = new Worker(this._loadableURL, { | ||
name: this.name | ||
}); | ||
worker.onmessage = event => { | ||
if (!event.data) { | ||
this.onError(new Error('No data received')); | ||
} else { | ||
this.onMessage(event.data); | ||
} | ||
}; | ||
worker.onerror = error => { | ||
this.onError(this._getErrorFromErrorEvent(error)); | ||
this.terminated = true; | ||
}; | ||
worker.onmessageerror = event => console.error(event); | ||
return worker; | ||
} | ||
_createNodeWorker() { | ||
let worker; | ||
if (this.url) { | ||
const absolute = this.url.includes(':/') || this.url.startsWith('/'); | ||
const url = absolute ? this.url : `./${this.url}`; | ||
worker = new NodeWorker(url, { | ||
eval: false | ||
}); | ||
} else if (this.source) { | ||
worker = new NodeWorker(this.source, { | ||
eval: true | ||
}); | ||
} else { | ||
throw new Error('no worker'); | ||
/** | ||
* Terminate this worker thread | ||
* @note Can free up significant memory | ||
*/ | ||
destroy() { | ||
this.onMessage = NOOP; | ||
this.onError = NOOP; | ||
this.worker.terminate(); // eslint-disable-line @typescript-eslint/no-floating-promises | ||
this.terminated = true; | ||
} | ||
worker.on('message', data => { | ||
this.onMessage(data); | ||
}); | ||
worker.on('error', error => { | ||
this.onError(error); | ||
}); | ||
worker.on('exit', code => {}); | ||
return worker; | ||
} | ||
get isRunning() { | ||
return Boolean(this.onMessage); | ||
} | ||
/** | ||
* Send a message to this worker thread | ||
* @param data any data structure, ideally consisting mostly of transferrable objects | ||
* @param transferList If not supplied, calculated automatically by traversing data | ||
*/ | ||
postMessage(data, transferList) { | ||
transferList = transferList || getTransferList(data); | ||
// @ts-ignore | ||
this.worker.postMessage(data, transferList); | ||
} | ||
// PRIVATE | ||
/** | ||
* Generate a standard Error from an ErrorEvent | ||
* @param event | ||
*/ | ||
_getErrorFromErrorEvent(event) { | ||
// Note Error object does not have the expected fields if loading failed completely | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Worker#Event_handlers | ||
// https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent | ||
let message = 'Failed to load '; | ||
message += `worker ${this.name} from ${this.url}. `; | ||
if (event.message) { | ||
message += `${event.message} in `; | ||
} | ||
// const hasFilename = event.filename && !event.filename.startsWith('blob:'); | ||
// message += hasFilename ? event.filename : this.source.slice(0, 100); | ||
if (event.lineno) { | ||
message += `:${event.lineno}:${event.colno}`; | ||
} | ||
return new Error(message); | ||
} | ||
/** | ||
* Creates a worker thread on the browser | ||
*/ | ||
_createBrowserWorker() { | ||
this._loadableURL = getLoadableWorkerURL({ source: this.source, url: this.url }); | ||
const worker = new Worker(this._loadableURL, { name: this.name }); | ||
worker.onmessage = (event) => { | ||
if (!event.data) { | ||
this.onError(new Error('No data received')); | ||
} | ||
else { | ||
this.onMessage(event.data); | ||
} | ||
}; | ||
// This callback represents an uncaught exception in the worker thread | ||
worker.onerror = (error) => { | ||
this.onError(this._getErrorFromErrorEvent(error)); | ||
this.terminated = true; | ||
}; | ||
// TODO - not clear when this would be called, for now just log in case it happens | ||
worker.onmessageerror = (event) => console.error(event); // eslint-disable-line | ||
return worker; | ||
} | ||
/** | ||
* Creates a worker thread in node.js | ||
* @todo https://nodejs.org/api/async_hooks.html#async-resource-worker-pool | ||
*/ | ||
_createNodeWorker() { | ||
let worker; | ||
if (this.url) { | ||
// Make sure relative URLs start with './' | ||
const absolute = this.url.includes(':/') || this.url.startsWith('/'); | ||
const url = absolute ? this.url : `./${this.url}`; | ||
// console.log('Starting work from', url); | ||
worker = new NodeWorker(url, { eval: false }); | ||
} | ||
else if (this.source) { | ||
worker = new NodeWorker(this.source, { eval: true }); | ||
} | ||
else { | ||
throw new Error('no worker'); | ||
} | ||
worker.on('message', (data) => { | ||
// console.error('message', data); | ||
this.onMessage(data); | ||
}); | ||
worker.on('error', (error) => { | ||
// console.error('error', error); | ||
this.onError(error); | ||
}); | ||
worker.on('exit', (code) => { | ||
// console.error('exit', code); | ||
}); | ||
return worker; | ||
} | ||
} | ||
//# sourceMappingURL=worker-thread.js.map |
@@ -0,34 +1,65 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { assert } from "../env-utils/assert.js"; | ||
const workerURLCache = new Map(); | ||
/** | ||
* Creates a loadable URL from worker source or URL | ||
* that can be used to create `Worker` instances. | ||
* Due to CORS issues it may be necessary to wrap a URL in a small importScripts | ||
* @param props | ||
* @param props.source Worker source | ||
* @param props.url Worker URL | ||
* @returns loadable url | ||
*/ | ||
export function getLoadableWorkerURL(props) { | ||
assert(props.source && !props.url || !props.source && props.url); | ||
let workerURL = workerURLCache.get(props.source || props.url); | ||
if (!workerURL) { | ||
if (props.url) { | ||
workerURL = getLoadableWorkerURLFromURL(props.url); | ||
workerURLCache.set(props.url, workerURL); | ||
assert((props.source && !props.url) || (!props.source && props.url)); // Either source or url must be defined | ||
let workerURL = workerURLCache.get(props.source || props.url); | ||
if (!workerURL) { | ||
// Differentiate worker urls from worker source code | ||
if (props.url) { | ||
workerURL = getLoadableWorkerURLFromURL(props.url); | ||
workerURLCache.set(props.url, workerURL); | ||
} | ||
if (props.source) { | ||
workerURL = getLoadableWorkerURLFromSource(props.source); | ||
workerURLCache.set(props.source, workerURL); | ||
} | ||
} | ||
if (props.source) { | ||
workerURL = getLoadableWorkerURLFromSource(props.source); | ||
workerURLCache.set(props.source, workerURL); | ||
} | ||
} | ||
assert(workerURL); | ||
return workerURL; | ||
assert(workerURL); | ||
return workerURL; | ||
} | ||
/** | ||
* Build a loadable worker URL from worker URL | ||
* @param url | ||
* @returns loadable URL | ||
*/ | ||
function getLoadableWorkerURLFromURL(url) { | ||
if (!url.startsWith('http')) { | ||
return url; | ||
} | ||
const workerSource = buildScriptSource(url); | ||
return getLoadableWorkerURLFromSource(workerSource); | ||
// A local script url, we can use it to initialize a Worker directly | ||
if (!url.startsWith('http')) { | ||
return url; | ||
} | ||
// A remote script, we need to use `importScripts` to load from different origin | ||
const workerSource = buildScriptSource(url); | ||
return getLoadableWorkerURLFromSource(workerSource); | ||
} | ||
/** | ||
* Build a loadable worker URL from worker source | ||
* @param workerSource | ||
* @returns loadable url | ||
*/ | ||
function getLoadableWorkerURLFromSource(workerSource) { | ||
const blob = new Blob([workerSource], { | ||
type: 'application/javascript' | ||
}); | ||
return URL.createObjectURL(blob); | ||
const blob = new Blob([workerSource], { type: 'application/javascript' }); | ||
return URL.createObjectURL(blob); | ||
} | ||
/** | ||
* Per spec, worker cannot be initialized with a script from a different origin | ||
* However a local worker script can still import scripts from other origins, | ||
* so we simply build a wrapper script. | ||
* | ||
* @param workerUrl | ||
* @returns source | ||
*/ | ||
function buildScriptSource(workerUrl) { | ||
return `\ | ||
return `\ | ||
try { | ||
@@ -41,2 +72,1 @@ importScripts('${workerUrl}'); | ||
} | ||
//# sourceMappingURL=get-loadable-worker-url.js.map |
@@ -1,50 +0,85 @@ | ||
export function getTransferList(object) { | ||
let recursive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
let transfers = arguments.length > 2 ? arguments[2] : undefined; | ||
const transfersSet = transfers || new Set(); | ||
if (!object) {} else if (isTransferable(object)) { | ||
transfersSet.add(object); | ||
} else if (isTransferable(object.buffer)) { | ||
transfersSet.add(object.buffer); | ||
} else if (ArrayBuffer.isView(object)) {} else if (recursive && typeof object === 'object') { | ||
for (const key in object) { | ||
getTransferList(object[key], recursive, transfersSet); | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
// NOTE - there is a copy of this function is both in core and loader-utils | ||
// core does not need all the utils in loader-utils, just this one. | ||
/** | ||
* Returns an array of Transferrable objects that can be used with postMessage | ||
* https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage | ||
* @param object data to be sent via postMessage | ||
* @param recursive - not for application use | ||
* @param transfers - not for application use | ||
* @returns a transfer list that can be passed to postMessage | ||
*/ | ||
export function getTransferList(object, recursive = true, transfers) { | ||
// Make sure that items in the transfer list is unique | ||
const transfersSet = transfers || new Set(); | ||
if (!object) { | ||
// ignore | ||
} | ||
} | ||
return transfers === undefined ? Array.from(transfersSet) : []; | ||
else if (isTransferable(object)) { | ||
transfersSet.add(object); | ||
} | ||
else if (isTransferable(object.buffer)) { | ||
// Typed array | ||
transfersSet.add(object.buffer); | ||
} | ||
else if (ArrayBuffer.isView(object)) { | ||
// object is a TypeArray viewing into a SharedArrayBuffer (not transferable) | ||
// Do not iterate through the content in this case | ||
} | ||
else if (recursive && typeof object === 'object') { | ||
for (const key in object) { | ||
// Avoid perf hit - only go one level deep | ||
getTransferList(object[key], recursive, transfersSet); | ||
} | ||
} | ||
// If transfers is defined, is internal recursive call | ||
// Otherwise it's called by the user | ||
return transfers === undefined ? Array.from(transfersSet) : []; | ||
} | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Transferable | ||
function isTransferable(object) { | ||
if (!object) { | ||
if (!object) { | ||
return false; | ||
} | ||
if (object instanceof ArrayBuffer) { | ||
return true; | ||
} | ||
if (typeof MessagePort !== 'undefined' && object instanceof MessagePort) { | ||
return true; | ||
} | ||
if (typeof ImageBitmap !== 'undefined' && object instanceof ImageBitmap) { | ||
return true; | ||
} | ||
// @ts-ignore | ||
if (typeof OffscreenCanvas !== 'undefined' && object instanceof OffscreenCanvas) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
if (object instanceof ArrayBuffer) { | ||
return true; | ||
} | ||
if (typeof MessagePort !== 'undefined' && object instanceof MessagePort) { | ||
return true; | ||
} | ||
if (typeof ImageBitmap !== 'undefined' && object instanceof ImageBitmap) { | ||
return true; | ||
} | ||
if (typeof OffscreenCanvas !== 'undefined' && object instanceof OffscreenCanvas) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Recursively drop non serializable values like functions and regexps. | ||
* @param object | ||
*/ | ||
export function getTransferListForWriter(object) { | ||
if (object === null) { | ||
return {}; | ||
} | ||
const clone = Object.assign({}, object); | ||
Object.keys(clone).forEach(key => { | ||
if (typeof object[key] === 'object' && !ArrayBuffer.isView(object[key]) && !(object[key] instanceof Array)) { | ||
clone[key] = getTransferListForWriter(object[key]); | ||
} else if (typeof clone[key] === 'function' || clone[key] instanceof RegExp) { | ||
clone[key] = {}; | ||
} else { | ||
clone[key] = object[key]; | ||
if (object === null) { | ||
return {}; | ||
} | ||
}); | ||
return clone; | ||
const clone = Object.assign({}, object); | ||
Object.keys(clone).forEach((key) => { | ||
// Typed Arrays and Arrays are passed with no change | ||
if (typeof object[key] === 'object' && | ||
!ArrayBuffer.isView(object[key]) && | ||
!(object[key] instanceof Array)) { | ||
clone[key] = getTransferListForWriter(object[key]); | ||
} | ||
else if (typeof clone[key] === 'function' || clone[key] instanceof RegExp) { | ||
clone[key] = {}; | ||
} | ||
else { | ||
clone[key] = object[key]; | ||
} | ||
}); | ||
return clone; | ||
} | ||
//# sourceMappingURL=get-transfer-list.js.map |
@@ -0,17 +1,26 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
/** | ||
* Recursively drop non serializable values like functions and regexps. | ||
* @param object | ||
*/ | ||
export function removeNontransferableOptions(object) { | ||
if (object === null) { | ||
return {}; | ||
} | ||
const clone = Object.assign({}, object); | ||
Object.keys(clone).forEach(key => { | ||
if (typeof object[key] === 'object' && !ArrayBuffer.isView(object[key])) { | ||
clone[key] = removeNontransferableOptions(object[key]); | ||
} else if (typeof clone[key] === 'function' || clone[key] instanceof RegExp) { | ||
clone[key] = {}; | ||
} else { | ||
clone[key] = object[key]; | ||
if (object === null) { | ||
return {}; | ||
} | ||
}); | ||
return clone; | ||
const clone = Object.assign({}, object); | ||
Object.keys(clone).forEach((key) => { | ||
// Checking if it is an object and not a typed array. | ||
if (typeof object[key] === 'object' && !ArrayBuffer.isView(object[key])) { | ||
clone[key] = removeNontransferableOptions(object[key]); | ||
} | ||
else if (typeof clone[key] === 'function' || clone[key] instanceof RegExp) { | ||
clone[key] = {}; | ||
} | ||
else { | ||
clone[key] = object[key]; | ||
} | ||
}); | ||
return clone; | ||
} | ||
//# sourceMappingURL=remove-nontransferable-options.js.map |
@@ -0,2 +1,4 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
export {}; | ||
//# sourceMappingURL=types.js.map |
@@ -0,5 +1,8 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import { createWorker } from "../lib/worker-api/create-worker.js"; | ||
createWorker(async data => { | ||
return data; | ||
createWorker(async (data) => { | ||
// @ts-ignore | ||
return data; | ||
}); | ||
//# sourceMappingURL=null-worker.js.map |
{ | ||
"name": "@loaders.gl/worker-utils", | ||
"version": "4.2.0-alpha.4", | ||
"version": "4.2.0-alpha.5", | ||
"description": "Utilities for running tasks on worker threads", | ||
@@ -52,6 +52,6 @@ "license": "MIT", | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.3.1" | ||
"peerDependencies": { | ||
"@loaders.gl/core": "^4.0.0" | ||
}, | ||
"gitHead": "6c52dee5c3f005648a394cc4aee7fc37005c8e83" | ||
"gitHead": "32d95a81971f104e4dfeb88ab57065f05321a76a" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5614
329836
109
13
205
5
9
0
48
+ Added@loaders.gl/core@4.3.2(transitive)
+ Added@loaders.gl/loader-utils@4.3.2(transitive)
+ Added@loaders.gl/schema@4.3.2(transitive)
+ Added@loaders.gl/worker-utils@4.3.2(transitive)
+ Added@probe.gl/env@4.0.9(transitive)
+ Added@probe.gl/log@4.0.9(transitive)
+ Added@probe.gl/stats@4.0.9(transitive)
+ Added@types/geojson@7946.0.14(transitive)
- Removed@babel/runtime@^7.3.1
- Removed@babel/runtime@7.26.0(transitive)
- Removedregenerator-runtime@0.14.1(transitive)