-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Allow to work in web workers #35
Conversation
globalThis polyfills are enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change caused .mjs
file doesn't work, for example in the index.mjs
:
import { init } from 'excelize-wasm';
import fs from 'fs';
init('./node_modules/excelize-wasm/excelize.wasm.gz').then((excelize) => {
const f = excelize.NewFile();
[
[null, 'Apple', 'Orange', 'Pear'],
['Small', 2, 3, 3],
['Normal', 5, 2, 4],
['Large', 6, 7, 8],
].forEach((row, idx) => {
const ret1 = excelize.CoordinatesToCellName(1, idx + 1);
if (ret1.error) {
console.log(ret1.error);
return;
}
const res2 = f.SetSheetRow('Sheet1', ret1.cell, row);
if (res2.error) {
console.log(res2.error);
return;
}
});
const ret3 = f.AddChart('Sheet1', 'E1', {
Type: excelize.Col3DClustered,
Series: [
{
Name: 'Sheet1!$A$2',
Categories: 'Sheet1!$B$1:$D$1',
Values: 'Sheet1!$B$2:$D$2',
},
{
Name: 'Sheet1!$A$3',
Categories: 'Sheet1!$B$1:$D$1',
Values: 'Sheet1!$B$3:$D$3',
},
{
Name: 'Sheet1!$A$4',
Categories: 'Sheet1!$B$1:$D$1',
Values: 'Sheet1!$B$4:$D$4',
},
],
Title: [{
Text: 'Fruit 3D Clustered Column Chart',
}],
});
if (ret3.error) {
console.log(ret3.error);
return;
}
const { buffer, error } = f.WriteToBuffer();
if (error) {
console.log(error);
return;
}
fs.writeFile('Book1.xlsx', buffer, 'binary', (error) => {
if (error) {
console.log(error);
}
});
});
Run with node:
% node index.mjs
node:internal/deps/undici/undici:13392
Error.captureStackTrace(err);
^
TypeError: Failed to parse URL from ./node_modules/excelize-wasm/excelize.wasm.gz
at node:internal/deps/undici/undici:13392:13
at async exports.init (~/github.com/xuri/excelize-wasm/dist/main.cjs:1:54548) {
[cause]: TypeError: Invalid URL
at new URL (node:internal/url:806:29)
at new Request (node:internal/deps/undici/undici:9474:25)
at fetch (node:internal/deps/undici/undici:10203:25)
at fetch (node:internal/deps/undici/undici:13390:10)
at fetch (node:internal/bootstrap/web/exposed-window-or-worker:72:12)
at exports.init (~/github.com/xuri/excelize-wasm/dist/main.cjs:1:54554)
at file://~/github.com/xuri/excelize-wasm-demo/node/index.mjs:4:1
at ModuleJob.run (node:internal/modules/esm/module_job:234:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:473:24)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:122:5) {
code: 'ERR_INVALID_URL',
input: './node_modules/excelize-wasm/excelize.wasm.gz'
}
}
Node.js v20.18.1
Suggest use globalThis.navigator to judge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made a change based on your branch, using globalThis
instead of all global
and window
. This patch will be released in the next version.
PR Details
Description
Previous version was using typeof window === 'undefined' to test if the running environment is Node or browser, but the window object doesn't exists in web worker env, which is in browser anyway...
This PR use globalThis to test if polyfills needs to be set
Related Issue
#34
Motivation and Context
How Has This Been Tested
Quick test in browser env with web worker
Types of changes
Checklist