soldair / node-qrcode

qr code generator
MIT License
7.48k stars 624 forks source link

Provide renderToDataUrlSync #186

Open mykhalov opened 5 years ago

mykhalov commented 5 years ago

Rendering to canvas and exporting the data URL are both sync operations, it would be great to have a sync rendering function exported as well.

Currently, it can be achieved as follows:

import * as QRCode from 'qrcode'
import { renderToDataURL } from 'qrcode/lib/renderer/canvas'

function renderToDataURLSync(text, opts) {
  const data = QRCode.create(text, opts)
  return renderToDataURL(data, opts)
}
soldair commented 5 years ago

i would love to add this. would you like to make a pr?

mykhalov commented 5 years ago

Yes, I'll take it.

JounQin commented 5 years ago

@growtofill Any update on it?

soldair commented 5 years ago

this is slightly more complicated because it requires refactoring of how renderers are picked in https://github.com/soldair/node-qrcode/blob/master/lib/server.js#L67 and https://github.com/soldair/node-qrcode/blob/master/lib/browser.js

Then a sync method would have to be added to supported renderers like https://github.com/soldair/node-qrcode/blob/master/lib/renderer/png.js

this is mostly unwrapping things with unnecessary callbacks.

the only renderer that needed to have an async version was toFile which isn't applicable to the browser anyway.