question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add workaround for `Function.toString()` to docs

See original GitHub issue

Lots of libraries rely on Function.toString() to function, so I think it’s important to provide some sort of “hacky” solution to be able to use bytenode with the lib.

Ref: https://github.com/bytenode/bytenode/issues/93 Not only can you not use functions with puppeteer, but puppeteer now wraps all the string options (ex. waitForSelector("selector")) in a function, then calls .toString() on the wrapped function (or one that you’ve provided) before sending it to the browser

Ref: https://github.com/bytenode/bytenode/issues/34#issuecomment-807982226 Framework uses Function.toString() to check if Function is a class

As mentioned in https://github.com/bytenode/bytenode/issues/93#issuecomment-753661616 , a workaround could be just importing the function from a plain .js file, and while this will work, it may be a little unnecessary and annoying at times

A simple fix that I’d like to propose to get added to the documentation (or at least a reference to this issue) is the ability to override .toString() after bytenode breaks it

For example, to make a quick puppeteer fix:

const makeUnsafeFunction = (fnString) => {
    const fn = new Function(fnString);
    fn.toString = () => fnString;
    return fn
}

const unsafeWaitForSelector = (selector, opts) => {
    return page.waitForSelector(makeUnsafeFunction(`() => document.querySelector("${selector}")`), opts);
}

await unsafeWaitForSelector("#selector")

Or a fix for checking if a Function is a class:

class YourClass {}

YourClass.toString = () => "class YourClass"

This is in hopes to save people’s time figuring out a good solution (because I know it would’ve saved mine 😅)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
zhangriyuemingcommented, Nov 25, 2021

I think it could be relatively easy though considering how easy it is to parse and walk the js ast tree with modern packages Purely a guess so correct me if I’m wrong but if the cached code just relies on some offset/position from the source that it tries to lookup when calling Function.prototype.toString, it should be relatively easy to just move the function (or even just the necessary parts of it) to the position in the dummy code that v8 would look into

And to be honest, since this feature is ever so rarely actually needed, you could just get away with two comments, one that marks the start of the source that you want to be preserved and one at the end. Do some simple regex matching or even pure js splitting and move those parts to the dummy code

Check here already a solution.

1reaction
saucestealscommented, Nov 1, 2021

I think it could be relatively easy though considering how easy it is to parse and walk the js ast tree with modern packages Purely a guess so correct me if I’m wrong but if the cached code just relies on some offset/position from the source that it tries to lookup when calling Function.prototype.toString, it should be relatively easy to just move the function (or even just the necessary parts of it) to the position in the dummy code that v8 would look into

And to be honest, since this feature is ever so rarely actually needed, you could just get away with two comments, one that marks the start of the source that you want to be preserved and one at the end. Do some simple regex matching or even pure js splitting and move those parts to the dummy code

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function.prototype.toString() - JavaScript - MDN Web Docs
The toString() method returns a string representing the source code of the specified Function.
Read more >
Guid.ToString Method (System) - Microsoft Learn
Returns a string representation of the value of this Guid instance, according to the provided format specifier. ToString(). Returns a string representation of ......
Read more >
typescript-eslint/no-base-to-string.md at main - GitHub
This rule reports on stringified values that aren't primitives and don't define a more useful .toString() method. Note that Function provides its own...
Read more >
Magic Methods - Manual - PHP
Objects with __toString() passed within methods with type declarations(either from function arguments or return types) automagically converts it to string. <?
Read more >
Get data with Cloud Firestore - Firebase
Any of these methods can be used with documents, collections of documents, ... example shows how to retrieve the contents of a single...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found