Skip to content

[WIP] Service worker support - #391

Open
johncoates-st wants to merge 3 commits into
rubycdp:mainfrom
johncoates-st:service-worker-support
Open

[WIP] Service worker support#391
johncoates-st wants to merge 3 commits into
rubycdp:mainfrom
johncoates-st:service-worker-support

Conversation

@johncoates-st

Copy link
Copy Markdown

Implementation of:

Hey @route, this is a follow up to our conversation as far as service worker support. I based this PR on your comment:

This is a first draft. I could use a bit more guidance, so I'll leave some notes across the changed files.

Comment threadlib/ferrum/page.rb
attr_reader :cookies

def initialize(target_id, browser, proxy: nil)
def initialize(target_id, browser, proxy: nil, type: "page")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Do you want me to use Page or create another class for workers?

Comment threadlib/ferrum/page.rb
@@ -355,13 +356,21 @@ def subscribe
end

def prepare_page

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Should I keep everything in this method or split out the worker preparation into a different method?

Comment threadlib/ferrum/target.rb
connection(**options)
end

def build_page(**options)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Should I just alias_method this to build?

Comment threadlib/ferrum/target.rb
sleep(NEW_WINDOW_WAIT) if window?
end

def connection(**options)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not sure what the proper naming would be for this if it can be both a page and a worker.

Comment threadlib/ferrum/target.rb

def page
@page ||= build_page
connection if page?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Initially I had this return nil if it was a worker because other parts of the code were causing failures otherwise, but not sure what the impact of this is.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Just an image I came up with on Figma, no particular attachment to it if you'd like something else. I tried a SVG but it didn't work with the createImageBitmap code.

let canvas = document.getElementById('canvas');
let context = canvas.getContext('2d');
let imageBitmap = event.data.data;
context.drawImage(imageBitmap, 0, 0);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Drawn to the canvas so the page isn't blank.

Comment threadspec/network_spec.rb
page.go_to("/ferrum/service_worker")

browser.network.wait_for_idle
traffic = browser.targets.values.map { _1.network.traffic }.flatten

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Should the worker bubble up its traffic to its parent page?

@route

Copy link
Copy Markdown
Member

I'm now on the road but I'll check your implementation this weekend, I hope by that time I'll be home.

@johncoates-st

Copy link
Copy Markdown
Author

Okay, sounds great!

@route

Copy link
Copy Markdown
Member

@johncoates-st let me first finish new headless mode for Ferrum, as there are architecture changes for the gem since frames don't work like they were. So we might need some changes here as well.

@johncoates-st

Copy link
Copy Markdown
Author

@route Okay, sounds good. What kind of timeline are you thinking for that new headless mode getting merged?

@route

Copy link
Copy Markdown
Member

@johncoates-st it's merged, so I'm fully yours ;) I'll start playing with it today.

@johncoates-st

Copy link
Copy Markdown
Author

@route Great! Looking forward to your feedback

@route

Copy link
Copy Markdown
Member

I think first of all we should address Target.setAutoAttach and change code to support Runtime.runIfWaitingForDebugger and sessions. Maybe even remove dedicated connection to page/target, but I currently have no idea how sessions work in Chrome with new flatten mode. There's little info about it, so we need to experiment. After that we can intro workers, doing it all at once seems a bit complicated.

@route

route commented Jan 6, 2024

Copy link
Copy Markdown
Member

The support for flatten mode and auto-attach landed to the main branch. Could you please rebase and now we can continue on service workers. To answer your questions I think it makes more sense not to derive from the Page class.

@routeroute added the needs feedback Needs feedback label Jan 6, 2024
@johncoates-st

Copy link
Copy Markdown
Author

@route That's great! Okay sounds good, I'll rebase.

@jpheos

Copy link
Copy Markdown

thanks for your work @johncoates-st
Any update of the merge ?

@mtomov

Copy link
Copy Markdown

Hi @johncoates-st, thanks for your work on this. Would you have time to look at the changes needed to get this out?

Thank you!

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs feedbackNeeds feedback

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@johncoates-st@route@jpheos@mtomov