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.

node-ical.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. const fs = require('fs');
  2. const fetch = require('node-fetch');
  3. const ical = require('./ical.js');
  4. /**
  5. * ICal event object.
  6. *
  7. * These two fields are always present:
  8. * - type
  9. * - params
  10. *
  11. * The rest of the fields may or may not be present depending on the input.
  12. * Do not assume any of these fields are valid and check them before using.
  13. * Most types are simply there as a general guide for IDEs and users.
  14. *
  15. * @typedef iCalEvent
  16. * @type {object}
  17. *
  18. * @property {string} type - Type of event.
  19. * @property {Array} params - Extra event parameters.
  20. *
  21. * @property {?object} start - When this event starts.
  22. * @property {?object} end - When this event ends.
  23. *
  24. * @property {?string} summary - Event summary string.
  25. * @property {?string} description - Event description.
  26. *
  27. * @property {?object} dtstamp - DTSTAMP field of this event.
  28. *
  29. * @property {?object} created - When this event was created.
  30. * @property {?object} lastmodified - When this event was last modified.
  31. *
  32. * @property {?string} uid - Unique event identifier.
  33. *
  34. * @property {?string} status - Event status.
  35. *
  36. * @property {?string} sequence - Event sequence.
  37. *
  38. * @property {?string} url - URL of this event.
  39. *
  40. * @property {?string} location - Where this event occurs.
  41. * @property {?{
  42. * lat: number, lon: number
  43. * }} geo - Lat/lon location of this event.
  44. *
  45. * @property {?Array.<string>} - Array of event catagories.
  46. */
  47. /**
  48. * Object containing iCal events.
  49. * @typedef {Object.<string, iCalEvent>} iCalData
  50. */
  51. /**
  52. * Callback for iCal parsing functions with error and iCal data as a JavaScript object.
  53. * @callback icsCallback
  54. * @param {Error} err
  55. * @param {iCalData} ics
  56. */
  57. /**
  58. * A Promise that is undefined if a compatible callback is passed.
  59. * @typedef {(Promise.<iCalData>|undefined)} optionalPromise
  60. */
  61. // utility to allow callbacks to be used for promises
  62. function promiseCallback(fn, cb) {
  63. const promise = new Promise(fn);
  64. if (!cb) {
  65. return promise;
  66. }
  67. promise
  68. .then(returnValue => {
  69. cb(null, returnValue);
  70. })
  71. .catch(error => {
  72. cb(error, null);
  73. });
  74. }
  75. // Sync functions
  76. const sync = {};
  77. // Async functions
  78. const async = {};
  79. // Auto-detect functions for backwards compatibility.
  80. const autodetect = {};
  81. /**
  82. * Download an iCal file from the web and parse it.
  83. *
  84. * @param {string} url - URL of file to request.
  85. * @param {Object|icsCallback} [opts] - Options to pass to fetch() from npm:node-fetch.
  86. * Alternatively you can pass the callback function directly.
  87. * If no callback is provided a promise will be returned.
  88. * @param {icsCallback} [cb] - Callback function.
  89. * If no callback is provided a promise will be returned.
  90. *
  91. * @returns {optionalPromise} Promise is returned if no callback is passed.
  92. */
  93. async.fromURL = function (url, options, cb) {
  94. return promiseCallback((resolve, reject) => {
  95. fetch(url, options)
  96. .then(response => {
  97. // If (response.status !== 200) {
  98. // all ok status codes should be accepted (any 2XX code)
  99. if (Math.floor(response.status / 100) !== 2) {
  100. reject(new Error(`${response.status} ${response.statusText}`));
  101. return;
  102. }
  103. return response;
  104. })
  105. .then(response => response.text())
  106. .then(data => {
  107. ical.parseICS(data, (error, ics) => {
  108. if (error) {
  109. reject(error);
  110. return;
  111. }
  112. resolve(ics);
  113. });
  114. })
  115. .catch(error => {
  116. reject(error);
  117. });
  118. }, cb);
  119. };
  120. /**
  121. * Load iCal data from a file and parse it.
  122. *
  123. * @param {string} filename - File path to load.
  124. * @param {icsCallback} [cb] - Callback function.
  125. * If no callback is provided a promise will be returned.
  126. *
  127. * @returns {optionalPromise} Promise is returned if no callback is passed.
  128. */
  129. async.parseFile = function (filename, cb) {
  130. return promiseCallback((resolve, reject) => {
  131. fs.readFile(filename, 'utf8', (error, data) => {
  132. if (error) {
  133. reject(error);
  134. return;
  135. }
  136. ical.parseICS(data, (error, ics) => {
  137. if (error) {
  138. reject(error);
  139. return;
  140. }
  141. resolve(ics);
  142. });
  143. });
  144. }, cb);
  145. };
  146. /**
  147. * Parse iCal data from a string.
  148. *
  149. * @param {string} data - String containing iCal data.
  150. * @param {icsCallback} [cb] - Callback function.
  151. * If no callback is provided a promise will be returned.
  152. *
  153. * @returns {optionalPromise} Promise is returned if no callback is passed.
  154. */
  155. async.parseICS = function (data, cb) {
  156. return promiseCallback((resolve, reject) => {
  157. ical.parseICS(data, (error, ics) => {
  158. if (error) {
  159. reject(error);
  160. return;
  161. }
  162. resolve(ics);
  163. });
  164. }, cb);
  165. };
  166. /**
  167. * Load iCal data from a file and parse it.
  168. *
  169. * @param {string} filename - File path to load.
  170. *
  171. * @returns {iCalData} Parsed iCal data.
  172. */
  173. sync.parseFile = function (filename) {
  174. const data = fs.readFileSync(filename, 'utf8');
  175. return ical.parseICS(data);
  176. };
  177. /**
  178. * Parse iCal data from a string.
  179. *
  180. * @param {string} data - String containing iCal data.
  181. *
  182. * @returns {iCalData} Parsed iCal data.
  183. */
  184. sync.parseICS = function (data) {
  185. return ical.parseICS(data);
  186. };
  187. /**
  188. * Load iCal data from a file and parse it.
  189. *
  190. * @param {string} filename - File path to load.
  191. * @param {icsCallback} [cb] - Callback function.
  192. * If no callback is provided this function runs synchronously.
  193. *
  194. * @returns {iCalData|undefined} Parsed iCal data or undefined if a callback is being used.
  195. */
  196. autodetect.parseFile = function (filename, cb) {
  197. if (!cb) {
  198. return sync.parseFile(filename);
  199. }
  200. async.parseFile(filename, cb);
  201. };
  202. /**
  203. * Parse iCal data from a string.
  204. *
  205. * @param {string} data - String containing iCal data.
  206. * @param {icsCallback} [cb] - Callback function.
  207. * If no callback is provided this function runs synchronously.
  208. *
  209. * @returns {iCalData|undefined} Parsed iCal data or undefined if a callback is being used.
  210. */
  211. autodetect.parseICS = function (data, cb) {
  212. if (!cb) {
  213. return sync.parseICS(data);
  214. }
  215. async.parseICS(data, cb);
  216. };
  217. // Export api functions
  218. module.exports = {
  219. // Autodetect
  220. fromURL: async.fromURL,
  221. parseFile: autodetect.parseFile,
  222. parseICS: autodetect.parseICS,
  223. // Sync
  224. sync,
  225. // Async
  226. async,
  227. // Other backwards compat things
  228. objectHandlers: ical.objectHandlers,
  229. handleObject: ical.handleObject,
  230. parseLines: ical.parseLines
  231. };