server.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var canPromise = require('./can-promise')
  2. var QRCode = require('./core/qrcode')
  3. var PngRenderer = require('./renderer/png')
  4. var Utf8Renderer = require('./renderer/utf8')
  5. var TerminalRenderer = require('./renderer/terminal')
  6. var SvgRenderer = require('./renderer/svg')
  7. function checkParams (text, opts, cb) {
  8. if (typeof text === 'undefined') {
  9. throw new Error('String required as first argument')
  10. }
  11. if (typeof cb === 'undefined') {
  12. cb = opts
  13. opts = {}
  14. }
  15. if (typeof cb !== 'function') {
  16. if (!canPromise()) {
  17. throw new Error('Callback required as last argument')
  18. } else {
  19. opts = cb || {}
  20. cb = null
  21. }
  22. }
  23. return {
  24. opts: opts,
  25. cb: cb
  26. }
  27. }
  28. function getTypeFromFilename (path) {
  29. return path.slice((path.lastIndexOf('.') - 1 >>> 0) + 2).toLowerCase()
  30. }
  31. function getRendererFromType (type) {
  32. switch (type) {
  33. case 'svg':
  34. return SvgRenderer
  35. case 'txt':
  36. case 'utf8':
  37. return Utf8Renderer
  38. case 'png':
  39. case 'image/png':
  40. default:
  41. return PngRenderer
  42. }
  43. }
  44. function getStringRendererFromType (type) {
  45. switch (type) {
  46. case 'svg':
  47. return SvgRenderer
  48. case 'terminal':
  49. return TerminalRenderer
  50. case 'utf8':
  51. default:
  52. return Utf8Renderer
  53. }
  54. }
  55. function render (renderFunc, text, params) {
  56. if (!params.cb) {
  57. return new Promise(function (resolve, reject) {
  58. try {
  59. var data = QRCode.create(text, params.opts)
  60. return renderFunc(data, params.opts, function (err, data) {
  61. return err ? reject(err) : resolve(data)
  62. })
  63. } catch (e) {
  64. reject(e)
  65. }
  66. })
  67. }
  68. try {
  69. var data = QRCode.create(text, params.opts)
  70. return renderFunc(data, params.opts, params.cb)
  71. } catch (e) {
  72. params.cb(e)
  73. }
  74. }
  75. exports.create = QRCode.create
  76. exports.toCanvas = require('./browser').toCanvas
  77. exports.toString = function toString (text, opts, cb) {
  78. var params = checkParams(text, opts, cb)
  79. var renderer = getStringRendererFromType(params.opts.type)
  80. return render(renderer.render, text, params)
  81. }
  82. exports.toDataURL = function toDataURL (text, opts, cb) {
  83. var params = checkParams(text, opts, cb)
  84. var renderer = getRendererFromType(params.opts.type)
  85. return render(renderer.renderToDataURL, text, params)
  86. }
  87. exports.toBuffer = function toBuffer (text, opts, cb) {
  88. var params = checkParams(text, opts, cb)
  89. var renderer = getRendererFromType(params.opts.type)
  90. return render(renderer.renderToBuffer, text, params)
  91. }
  92. exports.toFile = function toFile (path, text, opts, cb) {
  93. if (typeof path !== 'string' || !(typeof text === 'string' || typeof text === 'object')) {
  94. throw new Error('Invalid argument')
  95. }
  96. if ((arguments.length < 3) && !canPromise()) {
  97. throw new Error('Too few arguments provided')
  98. }
  99. var params = checkParams(text, opts, cb)
  100. var type = params.opts.type || getTypeFromFilename(path)
  101. var renderer = getRendererFromType(type)
  102. var renderToFile = renderer.renderToFile.bind(null, path)
  103. return render(renderToFile, text, params)
  104. }
  105. exports.toFileStream = function toFileStream (stream, text, opts) {
  106. if (arguments.length < 2) {
  107. throw new Error('Too few arguments provided')
  108. }
  109. var params = checkParams(text, opts, stream.emit.bind(stream, 'error'))
  110. var renderer = getRendererFromType('png') // Only png support for now
  111. var renderToFileStream = renderer.renderToFileStream.bind(null, stream)
  112. render(renderToFileStream, text, params)
  113. }