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

Do not pollute global scope in cli #237

Closed
stereobooster opened this issue Jan 4, 2024 · 13 comments
Closed

Do not pollute global scope in cli #237

stereobooster opened this issue Jan 4, 2024 · 13 comments
Assignees
Labels
bug Something isn't working

Comments

@stereobooster
Copy link

According antv/g documentation it should be possible to avoid polluting global scope:

// setup the env for renderer
global.window = dom.window as any
global.document = document

PS. there are faster JSDOM alternatives, I wonder if it would have any impact on the performance:

@hikerpig
Copy link
Owner

hikerpig commented Jan 4, 2024

  1. You do have the point, I will try to solve this pollution issue.
  2. I assume the performance bottleneck is not in dom manipulation because we do not animate the dom or so. Text calculation / layout are more likely be the case.

@hikerpig
Copy link
Owner

hikerpig commented Jan 5, 2024

Sadly, those global assignment are required for all the things to work for antv g now, because it mainly targets to browser environment.
I will have to wrap all the thing and run them in a vm context to prevent polluting global scope.

@tex0l
Copy link

tex0l commented Feb 27, 2024

I had a hard time debugging why I got a JSDom error without using directly using JSDom, apparently it is because of the pollution caused by importing @pintora/cli into my project, and another package infers if they are in a browser or not by detecting if window is defined... which makes this package fail when @pintora/cli has been already executed...

Why do you need a global scope? The link @stereobooster referenced explains how to avoid using globals with '@antv/g', are you in an edge case where it does not work?

@hikerpig
Copy link
Owner

hikerpig commented Feb 27, 2024

@tex0l Accually the method @antv/g's documentation decribes is not enough for it to run inside Node.js enviroment - it depends on other globals that is only available in browser.
Currently I need to pollute at least as this snippet shows.

I think you problem might have something to do with the window pollution ?

  // setup the env for renderer
  global.window = dom.window as any
  global.document = document
  ;(dom.window as any).devicePixelRatio = devicePixelRatio
  ;(global as any).CanvasPattern = CanvasPattern

I was thinking about creating a vm inside pintora/cli or adjusting the documentation about how to import @pintora/cli inside a vm. I didn't have time back then for a proper solution.

@hikerpig
Copy link
Owner

@tex0l Sorry about wasting your time on this. I will need some time to think about the proper isolation solution to fix it all.
Right now I can only think of a quick fix to restore globals as it was before pintora render method is called. Most of the time it should be OK.

@hikerpig hikerpig self-assigned this Feb 27, 2024
@hikerpig hikerpig added the bug Something isn't working label Feb 27, 2024
@tex0l
Copy link

tex0l commented Feb 27, 2024

Don't worry, I'm sorry if my message was rude, that wasn't the intention, I just posted it so that people with a similar issue could maybe debug their issue using this insight.

I couldn't manage to use a VM (because I cannot import modules in a VM without polluting the global scope as well) nor a worker (because node canvas is not compatible with workers). Therefore I resorted to using a child_process.fork and passing the result through IPC encoded in base64 when it is a Buffer: tex0l/astro-pintora#5

EDIT: this solution does not work because of bundling issues.. back to square one.

@hikerpig
Copy link
Owner

hikerpig commented Feb 28, 2024

Right now I can only think of a quick fix to restore globals as it was before pintora render method is called. Most of the time it should be OK.

@tex0l Try updating to @pintora/[email protected] and see if it gets better.

@tex0l
Copy link

tex0l commented Feb 29, 2024

Hi @hikerpig ,

Thanks for the attempt, unfortunately it is flaky when multiple render runs are started in parallel.. I've added logs in the patcher methods so that you can understand why it still fails:

patching global window
patching global document
patching global CanvasPattern
patching global window
patching global document
patching global CanvasPattern
restoring globals
restoring globals

I'll implement a waterfall on my end to ensure no parallel runs can happen.. But that's a bit disappointing in terms of performance.

@tex0l
Copy link

tex0l commented Feb 29, 2024

With both fixes combined (yours to revert the globals and mine to waterfall calls), I managed to make my bug disappear.

I wouldn't consider this issue closed, as it technically still pollutes the global object briefly, and the function being async there is no guarantee that nothing is executing while the globals are still patched.

@tex0l
Copy link

tex0l commented Feb 29, 2024

ERRATUM: my bad, I ran into the issue where an execution of render happens at the same time as my other function which needs to have an unpolluted env... I am therefore forced to expose my waterfall function to make sure this function is never ran in parallel with the render function..

This is very hacky, but it technically works.

@hikerpig
Copy link
Owner

@tex0l Yes it's not an elegant solution, let's keep this issue open and tracked.

I will do some experiments on vm and child_process approach.

@hikerpig
Copy link
Owner

hikerpig commented Apr 1, 2024

I've come to a solution of running actual rendering in a forked subprocess #276.
@tex0l You may try this version @pintora/[email protected]

@hikerpig
Copy link
Owner

hikerpig commented Apr 9, 2024

Merged into @pintora/[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants