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

npm:puppeteer-core does not work with Node compat #18913

Closed
lucacasonato opened this issue Apr 29, 2023 · 1 comment · Fixed by #19412
Closed

npm:puppeteer-core does not work with Node compat #18913

lucacasonato opened this issue Apr 29, 2023 · 1 comment · Fixed by #19412
Assignees
Labels
bug Something isn't working correctly node compat

Comments

@lucacasonato
Copy link
Member

import { join } from "https://deno.land/[email protected]/path/mod.ts";
import { DenoDir } from "https://deno.land/x/[email protected]/deno_dir.ts";
import puppeteer, { BrowserFetcher } from "npm:[email protected]";
import { PUPPETEER_REVISIONS } from "npm:[email protected]/internal/revisions.js";

// Make sure Chromium is downloaded for use with Puppeteer
const denoDir = new DenoDir(undefined, true);
const fetcher = new BrowserFetcher({ path: join(denoDir.root, "puppeteer") });
const revisions = fetcher.localRevisions();
const revision = PUPPETEER_REVISIONS["chromium"];
let executablePath: string;
if (!revisions.includes(revision)) {
  console.log(`Downloading Chromium ${revision}...`);
  const r = await fetcher.download(revision);
  if (!r) throw new Error("Failed to download Chromium");
  executablePath = r.executablePath;
} else {
  executablePath = fetcher.revisionInfo(revision).executablePath;
}

// Start puppeteer
const browser = await puppeteer.launch({ headless: "new", executablePath });
const page = await browser.newPage();
await page.goto("https://example.com");
await page.screenshot({ path: "example.png" });
await browser.close();

This is the output:

$ deno run -A puppteer.ts 
Downloading Chromium 1108766...
error: Uncaught (in promise) ErrorEvent {
  [Symbol(kTarget)]: WebSocket {
    _events: {
      open: [Function: onOpen] {
        [Symbol(kIsForOnEventAttribute)]: false,
        [Symbol(kListener)]: [Function (anonymous)]
      },
      error: [Function: onError] {
        [Symbol(kIsForOnEventAttribute)]: false,
        [Symbol(kListener)]: [Function (anonymous)]
      }
    },
    _eventsCount: 2,
    _maxListeners: undefined,
    _binaryType: "nodebuffer",
    _closeCode: 1006,
    _closeFrameReceived: false,
    _closeFrameSent: false,
    _closeMessage: Buffer(0) [],
    _closeTimer: null,
    _extensions: {},
    _paused: false,
    _protocol: "",
    _readyState: 3,
    _receiver: null,
    _sender: null,
    _socket: null,
    _bufferedAmount: 0,
    _isServer: false,
    _redirects: 0,
    _url: "ws://127.0.0.1:35795/devtools/browser/05527a42-706d-40c8-8c68-5b45551254de",
    _originalIpc: false,
    _originalSecure: false,
    _originalHostOrSocketPath: "127.0.0.1:35795",
    _req: null,
    [Symbol(kCapture)]: false
  },
  [Symbol(kType)]: "error",
  [Symbol(kError)]: Error: Unexpected server response: 101
    at ClientRequest.<anonymous> (file:///home/lucacasonato/.cache/deno/npm/registry.npmjs.org/ws/8.13.0/lib/websocket.js:888:7)
    at ClientRequest.emit (ext:deno_node/_events.mjs:379:28)
    at ClientRequest._final (ext:deno_node/http.ts:175:14)
    at eventLoopTick (ext:core/01_core.js:166:11),
  [Symbol(kMessage)]: "Unexpected server response: 101"
}

No error should be thrown. It seems likely that this is related to outbound WebSockets in node compat. Do we even support those?

deno 1.33.1 (release, x86_64-unknown-linux-gnu)
@lucacasonato lucacasonato added bug Something isn't working correctly node compat labels Apr 29, 2023
@bartlomieju bartlomieju self-assigned this Apr 29, 2023
@bartlomieju
Copy link
Member

Did an investigation and we essentially need to write the whole HTTP client polyfill. We will be working on that in the coming days.

bartlomieju added a commit that referenced this issue Jun 13, 2023
This commit adds support for "upgrade" events in "node:http"
"ClientRequest". Currently only "Websocket" upgrades are
handled. Thanks to this change package like "npm:puppeteer"
and "npm:discord" should work.

Closes #18913
Closes #17847
bartlomieju added a commit that referenced this issue Jun 15, 2023
This commit adds support for "upgrade" events in "node:http"
"ClientRequest". Currently only "Websocket" upgrades are
handled. Thanks to this change package like "npm:puppeteer"
and "npm:discord" should work.

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

Successfully merging a pull request may close this issue.

2 participants