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

Allow to work in web workers #35

Merged
merged 3 commits into from
Jan 17, 2025
Merged

Allow to work in web workers #35

merged 3 commits into from
Jan 17, 2025

Conversation

my-lalex
Copy link
Contributor

@my-lalex my-lalex commented Jan 9, 2025

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

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@xuri xuri added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jan 16, 2025
Copy link
Owner

@xuri xuri left a 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

.gitignore Outdated Show resolved Hide resolved
@peng
Copy link
Collaborator

peng commented Jan 16, 2025

Suggest use globalThis.navigator to judge.

Copy link
Owner

@xuri xuri left a 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.

@xuri xuri merged commit 18e8a39 into xuri:main Jan 17, 2025
1 check passed
@xuri xuri added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants