Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@loaders.gl/worker-utils

Package Overview
Dependencies
Maintainers
8
Versions
205
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@loaders.gl/worker-utils - npm Package Compare versions

Comparing version 3.0.0-alpha.7 to 3.0.0-alpha.8

12

dist/es5/index.d.ts

@@ -8,12 +8,4 @@ import {WorkerObject} from './types';

// GENERAL UTILS
export {default as assert} from './lib/env-utils/assert';
export {
isBrowser,
isWorker,
nodeVersion,
self,
window,
global,
document
} from './lib/env-utils/globals';
export {assert} from './lib/env-utils/assert';
export {isBrowser, isWorker} from './lib/env-utils/globals';

@@ -20,0 +12,0 @@ // WORKER UTILS - TYPES

66

dist/es5/index.js

@@ -10,4 +10,4 @@ "use strict";

enumerable: true,
get: function () {
return _assert.default;
get: function get() {
return _assert.assert;
}

@@ -17,3 +17,3 @@ });

enumerable: true,
get: function () {
get: function get() {
return _globals.isBrowser;

@@ -24,39 +24,9 @@ }

enumerable: true,
get: function () {
get: function get() {
return _globals.isWorker;
}
});
Object.defineProperty(exports, "nodeVersion", {
enumerable: true,
get: function () {
return _globals.nodeVersion;
}
});
Object.defineProperty(exports, "self", {
enumerable: true,
get: function () {
return _globals.self;
}
});
Object.defineProperty(exports, "window", {
enumerable: true,
get: function () {
return _globals.window;
}
});
Object.defineProperty(exports, "global", {
enumerable: true,
get: function () {
return _globals.global;
}
});
Object.defineProperty(exports, "document", {
enumerable: true,
get: function () {
return _globals.document;
}
});
Object.defineProperty(exports, "processOnWorker", {
enumerable: true,
get: function () {
get: function get() {
return _processOnWorker.processOnWorker;

@@ -67,3 +37,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _createWorker.createWorker;

@@ -74,3 +44,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _workerObjectUtils.getWorkerObjectURL;

@@ -81,3 +51,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _workerObjectUtils.validateWorkerVersion;

@@ -88,3 +58,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _workerFarm.default;

@@ -95,3 +65,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _workerPool.default;

@@ -102,3 +72,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _workerBody.default;

@@ -109,3 +79,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _libraryUtils.getLibraryUrl;

@@ -116,3 +86,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _libraryUtils.loadLibrary;

@@ -123,3 +93,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _childProcessProxy.default;

@@ -130,3 +100,3 @@ }

enumerable: true,
get: function () {
get: function get() {
return _asyncQueue.default;

@@ -137,3 +107,3 @@ }

var _assert = _interopRequireDefault(require("./lib/env-utils/assert"));
var _assert = require("./lib/env-utils/assert");

@@ -160,4 +130,4 @@ var _globals = require("./lib/env-utils/globals");

const VERSION = typeof "3.0.0-alpha.7" !== 'undefined' ? "3.0.0-alpha.7" : 'latest';
const NullWorker = {
var VERSION = typeof "3.0.0-alpha.8" !== 'undefined' ? "3.0.0-alpha.8" : 'latest';
var NullWorker = {
id: 'null',

@@ -164,0 +134,0 @@ name: 'null',

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -8,4 +10,9 @@ value: true

class AsyncQueue {
constructor() {
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var AsyncQueue = function () {
function AsyncQueue() {
(0, _classCallCheck2.default)(this, AsyncQueue);
this._values = [];

@@ -16,81 +23,90 @@ this._settlers = [];

[Symbol.asyncIterator]() {
return this;
}
(0, _createClass2.default)(AsyncQueue, [{
key: Symbol.asyncIterator,
value: function value() {
return this;
}
}, {
key: "push",
value: function push(value) {
return this.enqueue(value);
}
}, {
key: "enqueue",
value: function enqueue(value) {
if (this._closed) {
throw new Error('Closed');
}
push(value) {
return this.enqueue(value);
}
if (this._settlers.length > 0) {
if (this._values.length > 0) {
throw new Error('Illegal internal state');
}
enqueue(value) {
if (this._closed) {
throw new Error('Closed');
}
var settler = this._settlers.shift();
if (this._settlers.length > 0) {
if (this._values.length > 0) {
throw new Error('Illegal internal state');
if (value instanceof Error) {
settler.reject(value);
} else {
settler.resolve({
value: value
});
}
} else {
this._values.push(value);
}
}
}, {
key: "close",
value: function close() {
while (this._settlers.length > 0) {
var settler = this._settlers.shift();
const settler = this._settlers.shift();
if (value instanceof Error) {
settler.reject(value);
} else {
settler.resolve({
value
done: true
});
}
} else {
this._values.push(value);
}
}
close() {
while (this._settlers.length > 0) {
const settler = this._settlers.shift();
settler.resolve({
done: true
});
this._closed = true;
}
}, {
key: "next",
value: function next() {
var _this = this;
this._closed = true;
}
if (this._values.length > 0) {
var value = this._values.shift();
next() {
if (this._values.length > 0) {
const value = this._values.shift();
if (value instanceof Error) {
return Promise.reject(value);
}
if (value instanceof Error) {
return Promise.reject(value);
return Promise.resolve({
done: false,
value: value
});
}
return Promise.resolve({
done: false,
value
});
}
if (this._closed) {
if (this._settlers.length > 0) {
throw new Error('Illegal internal state');
}
if (this._closed) {
if (this._settlers.length > 0) {
throw new Error('Illegal internal state');
return Promise.resolve({
done: true
});
}
return Promise.resolve({
done: true
return new Promise(function (resolve, reject) {
_this._settlers.push({
resolve: resolve,
reject: reject
});
});
}
}]);
return AsyncQueue;
}();
return new Promise((resolve, reject) => {
this._settlers.push({
resolve,
reject
});
});
}
}
exports.default = AsyncQueue;
//# sourceMappingURL=async-queue.js.map
/** Throws an `Error` with the optional `message` if `condition` is falsy */
export default function assert(condition: any, message?: string);
export function assert(condition: any, message?: string);

@@ -6,3 +6,3 @@ "use strict";

});
exports.default = assert;
exports.assert = assert;

@@ -9,0 +9,0 @@ function assert(condition, message) {

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -7,3 +9,6 @@ value: true

exports.nodeVersion = exports.isMobile = exports.isWorker = exports.isBrowser = exports.document = exports.global = exports.window = exports.self = void 0;
const globals = {
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var globals = {
self: typeof self !== 'undefined' && self,

@@ -14,19 +19,19 @@ window: typeof window !== 'undefined' && window,

};
const self_ = globals.self || globals.window || globals.global;
var self_ = globals.self || globals.window || globals.global;
exports.self = self_;
const window_ = globals.window || globals.self || globals.global;
var window_ = globals.window || globals.self || globals.global;
exports.window = window_;
const global_ = globals.global || globals.self || globals.window;
var global_ = globals.global || globals.self || globals.window;
exports.global = global_;
const document_ = globals.document || {};
var document_ = globals.document || {};
exports.document = document_;
const isBrowser = typeof process !== 'object' || String(process) !== '[object process]' || process.browser;
var isBrowser = (typeof process === "undefined" ? "undefined" : (0, _typeof2.default)(process)) !== 'object' || String(process) !== '[object process]' || process.browser;
exports.isBrowser = isBrowser;
const isWorker = typeof importScripts === 'function';
var isWorker = typeof importScripts === 'function';
exports.isWorker = isWorker;
const isMobile = typeof window !== 'undefined' && typeof window.orientation !== 'undefined';
var isMobile = typeof window !== 'undefined' && typeof window.orientation !== 'undefined';
exports.isMobile = isMobile;
const matches = typeof process !== 'undefined' && process.version && process.version.match(/v([0-9]*)/);
const nodeVersion = matches && parseFloat(matches[1]) || 0;
var matches = typeof process !== 'undefined' && process.version && process.version.match(/v([0-9]*)/);
var nodeVersion = matches && parseFloat(matches[1]) || 0;
exports.nodeVersion = nodeVersion;
//# sourceMappingURL=globals.js.map
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
Object.defineProperty(exports, "__esModule", {

@@ -13,2 +13,6 @@ value: true

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _globals = require("../env-utils/globals");

@@ -18,15 +22,43 @@

var _assert = _interopRequireDefault(require("../env-utils/assert"));
var _assert = require("../env-utils/assert");
const LATEST = 'beta';
const VERSION = typeof "3.0.0-alpha.7" !== 'undefined' ? "3.0.0-alpha.7" : LATEST;
const loadLibraryPromises = {};
var LATEST = 'beta';
var VERSION = typeof "3.0.0-alpha.8" !== 'undefined' ? "3.0.0-alpha.8" : LATEST;
var loadLibraryPromises = {};
async function loadLibrary(libraryUrl, moduleName = null, options = {}) {
if (moduleName) {
libraryUrl = getLibraryUrl(libraryUrl, moduleName, options);
}
function loadLibrary(_x) {
return _loadLibrary.apply(this, arguments);
}
loadLibraryPromises[libraryUrl] = loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl);
return await loadLibraryPromises[libraryUrl];
function _loadLibrary() {
_loadLibrary = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(libraryUrl) {
var moduleName,
options,
_args = arguments;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
moduleName = _args.length > 1 && _args[1] !== undefined ? _args[1] : null;
options = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
if (moduleName) {
libraryUrl = getLibraryUrl(libraryUrl, moduleName, options);
}
loadLibraryPromises[libraryUrl] = loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl);
_context.next = 6;
return loadLibraryPromises[libraryUrl];
case 6:
return _context.abrupt("return", _context.sent);
case 7:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _loadLibrary.apply(this, arguments);
}

@@ -39,3 +71,3 @@

const modules = options.modules || {};
var modules = options.modules || {};

@@ -51,3 +83,3 @@ if (modules[library]) {

if (options.CDN) {
(0, _assert.default)(options.CDN.startsWith('http'));
(0, _assert.assert)(options.CDN.startsWith('http'));
return "".concat(options.CDN, "/").concat(moduleName, "@").concat(VERSION, "/dist/libs/").concat(library);

@@ -63,19 +95,81 @@ }

async function loadLibraryFromFile(libraryUrl) {
if (libraryUrl.endsWith('wasm')) {
const response = await fetch(libraryUrl);
return await response.arrayBuffer();
}
function loadLibraryFromFile(_x2) {
return _loadLibraryFromFile.apply(this, arguments);
}
if (!_globals.isBrowser) {
return node.requireFromFile && (await node.requireFromFile(libraryUrl));
}
function _loadLibraryFromFile() {
_loadLibraryFromFile = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee2(libraryUrl) {
var _response, response, scriptSource;
if (_globals.isWorker) {
return importScripts(libraryUrl);
}
return _regenerator.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (!libraryUrl.endsWith('wasm')) {
_context2.next = 7;
break;
}
const response = await fetch(libraryUrl);
const scriptSource = await response.text();
return loadLibraryFromString(scriptSource, libraryUrl);
_context2.next = 3;
return fetch(libraryUrl);
case 3:
_response = _context2.sent;
_context2.next = 6;
return _response.arrayBuffer();
case 6:
return _context2.abrupt("return", _context2.sent);
case 7:
if (_globals.isBrowser) {
_context2.next = 14;
break;
}
_context2.t0 = node.requireFromFile;
if (!_context2.t0) {
_context2.next = 13;
break;
}
_context2.next = 12;
return node.requireFromFile(libraryUrl);
case 12:
_context2.t0 = _context2.sent;
case 13:
return _context2.abrupt("return", _context2.t0);
case 14:
if (!_globals.isWorker) {
_context2.next = 16;
break;
}
return _context2.abrupt("return", importScripts(libraryUrl));
case 16:
_context2.next = 18;
return fetch(libraryUrl);
case 18:
response = _context2.sent;
_context2.next = 21;
return response.text();
case 21:
scriptSource = _context2.sent;
return _context2.abrupt("return", loadLibraryFromString(scriptSource, libraryUrl));
case 23:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return _loadLibraryFromFile.apply(this, arguments);
}

@@ -93,3 +187,3 @@

const script = document.createElement('script');
var script = document.createElement('script');
script.id = id;

@@ -96,0 +190,0 @@

@@ -11,2 +11,10 @@ "use strict";

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _module = _interopRequireDefault(require("module"));

@@ -16,18 +24,56 @@

async function requireFromFile(filename) {
if (filename.startsWith('http')) {
const response = await fetch(filename);
const code = await response.text();
return requireFromString(code);
}
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
if (!filename.startsWith('/')) {
filename = "".concat(process.cwd(), "/").concat(filename);
}
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
return require(filename);
function requireFromFile(_x) {
return _requireFromFile.apply(this, arguments);
}
function requireFromString(code, filename = '', options = {}) {
if (typeof filename === 'object') {
function _requireFromFile() {
_requireFromFile = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(filename) {
var response, code;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!filename.startsWith('http')) {
_context.next = 8;
break;
}
_context.next = 3;
return fetch(filename);
case 3:
response = _context.sent;
_context.next = 6;
return response.text();
case 6:
code = _context.sent;
return _context.abrupt("return", requireFromString(code));
case 8:
if (!filename.startsWith('/')) {
filename = "".concat(process.cwd(), "/").concat(filename);
}
return _context.abrupt("return", require(filename));
case 10:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _requireFromFile.apply(this, arguments);
}
function requireFromString(code) {
var filename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if ((0, _typeof2.default)(filename) === 'object') {
options = filename;

@@ -37,16 +83,15 @@ filename = '';

options = {
options = _objectSpread({
appendPaths: [],
prependPaths: [],
...options
};
prependPaths: []
}, options);
if (typeof code !== 'string') {
throw new Error("code must be a string, not ".concat(typeof code));
throw new Error("code must be a string, not ".concat((0, _typeof2.default)(code)));
}
const paths = _module.default._nodeModulePaths(_path.default.dirname(filename));
var paths = _module.default._nodeModulePaths(_path.default.dirname(filename));
const parent = module.parent;
const newModule = new _module.default(filename, parent);
var parent = module.parent;
var newModule = new _module.default(filename, parent);
newModule.filename = filename;

@@ -53,0 +98,0 @@ newModule.paths = [].concat(options.prependPaths).concat(paths).concat(options.appendPaths);

@@ -10,9 +10,25 @@ "use strict";

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _child_process = _interopRequireDefault(require("child_process"));
var _assert = _interopRequireDefault(require("../env-utils/assert"));
var _assert = require("../env-utils/assert");
var _processUtils = require("./process-utils");
const DEFAULT_PROCESS_OPTIONS = {
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var DEFAULT_PROCESS_OPTIONS = {
command: null,

@@ -27,3 +43,3 @@ arguments: [],

},
onSuccess: processProxy => {
onSuccess: function onSuccess(processProxy) {
console.log("Started ".concat(processProxy.options.command));

@@ -33,6 +49,9 @@ }

class ChildProcessProxy {
constructor({
id = 'browser-driver'
} = {}) {
var ChildProcessProxy = function () {
function ChildProcessProxy() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$id = _ref.id,
id = _ref$id === void 0 ? 'browser-driver' : _ref$id;
(0, _classCallCheck2.default)(this, ChildProcessProxy);
this.id = id;

@@ -45,94 +64,192 @@ this.childProcess = null;

async start(options = {}) {
options = { ...DEFAULT_PROCESS_OPTIONS,
...options
};
(0, _assert.default)(options.command && typeof options.command === 'string');
this.options = options;
const args = [...options.arguments];
this.port = options.port;
(0, _createClass2.default)(ChildProcessProxy, [{
key: "start",
value: function () {
var _start = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee() {
var _this = this;
if (options.portArg) {
if (this.port === 'auto') {
this.port = await (0, _processUtils.getAvailablePort)(options.basePort);
}
var options,
args,
_args = arguments;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
options = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
options = _objectSpread(_objectSpread({}, DEFAULT_PROCESS_OPTIONS), options);
(0, _assert.assert)(options.command && typeof options.command === 'string');
this.options = options;
args = (0, _toConsumableArray2.default)(options.arguments);
this.port = options.port;
args.push(options.portArg, this.port);
}
if (!options.portArg) {
_context.next = 12;
break;
}
return await new Promise((resolve, reject) => {
try {
this._setTimeout(() => {
if (options.onSuccess) {
options.onSuccess(this);
}
if (!(this.port === 'auto')) {
_context.next = 11;
break;
}
resolve({});
});
_context.next = 10;
return (0, _processUtils.getAvailablePort)(options.basePort);
console.log("Spawning ".concat(options.command, " ").concat(options.arguments.join(' ')));
this.childProcess = _child_process.default.spawn(options.command, args, options.spawn);
this.childProcess.stdout.on('data', data => {
console.log(data.toString());
});
this.childProcess.stderr.on('data', data => {
console.log("Child process wrote to stderr: \"".concat(data, "\"."));
case 10:
this.port = _context.sent;
this._clearTimeout();
case 11:
args.push(options.portArg, this.port);
reject(new Error(data));
});
this.childProcess.on('error', error => {
console.log("Child process errored with ".concat(error));
case 12:
_context.next = 14;
return new Promise(function (resolve, reject) {
try {
_this._setTimeout(function () {
if (options.onSuccess) {
options.onSuccess(_this);
}
this._clearTimeout();
resolve({});
});
reject(error);
});
this.childProcess.on('close', code => {
console.log("Child process exited with ".concat(code));
this.childProcess = null;
console.log("Spawning ".concat(options.command, " ").concat(options.arguments.join(' ')));
_this.childProcess = _child_process.default.spawn(options.command, args, options.spawn);
this._clearTimeout();
_this.childProcess.stdout.on('data', function (data) {
console.log(data.toString());
});
resolve({});
});
} catch (error) {
reject(error);
_this.childProcess.stderr.on('data', function (data) {
console.log("Child process wrote to stderr: \"".concat(data, "\"."));
_this._clearTimeout();
reject(new Error(data));
});
_this.childProcess.on('error', function (error) {
console.log("Child process errored with ".concat(error));
_this._clearTimeout();
reject(error);
});
_this.childProcess.on('close', function (code) {
console.log("Child process exited with ".concat(code));
_this.childProcess = null;
_this._clearTimeout();
resolve({});
});
} catch (error) {
reject(error);
}
});
case 14:
return _context.abrupt("return", _context.sent);
case 15:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function start() {
return _start.apply(this, arguments);
}
});
}
async stop() {
if (this.childProcess) {
this.childProcess.kill();
this.childProcess = null;
}
}
return start;
}()
}, {
key: "stop",
value: function () {
var _stop = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee2() {
return _regenerator.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (this.childProcess) {
this.childProcess.kill();
this.childProcess = null;
}
async exit(statusCode = 0) {
try {
await this.stop();
process.exit(statusCode);
} catch (error) {
console.error(error.message || error);
process.exit(1);
}
}
case 1:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
_setTimeout(callback) {
if (this.options.wait > 0) {
this.successTimer = setTimeout(callback, this.options.wait);
}
}
function stop() {
return _stop.apply(this, arguments);
}
_clearTimeout() {
if (this.successTimer) {
clearTimeout(this.successTimer);
return stop;
}()
}, {
key: "exit",
value: function () {
var _exit = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee3() {
var statusCode,
_args3 = arguments;
return _regenerator.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
statusCode = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : 0;
_context3.prev = 1;
_context3.next = 4;
return this.stop();
case 4:
process.exit(statusCode);
_context3.next = 11;
break;
case 7:
_context3.prev = 7;
_context3.t0 = _context3["catch"](1);
console.error(_context3.t0.message || _context3.t0);
process.exit(1);
case 11:
case "end":
return _context3.stop();
}
}
}, _callee3, this, [[1, 7]]);
}));
function exit() {
return _exit.apply(this, arguments);
}
return exit;
}()
}, {
key: "_setTimeout",
value: function _setTimeout(callback) {
if (this.options.wait > 0) {
this.successTimer = setTimeout(callback, this.options.wait);
}
}
}
}, {
key: "_clearTimeout",
value: function _clearTimeout() {
if (this.successTimer) {
clearTimeout(this.successTimer);
}
}
}]);
return ChildProcessProxy;
}();
}
exports.default = ChildProcessProxy;
//# sourceMappingURL=child-process-proxy.js.map

@@ -12,5 +12,6 @@ "use strict";

function getAvailablePort(defaultPort = 3000) {
return new Promise((resolve, reject) => {
_child_process.default.exec('lsof -i -P -n | grep LISTEN', (error, stdout, stderr) => {
function getAvailablePort() {
var defaultPort = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3000;
return new Promise(function (resolve, reject) {
_child_process.default.exec('lsof -i -P -n | grep LISTEN', function (error, stdout, stderr) {
if (error) {

@@ -21,6 +22,6 @@ resolve(defaultPort);

const portsInUse = [];
const regex = /:(\d+) \(LISTEN\)/;
stdout.split('\n').forEach(line => {
const match = line.match(regex);
var portsInUse = [];
var regex = /:(\d+) \(LISTEN\)/;
stdout.split('\n').forEach(function (line) {
var match = line.match(regex);

@@ -31,3 +32,3 @@ if (match) {

});
let port = defaultPort;
var port = defaultPort;

@@ -34,0 +35,0 @@ while (portsInUse.includes(port)) {

@@ -10,2 +10,8 @@ "use strict";

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _asyncIterator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncIterator"));
var _asyncQueue = _interopRequireDefault(require("../async-queue/async-queue"));

@@ -15,4 +21,4 @@

let inputBatches;
let options;
var inputBatches;
var options;

@@ -24,54 +30,158 @@ function createWorker(process, processInBatches) {

_workerBody.default.onmessage = async (type, payload) => {
try {
switch (type) {
case 'process':
if (!process) {
throw new Error('Worker does not support atomic processing');
}
_workerBody.default.onmessage = function () {
var _ref = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(type, payload) {
var result, resultIterator, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, batch;
const result = await process(payload.input, payload.options || {}, payload);
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.t0 = type;
_context.next = _context.t0 === 'process' ? 4 : _context.t0 === 'process-in-batches' ? 11 : _context.t0 === 'input-batch' ? 51 : _context.t0 === 'input-done' ? 53 : 55;
break;
_workerBody.default.postMessage('done', {
result
});
case 4:
if (process) {
_context.next = 6;
break;
}
break;
throw new Error('Worker does not support atomic processing');
case 'process-in-batches':
if (!processInBatches) {
throw new Error('Worker does not support batched processing');
}
case 6:
_context.next = 8;
return process(payload.input, payload.options || {}, payload);
inputBatches = new _asyncQueue.default();
options = payload.options || {};
const resultIterator = processInBatches(inputBatches, options);
case 8:
result = _context.sent;
for await (const batch of resultIterator) {
_workerBody.default.postMessage('output-batch', {
result: batch
});
}
_workerBody.default.postMessage('done', {
result: result
});
_workerBody.default.postMessage('done', {});
return _context.abrupt("break", 55);
break;
case 11:
if (processInBatches) {
_context.next = 13;
break;
}
case 'input-batch':
inputBatches.push(payload.input);
break;
throw new Error('Worker does not support batched processing');
case 'input-done':
inputBatches.close();
break;
case 13:
inputBatches = new _asyncQueue.default();
options = payload.options || {};
resultIterator = processInBatches(inputBatches, options);
_iteratorNormalCompletion = true;
_didIteratorError = false;
_context.prev = 18;
_iterator = (0, _asyncIterator2.default)(resultIterator);
default:
}
} catch (error) {
_workerBody.default.postMessage('error', {
error: error.message
});
}
};
case 20:
_context.next = 22;
return _iterator.next();
case 22:
_step = _context.sent;
_iteratorNormalCompletion = _step.done;
_context.next = 26;
return _step.value;
case 26:
_value = _context.sent;
if (_iteratorNormalCompletion) {
_context.next = 33;
break;
}
batch = _value;
_workerBody.default.postMessage('output-batch', {
result: batch
});
case 30:
_iteratorNormalCompletion = true;
_context.next = 20;
break;
case 33:
_context.next = 39;
break;
case 35:
_context.prev = 35;
_context.t1 = _context["catch"](18);
_didIteratorError = true;
_iteratorError = _context.t1;
case 39:
_context.prev = 39;
_context.prev = 40;
if (!(!_iteratorNormalCompletion && _iterator.return != null)) {
_context.next = 44;
break;
}
_context.next = 44;
return _iterator.return();
case 44:
_context.prev = 44;
if (!_didIteratorError) {
_context.next = 47;
break;
}
throw _iteratorError;
case 47:
return _context.finish(44);
case 48:
return _context.finish(39);
case 49:
_workerBody.default.postMessage('done', {});
return _context.abrupt("break", 55);
case 51:
inputBatches.push(payload.input);
return _context.abrupt("break", 55);
case 53:
inputBatches.close();
return _context.abrupt("break", 55);
case 55:
_context.next = 60;
break;
case 57:
_context.prev = 57;
_context.t2 = _context["catch"](0);
_workerBody.default.postMessage('error', {
error: _context.t2.message
});
case 60:
case "end":
return _context.stop();
}
}
}, _callee, null, [[0, 57], [18, 35, 39, 49], [40,, 44, 48]]);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();
}
//# sourceMappingURL=create-worker.js.map

@@ -10,2 +10,6 @@ "use strict";

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _workerFarm = _interopRequireDefault(require("../worker-farm/worker-farm"));

@@ -15,35 +19,69 @@

async function processOnWorker(worker, data, options = {}) {
const name = (0, _workerObjectUtils.getWorkerObjectName)(worker, options);
const url = (0, _workerObjectUtils.getWorkerObjectURL)(worker, options);
function processOnWorker(_x, _x2) {
return _processOnWorker.apply(this, arguments);
}
const workerFarm = _workerFarm.default.getWorkerFarm(options);
function _processOnWorker() {
_processOnWorker = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(worker, data) {
var options,
name,
url,
workerFarm,
workerPool,
job,
transferableOptions,
result,
_args = arguments;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
options = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
name = (0, _workerObjectUtils.getWorkerObjectName)(worker, options);
url = (0, _workerObjectUtils.getWorkerObjectURL)(worker, options);
workerFarm = _workerFarm.default.getWorkerFarm(options);
workerPool = workerFarm.getWorkerPool({
name: name,
url: url
});
_context.next = 7;
return workerPool.startJob(worker.name, function (job_, type, payload) {
switch (type) {
case 'done':
job.done(payload);
break;
const workerPool = workerFarm.getWorkerPool({
name,
url
});
const job = await workerPool.startJob(worker.name, (job_, type, payload) => {
switch (type) {
case 'done':
job.done(payload);
break;
case 'error':
job.error(payload.error);
break;
case 'error':
job.error(payload.error);
break;
default:
job.error(type);
break;
}
});
default:
job.error(type);
break;
}
});
const transferableOptions = (0, _workerObjectUtils.removeNontransferableOptions)(options);
job.postMessage('process', {
input: data,
options: transferableOptions
});
const result = await job.result;
return result.result;
case 7:
job = _context.sent;
transferableOptions = (0, _workerObjectUtils.removeNontransferableOptions)(options);
job.postMessage('process', {
input: data,
options: transferableOptions
});
_context.next = 12;
return job.result;
case 12:
result = _context.sent;
return _context.abrupt("return", result.result);
case 14:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _processOnWorker.apply(this, arguments);
}
//# sourceMappingURL=process-on-worker.js.map

@@ -13,12 +13,14 @@ "use strict";

var _assert = _interopRequireDefault(require("../env-utils/assert"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
const NPM_TAG = 'beta';
const VERSION = typeof "3.0.0-alpha.7" !== 'undefined' ? "3.0.0-alpha.7" : NPM_TAG;
var _assert = require("../env-utils/assert");
var NPM_TAG = 'beta';
var VERSION = typeof "3.0.0-alpha.8" !== 'undefined' ? "3.0.0-alpha.8" : NPM_TAG;
function getWorkerObjectURL(worker, options) {
const topOptions = options || {};
const workerOptions = topOptions[worker.id] || {};
const workerFile = "".concat(worker.id, "-worker.js");
let url = workerOptions.workerUrl;
var topOptions = options || {};
var workerOptions = topOptions[worker.id] || {};
var workerFile = "".concat(worker.id, "-worker.js");
var url = workerOptions.workerUrl;

@@ -30,3 +32,3 @@ if (topOptions._workerType === 'test') {

if (!url) {
let version = worker.version;
var version = worker.version;

@@ -37,7 +39,7 @@ if (version === 'latest') {

const versionTag = version ? "@".concat(version) : '';
var versionTag = version ? "@".concat(version) : '';
url = "https://unpkg.com/@loaders.gl/".concat(worker.module).concat(versionTag, "/dist/").concat(workerFile);
}
(0, _assert.default)(url);
(0, _assert.assert)(url);
return url;

@@ -47,9 +49,10 @@ }

function getWorkerObjectName(worker) {
const warning = worker.version !== VERSION ? " (lib@".concat(VERSION, ")") : '';
var warning = worker.version !== VERSION ? " (lib@".concat(VERSION, ")") : '';
return "".concat(worker.name, "-worker@").concat(worker.version).concat(warning);
}
function validateWorkerVersion(worker, coreVersion = VERSION) {
(0, _assert.default)(worker, 'no worker provided');
let workerVersion = worker.version;
function validateWorkerVersion(worker) {
var coreVersion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : VERSION;
(0, _assert.assert)(worker, 'no worker provided');
var workerVersion = worker.version;

@@ -65,3 +68,3 @@ if (!coreVersion || !workerVersion) {

function parseVersion(version) {
const parts = version.split('.').map(Number);
var parts = version.split('.').map(Number);
return {

@@ -78,5 +81,5 @@ major: parts[0],

function stringifyJSON(v) {
const cache = new Set();
return JSON.stringify(v, (key, value) => {
if (typeof value === 'object' && value !== null) {
var cache = new Set();
return JSON.stringify(v, function (key, value) {
if ((0, _typeof2.default)(value) === 'object' && value !== null) {
if (cache.has(value)) {

@@ -83,0 +86,0 @@ try {

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -10,12 +8,11 @@ value: true

var _assert = _interopRequireDefault(require("../env-utils/assert"));
var _assert = require("../env-utils/assert");
const workerURLCache = new Map();
var workerURLCache = new Map();
function buildWorkerURL({
source,
url
}) {
(0, _assert.default)(source && !url || !source && url);
let workerURL = workerURLCache.get(source || url);
function buildWorkerURL(_ref) {
var source = _ref.source,
url = _ref.url;
(0, _assert.assert)(source && !url || !source && url);
var workerURL = workerURLCache.get(source || url);

@@ -34,3 +31,3 @@ if (!workerURL) {

(0, _assert.default)(workerURL);
(0, _assert.assert)(workerURL);
return workerURL;

@@ -44,3 +41,3 @@ }

const workerSource = buildScriptSource(url);
var workerSource = buildScriptSource(url);
return getWorkerURLFromSource(workerSource);

@@ -50,4 +47,4 @@ }

function getWorkerURLFromSource(workerSource) {
(0, _assert.default)(typeof workerSource === 'string', 'worker source');
const blob = new Blob([workerSource], {
(0, _assert.assert)(typeof workerSource === 'string', 'worker source');
var blob = new Blob([workerSource], {
type: 'application/javascript'

@@ -54,0 +51,0 @@ });

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -8,5 +10,9 @@ value: true

function getTransferList(object, recursive = true, transfers) {
const transfersSet = transfers || new Set();
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
function getTransferList(object) {
var recursive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var transfers = arguments.length > 2 ? arguments[2] : undefined;
var transfersSet = transfers || new Set();
if (!object) {} else if (isTransferable(object)) {

@@ -16,4 +22,4 @@ transfersSet.add(object);

transfersSet.add(object.buffer);
} else if (ArrayBuffer.isView(object)) {} else if (recursive && typeof object === 'object') {
for (const key in object) {
} else if (ArrayBuffer.isView(object)) {} else if (recursive && (0, _typeof2.default)(object) === 'object') {
for (var key in object) {
getTransferList(object[key], recursive, transfersSet);

@@ -20,0 +26,0 @@ }

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -8,26 +10,19 @@ value: true

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _getTransferList = require("../worker-farm/get-transfer-list");
const onMessageWrapperMap = new Map();
var onMessageWrapperMap = new Map();
class WorkerBody {
static set onmessage(onMessage) {
self.onmessage = message => {
if (!isKnownMessage(message)) {
return;
}
const {
type,
payload
} = message.data;
onMessage(type, payload);
};
var WorkerBody = function () {
function WorkerBody() {
(0, _classCallCheck2.default)(this, WorkerBody);
}
static addEventListener(onMessage) {
let onMessageWrapper = onMessageWrapperMap.get(onMessage);
if (!onMessageWrapper) {
onMessageWrapper = message => {
(0, _createClass2.default)(WorkerBody, null, [{
key: "onmessage",
set: function set(onMessage) {
self.onmessage = function (message) {
if (!isKnownMessage(message)) {

@@ -37,42 +32,59 @@ return;

const {
type,
payload
} = message.data;
var _message$data = message.data,
type = _message$data.type,
payload = _message$data.payload;
onMessage(type, payload);
};
}
}, {
key: "addEventListener",
value: function addEventListener(onMessage) {
var onMessageWrapper = onMessageWrapperMap.get(onMessage);
self.addEventListener('message', onMessageWrapper);
}
if (!onMessageWrapper) {
onMessageWrapper = function onMessageWrapper(message) {
if (!isKnownMessage(message)) {
return;
}
static removeEventListener(onMessage) {
const onMessageWrapper = onMessageWrapperMap.get(onMessage);
onMessageWrapperMap.delete(onMessage);
self.removeEventListener('message', onMessageWrapper);
}
var _message$data2 = message.data,
type = _message$data2.type,
payload = _message$data2.payload;
onMessage(type, payload);
};
}
static postMessage(type, payload) {
if (self) {
const data = {
source: 'loaders.gl',
type,
payload
};
const transferList = (0, _getTransferList.getTransferList)(payload);
self.postMessage(data, transferList);
self.addEventListener('message', onMessageWrapper);
}
}
}, {
key: "removeEventListener",
value: function removeEventListener(onMessage) {
var onMessageWrapper = onMessageWrapperMap.get(onMessage);
onMessageWrapperMap.delete(onMessage);
self.removeEventListener('message', onMessageWrapper);
}
}, {
key: "postMessage",
value: function postMessage(type, payload) {
if (self) {
var data = {
source: 'loaders.gl',
type: type,
payload: payload
};
var transferList = (0, _getTransferList.getTransferList)(payload);
self.postMessage(data, transferList);
}
}
}]);
return WorkerBody;
}();
}
exports.default = WorkerBody;
function isKnownMessage(message) {
const {
type,
data
} = message;
var type = message.type,
data = message.data;
return type === 'message' && data && typeof data.source === 'string' && data.source.startsWith('loaders.gl');
}
//# sourceMappingURL=worker-body.js.map

@@ -10,2 +10,8 @@ "use strict";

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _globals = require("../env-utils/globals");

@@ -17,26 +23,18 @@

const DEFAULT_PROPS = {
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var DEFAULT_PROPS = {
maxConcurrency: 3,
maxMobileConcurrency: 1,
onDebug: () => {},
onDebug: function onDebug() {},
reuseWorkers: true
};
let _workerFarm = null;
var _workerFarm = null;
class WorkerFarm {
static isSupported() {
return _workerThread.default.isSupported();
}
static getWorkerFarm(props = {}) {
_workerFarm = _workerFarm || new WorkerFarm({});
_workerFarm.setProps(props);
return _workerFarm;
}
constructor(props) {
this.props = { ...DEFAULT_PROPS
};
var WorkerFarm = function () {
function WorkerFarm(props) {
(0, _classCallCheck2.default)(this, WorkerFarm);
this.props = _objectSpread({}, DEFAULT_PROPS);
this.setProps(props);

@@ -46,37 +44,56 @@ this.workerPools = new Map();

destroy() {
this.workerPools.forEach(workerPool => workerPool.destroy());
}
(0, _createClass2.default)(WorkerFarm, [{
key: "destroy",
value: function destroy() {
this.workerPools.forEach(function (workerPool) {
return workerPool.destroy();
});
}
}, {
key: "setProps",
value: function setProps(props) {
this.props = _objectSpread(_objectSpread({}, this.props), props);
}
}, {
key: "getWorkerPool",
value: function getWorkerPool(_ref) {
var name = _ref.name,
source = _ref.source,
url = _ref.url;
var workerPool = this.workerPools.get(name);
setProps(props) {
this.props = { ...this.props,
...props
};
}
if (!workerPool) {
workerPool = new _workerPool.default({
name: name,
source: source,
url: url,
maxConcurrency: _globals.isMobile ? this.props.maxMobileConcurrency : this.props.maxConcurrency,
onDebug: this.props.onDebug,
reuseWorkers: this.props.reuseWorkers
});
this.workerPools.set(name, workerPool);
}
getWorkerPool({
name,
source,
url
}) {
let workerPool = this.workerPools.get(name);
if (!workerPool) {
workerPool = new _workerPool.default({
name,
source,
url,
maxConcurrency: _globals.isMobile ? this.props.maxMobileConcurrency : this.props.maxConcurrency,
onDebug: this.props.onDebug,
reuseWorkers: this.props.reuseWorkers
});
this.workerPools.set(name, workerPool);
return workerPool;
}
}], [{
key: "isSupported",
value: function isSupported() {
return _workerThread.default.isSupported();
}
}, {
key: "getWorkerFarm",
value: function getWorkerFarm() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_workerFarm = _workerFarm || new WorkerFarm({});
return workerPool;
}
_workerFarm.setProps(props);
}
return _workerFarm;
}
}]);
return WorkerFarm;
}();
exports.default = WorkerFarm;
//# sourceMappingURL=worker-farm.js.map

@@ -10,40 +10,52 @@ "use strict";

var _assert = _interopRequireDefault(require("../env-utils/assert"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
class WorkerJob {
constructor(jobName, workerThread) {
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assert = require("../env-utils/assert");
var WorkerJob = function () {
function WorkerJob(jobName, workerThread) {
var _this = this;
(0, _classCallCheck2.default)(this, WorkerJob);
this.name = jobName;
this.workerThread = workerThread;
this.isRunning = true;
this.result = new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
this.result = new Promise(function (resolve, reject) {
_this._resolve = resolve;
_this._reject = reject;
});
}
postMessage(type, payload) {
this.workerThread.postMessage({
source: 'loaders.gl',
type,
payload
});
}
(0, _createClass2.default)(WorkerJob, [{
key: "postMessage",
value: function postMessage(type, payload) {
this.workerThread.postMessage({
source: 'loaders.gl',
type: type,
payload: payload
});
}
}, {
key: "done",
value: function done(value) {
(0, _assert.assert)(this.isRunning);
this.isRunning = false;
done(value) {
(0, _assert.default)(this.isRunning);
this.isRunning = false;
this._resolve(value);
}
}, {
key: "error",
value: function error(_error) {
(0, _assert.assert)(this.isRunning);
this.isRunning = false;
this._resolve(value);
}
this._reject(_error);
}
}]);
return WorkerJob;
}();
error(error) {
(0, _assert.default)(this.isRunning);
this.isRunning = false;
this._reject(error);
}
}
exports.default = WorkerJob;
//# sourceMappingURL=worker-job.js.map

@@ -10,4 +10,12 @@ "use strict";

var _assert = _interopRequireDefault(require("../env-utils/assert"));
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assert = require("../env-utils/assert");
var _workerThread = _interopRequireDefault(require("./worker-thread"));

@@ -17,13 +25,17 @@

class WorkerPool {
constructor({
source,
url,
name = 'unnamed',
maxConcurrency = 1,
onMessage,
onDebug = () => {},
reuseWorkers = true
}) {
(0, _assert.default)(source || url);
var WorkerPool = function () {
function WorkerPool(_ref) {
var source = _ref.source,
url = _ref.url,
_ref$name = _ref.name,
name = _ref$name === void 0 ? 'unnamed' : _ref$name,
_ref$maxConcurrency = _ref.maxConcurrency,
maxConcurrency = _ref$maxConcurrency === void 0 ? 1 : _ref$maxConcurrency,
onMessage = _ref.onMessage,
_ref$onDebug = _ref.onDebug,
onDebug = _ref$onDebug === void 0 ? function () {} : _ref$onDebug,
_ref$reuseWorkers = _ref.reuseWorkers,
reuseWorkers = _ref$reuseWorkers === void 0 ? true : _ref$reuseWorkers;
(0, _classCallCheck2.default)(this, WorkerPool);
(0, _assert.assert)(source || url);
this.source = source;

@@ -42,95 +54,172 @@ this.url = url;

destroy() {
this.idleQueue.forEach(worker => worker.destroy());
this.isDestroyed = true;
}
async startJob(name, onMessage = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) {
const startPromise = new Promise(onStart => {
this.jobQueue.push({
name,
onMessage,
onError,
onStart
(0, _createClass2.default)(WorkerPool, [{
key: "destroy",
value: function destroy() {
this.idleQueue.forEach(function (worker) {
return worker.destroy();
});
});
this.isDestroyed = true;
}
}, {
key: "startJob",
value: function () {
var _startJob = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(name) {
var _this = this;
this._startQueuedJob();
var onMessage,
onError,
startPromise,
_args = arguments;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
onMessage = _args.length > 1 && _args[1] !== undefined ? _args[1] : function (job, type, data) {
return job.done(data);
};
onError = _args.length > 2 && _args[2] !== undefined ? _args[2] : function (job, error) {
return job.error(error);
};
startPromise = new Promise(function (onStart) {
_this.jobQueue.push({
name: name,
onMessage: onMessage,
onError: onError,
onStart: onStart
});
});
return startPromise;
}
this._startQueuedJob();
async _startQueuedJob() {
if (!this.jobQueue.length) {
return;
}
return _context.abrupt("return", startPromise);
const workerThread = this._getAvailableWorker();
case 5:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
if (!workerThread) {
return;
}
function startJob(_x) {
return _startJob.apply(this, arguments);
}
const queuedJob = this.jobQueue.shift();
return startJob;
}()
}, {
key: "_startQueuedJob",
value: function () {
var _startQueuedJob2 = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee2() {
var workerThread, queuedJob, job;
return _regenerator.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (this.jobQueue.length) {
_context2.next = 2;
break;
}
if (queuedJob) {
this.onDebug({
message: 'Starting job',
jobName: queuedJob.name,
workerThread,
backlog: this.jobQueue.length
});
const job = new _workerJob.default(queuedJob.name, workerThread);
return _context2.abrupt("return");
workerThread.onMessage = data => queuedJob.onMessage(job, data.type, data.payload);
case 2:
workerThread = this._getAvailableWorker();
workerThread.onError = error => queuedJob.onError(job, error);
if (workerThread) {
_context2.next = 5;
break;
}
queuedJob.onStart(job);
return _context2.abrupt("return");
try {
await job.result;
} finally {
this.returnWorkerToQueue(workerThread);
}
}
}
case 5:
queuedJob = this.jobQueue.shift();
returnWorkerToQueue(worker) {
if (this.isDestroyed) {
worker.destroy();
return;
}
if (!queuedJob) {
_context2.next = 18;
break;
}
if (this.reuseWorkers) {
this.idleQueue.push(worker);
} else {
worker.destroy();
this.count--;
}
this.onDebug({
message: 'Starting job',
jobName: queuedJob.name,
workerThread: workerThread,
backlog: this.jobQueue.length
});
job = new _workerJob.default(queuedJob.name, workerThread);
this._startQueuedJob();
}
workerThread.onMessage = function (data) {
return queuedJob.onMessage(job, data.type, data.payload);
};
_getAvailableWorker() {
if (this.idleQueue.length > 0) {
return this.idleQueue.shift() || null;
}
workerThread.onError = function (error) {
return queuedJob.onError(job, error);
};
if (this.count < this.maxConcurrency) {
this.count++;
const name = "".concat(this.name.toLowerCase(), " (#").concat(this.count, " of ").concat(this.maxConcurrency, ")");
return new _workerThread.default({
name,
source: this.source,
url: this.url
});
queuedJob.onStart(job);
_context2.prev = 12;
_context2.next = 15;
return job.result;
case 15:
_context2.prev = 15;
this.returnWorkerToQueue(workerThread);
return _context2.finish(15);
case 18:
case "end":
return _context2.stop();
}
}
}, _callee2, this, [[12,, 15, 18]]);
}));
function _startQueuedJob() {
return _startQueuedJob2.apply(this, arguments);
}
return _startQueuedJob;
}()
}, {
key: "returnWorkerToQueue",
value: function returnWorkerToQueue(worker) {
if (this.isDestroyed) {
worker.destroy();
return;
}
if (this.reuseWorkers) {
this.idleQueue.push(worker);
} else {
worker.destroy();
this.count--;
}
this._startQueuedJob();
}
}, {
key: "_getAvailableWorker",
value: function _getAvailableWorker() {
if (this.idleQueue.length > 0) {
return this.idleQueue.shift() || null;
}
return null;
}
if (this.count < this.maxConcurrency) {
this.count++;
var name = "".concat(this.name.toLowerCase(), " (#").concat(this.count, " of ").concat(this.maxConcurrency, ")");
return new _workerThread.default({
name: name,
source: this.source,
url: this.url
});
}
}
return null;
}
}]);
return WorkerPool;
}();
exports.default = WorkerPool;
//# sourceMappingURL=worker-pool.js.map

@@ -10,4 +10,8 @@ "use strict";

var _assert = _interopRequireDefault(require("../env-utils/assert"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assert = require("../env-utils/assert");
var _buildWorkerUrl = require("./build-worker-url");

@@ -17,16 +21,11 @@

const NOOP = _ => {};
var NOOP = function NOOP(_) {};
class WorkerThread {
static isSupported() {
return typeof Worker !== 'undefined';
}
constructor(props) {
const {
name,
source,
url
} = props;
(0, _assert.default)(source || url);
var WorkerThread = function () {
function WorkerThread(props) {
(0, _classCallCheck2.default)(this, WorkerThread);
var name = props.name,
source = props.source,
url = props.url;
(0, _assert.assert)(source || url);
this.name = name;

@@ -37,3 +36,5 @@ this.source = source;

this.onError = error => console.log(error);
this.onError = function (error) {
return console.log(error);
};

@@ -44,63 +45,80 @@ this.terminated = false;

destroy() {
this.onMessage = NOOP;
this.onError = NOOP;
this.worker.terminate();
this.terminated = true;
}
(0, _createClass2.default)(WorkerThread, [{
key: "destroy",
value: function destroy() {
this.onMessage = NOOP;
this.onError = NOOP;
this.worker.terminate();
this.terminated = true;
}
}, {
key: "isRunning",
get: function get() {
return Boolean(this.onMessage);
}
}, {
key: "postMessage",
value: function postMessage(data, transferList) {
transferList = transferList || (0, _getTransferList.getTransferList)(data);
this.worker.postMessage(data, transferList);
}
}, {
key: "_getErrorFromErrorEvent",
value: function _getErrorFromErrorEvent(event) {
var message = "Failed to load ";
message += "worker ".concat(this.name, ". ");
get isRunning() {
return Boolean(this.onMessage);
}
if (event.message) {
message += "".concat(event.message, " in ");
}
postMessage(data, transferList) {
transferList = transferList || (0, _getTransferList.getTransferList)(data);
this.worker.postMessage(data, transferList);
}
if (event.lineno) {
message += ":".concat(event.lineno, ":").concat(event.colno);
}
_getErrorFromErrorEvent(event) {
let message = "Failed to load ";
message += "worker ".concat(this.name, ". ");
if (event.message) {
message += "".concat(event.message, " in ");
return new Error(message);
}
}, {
key: "_createBrowserWorker",
value: function _createBrowserWorker() {
var _this = this;
if (event.lineno) {
message += ":".concat(event.lineno, ":").concat(event.colno);
}
this._loadableURL = (0, _buildWorkerUrl.buildWorkerURL)({
source: this.source,
url: this.url
});
var worker = new Worker(this._loadableURL, {
name: this.name
});
return new Error(message);
}
worker.onmessage = function (event) {
if (!event.data) {
_this.onError('No data received');
} else {
_this.onMessage(event.data);
}
};
_createBrowserWorker() {
this._loadableURL = (0, _buildWorkerUrl.buildWorkerURL)({
source: this.source,
url: this.url
});
const worker = new Worker(this._loadableURL, {
name: this.name
});
worker.onerror = function (error) {
_this.onError(_this._getErrorFromErrorEvent(error));
worker.onmessage = event => {
if (!event.data) {
this.onError('No data received');
} else {
this.onMessage(event.data);
}
};
_this.terminated = true;
};
worker.onerror = error => {
this.onError(this._getErrorFromErrorEvent(error));
this.terminated = true;
};
worker.onmessageerror = function (event) {
return console.error(event);
};
worker.onmessageerror = event => console.error(event);
return worker;
}
}], [{
key: "isSupported",
value: function isSupported() {
return typeof Worker !== 'undefined';
}
}]);
return WorkerThread;
}();
return worker;
}
}
exports.default = WorkerThread;
//# sourceMappingURL=worker-thread.js.map
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -10,8 +8,8 @@ value: true

var _assert = _interopRequireDefault(require("../env-utils/assert"));
var _assert = require("../env-utils/assert");
const workerURLCache = new Map();
var workerURLCache = new Map();
function getWorkerURL(workerSource) {
(0, _assert.default)(typeof workerSource === 'string', 'worker source');
(0, _assert.assert)(typeof workerSource === 'string', 'worker source');

@@ -22,6 +20,6 @@ if (workerSource.startsWith('url(') && workerSource.endsWith(')')) {

let workerURL = workerURLCache.get(workerSource);
var workerURL = workerURLCache.get(workerSource);
if (!workerURL) {
const blob = new Blob([workerSource], {
var blob = new Blob([workerSource], {
type: 'application/javascript'

@@ -28,0 +26,0 @@ });

@@ -8,12 +8,4 @@ import {WorkerObject} from './types';

// GENERAL UTILS
export {default as assert} from './lib/env-utils/assert';
export {
isBrowser,
isWorker,
nodeVersion,
self,
window,
global,
document
} from './lib/env-utils/globals';
export {assert} from './lib/env-utils/assert';
export {isBrowser, isWorker} from './lib/env-utils/globals';

@@ -20,0 +12,0 @@ // WORKER UTILS - TYPES

@@ -1,3 +0,3 @@

export { default as assert } from './lib/env-utils/assert';
export { isBrowser, isWorker, nodeVersion, self, window, global, document } from './lib/env-utils/globals';
export { assert } from './lib/env-utils/assert';
export { isBrowser, isWorker } from './lib/env-utils/globals';
export { processOnWorker } from './lib/worker-api/process-on-worker';

@@ -12,4 +12,4 @@ export { createWorker } from './lib/worker-api/create-worker';

export { default as AsyncQueue } from './lib/async-queue/async-queue';
const VERSION = typeof "3.0.0-alpha.7" !== 'undefined' ? "3.0.0-alpha.7" : 'latest';
export const NullWorker = {
var VERSION = typeof "3.0.0-alpha.8" !== 'undefined' ? "3.0.0-alpha.8" : 'latest';
export var NullWorker = {
id: 'null',

@@ -16,0 +16,0 @@ name: 'null',

@@ -1,3 +0,8 @@

export default class AsyncQueue {
constructor() {
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
var AsyncQueue = function () {
function AsyncQueue() {
_classCallCheck(this, AsyncQueue);
this._values = [];

@@ -8,79 +13,91 @@ this._settlers = [];

[Symbol.asyncIterator]() {
return this;
}
_createClass(AsyncQueue, [{
key: Symbol.asyncIterator,
value: function value() {
return this;
}
}, {
key: "push",
value: function push(value) {
return this.enqueue(value);
}
}, {
key: "enqueue",
value: function enqueue(value) {
if (this._closed) {
throw new Error('Closed');
}
push(value) {
return this.enqueue(value);
}
if (this._settlers.length > 0) {
if (this._values.length > 0) {
throw new Error('Illegal internal state');
}
enqueue(value) {
if (this._closed) {
throw new Error('Closed');
}
var settler = this._settlers.shift();
if (this._settlers.length > 0) {
if (this._values.length > 0) {
throw new Error('Illegal internal state');
if (value instanceof Error) {
settler.reject(value);
} else {
settler.resolve({
value: value
});
}
} else {
this._values.push(value);
}
}
}, {
key: "close",
value: function close() {
while (this._settlers.length > 0) {
var settler = this._settlers.shift();
const settler = this._settlers.shift();
if (value instanceof Error) {
settler.reject(value);
} else {
settler.resolve({
value
done: true
});
}
} else {
this._values.push(value);
}
}
close() {
while (this._settlers.length > 0) {
const settler = this._settlers.shift();
settler.resolve({
done: true
});
this._closed = true;
}
}, {
key: "next",
value: function next() {
var _this = this;
this._closed = true;
}
if (this._values.length > 0) {
var value = this._values.shift();
next() {
if (this._values.length > 0) {
const value = this._values.shift();
if (value instanceof Error) {
return Promise.reject(value);
}
if (value instanceof Error) {
return Promise.reject(value);
return Promise.resolve({
done: false,
value: value
});
}
return Promise.resolve({
done: false,
value
});
}
if (this._closed) {
if (this._settlers.length > 0) {
throw new Error('Illegal internal state');
}
if (this._closed) {
if (this._settlers.length > 0) {
throw new Error('Illegal internal state');
return Promise.resolve({
done: true
});
}
return Promise.resolve({
done: true
return new Promise(function (resolve, reject) {
_this._settlers.push({
resolve: resolve,
reject: reject
});
});
}
}]);
return new Promise((resolve, reject) => {
this._settlers.push({
resolve,
reject
});
});
}
return AsyncQueue;
}();
}
export { AsyncQueue as default };
//# sourceMappingURL=async-queue.js.map
/** Throws an `Error` with the optional `message` if `condition` is falsy */
export default function assert(condition: any, message?: string);
export function assert(condition: any, message?: string);

@@ -1,2 +0,2 @@

export default function assert(condition, message) {
export function assert(condition, message) {
if (!condition) {

@@ -3,0 +3,0 @@ throw new Error(message || 'loaders.gl assertion failed.');

@@ -1,2 +0,3 @@

const globals = {
import _typeof from "@babel/runtime/helpers/esm/typeof";
var globals = {
self: typeof self !== 'undefined' && self,

@@ -7,12 +8,12 @@ window: typeof window !== 'undefined' && window,

};
const self_ = globals.self || globals.window || globals.global;
const window_ = globals.window || globals.self || globals.global;
const global_ = globals.global || globals.self || globals.window;
const document_ = globals.document || {};
var self_ = globals.self || globals.window || globals.global;
var window_ = globals.window || globals.self || globals.global;
var global_ = globals.global || globals.self || globals.window;
var document_ = globals.document || {};
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;
export const isWorker = typeof importScripts === 'function';
export const isMobile = typeof window !== 'undefined' && typeof window.orientation !== 'undefined';
const matches = typeof process !== 'undefined' && process.version && process.version.match(/v([0-9]*)/);
export const nodeVersion = matches && parseFloat(matches[1]) || 0;
export var isBrowser = (typeof process === "undefined" ? "undefined" : _typeof(process)) !== 'object' || String(process) !== '[object process]' || process.browser;
export var isWorker = typeof importScripts === 'function';
export var isMobile = typeof window !== 'undefined' && typeof window.orientation !== 'undefined';
var matches = typeof process !== 'undefined' && process.version && process.version.match(/v([0-9]*)/);
export var nodeVersion = matches && parseFloat(matches[1]) || 0;
//# sourceMappingURL=globals.js.map

@@ -0,15 +1,46 @@

import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { global, isBrowser, isWorker } from '../env-utils/globals';
import * as node from '../node/require-utils.node';
import assert from '../env-utils/assert';
const LATEST = 'beta';
const VERSION = typeof "3.0.0-alpha.7" !== 'undefined' ? "3.0.0-alpha.7" : LATEST;
const loadLibraryPromises = {};
export async function loadLibrary(libraryUrl, moduleName = null, options = {}) {
if (moduleName) {
libraryUrl = getLibraryUrl(libraryUrl, moduleName, options);
}
import { assert } from '../env-utils/assert';
var LATEST = 'beta';
var VERSION = typeof "3.0.0-alpha.8" !== 'undefined' ? "3.0.0-alpha.8" : LATEST;
var loadLibraryPromises = {};
export function loadLibrary(_x) {
return _loadLibrary.apply(this, arguments);
}
loadLibraryPromises[libraryUrl] = loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl);
return await loadLibraryPromises[libraryUrl];
function _loadLibrary() {
_loadLibrary = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(libraryUrl) {
var moduleName,
options,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
moduleName = _args.length > 1 && _args[1] !== undefined ? _args[1] : null;
options = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
if (moduleName) {
libraryUrl = getLibraryUrl(libraryUrl, moduleName, options);
}
loadLibraryPromises[libraryUrl] = loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl);
_context.next = 6;
return loadLibraryPromises[libraryUrl];
case 6:
return _context.abrupt("return", _context.sent);
case 7:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _loadLibrary.apply(this, arguments);
}
export function getLibraryUrl(library, moduleName, options) {

@@ -20,3 +51,3 @@ if (library.startsWith('http')) {

const modules = options.modules || {};
var modules = options.modules || {};

@@ -43,19 +74,81 @@ if (modules[library]) {

async function loadLibraryFromFile(libraryUrl) {
if (libraryUrl.endsWith('wasm')) {
const response = await fetch(libraryUrl);
return await response.arrayBuffer();
}
function loadLibraryFromFile(_x2) {
return _loadLibraryFromFile.apply(this, arguments);
}
if (!isBrowser) {
return node.requireFromFile && (await node.requireFromFile(libraryUrl));
}
function _loadLibraryFromFile() {
_loadLibraryFromFile = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2(libraryUrl) {
var _response, response, scriptSource;
if (isWorker) {
return importScripts(libraryUrl);
}
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (!libraryUrl.endsWith('wasm')) {
_context2.next = 7;
break;
}
const response = await fetch(libraryUrl);
const scriptSource = await response.text();
return loadLibraryFromString(scriptSource, libraryUrl);
_context2.next = 3;
return fetch(libraryUrl);
case 3:
_response = _context2.sent;
_context2.next = 6;
return _response.arrayBuffer();
case 6:
return _context2.abrupt("return", _context2.sent);
case 7:
if (isBrowser) {
_context2.next = 14;
break;
}
_context2.t0 = node.requireFromFile;
if (!_context2.t0) {
_context2.next = 13;
break;
}
_context2.next = 12;
return node.requireFromFile(libraryUrl);
case 12:
_context2.t0 = _context2.sent;
case 13:
return _context2.abrupt("return", _context2.t0);
case 14:
if (!isWorker) {
_context2.next = 16;
break;
}
return _context2.abrupt("return", importScripts(libraryUrl));
case 16:
_context2.next = 18;
return fetch(libraryUrl);
case 18:
response = _context2.sent;
_context2.next = 21;
return response.text();
case 21:
scriptSource = _context2.sent;
return _context2.abrupt("return", loadLibraryFromString(scriptSource, libraryUrl));
case 23:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return _loadLibraryFromFile.apply(this, arguments);
}

@@ -73,3 +166,3 @@

const script = document.createElement('script');
var script = document.createElement('script');
script.id = id;

@@ -76,0 +169,0 @@

@@ -0,18 +1,62 @@

import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
import Module from 'module';
import path from 'path';
export async function requireFromFile(filename) {
if (filename.startsWith('http')) {
const response = await fetch(filename);
const code = await response.text();
return requireFromString(code);
}
export function requireFromFile(_x) {
return _requireFromFile.apply(this, arguments);
}
if (!filename.startsWith('/')) {
filename = "".concat(process.cwd(), "/").concat(filename);
}
function _requireFromFile() {
_requireFromFile = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(filename) {
var response, code;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!filename.startsWith('http')) {
_context.next = 8;
break;
}
return require(filename);
_context.next = 3;
return fetch(filename);
case 3:
response = _context.sent;
_context.next = 6;
return response.text();
case 6:
code = _context.sent;
return _context.abrupt("return", requireFromString(code));
case 8:
if (!filename.startsWith('/')) {
filename = "".concat(process.cwd(), "/").concat(filename);
}
return _context.abrupt("return", require(filename));
case 10:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _requireFromFile.apply(this, arguments);
}
export function requireFromString(code, filename = '', options = {}) {
if (typeof filename === 'object') {
export function requireFromString(code) {
var filename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (_typeof(filename) === 'object') {
options = filename;

@@ -22,16 +66,15 @@ filename = '';

options = {
options = _objectSpread({
appendPaths: [],
prependPaths: [],
...options
};
prependPaths: []
}, options);
if (typeof code !== 'string') {
throw new Error("code must be a string, not ".concat(typeof code));
throw new Error("code must be a string, not ".concat(_typeof(code)));
}
const paths = Module._nodeModulePaths(path.dirname(filename));
var paths = Module._nodeModulePaths(path.dirname(filename));
const parent = module.parent;
const newModule = new Module(filename, parent);
var parent = module.parent;
var newModule = new Module(filename, parent);
newModule.filename = filename;

@@ -38,0 +81,0 @@ newModule.paths = [].concat(options.prependPaths).concat(paths).concat(options.appendPaths);

@@ -0,5 +1,16 @@

import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _regeneratorRuntime from "@babel/runtime/regenerator";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
import ChildProcess from 'child_process';
import assert from '../env-utils/assert';
import { assert } from '../env-utils/assert';
import { getAvailablePort } from './process-utils';
const DEFAULT_PROCESS_OPTIONS = {
var DEFAULT_PROCESS_OPTIONS = {
command: null,

@@ -14,10 +25,15 @@ arguments: [],

},
onSuccess: processProxy => {
onSuccess: function onSuccess(processProxy) {
console.log("Started ".concat(processProxy.options.command));
}
};
export default class ChildProcessProxy {
constructor({
id = 'browser-driver'
} = {}) {
var ChildProcessProxy = function () {
function ChildProcessProxy() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$id = _ref.id,
id = _ref$id === void 0 ? 'browser-driver' : _ref$id;
_classCallCheck(this, ChildProcessProxy);
this.id = id;

@@ -30,92 +46,193 @@ this.childProcess = null;

async start(options = {}) {
options = { ...DEFAULT_PROCESS_OPTIONS,
...options
};
assert(options.command && typeof options.command === 'string');
this.options = options;
const args = [...options.arguments];
this.port = options.port;
_createClass(ChildProcessProxy, [{
key: "start",
value: function () {
var _start = _asyncToGenerator(_regeneratorRuntime.mark(function _callee() {
var _this = this;
if (options.portArg) {
if (this.port === 'auto') {
this.port = await getAvailablePort(options.basePort);
}
var options,
args,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
options = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
options = _objectSpread(_objectSpread({}, DEFAULT_PROCESS_OPTIONS), options);
assert(options.command && typeof options.command === 'string');
this.options = options;
args = _toConsumableArray(options.arguments);
this.port = options.port;
args.push(options.portArg, this.port);
}
if (!options.portArg) {
_context.next = 12;
break;
}
return await new Promise((resolve, reject) => {
try {
this._setTimeout(() => {
if (options.onSuccess) {
options.onSuccess(this);
if (!(this.port === 'auto')) {
_context.next = 11;
break;
}
_context.next = 10;
return getAvailablePort(options.basePort);
case 10:
this.port = _context.sent;
case 11:
args.push(options.portArg, this.port);
case 12:
_context.next = 14;
return new Promise(function (resolve, reject) {
try {
_this._setTimeout(function () {
if (options.onSuccess) {
options.onSuccess(_this);
}
resolve({});
});
console.log("Spawning ".concat(options.command, " ").concat(options.arguments.join(' ')));
_this.childProcess = ChildProcess.spawn(options.command, args, options.spawn);
_this.childProcess.stdout.on('data', function (data) {
console.log(data.toString());
});
_this.childProcess.stderr.on('data', function (data) {
console.log("Child process wrote to stderr: \"".concat(data, "\"."));
_this._clearTimeout();
reject(new Error(data));
});
_this.childProcess.on('error', function (error) {
console.log("Child process errored with ".concat(error));
_this._clearTimeout();
reject(error);
});
_this.childProcess.on('close', function (code) {
console.log("Child process exited with ".concat(code));
_this.childProcess = null;
_this._clearTimeout();
resolve({});
});
} catch (error) {
reject(error);
}
});
case 14:
return _context.abrupt("return", _context.sent);
case 15:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
resolve({});
});
function start() {
return _start.apply(this, arguments);
}
console.log("Spawning ".concat(options.command, " ").concat(options.arguments.join(' ')));
this.childProcess = ChildProcess.spawn(options.command, args, options.spawn);
this.childProcess.stdout.on('data', data => {
console.log(data.toString());
});
this.childProcess.stderr.on('data', data => {
console.log("Child process wrote to stderr: \"".concat(data, "\"."));
return start;
}()
}, {
key: "stop",
value: function () {
var _stop = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2() {
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (this.childProcess) {
this.childProcess.kill();
this.childProcess = null;
}
this._clearTimeout();
case 1:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
reject(new Error(data));
});
this.childProcess.on('error', error => {
console.log("Child process errored with ".concat(error));
function stop() {
return _stop.apply(this, arguments);
}
this._clearTimeout();
return stop;
}()
}, {
key: "exit",
value: function () {
var _exit = _asyncToGenerator(_regeneratorRuntime.mark(function _callee3() {
var statusCode,
_args3 = arguments;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
statusCode = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : 0;
_context3.prev = 1;
_context3.next = 4;
return this.stop();
reject(error);
});
this.childProcess.on('close', code => {
console.log("Child process exited with ".concat(code));
this.childProcess = null;
case 4:
process.exit(statusCode);
_context3.next = 11;
break;
this._clearTimeout();
case 7:
_context3.prev = 7;
_context3.t0 = _context3["catch"](1);
console.error(_context3.t0.message || _context3.t0);
process.exit(1);
resolve({});
});
} catch (error) {
reject(error);
case 11:
case "end":
return _context3.stop();
}
}
}, _callee3, this, [[1, 7]]);
}));
function exit() {
return _exit.apply(this, arguments);
}
});
}
async stop() {
if (this.childProcess) {
this.childProcess.kill();
this.childProcess = null;
return exit;
}()
}, {
key: "_setTimeout",
value: function _setTimeout(callback) {
if (this.options.wait > 0) {
this.successTimer = setTimeout(callback, this.options.wait);
}
}
}
async exit(statusCode = 0) {
try {
await this.stop();
process.exit(statusCode);
} catch (error) {
console.error(error.message || error);
process.exit(1);
}, {
key: "_clearTimeout",
value: function _clearTimeout() {
if (this.successTimer) {
clearTimeout(this.successTimer);
}
}
}
}]);
_setTimeout(callback) {
if (this.options.wait > 0) {
this.successTimer = setTimeout(callback, this.options.wait);
}
}
return ChildProcessProxy;
}();
_clearTimeout() {
if (this.successTimer) {
clearTimeout(this.successTimer);
}
}
}
export { ChildProcessProxy as default };
//# sourceMappingURL=child-process-proxy.js.map
import ChildProcess from 'child_process';
export function getAvailablePort(defaultPort = 3000) {
return new Promise((resolve, reject) => {
ChildProcess.exec('lsof -i -P -n | grep LISTEN', (error, stdout, stderr) => {
export function getAvailablePort() {
var defaultPort = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3000;
return new Promise(function (resolve, reject) {
ChildProcess.exec('lsof -i -P -n | grep LISTEN', function (error, stdout, stderr) {
if (error) {

@@ -10,6 +11,6 @@ resolve(defaultPort);

const portsInUse = [];
const regex = /:(\d+) \(LISTEN\)/;
stdout.split('\n').forEach(line => {
const match = line.match(regex);
var portsInUse = [];
var regex = /:(\d+) \(LISTEN\)/;
stdout.split('\n').forEach(function (line) {
var match = line.match(regex);

@@ -20,3 +21,3 @@ if (match) {

});
let port = defaultPort;
var port = defaultPort;

@@ -23,0 +24,0 @@ while (portsInUse.includes(port)) {

@@ -0,5 +1,8 @@

import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _asyncIterator from "@babel/runtime/helpers/esm/asyncIterator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import AsyncQueue from '../async-queue/async-queue';
import WorkerBody from '../worker-farm/worker-body';
let inputBatches;
let options;
var inputBatches;
var options;
export function createWorker(process, processInBatches) {

@@ -10,51 +13,153 @@ if (typeof self === 'undefined') {

WorkerBody.onmessage = async (type, payload) => {
try {
switch (type) {
case 'process':
if (!process) {
throw new Error('Worker does not support atomic processing');
}
WorkerBody.onmessage = function () {
var _ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(type, payload) {
var result, resultIterator, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, batch;
const result = await process(payload.input, payload.options || {}, payload);
WorkerBody.postMessage('done', {
result
});
break;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.t0 = type;
_context.next = _context.t0 === 'process' ? 4 : _context.t0 === 'process-in-batches' ? 11 : _context.t0 === 'input-batch' ? 51 : _context.t0 === 'input-done' ? 53 : 55;
break;
case 'process-in-batches':
if (!processInBatches) {
throw new Error('Worker does not support batched processing');
}
case 4:
if (process) {
_context.next = 6;
break;
}
inputBatches = new AsyncQueue();
options = payload.options || {};
const resultIterator = processInBatches(inputBatches, options);
throw new Error('Worker does not support atomic processing');
for await (const batch of resultIterator) {
WorkerBody.postMessage('output-batch', {
result: batch
});
}
case 6:
_context.next = 8;
return process(payload.input, payload.options || {}, payload);
WorkerBody.postMessage('done', {});
break;
case 8:
result = _context.sent;
WorkerBody.postMessage('done', {
result: result
});
return _context.abrupt("break", 55);
case 'input-batch':
inputBatches.push(payload.input);
break;
case 11:
if (processInBatches) {
_context.next = 13;
break;
}
case 'input-done':
inputBatches.close();
break;
throw new Error('Worker does not support batched processing');
default:
}
} catch (error) {
WorkerBody.postMessage('error', {
error: error.message
});
}
};
case 13:
inputBatches = new AsyncQueue();
options = payload.options || {};
resultIterator = processInBatches(inputBatches, options);
_iteratorNormalCompletion = true;
_didIteratorError = false;
_context.prev = 18;
_iterator = _asyncIterator(resultIterator);
case 20:
_context.next = 22;
return _iterator.next();
case 22:
_step = _context.sent;
_iteratorNormalCompletion = _step.done;
_context.next = 26;
return _step.value;
case 26:
_value = _context.sent;
if (_iteratorNormalCompletion) {
_context.next = 33;
break;
}
batch = _value;
WorkerBody.postMessage('output-batch', {
result: batch
});
case 30:
_iteratorNormalCompletion = true;
_context.next = 20;
break;
case 33:
_context.next = 39;
break;
case 35:
_context.prev = 35;
_context.t1 = _context["catch"](18);
_didIteratorError = true;
_iteratorError = _context.t1;
case 39:
_context.prev = 39;
_context.prev = 40;
if (!(!_iteratorNormalCompletion && _iterator.return != null)) {
_context.next = 44;
break;
}
_context.next = 44;
return _iterator.return();
case 44:
_context.prev = 44;
if (!_didIteratorError) {
_context.next = 47;
break;
}
throw _iteratorError;
case 47:
return _context.finish(44);
case 48:
return _context.finish(39);
case 49:
WorkerBody.postMessage('done', {});
return _context.abrupt("break", 55);
case 51:
inputBatches.push(payload.input);
return _context.abrupt("break", 55);
case 53:
inputBatches.close();
return _context.abrupt("break", 55);
case 55:
_context.next = 60;
break;
case 57:
_context.prev = 57;
_context.t2 = _context["catch"](0);
WorkerBody.postMessage('error', {
error: _context.t2.message
});
case 60:
case "end":
return _context.stop();
}
}
}, _callee, null, [[0, 57], [18, 35, 39, 49], [40,, 44, 48]]);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();
}
//# sourceMappingURL=create-worker.js.map

@@ -0,34 +1,72 @@

import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import WorkerFarm from '../worker-farm/worker-farm';
import { getWorkerObjectURL, getWorkerObjectName, removeNontransferableOptions } from './worker-object-utils';
export async function processOnWorker(worker, data, options = {}) {
const name = getWorkerObjectName(worker, options);
const url = getWorkerObjectURL(worker, options);
const workerFarm = WorkerFarm.getWorkerFarm(options);
const workerPool = workerFarm.getWorkerPool({
name,
url
});
const job = await workerPool.startJob(worker.name, (job_, type, payload) => {
switch (type) {
case 'done':
job.done(payload);
break;
export function processOnWorker(_x, _x2) {
return _processOnWorker.apply(this, arguments);
}
case 'error':
job.error(payload.error);
break;
function _processOnWorker() {
_processOnWorker = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(worker, data) {
var options,
name,
url,
workerFarm,
workerPool,
job,
transferableOptions,
result,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
options = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
name = getWorkerObjectName(worker, options);
url = getWorkerObjectURL(worker, options);
workerFarm = WorkerFarm.getWorkerFarm(options);
workerPool = workerFarm.getWorkerPool({
name: name,
url: url
});
_context.next = 7;
return workerPool.startJob(worker.name, function (job_, type, payload) {
switch (type) {
case 'done':
job.done(payload);
break;
default:
job.error(type);
break;
}
});
const transferableOptions = removeNontransferableOptions(options);
job.postMessage('process', {
input: data,
options: transferableOptions
});
const result = await job.result;
return result.result;
case 'error':
job.error(payload.error);
break;
default:
job.error(type);
break;
}
});
case 7:
job = _context.sent;
transferableOptions = removeNontransferableOptions(options);
job.postMessage('process', {
input: data,
options: transferableOptions
});
_context.next = 12;
return job.result;
case 12:
result = _context.sent;
return _context.abrupt("return", result.result);
case 14:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _processOnWorker.apply(this, arguments);
}
//# sourceMappingURL=process-on-worker.js.map

@@ -1,9 +0,10 @@

import assert from '../env-utils/assert';
const NPM_TAG = 'beta';
const VERSION = typeof "3.0.0-alpha.7" !== 'undefined' ? "3.0.0-alpha.7" : NPM_TAG;
import _typeof from "@babel/runtime/helpers/esm/typeof";
import { assert } from '../env-utils/assert';
var NPM_TAG = 'beta';
var VERSION = typeof "3.0.0-alpha.8" !== 'undefined' ? "3.0.0-alpha.8" : NPM_TAG;
export function getWorkerObjectURL(worker, options) {
const topOptions = options || {};
const workerOptions = topOptions[worker.id] || {};
const workerFile = "".concat(worker.id, "-worker.js");
let url = workerOptions.workerUrl;
var topOptions = options || {};
var workerOptions = topOptions[worker.id] || {};
var workerFile = "".concat(worker.id, "-worker.js");
var url = workerOptions.workerUrl;

@@ -15,3 +16,3 @@ if (topOptions._workerType === 'test') {

if (!url) {
let version = worker.version;
var version = worker.version;

@@ -22,3 +23,3 @@ if (version === 'latest') {

const versionTag = version ? "@".concat(version) : '';
var versionTag = version ? "@".concat(version) : '';
url = "https://unpkg.com/@loaders.gl/".concat(worker.module).concat(versionTag, "/dist/").concat(workerFile);

@@ -31,8 +32,9 @@ }

export function getWorkerObjectName(worker) {
const warning = worker.version !== VERSION ? " (lib@".concat(VERSION, ")") : '';
var warning = worker.version !== VERSION ? " (lib@".concat(VERSION, ")") : '';
return "".concat(worker.name, "-worker@").concat(worker.version).concat(warning);
}
export function validateWorkerVersion(worker, coreVersion = VERSION) {
export function validateWorkerVersion(worker) {
var coreVersion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : VERSION;
assert(worker, 'no worker provided');
let workerVersion = worker.version;
var workerVersion = worker.version;

@@ -48,3 +50,3 @@ if (!coreVersion || !workerVersion) {

function parseVersion(version) {
const parts = version.split('.').map(Number);
var parts = version.split('.').map(Number);
return {

@@ -61,5 +63,5 @@ major: parts[0],

function stringifyJSON(v) {
const cache = new Set();
return JSON.stringify(v, (key, value) => {
if (typeof value === 'object' && value !== null) {
var cache = new Set();
return JSON.stringify(v, function (key, value) {
if (_typeof(value) === 'object' && value !== null) {
if (cache.has(value)) {

@@ -66,0 +68,0 @@ try {

@@ -1,9 +0,8 @@

import assert from '../env-utils/assert';
const workerURLCache = new Map();
export function buildWorkerURL({
source,
url
}) {
import { assert } from '../env-utils/assert';
var workerURLCache = new Map();
export function buildWorkerURL(_ref) {
var source = _ref.source,
url = _ref.url;
assert(source && !url || !source && url);
let workerURL = workerURLCache.get(source || url);
var workerURL = workerURLCache.get(source || url);

@@ -31,3 +30,3 @@ if (!workerURL) {

const workerSource = buildScriptSource(url);
var workerSource = buildScriptSource(url);
return getWorkerURLFromSource(workerSource);

@@ -38,3 +37,3 @@ }

assert(typeof workerSource === 'string', 'worker source');
const blob = new Blob([workerSource], {
var blob = new Blob([workerSource], {
type: 'application/javascript'

@@ -41,0 +40,0 @@ });

@@ -1,3 +0,6 @@

export function getTransferList(object, recursive = true, transfers) {
const transfersSet = transfers || new Set();
import _typeof from "@babel/runtime/helpers/esm/typeof";
export function getTransferList(object) {
var recursive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var transfers = arguments.length > 2 ? arguments[2] : undefined;
var transfersSet = transfers || new Set();

@@ -8,4 +11,4 @@ if (!object) {} else if (isTransferable(object)) {

transfersSet.add(object.buffer);
} else if (ArrayBuffer.isView(object)) {} else if (recursive && typeof object === 'object') {
for (const key in object) {
} else if (ArrayBuffer.isView(object)) {} else if (recursive && _typeof(object) === 'object') {
for (var key in object) {
getTransferList(object[key], recursive, transfersSet);

@@ -12,0 +15,0 @@ }

@@ -0,23 +1,15 @@

import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import { getTransferList } from '../worker-farm/get-transfer-list';
const onMessageWrapperMap = new Map();
export default class WorkerBody {
static set onmessage(onMessage) {
self.onmessage = message => {
if (!isKnownMessage(message)) {
return;
}
var onMessageWrapperMap = new Map();
const {
type,
payload
} = message.data;
onMessage(type, payload);
};
var WorkerBody = function () {
function WorkerBody() {
_classCallCheck(this, WorkerBody);
}
static addEventListener(onMessage) {
let onMessageWrapper = onMessageWrapperMap.get(onMessage);
if (!onMessageWrapper) {
onMessageWrapper = message => {
_createClass(WorkerBody, null, [{
key: "onmessage",
set: function set(onMessage) {
self.onmessage = function (message) {
if (!isKnownMessage(message)) {

@@ -27,40 +19,60 @@ return;

const {
type,
payload
} = message.data;
var _message$data = message.data,
type = _message$data.type,
payload = _message$data.payload;
onMessage(type, payload);
};
}
}, {
key: "addEventListener",
value: function addEventListener(onMessage) {
var onMessageWrapper = onMessageWrapperMap.get(onMessage);
self.addEventListener('message', onMessageWrapper);
}
if (!onMessageWrapper) {
onMessageWrapper = function onMessageWrapper(message) {
if (!isKnownMessage(message)) {
return;
}
static removeEventListener(onMessage) {
const onMessageWrapper = onMessageWrapperMap.get(onMessage);
onMessageWrapperMap.delete(onMessage);
self.removeEventListener('message', onMessageWrapper);
}
var _message$data2 = message.data,
type = _message$data2.type,
payload = _message$data2.payload;
onMessage(type, payload);
};
}
static postMessage(type, payload) {
if (self) {
const data = {
source: 'loaders.gl',
type,
payload
};
const transferList = getTransferList(payload);
self.postMessage(data, transferList);
self.addEventListener('message', onMessageWrapper);
}
}
}, {
key: "removeEventListener",
value: function removeEventListener(onMessage) {
var onMessageWrapper = onMessageWrapperMap.get(onMessage);
onMessageWrapperMap.delete(onMessage);
self.removeEventListener('message', onMessageWrapper);
}
}, {
key: "postMessage",
value: function postMessage(type, payload) {
if (self) {
var data = {
source: 'loaders.gl',
type: type,
payload: payload
};
var transferList = getTransferList(payload);
self.postMessage(data, transferList);
}
}
}]);
}
return WorkerBody;
}();
export { WorkerBody as default };
function isKnownMessage(message) {
const {
type,
data
} = message;
var type = message.type,
data = message.data;
return type === 'message' && data && typeof data.source === 'string' && data.source.startsWith('loaders.gl');
}
//# sourceMappingURL=worker-body.js.map

@@ -0,27 +1,25 @@

import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
import { isMobile } from '../env-utils/globals';
import WorkerPool from './worker-pool';
import WorkerThread from './worker-thread';
const DEFAULT_PROPS = {
var DEFAULT_PROPS = {
maxConcurrency: 3,
maxMobileConcurrency: 1,
onDebug: () => {},
onDebug: function onDebug() {},
reuseWorkers: true
};
let _workerFarm = null;
export default class WorkerFarm {
static isSupported() {
return WorkerThread.isSupported();
}
var _workerFarm = null;
static getWorkerFarm(props = {}) {
_workerFarm = _workerFarm || new WorkerFarm({});
var WorkerFarm = function () {
function WorkerFarm(props) {
_classCallCheck(this, WorkerFarm);
_workerFarm.setProps(props);
return _workerFarm;
}
constructor(props) {
this.props = { ...DEFAULT_PROPS
};
this.props = _objectSpread({}, DEFAULT_PROPS);
this.setProps(props);

@@ -31,35 +29,57 @@ this.workerPools = new Map();

destroy() {
this.workerPools.forEach(workerPool => workerPool.destroy());
}
_createClass(WorkerFarm, [{
key: "destroy",
value: function destroy() {
this.workerPools.forEach(function (workerPool) {
return workerPool.destroy();
});
}
}, {
key: "setProps",
value: function setProps(props) {
this.props = _objectSpread(_objectSpread({}, this.props), props);
}
}, {
key: "getWorkerPool",
value: function getWorkerPool(_ref) {
var name = _ref.name,
source = _ref.source,
url = _ref.url;
var workerPool = this.workerPools.get(name);
setProps(props) {
this.props = { ...this.props,
...props
};
}
if (!workerPool) {
workerPool = new WorkerPool({
name: name,
source: source,
url: url,
maxConcurrency: isMobile ? this.props.maxMobileConcurrency : this.props.maxConcurrency,
onDebug: this.props.onDebug,
reuseWorkers: this.props.reuseWorkers
});
this.workerPools.set(name, workerPool);
}
getWorkerPool({
name,
source,
url
}) {
let workerPool = this.workerPools.get(name);
return workerPool;
}
}], [{
key: "isSupported",
value: function isSupported() {
return WorkerThread.isSupported();
}
}, {
key: "getWorkerFarm",
value: function getWorkerFarm() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_workerFarm = _workerFarm || new WorkerFarm({});
if (!workerPool) {
workerPool = new WorkerPool({
name,
source,
url,
maxConcurrency: isMobile ? this.props.maxMobileConcurrency : this.props.maxConcurrency,
onDebug: this.props.onDebug,
reuseWorkers: this.props.reuseWorkers
});
this.workerPools.set(name, workerPool);
_workerFarm.setProps(props);
return _workerFarm;
}
}]);
return workerPool;
}
return WorkerFarm;
}();
}
export { WorkerFarm as default };
//# sourceMappingURL=worker-farm.js.map

@@ -1,36 +0,51 @@

import assert from '../env-utils/assert';
export default class WorkerJob {
constructor(jobName, workerThread) {
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import { assert } from '../env-utils/assert';
var WorkerJob = function () {
function WorkerJob(jobName, workerThread) {
var _this = this;
_classCallCheck(this, WorkerJob);
this.name = jobName;
this.workerThread = workerThread;
this.isRunning = true;
this.result = new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
this.result = new Promise(function (resolve, reject) {
_this._resolve = resolve;
_this._reject = reject;
});
}
postMessage(type, payload) {
this.workerThread.postMessage({
source: 'loaders.gl',
type,
payload
});
}
_createClass(WorkerJob, [{
key: "postMessage",
value: function postMessage(type, payload) {
this.workerThread.postMessage({
source: 'loaders.gl',
type: type,
payload: payload
});
}
}, {
key: "done",
value: function done(value) {
assert(this.isRunning);
this.isRunning = false;
done(value) {
assert(this.isRunning);
this.isRunning = false;
this._resolve(value);
}
}, {
key: "error",
value: function error(_error) {
assert(this.isRunning);
this.isRunning = false;
this._resolve(value);
}
this._reject(_error);
}
}]);
error(error) {
assert(this.isRunning);
this.isRunning = false;
return WorkerJob;
}();
this._reject(error);
}
}
export { WorkerJob as default };
//# sourceMappingURL=worker-job.js.map

@@ -1,14 +0,25 @@

import assert from '../env-utils/assert';
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { assert } from '../env-utils/assert';
import WorkerThread from './worker-thread';
import WorkerJob from './worker-job';
export default class WorkerPool {
constructor({
source,
url,
name = 'unnamed',
maxConcurrency = 1,
onMessage,
onDebug = () => {},
reuseWorkers = true
}) {
var WorkerPool = function () {
function WorkerPool(_ref) {
var source = _ref.source,
url = _ref.url,
_ref$name = _ref.name,
name = _ref$name === void 0 ? 'unnamed' : _ref$name,
_ref$maxConcurrency = _ref.maxConcurrency,
maxConcurrency = _ref$maxConcurrency === void 0 ? 1 : _ref$maxConcurrency,
onMessage = _ref.onMessage,
_ref$onDebug = _ref.onDebug,
onDebug = _ref$onDebug === void 0 ? function () {} : _ref$onDebug,
_ref$reuseWorkers = _ref.reuseWorkers,
reuseWorkers = _ref$reuseWorkers === void 0 ? true : _ref$reuseWorkers;
_classCallCheck(this, WorkerPool);
assert(source || url);

@@ -28,93 +39,173 @@ this.source = source;

destroy() {
this.idleQueue.forEach(worker => worker.destroy());
this.isDestroyed = true;
}
async startJob(name, onMessage = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) {
const startPromise = new Promise(onStart => {
this.jobQueue.push({
name,
onMessage,
onError,
onStart
_createClass(WorkerPool, [{
key: "destroy",
value: function destroy() {
this.idleQueue.forEach(function (worker) {
return worker.destroy();
});
});
this.isDestroyed = true;
}
}, {
key: "startJob",
value: function () {
var _startJob = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(name) {
var _this = this;
this._startQueuedJob();
var onMessage,
onError,
startPromise,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
onMessage = _args.length > 1 && _args[1] !== undefined ? _args[1] : function (job, type, data) {
return job.done(data);
};
onError = _args.length > 2 && _args[2] !== undefined ? _args[2] : function (job, error) {
return job.error(error);
};
startPromise = new Promise(function (onStart) {
_this.jobQueue.push({
name: name,
onMessage: onMessage,
onError: onError,
onStart: onStart
});
});
return startPromise;
}
this._startQueuedJob();
async _startQueuedJob() {
if (!this.jobQueue.length) {
return;
}
return _context.abrupt("return", startPromise);
const workerThread = this._getAvailableWorker();
case 5:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
if (!workerThread) {
return;
}
function startJob(_x) {
return _startJob.apply(this, arguments);
}
const queuedJob = this.jobQueue.shift();
return startJob;
}()
}, {
key: "_startQueuedJob",
value: function () {
var _startQueuedJob2 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2() {
var workerThread, queuedJob, job;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (this.jobQueue.length) {
_context2.next = 2;
break;
}
if (queuedJob) {
this.onDebug({
message: 'Starting job',
jobName: queuedJob.name,
workerThread,
backlog: this.jobQueue.length
});
const job = new WorkerJob(queuedJob.name, workerThread);
return _context2.abrupt("return");
workerThread.onMessage = data => queuedJob.onMessage(job, data.type, data.payload);
case 2:
workerThread = this._getAvailableWorker();
workerThread.onError = error => queuedJob.onError(job, error);
if (workerThread) {
_context2.next = 5;
break;
}
queuedJob.onStart(job);
return _context2.abrupt("return");
try {
await job.result;
} finally {
this.returnWorkerToQueue(workerThread);
case 5:
queuedJob = this.jobQueue.shift();
if (!queuedJob) {
_context2.next = 18;
break;
}
this.onDebug({
message: 'Starting job',
jobName: queuedJob.name,
workerThread: workerThread,
backlog: this.jobQueue.length
});
job = new WorkerJob(queuedJob.name, workerThread);
workerThread.onMessage = function (data) {
return queuedJob.onMessage(job, data.type, data.payload);
};
workerThread.onError = function (error) {
return queuedJob.onError(job, error);
};
queuedJob.onStart(job);
_context2.prev = 12;
_context2.next = 15;
return job.result;
case 15:
_context2.prev = 15;
this.returnWorkerToQueue(workerThread);
return _context2.finish(15);
case 18:
case "end":
return _context2.stop();
}
}
}, _callee2, this, [[12,, 15, 18]]);
}));
function _startQueuedJob() {
return _startQueuedJob2.apply(this, arguments);
}
}
}
returnWorkerToQueue(worker) {
if (this.isDestroyed) {
worker.destroy();
return;
}
return _startQueuedJob;
}()
}, {
key: "returnWorkerToQueue",
value: function returnWorkerToQueue(worker) {
if (this.isDestroyed) {
worker.destroy();
return;
}
if (this.reuseWorkers) {
this.idleQueue.push(worker);
} else {
worker.destroy();
this.count--;
if (this.reuseWorkers) {
this.idleQueue.push(worker);
} else {
worker.destroy();
this.count--;
}
this._startQueuedJob();
}
}, {
key: "_getAvailableWorker",
value: function _getAvailableWorker() {
if (this.idleQueue.length > 0) {
return this.idleQueue.shift() || null;
}
this._startQueuedJob();
}
if (this.count < this.maxConcurrency) {
this.count++;
var name = "".concat(this.name.toLowerCase(), " (#").concat(this.count, " of ").concat(this.maxConcurrency, ")");
return new WorkerThread({
name: name,
source: this.source,
url: this.url
});
}
_getAvailableWorker() {
if (this.idleQueue.length > 0) {
return this.idleQueue.shift() || null;
return null;
}
}]);
if (this.count < this.maxConcurrency) {
this.count++;
const name = "".concat(this.name.toLowerCase(), " (#").concat(this.count, " of ").concat(this.maxConcurrency, ")");
return new WorkerThread({
name,
source: this.source,
url: this.url
});
}
return WorkerPool;
}();
return null;
}
}
export { WorkerPool as default };
//# sourceMappingURL=worker-pool.js.map

@@ -1,18 +0,16 @@

import assert from '../env-utils/assert';
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import { assert } from '../env-utils/assert';
import { buildWorkerURL } from './build-worker-url';
import { getTransferList } from './get-transfer-list';
const NOOP = _ => {};
var NOOP = function NOOP(_) {};
export default class WorkerThread {
static isSupported() {
return typeof Worker !== 'undefined';
}
var WorkerThread = function () {
function WorkerThread(props) {
_classCallCheck(this, WorkerThread);
constructor(props) {
const {
name,
source,
url
} = props;
var name = props.name,
source = props.source,
url = props.url;
assert(source || url);

@@ -24,3 +22,5 @@ this.name = name;

this.onError = error => console.log(error);
this.onError = function (error) {
return console.log(error);
};

@@ -31,61 +31,81 @@ this.terminated = false;

destroy() {
this.onMessage = NOOP;
this.onError = NOOP;
this.worker.terminate();
this.terminated = true;
}
_createClass(WorkerThread, [{
key: "destroy",
value: function destroy() {
this.onMessage = NOOP;
this.onError = NOOP;
this.worker.terminate();
this.terminated = true;
}
}, {
key: "isRunning",
get: function get() {
return Boolean(this.onMessage);
}
}, {
key: "postMessage",
value: function postMessage(data, transferList) {
transferList = transferList || getTransferList(data);
this.worker.postMessage(data, transferList);
}
}, {
key: "_getErrorFromErrorEvent",
value: function _getErrorFromErrorEvent(event) {
var message = "Failed to load ";
message += "worker ".concat(this.name, ". ");
get isRunning() {
return Boolean(this.onMessage);
}
if (event.message) {
message += "".concat(event.message, " in ");
}
postMessage(data, transferList) {
transferList = transferList || getTransferList(data);
this.worker.postMessage(data, transferList);
}
if (event.lineno) {
message += ":".concat(event.lineno, ":").concat(event.colno);
}
_getErrorFromErrorEvent(event) {
let message = "Failed to load ";
message += "worker ".concat(this.name, ". ");
if (event.message) {
message += "".concat(event.message, " in ");
return new Error(message);
}
}, {
key: "_createBrowserWorker",
value: function _createBrowserWorker() {
var _this = this;
if (event.lineno) {
message += ":".concat(event.lineno, ":").concat(event.colno);
}
this._loadableURL = buildWorkerURL({
source: this.source,
url: this.url
});
var worker = new Worker(this._loadableURL, {
name: this.name
});
return new Error(message);
}
worker.onmessage = function (event) {
if (!event.data) {
_this.onError('No data received');
} else {
_this.onMessage(event.data);
}
};
_createBrowserWorker() {
this._loadableURL = buildWorkerURL({
source: this.source,
url: this.url
});
const worker = new Worker(this._loadableURL, {
name: this.name
});
worker.onerror = function (error) {
_this.onError(_this._getErrorFromErrorEvent(error));
worker.onmessage = event => {
if (!event.data) {
this.onError('No data received');
} else {
this.onMessage(event.data);
}
};
_this.terminated = true;
};
worker.onerror = error => {
this.onError(this._getErrorFromErrorEvent(error));
this.terminated = true;
};
worker.onmessageerror = function (event) {
return console.error(event);
};
worker.onmessageerror = event => console.error(event);
return worker;
}
}], [{
key: "isSupported",
value: function isSupported() {
return typeof Worker !== 'undefined';
}
}]);
return worker;
}
return WorkerThread;
}();
}
export { WorkerThread as default };
//# sourceMappingURL=worker-thread.js.map

@@ -1,3 +0,3 @@

import assert from '../env-utils/assert';
const workerURLCache = new Map();
import { assert } from '../env-utils/assert';
var workerURLCache = new Map();
export function getWorkerURL(workerSource) {

@@ -10,6 +10,6 @@ assert(typeof workerSource === 'string', 'worker source');

let workerURL = workerURLCache.get(workerSource);
var workerURL = workerURLCache.get(workerSource);
if (!workerURL) {
const blob = new Blob([workerSource], {
var blob = new Blob([workerSource], {
type: 'application/javascript'

@@ -16,0 +16,0 @@ });

{
"name": "@loaders.gl/worker-utils",
"version": "3.0.0-alpha.7",
"version": "3.0.0-alpha.8",
"description": "Utilities for running tasks on worker threads",

@@ -29,2 +29,3 @@ "license": "MIT",

"child_process": false,
"fs": false,
"./src/lib/node/require-utils.node.js": false,

@@ -44,3 +45,3 @@ "./dist/es5/lib/node/require-utils.node.js": false,

},
"gitHead": "42832866753e5b62ff1a07112e6f6466f26365b0"
"gitHead": "057c51eb80062656da9c3e7d4bdb1254aab6cbfc"
}

@@ -8,12 +8,4 @@ import {WorkerObject} from './types';

// GENERAL UTILS
export {default as assert} from './lib/env-utils/assert';
export {
isBrowser,
isWorker,
nodeVersion,
self,
window,
global,
document
} from './lib/env-utils/globals';
export {assert} from './lib/env-utils/assert';
export {isBrowser, isWorker} from './lib/env-utils/globals';

@@ -20,0 +12,0 @@ // WORKER UTILS - TYPES

/** @typedef {import('./types').WorkerObject} WorkerObject */
// GENERAL UTILS
export {default as assert} from './lib/env-utils/assert';
export {
isBrowser,
isWorker,
nodeVersion,
self,
window,
global,
document
} from './lib/env-utils/globals';
export {assert} from './lib/env-utils/assert';
export {isBrowser, isWorker} from './lib/env-utils/globals';

@@ -15,0 +7,0 @@ // WORKER UTILS

/** Throws an `Error` with the optional `message` if `condition` is falsy */
export default function assert(condition: any, message?: string);
export function assert(condition: any, message?: string);
// 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
export default function assert(condition, message) {
export function assert(condition, message) {
if (!condition) {

@@ -6,0 +6,0 @@ throw new Error(message || 'loaders.gl assertion failed.');

/* global fetch, document */
import {global, isBrowser, isWorker} from '../env-utils/globals';
import * as node from '../node/require-utils.node';
import assert from '../env-utils/assert';
import {assert} from '../env-utils/assert';

@@ -6,0 +6,0 @@ // TODO - unpkg.com doesn't seem to have a `latest` specifier for alpha releases...

@@ -6,3 +6,3 @@ // Forked from probe.gl under MIT license, Copyright (c) 2015 - 2017 Uber Technologies, Inc.

import ChildProcess from 'child_process';
import assert from '../env-utils/assert';
import {assert} from '../env-utils/assert';
import {getAvailablePort} from './process-utils';

@@ -9,0 +9,0 @@

/** @typedef {import('../../types').WorkerObject} WorkerObject */
import assert from '../env-utils/assert';
import {assert} from '../env-utils/assert';

@@ -4,0 +4,0 @@ const NPM_TAG = 'beta'; // Change to 'latest' on release-branch

/* global URL, Blob */
import assert from '../env-utils/assert';
import {assert} from '../env-utils/assert';

@@ -4,0 +4,0 @@ const workerURLCache = new Map();

@@ -1,2 +0,2 @@

import assert from '../env-utils/assert';
import {assert} from '../env-utils/assert';

@@ -3,0 +3,0 @@ export default class WorkerJob {

/** @typedef {import('../worker-protocol/protocol').WorkerMessageType} WorkerMessageType */
/** @typedef {import('../worker-protocol/protocol').WorkerMessagePayload} WorkerMessagePayload */
import assert from '../env-utils/assert';
import {assert} from '../env-utils/assert';
import WorkerThread from './worker-thread';

@@ -5,0 +5,0 @@ import WorkerJob from './worker-job';

/* global Worker */
/** @typedef {import('./worker-thread').WorkerThreadProps} WorkerThreadProps */
import assert from '../env-utils/assert';
import {assert} from '../env-utils/assert';
import {buildWorkerURL} from './build-worker-url';

@@ -5,0 +5,0 @@ import {getTransferList} from './get-transfer-list';

/* global URL, Blob */
import assert from '../env-utils/assert';
import {assert} from '../env-utils/assert';

@@ -4,0 +4,0 @@ const workerURLCache = new Map();

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

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

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc