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

puppeteer: safer args for page.eval and friends #32492

Conversation

bgschiller
Copy link
Contributor

page.evaluate and similar functions are currently typed as if
they accept 'any' args, but they will throw exceptions unless
the args are either Serializable (with JSON.stringify) or JSHandles.

In puppeteer/puppeteer#3591, @SimonSchick recommended that tighter
type definitions was a preferable way to address this rather than
runtime error checking.

I've been using these updated types in my project for the last couple of
months without problem.

I suspect that some of the X1, X2 type parameters should also
be changed to reflect that they can only be SerializableOrJSHandle,
but I wasn't sure how to change the existing UnwrapElementHandle
types.

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If changing an existing definition:

page.evaluate and similar functions are currently typed as if
they accept 'any' args, but they will throw exceptions unless
the args are either Serializable (with JSON.stringify) or JSHandles.

In puppeteer/puppeteer#3591, @SimonSchick recommended that tighter
type definitions was a preferable way to address this rather than
runtime error checking.

I've been using these updated types in my project for the last couple of
months without problem.

I suspect that some of the X1, X2 type parameters should also
be changed to reflect that they can only be SerializableOrJSHandle,
but I wasn't sure how to change the existing UnwrapElementHandle<X1>
types.
@typescript-bot typescript-bot added this to Waiting for Reviewers in Pull Request Status Board Jan 25, 2019
@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). Awaiting reviewer feedback labels Jan 25, 2019
@typescript-bot
Copy link
Contributor

typescript-bot commented Jan 25, 2019

@bgschiller Thank you for submitting this PR!

🔔 @marvinhagemeister @cdeutsch @ksm2 @SimonSchick - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@@ -106,7 +119,7 @@ export interface Evalable {
$eval<R>(
selector: string,
pageFunction: (element: Element, ...args: any[]) => R | Promise<R>,
Copy link
Contributor

Choose a reason for hiding this comment

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

args here should have value as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or rather, the same type but without JSHandle and Element instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I thought about that, but JSHandles are not only handles for Elements, right? I could pass a JSHandle to a Regex, and then my pageFunction would be given the regex itself. Is that right?

@typescript-bot typescript-bot moved this from Waiting for Reviewers to Review in Pull Request Status Board Jan 30, 2019
@typescript-bot typescript-bot added the Unmerged The author did not merge the PR when it was ready. label Jan 30, 2019
@typescript-bot
Copy link
Contributor

After 5 days, no one has reviewed the PR 😞. A maintainer will be reviewing the PR in the next few days and will either merge it or request revisions. Thank you for your patience!

export interface JSONObject {
[key: string]: Serializable;
}
export type SerializableOrJSHandle = Serializable | JSHandle;
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like you want to catch circular references but this actually won't do that. Here is an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Catching circular references would be great, but I'm not sure if the type system is actually powerful enough for that (aka, I don't know how to do it).

The goal of this Serializable is more to warn when an arg will not be correctly translated to JSON and back. For example, RegExp, Date, Function, etc.

The original impetus for this change was when I tried to pass an options object like { elem: JSHandle } as an arg. This contains a JSHandle, so I thought it would work. But it actually fails because it's neither Serializable nor a JSHandle.

More info on exactly why JSHandles are allowed, but not nested JSHandles is in the source near this edit: puppeteer/puppeteer#3591

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh ok I misread the linked github thread.

@PranavSenthilnathan
Copy link
Contributor

Congratulations on your first DefinitelyTyped contribution! Thank you for being a part of the community!

@PranavSenthilnathan PranavSenthilnathan merged commit 0ee3af6 into DefinitelyTyped:master Jan 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Popular package This PR affects a popular package (as counted by NPM download counts). Unmerged The author did not merge the PR when it was ready.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants