index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) 2014, Ben Noordhuis <info@bnoordhuis.nl>
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. try {
  15. var addon = require('./build/Release/addon');
  16. } catch (e) {
  17. if (e.code !== 'MODULE_NOT_FOUND') throw e;
  18. var addon = require('./build/Debug/addon');
  19. }
  20. var kSignalFlag = addon.kSignalFlag;
  21. var flags = kSignalFlag;
  22. var options = (process.env.NODE_HEAPDUMP_OPTIONS || '').split(/\s*,\s*/);
  23. for (var i = 0, n = options.length; i < n; i += 1) {
  24. var option = options[i];
  25. if (option === '') continue;
  26. else if (option === 'signal') flags |= kSignalFlag;
  27. else if (option === 'nosignal') flags &= ~kSignalFlag;
  28. else console.error('node-heapdump: unrecognized option:', option);
  29. }
  30. addon.configure(flags);
  31. exports.writeSnapshot = function(filename, cb) {
  32. if (typeof filename === 'function') cb = filename, filename = undefined;
  33. filename = addon.writeSnapshot(filename);
  34. var success = !!filename;
  35. // Make the callback. Yes, this is synchronous; it wasn't back when heapdump
  36. // forked before writing the snapshot, but it is now. index.js can postpone
  37. // the callback with process.nextTick() or setImmediate() if synchronicity
  38. // becomes an issue. Or just remove it, it's pretty pointless now.
  39. if (cb) {
  40. if (success) cb(null, filename);
  41. else cb(new Error('heapdump write error'));
  42. }
  43. return success;
  44. };