Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore(worker-utils): Move Node require utils to polyfills #2700

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions modules/parquet/src/buffer-polyfill/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,6 @@ function writeDouble(buf: Buffer, value, offset, littleEndian, noAssert): number
}

// CUSTOM ERRORS
// =============

// Simplified versions from Node, changed for Buffer-only usage
const errors: Record<string, any> = {};
Expand Down Expand Up @@ -1991,7 +1990,6 @@ function addNumericalSeparator(val) {
}

// CHECK FUNCTIONS
// ===============

function checkBounds(buf, offset, byteLength) {
validateNumber(offset, 'offset');
Expand Down Expand Up @@ -2040,7 +2038,6 @@ function boundsError(value, length, type?) {
}

// HELPER FUNCTIONS
// ================

const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;

Expand Down
41 changes: 0 additions & 41 deletions modules/polyfills/src/images/encode-image.node.ts

This file was deleted.

53 changes: 0 additions & 53 deletions modules/polyfills/src/images/parse-image.node.ts

This file was deleted.

2 changes: 1 addition & 1 deletion modules/polyfills/test/load-library/require-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
return;
}

test.skip('polyfills#requireFromFile#', (t) => {
test('polyfills#requireFromFile#', (t) => {
t.ok(requireFromFile(MODULE_URL), 'Require from file worked');
t.ok(requireFromFile(SUBMODULE_URL), 'Require from file worked');
t.end();
Expand Down Expand Up @@ -57,7 +57,7 @@
prependPaths: ['prepend']
});

console.error(result);

Check warning on line 60 in modules/polyfills/test/load-library/require-utils.spec.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected console statement
t.ok(result);
t.equal(result.paths.indexOf('append'), result.paths.length - 1);
t.equal(result.paths.indexOf('prepend'), 0);
Expand Down
30 changes: 0 additions & 30 deletions modules/polyfills/wip/fetch-browser/fetch-file.browser.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions modules/worker-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,9 @@
],
"browser": {
"child_process": false,
"module": false,
"path": false,
"fs": false,
"./src/lib/node/worker_threads.ts": "./src/lib/node/worker_threads-browser.ts",
"./dist/es5/lib/node/worker_threads.js": "./dist/es5/lib/node/worker_threads-browser.js",
"./dist/esm/lib/node/worker_threads.js": "./dist/esm/lib/node/worker_threads-browser.js",
"./src/lib/node/require-utils.node.ts": false,
"./dist/es5/lib/node/require-utils.node.js": false,
"./dist/esm/lib/node/require-utils.node.js": false,
"./src/lib/process-utils/child-process-proxy.ts": false,
"./dist/es5/lib/process-utils/child-process-proxy.js": false,
"./dist/esm/lib/process-utils/child-process-proxy.js": false
Expand Down
15 changes: 0 additions & 15 deletions modules/worker-utils/src/lib/env-utils/globals.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
// Purpose: include this in your module to avoids adding dependencies on
// micro modules like 'global' and 'is-browser';

/* eslint-disable no-restricted-globals */
const globals = {
self: typeof self !== 'undefined' && self,
window: typeof window !== 'undefined' && window,
global: typeof global !== 'undefined' && global,
document: typeof document !== 'undefined' && document
};

const self_: {[key: string]: any} = globals.self || globals.window || globals.global || {};
const window_: {[key: string]: any} = globals.window || globals.self || globals.global || {};
const global_: {[key: string]: any} = globals.global || globals.self || globals.window || {};
const document_: {[key: string]: any} = globals.document || {};

export {self_ as self, window_ as window, global_ as global, document_ as document};

/** true if running in the browser, false if running in Node.js */
export const isBrowser: boolean =
// @ts-ignore process.browser
Expand Down
24 changes: 11 additions & 13 deletions modules/worker-utils/src/lib/library-utils/library-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* global importScripts */
import {isBrowser, isWorker} from '../env-utils/globals';
import * as node from '../node/require-utils.node';
import {assert} from '../env-utils/assert';
import {VERSION} from '../env-utils/version';

Expand Down Expand Up @@ -84,14 +83,10 @@ async function loadLibraryFromFile(libraryUrl: string): Promise<any> {
}

if (!isBrowser) {
// TODO - Node doesn't yet support dynamic import from https URLs
// try {
// return await import(libraryUrl);
// } catch (error) {
// console.error(error);
// }
try {
return node && node.requireFromFile && (await node.requireFromFile(libraryUrl));
// TODO - Node doesn't yet support dynamic import from https URLs
// return await import(libraryUrl);
return await globalThis.loaders?.requireFromFile?.(libraryUrl);
} catch (error) {
console.error(error); // eslint-disable-line no-console
return null;
Expand Down Expand Up @@ -129,7 +124,10 @@ async function loadScriptFromFile(libraryUrl) {
// we could create a`LibraryLoader` or`ModuleLoader`
function loadLibraryFromString(scriptSource: string, id: string): null | any {
if (!isBrowser) {
return node.requireFromString && node.requireFromString(scriptSource, id);
return (
globalThis.loaders?.requireFromString &&
globalThis.loaders?.requireFromString(scriptSource, id)
);
}

if (isWorker) {
Expand Down Expand Up @@ -168,11 +166,11 @@ function combineWorkerWithLibrary(worker, jsContent) {
*/

async function loadAsArrayBuffer(url: string): Promise<ArrayBuffer> {
if (!node.readFileAsArrayBuffer || url.startsWith('http')) {
if (!globalThis.loaders?.readFileAsArrayBuffer || url.startsWith('http')) {
const response = await fetch(url);
return await response.arrayBuffer();
}
return await node.readFileAsArrayBuffer(url);
return await globalThis.loaders?.readFileAsArrayBuffer(url);
}

/**
Expand All @@ -181,9 +179,9 @@ async function loadAsArrayBuffer(url: string): Promise<ArrayBuffer> {
* @returns
*/
async function loadAsText(url: string): Promise<string> {
if (!node.readFileAsText || url.startsWith('http')) {
if (!globalThis.loaders?.readFileAsText || url.startsWith('http')) {
const response = await fetch(url);
return await response.text();
}
return await node.readFileAsText(url);
return await globalThis.loaders?.readFileAsText(url);
}
100 changes: 0 additions & 100 deletions modules/worker-utils/src/lib/node/require-utils.node.ts

This file was deleted.

6 changes: 4 additions & 2 deletions modules/worker-utils/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// loaders.gl, MIT license

import './lib/async-queue/async-queue.spec';

// import './lib/library-utils/require-utils.spec';
import './lib/library-utils/library-utils.spec';
// TESTED by polyfills?
// import './lib/library-utils/library-utils.spec';

import './lib/worker-utils/get-transfer-list.spec';
import './lib/worker-utils/get-loadable-worker-url.spec';
Expand Down
Loading
Loading