toaster.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Wrapper for the toaster (https://github.com/nels-o/toaster)
  3. */
  4. var path = require('path');
  5. var notifier = path.resolve(__dirname, '../vendor/snoreToast/snoretoast');
  6. var utils = require('../lib/utils');
  7. var Balloon = require('./balloon');
  8. var os = require('os');
  9. const { v4: uuid } = require('uuid');
  10. var EventEmitter = require('events').EventEmitter;
  11. var util = require('util');
  12. var fallback;
  13. const PIPE_NAME = 'notifierPipe';
  14. const PIPE_PATH_PREFIX = '\\\\.\\pipe\\';
  15. const PIPE_PATH_PREFIX_WSL = '/tmp/';
  16. module.exports = WindowsToaster;
  17. function WindowsToaster(options) {
  18. options = utils.clone(options || {});
  19. if (!(this instanceof WindowsToaster)) {
  20. return new WindowsToaster(options);
  21. }
  22. this.options = options;
  23. EventEmitter.call(this);
  24. }
  25. util.inherits(WindowsToaster, EventEmitter);
  26. function noop() {}
  27. function parseResult(data) {
  28. if (!data) {
  29. return {};
  30. }
  31. return data.split(';').reduce((acc, cur) => {
  32. const split = cur.split('=');
  33. if (split && split.length === 2) {
  34. acc[split[0]] = split[1];
  35. }
  36. return acc;
  37. }, {});
  38. }
  39. function getPipeName() {
  40. var pathPrefix = utils.isWSL() ? PIPE_PATH_PREFIX_WSL : PIPE_PATH_PREFIX;
  41. return `${pathPrefix}${PIPE_NAME}-${uuid()}`;
  42. }
  43. function notifyRaw(options, callback) {
  44. options = utils.clone(options || {});
  45. callback = callback || noop;
  46. var is64Bit = os.arch() === 'x64';
  47. var resultBuffer;
  48. const server = {
  49. namedPipe: getPipeName()
  50. };
  51. if (typeof options === 'string') {
  52. options = { title: 'node-notifier', message: options };
  53. }
  54. if (typeof callback !== 'function') {
  55. throw new TypeError(
  56. 'The second argument must be a function callback. You have passed ' +
  57. typeof fn
  58. );
  59. }
  60. var snoreToastResultParser = (err, callback) => {
  61. /* Possible exit statuses from SnoreToast, we only want to include err if it's -1 code
  62. Exit Status : Exit Code
  63. Failed : -1
  64. Success : 0
  65. Hidden : 1
  66. Dismissed : 2
  67. TimedOut : 3
  68. ButtonPressed : 4
  69. TextEntered : 5
  70. */
  71. const result = parseResult(
  72. resultBuffer && resultBuffer.toString('utf16le')
  73. );
  74. // parse action
  75. if (result.action === 'buttonClicked' && result.button) {
  76. result.activationType = result.button;
  77. } else if (result.action) {
  78. result.activationType = result.action;
  79. }
  80. if (err && err.code === -1) {
  81. callback(err, result);
  82. }
  83. callback(null, result);
  84. // https://github.com/mikaelbr/node-notifier/issues/334
  85. // Due to an issue with snoretoast not using stdio and pipe
  86. // when notifications are disabled, make sure named pipe server
  87. // is closed before exiting.
  88. server.instance && server.instance.close();
  89. };
  90. var actionJackedCallback = (err) =>
  91. snoreToastResultParser(
  92. err,
  93. utils.actionJackerDecorator(this, options, callback, (data) =>
  94. data === 'activate' ? 'click' : data || false
  95. )
  96. );
  97. options.title = options.title || 'Node Notification:';
  98. if (
  99. typeof options.message === 'undefined' &&
  100. typeof options.close === 'undefined'
  101. ) {
  102. callback(new Error('Message or ID to close is required.'));
  103. return this;
  104. }
  105. if (!utils.isWin8() && !utils.isWSL() && !!this.options.withFallback) {
  106. fallback = fallback || new Balloon(this.options);
  107. return fallback.notify(options, callback);
  108. }
  109. // Add pipeName option, to get the output
  110. utils.createNamedPipe(server).then((out) => {
  111. resultBuffer = out;
  112. options.pipeName = server.namedPipe;
  113. options = utils.mapToWin8(options);
  114. var argsList = utils.constructArgumentList(options, {
  115. explicitTrue: true,
  116. wrapper: '',
  117. keepNewlines: true,
  118. noEscape: true
  119. });
  120. var notifierWithArch = notifier + '-x' + (is64Bit ? '64' : '86') + '.exe';
  121. utils.fileCommand(
  122. this.options.customPath || notifierWithArch,
  123. argsList,
  124. actionJackedCallback
  125. );
  126. });
  127. return this;
  128. }
  129. Object.defineProperty(WindowsToaster.prototype, 'notify', {
  130. get: function () {
  131. if (!this._notify) this._notify = notifyRaw.bind(this);
  132. return this._notify;
  133. }
  134. });