mermaid-js / mermaid-cli

Command line tool for the Mermaid library
MIT License
2.2k stars 207 forks source link

Trying to use browserUrl or browserWSEndpoint without success #698

Closed zacharysyoung closed 2 weeks ago

zacharysyoung commented 3 weeks ago

Describe the bug I'd like to start Chromium and just have it running in the background and specify that puppeteer connect to that instance, to reduce startup time and increase processing speed.

I've tried supplying browserWSEndpoint, or the newer browserUrl, both from the ConnectOptions interface, but mmdc doesn't seem to respect them.

To Reproduce Steps to reproduce the behavior:

  1. I started the packaged Chromium:

    /Users/zyoung/.cache/puppeteer/chrome/mac-1108766/chrome-mac/Chromium.app/Contents/MacOS/Chromium \
    --headless \
    --disable-gpu \
    --remote-debugging-port=9222 \
    --devtools=false
    
    DevTools listening on ws://127.0.0.1:9222/devtools/browser/975ed025-0ffa-449b-8869-33146c680f8a
  2. I created puppeteer.json:
    {
      "browserUrl": "http://localhost:9222",
      "dumpio": true
    }

    or,

    {
      "browserWSEndpoint": "ws://127.0.0.1:9222/devtools/browser/975ed025-0ffa-449b-8869-33146c680f8a",
      "dumpio": true
    }
  3. I started mmdc with that puppeteer config file:
    mmdc -p puppeteer.json -i sd-v1.mmd -o sd-v1.svg
  4. I saw this debug message from puppeteer (dumpio:true):
    DevTools listening on ws://127.0.0.1:57837/devtools/browser/8dc9599f-fbd7-44b9-bc77-fc93077936d7

Expected behavior I'm not sure what I should see, but I believe seeing another instance of Chromium running on a different port indicates that mmdc or puppeteer doesn't know about my already-running instance.

aloisklink commented 3 weeks ago

Two possible things that might help you:

import {writeFile} from 'node:fs/promise';
import puppeteer from 'puppeteer'
import { renderMermaid } from '@mermaid-js/mermaid-cli';

// single instance of Chromium
const browser = await puppeteer.launch();

async function main() {
  while (true) {
    // run renderMermaid as many times as you want, they'll all share the same browser instance
    const { data } = await renderMermaid(browser, myMermaidCode, 'png');
    await writeFile(data, 'my-output-file.png');
  }
}

main();
zacharysyoung commented 2 weeks ago

@aloisklink, thank you very much for both recommendations. I tried both and ran into issues with each:

Either seems reconcilable, but at this point I'm happy to just accept the ~2s run time: it was more a matter of principle ("there's got to be a better/faster way!") than anything practical.

I really appreciate your help so far, but I don't need to pursue this any further. :)

I've also made my own just-SVG cli.