advisers 0.1.0-preview.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +131 -21
- package/NOTICE +34 -0
- package/README-zh.md +40 -9
- package/README.md +53 -10
- package/THIRD-PARTY-NOTICES.md +277 -0
- package/bin/advisers.js +4230 -122
- package/index.js +1 -1
- package/lib/agent.js +414 -0
- package/lib/attachments.js +283 -0
- package/lib/capture.js +168 -0
- package/lib/context-window.js +123 -0
- package/lib/digest.js +27 -0
- package/lib/doc-review.js +30 -0
- package/lib/egress.js +27 -0
- package/lib/evidence.js +1995 -0
- package/lib/ical.js +542 -0
- package/lib/imap.js +181 -0
- package/lib/importers.js +1001 -0
- package/lib/learning.js +46 -0
- package/lib/mcp-server.js +869 -0
- package/lib/model.js +307 -0
- package/lib/notify.js +189 -0
- package/lib/nudge.js +10 -0
- package/lib/ocr.js +47 -0
- package/lib/pdf.js +105 -0
- package/lib/pet-brief-store.js +51 -0
- package/lib/pet-brief.js +644 -0
- package/lib/receipt-verify.js +624 -0
- package/lib/relative-date.js +393 -0
- package/lib/remind.js +690 -0
- package/lib/research.js +193 -0
- package/lib/review.js +88 -0
- package/lib/routine.js +22 -0
- package/lib/safety/classifier.js +392 -0
- package/lib/safety/explain.js +157 -0
- package/lib/safety/index.js +333 -0
- package/lib/safety/redact.js +212 -0
- package/lib/safety/rules.js +824 -0
- package/lib/scheduler.js +399 -0
- package/lib/source.js +294 -0
- package/lib/sqlite.js +1022 -0
- package/lib/sync.js +129 -0
- package/lib/timestamp.js +45 -0
- package/lib/tos/index.js +332 -0
- package/lib/tos/manifest.json +32 -0
- package/lib/tos/privacy.zh-CN.md +136 -0
- package/lib/tos/safety.zh-CN.md +101 -0
- package/lib/tos/summary.zh-CN.md +12 -0
- package/lib/tos/terms.zh-CN.md +439 -0
- package/lib/ui.js +49 -0
- package/lib/undo.js +83 -0
- package/lib/vault.js +51 -0
- package/package.json +26 -4
package/lib/ical.js
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recurrence expansion for locally imported calendar events (E2, read-only).
|
|
3
|
+
*
|
|
4
|
+
* Pure, offline, dependency-free. Scope is a deliberately small RRULE subset:
|
|
5
|
+
*
|
|
6
|
+
* FREQ=DAILY|WEEKLY, INTERVAL, COUNT, UNTIL, BYDAY (plain weekdays) and WKST
|
|
7
|
+
*
|
|
8
|
+
* plus simple EXDATE exclusion. Everything is interpreted in a fixed
|
|
9
|
+
* Asia/Shanghai (UTC+08:00) zone: this package ships no time-zone database and
|
|
10
|
+
* mainland China has no DST, so a fixed +08:00 offset is exact for the local
|
|
11
|
+
* calendar display that consumes this module.
|
|
12
|
+
*
|
|
13
|
+
* Why this module exists: importers.parseIcs keeps the RRULE text verbatim
|
|
14
|
+
* ("it is evidence, not something to expand") and the SQLite row stores the
|
|
15
|
+
* series once, so a weekly meeting used to appear in today --calendar only on
|
|
16
|
+
* its DTSTART day. The read-only CLI layer calls expandRrule(event, { from,
|
|
17
|
+
* to }) to turn that one row into the occurrences visible in a date window.
|
|
18
|
+
* Nothing here writes to the database or filesystem.
|
|
19
|
+
*
|
|
20
|
+
* Deliberate limits (also documented in docs/CALENDAR.md):
|
|
21
|
+
* - unsupported FREQ (MONTHLY/YEARLY/...), BY* parts other than BYDAY, COUNT
|
|
22
|
+
* together with UNTIL, BYDAY ordinals (e.g. 1MO), or malformed rules are
|
|
23
|
+
* never guessed: the stored base occurrence is returned unexpanded instead
|
|
24
|
+
* of inventing dates (a wrong meeting time is worse than a missing one);
|
|
25
|
+
* - RECURRENCE-ID override components are not resolved;
|
|
26
|
+
* - at most MAX_INSTANCES (366) instances are returned per call and the
|
|
27
|
+
* internal scan is bounded, so a hostile RRULE cannot amplify work.
|
|
28
|
+
*
|
|
29
|
+
* @module ical
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/** Fixed zone for every stored calendar timestamp in this package. */
|
|
33
|
+
export const SHANGHAI_TIMEZONE = 'Asia/Shanghai'
|
|
34
|
+
|
|
35
|
+
/** Hard cap on the number of occurrences one expandRrule() call may return. */
|
|
36
|
+
export const MAX_INSTANCES = 366
|
|
37
|
+
|
|
38
|
+
const OFFSET_MS = 8 * 60 * 60 * 1000
|
|
39
|
+
const DAY_MS = 24 * 60 * 60 * 1000
|
|
40
|
+
// Upper bound on rule candidates examined per call (days or weeks). Keeps a
|
|
41
|
+
// hostile rule bounded even when the requested window is far from DTSTART.
|
|
42
|
+
const MAX_SCAN_STEPS = 100000
|
|
43
|
+
const MAX_INTERVAL = 10000
|
|
44
|
+
|
|
45
|
+
const WEEKDAY_NAMES = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']
|
|
46
|
+
const WEEKDAY_INDEX = new Map(WEEKDAY_NAMES.map((name, index) => [name, index]))
|
|
47
|
+
const SUPPORTED_RRULE_KEYS = new Set(['FREQ', 'INTERVAL', 'COUNT', 'UNTIL', 'BYDAY', 'WKST'])
|
|
48
|
+
|
|
49
|
+
const mod = (value, divisor) => ((value % divisor) + divisor) % divisor
|
|
50
|
+
|
|
51
|
+
function asString(value) {
|
|
52
|
+
return typeof value === 'string' ? value.trim() : ''
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function own(event, keys) {
|
|
56
|
+
for (const key of keys) {
|
|
57
|
+
if (event[key] !== undefined && event[key] !== null) return event[key]
|
|
58
|
+
}
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function toBoolean(value) {
|
|
63
|
+
return value === true || value === 1 || value === '1' || value === 'true'
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Monday-based weekday of an epoch-day number (1970-01-01 = Thursday). */
|
|
67
|
+
function weekdayOfDay(dayNumber) {
|
|
68
|
+
return mod(dayNumber + 3, 7)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function formatDay(dayNumber) {
|
|
72
|
+
const date = new Date(dayNumber * DAY_MS)
|
|
73
|
+
const year = String(date.getUTCFullYear()).padStart(4, '0')
|
|
74
|
+
const month = String(date.getUTCMonth() + 1).padStart(2, '0')
|
|
75
|
+
const day = String(date.getUTCDate()).padStart(2, '0')
|
|
76
|
+
return year + '-' + month + '-' + day
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Format an instant as Asia/Shanghai wall-clock ISO time (explicit +08:00). */
|
|
80
|
+
function formatTimed(instantMs) {
|
|
81
|
+
const date = new Date(instantMs + OFFSET_MS)
|
|
82
|
+
const year = String(date.getUTCFullYear()).padStart(4, '0')
|
|
83
|
+
const month = String(date.getUTCMonth() + 1).padStart(2, '0')
|
|
84
|
+
const day = String(date.getUTCDate()).padStart(2, '0')
|
|
85
|
+
const hour = String(date.getUTCHours()).padStart(2, '0')
|
|
86
|
+
const minute = String(date.getUTCMinutes()).padStart(2, '0')
|
|
87
|
+
const second = String(date.getUTCSeconds()).padStart(2, '0')
|
|
88
|
+
return year + '-' + month + '-' + day + 'T' + hour + ':' + minute + ':' + second + '+08:00'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function validYmd(year, month, day) {
|
|
92
|
+
if (!Number.isInteger(year) || !Number.isInteger(month) || !Number.isInteger(day)) return false
|
|
93
|
+
if (month < 1 || month > 12 || day < 1 || day > 31) return false
|
|
94
|
+
const probe = new Date(Date.UTC(year, month - 1, day))
|
|
95
|
+
return probe.getUTCFullYear() === year && probe.getUTCMonth() === month - 1 && probe.getUTCDate() === day
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function dayNumberFromYmd(year, month, day) {
|
|
99
|
+
if (!validYmd(year, month, day)) return null
|
|
100
|
+
return Math.floor(Date.UTC(year, month - 1, day) / DAY_MS)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Parse a date-only value: 'YYYY-MM-DD' or compact iCalendar 'YYYYMMDD'. */
|
|
104
|
+
function parseDay(value) {
|
|
105
|
+
if (typeof value !== 'string') return null
|
|
106
|
+
const text = value.trim()
|
|
107
|
+
let match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text)
|
|
108
|
+
if (!match) match = /^(\d{4})(\d{2})(\d{2})$/.exec(text)
|
|
109
|
+
if (!match) return null
|
|
110
|
+
const dayNumber = dayNumberFromYmd(Number(match[1]), Number(match[2]), Number(match[3]))
|
|
111
|
+
return dayNumber == null ? null : { dayNumber }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Parse a date-time value into an instant (epoch ms).
|
|
116
|
+
*
|
|
117
|
+
* Accepted: Date / finite number / 'YYYY-MM-DDTHH:MM[:SS]' (Asia/Shanghai
|
|
118
|
+
* wall clock when no zone suffix), compact iCalendar 'YYYYMMDDTHHMMSS[Z]',
|
|
119
|
+
* with an optional 'Z' or '+08:00' / '+0800' suffix.
|
|
120
|
+
*
|
|
121
|
+
* @param {unknown} value
|
|
122
|
+
* @returns {{ instantMs: number, dayNumber: number } | null}
|
|
123
|
+
*/
|
|
124
|
+
function parseDateTime(value) {
|
|
125
|
+
if (value instanceof Date) {
|
|
126
|
+
const instantMs = value.getTime()
|
|
127
|
+
if (!Number.isFinite(instantMs)) return null
|
|
128
|
+
return { instantMs, dayNumber: Math.floor((instantMs + OFFSET_MS) / DAY_MS) }
|
|
129
|
+
}
|
|
130
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
131
|
+
return { instantMs: value, dayNumber: Math.floor((value + OFFSET_MS) / DAY_MS) }
|
|
132
|
+
}
|
|
133
|
+
if (typeof value !== 'string') return null
|
|
134
|
+
const text = value.trim()
|
|
135
|
+
if (!text) return null
|
|
136
|
+
|
|
137
|
+
let match = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(Z)?$/i.exec(text)
|
|
138
|
+
if (match) {
|
|
139
|
+
const year = Number(match[1])
|
|
140
|
+
const month = Number(match[2])
|
|
141
|
+
const day = Number(match[3])
|
|
142
|
+
const hour = Number(match[4])
|
|
143
|
+
const minute = Number(match[5])
|
|
144
|
+
const second = Number(match[6])
|
|
145
|
+
if (!validYmd(year, month, day) || hour > 23 || minute > 59 || second > 59) return null
|
|
146
|
+
const utc = Date.UTC(year, month - 1, day, hour, minute, second)
|
|
147
|
+
const instantMs = match[7] ? utc : utc - OFFSET_MS
|
|
148
|
+
return { instantMs, dayNumber: Math.floor((instantMs + OFFSET_MS) / DAY_MS) }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
match = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?(Z|[+-]\d{2}:?\d{2})?$/i.exec(text)
|
|
152
|
+
if (match) {
|
|
153
|
+
const year = Number(match[1])
|
|
154
|
+
const month = Number(match[2])
|
|
155
|
+
const day = Number(match[3])
|
|
156
|
+
const hour = Number(match[4])
|
|
157
|
+
const minute = Number(match[5])
|
|
158
|
+
const second = match[6] == null ? 0 : Number(match[6])
|
|
159
|
+
const millis = match[7] == null ? 0 : Number(match[7].padEnd(3, '0'))
|
|
160
|
+
if (!validYmd(year, month, day) || hour > 23 || minute > 59 || second > 59) return null
|
|
161
|
+
const wallClock = Date.UTC(year, month - 1, day, hour, minute, second, millis)
|
|
162
|
+
const zone = match[8]
|
|
163
|
+
let instantMs
|
|
164
|
+
if (!zone) {
|
|
165
|
+
instantMs = wallClock - OFFSET_MS
|
|
166
|
+
} else if (zone.toUpperCase() === 'Z') {
|
|
167
|
+
instantMs = wallClock
|
|
168
|
+
} else {
|
|
169
|
+
const sign = zone[0] === '-' ? -1 : 1
|
|
170
|
+
const digits = zone.slice(1).replace(':', '')
|
|
171
|
+
const offsetMs = sign * (Number(digits.slice(0, 2)) * 60 + Number(digits.slice(2))) * 60 * 1000
|
|
172
|
+
instantMs = wallClock - offsetMs
|
|
173
|
+
}
|
|
174
|
+
return { instantMs, dayNumber: Math.floor((instantMs + OFFSET_MS) / DAY_MS) }
|
|
175
|
+
}
|
|
176
|
+
return null
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function readStart(event) {
|
|
180
|
+
const raw = own(event, ['startsAt', 'start', 'starts_at', 'dtstart', 'DTSTART'])
|
|
181
|
+
if (raw === undefined) return null
|
|
182
|
+
const allDayFlag = toBoolean(own(event, ['allDay', 'all_day', 'allday']))
|
|
183
|
+
const day = parseDay(raw)
|
|
184
|
+
if (day) {
|
|
185
|
+
return { allDay: true, dayNumber: day.dayNumber, todMs: 0, startMs: day.dayNumber * DAY_MS - OFFSET_MS }
|
|
186
|
+
}
|
|
187
|
+
const parsed = parseDateTime(raw)
|
|
188
|
+
if (!parsed) return null
|
|
189
|
+
if (allDayFlag) {
|
|
190
|
+
return { allDay: true, dayNumber: parsed.dayNumber, todMs: 0, startMs: parsed.dayNumber * DAY_MS - OFFSET_MS }
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
allDay: false,
|
|
194
|
+
dayNumber: parsed.dayNumber,
|
|
195
|
+
todMs: mod(parsed.instantMs + OFFSET_MS, DAY_MS),
|
|
196
|
+
startMs: parsed.instantMs,
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function readEnd(event, start) {
|
|
201
|
+
const raw = own(event, ['endsAt', 'end', 'ends_at', 'dtend', 'DTEND'])
|
|
202
|
+
if (raw === undefined) return null
|
|
203
|
+
if (start.allDay) {
|
|
204
|
+
const day = parseDay(raw)
|
|
205
|
+
const parsed = day ? null : parseDateTime(raw)
|
|
206
|
+
const dayNumber = day ? day.dayNumber : parsed ? parsed.dayNumber : null
|
|
207
|
+
if (dayNumber == null) return null
|
|
208
|
+
return { durationDays: Math.max(0, dayNumber - start.dayNumber) }
|
|
209
|
+
}
|
|
210
|
+
const day = parseDay(raw)
|
|
211
|
+
if (day) {
|
|
212
|
+
// A date-only DTEND on a timed event is tolerated as Shanghai midnight.
|
|
213
|
+
const durationMs = day.dayNumber * DAY_MS - OFFSET_MS - start.startMs
|
|
214
|
+
return durationMs < 0 ? null : { durationMs }
|
|
215
|
+
}
|
|
216
|
+
const parsed = parseDateTime(raw)
|
|
217
|
+
if (!parsed) return null
|
|
218
|
+
const durationMs = parsed.instantMs - start.startMs
|
|
219
|
+
return durationMs < 0 ? null : { durationMs }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function normalizeWindowInstant(value, name, { endOfDay = false } = {}) {
|
|
223
|
+
if (value === undefined || value === null) return null
|
|
224
|
+
if (value instanceof Date) {
|
|
225
|
+
const instantMs = value.getTime()
|
|
226
|
+
if (!Number.isFinite(instantMs)) throw new TypeError('expandRrule: invalid ' + name + ' date')
|
|
227
|
+
return instantMs
|
|
228
|
+
}
|
|
229
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value
|
|
230
|
+
const day = parseDay(value)
|
|
231
|
+
if (day) {
|
|
232
|
+
const startMs = day.dayNumber * DAY_MS - OFFSET_MS
|
|
233
|
+
return endOfDay ? startMs + DAY_MS - 1 : startMs
|
|
234
|
+
}
|
|
235
|
+
const parsed = parseDateTime(value)
|
|
236
|
+
if (!parsed) throw new TypeError('expandRrule: invalid ' + name + ' value: ' + String(value))
|
|
237
|
+
return parsed.instantMs
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function readWindow(options, start) {
|
|
241
|
+
const fromMs = normalizeWindowInstant(options?.from, 'from')
|
|
242
|
+
const toMs = normalizeWindowInstant(options?.to, 'to', { endOfDay: true })
|
|
243
|
+
const windowFrom = fromMs == null ? start.startMs : fromMs
|
|
244
|
+
const windowTo = toMs == null ? windowFrom + 366 * DAY_MS : toMs
|
|
245
|
+
return { fromMs: windowFrom, toMs: windowTo }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function parseUntil(value) {
|
|
249
|
+
const text = asString(value)
|
|
250
|
+
if (!text) return null
|
|
251
|
+
const day = parseDay(text)
|
|
252
|
+
if (day) {
|
|
253
|
+
// A DATE UNTIL bounds the whole local day: inclusive up to 23:59:59.999.
|
|
254
|
+
return { instantMs: day.dayNumber * DAY_MS - OFFSET_MS + DAY_MS - 1 }
|
|
255
|
+
}
|
|
256
|
+
const parsed = parseDateTime(text)
|
|
257
|
+
return parsed ? { instantMs: parsed.instantMs } : null
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Parse the supported RRULE subset. Exported for callers that want to explain
|
|
262
|
+
* why a rule was not expanded (e.g. a CLI warning); expandRrule() itself never
|
|
263
|
+
* throws on an unsupported rule.
|
|
264
|
+
*
|
|
265
|
+
* @param {unknown} text raw RRULE text (leading 'RRULE:' prefix tolerated)
|
|
266
|
+
* @returns {{ freq: 'DAILY'|'WEEKLY'|null, interval: number, count: number|null,
|
|
267
|
+
* until: {instantMs: number}|null, byday: number[]|null, wkst: number,
|
|
268
|
+
* supported: boolean, reason: string|null, raw: string|null }}
|
|
269
|
+
*/
|
|
270
|
+
export function parseRrule(text) {
|
|
271
|
+
const result = {
|
|
272
|
+
freq: null,
|
|
273
|
+
interval: 1,
|
|
274
|
+
count: null,
|
|
275
|
+
until: null,
|
|
276
|
+
byday: null,
|
|
277
|
+
wkst: 0,
|
|
278
|
+
supported: false,
|
|
279
|
+
reason: null,
|
|
280
|
+
raw: null,
|
|
281
|
+
}
|
|
282
|
+
if (typeof text !== 'string' || !text.trim()) {
|
|
283
|
+
result.reason = 'missing'
|
|
284
|
+
return result
|
|
285
|
+
}
|
|
286
|
+
let raw = text.trim()
|
|
287
|
+
if (/^RRULE:/i.test(raw)) raw = raw.slice(raw.indexOf(':') + 1).trim()
|
|
288
|
+
result.raw = raw
|
|
289
|
+
const parts = new Map()
|
|
290
|
+
for (const segment of raw.split(';')) {
|
|
291
|
+
if (!segment.trim()) continue
|
|
292
|
+
const equals = segment.indexOf('=')
|
|
293
|
+
if (equals <= 0) {
|
|
294
|
+
result.reason = 'malformed-rule'
|
|
295
|
+
return result
|
|
296
|
+
}
|
|
297
|
+
const key = segment.slice(0, equals).trim().toUpperCase()
|
|
298
|
+
const value = segment.slice(equals + 1).trim()
|
|
299
|
+
if (parts.has(key)) {
|
|
300
|
+
result.reason = 'duplicate-part:' + key
|
|
301
|
+
return result
|
|
302
|
+
}
|
|
303
|
+
parts.set(key, value)
|
|
304
|
+
}
|
|
305
|
+
for (const key of parts.keys()) {
|
|
306
|
+
if (!SUPPORTED_RRULE_KEYS.has(key)) {
|
|
307
|
+
result.reason = 'unsupported-part:' + key
|
|
308
|
+
return result
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const freq = asString(parts.get('FREQ')).toUpperCase()
|
|
312
|
+
if (freq !== 'DAILY' && freq !== 'WEEKLY') {
|
|
313
|
+
result.reason = freq ? 'unsupported-freq:' + freq : 'missing-freq'
|
|
314
|
+
return result
|
|
315
|
+
}
|
|
316
|
+
result.freq = freq
|
|
317
|
+
if (parts.has('COUNT') && parts.has('UNTIL')) {
|
|
318
|
+
// RFC 5545 forbids both; guessing which one wins risks inventing occurrences.
|
|
319
|
+
result.reason = 'count-and-until'
|
|
320
|
+
return result
|
|
321
|
+
}
|
|
322
|
+
if (parts.has('INTERVAL')) {
|
|
323
|
+
const interval = Number(parts.get('INTERVAL'))
|
|
324
|
+
if (!Number.isInteger(interval) || interval < 1 || interval > MAX_INTERVAL) {
|
|
325
|
+
result.reason = 'bad-interval'
|
|
326
|
+
return result
|
|
327
|
+
}
|
|
328
|
+
result.interval = interval
|
|
329
|
+
}
|
|
330
|
+
if (parts.has('COUNT')) {
|
|
331
|
+
const count = Number(parts.get('COUNT'))
|
|
332
|
+
if (!Number.isInteger(count) || count < 1) {
|
|
333
|
+
result.reason = 'bad-count'
|
|
334
|
+
return result
|
|
335
|
+
}
|
|
336
|
+
result.count = count
|
|
337
|
+
}
|
|
338
|
+
if (parts.has('UNTIL')) {
|
|
339
|
+
const until = parseUntil(parts.get('UNTIL'))
|
|
340
|
+
if (!until) {
|
|
341
|
+
result.reason = 'bad-until'
|
|
342
|
+
return result
|
|
343
|
+
}
|
|
344
|
+
result.until = until
|
|
345
|
+
}
|
|
346
|
+
if (parts.has('BYDAY')) {
|
|
347
|
+
const byday = []
|
|
348
|
+
for (const token of parts.get('BYDAY').split(',')) {
|
|
349
|
+
const weekday = token.trim().toUpperCase()
|
|
350
|
+
if (!WEEKDAY_INDEX.has(weekday)) {
|
|
351
|
+
result.reason = 'bad-byday'
|
|
352
|
+
return result
|
|
353
|
+
}
|
|
354
|
+
const index = WEEKDAY_INDEX.get(weekday)
|
|
355
|
+
if (!byday.includes(index)) byday.push(index)
|
|
356
|
+
}
|
|
357
|
+
if (!byday.length) {
|
|
358
|
+
result.reason = 'bad-byday'
|
|
359
|
+
return result
|
|
360
|
+
}
|
|
361
|
+
result.byday = byday.sort((a, b) => a - b)
|
|
362
|
+
}
|
|
363
|
+
if (parts.has('WKST')) {
|
|
364
|
+
const weekday = asString(parts.get('WKST')).toUpperCase()
|
|
365
|
+
if (!WEEKDAY_INDEX.has(weekday)) {
|
|
366
|
+
result.reason = 'bad-wkst'
|
|
367
|
+
return result
|
|
368
|
+
}
|
|
369
|
+
result.wkst = WEEKDAY_INDEX.get(weekday)
|
|
370
|
+
}
|
|
371
|
+
result.supported = true
|
|
372
|
+
return result
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function collectExdates(event) {
|
|
376
|
+
const raw = own(event, ['exdate', 'exdates', 'exDate', 'exDates', 'ex_date'])
|
|
377
|
+
const values = Array.isArray(raw) ? raw : typeof raw === 'string' ? raw.split(',') : []
|
|
378
|
+
const days = new Set()
|
|
379
|
+
const instants = new Set()
|
|
380
|
+
for (const value of values) {
|
|
381
|
+
const text = asString(value)
|
|
382
|
+
if (!text) continue
|
|
383
|
+
// Tolerate the full property text: 'EXDATE;TZID=Asia/Shanghai:20261116T100000'.
|
|
384
|
+
const afterParams = /^EXDATE/i.test(text) && text.includes(':') ? text.slice(text.indexOf(':') + 1) : text
|
|
385
|
+
const day = parseDay(afterParams)
|
|
386
|
+
if (day) {
|
|
387
|
+
days.add(day.dayNumber)
|
|
388
|
+
continue
|
|
389
|
+
}
|
|
390
|
+
const parsed = parseDateTime(afterParams)
|
|
391
|
+
if (parsed) instants.add(parsed.instantMs)
|
|
392
|
+
}
|
|
393
|
+
return { days, instants }
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function isExcluded(exdate, start, dayNumber, occurrenceMs) {
|
|
397
|
+
if (exdate.days.has(dayNumber)) return true
|
|
398
|
+
if (exdate.instants.size === 0) return false
|
|
399
|
+
for (const instantMs of exdate.instants) {
|
|
400
|
+
if (start.allDay) {
|
|
401
|
+
if (Math.floor((instantMs + OFFSET_MS) / DAY_MS) === dayNumber) return true
|
|
402
|
+
} else if (instantMs === occurrenceMs) {
|
|
403
|
+
return true
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return false
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function makeInstance(event, start, end, dayNumber, occurrenceMs, index, seriesStartsAt, recurring) {
|
|
410
|
+
const startsAt = start.allDay ? formatDay(dayNumber) : formatTimed(occurrenceMs)
|
|
411
|
+
let endsAt = null
|
|
412
|
+
if (end) {
|
|
413
|
+
endsAt = start.allDay ? formatDay(dayNumber + end.durationDays) : formatTimed(occurrenceMs + end.durationMs)
|
|
414
|
+
}
|
|
415
|
+
return {
|
|
416
|
+
uid: event.uid == null ? null : String(event.uid),
|
|
417
|
+
summary: typeof event.summary === 'string' ? event.summary : String(event.summary ?? ''),
|
|
418
|
+
startsAt,
|
|
419
|
+
endsAt,
|
|
420
|
+
allDay: start.allDay,
|
|
421
|
+
tzid: event.tzid == null ? null : String(event.tzid),
|
|
422
|
+
location: event.location == null ? null : String(event.location),
|
|
423
|
+
rrule: typeof event.rrule === 'string' ? event.rrule : null,
|
|
424
|
+
source: event.source == null ? null : event.source,
|
|
425
|
+
recurring,
|
|
426
|
+
occurrenceIndex: index,
|
|
427
|
+
seriesStartsAt,
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Expand one stored calendar event into the occurrences that start inside
|
|
433
|
+
* {from, to} (Asia/Shanghai wall clock, inclusive).
|
|
434
|
+
*
|
|
435
|
+
* Input accepts the camelCase row returned by SqliteStore.listCalendarEvents()
|
|
436
|
+
* (uid/summary/startsAt/endsAt/allDay/tzid/location/rrule/source) as well as
|
|
437
|
+
* snake_case / parseIcs field names (start/end/starts_at/...), plus optional
|
|
438
|
+
* exdate / exdates entries (string, comma-separated string, or array).
|
|
439
|
+
*
|
|
440
|
+
* Returned instance objects keep the row's identity/summary metadata and add:
|
|
441
|
+
* startsAt / endsAt 'YYYY-MM-DD' (all-day) or
|
|
442
|
+
* 'YYYY-MM-DDTHH:MM:SS+08:00' (timed);
|
|
443
|
+
* recurring true when the instance came from an expanded RRULE;
|
|
444
|
+
* occurrenceIndex 0-based index inside this expansion result;
|
|
445
|
+
* seriesStartsAt formatted DTSTART of the stored series.
|
|
446
|
+
*
|
|
447
|
+
* Window bounds may be Date objects, epoch milliseconds, 'YYYY-MM-DD' dates
|
|
448
|
+
* (a bare 'to' date means end of that local day) or ISO/iCalendar timestamps.
|
|
449
|
+
* They default to the DTSTART instant / DTSTART + 366 days.
|
|
450
|
+
*
|
|
451
|
+
* Events without a usable start return []; unsupported or malformed RRULEs
|
|
452
|
+
* return the single stored occurrence (never throw, never invent dates).
|
|
453
|
+
*
|
|
454
|
+
* @param {object} event stored event row
|
|
455
|
+
* @param {{from?: unknown, to?: unknown}} [options] display window
|
|
456
|
+
* @returns {Array<object>} occurrence instances, at most MAX_INSTANCES
|
|
457
|
+
* @throws {TypeError} when event is not an object or a window bound is invalid
|
|
458
|
+
*/
|
|
459
|
+
export function expandRrule(event, options = {}) {
|
|
460
|
+
if (!event || typeof event !== 'object' || Array.isArray(event)) {
|
|
461
|
+
throw new TypeError('expandRrule: event must be an object')
|
|
462
|
+
}
|
|
463
|
+
const start = readStart(event)
|
|
464
|
+
if (!start) return []
|
|
465
|
+
const end = readEnd(event, start)
|
|
466
|
+
const { fromMs, toMs } = readWindow(options, start)
|
|
467
|
+
if (toMs < fromMs) return []
|
|
468
|
+
if (start.startMs > toMs) return []
|
|
469
|
+
|
|
470
|
+
const seriesStartsAt = start.allDay ? formatDay(start.dayNumber) : formatTimed(start.startMs)
|
|
471
|
+
const rule = parseRrule(typeof event.rrule === 'string' ? event.rrule : null)
|
|
472
|
+
if (!rule.raw || !rule.supported) {
|
|
473
|
+
// Keep the previous behaviour for rules outside the supported subset: show
|
|
474
|
+
// the stored occurrence when it falls inside the window, never a guessed
|
|
475
|
+
// recurrence.
|
|
476
|
+
if (start.startMs < fromMs) return []
|
|
477
|
+
return [makeInstance(event, start, end, start.dayNumber, start.startMs, 0, seriesStartsAt, false)]
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const records = []
|
|
481
|
+
let emitted = 0
|
|
482
|
+
const consider = (dayNumber, occurrenceMs) => {
|
|
483
|
+
if (rule.until && occurrenceMs > rule.until.instantMs) return 'stop'
|
|
484
|
+
if (rule.count != null && emitted >= rule.count) return 'stop'
|
|
485
|
+
emitted += 1
|
|
486
|
+
if (occurrenceMs >= fromMs && occurrenceMs <= toMs) {
|
|
487
|
+
records.push({ dayNumber, occurrenceMs })
|
|
488
|
+
if (records.length >= MAX_INSTANCES) return 'stop'
|
|
489
|
+
}
|
|
490
|
+
return 'continue'
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// DTSTART is always the first instance of the series, even when it does not
|
|
494
|
+
// match BYDAY (RFC 5545 leaves that combination undefined; keeping the stored
|
|
495
|
+
// occurrence is the only choice that never hides a real meeting).
|
|
496
|
+
let stopped = consider(start.dayNumber, start.startMs) === 'stop'
|
|
497
|
+
|
|
498
|
+
let steps = 0
|
|
499
|
+
if (!stopped && rule.freq === 'DAILY') {
|
|
500
|
+
const byday = rule.byday
|
|
501
|
+
let multiplier = 1
|
|
502
|
+
while (steps < MAX_SCAN_STEPS) {
|
|
503
|
+
steps += 1
|
|
504
|
+
const dayNumber = start.dayNumber + multiplier * rule.interval
|
|
505
|
+
multiplier += 1
|
|
506
|
+
if (byday && !byday.includes(weekdayOfDay(dayNumber))) continue
|
|
507
|
+
const occurrenceMs = dayNumber * DAY_MS - OFFSET_MS + start.todMs
|
|
508
|
+
if (occurrenceMs > toMs) break
|
|
509
|
+
if (consider(dayNumber, occurrenceMs) === 'stop') break
|
|
510
|
+
}
|
|
511
|
+
} else if (!stopped) {
|
|
512
|
+
// WEEKLY: walk whole weeks aligned to WKST, stepping INTERVAL weeks.
|
|
513
|
+
const byday = rule.byday ?? [weekdayOfDay(start.dayNumber)]
|
|
514
|
+
const weekdayOffsets = byday.map((weekday) => mod(weekday - rule.wkst, 7)).sort((a, b) => a - b)
|
|
515
|
+
const firstWeekStart = start.dayNumber - mod(weekdayOfDay(start.dayNumber) - rule.wkst, 7)
|
|
516
|
+
let week = 0
|
|
517
|
+
while (steps < MAX_SCAN_STEPS) {
|
|
518
|
+
steps += 1
|
|
519
|
+
const weekStart = firstWeekStart + week * rule.interval * 7
|
|
520
|
+
week += 1
|
|
521
|
+
for (const offset of weekdayOffsets) {
|
|
522
|
+
const dayNumber = weekStart + offset
|
|
523
|
+
if (dayNumber <= start.dayNumber) continue
|
|
524
|
+
const occurrenceMs = dayNumber * DAY_MS - OFFSET_MS + start.todMs
|
|
525
|
+
if (occurrenceMs > toMs) {
|
|
526
|
+
steps = MAX_SCAN_STEPS
|
|
527
|
+
break
|
|
528
|
+
}
|
|
529
|
+
if (consider(dayNumber, occurrenceMs) === 'stop') {
|
|
530
|
+
steps = MAX_SCAN_STEPS
|
|
531
|
+
break
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const exdate = collectExdates(event)
|
|
538
|
+
const keep = (record) => !isExcluded(exdate, start, record.dayNumber, record.occurrenceMs)
|
|
539
|
+
return records
|
|
540
|
+
.filter(keep)
|
|
541
|
+
.map((record, index) => makeInstance(event, start, end, record.dayNumber, record.occurrenceMs, index, seriesStartsAt, true))
|
|
542
|
+
}
|