Repository navigation
Expand file tree
/
Copy pathical.js
More file actions
592 lines (520 loc) · 19.2 KB
/
Copy pathical.js
File metadata and controls
592 lines (520 loc) · 19.2 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/* eslint-disable max-params */
import {randomUUID} from 'node:crypto';
import {RRuleTemporal} from 'rrule-temporal';
import {toText as toTextFunction} from 'rrule-temporal/totext';
import {getDateKey, localDate} from './lib/date-utils.js';
import {
PARAMETER_VALUE,
parseValue,
finalizeEndedComponent,
createTemporalRule,
normalizeRruleValue,
storeParameter,
typeParameter,
addTZFactory,
createDateParameterFactory,
createComponentParameterHandlers,
createExdateParameterFactory,
} from './lib/ical-parser-utils.js';
import {Temporal} from './lib/temporal.js';
import tzUtil from './lib/tz-utils.js';
import {storeRecurrenceOverride} from './lib/uid-revision-merge.js';
/**
* Wrapper class to convert RRuleTemporal (Temporal.ZonedDateTime) to Date objects
* This maintains backward compatibility while using rrule-temporal internally
*/
class RRuleCompatWrapper {
static #temporalToDate(value) {
if (value === undefined || value === null) {
return value;
}
if (Array.isArray(value)) {
return value.map(item => RRuleCompatWrapper.#temporalToDate(item));
}
// Convert known Temporal instances to Date
return typeof value === 'object' && !(value instanceof Date) && typeof value.epochMilliseconds === 'number' ? new Date(value.epochMilliseconds) : value;
}
constructor(rruleTemporal, dateOnly = false) {
this._rrule = rruleTemporal;
// VALUE=DATE events are anchored to UTC midnight in rrule-temporal.
// Converting via epochMilliseconds shifts the date backwards in timezones
// west of UTC; instead we use the ZonedDateTime calendar components directly.
this._dateOnly = dateOnly;
}
#serializeOptions() {
const raw = this._rrule.options();
const converted = {};
for (const [key, value] of Object.entries(raw)) {
converted[key] = RRuleCompatWrapper.#temporalToDate(value);
}
// Map rrule-temporal `byDay` to legacy `byweekday`
if (converted.byweekday === undefined && raw.byDay !== undefined) {
converted.byweekday = RRuleCompatWrapper.#temporalToDate(raw.byDay);
}
return converted;
}
// Convert a ZonedDateTime to a JS Date.
// For VALUE=DATE events the ZDT calendar components (year/month/day in UTC)
// represent the intended calendar date; create a local-midnight Date so that
// .toDateString() returns the correct day regardless of the host timezone.
// Mark the result with dateOnly=true so that downstream helpers that
// distinguish date-only from timed dates (e.g. createLocalDateFromUTC) also
// use local getters rather than UTC getters.
#zdtToDate(zdt) {
if (this._dateOnly) {
const d = localDate(zdt.year, zdt.month - 1, zdt.day);
d.dateOnly = true;
return d;
}
return new Date(zdt.epochMilliseconds);
}
between(after, before, inclusive = false) {
const results = this._rrule.between(after, before, inclusive);
return results.map(zdt => this.#zdtToDate(zdt));
}
all(iterator) {
// If the caller supplied an iterator, wrap it so it receives a converted Date
// rather than a raw Temporal.ZonedDateTime — keeping the public API consistent
// with between() and matching the declared return type.
const wrappedIterator = iterator
? (zdt, index) => iterator(this.#zdtToDate(zdt), index)
: undefined;
const results = this._rrule.all(wrappedIterator);
return results.map(zdt => this.#zdtToDate(zdt));
}
before(date, inclusive = false) {
const result = this._rrule.previous(date, inclusive);
return result ? this.#zdtToDate(result) : undefined;
}
after(date, inclusive = false) {
const result = this._rrule.next(date, inclusive);
return result ? this.#zdtToDate(result) : undefined;
}
toText(locale) {
return toTextFunction(this._rrule, locale);
}
// Delegate other methods
toString() {
return this._rrule.toString();
}
// Expose options as a property for compatibility with the old rrule.js API
// (the wrapper hides the underlying method-based interface)
get options() {
return this.#serializeOptions();
}
// OrigOptions: the original options as passed to the constructor (before processing).
// In rrule.js, this was used for toString() and clone() operations.
// For rrule-temporal, options() already returns the unprocessed original options,
// so origOptions and options are equivalent.
get origOptions() {
return this.#serializeOptions();
}
}
/**
* A tolerant, minimal icalendar parser
* (https://tools.ietf.org/html/rfc5545)
*
* <peterbraden@peterbraden.co.uk>
*/
const addTZ = addTZFactory(tzUtil.attachTz);
const dateParameter = createDateParameterFactory({
addTZ,
tzUtil,
});
const {
geoParameter,
categoriesParameter,
recurrenceParameter,
freebusyParameter,
} = createComponentParameterHandlers({
dateParameter,
utcAdd: tzUtil.utcAdd,
});
const exdateParameter = createExdateParameterFactory({
dateParameter,
getDateKey,
});
// EXDATE is an entry that represents exceptions to a recurrence rule (ex: "repeat every day except on 7/4").
// The EXDATE entry itself can also contain a comma-separated list, so we parse each date separately.
// Multiple EXDATE entries can exist in a calendar record.
//
// Storage strategy (RFC 5545 compliant):
// We create an object with the exception dates as keys and Date objects as values.
// - For VALUE=DATE (date-only): key is "YYYY-MM-DD"
// - For DATE-TIME: BOTH "YYYY-MM-DD" AND full ISO string keys are created
//
// This dual-key approach provides:
// 1. Backward compatibility: date-only lookups continue to work
// 2. Precision matching: events recurring multiple times per day can exclude specific instances
// 3. RFC 5545 compliance: supports both DATE and DATE-TIME exclusions
//
// Usage examples:
// if (event.exdate?.['2024-01-15']) { ... } // Check if any instance on this day is excluded
// if (event.exdate?.['2024-01-15T14:00:00.000Z']) { ... } // Check specific time instance
//
// NOTE: We intentionally use date-based keys as the primary lookup because:
// 1. Floating times (without timezone) would create inconsistent ISO strings
// 2. DST transitions can affect exact time matching
// 3. Real-world calendar data often has mismatched times between RRULE and EXDATE
// Default batch size for async parsing to prevent event loop blocking
const PARSE_BATCH_SIZE = 2000;
// A content line: name, optional ;parameters and the value after the first unquoted colon
const CONTENT_LINE = new RegExp(String.raw`^((?:\w|-)+)((?:;(?:\w|-)+=${PARAMETER_VALUE})*):(.*)$`, 'v');
// One ;name=value parameter of a matched content line
const PARAMETER = new RegExp(String.raw`;((?:\w|-)+=${PARAMETER_VALUE})`, 'gv');
/**
* Yield the lines of an iCalendar string one at a time, so parsing does not hold an array
* of all lines next to the string. Lines end at LF, and a CR right before it belongs to the
* line ending, as with `string.split(/\r?\n/)`.
* @param {string} string - Raw iCalendar data
* @yields {string} One line without its line ending
*/
function * splitLines(string) {
let start = 0;
let end = string.indexOf('\n');
while (end !== -1) {
const lineEnd = end > start && string[end - 1] === '\r' ? end - 1 : end;
yield string.slice(start, lineEnd);
start = end + 1;
end = string.indexOf('\n', start);
}
yield string.slice(start);
}
/**
* Yield the entries of a line array from a start index on.
* @param {string[]} lines
* @param {number} startIndex
* @yields {string} One line
*/
function * linesFrom(lines, startIndex) {
for (let i = startIndex; i < lines.length; i++) {
yield lines[i];
}
}
/**
* Join folded lines (RFC 5545 §3.1): a line starting with a space or tab continues the
* previous one, without that first character.
* @param {Iterable<string>} lines - Raw lines
* @yields {string} One unfolded content line
*/
function * unfoldLines(lines) {
let current;
for (const line of lines) {
if (current !== undefined && (line[0] === ' ' || line[0] === '\t')) {
current += line.slice(1);
continue;
}
if (current !== undefined) {
yield current;
}
current = line;
}
if (current !== undefined) {
yield current;
}
}
// A TZID parameter with a quoted value
const QUOTED_TZID = /^TZID="[^"]*"$/v;
/**
* Parse one unfolded content line into the current component.
* @param {object} parser - The ical object, for its handleObject()
* @param {string} line - Unfolded content line
* @param {object} ctx - Current component
* @param {Array} stack - Parent components
* @returns {object} The component to continue with
*/
function parseContentLine(parser, line, ctx, stack) {
// Remove the double quotes around a TZID value and in the value of a line with a TZID,
// except around "(UTC+01:00) ...". Other quoted parameters keep their quotes, so a colon
// inside them does not end the parameters. A line that does not parse this way loses
// all its quotes, as before.
if (line.includes('TZID=') && !line.includes('"(')) {
const tzidMatch = CONTENT_LINE.exec(line);
if (tzidMatch) {
const [, name, parameterString, value] = tzidMatch;
const parameters = parameterString.replaceAll(PARAMETER, (parameter, nameValue) => (
QUOTED_TZID.test(nameValue) ? `;${nameValue.replaceAll('"', '')}` : parameter
));
line = `${name}${parameters}:${value.replaceAll('"', '')}`;
} else {
line = line.replaceAll('"', '');
}
}
const match = CONTENT_LINE.exec(line);
if (match === null) {
// Invalid line - must have k&v
return ctx;
}
const [, name, parameterString, value] = match;
const parameters = Array.from(parameterString.matchAll(PARAMETER), parameter => parameter[1]);
return parser.handleObject(name, value, parameters, ctx, stack, line) || {};
}
function finishParsing(ctx) {
delete ctx.type;
delete ctx.params;
return ctx;
}
/**
* Parse content lines in one go.
* @param {object} parser - The ical object
* @param {Iterable<string>} contentLines - Unfolded content lines
* @param {object} ctx - Starting component
* @param {Array} stack - Parent components
* @returns {object} Parsed calendar data
*/
function parseContentLines(parser, contentLines, ctx, stack) {
for (const line of contentLines) {
ctx = parseContentLine(parser, line, ctx, stack);
}
return finishParsing(ctx);
}
/**
* Parse content lines in batches, yielding to the event loop between batches so a large
* calendar does not block it. Errors reject the returned promise.
* @param {object} parser - The ical object
* @param {Iterable<string>} contentLines - Unfolded content lines
* @param {number} batchSize - Content lines per batch
* @param {object} ctx - Starting component
* @param {Array} stack - Parent components
* @returns {Promise<object>} Parsed calendar data
*/
async function parseContentLinesBatched(parser, contentLines, batchSize, ctx, stack) {
let linesInBatch = 0;
for (const line of contentLines) {
if (linesInBatch === batchSize) {
linesInBatch = 0;
// eslint-disable-next-line no-await-in-loop -- yielding between batches is the point
await new Promise(resolve => {
setImmediate(resolve);
});
}
ctx = parseContentLine(parser, line, ctx, stack);
linesInBatch++;
}
return finishParsing(ctx);
}
/**
* Report a parse result to a node-style callback: cb(null, data) or cb(error, {}).
* The callback runs in its own microtask, so it is called exactly once and an error it
* throws surfaces as an uncaught exception instead of a rejected promise.
* @param {Promise<object>} parsing
* @param {icsCallback} cb
* @returns {void}
*/
function settleCallback(parsing, cb) {
// The handlers only schedule the callback and cannot throw, so .catch() never sees
// an error from the success path
parsing
.then(result => {
queueMicrotask(() => {
cb(null, result);
});
})
.catch(error => {
queueMicrotask(() => {
cb(error, {});
});
});
}
const ical = {
objectHandlers: {
BEGIN(component, parameters, curr, stack) {
stack.push(curr);
return {type: component};
},
END(value, parameters, curr, stack) {
// Recurrence rules are only valid for VEVENT, VTODO, and VJOURNAL.
// More specifically, we need to filter the VCALENDAR type because we might end up with a defined rrule
// due to the subtypes.
if (['VEVENT', 'VTODO', 'VJOURNAL'].includes(value) && curr.rrule) {
const rule = normalizeRruleValue(curr.rrule);
if (curr.start) {
const temporalRule = createTemporalRule(curr, rule, {
RRuleTemporal,
RRuleCompatWrapper,
Temporal,
tzUtil,
});
if (temporalRule) {
curr.rrule = temporalRule;
} else {
delete curr.rrule;
}
}
}
return finalizeEndedComponent(value, curr, stack, {
storeRecurrenceOverride,
randomIdFactory: randomUUID,
utcAdd: tzUtil.utcAdd,
findLaterOccurrence: tzUtil.findLaterOccurrence,
});
},
SUMMARY: storeParameter('summary'),
DESCRIPTION: storeParameter('description'),
URL: storeParameter('url'),
UID: storeParameter('uid'),
LOCATION: storeParameter('location'),
DTSTART(value, parameters, curr, stack, line) {
// If already defined, this is a duplicate for this event
if (curr.start === undefined) {
curr = dateParameter('start')(value, parameters, curr, stack);
return typeParameter('datetype')(value, parameters, curr);
}
throw new Error('duplicate DTSTART encountered, line=' + line);
},
DTEND(value, parameters, curr, stack, line) {
// If already defined, this is a duplicate for this event
if (curr.end === undefined) {
return dateParameter('end')(value, parameters, curr, stack);
}
throw new Error('duplicate DTEND encountered, line=' + line);
},
DUE(value, parameters, curr, stack, line) {
// If already defined, this is a duplicate for this event
if (curr.due === undefined) {
return dateParameter('due')(value, parameters, curr, stack);
}
throw new Error('duplicate DUE encountered, line=' + line);
},
EXDATE: exdateParameter('exdate'),
CLASS: storeParameter('class'),
TRANSP: storeParameter('transparency'),
GEO: geoParameter('geo'),
'PERCENT-COMPLETE': storeParameter('completion'),
COMPLETED: dateParameter('completed'),
CATEGORIES: categoriesParameter('categories'),
FREEBUSY: freebusyParameter('freebusy'),
DTSTAMP: dateParameter('dtstamp'),
CREATED: dateParameter('created'),
'LAST-MODIFIED': dateParameter('lastmodified'),
'RECURRENCE-ID': recurrenceParameter('recurrenceid'),
SEQUENCE(value, parameters, curr) {
const sequence = Number(value);
curr.sequence = Number.isSafeInteger(sequence) ? sequence : parseValue(value);
return curr;
},
RRULE(value, parameters, curr, stack, line) {
curr.rrule = line;
return curr;
},
},
handleObject(name, value, parameters, ctx, stack, line) {
if (Object.hasOwn(this.objectHandlers, name)) {
return this.objectHandlers[name](value, parameters, ctx, stack, line);
}
// Handling custom properties
if (/X-(?:\w|-)+/v.test(name) && stack.length > 0) {
// Trimming the leading and perform storeParam
name = name.slice(2);
return storeParameter(name)(value, parameters, ctx, stack, line);
}
return storeParameter(name.toLowerCase())(value, parameters, ctx);
},
/**
* Parse iCalendar lines into a structured object.
* Supports both sync and async (batched) modes.
*
* @param {string[]} lines - Array of iCalendar lines
* @param {number} [batchSize=0] - Lines per batch (0=sync mode, >0=async batching)
* @param {object} [ctx] - Context object (internal, created if not provided)
* @param {Array} [stack] - Parser stack for nested components (internal)
* @param {number} [startIndex=0] - Position in lines array to start at (internal)
* @param {icsCallback} [cb] - Callback for async mode: cb(error, data)
* @returns {object | undefined} Parsed calendar data (sync mode), undefined (async mode with callback)
*
* @example
* // Sync mode (no batching)
* const data = parseLines(lines);
*
* @example
* // Async mode (with batching)
* parseLines(lines, 2000, undefined, undefined, 0, (err, data) => { ... });
*/
parseLines(lines, batchSize = 0, ctx, stack, startIndex = 0, cb) {
ctx ||= {};
stack ||= [];
const contentLines = unfoldLines(linesFrom(lines, startIndex));
if (batchSize > 0) {
const parsing = parseContentLinesBatched(this, contentLines, batchSize, ctx, stack);
if (cb) {
settleCallback(parsing, cb);
} else {
// Without a callback nobody receives the result; still surface an error
parsing.catch(error => {
queueMicrotask(() => {
throw error;
});
});
}
return;
}
let result;
try {
result = parseContentLines(this, contentLines, ctx, stack);
} catch (error) {
if (!cb) {
throw error;
}
// Call the callback outside the try block so an error it throws is not reported to it
cb(error, {});
return;
}
if (cb) {
cb(null, result);
return;
}
return result;
},
/**
* Parse an iCalendar string.
* Lines are read from the string one at a time instead of splitting it into an array first.
*
* @param {string} string - Raw iCalendar data (ICS format)
* @param {icsCallback} [cb] - Optional callback for async mode: cb(error, data)
* @returns {object | undefined} Parsed calendar data (sync) or undefined (async)
*
* @example
* // Synchronous parsing
* const data = ical.parseICS(icsString);
*
* @example
* // Asynchronous parsing with callback
* ical.parseICS(icsString, (err, data) => {
* if (err) console.error(err);
* else console.log(data);
* });
*
* @todo for v1.0: Split into separate parseICS() (sync) and parseICSAsync() (Promise-based) functions.
* The current dual-mode API (sync if no callback, async if callback) is an anti-pattern that
* makes the function behavior unpredictable and harder to type correctly in TypeScript.
*/
parseICS(string, cb) {
// Accept strings and String objects only, and fail like the former string.split() did
// instead of reading a Buffer or an array as lines
const text = string?.valueOf();
if (typeof text !== 'string') {
throw new TypeError(`parseICS() expects a string, got ${string === null ? 'null' : typeof string}; decode a Buffer with buffer.toString('utf8') first`);
}
const contentLines = unfoldLines(splitLines(text));
if (cb) {
// Async mode: start on the next turn of the event loop and parse in batches
setImmediate(() => {
settleCallback(parseContentLinesBatched(this, contentLines, PARSE_BATCH_SIZE, {}, []), cb);
});
return;
}
// Sync mode: parse all at once (no batching)
return parseContentLines(this, contentLines, {}, []);
},
};
const {objectHandlers} = ical;
const handleObject = ical.handleObject.bind(ical);
const parseLines = ical.parseLines.bind(ical);
const parseICS = ical.parseICS.bind(ical);
export {
objectHandlers,
handleObject,
parseLines,
parseICS,
};
export default ical;