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

Connect to Puppeteer Endpoint, Update Puppeteer to 2.0.0 #19

Closed
wants to merge 12 commits into from

Conversation

qqilihq
Copy link

@qqilihq qqilihq commented Dec 1, 2019

This PR adds the possibility to connect to a Browserless endpoint using browserWSEndpoint as described in #9

  • regarding API, I basically followed the suggestion in the ticket
  • when running remotely, the SVG image data obviously needs to be base64 encoded, so I modified renderSvg in helpers.ts accordingly
  • refactored internal structure in index.ts to allow to keep the necessary state
  • update readme.md
  • also, I updated Puppeteer to 2.0.0 (required updating the test hashes -- I visually compared all the images and they looked identical)

Open questions:

I personally would prefer to use the puppeteer-core dependency in this library (and puppeteer only within devDependecies). This way, Chromium would not automatically be pulled. People who want to use an “embedded” Chromium could still achieve this by explicitly adding puppeteer to their dependencies. However this would be a breaking change.

With the current version, for people who do not want the “embedded” Chromium, there’s the PUPPETEER_SKIP_CHROMIUM_DOWNLOAD flag (see updated readme.md) which would need to be set before running installation via npm resp. yarn.

Kudos also go to @danielesser who helped testing this!

README.md Outdated Show resolved Hide resolved
- `options` <[Object]> Pupetteer [connect options](https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) object which will typically contain a `browserWSEndpoint` property.
- returns: <[SvgToImg]> an instance which is connected with the specified Puppeteer endpoint.

Use this method if you want to connect to a running Puppeteer endpoint (see [here](https://github.com/etienne-martin/svg-to-img/issues/9)). Note that the actual connection is established lazily; this means this method will always return, even when the given `browserWSEndpoint` cannot be reached -- in this case, errors will be thrown later when calling one of the `to` functions.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this portion of the doc is not up-to-date with the actual behavior.

errors will be thrown later when calling one of the to functions

Looking at the tests, it seams the connect method will throw if it can't connect to the websocket endpoint.

https://github.com/etienne-martin/svg-to-img/pull/19/files#diff-4ac74674fef9fbe68755b3bdcb9fbfd4R287

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the test case was not quite accurate (connect should be outside of try). I updated the test code to reflect this. The readme is actually correct.

dist/helpers.js Outdated
@@ -93,6 +93,6 @@ exports.renderSvg = async (svg, options) => {
img.addEventListener("load", onLoad);
img.addEventListener("error", onError);
document.body.appendChild(img);
img.src = "data:image/svg+xml;charset=utf8," + svg;
img.src = "data:image/svg+xml;base64," + btoa(svg);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the svg string needs to be base64 encoded because of the websocket endpoint. Should it just work the same regardless if you're connected to a local Chromium instance or a remote browser?

Copy link
Author

@qqilihq qqilihq Dec 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running via WebSocket I get the "Malformed SVG" error triggered in onError.

Admittedly, I have no real clue why this only happens when running remotely (my naive assumption would probably be that the WebSocket connection breaks something with the transmitted SVG string/function?). 🤔

I've added a code comment why the encoding is necessary.

src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
* Add `contributors` section to `package.json`
* Add global ignore rule for `max-classes-per-file`
* Add explicit `IConnectOptions` interface instead of using the Puppeteer default
* Tests: Move line of of `try` block to make clear when exception is thrown
* Add some more explanation to `readme.md`
* Remove temporary testing code
* Add comment why base64 encoding is necessary
@qqilihq
Copy link
Author

qqilihq commented Dec 2, 2019

@etienne-martin We're currently still investigating a potential issue with timeouts when running via WebSockets. Please wait before merging. Sorry!

Fixes issue with `btoa` and “The string to be encoded contains characters outside of the Latin1 range.” when encoding non-ASCII characters
@qqilihq
Copy link
Author

qqilihq commented Dec 2, 2019

@etienne-martin Okay, mentioned issue is fixed with previous commit (btoa required some additional treatment for encoding special chars).

@qqilihq
Copy link
Author

qqilihq commented Dec 9, 2019

@etienne-martin Any more feedback so far? :-)

@qqilihq
Copy link
Author

qqilihq commented Jan 10, 2020

@etienne-martin Are you still interested in integrating this? Else wise I’d push a forked version to NPM.

@etienne-martin
Copy link
Owner

Yes sorry I've been super busy lately!

@qqilihq
Copy link
Author

qqilihq commented Jan 10, 2020

No worries -- just wanted to double check. I’d really like to avoid having a fork on NPM, so I’ll be patient instead :-) Cheers!

@qqilihq qqilihq force-pushed the connect-puppeteer branch from 691e755 to ce367c1 Compare April 15, 2020 10:06
@qqilihq
Copy link
Author

qqilihq commented Apr 15, 2020

FYI: In case anyone needs this, we made this fork available on @nodepit/svg-to-img now.

@qqilihq qqilihq closed this Mar 19, 2022
@qqilihq qqilihq deleted the connect-puppeteer branch March 19, 2022 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants