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.

auto.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. const http = require('http');
  3. const https = require('https');
  4. const resolveALPN = require('resolve-alpn');
  5. const QuickLRU = require('quick-lru');
  6. const Http2ClientRequest = require('./client-request');
  7. const calculateServerName = require('./utils/calculate-server-name');
  8. const urlToOptions = require('./utils/url-to-options');
  9. const cache = new QuickLRU({maxSize: 100});
  10. const queue = new Map();
  11. const installSocket = (agent, socket, options) => {
  12. socket._httpMessage = {shouldKeepAlive: true};
  13. const onFree = () => {
  14. agent.emit('free', socket, options);
  15. };
  16. socket.on('free', onFree);
  17. const onClose = () => {
  18. agent.removeSocket(socket, options);
  19. };
  20. socket.on('close', onClose);
  21. const onRemove = () => {
  22. agent.removeSocket(socket, options);
  23. socket.off('close', onClose);
  24. socket.off('free', onFree);
  25. socket.off('agentRemove', onRemove);
  26. };
  27. socket.on('agentRemove', onRemove);
  28. agent.emit('free', socket, options);
  29. };
  30. const resolveProtocol = async options => {
  31. const name = `${options.host}:${options.port}:${options.ALPNProtocols.sort()}`;
  32. if (!cache.has(name)) {
  33. if (queue.has(name)) {
  34. const result = await queue.get(name);
  35. return result.alpnProtocol;
  36. }
  37. const {path, agent} = options;
  38. options.path = options.socketPath;
  39. const resultPromise = resolveALPN(options);
  40. queue.set(name, resultPromise);
  41. try {
  42. const {socket, alpnProtocol} = await resultPromise;
  43. cache.set(name, alpnProtocol);
  44. options.path = path;
  45. if (alpnProtocol === 'h2') {
  46. // https://github.com/nodejs/node/issues/33343
  47. socket.destroy();
  48. } else {
  49. const {globalAgent} = https;
  50. const defaultCreateConnection = https.Agent.prototype.createConnection;
  51. if (agent) {
  52. if (agent.createConnection === defaultCreateConnection) {
  53. installSocket(agent, socket, options);
  54. } else {
  55. socket.destroy();
  56. }
  57. } else if (globalAgent.createConnection === defaultCreateConnection) {
  58. installSocket(globalAgent, socket, options);
  59. } else {
  60. socket.destroy();
  61. }
  62. }
  63. queue.delete(name);
  64. return alpnProtocol;
  65. } catch (error) {
  66. queue.delete(name);
  67. throw error;
  68. }
  69. }
  70. return cache.get(name);
  71. };
  72. module.exports = async (input, options, callback) => {
  73. if (typeof input === 'string' || input instanceof URL) {
  74. input = urlToOptions(new URL(input));
  75. }
  76. if (typeof options === 'function') {
  77. callback = options;
  78. options = undefined;
  79. }
  80. options = {
  81. ALPNProtocols: ['h2', 'http/1.1'],
  82. ...input,
  83. ...options,
  84. resolveSocket: true
  85. };
  86. if (!Array.isArray(options.ALPNProtocols) || options.ALPNProtocols.length === 0) {
  87. throw new Error('The `ALPNProtocols` option must be an Array with at least one entry');
  88. }
  89. options.protocol = options.protocol || 'https:';
  90. const isHttps = options.protocol === 'https:';
  91. options.host = options.hostname || options.host || 'localhost';
  92. options.session = options.tlsSession;
  93. options.servername = options.servername || calculateServerName(options);
  94. options.port = options.port || (isHttps ? 443 : 80);
  95. options._defaultAgent = isHttps ? https.globalAgent : http.globalAgent;
  96. const agents = options.agent;
  97. if (agents) {
  98. if (agents.addRequest) {
  99. throw new Error('The `options.agent` object can contain only `http`, `https` or `http2` properties');
  100. }
  101. options.agent = agents[isHttps ? 'https' : 'http'];
  102. }
  103. if (isHttps) {
  104. const protocol = await resolveProtocol(options);
  105. if (protocol === 'h2') {
  106. if (agents) {
  107. options.agent = agents.http2;
  108. }
  109. return new Http2ClientRequest(options, callback);
  110. }
  111. }
  112. return http.request(options, callback);
  113. };
  114. module.exports.protocolCache = cache;