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