Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. /* Drop this dependency once node.js 12 is required. */
  3. const fromEntries = require('fromentries');
  4. const state = getState(1);
  5. function getState(version) {
  6. const stateId = Symbol.for('process-on-spawn@*:singletonId');
  7. /* istanbul ignore next: cannot cover this once nyc depends on this module */
  8. if (stateId in global === false) {
  9. /* Hopefully version and unwrap forward compatibility is never actually needed */
  10. Object.defineProperty(global, stateId, {
  11. writable: true,
  12. value: {
  13. version,
  14. listeners: [],
  15. unwrap: wrapSpawnFunctions()
  16. }
  17. });
  18. }
  19. return global[stateId];
  20. }
  21. function wrappedSpawnFunction(fn) {
  22. return function (options) {
  23. let env = fromEntries(
  24. options.envPairs.map(nvp => nvp.split(/^([^=]*)=/).slice(1))
  25. );
  26. const opts = Object.create(null, {
  27. env: {
  28. enumerable: true,
  29. get() {
  30. return env;
  31. },
  32. set(value) {
  33. if (!value || typeof value !== 'object') {
  34. throw new TypeError('env must be an object');
  35. }
  36. env = value;
  37. }
  38. },
  39. cwd: {
  40. enumerable: true,
  41. get() {
  42. return options.cwd || process.cwd();
  43. }
  44. }
  45. });
  46. const args = [...options.args];
  47. Object.freeze(args);
  48. Object.assign(opts, {
  49. execPath: options.file,
  50. args,
  51. detached: Boolean(options.detached),
  52. uid: options.uid,
  53. gid: options.gid,
  54. windowsVerbatimArguments: Boolean(options.windowsVerbatimArguments),
  55. windowsHide: Boolean(options.windowsHide)
  56. });
  57. Object.freeze(opts);
  58. state.listeners.forEach(listener => {
  59. listener(opts);
  60. });
  61. options.envPairs = Object.entries(opts.env).map(([name, value]) => `${name}=${value}`);
  62. return fn.call(this, options);
  63. };
  64. }
  65. function wrapSpawnFunctions() {
  66. const {ChildProcess} = require('child_process');
  67. /* eslint-disable-next-line node/no-deprecated-api */
  68. const spawnSyncBinding = process.binding('spawn_sync');
  69. const originalSync = spawnSyncBinding.spawn;
  70. const originalAsync = ChildProcess.prototype.spawn;
  71. spawnSyncBinding.spawn = wrappedSpawnFunction(spawnSyncBinding.spawn);
  72. ChildProcess.prototype.spawn = wrappedSpawnFunction(ChildProcess.prototype.spawn);
  73. /* istanbul ignore next: forward compatibility code */
  74. return () => {
  75. spawnSyncBinding.spawn = originalSync;
  76. ChildProcess.prototype.spawn = originalAsync;
  77. };
  78. }
  79. module.exports = {
  80. addListener(listener) {
  81. state.listeners.push(listener);
  82. },
  83. prependListener(listener) {
  84. state.listeners.unshift(listener);
  85. },
  86. removeListener(listener) {
  87. const idx = state.listeners.indexOf(listener);
  88. if (idx !== -1) {
  89. state.listeners.splice(idx, 1);
  90. }
  91. },
  92. removeAllListeners() {
  93. state.listeners = [];
  94. }
  95. };