From 45e4b847a22c9409c59ef94da5a2392e6d6501ef Mon Sep 17 00:00:00 2001 From: iOS Date: Wed, 20 Sep 2023 09:35:48 +0200 Subject: [PATCH] wed update --- .obsidian/graph.json | 2 +- .obsidian/plugins/auto-card-link/main.js | 129 +- .../plugins/auto-card-link/manifest.json | 2 +- .obsidian/plugins/auto-card-link/styles.css | 135 +- .obsidian/plugins/cron/data.json | 2 +- .obsidian/plugins/dataview/main.js | 19671 ++++++++-------- .obsidian/plugins/dataview/manifest.json | 2 +- .obsidian/plugins/dataview/styles.css | 2 +- .obsidian/plugins/emoji-shortcodes/data.json | 11 +- .../obsidian-activity-history/data.json | 38 +- .../plugins/obsidian-admonition/data.json | 2 +- .obsidian/plugins/obsidian-admonition/main.js | 42 +- .../plugins/obsidian-admonition/manifest.json | 2 +- .obsidian/plugins/obsidian-commits/data.json | 338 +- .../plugins/obsidian-dice-roller/main.js | 56 +- .../obsidian-dice-roller/manifest.json | 2 +- .../obsidian-reminder-plugin/data.json | 78 +- .../plugins/obsidian-tasks-plugin/main.js | 96 +- .../obsidian-tasks-plugin/manifest.json | 2 +- .obsidian/plugins/obsidian42-brat/data.json | 1 + .obsidian/workspace-mobile.json | 66 +- 00.01 Admin/Calendars/2023-09-03.md | 2 + 00.01 Admin/Calendars/2023-09-12.md | 16 +- 00.01 Admin/Calendars/2023-09-13.md | 136 + 00.01 Admin/Calendars/2023-09-14.md | 139 + 00.01 Admin/Calendars/2023-09-15.md | 136 + 00.01 Admin/Calendars/2023-09-16.md | 134 + 00.01 Admin/Calendars/2023-09-17.md | 135 + 00.01 Admin/Calendars/2023-09-18.md | 134 + 00.01 Admin/Calendars/2023-09-19.md | 140 + 00.01 Admin/Calendars/2023-09-20.md | 134 + .../2023-09-15 ⚽️ PSG - OGC Nice (2-3).md | 14 + ...19 ⚽️ PSG - Borussia Dortmund (2-0).md | 14 + ...24-06-08 💍 Mariage Rémi & Séverine.md | 22 + ...sing Partisan Divide on Life Expectancy.md | 111 + 00.03 News/Buried under the ice.md | 2 +- 00.03 News/Can We Talk to Whales.md | 334 + ...Confessions of a McKinsey Whistleblower.md | 203 + ...sele Fetterman’s Had a Hell of a Year.md | 88 + ...y and Misogyny at the Edge of the World.md | 2 +- ...ntered an alternative political reality.md | 223 + ...The Serial Killer Hiding in Plain Sight.md | 229 + 00.03 News/The Source Years.md | 141 + ...the biggest match-fixing ring in tennis.md | 505 + ...rma Billionaire Barry Sherman and Honey.md | 2 +- 01.02 Home/@Shopping list.md | 10 +- 01.02 Home/Household.md | 6 +- 01.07 Animals/2023-07-13 Health check.md | 3 +- 01.07 Animals/2023-09-02 First Tournament.md | 4 +- 01.07 Animals/2023-09-19 Influenza vaccine.md | 54 + 05.02 Networks/Configuring UFW.md | 6 +- 05.02 Networks/Server Tools.md | 3 +- 05.02 Networks/Server VPN.md | 3 +- 53 files changed, 13650 insertions(+), 10114 deletions(-) create mode 100644 00.01 Admin/Calendars/2023-09-13.md create mode 100644 00.01 Admin/Calendars/2023-09-14.md create mode 100644 00.01 Admin/Calendars/2023-09-15.md create mode 100644 00.01 Admin/Calendars/2023-09-16.md create mode 100644 00.01 Admin/Calendars/2023-09-17.md create mode 100644 00.01 Admin/Calendars/2023-09-18.md create mode 100644 00.01 Admin/Calendars/2023-09-19.md create mode 100644 00.01 Admin/Calendars/2023-09-20.md create mode 100644 00.01 Admin/Calendars/Events/2023-09-15 ⚽️ PSG - OGC Nice (2-3).md create mode 100644 00.01 Admin/Calendars/Events/2023-09-19 ⚽️ PSG - Borussia Dortmund (2-0).md create mode 100644 00.01 Admin/Calendars/Events/2024-06-08 💍 Mariage Rémi & Séverine.md create mode 100644 00.03 News/America’s Surprising Partisan Divide on Life Expectancy.md create mode 100644 00.03 News/Can We Talk to Whales.md create mode 100644 00.03 News/Confessions of a McKinsey Whistleblower.md create mode 100644 00.03 News/Gisele Fetterman’s Had a Hell of a Year.md create mode 100644 00.03 News/Naomi Klein on following her ‘doppelganger’ down the conspiracy rabbit hole – and why millions of people have entered an alternative political reality.md create mode 100644 00.03 News/The Serial Killer Hiding in Plain Sight.md create mode 100644 00.03 News/The Source Years.md create mode 100644 00.03 News/The maestro The man who built the biggest match-fixing ring in tennis.md create mode 100644 01.07 Animals/2023-09-19 Influenza vaccine.md diff --git a/.obsidian/graph.json b/.obsidian/graph.json index b18c3d2c..cc568e47 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -95,6 +95,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, - "scale": 0.8002457137330629, + "scale": 0.19685905652792965, "close": true } \ No newline at end of file diff --git a/.obsidian/plugins/auto-card-link/main.js b/.obsidian/plugins/auto-card-link/main.js index e4048e5c..bf56271c 100644 --- a/.obsidian/plugins/auto-card-link/main.js +++ b/.obsidian/plugins/auto-card-link/main.js @@ -181,23 +181,25 @@ var LinkMetadataParser = class { this.htmlDoc = htmlDoc; } parse() { - var _a, _b; - const title = (_a = this.getTitle()) == null ? void 0 : _a.replace(/\r\n|\n|\r/g, "").replace(/"/g, '\\"').trim(); - if (!title) - return; - const description = (_b = this.getDescription()) == null ? void 0 : _b.replace(/\r\n|\n|\r/g, "").replace(/"/g, '\\"').trim(); - const { hostname } = new URL(this.url); - const favicon = this.getFavicon(); - const image = this.getImage(); - return { - url: this.url, - title, - description, - host: hostname, - favicon, - image, - indent: 0 - }; + return __async(this, null, function* () { + var _a, _b; + const title = (_a = this.getTitle()) == null ? void 0 : _a.replace(/\r\n|\n|\r/g, "").replace(/"/g, '\\"').trim(); + if (!title) + return; + const description = (_b = this.getDescription()) == null ? void 0 : _b.replace(/\r\n|\n|\r/g, "").replace(/"/g, '\\"').trim(); + const { hostname } = new URL(this.url); + const favicon = yield this.getFavicon(); + const image = yield this.getImage(); + return { + url: this.url, + title, + description, + host: hostname, + favicon, + image, + indent: 0 + }; + }); } getTitle() { var _a, _b; @@ -218,16 +220,58 @@ var LinkMetadataParser = class { return metaDescription; } getFavicon() { - var _a; - const favicon = (_a = this.htmlDoc.querySelector("link[rel='icon']")) == null ? void 0 : _a.getAttr("href"); - if (favicon) - return favicon; + return __async(this, null, function* () { + var _a; + const favicon = (_a = this.htmlDoc.querySelector("link[rel='icon']")) == null ? void 0 : _a.getAttr("href"); + if (favicon) + return yield this.fixImageUrl(favicon); + }); } getImage() { - var _a; - const ogImage = (_a = this.htmlDoc.querySelector("meta[property='og:image']")) == null ? void 0 : _a.getAttr("content"); - if (ogImage) - return ogImage; + return __async(this, null, function* () { + var _a; + const ogImage = (_a = this.htmlDoc.querySelector("meta[property='og:image']")) == null ? void 0 : _a.getAttr("content"); + if (ogImage) + return yield this.fixImageUrl(ogImage); + }); + } + fixImageUrl(url) { + return __async(this, null, function* () { + if (url === void 0) + return ""; + const { hostname } = new URL(this.url); + let image = url; + if (url && url.startsWith("//")) { + const testUrlHttps = `https:${url}`; + const testUrlHttp = `http:${url}`; + if (yield checkUrlAccessibility(testUrlHttps)) { + image = testUrlHttps; + } else if (yield checkUrlAccessibility(testUrlHttp)) { + image = testUrlHttp; + } + } else if (url && url.startsWith("/") && hostname) { + const testUrlHttps = `https://${hostname}${url}`; + const testUrlHttp = `http://${hostname}${url}`; + const resUrlHttps = yield checkUrlAccessibility(testUrlHttps); + const resUrlHttp = yield checkUrlAccessibility(testUrlHttp); + if (resUrlHttps) { + image = testUrlHttps; + } else if (resUrlHttp) { + image = testUrlHttp; + } + } + function checkUrlAccessibility(url2) { + return __async(this, null, function* () { + return new Promise((resolve) => { + const img = new Image(); + img.onload = () => resolve(true); + img.onerror = () => resolve(false); + img.src = url2; + }); + }); + } + return image; + }); } }; @@ -386,41 +430,38 @@ var CodeBlockProcessor = class { titleEl.addClass("auto-card-link-title"); titleEl.textContent = data.title; mainEl.appendChild(titleEl); - const descriptionEl = document.createElement("div"); - descriptionEl.addClass("auto-card-link-description"); if (data.description) { + const descriptionEl = document.createElement("div"); + descriptionEl.addClass("auto-card-link-description"); descriptionEl.textContent = data.description; + mainEl.appendChild(descriptionEl); } - mainEl.appendChild(descriptionEl); const hostEl = document.createElement("div"); hostEl.addClass("auto-card-link-host"); mainEl.appendChild(hostEl); if (data.favicon) { const faviconEl = document.createElement("img"); faviconEl.addClass("auto-card-link-favicon"); - if (data.favicon) { - faviconEl.setAttr("src", data.favicon); - } - faviconEl.setAttr("width", 14); - faviconEl.setAttr("height", 14); - faviconEl.setAttr("alt", ""); + faviconEl.setAttr("src", data.favicon); hostEl.appendChild(faviconEl); } - const hostNameEl = document.createElement("span"); if (data.host) { + const hostNameEl = document.createElement("span"); hostNameEl.textContent = data.host; + hostEl.appendChild(hostNameEl); } - hostEl.appendChild(hostNameEl); - const thumbnailEl = document.createElement("div"); - thumbnailEl.addClass("auto-card-link-thumbnail"); - cardEl.appendChild(thumbnailEl); - const thumbnailImgEl = document.createElement("img"); - thumbnailImgEl.addClass("auto-card-link-thumbnail-img"); if (data.image) { - thumbnailImgEl.setAttr("src", data.image); + const thumbnailEl = document.createElement("img"); + thumbnailEl.addClass("auto-card-link-thumbnail"); + thumbnailEl.setAttr("src", data.image); + thumbnailEl.setAttr("draggable", "false"); + cardEl.appendChild(thumbnailEl); } - thumbnailImgEl.setAttr("alt", ""); - thumbnailEl.appendChild(thumbnailImgEl); + new import_obsidian3.ButtonComponent(containerEl).setClass("auto-card-link-copy-url").setClass("clickable-icon").setIcon("copy").setTooltip(`Copy URL +${data.url}`).onClick(() => { + navigator.clipboard.writeText(data.url); + new import_obsidian3.Notice("URL copied to your clipboard"); + }); return containerEl; } }; diff --git a/.obsidian/plugins/auto-card-link/manifest.json b/.obsidian/plugins/auto-card-link/manifest.json index e63844d8..82d90c6f 100644 --- a/.obsidian/plugins/auto-card-link/manifest.json +++ b/.obsidian/plugins/auto-card-link/manifest.json @@ -1,7 +1,7 @@ { "id": "auto-card-link", "name": "Auto Card Link", - "version": "1.1.2", + "version": "1.2.1", "minAppVersion": "0.12.0", "description": "Automatically fetches metadata from a url and makes it as a card-styled link", "author": "Nekoshita Yuki", diff --git a/.obsidian/plugins/auto-card-link/styles.css b/.obsidian/plugins/auto-card-link/styles.css index f531e7e0..88644aa8 100644 --- a/.obsidian/plugins/auto-card-link/styles.css +++ b/.obsidian/plugins/auto-card-link/styles.css @@ -1,13 +1,13 @@ -.auto-card-link-container { - margin: 0 auto; - border: solid 1px rgba(92, 147, 187, 0.2); - border-radius: 8px; - overflow: hidden; - font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", - "Helvetica Neue", Arial, sans-serif; +.markdown-reading-view .block-language-cardlink { + margin: 1em 0; } .auto-card-link-container { + container-type: inline-size; + position: relative; + overflow: hidden; + user-select: none; + --auto-card-link-indent-size: 2.5em; &[data-auto-card-link-depth="0"] { @@ -36,81 +36,126 @@ } } +@container (max-width: 300px) { + .auto-card-link-thumbnail { + display: none; + } +} + +@container (max-width: 500px) { + .auto-card-link-description { + display: none; + } + .auto-card-link-thumbnail { + max-width: 40% !important; + } + .auto-card-link-title { + white-space: normal !important; + --lh: 1.5em; + line-height: var(--lh); + height: calc(var(--lh) * 3); + } +} + .auto-card-link-error-container { max-width: 780px; - margin: 0 auto; + margin: 1em auto; border-radius: 8px; overflow: hidden; - font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", - "Helvetica Neue", Arial, sans-serif; - background-color: rgb(255, 246, 228); + background-color: var(--background-modifier-error); padding: 10px; + font-family: var(--font-text); + + &:hover { + background: var(--background-modifier-error-hover); + } } .auto-card-link-card { display: flex; - align-items: center; - height: 120px; - line-height: 1.5; - transition: 0.2s; - color: rgba(0, 0, 0, 0.82); - text-decoration: none; - background: #fff; + flex-direction: row-reverse; + height: 8em; + transition: 20ms ease-in 0s; cursor: pointer; + text-decoration: none; + color: var(--link-external-color); + background: var(--background-primary-alt); + border: solid var(--border-width) var(--divider-color); + border-radius: var(--radius-s); + + &:hover { + background: var(--background-modifier-hover); + border-color: var(--background-modifier-hover); + text-decoration: none; + } } .auto-card-link-main { - flex: 1; - height: 100%; - padding: 0.25em 1.2em; - min-width: 0; display: flex; + flex-grow: 1; flex-direction: column; - justify-content: space-around; + justify-content: space-between; + gap: 0.18em; + padding: 0.5em 0.6em; + overflow: hidden; + text-align: left; /* necessary for ellipsis to work */ } .auto-card-link-title { - display: -webkit-box; - -webkit-box-orient: vertical; - -webkit-line-clamp: 2; overflow: hidden; - word-break: break-word; - margin: 0; - font-size: 1em !important; - user-select: none; + white-space: nowrap; + text-overflow: ellipsis; + + &:hover { + color: var(--link-external-color-hover) + } } .auto-card-link-description { - display: -webkit-box; - -webkit-box-orient: vertical; - -webkit-line-clamp: 2; - max-height: 4.65em; overflow: hidden; - color: #77838c; - font-size: 0.8em !important; + --lh: 1.4em; + line-height: var(--lh); + height: calc(var(--lh) * 3); + color: var(--text-muted); + font-size: var(--font-smallest); } .auto-card-link-host { - font-size: 0.78em !important; + font-size: var(--font-smallest); display: flex; + flex-direction: row; align-items: center; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; + + &:hover { + color: var(--link-external-color-hover) + } } .auto-card-link-favicon { - margin-right: 8px; - flex-shrink: 0; + width: 16px; + height: 16px; + margin: 0 0.5em 0 0 !important; } .auto-card-link-thumbnail { - height: 120px; - max-width: 230px; + border-radius: var(--radius-s) 0 0 var(--radius-s) !important; + height: 100%; + object-fit: cover; + max-width: 50% !important; + pointer-events: none; } -.auto-card-link-thumbnail-img { - object-fit: cover; - height: 100%; - flex-shrink: 0; +.auto-card-link-copy-url { + background: var(--background-primary-alt); + position: absolute; + right: 4px; + bottom: 4px; + z-index: 1; + + .is-phone & { + width: var(--input-height); + } } diff --git a/.obsidian/plugins/cron/data.json b/.obsidian/plugins/cron/data.json index ba4c6010..fef95c2e 100644 --- a/.obsidian/plugins/cron/data.json +++ b/.obsidian/plugins/cron/data.json @@ -19,7 +19,7 @@ "601d1cc7-a4f3-4f19-aa9f-3bddd7ab6b1d": { "locked": false, "lockedDeviceName": "iPhone", - "lastRun": "2023-09-12T07:32:02+02:00" + "lastRun": "2023-09-20T07:48:19+02:00" } } } \ No newline at end of file diff --git a/.obsidian/plugins/dataview/main.js b/.obsidian/plugins/dataview/main.js index 82a06086..d4804b43 100644 --- a/.obsidian/plugins/dataview/main.js +++ b/.obsidian/plugins/dataview/main.js @@ -695,7 +695,7 @@ function intlConfigString(localeStr, numberingSystem, outputCalendar) { function mapMonths(f) { const ms = []; for (let i = 1; i <= 12; i++) { - const dt = DateTime.utc(2016, i, 1); + const dt = DateTime.utc(2009, i, 1); ms.push(f(dt)); } return ms; @@ -710,8 +710,8 @@ function mapWeekdays(f) { return ms; } -function listStuff(loc, length, defaultOK, englishFn, intlFn) { - const mode = loc.listingMode(defaultOK); +function listStuff(loc, length, englishFn, intlFn) { + const mode = loc.listingMode(); if (mode === "error") { return null; @@ -772,9 +772,13 @@ class PolyNumberFormatter { class PolyDateFormatter { constructor(dt, intl, opts) { this.opts = opts; + this.originalZone = undefined; let z = undefined; - if (dt.zone.isUniversal) { + if (this.opts.timeZone) { + // Don't apply any workarounds if a timeZone is explicitly provided in opts + this.dt = dt; + } else if (dt.zone.type === "fixed") { // UTC-8 or Etc/UTC-8 are not part of tzdata, only Etc/GMT+8 and the like. // That is why fixed-offset TZ is set to that unless it is: // 1. Representing offset 0 when UTC is used to maintain previous behavior and does not become GMT. @@ -787,25 +791,23 @@ class PolyDateFormatter { z = offsetZ; this.dt = dt; } else { - // Not all fixed-offset zones like Etc/+4:30 are present in tzdata. - // So we have to make do. Two cases: - // 1. The format options tell us to show the zone. We can't do that, so the best - // we can do is format the date in UTC. - // 2. The format options don't tell us to show the zone. Then we can adjust them - // the time and tell the formatter to show it to us in UTC, so that the time is right - // and the bad zone doesn't show up. + // Not all fixed-offset zones like Etc/+4:30 are present in tzdata so + // we manually apply the offset and substitute the zone as needed. z = "UTC"; - if (opts.timeZoneName) { - this.dt = dt; - } else { - this.dt = dt.offset === 0 ? dt : DateTime.fromMillis(dt.ts + dt.offset * 60 * 1000); - } + this.dt = dt.offset === 0 ? dt : dt.setZone("UTC").plus({ minutes: dt.offset }); + this.originalZone = dt.zone; } } else if (dt.zone.type === "system") { this.dt = dt; - } else { + } else if (dt.zone.type === "iana") { this.dt = dt; z = dt.zone.name; + } else { + // Custom zones can have any offset / offsetName so we just manually + // apply the offset and substitute the zone as needed. + z = "UTC"; + this.dt = dt.setZone("UTC").plus({ minutes: dt.offset }); + this.originalZone = dt.zone; } const intlOpts = { ...this.opts }; @@ -814,11 +816,35 @@ class PolyDateFormatter { } format() { + if (this.originalZone) { + // If we have to substitute in the actual zone name, we have to use + // formatToParts so that the timezone can be replaced. + return this.formatToParts() + .map(({ value }) => value) + .join(""); + } return this.dtf.format(this.dt.toJSDate()); } formatToParts() { - return this.dtf.formatToParts(this.dt.toJSDate()); + const parts = this.dtf.formatToParts(this.dt.toJSDate()); + if (this.originalZone) { + return parts.map((part) => { + if (part.type === "timeZoneName") { + const offsetName = this.originalZone.offsetName(this.dt.ts, { + locale: this.dt.locale, + format: this.opts.timeZoneName, + }); + return { + ...part, + value: offsetName, + }; + } else { + return part; + } + }); + } + return parts; } resolvedOptions() { @@ -937,8 +963,8 @@ class Locale { return this.clone({ ...alts, defaultToEN: false }); } - months(length, format = false, defaultOK = true) { - return listStuff(this, length, defaultOK, months, () => { + months(length, format = false) { + return listStuff(this, length, months, () => { const intl = format ? { month: length, day: "numeric" } : { month: length }, formatStr = format ? "format" : "standalone"; if (!this.monthsCache[formatStr][length]) { @@ -948,8 +974,8 @@ class Locale { }); } - weekdays(length, format = false, defaultOK = true) { - return listStuff(this, length, defaultOK, weekdays, () => { + weekdays(length, format = false) { + return listStuff(this, length, weekdays, () => { const intl = format ? { weekday: length, year: "numeric", month: "long", day: "numeric" } : { weekday: length }, @@ -963,11 +989,10 @@ class Locale { }); } - meridiems(defaultOK = true) { + meridiems() { return listStuff( this, undefined, - defaultOK, () => meridiems, () => { // In theory there could be aribitrary day periods. We're gonna assume there are exactly two @@ -984,8 +1009,8 @@ class Locale { ); } - eras(length, defaultOK = true) { - return listStuff(this, length, defaultOK, eras, () => { + eras(length) { + return listStuff(this, length, eras, () => { const intl = { era: length }; // This is problematic. Different calendars are going to define eras totally differently. What I need is the minimum set of dates @@ -1211,7 +1236,7 @@ function normalizeZone(input, defaultZone) { else return FixedOffsetZone.parseSpecifier(lowered) || IANAZone.create(input); } else if (isNumber(input)) { return FixedOffsetZone.instance(input); - } else if (typeof input === "object" && input.offset && typeof input.offset === "number") { + } else if (typeof input === "object" && "offset" in input && typeof input.offset === "function") { // This is dumb, but the instanceof check above doesn't seem to really work // so we're duck checking it return input; @@ -1328,10 +1353,10 @@ class Settings { /** * Set the cutoff year after which a string encoding a year as two digits is interpreted to occur in the current century. * @type {number} - * @example Settings.twoDigitCutoffYear = 0 // cut-off year is 0, so all 'yy' are interpretted as current century + * @example Settings.twoDigitCutoffYear = 0 // cut-off year is 0, so all 'yy' are interpreted as current century * @example Settings.twoDigitCutoffYear = 50 // '49' -> 1949; '50' -> 2050 - * @example Settings.twoDigitCutoffYear = 1950 // interpretted as 50 - * @example Settings.twoDigitCutoffYear = 2050 // ALSO interpretted as 50 + * @example Settings.twoDigitCutoffYear = 1950 // interpreted as 50 + * @example Settings.twoDigitCutoffYear = 2050 // ALSO interpreted as 50 */ static set twoDigitCutoffYear(cutoffYear) { twoDigitCutoffYear = cutoffYear % 100; @@ -1513,7 +1538,7 @@ function daysInMonth(year, month) { } } -// covert a calendar object to a local timestamp (epoch, but with the offset baked in) +// convert a calendar object to a local timestamp (epoch, but with the offset baked in) function objToLocalTS(obj) { let d = Date.UTC( obj.year, @@ -1528,7 +1553,10 @@ function objToLocalTS(obj) { // for legacy reasons, years between 0 and 99 are interpreted as 19XX; revert that if (obj.year < 100 && obj.year >= 0) { d = new Date(d); - d.setUTCFullYear(d.getUTCFullYear() - 1900); + // set the month and day again, this is necessary because year 2000 is a leap year, but year 100 is not + // so if obj.year is in 99, but obj.day makes it roll over into year 100, + // the calculations done by Date.UTC are using year 2000 - which is incorrect + d.setUTCFullYear(obj.year, obj.month - 1, obj.day); } return +d; } @@ -1836,6 +1864,9 @@ class Formatter { } static parseFormat(fmt) { + // white-space is always considered a literal in user-provided formats + // the " " token has a special meaning (see unitForToken) + let current = null, currentFull = "", bracketed = false; @@ -1844,7 +1875,7 @@ class Formatter { const c = fmt.charAt(i); if (c === "'") { if (currentFull.length > 0) { - splits.push({ literal: bracketed, val: currentFull }); + splits.push({ literal: bracketed || /^\s+$/.test(currentFull), val: currentFull }); } current = null; currentFull = ""; @@ -1855,7 +1886,7 @@ class Formatter { currentFull += c; } else { if (currentFull.length > 0) { - splits.push({ literal: false, val: currentFull }); + splits.push({ literal: /^\s+$/.test(currentFull), val: currentFull }); } currentFull = c; current = c; @@ -1863,7 +1894,7 @@ class Formatter { } if (currentFull.length > 0) { - splits.push({ literal: bracketed, val: currentFull }); + splits.push({ literal: bracketed || /^\s+$/.test(currentFull), val: currentFull }); } return splits; @@ -1887,24 +1918,25 @@ class Formatter { return df.format(); } - formatDateTime(dt, opts = {}) { - const df = this.loc.dtFormatter(dt, { ...this.opts, ...opts }); - return df.format(); + dtFormatter(dt, opts = {}) { + return this.loc.dtFormatter(dt, { ...this.opts, ...opts }); + } + + formatDateTime(dt, opts) { + return this.dtFormatter(dt, opts).format(); } - formatDateTimeParts(dt, opts = {}) { - const df = this.loc.dtFormatter(dt, { ...this.opts, ...opts }); - return df.formatToParts(); + formatDateTimeParts(dt, opts) { + return this.dtFormatter(dt, opts).formatToParts(); } - formatInterval(interval, opts = {}) { - const df = this.loc.dtFormatter(interval.start, { ...this.opts, ...opts }); + formatInterval(interval, opts) { + const df = this.dtFormatter(interval.start, opts); return df.dtf.formatRange(interval.start.toJSDate(), interval.end.toJSDate()); } - resolvedOptions(dt, opts = {}) { - const df = this.loc.dtFormatter(dt, { ...this.opts, ...opts }); - return df.resolvedOptions(); + resolvedOptions(dt, opts) { + return this.dtFormatter(dt, opts).resolvedOptions(); } num(n, p = 0) { @@ -1959,7 +1991,7 @@ class Formatter { era = (length) => knownEnglish ? eraForDateTime(dt, length) : string({ era: length }, "era"), tokenToString = (token) => { - // Where possible: http://cldr.unicode.org/translation/date-time-1/date-time#TOC-Standalone-vs.-Format-Styles + // Where possible: https://cldr.unicode.org/translation/date-time/date-time-symbols switch (token) { // ms case "S": @@ -2636,28 +2668,61 @@ function clone$1(dur, alts, clear = false) { return new Duration(conf); } -function antiTrunc(n) { - return n < 0 ? Math.floor(n) : Math.ceil(n); -} - -// NB: mutates parameters -function convert(matrix, fromMap, fromUnit, toMap, toUnit) { - const conv = matrix[toUnit][fromUnit], - raw = fromMap[fromUnit] / conv, - sameSign = Math.sign(raw) === Math.sign(toMap[toUnit]), - // ok, so this is wild, but see the matrix in the tests - added = - !sameSign && toMap[toUnit] !== 0 && Math.abs(raw) <= 1 ? antiTrunc(raw) : Math.trunc(raw); - toMap[toUnit] += added; - fromMap[fromUnit] -= added * conv; +function durationToMillis(matrix, vals) { + let sum = vals.milliseconds ?? 0; + for (const unit of reverseUnits.slice(1)) { + if (vals[unit]) { + sum += vals[unit] * matrix[unit]["milliseconds"]; + } + } + return sum; } // NB: mutates parameters function normalizeValues(matrix, vals) { - reverseUnits.reduce((previous, current) => { + // the logic below assumes the overall value of the duration is positive + // if this is not the case, factor is used to make it so + const factor = durationToMillis(matrix, vals) < 0 ? -1 : 1; + + orderedUnits$1.reduceRight((previous, current) => { + if (!isUndefined(vals[current])) { + if (previous) { + const previousVal = vals[previous] * factor; + const conv = matrix[current][previous]; + + // if (previousVal < 0): + // lower order unit is negative (e.g. { years: 2, days: -2 }) + // normalize this by reducing the higher order unit by the appropriate amount + // and increasing the lower order unit + // this can never make the higher order unit negative, because this function only operates + // on positive durations, so the amount of time represented by the lower order unit cannot + // be larger than the higher order unit + // else: + // lower order unit is positive (e.g. { years: 2, days: 450 } or { years: -2, days: 450 }) + // in this case we attempt to convert as much as possible from the lower order unit into + // the higher order one + // + // Math.floor takes care of both of these cases, rounding away from 0 + // if previousVal < 0 it makes the absolute value larger + // if previousVal >= it makes the absolute value smaller + const rollUp = Math.floor(previousVal / conv); + vals[current] += rollUp * factor; + vals[previous] -= rollUp * conv * factor; + } + return current; + } else { + return previous; + } + }, null); + + // try to convert any decimals into smaller units if possible + // for example for { years: 2.5, days: 0, seconds: 0 } we want to get { years: 2, days: 182, hours: 12 } + orderedUnits$1.reduce((previous, current) => { if (!isUndefined(vals[current])) { if (previous) { - convert(matrix, vals, previous, vals, current); + const fraction = vals[previous] % 1; + vals[previous] -= fraction; + vals[current] += fraction * matrix[previous][current]; } return current; } else { @@ -2973,6 +3038,8 @@ class Duration { * ``` */ toHuman(opts = {}) { + if (!this.isValid) return INVALID$2; + const l = orderedUnits$1 .map((unit) => { const val = this.values[unit]; @@ -3059,26 +3126,11 @@ class Duration { includePrefix: false, format: "extended", ...opts, + includeOffset: false, }; - const value = this.shiftTo("hours", "minutes", "seconds", "milliseconds"); - - let fmt = opts.format === "basic" ? "hhmm" : "hh:mm"; - - if (!opts.suppressSeconds || value.seconds !== 0 || value.milliseconds !== 0) { - fmt += opts.format === "basic" ? "ss" : ":ss"; - if (!opts.suppressMilliseconds || value.milliseconds !== 0) { - fmt += ".SSS"; - } - } - - let str = value.toFormat(fmt); - - if (opts.includePrefix) { - str = "T" + str; - } - - return str; + const dateTime = DateTime.fromMillis(millis, { zone: "UTC" }); + return dateTime.toISOTime(opts); } /** @@ -3102,7 +3154,9 @@ class Duration { * @return {number} */ toMillis() { - return this.as("milliseconds"); + if (!this.isValid) return NaN; + + return durationToMillis(this.matrix, this.values); } /** @@ -3212,8 +3266,17 @@ class Duration { /** * Reduce this Duration to its canonical representation in its current units. + * Assuming the overall value of the Duration is positive, this means: + * - excessive values for lower-order units are converted to higher-order units (if possible, see first and second example) + * - negative lower-order units are converted to higher order units (there must be such a higher order unit, otherwise + * the overall value would be negative, see second example) + * - fractional values for higher-order units are converted to lower-order units (if possible, see fourth example) + * + * If the overall value is negative, the result of this method is equivalent to `this.negate().normalize().negate()`. * @example Duration.fromObject({ years: 2, days: 5000 }).normalize().toObject() //=> { years: 15, days: 255 } + * @example Duration.fromObject({ days: 5000 }).normalize().toObject() //=> { days: 5000 } * @example Duration.fromObject({ hours: 12, minutes: -45 }).normalize().toObject() //=> { hours: 11, minutes: 15 } + * @example Duration.fromObject({ years: 2.5, days: 0, hours: 0 }).normalize().toObject() //=> { years: 2, days: 182, hours: 12 } * @return {Duration} */ normalize() { @@ -3270,16 +3333,12 @@ class Duration { own += vals[k]; } + // only keep the integer part for now in the hopes of putting any decimal part + // into a smaller unit later const i = Math.trunc(own); built[k] = i; accumulated[k] = (own * 1000 - i * 1000) / 1000; - // plus anything further down the chain that should be rolled up in to this - for (const down in vals) { - if (orderedUnits$1.indexOf(down) > orderedUnits$1.indexOf(k)) { - convert(this.matrix, vals, down, built, k); - } - } // otherwise, keep it in the wings to boil it later } else if (isNumber(vals[k])) { accumulated[k] = vals[k]; @@ -3295,7 +3354,8 @@ class Duration { } } - return clone$1(this, { values: built }, true).normalize(); + normalizeValues(this.matrix, built); + return clone$1(this, { values: built }, true); } /** @@ -3692,7 +3752,7 @@ class Interval { if (!this.isValid) return NaN; const start = this.start.startOf(unit), end = this.end.startOf(unit); - return Math.floor(end.diff(start, unit).get(unit)) + 1; + return Math.floor(end.diff(start, unit).get(unit)) + (end.valueOf() !== this.end.valueOf()); } /** @@ -4275,6 +4335,14 @@ function highOrderDiffs(cursor, later, units) { const earlier = cursor; let lowestOrder, highWater; + /* This loop tries to diff using larger units first. + If we overshoot, we backtrack and try the next smaller unit. + "cursor" starts out at the earlier timestamp and moves closer and closer to "later" + as we use smaller and smaller units. + highWater keeps track of where we would be if we added one more of the smallest unit, + this is used later to potentially convert any difference smaller than the smallest higher order unit + into a fraction of that smallest higher order unit + */ for (const [unit, differ] of differs) { if (units.indexOf(unit) >= 0) { lowestOrder = unit; @@ -4283,8 +4351,20 @@ function highOrderDiffs(cursor, later, units) { highWater = earlier.plus(results); if (highWater > later) { + // we overshot the end point, backtrack cursor by 1 results[unit]--; cursor = earlier.plus(results); + + // if we are still overshooting now, we need to backtrack again + // this happens in certain situations when diffing times in different zones, + // because this calculation ignores time zones + if (cursor > later) { + // keep the "overshot by 1" around as highWater + highWater = cursor; + // backtrack cursor by 1 + results[unit]--; + cursor = earlier.plus(results); + } } else { cursor = highWater; } @@ -4447,6 +4527,10 @@ function escapeToken(value) { return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); } +/** + * @param token + * @param {Locale} loc + */ function unitForToken(token, loc) { const one = digitRegex(loc), two = digitRegex(loc, "{2}"), @@ -4467,9 +4551,9 @@ function unitForToken(token, loc) { switch (t.val) { // era case "G": - return oneOf(loc.eras("short", false), 0); + return oneOf(loc.eras("short"), 0); case "GG": - return oneOf(loc.eras("long", false), 0); + return oneOf(loc.eras("long"), 0); // years case "y": return intUnit(oneToSix); @@ -4487,17 +4571,17 @@ function unitForToken(token, loc) { case "MM": return intUnit(two); case "MMM": - return oneOf(loc.months("short", true, false), 1); + return oneOf(loc.months("short", true), 1); case "MMMM": - return oneOf(loc.months("long", true, false), 1); + return oneOf(loc.months("long", true), 1); case "L": return intUnit(oneOrTwo); case "LL": return intUnit(two); case "LLL": - return oneOf(loc.months("short", false, false), 1); + return oneOf(loc.months("short", false), 1); case "LLLL": - return oneOf(loc.months("long", false, false), 1); + return oneOf(loc.months("long", false), 1); // dates case "d": return intUnit(oneOrTwo); @@ -4557,13 +4641,13 @@ function unitForToken(token, loc) { case "c": return intUnit(one); case "EEE": - return oneOf(loc.weekdays("short", false, false), 1); + return oneOf(loc.weekdays("short", false), 1); case "EEEE": - return oneOf(loc.weekdays("long", false, false), 1); + return oneOf(loc.weekdays("long", false), 1); case "ccc": - return oneOf(loc.weekdays("short", true, false), 1); + return oneOf(loc.weekdays("short", true), 1); case "cccc": - return oneOf(loc.weekdays("long", true, false), 1); + return oneOf(loc.weekdays("long", true), 1); // offset/zone case "Z": case "ZZ": @@ -4574,6 +4658,10 @@ function unitForToken(token, loc) { // because we don't have any way to figure out what they are case "z": return simple(/[a-z_+-/]{1,256}?/i); + // this special-case "token" represents a place where a macro-token expanded into a white-space literal + // in this case we accept any non-newline white-space + case " ": + return simple(/[^\S\n\r]/); default: return literal(t); } @@ -4609,10 +4697,14 @@ const partTypeStyleToTokenVal = { }, dayperiod: "a", dayPeriod: "a", - hour: { + hour12: { numeric: "h", "2-digit": "hh", }, + hour24: { + numeric: "H", + "2-digit": "HH", + }, minute: { numeric: "m", "2-digit": "mm", @@ -4627,19 +4719,39 @@ const partTypeStyleToTokenVal = { }, }; -function tokenForPart(part, formatOpts) { +function tokenForPart(part, formatOpts, resolvedOpts) { const { type, value } = part; if (type === "literal") { + const isSpace = /^\s+$/.test(value); return { - literal: true, - val: value, + literal: !isSpace, + val: isSpace ? " " : value, }; } const style = formatOpts[type]; - let val = partTypeStyleToTokenVal[type]; + // The user might have explicitly specified hour12 or hourCycle + // if so, respect their decision + // if not, refer back to the resolvedOpts, which are based on the locale + let actualType = type; + if (type === "hour") { + if (formatOpts.hour12 != null) { + actualType = formatOpts.hour12 ? "hour12" : "hour24"; + } else if (formatOpts.hourCycle != null) { + if (formatOpts.hourCycle === "h11" || formatOpts.hourCycle === "h12") { + actualType = "hour12"; + } else { + actualType = "hour24"; + } + } else { + // tokens only differentiate between 24 hours or not, + // so we do not need to check hourCycle here, which is less supported anyways + actualType = resolvedOpts.hour12 ? "hour12" : "hour24"; + } + } + let val = partTypeStyleToTokenVal[actualType]; if (typeof val === "object") { val = val[style]; } @@ -4828,8 +4940,10 @@ function formatOptsToTokens(formatOpts, locale) { } const formatter = Formatter.create(locale, formatOpts); - const parts = formatter.formatDateTimeParts(getDummyDateTime()); - return parts.map((p) => tokenForPart(p, formatOpts)); + const df = formatter.dtFormatter(getDummyDateTime()); + const parts = df.formatToParts(); + const resolvedOpts = df.resolvedOptions(); + return parts.map((p) => tokenForPart(p, formatOpts, resolvedOpts)); } const nonLeapLadder = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], @@ -5103,7 +5217,7 @@ function adjustTime(inst, dur) { // by handling the zone options function parseDataToDateTime(parsed, parsedZone, opts, format, text, specificOffset) { const { setZone, zone } = opts; - if (parsed && Object.keys(parsed).length !== 0) { + if ((parsed && Object.keys(parsed).length !== 0) || parsedZone) { const interpretationZone = parsedZone || zone, inst = DateTime.fromObject(parsed, { ...opts, @@ -5159,14 +5273,14 @@ function toISOTime( if (extended) { c += ":"; c += padStart(o.c.minute); - if (o.c.second !== 0 || !suppressSeconds) { + if (o.c.millisecond !== 0 || o.c.second !== 0 || !suppressSeconds) { c += ":"; } } else { c += padStart(o.c.minute); } - if (o.c.second !== 0 || !suppressSeconds) { + if (o.c.millisecond !== 0 || o.c.second !== 0 || !suppressSeconds) { c += padStart(o.c.second); if (o.c.millisecond !== 0 || !suppressMilliseconds) { @@ -5829,7 +5943,7 @@ class DateTime { /** * Create an invalid DateTime. - * @param {DateTime} reason - simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent + * @param {string} reason - simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent. * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information * @return {DateTime} */ @@ -6178,6 +6292,43 @@ class DateTime { } } + /** + * Get those DateTimes which have the same local time as this DateTime, but a different offset from UTC + * in this DateTime's zone. During DST changes local time can be ambiguous, for example + * `2023-10-29T02:30:00` in `Europe/Berlin` can have offset `+01:00` or `+02:00`. + * This method will return both possible DateTimes if this DateTime's local time is ambiguous. + * @returns {DateTime[]} + */ + getPossibleOffsets() { + if (!this.isValid || this.isOffsetFixed) { + return [this]; + } + const dayMs = 86400000; + const minuteMs = 60000; + const localTS = objToLocalTS(this.c); + const oEarlier = this.zone.offset(localTS - dayMs); + const oLater = this.zone.offset(localTS + dayMs); + + const o1 = this.zone.offset(localTS - oEarlier * minuteMs); + const o2 = this.zone.offset(localTS - oLater * minuteMs); + if (o1 === o2) { + return [this]; + } + const ts1 = localTS - o1 * minuteMs; + const ts2 = localTS - o2 * minuteMs; + const c1 = tsToObj(ts1, o1); + const c2 = tsToObj(ts2, o2); + if ( + c1.hour === c2.hour && + c1.minute === c2.minute && + c1.second === c2.second && + c1.millisecond === c2.millisecond + ) { + return [clone(this, { ts: ts1 }), clone(this, { ts: ts2 })]; + } + return [this]; + } + /** * Returns true if this DateTime is in a leap year, false otherwise * @example DateTime.local(2016).isInLeapYear //=> true @@ -7180,7 +7331,7 @@ function friendlyDateTime(dateTimeish) { } } -const VERSION = "3.2.1"; +const VERSION = "3.4.3"; var Luxon = /*#__PURE__*/Object.freeze({ __proto__: null, @@ -7197,7303 +7348,7367 @@ var Luxon = /*#__PURE__*/Object.freeze({ Settings: Settings }); -//////////////////// -// Query Settings // -//////////////////// -const DEFAULT_QUERY_SETTINGS = { - renderNullAs: "\\-", - taskCompletionTracking: false, - taskCompletionUseEmojiShorthand: false, - taskCompletionText: "completion", - taskCompletionDateFormat: "yyyy-MM-dd", - recursiveSubTaskCompletion: false, - warnOnEmptyResult: true, - refreshEnabled: true, - refreshInterval: 2500, - defaultDateFormat: "MMMM dd, yyyy", - defaultDateTimeFormat: "h:mm a - MMMM dd, yyyy", - maxRecursiveRenderDepth: 4, - tableIdColumnName: "File", - tableGroupColumnName: "Group", - showResultCount: true, -}; -const DEFAULT_EXPORT_SETTINGS = { - allowHtml: true, -}; -/** Default settings for dataview on install. */ -const DEFAULT_SETTINGS = { - ...DEFAULT_QUERY_SETTINGS, - ...DEFAULT_EXPORT_SETTINGS, - ...{ - inlineQueryPrefix: "=", - inlineJsQueryPrefix: "$=", - inlineQueriesInCodeblocks: true, - enableInlineDataview: true, - enableDataviewJs: false, - enableInlineDataviewJs: false, - prettyRenderInlineFields: true, - dataviewJsKeyword: "dataviewjs", - }, +//////////////////// +// Query Settings // +//////////////////// +const DEFAULT_QUERY_SETTINGS = { + renderNullAs: "\\-", + taskCompletionTracking: false, + taskCompletionUseEmojiShorthand: false, + taskCompletionText: "completion", + taskCompletionDateFormat: "yyyy-MM-dd", + recursiveSubTaskCompletion: false, + warnOnEmptyResult: true, + refreshEnabled: true, + refreshInterval: 2500, + defaultDateFormat: "MMMM dd, yyyy", + defaultDateTimeFormat: "h:mm a - MMMM dd, yyyy", + maxRecursiveRenderDepth: 4, + tableIdColumnName: "File", + tableGroupColumnName: "Group", + showResultCount: true, +}; +const DEFAULT_EXPORT_SETTINGS = { + allowHtml: true, +}; +/** Default settings for dataview on install. */ +const DEFAULT_SETTINGS = { + ...DEFAULT_QUERY_SETTINGS, + ...DEFAULT_EXPORT_SETTINGS, + ...{ + inlineQueryPrefix: "=", + inlineJsQueryPrefix: "$=", + inlineQueriesInCodeblocks: true, + enableInlineDataview: true, + enableDataviewJs: false, + enableInlineDataviewJs: false, + prettyRenderInlineFields: true, + dataviewJsKeyword: "dataviewjs", + }, }; -/** Functional return type for error handling. */ -class Success { - constructor(value) { - this.value = value; - this.successful = true; - } - map(f) { - return new Success(f(this.value)); - } - flatMap(f) { - return f(this.value); - } - mapErr(f) { - return this; - } - bimap(succ, _fail) { - return this.map(succ); - } - orElse(_value) { - return this.value; - } - cast() { - return this; - } - orElseThrow(_message) { - return this.value; - } -} -/** Functional return type for error handling. */ -class Failure { - constructor(error) { - this.error = error; - this.successful = false; - } - map(_f) { - return this; - } - flatMap(_f) { - return this; - } - mapErr(f) { - return new Failure(f(this.error)); - } - bimap(_succ, fail) { - return this.mapErr(fail); - } - orElse(value) { - return value; - } - cast() { - return this; - } - orElseThrow(message) { - if (message) - throw new Error(message(this.error)); - else - throw new Error("" + this.error); - } -} -/** Monadic 'Result' type which encapsulates whether a procedure succeeded or failed, as well as it's return value. */ -var Result; -(function (Result) { - /** Construct a new success result wrapping the given value. */ - function success(value) { - return new Success(value); - } - Result.success = success; - /** Construct a new failure value wrapping the given error. */ - function failure(error) { - return new Failure(error); - } - Result.failure = failure; - /** Join two results with a bi-function and return a new result. */ - function flatMap2(first, second, f) { - if (first.successful) { - if (second.successful) - return f(first.value, second.value); - else - return failure(second.error); - } - else { - return failure(first.error); - } - } - Result.flatMap2 = flatMap2; - /** Join two results with a bi-function and return a new result. */ - function map2(first, second, f) { - return flatMap2(first, second, (a, b) => success(f(a, b))); - } - Result.map2 = map2; +/** Functional return type for error handling. */ +class Success { + constructor(value) { + this.value = value; + this.successful = true; + } + map(f) { + return new Success(f(this.value)); + } + flatMap(f) { + return f(this.value); + } + mapErr(f) { + return this; + } + bimap(succ, _fail) { + return this.map(succ); + } + orElse(_value) { + return this.value; + } + cast() { + return this; + } + orElseThrow(_message) { + return this.value; + } +} +/** Functional return type for error handling. */ +class Failure { + constructor(error) { + this.error = error; + this.successful = false; + } + map(_f) { + return this; + } + flatMap(_f) { + return this; + } + mapErr(f) { + return new Failure(f(this.error)); + } + bimap(_succ, fail) { + return this.mapErr(fail); + } + orElse(value) { + return value; + } + cast() { + return this; + } + orElseThrow(message) { + if (message) + throw new Error(message(this.error)); + else + throw new Error("" + this.error); + } +} +/** Monadic 'Result' type which encapsulates whether a procedure succeeded or failed, as well as it's return value. */ +var Result; +(function (Result) { + /** Construct a new success result wrapping the given value. */ + function success(value) { + return new Success(value); + } + Result.success = success; + /** Construct a new failure value wrapping the given error. */ + function failure(error) { + return new Failure(error); + } + Result.failure = failure; + /** Join two results with a bi-function and return a new result. */ + function flatMap2(first, second, f) { + if (first.successful) { + if (second.successful) + return f(first.value, second.value); + else + return failure(second.error); + } + else { + return failure(first.error); + } + } + Result.flatMap2 = flatMap2; + /** Join two results with a bi-function and return a new result. */ + function map2(first, second, f) { + return flatMap2(first, second, (a, b) => success(f(a, b))); + } + Result.map2 = map2; })(Result || (Result = {})); var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; -function commonjsRequire (path) { - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'); +function getDefaultExportFromCjs (x) { + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } var parsimmon_umd_min = {exports: {}}; +parsimmon_umd_min.exports; + (function (module, exports) { -!function(n,t){module.exports=t();}("undefined"!=typeof self?self:commonjsGlobal,function(){return function(n){var t={};function r(e){if(t[e])return t[e].exports;var u=t[e]={i:e,l:!1,exports:{}};return n[e].call(u.exports,u,u.exports,r),u.l=!0,u.exports}return r.m=n,r.c=t,r.d=function(n,t,e){r.o(n,t)||Object.defineProperty(n,t,{configurable:!1,enumerable:!0,get:e});},r.r=function(n){Object.defineProperty(n,"__esModule",{value:!0});},r.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return r.d(t,"a",t),t},r.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},r.p="",r(r.s=0)}([function(n,t,r){function e(n){if(!(this instanceof e))return new e(n);this._=n;}var u=e.prototype;function o(n,t){for(var r=0;r>7),buf:function(n){var t=i(function(n,t,r,e){return n.concat(r===e.length-1?Buffer.from([t,0]).readUInt16BE(0):e.readUInt16BE(r))},[],n);return Buffer.from(a(function(n){return (n<<1&65535)>>8},t))}(r.buf)};}),r}function c(){return "undefined"!=typeof Buffer}function s(){if(!c())throw new Error("Buffer global does not exist; please use webpack if you need to parse Buffers in the browser.")}function l(n){s();var t=i(function(n,t){return n+t},0,n);if(t%8!=0)throw new Error("The bits ["+n.join(", ")+"] add up to "+t+" which is not an even number of bytes; the total should be divisible by 8");var r,u=t/8,o=(r=function(n){return n>48},i(function(n,t){return n||(r(t)?t:n)},null,n));if(o)throw new Error(o+" bit range requested exceeds 48 bit (6 byte) Number max.");return new e(function(t,r){var e=u+r;return e>t.length?x(r,u.toString()+" bytes"):b(e,i(function(n,t){var r=f(t,n.buf);return {coll:n.coll.concat(r.v),buf:r.buf}},{coll:[],buf:t.slice(r,e)},n).coll)})}function h(n,t){return new e(function(r,e){return s(),e+t>r.length?x(e,t+" bytes for "+n):b(e+t,r.slice(e,e+t))})}function p(n,t){if("number"!=typeof(r=t)||Math.floor(r)!==r||t<0||t>6)throw new Error(n+" requires integer length in range [0, 6].");var r;}function d(n){return p("uintBE",n),h("uintBE("+n+")",n).map(function(t){return t.readUIntBE(0,n)})}function v(n){return p("uintLE",n),h("uintLE("+n+")",n).map(function(t){return t.readUIntLE(0,n)})}function g(n){return p("intBE",n),h("intBE("+n+")",n).map(function(t){return t.readIntBE(0,n)})}function m(n){return p("intLE",n),h("intLE("+n+")",n).map(function(t){return t.readIntLE(0,n)})}function y(n){return n instanceof e}function E(n){return "[object Array]"==={}.toString.call(n)}function w(n){return c()&&Buffer.isBuffer(n)}function b(n,t){return {status:!0,index:n,value:t,furthest:-1,expected:[]}}function x(n,t){return E(t)||(t=[t]),{status:!1,index:-1,value:null,furthest:n,expected:t}}function B(n,t){if(!t)return n;if(n.furthest>t.furthest)return n;var r=n.furthest===t.furthest?function(n,t){if(function(){if(void 0!==e._supportsSet)return e._supportsSet;var n="undefined"!=typeof Set;return e._supportsSet=n,n}()&&Array.from){for(var r=new Set(n),u=0;u=0;){if(i in r){e=r[i].line,0===o&&(o=r[i].lineStart);break}("\n"===n.charAt(i)||"\r"===n.charAt(i)&&"\n"!==n.charAt(i+1))&&(u++,0===o&&(o=i+1)),i--;}var a=e+u,f=t-o;return r[t]={line:a,lineStart:o},{offset:t,line:a+1,column:f+1}}function _(n){if(!y(n))throw new Error("not a parser: "+n)}function L(n,t){return "string"==typeof n?n.charAt(t):n[t]}function O(n){if("number"!=typeof n)throw new Error("not a number: "+n)}function k(n){if("function"!=typeof n)throw new Error("not a function: "+n)}function P(n){if("string"!=typeof n)throw new Error("not a string: "+n)}var q=2,A=3,I=8,F=5*I,M=4*I,z=" ";function R(n,t){return new Array(t+1).join(n)}function U(n,t,r){var e=t-n.length;return e<=0?n:R(r,e)+n}function W(n,t,r,e){return {from:n-t>0?n-t:0,to:n+r>e?e:n+r}}function D(n,t){var r,e,u,o,f,c=t.index,s=c.offset,l=1;if(s===n.length)return "Got the end of the input";if(w(n)){var h=s-s%I,p=s-h,d=W(h,F,M+I,n.length),v=a(function(n){return a(function(n){return U(n.toString(16),2,"0")},n)},function(n,t){var r=n.length,e=[],u=0;if(r<=t)return [n.slice()];for(var o=0;o=4&&(r+=1),l=2,u=a(function(n){return n.length<=4?n.join(" "):n.slice(0,4).join(" ")+" "+n.slice(4).join(" ")},v),(f=(8*(o.to>0?o.to-1:o.to)).toString(16).length)<2&&(f=2);}else {var g=n.split(/\r\n|[\n\r\u2028\u2029]/);r=c.column-1,e=c.line-1,o=W(e,q,A,g.length),u=g.slice(o.from,o.to),f=o.to.toString().length;}var m=e-o.from;return w(n)&&(f=(8*(o.to>0?o.to-1:o.to)).toString(16).length)<2&&(f=2),i(function(t,e,u){var i,a=u===m,c=a?"> ":z;return i=w(n)?U((8*(o.from+u)).toString(16),f,"0"):U((o.from+u+1).toString(),f," "),[].concat(t,[c+i+" | "+e],a?[z+R(" ",f)+" | "+U("",r," ")+R("^",l)]:[])},[],u).join("\n")}function N(n,t){return ["\n","-- PARSING FAILED "+R("-",50),"\n\n",D(n,t),"\n\n",(r=t.expected,1===r.length?"Expected:\n\n"+r[0]:"Expected one of the following: \n\n"+r.join(", ")),"\n"].join("");var r;}function G(n){return void 0!==n.flags?n.flags:[n.global?"g":"",n.ignoreCase?"i":"",n.multiline?"m":"",n.unicode?"u":"",n.sticky?"y":""].join("")}function C(){for(var n=[].slice.call(arguments),t=n.length,r=0;r=2?O(t):t=0;var r=function(n){return RegExp("^(?:"+n.source+")",G(n))}(n),u=""+n;return e(function(n,e){var o=r.exec(n.slice(e));if(o){if(0<=t&&t<=o.length){var i=o[0],a=o[t];return b(e+i.length,a)}return x(e,"valid match group (0 to "+o.length+") in "+u)}return x(e,u)})}function X(n){return e(function(t,r){return b(r,n)})}function Y(n){return e(function(t,r){return x(r,n)})}function Z(n){if(y(n))return e(function(t,r){var e=n._(t,r);return e.index=r,e.value="",e});if("string"==typeof n)return Z(K(n));if(n instanceof RegExp)return Z(Q(n));throw new Error("not a string, regexp, or parser: "+n)}function $(n){return _(n),e(function(t,r){var e=n._(t,r),u=t.slice(r,e.index);return e.status?x(r,'not "'+u+'"'):b(r,null)})}function nn(n){return k(n),e(function(t,r){var e=L(t,r);return r=n.length?x(t,"any character/byte"):b(t+1,L(n,t))}),on=e(function(n,t){return b(n.length,n.slice(t))}),an=e(function(n,t){return t=0}).desc(t)},e.optWhitespace=hn,e.Parser=e,e.range=function(n,t){return nn(function(r){return n<=r&&r<=t}).desc(n+"-"+t)},e.regex=Q,e.regexp=Q,e.sepBy=V,e.sepBy1=H,e.seq=C,e.seqMap=J,e.seqObj=function(){for(var n,t={},r=0,u=(n=arguments,Array.prototype.slice.call(n)),o=u.length,i=0;i255)throw new Error("Value specified to byte constructor ("+n+"=0x"+n.toString(16)+") is larger in value than a single byte.");var t=(n>15?"0x":"0x0")+n.toString(16);return e(function(r,e){var u=L(r,e);return u===n?b(e+1,u):x(e,t)})},buffer:function(n){return h("buffer",n).map(function(n){return Buffer.from(n)})},encodedString:function(n,t){return h("string",t).map(function(t){return t.toString(n)})},uintBE:d,uint8BE:d(1),uint16BE:d(2),uint32BE:d(4),uintLE:v,uint8LE:v(1),uint16LE:v(2),uint32LE:v(4),intBE:g,int8BE:g(1),int16BE:g(2),int32BE:g(4),intLE:m,int8LE:m(1),int16LE:m(2),int32LE:m(4),floatBE:h("floatBE",4).map(function(n){return n.readFloatBE(0)}),floatLE:h("floatLE",4).map(function(n){return n.readFloatLE(0)}),doubleBE:h("doubleBE",8).map(function(n){return n.readDoubleBE(0)}),doubleLE:h("doubleLE",8).map(function(n){return n.readDoubleLE(0)})},n.exports=e;}])}); -}(parsimmon_umd_min)); + !function(n,t){module.exports=t();}("undefined"!=typeof self?self:commonjsGlobal,function(){return function(n){var t={};function r(e){if(t[e])return t[e].exports;var u=t[e]={i:e,l:!1,exports:{}};return n[e].call(u.exports,u,u.exports,r),u.l=!0,u.exports}return r.m=n,r.c=t,r.d=function(n,t,e){r.o(n,t)||Object.defineProperty(n,t,{configurable:!1,enumerable:!0,get:e});},r.r=function(n){Object.defineProperty(n,"__esModule",{value:!0});},r.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return r.d(t,"a",t),t},r.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},r.p="",r(r.s=0)}([function(n,t,r){function e(n){if(!(this instanceof e))return new e(n);this._=n;}var u=e.prototype;function o(n,t){for(var r=0;r>7),buf:function(n){var t=i(function(n,t,r,e){return n.concat(r===e.length-1?Buffer.from([t,0]).readUInt16BE(0):e.readUInt16BE(r))},[],n);return Buffer.from(a(function(n){return (n<<1&65535)>>8},t))}(r.buf)};}),r}function c(){return "undefined"!=typeof Buffer}function s(){if(!c())throw new Error("Buffer global does not exist; please use webpack if you need to parse Buffers in the browser.")}function l(n){s();var t=i(function(n,t){return n+t},0,n);if(t%8!=0)throw new Error("The bits ["+n.join(", ")+"] add up to "+t+" which is not an even number of bytes; the total should be divisible by 8");var r,u=t/8,o=(r=function(n){return n>48},i(function(n,t){return n||(r(t)?t:n)},null,n));if(o)throw new Error(o+" bit range requested exceeds 48 bit (6 byte) Number max.");return new e(function(t,r){var e=u+r;return e>t.length?x(r,u.toString()+" bytes"):b(e,i(function(n,t){var r=f(t,n.buf);return {coll:n.coll.concat(r.v),buf:r.buf}},{coll:[],buf:t.slice(r,e)},n).coll)})}function h(n,t){return new e(function(r,e){return s(),e+t>r.length?x(e,t+" bytes for "+n):b(e+t,r.slice(e,e+t))})}function p(n,t){if("number"!=typeof(r=t)||Math.floor(r)!==r||t<0||t>6)throw new Error(n+" requires integer length in range [0, 6].");var r;}function d(n){return p("uintBE",n),h("uintBE("+n+")",n).map(function(t){return t.readUIntBE(0,n)})}function v(n){return p("uintLE",n),h("uintLE("+n+")",n).map(function(t){return t.readUIntLE(0,n)})}function g(n){return p("intBE",n),h("intBE("+n+")",n).map(function(t){return t.readIntBE(0,n)})}function m(n){return p("intLE",n),h("intLE("+n+")",n).map(function(t){return t.readIntLE(0,n)})}function y(n){return n instanceof e}function E(n){return "[object Array]"==={}.toString.call(n)}function w(n){return c()&&Buffer.isBuffer(n)}function b(n,t){return {status:!0,index:n,value:t,furthest:-1,expected:[]}}function x(n,t){return E(t)||(t=[t]),{status:!1,index:-1,value:null,furthest:n,expected:t}}function B(n,t){if(!t)return n;if(n.furthest>t.furthest)return n;var r=n.furthest===t.furthest?function(n,t){if(function(){if(void 0!==e._supportsSet)return e._supportsSet;var n="undefined"!=typeof Set;return e._supportsSet=n,n}()&&Array.from){for(var r=new Set(n),u=0;u=0;){if(i in r){e=r[i].line,0===o&&(o=r[i].lineStart);break}("\n"===n.charAt(i)||"\r"===n.charAt(i)&&"\n"!==n.charAt(i+1))&&(u++,0===o&&(o=i+1)),i--;}var a=e+u,f=t-o;return r[t]={line:a,lineStart:o},{offset:t,line:a+1,column:f+1}}function _(n){if(!y(n))throw new Error("not a parser: "+n)}function L(n,t){return "string"==typeof n?n.charAt(t):n[t]}function O(n){if("number"!=typeof n)throw new Error("not a number: "+n)}function k(n){if("function"!=typeof n)throw new Error("not a function: "+n)}function P(n){if("string"!=typeof n)throw new Error("not a string: "+n)}var q=2,A=3,I=8,F=5*I,M=4*I,z=" ";function R(n,t){return new Array(t+1).join(n)}function U(n,t,r){var e=t-n.length;return e<=0?n:R(r,e)+n}function W(n,t,r,e){return {from:n-t>0?n-t:0,to:n+r>e?e:n+r}}function D(n,t){var r,e,u,o,f,c=t.index,s=c.offset,l=1;if(s===n.length)return "Got the end of the input";if(w(n)){var h=s-s%I,p=s-h,d=W(h,F,M+I,n.length),v=a(function(n){return a(function(n){return U(n.toString(16),2,"0")},n)},function(n,t){var r=n.length,e=[],u=0;if(r<=t)return [n.slice()];for(var o=0;o=4&&(r+=1),l=2,u=a(function(n){return n.length<=4?n.join(" "):n.slice(0,4).join(" ")+" "+n.slice(4).join(" ")},v),(f=(8*(o.to>0?o.to-1:o.to)).toString(16).length)<2&&(f=2);}else {var g=n.split(/\r\n|[\n\r\u2028\u2029]/);r=c.column-1,e=c.line-1,o=W(e,q,A,g.length),u=g.slice(o.from,o.to),f=o.to.toString().length;}var m=e-o.from;return w(n)&&(f=(8*(o.to>0?o.to-1:o.to)).toString(16).length)<2&&(f=2),i(function(t,e,u){var i,a=u===m,c=a?"> ":z;return i=w(n)?U((8*(o.from+u)).toString(16),f,"0"):U((o.from+u+1).toString(),f," "),[].concat(t,[c+i+" | "+e],a?[z+R(" ",f)+" | "+U("",r," ")+R("^",l)]:[])},[],u).join("\n")}function N(n,t){return ["\n","-- PARSING FAILED "+R("-",50),"\n\n",D(n,t),"\n\n",(r=t.expected,1===r.length?"Expected:\n\n"+r[0]:"Expected one of the following: \n\n"+r.join(", ")),"\n"].join("");var r;}function G(n){return void 0!==n.flags?n.flags:[n.global?"g":"",n.ignoreCase?"i":"",n.multiline?"m":"",n.unicode?"u":"",n.sticky?"y":""].join("")}function C(){for(var n=[].slice.call(arguments),t=n.length,r=0;r=2?O(t):t=0;var r=function(n){return RegExp("^(?:"+n.source+")",G(n))}(n),u=""+n;return e(function(n,e){var o=r.exec(n.slice(e));if(o){if(0<=t&&t<=o.length){var i=o[0],a=o[t];return b(e+i.length,a)}return x(e,"valid match group (0 to "+o.length+") in "+u)}return x(e,u)})}function X(n){return e(function(t,r){return b(r,n)})}function Y(n){return e(function(t,r){return x(r,n)})}function Z(n){if(y(n))return e(function(t,r){var e=n._(t,r);return e.index=r,e.value="",e});if("string"==typeof n)return Z(K(n));if(n instanceof RegExp)return Z(Q(n));throw new Error("not a string, regexp, or parser: "+n)}function $(n){return _(n),e(function(t,r){var e=n._(t,r),u=t.slice(r,e.index);return e.status?x(r,'not "'+u+'"'):b(r,null)})}function nn(n){return k(n),e(function(t,r){var e=L(t,r);return r=n.length?x(t,"any character/byte"):b(t+1,L(n,t))}),on=e(function(n,t){return b(n.length,n.slice(t))}),an=e(function(n,t){return t=0}).desc(t)},e.optWhitespace=hn,e.Parser=e,e.range=function(n,t){return nn(function(r){return n<=r&&r<=t}).desc(n+"-"+t)},e.regex=Q,e.regexp=Q,e.sepBy=V,e.sepBy1=H,e.seq=C,e.seqMap=J,e.seqObj=function(){for(var n,t={},r=0,u=(n=arguments,Array.prototype.slice.call(n)),o=u.length,i=0;i255)throw new Error("Value specified to byte constructor ("+n+"=0x"+n.toString(16)+") is larger in value than a single byte.");var t=(n>15?"0x":"0x0")+n.toString(16);return e(function(r,e){var u=L(r,e);return u===n?b(e+1,u):x(e,t)})},buffer:function(n){return h("buffer",n).map(function(n){return Buffer.from(n)})},encodedString:function(n,t){return h("string",t).map(function(t){return t.toString(n)})},uintBE:d,uint8BE:d(1),uint16BE:d(2),uint32BE:d(4),uintLE:v,uint8LE:v(1),uint16LE:v(2),uint32LE:v(4),intBE:g,int8BE:g(1),int16BE:g(2),int32BE:g(4),intLE:m,int8LE:m(1),int16LE:m(2),int32LE:m(4),floatBE:h("floatBE",4).map(function(n){return n.readFloatBE(0)}),floatLE:h("floatLE",4).map(function(n){return n.readFloatLE(0)}),doubleBE:h("doubleBE",8).map(function(n){return n.readDoubleBE(0)}),doubleLE:h("doubleLE",8).map(function(n){return n.readDoubleLE(0)})},n.exports=e;}])}); +} (parsimmon_umd_min, parsimmon_umd_min.exports)); + +var parsimmon_umd_minExports = parsimmon_umd_min.exports; var emojiRegex = () => { // https://mths.be/emoji - return /(?:[#*0-9]\uFE0F?\u20E3|[\xA9\xAE\u203C\u2049\u2122\u2139\u2194-\u2199\u21A9\u21AA\u231A\u231B\u2328\u23CF\u23ED-\u23EF\u23F1\u23F2\u23F8-\u23FA\u24C2\u25AA\u25AB\u25B6\u25C0\u25FB\u25FC\u25FE\u2600-\u2604\u260E\u2611\u2614\u2615\u2618\u2620\u2622\u2623\u2626\u262A\u262E\u262F\u2638-\u263A\u2640\u2642\u2648-\u2653\u265F\u2660\u2663\u2665\u2666\u2668\u267B\u267E\u267F\u2692\u2694-\u2697\u2699\u269B\u269C\u26A0\u26A7\u26AA\u26B0\u26B1\u26BD\u26BE\u26C4\u26C8\u26CF\u26D1\u26D3\u26E9\u26F0-\u26F5\u26F7\u26F8\u26FA\u2702\u2708\u2709\u270F\u2712\u2714\u2716\u271D\u2721\u2733\u2734\u2744\u2747\u2757\u2763\u27A1\u2934\u2935\u2B05-\u2B07\u2B1B\u2B1C\u2B55\u3030\u303D\u3297\u3299]\uFE0F?|[\u261D\u270C\u270D](?:\uFE0F|\uD83C[\uDFFB-\uDFFF])?|[\u270A\u270B](?:\uD83C[\uDFFB-\uDFFF])?|[\u23E9-\u23EC\u23F0\u23F3\u25FD\u2693\u26A1\u26AB\u26C5\u26CE\u26D4\u26EA\u26FD\u2705\u2728\u274C\u274E\u2753-\u2755\u2795-\u2797\u27B0\u27BF\u2B50]|\u26F9(?:\uFE0F|\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|\u2764\uFE0F?(?:\u200D(?:\uD83D\uDD25|\uD83E\uDE79))?|\uD83C(?:[\uDC04\uDD70\uDD71\uDD7E\uDD7F\uDE02\uDE37\uDF21\uDF24-\uDF2C\uDF36\uDF7D\uDF96\uDF97\uDF99-\uDF9B\uDF9E\uDF9F\uDFCD\uDFCE\uDFD4-\uDFDF\uDFF5\uDFF7]\uFE0F?|[\uDF85\uDFC2\uDFC7](?:\uD83C[\uDFFB-\uDFFF])?|[\uDFC3\uDFC4\uDFCA](?:\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|[\uDFCB\uDFCC](?:\uFE0F|\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|[\uDCCF\uDD8E\uDD91-\uDD9A\uDE01\uDE1A\uDE2F\uDE32-\uDE36\uDE38-\uDE3A\uDE50\uDE51\uDF00-\uDF20\uDF2D-\uDF35\uDF37-\uDF7C\uDF7E-\uDF84\uDF86-\uDF93\uDFA0-\uDFC1\uDFC5\uDFC6\uDFC8\uDFC9\uDFCF-\uDFD3\uDFE0-\uDFF0\uDFF8-\uDFFF]|\uDDE6\uD83C[\uDDE8-\uDDEC\uDDEE\uDDF1\uDDF2\uDDF4\uD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\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|[\uDD74\uDD90](?:\uFE0F|\uD83C[\uDFFB-\uDFFF])?|[\uDC00-\uDC07\uDC09-\uDC14\uDC16-\uDC3A\uDC3C-\uDC3E\uDC40\uDC44\uDC45\uDC51-\uDC65\uDC6A\uDC79-\uDC7B\uDC7D-\uDC80\uDC84\uDC88-\uDC8E\uDC90\uDC92-\uDCA9\uDCAB-\uDCFC\uDCFF-\uDD3D\uDD4B-\uDD4E\uDD50-\uDD67\uDDA4\uDDFB-\uDE2D\uDE2F-\uDE34\uDE37-\uDE44\uDE48-\uDE4A\uDE80-\uDEA2\uDEA4-\uDEB3\uDEB7-\uDEBF\uDEC1-\uDEC5\uDED0-\uDED2\uDED5-\uDED7\uDEDD-\uDEDF\uDEEB\uDEEC\uDEF4-\uDEFC\uDFE0-\uDFEB\uDFF0]|\uDC08(?:\u200D\u2B1B)?|\uDC15(?:\u200D\uD83E\uDDBA)?|\uDC3B(?:\u200D\u2744\uFE0F?)?|\uDC41\uFE0F?(?:\u200D\uD83D\uDDE8\uFE0F?)?|\uDC68(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D(?:[\uDC68\uDC69]\u200D\uD83D(?:\uDC66(?:\u200D\uD83D\uDC66)?|\uDC67(?:\u200D\uD83D[\uDC66\uDC67])?)|[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uDC66(?:\u200D\uD83D\uDC66)?|\uDC67(?:\u200D\uD83D[\uDC66\uDC67])?)|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C(?:\uDFFB(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFC-\uDFFF])))?|\uDFFC(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFB\uDFFD-\uDFFF])))?|\uDFFD(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])))?|\uDFFE(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFB-\uDFFD\uDFFF])))?|\uDFFF(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFB-\uDFFE])))?))?|\uDC69(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?[\uDC68\uDC69]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D(?:[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uDC66(?:\u200D\uD83D\uDC66)?|\uDC67(?:\u200D\uD83D[\uDC66\uDC67])?|\uDC69\u200D\uD83D(?:\uDC66(?:\u200D\uD83D\uDC66)?|\uDC67(?:\u200D\uD83D[\uDC66\uDC67])?))|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C(?:\uDFFB(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFC-\uDFFF])))?|\uDFFC(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFB\uDFFD-\uDFFF])))?|\uDFFD(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])))?|\uDFFE(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFB-\uDFFD\uDFFF])))?|\uDFFF(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFB-\uDFFE])))?))?|\uDC6F(?:\u200D[\u2640\u2642]\uFE0F?)?|\uDD75(?:\uFE0F|\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|\uDE2E(?:\u200D\uD83D\uDCA8)?|\uDE35(?:\u200D\uD83D\uDCAB)?|\uDE36(?:\u200D\uD83C\uDF2B\uFE0F?)?)|\uD83E(?:[\uDD0C\uDD0F\uDD18-\uDD1F\uDD30-\uDD34\uDD36\uDD77\uDDB5\uDDB6\uDDBB\uDDD2\uDDD3\uDDD5\uDEC3-\uDEC5\uDEF0\uDEF2-\uDEF6](?:\uD83C[\uDFFB-\uDFFF])?|[\uDD26\uDD35\uDD37-\uDD39\uDD3D\uDD3E\uDDB8\uDDB9\uDDCD-\uDDCF\uDDD4\uDDD6-\uDDDD](?:\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|[\uDDDE\uDDDF](?:\u200D[\u2640\u2642]\uFE0F?)?|[\uDD0D\uDD0E\uDD10-\uDD17\uDD20-\uDD25\uDD27-\uDD2F\uDD3A\uDD3F-\uDD45\uDD47-\uDD76\uDD78-\uDDB4\uDDB7\uDDBA\uDDBC-\uDDCC\uDDD0\uDDE0-\uDDFF\uDE70-\uDE74\uDE78-\uDE7C\uDE80-\uDE86\uDE90-\uDEAC\uDEB0-\uDEBA\uDEC0-\uDEC2\uDED0-\uDED9\uDEE0-\uDEE7]|\uDD3C(?:\u200D[\u2640\u2642]\uFE0F?|\uD83C[\uDFFB-\uDFFF])?|\uDDD1(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1))|\uD83C(?:\uDFFB(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFC-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?|\uDFFC(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFB\uDFFD-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?|\uDFFD(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?|\uDFFE(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFB-\uDFFD\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?|\uDFFF(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFB-\uDFFE]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?))?|\uDEF1(?:\uD83C(?:\uDFFB(?:\u200D\uD83E\uDEF2\uD83C[\uDFFC-\uDFFF])?|\uDFFC(?:\u200D\uD83E\uDEF2\uD83C[\uDFFB\uDFFD-\uDFFF])?|\uDFFD(?:\u200D\uD83E\uDEF2\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])?|\uDFFE(?:\u200D\uD83E\uDEF2\uD83C[\uDFFB-\uDFFD\uDFFF])?|\uDFFF(?:\u200D\uD83E\uDEF2\uD83C[\uDFFB-\uDFFE])?))?))/g; -}; - -/** Normalize a duration to all of the proper units. */ -function normalizeDuration(dur) { - if (dur === undefined || dur === null) - return dur; - return dur.shiftToAll().normalize(); -} -/** Strip the time components of a date time object. */ -function stripTime(dt) { - if (dt === null || dt === undefined) - return dt; - return DateTime.fromObject({ - year: dt.year, - month: dt.month, - day: dt.day, - }); -} -/** Get the folder containing the given path (i.e., like computing 'path/..'). */ -function getParentFolder(path) { - return path.split("/").slice(0, -1).join("/"); -} -/** Get the "title" for a file, by stripping other parts of the path as well as the extension. */ -function getFileTitle(path) { - if (path.includes("/")) - path = path.substring(path.lastIndexOf("/") + 1); - if (path.endsWith(".md")) - path = path.substring(0, path.length - 3); - return path; -} -/** Get the extension of a file from the file path. */ -function getExtension(path) { - if (!path.includes(".")) - return ""; - return path.substring(path.lastIndexOf(".") + 1); -} -/** Parse all subtags out of the given tag. I.e., #hello/i/am would yield [#hello/i/am, #hello/i, #hello]. */ -function extractSubtags(tag) { - let result = [tag]; - while (tag.includes("/")) { - tag = tag.substring(0, tag.lastIndexOf("/")); - result.push(tag); - } - return result; -} -/** Try calling the given function; on failure, return the error message. */ -function tryOrPropogate(func) { - try { - return func(); - } - catch (error) { - return Result.failure("" + error + "\n\n" + error.stack); - } -} -/** Try asynchronously calling the given function; on failure, return the error message. */ -async function asyncTryOrPropogate(func) { - try { - return await func(); - } - catch (error) { - return Result.failure("" + error + "\n\n" + error.stack); - } -} -/** - * Escape regex characters in a string. - * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions. - */ -function escapeRegex(str) { - return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); -} -/** A parsimmon parser which canonicalizes variable names while properly respecting emoji. */ -const VAR_NAME_CANONICALIZER = parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.regex(new RegExp(emojiRegex(), "")), parsimmon_umd_min.exports.regex(/[0-9\p{Letter}_-]+/u).map(str => str.toLocaleLowerCase()), parsimmon_umd_min.exports.whitespace.map(_ => "-"), parsimmon_umd_min.exports.any.map(_ => "")) - .many() - .map(result => result.join("")); -/** Convert an arbitrary variable name into something JS/query friendly. */ -function canonicalizeVarName(name) { - return VAR_NAME_CANONICALIZER.tryParse(name); -} -const HEADER_CANONICALIZER = parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.regex(new RegExp(emojiRegex(), "")), parsimmon_umd_min.exports.regex(/[0-9\p{Letter}_-]+/u), parsimmon_umd_min.exports.whitespace.map(_ => " "), parsimmon_umd_min.exports.any.map(_ => " ")) - .many() - .map(result => { - return result.join("").split(/\s+/).join(" ").trim(); -}); -/** - * Normalizes the text in a header to be something that is actually linkable to. This mimics - * how Obsidian does it's normalization, collapsing repeated spaces and stripping out control characters. - */ -function normalizeHeaderForLink(header) { - return HEADER_CANONICALIZER.tryParse(header); -} -/** Render a DateTime in a minimal format to save space. */ -function renderMinimalDate(time, settings, locale) { - // If there is no relevant time specified, fall back to just rendering the date. - if (time.second == 0 && time.minute == 0 && time.hour == 0) { - return time.toLocal().toFormat(settings.defaultDateFormat, { locale }); - } - return time.toLocal().toFormat(settings.defaultDateTimeFormat, { locale }); -} -/** Render a duration in a minimal format to save space. */ -function renderMinimalDuration(dur) { - dur = normalizeDuration(dur); - // toHuman outputs zero quantities e.g. "0 seconds" - dur = Duration.fromObject(Object.fromEntries(Object.entries(dur.toObject()).filter(([, quantity]) => quantity > 0))); - return dur.toHuman(); -} -/** Determine if two sets are equal in contents. */ -function setsEqual(first, second) { - if (first.size != second.size) - return false; - for (let elem of first) - if (!second.has(elem)) - return false; - return true; -} - -var Values; -(function (Values) { - /** Convert an arbitrary value into a reasonable, Markdown-friendly string if possible. */ - function toString(field, setting = DEFAULT_QUERY_SETTINGS, recursive = false) { - let wrapped = wrapValue(field); - if (!wrapped) - return setting.renderNullAs; - switch (wrapped.type) { - case "null": - return setting.renderNullAs; - case "string": - return wrapped.value; - case "number": - case "boolean": - return "" + wrapped.value; - case "html": - return wrapped.value.outerHTML; - case "widget": - return wrapped.value.markdown(); - case "link": - return wrapped.value.markdown(); - case "function": - return ""; - case "array": - let result = ""; - if (recursive) - result += "["; - result += wrapped.value.map(f => toString(f, setting, true)).join(", "); - if (recursive) - result += "]"; - return result; - case "object": - return ("{ " + - Object.entries(wrapped.value) - .map(e => e[0] + ": " + toString(e[1], setting, true)) - .join(", ") + - " }"); - case "date": - if (wrapped.value.second == 0 && wrapped.value.hour == 0 && wrapped.value.minute == 0) { - return wrapped.value.toFormat(setting.defaultDateFormat); - } - return wrapped.value.toFormat(setting.defaultDateTimeFormat); - case "duration": - return renderMinimalDuration(wrapped.value); - } - } - Values.toString = toString; - /** Wrap a literal value so you can switch on it easily. */ - function wrapValue(val) { - if (isNull(val)) - return { type: "null", value: val }; - else if (isNumber(val)) - return { type: "number", value: val }; - else if (isString(val)) - return { type: "string", value: val }; - else if (isBoolean(val)) - return { type: "boolean", value: val }; - else if (isDuration(val)) - return { type: "duration", value: val }; - else if (isDate(val)) - return { type: "date", value: val }; - else if (isWidget(val)) - return { type: "widget", value: val }; - else if (isArray(val)) - return { type: "array", value: val }; - else if (isLink(val)) - return { type: "link", value: val }; - else if (isFunction(val)) - return { type: "function", value: val }; - else if (isHtml(val)) - return { type: "html", value: val }; - else if (isObject(val)) - return { type: "object", value: val }; - else - return undefined; - } - Values.wrapValue = wrapValue; - /** Recursively map complex objects at the leaves. */ - function mapLeaves(val, func) { - if (isObject(val)) { - let result = {}; - for (let [key, value] of Object.entries(val)) - result[key] = mapLeaves(value, func); - return result; - } - else if (isArray(val)) { - let result = []; - for (let value of val) - result.push(mapLeaves(value, func)); - return result; - } - else { - return func(val); - } - } - Values.mapLeaves = mapLeaves; - /** Compare two arbitrary JavaScript values. Produces a total ordering over ANY possible dataview value. */ - function compareValue(val1, val2, linkNormalizer) { - var _a, _b; - // Handle undefined/nulls first. - if (val1 === undefined) - val1 = null; - if (val2 === undefined) - val2 = null; - if (val1 === null && val2 === null) - return 0; - else if (val1 === null) - return -1; - else if (val2 === null) - return 1; - // A non-null value now which we can wrap & compare on. - let wrap1 = wrapValue(val1); - let wrap2 = wrapValue(val2); - if (wrap1 === undefined && wrap2 === undefined) - return 0; - else if (wrap1 === undefined) - return -1; - else if (wrap2 === undefined) - return 1; - // Short-circuit on different types or on reference equality. - if (wrap1.type != wrap2.type) - return wrap1.type.localeCompare(wrap2.type); - if (wrap1.value === wrap2.value) - return 0; - switch (wrap1.type) { - case "string": - return wrap1.value.localeCompare(wrap2.value); - case "number": - if (wrap1.value < wrap2.value) - return -1; - else if (wrap1.value == wrap2.value) - return 0; - return 1; - case "null": - return 0; - case "boolean": - if (wrap1.value == wrap2.value) - return 0; - else - return wrap1.value ? 1 : -1; - case "link": - let link1 = wrap1.value; - let link2 = wrap2.value; - let normalize = linkNormalizer !== null && linkNormalizer !== void 0 ? linkNormalizer : ((x) => x); - // We can't compare by file name or display, since that would break link equality. Compare by path. - let pathCompare = normalize(link1.path).localeCompare(normalize(link2.path)); - if (pathCompare != 0) - return pathCompare; - // Then compare by type. - let typeCompare = link1.type.localeCompare(link2.type); - if (typeCompare != 0) - return typeCompare; - // Then compare by subpath existence. - if (link1.subpath && !link2.subpath) - return 1; - if (!link1.subpath && link2.subpath) - return -1; - if (!link1.subpath && !link2.subpath) - return 0; - // Since both have a subpath, compare by subpath. - return ((_a = link1.subpath) !== null && _a !== void 0 ? _a : "").localeCompare((_b = link2.subpath) !== null && _b !== void 0 ? _b : ""); - case "date": - return wrap1.value < wrap2.value - ? -1 - : wrap1.value.equals(wrap2.value) - ? 0 - : 1; - case "duration": - return wrap1.value < wrap2.value - ? -1 - : wrap1.value.equals(wrap2.value) - ? 0 - : 1; - case "array": - let f1 = wrap1.value; - let f2 = wrap2.value; - for (let index = 0; index < Math.min(f1.length, f2.length); index++) { - let comp = compareValue(f1[index], f2[index]); - if (comp != 0) - return comp; - } - return f1.length - f2.length; - case "object": - let o1 = wrap1.value; - let o2 = wrap2.value; - let k1 = Array.from(Object.keys(o1)); - let k2 = Array.from(Object.keys(o2)); - k1.sort(); - k2.sort(); - let keyCompare = compareValue(k1, k2); - if (keyCompare != 0) - return keyCompare; - for (let key of k1) { - let comp = compareValue(o1[key], o2[key]); - if (comp != 0) - return comp; - } - return 0; - case "widget": - case "html": - case "function": - return 0; - } - } - Values.compareValue = compareValue; - /** Find the corresponding Dataveiw type for an arbitrary value. */ - function typeOf(val) { - var _a; - return (_a = wrapValue(val)) === null || _a === void 0 ? void 0 : _a.type; - } - Values.typeOf = typeOf; - /** Determine if the given value is "truthy" (i.e., is non-null and has data in it). */ - function isTruthy(field) { - let wrapped = wrapValue(field); - if (!wrapped) - return false; - switch (wrapped.type) { - case "number": - return wrapped.value != 0; - case "string": - return wrapped.value.length > 0; - case "boolean": - return wrapped.value; - case "link": - return !!wrapped.value.path; - case "date": - return wrapped.value.toMillis() != 0; - case "duration": - return wrapped.value.as("seconds") != 0; - case "object": - return Object.keys(wrapped.value).length > 0; - case "array": - return wrapped.value.length > 0; - case "null": - return false; - case "html": - case "widget": - case "function": - return true; - } - } - Values.isTruthy = isTruthy; - /** Deep copy a field. */ - function deepCopy(field) { - if (field === null || field === undefined) - return field; - if (Values.isArray(field)) { - return [].concat(field.map(v => deepCopy(v))); - } - else if (Values.isObject(field)) { - let result = {}; - for (let [key, value] of Object.entries(field)) - result[key] = deepCopy(value); - return result; - } - else { - return field; - } - } - Values.deepCopy = deepCopy; - function isString(val) { - return typeof val == "string"; - } - Values.isString = isString; - function isNumber(val) { - return typeof val == "number"; - } - Values.isNumber = isNumber; - function isDate(val) { - return val instanceof DateTime; - } - Values.isDate = isDate; - function isDuration(val) { - return val instanceof Duration; - } - Values.isDuration = isDuration; - function isNull(val) { - return val === null || val === undefined; - } - Values.isNull = isNull; - function isArray(val) { - return Array.isArray(val); - } - Values.isArray = isArray; - function isBoolean(val) { - return typeof val === "boolean"; - } - Values.isBoolean = isBoolean; - function isLink(val) { - return val instanceof Link; - } - Values.isLink = isLink; - function isWidget(val) { - return val instanceof Widget; - } - Values.isWidget = isWidget; - function isHtml(val) { - if (typeof HTMLElement !== "undefined") { - return val instanceof HTMLElement; - } - else { - return false; - } - } - Values.isHtml = isHtml; - /** Checks if the given value is an object (and not any other dataview-recognized object-like type). */ - function isObject(val) { - return (typeof val == "object" && - !isHtml(val) && - !isWidget(val) && - !isArray(val) && - !isDuration(val) && - !isDate(val) && - !isLink(val) && - val !== undefined && - !isNull(val)); - } - Values.isObject = isObject; - function isFunction(val) { - return typeof val == "function"; - } - Values.isFunction = isFunction; -})(Values || (Values = {})); -/////////////// -// Groupings // -/////////////// -var Groupings; -(function (Groupings) { - /** Determines if the given group entry is a standalone value, or a grouping of sub-entries. */ - function isElementGroup(entry) { - return Values.isObject(entry) && Object.keys(entry).length == 2 && "key" in entry && "rows" in entry; - } - Groupings.isElementGroup = isElementGroup; - /** Determines if the given array is a grouping array. */ - function isGrouping(entry) { - for (let element of entry) - if (!isElementGroup(element)) - return false; - return true; - } - Groupings.isGrouping = isGrouping; - /** Count the total number of elements in a recursive grouping. */ - function count(elements) { - if (isGrouping(elements)) { - let result = 0; - for (let subgroup of elements) - result += count(subgroup.rows); - return result; - } - else { - return elements.length; - } - } - Groupings.count = count; -})(Groupings || (Groupings = {})); -////////// -// LINK // -////////// -/** The Obsidian 'link', used for uniquely describing a file, header, or block. */ -class Link { - constructor(fields) { - Object.assign(this, fields); - } - /** Create a link to a specific file. */ - static file(path, embed = false, display) { - return new Link({ - path, - embed, - display, - subpath: undefined, - type: "file", - }); - } - static infer(linkpath, embed = false, display) { - if (linkpath.includes("#^")) { - let split = linkpath.split("#^"); - return Link.block(split[0], split[1], embed, display); - } - else if (linkpath.includes("#")) { - let split = linkpath.split("#"); - return Link.header(split[0], split[1], embed, display); - } - else - return Link.file(linkpath, embed, display); - } - /** Create a link to a specific file and header in that file. */ - static header(path, header, embed, display) { - // Headers need to be normalized to alpha-numeric & with extra spacing removed. - return new Link({ - path, - embed, - display, - subpath: normalizeHeaderForLink(header), - type: "header", - }); - } - /** Create a link to a specific file and block in that file. */ - static block(path, blockId, embed, display) { - return new Link({ - path, - embed, - display, - subpath: blockId, - type: "block", - }); - } - static fromObject(object) { - return new Link(object); - } - /** Checks for link equality (i.e., that the links are pointing to the same exact location). */ - equals(other) { - if (other == undefined || other == null) - return false; - return this.path == other.path && this.type == other.type && this.subpath == other.subpath; - } - /** Convert this link to it's markdown representation. */ - toString() { - return this.markdown(); - } - /** Convert this link to a raw object which is serialization-friendly. */ - toObject() { - return { path: this.path, type: this.type, subpath: this.subpath, display: this.display, embed: this.embed }; - } - /** Update this link with a new path. */ - //@ts-ignore; error appeared after updating Obsidian to 0.15.4; it also updated other packages but didn't say which - withPath(path) { - return new Link(Object.assign({}, this, { path })); - } - /** Return a new link which points to the same location but with a new display value. */ - withDisplay(display) { - return new Link(Object.assign({}, this, { display })); - } - /** Convert a file link into a link to a specific header. */ - withHeader(header) { - return Link.header(this.path, header, this.embed, this.display); - } - /** Convert any link into a link to its file. */ - toFile() { - return Link.file(this.path, this.embed, this.display); - } - /** Convert this link into an embedded link. */ - toEmbed() { - if (this.embed) { - return this; - } - else { - let link = new Link(this); - link.embed = true; - return link; - } - } - /** Convert this link into a non-embedded link. */ - fromEmbed() { - if (!this.embed) { - return this; - } - else { - let link = new Link(this); - link.embed = false; - return link; - } - } - /** Convert this link to markdown so it can be rendered. */ - markdown() { - let result = (this.embed ? "!" : "") + "[[" + this.obsidianLink(); - if (this.display) { - result += "|" + this.display; - } - else { - result += "|" + getFileTitle(this.path); - if (this.type == "header" || this.type == "block") - result += " > " + this.subpath; - } - result += "]]"; - return result; - } - /** Convert the inner part of the link to something that Obsidian can open / understand. */ - obsidianLink() { - var _a, _b; - const escaped = this.path.replace("|", "\\|"); - if (this.type == "header") - return escaped + "#" + ((_a = this.subpath) === null || _a === void 0 ? void 0 : _a.replace("|", "\\|")); - if (this.type == "block") - return escaped + "#^" + ((_b = this.subpath) === null || _b === void 0 ? void 0 : _b.replace("|", "\\|")); - else - return escaped; - } - /** The stripped name of the file this link points to. */ - fileName() { - return getFileTitle(this.path).replace(".md", ""); - } -} -///////////////// -// WIDGET BASE // -///////////////// -/** - * A trivial base class which just defines the '$widget' identifier type. Subtypes of - * widget are responsible for adding whatever metadata is relevant. If you want your widget - * to have rendering functionality (which you probably do), you should extend `RenderWidget`. - */ -class Widget { - constructor($widget) { - this.$widget = $widget; - } -} -/** A trivial widget which renders a (key, value) pair, and allows accessing the key and value. */ -class ListPairWidget extends Widget { - constructor(key, value) { - super("dataview:list-pair"); - this.key = key; - this.value = value; - } - markdown() { - return `${Values.toString(this.key)}: ${Values.toString(this.value)}`; - } -} -/** A simple widget which renders an external link. */ -class ExternalLinkWidget extends Widget { - constructor(url, display) { - super("dataview:external-link"); - this.url = url; - this.display = display; - } - markdown() { - var _a; - return `[${(_a = this.display) !== null && _a !== void 0 ? _a : this.url}](${this.url})`; - } -} -var Widgets; -(function (Widgets) { - /** Create a list pair widget matching the given key and value. */ - function listPair(key, value) { - return new ListPairWidget(key, value); - } - Widgets.listPair = listPair; - /** Create an external link widget which renders an external Obsidian link. */ - function externalLink(url, display) { - return new ExternalLinkWidget(url, display); - } - Widgets.externalLink = externalLink; - /** Checks if the given widget is a list pair widget. */ - function isListPair(widget) { - return widget.$widget === "dataview:list-pair"; - } - Widgets.isListPair = isListPair; - function isExternalLink(widget) { - return widget.$widget === "dataview:external-link"; - } - Widgets.isExternalLink = isExternalLink; - /** Determines if the given widget is any kind of built-in widget with special rendering handling. */ - function isBuiltin(widget) { - return isListPair(widget) || isExternalLink(widget); - } - Widgets.isBuiltin = isBuiltin; -})(Widgets || (Widgets = {})); - -/** Implementation of DataArray, minus the dynamic variable access, which is implemented via proxy. */ -class DataArrayImpl { - constructor(values, settings, defaultComparator = Values.compareValue) { - this.values = values; - this.settings = settings; - this.defaultComparator = defaultComparator; - this.length = values.length; - } - static wrap(arr, settings, defaultComparator = Values.compareValue) { - return new Proxy(new DataArrayImpl(arr, settings, defaultComparator), DataArrayImpl.ARRAY_PROXY); - } - lwrap(values) { - return DataArrayImpl.wrap(values, this.settings, this.defaultComparator); - } - where(predicate) { - return this.lwrap(this.values.filter(predicate)); - } - filter(predicate) { - return this.where(predicate); - } - map(f) { - return this.lwrap(this.values.map(f)); - } - flatMap(f) { - let result = []; - for (let index = 0; index < this.length; index++) { - let value = f(this.values[index], index, this.values); - if (!value || value.length == 0) - continue; - for (let r of value) - result.push(r); - } - return this.lwrap(result); - } - mutate(f) { - for (let index = 0; index < this.values.length; index++) { - f(this.values[index], index, this.values); - } - return this; - } - limit(count) { - return this.lwrap(this.values.slice(0, count)); - } - slice(start, end) { - return this.lwrap(this.values.slice(start, end)); - } - concat(other) { - return this.lwrap(this.values.concat(other.values)); - } - /** Return the first index of the given (optionally starting the search) */ - indexOf(element, fromIndex) { - return this.findIndex(e => this.defaultComparator(e, element) == 0, fromIndex); - } - /** Return the first element that satisfies the given predicate. */ - find(pred) { - let index = this.findIndex(pred); - if (index == -1) - return undefined; - else - return this.values[index]; - } - findIndex(pred, fromIndex) { - for (let index = fromIndex !== null && fromIndex !== void 0 ? fromIndex : 0; index < this.length; index++) { - if (pred(this.values[index], index, this.values)) - return index; - } - return -1; - } - includes(element) { - return this.indexOf(element, 0) != -1; - } - join(sep) { - return this.map(s => Values.toString(s, this.settings)) - .array() - .join(sep !== null && sep !== void 0 ? sep : ", "); - } - sort(key, direction, comparator) { - if (this.values.length == 0) - return this; - let realComparator = comparator !== null && comparator !== void 0 ? comparator : this.defaultComparator; - let realKey = key !== null && key !== void 0 ? key : ((l) => l); - // Associate each entry with it's index for the key function, and then do a normal sort. - let copy = [].concat(this.array()).map((elem, index) => { - return { index: index, value: elem }; - }); - copy.sort((a, b) => { - let aKey = realKey(a.value, a.index, this.values); - let bKey = realKey(b.value, b.index, this.values); - return direction === "desc" ? -realComparator(aKey, bKey) : realComparator(aKey, bKey); - }); - return this.lwrap(copy.map(e => e.value)); - } - sortInPlace(key, direction, comparator) { - if (this.values.length == 0) - return this; - let realComparator = comparator !== null && comparator !== void 0 ? comparator : this.defaultComparator; - let realKey = key !== null && key !== void 0 ? key : ((l) => l); - this.values.sort((a, b) => { - let aKey = realKey(a); - let bKey = realKey(b); - return direction == "desc" ? -realComparator(aKey, bKey) : realComparator(aKey, bKey); - }); - return this; - } - groupBy(key, comparator) { - if (this.values.length == 0) - return this.lwrap([]); - // JavaScript sucks and we can't make hash maps over arbitrary types (only strings/ints), so - // we do a poor man algorithm where we SORT, followed by grouping. - let intermediate = this.sort(key, "asc", comparator); - comparator = comparator !== null && comparator !== void 0 ? comparator : this.defaultComparator; - let result = []; - let currentRow = [intermediate[0]]; - let current = key(intermediate[0], 0, intermediate.values); - for (let index = 1; index < intermediate.length; index++) { - let newKey = key(intermediate[index], index, intermediate.values); - if (comparator(current, newKey) != 0) { - result.push({ key: current, rows: this.lwrap(currentRow) }); - current = newKey; - currentRow = [intermediate[index]]; - } - else { - currentRow.push(intermediate[index]); - } - } - result.push({ key: current, rows: this.lwrap(currentRow) }); - return this.lwrap(result); - } - groupIn(key, comparator) { - if (Groupings.isGrouping(this.values)) { - return this.map(v => { - return { - key: v.key, - rows: DataArray.wrap(v.rows, this.settings).groupIn(key, comparator), - }; - }); - } - else { - return this.groupBy(key, comparator); - } - } - distinct(key, comparator) { - if (this.values.length == 0) - return this; - let realKey = key !== null && key !== void 0 ? key : (x => x); - // For similar reasons to groupBy, do a sort and take the first element of each block. - let intermediate = this.map((x, index) => { - return { key: realKey(x, index, this.values), value: x }; - }).sort(x => x.key, "asc", comparator); - comparator = comparator !== null && comparator !== void 0 ? comparator : this.defaultComparator; - let result = [intermediate[0].value]; - for (let index = 1; index < intermediate.length; index++) { - if (comparator(intermediate[index - 1].key, intermediate[index].key) != 0) { - result.push(intermediate[index].value); - } - } - return this.lwrap(result); - } - every(f) { - return this.values.every(f); - } - some(f) { - return this.values.some(f); - } - none(f) { - return this.values.every((v, i, a) => !f(v, i, a)); - } - first() { - return this.values.length > 0 ? this.values[0] : undefined; - } - last() { - return this.values.length > 0 ? this.values[this.values.length - 1] : undefined; - } - to(key) { - let result = []; - for (let child of this.values) { - let value = child[key]; - if (value === undefined || value === null) - continue; - if (Array.isArray(value) || DataArray.isDataArray(value)) - value.forEach(v => result.push(v)); - else - result.push(value); - } - return this.lwrap(result); - } - into(key) { - let result = []; - for (let child of this.values) { - let value = child[key]; - if (value === undefined || value === null) - continue; - result.push(value); - } - return this.lwrap(result); - } - expand(key) { - let result = []; - let queue = [].concat(this.values); - while (queue.length > 0) { - let next = queue.pop(); - let value = next[key]; - if (value === undefined || value === null) - continue; - if (Array.isArray(value)) - value.forEach(v => queue.push(v)); - else if (value instanceof DataArrayImpl) - value.forEach(v => queue.push(v)); - else - queue.push(value); - result.push(next); - } - return this.lwrap(result); - } - forEach(f) { - for (let index = 0; index < this.values.length; index++) { - f(this.values[index], index, this.values); - } - } - array() { - return [].concat(this.values); - } - [Symbol.iterator]() { - return this.values[Symbol.iterator](); - } - toString() { - return "[" + this.values.join(", ") + "]"; - } -} -DataArrayImpl.ARRAY_FUNCTIONS = new Set([ - "where", - "filter", - "map", - "flatMap", - "mutate", - "slice", - "concat", - "indexOf", - "limit", - "find", - "findIndex", - "includes", - "join", - "sort", - "sortInPlace", - "groupBy", - "groupIn", - "distinct", - "every", - "some", - "none", - "first", - "last", - "to", - "into", - "lwrap", - "expand", - "forEach", - "length", - "values", - "array", - "defaultComparator", - "toString", - "settings", -]); -DataArrayImpl.ARRAY_PROXY = { - get: function (target, prop, reciever) { - if (typeof prop === "symbol") - return target[prop]; - else if (typeof prop === "number") - return target.values[prop]; - else if (prop === "constructor") - return target.values.constructor; - else if (!isNaN(parseInt(prop))) - return target.values[parseInt(prop)]; - else if (DataArrayImpl.ARRAY_FUNCTIONS.has(prop.toString())) - return target[prop.toString()]; - return target.to(prop); - }, -}; -/** Provides utility functions for generating data arrays. */ -var DataArray; -(function (DataArray) { - /** Create a new Dataview data array. */ - function wrap(raw, settings) { - if (isDataArray(raw)) - return raw; - return DataArrayImpl.wrap(raw, settings); - } - DataArray.wrap = wrap; - /** Create a new DataArray from an iterable object. */ - function from(raw, settings) { - if (isDataArray(raw)) - return raw; - let data = []; - for (let elem of raw) - data.push(elem); - return DataArrayImpl.wrap(data, settings); - } - DataArray.from = from; - /** Return true if the given object is a data array. */ - function isDataArray(obj) { - return obj instanceof DataArrayImpl; - } - DataArray.isDataArray = isDataArray; -})(DataArray || (DataArray = {})); -// A scary looking polyfill, sure, but it fixes up data array/array interop for us. -const oldArrayIsArray = Array.isArray; -Array.isArray = (arg) => { - return oldArrayIsArray(arg) || DataArray.isDataArray(arg); + return /[#*0-9]\uFE0F?\u20E3|[\xA9\xAE\u203C\u2049\u2122\u2139\u2194-\u2199\u21A9\u21AA\u231A\u231B\u2328\u23CF\u23ED-\u23EF\u23F1\u23F2\u23F8-\u23FA\u24C2\u25AA\u25AB\u25B6\u25C0\u25FB\u25FC\u25FE\u2600-\u2604\u260E\u2611\u2614\u2615\u2618\u2620\u2622\u2623\u2626\u262A\u262E\u262F\u2638-\u263A\u2640\u2642\u2648-\u2653\u265F\u2660\u2663\u2665\u2666\u2668\u267B\u267E\u267F\u2692\u2694-\u2697\u2699\u269B\u269C\u26A0\u26A7\u26AA\u26B0\u26B1\u26BD\u26BE\u26C4\u26C8\u26CF\u26D1\u26D3\u26E9\u26F0-\u26F5\u26F7\u26F8\u26FA\u2702\u2708\u2709\u270F\u2712\u2714\u2716\u271D\u2721\u2733\u2734\u2744\u2747\u2757\u2763\u27A1\u2934\u2935\u2B05-\u2B07\u2B1B\u2B1C\u2B55\u3030\u303D\u3297\u3299]\uFE0F?|[\u261D\u270C\u270D](?:\uFE0F|\uD83C[\uDFFB-\uDFFF])?|[\u270A\u270B](?:\uD83C[\uDFFB-\uDFFF])?|[\u23E9-\u23EC\u23F0\u23F3\u25FD\u2693\u26A1\u26AB\u26C5\u26CE\u26D4\u26EA\u26FD\u2705\u2728\u274C\u274E\u2753-\u2755\u2795-\u2797\u27B0\u27BF\u2B50]|\u26F9(?:\uFE0F|\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|\u2764\uFE0F?(?:\u200D(?:\uD83D\uDD25|\uD83E\uDE79))?|\uD83C(?:[\uDC04\uDD70\uDD71\uDD7E\uDD7F\uDE02\uDE37\uDF21\uDF24-\uDF2C\uDF36\uDF7D\uDF96\uDF97\uDF99-\uDF9B\uDF9E\uDF9F\uDFCD\uDFCE\uDFD4-\uDFDF\uDFF5\uD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0F?(?:\u200D\uD83D\uDDE8\uFE0F?)?|\uDC68(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D(?:[\uDC68\uDC69]\u200D\uD83D(?:\uDC66(?:\u200D\uD83D\uDC66)?|\uDC67(?:\u200D\uD83D[\uDC66\uDC67])?)|[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uDC66(?:\u200D\uD83D\uDC66)?|\uDC67(?:\u200D\uD83D[\uDC66\uDC67])?)|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C(?:\uDFFB(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFC-\uDFFF])))?|\uDFFC(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFB\uDFFD-\uDFFF])))?|\uDFFD(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])))?|\uDFFE(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFB-\uDFFD\uDFFF])))?|\uDFFF(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?\uDC68\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D\uDC68\uD83C[\uDFFB-\uDFFE])))?))?|\uDC69(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:\uDC8B\u200D\uD83D)?[\uDC68\uDC69]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D(?:[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uDC66(?:\u200D\uD83D\uDC66)?|\uDC67(?:\u200D\uD83D[\uDC66\uDC67])?|\uDC69\u200D\uD83D(?:\uDC66(?:\u200D\uD83D\uDC66)?|\uDC67(?:\u200D\uD83D[\uDC66\uDC67])?))|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C(?:\uDFFB(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFC-\uDFFF])))?|\uDFFC(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFB\uDFFD-\uDFFF])))?|\uDFFD(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])))?|\uDFFE(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFB-\uDFFD\uDFFF])))?|\uDFFF(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D\uD83D(?:[\uDC68\uDC69]|\uDC8B\u200D\uD83D[\uDC68\uDC69])\uD83C[\uDFFB-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83D[\uDC68\uDC69]\uD83C[\uDFFB-\uDFFE])))?))?|\uDC6F(?:\u200D[\u2640\u2642]\uFE0F?)?|\uDD75(?:\uFE0F|\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|\uDE2E(?:\u200D\uD83D\uDCA8)?|\uDE35(?:\u200D\uD83D\uDCAB)?|\uDE36(?:\u200D\uD83C\uDF2B\uFE0F?)?)|\uD83E(?:[\uDD0C\uDD0F\uDD18-\uDD1F\uDD30-\uDD34\uDD36\uDD77\uDDB5\uDDB6\uDDBB\uDDD2\uDDD3\uDDD5\uDEC3-\uDEC5\uDEF0\uDEF2-\uDEF8](?:\uD83C[\uDFFB-\uDFFF])?|[\uDD26\uDD35\uDD37-\uDD39\uDD3D\uDD3E\uDDB8\uDDB9\uDDCD-\uDDCF\uDDD4\uDDD6-\uDDDD](?:\uD83C[\uDFFB-\uDFFF])?(?:\u200D[\u2640\u2642]\uFE0F?)?|[\uDDDE\uDDDF](?:\u200D[\u2640\u2642]\uFE0F?)?|[\uDD0D\uDD0E\uDD10-\uDD17\uDD20-\uDD25\uDD27-\uDD2F\uDD3A\uDD3F-\uDD45\uDD47-\uDD76\uDD78-\uDDB4\uDDB7\uDDBA\uDDBC-\uDDCC\uDDD0\uDDE0-\uDDFF\uDE70-\uDE7C\uDE80-\uDE88\uDE90-\uDEBD\uDEBF-\uDEC2\uDECE-\uDEDB\uDEE0-\uDEE8]|\uDD3C(?:\u200D[\u2640\u2642]\uFE0F?|\uD83C[\uDFFB-\uDFFF])?|\uDDD1(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1))|\uD83C(?:\uDFFB(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFC-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?|\uDFFC(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFB\uDFFD-\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?|\uDFFD(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?|\uDFFE(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFB-\uDFFD\uDFFF]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?|\uDFFF(?:\u200D(?:[\u2695\u2696\u2708]\uFE0F?|\u2764\uFE0F?\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1\uD83C[\uDFFB-\uDFFE]|\uD83C[\uDF3E\uDF73\uDF7C\uDF84\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E(?:[\uDDAF-\uDDB3\uDDBC\uDDBD]|\uDD1D\u200D\uD83E\uDDD1\uD83C[\uDFFB-\uDFFF])))?))?|\uDEF1(?:\uD83C(?:\uDFFB(?:\u200D\uD83E\uDEF2\uD83C[\uDFFC-\uDFFF])?|\uDFFC(?:\u200D\uD83E\uDEF2\uD83C[\uDFFB\uDFFD-\uDFFF])?|\uDFFD(?:\u200D\uD83E\uDEF2\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])?|\uDFFE(?:\u200D\uD83E\uDEF2\uD83C[\uDFFB-\uDFFD\uDFFF])?|\uDFFF(?:\u200D\uD83E\uDEF2\uD83C[\uDFFB-\uDFFE])?))?)/g; }; -/** Test-environment-friendly function which fetches the current system locale. */ -function currentLocale() { - if (typeof window === "undefined") - return "en-US"; - return window.navigator.language; -} - -/** Render simple fields compactly, removing wrapping content like paragraph and span. */ -async function renderCompactMarkdown(markdown, container, sourcePath, component) { - let subcontainer = container.createSpan(); - await obsidian.MarkdownRenderer.renderMarkdown(markdown, subcontainer, sourcePath, component); - let paragraph = subcontainer.querySelector(":scope > p"); - if (subcontainer.children.length == 1 && paragraph) { - while (paragraph.firstChild) { - subcontainer.appendChild(paragraph.firstChild); - } - subcontainer.removeChild(paragraph); - } -} -/** Render a pre block with an error in it; returns the element to allow for dynamic updating. */ -function renderErrorPre(container, error) { - let pre = container.createEl("pre", { cls: ["dataview", "dataview-error"] }); - pre.appendText(error); - return pre; -} -/** Render a static codeblock. */ -function renderCodeBlock(container, source, language) { - let code = container.createEl("code", { cls: ["dataview"] }); - if (language) - code.classList.add("language-" + language); - code.appendText(source); - return code; -} -/** Prettily render a value into a container with the given settings. */ -async function renderValue(field, container, originFile, component, settings, expandList = false, context = "root", depth = 0) { - var _a, _b, _c; - // Prevent infinite recursion. - if (depth > settings.maxRecursiveRenderDepth) { - container.appendText("..."); - return; - } - if (Values.isNull(field)) { - await renderCompactMarkdown(settings.renderNullAs, container, originFile, component); - } - else if (Values.isDate(field)) { - container.appendText(renderMinimalDate(field, settings, currentLocale())); - } - else if (Values.isDuration(field)) { - container.appendText(renderMinimalDuration(field)); - } - else if (Values.isString(field) || Values.isBoolean(field) || Values.isNumber(field)) { - await renderCompactMarkdown("" + field, container, originFile, component); - } - else if (Values.isLink(field)) { - await renderCompactMarkdown(field.markdown(), container, originFile, component); - } - else if (Values.isHtml(field)) { - container.appendChild(field); - } - else if (Values.isWidget(field)) { - if (Widgets.isListPair(field)) { - await renderValue(field.key, container, originFile, component, settings, expandList, context, depth); - container.appendText(": "); - await renderValue(field.value, container, originFile, component, settings, expandList, context, depth); - } - else if (Widgets.isExternalLink(field)) { - let elem = document.createElement("a"); - elem.textContent = (_a = field.display) !== null && _a !== void 0 ? _a : field.url; - elem.rel = "noopener"; - elem.target = "_blank"; - elem.classList.add("external-link"); - elem.href = field.url; - container.appendChild(elem); - } - else { - container.appendText(``); - } - } - else if (Values.isFunction(field)) { - container.appendText(""); - } - else if (Values.isArray(field) || DataArray.isDataArray(field)) { - if (expandList) { - let list = container.createEl("ul", { - cls: [ - "dataview", - "dataview-ul", - context == "list" ? "dataview-result-list-ul" : "dataview-result-list-root-ul", - ], - }); - for (let child of field) { - let li = list.createEl("li", { cls: "dataview-result-list-li" }); - await renderValue(child, li, originFile, component, settings, expandList, "list", depth + 1); - } - } - else { - if (field.length == 0) { - container.appendText(""); - return; - } - let span = container.createEl("span", { cls: ["dataview", "dataview-result-list-span"] }); - let first = true; - for (let val of field) { - if (first) - first = false; - else - span.appendText(", "); - await renderValue(val, span, originFile, component, settings, expandList, "list", depth + 1); - } - } - } - else if (Values.isObject(field)) { - // Don't render classes in case they have recursive references; spoopy. - if (((_b = field === null || field === void 0 ? void 0 : field.constructor) === null || _b === void 0 ? void 0 : _b.name) && ((_c = field === null || field === void 0 ? void 0 : field.constructor) === null || _c === void 0 ? void 0 : _c.name) != "Object") { - container.appendText(`<${field.constructor.name}>`); - return; - } - if (expandList) { - let list = container.createEl("ul", { cls: ["dataview", "dataview-ul", "dataview-result-object-ul"] }); - for (let [key, value] of Object.entries(field)) { - let li = list.createEl("li", { cls: ["dataview", "dataview-li", "dataview-result-object-li"] }); - li.appendText(key + ": "); - await renderValue(value, li, originFile, component, settings, expandList, "list", depth + 1); - } - } - else { - if (Object.keys(field).length == 0) { - container.appendText(""); - return; - } - let span = container.createEl("span", { cls: ["dataview", "dataview-result-object-span"] }); - let first = true; - for (let [key, value] of Object.entries(field)) { - if (first) - first = false; - else - span.appendText(", "); - span.appendText(key + ": "); - await renderValue(value, span, originFile, component, settings, expandList, "list", depth + 1); - } - } - } - else { - container.appendText("Unrecognized: " + JSON.stringify(field)); - } -} - -var papaparse_min = {exports: {}}; - -/* @license -Papa Parse -v5.3.2 -https://github.com/mholt/PapaParse -License: MIT -*/ - -(function (module, exports) { -!function(e,t){module.exports=t();}(commonjsGlobal,function s(){var f="undefined"!=typeof self?self:"undefined"!=typeof window?window:void 0!==f?f:{};var n=!f.document&&!!f.postMessage,o=n&&/blob:/i.test((f.location||{}).protocol),a={},h=0,b={parse:function(e,t){var i=(t=t||{}).dynamicTyping||!1;M(i)&&(t.dynamicTypingFunction=i,i={});if(t.dynamicTyping=i,t.transform=!!M(t.transform)&&t.transform,t.worker&&b.WORKERS_SUPPORTED){var r=function(){if(!b.WORKERS_SUPPORTED)return !1;var e=(i=f.URL||f.webkitURL||null,r=s.toString(),b.BLOB_URL||(b.BLOB_URL=i.createObjectURL(new Blob(["(",r,")();"],{type:"text/javascript"})))),t=new f.Worker(e);var i,r;return t.onmessage=_,t.id=h++,a[t.id]=t}();return r.userStep=t.step,r.userChunk=t.chunk,r.userComplete=t.complete,r.userError=t.error,t.step=M(t.step),t.chunk=M(t.chunk),t.complete=M(t.complete),t.error=M(t.error),delete t.worker,void r.postMessage({input:e,config:t,workerId:r.id})}var n=null;b.NODE_STREAM_INPUT,"string"==typeof e?n=t.download?new l(t):new p(t):!0===e.readable&&M(e.read)&&M(e.on)?n=new g(t):(f.File&&e instanceof File||e instanceof Object)&&(n=new c(t));return n.stream(e)},unparse:function(e,t){var n=!1,_=!0,m=",",y="\r\n",s='"',a=s+s,i=!1,r=null,o=!1;!function(){if("object"!=typeof t)return;"string"!=typeof t.delimiter||b.BAD_DELIMITERS.filter(function(e){return -1!==t.delimiter.indexOf(e)}).length||(m=t.delimiter);("boolean"==typeof t.quotes||"function"==typeof t.quotes||Array.isArray(t.quotes))&&(n=t.quotes);"boolean"!=typeof t.skipEmptyLines&&"string"!=typeof t.skipEmptyLines||(i=t.skipEmptyLines);"string"==typeof t.newline&&(y=t.newline);"string"==typeof t.quoteChar&&(s=t.quoteChar);"boolean"==typeof t.header&&(_=t.header);if(Array.isArray(t.columns)){if(0===t.columns.length)throw new Error("Option columns is empty");r=t.columns;}void 0!==t.escapeChar&&(a=t.escapeChar+s);("boolean"==typeof t.escapeFormulae||t.escapeFormulae instanceof RegExp)&&(o=t.escapeFormulae instanceof RegExp?t.escapeFormulae:/^[=+\-@\t\r].*$/);}();var h=new RegExp(j(s),"g");"string"==typeof e&&(e=JSON.parse(e));if(Array.isArray(e)){if(!e.length||Array.isArray(e[0]))return u(null,e,i);if("object"==typeof e[0])return u(r||Object.keys(e[0]),e,i)}else if("object"==typeof e)return "string"==typeof e.data&&(e.data=JSON.parse(e.data)),Array.isArray(e.data)&&(e.fields||(e.fields=e.meta&&e.meta.fields||r),e.fields||(e.fields=Array.isArray(e.data[0])?e.fields:"object"==typeof e.data[0]?Object.keys(e.data[0]):[]),Array.isArray(e.data[0])||"object"==typeof e.data[0]||(e.data=[e.data])),u(e.fields||[],e.data||[],i);throw new Error("Unable to serialize unrecognized input");function u(e,t,i){var r="";"string"==typeof e&&(e=JSON.parse(e)),"string"==typeof t&&(t=JSON.parse(t));var n=Array.isArray(e)&&0=this._config.preview;if(o)f.postMessage({results:n,workerId:b.WORKER_ID,finished:a});else if(M(this._config.chunk)&&!t){if(this._config.chunk(n,this._handle),this._handle.paused()||this._handle.aborted())return void(this._halted=!0);n=void 0,this._completeResults=void 0;}return this._config.step||this._config.chunk||(this._completeResults.data=this._completeResults.data.concat(n.data),this._completeResults.errors=this._completeResults.errors.concat(n.errors),this._completeResults.meta=n.meta),this._completed||!a||!M(this._config.complete)||n&&n.meta.aborted||(this._config.complete(this._completeResults,this._input),this._completed=!0),a||n&&n.meta.paused||this._nextChunk(),n}this._halted=!0;},this._sendError=function(e){M(this._config.error)?this._config.error(e):o&&this._config.error&&f.postMessage({workerId:b.WORKER_ID,error:e,finished:!1});};}function l(e){var r;(e=e||{}).chunkSize||(e.chunkSize=b.RemoteChunkSize),u.call(this,e),this._nextChunk=n?function(){this._readChunk(),this._chunkLoaded();}:function(){this._readChunk();},this.stream=function(e){this._input=e,this._nextChunk();},this._readChunk=function(){if(this._finished)this._chunkLoaded();else {if(r=new XMLHttpRequest,this._config.withCredentials&&(r.withCredentials=this._config.withCredentials),n||(r.onload=v(this._chunkLoaded,this),r.onerror=v(this._chunkError,this)),r.open(this._config.downloadRequestBody?"POST":"GET",this._input,!n),this._config.downloadRequestHeaders){var e=this._config.downloadRequestHeaders;for(var t in e)r.setRequestHeader(t,e[t]);}if(this._config.chunkSize){var i=this._start+this._config.chunkSize-1;r.setRequestHeader("Range","bytes="+this._start+"-"+i);}try{r.send(this._config.downloadRequestBody);}catch(e){this._chunkError(e.message);}n&&0===r.status&&this._chunkError();}},this._chunkLoaded=function(){4===r.readyState&&(r.status<200||400<=r.status?this._chunkError():(this._start+=this._config.chunkSize?this._config.chunkSize:r.responseText.length,this._finished=!this._config.chunkSize||this._start>=function(e){var t=e.getResponseHeader("Content-Range");if(null===t)return -1;return parseInt(t.substring(t.lastIndexOf("/")+1))}(r),this.parseChunk(r.responseText)));},this._chunkError=function(e){var t=r.statusText||e;this._sendError(new Error(t));};}function c(e){var r,n;(e=e||{}).chunkSize||(e.chunkSize=b.LocalChunkSize),u.call(this,e);var s="undefined"!=typeof FileReader;this.stream=function(e){this._input=e,n=e.slice||e.webkitSlice||e.mozSlice,s?((r=new FileReader).onload=v(this._chunkLoaded,this),r.onerror=v(this._chunkError,this)):r=new FileReaderSync,this._nextChunk();},this._nextChunk=function(){this._finished||this._config.preview&&!(this._rowCount=this._input.size,this.parseChunk(e.target.result);},this._chunkError=function(){this._sendError(r.error);};}function p(e){var i;u.call(this,e=e||{}),this.stream=function(e){return i=e,this._nextChunk()},this._nextChunk=function(){if(!this._finished){var e,t=this._config.chunkSize;return t?(e=i.substring(0,t),i=i.substring(t)):(e=i,i=""),this._finished=!i,this.parseChunk(e)}};}function g(e){u.call(this,e=e||{});var t=[],i=!0,r=!1;this.pause=function(){u.prototype.pause.apply(this,arguments),this._input.pause();},this.resume=function(){u.prototype.resume.apply(this,arguments),this._input.resume();},this.stream=function(e){this._input=e,this._input.on("data",this._streamData),this._input.on("end",this._streamEnd),this._input.on("error",this._streamError);},this._checkIsFinished=function(){r&&1===t.length&&(this._finished=!0);},this._nextChunk=function(){this._checkIsFinished(),t.length?this.parseChunk(t.shift()):i=!0;},this._streamData=v(function(e){try{t.push("string"==typeof e?e:e.toString(this._config.encoding)),i&&(i=!1,this._checkIsFinished(),this.parseChunk(t.shift()));}catch(e){this._streamError(e);}},this),this._streamError=v(function(e){this._streamCleanUp(),this._sendError(e);},this),this._streamEnd=v(function(){this._streamCleanUp(),r=!0,this._streamData("");},this),this._streamCleanUp=v(function(){this._input.removeListener("data",this._streamData),this._input.removeListener("end",this._streamEnd),this._input.removeListener("error",this._streamError);},this);}function i(m){var a,o,h,r=Math.pow(2,53),n=-r,s=/^\s*-?(\d+\.?|\.\d+|\d+\.\d+)([eE][-+]?\d+)?\s*$/,u=/^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))$/,t=this,i=0,f=0,d=!1,e=!1,l=[],c={data:[],errors:[],meta:{}};if(M(m.step)){var p=m.step;m.step=function(e){if(c=e,_())g();else {if(g(),0===c.data.length)return;i+=e.data.length,m.preview&&i>m.preview?o.abort():(c.data=c.data[0],p(c,t));}};}function y(e){return "greedy"===m.skipEmptyLines?""===e.join("").trim():1===e.length&&0===e[0].length}function g(){return c&&h&&(k("Delimiter","UndetectableDelimiter","Unable to auto-detect delimiting character; defaulted to '"+b.DefaultDelimiter+"'"),h=!1),m.skipEmptyLines&&(c.data=c.data.filter(function(e){return !y(e)})),_()&&function(){if(!c)return;function e(e,t){M(m.transformHeader)&&(e=m.transformHeader(e,t)),l.push(e);}if(Array.isArray(c.data[0])){for(var t=0;_()&&t=l.length?"__parsed_extra":l[i]),m.transform&&(s=m.transform(s,n)),s=v(n,s),"__parsed_extra"===n?(r[n]=r[n]||[],r[n].push(s)):r[n]=s;}return m.header&&(i>l.length?k("FieldMismatch","TooManyFields","Too many fields: expected "+l.length+" fields but parsed "+i,f+t):i=r.length/2?"\r\n":"\r"}(e,r)),h=!1,m.delimiter)M(m.delimiter)&&(m.delimiter=m.delimiter(e),c.meta.delimiter=m.delimiter);else {var n=function(e,t,i,r,n){var s,a,o,h;n=n||[",","\t","|",";",b.RECORD_SEP,b.UNIT_SEP];for(var u=0;u=D)return C(!0)}else for(m=F,F++;;){if(-1===(m=r.indexOf(S,m+1)))return i||u.push({type:"Quotes",code:"MissingQuotes",message:"Quoted field unterminated",row:h.length,index:F}),E();if(m===n-1)return E(r.substring(F,m).replace(_,S));if(S!==L||r[m+1]!==L){if(S===L||0===m||r[m-1]!==L){-1!==p&&p=D)return C(!0);break}u.push({type:"Quotes",code:"InvalidQuotes",message:"Trailing quote on quoted field is malformed",row:h.length,index:F}),m++;}}else m++;}return E();function k(e){h.push(e),d=F;}function b(e){var t=0;if(-1!==e){var i=r.substring(m+1,e);i&&""===i.trim()&&(t=i.length);}return t}function E(e){return i||(void 0===e&&(e=r.substring(F)),f.push(e),F=n,k(f),o&&R()),C()}function w(e){F=e,k(f),f=[],g=r.indexOf(x,F);}function C(e){return {data:h,errors:u,meta:{delimiter:O,linebreak:x,aborted:z,truncated:!!e,cursor:d+(t||0)}}}function R(){T(C()),h=[],u=[];}},this.abort=function(){z=!0;},this.getCharIndex=function(){return F};}function _(e){var t=e.data,i=a[t.workerId],r=!1;if(t.error)i.userError(t.error,t.file);else if(t.results&&t.results.data){var n={abort:function(){r=!0,m(t.workerId,{data:[],errors:[],meta:{aborted:!0}});},pause:y,resume:y};if(M(i.userStep)){for(var s=0;s" || op == ">=" || op == "!=" || op == "="; - } - Fields.isCompareOp = isCompareOp; - Fields.NULL = Fields.literal(null); -})(Fields || (Fields = {})); - -/** AST implementation for queries over data sources. */ -/** Utility functions for creating and manipulating sources. */ -var Sources; -(function (Sources) { - /** Create a source which searches from a tag. */ - function tag(tag) { - return { type: "tag", tag }; - } - Sources.tag = tag; - /** Create a source which fetches from a CSV file. */ - function csv(path) { - return { type: "csv", path }; - } - Sources.csv = csv; - /** Create a source which searches for files under a folder prefix. */ - function folder(prefix) { - return { type: "folder", folder: prefix }; - } - Sources.folder = folder; - /** Create a source which searches for files which link to/from a given file. */ - function link(file, incoming) { - return { type: "link", file, direction: incoming ? "incoming" : "outgoing" }; - } - Sources.link = link; - /** Create a source which joins two sources by a logical operator (and/or). */ - function binaryOp(left, op, right) { - return { type: "binaryop", left, op, right }; - } - Sources.binaryOp = binaryOp; - /** Create a source which takes the intersection of two sources. */ - function and(left, right) { - return { type: "binaryop", left, op: "&", right }; - } - Sources.and = and; - /** Create a source which takes the union of two sources. */ - function or(left, right) { - return { type: "binaryop", left, op: "|", right }; - } - Sources.or = or; - /** Create a source which negates the underlying source. */ - function negate(child) { - return { type: "negate", child }; - } - Sources.negate = negate; - function empty() { - return { type: "empty" }; - } - Sources.empty = empty; -})(Sources || (Sources = {})); - -/** Emoji regex without any additional flags. */ -const EMOJI_REGEX = new RegExp(emojiRegex(), ""); -/** Provides a lookup table for unit durations of the given type. */ -const DURATION_TYPES = { - year: Duration.fromObject({ years: 1 }), - years: Duration.fromObject({ years: 1 }), - yr: Duration.fromObject({ years: 1 }), - yrs: Duration.fromObject({ years: 1 }), - month: Duration.fromObject({ months: 1 }), - months: Duration.fromObject({ months: 1 }), - mo: Duration.fromObject({ months: 1 }), - mos: Duration.fromObject({ months: 1 }), - week: Duration.fromObject({ weeks: 1 }), - weeks: Duration.fromObject({ weeks: 1 }), - wk: Duration.fromObject({ weeks: 1 }), - wks: Duration.fromObject({ weeks: 1 }), - w: Duration.fromObject({ weeks: 1 }), - day: Duration.fromObject({ days: 1 }), - days: Duration.fromObject({ days: 1 }), - d: Duration.fromObject({ days: 1 }), - hour: Duration.fromObject({ hours: 1 }), - hours: Duration.fromObject({ hours: 1 }), - hr: Duration.fromObject({ hours: 1 }), - hrs: Duration.fromObject({ hours: 1 }), - h: Duration.fromObject({ hours: 1 }), - minute: Duration.fromObject({ minutes: 1 }), - minutes: Duration.fromObject({ minutes: 1 }), - min: Duration.fromObject({ minutes: 1 }), - mins: Duration.fromObject({ minutes: 1 }), - m: Duration.fromObject({ minutes: 1 }), - second: Duration.fromObject({ seconds: 1 }), - seconds: Duration.fromObject({ seconds: 1 }), - sec: Duration.fromObject({ seconds: 1 }), - secs: Duration.fromObject({ seconds: 1 }), - s: Duration.fromObject({ seconds: 1 }), -}; -/** Shorthand for common dates (relative to right now). */ -const DATE_SHORTHANDS = { - now: () => DateTime.local(), - today: () => DateTime.local().startOf("day"), - yesterday: () => DateTime.local() - .startOf("day") - .minus(Duration.fromObject({ days: 1 })), - tomorrow: () => DateTime.local() - .startOf("day") - .plus(Duration.fromObject({ days: 1 })), - sow: () => DateTime.local().startOf("week"), - "start-of-week": () => DateTime.local().startOf("week"), - eow: () => DateTime.local().endOf("week"), - "end-of-week": () => DateTime.local().endOf("week"), - soy: () => DateTime.local().startOf("year"), - "start-of-year": () => DateTime.local().startOf("year"), - eoy: () => DateTime.local().endOf("year"), - "end-of-year": () => DateTime.local().endOf("year"), - som: () => DateTime.local().startOf("month"), - "start-of-month": () => DateTime.local().startOf("month"), - eom: () => DateTime.local().endOf("month"), - "end-of-month": () => DateTime.local().endOf("month"), -}; -/** - * Keywords which cannot be used as variables directly. Use `row.` if it is a variable you have defined and want - * to access. - */ -const KEYWORDS = ["FROM", "WHERE", "LIMIT", "GROUP", "FLATTEN"]; -/////////////// -// Utilities // -/////////////// -/** Split on unescaped pipes in an inner link. */ -function splitOnUnescapedPipe(link) { - let pipe = -1; - while ((pipe = link.indexOf("|", pipe + 1)) >= 0) { - if (pipe > 0 && link[pipe - 1] == "\\") - continue; - return [link.substring(0, pipe).replace(/\\\|/g, "|"), link.substring(pipe + 1)]; - } - return [link.replace(/\\\|/g, "|"), undefined]; -} -/** Attempt to parse the inside of a link to pull out display name, subpath, etc. */ -function parseInnerLink(rawlink) { - let [link, display] = splitOnUnescapedPipe(rawlink); - return Link.infer(link, false, display); -} -/** Create a left-associative binary parser which parses the given sub-element and separator. Handles whitespace. */ -function createBinaryParser(child, sep, combine) { - return parsimmon_umd_min.exports.seqMap(child, parsimmon_umd_min.exports.seq(parsimmon_umd_min.exports.optWhitespace, sep, parsimmon_umd_min.exports.optWhitespace, child).many(), (first, rest) => { - if (rest.length == 0) - return first; - let node = combine(first, rest[0][1], rest[0][3]); - for (let index = 1; index < rest.length; index++) { - node = combine(node, rest[index][1], rest[index][3]); - } - return node; - }); -} -function chainOpt(base, ...funcs) { - return parsimmon_umd_min.exports.custom((success, failure) => { - return (input, i) => { - let result = base._(input, i); - if (!result.status) - return result; - for (let func of funcs) { - let next = func(result.value)._(input, result.index); - if (!next.status) - return result; - result = next; - } - return result; - }; - }); -} -const EXPRESSION = parsimmon_umd_min.exports.createLanguage({ - // A floating point number; the decimal point is optional. - number: q => parsimmon_umd_min.exports.regexp(/-?[0-9]+(\.[0-9]+)?/) - .map(str => Number.parseFloat(str)) - .desc("number"), - // A quote-surrounded string which supports escape characters ('\'). - string: q => parsimmon_umd_min.exports.string('"') - .then(parsimmon_umd_min.exports.alt(q.escapeCharacter, parsimmon_umd_min.exports.noneOf('"\\')) - .atLeast(0) - .map(chars => chars.join(""))) - .skip(parsimmon_umd_min.exports.string('"')) - .desc("string"), - escapeCharacter: _ => parsimmon_umd_min.exports.string("\\") - .then(parsimmon_umd_min.exports.any) - .map(escaped => { - // If we are escaping a backslash or a quote, pass in on in escaped form - if (escaped === '"') - return '"'; - if (escaped === "\\") - return "\\"; - else - return "\\" + escaped; - }), - // A boolean true/false value. - bool: _ => parsimmon_umd_min.exports.regexp(/true|false|True|False/) - .map(str => str.toLowerCase() == "true") - .desc("boolean ('true' or 'false')"), - // A tag of the form '#stuff/hello-there'. - tag: _ => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("#"), parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.regexp(/[^\u2000-\u206F\u2E00-\u2E7F'!"#$%&()*+,.:;<=>?@^`{|}~\[\]\\\s]/).desc("text")).many(), (start, rest) => start + rest.join("")).desc("tag ('#hello/stuff')"), - // A variable identifier, which is alphanumeric and must start with a letter or... emoji. - identifier: _ => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.regexp(/\p{Letter}/u), parsimmon_umd_min.exports.regexp(EMOJI_REGEX).desc("text")), parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.regexp(/[0-9\p{Letter}_-]/u), parsimmon_umd_min.exports.regexp(EMOJI_REGEX).desc("text")).many(), (first, rest) => first + rest.join("")).desc("variable identifier"), - // An Obsidian link of the form [[]]. - link: _ => parsimmon_umd_min.exports.regexp(/\[\[([^\[\]]*?)\]\]/u, 1) - .map(linkInner => parseInnerLink(linkInner)) - .desc("file link"), - // An embeddable link which can start with '!'. This overlaps with the normal negation operator, so it is only - // provided for metadata parsing. - embedLink: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("!").atMost(1), q.link, (p, l) => { - if (p.length > 0) - l.embed = true; - return l; - }).desc("file link"), - // Binary plus or minus operator. - binaryPlusMinus: _ => parsimmon_umd_min.exports.regexp(/\+|-/) - .map(str => str) - .desc("'+' or '-'"), - // Binary times or divide operator. - binaryMulDiv: _ => parsimmon_umd_min.exports.regexp(/\*|\/|%/) - .map(str => str) - .desc("'*' or '/' or '%'"), - // Binary comparison operator. - binaryCompareOp: _ => parsimmon_umd_min.exports.regexp(/>=|<=|!=|>|<|=/) - .map(str => str) - .desc("'>=' or '<=' or '!=' or '=' or '>' or '<'"), - // Binary boolean combination operator. - binaryBooleanOp: _ => parsimmon_umd_min.exports.regexp(/and|or|&|\|/i) - .map(str => { - if (str.toLowerCase() == "and") - return "&"; - else if (str.toLowerCase() == "or") - return "|"; - else - return str; - }) - .desc("'and' or 'or'"), - // A date which can be YYYY-MM[-DDTHH:mm:ss]. - rootDate: _ => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/\d{4}/), parsimmon_umd_min.exports.string("-"), parsimmon_umd_min.exports.regexp(/\d{2}/), (year, _, month) => { - return DateTime.fromObject({ year: Number.parseInt(year), month: Number.parseInt(month) }); - }).desc("date in format YYYY-MM[-DDTHH-MM-SS.MS]"), - dateShorthand: _ => parsimmon_umd_min.exports.alt(...Object.keys(DATE_SHORTHANDS) - .sort((a, b) => b.length - a.length) - .map(parsimmon_umd_min.exports.string)), - date: q => chainOpt(q.rootDate, (ym) => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("-"), parsimmon_umd_min.exports.regexp(/\d{2}/), (_, day) => ym.set({ day: Number.parseInt(day) })), (ymd) => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("T"), parsimmon_umd_min.exports.regexp(/\d{2}/), (_, hour) => ymd.set({ hour: Number.parseInt(hour) })), (ymdh) => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string(":"), parsimmon_umd_min.exports.regexp(/\d{2}/), (_, minute) => ymdh.set({ minute: Number.parseInt(minute) })), (ymdhm) => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string(":"), parsimmon_umd_min.exports.regexp(/\d{2}/), (_, second) => ymdhm.set({ second: Number.parseInt(second) })), (ymdhms) => parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("."), parsimmon_umd_min.exports.regexp(/\d{3}/), (_, millisecond) => ymdhms.set({ millisecond: Number.parseInt(millisecond) })), parsimmon_umd_min.exports.succeed(ymdhms) // pass - ), (dt) => parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("+").or(parsimmon_umd_min.exports.string("-")), parsimmon_umd_min.exports.regexp(/\d{1,2}(:\d{2})?/), (pm, hr) => dt.setZone("UTC" + pm + hr, { keepLocalTime: true })), parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("Z"), () => dt.setZone("utc", { keepLocalTime: true })), parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("["), parsimmon_umd_min.exports.regexp(/[0-9A-Za-z+-\/]+/u), parsimmon_umd_min.exports.string("]"), (_a, zone, _b) => dt.setZone(zone, { keepLocalTime: true })))) - .assert((dt) => dt.isValid, "valid date") - .desc("date in format YYYY-MM[-DDTHH-MM-SS.MS]"), - // A date, plus various shorthand times of day it could be. - datePlus: q => parsimmon_umd_min.exports.alt(q.dateShorthand.map(d => DATE_SHORTHANDS[d]()), q.date).desc("date in format YYYY-MM[-DDTHH-MM-SS.MS] or in shorthand"), - // A duration of time. - durationType: _ => parsimmon_umd_min.exports.alt(...Object.keys(DURATION_TYPES) - .sort((a, b) => b.length - a.length) - .map(parsimmon_umd_min.exports.string)), - duration: q => parsimmon_umd_min.exports.seqMap(q.number, parsimmon_umd_min.exports.optWhitespace, q.durationType, (count, _, t) => DURATION_TYPES[t].mapUnits(x => x * count)) - .sepBy1(parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace).or(parsimmon_umd_min.exports.optWhitespace)) - .map(durations => durations.reduce((p, c) => p.plus(c))) - .desc("duration like 4hr2min"), - // A raw null value. - rawNull: _ => parsimmon_umd_min.exports.string("null"), - // Source parsing. - tagSource: q => q.tag.map(tag => Sources.tag(tag)), - csvSource: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("csv(").skip(parsimmon_umd_min.exports.optWhitespace), q.string, parsimmon_umd_min.exports.string(")"), (_1, path, _2) => Sources.csv(path)), - linkIncomingSource: q => q.link.map(link => Sources.link(link.path, true)), - linkOutgoingSource: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("outgoing(").skip(parsimmon_umd_min.exports.optWhitespace), q.link, parsimmon_umd_min.exports.string(")"), (_1, link, _2) => Sources.link(link.path, false)), - folderSource: q => q.string.map(str => Sources.folder(str)), - parensSource: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("("), parsimmon_umd_min.exports.optWhitespace, q.source, parsimmon_umd_min.exports.optWhitespace, parsimmon_umd_min.exports.string(")"), (_1, _2, field, _3, _4) => field), - negateSource: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.string("-"), parsimmon_umd_min.exports.string("!")), q.atomSource, (_, source) => Sources.negate(source)), - atomSource: q => parsimmon_umd_min.exports.alt(q.parensSource, q.negateSource, q.linkOutgoingSource, q.linkIncomingSource, q.folderSource, q.tagSource, q.csvSource), - binaryOpSource: q => createBinaryParser(q.atomSource, q.binaryBooleanOp.map(s => s), Sources.binaryOp), - source: q => q.binaryOpSource, - // Field parsing. - variableField: q => q.identifier - .chain(r => { - if (KEYWORDS.includes(r.toUpperCase())) { - return parsimmon_umd_min.exports.fail("Variable fields cannot be a keyword (" + KEYWORDS.join(" or ") + ")"); - } - else { - return parsimmon_umd_min.exports.succeed(Fields.variable(r)); - } - }) - .desc("variable"), - numberField: q => q.number.map(val => Fields.literal(val)).desc("number"), - stringField: q => q.string.map(val => Fields.literal(val)).desc("string"), - boolField: q => q.bool.map(val => Fields.literal(val)).desc("boolean"), - dateField: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("date("), parsimmon_umd_min.exports.optWhitespace, q.datePlus, parsimmon_umd_min.exports.optWhitespace, parsimmon_umd_min.exports.string(")"), (prefix, _1, date, _2, postfix) => Fields.literal(date)).desc("date"), - durationField: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("dur("), parsimmon_umd_min.exports.optWhitespace, q.duration, parsimmon_umd_min.exports.optWhitespace, parsimmon_umd_min.exports.string(")"), (prefix, _1, dur, _2, postfix) => Fields.literal(dur)).desc("duration"), - nullField: q => q.rawNull.map(_ => Fields.NULL), - linkField: q => q.link.map(f => Fields.literal(f)), - listField: q => q.field - .sepBy(parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace)) - .wrap(parsimmon_umd_min.exports.string("[").skip(parsimmon_umd_min.exports.optWhitespace), parsimmon_umd_min.exports.optWhitespace.then(parsimmon_umd_min.exports.string("]"))) - .map(l => Fields.list(l)) - .desc("list ('[1, 2, 3]')"), - objectField: q => parsimmon_umd_min.exports.seqMap(q.identifier.or(q.string), parsimmon_umd_min.exports.string(":").trim(parsimmon_umd_min.exports.optWhitespace), q.field, (name, _sep, value) => { - return { name, value }; - }) - .sepBy(parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace)) - .wrap(parsimmon_umd_min.exports.string("{").skip(parsimmon_umd_min.exports.optWhitespace), parsimmon_umd_min.exports.optWhitespace.then(parsimmon_umd_min.exports.string("}"))) - .map(vals => { - let res = {}; - for (let entry of vals) - res[entry.name] = entry.value; - return Fields.object(res); - }) - .desc("object ('{ a: 1, b: 2 }')"), - atomInlineField: q => parsimmon_umd_min.exports.alt(q.date, q.duration.map(d => normalizeDuration(d)), q.string, q.tag, q.embedLink, q.bool, q.number, q.rawNull), - inlineFieldList: q => q.atomInlineField.sepBy(parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace).lookahead(q.atomInlineField)), - inlineField: q => parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.seqMap(q.atomInlineField, parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace), q.inlineFieldList, (f, _s, l) => [f].concat(l)), q.atomInlineField), - atomField: q => parsimmon_umd_min.exports.alt( - // Place embed links above negated fields as they are the special parser case '![[thing]]' and are generally unambigious. - q.embedLink.map(l => Fields.literal(l)), q.negatedField, q.linkField, q.listField, q.objectField, q.lambdaField, q.parensField, q.boolField, q.numberField, q.stringField, q.dateField, q.durationField, q.nullField, q.variableField), - indexField: q => parsimmon_umd_min.exports.seqMap(q.atomField, parsimmon_umd_min.exports.alt(q.dotPostfix, q.indexPostfix, q.functionPostfix).many(), (obj, postfixes) => { - let result = obj; - for (let post of postfixes) { - switch (post.type) { - case "dot": - result = Fields.index(result, Fields.literal(post.field)); - break; - case "index": - result = Fields.index(result, post.field); - break; - case "function": - result = Fields.func(result, post.fields); - break; - } - } - return result; - }), - negatedField: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("!"), q.indexField, (_, field) => Fields.negate(field)).desc("negated field"), - parensField: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("("), parsimmon_umd_min.exports.optWhitespace, q.field, parsimmon_umd_min.exports.optWhitespace, parsimmon_umd_min.exports.string(")"), (_1, _2, field, _3, _4) => field), - lambdaField: q => parsimmon_umd_min.exports.seqMap(q.identifier - .sepBy(parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace)) - .wrap(parsimmon_umd_min.exports.string("(").trim(parsimmon_umd_min.exports.optWhitespace), parsimmon_umd_min.exports.string(")").trim(parsimmon_umd_min.exports.optWhitespace)), parsimmon_umd_min.exports.string("=>").trim(parsimmon_umd_min.exports.optWhitespace), q.field, (ident, _ignore, value) => { - return { type: "lambda", arguments: ident, value }; - }), - dotPostfix: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("."), q.identifier, (_, field) => { - return { type: "dot", field: field }; - }), - indexPostfix: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("["), parsimmon_umd_min.exports.optWhitespace, q.field, parsimmon_umd_min.exports.optWhitespace, parsimmon_umd_min.exports.string("]"), (_, _2, field, _3, _4) => { - return { type: "index", field }; - }), - functionPostfix: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.string("("), parsimmon_umd_min.exports.optWhitespace, q.field.sepBy(parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace)), parsimmon_umd_min.exports.optWhitespace, parsimmon_umd_min.exports.string(")"), (_, _1, fields, _2, _3) => { - return { type: "function", fields }; - }), - // The precedence hierarchy of operators - multiply/divide, add/subtract, compare, and then boolean operations. - binaryMulDivField: q => createBinaryParser(q.indexField, q.binaryMulDiv, Fields.binaryOp), - binaryPlusMinusField: q => createBinaryParser(q.binaryMulDivField, q.binaryPlusMinus, Fields.binaryOp), - binaryCompareField: q => createBinaryParser(q.binaryPlusMinusField, q.binaryCompareOp, Fields.binaryOp), - binaryBooleanField: q => createBinaryParser(q.binaryCompareField, q.binaryBooleanOp, Fields.binaryOp), - binaryOpField: q => q.binaryBooleanField, - field: q => q.binaryOpField, -}); -/** - * Attempt to parse a field from the given text, returning a string error if the - * parse failed. - */ -function parseField(text) { - try { - return Result.success(EXPRESSION.field.tryParse(text)); - } - catch (error) { - return Result.failure("" + error); - } -} - -/** Parse inline fields and other embedded metadata in a line. */ -/** The wrapper characters that can be used to define an inline field. */ -const INLINE_FIELD_WRAPPERS = Object.freeze({ - "[": "]", - "(": ")", -}); -/** - * Find a matching closing bracket that occurs at or after `start`, respecting nesting and escapes. If found, - * returns the value contained within and the string index after the end of the value. - */ -function findClosing(line, start, open, close) { - let nesting = 0; - let escaped = false; - for (let index = start; index < line.length; index++) { - let char = line.charAt(index); - // Allows for double escapes like '\\' to be rendered normally. - if (char == "\\") { - escaped = !escaped; - continue; - } - // If escaped, ignore the next character for computing nesting, regardless of what it is. - if (escaped) { - escaped = false; - continue; - } - if (char == open) - nesting++; - else if (char == close) - nesting--; - // Only occurs if we are on a close character and trhere is no more nesting. - if (nesting < 0) - return { value: line.substring(start, index).trim(), endIndex: index + 1 }; - escaped = false; - } - return undefined; -} -/** Find the '::' separator in an inline field. */ -function findSeparator(line, start) { - let sep = line.indexOf("::", start); - if (sep < 0) - return undefined; - return { key: line.substring(start, sep).trim(), valueIndex: sep + 2 }; -} -/** Try to completely parse an inline field starting at the given position. Assuems `start` is on a wrapping character. */ -function findSpecificInlineField(line, start) { - let open = line.charAt(start); - let key = findSeparator(line, start + 1); - if (key === undefined) - return undefined; - // Fail the match if we find any separator characters (not allowed in keys). - for (let sep of Object.keys(INLINE_FIELD_WRAPPERS).concat(Object.values(INLINE_FIELD_WRAPPERS))) { - if (key.key.includes(sep)) - return undefined; - } - let value = findClosing(line, key.valueIndex, open, INLINE_FIELD_WRAPPERS[open]); - if (value === undefined) - return undefined; - return { - key: key.key, - value: value.value, - start: start, - startValue: key.valueIndex, - end: value.endIndex, - wrapping: open, - }; -} -/** Parse a textual inline field value into something we can work with. */ -function parseInlineValue(value) { - // Empty inline values (i.e., no text) should map to null to match long-term Dataview semantics. - // Null is also a more universal type to deal with than strings, since all functions accept nulls. - if (value.trim() == "") - return null; - // The stripped literal field parser understands all of the non-array/non-object fields and can parse them for us. - // Inline field objects are not currently supported; inline array objects have to be handled by the parser - // separately. - let inline = EXPRESSION.inlineField.parse(value); - if (inline.status) - return inline.value; - else - return value; -} -/** Extracts inline fields of the form '[key:: value]' from a line of text. This is done in a relatively - * "robust" way to avoid failing due to bad nesting or other interfering Markdown symbols: - * - * - Look for any wrappers ('[' and '(') in the line, trying to parse whatever comes after it as an inline key::. - * - If successful, scan until you find a matching end bracket, and parse whatever remains as an inline value. - */ -function extractInlineFields(line, includeTaskFields = false) { - let fields = []; - for (let wrapper of Object.keys(INLINE_FIELD_WRAPPERS)) { - let foundIndex = line.indexOf(wrapper); - while (foundIndex >= 0) { - let parsedField = findSpecificInlineField(line, foundIndex); - if (!parsedField) { - foundIndex = line.indexOf(wrapper, foundIndex + 1); - continue; - } - fields.push(parsedField); - foundIndex = line.indexOf(wrapper, parsedField.end); - } - } - if (includeTaskFields) - fields = fields.concat(extractSpecialTaskFields(line)); - fields.sort((a, b) => a.start - b.start); - let filteredFields = []; - for (let i = 0; i < fields.length; i++) { - if (i == 0 || filteredFields[filteredFields.length - 1].end < fields[i].start) { - filteredFields.push(fields[i]); - } - } - return filteredFields; -} -/** Validates that a raw field name has a valid form. */ -const FULL_LINE_KEY_PART = parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.regexp(new RegExp(emojiRegex(), "u")), parsimmon_umd_min.exports.regexp(/[0-9\p{Letter}\w\s_/-]+/u)) - .many() - .map(parts => parts.join("")); -parsimmon_umd_min.exports.regexp(/[^0-9\w\p{Letter}]*/u) - .then(FULL_LINE_KEY_PART) - .skip(parsimmon_umd_min.exports.regexp(/[_\*~`]*/u)); -const CREATED_DATE_REGEX = /\u{2795}\s*(\d{4}-\d{2}-\d{2})/u; -const DUE_DATE_REGEX = /(?:\u{1F4C5}|\u{1F4C6}|\u{1F5D3}\u{FE0F}?)\s*(\d{4}-\d{2}-\d{2})/u; -const DONE_DATE_REGEX = /\u{2705}\s*(\d{4}-\d{2}-\d{2})/u; -const SCHEDULED_DATE_REGEX = /[\u{23F3}\u{231B}]\s*(\d{4}-\d{2}-\d{2})/u; -const START_DATE_REGEX = /\u{1F6EB}\s*(\d{4}-\d{2}-\d{2})/u; -const EMOJI_REGEXES = [ - { regex: CREATED_DATE_REGEX, key: "created" }, - { regex: START_DATE_REGEX, key: "start" }, - { regex: SCHEDULED_DATE_REGEX, key: "scheduled" }, - { regex: DUE_DATE_REGEX, key: "due" }, - { regex: DONE_DATE_REGEX, key: "completion" }, -]; -/** Parse special completed/due/done task fields which are marked via emoji. */ -function extractSpecialTaskFields(line) { - let results = []; - for (let { regex, key } of EMOJI_REGEXES) { - const match = regex.exec(line); - if (!match) - continue; - results.push({ - key, - value: match[1], - start: match.index, - startValue: match.index + 1, - end: match.index + match[0].length, - wrapping: "emoji-shorthand", - }); - } - return results; -} -/** Sets or replaces the value of an inline field; if the value is 'undefined', deletes the key. */ -function setInlineField(source, key, value) { - let existing = extractInlineFields(source); - let existingKeys = existing.filter(f => f.key == key); - // Don't do anything if there are duplicate keys OR the key already doesn't exist. - if (existingKeys.length > 2 || (existingKeys.length == 0 && !value)) - return source; - let existingKey = existingKeys[0]; - let annotation = value ? `[${key}:: ${value}]` : ""; - if (existingKey) { - let prefix = source.substring(0, existingKey.start); - let suffix = source.substring(existingKey.end); - if (annotation) - return `${prefix}${annotation}${suffix}`; - else - return `${prefix}${suffix.trimStart()}`; - } - else if (annotation) { - return `${source.trimEnd()} ${annotation}`; - } - return source; -} -function setEmojiShorthandCompletionField(source, value) { - const existing = extractInlineFields(source, true); - const existingKeys = existing.filter(f => f.key === "completion" && f.wrapping === "emoji-shorthand"); - // Don't do anything if there are duplicate keys OR the key already doesn't exist. - if (existingKeys.length > 2 || (existingKeys.length == 0 && !value)) - return source; - /* No wrapper, add own spacing at start */ - const annotation = value ? ` ✅ ${value}` : ""; - let existingKey = existingKeys[0]; - if (existingKey) { - const prefix = source.substring(0, existingKey.start); - const suffix = source.substring(existingKey.end); - return `${prefix.trimEnd()}${annotation}${suffix}`; - } - else { - return `${source.trimEnd()}${annotation}`; - } -} - -/** All extracted markdown file metadata obtained from a file. */ -class PageMetadata { - constructor(path, init) { - this.path = path; - this.fields = new Map(); - this.frontmatter = {}; - this.tags = new Set(); - this.aliases = new Set(); - this.links = []; - Object.assign(this, init); - this.lists = (this.lists || []).map(l => new ListItem$1(l)); - } - /** Canonicalize raw links and other data in partial data with normalizers, returning a completed object. */ - static canonicalize(data, linkNormalizer) { - // Mutate the data for now, which is probably a bad idea but... all well. - if (data.frontmatter) { - data.frontmatter = Values.mapLeaves(data.frontmatter, t => Values.isLink(t) ? linkNormalizer(t) : t); - } - if (data.fields) { - for (let [key, value] of data.fields.entries()) { - data.fields.set(key, Values.mapLeaves(value, t => (Values.isLink(t) ? linkNormalizer(t) : t))); - } - } - if (data.lists) { - for (let item of data.lists) { - for (let [key, value] of item.fields.entries()) { - item.fields.set(key, value.map(x => Values.mapLeaves(x, t => (Values.isLink(t) ? linkNormalizer(t) : t)))); - } - } - } - if (data.links) { - data.links = data.links.map(l => linkNormalizer(l)); - } - // This is pretty ugly, but it's not possible to normalize on the worker thread that does parsing. - // The best way to improve this is to instead just canonicalize the entire data object; I can try to - // optimize `Values.mapLeaves` to only mutate if it actually changes things. - return new PageMetadata(data.path, data); - } - /** The name (based on path) of this file. */ - name() { - return getFileTitle(this.path); - } - /** The containing folder (based on path) of this file. */ - folder() { - return getParentFolder(this.path); - } - /** The extension of this file (likely 'md'). */ - extension() { - return getExtension(this.path); - } - /** Return a set of tags AND all of their parent tags (so #hello/yes would become #hello, #hello/yes). */ - fullTags() { - let result = new Set(); - for (let tag of this.tags) { - for (let subtag of extractSubtags(tag)) - result.add(subtag); - } - return result; - } - /** Convert all links in this file to file links. */ - fileLinks() { - // We want to make them distinct, but where links are not raw links we - // now keep the additional metadata. - let distinctLinks = new Set(this.links); - return Array.from(distinctLinks); - } - /** Map this metadata to a full object; uses the index for additional data lookups. */ - serialize(index, cache) { - // Convert list items via the canonicalization cache. - let realCache = cache !== null && cache !== void 0 ? cache : new ListSerializationCache(this.lists); - let result = { - file: { - path: this.path, - folder: this.folder(), - name: this.name(), - link: Link.file(this.path), - outlinks: this.fileLinks(), - inlinks: Array.from(index.links.getInverse(this.path)).map(l => Link.file(l)), - etags: Array.from(this.tags), - tags: Array.from(this.fullTags()), - aliases: Array.from(this.aliases), - lists: this.lists.map(l => realCache.get(l.line)), - tasks: this.lists.filter(l => !!l.task).map(l => realCache.get(l.line)), - ctime: this.ctime, - cday: stripTime(this.ctime), - mtime: this.mtime, - mday: stripTime(this.mtime), - size: this.size, - starred: index.starred.starred(this.path), - frontmatter: Values.deepCopy(this.frontmatter), - ext: this.extension(), - }, - }; - // Add the current day if present. - if (this.day) - result.file.day = this.day; - // Then append the computed fields. - for (let [key, value] of this.fields.entries()) { - if (key in result) - continue; // Don't allow fields to override existing keys. - result[key] = value; - } - return result; - } -} -/** A list item inside of a list. */ -class ListItem$1 { - constructor(init) { - Object.assign(this, init); - this.fields = this.fields || new Map(); - this.tags = this.tags || new Set(); - this.children = this.children || []; - this.links = this.links || []; - } - id() { - return `${this.file().path}-${this.line}`; - } - file() { - return this.link.toFile(); - } - markdown() { - if (this.task) - return `${this.symbol} [${this.task.completed ? "x" : " "}] ${this.text}`; - else - return `${this.symbol} ${this.text}`; - } - created() { - var _a, _b, _c; - return (_c = ((_b = (_a = this.fields.get("created")) !== null && _a !== void 0 ? _a : this.fields.get("ctime")) !== null && _b !== void 0 ? _b : this.fields.get("cday"))) === null || _c === void 0 ? void 0 : _c[0]; - } - due() { - var _a, _b, _c; - return (_c = ((_b = (_a = this.fields.get("due")) !== null && _a !== void 0 ? _a : this.fields.get("duetime")) !== null && _b !== void 0 ? _b : this.fields.get("dueday"))) === null || _c === void 0 ? void 0 : _c[0]; - } - completed() { - var _a, _b, _c, _d; - return (_d = ((_c = (_b = (_a = this.fields.get("completed")) !== null && _a !== void 0 ? _a : this.fields.get("completion")) !== null && _b !== void 0 ? _b : this.fields.get("comptime")) !== null && _c !== void 0 ? _c : this.fields.get("compday"))) === null || _d === void 0 ? void 0 : _d[0]; - } - start() { - var _a; - return (_a = this.fields.get("start")) === null || _a === void 0 ? void 0 : _a[0]; - } - scheduled() { - var _a; - return (_a = this.fields.get("scheduled")) === null || _a === void 0 ? void 0 : _a[0]; - } - /** Create an API-friendly copy of this list item. De-duplication is done via the provided cache. */ - serialize(cache) { - // Map children to their serialized/de-duplicated equivalents right away. - let children = this.children.map(l => cache.get(l)).filter((l) => l !== undefined); - let result = { - symbol: this.symbol, - link: this.link, - section: this.section, - text: this.text, - tags: Array.from(this.tags), - line: this.line, - lineCount: this.lineCount, - list: this.list, - outlinks: Array.from(this.links), - path: this.link.path, - children: children, - task: !!this.task, - annotated: this.fields.size > 0, - position: Values.deepCopy(this.position), - subtasks: children, - real: !!this.task, - header: this.section, // @deprecated, use 'item.section' instead. - }; - if (this.parent || this.parent === 0) - result.parent = this.parent; - if (this.blockId) - result.blockId = this.blockId; - addFields(this.fields, result); - if (this.task) { - result.status = this.task.status; - result.checked = this.task.checked; - result.completed = this.task.completed; - result.fullyCompleted = this.task.fullyCompleted; - let created = this.created(), due = this.due(), completed = this.completed(), start = this.start(), scheduled = this.scheduled(); - if (created) - result.created = Values.deepCopy(created); - if (due) - result.due = Values.deepCopy(due); - if (completed) - result.completion = Values.deepCopy(completed); - if (start) - result.start = Values.deepCopy(start); - if (scheduled) - result.scheduled = Values.deepCopy(scheduled); - } - return result; - } -} -////////////////////////////////////////// -// Conversion / Serialization Utilities // -////////////////////////////////////////// -/** De-duplicates list items across section metadata and page metadata. */ -class ListSerializationCache { - constructor(listItems) { - this.listItems = {}; - this.cache = {}; - this.seen = new Set(); - for (let item of listItems) - this.listItems[item.line] = item; - } - get(lineno) { - if (lineno in this.cache) - return this.cache[lineno]; - else if (this.seen.has(lineno)) { - console.log(`Dataview: Encountered a circular list (line number ${lineno}; children ${this.listItems[lineno].children.join(", ")})`); - return undefined; - } - this.seen.add(lineno); - let result = this.listItems[lineno].serialize(this); - this.cache[lineno] = result; - return result; - } -} -function addFields(fields, target) { - for (let [key, values] of fields.entries()) { - if (key in target) - continue; - target[key] = values.length == 1 ? values[0] : values; - } - return target; -} - -/** Importer for markdown documents. */ -// TODO: Consider using an actual parser in leiu of a more expensive regex. -const LIST_ITEM_REGEX = /^[\s>]*(\d+\.|\d+\)|\*|-|\+)\s*(\[.{0,1}\])?\s*(.*)$/mu; -/** Recursively convert frontmatter into fields. We have to dance around YAML structure. */ -function parseFrontmatter(value) { - if (value == null) { - return null; - } - else if (typeof value === "object") { - if (Array.isArray(value)) { - let result = []; - for (let child of value) { - result.push(parseFrontmatter(child)); - } - return result; - } - else if (value instanceof Date) { - let dateParse = DateTime.fromJSDate(value); - return dateParse; - } - else { - let object = value; - let result = {}; - for (let key in object) { - result[key] = parseFrontmatter(object[key]); - } - return result; - } - } - else if (typeof value === "number") { - return value; - } - else if (typeof value === "boolean") { - return value; - } - else if (typeof value === "string") { - let dateParse = EXPRESSION.date.parse(value); - if (dateParse.status) - return dateParse.value; - let durationParse = EXPRESSION.duration.parse(value); - if (durationParse.status) - return durationParse.value; - let linkParse = EXPRESSION.embedLink.parse(value); - if (linkParse.status) - return linkParse.value; - return value; - } - // Backup if we don't understand the type. - return null; -} - -/** Parse a CSV file into a collection of data rows. */ -function parseCsv(content) { - let parsed = papaparse_min.exports.parse(content, { - header: true, - skipEmptyLines: true, - comments: "#", - dynamicTyping: true, - }); - let rows = []; - for (let parsedRow of parsed.data) { - let fields = parseFrontmatter(parsedRow); - let result = {}; - for (let [key, value] of Object.entries(fields)) { - result[key] = value; - result[canonicalizeVarName(key)] = value; - } - rows.push(result); - } - return rows; -} - -/** Simplifies passing dataview values across the JS web worker barrier. */ -var Transferable; -(function (Transferable) { - /** Convert a literal value to a serializer-friendly transferable value. */ - function transferable(value) { - // Handle simple universal types first. - if (value instanceof Map) { - let copied = new Map(); - for (let [key, val] of value.entries()) - copied.set(transferable(key), transferable(val)); - return copied; - } - else if (value instanceof Set) { - let copied = new Set(); - for (let val of value) - copied.add(transferable(val)); - return copied; - } - let wrapped = Values.wrapValue(value); - if (wrapped === undefined) - throw Error("Unrecognized transferable value: " + value); - switch (wrapped.type) { - case "null": - case "number": - case "string": - case "boolean": - return wrapped.value; - case "date": - return { - "___transfer-type": "date", - value: transferable(wrapped.value.toObject()), - options: { - zone: wrapped.value.zone.equals(SystemZone.instance) ? undefined : wrapped.value.zoneName, - }, - }; - case "duration": - return { "___transfer-type": "duration", value: transferable(wrapped.value.toObject()) }; - case "array": - return wrapped.value.map(v => transferable(v)); - case "link": - return { "___transfer-type": "link", value: transferable(wrapped.value.toObject()) }; - case "object": - let result = {}; - for (let [key, value] of Object.entries(wrapped.value)) - result[key] = transferable(value); - return result; - } - } - Transferable.transferable = transferable; - /** Convert a transferable value back to a literal value we can work with. */ - function value(transferable) { - if (transferable === null) { - return null; - } - else if (transferable === undefined) { - return undefined; - } - else if (transferable instanceof Map) { - let real = new Map(); - for (let [key, val] of transferable.entries()) - real.set(value(key), value(val)); - return real; - } - else if (transferable instanceof Set) { - let real = new Set(); - for (let val of transferable) - real.add(value(val)); - return real; - } - else if (Array.isArray(transferable)) { - return transferable.map(v => value(v)); - } - else if (typeof transferable === "object") { - if ("___transfer-type" in transferable) { - switch (transferable["___transfer-type"]) { - case "date": - let dateOpts = value(transferable.options); - let dateData = value(transferable.value); - return DateTime.fromObject(dateData, { zone: dateOpts.zone }); - case "duration": - return Duration.fromObject(value(transferable.value)); - case "link": - return Link.fromObject(value(transferable.value)); - default: - throw Error(`Unrecognized transfer type '${transferable["___transfer-type"]}'`); - } - } - let result = {}; - for (let [key, val] of Object.entries(transferable)) - result[key] = value(val); - return result; - } - return transferable; - } - Transferable.value = value; -})(Transferable || (Transferable = {})); - -var localforage$1 = {exports: {}}; - -/*! - localForage -- Offline Storage, Improved - Version 1.10.0 - https://localforage.github.io/localForage - (c) 2013-2017 Mozilla, Apache License 2.0 -*/ - -(function (module, exports) { -(function(f){{module.exports=f();}})(function(){return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof commonjsRequire=="function"&&commonjsRequire;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw (f.code="MODULE_NOT_FOUND", f)}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r);}return n[o].exports}var i=typeof commonjsRequire=="function"&&commonjsRequire;for(var o=0;o element; its readystatechange event will be fired asynchronously once it is inserted - // into the document. Do so, thus queuing up the task. Remember to clean up once it's been called. - var scriptEl = global.document.createElement('script'); - scriptEl.onreadystatechange = function () { - nextTick(); - - scriptEl.onreadystatechange = null; - scriptEl.parentNode.removeChild(scriptEl); - scriptEl = null; - }; - global.document.documentElement.appendChild(scriptEl); - }; - } else { - scheduleDrain = function () { - setTimeout(nextTick, 0); - }; - } } - -var draining; -var queue = []; -//named nextTick for less confusing stack traces -function nextTick() { - draining = true; - var i, oldQueue; - var len = queue.length; - while (len) { - oldQueue = queue; - queue = []; - i = -1; - while (++i < len) { - oldQueue[i](); +/** Get the folder containing the given path (i.e., like computing 'path/..'). */ +function getParentFolder(path) { + return path.split("/").slice(0, -1).join("/"); +} +/** Get the "title" for a file, by stripping other parts of the path as well as the extension. */ +function getFileTitle(path) { + if (path.includes("/")) + path = path.substring(path.lastIndexOf("/") + 1); + if (path.endsWith(".md")) + path = path.substring(0, path.length - 3); + return path; +} +/** Get the extension of a file from the file path. */ +function getExtension(path) { + if (!path.includes(".")) + return ""; + return path.substring(path.lastIndexOf(".") + 1); +} +/** Parse all subtags out of the given tag. I.e., #hello/i/am would yield [#hello/i/am, #hello/i, #hello]. */ +function extractSubtags(tag) { + let result = [tag]; + while (tag.includes("/")) { + tag = tag.substring(0, tag.lastIndexOf("/")); + result.push(tag); } - len = queue.length; - } - draining = false; -} - -module.exports = immediate; -function immediate(task) { - if (queue.push(task) === 1 && !draining) { - scheduleDrain(); - } -} - -}).call(this,typeof commonjsGlobal !== "undefined" ? commonjsGlobal : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}); -},{}],2:[function(_dereq_,module,exports){ -var immediate = _dereq_(1); - -/* istanbul ignore next */ -function INTERNAL() {} - -var handlers = {}; - -var REJECTED = ['REJECTED']; -var FULFILLED = ['FULFILLED']; -var PENDING = ['PENDING']; - -module.exports = Promise; - -function Promise(resolver) { - if (typeof resolver !== 'function') { - throw new TypeError('resolver must be a function'); - } - this.state = PENDING; - this.queue = []; - this.outcome = void 0; - if (resolver !== INTERNAL) { - safelyResolveThenable(this, resolver); - } -} - -Promise.prototype["catch"] = function (onRejected) { - return this.then(null, onRejected); -}; -Promise.prototype.then = function (onFulfilled, onRejected) { - if (typeof onFulfilled !== 'function' && this.state === FULFILLED || - typeof onRejected !== 'function' && this.state === REJECTED) { - return this; - } - var promise = new this.constructor(INTERNAL); - if (this.state !== PENDING) { - var resolver = this.state === FULFILLED ? onFulfilled : onRejected; - unwrap(promise, resolver, this.outcome); - } else { - this.queue.push(new QueueItem(promise, onFulfilled, onRejected)); - } - - return promise; -}; -function QueueItem(promise, onFulfilled, onRejected) { - this.promise = promise; - if (typeof onFulfilled === 'function') { - this.onFulfilled = onFulfilled; - this.callFulfilled = this.otherCallFulfilled; - } - if (typeof onRejected === 'function') { - this.onRejected = onRejected; - this.callRejected = this.otherCallRejected; - } + return result; } -QueueItem.prototype.callFulfilled = function (value) { - handlers.resolve(this.promise, value); -}; -QueueItem.prototype.otherCallFulfilled = function (value) { - unwrap(this.promise, this.onFulfilled, value); -}; -QueueItem.prototype.callRejected = function (value) { - handlers.reject(this.promise, value); -}; -QueueItem.prototype.otherCallRejected = function (value) { - unwrap(this.promise, this.onRejected, value); -}; - -function unwrap(promise, func, value) { - immediate(function () { - var returnValue; +/** Try calling the given function; on failure, return the error message. */ +function tryOrPropogate(func) { try { - returnValue = func(value); - } catch (e) { - return handlers.reject(promise, e); + return func(); } - if (returnValue === promise) { - handlers.reject(promise, new TypeError('Cannot resolve promise with itself')); - } else { - handlers.resolve(promise, returnValue); - } - }); -} - -handlers.resolve = function (self, value) { - var result = tryCatch(getThen, value); - if (result.status === 'error') { - return handlers.reject(self, result.value); - } - var thenable = result.value; - - if (thenable) { - safelyResolveThenable(self, thenable); - } else { - self.state = FULFILLED; - self.outcome = value; - var i = -1; - var len = self.queue.length; - while (++i < len) { - self.queue[i].callFulfilled(value); + catch (error) { + return Result.failure("" + error + "\n\n" + error.stack); } - } - return self; -}; -handlers.reject = function (self, error) { - self.state = REJECTED; - self.outcome = error; - var i = -1; - var len = self.queue.length; - while (++i < len) { - self.queue[i].callRejected(error); - } - return self; -}; - -function getThen(obj) { - // Make sure we only access the accessor once as required by the spec - var then = obj && obj.then; - if (obj && (typeof obj === 'object' || typeof obj === 'function') && typeof then === 'function') { - return function appyThen() { - then.apply(obj, arguments); - }; - } } - -function safelyResolveThenable(self, thenable) { - // Either fulfill, reject or reject with error - var called = false; - function onError(value) { - if (called) { - return; +/** Try asynchronously calling the given function; on failure, return the error message. */ +async function asyncTryOrPropogate(func) { + try { + return await func(); } - called = true; - handlers.reject(self, value); - } - - function onSuccess(value) { - if (called) { - return; + catch (error) { + return Result.failure("" + error + "\n\n" + error.stack); } - called = true; - handlers.resolve(self, value); - } - - function tryToUnwrap() { - thenable(onSuccess, onError); - } - - var result = tryCatch(tryToUnwrap); - if (result.status === 'error') { - onError(result.value); - } -} - -function tryCatch(func, value) { - var out = {}; - try { - out.value = func(value); - out.status = 'success'; - } catch (e) { - out.status = 'error'; - out.value = e; - } - return out; -} - -Promise.resolve = resolve; -function resolve(value) { - if (value instanceof this) { - return value; - } - return handlers.resolve(new this(INTERNAL), value); -} - -Promise.reject = reject; -function reject(reason) { - var promise = new this(INTERNAL); - return handlers.reject(promise, reason); } - -Promise.all = all; -function all(iterable) { - var self = this; - if (Object.prototype.toString.call(iterable) !== '[object Array]') { - return this.reject(new TypeError('must be an array')); - } - - var len = iterable.length; - var called = false; - if (!len) { - return this.resolve([]); - } - - var values = new Array(len); - var resolved = 0; - var i = -1; - var promise = new this(INTERNAL); - - while (++i < len) { - allResolver(iterable[i], i); - } - return promise; - function allResolver(value, i) { - self.resolve(value).then(resolveFromAll, function (error) { - if (!called) { - called = true; - handlers.reject(promise, error); - } - }); - function resolveFromAll(outValue) { - values[i] = outValue; - if (++resolved === len && !called) { - called = true; - handlers.resolve(promise, values); - } +/** + * Escape regex characters in a string. + * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions. + */ +function escapeRegex(str) { + return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +} +/** A parsimmon parser which canonicalizes variable names while properly respecting emoji. */ +const VAR_NAME_CANONICALIZER = parsimmon_umd_minExports.alt(parsimmon_umd_minExports.regex(new RegExp(emojiRegex(), "")), parsimmon_umd_minExports.regex(/[0-9\p{Letter}_-]+/u).map(str => str.toLocaleLowerCase()), parsimmon_umd_minExports.whitespace.map(_ => "-"), parsimmon_umd_minExports.any.map(_ => "")) + .many() + .map(result => result.join("")); +/** Convert an arbitrary variable name into something JS/query friendly. */ +function canonicalizeVarName(name) { + return VAR_NAME_CANONICALIZER.tryParse(name); +} +const HEADER_CANONICALIZER = parsimmon_umd_minExports.alt(parsimmon_umd_minExports.regex(new RegExp(emojiRegex(), "")), parsimmon_umd_minExports.regex(/[0-9\p{Letter}_-]+/u), parsimmon_umd_minExports.whitespace.map(_ => " "), parsimmon_umd_minExports.any.map(_ => " ")) + .many() + .map(result => { + return result.join("").split(/\s+/).join(" ").trim(); +}); +/** + * Normalizes the text in a header to be something that is actually linkable to. This mimics + * how Obsidian does it's normalization, collapsing repeated spaces and stripping out control characters. + */ +function normalizeHeaderForLink(header) { + return HEADER_CANONICALIZER.tryParse(header); +} +/** Render a DateTime in a minimal format to save space. */ +function renderMinimalDate(time, settings, locale) { + // If there is no relevant time specified, fall back to just rendering the date. + if (time.second == 0 && time.minute == 0 && time.hour == 0) { + return time.toLocal().toFormat(settings.defaultDateFormat, { locale }); } - } -} - -Promise.race = race; -function race(iterable) { - var self = this; - if (Object.prototype.toString.call(iterable) !== '[object Array]') { - return this.reject(new TypeError('must be an array')); - } - - var len = iterable.length; - var called = false; - if (!len) { - return this.resolve([]); - } - - var i = -1; - var promise = new this(INTERNAL); - - while (++i < len) { - resolver(iterable[i]); - } - return promise; - function resolver(value) { - self.resolve(value).then(function (response) { - if (!called) { - called = true; - handlers.resolve(promise, response); - } - }, function (error) { - if (!called) { - called = true; - handlers.reject(promise, error); - } - }); - } -} - -},{"1":1}],3:[function(_dereq_,module,exports){ -(function (global){ -if (typeof global.Promise !== 'function') { - global.Promise = _dereq_(2); + return time.toLocal().toFormat(settings.defaultDateTimeFormat, { locale }); +} +/** Render a duration in a minimal format to save space. */ +function renderMinimalDuration(dur) { + dur = normalizeDuration(dur); + // toHuman outputs zero quantities e.g. "0 seconds" + dur = Duration.fromObject(Object.fromEntries(Object.entries(dur.toObject()).filter(([, quantity]) => quantity != 0))); + return dur.toHuman(); +} +/** Determine if two sets are equal in contents. */ +function setsEqual(first, second) { + if (first.size != second.size) + return false; + for (let elem of first) + if (!second.has(elem)) + return false; + return true; } -}).call(this,typeof commonjsGlobal !== "undefined" ? commonjsGlobal : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}); -},{"2":2}],4:[function(_dereq_,module,exports){ - -var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - -function getIDB() { - /* global indexedDB,webkitIndexedDB,mozIndexedDB,OIndexedDB,msIndexedDB */ - try { - if (typeof indexedDB !== 'undefined') { - return indexedDB; +var Values; +(function (Values) { + /** Convert an arbitrary value into a reasonable, Markdown-friendly string if possible. */ + function toString(field, setting = DEFAULT_QUERY_SETTINGS, recursive = false) { + let wrapped = wrapValue(field); + if (!wrapped) + return setting.renderNullAs; + switch (wrapped.type) { + case "null": + return setting.renderNullAs; + case "string": + return wrapped.value; + case "number": + case "boolean": + return "" + wrapped.value; + case "html": + return wrapped.value.outerHTML; + case "widget": + return wrapped.value.markdown(); + case "link": + return wrapped.value.markdown(); + case "function": + return ""; + case "array": + let result = ""; + if (recursive) + result += "["; + result += wrapped.value.map(f => toString(f, setting, true)).join(", "); + if (recursive) + result += "]"; + return result; + case "object": + return ("{ " + + Object.entries(wrapped.value) + .map(e => e[0] + ": " + toString(e[1], setting, true)) + .join(", ") + + " }"); + case "date": + if (wrapped.value.second == 0 && wrapped.value.hour == 0 && wrapped.value.minute == 0) { + return wrapped.value.toFormat(setting.defaultDateFormat); + } + return wrapped.value.toFormat(setting.defaultDateTimeFormat); + case "duration": + return renderMinimalDuration(wrapped.value); } - if (typeof webkitIndexedDB !== 'undefined') { - return webkitIndexedDB; + } + Values.toString = toString; + /** Wrap a literal value so you can switch on it easily. */ + function wrapValue(val) { + if (isNull(val)) + return { type: "null", value: val }; + else if (isNumber(val)) + return { type: "number", value: val }; + else if (isString(val)) + return { type: "string", value: val }; + else if (isBoolean(val)) + return { type: "boolean", value: val }; + else if (isDuration(val)) + return { type: "duration", value: val }; + else if (isDate(val)) + return { type: "date", value: val }; + else if (isWidget(val)) + return { type: "widget", value: val }; + else if (isArray(val)) + return { type: "array", value: val }; + else if (isLink(val)) + return { type: "link", value: val }; + else if (isFunction(val)) + return { type: "function", value: val }; + else if (isHtml(val)) + return { type: "html", value: val }; + else if (isObject(val)) + return { type: "object", value: val }; + else + return undefined; + } + Values.wrapValue = wrapValue; + /** Recursively map complex objects at the leaves. */ + function mapLeaves(val, func) { + if (isObject(val)) { + let result = {}; + for (let [key, value] of Object.entries(val)) + result[key] = mapLeaves(value, func); + return result; } - if (typeof mozIndexedDB !== 'undefined') { - return mozIndexedDB; + else if (isArray(val)) { + let result = []; + for (let value of val) + result.push(mapLeaves(value, func)); + return result; } - if (typeof OIndexedDB !== 'undefined') { - return OIndexedDB; + else { + return func(val); } - if (typeof msIndexedDB !== 'undefined') { - return msIndexedDB; + } + Values.mapLeaves = mapLeaves; + /** Compare two arbitrary JavaScript values. Produces a total ordering over ANY possible dataview value. */ + function compareValue(val1, val2, linkNormalizer) { + var _a, _b; + // Handle undefined/nulls first. + if (val1 === undefined) + val1 = null; + if (val2 === undefined) + val2 = null; + if (val1 === null && val2 === null) + return 0; + else if (val1 === null) + return -1; + else if (val2 === null) + return 1; + // A non-null value now which we can wrap & compare on. + let wrap1 = wrapValue(val1); + let wrap2 = wrapValue(val2); + if (wrap1 === undefined && wrap2 === undefined) + return 0; + else if (wrap1 === undefined) + return -1; + else if (wrap2 === undefined) + return 1; + // Short-circuit on different types or on reference equality. + if (wrap1.type != wrap2.type) + return wrap1.type.localeCompare(wrap2.type); + if (wrap1.value === wrap2.value) + return 0; + switch (wrap1.type) { + case "string": + return wrap1.value.localeCompare(wrap2.value); + case "number": + if (wrap1.value < wrap2.value) + return -1; + else if (wrap1.value == wrap2.value) + return 0; + return 1; + case "null": + return 0; + case "boolean": + if (wrap1.value == wrap2.value) + return 0; + else + return wrap1.value ? 1 : -1; + case "link": + let link1 = wrap1.value; + let link2 = wrap2.value; + let normalize = linkNormalizer !== null && linkNormalizer !== void 0 ? linkNormalizer : ((x) => x); + // We can't compare by file name or display, since that would break link equality. Compare by path. + let pathCompare = normalize(link1.path).localeCompare(normalize(link2.path)); + if (pathCompare != 0) + return pathCompare; + // Then compare by type. + let typeCompare = link1.type.localeCompare(link2.type); + if (typeCompare != 0) + return typeCompare; + // Then compare by subpath existence. + if (link1.subpath && !link2.subpath) + return 1; + if (!link1.subpath && link2.subpath) + return -1; + if (!link1.subpath && !link2.subpath) + return 0; + // Since both have a subpath, compare by subpath. + return ((_a = link1.subpath) !== null && _a !== void 0 ? _a : "").localeCompare((_b = link2.subpath) !== null && _b !== void 0 ? _b : ""); + case "date": + return wrap1.value < wrap2.value + ? -1 + : wrap1.value.equals(wrap2.value) + ? 0 + : 1; + case "duration": + return wrap1.value < wrap2.value + ? -1 + : wrap1.value.equals(wrap2.value) + ? 0 + : 1; + case "array": + let f1 = wrap1.value; + let f2 = wrap2.value; + for (let index = 0; index < Math.min(f1.length, f2.length); index++) { + let comp = compareValue(f1[index], f2[index]); + if (comp != 0) + return comp; + } + return f1.length - f2.length; + case "object": + let o1 = wrap1.value; + let o2 = wrap2.value; + let k1 = Array.from(Object.keys(o1)); + let k2 = Array.from(Object.keys(o2)); + k1.sort(); + k2.sort(); + let keyCompare = compareValue(k1, k2); + if (keyCompare != 0) + return keyCompare; + for (let key of k1) { + let comp = compareValue(o1[key], o2[key]); + if (comp != 0) + return comp; + } + return 0; + case "widget": + case "html": + case "function": + return 0; } - } catch (e) { - return; } -} - -var idb = getIDB(); - -function isIndexedDBValid() { - try { - // Initialize IndexedDB; fall back to vendor-prefixed versions - // if needed. - if (!idb || !idb.open) { + Values.compareValue = compareValue; + /** Find the corresponding Dataveiw type for an arbitrary value. */ + function typeOf(val) { + var _a; + return (_a = wrapValue(val)) === null || _a === void 0 ? void 0 : _a.type; + } + Values.typeOf = typeOf; + /** Determine if the given value is "truthy" (i.e., is non-null and has data in it). */ + function isTruthy(field) { + let wrapped = wrapValue(field); + if (!wrapped) return false; + switch (wrapped.type) { + case "number": + return wrapped.value != 0; + case "string": + return wrapped.value.length > 0; + case "boolean": + return wrapped.value; + case "link": + return !!wrapped.value.path; + case "date": + return wrapped.value.toMillis() != 0; + case "duration": + return wrapped.value.as("seconds") != 0; + case "object": + return Object.keys(wrapped.value).length > 0; + case "array": + return wrapped.value.length > 0; + case "null": + return false; + case "html": + case "widget": + case "function": + return true; } - // We mimic PouchDB here; - // - // We test for openDatabase because IE Mobile identifies itself - // as Safari. Oh the lulz... - var isSafari = typeof openDatabase !== 'undefined' && /(Safari|iPhone|iPad|iPod)/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent) && !/BlackBerry/.test(navigator.platform); - - var hasFetch = typeof fetch === 'function' && fetch.toString().indexOf('[native code') !== -1; - - // Safari <10.1 does not meet our requirements for IDB support - // (see: https://github.com/pouchdb/pouchdb/issues/5572). - // Safari 10.1 shipped with fetch, we can use that to detect it. - // Note: this creates issues with `window.fetch` polyfills and - // overrides; see: - // https://github.com/localForage/localForage/issues/856 - return (!isSafari || hasFetch) && typeof indexedDB !== 'undefined' && - // some outdated implementations of IDB that appear on Samsung - // and HTC Android devices <4.4 are missing IDBKeyRange - // See: https://github.com/mozilla/localForage/issues/128 - // See: https://github.com/mozilla/localForage/issues/272 - typeof IDBKeyRange !== 'undefined'; - } catch (e) { - return false; } -} - -// Abstracts constructing a Blob object, so it also works in older -// browsers that don't support the native Blob constructor. (i.e. -// old QtWebKit versions, at least). -// Abstracts constructing a Blob object, so it also works in older -// browsers that don't support the native Blob constructor. (i.e. -// old QtWebKit versions, at least). -function createBlob(parts, properties) { - /* global BlobBuilder,MSBlobBuilder,MozBlobBuilder,WebKitBlobBuilder */ - parts = parts || []; - properties = properties || {}; - try { - return new Blob(parts, properties); - } catch (e) { - if (e.name !== 'TypeError') { - throw e; + Values.isTruthy = isTruthy; + /** Deep copy a field. */ + function deepCopy(field) { + if (field === null || field === undefined) + return field; + if (Values.isArray(field)) { + return [].concat(field.map(v => deepCopy(v))); + } + else if (Values.isObject(field)) { + let result = {}; + for (let [key, value] of Object.entries(field)) + result[key] = deepCopy(value); + return result; } - var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder : typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder : typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder : WebKitBlobBuilder; - var builder = new Builder(); - for (var i = 0; i < parts.length; i += 1) { - builder.append(parts[i]); + else { + return field; } - return builder.getBlob(properties.type); } -} - -// This is CommonJS because lie is an external dependency, so Rollup -// can just ignore it. -if (typeof Promise === 'undefined') { - // In the "nopromises" build this will just throw if you don't have - // a global promise object, but it would throw anyway later. - _dereq_(3); -} -var Promise$1 = Promise; - -function executeCallback(promise, callback) { - if (callback) { - promise.then(function (result) { - callback(null, result); - }, function (error) { - callback(error); - }); + Values.deepCopy = deepCopy; + function isString(val) { + return typeof val == "string"; } -} - -function executeTwoCallbacks(promise, callback, errorCallback) { - if (typeof callback === 'function') { - promise.then(callback); + Values.isString = isString; + function isNumber(val) { + return typeof val == "number"; } - - if (typeof errorCallback === 'function') { - promise["catch"](errorCallback); + Values.isNumber = isNumber; + function isDate(val) { + return val instanceof DateTime; } -} - -function normalizeKey(key) { - // Cast the key to a string, as that's all we can set as a key. - if (typeof key !== 'string') { - console.warn(key + ' used as a key, but it is not a string.'); - key = String(key); + Values.isDate = isDate; + function isDuration(val) { + return val instanceof Duration; } - - return key; -} - -function getCallback() { - if (arguments.length && typeof arguments[arguments.length - 1] === 'function') { - return arguments[arguments.length - 1]; + Values.isDuration = isDuration; + function isNull(val) { + return val === null || val === undefined; } -} - -// Some code originally from async_storage.js in -// [Gaia](https://github.com/mozilla-b2g/gaia). - -var DETECT_BLOB_SUPPORT_STORE = 'local-forage-detect-blob-support'; -var supportsBlobs = void 0; -var dbContexts = {}; -var toString = Object.prototype.toString; - -// Transaction Modes -var READ_ONLY = 'readonly'; -var READ_WRITE = 'readwrite'; - -// Transform a binary string to an array buffer, because otherwise -// weird stuff happens when you try to work with the binary string directly. -// It is known. -// From http://stackoverflow.com/questions/14967647/ (continues on next line) -// encode-decode-image-with-base64-breaks-image (2013-04-21) -function _binStringToArrayBuffer(bin) { - var length = bin.length; - var buf = new ArrayBuffer(length); - var arr = new Uint8Array(buf); - for (var i = 0; i < length; i++) { - arr[i] = bin.charCodeAt(i); + Values.isNull = isNull; + function isArray(val) { + return Array.isArray(val); } - return buf; -} - -// -// Blobs are not supported in all versions of IndexedDB, notably -// Chrome <37 and Android <5. In those versions, storing a blob will throw. -// -// Various other blob bugs exist in Chrome v37-42 (inclusive). -// Detecting them is expensive and confusing to users, and Chrome 37-42 -// is at very low usage worldwide, so we do a hacky userAgent check instead. -// -// content-type bug: https://code.google.com/p/chromium/issues/detail?id=408120 -// 404 bug: https://code.google.com/p/chromium/issues/detail?id=447916 -// FileReader bug: https://code.google.com/p/chromium/issues/detail?id=447836 -// -// Code borrowed from PouchDB. See: -// https://github.com/pouchdb/pouchdb/blob/master/packages/node_modules/pouchdb-adapter-idb/src/blobSupport.js -// -function _checkBlobSupportWithoutCaching(idb) { - return new Promise$1(function (resolve) { - var txn = idb.transaction(DETECT_BLOB_SUPPORT_STORE, READ_WRITE); - var blob = createBlob(['']); - txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob, 'key'); - - txn.onabort = function (e) { - // If the transaction aborts now its due to not being able to - // write to the database, likely due to the disk being full - e.preventDefault(); - e.stopPropagation(); - resolve(false); - }; - - txn.oncomplete = function () { - var matchedChrome = navigator.userAgent.match(/Chrome\/(\d+)/); - var matchedEdge = navigator.userAgent.match(/Edge\//); - // MS Edge pretends to be Chrome 42: - // https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx - resolve(matchedEdge || !matchedChrome || parseInt(matchedChrome[1], 10) >= 43); - }; - })["catch"](function () { - return false; // error, so assume unsupported - }); -} - -function _checkBlobSupport(idb) { - if (typeof supportsBlobs === 'boolean') { - return Promise$1.resolve(supportsBlobs); + Values.isArray = isArray; + function isBoolean(val) { + return typeof val === "boolean"; } - return _checkBlobSupportWithoutCaching(idb).then(function (value) { - supportsBlobs = value; - return supportsBlobs; - }); -} - -function _deferReadiness(dbInfo) { - var dbContext = dbContexts[dbInfo.name]; - - // Create a deferred object representing the current database operation. - var deferredOperation = {}; - - deferredOperation.promise = new Promise$1(function (resolve, reject) { - deferredOperation.resolve = resolve; - deferredOperation.reject = reject; - }); - - // Enqueue the deferred operation. - dbContext.deferredOperations.push(deferredOperation); - - // Chain its promise to the database readiness. - if (!dbContext.dbReady) { - dbContext.dbReady = deferredOperation.promise; - } else { - dbContext.dbReady = dbContext.dbReady.then(function () { - return deferredOperation.promise; + Values.isBoolean = isBoolean; + function isLink(val) { + return val instanceof Link; + } + Values.isLink = isLink; + function isWidget(val) { + return val instanceof Widget; + } + Values.isWidget = isWidget; + function isHtml(val) { + if (typeof HTMLElement !== "undefined") { + return val instanceof HTMLElement; + } + else { + return false; + } + } + Values.isHtml = isHtml; + /** Checks if the given value is an object (and not any other dataview-recognized object-like type). */ + function isObject(val) { + return (typeof val == "object" && + !isHtml(val) && + !isWidget(val) && + !isArray(val) && + !isDuration(val) && + !isDate(val) && + !isLink(val) && + val !== undefined && + !isNull(val)); + } + Values.isObject = isObject; + function isFunction(val) { + return typeof val == "function"; + } + Values.isFunction = isFunction; +})(Values || (Values = {})); +/////////////// +// Groupings // +/////////////// +var Groupings; +(function (Groupings) { + /** Determines if the given group entry is a standalone value, or a grouping of sub-entries. */ + function isElementGroup(entry) { + return Values.isObject(entry) && Object.keys(entry).length == 2 && "key" in entry && "rows" in entry; + } + Groupings.isElementGroup = isElementGroup; + /** Determines if the given array is a grouping array. */ + function isGrouping(entry) { + for (let element of entry) + if (!isElementGroup(element)) + return false; + return true; + } + Groupings.isGrouping = isGrouping; + /** Count the total number of elements in a recursive grouping. */ + function count(elements) { + if (isGrouping(elements)) { + let result = 0; + for (let subgroup of elements) + result += count(subgroup.rows); + return result; + } + else { + return elements.length; + } + } + Groupings.count = count; +})(Groupings || (Groupings = {})); +////////// +// LINK // +////////// +/** The Obsidian 'link', used for uniquely describing a file, header, or block. */ +class Link { + /** Create a link to a specific file. */ + static file(path, embed = false, display) { + return new Link({ + path, + embed, + display, + subpath: undefined, + type: "file", }); } -} - -function _advanceReadiness(dbInfo) { - var dbContext = dbContexts[dbInfo.name]; - - // Dequeue a deferred operation. - var deferredOperation = dbContext.deferredOperations.pop(); - - // Resolve its promise (which is part of the database readiness - // chain of promises). - if (deferredOperation) { - deferredOperation.resolve(); - return deferredOperation.promise; + static infer(linkpath, embed = false, display) { + if (linkpath.includes("#^")) { + let split = linkpath.split("#^"); + return Link.block(split[0], split[1], embed, display); + } + else if (linkpath.includes("#")) { + let split = linkpath.split("#"); + return Link.header(split[0], split[1], embed, display); + } + else + return Link.file(linkpath, embed, display); } -} - -function _rejectReadiness(dbInfo, err) { - var dbContext = dbContexts[dbInfo.name]; - - // Dequeue a deferred operation. - var deferredOperation = dbContext.deferredOperations.pop(); - - // Reject its promise (which is part of the database readiness - // chain of promises). - if (deferredOperation) { - deferredOperation.reject(err); - return deferredOperation.promise; + /** Create a link to a specific file and header in that file. */ + static header(path, header, embed, display) { + // Headers need to be normalized to alpha-numeric & with extra spacing removed. + return new Link({ + path, + embed, + display, + subpath: normalizeHeaderForLink(header), + type: "header", + }); } -} - -function _getConnection(dbInfo, upgradeNeeded) { - return new Promise$1(function (resolve, reject) { - dbContexts[dbInfo.name] = dbContexts[dbInfo.name] || createDbContext(); - - if (dbInfo.db) { - if (upgradeNeeded) { - _deferReadiness(dbInfo); - dbInfo.db.close(); - } else { - return resolve(dbInfo.db); - } + /** Create a link to a specific file and block in that file. */ + static block(path, blockId, embed, display) { + return new Link({ + path, + embed, + display, + subpath: blockId, + type: "block", + }); + } + static fromObject(object) { + return new Link(object); + } + constructor(fields) { + Object.assign(this, fields); + } + /** Checks for link equality (i.e., that the links are pointing to the same exact location). */ + equals(other) { + if (other == undefined || other == null) + return false; + return this.path == other.path && this.type == other.type && this.subpath == other.subpath; + } + /** Convert this link to it's markdown representation. */ + toString() { + return this.markdown(); + } + /** Convert this link to a raw object which is serialization-friendly. */ + toObject() { + return { path: this.path, type: this.type, subpath: this.subpath, display: this.display, embed: this.embed }; + } + /** Update this link with a new path. */ + //@ts-ignore; error appeared after updating Obsidian to 0.15.4; it also updated other packages but didn't say which + withPath(path) { + return new Link(Object.assign({}, this, { path })); + } + /** Return a new link which points to the same location but with a new display value. */ + withDisplay(display) { + return new Link(Object.assign({}, this, { display })); + } + /** Convert a file link into a link to a specific header. */ + withHeader(header) { + return Link.header(this.path, header, this.embed, this.display); + } + /** Convert any link into a link to its file. */ + toFile() { + return Link.file(this.path, this.embed, this.display); + } + /** Convert this link into an embedded link. */ + toEmbed() { + if (this.embed) { + return this; } - - var dbArgs = [dbInfo.name]; - - if (upgradeNeeded) { - dbArgs.push(dbInfo.version); + else { + let link = new Link(this); + link.embed = true; + return link; } - - var openreq = idb.open.apply(idb, dbArgs); - - if (upgradeNeeded) { - openreq.onupgradeneeded = function (e) { - var db = openreq.result; - try { - db.createObjectStore(dbInfo.storeName); - if (e.oldVersion <= 1) { - // Added when support for blob shims was added - db.createObjectStore(DETECT_BLOB_SUPPORT_STORE); - } - } catch (ex) { - if (ex.name === 'ConstraintError') { - console.warn('The database "' + dbInfo.name + '"' + ' has been upgraded from version ' + e.oldVersion + ' to version ' + e.newVersion + ', but the storage "' + dbInfo.storeName + '" already exists.'); - } else { - throw ex; - } - } - }; + } + /** Convert this link into a non-embedded link. */ + fromEmbed() { + if (!this.embed) { + return this; } - - openreq.onerror = function (e) { - e.preventDefault(); - reject(openreq.error); - }; - - openreq.onsuccess = function () { - var db = openreq.result; - db.onversionchange = function (e) { - // Triggered when the database is modified (e.g. adding an objectStore) or - // deleted (even when initiated by other sessions in different tabs). - // Closing the connection here prevents those operations from being blocked. - // If the database is accessed again later by this instance, the connection - // will be reopened or the database recreated as needed. - e.target.close(); - }; - resolve(db); - _advanceReadiness(dbInfo); - }; - }); + else { + let link = new Link(this); + link.embed = false; + return link; + } + } + /** Convert this link to markdown so it can be rendered. */ + markdown() { + let result = (this.embed ? "!" : "") + "[[" + this.obsidianLink(); + if (this.display) { + result += "|" + this.display; + } + else { + result += "|" + getFileTitle(this.path); + if (this.type == "header" || this.type == "block") + result += " > " + this.subpath; + } + result += "]]"; + return result; + } + /** Convert the inner part of the link to something that Obsidian can open / understand. */ + obsidianLink() { + var _a, _b; + const escaped = this.path.replace("|", "\\|"); + if (this.type == "header") + return escaped + "#" + ((_a = this.subpath) === null || _a === void 0 ? void 0 : _a.replace("|", "\\|")); + if (this.type == "block") + return escaped + "#^" + ((_b = this.subpath) === null || _b === void 0 ? void 0 : _b.replace("|", "\\|")); + else + return escaped; + } + /** The stripped name of the file this link points to. */ + fileName() { + return getFileTitle(this.path).replace(".md", ""); + } } - -function _getOriginalConnection(dbInfo) { - return _getConnection(dbInfo, false); +///////////////// +// WIDGET BASE // +///////////////// +/** + * A trivial base class which just defines the '$widget' identifier type. Subtypes of + * widget are responsible for adding whatever metadata is relevant. If you want your widget + * to have rendering functionality (which you probably do), you should extend `RenderWidget`. + */ +class Widget { + constructor($widget) { + this.$widget = $widget; + } } - -function _getUpgradedConnection(dbInfo) { - return _getConnection(dbInfo, true); +/** A trivial widget which renders a (key, value) pair, and allows accessing the key and value. */ +class ListPairWidget extends Widget { + constructor(key, value) { + super("dataview:list-pair"); + this.key = key; + this.value = value; + } + markdown() { + return `${Values.toString(this.key)}: ${Values.toString(this.value)}`; + } } - -function _isUpgradeNeeded(dbInfo, defaultVersion) { - if (!dbInfo.db) { - return true; +/** A simple widget which renders an external link. */ +class ExternalLinkWidget extends Widget { + constructor(url, display) { + super("dataview:external-link"); + this.url = url; + this.display = display; } + markdown() { + var _a; + return `[${(_a = this.display) !== null && _a !== void 0 ? _a : this.url}](${this.url})`; + } +} +var Widgets; +(function (Widgets) { + /** Create a list pair widget matching the given key and value. */ + function listPair(key, value) { + return new ListPairWidget(key, value); + } + Widgets.listPair = listPair; + /** Create an external link widget which renders an external Obsidian link. */ + function externalLink(url, display) { + return new ExternalLinkWidget(url, display); + } + Widgets.externalLink = externalLink; + /** Checks if the given widget is a list pair widget. */ + function isListPair(widget) { + return widget.$widget === "dataview:list-pair"; + } + Widgets.isListPair = isListPair; + function isExternalLink(widget) { + return widget.$widget === "dataview:external-link"; + } + Widgets.isExternalLink = isExternalLink; + /** Determines if the given widget is any kind of built-in widget with special rendering handling. */ + function isBuiltin(widget) { + return isListPair(widget) || isExternalLink(widget); + } + Widgets.isBuiltin = isBuiltin; +})(Widgets || (Widgets = {})); - var isNewStore = !dbInfo.db.objectStoreNames.contains(dbInfo.storeName); - var isDowngrade = dbInfo.version < dbInfo.db.version; - var isUpgrade = dbInfo.version > dbInfo.db.version; - - if (isDowngrade) { - // If the version is not the default one - // then warn for impossible downgrade. - if (dbInfo.version !== defaultVersion) { - console.warn('The database "' + dbInfo.name + '"' + " can't be downgraded from version " + dbInfo.db.version + ' to version ' + dbInfo.version + '.'); +/** Implementation of DataArray, minus the dynamic variable access, which is implemented via proxy. */ +class DataArrayImpl { + static wrap(arr, settings, defaultComparator = Values.compareValue) { + return new Proxy(new DataArrayImpl(arr, settings, defaultComparator), DataArrayImpl.ARRAY_PROXY); + } + constructor(values, settings, defaultComparator = Values.compareValue) { + this.values = values; + this.settings = settings; + this.defaultComparator = defaultComparator; + this.length = values.length; + } + lwrap(values) { + return DataArrayImpl.wrap(values, this.settings, this.defaultComparator); + } + where(predicate) { + return this.lwrap(this.values.filter(predicate)); + } + filter(predicate) { + return this.where(predicate); + } + map(f) { + return this.lwrap(this.values.map(f)); + } + flatMap(f) { + let result = []; + for (let index = 0; index < this.length; index++) { + let value = f(this.values[index], index, this.values); + if (!value || value.length == 0) + continue; + for (let r of value) + result.push(r); } - // Align the versions to prevent errors. - dbInfo.version = dbInfo.db.version; - } - - if (isUpgrade || isNewStore) { - // If the store is new then increment the version (if needed). - // This will trigger an "upgradeneeded" event which is required - // for creating a store. - if (isNewStore) { - var incVersion = dbInfo.db.version + 1; - if (incVersion > dbInfo.version) { - dbInfo.version = incVersion; + return this.lwrap(result); + } + mutate(f) { + for (let index = 0; index < this.values.length; index++) { + f(this.values[index], index, this.values); + } + return this; + } + limit(count) { + return this.lwrap(this.values.slice(0, count)); + } + slice(start, end) { + return this.lwrap(this.values.slice(start, end)); + } + concat(other) { + return this.lwrap(this.values.concat(other.values)); + } + /** Return the first index of the given (optionally starting the search) */ + indexOf(element, fromIndex) { + return this.findIndex(e => this.defaultComparator(e, element) == 0, fromIndex); + } + /** Return the first element that satisfies the given predicate. */ + find(pred) { + let index = this.findIndex(pred); + if (index == -1) + return undefined; + else + return this.values[index]; + } + findIndex(pred, fromIndex) { + for (let index = fromIndex !== null && fromIndex !== void 0 ? fromIndex : 0; index < this.length; index++) { + if (pred(this.values[index], index, this.values)) + return index; + } + return -1; + } + includes(element) { + return this.indexOf(element, 0) != -1; + } + join(sep) { + return this.map(s => Values.toString(s, this.settings)) + .array() + .join(sep !== null && sep !== void 0 ? sep : ", "); + } + sort(key, direction, comparator) { + if (this.values.length == 0) + return this; + let realComparator = comparator !== null && comparator !== void 0 ? comparator : this.defaultComparator; + let realKey = key !== null && key !== void 0 ? key : ((l) => l); + // Associate each entry with it's index for the key function, and then do a normal sort. + let copy = [].concat(this.array()).map((elem, index) => { + return { index: index, value: elem }; + }); + copy.sort((a, b) => { + let aKey = realKey(a.value, a.index, this.values); + let bKey = realKey(b.value, b.index, this.values); + return direction === "desc" ? -realComparator(aKey, bKey) : realComparator(aKey, bKey); + }); + return this.lwrap(copy.map(e => e.value)); + } + sortInPlace(key, direction, comparator) { + if (this.values.length == 0) + return this; + let realComparator = comparator !== null && comparator !== void 0 ? comparator : this.defaultComparator; + let realKey = key !== null && key !== void 0 ? key : ((l) => l); + this.values.sort((a, b) => { + let aKey = realKey(a); + let bKey = realKey(b); + return direction == "desc" ? -realComparator(aKey, bKey) : realComparator(aKey, bKey); + }); + return this; + } + groupBy(key, comparator) { + if (this.values.length == 0) + return this.lwrap([]); + // JavaScript sucks and we can't make hash maps over arbitrary types (only strings/ints), so + // we do a poor man algorithm where we SORT, followed by grouping. + let intermediate = this.sort(key, "asc", comparator); + comparator = comparator !== null && comparator !== void 0 ? comparator : this.defaultComparator; + let result = []; + let currentRow = [intermediate[0]]; + let current = key(intermediate[0], 0, intermediate.values); + for (let index = 1; index < intermediate.length; index++) { + let newKey = key(intermediate[index], index, intermediate.values); + if (comparator(current, newKey) != 0) { + result.push({ key: current, rows: this.lwrap(currentRow) }); + current = newKey; + currentRow = [intermediate[index]]; + } + else { + currentRow.push(intermediate[index]); } } - - return true; + result.push({ key: current, rows: this.lwrap(currentRow) }); + return this.lwrap(result); } - - return false; -} - -// encode a blob for indexeddb engines that don't support blobs -function _encodeBlob(blob) { - return new Promise$1(function (resolve, reject) { - var reader = new FileReader(); - reader.onerror = reject; - reader.onloadend = function (e) { - var base64 = btoa(e.target.result || ''); - resolve({ - __local_forage_encoded_blob: true, - data: base64, - type: blob.type + groupIn(key, comparator) { + if (Groupings.isGrouping(this.values)) { + return this.map(v => { + return { + key: v.key, + rows: DataArray.wrap(v.rows, this.settings).groupIn(key, comparator), + }; }); - }; - reader.readAsBinaryString(blob); - }); -} - -// decode an encoded blob -function _decodeBlob(encodedBlob) { - var arrayBuff = _binStringToArrayBuffer(atob(encodedBlob.data)); - return createBlob([arrayBuff], { type: encodedBlob.type }); -} - -// is this one of our fancy encoded blobs? -function _isEncodedBlob(value) { - return value && value.__local_forage_encoded_blob; -} - -// Specialize the default `ready()` function by making it dependent -// on the current database operations. Thus, the driver will be actually -// ready when it's been initialized (default) *and* there are no pending -// operations on the database (initiated by some other instances). -function _fullyReady(callback) { - var self = this; - - var promise = self._initReady().then(function () { - var dbContext = dbContexts[self._dbInfo.name]; - - if (dbContext && dbContext.dbReady) { - return dbContext.dbReady; } - }); - - executeTwoCallbacks(promise, callback, callback); - return promise; -} - -// Try to establish a new db connection to replace the -// current one which is broken (i.e. experiencing -// InvalidStateError while creating a transaction). -function _tryReconnect(dbInfo) { - _deferReadiness(dbInfo); - - var dbContext = dbContexts[dbInfo.name]; - var forages = dbContext.forages; - - for (var i = 0; i < forages.length; i++) { - var forage = forages[i]; - if (forage._dbInfo.db) { - forage._dbInfo.db.close(); - forage._dbInfo.db = null; + else { + return this.groupBy(key, comparator); } } - dbInfo.db = null; - - return _getOriginalConnection(dbInfo).then(function (db) { - dbInfo.db = db; - if (_isUpgradeNeeded(dbInfo)) { - // Reopen the database for upgrading. - return _getUpgradedConnection(dbInfo); + distinct(key, comparator) { + if (this.values.length == 0) + return this; + let realKey = key !== null && key !== void 0 ? key : (x => x); + // For similar reasons to groupBy, do a sort and take the first element of each block. + let intermediate = this.map((x, index) => { + return { key: realKey(x, index, this.values), value: x }; + }).sort(x => x.key, "asc", comparator); + comparator = comparator !== null && comparator !== void 0 ? comparator : this.defaultComparator; + let result = [intermediate[0].value]; + for (let index = 1; index < intermediate.length; index++) { + if (comparator(intermediate[index - 1].key, intermediate[index].key) != 0) { + result.push(intermediate[index].value); + } } - return db; - }).then(function (db) { - // store the latest db reference - // in case the db was upgraded - dbInfo.db = dbContext.db = db; - for (var i = 0; i < forages.length; i++) { - forages[i]._dbInfo.db = db; + return this.lwrap(result); + } + every(f) { + return this.values.every(f); + } + some(f) { + return this.values.some(f); + } + none(f) { + return this.values.every((v, i, a) => !f(v, i, a)); + } + first() { + return this.values.length > 0 ? this.values[0] : undefined; + } + last() { + return this.values.length > 0 ? this.values[this.values.length - 1] : undefined; + } + to(key) { + let result = []; + for (let child of this.values) { + let value = child[key]; + if (value === undefined || value === null) + continue; + if (Array.isArray(value) || DataArray.isDataArray(value)) + value.forEach(v => result.push(v)); + else + result.push(value); } - })["catch"](function (err) { - _rejectReadiness(dbInfo, err); - throw err; - }); -} - -// FF doesn't like Promises (micro-tasks) and IDDB store operations, -// so we have to do it with callbacks -function createTransaction(dbInfo, mode, callback, retries) { - if (retries === undefined) { - retries = 1; + return this.lwrap(result); } - - try { - var tx = dbInfo.db.transaction(dbInfo.storeName, mode); - callback(null, tx); - } catch (err) { - if (retries > 0 && (!dbInfo.db || err.name === 'InvalidStateError' || err.name === 'NotFoundError')) { - return Promise$1.resolve().then(function () { - if (!dbInfo.db || err.name === 'NotFoundError' && !dbInfo.db.objectStoreNames.contains(dbInfo.storeName) && dbInfo.version <= dbInfo.db.version) { - // increase the db version, to create the new ObjectStore - if (dbInfo.db) { - dbInfo.version = dbInfo.db.version + 1; - } - // Reopen the database for upgrading. - return _getUpgradedConnection(dbInfo); - } - }).then(function () { - return _tryReconnect(dbInfo).then(function () { - createTransaction(dbInfo, mode, callback, retries - 1); - }); - })["catch"](callback); + into(key) { + let result = []; + for (let child of this.values) { + let value = child[key]; + if (value === undefined || value === null) + continue; + result.push(value); } - - callback(err); + return this.lwrap(result); + } + expand(key) { + let result = []; + let queue = [].concat(this.values); + while (queue.length > 0) { + let next = queue.pop(); + let value = next[key]; + if (value === undefined || value === null) + continue; + if (Array.isArray(value)) + value.forEach(v => queue.push(v)); + else if (value instanceof DataArrayImpl) + value.forEach(v => queue.push(v)); + else + queue.push(value); + result.push(next); + } + return this.lwrap(result); + } + forEach(f) { + for (let index = 0; index < this.values.length; index++) { + f(this.values[index], index, this.values); + } + } + array() { + return [].concat(this.values); + } + [Symbol.iterator]() { + return this.values[Symbol.iterator](); + } + toString() { + return "[" + this.values.join(", ") + "]"; } } +DataArrayImpl.ARRAY_FUNCTIONS = new Set([ + "where", + "filter", + "map", + "flatMap", + "mutate", + "slice", + "concat", + "indexOf", + "limit", + "find", + "findIndex", + "includes", + "join", + "sort", + "sortInPlace", + "groupBy", + "groupIn", + "distinct", + "every", + "some", + "none", + "first", + "last", + "to", + "into", + "lwrap", + "expand", + "forEach", + "length", + "values", + "array", + "defaultComparator", + "toString", + "settings", +]); +DataArrayImpl.ARRAY_PROXY = { + get: function (target, prop, reciever) { + if (typeof prop === "symbol") + return target[prop]; + else if (typeof prop === "number") + return target.values[prop]; + else if (prop === "constructor") + return target.values.constructor; + else if (!isNaN(parseInt(prop))) + return target.values[parseInt(prop)]; + else if (DataArrayImpl.ARRAY_FUNCTIONS.has(prop.toString())) + return target[prop.toString()]; + return target.to(prop); + }, +}; +/** Provides utility functions for generating data arrays. */ +var DataArray; +(function (DataArray) { + /** Create a new Dataview data array. */ + function wrap(raw, settings) { + if (isDataArray(raw)) + return raw; + return DataArrayImpl.wrap(raw, settings); + } + DataArray.wrap = wrap; + /** Create a new DataArray from an iterable object. */ + function from(raw, settings) { + if (isDataArray(raw)) + return raw; + let data = []; + for (let elem of raw) + data.push(elem); + return DataArrayImpl.wrap(data, settings); + } + DataArray.from = from; + /** Return true if the given object is a data array. */ + function isDataArray(obj) { + return obj instanceof DataArrayImpl; + } + DataArray.isDataArray = isDataArray; +})(DataArray || (DataArray = {})); +// A scary looking polyfill, sure, but it fixes up data array/array interop for us. +const oldArrayIsArray = Array.isArray; +Array.isArray = (arg) => { + return oldArrayIsArray(arg) || DataArray.isDataArray(arg); +}; -function createDbContext() { - return { - // Running localForages sharing a database. - forages: [], - // Shared database. - db: null, - // Database readiness (promise). - dbReady: null, - // Deferred operations on the database. - deferredOperations: [] - }; -} - -// Open the IndexedDB database (automatically creates one if one didn't -// previously exist), using any options set in the config. -function _initStorage(options) { - var self = this; - var dbInfo = { - db: null - }; - - if (options) { - for (var i in options) { - dbInfo[i] = options[i]; +/** Test-environment-friendly function which fetches the current system locale. */ +function currentLocale() { + if (typeof window === "undefined") + return "en-US"; + return window.navigator.language; +} + +/** Render simple fields compactly, removing wrapping content like paragraph and span. */ +async function renderCompactMarkdown(markdown, container, sourcePath, component) { + let subcontainer = container.createSpan(); + await obsidian.MarkdownRenderer.renderMarkdown(markdown, subcontainer, sourcePath, component); + let paragraph = subcontainer.querySelector(":scope > p"); + if (subcontainer.children.length == 1 && paragraph) { + while (paragraph.firstChild) { + subcontainer.appendChild(paragraph.firstChild); } + subcontainer.removeChild(paragraph); } - - // Get the current context of the database; - var dbContext = dbContexts[dbInfo.name]; - - // ...or create a new context. - if (!dbContext) { - dbContext = createDbContext(); - // Register the new context in the global container. - dbContexts[dbInfo.name] = dbContext; +} +/** Render a pre block with an error in it; returns the element to allow for dynamic updating. */ +function renderErrorPre(container, error) { + let pre = container.createEl("pre", { cls: ["dataview", "dataview-error"] }); + pre.appendText(error); + return pre; +} +/** Render a static codeblock. */ +function renderCodeBlock(container, source, language) { + let code = container.createEl("code", { cls: ["dataview"] }); + if (language) + code.classList.add("language-" + language); + code.appendText(source); + return code; +} +/** Prettily render a value into a container with the given settings. */ +async function renderValue(field, container, originFile, component, settings, expandList = false, context = "root", depth = 0) { + var _a, _b, _c; + // Prevent infinite recursion. + if (depth > settings.maxRecursiveRenderDepth) { + container.appendText("..."); + return; } - - // Register itself as a running localForage in the current context. - dbContext.forages.push(self); - - // Replace the default `ready()` function with the specialized one. - if (!self._initReady) { - self._initReady = self.ready; - self.ready = _fullyReady; + if (Values.isNull(field)) { + await renderCompactMarkdown(settings.renderNullAs, container, originFile, component); } - - // Create an array of initialization states of the related localForages. - var initPromises = []; - - function ignoreErrors() { - // Don't handle errors here, - // just makes sure related localForages aren't pending. - return Promise$1.resolve(); + else if (Values.isDate(field)) { + container.appendText(renderMinimalDate(field, settings, currentLocale())); } - - for (var j = 0; j < dbContext.forages.length; j++) { - var forage = dbContext.forages[j]; - if (forage !== self) { - // Don't wait for itself... - initPromises.push(forage._initReady()["catch"](ignoreErrors)); + else if (Values.isDuration(field)) { + container.appendText(renderMinimalDuration(field)); + } + else if (Values.isString(field) || Values.isBoolean(field) || Values.isNumber(field)) { + await renderCompactMarkdown("" + field, container, originFile, component); + } + else if (Values.isLink(field)) { + await renderCompactMarkdown(field.markdown(), container, originFile, component); + } + else if (Values.isHtml(field)) { + container.appendChild(field); + } + else if (Values.isWidget(field)) { + if (Widgets.isListPair(field)) { + await renderValue(field.key, container, originFile, component, settings, expandList, context, depth); + container.appendText(": "); + await renderValue(field.value, container, originFile, component, settings, expandList, context, depth); + } + else if (Widgets.isExternalLink(field)) { + let elem = document.createElement("a"); + elem.textContent = (_a = field.display) !== null && _a !== void 0 ? _a : field.url; + elem.rel = "noopener"; + elem.target = "_blank"; + elem.classList.add("external-link"); + elem.href = field.url; + container.appendChild(elem); + } + else { + container.appendText(``); } } - - // Take a snapshot of the related localForages. - var forages = dbContext.forages.slice(0); - - // Initialize the connection process only when - // all the related localForages aren't pending. - return Promise$1.all(initPromises).then(function () { - dbInfo.db = dbContext.db; - // Get the connection or open a new one without upgrade. - return _getOriginalConnection(dbInfo); - }).then(function (db) { - dbInfo.db = db; - if (_isUpgradeNeeded(dbInfo, self._defaultConfig.version)) { - // Reopen the database for upgrading. - return _getUpgradedConnection(dbInfo); + else if (Values.isFunction(field)) { + container.appendText(""); + } + else if (Values.isArray(field) || DataArray.isDataArray(field)) { + if (expandList) { + let list = container.createEl("ul", { + cls: [ + "dataview", + "dataview-ul", + context == "list" ? "dataview-result-list-ul" : "dataview-result-list-root-ul", + ], + }); + for (let child of field) { + let li = list.createEl("li", { cls: "dataview-result-list-li" }); + await renderValue(child, li, originFile, component, settings, expandList, "list", depth + 1); + } + } + else { + if (field.length == 0) { + container.appendText(""); + return; + } + let span = container.createEl("span", { cls: ["dataview", "dataview-result-list-span"] }); + let first = true; + for (let val of field) { + if (first) + first = false; + else + span.appendText(", "); + await renderValue(val, span, originFile, component, settings, expandList, "list", depth + 1); + } + } + } + else if (Values.isObject(field)) { + // Don't render classes in case they have recursive references; spoopy. + if (((_b = field === null || field === void 0 ? void 0 : field.constructor) === null || _b === void 0 ? void 0 : _b.name) && ((_c = field === null || field === void 0 ? void 0 : field.constructor) === null || _c === void 0 ? void 0 : _c.name) != "Object") { + container.appendText(`<${field.constructor.name}>`); + return; } - return db; - }).then(function (db) { - dbInfo.db = dbContext.db = db; - self._dbInfo = dbInfo; - // Share the final connection amongst related localForages. - for (var k = 0; k < forages.length; k++) { - var forage = forages[k]; - if (forage !== self) { - // Self is already up-to-date. - forage._dbInfo.db = dbInfo.db; - forage._dbInfo.version = dbInfo.version; + if (expandList) { + let list = container.createEl("ul", { cls: ["dataview", "dataview-ul", "dataview-result-object-ul"] }); + for (let [key, value] of Object.entries(field)) { + let li = list.createEl("li", { cls: ["dataview", "dataview-li", "dataview-result-object-li"] }); + li.appendText(key + ": "); + await renderValue(value, li, originFile, component, settings, expandList, "list", depth + 1); } } - }); + else { + if (Object.keys(field).length == 0) { + container.appendText(""); + return; + } + let span = container.createEl("span", { cls: ["dataview", "dataview-result-object-span"] }); + let first = true; + for (let [key, value] of Object.entries(field)) { + if (first) + first = false; + else + span.appendText(", "); + span.appendText(key + ": "); + await renderValue(value, span, originFile, component, settings, expandList, "list", depth + 1); + } + } + } + else { + container.appendText("Unrecognized: " + JSON.stringify(field)); + } } -function getItem(key, callback) { - var self = this; +var papaparse_min = {exports: {}}; - key = normalizeKey(key); +/* @license +Papa Parse +v5.4.1 +https://github.com/mholt/PapaParse +License: MIT +*/ +papaparse_min.exports; - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { - if (err) { - return reject(err); - } +(function (module, exports) { + !function(e,t){module.exports=t();}(commonjsGlobal,function s(){var f="undefined"!=typeof self?self:"undefined"!=typeof window?window:void 0!==f?f:{};var n=!f.document&&!!f.postMessage,o=f.IS_PAPA_WORKER||!1,a={},u=0,b={parse:function(e,t){var r=(t=t||{}).dynamicTyping||!1;J(r)&&(t.dynamicTypingFunction=r,r={});if(t.dynamicTyping=r,t.transform=!!J(t.transform)&&t.transform,t.worker&&b.WORKERS_SUPPORTED){var i=function(){if(!b.WORKERS_SUPPORTED)return !1;var e=(r=f.URL||f.webkitURL||null,i=s.toString(),b.BLOB_URL||(b.BLOB_URL=r.createObjectURL(new Blob(["var global = (function() { if (typeof self !== 'undefined') { return self; } if (typeof window !== 'undefined') { return window; } if (typeof global !== 'undefined') { return global; } return {}; })(); global.IS_PAPA_WORKER=true; ","(",i,")();"],{type:"text/javascript"})))),t=new f.Worker(e);var r,i;return t.onmessage=_,t.id=u++,a[t.id]=t}();return i.userStep=t.step,i.userChunk=t.chunk,i.userComplete=t.complete,i.userError=t.error,t.step=J(t.step),t.chunk=J(t.chunk),t.complete=J(t.complete),t.error=J(t.error),delete t.worker,void i.postMessage({input:e,config:t,workerId:i.id})}var n=null;b.NODE_STREAM_INPUT,"string"==typeof e?(e=function(e){if(65279===e.charCodeAt(0))return e.slice(1);return e}(e),n=t.download?new l(t):new p(t)):!0===e.readable&&J(e.read)&&J(e.on)?n=new g(t):(f.File&&e instanceof File||e instanceof Object)&&(n=new c(t));return n.stream(e)},unparse:function(e,t){var n=!1,_=!0,m=",",y="\r\n",s='"',a=s+s,r=!1,i=null,o=!1;!function(){if("object"!=typeof t)return;"string"!=typeof t.delimiter||b.BAD_DELIMITERS.filter(function(e){return -1!==t.delimiter.indexOf(e)}).length||(m=t.delimiter);("boolean"==typeof t.quotes||"function"==typeof t.quotes||Array.isArray(t.quotes))&&(n=t.quotes);"boolean"!=typeof t.skipEmptyLines&&"string"!=typeof t.skipEmptyLines||(r=t.skipEmptyLines);"string"==typeof t.newline&&(y=t.newline);"string"==typeof t.quoteChar&&(s=t.quoteChar);"boolean"==typeof t.header&&(_=t.header);if(Array.isArray(t.columns)){if(0===t.columns.length)throw new Error("Option columns is empty");i=t.columns;}void 0!==t.escapeChar&&(a=t.escapeChar+s);("boolean"==typeof t.escapeFormulae||t.escapeFormulae instanceof RegExp)&&(o=t.escapeFormulae instanceof RegExp?t.escapeFormulae:/^[=+\-@\t\r].*$/);}();var u=new RegExp(Q(s),"g");"string"==typeof e&&(e=JSON.parse(e));if(Array.isArray(e)){if(!e.length||Array.isArray(e[0]))return h(null,e,r);if("object"==typeof e[0])return h(i||Object.keys(e[0]),e,r)}else if("object"==typeof e)return "string"==typeof e.data&&(e.data=JSON.parse(e.data)),Array.isArray(e.data)&&(e.fields||(e.fields=e.meta&&e.meta.fields||i),e.fields||(e.fields=Array.isArray(e.data[0])?e.fields:"object"==typeof e.data[0]?Object.keys(e.data[0]):[]),Array.isArray(e.data[0])||"object"==typeof e.data[0]||(e.data=[e.data])),h(e.fields||[],e.data||[],r);throw new Error("Unable to serialize unrecognized input");function h(e,t,r){var i="";"string"==typeof e&&(e=JSON.parse(e)),"string"==typeof t&&(t=JSON.parse(t));var n=Array.isArray(e)&&0=this._config.preview;if(o)f.postMessage({results:n,workerId:b.WORKER_ID,finished:a});else if(J(this._config.chunk)&&!t){if(this._config.chunk(n,this._handle),this._handle.paused()||this._handle.aborted())return void(this._halted=!0);n=void 0,this._completeResults=void 0;}return this._config.step||this._config.chunk||(this._completeResults.data=this._completeResults.data.concat(n.data),this._completeResults.errors=this._completeResults.errors.concat(n.errors),this._completeResults.meta=n.meta),this._completed||!a||!J(this._config.complete)||n&&n.meta.aborted||(this._config.complete(this._completeResults,this._input),this._completed=!0),a||n&&n.meta.paused||this._nextChunk(),n}this._halted=!0;},this._sendError=function(e){J(this._config.error)?this._config.error(e):o&&this._config.error&&f.postMessage({workerId:b.WORKER_ID,error:e,finished:!1});};}function l(e){var i;(e=e||{}).chunkSize||(e.chunkSize=b.RemoteChunkSize),h.call(this,e),this._nextChunk=n?function(){this._readChunk(),this._chunkLoaded();}:function(){this._readChunk();},this.stream=function(e){this._input=e,this._nextChunk();},this._readChunk=function(){if(this._finished)this._chunkLoaded();else {if(i=new XMLHttpRequest,this._config.withCredentials&&(i.withCredentials=this._config.withCredentials),n||(i.onload=v(this._chunkLoaded,this),i.onerror=v(this._chunkError,this)),i.open(this._config.downloadRequestBody?"POST":"GET",this._input,!n),this._config.downloadRequestHeaders){var e=this._config.downloadRequestHeaders;for(var t in e)i.setRequestHeader(t,e[t]);}if(this._config.chunkSize){var r=this._start+this._config.chunkSize-1;i.setRequestHeader("Range","bytes="+this._start+"-"+r);}try{i.send(this._config.downloadRequestBody);}catch(e){this._chunkError(e.message);}n&&0===i.status&&this._chunkError();}},this._chunkLoaded=function(){4===i.readyState&&(i.status<200||400<=i.status?this._chunkError():(this._start+=this._config.chunkSize?this._config.chunkSize:i.responseText.length,this._finished=!this._config.chunkSize||this._start>=function(e){var t=e.getResponseHeader("Content-Range");if(null===t)return -1;return parseInt(t.substring(t.lastIndexOf("/")+1))}(i),this.parseChunk(i.responseText)));},this._chunkError=function(e){var t=i.statusText||e;this._sendError(new Error(t));};}function c(e){var i,n;(e=e||{}).chunkSize||(e.chunkSize=b.LocalChunkSize),h.call(this,e);var s="undefined"!=typeof FileReader;this.stream=function(e){this._input=e,n=e.slice||e.webkitSlice||e.mozSlice,s?((i=new FileReader).onload=v(this._chunkLoaded,this),i.onerror=v(this._chunkError,this)):i=new FileReaderSync,this._nextChunk();},this._nextChunk=function(){this._finished||this._config.preview&&!(this._rowCount=this._input.size,this.parseChunk(e.target.result);},this._chunkError=function(){this._sendError(i.error);};}function p(e){var r;h.call(this,e=e||{}),this.stream=function(e){return r=e,this._nextChunk()},this._nextChunk=function(){if(!this._finished){var e,t=this._config.chunkSize;return t?(e=r.substring(0,t),r=r.substring(t)):(e=r,r=""),this._finished=!r,this.parseChunk(e)}};}function g(e){h.call(this,e=e||{});var t=[],r=!0,i=!1;this.pause=function(){h.prototype.pause.apply(this,arguments),this._input.pause();},this.resume=function(){h.prototype.resume.apply(this,arguments),this._input.resume();},this.stream=function(e){this._input=e,this._input.on("data",this._streamData),this._input.on("end",this._streamEnd),this._input.on("error",this._streamError);},this._checkIsFinished=function(){i&&1===t.length&&(this._finished=!0);},this._nextChunk=function(){this._checkIsFinished(),t.length?this.parseChunk(t.shift()):r=!0;},this._streamData=v(function(e){try{t.push("string"==typeof e?e:e.toString(this._config.encoding)),r&&(r=!1,this._checkIsFinished(),this.parseChunk(t.shift()));}catch(e){this._streamError(e);}},this),this._streamError=v(function(e){this._streamCleanUp(),this._sendError(e);},this),this._streamEnd=v(function(){this._streamCleanUp(),i=!0,this._streamData("");},this),this._streamCleanUp=v(function(){this._input.removeListener("data",this._streamData),this._input.removeListener("end",this._streamEnd),this._input.removeListener("error",this._streamError);},this);}function r(m){var a,o,u,i=Math.pow(2,53),n=-i,s=/^\s*-?(\d+\.?|\.\d+|\d+\.\d+)([eE][-+]?\d+)?\s*$/,h=/^((\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)))$/,t=this,r=0,f=0,d=!1,e=!1,l=[],c={data:[],errors:[],meta:{}};if(J(m.step)){var p=m.step;m.step=function(e){if(c=e,_())g();else {if(g(),0===c.data.length)return;r+=e.data.length,m.preview&&r>m.preview?o.abort():(c.data=c.data[0],p(c,t));}};}function y(e){return "greedy"===m.skipEmptyLines?""===e.join("").trim():1===e.length&&0===e[0].length}function g(){return c&&u&&(k("Delimiter","UndetectableDelimiter","Unable to auto-detect delimiting character; defaulted to '"+b.DefaultDelimiter+"'"),u=!1),m.skipEmptyLines&&(c.data=c.data.filter(function(e){return !y(e)})),_()&&function(){if(!c)return;function e(e,t){J(m.transformHeader)&&(e=m.transformHeader(e,t)),l.push(e);}if(Array.isArray(c.data[0])){for(var t=0;_()&&t=l.length?"__parsed_extra":l[r]),m.transform&&(s=m.transform(s,n)),s=v(n,s),"__parsed_extra"===n?(i[n]=i[n]||[],i[n].push(s)):i[n]=s;}return m.header&&(r>l.length?k("FieldMismatch","TooManyFields","Too many fields: expected "+l.length+" fields but parsed "+r,f+t):r=i.length/2?"\r\n":"\r"}(e,i)),u=!1,m.delimiter)J(m.delimiter)&&(m.delimiter=m.delimiter(e),c.meta.delimiter=m.delimiter);else {var n=function(e,t,r,i,n){var s,a,o,u;n=n||[",","\t","|",";",b.RECORD_SEP,b.UNIT_SEP];for(var h=0;h=N)return L(!0)}else for(S=W,W++;;){if(-1===(S=i.indexOf(z,S+1)))return r||h.push({type:"Quotes",code:"MissingQuotes",message:"Quoted field unterminated",row:u.length,index:W}),T();if(S===n-1)return T(i.substring(W,S).replace(C,z));if(z!==K||i[S+1]!==K){if(z===K||0===S||i[S-1]!==K){-1!==w&&w=N)return L(!0);break}h.push({type:"Quotes",code:"InvalidQuotes",message:"Trailing quote on quoted field is malformed",row:u.length,index:W}),S++;}}else S++;}return T();function I(e){u.push(e),d=W;}function A(e){var t=0;if(-1!==e){var r=i.substring(S+1,e);r&&""===r.trim()&&(t=r.length);}return t}function T(e){return r||(void 0===e&&(e=i.substring(W)),f.push(e),W=n,I(f),o&&F()),L()}function D(e){W=e,I(f),f=[],R=i.indexOf(P,W);}function L(e){return {data:u,errors:h,meta:{delimiter:M,linebreak:P,aborted:H,truncated:!!e,cursor:d+(t||0)}}}function F(){q(L()),u=[],h=[];}},this.abort=function(){H=!0;},this.getCharIndex=function(){return W};}function _(e){var t=e.data,r=a[t.workerId],i=!1;if(t.error)r.userError(t.error,t.file);else if(t.results&&t.results.data){var n={abort:function(){i=!0,m(t.workerId,{data:[],errors:[],meta:{aborted:!0}});},pause:y,resume:y};if(J(r.userStep)){for(var s=0;s" || op == ">=" || op == "!=" || op == "="; + } + Fields.isCompareOp = isCompareOp; + Fields.NULL = Fields.literal(null); +})(Fields || (Fields = {})); - req.onerror = function () { - reject(req.error); - }; - } catch (e) { - reject(e); - } - }); - })["catch"](reject); +/** AST implementation for queries over data sources. */ +/** Utility functions for creating and manipulating sources. */ +var Sources; +(function (Sources) { + /** Create a source which searches from a tag. */ + function tag(tag) { + return { type: "tag", tag }; + } + Sources.tag = tag; + /** Create a source which fetches from a CSV file. */ + function csv(path) { + return { type: "csv", path }; + } + Sources.csv = csv; + /** Create a source which searches for files under a folder prefix. */ + function folder(prefix) { + return { type: "folder", folder: prefix }; + } + Sources.folder = folder; + /** Create a source which searches for files which link to/from a given file. */ + function link(file, incoming) { + return { type: "link", file, direction: incoming ? "incoming" : "outgoing" }; + } + Sources.link = link; + /** Create a source which joins two sources by a logical operator (and/or). */ + function binaryOp(left, op, right) { + return { type: "binaryop", left, op, right }; + } + Sources.binaryOp = binaryOp; + /** Create a source which takes the intersection of two sources. */ + function and(left, right) { + return { type: "binaryop", left, op: "&", right }; + } + Sources.and = and; + /** Create a source which takes the union of two sources. */ + function or(left, right) { + return { type: "binaryop", left, op: "|", right }; + } + Sources.or = or; + /** Create a source which negates the underlying source. */ + function negate(child) { + return { type: "negate", child }; + } + Sources.negate = negate; + function empty() { + return { type: "empty" }; + } + Sources.empty = empty; +})(Sources || (Sources = {})); + +/** Emoji regex without any additional flags. */ +const EMOJI_REGEX = new RegExp(emojiRegex(), ""); +/** Provides a lookup table for unit durations of the given type. */ +const DURATION_TYPES = { + year: Duration.fromObject({ years: 1 }), + years: Duration.fromObject({ years: 1 }), + yr: Duration.fromObject({ years: 1 }), + yrs: Duration.fromObject({ years: 1 }), + month: Duration.fromObject({ months: 1 }), + months: Duration.fromObject({ months: 1 }), + mo: Duration.fromObject({ months: 1 }), + mos: Duration.fromObject({ months: 1 }), + week: Duration.fromObject({ weeks: 1 }), + weeks: Duration.fromObject({ weeks: 1 }), + wk: Duration.fromObject({ weeks: 1 }), + wks: Duration.fromObject({ weeks: 1 }), + w: Duration.fromObject({ weeks: 1 }), + day: Duration.fromObject({ days: 1 }), + days: Duration.fromObject({ days: 1 }), + d: Duration.fromObject({ days: 1 }), + hour: Duration.fromObject({ hours: 1 }), + hours: Duration.fromObject({ hours: 1 }), + hr: Duration.fromObject({ hours: 1 }), + hrs: Duration.fromObject({ hours: 1 }), + h: Duration.fromObject({ hours: 1 }), + minute: Duration.fromObject({ minutes: 1 }), + minutes: Duration.fromObject({ minutes: 1 }), + min: Duration.fromObject({ minutes: 1 }), + mins: Duration.fromObject({ minutes: 1 }), + m: Duration.fromObject({ minutes: 1 }), + second: Duration.fromObject({ seconds: 1 }), + seconds: Duration.fromObject({ seconds: 1 }), + sec: Duration.fromObject({ seconds: 1 }), + secs: Duration.fromObject({ seconds: 1 }), + s: Duration.fromObject({ seconds: 1 }), +}; +/** Shorthand for common dates (relative to right now). */ +const DATE_SHORTHANDS = { + now: () => DateTime.local(), + today: () => DateTime.local().startOf("day"), + yesterday: () => DateTime.local() + .startOf("day") + .minus(Duration.fromObject({ days: 1 })), + tomorrow: () => DateTime.local() + .startOf("day") + .plus(Duration.fromObject({ days: 1 })), + sow: () => DateTime.local().startOf("week"), + "start-of-week": () => DateTime.local().startOf("week"), + eow: () => DateTime.local().endOf("week"), + "end-of-week": () => DateTime.local().endOf("week"), + soy: () => DateTime.local().startOf("year"), + "start-of-year": () => DateTime.local().startOf("year"), + eoy: () => DateTime.local().endOf("year"), + "end-of-year": () => DateTime.local().endOf("year"), + som: () => DateTime.local().startOf("month"), + "start-of-month": () => DateTime.local().startOf("month"), + eom: () => DateTime.local().endOf("month"), + "end-of-month": () => DateTime.local().endOf("month"), +}; +/** + * Keywords which cannot be used as variables directly. Use `row.` if it is a variable you have defined and want + * to access. + */ +const KEYWORDS = ["FROM", "WHERE", "LIMIT", "GROUP", "FLATTEN"]; +/////////////// +// Utilities // +/////////////// +/** Split on unescaped pipes in an inner link. */ +function splitOnUnescapedPipe(link) { + let pipe = -1; + while ((pipe = link.indexOf("|", pipe + 1)) >= 0) { + if (pipe > 0 && link[pipe - 1] == "\\") + continue; + return [link.substring(0, pipe).replace(/\\\|/g, "|"), link.substring(pipe + 1)]; + } + return [link.replace(/\\\|/g, "|"), undefined]; +} +/** Attempt to parse the inside of a link to pull out display name, subpath, etc. */ +function parseInnerLink(rawlink) { + let [link, display] = splitOnUnescapedPipe(rawlink); + return Link.infer(link, false, display); +} +/** Create a left-associative binary parser which parses the given sub-element and separator. Handles whitespace. */ +function createBinaryParser(child, sep, combine) { + return parsimmon_umd_minExports.seqMap(child, parsimmon_umd_minExports.seq(parsimmon_umd_minExports.optWhitespace, sep, parsimmon_umd_minExports.optWhitespace, child).many(), (first, rest) => { + if (rest.length == 0) + return first; + let node = combine(first, rest[0][1], rest[0][3]); + for (let index = 1; index < rest.length; index++) { + node = combine(node, rest[index][1], rest[index][3]); + } + return node; + }); +} +function chainOpt(base, ...funcs) { + return parsimmon_umd_minExports.custom((success, failure) => { + return (input, i) => { + let result = base._(input, i); + if (!result.status) + return result; + for (let func of funcs) { + let next = func(result.value)._(input, result.index); + if (!next.status) + return result; + result = next; + } + return result; + }; }); +} +const EXPRESSION = parsimmon_umd_minExports.createLanguage({ + // A floating point number; the decimal point is optional. + number: q => parsimmon_umd_minExports.regexp(/-?[0-9]+(\.[0-9]+)?/) + .map(str => Number.parseFloat(str)) + .desc("number"), + // A quote-surrounded string which supports escape characters ('\'). + string: q => parsimmon_umd_minExports.string('"') + .then(parsimmon_umd_minExports.alt(q.escapeCharacter, parsimmon_umd_minExports.noneOf('"\\')) + .atLeast(0) + .map(chars => chars.join(""))) + .skip(parsimmon_umd_minExports.string('"')) + .desc("string"), + escapeCharacter: _ => parsimmon_umd_minExports.string("\\") + .then(parsimmon_umd_minExports.any) + .map(escaped => { + // If we are escaping a backslash or a quote, pass in on in escaped form + if (escaped === '"') + return '"'; + if (escaped === "\\") + return "\\"; + else + return "\\" + escaped; + }), + // A boolean true/false value. + bool: _ => parsimmon_umd_minExports.regexp(/true|false|True|False/) + .map(str => str.toLowerCase() == "true") + .desc("boolean ('true' or 'false')"), + // A tag of the form '#stuff/hello-there'. + tag: _ => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("#"), parsimmon_umd_minExports.alt(parsimmon_umd_minExports.regexp(/[^\u2000-\u206F\u2E00-\u2E7F'!"#$%&()*+,.:;<=>?@^`{|}~\[\]\\\s]/).desc("text")).many(), (start, rest) => start + rest.join("")).desc("tag ('#hello/stuff')"), + // A variable identifier, which is alphanumeric and must start with a letter or... emoji. + identifier: _ => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.alt(parsimmon_umd_minExports.regexp(/\p{Letter}/u), parsimmon_umd_minExports.regexp(EMOJI_REGEX).desc("text")), parsimmon_umd_minExports.alt(parsimmon_umd_minExports.regexp(/[0-9\p{Letter}_-]/u), parsimmon_umd_minExports.regexp(EMOJI_REGEX).desc("text")).many(), (first, rest) => first + rest.join("")).desc("variable identifier"), + // An Obsidian link of the form [[]]. + link: _ => parsimmon_umd_minExports.regexp(/\[\[([^\[\]]*?)\]\]/u, 1) + .map(linkInner => parseInnerLink(linkInner)) + .desc("file link"), + // An embeddable link which can start with '!'. This overlaps with the normal negation operator, so it is only + // provided for metadata parsing. + embedLink: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("!").atMost(1), q.link, (p, l) => { + if (p.length > 0) + l.embed = true; + return l; + }).desc("file link"), + // Binary plus or minus operator. + binaryPlusMinus: _ => parsimmon_umd_minExports.regexp(/\+|-/) + .map(str => str) + .desc("'+' or '-'"), + // Binary times or divide operator. + binaryMulDiv: _ => parsimmon_umd_minExports.regexp(/\*|\/|%/) + .map(str => str) + .desc("'*' or '/' or '%'"), + // Binary comparison operator. + binaryCompareOp: _ => parsimmon_umd_minExports.regexp(/>=|<=|!=|>|<|=/) + .map(str => str) + .desc("'>=' or '<=' or '!=' or '=' or '>' or '<'"), + // Binary boolean combination operator. + binaryBooleanOp: _ => parsimmon_umd_minExports.regexp(/and|or|&|\|/i) + .map(str => { + if (str.toLowerCase() == "and") + return "&"; + else if (str.toLowerCase() == "or") + return "|"; + else + return str; + }) + .desc("'and' or 'or'"), + // A date which can be YYYY-MM[-DDTHH:mm:ss]. + rootDate: _ => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/\d{4}/), parsimmon_umd_minExports.string("-"), parsimmon_umd_minExports.regexp(/\d{2}/), (year, _, month) => { + return DateTime.fromObject({ year: Number.parseInt(year), month: Number.parseInt(month) }); + }).desc("date in format YYYY-MM[-DDTHH-MM-SS.MS]"), + dateShorthand: _ => parsimmon_umd_minExports.alt(...Object.keys(DATE_SHORTHANDS) + .sort((a, b) => b.length - a.length) + .map(parsimmon_umd_minExports.string)), + date: q => chainOpt(q.rootDate, (ym) => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("-"), parsimmon_umd_minExports.regexp(/\d{2}/), (_, day) => ym.set({ day: Number.parseInt(day) })), (ymd) => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("T"), parsimmon_umd_minExports.regexp(/\d{2}/), (_, hour) => ymd.set({ hour: Number.parseInt(hour) })), (ymdh) => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string(":"), parsimmon_umd_minExports.regexp(/\d{2}/), (_, minute) => ymdh.set({ minute: Number.parseInt(minute) })), (ymdhm) => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string(":"), parsimmon_umd_minExports.regexp(/\d{2}/), (_, second) => ymdhm.set({ second: Number.parseInt(second) })), (ymdhms) => parsimmon_umd_minExports.alt(parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("."), parsimmon_umd_minExports.regexp(/\d{3}/), (_, millisecond) => ymdhms.set({ millisecond: Number.parseInt(millisecond) })), parsimmon_umd_minExports.succeed(ymdhms) // pass + ), (dt) => parsimmon_umd_minExports.alt(parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("+").or(parsimmon_umd_minExports.string("-")), parsimmon_umd_minExports.regexp(/\d{1,2}(:\d{2})?/), (pm, hr) => dt.setZone("UTC" + pm + hr, { keepLocalTime: true })), parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("Z"), () => dt.setZone("utc", { keepLocalTime: true })), parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("["), parsimmon_umd_minExports.regexp(/[0-9A-Za-z+-\/]+/u), parsimmon_umd_minExports.string("]"), (_a, zone, _b) => dt.setZone(zone, { keepLocalTime: true })))) + .assert((dt) => dt.isValid, "valid date") + .desc("date in format YYYY-MM[-DDTHH-MM-SS.MS]"), + // A date, plus various shorthand times of day it could be. + datePlus: q => parsimmon_umd_minExports.alt(q.dateShorthand.map(d => DATE_SHORTHANDS[d]()), q.date).desc("date in format YYYY-MM[-DDTHH-MM-SS.MS] or in shorthand"), + // A duration of time. + durationType: _ => parsimmon_umd_minExports.alt(...Object.keys(DURATION_TYPES) + .sort((a, b) => b.length - a.length) + .map(parsimmon_umd_minExports.string)), + duration: q => parsimmon_umd_minExports.seqMap(q.number, parsimmon_umd_minExports.optWhitespace, q.durationType, (count, _, t) => DURATION_TYPES[t].mapUnits(x => x * count)) + .sepBy1(parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace).or(parsimmon_umd_minExports.optWhitespace)) + .map(durations => durations.reduce((p, c) => p.plus(c))) + .desc("duration like 4hr2min"), + // A raw null value. + rawNull: _ => parsimmon_umd_minExports.string("null"), + // Source parsing. + tagSource: q => q.tag.map(tag => Sources.tag(tag)), + csvSource: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("csv(").skip(parsimmon_umd_minExports.optWhitespace), q.string, parsimmon_umd_minExports.string(")"), (_1, path, _2) => Sources.csv(path)), + linkIncomingSource: q => q.link.map(link => Sources.link(link.path, true)), + linkOutgoingSource: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("outgoing(").skip(parsimmon_umd_minExports.optWhitespace), q.link, parsimmon_umd_minExports.string(")"), (_1, link, _2) => Sources.link(link.path, false)), + folderSource: q => q.string.map(str => Sources.folder(str)), + parensSource: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("("), parsimmon_umd_minExports.optWhitespace, q.source, parsimmon_umd_minExports.optWhitespace, parsimmon_umd_minExports.string(")"), (_1, _2, field, _3, _4) => field), + negateSource: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.alt(parsimmon_umd_minExports.string("-"), parsimmon_umd_minExports.string("!")), q.atomSource, (_, source) => Sources.negate(source)), + atomSource: q => parsimmon_umd_minExports.alt(q.parensSource, q.negateSource, q.linkOutgoingSource, q.linkIncomingSource, q.folderSource, q.tagSource, q.csvSource), + binaryOpSource: q => createBinaryParser(q.atomSource, q.binaryBooleanOp.map(s => s), Sources.binaryOp), + source: q => q.binaryOpSource, + // Field parsing. + variableField: q => q.identifier + .chain(r => { + if (KEYWORDS.includes(r.toUpperCase())) { + return parsimmon_umd_minExports.fail("Variable fields cannot be a keyword (" + KEYWORDS.join(" or ") + ")"); + } + else { + return parsimmon_umd_minExports.succeed(Fields.variable(r)); + } + }) + .desc("variable"), + numberField: q => q.number.map(val => Fields.literal(val)).desc("number"), + stringField: q => q.string.map(val => Fields.literal(val)).desc("string"), + boolField: q => q.bool.map(val => Fields.literal(val)).desc("boolean"), + dateField: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("date("), parsimmon_umd_minExports.optWhitespace, q.datePlus, parsimmon_umd_minExports.optWhitespace, parsimmon_umd_minExports.string(")"), (prefix, _1, date, _2, postfix) => Fields.literal(date)).desc("date"), + durationField: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("dur("), parsimmon_umd_minExports.optWhitespace, q.duration, parsimmon_umd_minExports.optWhitespace, parsimmon_umd_minExports.string(")"), (prefix, _1, dur, _2, postfix) => Fields.literal(dur)).desc("duration"), + nullField: q => q.rawNull.map(_ => Fields.NULL), + linkField: q => q.link.map(f => Fields.literal(f)), + listField: q => q.field + .sepBy(parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace)) + .wrap(parsimmon_umd_minExports.string("[").skip(parsimmon_umd_minExports.optWhitespace), parsimmon_umd_minExports.optWhitespace.then(parsimmon_umd_minExports.string("]"))) + .map(l => Fields.list(l)) + .desc("list ('[1, 2, 3]')"), + objectField: q => parsimmon_umd_minExports.seqMap(q.identifier.or(q.string), parsimmon_umd_minExports.string(":").trim(parsimmon_umd_minExports.optWhitespace), q.field, (name, _sep, value) => { + return { name, value }; + }) + .sepBy(parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace)) + .wrap(parsimmon_umd_minExports.string("{").skip(parsimmon_umd_minExports.optWhitespace), parsimmon_umd_minExports.optWhitespace.then(parsimmon_umd_minExports.string("}"))) + .map(vals => { + let res = {}; + for (let entry of vals) + res[entry.name] = entry.value; + return Fields.object(res); + }) + .desc("object ('{ a: 1, b: 2 }')"), + atomInlineField: q => parsimmon_umd_minExports.alt(q.date, q.duration.map(d => normalizeDuration(d)), q.string, q.tag, q.embedLink, q.bool, q.number, q.rawNull), + inlineFieldList: q => q.atomInlineField.sepBy(parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace).lookahead(q.atomInlineField)), + inlineField: q => parsimmon_umd_minExports.alt(parsimmon_umd_minExports.seqMap(q.atomInlineField, parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace), q.inlineFieldList, (f, _s, l) => [f].concat(l)), q.atomInlineField), + atomField: q => parsimmon_umd_minExports.alt( + // Place embed links above negated fields as they are the special parser case '![[thing]]' and are generally unambigious. + q.embedLink.map(l => Fields.literal(l)), q.negatedField, q.linkField, q.listField, q.objectField, q.lambdaField, q.parensField, q.boolField, q.numberField, q.stringField, q.dateField, q.durationField, q.nullField, q.variableField), + indexField: q => parsimmon_umd_minExports.seqMap(q.atomField, parsimmon_umd_minExports.alt(q.dotPostfix, q.indexPostfix, q.functionPostfix).many(), (obj, postfixes) => { + let result = obj; + for (let post of postfixes) { + switch (post.type) { + case "dot": + result = Fields.index(result, Fields.literal(post.field)); + break; + case "index": + result = Fields.index(result, post.field); + break; + case "function": + result = Fields.func(result, post.fields); + break; + } + } + return result; + }), + negatedField: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("!"), q.indexField, (_, field) => Fields.negate(field)).desc("negated field"), + parensField: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("("), parsimmon_umd_minExports.optWhitespace, q.field, parsimmon_umd_minExports.optWhitespace, parsimmon_umd_minExports.string(")"), (_1, _2, field, _3, _4) => field), + lambdaField: q => parsimmon_umd_minExports.seqMap(q.identifier + .sepBy(parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace)) + .wrap(parsimmon_umd_minExports.string("(").trim(parsimmon_umd_minExports.optWhitespace), parsimmon_umd_minExports.string(")").trim(parsimmon_umd_minExports.optWhitespace)), parsimmon_umd_minExports.string("=>").trim(parsimmon_umd_minExports.optWhitespace), q.field, (ident, _ignore, value) => { + return { type: "lambda", arguments: ident, value }; + }), + dotPostfix: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("."), q.identifier, (_, field) => { + return { type: "dot", field: field }; + }), + indexPostfix: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("["), parsimmon_umd_minExports.optWhitespace, q.field, parsimmon_umd_minExports.optWhitespace, parsimmon_umd_minExports.string("]"), (_, _2, field, _3, _4) => { + return { type: "index", field }; + }), + functionPostfix: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.string("("), parsimmon_umd_minExports.optWhitespace, q.field.sepBy(parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace)), parsimmon_umd_minExports.optWhitespace, parsimmon_umd_minExports.string(")"), (_, _1, fields, _2, _3) => { + return { type: "function", fields }; + }), + // The precedence hierarchy of operators - multiply/divide, add/subtract, compare, and then boolean operations. + binaryMulDivField: q => createBinaryParser(q.indexField, q.binaryMulDiv, Fields.binaryOp), + binaryPlusMinusField: q => createBinaryParser(q.binaryMulDivField, q.binaryPlusMinus, Fields.binaryOp), + binaryCompareField: q => createBinaryParser(q.binaryPlusMinusField, q.binaryCompareOp, Fields.binaryOp), + binaryBooleanField: q => createBinaryParser(q.binaryCompareField, q.binaryBooleanOp, Fields.binaryOp), + binaryOpField: q => q.binaryBooleanField, + field: q => q.binaryOpField, +}); +/** + * Attempt to parse a field from the given text, returning a string error if the + * parse failed. + */ +function parseField(text) { + try { + return Result.success(EXPRESSION.field.tryParse(text)); + } + catch (error) { + return Result.failure("" + error); + } +} + +/** Parse inline fields and other embedded metadata in a line. */ +/** The wrapper characters that can be used to define an inline field. */ +const INLINE_FIELD_WRAPPERS = Object.freeze({ + "[": "]", + "(": ")", +}); +/** + * Find a matching closing bracket that occurs at or after `start`, respecting nesting and escapes. If found, + * returns the value contained within and the string index after the end of the value. + */ +function findClosing(line, start, open, close) { + let nesting = 0; + let escaped = false; + for (let index = start; index < line.length; index++) { + let char = line.charAt(index); + // Allows for double escapes like '\\' to be rendered normally. + if (char == "\\") { + escaped = !escaped; + continue; + } + // If escaped, ignore the next character for computing nesting, regardless of what it is. + if (escaped) { + escaped = false; + continue; + } + if (char == open) + nesting++; + else if (char == close) + nesting--; + // Only occurs if we are on a close character and trhere is no more nesting. + if (nesting < 0) + return { value: line.substring(start, index).trim(), endIndex: index + 1 }; + escaped = false; + } + return undefined; +} +/** Find the '::' separator in an inline field. */ +function findSeparator(line, start) { + let sep = line.indexOf("::", start); + if (sep < 0) + return undefined; + return { key: line.substring(start, sep).trim(), valueIndex: sep + 2 }; +} +/** Try to completely parse an inline field starting at the given position. Assuems `start` is on a wrapping character. */ +function findSpecificInlineField(line, start) { + let open = line.charAt(start); + let key = findSeparator(line, start + 1); + if (key === undefined) + return undefined; + // Fail the match if we find any separator characters (not allowed in keys). + for (let sep of Object.keys(INLINE_FIELD_WRAPPERS).concat(Object.values(INLINE_FIELD_WRAPPERS))) { + if (key.key.includes(sep)) + return undefined; + } + let value = findClosing(line, key.valueIndex, open, INLINE_FIELD_WRAPPERS[open]); + if (value === undefined) + return undefined; + return { + key: key.key, + value: value.value, + start: start, + startValue: key.valueIndex, + end: value.endIndex, + wrapping: open, + }; +} +/** Parse a textual inline field value into something we can work with. */ +function parseInlineValue(value) { + // Empty inline values (i.e., no text) should map to null to match long-term Dataview semantics. + // Null is also a more universal type to deal with than strings, since all functions accept nulls. + if (value.trim() == "") + return null; + // The stripped literal field parser understands all of the non-array/non-object fields and can parse them for us. + // Inline field objects are not currently supported; inline array objects have to be handled by the parser + // separately. + let inline = EXPRESSION.inlineField.parse(value); + if (inline.status) + return inline.value; + else + return value; +} +/** Extracts inline fields of the form '[key:: value]' from a line of text. This is done in a relatively + * "robust" way to avoid failing due to bad nesting or other interfering Markdown symbols: + * + * - Look for any wrappers ('[' and '(') in the line, trying to parse whatever comes after it as an inline key::. + * - If successful, scan until you find a matching end bracket, and parse whatever remains as an inline value. + */ +function extractInlineFields(line, includeTaskFields = false) { + let fields = []; + for (let wrapper of Object.keys(INLINE_FIELD_WRAPPERS)) { + let foundIndex = line.indexOf(wrapper); + while (foundIndex >= 0) { + let parsedField = findSpecificInlineField(line, foundIndex); + if (!parsedField) { + foundIndex = line.indexOf(wrapper, foundIndex + 1); + continue; + } + fields.push(parsedField); + foundIndex = line.indexOf(wrapper, parsedField.end); + } + } + if (includeTaskFields) + fields = fields.concat(extractSpecialTaskFields(line)); + fields.sort((a, b) => a.start - b.start); + let filteredFields = []; + for (let i = 0; i < fields.length; i++) { + if (i == 0 || filteredFields[filteredFields.length - 1].end < fields[i].start) { + filteredFields.push(fields[i]); + } + } + return filteredFields; +} +/** Validates that a raw field name has a valid form. */ +const FULL_LINE_KEY_PART = parsimmon_umd_minExports.alt(parsimmon_umd_minExports.regexp(new RegExp(emojiRegex(), "u")), parsimmon_umd_minExports.regexp(/[0-9\p{Letter}\w\s_/-]+/u)) + .many() + .map(parts => parts.join("")); +parsimmon_umd_minExports.regexp(/[^0-9\w\p{Letter}]*/u) + .then(FULL_LINE_KEY_PART) + .skip(parsimmon_umd_minExports.regexp(/[_\*~`]*/u)); +const CREATED_DATE_REGEX = /\u{2795}\s*(\d{4}-\d{2}-\d{2})/u; +const DUE_DATE_REGEX = /(?:\u{1F4C5}|\u{1F4C6}|\u{1F5D3}\u{FE0F}?)\s*(\d{4}-\d{2}-\d{2})/u; +const DONE_DATE_REGEX = /\u{2705}\s*(\d{4}-\d{2}-\d{2})/u; +const SCHEDULED_DATE_REGEX = /[\u{23F3}\u{231B}]\s*(\d{4}-\d{2}-\d{2})/u; +const START_DATE_REGEX = /\u{1F6EB}\s*(\d{4}-\d{2}-\d{2})/u; +const EMOJI_REGEXES = [ + { regex: CREATED_DATE_REGEX, key: "created" }, + { regex: START_DATE_REGEX, key: "start" }, + { regex: SCHEDULED_DATE_REGEX, key: "scheduled" }, + { regex: DUE_DATE_REGEX, key: "due" }, + { regex: DONE_DATE_REGEX, key: "completion" }, +]; +/** Parse special completed/due/done task fields which are marked via emoji. */ +function extractSpecialTaskFields(line) { + let results = []; + for (let { regex, key } of EMOJI_REGEXES) { + const match = regex.exec(line); + if (!match) + continue; + results.push({ + key, + value: match[1], + start: match.index, + startValue: match.index + 1, + end: match.index + match[0].length, + wrapping: "emoji-shorthand", + }); + } + return results; +} +/** Sets or replaces the value of an inline field; if the value is 'undefined', deletes the key. */ +function setInlineField(source, key, value) { + let existing = extractInlineFields(source); + let existingKeys = existing.filter(f => f.key == key); + // Don't do anything if there are duplicate keys OR the key already doesn't exist. + if (existingKeys.length > 2 || (existingKeys.length == 0 && !value)) + return source; + let existingKey = existingKeys[0]; + let annotation = value ? `[${key}:: ${value}]` : ""; + if (existingKey) { + let prefix = source.substring(0, existingKey.start); + let suffix = source.substring(existingKey.end); + if (annotation) + return `${prefix}${annotation}${suffix}`; + else + return `${prefix}${suffix.trimStart()}`; + } + else if (annotation) { + return `${source.trimEnd()} ${annotation}`; + } + return source; +} +function setEmojiShorthandCompletionField(source, value) { + const existing = extractInlineFields(source, true); + const existingKeys = existing.filter(f => f.key === "completion" && f.wrapping === "emoji-shorthand"); + // Don't do anything if there are duplicate keys OR the key already doesn't exist. + if (existingKeys.length > 2 || (existingKeys.length == 0 && !value)) + return source; + /* No wrapper, add own spacing at start */ + const annotation = value ? ` ✅ ${value}` : ""; + let existingKey = existingKeys[0]; + if (existingKey) { + const prefix = source.substring(0, existingKey.start); + const suffix = source.substring(existingKey.end); + return `${prefix.trimEnd()}${annotation}${suffix}`; + } + else { + return `${source.trimEnd()}${annotation}`; + } +} - executeCallback(promise, callback); - return promise; +/** All extracted markdown file metadata obtained from a file. */ +class PageMetadata { + constructor(path, init) { + this.path = path; + this.fields = new Map(); + this.frontmatter = {}; + this.tags = new Set(); + this.aliases = new Set(); + this.links = []; + Object.assign(this, init); + this.lists = (this.lists || []).map(l => new ListItem$1(l)); + } + /** Canonicalize raw links and other data in partial data with normalizers, returning a completed object. */ + static canonicalize(data, linkNormalizer) { + // Mutate the data for now, which is probably a bad idea but... all well. + if (data.frontmatter) { + data.frontmatter = Values.mapLeaves(data.frontmatter, t => Values.isLink(t) ? linkNormalizer(t) : t); + } + if (data.fields) { + for (let [key, value] of data.fields.entries()) { + data.fields.set(key, Values.mapLeaves(value, t => (Values.isLink(t) ? linkNormalizer(t) : t))); + } + } + if (data.lists) { + for (let item of data.lists) { + for (let [key, value] of item.fields.entries()) { + item.fields.set(key, value.map(x => Values.mapLeaves(x, t => (Values.isLink(t) ? linkNormalizer(t) : t)))); + } + } + } + if (data.links) { + data.links = data.links.map(l => linkNormalizer(l)); + } + // This is pretty ugly, but it's not possible to normalize on the worker thread that does parsing. + // The best way to improve this is to instead just canonicalize the entire data object; I can try to + // optimize `Values.mapLeaves` to only mutate if it actually changes things. + return new PageMetadata(data.path, data); + } + /** The name (based on path) of this file. */ + name() { + return getFileTitle(this.path); + } + /** The containing folder (based on path) of this file. */ + folder() { + return getParentFolder(this.path); + } + /** The extension of this file (likely 'md'). */ + extension() { + return getExtension(this.path); + } + /** Return a set of tags AND all of their parent tags (so #hello/yes would become #hello, #hello/yes). */ + fullTags() { + let result = new Set(); + for (let tag of this.tags) { + for (let subtag of extractSubtags(tag)) + result.add(subtag); + } + return result; + } + /** Convert all links in this file to file links. */ + fileLinks() { + // We want to make them distinct, but where links are not raw links we + // now keep the additional metadata. + let distinctLinks = new Set(this.links); + return Array.from(distinctLinks); + } + /** Map this metadata to a full object; uses the index for additional data lookups. */ + serialize(index, cache) { + // Convert list items via the canonicalization cache. + let realCache = cache !== null && cache !== void 0 ? cache : new ListSerializationCache(this.lists); + let result = { + file: { + path: this.path, + folder: this.folder(), + name: this.name(), + link: Link.file(this.path), + outlinks: this.fileLinks(), + inlinks: Array.from(index.links.getInverse(this.path)).map(l => Link.file(l)), + etags: Array.from(this.tags), + tags: Array.from(this.fullTags()), + aliases: Array.from(this.aliases), + lists: this.lists.map(l => realCache.get(l.line)), + tasks: this.lists.filter(l => !!l.task).map(l => realCache.get(l.line)), + ctime: this.ctime, + cday: stripTime(this.ctime), + mtime: this.mtime, + mday: stripTime(this.mtime), + size: this.size, + starred: index.starred.starred(this.path), + frontmatter: Values.deepCopy(this.frontmatter), + ext: this.extension(), + }, + }; + // Add the current day if present. + if (this.day) + result.file.day = this.day; + // Then append the computed fields. + for (let [key, value] of this.fields.entries()) { + if (key in result) + continue; // Don't allow fields to override existing keys. + result[key] = value; + } + return result; + } +} +/** A list item inside of a list. */ +class ListItem$1 { + constructor(init) { + Object.assign(this, init); + this.fields = this.fields || new Map(); + this.tags = this.tags || new Set(); + this.children = this.children || []; + this.links = this.links || []; + } + id() { + return `${this.file().path}-${this.line}`; + } + file() { + return this.link.toFile(); + } + markdown() { + if (this.task) + return `${this.symbol} [${this.task.completed ? "x" : " "}] ${this.text}`; + else + return `${this.symbol} ${this.text}`; + } + created() { + var _a, _b, _c; + return (_c = ((_b = (_a = this.fields.get("created")) !== null && _a !== void 0 ? _a : this.fields.get("ctime")) !== null && _b !== void 0 ? _b : this.fields.get("cday"))) === null || _c === void 0 ? void 0 : _c[0]; + } + due() { + var _a, _b, _c; + return (_c = ((_b = (_a = this.fields.get("due")) !== null && _a !== void 0 ? _a : this.fields.get("duetime")) !== null && _b !== void 0 ? _b : this.fields.get("dueday"))) === null || _c === void 0 ? void 0 : _c[0]; + } + completed() { + var _a, _b, _c, _d; + return (_d = ((_c = (_b = (_a = this.fields.get("completed")) !== null && _a !== void 0 ? _a : this.fields.get("completion")) !== null && _b !== void 0 ? _b : this.fields.get("comptime")) !== null && _c !== void 0 ? _c : this.fields.get("compday"))) === null || _d === void 0 ? void 0 : _d[0]; + } + start() { + var _a; + return (_a = this.fields.get("start")) === null || _a === void 0 ? void 0 : _a[0]; + } + scheduled() { + var _a; + return (_a = this.fields.get("scheduled")) === null || _a === void 0 ? void 0 : _a[0]; + } + /** Create an API-friendly copy of this list item. De-duplication is done via the provided cache. */ + serialize(cache) { + // Map children to their serialized/de-duplicated equivalents right away. + let children = this.children.map(l => cache.get(l)).filter((l) => l !== undefined); + let result = { + symbol: this.symbol, + link: this.link, + section: this.section, + text: this.text, + tags: Array.from(this.tags), + line: this.line, + lineCount: this.lineCount, + list: this.list, + outlinks: Array.from(this.links), + path: this.link.path, + children: children, + task: !!this.task, + annotated: this.fields.size > 0, + position: Values.deepCopy(this.position), + subtasks: children, + real: !!this.task, + header: this.section, // @deprecated, use 'item.section' instead. + }; + if (this.parent || this.parent === 0) + result.parent = this.parent; + if (this.blockId) + result.blockId = this.blockId; + addFields(this.fields, result); + if (this.task) { + result.status = this.task.status; + result.checked = this.task.checked; + result.completed = this.task.completed; + result.fullyCompleted = this.task.fullyCompleted; + let created = this.created(), due = this.due(), completed = this.completed(), start = this.start(), scheduled = this.scheduled(); + if (created) + result.created = Values.deepCopy(created); + if (due) + result.due = Values.deepCopy(due); + if (completed) + result.completion = Values.deepCopy(completed); + if (start) + result.start = Values.deepCopy(start); + if (scheduled) + result.scheduled = Values.deepCopy(scheduled); + } + return result; + } +} +////////////////////////////////////////// +// Conversion / Serialization Utilities // +////////////////////////////////////////// +/** De-duplicates list items across section metadata and page metadata. */ +class ListSerializationCache { + constructor(listItems) { + this.listItems = {}; + this.cache = {}; + this.seen = new Set(); + for (let item of listItems) + this.listItems[item.line] = item; + } + get(lineno) { + if (lineno in this.cache) + return this.cache[lineno]; + else if (this.seen.has(lineno)) { + console.log(`Dataview: Encountered a circular list (line number ${lineno}; children ${this.listItems[lineno].children.join(", ")})`); + return undefined; + } + this.seen.add(lineno); + let result = this.listItems[lineno].serialize(this); + this.cache[lineno] = result; + return result; + } +} +function addFields(fields, target) { + for (let [key, values] of fields.entries()) { + if (key in target) + continue; + target[key] = values.length == 1 ? values[0] : values; + } + return target; } -// Iterate over all items stored in database. -function iterate(iterator, callback) { - var self = this; +/** Importer for markdown documents. */ +// TODO: Consider using an actual parser in leiu of a more expensive regex. +const LIST_ITEM_REGEX = /^[\s>]*(\d+\.|\d+\)|\*|-|\+)\s*(\[.{0,1}\])?\s*(.*)$/mu; +/** Recursively convert frontmatter into fields. We have to dance around YAML structure. */ +function parseFrontmatter(value) { + if (value == null) { + return null; + } + else if (typeof value === "object") { + if (Array.isArray(value)) { + let result = []; + for (let child of value) { + result.push(parseFrontmatter(child)); + } + return result; + } + else if (value instanceof Date) { + let dateParse = DateTime.fromJSDate(value); + return dateParse; + } + else { + let object = value; + let result = {}; + for (let key in object) { + result[key] = parseFrontmatter(object[key]); + } + return result; + } + } + else if (typeof value === "number") { + return value; + } + else if (typeof value === "boolean") { + return value; + } + else if (typeof value === "string") { + let dateParse = EXPRESSION.date.parse(value); + if (dateParse.status) + return dateParse.value; + let durationParse = EXPRESSION.duration.parse(value); + if (durationParse.status) + return durationParse.value; + let linkParse = EXPRESSION.embedLink.parse(value); + if (linkParse.status) + return linkParse.value; + return value; + } + // Backup if we don't understand the type. + return null; +} - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { - if (err) { - return reject(err); +/** Parse a CSV file into a collection of data rows. */ +function parseCsv(content) { + let parsed = papaparse_minExports.parse(content, { + header: true, + skipEmptyLines: true, + comments: "#", + dynamicTyping: true, + }); + let rows = []; + for (let parsedRow of parsed.data) { + let fields = parseFrontmatter(parsedRow); + let result = {}; + for (let [key, value] of Object.entries(fields)) { + result[key] = value; + result[canonicalizeVarName(key)] = value; + } + rows.push(result); + } + return rows; +} + +/** Simplifies passing dataview values across the JS web worker barrier. */ +var Transferable; +(function (Transferable) { + /** Convert a literal value to a serializer-friendly transferable value. */ + function transferable(value) { + // Handle simple universal types first. + if (value instanceof Map) { + let copied = new Map(); + for (let [key, val] of value.entries()) + copied.set(transferable(key), transferable(val)); + return copied; + } + else if (value instanceof Set) { + let copied = new Set(); + for (let val of value) + copied.add(transferable(val)); + return copied; + } + let wrapped = Values.wrapValue(value); + if (wrapped === undefined) + throw Error("Unrecognized transferable value: " + value); + switch (wrapped.type) { + case "null": + case "number": + case "string": + case "boolean": + return wrapped.value; + case "date": + return { + "___transfer-type": "date", + value: transferable(wrapped.value.toObject()), + options: { + zone: wrapped.value.zone.equals(SystemZone.instance) ? undefined : wrapped.value.zoneName, + }, + }; + case "duration": + return { "___transfer-type": "duration", value: transferable(wrapped.value.toObject()) }; + case "array": + return wrapped.value.map(v => transferable(v)); + case "link": + return { "___transfer-type": "link", value: transferable(wrapped.value.toObject()) }; + case "object": + let result = {}; + for (let [key, value] of Object.entries(wrapped.value)) + result[key] = transferable(value); + return result; + } + } + Transferable.transferable = transferable; + /** Convert a transferable value back to a literal value we can work with. */ + function value(transferable) { + if (transferable === null) { + return null; + } + else if (transferable === undefined) { + return undefined; + } + else if (transferable instanceof Map) { + let real = new Map(); + for (let [key, val] of transferable.entries()) + real.set(value(key), value(val)); + return real; + } + else if (transferable instanceof Set) { + let real = new Set(); + for (let val of transferable) + real.add(value(val)); + return real; + } + else if (Array.isArray(transferable)) { + return transferable.map(v => value(v)); + } + else if (typeof transferable === "object") { + if ("___transfer-type" in transferable) { + switch (transferable["___transfer-type"]) { + case "date": + let dateOpts = value(transferable.options); + let dateData = value(transferable.value); + return DateTime.fromObject(dateData, { zone: dateOpts.zone }); + case "duration": + return Duration.fromObject(value(transferable.value)); + case "link": + return Link.fromObject(value(transferable.value)); + default: + throw Error(`Unrecognized transfer type '${transferable["___transfer-type"]}'`); } + } + let result = {}; + for (let [key, val] of Object.entries(transferable)) + result[key] = value(val); + return result; + } + return transferable; + } + Transferable.value = value; +})(Transferable || (Transferable = {})); - try { - var store = transaction.objectStore(self._dbInfo.storeName); - var req = store.openCursor(); - var iterationNumber = 1; +function commonjsRequire(path) { + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'); +} - req.onsuccess = function () { - var cursor = req.result; +var localforage$1 = {exports: {}}; - if (cursor) { - var value = cursor.value; - if (_isEncodedBlob(value)) { - value = _decodeBlob(value); - } - var result = iterator(value, cursor.key, iterationNumber++); - - // when the iterator callback returns any - // (non-`undefined`) value, then we stop - // the iteration immediately - if (result !== void 0) { - resolve(result); - } else { - cursor["continue"](); - } - } else { - resolve(); - } - }; +/*! + localForage -- Offline Storage, Improved + Version 1.10.0 + https://localforage.github.io/localForage + (c) 2013-2017 Mozilla, Apache License 2.0 +*/ +localforage$1.exports; - req.onerror = function () { - reject(req.error); - }; - } catch (e) { - reject(e); - } - }); - })["catch"](reject); - }); +(function (module, exports) { + (function(f){{module.exports=f();}})(function(){return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof commonjsRequire=="function"&&commonjsRequire;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw (f.code="MODULE_NOT_FOUND", f)}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r);}return n[o].exports}var i=typeof commonjsRequire=="function"&&commonjsRequire;for(var o=0;o element; its readystatechange event will be fired asynchronously once it is inserted + // into the document. Do so, thus queuing up the task. Remember to clean up once it's been called. + var scriptEl = global.document.createElement('script'); + scriptEl.onreadystatechange = function () { + nextTick(); + + scriptEl.onreadystatechange = null; + scriptEl.parentNode.removeChild(scriptEl); + scriptEl = null; + }; + global.document.documentElement.appendChild(scriptEl); + }; + } else { + scheduleDrain = function () { + setTimeout(nextTick, 0); + }; + } + } - executeCallback(promise, callback); + var draining; + var queue = []; + //named nextTick for less confusing stack traces + function nextTick() { + draining = true; + var i, oldQueue; + var len = queue.length; + while (len) { + oldQueue = queue; + queue = []; + i = -1; + while (++i < len) { + oldQueue[i](); + } + len = queue.length; + } + draining = false; + } - return promise; -} + module.exports = immediate; + function immediate(task) { + if (queue.push(task) === 1 && !draining) { + scheduleDrain(); + } + } -function setItem(key, value, callback) { - var self = this; + }).call(this,typeof commonjsGlobal !== "undefined" ? commonjsGlobal : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}); + },{}],2:[function(_dereq_,module,exports){ + var immediate = _dereq_(1); - key = normalizeKey(key); + /* istanbul ignore next */ + function INTERNAL() {} - var promise = new Promise$1(function (resolve, reject) { - var dbInfo; - self.ready().then(function () { - dbInfo = self._dbInfo; - if (toString.call(value) === '[object Blob]') { - return _checkBlobSupport(dbInfo.db).then(function (blobSupport) { - if (blobSupport) { - return value; - } - return _encodeBlob(value); - }); - } - return value; - }).then(function (value) { - createTransaction(self._dbInfo, READ_WRITE, function (err, transaction) { - if (err) { - return reject(err); - } + var handlers = {}; - try { - var store = transaction.objectStore(self._dbInfo.storeName); - - // The reason we don't _save_ null is because IE 10 does - // not support saving the `null` type in IndexedDB. How - // ironic, given the bug below! - // See: https://github.com/mozilla/localForage/issues/161 - if (value === null) { - value = undefined; - } + var REJECTED = ['REJECTED']; + var FULFILLED = ['FULFILLED']; + var PENDING = ['PENDING']; - var req = store.put(value, key); - - transaction.oncomplete = function () { - // Cast to undefined so the value passed to - // callback/promise is the same as what one would get out - // of `getItem()` later. This leads to some weirdness - // (setItem('foo', undefined) will return `null`), but - // it's not my fault localStorage is our baseline and that - // it's weird. - if (value === undefined) { - value = null; - } + module.exports = Promise; - resolve(value); - }; - transaction.onabort = transaction.onerror = function () { - var err = req.error ? req.error : req.transaction.error; - reject(err); - }; - } catch (e) { - reject(e); - } - }); - })["catch"](reject); - }); + function Promise(resolver) { + if (typeof resolver !== 'function') { + throw new TypeError('resolver must be a function'); + } + this.state = PENDING; + this.queue = []; + this.outcome = void 0; + if (resolver !== INTERNAL) { + safelyResolveThenable(this, resolver); + } + } - executeCallback(promise, callback); - return promise; -} + Promise.prototype["catch"] = function (onRejected) { + return this.then(null, onRejected); + }; + Promise.prototype.then = function (onFulfilled, onRejected) { + if (typeof onFulfilled !== 'function' && this.state === FULFILLED || + typeof onRejected !== 'function' && this.state === REJECTED) { + return this; + } + var promise = new this.constructor(INTERNAL); + if (this.state !== PENDING) { + var resolver = this.state === FULFILLED ? onFulfilled : onRejected; + unwrap(promise, resolver, this.outcome); + } else { + this.queue.push(new QueueItem(promise, onFulfilled, onRejected)); + } + + return promise; + }; + function QueueItem(promise, onFulfilled, onRejected) { + this.promise = promise; + if (typeof onFulfilled === 'function') { + this.onFulfilled = onFulfilled; + this.callFulfilled = this.otherCallFulfilled; + } + if (typeof onRejected === 'function') { + this.onRejected = onRejected; + this.callRejected = this.otherCallRejected; + } + } + QueueItem.prototype.callFulfilled = function (value) { + handlers.resolve(this.promise, value); + }; + QueueItem.prototype.otherCallFulfilled = function (value) { + unwrap(this.promise, this.onFulfilled, value); + }; + QueueItem.prototype.callRejected = function (value) { + handlers.reject(this.promise, value); + }; + QueueItem.prototype.otherCallRejected = function (value) { + unwrap(this.promise, this.onRejected, value); + }; -function removeItem(key, callback) { - var self = this; + function unwrap(promise, func, value) { + immediate(function () { + var returnValue; + try { + returnValue = func(value); + } catch (e) { + return handlers.reject(promise, e); + } + if (returnValue === promise) { + handlers.reject(promise, new TypeError('Cannot resolve promise with itself')); + } else { + handlers.resolve(promise, returnValue); + } + }); + } - key = normalizeKey(key); + handlers.resolve = function (self, value) { + var result = tryCatch(getThen, value); + if (result.status === 'error') { + return handlers.reject(self, result.value); + } + var thenable = result.value; + + if (thenable) { + safelyResolveThenable(self, thenable); + } else { + self.state = FULFILLED; + self.outcome = value; + var i = -1; + var len = self.queue.length; + while (++i < len) { + self.queue[i].callFulfilled(value); + } + } + return self; + }; + handlers.reject = function (self, error) { + self.state = REJECTED; + self.outcome = error; + var i = -1; + var len = self.queue.length; + while (++i < len) { + self.queue[i].callRejected(error); + } + return self; + }; - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - createTransaction(self._dbInfo, READ_WRITE, function (err, transaction) { - if (err) { - return reject(err); - } + function getThen(obj) { + // Make sure we only access the accessor once as required by the spec + var then = obj && obj.then; + if (obj && (typeof obj === 'object' || typeof obj === 'function') && typeof then === 'function') { + return function appyThen() { + then.apply(obj, arguments); + }; + } + } - try { - var store = transaction.objectStore(self._dbInfo.storeName); - // We use a Grunt task to make this safe for IE and some - // versions of Android (including those used by Cordova). - // Normally IE won't like `.delete()` and will insist on - // using `['delete']()`, but we have a build step that - // fixes this for us now. - var req = store["delete"](key); - transaction.oncomplete = function () { - resolve(); - }; + function safelyResolveThenable(self, thenable) { + // Either fulfill, reject or reject with error + var called = false; + function onError(value) { + if (called) { + return; + } + called = true; + handlers.reject(self, value); + } + + function onSuccess(value) { + if (called) { + return; + } + called = true; + handlers.resolve(self, value); + } + + function tryToUnwrap() { + thenable(onSuccess, onError); + } + + var result = tryCatch(tryToUnwrap); + if (result.status === 'error') { + onError(result.value); + } + } - transaction.onerror = function () { - reject(req.error); - }; + function tryCatch(func, value) { + var out = {}; + try { + out.value = func(value); + out.status = 'success'; + } catch (e) { + out.status = 'error'; + out.value = e; + } + return out; + } - // The request will be also be aborted if we've exceeded our storage - // space. - transaction.onabort = function () { - var err = req.error ? req.error : req.transaction.error; - reject(err); - }; - } catch (e) { - reject(e); - } - }); - })["catch"](reject); - }); + Promise.resolve = resolve; + function resolve(value) { + if (value instanceof this) { + return value; + } + return handlers.resolve(new this(INTERNAL), value); + } - executeCallback(promise, callback); - return promise; -} + Promise.reject = reject; + function reject(reason) { + var promise = new this(INTERNAL); + return handlers.reject(promise, reason); + } -function clear(callback) { - var self = this; + Promise.all = all; + function all(iterable) { + var self = this; + if (Object.prototype.toString.call(iterable) !== '[object Array]') { + return this.reject(new TypeError('must be an array')); + } + + var len = iterable.length; + var called = false; + if (!len) { + return this.resolve([]); + } + + var values = new Array(len); + var resolved = 0; + var i = -1; + var promise = new this(INTERNAL); + + while (++i < len) { + allResolver(iterable[i], i); + } + return promise; + function allResolver(value, i) { + self.resolve(value).then(resolveFromAll, function (error) { + if (!called) { + called = true; + handlers.reject(promise, error); + } + }); + function resolveFromAll(outValue) { + values[i] = outValue; + if (++resolved === len && !called) { + called = true; + handlers.resolve(promise, values); + } + } + } + } - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - createTransaction(self._dbInfo, READ_WRITE, function (err, transaction) { - if (err) { - return reject(err); - } + Promise.race = race; + function race(iterable) { + var self = this; + if (Object.prototype.toString.call(iterable) !== '[object Array]') { + return this.reject(new TypeError('must be an array')); + } + + var len = iterable.length; + var called = false; + if (!len) { + return this.resolve([]); + } + + var i = -1; + var promise = new this(INTERNAL); + + while (++i < len) { + resolver(iterable[i]); + } + return promise; + function resolver(value) { + self.resolve(value).then(function (response) { + if (!called) { + called = true; + handlers.resolve(promise, response); + } + }, function (error) { + if (!called) { + called = true; + handlers.reject(promise, error); + } + }); + } + } - try { - var store = transaction.objectStore(self._dbInfo.storeName); - var req = store.clear(); + },{"1":1}],3:[function(_dereq_,module,exports){ + (function (global){ + if (typeof global.Promise !== 'function') { + global.Promise = _dereq_(2); + } - transaction.oncomplete = function () { - resolve(); - }; + }).call(this,typeof commonjsGlobal !== "undefined" ? commonjsGlobal : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}); + },{"2":2}],4:[function(_dereq_,module,exports){ + + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + + function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + + function getIDB() { + /* global indexedDB,webkitIndexedDB,mozIndexedDB,OIndexedDB,msIndexedDB */ + try { + if (typeof indexedDB !== 'undefined') { + return indexedDB; + } + if (typeof webkitIndexedDB !== 'undefined') { + return webkitIndexedDB; + } + if (typeof mozIndexedDB !== 'undefined') { + return mozIndexedDB; + } + if (typeof OIndexedDB !== 'undefined') { + return OIndexedDB; + } + if (typeof msIndexedDB !== 'undefined') { + return msIndexedDB; + } + } catch (e) { + return; + } + } - transaction.onabort = transaction.onerror = function () { - var err = req.error ? req.error : req.transaction.error; - reject(err); - }; - } catch (e) { - reject(e); - } - }); - })["catch"](reject); - }); + var idb = getIDB(); + + function isIndexedDBValid() { + try { + // Initialize IndexedDB; fall back to vendor-prefixed versions + // if needed. + if (!idb || !idb.open) { + return false; + } + // We mimic PouchDB here; + // + // We test for openDatabase because IE Mobile identifies itself + // as Safari. Oh the lulz... + var isSafari = typeof openDatabase !== 'undefined' && /(Safari|iPhone|iPad|iPod)/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent) && !/BlackBerry/.test(navigator.platform); + + var hasFetch = typeof fetch === 'function' && fetch.toString().indexOf('[native code') !== -1; + + // Safari <10.1 does not meet our requirements for IDB support + // (see: https://github.com/pouchdb/pouchdb/issues/5572). + // Safari 10.1 shipped with fetch, we can use that to detect it. + // Note: this creates issues with `window.fetch` polyfills and + // overrides; see: + // https://github.com/localForage/localForage/issues/856 + return (!isSafari || hasFetch) && typeof indexedDB !== 'undefined' && + // some outdated implementations of IDB that appear on Samsung + // and HTC Android devices <4.4 are missing IDBKeyRange + // See: https://github.com/mozilla/localForage/issues/128 + // See: https://github.com/mozilla/localForage/issues/272 + typeof IDBKeyRange !== 'undefined'; + } catch (e) { + return false; + } + } - executeCallback(promise, callback); - return promise; -} + // Abstracts constructing a Blob object, so it also works in older + // browsers that don't support the native Blob constructor. (i.e. + // old QtWebKit versions, at least). + // Abstracts constructing a Blob object, so it also works in older + // browsers that don't support the native Blob constructor. (i.e. + // old QtWebKit versions, at least). + function createBlob(parts, properties) { + /* global BlobBuilder,MSBlobBuilder,MozBlobBuilder,WebKitBlobBuilder */ + parts = parts || []; + properties = properties || {}; + try { + return new Blob(parts, properties); + } catch (e) { + if (e.name !== 'TypeError') { + throw e; + } + var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder : typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder : typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder : WebKitBlobBuilder; + var builder = new Builder(); + for (var i = 0; i < parts.length; i += 1) { + builder.append(parts[i]); + } + return builder.getBlob(properties.type); + } + } -function length(callback) { - var self = this; + // This is CommonJS because lie is an external dependency, so Rollup + // can just ignore it. + if (typeof Promise === 'undefined') { + // In the "nopromises" build this will just throw if you don't have + // a global promise object, but it would throw anyway later. + _dereq_(3); + } + var Promise$1 = Promise; + + function executeCallback(promise, callback) { + if (callback) { + promise.then(function (result) { + callback(null, result); + }, function (error) { + callback(error); + }); + } + } - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { - if (err) { - return reject(err); - } + function executeTwoCallbacks(promise, callback, errorCallback) { + if (typeof callback === 'function') { + promise.then(callback); + } - try { - var store = transaction.objectStore(self._dbInfo.storeName); - var req = store.count(); + if (typeof errorCallback === 'function') { + promise["catch"](errorCallback); + } + } - req.onsuccess = function () { - resolve(req.result); - }; + function normalizeKey(key) { + // Cast the key to a string, as that's all we can set as a key. + if (typeof key !== 'string') { + console.warn(key + ' used as a key, but it is not a string.'); + key = String(key); + } - req.onerror = function () { - reject(req.error); - }; - } catch (e) { - reject(e); - } - }); - })["catch"](reject); - }); + return key; + } - executeCallback(promise, callback); - return promise; -} + function getCallback() { + if (arguments.length && typeof arguments[arguments.length - 1] === 'function') { + return arguments[arguments.length - 1]; + } + } -function key(n, callback) { - var self = this; + // Some code originally from async_storage.js in + // [Gaia](https://github.com/mozilla-b2g/gaia). + + var DETECT_BLOB_SUPPORT_STORE = 'local-forage-detect-blob-support'; + var supportsBlobs = void 0; + var dbContexts = {}; + var toString = Object.prototype.toString; + + // Transaction Modes + var READ_ONLY = 'readonly'; + var READ_WRITE = 'readwrite'; + + // Transform a binary string to an array buffer, because otherwise + // weird stuff happens when you try to work with the binary string directly. + // It is known. + // From http://stackoverflow.com/questions/14967647/ (continues on next line) + // encode-decode-image-with-base64-breaks-image (2013-04-21) + function _binStringToArrayBuffer(bin) { + var length = bin.length; + var buf = new ArrayBuffer(length); + var arr = new Uint8Array(buf); + for (var i = 0; i < length; i++) { + arr[i] = bin.charCodeAt(i); + } + return buf; + } - var promise = new Promise$1(function (resolve, reject) { - if (n < 0) { - resolve(null); + // + // Blobs are not supported in all versions of IndexedDB, notably + // Chrome <37 and Android <5. In those versions, storing a blob will throw. + // + // Various other blob bugs exist in Chrome v37-42 (inclusive). + // Detecting them is expensive and confusing to users, and Chrome 37-42 + // is at very low usage worldwide, so we do a hacky userAgent check instead. + // + // content-type bug: https://code.google.com/p/chromium/issues/detail?id=408120 + // 404 bug: https://code.google.com/p/chromium/issues/detail?id=447916 + // FileReader bug: https://code.google.com/p/chromium/issues/detail?id=447836 + // + // Code borrowed from PouchDB. See: + // https://github.com/pouchdb/pouchdb/blob/master/packages/node_modules/pouchdb-adapter-idb/src/blobSupport.js + // + function _checkBlobSupportWithoutCaching(idb) { + return new Promise$1(function (resolve) { + var txn = idb.transaction(DETECT_BLOB_SUPPORT_STORE, READ_WRITE); + var blob = createBlob(['']); + txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob, 'key'); + + txn.onabort = function (e) { + // If the transaction aborts now its due to not being able to + // write to the database, likely due to the disk being full + e.preventDefault(); + e.stopPropagation(); + resolve(false); + }; + + txn.oncomplete = function () { + var matchedChrome = navigator.userAgent.match(/Chrome\/(\d+)/); + var matchedEdge = navigator.userAgent.match(/Edge\//); + // MS Edge pretends to be Chrome 42: + // https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx + resolve(matchedEdge || !matchedChrome || parseInt(matchedChrome[1], 10) >= 43); + }; + })["catch"](function () { + return false; // error, so assume unsupported + }); + } - return; - } + function _checkBlobSupport(idb) { + if (typeof supportsBlobs === 'boolean') { + return Promise$1.resolve(supportsBlobs); + } + return _checkBlobSupportWithoutCaching(idb).then(function (value) { + supportsBlobs = value; + return supportsBlobs; + }); + } - self.ready().then(function () { - createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { - if (err) { - return reject(err); - } + function _deferReadiness(dbInfo) { + var dbContext = dbContexts[dbInfo.name]; - try { - var store = transaction.objectStore(self._dbInfo.storeName); - var advanced = false; - var req = store.openKeyCursor(); + // Create a deferred object representing the current database operation. + var deferredOperation = {}; - req.onsuccess = function () { - var cursor = req.result; - if (!cursor) { - // this means there weren't enough keys - resolve(null); + deferredOperation.promise = new Promise$1(function (resolve, reject) { + deferredOperation.resolve = resolve; + deferredOperation.reject = reject; + }); - return; - } - - if (n === 0) { - // We have the first key, return it if that's what they - // wanted. - resolve(cursor.key); - } else { - if (!advanced) { - // Otherwise, ask the cursor to skip ahead n - // records. - advanced = true; - cursor.advance(n); - } else { - // When we get here, we've got the nth key. - resolve(cursor.key); - } - } - }; - - req.onerror = function () { - reject(req.error); - }; - } catch (e) { - reject(e); - } - }); - })["catch"](reject); - }); + // Enqueue the deferred operation. + dbContext.deferredOperations.push(deferredOperation); - executeCallback(promise, callback); - return promise; -} + // Chain its promise to the database readiness. + if (!dbContext.dbReady) { + dbContext.dbReady = deferredOperation.promise; + } else { + dbContext.dbReady = dbContext.dbReady.then(function () { + return deferredOperation.promise; + }); + } + } -function keys(callback) { - var self = this; + function _advanceReadiness(dbInfo) { + var dbContext = dbContexts[dbInfo.name]; - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { - if (err) { - return reject(err); - } + // Dequeue a deferred operation. + var deferredOperation = dbContext.deferredOperations.pop(); - try { - var store = transaction.objectStore(self._dbInfo.storeName); - var req = store.openKeyCursor(); - var keys = []; + // Resolve its promise (which is part of the database readiness + // chain of promises). + if (deferredOperation) { + deferredOperation.resolve(); + return deferredOperation.promise; + } + } - req.onsuccess = function () { - var cursor = req.result; + function _rejectReadiness(dbInfo, err) { + var dbContext = dbContexts[dbInfo.name]; - if (!cursor) { - resolve(keys); - return; - } + // Dequeue a deferred operation. + var deferredOperation = dbContext.deferredOperations.pop(); - keys.push(cursor.key); - cursor["continue"](); - }; + // Reject its promise (which is part of the database readiness + // chain of promises). + if (deferredOperation) { + deferredOperation.reject(err); + return deferredOperation.promise; + } + } - req.onerror = function () { - reject(req.error); - }; - } catch (e) { - reject(e); - } - }); - })["catch"](reject); - }); + function _getConnection(dbInfo, upgradeNeeded) { + return new Promise$1(function (resolve, reject) { + dbContexts[dbInfo.name] = dbContexts[dbInfo.name] || createDbContext(); + + if (dbInfo.db) { + if (upgradeNeeded) { + _deferReadiness(dbInfo); + dbInfo.db.close(); + } else { + return resolve(dbInfo.db); + } + } + + var dbArgs = [dbInfo.name]; + + if (upgradeNeeded) { + dbArgs.push(dbInfo.version); + } + + var openreq = idb.open.apply(idb, dbArgs); + + if (upgradeNeeded) { + openreq.onupgradeneeded = function (e) { + var db = openreq.result; + try { + db.createObjectStore(dbInfo.storeName); + if (e.oldVersion <= 1) { + // Added when support for blob shims was added + db.createObjectStore(DETECT_BLOB_SUPPORT_STORE); + } + } catch (ex) { + if (ex.name === 'ConstraintError') { + console.warn('The database "' + dbInfo.name + '"' + ' has been upgraded from version ' + e.oldVersion + ' to version ' + e.newVersion + ', but the storage "' + dbInfo.storeName + '" already exists.'); + } else { + throw ex; + } + } + }; + } + + openreq.onerror = function (e) { + e.preventDefault(); + reject(openreq.error); + }; + + openreq.onsuccess = function () { + var db = openreq.result; + db.onversionchange = function (e) { + // Triggered when the database is modified (e.g. adding an objectStore) or + // deleted (even when initiated by other sessions in different tabs). + // Closing the connection here prevents those operations from being blocked. + // If the database is accessed again later by this instance, the connection + // will be reopened or the database recreated as needed. + e.target.close(); + }; + resolve(db); + _advanceReadiness(dbInfo); + }; + }); + } - executeCallback(promise, callback); - return promise; -} + function _getOriginalConnection(dbInfo) { + return _getConnection(dbInfo, false); + } -function dropInstance(options, callback) { - callback = getCallback.apply(this, arguments); + function _getUpgradedConnection(dbInfo) { + return _getConnection(dbInfo, true); + } - var currentConfig = this.config(); - options = typeof options !== 'function' && options || {}; - if (!options.name) { - options.name = options.name || currentConfig.name; - options.storeName = options.storeName || currentConfig.storeName; - } + function _isUpgradeNeeded(dbInfo, defaultVersion) { + if (!dbInfo.db) { + return true; + } + + var isNewStore = !dbInfo.db.objectStoreNames.contains(dbInfo.storeName); + var isDowngrade = dbInfo.version < dbInfo.db.version; + var isUpgrade = dbInfo.version > dbInfo.db.version; + + if (isDowngrade) { + // If the version is not the default one + // then warn for impossible downgrade. + if (dbInfo.version !== defaultVersion) { + console.warn('The database "' + dbInfo.name + '"' + " can't be downgraded from version " + dbInfo.db.version + ' to version ' + dbInfo.version + '.'); + } + // Align the versions to prevent errors. + dbInfo.version = dbInfo.db.version; + } + + if (isUpgrade || isNewStore) { + // If the store is new then increment the version (if needed). + // This will trigger an "upgradeneeded" event which is required + // for creating a store. + if (isNewStore) { + var incVersion = dbInfo.db.version + 1; + if (incVersion > dbInfo.version) { + dbInfo.version = incVersion; + } + } + + return true; + } + + return false; + } - var self = this; - var promise; - if (!options.name) { - promise = Promise$1.reject('Invalid arguments'); - } else { - var isCurrentDb = options.name === currentConfig.name && self._dbInfo.db; - - var dbPromise = isCurrentDb ? Promise$1.resolve(self._dbInfo.db) : _getOriginalConnection(options).then(function (db) { - var dbContext = dbContexts[options.name]; - var forages = dbContext.forages; - dbContext.db = db; - for (var i = 0; i < forages.length; i++) { - forages[i]._dbInfo.db = db; - } - return db; - }); + // encode a blob for indexeddb engines that don't support blobs + function _encodeBlob(blob) { + return new Promise$1(function (resolve, reject) { + var reader = new FileReader(); + reader.onerror = reject; + reader.onloadend = function (e) { + var base64 = btoa(e.target.result || ''); + resolve({ + __local_forage_encoded_blob: true, + data: base64, + type: blob.type + }); + }; + reader.readAsBinaryString(blob); + }); + } - if (!options.storeName) { - promise = dbPromise.then(function (db) { - _deferReadiness(options); + // decode an encoded blob + function _decodeBlob(encodedBlob) { + var arrayBuff = _binStringToArrayBuffer(atob(encodedBlob.data)); + return createBlob([arrayBuff], { type: encodedBlob.type }); + } - var dbContext = dbContexts[options.name]; - var forages = dbContext.forages; + // is this one of our fancy encoded blobs? + function _isEncodedBlob(value) { + return value && value.__local_forage_encoded_blob; + } - db.close(); - for (var i = 0; i < forages.length; i++) { - var forage = forages[i]; - forage._dbInfo.db = null; - } + // Specialize the default `ready()` function by making it dependent + // on the current database operations. Thus, the driver will be actually + // ready when it's been initialized (default) *and* there are no pending + // operations on the database (initiated by some other instances). + function _fullyReady(callback) { + var self = this; - var dropDBPromise = new Promise$1(function (resolve, reject) { - var req = idb.deleteDatabase(options.name); + var promise = self._initReady().then(function () { + var dbContext = dbContexts[self._dbInfo.name]; - req.onerror = function () { - var db = req.result; - if (db) { - db.close(); - } - reject(req.error); - }; + if (dbContext && dbContext.dbReady) { + return dbContext.dbReady; + } + }); - req.onblocked = function () { - // Closing all open connections in onversionchange handler should prevent this situation, but if - // we do get here, it just means the request remains pending - eventually it will succeed or error - console.warn('dropInstance blocked for database "' + options.name + '" until all open connections are closed'); - }; + executeTwoCallbacks(promise, callback, callback); + return promise; + } - req.onsuccess = function () { - var db = req.result; - if (db) { - db.close(); - } - resolve(db); - }; - }); + // Try to establish a new db connection to replace the + // current one which is broken (i.e. experiencing + // InvalidStateError while creating a transaction). + function _tryReconnect(dbInfo) { + _deferReadiness(dbInfo); + + var dbContext = dbContexts[dbInfo.name]; + var forages = dbContext.forages; + + for (var i = 0; i < forages.length; i++) { + var forage = forages[i]; + if (forage._dbInfo.db) { + forage._dbInfo.db.close(); + forage._dbInfo.db = null; + } + } + dbInfo.db = null; + + return _getOriginalConnection(dbInfo).then(function (db) { + dbInfo.db = db; + if (_isUpgradeNeeded(dbInfo)) { + // Reopen the database for upgrading. + return _getUpgradedConnection(dbInfo); + } + return db; + }).then(function (db) { + // store the latest db reference + // in case the db was upgraded + dbInfo.db = dbContext.db = db; + for (var i = 0; i < forages.length; i++) { + forages[i]._dbInfo.db = db; + } + })["catch"](function (err) { + _rejectReadiness(dbInfo, err); + throw err; + }); + } - return dropDBPromise.then(function (db) { - dbContext.db = db; - for (var i = 0; i < forages.length; i++) { - var _forage = forages[i]; - _advanceReadiness(_forage._dbInfo); - } - })["catch"](function (err) { - (_rejectReadiness(options, err) || Promise$1.resolve())["catch"](function () {}); - throw err; - }); - }); - } else { - promise = dbPromise.then(function (db) { - if (!db.objectStoreNames.contains(options.storeName)) { - return; - } + // FF doesn't like Promises (micro-tasks) and IDDB store operations, + // so we have to do it with callbacks + function createTransaction(dbInfo, mode, callback, retries) { + if (retries === undefined) { + retries = 1; + } + + try { + var tx = dbInfo.db.transaction(dbInfo.storeName, mode); + callback(null, tx); + } catch (err) { + if (retries > 0 && (!dbInfo.db || err.name === 'InvalidStateError' || err.name === 'NotFoundError')) { + return Promise$1.resolve().then(function () { + if (!dbInfo.db || err.name === 'NotFoundError' && !dbInfo.db.objectStoreNames.contains(dbInfo.storeName) && dbInfo.version <= dbInfo.db.version) { + // increase the db version, to create the new ObjectStore + if (dbInfo.db) { + dbInfo.version = dbInfo.db.version + 1; + } + // Reopen the database for upgrading. + return _getUpgradedConnection(dbInfo); + } + }).then(function () { + return _tryReconnect(dbInfo).then(function () { + createTransaction(dbInfo, mode, callback, retries - 1); + }); + })["catch"](callback); + } + + callback(err); + } + } - var newVersion = db.version + 1; + function createDbContext() { + return { + // Running localForages sharing a database. + forages: [], + // Shared database. + db: null, + // Database readiness (promise). + dbReady: null, + // Deferred operations on the database. + deferredOperations: [] + }; + } - _deferReadiness(options); + // Open the IndexedDB database (automatically creates one if one didn't + // previously exist), using any options set in the config. + function _initStorage(options) { + var self = this; + var dbInfo = { + db: null + }; + + if (options) { + for (var i in options) { + dbInfo[i] = options[i]; + } + } + + // Get the current context of the database; + var dbContext = dbContexts[dbInfo.name]; + + // ...or create a new context. + if (!dbContext) { + dbContext = createDbContext(); + // Register the new context in the global container. + dbContexts[dbInfo.name] = dbContext; + } + + // Register itself as a running localForage in the current context. + dbContext.forages.push(self); + + // Replace the default `ready()` function with the specialized one. + if (!self._initReady) { + self._initReady = self.ready; + self.ready = _fullyReady; + } + + // Create an array of initialization states of the related localForages. + var initPromises = []; + + function ignoreErrors() { + // Don't handle errors here, + // just makes sure related localForages aren't pending. + return Promise$1.resolve(); + } + + for (var j = 0; j < dbContext.forages.length; j++) { + var forage = dbContext.forages[j]; + if (forage !== self) { + // Don't wait for itself... + initPromises.push(forage._initReady()["catch"](ignoreErrors)); + } + } + + // Take a snapshot of the related localForages. + var forages = dbContext.forages.slice(0); + + // Initialize the connection process only when + // all the related localForages aren't pending. + return Promise$1.all(initPromises).then(function () { + dbInfo.db = dbContext.db; + // Get the connection or open a new one without upgrade. + return _getOriginalConnection(dbInfo); + }).then(function (db) { + dbInfo.db = db; + if (_isUpgradeNeeded(dbInfo, self._defaultConfig.version)) { + // Reopen the database for upgrading. + return _getUpgradedConnection(dbInfo); + } + return db; + }).then(function (db) { + dbInfo.db = dbContext.db = db; + self._dbInfo = dbInfo; + // Share the final connection amongst related localForages. + for (var k = 0; k < forages.length; k++) { + var forage = forages[k]; + if (forage !== self) { + // Self is already up-to-date. + forage._dbInfo.db = dbInfo.db; + forage._dbInfo.version = dbInfo.version; + } + } + }); + } - var dbContext = dbContexts[options.name]; - var forages = dbContext.forages; + function getItem(key, callback) { + var self = this; + + key = normalizeKey(key); + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { + if (err) { + return reject(err); + } + + try { + var store = transaction.objectStore(self._dbInfo.storeName); + var req = store.get(key); + + req.onsuccess = function () { + var value = req.result; + if (value === undefined) { + value = null; + } + if (_isEncodedBlob(value)) { + value = _decodeBlob(value); + } + resolve(value); + }; + + req.onerror = function () { + reject(req.error); + }; + } catch (e) { + reject(e); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - db.close(); - for (var i = 0; i < forages.length; i++) { - var forage = forages[i]; - forage._dbInfo.db = null; - forage._dbInfo.version = newVersion; - } + // Iterate over all items stored in database. + function iterate(iterator, callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { + if (err) { + return reject(err); + } + + try { + var store = transaction.objectStore(self._dbInfo.storeName); + var req = store.openCursor(); + var iterationNumber = 1; + + req.onsuccess = function () { + var cursor = req.result; + + if (cursor) { + var value = cursor.value; + if (_isEncodedBlob(value)) { + value = _decodeBlob(value); + } + var result = iterator(value, cursor.key, iterationNumber++); + + // when the iterator callback returns any + // (non-`undefined`) value, then we stop + // the iteration immediately + if (result !== void 0) { + resolve(result); + } else { + cursor["continue"](); + } + } else { + resolve(); + } + }; + + req.onerror = function () { + reject(req.error); + }; + } catch (e) { + reject(e); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + + return promise; + } - var dropObjectPromise = new Promise$1(function (resolve, reject) { - var req = idb.open(options.name, newVersion); + function setItem(key, value, callback) { + var self = this; + + key = normalizeKey(key); + + var promise = new Promise$1(function (resolve, reject) { + var dbInfo; + self.ready().then(function () { + dbInfo = self._dbInfo; + if (toString.call(value) === '[object Blob]') { + return _checkBlobSupport(dbInfo.db).then(function (blobSupport) { + if (blobSupport) { + return value; + } + return _encodeBlob(value); + }); + } + return value; + }).then(function (value) { + createTransaction(self._dbInfo, READ_WRITE, function (err, transaction) { + if (err) { + return reject(err); + } + + try { + var store = transaction.objectStore(self._dbInfo.storeName); + + // The reason we don't _save_ null is because IE 10 does + // not support saving the `null` type in IndexedDB. How + // ironic, given the bug below! + // See: https://github.com/mozilla/localForage/issues/161 + if (value === null) { + value = undefined; + } + + var req = store.put(value, key); + + transaction.oncomplete = function () { + // Cast to undefined so the value passed to + // callback/promise is the same as what one would get out + // of `getItem()` later. This leads to some weirdness + // (setItem('foo', undefined) will return `null`), but + // it's not my fault localStorage is our baseline and that + // it's weird. + if (value === undefined) { + value = null; + } + + resolve(value); + }; + transaction.onabort = transaction.onerror = function () { + var err = req.error ? req.error : req.transaction.error; + reject(err); + }; + } catch (e) { + reject(e); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - req.onerror = function (err) { - var db = req.result; - db.close(); - reject(err); - }; + function removeItem(key, callback) { + var self = this; + + key = normalizeKey(key); + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + createTransaction(self._dbInfo, READ_WRITE, function (err, transaction) { + if (err) { + return reject(err); + } + + try { + var store = transaction.objectStore(self._dbInfo.storeName); + // We use a Grunt task to make this safe for IE and some + // versions of Android (including those used by Cordova). + // Normally IE won't like `.delete()` and will insist on + // using `['delete']()`, but we have a build step that + // fixes this for us now. + var req = store["delete"](key); + transaction.oncomplete = function () { + resolve(); + }; + + transaction.onerror = function () { + reject(req.error); + }; + + // The request will be also be aborted if we've exceeded our storage + // space. + transaction.onabort = function () { + var err = req.error ? req.error : req.transaction.error; + reject(err); + }; + } catch (e) { + reject(e); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - req.onupgradeneeded = function () { - var db = req.result; - db.deleteObjectStore(options.storeName); - }; + function clear(callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + createTransaction(self._dbInfo, READ_WRITE, function (err, transaction) { + if (err) { + return reject(err); + } + + try { + var store = transaction.objectStore(self._dbInfo.storeName); + var req = store.clear(); + + transaction.oncomplete = function () { + resolve(); + }; + + transaction.onabort = transaction.onerror = function () { + var err = req.error ? req.error : req.transaction.error; + reject(err); + }; + } catch (e) { + reject(e); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - req.onsuccess = function () { - var db = req.result; - db.close(); - resolve(db); - }; - }); + function length(callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { + if (err) { + return reject(err); + } + + try { + var store = transaction.objectStore(self._dbInfo.storeName); + var req = store.count(); + + req.onsuccess = function () { + resolve(req.result); + }; + + req.onerror = function () { + reject(req.error); + }; + } catch (e) { + reject(e); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - return dropObjectPromise.then(function (db) { - dbContext.db = db; - for (var j = 0; j < forages.length; j++) { - var _forage2 = forages[j]; - _forage2._dbInfo.db = db; - _advanceReadiness(_forage2._dbInfo); - } - })["catch"](function (err) { - (_rejectReadiness(options, err) || Promise$1.resolve())["catch"](function () {}); - throw err; - }); - }); - } - } + function key(n, callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + if (n < 0) { + resolve(null); + + return; + } + + self.ready().then(function () { + createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { + if (err) { + return reject(err); + } + + try { + var store = transaction.objectStore(self._dbInfo.storeName); + var advanced = false; + var req = store.openKeyCursor(); + + req.onsuccess = function () { + var cursor = req.result; + if (!cursor) { + // this means there weren't enough keys + resolve(null); + + return; + } + + if (n === 0) { + // We have the first key, return it if that's what they + // wanted. + resolve(cursor.key); + } else { + if (!advanced) { + // Otherwise, ask the cursor to skip ahead n + // records. + advanced = true; + cursor.advance(n); + } else { + // When we get here, we've got the nth key. + resolve(cursor.key); + } + } + }; + + req.onerror = function () { + reject(req.error); + }; + } catch (e) { + reject(e); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - executeCallback(promise, callback); - return promise; -} + function keys(callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + createTransaction(self._dbInfo, READ_ONLY, function (err, transaction) { + if (err) { + return reject(err); + } + + try { + var store = transaction.objectStore(self._dbInfo.storeName); + var req = store.openKeyCursor(); + var keys = []; + + req.onsuccess = function () { + var cursor = req.result; + + if (!cursor) { + resolve(keys); + return; + } + + keys.push(cursor.key); + cursor["continue"](); + }; + + req.onerror = function () { + reject(req.error); + }; + } catch (e) { + reject(e); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } -var asyncStorage = { - _driver: 'asyncStorage', - _initStorage: _initStorage, - _support: isIndexedDBValid(), - iterate: iterate, - getItem: getItem, - setItem: setItem, - removeItem: removeItem, - clear: clear, - length: length, - key: key, - keys: keys, - dropInstance: dropInstance -}; + function dropInstance(options, callback) { + callback = getCallback.apply(this, arguments); + + var currentConfig = this.config(); + options = typeof options !== 'function' && options || {}; + if (!options.name) { + options.name = options.name || currentConfig.name; + options.storeName = options.storeName || currentConfig.storeName; + } + + var self = this; + var promise; + if (!options.name) { + promise = Promise$1.reject('Invalid arguments'); + } else { + var isCurrentDb = options.name === currentConfig.name && self._dbInfo.db; + + var dbPromise = isCurrentDb ? Promise$1.resolve(self._dbInfo.db) : _getOriginalConnection(options).then(function (db) { + var dbContext = dbContexts[options.name]; + var forages = dbContext.forages; + dbContext.db = db; + for (var i = 0; i < forages.length; i++) { + forages[i]._dbInfo.db = db; + } + return db; + }); + + if (!options.storeName) { + promise = dbPromise.then(function (db) { + _deferReadiness(options); + + var dbContext = dbContexts[options.name]; + var forages = dbContext.forages; + + db.close(); + for (var i = 0; i < forages.length; i++) { + var forage = forages[i]; + forage._dbInfo.db = null; + } + + var dropDBPromise = new Promise$1(function (resolve, reject) { + var req = idb.deleteDatabase(options.name); + + req.onerror = function () { + var db = req.result; + if (db) { + db.close(); + } + reject(req.error); + }; + + req.onblocked = function () { + // Closing all open connections in onversionchange handler should prevent this situation, but if + // we do get here, it just means the request remains pending - eventually it will succeed or error + console.warn('dropInstance blocked for database "' + options.name + '" until all open connections are closed'); + }; + + req.onsuccess = function () { + var db = req.result; + if (db) { + db.close(); + } + resolve(db); + }; + }); + + return dropDBPromise.then(function (db) { + dbContext.db = db; + for (var i = 0; i < forages.length; i++) { + var _forage = forages[i]; + _advanceReadiness(_forage._dbInfo); + } + })["catch"](function (err) { + (_rejectReadiness(options, err) || Promise$1.resolve())["catch"](function () {}); + throw err; + }); + }); + } else { + promise = dbPromise.then(function (db) { + if (!db.objectStoreNames.contains(options.storeName)) { + return; + } + + var newVersion = db.version + 1; + + _deferReadiness(options); + + var dbContext = dbContexts[options.name]; + var forages = dbContext.forages; + + db.close(); + for (var i = 0; i < forages.length; i++) { + var forage = forages[i]; + forage._dbInfo.db = null; + forage._dbInfo.version = newVersion; + } + + var dropObjectPromise = new Promise$1(function (resolve, reject) { + var req = idb.open(options.name, newVersion); + + req.onerror = function (err) { + var db = req.result; + db.close(); + reject(err); + }; + + req.onupgradeneeded = function () { + var db = req.result; + db.deleteObjectStore(options.storeName); + }; + + req.onsuccess = function () { + var db = req.result; + db.close(); + resolve(db); + }; + }); + + return dropObjectPromise.then(function (db) { + dbContext.db = db; + for (var j = 0; j < forages.length; j++) { + var _forage2 = forages[j]; + _forage2._dbInfo.db = db; + _advanceReadiness(_forage2._dbInfo); + } + })["catch"](function (err) { + (_rejectReadiness(options, err) || Promise$1.resolve())["catch"](function () {}); + throw err; + }); + }); + } + } + + executeCallback(promise, callback); + return promise; + } -function isWebSQLValid() { - return typeof openDatabase === 'function'; -} - -// Sadly, the best way to save binary data in WebSQL/localStorage is serializing -// it to Base64, so this is how we store it to prevent very strange errors with less -// verbose ways of binary <-> string data storage. -var BASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - -var BLOB_TYPE_PREFIX = '~~local_forage_type~'; -var BLOB_TYPE_PREFIX_REGEX = /^~~local_forage_type~([^~]+)~/; - -var SERIALIZED_MARKER = '__lfsc__:'; -var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length; - -// OMG the serializations! -var TYPE_ARRAYBUFFER = 'arbf'; -var TYPE_BLOB = 'blob'; -var TYPE_INT8ARRAY = 'si08'; -var TYPE_UINT8ARRAY = 'ui08'; -var TYPE_UINT8CLAMPEDARRAY = 'uic8'; -var TYPE_INT16ARRAY = 'si16'; -var TYPE_INT32ARRAY = 'si32'; -var TYPE_UINT16ARRAY = 'ur16'; -var TYPE_UINT32ARRAY = 'ui32'; -var TYPE_FLOAT32ARRAY = 'fl32'; -var TYPE_FLOAT64ARRAY = 'fl64'; -var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + TYPE_ARRAYBUFFER.length; - -var toString$1 = Object.prototype.toString; - -function stringToBuffer(serializedString) { - // Fill the string into a ArrayBuffer. - var bufferLength = serializedString.length * 0.75; - var len = serializedString.length; - var i; - var p = 0; - var encoded1, encoded2, encoded3, encoded4; - - if (serializedString[serializedString.length - 1] === '=') { - bufferLength--; - if (serializedString[serializedString.length - 2] === '=') { - bufferLength--; - } - } + var asyncStorage = { + _driver: 'asyncStorage', + _initStorage: _initStorage, + _support: isIndexedDBValid(), + iterate: iterate, + getItem: getItem, + setItem: setItem, + removeItem: removeItem, + clear: clear, + length: length, + key: key, + keys: keys, + dropInstance: dropInstance + }; - var buffer = new ArrayBuffer(bufferLength); - var bytes = new Uint8Array(buffer); + function isWebSQLValid() { + return typeof openDatabase === 'function'; + } - for (i = 0; i < len; i += 4) { - encoded1 = BASE_CHARS.indexOf(serializedString[i]); - encoded2 = BASE_CHARS.indexOf(serializedString[i + 1]); - encoded3 = BASE_CHARS.indexOf(serializedString[i + 2]); - encoded4 = BASE_CHARS.indexOf(serializedString[i + 3]); + // Sadly, the best way to save binary data in WebSQL/localStorage is serializing + // it to Base64, so this is how we store it to prevent very strange errors with less + // verbose ways of binary <-> string data storage. + var BASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + + var BLOB_TYPE_PREFIX = '~~local_forage_type~'; + var BLOB_TYPE_PREFIX_REGEX = /^~~local_forage_type~([^~]+)~/; + + var SERIALIZED_MARKER = '__lfsc__:'; + var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length; + + // OMG the serializations! + var TYPE_ARRAYBUFFER = 'arbf'; + var TYPE_BLOB = 'blob'; + var TYPE_INT8ARRAY = 'si08'; + var TYPE_UINT8ARRAY = 'ui08'; + var TYPE_UINT8CLAMPEDARRAY = 'uic8'; + var TYPE_INT16ARRAY = 'si16'; + var TYPE_INT32ARRAY = 'si32'; + var TYPE_UINT16ARRAY = 'ur16'; + var TYPE_UINT32ARRAY = 'ui32'; + var TYPE_FLOAT32ARRAY = 'fl32'; + var TYPE_FLOAT64ARRAY = 'fl64'; + var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + TYPE_ARRAYBUFFER.length; + + var toString$1 = Object.prototype.toString; + + function stringToBuffer(serializedString) { + // Fill the string into a ArrayBuffer. + var bufferLength = serializedString.length * 0.75; + var len = serializedString.length; + var i; + var p = 0; + var encoded1, encoded2, encoded3, encoded4; + + if (serializedString[serializedString.length - 1] === '=') { + bufferLength--; + if (serializedString[serializedString.length - 2] === '=') { + bufferLength--; + } + } + + var buffer = new ArrayBuffer(bufferLength); + var bytes = new Uint8Array(buffer); + + for (i = 0; i < len; i += 4) { + encoded1 = BASE_CHARS.indexOf(serializedString[i]); + encoded2 = BASE_CHARS.indexOf(serializedString[i + 1]); + encoded3 = BASE_CHARS.indexOf(serializedString[i + 2]); + encoded4 = BASE_CHARS.indexOf(serializedString[i + 3]); + + /*jslint bitwise: true */ + bytes[p++] = encoded1 << 2 | encoded2 >> 4; + bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2; + bytes[p++] = (encoded3 & 3) << 6 | encoded4 & 63; + } + return buffer; + } - /*jslint bitwise: true */ - bytes[p++] = encoded1 << 2 | encoded2 >> 4; - bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2; - bytes[p++] = (encoded3 & 3) << 6 | encoded4 & 63; - } - return buffer; -} + // Converts a buffer to a string to store, serialized, in the backend + // storage library. + function bufferToString(buffer) { + // base64-arraybuffer + var bytes = new Uint8Array(buffer); + var base64String = ''; + var i; + + for (i = 0; i < bytes.length; i += 3) { + /*jslint bitwise: true */ + base64String += BASE_CHARS[bytes[i] >> 2]; + base64String += BASE_CHARS[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4]; + base64String += BASE_CHARS[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6]; + base64String += BASE_CHARS[bytes[i + 2] & 63]; + } + + if (bytes.length % 3 === 2) { + base64String = base64String.substring(0, base64String.length - 1) + '='; + } else if (bytes.length % 3 === 1) { + base64String = base64String.substring(0, base64String.length - 2) + '=='; + } + + return base64String; + } -// Converts a buffer to a string to store, serialized, in the backend -// storage library. -function bufferToString(buffer) { - // base64-arraybuffer - var bytes = new Uint8Array(buffer); - var base64String = ''; - var i; + // Serialize a value, afterwards executing a callback (which usually + // instructs the `setItem()` callback/promise to be executed). This is how + // we store binary data with localStorage. + function serialize(value, callback) { + var valueType = ''; + if (value) { + valueType = toString$1.call(value); + } + + // Cannot use `value instanceof ArrayBuffer` or such here, as these + // checks fail when running the tests using casper.js... + // + // TODO: See why those tests fail and use a better solution. + if (value && (valueType === '[object ArrayBuffer]' || value.buffer && toString$1.call(value.buffer) === '[object ArrayBuffer]')) { + // Convert binary arrays to a string and prefix the string with + // a special marker. + var buffer; + var marker = SERIALIZED_MARKER; + + if (value instanceof ArrayBuffer) { + buffer = value; + marker += TYPE_ARRAYBUFFER; + } else { + buffer = value.buffer; + + if (valueType === '[object Int8Array]') { + marker += TYPE_INT8ARRAY; + } else if (valueType === '[object Uint8Array]') { + marker += TYPE_UINT8ARRAY; + } else if (valueType === '[object Uint8ClampedArray]') { + marker += TYPE_UINT8CLAMPEDARRAY; + } else if (valueType === '[object Int16Array]') { + marker += TYPE_INT16ARRAY; + } else if (valueType === '[object Uint16Array]') { + marker += TYPE_UINT16ARRAY; + } else if (valueType === '[object Int32Array]') { + marker += TYPE_INT32ARRAY; + } else if (valueType === '[object Uint32Array]') { + marker += TYPE_UINT32ARRAY; + } else if (valueType === '[object Float32Array]') { + marker += TYPE_FLOAT32ARRAY; + } else if (valueType === '[object Float64Array]') { + marker += TYPE_FLOAT64ARRAY; + } else { + callback(new Error('Failed to get type for BinaryArray')); + } + } + + callback(marker + bufferToString(buffer)); + } else if (valueType === '[object Blob]') { + // Conver the blob to a binaryArray and then to a string. + var fileReader = new FileReader(); + + fileReader.onload = function () { + // Backwards-compatible prefix for the blob type. + var str = BLOB_TYPE_PREFIX + value.type + '~' + bufferToString(this.result); + + callback(SERIALIZED_MARKER + TYPE_BLOB + str); + }; + + fileReader.readAsArrayBuffer(value); + } else { + try { + callback(JSON.stringify(value)); + } catch (e) { + console.error("Couldn't convert value into a JSON string: ", value); + + callback(null, e); + } + } + } - for (i = 0; i < bytes.length; i += 3) { - /*jslint bitwise: true */ - base64String += BASE_CHARS[bytes[i] >> 2]; - base64String += BASE_CHARS[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4]; - base64String += BASE_CHARS[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6]; - base64String += BASE_CHARS[bytes[i + 2] & 63]; - } + // Deserialize data we've inserted into a value column/field. We place + // special markers into our strings to mark them as encoded; this isn't + // as nice as a meta field, but it's the only sane thing we can do whilst + // keeping localStorage support intact. + // + // Oftentimes this will just deserialize JSON content, but if we have a + // special marker (SERIALIZED_MARKER, defined above), we will extract + // some kind of arraybuffer/binary data/typed array out of the string. + function deserialize(value) { + // If we haven't marked this string as being specially serialized (i.e. + // something other than serialized JSON), we can just return it and be + // done with it. + if (value.substring(0, SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER) { + return JSON.parse(value); + } + + // The following code deals with deserializing some kind of Blob or + // TypedArray. First we separate out the type of data we're dealing + // with from the data itself. + var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH); + var type = value.substring(SERIALIZED_MARKER_LENGTH, TYPE_SERIALIZED_MARKER_LENGTH); + + var blobType; + // Backwards-compatible blob type serialization strategy. + // DBs created with older versions of localForage will simply not have the blob type. + if (type === TYPE_BLOB && BLOB_TYPE_PREFIX_REGEX.test(serializedString)) { + var matcher = serializedString.match(BLOB_TYPE_PREFIX_REGEX); + blobType = matcher[1]; + serializedString = serializedString.substring(matcher[0].length); + } + var buffer = stringToBuffer(serializedString); + + // Return the right type based on the code/type set during + // serialization. + switch (type) { + case TYPE_ARRAYBUFFER: + return buffer; + case TYPE_BLOB: + return createBlob([buffer], { type: blobType }); + case TYPE_INT8ARRAY: + return new Int8Array(buffer); + case TYPE_UINT8ARRAY: + return new Uint8Array(buffer); + case TYPE_UINT8CLAMPEDARRAY: + return new Uint8ClampedArray(buffer); + case TYPE_INT16ARRAY: + return new Int16Array(buffer); + case TYPE_UINT16ARRAY: + return new Uint16Array(buffer); + case TYPE_INT32ARRAY: + return new Int32Array(buffer); + case TYPE_UINT32ARRAY: + return new Uint32Array(buffer); + case TYPE_FLOAT32ARRAY: + return new Float32Array(buffer); + case TYPE_FLOAT64ARRAY: + return new Float64Array(buffer); + default: + throw new Error('Unkown type: ' + type); + } + } - if (bytes.length % 3 === 2) { - base64String = base64String.substring(0, base64String.length - 1) + '='; - } else if (bytes.length % 3 === 1) { - base64String = base64String.substring(0, base64String.length - 2) + '=='; - } + var localforageSerializer = { + serialize: serialize, + deserialize: deserialize, + stringToBuffer: stringToBuffer, + bufferToString: bufferToString + }; - return base64String; -} + /* + * Includes code from: + * + * base64-arraybuffer + * https://github.com/niklasvh/base64-arraybuffer + * + * Copyright (c) 2012 Niklas von Hertzen + * Licensed under the MIT license. + */ + + function createDbTable(t, dbInfo, callback, errorCallback) { + t.executeSql('CREATE TABLE IF NOT EXISTS ' + dbInfo.storeName + ' ' + '(id INTEGER PRIMARY KEY, key unique, value)', [], callback, errorCallback); + } -// Serialize a value, afterwards executing a callback (which usually -// instructs the `setItem()` callback/promise to be executed). This is how -// we store binary data with localStorage. -function serialize(value, callback) { - var valueType = ''; - if (value) { - valueType = toString$1.call(value); - } + // Open the WebSQL database (automatically creates one if one didn't + // previously exist), using any options set in the config. + function _initStorage$1(options) { + var self = this; + var dbInfo = { + db: null + }; + + if (options) { + for (var i in options) { + dbInfo[i] = typeof options[i] !== 'string' ? options[i].toString() : options[i]; + } + } + + var dbInfoPromise = new Promise$1(function (resolve, reject) { + // Open the database; the openDatabase API will automatically + // create it for us if it doesn't exist. + try { + dbInfo.db = openDatabase(dbInfo.name, String(dbInfo.version), dbInfo.description, dbInfo.size); + } catch (e) { + return reject(e); + } + + // Create our key/value table if it doesn't exist. + dbInfo.db.transaction(function (t) { + createDbTable(t, dbInfo, function () { + self._dbInfo = dbInfo; + resolve(); + }, function (t, error) { + reject(error); + }); + }, reject); + }); + + dbInfo.serializer = localforageSerializer; + return dbInfoPromise; + } - // Cannot use `value instanceof ArrayBuffer` or such here, as these - // checks fail when running the tests using casper.js... - // - // TODO: See why those tests fail and use a better solution. - if (value && (valueType === '[object ArrayBuffer]' || value.buffer && toString$1.call(value.buffer) === '[object ArrayBuffer]')) { - // Convert binary arrays to a string and prefix the string with - // a special marker. - var buffer; - var marker = SERIALIZED_MARKER; + function tryExecuteSql(t, dbInfo, sqlStatement, args, callback, errorCallback) { + t.executeSql(sqlStatement, args, callback, function (t, error) { + if (error.code === error.SYNTAX_ERR) { + t.executeSql('SELECT name FROM sqlite_master ' + "WHERE type='table' AND name = ?", [dbInfo.storeName], function (t, results) { + if (!results.rows.length) { + // if the table is missing (was deleted) + // re-create it table and retry + createDbTable(t, dbInfo, function () { + t.executeSql(sqlStatement, args, callback, errorCallback); + }, errorCallback); + } else { + errorCallback(t, error); + } + }, errorCallback); + } else { + errorCallback(t, error); + } + }, errorCallback); + } - if (value instanceof ArrayBuffer) { - buffer = value; - marker += TYPE_ARRAYBUFFER; - } else { - buffer = value.buffer; - - if (valueType === '[object Int8Array]') { - marker += TYPE_INT8ARRAY; - } else if (valueType === '[object Uint8Array]') { - marker += TYPE_UINT8ARRAY; - } else if (valueType === '[object Uint8ClampedArray]') { - marker += TYPE_UINT8CLAMPEDARRAY; - } else if (valueType === '[object Int16Array]') { - marker += TYPE_INT16ARRAY; - } else if (valueType === '[object Uint16Array]') { - marker += TYPE_UINT16ARRAY; - } else if (valueType === '[object Int32Array]') { - marker += TYPE_INT32ARRAY; - } else if (valueType === '[object Uint32Array]') { - marker += TYPE_UINT32ARRAY; - } else if (valueType === '[object Float32Array]') { - marker += TYPE_FLOAT32ARRAY; - } else if (valueType === '[object Float64Array]') { - marker += TYPE_FLOAT64ARRAY; - } else { - callback(new Error('Failed to get type for BinaryArray')); - } - } + function getItem$1(key, callback) { + var self = this; + + key = normalizeKey(key); + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + var dbInfo = self._dbInfo; + dbInfo.db.transaction(function (t) { + tryExecuteSql(t, dbInfo, 'SELECT * FROM ' + dbInfo.storeName + ' WHERE key = ? LIMIT 1', [key], function (t, results) { + var result = results.rows.length ? results.rows.item(0).value : null; + + // Check to see if this is serialized content we need to + // unpack. + if (result) { + result = dbInfo.serializer.deserialize(result); + } + + resolve(result); + }, function (t, error) { + reject(error); + }); + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - callback(marker + bufferToString(buffer)); - } else if (valueType === '[object Blob]') { - // Conver the blob to a binaryArray and then to a string. - var fileReader = new FileReader(); + function iterate$1(iterator, callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + var dbInfo = self._dbInfo; + + dbInfo.db.transaction(function (t) { + tryExecuteSql(t, dbInfo, 'SELECT * FROM ' + dbInfo.storeName, [], function (t, results) { + var rows = results.rows; + var length = rows.length; + + for (var i = 0; i < length; i++) { + var item = rows.item(i); + var result = item.value; + + // Check to see if this is serialized content + // we need to unpack. + if (result) { + result = dbInfo.serializer.deserialize(result); + } + + result = iterator(result, item.key, i + 1); + + // void(0) prevents problems with redefinition + // of `undefined`. + if (result !== void 0) { + resolve(result); + return; + } + } + + resolve(); + }, function (t, error) { + reject(error); + }); + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - fileReader.onload = function () { - // Backwards-compatible prefix for the blob type. - var str = BLOB_TYPE_PREFIX + value.type + '~' + bufferToString(this.result); + function _setItem(key, value, callback, retriesLeft) { + var self = this; + + key = normalizeKey(key); + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + // The localStorage API doesn't return undefined values in an + // "expected" way, so undefined is always cast to null in all + // drivers. See: https://github.com/mozilla/localForage/pull/42 + if (value === undefined) { + value = null; + } + + // Save the original value to pass to the callback. + var originalValue = value; + + var dbInfo = self._dbInfo; + dbInfo.serializer.serialize(value, function (value, error) { + if (error) { + reject(error); + } else { + dbInfo.db.transaction(function (t) { + tryExecuteSql(t, dbInfo, 'INSERT OR REPLACE INTO ' + dbInfo.storeName + ' ' + '(key, value) VALUES (?, ?)', [key, value], function () { + resolve(originalValue); + }, function (t, error) { + reject(error); + }); + }, function (sqlError) { + // The transaction failed; check + // to see if it's a quota error. + if (sqlError.code === sqlError.QUOTA_ERR) { + // We reject the callback outright for now, but + // it's worth trying to re-run the transaction. + // Even if the user accepts the prompt to use + // more storage on Safari, this error will + // be called. + // + // Try to re-run the transaction. + if (retriesLeft > 0) { + resolve(_setItem.apply(self, [key, originalValue, callback, retriesLeft - 1])); + return; + } + reject(sqlError); + } + }); + } + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - callback(SERIALIZED_MARKER + TYPE_BLOB + str); - }; + function setItem$1(key, value, callback) { + return _setItem.apply(this, [key, value, callback, 1]); + } - fileReader.readAsArrayBuffer(value); - } else { - try { - callback(JSON.stringify(value)); - } catch (e) { - console.error("Couldn't convert value into a JSON string: ", value); + function removeItem$1(key, callback) { + var self = this; + + key = normalizeKey(key); + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + var dbInfo = self._dbInfo; + dbInfo.db.transaction(function (t) { + tryExecuteSql(t, dbInfo, 'DELETE FROM ' + dbInfo.storeName + ' WHERE key = ?', [key], function () { + resolve(); + }, function (t, error) { + reject(error); + }); + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } - callback(null, e); - } - } -} + // Deletes every item in the table. + // TODO: Find out if this resets the AUTO_INCREMENT number. + function clear$1(callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + var dbInfo = self._dbInfo; + dbInfo.db.transaction(function (t) { + tryExecuteSql(t, dbInfo, 'DELETE FROM ' + dbInfo.storeName, [], function () { + resolve(); + }, function (t, error) { + reject(error); + }); + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } -// Deserialize data we've inserted into a value column/field. We place -// special markers into our strings to mark them as encoded; this isn't -// as nice as a meta field, but it's the only sane thing we can do whilst -// keeping localStorage support intact. -// -// Oftentimes this will just deserialize JSON content, but if we have a -// special marker (SERIALIZED_MARKER, defined above), we will extract -// some kind of arraybuffer/binary data/typed array out of the string. -function deserialize(value) { - // If we haven't marked this string as being specially serialized (i.e. - // something other than serialized JSON), we can just return it and be - // done with it. - if (value.substring(0, SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER) { - return JSON.parse(value); - } - - // The following code deals with deserializing some kind of Blob or - // TypedArray. First we separate out the type of data we're dealing - // with from the data itself. - var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH); - var type = value.substring(SERIALIZED_MARKER_LENGTH, TYPE_SERIALIZED_MARKER_LENGTH); - - var blobType; - // Backwards-compatible blob type serialization strategy. - // DBs created with older versions of localForage will simply not have the blob type. - if (type === TYPE_BLOB && BLOB_TYPE_PREFIX_REGEX.test(serializedString)) { - var matcher = serializedString.match(BLOB_TYPE_PREFIX_REGEX); - blobType = matcher[1]; - serializedString = serializedString.substring(matcher[0].length); - } - var buffer = stringToBuffer(serializedString); - - // Return the right type based on the code/type set during - // serialization. - switch (type) { - case TYPE_ARRAYBUFFER: - return buffer; - case TYPE_BLOB: - return createBlob([buffer], { type: blobType }); - case TYPE_INT8ARRAY: - return new Int8Array(buffer); - case TYPE_UINT8ARRAY: - return new Uint8Array(buffer); - case TYPE_UINT8CLAMPEDARRAY: - return new Uint8ClampedArray(buffer); - case TYPE_INT16ARRAY: - return new Int16Array(buffer); - case TYPE_UINT16ARRAY: - return new Uint16Array(buffer); - case TYPE_INT32ARRAY: - return new Int32Array(buffer); - case TYPE_UINT32ARRAY: - return new Uint32Array(buffer); - case TYPE_FLOAT32ARRAY: - return new Float32Array(buffer); - case TYPE_FLOAT64ARRAY: - return new Float64Array(buffer); - default: - throw new Error('Unkown type: ' + type); - } -} + // Does a simple `COUNT(key)` to get the number of items stored in + // localForage. + function length$1(callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + var dbInfo = self._dbInfo; + dbInfo.db.transaction(function (t) { + // Ahhh, SQL makes this one soooooo easy. + tryExecuteSql(t, dbInfo, 'SELECT COUNT(key) as c FROM ' + dbInfo.storeName, [], function (t, results) { + var result = results.rows.item(0).c; + resolve(result); + }, function (t, error) { + reject(error); + }); + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } -var localforageSerializer = { - serialize: serialize, - deserialize: deserialize, - stringToBuffer: stringToBuffer, - bufferToString: bufferToString -}; + // Return the key located at key index X; essentially gets the key from a + // `WHERE id = ?`. This is the most efficient way I can think to implement + // this rarely-used (in my experience) part of the API, but it can seem + // inconsistent, because we do `INSERT OR REPLACE INTO` on `setItem()`, so + // the ID of each key will change every time it's updated. Perhaps a stored + // procedure for the `setItem()` SQL would solve this problem? + // TODO: Don't change ID on `setItem()`. + function key$1(n, callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + var dbInfo = self._dbInfo; + dbInfo.db.transaction(function (t) { + tryExecuteSql(t, dbInfo, 'SELECT key FROM ' + dbInfo.storeName + ' WHERE id = ? LIMIT 1', [n + 1], function (t, results) { + var result = results.rows.length ? results.rows.item(0).key : null; + resolve(result); + }, function (t, error) { + reject(error); + }); + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } -/* - * Includes code from: - * - * base64-arraybuffer - * https://github.com/niklasvh/base64-arraybuffer - * - * Copyright (c) 2012 Niklas von Hertzen - * Licensed under the MIT license. - */ + function keys$1(callback) { + var self = this; + + var promise = new Promise$1(function (resolve, reject) { + self.ready().then(function () { + var dbInfo = self._dbInfo; + dbInfo.db.transaction(function (t) { + tryExecuteSql(t, dbInfo, 'SELECT key FROM ' + dbInfo.storeName, [], function (t, results) { + var keys = []; + + for (var i = 0; i < results.rows.length; i++) { + keys.push(results.rows.item(i).key); + } + + resolve(keys); + }, function (t, error) { + reject(error); + }); + }); + })["catch"](reject); + }); + + executeCallback(promise, callback); + return promise; + } -function createDbTable(t, dbInfo, callback, errorCallback) { - t.executeSql('CREATE TABLE IF NOT EXISTS ' + dbInfo.storeName + ' ' + '(id INTEGER PRIMARY KEY, key unique, value)', [], callback, errorCallback); -} + // https://www.w3.org/TR/webdatabase/#databases + // > There is no way to enumerate or delete the databases available for an origin from this API. + function getAllStoreNames(db) { + return new Promise$1(function (resolve, reject) { + db.transaction(function (t) { + t.executeSql('SELECT name FROM sqlite_master ' + "WHERE type='table' AND name <> '__WebKitDatabaseInfoTable__'", [], function (t, results) { + var storeNames = []; + + for (var i = 0; i < results.rows.length; i++) { + storeNames.push(results.rows.item(i).name); + } + + resolve({ + db: db, + storeNames: storeNames + }); + }, function (t, error) { + reject(error); + }); + }, function (sqlError) { + reject(sqlError); + }); + }); + } -// Open the WebSQL database (automatically creates one if one didn't -// previously exist), using any options set in the config. -function _initStorage$1(options) { - var self = this; - var dbInfo = { - db: null - }; + function dropInstance$1(options, callback) { + callback = getCallback.apply(this, arguments); + + var currentConfig = this.config(); + options = typeof options !== 'function' && options || {}; + if (!options.name) { + options.name = options.name || currentConfig.name; + options.storeName = options.storeName || currentConfig.storeName; + } + + var self = this; + var promise; + if (!options.name) { + promise = Promise$1.reject('Invalid arguments'); + } else { + promise = new Promise$1(function (resolve) { + var db; + if (options.name === currentConfig.name) { + // use the db reference of the current instance + db = self._dbInfo.db; + } else { + db = openDatabase(options.name, '', '', 0); + } + + if (!options.storeName) { + // drop all database tables + resolve(getAllStoreNames(db)); + } else { + resolve({ + db: db, + storeNames: [options.storeName] + }); + } + }).then(function (operationInfo) { + return new Promise$1(function (resolve, reject) { + operationInfo.db.transaction(function (t) { + function dropTable(storeName) { + return new Promise$1(function (resolve, reject) { + t.executeSql('DROP TABLE IF EXISTS ' + storeName, [], function () { + resolve(); + }, function (t, error) { + reject(error); + }); + }); + } + + var operations = []; + for (var i = 0, len = operationInfo.storeNames.length; i < len; i++) { + operations.push(dropTable(operationInfo.storeNames[i])); + } + + Promise$1.all(operations).then(function () { + resolve(); + })["catch"](function (e) { + reject(e); + }); + }, function (sqlError) { + reject(sqlError); + }); + }); + }); + } + + executeCallback(promise, callback); + return promise; + } - if (options) { - for (var i in options) { - dbInfo[i] = typeof options[i] !== 'string' ? options[i].toString() : options[i]; - } - } + var webSQLStorage = { + _driver: 'webSQLStorage', + _initStorage: _initStorage$1, + _support: isWebSQLValid(), + iterate: iterate$1, + getItem: getItem$1, + setItem: setItem$1, + removeItem: removeItem$1, + clear: clear$1, + length: length$1, + key: key$1, + keys: keys$1, + dropInstance: dropInstance$1 + }; - var dbInfoPromise = new Promise$1(function (resolve, reject) { - // Open the database; the openDatabase API will automatically - // create it for us if it doesn't exist. - try { - dbInfo.db = openDatabase(dbInfo.name, String(dbInfo.version), dbInfo.description, dbInfo.size); - } catch (e) { - return reject(e); - } + function isLocalStorageValid() { + try { + return typeof localStorage !== 'undefined' && 'setItem' in localStorage && + // in IE8 typeof localStorage.setItem === 'object' + !!localStorage.setItem; + } catch (e) { + return false; + } + } - // Create our key/value table if it doesn't exist. - dbInfo.db.transaction(function (t) { - createDbTable(t, dbInfo, function () { - self._dbInfo = dbInfo; - resolve(); - }, function (t, error) { - reject(error); - }); - }, reject); - }); + function _getKeyPrefix(options, defaultConfig) { + var keyPrefix = options.name + '/'; - dbInfo.serializer = localforageSerializer; - return dbInfoPromise; -} - -function tryExecuteSql(t, dbInfo, sqlStatement, args, callback, errorCallback) { - t.executeSql(sqlStatement, args, callback, function (t, error) { - if (error.code === error.SYNTAX_ERR) { - t.executeSql('SELECT name FROM sqlite_master ' + "WHERE type='table' AND name = ?", [dbInfo.storeName], function (t, results) { - if (!results.rows.length) { - // if the table is missing (was deleted) - // re-create it table and retry - createDbTable(t, dbInfo, function () { - t.executeSql(sqlStatement, args, callback, errorCallback); - }, errorCallback); - } else { - errorCallback(t, error); - } - }, errorCallback); - } else { - errorCallback(t, error); - } - }, errorCallback); -} + if (options.storeName !== defaultConfig.storeName) { + keyPrefix += options.storeName + '/'; + } + return keyPrefix; + } -function getItem$1(key, callback) { - var self = this; + // Check if localStorage throws when saving an item + function checkIfLocalStorageThrows() { + var localStorageTestKey = '_localforage_support_test'; - key = normalizeKey(key); + try { + localStorage.setItem(localStorageTestKey, true); + localStorage.removeItem(localStorageTestKey); - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - var dbInfo = self._dbInfo; - dbInfo.db.transaction(function (t) { - tryExecuteSql(t, dbInfo, 'SELECT * FROM ' + dbInfo.storeName + ' WHERE key = ? LIMIT 1', [key], function (t, results) { - var result = results.rows.length ? results.rows.item(0).value : null; + return false; + } catch (e) { + return true; + } + } - // Check to see if this is serialized content we need to - // unpack. - if (result) { - result = dbInfo.serializer.deserialize(result); - } + // Check if localStorage is usable and allows to save an item + // This method checks if localStorage is usable in Safari Private Browsing + // mode, or in any other case where the available quota for localStorage + // is 0 and there wasn't any saved items yet. + function _isLocalStorageUsable() { + return !checkIfLocalStorageThrows() || localStorage.length > 0; + } - resolve(result); - }, function (t, error) { - reject(error); - }); - }); - })["catch"](reject); - }); + // Config the localStorage backend, using options set in the config. + function _initStorage$2(options) { + var self = this; + var dbInfo = {}; + if (options) { + for (var i in options) { + dbInfo[i] = options[i]; + } + } - executeCallback(promise, callback); - return promise; -} + dbInfo.keyPrefix = _getKeyPrefix(options, self._defaultConfig); -function iterate$1(iterator, callback) { - var self = this; + if (!_isLocalStorageUsable()) { + return Promise$1.reject(); + } - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - var dbInfo = self._dbInfo; + self._dbInfo = dbInfo; + dbInfo.serializer = localforageSerializer; - dbInfo.db.transaction(function (t) { - tryExecuteSql(t, dbInfo, 'SELECT * FROM ' + dbInfo.storeName, [], function (t, results) { - var rows = results.rows; - var length = rows.length; + return Promise$1.resolve(); + } - for (var i = 0; i < length; i++) { - var item = rows.item(i); - var result = item.value; + // Remove all keys from the datastore, effectively destroying all data in + // the app's key/value store! + function clear$2(callback) { + var self = this; + var promise = self.ready().then(function () { + var keyPrefix = self._dbInfo.keyPrefix; - // Check to see if this is serialized content - // we need to unpack. - if (result) { - result = dbInfo.serializer.deserialize(result); - } + for (var i = localStorage.length - 1; i >= 0; i--) { + var key = localStorage.key(i); - result = iterator(result, item.key, i + 1); + if (key.indexOf(keyPrefix) === 0) { + localStorage.removeItem(key); + } + } + }); - // void(0) prevents problems with redefinition - // of `undefined`. - if (result !== void 0) { - resolve(result); - return; - } - } + executeCallback(promise, callback); + return promise; + } - resolve(); - }, function (t, error) { - reject(error); - }); - }); - })["catch"](reject); - }); + // Retrieve an item from the store. Unlike the original async_storage + // library in Gaia, we don't modify return values at all. If a key's value + // is `undefined`, we pass that value to the callback function. + function getItem$2(key, callback) { + var self = this; - executeCallback(promise, callback); - return promise; -} + key = normalizeKey(key); -function _setItem(key, value, callback, retriesLeft) { - var self = this; + var promise = self.ready().then(function () { + var dbInfo = self._dbInfo; + var result = localStorage.getItem(dbInfo.keyPrefix + key); - key = normalizeKey(key); + // If a result was found, parse it from the serialized + // string into a JS object. If result isn't truthy, the key + // is likely undefined and we'll pass it straight to the + // callback. + if (result) { + result = dbInfo.serializer.deserialize(result); + } - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - // The localStorage API doesn't return undefined values in an - // "expected" way, so undefined is always cast to null in all - // drivers. See: https://github.com/mozilla/localForage/pull/42 - if (value === undefined) { - value = null; - } + return result; + }); - // Save the original value to pass to the callback. - var originalValue = value; - - var dbInfo = self._dbInfo; - dbInfo.serializer.serialize(value, function (value, error) { - if (error) { - reject(error); - } else { - dbInfo.db.transaction(function (t) { - tryExecuteSql(t, dbInfo, 'INSERT OR REPLACE INTO ' + dbInfo.storeName + ' ' + '(key, value) VALUES (?, ?)', [key, value], function () { - resolve(originalValue); - }, function (t, error) { - reject(error); - }); - }, function (sqlError) { - // The transaction failed; check - // to see if it's a quota error. - if (sqlError.code === sqlError.QUOTA_ERR) { - // We reject the callback outright for now, but - // it's worth trying to re-run the transaction. - // Even if the user accepts the prompt to use - // more storage on Safari, this error will - // be called. - // - // Try to re-run the transaction. - if (retriesLeft > 0) { - resolve(_setItem.apply(self, [key, originalValue, callback, retriesLeft - 1])); - return; - } - reject(sqlError); - } - }); - } - }); - })["catch"](reject); - }); + executeCallback(promise, callback); + return promise; + } - executeCallback(promise, callback); - return promise; -} + // Iterate over all items in the store. + function iterate$2(iterator, callback) { + var self = this; + + var promise = self.ready().then(function () { + var dbInfo = self._dbInfo; + var keyPrefix = dbInfo.keyPrefix; + var keyPrefixLength = keyPrefix.length; + var length = localStorage.length; + + // We use a dedicated iterator instead of the `i` variable below + // so other keys we fetch in localStorage aren't counted in + // the `iterationNumber` argument passed to the `iterate()` + // callback. + // + // See: github.com/mozilla/localForage/pull/435#discussion_r38061530 + var iterationNumber = 1; + + for (var i = 0; i < length; i++) { + var key = localStorage.key(i); + if (key.indexOf(keyPrefix) !== 0) { + continue; + } + var value = localStorage.getItem(key); + + // If a result was found, parse it from the serialized + // string into a JS object. If result isn't truthy, the + // key is likely undefined and we'll pass it straight + // to the iterator. + if (value) { + value = dbInfo.serializer.deserialize(value); + } + + value = iterator(value, key.substring(keyPrefixLength), iterationNumber++); + + if (value !== void 0) { + return value; + } + } + }); + + executeCallback(promise, callback); + return promise; + } -function setItem$1(key, value, callback) { - return _setItem.apply(this, [key, value, callback, 1]); -} + // Same as localStorage's key() method, except takes a callback. + function key$2(n, callback) { + var self = this; + var promise = self.ready().then(function () { + var dbInfo = self._dbInfo; + var result; + try { + result = localStorage.key(n); + } catch (error) { + result = null; + } + + // Remove the prefix from the key, if a key is found. + if (result) { + result = result.substring(dbInfo.keyPrefix.length); + } + + return result; + }); + + executeCallback(promise, callback); + return promise; + } -function removeItem$1(key, callback) { - var self = this; + function keys$2(callback) { + var self = this; + var promise = self.ready().then(function () { + var dbInfo = self._dbInfo; + var length = localStorage.length; + var keys = []; + + for (var i = 0; i < length; i++) { + var itemKey = localStorage.key(i); + if (itemKey.indexOf(dbInfo.keyPrefix) === 0) { + keys.push(itemKey.substring(dbInfo.keyPrefix.length)); + } + } + + return keys; + }); + + executeCallback(promise, callback); + return promise; + } - key = normalizeKey(key); + // Supply the number of keys in the datastore to the callback function. + function length$2(callback) { + var self = this; + var promise = self.keys().then(function (keys) { + return keys.length; + }); - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - var dbInfo = self._dbInfo; - dbInfo.db.transaction(function (t) { - tryExecuteSql(t, dbInfo, 'DELETE FROM ' + dbInfo.storeName + ' WHERE key = ?', [key], function () { - resolve(); - }, function (t, error) { - reject(error); - }); - }); - })["catch"](reject); - }); + executeCallback(promise, callback); + return promise; + } - executeCallback(promise, callback); - return promise; -} + // Remove an item from the store, nice and simple. + function removeItem$2(key, callback) { + var self = this; -// Deletes every item in the table. -// TODO: Find out if this resets the AUTO_INCREMENT number. -function clear$1(callback) { - var self = this; + key = normalizeKey(key); - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - var dbInfo = self._dbInfo; - dbInfo.db.transaction(function (t) { - tryExecuteSql(t, dbInfo, 'DELETE FROM ' + dbInfo.storeName, [], function () { - resolve(); - }, function (t, error) { - reject(error); - }); - }); - })["catch"](reject); - }); + var promise = self.ready().then(function () { + var dbInfo = self._dbInfo; + localStorage.removeItem(dbInfo.keyPrefix + key); + }); - executeCallback(promise, callback); - return promise; -} - -// Does a simple `COUNT(key)` to get the number of items stored in -// localForage. -function length$1(callback) { - var self = this; - - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - var dbInfo = self._dbInfo; - dbInfo.db.transaction(function (t) { - // Ahhh, SQL makes this one soooooo easy. - tryExecuteSql(t, dbInfo, 'SELECT COUNT(key) as c FROM ' + dbInfo.storeName, [], function (t, results) { - var result = results.rows.item(0).c; - resolve(result); - }, function (t, error) { - reject(error); - }); - }); - })["catch"](reject); - }); + executeCallback(promise, callback); + return promise; + } - executeCallback(promise, callback); - return promise; -} - -// Return the key located at key index X; essentially gets the key from a -// `WHERE id = ?`. This is the most efficient way I can think to implement -// this rarely-used (in my experience) part of the API, but it can seem -// inconsistent, because we do `INSERT OR REPLACE INTO` on `setItem()`, so -// the ID of each key will change every time it's updated. Perhaps a stored -// procedure for the `setItem()` SQL would solve this problem? -// TODO: Don't change ID on `setItem()`. -function key$1(n, callback) { - var self = this; - - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - var dbInfo = self._dbInfo; - dbInfo.db.transaction(function (t) { - tryExecuteSql(t, dbInfo, 'SELECT key FROM ' + dbInfo.storeName + ' WHERE id = ? LIMIT 1', [n + 1], function (t, results) { - var result = results.rows.length ? results.rows.item(0).key : null; - resolve(result); - }, function (t, error) { - reject(error); - }); - }); - })["catch"](reject); - }); + // Set a key's value and run an optional callback once the value is set. + // Unlike Gaia's implementation, the callback function is passed the value, + // in case you want to operate on that value only after you're sure it + // saved, or something like that. + function setItem$2(key, value, callback) { + var self = this; + + key = normalizeKey(key); + + var promise = self.ready().then(function () { + // Convert undefined values to null. + // https://github.com/mozilla/localForage/pull/42 + if (value === undefined) { + value = null; + } + + // Save the original value to pass to the callback. + var originalValue = value; + + return new Promise$1(function (resolve, reject) { + var dbInfo = self._dbInfo; + dbInfo.serializer.serialize(value, function (value, error) { + if (error) { + reject(error); + } else { + try { + localStorage.setItem(dbInfo.keyPrefix + key, value); + resolve(originalValue); + } catch (e) { + // localStorage capacity exceeded. + // TODO: Make this a specific error/event. + if (e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') { + reject(e); + } + reject(e); + } + } + }); + }); + }); + + executeCallback(promise, callback); + return promise; + } - executeCallback(promise, callback); - return promise; -} + function dropInstance$2(options, callback) { + callback = getCallback.apply(this, arguments); + + options = typeof options !== 'function' && options || {}; + if (!options.name) { + var currentConfig = this.config(); + options.name = options.name || currentConfig.name; + options.storeName = options.storeName || currentConfig.storeName; + } + + var self = this; + var promise; + if (!options.name) { + promise = Promise$1.reject('Invalid arguments'); + } else { + promise = new Promise$1(function (resolve) { + if (!options.storeName) { + resolve(options.name + '/'); + } else { + resolve(_getKeyPrefix(options, self._defaultConfig)); + } + }).then(function (keyPrefix) { + for (var i = localStorage.length - 1; i >= 0; i--) { + var key = localStorage.key(i); + + if (key.indexOf(keyPrefix) === 0) { + localStorage.removeItem(key); + } + } + }); + } + + executeCallback(promise, callback); + return promise; + } -function keys$1(callback) { - var self = this; + var localStorageWrapper = { + _driver: 'localStorageWrapper', + _initStorage: _initStorage$2, + _support: isLocalStorageValid(), + iterate: iterate$2, + getItem: getItem$2, + setItem: setItem$2, + removeItem: removeItem$2, + clear: clear$2, + length: length$2, + key: key$2, + keys: keys$2, + dropInstance: dropInstance$2 + }; - var promise = new Promise$1(function (resolve, reject) { - self.ready().then(function () { - var dbInfo = self._dbInfo; - dbInfo.db.transaction(function (t) { - tryExecuteSql(t, dbInfo, 'SELECT key FROM ' + dbInfo.storeName, [], function (t, results) { - var keys = []; + var sameValue = function sameValue(x, y) { + return x === y || typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y); + }; - for (var i = 0; i < results.rows.length; i++) { - keys.push(results.rows.item(i).key); - } + var includes = function includes(array, searchElement) { + var len = array.length; + var i = 0; + while (i < len) { + if (sameValue(array[i], searchElement)) { + return true; + } + i++; + } + + return false; + }; - resolve(keys); - }, function (t, error) { - reject(error); - }); - }); - })["catch"](reject); - }); + var isArray = Array.isArray || function (arg) { + return Object.prototype.toString.call(arg) === '[object Array]'; + }; - executeCallback(promise, callback); - return promise; -} + // Drivers are stored here when `defineDriver()` is called. + // They are shared across all instances of localForage. + var DefinedDrivers = {}; -// https://www.w3.org/TR/webdatabase/#databases -// > There is no way to enumerate or delete the databases available for an origin from this API. -function getAllStoreNames(db) { - return new Promise$1(function (resolve, reject) { - db.transaction(function (t) { - t.executeSql('SELECT name FROM sqlite_master ' + "WHERE type='table' AND name <> '__WebKitDatabaseInfoTable__'", [], function (t, results) { - var storeNames = []; + var DriverSupport = {}; - for (var i = 0; i < results.rows.length; i++) { - storeNames.push(results.rows.item(i).name); - } + var DefaultDrivers = { + INDEXEDDB: asyncStorage, + WEBSQL: webSQLStorage, + LOCALSTORAGE: localStorageWrapper + }; - resolve({ - db: db, - storeNames: storeNames - }); - }, function (t, error) { - reject(error); - }); - }, function (sqlError) { - reject(sqlError); - }); - }); -} + var DefaultDriverOrder = [DefaultDrivers.INDEXEDDB._driver, DefaultDrivers.WEBSQL._driver, DefaultDrivers.LOCALSTORAGE._driver]; -function dropInstance$1(options, callback) { - callback = getCallback.apply(this, arguments); + var OptionalDriverMethods = ['dropInstance']; - var currentConfig = this.config(); - options = typeof options !== 'function' && options || {}; - if (!options.name) { - options.name = options.name || currentConfig.name; - options.storeName = options.storeName || currentConfig.storeName; - } + var LibraryMethods = ['clear', 'getItem', 'iterate', 'key', 'keys', 'length', 'removeItem', 'setItem'].concat(OptionalDriverMethods); - var self = this; - var promise; - if (!options.name) { - promise = Promise$1.reject('Invalid arguments'); - } else { - promise = new Promise$1(function (resolve) { - var db; - if (options.name === currentConfig.name) { - // use the db reference of the current instance - db = self._dbInfo.db; - } else { - db = openDatabase(options.name, '', '', 0); - } + var DefaultConfig = { + description: '', + driver: DefaultDriverOrder.slice(), + name: 'localforage', + // Default DB size is _JUST UNDER_ 5MB, as it's the highest size + // we can use without a prompt. + size: 4980736, + storeName: 'keyvaluepairs', + version: 1.0 + }; - if (!options.storeName) { - // drop all database tables - resolve(getAllStoreNames(db)); - } else { - resolve({ - db: db, - storeNames: [options.storeName] - }); - } - }).then(function (operationInfo) { - return new Promise$1(function (resolve, reject) { - operationInfo.db.transaction(function (t) { - function dropTable(storeName) { - return new Promise$1(function (resolve, reject) { - t.executeSql('DROP TABLE IF EXISTS ' + storeName, [], function () { - resolve(); - }, function (t, error) { - reject(error); - }); - }); - } + function callWhenReady(localForageInstance, libraryMethod) { + localForageInstance[libraryMethod] = function () { + var _args = arguments; + return localForageInstance.ready().then(function () { + return localForageInstance[libraryMethod].apply(localForageInstance, _args); + }); + }; + } - var operations = []; - for (var i = 0, len = operationInfo.storeNames.length; i < len; i++) { - operations.push(dropTable(operationInfo.storeNames[i])); - } + function extend() { + for (var i = 1; i < arguments.length; i++) { + var arg = arguments[i]; + + if (arg) { + for (var _key in arg) { + if (arg.hasOwnProperty(_key)) { + if (isArray(arg[_key])) { + arguments[0][_key] = arg[_key].slice(); + } else { + arguments[0][_key] = arg[_key]; + } + } + } + } + } + + return arguments[0]; + } - Promise$1.all(operations).then(function () { - resolve(); - })["catch"](function (e) { - reject(e); - }); - }, function (sqlError) { - reject(sqlError); - }); - }); + var LocalForage = function () { + function LocalForage(options) { + _classCallCheck(this, LocalForage); + + for (var driverTypeKey in DefaultDrivers) { + if (DefaultDrivers.hasOwnProperty(driverTypeKey)) { + var driver = DefaultDrivers[driverTypeKey]; + var driverName = driver._driver; + this[driverTypeKey] = driverName; + + if (!DefinedDrivers[driverName]) { + // we don't need to wait for the promise, + // since the default drivers can be defined + // in a blocking manner + this.defineDriver(driver); + } + } + } + + this._defaultConfig = extend({}, DefaultConfig); + this._config = extend({}, this._defaultConfig, options); + this._driverSet = null; + this._initDriver = null; + this._ready = false; + this._dbInfo = null; + + this._wrapLibraryMethodsWithReady(); + this.setDriver(this._config.driver)["catch"](function () {}); + } + + // Set any config values for localForage; can be called anytime before + // the first API call (e.g. `getItem`, `setItem`). + // We loop through options so we don't overwrite existing config + // values. + + + LocalForage.prototype.config = function config(options) { + // If the options argument is an object, we use it to set values. + // Otherwise, we return either a specified config value or all + // config values. + if ((typeof options === 'undefined' ? 'undefined' : _typeof(options)) === 'object') { + // If localforage is ready and fully initialized, we can't set + // any new configuration values. Instead, we return an error. + if (this._ready) { + return new Error("Can't call config() after localforage " + 'has been used.'); + } + + for (var i in options) { + if (i === 'storeName') { + options[i] = options[i].replace(/\W/g, '_'); + } + + if (i === 'version' && typeof options[i] !== 'number') { + return new Error('Database version must be a number.'); + } + + this._config[i] = options[i]; + } + + // after all config options are set and + // the driver option is used, try setting it + if ('driver' in options && options.driver) { + return this.setDriver(this._config.driver); + } + + return true; + } else if (typeof options === 'string') { + return this._config[options]; + } else { + return this._config; + } + }; + + // Used to define a custom driver, shared across all instances of + // localForage. + + + LocalForage.prototype.defineDriver = function defineDriver(driverObject, callback, errorCallback) { + var promise = new Promise$1(function (resolve, reject) { + try { + var driverName = driverObject._driver; + var complianceError = new Error('Custom driver not compliant; see ' + 'https://mozilla.github.io/localForage/#definedriver'); + + // A driver name should be defined and not overlap with the + // library-defined, default drivers. + if (!driverObject._driver) { + reject(complianceError); + return; + } + + var driverMethods = LibraryMethods.concat('_initStorage'); + for (var i = 0, len = driverMethods.length; i < len; i++) { + var driverMethodName = driverMethods[i]; + + // when the property is there, + // it should be a method even when optional + var isRequired = !includes(OptionalDriverMethods, driverMethodName); + if ((isRequired || driverObject[driverMethodName]) && typeof driverObject[driverMethodName] !== 'function') { + reject(complianceError); + return; + } + } + + var configureMissingMethods = function configureMissingMethods() { + var methodNotImplementedFactory = function methodNotImplementedFactory(methodName) { + return function () { + var error = new Error('Method ' + methodName + ' is not implemented by the current driver'); + var promise = Promise$1.reject(error); + executeCallback(promise, arguments[arguments.length - 1]); + return promise; + }; + }; + + for (var _i = 0, _len = OptionalDriverMethods.length; _i < _len; _i++) { + var optionalDriverMethod = OptionalDriverMethods[_i]; + if (!driverObject[optionalDriverMethod]) { + driverObject[optionalDriverMethod] = methodNotImplementedFactory(optionalDriverMethod); + } + } + }; + + configureMissingMethods(); + + var setDriverSupport = function setDriverSupport(support) { + if (DefinedDrivers[driverName]) { + console.info('Redefining LocalForage driver: ' + driverName); + } + DefinedDrivers[driverName] = driverObject; + DriverSupport[driverName] = support; + // don't use a then, so that we can define + // drivers that have simple _support methods + // in a blocking manner + resolve(); + }; + + if ('_support' in driverObject) { + if (driverObject._support && typeof driverObject._support === 'function') { + driverObject._support().then(setDriverSupport, reject); + } else { + setDriverSupport(!!driverObject._support); + } + } else { + setDriverSupport(true); + } + } catch (e) { + reject(e); + } + }); + + executeTwoCallbacks(promise, callback, errorCallback); + return promise; + }; + + LocalForage.prototype.driver = function driver() { + return this._driver || null; + }; + + LocalForage.prototype.getDriver = function getDriver(driverName, callback, errorCallback) { + var getDriverPromise = DefinedDrivers[driverName] ? Promise$1.resolve(DefinedDrivers[driverName]) : Promise$1.reject(new Error('Driver not found.')); + + executeTwoCallbacks(getDriverPromise, callback, errorCallback); + return getDriverPromise; + }; + + LocalForage.prototype.getSerializer = function getSerializer(callback) { + var serializerPromise = Promise$1.resolve(localforageSerializer); + executeTwoCallbacks(serializerPromise, callback); + return serializerPromise; + }; + + LocalForage.prototype.ready = function ready(callback) { + var self = this; + + var promise = self._driverSet.then(function () { + if (self._ready === null) { + self._ready = self._initDriver(); + } + + return self._ready; + }); + + executeTwoCallbacks(promise, callback, callback); + return promise; + }; + + LocalForage.prototype.setDriver = function setDriver(drivers, callback, errorCallback) { + var self = this; + + if (!isArray(drivers)) { + drivers = [drivers]; + } + + var supportedDrivers = this._getSupportedDrivers(drivers); + + function setDriverToConfig() { + self._config.driver = self.driver(); + } + + function extendSelfWithDriver(driver) { + self._extend(driver); + setDriverToConfig(); + + self._ready = self._initStorage(self._config); + return self._ready; + } + + function initDriver(supportedDrivers) { + return function () { + var currentDriverIndex = 0; + + function driverPromiseLoop() { + while (currentDriverIndex < supportedDrivers.length) { + var driverName = supportedDrivers[currentDriverIndex]; + currentDriverIndex++; + + self._dbInfo = null; + self._ready = null; + + return self.getDriver(driverName).then(extendSelfWithDriver)["catch"](driverPromiseLoop); + } + + setDriverToConfig(); + var error = new Error('No available storage method found.'); + self._driverSet = Promise$1.reject(error); + return self._driverSet; + } + + return driverPromiseLoop(); + }; + } + + // There might be a driver initialization in progress + // so wait for it to finish in order to avoid a possible + // race condition to set _dbInfo + var oldDriverSetDone = this._driverSet !== null ? this._driverSet["catch"](function () { + return Promise$1.resolve(); + }) : Promise$1.resolve(); + + this._driverSet = oldDriverSetDone.then(function () { + var driverName = supportedDrivers[0]; + self._dbInfo = null; + self._ready = null; + + return self.getDriver(driverName).then(function (driver) { + self._driver = driver._driver; + setDriverToConfig(); + self._wrapLibraryMethodsWithReady(); + self._initDriver = initDriver(supportedDrivers); + }); + })["catch"](function () { + setDriverToConfig(); + var error = new Error('No available storage method found.'); + self._driverSet = Promise$1.reject(error); + return self._driverSet; + }); + + executeTwoCallbacks(this._driverSet, callback, errorCallback); + return this._driverSet; + }; + + LocalForage.prototype.supports = function supports(driverName) { + return !!DriverSupport[driverName]; + }; + + LocalForage.prototype._extend = function _extend(libraryMethodsAndProperties) { + extend(this, libraryMethodsAndProperties); + }; + + LocalForage.prototype._getSupportedDrivers = function _getSupportedDrivers(drivers) { + var supportedDrivers = []; + for (var i = 0, len = drivers.length; i < len; i++) { + var driverName = drivers[i]; + if (this.supports(driverName)) { + supportedDrivers.push(driverName); + } + } + return supportedDrivers; + }; + + LocalForage.prototype._wrapLibraryMethodsWithReady = function _wrapLibraryMethodsWithReady() { + // Add a stub for each driver API method that delays the call to the + // corresponding driver method until localForage is ready. These stubs + // will be replaced by the driver methods as soon as the driver is + // loaded, so there is no performance impact. + for (var i = 0, len = LibraryMethods.length; i < len; i++) { + callWhenReady(this, LibraryMethods[i]); + } + }; + + LocalForage.prototype.createInstance = function createInstance(options) { + return new LocalForage(options); + }; + + return LocalForage; + }(); + + // The actual localForage object that we expose as a module or via a + // global. It's extended by pulling in one of our other libraries. + + + var localforage_js = new LocalForage(); + + module.exports = localforage_js; + + },{"3":3}]},{},[4])(4) + }); +} (localforage$1, localforage$1.exports)); + +var localforageExports = localforage$1.exports; +var localforage = /*@__PURE__*/getDefaultExportFromCjs(localforageExports); + +/** Simpler wrapper for a file-backed cache for arbitrary metadata. */ +class LocalStorageCache { + constructor(appId, version) { + this.appId = appId; + this.version = version; + this.persister = localforage.createInstance({ + name: "dataview/cache/" + appId, + driver: [localforage.INDEXEDDB], + description: "Cache metadata about files and sections in the dataview index.", }); } - - executeCallback(promise, callback); - return promise; -} - -var webSQLStorage = { - _driver: 'webSQLStorage', - _initStorage: _initStorage$1, - _support: isWebSQLValid(), - iterate: iterate$1, - getItem: getItem$1, - setItem: setItem$1, - removeItem: removeItem$1, - clear: clear$1, - length: length$1, - key: key$1, - keys: keys$1, - dropInstance: dropInstance$1 -}; - -function isLocalStorageValid() { - try { - return typeof localStorage !== 'undefined' && 'setItem' in localStorage && - // in IE8 typeof localStorage.setItem === 'object' - !!localStorage.setItem; - } catch (e) { - return false; + /** Drop the entire cache instance and re-create a new fresh instance. */ + async recreate() { + await localforage.dropInstance({ name: "dataview/cache/" + this.appId }); + this.persister = localforage.createInstance({ + name: "dataview/cache/" + this.appId, + driver: [localforage.INDEXEDDB], + description: "Cache metadata about files and sections in the dataview index.", + }); } -} - -function _getKeyPrefix(options, defaultConfig) { - var keyPrefix = options.name + '/'; - - if (options.storeName !== defaultConfig.storeName) { - keyPrefix += options.storeName + '/'; + /** Load file metadata by path. */ + async loadFile(path) { + return this.persister.getItem(this.fileKey(path)).then(raw => { + let result = raw; + if (result) + result.data = Transferable.value(result.data); + return result; + }); } - return keyPrefix; -} - -// Check if localStorage throws when saving an item -function checkIfLocalStorageThrows() { - var localStorageTestKey = '_localforage_support_test'; - - try { - localStorage.setItem(localStorageTestKey, true); - localStorage.removeItem(localStorageTestKey); - - return false; - } catch (e) { - return true; + /** Store file metadata by path. */ + async storeFile(path, data) { + await this.persister.setItem(this.fileKey(path), { + version: this.version, + time: Date.now(), + data: Transferable.transferable(data), + }); + } + /** Drop old file keys that no longer exist. */ + async synchronize(existing) { + let keys = new Set(await this.allFiles()); + for (let exist of existing) + keys.delete(exist); + // Any keys remaining after deleting existing keys are non-existent keys that should be cleared from cache. + for (let key of keys) + await this.persister.removeItem(this.fileKey(key)); + return keys; + } + /** Obtain a list of all metadata keys. */ + async allKeys() { + return this.persister.keys(); + } + /** Obtain a list of all persisted files. */ + async allFiles() { + let keys = await this.allKeys(); + return keys.filter(k => k.startsWith("file:")).map(k => k.substring(5)); + } + fileKey(path) { + return "file:" + path; } } -// Check if localStorage is usable and allows to save an item -// This method checks if localStorage is usable in Safari Private Browsing -// mode, or in any other case where the available quota for localStorage -// is 0 and there wasn't any saved items yet. -function _isLocalStorageUsable() { - return !checkIfLocalStorageThrows() || localStorage.length > 0; -} - -// Config the localStorage backend, using options set in the config. -function _initStorage$2(options) { - var self = this; - var dbInfo = {}; - if (options) { - for (var i in options) { - dbInfo[i] = options[i]; +function decodeBase64(base64, enableUnicode) { + var binaryString = atob(base64); + if (enableUnicode) { + var binaryView = new Uint8Array(binaryString.length); + for (var i = 0, n = binaryString.length; i < n; ++i) { + binaryView[i] = binaryString.charCodeAt(i); } + return String.fromCharCode.apply(null, new Uint16Array(binaryView.buffer)); } + return binaryString; +} - dbInfo.keyPrefix = _getKeyPrefix(options, self._defaultConfig); - - if (!_isLocalStorageUsable()) { - return Promise$1.reject(); - } - - self._dbInfo = dbInfo; - dbInfo.serializer = localforageSerializer; - - return Promise$1.resolve(); +function createURL(base64, sourcemapArg, enableUnicodeArg) { + var sourcemap = sourcemapArg === undefined ? null : sourcemapArg; + var enableUnicode = enableUnicodeArg === undefined ? false : enableUnicodeArg; + var source = decodeBase64(base64, enableUnicode); + var start = source.indexOf('\n', 10) + 1; + var body = source.substring(start) + (sourcemap ? '\/\/# sourceMappingURL=' + sourcemap : ''); + var blob = new Blob([body], { type: 'application/javascript' }); + return URL.createObjectURL(blob); } -// Remove all keys from the datastore, effectively destroying all data in -// the app's key/value store! -function clear$2(callback) { - var self = this; - var promise = self.ready().then(function () { - var keyPrefix = self._dbInfo.keyPrefix; - - for (var i = localStorage.length - 1; i >= 0; i--) { - var key = localStorage.key(i); - - if (key.indexOf(keyPrefix) === 0) { - localStorage.removeItem(key); - } - } - }); - - executeCallback(promise, callback); - return promise; +function createBase64WorkerFactory(base64, sourcemapArg, enableUnicodeArg) { + var url; + return function WorkerFactory(options) { + url = url || createURL(base64, sourcemapArg, enableUnicodeArg); + return new Worker(url, options); + }; } -// Retrieve an item from the store. Unlike the original async_storage -// library in Gaia, we don't modify return values at all. If a key's value -// is `undefined`, we pass that value to the callback function. -function getItem$2(key, callback) { - var self = this; - - key = normalizeKey(key); - - var promise = self.ready().then(function () { - var dbInfo = self._dbInfo; - var result = localStorage.getItem(dbInfo.keyPrefix + key); +var WorkerFactory = createBase64WorkerFactory('Lyogcm9sbHVwLXBsdWdpbi13ZWItd29ya2VyLWxvYWRlciAqLwooZnVuY3Rpb24gKCkgewogICd1c2Ugc3RyaWN0JzsKCiAgLy8gdGhlc2UgYXJlbid0IHJlYWxseSBwcml2YXRlLCBidXQgbm9yIGFyZSB0aGV5IHJlYWxseSB1c2VmdWwgdG8gZG9jdW1lbnQKCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBMdXhvbkVycm9yIGV4dGVuZHMgRXJyb3Ige30KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBJbnZhbGlkRGF0ZVRpbWVFcnJvciBleHRlbmRzIEx1eG9uRXJyb3IgewogICAgY29uc3RydWN0b3IocmVhc29uKSB7CiAgICAgIHN1cGVyKGBJbnZhbGlkIERhdGVUaW1lOiAke3JlYXNvbi50b01lc3NhZ2UoKX1gKTsKICAgIH0KICB9CgogIC8qKgogICAqIEBwcml2YXRlCiAgICovCiAgY2xhc3MgSW52YWxpZEludGVydmFsRXJyb3IgZXh0ZW5kcyBMdXhvbkVycm9yIHsKICAgIGNvbnN0cnVjdG9yKHJlYXNvbikgewogICAgICBzdXBlcihgSW52YWxpZCBJbnRlcnZhbDogJHtyZWFzb24udG9NZXNzYWdlKCl9YCk7CiAgICB9CiAgfQoKICAvKioKICAgKiBAcHJpdmF0ZQogICAqLwogIGNsYXNzIEludmFsaWREdXJhdGlvbkVycm9yIGV4dGVuZHMgTHV4b25FcnJvciB7CiAgICBjb25zdHJ1Y3RvcihyZWFzb24pIHsKICAgICAgc3VwZXIoYEludmFsaWQgRHVyYXRpb246ICR7cmVhc29uLnRvTWVzc2FnZSgpfWApOwogICAgfQogIH0KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBDb25mbGljdGluZ1NwZWNpZmljYXRpb25FcnJvciBleHRlbmRzIEx1eG9uRXJyb3Ige30KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBJbnZhbGlkVW5pdEVycm9yIGV4dGVuZHMgTHV4b25FcnJvciB7CiAgICBjb25zdHJ1Y3Rvcih1bml0KSB7CiAgICAgIHN1cGVyKGBJbnZhbGlkIHVuaXQgJHt1bml0fWApOwogICAgfQogIH0KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBJbnZhbGlkQXJndW1lbnRFcnJvciBleHRlbmRzIEx1eG9uRXJyb3Ige30KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBab25lSXNBYnN0cmFjdEVycm9yIGV4dGVuZHMgTHV4b25FcnJvciB7CiAgICBjb25zdHJ1Y3RvcigpIHsKICAgICAgc3VwZXIoIlpvbmUgaXMgYW4gYWJzdHJhY3QgY2xhc3MiKTsKICAgIH0KICB9CgogIC8qKgogICAqIEBwcml2YXRlCiAgICovCgogIGNvbnN0IG4gPSAibnVtZXJpYyIsCiAgICBzID0gInNob3J0IiwKICAgIGwgPSAibG9uZyI7CgogIGNvbnN0IERBVEVfU0hPUlQgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IG4sCiAgICBkYXk6IG4sCiAgfTsKCiAgY29uc3QgREFURV9NRUQgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IHMsCiAgICBkYXk6IG4sCiAgfTsKCiAgY29uc3QgREFURV9NRURfV0lUSF9XRUVLREFZID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBzLAogICAgZGF5OiBuLAogICAgd2Vla2RheTogcywKICB9OwoKICBjb25zdCBEQVRFX0ZVTEwgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IGwsCiAgICBkYXk6IG4sCiAgfTsKCiAgY29uc3QgREFURV9IVUdFID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBsLAogICAgZGF5OiBuLAogICAgd2Vla2RheTogbCwKICB9OwoKICBjb25zdCBUSU1FX1NJTVBMRSA9IHsKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgfTsKCiAgY29uc3QgVElNRV9XSVRIX1NFQ09ORFMgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogIH07CgogIGNvbnN0IFRJTUVfV0lUSF9TSE9SVF9PRkZTRVQgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogICAgdGltZVpvbmVOYW1lOiBzLAogIH07CgogIGNvbnN0IFRJTUVfV0lUSF9MT05HX09GRlNFVCA9IHsKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgICBzZWNvbmQ6IG4sCiAgICB0aW1lWm9uZU5hbWU6IGwsCiAgfTsKCiAgY29uc3QgVElNRV8yNF9TSU1QTEUgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgaG91ckN5Y2xlOiAiaDIzIiwKICB9OwoKICBjb25zdCBUSU1FXzI0X1dJVEhfU0VDT05EUyA9IHsKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgICBzZWNvbmQ6IG4sCiAgICBob3VyQ3ljbGU6ICJoMjMiLAogIH07CgogIGNvbnN0IFRJTUVfMjRfV0lUSF9TSE9SVF9PRkZTRVQgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogICAgaG91ckN5Y2xlOiAiaDIzIiwKICAgIHRpbWVab25lTmFtZTogcywKICB9OwoKICBjb25zdCBUSU1FXzI0X1dJVEhfTE9OR19PRkZTRVQgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogICAgaG91ckN5Y2xlOiAiaDIzIiwKICAgIHRpbWVab25lTmFtZTogbCwKICB9OwoKICBjb25zdCBEQVRFVElNRV9TSE9SVCA9IHsKICAgIHllYXI6IG4sCiAgICBtb250aDogbiwKICAgIGRheTogbiwKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgfTsKCiAgY29uc3QgREFURVRJTUVfU0hPUlRfV0lUSF9TRUNPTkRTID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBuLAogICAgZGF5OiBuLAogICAgaG91cjogbiwKICAgIG1pbnV0ZTogbiwKICAgIHNlY29uZDogbiwKICB9OwoKICBjb25zdCBEQVRFVElNRV9NRUQgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IHMsCiAgICBkYXk6IG4sCiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogIH07CgogIGNvbnN0IERBVEVUSU1FX01FRF9XSVRIX1NFQ09ORFMgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IHMsCiAgICBkYXk6IG4sCiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogIH07CgogIGNvbnN0IERBVEVUSU1FX01FRF9XSVRIX1dFRUtEQVkgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IHMsCiAgICBkYXk6IG4sCiAgICB3ZWVrZGF5OiBzLAogICAgaG91cjogbiwKICAgIG1pbnV0ZTogbiwKICB9OwoKICBjb25zdCBEQVRFVElNRV9GVUxMID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBsLAogICAgZGF5OiBuLAogICAgaG91cjogbiwKICAgIG1pbnV0ZTogbiwKICAgIHRpbWVab25lTmFtZTogcywKICB9OwoKICBjb25zdCBEQVRFVElNRV9GVUxMX1dJVEhfU0VDT05EUyA9IHsKICAgIHllYXI6IG4sCiAgICBtb250aDogbCwKICAgIGRheTogbiwKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgICBzZWNvbmQ6IG4sCiAgICB0aW1lWm9uZU5hbWU6IHMsCiAgfTsKCiAgY29uc3QgREFURVRJTUVfSFVHRSA9IHsKICAgIHllYXI6IG4sCiAgICBtb250aDogbCwKICAgIGRheTogbiwKICAgIHdlZWtkYXk6IGwsCiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgdGltZVpvbmVOYW1lOiBsLAogIH07CgogIGNvbnN0IERBVEVUSU1FX0hVR0VfV0lUSF9TRUNPTkRTID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBsLAogICAgZGF5OiBuLAogICAgd2Vla2RheTogbCwKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgICBzZWNvbmQ6IG4sCiAgICB0aW1lWm9uZU5hbWU6IGwsCiAgfTsKCiAgLyoqCiAgICogQGludGVyZmFjZQogICAqLwogIGNsYXNzIFpvbmUgewogICAgLyoqCiAgICAgKiBUaGUgdHlwZSBvZiB6b25lCiAgICAgKiBAYWJzdHJhY3QKICAgICAqIEB0eXBlIHtzdHJpbmd9CiAgICAgKi8KICAgIGdldCB0eXBlKCkgewogICAgICB0aHJvdyBuZXcgWm9uZUlzQWJzdHJhY3RFcnJvcigpOwogICAgfQoKICAgIC8qKgogICAgICogVGhlIG5hbWUgb2YgdGhpcyB6b25lLgogICAgICogQGFic3RyYWN0CiAgICAgKiBAdHlwZSB7c3R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hY3QKICAgICAqIEBwYXJhbSB7Wm9uZX0gb3RoZXJab25lIC0gdGhlIHpvbmUgdG8gY29tcGFyZQogICAgICogQHJldHVybiB7Ym9vbGVhbn0KICAgICAqLwogICAgZXF1YWxzKG90aGVyWm9uZSkgewogICAgICB0aHJvdyBuZXcgWm9uZUlzQWJzdHJhY3RFcnJvcigpOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJuIHdoZXRoZXIgdGhpcyBab25lIGlzIHZhbGlkLgogICAgICogQGFic3RyYWN0CiAgICAgKiBAdHlwZSB7Ym9vbGVhbn0KICAgICAqLwogICAgZ2V0IGlzVmFsaWQoKSB7CiAgICAgIHRocm93IG5ldyBab25lSXNBYnN0cmFjdEVycm9yKCk7CiAgICB9CiAgfQoKICBsZXQgc2luZ2xldG9uJDEgPSBudWxsOwoKICAvKioKICAgKiBSZXByZXNlbnRzIHRoZSBsb2NhbCB6b25lIGZvciB0aGlzIEphdmFTY3JpcHQgZW52aXJvbm1lbnQuCiAgICogQGltcGxlbWVudHMge1pvbmV9CiAgICovCiAgY2xhc3MgU3lzdGVtWm9uZSBleHRlbmRzIFpvbmUgewogICAgLyoqCiAgICAgKiBHZXQgYSBzaW5nbGV0b24gaW5zdGFuY2Ugb2YgdGhlIGxvY2FsIHpvbmUKICAgICAqIEByZXR1cm4ge1N5c3RlbVpvbmV9CiAgICAgKi8KICAgIHN0YXRpYyBnZXQgaW5zdGFuY2UoKSB7CiAgICAgIGlmIChzaW5nbGV0b24kMSA9PT0gbnVsbCkgewogICAgICAgIHNpbmdsZXRvbiQxID0gbmV3IFN5c3RlbVpvbmUoKTsKICAgICAgfQogICAgICByZXR1cm4gc2luZ2xldG9uJDE7CiAgICB9CgogICAgLyoqIEBvdmVycmlkZSAqKi8KICAgIGdldCB0eXBlKCkgewogICAgICByZXR1cm4gInN5c3RlbSI7CiAgICB9CgogICAgLyoqIEBvdmVycmlkZSAqKi8KICAgIGdldCBuYW1lKCkgewogICAgICByZXR1cm4gbmV3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AobG9jLm51bWJlcmluZ1N5c3RlbSAmJiBsb2MubnVtYmVyaW5nU3lzdGVtICE9PSAibGF0biIpIHsKICAgICAgcmV0dXJuIGZhbHNlOwogICAgfSBlbHNlIHsKICAgICAgcmV0dXJuICgKICAgICAgICBsb2MubnVtYmVyaW5nU3lzdGVtID09PSAibGF0biIgfHwKICAgICAgICAhbG9jLmxvY2FsZSB8fAogICAgICAgIGxvYy5sb2NhbGUuc3RhcnRzV2l0aCgiZW4iKSB8fAogICAgICAgIG5ldyBJbnRsLkRhdGVUaW1lRm9ybWF0KGxvYy5pbnRsKS5yZXNvbHZlZE9wdGlvbnMoKS5udW1iZXJpbmdTeXN0ZW0gPT09ICJsYXRuIgogICAgICApOwogICAgfQogIH0KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KCiAgY2xhc3MgUG9seU51bWJlckZvcm1hdHRlciB7CiAgICBjb25zdHJ1Y3RvcihpbnRsLCBmb3JjZVNpbXBsZSwgb3B0cykgewogICAgICB0aGlzLnBhZFRvID0gb3B0cy5wYWRUbyB8fCAwOwogICAgICB0aGlzLmZsb29yID0gb3B0cy5mbG9vciB8fCBmYWxzZTsKCiAgICAgIGNvbnN0IHsgcGFkVG8sIGZsb29yLCAuLi5vdGhlck9wdHMgfSA9IG9wdHM7CgogICAgICBpZiAoIWZvcmNlU2ltcGxlIHx8IE9iamVjdC5rZXlzKG90aGVyT3B0cykubGVuZ3RoID4gMCkgewogICAgICAgIGNvbnN0IGludGxPcHRzID0geyB1c2VHcm91cGluZzogZmFsc2UsIC4uLm9wdHMgfTsKICAgICAgICBpZiAob3B0cy5wYWRUbyA+IDApIGludGxPcHRzLm1pbmltdW1JbnRlZ2VyRGlnaXRzID0gb3B0cy5wYWRUbzsKICAgICAgICB0aGlzLmluZiA9IGdldENhY2hlZElORihpbnRsLCBpbnRsT3B0cyk7CiAgICAgIH0KICAgIH0KCiAgICBmb3JtYXQoaSkgewogICAgICBpZiAodGhpcy5pbmYpIHsKICAgICAgIC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ICAgICAgIC5tYXAoKHsgdmFsdWUgfSkgPT4gdmFsdWUpCiAgICAgICAgICAuam9pbigiIik7CiAgICAgIH0KICAgICAgcmV0dXJuIHRoaXMuZHRmLmZvcm1hdCh0aGlzLmR0LnRvSlNEYXRlKCkpOwogICAgfQoKICAgIGZvcm1hdFRvUGFydHMoKSB7CiAgICAgIGNvbnN0IHBhcnRzID0gdGhpcy5kdGYuZm9ybWF0VG9QYXJ0cyh0aGlzLmR0LnRvSlNEYXRlKCkpOwogICAgICBpZiAodGhpcy5vcmlnaW5hbFpvbmUpIHsKICAgICAgICByZXR1cm4gcGFydHMubWFwKChwYXJ0KSA9PiB7CiAgICAgICAgICBpZiAocGFydC50eXBlID09PSAidGltZVpvbmVOYW1lIikgewogICAgICAgICAgICBjb25zdCBvZmZzZXROYW1lID0gdGhpcy5vcmlnaW5hbFpvbmUub2Zmc2V0TmFtZSh0aGlzLmR0LnRzLCB7CiAgICAgICAgICAgICAgbG9jYWxlOiB0aGlzLmR0LmxvY2FsZSwKICAgICAgICAgICAgICBmb3JtYXQ6IHRoaXMub3B0cy50aW1lWm9uZU5hbWUsCiAgICAgICAgICAgIH0pOwogICAgICAgICAgICByZXR1cm4gewogICAgICAgICAgICAgIC4uLnBhcnQsCiAgICAgICAgICAgICAgdmFsdWU6IG9mZnNldE5hbWUsCiAgICAgICAgICAgIH07CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICByZXR1cm4gcGFydDsKICAgICAgICAgIH0KICAgICAgICB9KTsKICAgICAgfQogICAgICByZXR1cm4gcGFydHM7CiAgICB9CgogICAgcmVzb2x2ZWRPcHRpb25zKCkgewogICAgICByZXR1cm4gdGhpcy5kdGYucmVzb2x2ZWRPcHRpb25zKCk7CiAgICB9CiAgfQoKICAvKioKICAgKiBAcHJpdmF0ZQogICAqLwogIGNsYXNzIFBvbHlSZWxGb3JtYXR0ZXIgewogICAgY29uc3RydWN0b3IoaW50bCwgaXNFbmdsaXNoLCBvcHRzKSB7CiAg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HRoYXQgZmFpbGVkIHRvIHBhcnNlLiBZb3Ugc2hvdWxkIG5ldmVyIG5lZWQgdG8gaW5zdGFudGlhdGUgdGhpcy4KICAgKiBAaW1wbGVtZW50cyB7Wm9uZX0KICAgKi8KICBjbGFzcyBJbnZhbGlkWm9uZSBleHRlbmRzIFpvbmUgewogICAgY29uc3RydWN0b3Ioem9uZU5hbWUpIHsKICAgICAgc3VwZXIoKTsKICAgICAgLyoqICBAcHJpdmF0ZSAqLwogICAgICB0aGlzLnpvbmVOYW1lID0gem9uZU5hbWU7CiAgICB9CgogICAgLyoqIEBvdmVycmlkZSAqKi8KICAgIGdldCB0eXBlKCkgewogICAgICByZXR1cm4gImludmFsaWQiOwogICAgfQoKICAgIC8qKiBAb3ZlcnJpZGUgKiovCiAgICBnZXQgbmFtZSgpIHsKICAgICAgcmV0dXJuIHRoaXMuem9uZU5hbWU7CiAgICB9CgogICAgLyoqIEBvdmVycmlkZSAqKi8KICAgIGdldCBpc1VuaXZlcnNhbCgpIHsKICAgICAgcmV0dXJuIGZhbHNlOwogICAgfQoKICAgIC8qKiBAb3ZlcnJpZGUgKiovCiAgICBvZmZzZXROYW1lKCkgewogICAgICByZXR1cm4gbnVsbDsKICAgIH0KCiAgICAvKiogQG92ZXJyaWRlICoqLwogICAgZm9ybWF0T2Zmc2V0KCkgewogICAgICByZXR1cm4gIiI7CiAgICB9CgogICAgLyoqIEBvdmVycmlkZSAqKi8KICAgIG9mZnNldCgpIHsKICAgICAgcmV0dXJuIE5hTjsKICAgIH0KCiAgICAvKiogQG92ZXJyaWRlICoqLwogICAgZXF1YWxzKCkgewogICAgICByZXR1cm4gZmFsc2U7CiAgICB9CgogICAgLyoqIEBvdmVycmlkZSAqKi8KICAgIGdldCBpc1ZhbGlkKCkgewogICAgICByZXR1cm4gZmFsc2U7CiAgICB9CiAgfQoKICAvKioKICAgKiBAcHJpdmF0ZQogICAqLwoKICBmdW5jdGlvbiBub3JtYWxpemVab25lKGlucHV0LCBkZWZh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ZhdGUKICAgKi8KCiAgY29uc3QgbW9udGhzTG9uZyA9IFsKICAgICJKYW51YXJ5IiwKICAgICJGZWJydWFyeSIsCiAgICAiTWFyY2giLAogICAgIkFwcmlsIiwKICAgICJNYXkiLAogICAgIkp1bmUiLAogICAgIkp1bHkiLAogICAgIkF1Z3VzdCIsCiAgICAiU2VwdGVtYmVyIiwKICAgICJPY3RvYmVyIiwKICAgICJOb3ZlbWJlciIsCiAgICAiRGVjZW1iZXIiLAogIF07CgogIGNvbnN0IG1vbnRoc1Nob3J0ID0gWwogICAgIkphbiIsCiAgICAiRmViIiwKICAgICJNYXIiLAogICAgIkFwciIsCiAgICAiTWF5IiwKICAgICJKdW4iLAogICAgIkp1bCIsCiAgICAiQXVnIiwKICAgICJTZXAiLAogICAgIk9jdCIsCiAgICAiTm92IiwKICAgICJEZWMiLAogIF07CgogIGNvbnN0IG1vbnRoc05hcnJvdyA9IFsiSiIsICJGIiwgIk0iLCAiQSIsICJNIiwgIkoiLCAiSiIsICJBIiwgIlMiLCAiTyIsICJOIiwgIkQiXTsKCiAgZnVuY3Rpb24gbW9udGhzKGxlbmd0aCkgewogICAgc3dpdGNoIChsZW5ndGgpIHsKICAgICAgY2FzZSAibmFycm93IjoKICAgICAgICByZXR1cm4gWy4uLm1vbnRoc05hcnJvd107CiAgICAgIGNhc2UgInNob3J0IjoKICAgICAgICByZXR1cm4gWy4uLm1vbnRoc1Nob3J0XTsKICAgICAgY2FzZSAibG9uZyI6CiAgICAgICAgcmV0dXJuIFsuLi5tb250aHNMb25nXTsKICAgICAgY2FzZSAibnVtZXJpYyI6CiAgICAgICAgcmV0dXJuIFsiMSIsICIyIiwgIjMiLCAiNCIsICI1IiwgIjYiLCAiNyIsICI4IiwgIjkiLCAiMTAiLCAiMTEiLCAiMTIiXTsKICAgICAgY2FzZSAiMi1kaWdpdCI6CiAgICAgICAgcmV0dXJuIFsiMDEiLCAiMDIiLCAiMDMiLCAiMDQiLCAiMDUiLCAiMDYiLCAiMDciLCAiMDgiLCAiMDkiLCAiMTAiLCAiMTEiLCAiMTIiXTsKICAgICAgZGVmYXVsdDoKICAgICAgICByZXR1cm4gbnVsbDsKICAgIH0KICB9CgogIGNvbnN0IHdlZWtkYXlzTG9uZyA9IFsKICAgICJNb25kYXkiLAogICAgIlR1ZXNkYXkiLAogICAgIldlZG5lc2RheSIsCiAgICAiVGh1cnNkYXkiLAogICAgIkZyaWRheSIsCiAgICAiU2F0dXJkYXkiLAogICAgIlN1bmRheSIsCiAgXTsKCiAgY29uc3Qgd2Vla2RheXNTaG9ydCA9IFsiTW9uIiwgIlR1ZSIsICJXZWQiLCAiVGh1IiwgIkZyaSIsICJTYXQiLCAiU3VuIl07CgogIGNvbnN0IHdlZWtkYXlzTmFycm93ID0gWyJNIiwgIlQiLCAiVyIsICJUIiwgIkYiLCAiUyIsICJTIl07CgogIGZ1bmN0aW9uIHdlZWtkYXlzKGxlbmd0aCkgewogICAgc3dpdGNoIChsZW5ndGgpIHsKICAgICAgY2FzZSAibmFycm93IjoKICAgICAgICByZXR1cm4gWy4uLndlZWtkYXlzTmFycm93XTsKICAgICAgY2FzZSAic2hvcnQiOgogICAgICAgIHJldHVybiBbLi4ud2Vla2RheXNTaG9ydF07CiAgICAgIGNhc2UgImxvbmciOgogICAgICAgIHJldHVybiBbLi4ud2Vla2RheXNMb25nXTsKICAgICAgY2FzZSAibnVtZXJpYyI6CiAgICAgICAgcmV0dXJuIFsiMSIsICIyIiwgIjMiLCAiNCIsICI1IiwgIjYiLCAiNyJdOwogICAgICBkZWZhdWx0OgogICAgICAgIHJldHVybiBudWxsOwogICAgfQogIH0KCiAgY29uc3QgbWVyaWRpZW1zID0gWyJBTSIsICJQTSJdOwoKICBjb25zdCBlcmFzTG9uZyA9IFsiQmVmb3JlIENocmlzdCIsICJBbm5vIERvbWluaSJdOwoKICBjb25zdCBlcmFzU2hvcnQgPSBbIkJDIiwgIkFEIl07CgogIGNvbnN0IGVyYXNOYXJyb3cgPSBbIkIiLC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IsICJ3ay4iXSwKICAgICAgZGF5czogWyJkYXkiLCAiZGF5IiwgImRheXMiXSwKICAgICAgaG91cnM6IFsiaG91ciIsICJoci4iXSwKICAgICAgbWludXRlczogWyJtaW51dGUiLCAibWluLiJdLAogICAgICBzZWNvbmRzOiBbInNlY29uZCIsICJzZWMuIl0sCiAgICB9OwoKICAgIGNvbnN0IGxhc3RhYmxlID0gWyJob3VycyIsICJtaW51dGVzIiwgInNlY29uZHMiXS5pbmRleE9mKHVuaXQpID09PSAtMTsKCiAgICBpZiAobnVtZXJpYyA9PT0gImF1dG8iICYmIGxhc3RhYmxlKSB7CiAgICAgIGNvbnN0IGlzRGF5ID0gdW5pdCA9PT0gImRheXMiOwogICAgICBzd2l0Y2ggKGNvdW50KSB7CiAgICAgICAgY2FzZSAxOgogICAgICAgICAgcmV0dXJuIGlzRGF5ID8gInRvbW9ycm93IiA6IGBuZXh0ICR7dW5pdHNbdW5pdF1bMF19YDsKICAgICAgICBjYXNlIC0xOgogICAgICAgICAgcmV0dXJuIGlzRGF5ID8gInllc3RlcmRheSIgOiBgbGFzdCAke3VuaXRzW3VuaXRdWzBdfWA7CiAgICAgICAgY2FzZSAwOgogICAgICAgICAgcmV0dXJuIGlzRGF5ID8gInRvZGF5IiA6IGB0aGlzICR7dW5pdHNbdW5pdF1bMF19YDsKICAgICAgfQogICAgfQoKICAgIGNvbnN0IGlzSW5QYXN0ID0gT2JqZWN0LmlzKGNvdW50LCAtMCkgfHwgY291bnQgPCAwLAogICAgICBmbXRWYWx1ZSA9IE1hdGguYWJzKGNvdW50KSwKICAgICAgc2luZ3VsYXIgPSBmbXRWYWx1ZSA9PT0gMSwKICAgICAgbGlsVW5pdHMgPSB1bml0c1t1bml0XSwKICAgICAgZm10VW5pdCA9IG5hcnJvdwogICAgICAgID8gc2luZ3VsYXIKICAgICAgICAgID8gbGlsVW5pdHNbMV0KICAgICAgICAgIDogbGlsVW5pdHNbMl0gfHwgbGlsVW5pdHNbMV0KICAgICAgICA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kb2luZyB0aGlzIGhlcmUsIGFubm95aW5nbHkKICAgICAgaWYgKHRoaXMub3B0cy5mb3JjZVNpbXBsZSkgewogICAgICAgIHJldHVybiBwYWRTdGFydChuLCBwKTsKICAgICAgfQoKICAgICAgY29uc3Qgb3B0cyA9IHsgLi4udGhpcy5vcHRzIH07CgogICAgICBpZiAocCA+IDApIHsKICAgICAgICBvcHRzLnBhZFRvID0gcDsKICAgICAgfQoKICAgICAgcmV0dXJuIHRoaXMubG9jLm51bWJlckZvcm1hdHRlcihvcHRzKS5mb3JtYXQobik7CiAgICB9CgogICAgZm9ybWF0RGF0ZVRpbWVGcm9tU3RyaW5nKGR0LCBmbXQpIHsKICAgICAgY29uc3Qga25vd25FbmdsaXNoID0gdGhpcy5sb2MubGlzdGluZ01vZGUoKSA9PT0gImVuIiwKICAgICAgICB1c2VEYXRlVGltZUZvcm1hdHRlciA9IHRoaXMubG9jLm91dHB1dENhbGVuZGFyICYmIHRoaXMubG9jLm91dHB1dENhbGVuZGFyICE9PSAiZ3JlZ29yeSIsCiAgICAgICAgc3RyaW5nID0gKG9wdHMsIGV4dHJhY3QpID0+IHRoaXMubG9jLmV4dHJhY3QoZHQsIG9wdHMsIGV4dHJhY3QpLAogICAgICAgIGZvcm1hdE9mZnNldCA9IChvcHRzKSA9PiB7CiAgICAgICAgICBpZiAoZHQuaXNPZmZzZXRGaXhlZCAmJiBkdC5vZmZzZXQgPT09IDAgJiYgb3B0cy5hbGxvd1opIHsKICAgICAgICAgICAgcmV0dXJuICJaIjsKICAgICAgICAgIH0KCiAgICAgICAgICByZXR1cm4gZHQuaXNWYWxpZCA/IGR0LnpvbmUuZm9ybWF0T2Zmc2V0KGR0LnRzLCBvcHRzLmZvcm1hdCkgOiAiIjsKICAgICAgICB9LAogICAgICAgIG1lcmlkaWVtID0gKCkgPT4KICAgICAgICAgIGtub3duRW5nbGlzaAogICAgICAgICAgICA/IG1lcmlkaWVtRm9yRGF0ZVRpbWUoZHQpCiAgICAgICAgICAgIDogc3RyaW5nKHsgaG91cjogIm51bWVyaWMiLCBob3VyQ3ljbGU6ICJoMTIiIH0sICJkYXlwZXJpb2QiKSwKICAgICAgICBtb250aCA9IChsZW5ndGgsIHN0YW5kYWxvbmUpID0+CiAgICAgICAgICBrbm93bkVuZ2xpc2gKICAgICAgICAgICAgPyBtb250aEZvckRhdGVUaW1lKGR0LCBsZW5ndGgpCiAgICAgICAgICAgIDogc3RyaW5nKHN0YW5kYWxvbmUgPyB7IG1vbnRoOiBsZW5ndGggfSA6IHsgbW9udGg6IGxlbmd0aCwgZGF5OiAibnVtZXJpYyIgfSwgIm1vbnRoIiksCiAgICAgICAgd2Vla2RheSA9IChsZW5ndGgsIHN0YW5kYWxvbmUpID0+CiAgICAgICAgICBrbm93bkVuZ2xpc2gKICAgICAgICAgICAgPyB3ZWVrZGF5Rm9yRGF0ZVRpbWUoZHQsIGxlbmd0aCkKICAgICAgICAgICAgOiBzdHJpbmcoCiAgICAgICAgICAgICAgICBzdGFuZGFsb25lID8geyB3ZWVrZGF5OiBsZW5ndGggfSA6IHsgd2Vla2RheTogbGVuZ3RoLCBtb250aDogImxvbmciLCBkYXk6ICJudW1lcmljIiB9LAogICAgICAgICAgICAgICAgIndlZWtkYXkiCiAgICAgICAgICAgICAgKSwKICAgICAgICBtYXliZU1hY3JvID0gKHRva2VuKSA9PiB7CiAgICAgICAgICBjb25zdCBmb3JtYXRPcHRzID0gRm9ybWF0dGVyLm1hY3JvVG9rZW5Ub0Zvcm1hdE9wdHModG9rZW4pOwogICAgICAgICAgaWYgKGZvcm1hdE9wdHMpIHsKICAgICAgICAgICAgcmV0dXJuIHRoaXMuZm9ybWF0V2l0aFN5c3RlbURlZmF1bHQoZHQsIGZvcm1hdE9wdHMpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmV0dXJuIHRva2VuOwogICAgICAgICAgfQogICAgICAgIH0sCiAgICAgICAgZXJhID0gKGxlbmd0aCkgPT4KICAgICAgICAgIGtub3duRW5nbGlzaCA/IGVyYUZvckRhdGVUaW1lKGR0LCBsZW5ndGgpIDogc3RyaW5nKHsgZXJhOiBsZW5ndGggfSwgImVyYSIpLAogICAgICAgIHRva2VuVG9TdHJpbmcgPSAodG9rZW4pID0+IHsKICAgICAgICAgIC8vIFdoZXJlIHBvc3NpYmxlOiBodHRwczovL2NsZHIudW5pY29kZS5vcmcvdHJhbnNsYXRpb24vZGF0ZS10aW1lL2RhdGUtdGltZS1zeW1ib2xzCiAgICAgICAgICBzd2l0Y2ggKHRva2VuKSB7CiAgICAgICAgICAgIC8vIG1zCiAgICAgICAgICAgIGNhc2UgIlMiOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShkdC5taWxsaXNlY29uZCk7CiAgICAgICAgICAgIGNhc2UgInUiOgogICAgICAgICAgICAvLyBmYWxscyB0aHJvdWdoCiAgICAgICAgICAgIGNhc2UgIlNTUyI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0Lm1pbGxpc2Vjb25kLCAzKTsKICAgICAgICAgICAgLy8gc2Vjb25kcwogICAgICAgICAgICBjYXNlICJzIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQuc2Vjb25kKTsKICAgICAgICAgICAgY2FzZSAic3MiOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShkdC5zZWNvbmQsIDIpOwogICAgICAgICAgICAvLyBmcmFjdGlvbmFsIHNlY29uZHMKICAgICAgICAgICAgY2FzZSAidXUiOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShNYXRoLmZsb29yKGR0Lm1pbGxpc2Vjb25kIC8gMTApLCAyKTsKICAgICAgICAgICAgY2FzZSAidXV1IjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oTWF0aC5mbG9vcihkdC5taWxsaXNlY29uZCAvIDEwMCkpOwogICAgICAgICAgICAvLyBtaW51dGVzCiAgICAgICAgICAgIGNhc2UgIm0iOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShkdC5taW51dGUpOwogICAgICAgICAgICBjYXNlICJtbSI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0Lm1pbnV0ZSwgMik7CiAgICAgICAgICAgIC8vIGhvdXJzCiAgICAgICAgICAgIGNhc2UgImgiOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShkdC5ob3VyICUgMTIgPT09IDAgPyAxMiA6IGR0LmhvdXIgJSAxMik7CiAgICAgICAgICAgIGNhc2UgImhoIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQuaG91ciAlIDEyID09PSAwID8gMTIgOiBkdC5ob3VyICUgMTIsIDIpOwogICAgICAgICAgICBjYXNlICJIIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQuaG91cik7CiAgICAgICAgICAgIGNhc2UgIkhIIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQuaG91ciwgMik7CiAgICAgICAgICAgIC8vIG9mZnNldAogICAgICAgICAgICBjYXNlICJaIjoKICAgICAgICAgICAgICAvLyBsaWtlICs2CiAgICAgICAgICAgICAgcmV0dXJuIGZvcm1hdE9mZnNldCh7IGZvcm1hdDogIm5hcnJvdyIsIGFsbG93WjogdGhpcy5vcHRzLmFsbG93WiB9KTsKICAgICAgICAgICAgY2FzZSAiWloiOgogICAgICAgICAgICAgIC8vIGxpa2UgKzA2OjAwCiAgICAgICAgICAgICAgcmV0dXJuIGZvcm1hdE9mZnNldCh7IGZvcm1hdDogInNob3J0IiwgYWxsb3daOiB0aGlzLm9wdHMuYWxsb3daIH0pOwogICAgICAgICAgICBjYXNlICJaWloiOgogICAgICAgICAgICAgIC8vIGxpa2UgKzA2MDAKICAgICAgICAgICAgICByZXR1cm4gZm9ybWF0T2Zmc2V0KHsgZm9ybWF0OiAidGVjaGllIiwgYWxsb3daOiB0aGlzLm9wdHMuYWxsb3daIH0pOwogICAgICAgICAgICBjYXNlICJaWlpaIjoKICAgICAgICAgICAgICAvLyBsaWtlIEVTVAogICAgICAgICAgICAgIHJldHVybiBkdC56b25lLm9mZnNldE5hbWUoZHQudHMsIHsgZm9ybWF0OiAic2hvcnQiLCBsb2NhbGU6IHRoaXMubG9jLmxvY2FsZSB9KTsKICAgICAgICAgICAgY2FzZSAiWlpaWloiOgogICAgICAgICAgICAgIC8vIGxpa2UgRWFzdGVybiBTdGFuZGFyZCBUaW1lCiAgICAgICAgICAgICAgcmV0dXJuIGR0LnpvbmUub2Zmc2V0TmFtZShkdC50cywgeyBmb3JtYXQ6ICJsb25nIiwgbG9jYWxlOiB0aGlzLmxvYy5sb2NhbGUgfSk7CiAgICAgICAgICAgIC8vIHpvbmUKICAgICAgICAgICAgY2FzZSAieiI6CiAgICAgICAgICAgICAgLy8gbGlrZSBBbWVyaWNhL05ld19Zb3JrCiAgICAgICAgICAgICAgcmV0dXJuIGR0LnpvbmVOYW1lOwogICAgICAgICAgICAvLyBtZXJpZGllbXMKICAgICAgICAgICAgY2FzZSAiYSI6CiAgICAgICAgICAgICAgcmV0dXJuIG1lcmlkaWVtKCk7CiAgICAgICAgICAgIC8vIGRhdGVzCiAgICAgICAgICAgIGNhc2UgImQiOgogICAgICAgICAgICAgIHJldHVybiB1c2VEYXRlVGltZUZvcm1hdHRlciA/IHN0cmluZyh7IGRheTogIm51bWVyaWMiIH0sICJkYXkiKSA6IHRoaXMubnVtKGR0LmRheSk7CiAgICAgICAgICAgIGNhc2UgImRkIjoKICAgICAgICAgICAgICByZXR1cm4gdXNlRGF0ZVRpbWVGb3JtYXR0ZXIgPyBzdHJpbmcoeyBkYXk6ICIyLWRpZ2l0IiB9LCAiZGF5IikgOiB0aGlzLm51bShkdC5kYXksIDIpOwogICAgICAgICAgICAvLyB3ZWVrZGF5cyAtIHN0YW5kYWxvbmUKICAgICAgICAgICAgY2FzZSAiYyI6CiAgICAgICAgICAgICAgLy8gbGlrZSAxCiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtkYXkpOwogICAgICAgICAgICBjYXNlICJjY2MiOgogICAgICAgICAgICAgIC8vIGxpa2UgJ1R1ZXMnCiAgICAgICAgICAgICAgcmV0dXJuIHdlZWtkYXkoInNob3J0IiwgdHJ1ZSk7CiAgICAgICAgICAgIGNhc2UgImNjY2MiOgogICAgICAgICAgICAgIC8vIGxpa2UgJ1R1ZXNkYXknCiAgICAgICAgICAgICAgcmV0dXJuIHdlZWtkYXkoImxvbmciLCB0cnVlKTsKICAgICAgICAgICAgY2FzZSAiY2NjY2MiOgogICAgICAgICAgICAgIC8vIGxpa2UgJ1QnCiAgICAgICAgICAgICAgcmV0dXJuIHdlZWtkYXkoIm5hcnJvdyIsIHRydWUpOwogICAgICAgICAgICAvLyB3ZWVrZGF5cyAtIGZvcm1hdAogICAgICAgICAgICBjYXNlICJFIjoKICAgICAgICAgICAgICAvLyBsaWtlIDEKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQud2Vla2RheSk7CiAgICAgICAgICAgIGNhc2UgIkVFRSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAnVHVlcycKICAgICAgICAgICAgICByZXR1cm4gd2Vla2RheSgic2hvcnQiLCBmYWxzZSk7CiAgICAgICAgICAgIGNhc2UgIkVFRUUiOgogICAgICAgICAgICAgIC8vIGxpa2UgJ1R1ZXNkYXknCiAgICAgICAgICAgICAgcmV0dXJuIHdlZWtkYXkoImxvbmciLCBmYWxzZSk7CiAgICAgICAgICAgIGNhc2UgIkVFRUVFIjoKICAgICAgICAgICAgICAvLyBsaWtlICdUJwogICAgICAgICAgICAgIHJldHVybiB3ZWVrZGF5KCJuYXJyb3ciLCBmYWxzZSk7CiAgICAgICAgICAgIC8vIG1vbnRocyAtIHN0YW5kYWxvbmUKICAgICAgICAgICAgY2FzZSAiTCI6CiAgICAgICAgICAgICAgLy8gbGlrZSAxCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IG1vbnRoOiAibnVtZXJpYyIsIGRheTogIm51bWVyaWMiIH0sICJtb250aCIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0Lm1vbnRoKTsKICAgICAgICAgICAgY2FzZSAiTEwiOgogICAgICAgICAgICAgIC8vIGxpa2UgMDEsIGRvZXNuJ3Qgc2VlbSB0byB3b3JrCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IG1vbnRoOiAiMi1kaWdpdCIsIGRheTogIm51bWVyaWMiIH0sICJtb250aCIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0Lm1vbnRoLCAyKTsKICAgICAgICAgICAgY2FzZSAiTExMIjoKICAgICAgICAgICAgICAvLyBsaWtlIEphbgogICAgICAgICAgICAgIHJldHVybiBtb250aCgic2hvcnQiLCB0cnVlKTsKICAgICAgICAgICAgY2FzZSAiTExMTCI6CiAgICAgICAgICAgICAgLy8gbGlrZSBKYW51YXJ5CiAgICAgICAgICAgICAgcmV0dXJuIG1vbnRoKCJsb25nIiwgdHJ1ZSk7CiAgICAgICAgICAgIGNhc2UgIkxMTExMIjoKICAgICAgICAgICAgICAvLyBsaWtlIEoKICAgICAgICAgICAgICByZXR1cm4gbW9udGgoIm5hcnJvdyIsIHRydWUpOwogICAgICAgICAgICAvLyBtb250aHMgLSBmb3JtYXQKICAgICAgICAgICAgY2FzZSAiTSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAxCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IG1vbnRoOiAibnVtZXJpYyIgfSwgIm1vbnRoIikKICAgICAgICAgICAgICAgIDogdGhpcy5udW0oZHQubW9udGgpOwogICAgICAgICAgICBjYXNlICJNTSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAwMQogICAgICAgICAgICAgIHJldHVybiB1c2VEYXRlVGltZUZvcm1hdHRlcgogICAgICAgICAgICAgICAgPyBzdHJpbmcoeyBtb250aDogIjItZGlnaXQiIH0sICJtb250aCIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0Lm1vbnRoLCAyKTsKICAgICAgICAgICAgY2FzZSAiTU1NIjoKICAgICAgICAgICAgICAvLyBsaWtlIEphbgogICAgICAgICAgICAgIHJldHVybiBtb250aCgic2hvcnQiLCBmYWxzZSk7CiAgICAgICAgICAgIGNhc2UgIk1NTU0iOgogICAgICAgICAgICAgIC8vIGxpa2UgSmFudWFyeQogICAgICAgICAgICAgIHJldHVybiBtb250aCgibG9uZyIsIGZhbHNlKTsKICAgICAgICAgICAgY2FzZSAiTU1NTU0iOgogICAgICAgICAgICAgIC8vIGxpa2UgSgogICAgICAgICAgICAgIHJldHVybiBtb250aCgibmFycm93IiwgZmFsc2UpOwogICAgICAgICAgICAvLyB5ZWFycwogICAgICAgICAgICBjYXNlICJ5IjoKICAgICAgICAgICAgICAvLyBsaWtlIDIwMTQKICAgICAgICAgICAgICByZXR1cm4gdXNlRGF0ZVRpbWVGb3JtYXR0ZXIgPyBzdHJpbmcoeyB5ZWFyOiAibnVtZXJpYyIgfSwgInllYXIiKSA6IHRoaXMubnVtKGR0LnllYXIpOwogICAgICAgICAgICBjYXNlICJ5eSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAxNAogICAgICAgICAgICAgIHJldHVybiB1c2VEYXRlVGltZUZvcm1hdHRlcgogICAgICAgICAgICAgICAgPyBzdHJpbmcoeyB5ZWFyOiAiMi1kaWdpdCIgfSwgInllYXIiKQogICAgICAgICAgICAgICAgOiB0aGlzLm51bShkdC55ZWFyLnRvU3RyaW5nKCkuc2xpY2UoLTIpLCAyKTsKICAgICAgICAgICAgY2FzZSAieXl5eSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAwMDEyCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IHllYXI6ICJudW1lcmljIiB9LCAieWVhciIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0LnllYXIsIDQpOwogICAgICAgICAgICBjYXNlICJ5eXl5eXkiOgogICAgICAgICAgICAgIC8vIGxpa2UgMDAwMDEyCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IHllYXI6ICJudW1lcmljIiB9LCAieWVhciIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0LnllYXIsIDYpOwogICAgICAgICAgICAvLyBlcmFzCiAgICAgICAgICAgIGNhc2UgIkciOgogICAgICAgICAgICAgIC8vIGxpa2UgQUQKICAgICAgICAgICAgICByZXR1cm4gZXJhKCJzaG9ydCIpOwogICAgICAgICAgICBjYXNlICJHRyI6CiAgICAgICAgICAgICAgLy8gbGlrZSBBbm5vIERvbWluaQogICAgICAgICAgICAgIHJldHVybiBlcmEoImxvbmciKTsKICAgICAgICAgICAgY2FzZSAiR0dHR0ciOgogICAgICAgICAgICAgIHJldHVybiBlcmEoIm5hcnJvdyIpOwogICAgICAgICAgICBjYXNlICJrayI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtZZWFyLnRvU3RyaW5nKCkuc2xpY2UoLTIpLCAyKTsKICAgICAgICAgICAgY2FzZSAia2trayI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtZZWFyLCA0KTsKICAgICAgICAgICAgY2FzZSAiVyI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtOdW1iZXIpOwogICAgICAgICAgICBjYXNlICJXVyI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtOdW1iZXIsIDIpOwogICAgICAgICAgICBjYXNlICJvIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQub3JkaW5hbCk7CiAgICAgICAgICAgIGNhc2UgIm9vbyI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0Lm9yZGluYWwsIDMpOwogICAgICAgICAgICBjYXNlICJxIjoKICAgICAgICAgICAgICAvLyBsaWtlIDEKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQucXVhcnRlcik7CiAgICAgICAgICAgIGNhc2UgInFxIjoKICAgICAgICAgICAgICAvLyBsaWtlIDAxCiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LnF1YXJ0ZXIsIDIpOwogICAgICAgICAgICBjYXNlICJYIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oTWF0aC5mbG9vcihkdC50cyAvIDEwMDApKTsKICAgICAgICAgICAgY2FzZSAieCI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LnRzKTsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICByZXR1cm4gbWF5YmVNYWNybyh0b2tlbik7CiAgICAgICAgICB9CiAgICAgICAgfTsKCiAgICAgIHJldHVybiBzdHJpbmdpZnlUb2tlbnMoRm9ybWF0dGVyLnBhcnNlRm9ybWF0KGZtdCksIHRva2VuVG9TdHJpbmcpOwogICAgfQoKICAgIGZvcm1hdER1cmF0aW9uRnJvbVN0cmluZyhkdXIsIGZtdCkgewogICAgICBjb25zdCB0b2tlblRvRmllbGQgPSAodG9rZW4pID0+IHsKICAgICAgICAgIHN3aXRjaCAodG9rZW5bMF0pIHsKICAgICAgICAgICAgY2FzZSAiUyI6CiAgICAgICAgICAgICAgcmV0dXJuICJtaWxsaXNlY29uZCI7CiAgICAgICAgICAgIGNhc2UgInMiOgogICAgICAgICAgICAgIHJldHVybiAic2Vjb25kIjsKICAgICAgICAgICAgY2FzZSAibSI6CiAgICAgICAgICAgICAgcmV0dXJuICJtaW51dGUiOwogICAgICAgICAgICBjYXNlICJoIjoKICAgICAgICAgICAgICByZXR1cm4gImhvdXIiOwogICAgICAgICAgICBjYXNlICJkIjoKICAgICAgICAgICAgICByZXR1cm4gImRheSI7CiAgICAgICAgICAgIGNhc2UgInciOgogICAgICAgICAgICAgIHJldHVybiAid2VlayI7CiAgICAgICAgICAgIGNhc2UgIk0iOgogICAgICAgICAgICAgIHJldHVybiAibW9udGgiOwogICAgICAgICAgICBjYXNlICJ5IjoKICAgICAgICAgICAgICByZXR1cm4gInllYXIiOwogICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgIHJldHVybiBudWxsOwogICAgICAgICAgfQogICAgICAgIH0sCiAgICAgICAgdG9rZW5Ub1N0cmluZyA9IChsaWxkdXIpID0+ICh0b2tlbikgPT4gewogICAgICAgICAgY29uc3QgbWFwcGVkID0gdG9rZW5Ub0ZpZWxkKHRva2VuKTsKICAgICAgICAgIGlmIChtYXBwZWQpIHsKICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGxpbGR1ci5nZXQobWFwcGVkKSwgdG9rZW4ubGVuZ3RoKTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldHVybiB0b2tlbjsKICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgIHRva2VucyA9IEZvcm1hdHRlci5wYXJzZUZvcm1hdChmbXQpLAogICAgICAgIHJlYWxUb2tlbnMgPSB0b2tlbnMucmVkdWNlKAogICAgICAgICAgKGZvdW5kLCB7IGxpdGVyYWwsIHZhbCB9KSA9PiAobGl0ZXJhbCA/IGZvdW5kIDogZm91bmQuY29uY2F0KHZhbCkpLAogICAgICAgICAgW10KICAgICAgICApLAogICAgICAgIGNvbGxhcHNlZCA9IGR1ci5zaGlmdFRvKC4uLnJlYWxUb2tlbnMubWFwKHRva2VuVG9GaWVsZCkuZmlsdGVyKCh0KSA9PiB0KSk7CiAgICAgIHJldHVybiBzdHJpbmdpZnlUb2tlbnModG9rZW5zLCB0b2tlblRvU3RyaW5nKGNvbGxhcHNlZCkpOw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pbGxpc2Vjb25kczogNjAgKiAxMDAwIH0sCiAgICAgIHNlY29uZHM6IHsgbWlsbGlzZWNvbmRzOiAxMDAwIH0sCiAgICB9LAogICAgY2FzdWFsTWF0cml4ID0gewogICAgICB5ZWFyczogewogICAgICAgIHF1YXJ0ZXJzOiA0LAogICAgICAgIG1vbnRoczogMTIsCiAgICAgICAgd2Vla3M6IDUyLAogICAgICAgIGRheXM6IDM2NSwKICAgICAgICBob3VyczogMzY1ICogMjQsCiAgICAgICAgbWludXRlczogMzY1ICogMjQgKiA2MCwKICAgICAgICBzZWNvbmRzOiAzNjUgKiAyNCAqIDYwICogNjAsCiAgICAgICAgbWlsbGlzZWNvbmRzOiAzNjUgKiAyNCAqIDYwICogNjAgKiAxMDAwLAogICAgICB9LAogICAgICBxdWFydGVyczogewogICAgICAgIG1vbnRoczogMywKICAgICAgICB3ZWVrczogMTMsCiAgICAgICAgZGF5czogOTEsCiAgICAgICAgaG91cnM6IDkxICogMjQsCiAgICAgICAgbWludXRlczogOTEgKiAyNCAqIDYwLAogICAgICAgIHNlY29uZHM6IDkxICogMjQgKiA2MCAqIDYwLAogICAgICAgIG1pbGxpc2Vjb25kczogOTEgKiAyNCAqIDYwICogNjAgKiAxMDAwLAogICAgICB9LAogICAgICBtb250aHM6IHsKICAgICAgICB3ZWVrczogNCwKICAgICAgICBkYXlzOiAzMCwKICAgICAgICBob3VyczogMzAgKiAyNCwKICAgICAgICBtaW51dGVzOiAzMCAqIDI0ICogNjAsCiAgICAgICAgc2Vjb25kczogMzAgKiAyNCAqIDYwICogNjAsCiAgICAgICAgbWlsbGlzZWNvbmRzOiAzMCAqIDI0ICogNjAgKiA2MCAqIDEwMDAsCiAgICAgIH0sCgogICAgICAuLi5sb3dPcmRlck1hdHJpeCwKICAgIH0sCiAgICBkYXlzSW5ZZWFyQWNjdXJhdGUgPSAxNDYwOTcuMCAvIDQwMCwKICAgIGRheXNJbk1vbnRoQWNjdXJhdGUgPSAxNDYwOTcuMCAvIDQ4MDAsCiAgICBhY2N1cmF0ZU1hdHJpeCA9IHsKICAgICAgeWVhcnM6IHsKICAgICAgICBxdWFydGVyczogNCwKICAgICAgICBtb250aHM6IDEyLAogICAgICAgIHdlZWtzOiBkYXlzSW5ZZWFyQWNjdXJhdGUgLyA3LAogICAgICAgIGRheXM6IGRheXNJblllYXJBY2N1cmF0ZSwKICAgICAgICBob3VyczogZGF5c0luWWVhckFjY3VyYXRlICogMjQsCiAgICAgICAgbWludXRlczogZGF5c0luWWVhckFjY3VyYXRlICogMjQgKiA2MCwKICAgICAgICBzZWNvbmRzOiBkYXlzSW5ZZWFyQWNjdXJhdGUgKiAyNCAqIDYwICogNjAsCiAgICAgICAgbWlsbGlzZWNvbmRzOiBkYXlzSW5ZZWFyQWNjdXJhdGUgKiAyNCAqIDYwICogNjAgKiAxMDAwLAogICAgICB9LAogICAgICBxdWFydGVyczogewogICAgICAgIG1vbnRoczogMywKICAgICAgICB3ZWVrczogZGF5c0luWWVhckFjY3VyYXRlIC8gMjgsCiAgICAgICAgZGF5czogZGF5c0luWWVhckFjY3VyYXRlIC8gNCwKICAgICAgICBob3VyczogKGRheXNJblllYXJBY2N1cmF0ZSAqIDI0KSAvIDQsCiAgICAgICAgbWludXRlczogKGRheXNJblllYXJBY2N1cmF0ZSAqIDI0ICogNjApIC8gNCwKICAgICAgICBzZWNvbmRzOiAoZGF5c0luWWVhckFjY3VyYXRlICogMjQgKiA2MCAqIDYwKSAvIDQsCiAgICAgICAgbWlsbGlzZWNvbmRzOiAoZGF5c0luWWVhckFjY3VyYXRlICogMjQgKiA2MCAqIDYwICogMTAwMCkgLyA0LAogICAgICB9LAogICAgICBtb250aHM6IHsKICAgICAgICB3ZWVrczogZGF5c0luTW9udGhBY2N1cmF0ZSAvIDcsCiAgICAgICAgZGF5czogZGF5c0luTW9udGhBY2N1cmF0ZSwKICAgICAgICBob3VyczogZGF5c0luTW9udGhBY2N1cmF0ZSAqIDI0LAogICAgICAgIG1pbnV0ZXM6IG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XkgZnJvbSAwCiAgICAgICAgICAvLyBpZiBwcmV2aW91c1ZhbCA8IDAgaXQgbWFrZXMgdGhlIGFic29sdXRlIHZhbHVlIGxhcmdlcgogICAgICAgICAgLy8gaWYgcHJldmlvdXNWYWwgPj0gaXQgbWFrZXMgdGhlIGFic29sdXRlIHZhbHVlIHNtYWxsZXIKICAgICAgICAgIGNvbnN0IHJvbGxVcCA9IE1hdGguZmxvb3IocHJldmlvdXNWYWwgLyBjb252KTsKICAgICAgICAgIHZhbHNbY3VycmVudF0gKz0gcm9sbFVwICogZmFjdG9yOwogICAgICAgICAgdmFsc1twcmV2aW91c10gLT0gcm9sbFVwICogY29udiAqIGZhY3RvcjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGN1cnJlbnQ7CiAgICAgIH0gZWxzZSB7CiAgICAgICAgcmV0dXJuIHByZXZpb3VzOwogICAgICB9CiAgICB9LCBudWxsKTsKCiAgICAvLyB0cnkgdG8gY29udmVydCBhbnkgZGVjaW1hbHMgaW50byBzbWFsbGVyIHVuaXRzIGlmIHBvc3NpYmxlCiAgICAvLyBmb3IgZXhhbXBsZSBmb3IgeyB5ZWFyczogMi41LCBkYXlzOiAwLCBzZWNvbmRzOiAwIH0gd2Ugd2FudCB0byBnZXQgeyB5ZWFyczogMiwgZGF5czogMTgyLCBob3VyczogMTIgfQogICAgb3JkZXJlZFVuaXRzJDEucmVkdWNlKChwcmV2aW91cywgY3VycmVudCkgPT4gewogICAgICBpZiAoIWlzVW5kZWZpbmVkKHZhbHNbY3VycmVudF0pKSB7CiAgICAgICAgaWYgKHByZXZpb3VzKSB7CiAgICAgICAgICBjb25zdCBmcmFjdGlvbiA9IHZhbHNbcHJldmlvdXNdICUgMTsKICAgICAgICAgIHZhbHNbcHJldmlvdXNdIC09IGZyYWN0aW9uOwogICAgICAgICAgdmFsc1tjdXJyZW50XSArPSBmcmFjdGlvbiAqIG1hdHJpeFtwcmV2aW91c11bY3VycmVudF07CiAgICAgICAgfQogICAgICAg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gdGhlIHByZXNldCBjb252ZXJzaW9uIHN5c3RlbSB0byB1c2UKICAgICAqIEBwYXJhbSB7c3RyaW5nfSBbb3B0cy5tYXRyaXg9T2JqZWN0XSAtIHRoZSBjdXN0b20gY29udmVyc2lvbiBzeXN0ZW0gdG8gdXNlCiAgICAgKiBAcmV0dXJuIHtEdXJhdGlvbn0KICAgICAqLwogICAgc3RhdGljIGZyb21PYmplY3Qob2JqLCBvcHRzID0ge30pIHsKICAgICAgaWYgKG9iaiA9PSBudWxsIHx8IHR5cGVvZiBvYmogIT09ICJvYmplY3QiKSB7CiAgICAgICAgdGhyb3cgbmV3IEludmFsaWRBcmd1bWVudEVycm9yKAogICAgICAgICAgYER1cmF0aW9uLmZyb21PYmplY3Q6IGFyZ3VtZW50IGV4cGVjdGVkIHRvIGJlIGFuIG9iamVjdCwgZ290ICR7CiAgICAgICAgICBvYmogPT09IG51bGwgPyAibnVsbCIgOiB0eXBlb2Ygb2JqCiAgICAgICAgfWAKICAgICAgICApOwogICAgICB9CgogICAgICByZXR1cm4gbmV3IER1cmF0aW9uKHsKICAgICAgICB2YWx1ZXM6IG5vcm1hbGl6ZU9iamVjdChvYmosIER1cmF0aW9uLm5vcm1hbGl6ZVVuaXQpLAogICAgICAgIGxvYzogTG9jYWxlLmZyb21PYmplY3Qob3B0cyksCiAgICAgICAgY29udmVyc2lvbkFjY3VyYWN5OiBvcHRzLmNvbnZlcnNpb25BY2N1cmFjeSwKICAgICAgICBtYXRyaXg6IG9wdHMubWF0cml4LAogICAgICB9KTsKICAgIH0KCiAgICAvKioKICAgICAqIENyZWF0ZSBhIER1cmF0aW9uIGZyb20gRHVyYXRpb25MaWtlLgogICAgICoKICAgICAqIEBwYXJhbSB7T2JqZWN0IHwgbnVtYmVyIHwgRHVyYXRpb259IGR1cmF0aW9uTGlrZQogICAgICogT25lIG9mOgogICAgICogLSBvYmplY3Qgd2l0aCBrZXlzIGxpa2UgJ3llYXJzJyBhbmQgJ2hvdXJzJy4KICAgIC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IC0gbG9uZ2VyIGV4cGxhbmF0aW9uLCBtYXkgaW5jbHVkZSBwYXJhbWV0ZXJzIGFuZCBvdGhlciB1c2VmdWwgZGVidWdnaW5nIGluZm9ybWF0aW9uCiAgICAgKiBAcmV0dXJuIHtEdXJhdGlvbn0KICAgICAqLwogICAgc3RhdGljIGludmFsaWQocmVhc29uLCBleHBsYW5hdGlvbiA9IG51bGwpIHsKICAgICAgaWYgKCFyZWFzb24pIHsKICAgICAgICB0aHJvdyBuZXcgSW52YWxpZEFyZ3VtZW50RXJyb3IoIm5lZWQgdG8gc3BlY2lmeSBhIHJlYXNvbiB0aGUgRHVyYXRpb24gaXMgaW52YWxpZCIpOwogICAgICB9CgogICAgICBjb25zdCBpbnZhbGlkID0gcmVhc29uIGluc3RhbmNlb2YgSW52YWxpZCA/IHJlYXNvbiA6IG5ldyBJbnZhbGlkKHJlYXNvbiwgZXhwbGFuYXRpb24pOwoKICAgICAgaWYgKFNldHRpbmdzLnRocm93T25JbnZhbGlkKSB7CiAgICAgICAgdGhyb3cgbmV3IEludmFsaWREdXJhdGlvbkVycm9yKGludmFsaWQpOwogICAgICB9IGVsc2UgewogICAgICAgIHJldHVybiBuZXcgRHVyYXRpb24oeyBpbnZhbGlkIH0pOwogICAgICB9CiAgICB9CgogICAgLyoqCiAgICAgKiBAcHJpdmF0ZQogICAgICovCiAgICBzdGF0aWMgbm9ybWFsaXplVW5pdCh1bml0KSB7CiAgICAgIGNvbnN0IG5vcm1hbGl6ZWQgPSB7CiAgICAgICAgeWVhcjogInllYXJzIiwKICAgICAgICB5ZWFyczogInllYXJzIiwKICAgICAgICBxdWFydGVyOiAicXVhcnRlcnMiLAogICAgICAgIHF1YXJ0ZXJzOiAicXVhcnRlcnMiLAogICAgICAgIG1vbnRoOiAibW9udGhzIiwKICAgICAgICBtb250aHM6ICJtb250aHMiLAogICAgICAgIHdlZWs6ICJ3ZWVrcyIsCiAgICAgICAgd2Vla3M6ICJ3ZWVrcyIsCiAgICAgICAgZGF5OiAiZGF5cyIsCiAgICAgICAgZGF5czogImRheXMiLAogICAgICAgIGhvdXI6ICJob3VycyIsCiAgICAgICAgaG91cnM6ICJob3VycyIsCiAgICAgICAgbWludXRlOiAibWludXRlcyIsCiAgICAgICAgbWludXRlczogIm1pbnV0ZXMiLAogICAgICAgIHNlY29uZDogInNlY29uZHMiLAogICAgICAgIHNlY29uZHM6ICJzZWNvbmRzIiwKICAgICAgICBtaWxsaXNlY29uZDogIm1pbGxpc2Vjb25kcyIsCiAgICAgICAgbWlsbGlzZWNvbmRzOiAibWlsbGlzZWNvbmRzIiwKICAgICAgfVt1bml0ID8gdW5pdC50b0xvd2VyQ2FzZSgpIDogdW5pdF07CgogICAgICBpZiAoIW5vcm1hbGl6ZWQpIHRocm93IG5ldyBJbnZhbGlkVW5pdEVycm9yKHVuaXQpOwoKICAgICAgcmV0dXJuIG5vcm1hbGl6ZWQ7CiAgICB9CgogICAgLyoqCiAgICAgKiBDaGVjayBpZiBhbiBvYmplY3QgaXMgYSBEdXJhdGlvbi4gV29ya3MgYWNyb3NzIGNvbnRleHQgYm91bmRhcmllcwogICAgICogQHBhcmFtIHtvYmplY3R9IG8KICAgICAqIEByZXR1cm4ge2Jvb2xlYW59CiAgICAgKi8KICAgIHN0YXRpYyBpc0R1cmF0aW9uKG8pIHsKICAgICAgcmV0dXJuIChvICYmIG8uaXNMdXhvbkR1cmF0aW9uKSB8fCBmYWxzZTsKICAgIH0KCiAgICAvKioKICAgICAqIEdldCAgdGhlIGxvY2FsZSBvZiBhIER1cmF0aW9uLCBzdWNoICdlbi1HQicKICAgICAqIEB0eXBlIHtzdHJpbmd9CiAgICAgKi8KICAgIGdldCBsb2NhbGUoKSB7CiAgICAgIHJldHVybiB0aGlzLmlzVmFsaWQgPyB0aGlzLmxvYy5sb2NhbGUgOiBudWxsOwogICAgfQoKICAgIC8qKgogICAgICogR2V0IHRoZSBudW1iZXJpbmcgc3lzdGVtIG9mIGEgRHVyYXRpb24sIHN1Y2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sZTogImxvbmciIH0pIC8vPT4gJzEgZGF5LCA1IGhvdXJzLCBhbmQgNiBtaW51dGVzJwogICAgICogZHVyLnRvSHVtYW4oeyB1bml0RGlzcGxheTogInNob3J0IiB9KSAvLz0+ICcxIGRheSwgNSBociwgNiBtaW4nCiAgICAgKiBgYGAKICAgICAqLwogICAgdG9IdW1hbihvcHRzID0ge30pIHsKICAgICAgaWYgKCF0aGlzLmlzVmFsaWQpIHJldHVybiBJTlZBTElEJDI7CgogICAgICBjb25zdCBsID0gb3JkZXJlZFVuaXRzJDEKICAgICAgICAubWFwKCh1bml0KSA9PiB7CiAgICAgICAgICBjb25zdCB2YWwgPSB0aGlzLnZhbHVlc1t1bml0XTsKICAgICAgICAgIGlmIChpc1VuZGVmaW5lZCh2YWwpKSB7CiAgICAgICAgICAgIHJldHVybiBudWxsOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHRoaXMubG9jCiAgICAgICAgICAgIC5udW1iZXJGb3JtYXR0ZXIoeyBzdHlsZTogInVuaXQiLCB1bml0RGlzcGxheTogImxvbmciLCAuLi5vcHRzLCB1bml0OiB1bml0LnNsaWNlKDAsIC0xKSB9KQogICAgICAgICAgICAuZm9ybWF0KHZhbCk7CiAgICAgICAgfSkKICAgICAgICAuZmlsdGVyKChuKSA9PiBuKTsKCiAgICAgIHJldHVybiB0aGlzLmxvYwogICAgICAgIC5saXN0Rm9ybWF0dGVyKHsgdHlwZTogImNvbmp1bmN0aW9uIiwgc3R5bGU6IG9wdHMubGlzdFN0eWxlIHx8ICJuYXJyb3ciLCAuLi5vcHRzIH0pCiAgICAgICAgLmZvcm1hdChsKTsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgYSBKYXZhU2NyaXB0IG9iamVjdCB3aXRoIHRoaXMgRHVyYXRpb24ncyB2YWx1ZXMuCiAgICAgKiBAZXhhbXBsZSBEdXJhdGlvbi5mcm9tT2JqZWN0KHsgeWVhcnM6IDEsIGRheXM6IDYsIHNlY29uZHM6IDIgfSkudG9PYmplY3QoKSAvLz0+IHsgeWVhcnM6IDEsIGRheXM6IDYsIHNlY29uZHM6IDIgfQogICAgICogQHJldHVybiB7T2JqZWN0fQogICAgICovCiAgICB0b09iamVjdCgpIHsKICAgICAgaWYgKCF0aGlzLmlzVmFsaWQpIHJldHVybiB7fTsKICAgICAgcmV0dXJuIHsgLi4udGhpcy52YWx1ZXMgfTsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgYW4gSVNPIDg2MDEtY29tcGxpYW50IHN0cmluZyByZXByZXNlbnRhdGlvbiBvZiB0aGlzIER1cmF0aW9uLgogICAgICogQHNlZSBodHRwczovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9JU09fODYwMSNEdXJhdGlvbnMKICAgICAqIEBleGFtcGxlIER1cmF0aW9uLmZyb21PYmplY3QoeyB5ZWFyczogMywgc2Vjb25kczogNDUgfSkudG9JU08oKSAvLz0+ICdQM1lUNDVTJwogICAgICogQGV4YW1wbGUgRHVyYXRpb24uZnJvbU9iamVjdCh7IG1vbnRoczogNCwgc2Vjb25kczogNDUgfSkudG9JU08oKSAvLz0+ICdQNE1UNDVTJwogICAgICogQGV4YW1wbGUgRHVyYXRpb24uZnJvbU9iamVjdCh7IG1vbnRoczogNSB9KS50b0lTTygpIC8vPT4gJ1A1TScKICAgICAqIEBleGFtcGxlIER1cmF0aW9uLmZyb21PYmplY3QoeyBtaW51dGVzOiA1IH0pLnRvSVNPKCkgLy89PiAnUFQ1TScKICAgICAqIEBleGFtcGxlIER1cmF0aW9uLmZyb21PYmplY3QoeyBtaWxsaXNlY29uZHM6IDYgfSkudG9JU08oKSAvLz0+ICdQVDAuMDA2UycKICAgICAqIEByZXR1cm4ge3N0cmluZ30KICAgICAqLwogICAgdG9JU08oKSB7CiAgICAgIC8vIHdlIGNvdWxkIHVzZSB0aGUgZm9ybWF0dGVyLCBidXQgdGhpcyBpcyBhbiBlYXNpZXIgd2F5IHRvIGdldCB0aGUgbWluaW11bSBzdH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gICBjb25zdCBkYXRlVGltZSA9IERhdGVUaW1lLmZyb21NaWxsaXMobWlsbGlzLCB7IHpvbmU6ICJVVEMiIH0pOwogICAgICByZXR1cm4gZGF0ZVRpbWUudG9JU09UaW1lKG9wdHMpOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJucyBhbiBJU08gODYwMSByZXByZXNlbnRhdGlvbiBvZiB0aGlzIER1cmF0aW9uIGFwcHJvcHJpYXRlIGZvciB1c2UgaW4gSlNPTi4KICAgICAqIEByZXR1cm4ge3N0cmluZ30KICAgICAqLwogICAgdG9KU09OKCkgewogICAgICByZXR1cm4gdGhpcy50b0lTTygpOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJucyBhbiBJU08gODYwMSByZXByZXNlbnRhdGlvbiBvZiB0aGlzIER1cmF0aW9uIGFwcHJvcHJpYXRlIGZvciB1c2UgaW4gZGVidWdnaW5nLgogICAgICogQHJldHVybiB7c3RyaW5nfQogICAgICovCiAgICB0b1N0cmluZygpIHsKICAgICAgcmV0dXJuIHRoaXMudG9JU08oKTsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgYW4gbWlsbGlzZWNvbmRzIHZhbHVlIG9mIHRoaXMgRHVyYXRpb24uCiAgICAgKiBAcmV0dXJuIHtudW1iZXJ9CiAgICAgKi8KICAgIHRvTWlsbGlzKCkgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCkgcmV0dXJuIE5hTjsKCiAgICAgIHJldHVybiBkdXJhdGlvblRvTWlsbGlzKHRoaXMubWF0cml4LCB0aGlzLnZhbHVlcyk7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm5zIGFuIG1pbGxpc2Vjb25kcyB2YWx1ZSBvZiB0aGlzIER1cmF0aW9uLiBBbGlhcyBvZiB7QGxpbmsgdG9NaWxsaXN9CiAgICAgKiBAcmV0dXJuIHtudW1iZXJ9CiAgICAgKi8KICAgIHZhbHVlT2YoKSB7CiAgICAgIHJldHVybiB0aGlzLnRvTWl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plY3QoKSAvLz0+IHsgbWludXRlczogNjAsIG1pbGxpc2Vjb25kczogMzAwMDAgfQogICAgICogQHJldHVybiB7RHVyYXRpb259CiAgICAgKi8KICAgIHNoaWZ0VG8oLi4udW5pdHMpIHsKICAgICAgaWYgKCF0aGlzLmlzVmFsaWQpIHJldHVybiB0aGlzOwoKICAgICAgaWYgKHVuaXRzLmxlbmd0aCA9PT0gMCkgewogICAgICAgIHJldHVybiB0aGlzOwogICAgICB9CgogICAgICB1bml0cyA9IHVuaXRzLm1hcCgodSkgPT4gRHVyYXRpb24ubm9ybWFsaXplVW5pdCh1KSk7CgogICAgICBjb25zdCBidWlsdCA9IHt9LAogICAgICAgIGFjY3VtdWxhdGVkID0ge30sCiAgICAgICAgdmFscyA9IHRoaXMudG9PYmplY3QoKTsKICAgICAgbGV0IGxhc3RVbml0OwoKICAgICAgZm9yIChjb25zdCBrIG9mIG9yZGVyZWRVbml0cyQxKSB7CiAgICAgICAgaWYgKHVuaXRzLmluZGV4T2YoaykgPj0gMCkgewogICAgICAgICAgbGFzdFVuaXQgPSBrOwoKICAgICAgICAgIGxldCBvd24gPSAwOwoKICAgICAgICAgIC8vIGFueXRoaW5nIHdlIGhhdmVuJ3QgYm9pbGVkIGRvd24geWV0IHNob3VsZCBnZXQgYm9pbGVkIHRvIHRoaXMgdW5pdAogICAgICAgICAgZm9yIChjb25zdCBhayBpbiBhY2N1bXVsYXRlZCkgewogICAgICAgICAgICBvd24gKz0gdGhpcy5tYXRyaXhbYWtdW2tdICogYWNjdW11bGF0ZWRbYWtdOwogICAgICAgICAgICBhY2N1bXVsYXRlZFtha10gPSAwOwogICAgICAgICAgfQoKICAgICAgICAgIC8vIHBsdXMgYW55dGhpbmcgdGhhdCdzIGFscmVhZHkgaW4gdGhpcyB1bml0CiAgICAgICAgICBpZiAoaXNOdW1iZXIodmFsc1trXSkpIHsKICAgICAgICAgICAgb3duICs9IHZhbHNba107CiAgICAgICAgICB9CgogICAgICAgICAgLy8gb25seSBr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gLyoqCiAgICAgKiBTaGlmdCB0aGlzIER1cmF0aW9uIHRvIGFsbCBhdmFpbGFibGUgdW5pdHMuCiAgICAgKiBTYW1lIGFzIHNoaWZ0VG8oInllYXJzIiwgIm1vbnRocyIsICJ3ZWVrcyIsICJkYXlzIiwgImhvdXJzIiwgIm1pbnV0ZXMiLCAic2Vjb25kcyIsICJtaWxsaXNlY29uZHMiKQogICAgICogQHJldHVybiB7RHVyYXRpb259CiAgICAgKi8KICAgIHNoaWZ0VG9BbGwoKSB7CiAgICAgIGlmICghdGhpcy5pc1ZhbGlkKSByZXR1cm4gdGhpczsKICAgICAgcmV0dXJuIHRoaXMuc2hpZnRUbygKICAgICAgICAieWVhcnMiLAogICAgICAgICJtb250aHMiLAogICAgICAgICJ3ZWVrcyIsCiAgICAgICAgImRheXMiLAogICAgICAgICJob3VycyIsCiAgICAgICAgIm1pbnV0ZXMiLAogICAgICAgICJzZWNvbmRzIiwKICAgICAgICAibWlsbGlzZWNvbmRzIgogICAgICApOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJuIHRoZSBuZWdhdGl2ZSBvZiB0aGlzIER1cmF0aW9uLgogICAgICogQGV4YW1wbGUgRHVyYXRpb24uZnJvbU9iamVjdCh7IGhvdXJzOiAxLCBzZWNvbmRzOiAzMCB9KS5uZWdhdGUoKS50b09iamVjdCgpIC8vPT4geyBob3VyczogLTEsIHNlY29uZHM6IC0zMCB9CiAgICAgKiBAcmV0dXJuIHtEdXJhdGlvbn0KICAgICAqLwogICAgbmVnYXRlKCkgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCkgcmV0dXJuIHRoaXM7CiAgICAgIGNvbnN0IG5lZ2F0ZWQgPSB7fTsKICAgICAgZm9yIChjb25zdCBrIG9mIE9iamVjdC5rZXlzKHRoaXMudmFsdWVzKSkgewogICAgICAgIG5lZ2F0ZWRba10gPSB0aGlzLnZhbHVlc1trXSA9PT0gMCA/IDAgOiAtdGhpcy52YWx1ZXNba107CiAgICAgIH0KICAgICAgcmV0dXJuIGNsb25lJDEodGhpcywgeyB2YWx1ZXM6IG5lZ2F0ZWQgfSwgdHJ1ZSk7CiAgICB9CgogICAgLyoqCiAgICAgKiBHZXQgdGhlIHllYXJzLgogICAgICogQHR5cGUge251bWJlcn0KICAgICAqLwogICAgZ2V0IHllYXJzKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy52YWx1ZXMueWVhcnMgfHwgMCA6IE5hTjsKICAgIH0KCiAgICAvKioKICAgICAqIEdldCB0aGUgcXVhcnRlcnMuCiAgICAgKiBAdHlwZSB7bnVtYmVyfQogICAgICovCiAgICBnZXQgcXVhcnRlcnMoKSB7CiAgICAgIHJldHVybiB0aGlzLmlzVmFsaWQgPyB0aGlzLnZhbHVlcy5xdWFydGVycyB8fCAwIDogTmFOOwogICAgfQoKICAgIC8qKgogICAgICogR2V0IHRoZSBtb250aHMuCiAgICAgKiBAdHlwZSB7bnVtYmVyfQogICAgICovCiAgICBnZXQgbW9udGhzKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy52YWx1ZXMubW9udGhzIHx8IDAgOiBOYU47CiAgICB9CgogICAgLyoqCiAgICAgKiBHZXQgdGhlIHdlZWtzCiAgICAgKiBAdHlwZSB7bnVtYmVyfQogICAgICovCiAgICBnZXQgd2Vla3MoKSB7CiAgICAgIHJldHVybiB0aGlzLmlzVmFsaWQgPyB0aGlzLnZhbHVlcy53ZWVrcyB8fCAwIDogTmFOOwogICAgfQoKICAgIC8qKgogICAgICogR2V0IHRoZSBkYXlzLgogICAgICogQHR5cGUge251bWJlcn0KICAgICAqLwogICAgZ2V0IGRheXMoKSB7CiAgICAgIHJldHVybiB0aGlzLmlzVmFsaWQgPyB0aGlzLnZhbHVlcy5kYXlzIHx8IDAgOiBOYU47CiAgICB9CgogICAgLyoqCiAgICAgKiBHZXQgdGhlIGhvdXJzLgogICAgICogQHR5cGUge251bWJlcn0KICAgICAqLwogICAgZ2V0IGhvdXJzKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy52YWx1ZXMuaG91cnMgfHwgMCA6IE5hTjsKICAgIH0KCiAgICAvKioKICAgICAqIEdldCB0aGUgbWludXRlcy4KICAgICAqIEB0eXBlIHtudW1iZXJ9CiAgICAgKi8KICAgIGdldCBtaW51dGVzKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy52YWx1ZXMubWludXRlcyB8fCAwIDogTmFOOwogICAgfQoKICAgIC8qKgogICAgICogR2V0IHRoZSBzZWNvbmRzLgogICAgICogQHJldHVybiB7bnVtYmVyfQogICAgICovCiAgICBnZXQgc2Vjb25kcygpIHsKICAgICAgcmV0dXJuIHRoaXMuaXNWYWxpZCA/IHRoaXMudmFsdWVzLnNlY29uZHMgfHwgMCA6IE5hTjsKICAgIH0KCiAgICAvKioKICAgICAqIEdldCB0aGUgbWlsbGlzZWNvbmRzLgogICAgICogQHJldHVybiB7bnVtYmVyfQogICAgICovCiAgICBnZXQgbWlsbGlzZWNvbmRzKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy52YWx1ZXMubWlsbGlzZWNvbmRzIHx8IDAgOiBOYU47CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm5zIHdoZXRoZXIgdGhlIER1cmF0aW9uIGlzIGludmFsaWQuIEludmFsaWQgZHVyYXRpb25zIGFyZSByZXR1cm5lZCBieSBkaWZmIG9wZXJhdGlvbnMKICAgICAqIG9uIGludmFsaWQgRGF0ZVRpbWVzIG9yIEludGVydmFscy4KICAgICAqIEByZXR1cm4ge2Jvb2xlYW59CiAgICAgKi8KICAgIGdldCBpc1ZhbGlkKCkgewogICAgICByZXR1cm4gdGhpcy5pbnZhbGlkID09PSBudWxsOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJucyBhbiBlcnJvciBjb2RlIGlmIHRoaXMgRHVyYXRpb24gYmVjYW1lIGludmFsaWQsIG9yI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7QGxpbmsgSW50ZXJ2YWwjdG9EdXJhdGlvbn0uCiAgICovCiAgY2xhc3MgSW50ZXJ2YWwgewogICAgLyoqCiAgICAgKiBAcHJpdmF0ZQogICAgICovCiAgICBjb25zdHJ1Y3Rvcihjb25maWcpIHsKICAgICAgLyoqCiAgICAgICAqIEBhY2Nlc3MgcHJpdmF0ZQogICAgICAgKi8KICAgICAgdGhpcy5zID0gY29uZmlnLnN0YXJ0OwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLmUgPSBjb25maWcuZW5kOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLmludmFsaWQgPSBjb25maWcuaW52YWxpZCB8fCBudWxsOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLmlzTHV4b25JbnRlcnZhbCA9IHRydWU7CiAgICB9CgogICAgLyoqCiAgICAgKiBDcmVhdGUgYW4gaW52YWxpZCBJbnRlcnZhbC4KICAgICAqIEBwYXJhbSB7c3RyaW5nfSByZWFzb24gLSBzaW1wbGUgc3RyaW5nIG9mIHdoeSB0aGlzIEludGVydmFsIGlzIGludmFsaWQuIFNob3VsZCBub3QgY29udGFpbiBwYXJhbWV0ZXJzIG9yIGFueXRoaW5nIGVsc2UgZGF0YS1kZXBlbmRlbnQKICAgICAqIEBwYXJhbSB7c3RyaW5nfSBbZXhwbGFuYXRpb249bnVsbF0gLSBsb25nZXIgZXhwbGFuYXRpb24sIG1heSBpbmNsdWRlIHBhcmFtZXRlcnMgYW5kIG90aGVyIHVzZWZ1bCBkZWJ1Z2dpbmcgaW5mb3JtYXRpb24KICAgICAqIEByZXR1cm4ge0ludGVydmFsfQogICAgICovCiAgICBzdGF0aWMgaW52YWxpZChyZWFzb24sIGV4cGxhbmF0aW9uID0gbnVsbCkgewogICAgICBpZiAoIXJlYXNvbikgewogICAgICA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IG90aGVyCiAgICAgKiBAcmV0dXJuIHtib29sZWFufQogICAgICovCiAgICBvdmVybGFwcyhvdGhlcikgewogICAgICByZXR1cm4gdGhpcy5lID4gb3RoZXIucyAmJiB0aGlzLnMgPCBvdGhlci5lOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJuIHdoZXRoZXIgdGhpcyBJbnRlcnZhbCdzIGVuZCBpcyBhZGphY2VudCB0byB0aGUgc3BlY2lmaWVkIEludGVydmFsJ3Mgc3RhcnQuCiAgICAgKiBAcGFyYW0ge0ludGVydmFsfSBvdGhlcgogICAgICogQHJldHVybiB7Ym9vbGVhbn0KICAgICAqLwogICAgYWJ1dHNTdGFydChvdGhlcikgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCkgcmV0dXJuIGZhbHNlOwogICAgICByZXR1cm4gK3RoaXMuZSA9PT0gK290aGVyLnM7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm4gd2hldGhlciB0aGlzIEludGVydmFsJ3Mgc3RhcnQgaXMgYWRqYWNlbnQgdG8gdGhlIHNwZWNpZmllZCBJbnRlcnZhbCdzIGVuZC4KICAgICAqIEBwYXJhbSB7SW50ZXJ2YWx9IG90aGVyCiAgICAgKiBAcmV0dXJuIHtib29sZWFufQogICAgICovCiAgICBhYnV0c0VuZChvdGhlcikgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCkgcmV0dXJuIGZhbHNlOwogICAgICByZXR1cm4gK290aGVyLmUgPT09ICt0aGlzLnM7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm4gd2hldGhlciB0aGlzIEludGVydmFsIGVuZ3VsZnMgdGhlIHN0YXJ0IGFuZCBlbmQgb2YgdGhlIHNwZWNpZmllZCBJbnRlcnZhbC4KICAgICAqIEBwYXJhbSB7SW50ZXJ2YWx9IG90aGVyCiAgICAgKiBAcmV0dXJuIHtib29sZWFufQogICAgICovCiAgICBlbmd1bGZzKG90aGVyKSB7CiAgICAgIGlmICghdGhpcy5pc1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IENvbWJpbmVzIG92ZXJsYXBwaW5nIGFuZCBhZGphY2VudCBJbnRlcnZhbHMuCiAgICAgKiBAcGFyYW0ge0FycmF5fSBpbnRlcnZhbHMKICAgICAqIEByZXR1cm4ge0FycmF5fQogICAgICovCiAgICBzdGF0aWMgbWVyZ2UoaW50ZXJ2YWxzKSB7CiAgICAgIGNvbnN0IFtmb3VuZCwgZmluYWxdID0gaW50ZXJ2YWxzCiAgICAgICAgLnNvcnQoKGEsIGIpID0+IGEucyAtIGIucykKICAgICAgICAucmVkdWNlKAogICAgICAgICAgKFtzb2ZhciwgY3VycmVudF0sIGl0ZW0pID0+IHsKICAgICAgICAgICAgaWYgKCFjdXJyZW50KSB7CiAgICAgICAgICAgICAgcmV0dXJuIFtzb2ZhciwgaXRlbV07CiAgICAgICAgICAgIH0gZWxzZSBpZiAoY3VycmVudC5vdmVybGFwcyhpdGVtKSB8fCBjdXJyZW50LmFidXRzU3RhcnQoaXRlbSkpIHsKICAgICAgICAgICAgICByZXR1cm4gW3NvZmFyLCBjdXJyZW50LnVuaW9uKGl0ZW0pXTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICByZXR1cm4gW3NvZmFyLmNvbmNhdChbY3VycmVudF0pLCBpdGVtXTsKICAgICAgICAgICAgfQogICAgICAgICAgfSwKICAgICAgICAgIFtbXSwgbnVsbF0KICAgICAgICApOwogICAgICBpZiAoZmluYWwpIHsKICAgICAgICBmb3VuZC5wdXNoKGZpbmFsKTsKICAgICAgfQogICAgICByZXR1cm4gZm91bmQ7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm4gYW4gYXJyYXkgb2YgSW50ZXJ2YWxzIHJlcHJlc2VudGluZyB0aGUgc3BhbnMgb2YgdGltZSB0aGF0IG9ubHkgYXBwZWFyIGluIG9uZSBvZiB0aGUgc3BlY2lmaWVkIEludGVydmFscy4KICAgICAqIEBwYXJhbSB7QXJyYXl9IGludGVydmFscwogICAgICogQHJldHVybiB7QXJyYX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1ZSB9KSwKICAgICAgdW5pdGF0ZSA9ICh0KSA9PiB7CiAgICAgICAgaWYgKHRva2VuLmxpdGVyYWwpIHsKICAgICAgICAgIHJldHVybiBsaXRlcmFsKHQpOwogICAgICAgIH0KICAgICAgICBzd2l0Y2ggKHQudmFsKSB7CiAgICAgICAgICAvLyBlcmEKICAgICAgICAgIGNhc2UgIkciOgogICAgICAgICAgICByZXR1cm4gb25lT2YobG9jLmVyYXMoInNob3J0IiksIDApOwogICAgICAgICAgY2FzZSAiR0ciOgogICAgICAgICAgICByZXR1cm4gb25lT2YobG9jLmVyYXMoImxvbmciKSwgMCk7CiAgICAgICAgICAvLyB5ZWFycwogICAgICAgICAgY2FzZSAieSI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZVRvU2l4KTsKICAgICAgICAgIGNhc2UgInl5IjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvVG9Gb3VyLCB1bnRydW5jYXRlWWVhcik7CiAgICAgICAgICBjYXNlICJ5eXl5IjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQoZm91cik7CiAgICAgICAgICBjYXNlICJ5eXl5eSI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KGZvdXJUb1NpeCk7CiAgICAgICAgICBjYXNlICJ5eXl5eXkiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChzaXgpOwogICAgICAgICAgLy8gbW9udGhzCiAgICAgICAgICBjYXNlICJNIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQob25lT3JUd28pOwogICAgICAgICAgY2FzZSAiTU0iOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdCh0d28pOwogICAgICAgICAgY2FzZSAiTU1NIjoKICAgICAgICAgICAgcmV0dXJuIG9uZU9mKGxvYy5tb250aHMoInNob3J0IiwgdHJ1ZSksIDEpOwogICAgICAgICAgY2FzZSAiTU1NTSI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2MubW9udGhzKCJsb25nIiwgdHJ1ZSksIDEpOwogICAgICAgICAgY2FzZSAiTCI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgIkxMIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIGNhc2UgIkxMTCI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2MubW9udGhzKCJzaG9ydCIsIGZhbHNlKSwgMSk7CiAgICAgICAgICBjYXNlICJMTExMIjoKICAgICAgICAgICAgcmV0dXJuIG9uZU9mKGxvYy5tb250aHMoImxvbmciLCBmYWxzZSksIDEpOwogICAgICAgICAgLy8gZGF0ZXMKICAgICAgICAgIGNhc2UgImQiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChvbmVPclR3byk7CiAgICAgICAgICBjYXNlICJkZCI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KHR3byk7CiAgICAgICAgICAvLyBvcmRpbmFscwogICAgICAgICAgY2FzZSAibyI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZVRvVGhyZWUpOwogICAgICAgICAgY2FzZSAib29vIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodGhyZWUpOwogICAgICAgICAgLy8gdGltZQogICAgICAgICAgY2FzZSAiSEgiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdCh0d28pOwogICAgICAgICAgY2FzZSAiSCI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgImhoIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIGNhc2UgImgiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChvbmVPclR3byk7CiAgICAgICAgICBjYXNlICJtbSI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KHR3byk7CiAgICAgICAgICBjYXNlICJtIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQob25lT3JUd28pOwogICAgICAgICAgY2FzZSAicSI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgInFxIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIGNhc2UgInMiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChvbmVPclR3byk7CiAgICAgICAgICBjYXNlICJzcyI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KHR3byk7CiAgICAgICAgICBjYXNlICJTIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQob25lVG9UaHJlZSk7CiAgICAgICAgICBjYXNlICJTU1MiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdCh0aHJlZSk7CiAgICAgICAgICBjYXNlICJ1IjoKICAgICAgICAgICAgcmV0dXJuIHNpbXBsZShvbmVUb05pbmUpOwogICAgICAgICAgY2FzZSAidXUiOgogICAgICAgICAgICByZXR1cm4gc2ltcGxlKG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgInV1dSI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZSk7CiAgICAgICAgICAvLyBtZXJpZGllbQogICAgICAgICAgY2FzZSAiYSI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2MubWVyaWRpZW1zKCksIDApOwogICAgICAgICAgLy8gd2Vla1llYXIgKGspCiAgICAgICAgICBjYXNlICJra2trIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQoZm91cik7CiAgICAgICAgICBjYXNlICJrayI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KHR3b1RvRm91ciwgdW50cnVuY2F0ZVllYXIpOwogICAgICAgICAgLy8gd2Vla051bWJlciAoVykKICAgICAgICAgIGNhc2UgIlciOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChvbmVPclR3byk7CiAgICAgICAgICBjYXNlICJXVyI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KHR3byk7CiAgICAgICAgICAvLyB3ZWVrZGF5cwogICAgICAgICAgY2FzZSAiRSI6CiAgICAgICAgICBjYXNlICJjIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQob25lKTsKICAgICAgICAgIGNhc2UgIkVFRSI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2Mud2Vla2RheXMoInNob3J0IiwgZmFsc2UpLCAxKTsKICAgICAgICAgIGNhc2UgIkVFRUUiOgogICAgICAgICAgICByZXR1cm4gb25lT2YobG9jLndlZWtkYXlzKCJsb25nIiwgZmFsc2UpLCAxKTsKICAgICAgICAgIGNhc2UgImNjYyI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2Mud2Vla2RheXMoInNob3J0IiwgdHJ1ZSksIDEpOwogICAgICAgICAgY2FzZSAiY2NjYyI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2Mud2Vla2RheXMoImxvbmciLCB0cnVlKSwgMSk7CiAgICAgICAgICAvLyBvZmZzZXQvem9uZQogICAgICAgICAgY2FzZSAiWiI6CiAgICAgICAgICBjYXNlICJaWiI6CiAgICAgICAgICAgIHJldHVybiBvZmZzZXQobmV3IFJlZ0V4cChgKFsrLV0ke29uZU9yVHdvLnNvdXJjZX0pKD86Oigke3R3by5zb3VyY2V9KSk/YCksIDIpOwogICAgICAgICAgY2FzZSAiWlpaIjoKICAgICAgICAgICAgcmV0dXJuIG9mZnNldChuZXcgUmVnRXhwKGAoWystXSR7b25lT3JUd28uc291cmNlfSkoJHt0d28uc291cmNlfSk/YCksIDIpOwogICAgICAgICAgLy8gd2UgZG9uJ3Qgc3VwcG9ydCBaWlpaIChQU1QpIG9yIFpaWlpaIChQYWNpZmljIFN0YW5kYXJkIFRpbWUpIGluIHBhcnNpbmcKICAgICAgICAgIC8vIGJlY2F1c2Ugd2UgZG9uJ3QgaGF2ZSBhbnkgd2F5IHRvIGZpZ3VyZSBvdXQgd2hhdCB0aGV5IGFyZQogICAgICAgICAgY2FzZSAieiI6CiAgIC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pbGRSZWdleCh1bml0cykgewogICAgY29uc3QgcmUgPSB1bml0cy5tYXAoKHUpID0+IHUucmVnZXgpLnJlZHVjZSgoZiwgcikgPT4gYCR7Zn0oJHtyLnNvdXJjZX0pYCwgIiIpOwogICAgcmV0dXJuIFtgXiR7cmV9JGAsIHVuaXRzXTsKICB9CgogIGZ1bmN0aW9uIG1hdGNoKGlucHV0LCByZWdleCwgaGFuZGxlcnMpIHsKICAgIGNvbnN0IG1hdGNoZXMgPSBpbnB1dC5tYXRjaChyZWdleCk7CgogICAgaWYgKG1hdGNoZXMpIHsKICAgICAgY29uc3QgYWxsID0ge307CiAgICAgIGxldCBtYXRjaEluZGV4ID0gMTsKICAgICAgZm9yIChjb25zdCBpIGluIGhhbmRsZXJzKSB7CiAgICAgICAgaWYgKGhhc093blByb3BlcnR5KGhhbmRsZXJzLCBpKSkgewogICAgICAgICAgY29uc3QgaCA9IGhhbmRsZXJzW2ldLAogICAgICAgICAgICBncm91cHMgPSBoLmdyb3VwcyA/IGguZ3JvdXBzICsgMSA6IDE7CiAgICAgICAgICBpZiAoIWgubGl0ZXJhbCAmJiBoLnRva2VuKSB7CiAgICAgICAgICAgIGFsbFtoLnRva2VuLnZhbFswXV0gPSBoLmRlc2VyKG1hdGNoZXMuc2xpY2UobWF0Y2hJbmRleCwgbWF0Y2hJbmRleCArIGdyb3VwcykpOwogICAgICAgICAgfQogICAgICAgICAgbWF0Y2hJbmRleCArPSBncm91cHM7CiAgICAgICAgfQogICAgICB9CiAgICAgIHJldHVybiBbbWF0Y2hlcywgYWxsXTsKICAgIH0gZWxzZSB7CiAgICAgIHJldHVybiBbbWF0Y2hlcywge31dOwogICAgfQogIH0KCiAgZnVuY3Rpb24gZGF0ZVRpbWVGcm9tTWF0Y2hlcyhtYXRjaGVzKSB7CiAgICBjb25zdCB0b0ZpZWxkID0gKHRva2VuKSA9PiB7CiAgICAgIHN3aXRjaCAodG9rZW4pIHsKICAgICAgICBjYXNlICJTIjoKICAgICAgICAgIHJldHVybiAibWlsbGlzZWNvbmQiOwogICAgICAgIGNhc2UgInMiOgogICAgICAgICAgcmV0dXJuICJzZWNvbmQiOwogICAgICAgIGNhc2UgIm0iOgogICAgICAgICAgcmV0dXJuICJtaW51dGUiOwogICAgICAgIGNhc2UgImgiOgogICAgICAgIGNhc2UgIkgiOgogICAgICAgICAgcmV0dXJuICJob3VyIjsKICAgICAgICBjYXNlICJkIjoKICAgICAgICAgIHJldHVybiAiZGF5IjsKICAgICAgICBjYXNlICJvIjoKICAgICAgICAgIHJldHVybiAib3JkaW5hbCI7CiAgICAgICAgY2FzZSAiTCI6CiAgICAgICAgY2FzZSAiTSI6CiAgICAgICAgICByZXR1cm4gIm1vbnRoIjsKICAgICAgICBjYXNlICJ5IjoKICAgICAgICAgIHJldHVybiAieWVhciI7CiAgICAgICAgY2FzZSAiRSI6CiAgICAgICAgY2FzZSAiYyI6CiAgICAgICAgICByZXR1cm4gIndlZWtkYXkiOwogICAgICAgIGNhc2UgIlciOgogICAgICAgICAgcmV0dXJuICJ3ZWVrTnVtYmVyIjsKICAgICAgICBjYXNlICJrIjoKICAgICAgICAgIHJldHVybiAid2Vla1llYXIiOwogICAgICAgIGNhc2UgInEiOgogICAgICAgICAgcmV0dXJuICJxdWFydGVyIjsKICAgICAgICBkZWZhdWx0OgogICAgICAgICAgcmV0dXJuIG51bGw7CiAgICAgIH0KICAgIH07CgogICAgbGV0IHpvbmUgPSBudWxsOwogICAgbGV0IHNwZWNpZmljT2Zmc2V0OwogICAgaWYgKCFpc1VuZGVmaW5lZChtYXRjaGVzLnopKSB7CiAgICAgIHpvbmUgPSBJQU5BWm9uZS5jcmVhdGUobWF0Y2hlcy56KTsKICAgIH0KCiAgICBpZiAoIWlzVW5kZWZpbmVkKG1hdGNoZXMuWikpIHsKICAgICAgaWYgKCF6b25lKSB7CiAgICAgICAgem9uZSA9IG5ldyBGaXhlZE9mZnNldFpvbmUobWF0Y2hlcy5aKTsKICAgICAgfQogICAgICBzcGVja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KICBmdW5jdGlvbiBvYmpUb1RTKG9iaiwgb2Zmc2V0LCB6b25lKSB7CiAgICByZXR1cm4gZml4T2Zmc2V0KG9ialRvTG9jYWxUUyhvYmopLCBvZmZzZXQsIHpvbmUpOwogIH0KCiAgLy8gY3JlYXRlIGEgbmV3IERUIGluc3RhbmNlIGJ5IGFkZGluZyBhIGR1cmF0aW9uLCBhZGp1c3RpbmcgZm9yIERTVHMKICBmdW5jdGlvbiBhZGp1c3RUaW1lKGluc3QsIGR1cikgewogICAgY29uc3Qgb1ByZSA9IGluc3QubywKICAgICAgeWVhciA9IGluc3QuYy55ZWFyICsgTWF0aC50cnVuYyhkdXIueWVhcnMpLAogICAgICBtb250aCA9IGluc3QuYy5tb250aCArIE1hdGgudHJ1bmMoZHVyLm1vbnRocykgKyBNYXRoLnRydW5jKGR1ci5xdWFydGVycykgKiAzLAogICAgICBjID0gewogICAgICAgIC4uLmluc3QuYywKICAgICAgICB5ZWFyLAogICAgICAgIG1vbnRoLAogICAgICAgIGRheToKICAgICAgICAgIE1hdGgubWluKGluc3QuYy5kYXksIGRheXNJbk1vbnRoKHllYXIsIG1vbnRoKSkgKwogICAgICAgICAgTWF0aC50cnVuYyhkdXIuZGF5cykgKwogICAgICAgICAgTWF0aC50cnVuYyhkdXIud2Vla3MpICogNywKICAgICAgfSwKICAgICAgbWlsbGlzVG9BZGQgPSBEdXJhdGlvbi5mcm9tT2JqZWN0KHsKICAgICAgICB5ZWFyczogZHVyLnllYXJzIC0gTWF0aC50cnVuYyhkdXIueWVhcnMpLAogICAgICAgIHF1YXJ0ZXJzOiBkdXIucXVhcnRlcnMgLSBNYXRoLnRydW5jKGR1ci5xdWFydGVycyksCiAgICAgICAgbW9udGhzOiBkdXIubW9udGhzIC0gTWF0aC50cnVuYyhkdXIubW9udGhzKSwKICAgICAgICB3ZWVrczogZHVyLndlZWtzIC0gTWF0aC50cnVuYyhkdXIud2Vla3MpLAogICAgICAgIGRheXM6IGR1ci5kYXlzIC0gTWF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IrIjsKICAgIGMgKz0gcGFkU3RhcnQoby5jLnllYXIsIGxvbmdGb3JtYXQgPyA2IDogNCk7CgogICAgaWYgKGV4dGVuZGVkKSB7CiAgICAgIGMgKz0gIi0iOwogICAgICBjICs9IHBhZFN0YXJ0KG8uYy5tb250aCk7CiAgICAgIGMgKz0gIi0iOwogICAgICBjICs9IHBhZFN0YXJ0KG8uYy5kYXkpOwogICAgfSBlbHNlIHsKICAgICAgYyArPSBwYWRTdGFydChvLmMubW9udGgpOwogICAgICBjICs9IHBhZFN0YXJ0KG8uYy5kYXkpOwogICAgfQogICAgcmV0dXJuIGM7CiAgfQoKICBmdW5jdGlvbiB0b0lTT1RpbWUoCiAgICBvLAogICAgZXh0ZW5kZWQsCiAgICBzdXBwcmVzc1NlY29uZHMsCiAgICBzdXBwcmVzc01pbGxpc2Vjb25kcywKICAgIGluY2x1ZGVPZmZzZXQsCiAgICBleHRlbmRlZFpvbmUKICApIHsKICAgIGxldCBjID0gcGFkU3RhcnQoby5jLmhvdXIpOwogICAgaWYgKGV4dGVuZGVkKSB7CiAgICAgIGMgKz0gIjoiOwogICAgICBjICs9IHBhZFN0YXJ0KG8uYy5taW51dGUpOwogICAgICBpZiAoby5jLm1pbGxpc2Vjb25kICE9PSAwIHx8IG8uYy5zZWNvbmQgIT09IDAgfHwgIXN1cHByZXNzU2Vjb25kcykgewogICAgICAgIGMgKz0gIjoiOwogICAgICB9CiAgICB9IGVsc2UgewogICAgICBjICs9IHBhZFN0YXJ0KG8uYy5taW51dGUpOwogICAgfQoKICAgIGlmIChvLmMubWlsbGlzZWNvbmQgIT09IDAgfHwgby5jLnNlY29uZCAhPT0gMCB8fCAhc3VwcHJlc3NTZWNvbmRzKSB7CiAgICAgIGMgKz0gcGFkU3RhcnQoby5jLnNlY29uZCk7CgogICAgICBpZiAoby5jLm1pbGxpc2Vjb25kICE9PSAwIHx8ICFzdXBwcmVzc01pbGxpc2Vjb25kcykgewogICAgICAgIGMgKz0gIi4iOwogICAgIC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IGhvdXI6IDAsCiAgICAgIG1pbnV0ZTogMCwKICAgICAgc2Vjb25kOiAwLAogICAgICBtaWxsaXNlY29uZDogMCwKICAgIH0sCiAgICBkZWZhdWx0T3JkaW5hbFVuaXRWYWx1ZXMgPSB7CiAgICAgIG9yZGluYWw6IDEsCiAgICAgIGhvdXI6IDAsCiAgICAgIG1pbnV0ZTogMCwKICAgICAgc2Vjb25kOiAwLAogICAgICBtaWxsaXNlY29uZDogMCwKICAgIH07CgogIC8vIFVuaXRzIGluIHRoZSBzdXBwb3J0ZWQgY2FsZW5kYXJzLCBzb3J0ZWQgYnkgYmlnbmVzcwogIGNvbnN0IG9yZGVyZWRVbml0cyA9IFsieWVhciIsICJtb250aCIsICJkYXkiLCAiaG91ciIsICJtaW51dGUiLCAic2Vjb25kIiwgIm1pbGxpc2Vjb25kIl0sCiAgICBvcmRlcmVkV2Vla1VuaXRzID0gWwogICAgICAid2Vla1llYXIiLAogICAgICAid2Vla051bWJlciIsCiAgICAgICJ3ZWVrZGF5IiwKICAgICAgImhvdXIiLAogICAgICAibWludXRlIiwKICAgICAgInNlY29uZCIsCiAgICAgICJtaWxsaXNlY29uZCIsCiAgICBdLAogICAgb3JkZXJlZE9yZGluYWxVbml0cyA9IFsieWVhciIsICJvcmRpbmFsIiwgImhvdXIiLCAibWludXRlIiwgInNlY29uZCIsICJtaWxsaXNlY29uZCJdOwoKICAvLyBzdGFuZGFyZGl6ZSBjYXNlIGFuZCBwbHVyYWxpdHkgaW4gdW5pdHMKICBmdW5jdGlvbiBub3JtYWxpemVVbml0KHVuaXQpIHsKICAgIGNvbnN0IG5vcm1hbGl6ZWQgPSB7CiAgICAgIHllYXI6ICJ5ZWFyIiwKICAgICAgeWVhcnM6ICJ5ZWFyIiwKICAgICAgbW9udGg6ICJtb250aCIsCiAgICAgIG1vbnRoczogIm1vbnRoIiwKICAgICAgZGF5OiAiZGF5IiwKICAgICAgZGF5czogImRheSIsCiAgICAgIGhvdXI6ICJob3VyIiwKICA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pZCgiaW52YWxpZCBpbnB1dCIpIDogbnVsbCkgfHwKICAgICAgICAoIXpvbmUuaXNWYWxpZCA/IHVuc3VwcG9ydGVkWm9uZSh6b25lKSA6IG51bGwpOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLnRzID0gaXNVbmRlZmluZWQoY29uZmlnLnRzKSA/IFNldHRpbmdzLm5vdygpIDogY29uZmlnLnRzOwoKICAgICAgbGV0IGMgPSBudWxsLAogICAgICAgIG8gPSBudWxsOwogICAgICBpZiAoIWludmFsaWQpIHsKICAgICAgICBjb25zdCB1bmNoYW5nZWQgPSBjb25maWcub2xkICYmIGNvbmZpZy5vbGQudHMgPT09IHRoaXMudHMgJiYgY29uZmlnLm9sZC56b25lLmVxdWFscyh6b25lKTsKCiAgICAgICAgaWYgKHVuY2hhbmdlZCkgewogICAgICAgICAgW2MsIG9dID0gW2NvbmZpZy5vbGQuYywgY29uZmlnLm9sZC5vXTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgY29uc3Qgb3QgPSB6b25lLm9mZnNldCh0aGlzLnRzKTsKICAgICAgICAgIGMgPSB0c1RvT2JqKHRoaXMudHMsIG90KTsKICAgICAgICAgIGludmFsaWQgPSBOdW1iZXIuaXNOYU4oYy55ZWFyKSA/IG5ldyBJbnZhbGlkKCJpbnZhbGlkIGlucHV0IikgOiBudWxsOwogICAgICAgICAgYyA9IGludmFsaWQgPyBudWxsIDogYzsKICAgICAgICAgIG8gPSBpbnZhbGlkID8gbnVsbCA6IG90OwogICAgICAgIH0KICAgICAgfQoKICAgICAgLyoqCiAgICAgICAqIEBhY2Nlc3MgcHJpdmF0ZQogICAgICAgKi8KICAgICAgdGhpcy5fem9uZSA9IHpvbmU7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMubG9jID0gY29uZmlnLmxvYyB8fCBMb2NhbGUuY3JlYXRlKCk7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMuaW52YWxpZCA9IGludmFsaWQ7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMud2Vla0RhdGEgPSBudWxsOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLmMgPSBjOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLm8gPSBvOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLmlzTHV4b25EYXRlVGltZSA9IHRydWU7CiAgICB9CgogICAgLy8gQ09OU1RSVUNUCgogICAgLyoqCiAgICAgKiBDcmVhdGUgYSBEYXRlVGltZSBmb3IgdGhlIGN1cnJlbnQgaW5zdGFudCwgaW4gdGhlIHN5c3RlbSdzIHRpbWUgem9uZS4KICAgICAqCiAgICAgKiBVc2UgU2V0dGluZ3MgdG8gb3ZlcnJpZGUgdGhlc2UgZGVmYXVsdCB2YWx1ZXMgaWYgbmVlZGVkLgogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUubm93KCkudG9JU08oKSAvL34+IG5vdyBpbiB0aGUgSVNPIGZvcm1hdAogICAgICogQHJldHVybiB7RGF0ZVRpbWV9CiAgICAgKi8KICAgIHN0YXRpYyBub3coKSB7CiAgICAgIHJldHVybiBuZXcgRGF0ZVRpbWUoe30pOwogICAgfQoKICAgIC8qKgogICAgICogQ3JlYXRlIGEgbG9jYWwgRGF0ZVRpbWUKICAgICAqIEBwYXJhbSB7bnVtYmVyfSBbeWVhcl0gLSBUaGUgY2FsZW5kYXIgeWVhci4gSWYgb21pdHRlZCAoYXMgaW4sIGNhbGwgYGxvY2FsKClgIHdpdGggbm8gYXJndW1lbnRzKSwgdGhlIGN1cnJlbnQgdGltZSB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hbGUgdG8gc2V0IG9uIHRoZSByZXN1bHRpbmcgRGF0ZVRpbWUgaW5zdGFuY2UKICAgICAqIEBwYXJhbSB7c3RyaW5nfSBbb3B0aW9ucy5vdXRwdXRDYWxlbmRhcl0gLSB0aGUgb3V0cHV0IGNhbGVuZGFyIHRvIHNldCBvbiB0aGUgcmVzdWx0aW5nIERhdGVUaW1lIGluc3RhbmNlCiAgICAgKiBAcGFyYW0ge3N0cmluZ30gW29wdGlvbnMubnVtYmVyaW5nU3lzdGVtXSAtIHRoZSBudW1iZXJpbmcgc3lzdGVtIHRvIHNldCBvbiB0aGUgcmVzdWx0aW5nIERhdGVUaW1lIGluc3RhbmNlCiAgICAgKiBAZXhhbXBsZSBEYXRlVGltZS51dGMoKSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvL34+IG5vdwogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUudXRjKDIwMTcpICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy9+PiAyMDE3LTAxLTAxVDAwOjAwOjAwWgogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUudXRjKDIwMTcsIDMpICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy9+PiAyMDE3LTAzLTAxVDAwOjAwOjAwWgogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUudXRjKDIwMTcsIDMsIDEyKSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy9+PiAyMDE3LTAzLTEyVDAwOjAwOjAwWgogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUudXRjKDIwMTcsIDMsIDEyLCA1KSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy9+PiAyMDE3LTAzLTEyVDA1OjAwOjAwWgogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUudXRjKDIwMTcsIDMsIDEyLCA1LCA0NSkgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy9+PiAyMDE3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c1RvT2JqKHRzMiwgbzIpOwogICAgICBpZiAoCiAgICAgICAgYzEuaG91ciA9PT0gYzIuaG91ciAmJgogICAgICAgIGMxLm1pbnV0ZSA9PT0gYzIubWludXRlICYmCiAgICAgICAgYzEuc2Vjb25kID09PSBjMi5zZWNvbmQgJiYKICAgICAgICBjMS5taWxsaXNlY29uZCA9PT0gYzIubWlsbGlzZWNvbmQKICAgICAgKSB7CiAgICAgICAgcmV0dXJuIFtjbG9uZSh0aGlzLCB7IHRzOiB0czEgfSksIGNsb25lKHRoaXMsIHsgdHM6IHRzMiB9KV07CiAgICAgIH0KICAgICAgcmV0dXJuIFt0aGlzXTsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgdHJ1ZSBpZiB0aGlzIERhdGVUaW1lIGlzIGluIGEgbGVhcCB5ZWFyLCBmYWxzZSBvdGhlcndpc2UKICAgICAqIEBleGFtcGxlIERhdGVUaW1lLmxvY2FsKDIwMTYpLmlzSW5MZWFwWWVhciAvLz0+IHRydWUKICAgICAqIEBleGFtcGxlIERhdGVUaW1lLmxvY2FsKDIwMTMpLmlzSW5MZWFwWWVhciAvLz0+IGZhbHNlCiAgICAgKiBAdHlwZSB7Ym9vbGVhbn0KICAgICAqLwogICAgZ2V0IGlzSW5MZWFwWWVhcigpIHsKICAgICAgcmV0dXJuIGlzTGVhcFllYXIodGhpcy55ZWFyKTsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgdGhlIG51bWJlciBvZiBkYXlzIGluIHRoaXMgRGF0ZVRpbWUncyBtb250aAogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUubG9jYWwoMjAxNiwgMikuZGF5c0luTW9udGggLy89PiAyOQogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUubG9jYWwoMjAxNiwgMykuZGF5c0luTW9udGggLy89PiAzMQogICAgICogQHR5cGUge251bWJlcn0KICAgICAqLwogICAgZ2V0IGRheXNJbk1vbnRoKCkgewogICAgICByZXR1cm4gZGF5c0luTW9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VrWWVhcikgfHwKICAgICAgICAgICFpc1VuZGVmaW5lZChub3JtYWxpemVkLndlZWtOdW1iZXIpIHx8CiAgICAgICAgICAhaXNVbmRlZmluZWQobm9ybWFsaXplZC53ZWVrZGF5KSwKICAgICAgICBjb250YWluc09yZGluYWwgPSAhaXNVbmRlZmluZWQobm9ybWFsaXplZC5vcmRpbmFsKSwKICAgICAgICBjb250YWluc0dyZWdvclllYXIgPSAhaXNVbmRlZmluZWQobm9ybWFsaXplZC55ZWFyKSwKICAgICAgICBjb250YWluc0dyZWdvck1EID0gIWlzVW5kZWZpbmVkKG5vcm1hbGl6ZWQubW9udGgpIHx8ICFpc1VuZGVmaW5lZChub3JtYWxpemVkLmRheSksCiAgICAgICAgY29udGFpbnNHcmVnb3IgPSBjb250YWluc0dyZWdvclllYXIgfHwgY29udGFpbnNHcmVnb3JNRCwKICAgICAgICBkZWZpbml0ZVdlZWtEZWYgPSBub3JtYWxpemVkLndlZWtZZWFyIHx8IG5vcm1hbGl6ZWQud2Vla051bWJlcjsKCiAgICAgIGlmICgoY29udGFpbnNHcmVnb3IgfHwgY29udGFpbnNPcmRpbmFsKSAmJiBkZWZpbml0ZVdlZWtEZWYpIHsKICAgICAgICB0aHJvdyBuZXcgQ29uZmxpY3RpbmdTcGVjaWZpY2F0aW9uRXJyb3IoCiAgICAgICAgICAiQ2FuJ3QgbWl4IHdlZWtZZWFyL3dlZWtOdW1iZXIgdW5pdHMgd2l0aCB5ZWFyL21vbnRoL2RheSBvciBvcmRpbmFscyIKICAgICAgICApOwogICAgICB9CgogICAgICBpZiAoY29udGFpbnNHcmVnb3JNRCAmJiBjb250YWluc09yZGluYWwpIHsKICAgICAgICB0aHJvdyBuZXcgQ29uZmxpY3RpbmdTcGVjaWZpY2F0aW9uRXJyb3IoIkNhbid0IG1peCBvcmRpbmFsIGRhdGVzIHdpdGggbW9udGgvZGF5Iik7CiAgICAgIH0KCiAgICAgIGxldCBtaXhlZDsKICAgICAgaWYgKHNldHRpbmd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M7CiAgICAgIGNvbnN0IG8gPSB7fSwKICAgICAgICBub3JtYWxpemVkVW5pdCA9IER1cmF0aW9uLm5vcm1hbGl6ZVVuaXQodW5pdCk7CiAgICAgIHN3aXRjaCAobm9ybWFsaXplZFVuaXQpIHsKICAgICAgICBjYXNlICJ5ZWFycyI6CiAgICAgICAgICBvLm1vbnRoID0gMTsKICAgICAgICAvLyBmYWxscyB0aHJvdWdoCiAgICAgICAgY2FzZSAicXVhcnRlcnMiOgogICAgICAgIGNhc2UgIm1vbnRocyI6CiAgICAgICAgICBvLmRheSA9IDE7CiAgICAgICAgLy8gZmFsbHMgdGhyb3VnaAogICAgICAgIGNhc2UgIndlZWtzIjoKICAgICAgICBjYXNlICJkYXlzIjoKICAgICAgICAgIG8uaG91ciA9IDA7CiAgICAgICAgLy8gZmFsbHMgdGhyb3VnaAogICAgICAgIGNhc2UgImhvdXJzIjoKICAgICAgICAgIG8ubWludXRlID0gMDsKICAgICAgICAvLyBmYWxscyB0aHJvdWdoCiAgICAgICAgY2FzZSAibWludXRlcyI6CiAgICAgICAgICBvLnNlY29uZCA9IDA7CiAgICAgICAgLy8gZmFsbHMgdGhyb3VnaAogICAgICAgIGNhc2UgInNlY29uZHMiOgogICAgICAgICAgby5taWxsaXNlY29uZCA9IDA7CiAgICAgICAgICBicmVhazsKICAgICAgICAvLyBubyBkZWZhdWx0LCBpbnZhbGlkIHVuaXRzIHRocm93IGluIG5vcm1hbGl6ZVVuaXQoKQogICAgICB9CgogICAgICBpZiAobm9ybWFsaXplZFVuaXQgPT09ICJ3ZWVrcyIpIHsKICAgICAgICBvLndlZWtkYXkgPSAxOwogICAgICB9CgogICAgICBpZiAobm9ybWFsaXplZFVuaXQgPT09ICJxdWFydGVycyIpIHsKICAgICAgICBjb25zdCBxID0gTWF0aC5jZWlsKHRoaXMubW9udGggLyAzKTsKICAgICAgICBvLm1vbnRoID0gKHEgLSAxKSAqIDMgKyAxOwogICAgICB9CgogICAgICByZXR1cm4gdGhpcy5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AgICByZXR1cm4gKAogICAgICAgIGMgKwogICAgICAgIHRvSVNPVGltZSgKICAgICAgICAgIHRoaXMsCiAgICAgICAgICBmb3JtYXQgPT09ICJleHRlbmRlZCIsCiAgICAgICAgICBzdXBwcmVzc1NlY29uZHMsCiAgICAgICAgICBzdXBwcmVzc01pbGxpc2Vjb25kcywKICAgICAgICAgIGluY2x1ZGVPZmZzZXQsCiAgICAgICAgICBleHRlbmRlZFpvbmUKICAgICAgICApCiAgICAgICk7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm5zIGFuIFJGQyAyODIyLWNvbXBhdGlibGUgc3RyaW5nIHJlcHJlc2VudGF0aW9uIG9mIHRoaXMgRGF0ZVRpbWUKICAgICAqIEBleGFtcGxlIERhdGVUaW1lLnV0YygyMDE0LCA3LCAxMykudG9SRkMyODIyKCkgLy89PiAnU3VuLCAxMyBKdWwgMjAxNCAwMDowMDowMCArMDAwMCcKICAgICAqIEBleGFtcGxlIERhdGVUaW1lLmxvY2FsKDIwMTQsIDcsIDEzKS50b1JGQzI4MjIoKSAvLz0+ICdTdW4sIDEzIEp1bCAyMDE0IDAwOjAwOjAwIC0wNDAwJwogICAgICogQHJldHVybiB7c3RyaW5nfQogICAgICovCiAgICB0b1JGQzI4MjIoKSB7CiAgICAgIHJldHVybiB0b1RlY2hGb3JtYXQodGhpcywgIkVFRSwgZGQgTExMIHl5eXkgSEg6bW06c3MgWlpaIiwgZmFsc2UpOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJucyBhIHN0cmluZyByZXByZXNlbnRhdGlvbiBvZiB0aGlzIERhdGVUaW1lIGFwcHJvcHJpYXRlIGZvciB1c2UgaW4gSFRUUCBoZWFkZXJzLiBUaGUgb3V0cHV0IGlzIGFsd2F5cyBleHByZXNzZWQgaW4gR01ULgogICAgICogU3BlY2lmaWNhbGx5LCB0aGUgc3RyaW5nIGNvbmZvcm1zIHRvIFJGQyAxMTIzLgogICAgICogQHNlZSBodHRwczovL3d3dy53My5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89PiB7IG1pbGxpc2Vjb25kczogNDM4MDc1MDAwMDAgfQogICAgICogaTIuZGlmZihpMSwgJ2hvdXJzJykudG9PYmplY3QoKSAvLz0+IHsgaG91cnM6IDEyMTY4Ljc1IH0KICAgICAqIGkyLmRpZmYoaTEsIFsnbW9udGhzJywgJ2RheXMnXSkudG9PYmplY3QoKSAvLz0+IHsgbW9udGhzOiAxNiwgZGF5czogMTkuMDMxMjUgfQogICAgICogaTIuZGlmZihpMSwgWydtb250aHMnLCAnZGF5cycsICdob3VycyddKS50b09iamVjdCgpIC8vPT4geyBtb250aHM6IDE2LCBkYXlzOiAxOSwgaG91cnM6IDAuNzUgfQogICAgICogQHJldHVybiB7RHVyYXRpb259CiAgICAgKi8KICAgIGRpZmYob3RoZXJEYXRlVGltZSwgdW5pdCA9ICJtaWxsaXNlY29uZHMiLCBvcHRzID0ge30pIHsKICAgICAgaWYgKCF0aGlzLmlzVmFsaWQgfHwgIW90aGVyRGF0ZVRpbWUuaXNWYWxpZCkgewogICAgICAgIHJldHVybiBEdXJhdGlvbi5pbnZhbGlkKCJjcmVhdGVkIGJ5IGRpZmZpbmcgYW4gaW52YWxpZCBEYXRlVGltZSIpOwogICAgICB9CgogICAgICBjb25zdCBkdXJPcHRzID0geyBsb2NhbGU6IHRoaXMubG9jYWxlLCBudW1iZXJpbmdTeXN0ZW06IHRoaXMubnVtYmVyaW5nU3lzdGVtLCAuLi5vcHRzIH07CgogICAgICBjb25zdCB1bml0cyA9IG1heWJlQXJyYXkodW5pdCkubWFwKER1cmF0aW9uLm5vcm1hbGl6ZVVuaXQpLAogICAgICAgIG90aGVySXNMYXRlciA9IG90aGVyRGF0ZVRpbWUudmFsdWVPZigpID4gdGhpcy52YWx1ZU9mKCksCiAgICAgICAgZWFybGllciA9IG90aGVySXNMYXRlciA/IHRoaXMgOiBvdGhlckRhdGVUaW1lLAogICAgICAgIGxhdGVyID0gb3RoZXJJc0xhdGVyID8gb3RoZXJEYXRlVGltZSA6IHRoaXM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xpa2UgMTAvMTQvMTk4MwogICAgICogQHR5cGUge09iamVjdH0KICAgICAqLwogICAgc3RhdGljIGdldCBEQVRFX1NIT1JUKCkgewogICAgICByZXR1cm4gREFURV9TSE9SVDsKICAgIH0KCiAgICAvKioKICAgICAqIHtAbGluayBEYXRlVGltZSN0b0xvY2FsZVN0cmluZ30gZm9ybWF0IGxpa2UgJ09jdCAxNCwgMTk4MycKICAgICAqIEB0eXBlIHtPYmplY3R9CiAgICAgKi8KICAgIHN0YXRpYyBnZXQgREFURV9NRUQoKSB7CiAgICAgIHJldHVybiBEQVRFX01FRDsKICAgIH0KCiAgICAvKioKICAgICAqIHtAbGluayBEYXRlVGltZSN0b0xvY2FsZVN0cmluZ30gZm9ybWF0IGxpa2UgJ0ZyaSwgT2N0IDE0LCAxOTgzJwogICAgICogQHR5cGUge09iamVjdH0KICAgICAqLwogICAgc3RhdGljIGdldCBEQVRFX01FRF9XSVRIX1dFRUtEQVkoKSB7CiAgICAgIHJldHVybiBEQVRFX01FRF9XSVRIX1dFRUtEQVk7CiAgICB9CgogICAgLyoqCiAgICAgKiB7QGxpbmsgRGF0ZVRpbWUjdG9Mb2NhbGVTdHJpbmd9IGZvcm1hdCBsaWtlICdPY3RvYmVyIDE0LCAxOTgzJwogICAgICogQHR5cGUge09iamVjdH0KICAgICAqLwogICAgc3RhdGljIGdldCBEQVRFX0ZVTEwoKSB7CiAgICAgIHJldHVybiBEQVRFX0ZVTEw7CiAgICB9CgogICAgLyoqCiAgICAgKiB7QGxpbmsgRGF0ZVRpbWUjdG9Mb2NhbGVTdHJpbmd9IGZvcm1hdCBsaWtlICdUdWVzZGF5LCBPY3RvYmVyIDE0LCAxOTgzJwogICAgICogQHR5cGUge09iamVjdH0KICAgICAqLwogICAgc3RhdGljIGdldCBEQVRFX0hVR0UoKSB7CiAgICAgIHJldHVybiBEQVRFX0hVR0U7CiAgICB9CgogICAgLyoqCiAgICAgKiB7QGxpbmsgRGF0ZVRpbWUjdG9Mb2NhbG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ICB9CiAgICAgIG1hcEVycihmKSB7CiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfQogICAgICBiaW1hcChzdWNjLCBfZmFpbCkgewogICAgICAgICAgcmV0dXJuIHRoaXMubWFwKHN1Y2MpOwogICAgICB9CiAgICAgIG9yRWxzZShfdmFsdWUpIHsKICAgICAgICAgIHJldHVybiB0aGlzLnZhbHVlOwogICAgICB9CiAgICAgIGNhc3QoKSB7CiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfQogICAgICBvckVsc2VUaHJvdyhfbWVzc2FnZSkgewogICAgICAgICAgcmV0dXJuIHRoaXMudmFsdWU7CiAgICAgIH0KICB9CiAgLyoqIEZ1bmN0aW9uYWwgcmV0dXJuIHR5cGUgZm9yIGVycm9yIGhhbmRsaW5nLiAqLwogIGNsYXNzIEZhaWx1cmUgewogICAgICBjb25zdHJ1Y3RvcihlcnJvcikgewogICAgICAgICAgdGhpcy5lcnJvciA9IGVycm9yOwogICAgICAgICAgdGhpcy5zdWNjZXNzZnVsID0gZmFsc2U7CiAgICAgIH0KICAgICAgbWFwKF9mKSB7CiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfQogICAgICBmbGF0TWFwKF9mKSB7CiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfQogICAgICBtYXBFcnIoZikgewogICAgICAgICAgcmV0dXJuIG5ldyBGYWlsdXJlKGYodGhpcy5lcnJvcikpOwogICAgICB9CiAgICAgIGJpbWFwKF9zdWNjLCBmYWlsKSB7CiAgICAgICAgICByZXR1cm4gdGhpcy5tYXBFcnIoZmFpbCk7CiAgICAgIH0KICAgICAgb3JFbHNlKHZhbHVlKSB7CiAgICAgICAgICByZXR1cm4gdmFsdWU7CiAgICAgIH0KICAgICAgY2FzdCgpIHsKICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICB9CiAgICAgIG9yRWxzZVRocm93KG1lc3NhZ2UpIHsKICAgICAgICAgIGlmIChtZXNzYWdlKQogICAgICAgICAgICA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lx1RERGQ1x1RERGRVx1RERGRl18XHVEREU4XHVEODNDW1x1RERFNlx1RERFOFx1RERFOVx1RERFQi1cdURERUVcdURERjAtXHVEREY1XHVEREY3XHVEREZBLVx1RERGRl18XHVEREU5XHVEODNDW1x1RERFQVx1RERFQ1x1RERFRlx1RERGMFx1RERGMlx1RERGNFx1RERGRl18XHVEREVBXHVEODNDW1x1RERFNlx1RERFOFx1RERFQVx1RERFQ1x1RERFRFx1RERGNy1cdURERkFdfFx1RERFQlx1RDgzQ1tcdURERUUtXHVEREYwXHVEREYyXHVEREY0XHVEREY3XXxcdURERUNcdUQ4M0NbXHVEREU2XHVEREU3XHVEREU5LVx1RERFRVx1RERGMS1cdURERjNcdURERjUtXHVEREZBXHVEREZDXHVEREZFXXxcdURERURcdUQ4M0NbXHVEREYwXHVEREYyXHVEREYzXHVEREY3XHVEREY5XHVEREZBXXxcdURERUVcdUQ4M0NbXHVEREU4LVx1RERFQVx1RERGMS1cdURERjRcdURERjYtXHVEREY5XXxcdURERUZcdUQ4M0NbXHVEREVBXHVEREYyXHVEREY0XHVEREY1XXxcdURERjBcdUQ4M0NbXHVEREVBXHVEREVDLVx1RERFRVx1RERGMlx1RERGM1x1RERGNVx1RERGN1x1RERGQ1x1RERGRVx1RERGRl18XHVEREYxXHVEODNDW1x1RERFNi1cdURERThcdURERUVcdURERjBcdURERjctXHVEREZCXHVEREZFXXxcdURERjJcdUQ4M0NbXHVEREU2XHVEREU4LVx1RERFRFx1RERGMC1cdURERkZdfFx1RERGM1x1RDgzQ1tcdURERTZcdURERThcdURERUEtXHVEREVDXHVEREVFXHVEREYxXHVEREY0XHVEREY1XHVEREY3XHVEREZBXHVEREZGXXxcdURERjRcdUQ4M0NcdURERjJ8XHVEREY1XHVEODNDW1x1RERFNlx1RERFQS1cdURERURcdURERjAtXHVEREYzXHVEREY3LVx1RERGOVx1RERGQ1x1RERGRV18XHVEREY2XHVEODNDXHVEREU2fFx1RERGN1x1RDgzQ1tcdURERUFcdURERjRcdURERjhcdURERkFcdURERkNdfFx1RERGOFx1RDgzQ1tcdURERTYtXHVEREVBXHVEREVDLVx1RERGNFx1RERGNy1cdURERjlcdURERkJcdURERkQtXHVEREZGXXxcdURERjlcdUQ4M0NbXHVEREU2XHVEREU4XHVEREU5XHVEREVCLVx1RERFRFx1RERFRi1cdURERjRcdURERjdcdURERjlcdURERkJcdURERkNcdURERkZdfFx1RERGQVx1RDgzQ1tcdURERTZcdURERUNcdURERjJcdURERjNcdURERjhcdURERkVcdURERkZdfFx1RERGQlx1RDgzQ1tcdURERTZcdURERThcdURERUFcdURERUNcdURERUVcdURERjNcdURERkFdfFx1RERGQ1x1RDgzQ1tcdURERUJcdURERjhdfFx1RERGRFx1RDgzQ1x1RERGMHxcdURERkVcdUQ4M0NbXHVEREVBXHVEREY5XXxcdURERkZcdUQ4M0NbXHVEREU2XHVEREYyXHVEREZDXXxcdURGRjNcdUZFMEY/KD86XHUyMDBEKD86XHUyNkE3XHVGRTBGP3xcdUQ4M0NcdURGMDgpKT98XHVERkY0KD86XHUyMDBEXHUyNjIwXHVGRTBGP3xcdURCNDBcdURDNjdcdURCNDBcdURDNjJcdURCNDAoPzpcdURDNjVcdURCNDBcdURDNkVcdURCNDBcdURDNjd8XHVEQzczXHVEQjQwXHVEQzYzXHVEQjQwXHVEQzc0fFx1REM3N1x1REI0MFx1REM2Q1x1REI0MFx1REM3MylcdURCNDBcdURDN0YpPyl8XHVEODNEKD86W1x1REMwOFx1REMyNl0oPzpcdTIwMERcdTJCMUIpP3xbXHVEQzNGXHVEQ0ZEXHVERDQ5XHVERDRBXHVERDZGXHVERDcwXHVERDczXHVERDc2LVx1REQ3OVx1REQ4N1x1REQ4QS1cdUREOERcdUREQTVcdUREQThcdUREQjFcdUREQjJcdUREQkNcdUREQzItXHVEREM0XHVEREQxLVx1REREM1x1REREQy1cdUREREVcdURERTFcdURERTNcdURERThcdURERUZcdURERjNcdURERkFcdURFQ0JcdURFQ0QtXHVERUNGXHVERUUwL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UREMkNcdURFODBcdURFOTJdfFx1RDgzRSg/OltcdUREQUYtXHVEREIzXHVEREJDXHVEREJEXXxcdUREMURcdTIwMERcdUQ4M0RbXHVEQzY4XHVEQzY5XVx1RDgzQ1tcdURGRkItXHVERkZFXSkpKT8pKT98XHVEQzZGKD86XHUyMDBEW1x1MjY0MFx1MjY0Ml1cdUZFMEY/KT98XHVERDc1KD86XHVGRTBGfFx1RDgzQ1tcdURGRkItXHVERkZGXSk/KD86XHUyMDBEW1x1MjY0MFx1MjY0Ml1cdUZFMEY/KT98XHVERTJFKD86XHUyMDBEXHVEODNEXHVEQ0E4KT98XHVERTM1KD86XHUyMDBEXHVEODNEXHVEQ0FCKT98XHVERTM2KD86XHUyMDBEXHVEODNDXHVERjJCXHVGRTBGPyk/KXxcdUQ4M0UoPzpbXHVERDBDXHVERDBGXHVERDE4LVx1REQxRlx1REQzMC1cdUREMzRcdUREMzZcdURENzdcdUREQjVcdUREQjZcdUREQkJcdURERDJcdURERDNcdURERDVcdURFQzMtXHVERUM1XHVERUYwXHVERUYyLVx1REVGOF0oPzpcdUQ4M0NbXHVERkZCLVx1REZGRl0pP3xbXHVERDI2XHVERDM1XHVERDM3LVx1REQzOVx1REQzRFx1REQzRVx1RERCOFx1RERCOVx1RERDRC1cdUREQ0ZcdURERDRcdURERDYtXHVEREREXSg/Olx1RDgzQ1tcdURGRkItXHVERkZGXSk/KD86XHUyMDBEW1x1MjY0MFx1MjY0Ml1cdUZFMEY/KT98W1x1RERERVx1RERERl0oPzpcdTIwMERbXHUyNjQwXHUyNjQyXVx1RkUwRj8pP3xbXHVERDBEXHVERDBFXHVERDEwLVx1REQxN1x1REQyMC1cdUREMjVcdUREMjctXHVERDJGXHVERDNBXHVERDNGLVx1REQ0NVx1REQ0Ny1cdURENzZcdURENzgtXHVEREI0XHVEREI3XHVEREJBXHVEREJDLVx1RERDQ1x1REREMFx1RERFMC1cdURERkZcdURFNzAtXHVERTdDXHVERTgwLVx1REU4OFx1REU5MC1c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GR1ciA9IER1cmF0aW9uLmZyb21PYmplY3QoT2JqZWN0LmZyb21FbnRyaWVzKE9iamVjdC5lbnRyaWVzKGR1ci50b09iamVjdCgpKS5maWx0ZXIoKFssIHF1YW50aXR5XSkgPT4gcXVhbnRpdHkgIT0gMCkpKTsKICAgICAgcmV0dXJuIGR1ci50b0h1bWFuKCk7CiAgfQoKICB2YXIgVmFsdWVzOwogIChmdW5jdGlvbiAoVmFsdWVzKSB7CiAgICAgIC8qKiBDb252ZXJ0IGFuIGFyYml0cmFyeSB2YWx1ZSBpbnRvIGEgcmVhc29uYWJsZSwgTWFya2Rvd24tZnJpZW5kbHkgc3RyaW5nIGlmIHBvc3NpYmxlLiAqLwogICAgICBmdW5jdGlvbiB0b1N0cmluZyhmaWVsZCwgc2V0dGluZyA9IERFRkFVTFRfUVVFUllfU0VUVElOR1MsIHJlY3Vyc2l2ZSA9IGZhbHNlKSB7CiAgICAgICAgICBsZXQgd3JhcHBlZCA9IHdyYXBWYWx1ZShmaWVsZCk7CiAgICAgICAgICBpZiAoIXdyYXBwZWQpCiAgICAgICAgICAgICAgcmV0dXJuIHNldHRpbmcucmVuZGVyTnVsbEFzOwogICAgICAgICAgc3dpdGNoICh3cmFwcGVkLnR5cGUpIHsKICAgICAgICAgICAgICBjYXNlICJudWxsIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIHNldHRpbmcucmVuZGVyTnVsbEFzOwogICAgICAgICAgICAgIGNhc2UgInN0cmluZyI6CiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwcGVkLnZhbHVlOwogICAgICAgICAgICAgIGNhc2UgIm51bWJlciI6CiAgICAgICAgICAgICAgY2FzZSAiYm9vbGVhbiI6CiAgICAgICAgICAgICAgICAgIHJldHVybiAiIiArIHdyYXBwZWQudmFsdWU7CiAgICAgICAgICAgICAgY2FzZSAiaHRtbCI6CiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwcGVkLnZhbHVlLm91dGVySFRNTDsKICAgICAgICAgICAgICBjYXNlICJ3aWRnZXQiOgogICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcHBlZC52YWx1ZS5tYXJrZG93bigpOwogICAgICAgICAgICAgIGNhc2UgImxpbmsiOgogICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcHBlZC52YWx1ZS5tYXJrZG93bigpOwogICAgICAgICAgICAgIGNhc2UgImZ1bmN0aW9uIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuICI8ZnVuY3Rpb24+IjsKICAgICAgICAgICAgICBjYXNlICJhcnJheSI6CiAgICAgICAgICAgICAgICAgIGxldCByZXN1bHQgPSAiIjsKICAgICAgICAgICAgICAgICAgaWYgKHJlY3Vyc2l2ZSkKICAgICAgICAgICAgICAgICAgICAgIHJlc3VsdCArPSAiWyI7CiAgICAgICAgICAgICAgICAgIHJlc3VsdCArPSB3cmFwcGVkLnZhbHVlLm1hcChmID0+IHRvU3RyaW5nKGYsIHNldHRpbmcsIHRydWUpKS5qb2luKCIsICIpOwogICAgICAgICAgICAgICAgICBpZiAocmVjdXJzaXZlKQogICAgICAgICAgICAgICAgICAgICAgcmVzdWx0ICs9ICJdIjsKICAgICAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICAgICAgICBjYXNlICJvYmplY3QiOgogICAgICAgICAgICAgICAgICByZXR1cm4gKCJ7ICIgKwogICAgICAgICAgICAgICAgICAgICAgT2JqZWN0LmVudHJpZXMod3JhcHBlZC52YWx1ZSkKICAgICAgICAgICAgICAgICAgICAgICAgICAubWFwKGUgPT4gZVswXSArICI6ICIgKyB0b1N0cmluZyhlWzFdLCBzZXR0aW5nLCB0cnVlKSkKICAgICAgICAgICAgICAgICAgICAgICAgICAuam9pbigiLCAiKSArCiAgICAgICAgICAgICAgICAgICAgICAiIH0iKTsKICAgICAgICAgICAgICBjYXNlICJkYXRlIjoKICAgICAgICAgICAgICAgICAgaWYgKHdyYXBwZWQudmFsdWUuc2Vjb25kID09IDAgJiYgd3JhcHBlZC52YWx1ZS5ob3VyID09IDAgJiYgd3JhcHBlZC52YWx1ZS5taW51dGUgPT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUudG9Gb3JtYXQoc2V0dGluZy5kZWZhdWx0RGF0ZUZvcm1hdCk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUudG9Gb3JtYXQoc2V0dGluZy5kZWZhdWx0RGF0ZVRpbWVGb3JtYXQpOwogICAgICAgICAgICAgIGNhc2UgImR1cmF0aW9uIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIHJlbmRlck1pbmltYWxEdXJhdGlvbih3cmFwcGVkLnZhbHVlKTsKICAgICAgICAgIH0KICAgICAgfQogICAgICBWYWx1ZXMudG9TdHJpbmcgPSB0b1N0cmluZzsKICAgICAgLyoqIFdyYXAgYSBsaXRlcmFsIHZhbHVlIHNvIHlvdSBjYW4gc3dpdGNoIG9uIGl0IGVhc2lseS4gKi8KICAgICAgZnVuY3Rpb24gd3JhcFZhbHVlKHZhbCkgewogICAgICAgICAgaWYgKGlzTnVsbCh2YWwpKQogICAgICAgICAgICAgIHJldHVybiB7IHR5cGU6ICJudWxsIiwgdmFsdWU6IHZhbCB9OwogICAgICAgICAgZWxzZSBpZiAoaXNOdW1iZXIodmFsKSkKICAgICAgICAgICAgICByZXR1cm4geyB0eXBlOiAibnVtYmVyIiwgdmFsdWU6IHZhbCB9OwogICAgICAgICAgZWxzZSBpZiAoaXNTdHJpbmcodmFsKSkKICAgICAgICAgICAgICByZXR1cm4geyB0eXBlOiAic3RyaW5nIiwgdmFsdWU6IHZhbCB9OwogICAgICAgICAgZWxzZSBpZiAoaXNCb29sZWFuKHZhbCkpCiAgICAgICAgICAgICAgcmV0dXJuIHsgdHlwZTogImJvb2xlYW4iLCB2YWx1ZTogdmFsIH07CiAgICAgICAgICBlbHNlIGlmIChpc0R1cmF0aW9uKHZhbCkpCiAgICAgICAgICAgICAgcmV0dXJuIHsgdHlwZTogImR1cmF0aW9uIiwgdmFsdWU6IHZhbCB9OwogICAgICAgICAgZWxzZSBpZiAoaXNEYXRlKHZhbCkpCiAgICAgICAgICAgICAgcmV0dXJuIHsgdHlwZTogImRhdGUiLCB2YWx1ZTogdmFsIH07CiAgICAgICAgICBlbHNlIGlmIChpc1dpZGdldCh2YWwpKQogICAgICAgICAgICAgIHJldHVybiB7IHR5cGU6ICJ3aWRnZXQiLCB2YWx1ZTogdmFsIH07CiAgICAgICAgICBlbHNlIGlmIChpc0FycmF5KHZhbCkpCiAgICAgICAgICAgICAgcmV0dXJuIHsgdHlwZTogImFycmF5IiwgdmFsdWU6IHZhbCB9OwogICAgICAgICAgZWxzZSBpZiAoaXNMaW5rKHZhbCkpCiAgICAgICAgICAgICAgcmV0dXJuIHsgdHlwZTogImxpbmsiLCB2YWx1ZTogdmFsIH07CiAgICAgICAgICBlbHNlIGlmIChpc0Z1bmN0aW9uKHZhbCkpCiAgICAgICAgICAgICAgcmV0dXJuIHsgdHlwZTogImZ1bmN0aW9uIiwgdmFsdWU6IHZhbCB9OwogICAgICAgICAgZWxzZSBpZiAoaXNIdG1sKHZhbCkpCiAgICAgICAgICAgICAgcmV0dXJuIHsgdHlwZTogImh0bWwiLCB2YWx1ZTogdmFsIH07CiAgICAgICAgICBlbHNlIGlmIChpc09iamVjdCh2YWwpKQogICAgICAgICAgICAgIHJldHVybiB7IHR5cGU6ICJvYmplY3QiLCB2YWx1ZTogdmFsIH07CiAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgcmV0dXJuIHVuZGVmaW5lZDsKICAgICAgfQogICAgICBWYWx1ZXMud3JhcFZhbHVlID0gd3JhcFZhbHVlOwogICAgICAvKiogUmVjdXJzaXZlbHkgbWFwIGNvbXBsZXggb2JqZWN0cyBhdCB0aGUgbGVhdmVzLiAqLwogICAgICBmdW5jdGlvbiBtYXBMZWF2ZXModmFsLCBmdW5jKSB7CiAgICAgICAgICBpZiAoaXNPYmplY3QodmFsKSkgewogICAgICAgICAgICAgIGxldCByZXN1bHQgPSB7fTsKICAgICAgICAgICAgICBmb3IgKGxldCBba2V5LCB2YWx1ZV0gb2YgT2JqZWN0LmVudHJpZXModmFsKSkKICAgICAgICAgICAgICAgICAgcmVzdWx0W2tleV0gPSBtYXBMZWF2ZXModmFsdWUsIGZ1bmMpOwogICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIGlmIChpc0FycmF5KHZhbCkpIHsKICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0gW107CiAgICAgICAgICAgICAgZm9yIChsZXQgdmFsdWUgb2YgdmFsKQogICAgICAgICAgICAgICAgICByZXN1bHQucHVzaChtYXBMZWF2ZXModmFsdWUsIGZ1bmMpKTsKICAgICAgICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgICAgICAgfQogICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgcmV0dXJuIGZ1bmModmFsKTsKICAgICAgICAgIH0KICAgICAgfQogICAgICBWYWx1ZXMubWFwTGVhdmVzID0gbWFwTGVhdmVzOwogICAgICAvKiogQ29tcGFyZSB0d28gYXJiaXRyYXJ5IEphdmFTY3JpcHQgdmFsdWVzLiBQcm9kdWNlcyBhIHRvdGFsIG9yZGVyaW5nIG92ZXIgQU5ZIHBvc3NpYmxlIGRhdGF2aWV3IHZhbHVlLiAqLwogICAgICBmdW5jdGlvbiBjb21wYXJlVmFsdWUodmFsMSwgdmFsMiwgbGlua05vcm1hbGl6ZXIpIHsKICAgICAgICAgIHZhciBfYSwgX2I7CiAgICAgICAgICAvLyBIYW5kbGUgdW5kZWZpbmVkL251bGxzIGZpcnN0LgogICAgICAgICAgaWYgKHZhbDEgPT09IHVuZGVmaW5lZCkKICAgICAgICAgICAgICB2YWwxID0gbnVsbDsKICAgICAgICAgIGlmICh2YWwyID09PSB1bmRlZmluZWQpCiAgICAgICAgICAgICAgdmFsMiA9IG51bGw7CiAgICAgICAgICBpZiAodmFsMSA9PT0gbnVsbCAmJiB2YWwyID09PSBudWxsKQogICAgICAgICAgICAgIHJldHVybiAwOwogICAgICAgICAgZWxzZSBpZiAodmFsMSA9PT0gbnVsbCkKICAgICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgICBlbHNlIGlmICh2YWwyID09PSBudWxsKQogICAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgICAgLy8gQSBub24tbnVsbCB2YWx1ZSBub3cgd2hpY2ggd2UgY2FuIHdyYXAgJiBjb21wYXJlIG9uLgogICAgICAgICAgbGV0IHdyYXAxID0gd3JhcFZhbHVlKHZhbDEpOwogICAgICAgICAgbGV0IHdyYXAyID0gd3JhcFZhbHVlKHZhbDIpOwogICAgICAgICAgaWYgKHdyYXAxID09PSB1bmRlZmluZWQgJiYgd3JhcDIgPT09IHVuZGVmaW5lZCkKICAgICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICAgIGVsc2UgaWYgKHdyYXAxID09PSB1bmRlZmluZWQpCiAgICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgZWxzZSBpZiAod3JhcDIgPT09IHVuZGVmaW5lZCkKICAgICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICAgIC8vIFNob3J0LWNpcmN1aXQgb24gZGlmZmVyZW50IHR5cGVzIG9yIG9uIHJlZmVyZW5jZSBlcXVhbGl0eS4KICAgICAgICAgIGlmICh3cmFwMS50eXBlICE9IHdyYXAyLnR5cGUpCiAgICAgICAgICAgICAgcmV0dXJuIHdyYXAxLnR5cGUubG9jYWxlQ29tcGFyZSh3cmFwMi50eXBlKTsKICAgICAgICAgIGlmICh3cmFwMS52YWx1ZSA9PT0gd3JhcDIudmFsdWUpCiAgICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgICBzd2l0Y2ggKHdyYXAxLnR5cGUpIHsKICAgICAgICAgICAgICBjYXNlICJzdHJpbmciOgogICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcDEudmFsdWUubG9jYWxlQ29tcGFyZSh3cmFwMi52YWx1ZSk7CiAgICAgICAgICAgICAgY2FzZSAibnVtYmVyIjoKICAgICAgICAgICAgICAgICAgaWYgKHdyYXAxLnZhbHVlIDwgd3JhcDIudmFsdWUpCiAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKHdyYXAxLnZhbHVlID09IHdyYXAyLnZhbHVlKQogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgICAgICAgIGNhc2UgIm51bGwiOgogICAgICAgICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICAgICAgICBjYXNlICJib29sZWFuIjoKICAgICAgICAgICAgICAgICAgaWYgKHdyYXAxLnZhbHVlID09IHdyYXAyLnZhbHVlKQogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwMS52YWx1ZSA/IDEgOiAtMTsKICAgICAgICAgICAgICBjYXNlICJsaW5rIjoKICAgICAgICAgICAgICAgICAgbGV0IGxpbmsxID0gd3JhcDEudmFsdWU7CiAgICAgICAgICAgICAgICAgIGxldCBsaW5rMiA9IHdyYXAyLnZhbHVlOwogICAgICAgICAgICAgICAgICBsZXQgbm9ybWFsaXplID0gbGlua05vcm1hbGl6ZXIgIT09IG51bGwgJiYgbGlua05vcm1hbGl6ZXIgIT09IHZvaWQgMCA/IGxpbmtOb3JtYWxpemVyIDogKCh4KSA9PiB4KTsKICAgICAgICAgICAgICAgICAgLy8gV2UgY2FuJ3QgY29tcGFyZSBieSBmaWxlIG5hbWUgb3IgZGlzcGxheSwgc2luY2UgdGhhdCB3b3VsZCBicmVhayBsaW5rIGVxdWFsaXR5LiBDb21wYXJlIGJ5IHBhdGguCiAgICAgICAgICAgICAgICAgIGxldCBwYXRoQ29tcGFyZSA9IG5vcm1hbGl6ZShsaW5rMS5wYXRoKS5sb2NhbGVDb21wYXJlKG5vcm1hbGl6ZShsaW5rMi5wYXRoKSk7CiAgICAgICAgICAgICAgICAgIGlmIChwYXRoQ29tcGFyZSAhPSAwKQogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHBhdGhDb21wYXJlOwogICAgICAgICAgICAgICAgICAvLyBUaGVuIGNvbXBhcmUgYnkgdHlwZS4KICAgICAgICAgICAgICAgICAgbGV0IHR5cGVDb21wYXJlID0gbGluazEudHlwZS5sb2NhbGVDb21wYXJlKGxpbmsyLnR5cGUpOwogICAgICAgICAgICAgICAgICBpZiAodHlwZUNvbXBhcmUgIT0gMCkKICAgICAgICAgICAgICAgICAgICAgIHJldHVybiB0eXBlQ29tcGFyZTsKICAgICAgICAgICAgICAgICAgLy8gVGhlbiBjb21wYXJlIGJ5IHN1YnBhdGggZXhpc3RlbmNlLgogICAgICAgICAgICAgICAgICBpZiAobGluazEuc3VicGF0aCAmJiAhbGluazIuc3VicGF0aCkKICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgICAgICAgICAgICBpZiAoIWxpbmsxLnN1YnBhdGggJiYgbGluazIuc3VicGF0aCkKICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICAgICAgICAgICAgaWYgKCFsaW5rMS5zdWJwYXRoICYmICFsaW5rMi5zdWJwYXRoKQogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgICAgICAgICAgIC8vIFNpbmNlIGJvdGggaGF2ZSBhIHN1YnBhdGgsIGNvbXBhcmUgYnkgc3VicGF0aC4KICAgICAgICAgICAgICAgICAgcmV0dXJuICgoX2EgPSBsaW5rMS5zdWJwYXRoKSAhPT0gbnVsbCAmJiBfYSAhPT0gdm9pZCAwID8gX2EgOiAiIikubG9jYWxlQ29tcGFyZSgoX2IgPSBsaW5rMi5zdWJwYXRoKSAhPT0gbnVsbCAmJiBfYiAhPT0gdm9pZCAwID8gX2IgOiAiIik7CiAgICAgICAgICAgICAgY2FzZSAiZGF0ZSI6CiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwMS52YWx1ZSA8IHdyYXAyLnZhbHVlCiAgICAgICAgICAgICAgICAgICAgICA/IC0xCiAgICAgICAgICAgICAgICAgICAgICA6IHdyYXAxLnZhbHVlLmVxdWFscyh3cmFwMi52YWx1ZSkKICAgICAgICAgICAgICAgICAgICAgICAgICA/IDAKICAgICAgICAgICAgICAgICAgICAgICAgICA6IDE7CiAgICAgICAgICAgICAgY2FzZSAiZHVyYXRpb24iOgogICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcDEudmFsdWUgPCB3cmFwMi52YWx1ZQogICAgICAgICAgICAgICAgICAgICAgPyAtMQogICAgICAgICAgICAgICAgICAgICAgOiB3cmFwMS52YWx1ZS5lcXVhbHMod3JhcDIudmFsdWUpCiAgICAgICAgICAgICAgICAgICAgICAgICAgPyAwCiAgICAgICAgICAgICAgICAgICAgICAgICAgOiAxOwogICAgICAgICAgICAgIGNhc2UgImFycmF5IjoKICAgICAgICAgICAgICAgICAgbGV0IGYxID0gd3JhcDEudmFsdWU7CiAgICAgICAgICAgICAgICAgIGxldCBmMiA9IHdyYXAyLnZhbHVlOwogICAgICAgICAgICAgICAgICBmb3IgKGxldCBpbmRleCA9IDA7IGluZGV4IDwgTWF0aC5taW4oZjEubGVuZ3RoLCBmMi5sZW5ndGgpOyBpbmRleCsrKSB7CiAgICAgICAgICAgICAgICAgICAgICBsZXQgY29tcCA9IGNvbXBhcmVWYWx1ZShmMVtpbmRleF0sIGYyW2luZGV4XSk7CiAgICAgICAgICAgICAgICAgICAgICBpZiAoY29tcCAhPSAwKQogICAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBjb21wOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHJldHVybiBmMS5sZW5ndGggLSBmMi5sZW5ndGg7CiAgICAgICAgICAgICAgY2FzZSAib2JqZWN0IjoKICAgICAgICAgICAgICAgICAgbGV0IG8xID0gd3JhcDEudmFsdWU7CiAgICAgICAgICAgICAgICAgIGxldCBvMiA9IHdyYXAyLnZhbHVlOwogICAgICAgICAgICAgICAgICBsZXQgazEgPSBBcnJheS5mcm9tKE9iamVjdC5rZXlzKG8xKSk7CiAgICAgICAgICAgICAgICAgIGxldCBrMiA9IEFycmF5LmZyb20oT2JqZWN0LmtleXMobzIpKTsKICAgICAgICAgICAgICAgICAgazEuc29ydCgpOwogICAgICAgICAgICAgICAgICBrMi5zb3J0KCk7CiAgICAgICAgICAgICAgICAgIGxldCBrZXlDb21wYXJlID0gY29tcGFyZVZhbHVlKGsxLCBrMik7CiAgICAgICAgICAgICAgICAgIGlmIChrZXlDb21wYXJlICE9IDApCiAgICAgICAgICAgICAgICAgICAgICByZXR1cm4ga2V5Q29tcGFyZTsKICAgICAgICAgICAgICAgICAgZm9yIChsZXQga2V5IG9mIGsxKSB7CiAgICAgICAgICAgICAgICAgICAgICBsZXQgY29tcCA9IGNvbXBhcmVWYWx1ZShvMVtrZXldLCBvMltrZXldKTsKICAgICAgICAgICAgICAgICAgICAgIGlmIChjb21wICE9IDApCiAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGNvbXA7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgICAgICAgY2FzZSAid2lkZ2V0IjoKICAgICAgICAgICAgICBjYXNlICJodG1sIjoKICAgICAgICAgICAgICBjYXNlICJmdW5jdGlvbiI6CiAgICAgICAgICAgICAgICAgIHJldHVybiAwOwogICAgICAgICAgfQogICAgICB9CiAgICAgIFZhbHVlcy5jb21wYXJlVmFsdWUgPSBjb21wYXJlVmFsdWU7CiAgICAgIC8qKiBGaW5kIHRoZSBjb3JyZXNwb25kaW5nIERhdGF2ZWl3IHR5cGUgZm9yIGFuIGFyYml0cmFyeSB2YWx1ZS4gKi8KICAgICAgZnVuY3Rpb24gdHlwZU9mKHZhbCkgewogICAgICAgICAgdmFyIF9hOwogICAgICAgICAgcmV0dXJuIChfYSA9IHdyYXBWYWx1ZSh2YWwpKSA9PT0gbnVsbCB8fCBfYSA9PT0gdm9pZCAwID8gdm9pZCAwIDogX2EudHlwZTsKICAgICAgfQogICAgICBWYWx1ZXMudHlwZU9mID0gdHlwZU9mOwogICAgICAvKiogRGV0ZXJtaW5lIGlmIHRoZSBnaXZlbiB2YWx1ZSBpcyAidHJ1dGh5IiAoaS5lLiwgaXMgbm9uLW51bGwgYW5kIGhhcyBkYXRhIGluIGl0KS4gKi8KICAgICAgZnVuY3Rpb24gaXNUcnV0aHkoZmllbGQpIHsKICAgICAgICAgIGxldCB3cmFwcGVkID0gd3JhcFZhbHVlKGZpZWxkKTsKICAgICAgICAgIGlmICghd3JhcHBlZCkKICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgICBzd2l0Y2ggKHdyYXBwZWQudHlwZSkgewogICAgICAgICAgICAgIGNhc2UgIm51bWJlciI6CiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwcGVkLnZhbHVlICE9IDA7CiAgICAgICAgICAgICAgY2FzZSAic3RyaW5nIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUubGVuZ3RoID4gMDsKICAgICAgICAgICAgICBjYXNlICJib29sZWFuIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWU7CiAgICAgICAgICAgICAgY2FzZSAibGluayI6CiAgICAgICAgICAgICAgICAgIHJldHVybiAhIXdyYXBwZWQudmFsdWUucGF0aDsKICAgICAgICAgICAgICBjYXNlICJkYXRlIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUudG9NaWxsaXMoKSAhPSAwOwogICAgICAgICAgICAgIGNhc2UgImR1cmF0aW9uIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUuYXMoInNlY29uZHMiKSAhPSAwOwogICAgICAgICAgICAgIGNhc2UgIm9iamVjdCI6CiAgICAgICAgICAgICAgICAgIHJldHVybiBPYmplY3Qua2V5cyh3cmFwcGVkLnZhbHVlKS5sZW5ndGggPiAwOwogICAgICAgICAgICAgIGNhc2UgImFycmF5IjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUubGVuZ3RoID4gMDsKICAgICAgICAgICAgICBjYXNlICJudWxsIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgICAgICAgIGNhc2UgImh0bWwiOgogICAgICAgICAgICAgIGNhc2UgIndpZGdldCI6CiAgICAgICAgICAgICAgY2FzZSAiZnVuY3Rpb24iOgogICAgICAgICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICAgIH0KICAgICAgfQogICAgICBWYWx1ZXMuaXNUcnV0aHkgPSBpc1RydXRoeTsKICAgICAgLyoqIERlZXAgY29weSBhIGZpZWxkLiAqLwogICAgICBmdW5jdGlvbiBkZWVwQ29weShmaWVsZCkgewogICAgICAgICAgaWYgKGZpZWxkID09PSBudWxsIHx8IGZpZWxkID09PSB1bmRlZmluZWQpCiAgICAgICAgICAgICAgcmV0dXJuIGZpZWxkOwogICAgICAgICAgaWYgKFZhbHVlcy5pc0FycmF5KGZpZWxkKSkgewogICAgICAgICAgICAgIHJldHVybiBbXS5jb25jYXQoZmllbGQubWFwKHYgPT4gZGVlcENvcHkodikpKTsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgaWYgKFZhbHVlcy5pc09iamVjdChmaWVsZCkpIHsKICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0ge307CiAgICAgICAgICAgICAgZm9yIChsZXQgW2tleSwgdmFsdWVdIG9mIE9iamVjdC5lbnRyaWVzKGZpZWxkKSkKICAgICAgICAgICAgICAgICAgcmVzdWx0W2tleV0gPSBkZWVwQ29weSh2YWx1ZSk7CiAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgIHJldHVybiBmaWVsZDsKICAgICAgICAgIH0KICAgICAgfQogICAgICBWYWx1ZXMuZGVlcENvcHkgPSBkZWVwQ29weTsKICAgICAgZnVuY3Rpb24gaXNTdHJpbmcodmFsKSB7CiAgICAgICAgICByZXR1cm4gdHlwZW9mIHZhbCA9PSAic3RyaW5nIjsKICAgICAgfQogICAgICBWYWx1ZXMuaXNTdHJpbmcgPSBpc1N0cmluZzsKICAgICAgZnVuY3Rpb24gaXNOdW1iZXIodmFsKSB7CiAgICAgICAgICByZXR1cm4gdHlwZW9mIHZhbCA9PSAibnVtYmVyIjsKICAgICAgfQogICAgICBWYWx1ZXMuaXNOdW1iZXIgPSBpc051bWJlcjsKICAgICAgZnVuY3Rpb24gaXNEYXRlKHZhbCkgewogICAgICAgICAgcmV0dXJuIHZhbCBpbnN0YW5jZW9mIERhdGVUaW1lOwogICAgICB9CiAgICAgIFZhbHVlcy5pc0RhdGUgPSBpc0RhdGU7CiAgICAgIGZ1bmN0aW9uIGlzRHVyYXRpb24odmFsKSB7CiAgICAgICAgICByZXR1cm4gdmFsIGluc3RhbmNlb2YgRHVyYXRpb247CiAgICAgIH0KICAgICAgVmFsdWVzLmlzRHVyYXRpb24gPSBpc0R1cmF0aW9uOwogICAgICBmdW5jdGlvbiBpc051bGwodmFsKSB7CiAgICAgICAgICByZXR1cm4gdmFsID09PSBudWxsIHx8IHZhbCA9PT0gdW5kZWZpbmVkOwogICAgICB9CiAgICAgIFZhbHVlcy5pc051bGwgPSBpc051bGw7CiAgICAgIGZ1bmN0aW9uIGlzQXJyYXkodmFsKSB7CiAgICAgICAgICByZXR1cm4gQXJyYXkuaXNBcnJheSh2YWwpOwogICAgICB9CiAgICAgIFZhbHVlcy5pc0FycmF5ID0gaXNBcnJheTsKICAgICAgZnVuY3Rpb24gaXNCb29sZWFuKHZhbCkgewogICAgICAgICAgcmV0dXJuIHR5cGVvZiB2YWwgPT09ICJib29sZWFuIjsKICAgICAgfQogICAgICBWYWx1ZXMuaXNCb29sZWFuID0gaXNCb29sZWFuOwogICAgICBmdW5jdGlvbiBpc0xpbmsodmFsKSB7CiAgICAgICAgICByZXR1cm4gdmFsIGluc3RhbmNlb2YgTGluazsKICAgICAgfQogICAgICBWYWx1ZXMuaXNMaW5rID0gaXNMaW5rOwogICAgICBmdW5jdGlvbiBpc1dpZGdldCh2YWwpIHsKICAgICAgICAgIHJldHVybiB2YWwgaW5zdGFuY2VvZiBXaWRnZXQ7CiAgICAgIH0KICAgICAgVmFsdWVzLmlzV2lkZ2V0ID0gaXNXaWRnZXQ7CiAgICAgIGZ1bmN0aW9uIGlzSHRtbCh2YWwpIHsKICAgICAgICAgIGlmICh0eXBlb2YgSFRNTEVsZW1lbnQgIT09ICJ1bmRlZmluZWQiKSB7CiAgICAgICAgICAgICAgcmV0dXJuIHZhbCBpbnN0YW5jZW9mIEhUTUxFbGVtZW50OwogICAgICAgICAgfQogICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgICAgfQogICAgICB9CiAgICAgIFZhbHVlcy5pc0h0bWwgPSBpc0h0bWw7CiAgICAgIC8qKiBDaGVja3MgaWYgdGhlIGdpdmVuIHZhbHVlIGlzIGFuIG9iamVjdCAoYW5kIG5vdCBhbnkgb3RoZXIgZGF0YXZpZXctcmVjb2duaXplZCBvYmplY3QtbGlrZSB0eXBlKS4gKi8KICAgICAgZnVuY3Rpb24gaXNPYmplY3QodmFsKSB7CiAgICAgICAgICByZXR1cm4gKHR5cGVvZiB2YWwgPT0gIm9iamVjdCIgJiYKICAgICAgICAgICAgICAhaXNIdG1sKHZhbCkgJiYKICAgICAgICAgICAgICAhaXNXaWRnZXQodmFsKSAmJgogICAgICAgICAgICAgICFpc0FycmF5KHZhbCkgJiYKICAgICAgICAgICAgICAhaXNEdXJhdGlvbih2YWwpICYmCiAgICAg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GxldCBlbGVtZW50IG9mIGVudHJ5KQogICAgICAgICAgICAgIGlmICghaXNFbGVtZW50R3JvdXAoZWxlbWVudCkpCiAgICAgICAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICB9CiAgICAgIEdyb3VwaW5ncy5pc0dyb3VwaW5nID0gaXNHcm91cGluZzsKICAgICAgLyoqIENvdW50IHRoZSB0b3RhbCBudW1iZXIgb2YgZWxlbWVudHMgaW4gYSByZWN1cnNpdmUgZ3JvdXBpbmcuICovCiAgICAgIGZ1bmN0aW9uIGNvdW50KGVsZW1lbnRzKSB7CiAgICAgICAgICBpZiAoaXNHcm91cGluZyhlbGVtZW50cykpIHsKICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0gMDsKICAgICAgICAgICAgICBmb3IgKGxldCBzdWJncm91cCBvZiBlbGVtZW50cykKICAgICAgICAgICAgICAgICAgcmVzdWx0ICs9IGNvdW50KHN1Ymdyb3VwLnJvd3MpOwogICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICByZXR1cm4gZWxlbWVudHMubGVuZ3RoOwogICAgICAgICAgfQogICAgICB9CiAgICAgIEdyb3VwaW5ncy5jb3VudCA9IGNvdW50OwogIH0pKEdyb3VwaW5ncyB8fCAoR3JvdXBpbmdzID0ge30pKTsKICAvLy8vLy8vLy8vCiAgLy8gTElOSyAvLwogIC8vLy8vLy8vLy8KICAvKiogVGhlIE9ic2lkaWFuICdsaW5rJywgdXNlZCBmb3IgdW5pcXVlbHkgZGVzY3JpYmluZyBhIGZpbGUsIGhlYWRlciwgb3IgYmxvY2suICovCiAgY2xhc3MgTGluayB7CiAgICAgIC8qKiBDcmVhdGUgYSBsaW5rIHRvIGEgc3BlY2lmaWMgZmlsZS4gKi8KICAgICAgc3RhdGljIGZpbGUocGF0aCwgZW1iZWQgPSBmYWxzZSwgZGlzcGxheSkgewogICAgICAgICAgcmV0dXJuIG5ldyBMaW5rKHsKICAgICAgICAgICAgICBwYXRoLAogICAgICAgICAgICAgIGVtYmVkLAogICAgICAgICAgICAgIGRpc3BsYXksCiAgICAgICAgICAgICAgc3VicGF0aDogdW5kZWZpbmVkLAogICAgICAgICAgICAgIHR5cGU6ICJmaWxlIiwKICAgICAgICAgIH0pOwogICAgICB9CiAgICAgIHN0YXRpYyBpbmZlcihsaW5rcGF0aCwgZW1iZWQgPSBmYWxzZSwgZGlzcGxheSkgewogICAgICAgICAgaWYgKGxpbmtwYXRoLmluY2x1ZGVzKCIjXiIpKSB7CiAgICAgICAgICAgICAgbGV0IHNwbGl0ID0gbGlua3BhdGguc3BsaXQoIiNeIik7CiAgICAgICAgICAgICAgcmV0dXJuIExpbmsuYmxvY2soc3BsaXRbMF0sIHNwbGl0WzFdLCBlbWJlZCwgZGlzcGxheSk7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIGlmIChsaW5rcGF0aC5pbmNsdWRlcygiIyIpKSB7CiAgICAgICAgICAgICAgbGV0IHNwbGl0ID0gbGlua3BhdGguc3BsaXQoIiMiKTsKICAgICAgICAgICAgICByZXR1cm4gTGluay5oZWFkZXIoc3BsaXRbMF0sIHNwbGl0WzFdLCBlbWJlZCwgZGlzcGxheSk7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgcmV0dXJuIExpbmsuZmlsZShsaW5rcGF0aCwgZW1iZWQsIGRpc3BsYXkpOwogICAgICB9CiAgICAgIC8qKiBDcmVhdGUgYSBsaW5rIHRvIGEgc3BlY2lmaWMgZmlsZSBhbmQgaGVhZGVyIGluIHRoYXQgZmlsZS4gKi8KICAgICAgc3RhdGljIGhlYWRlcihwYXRoLCBoZWFkZXIsIGVtYmVkLCBkaXNwbGF5KSB7CiAgICAgICAgICAvLyBIZWFkZXJzIG5lZWQgdG8gYmUgbm9ybWFsaXplZCB0byBhbHBoYS1udW1lcmljICYgd2l0aCBleHRyYSBzcGFjaW5nIHJlbW92ZWQuCiAgICAgICAgICByZXR1cm4gbmV3IExpbmsoewogICAgICAgICAgICAgIHBhdGgsCiAgICAgICAgICAgICAgZW1iZWQsCiAgICAgICAgICAgICAgZGlzcGxheSwKICAgICAgICAgICAgICBzdWJwYXRoOiBub3JtYWxpemVIZWFkZXJGb3JMaW5rKGhlYWRlciksCiAgICAgICAgICAgICAgdHlwZTogImhlYWRlciIsCiAgICAgICAgICB9KTsKICAgICAgfQogICAgICAvKiogQ3JlYXRlIGEgbGluayB0byBhIHNwZWNpZmljIGZpbGUgYW5kIGJsb2NrIGluIHRoYXQgZmlsZS4gKi8KICAgICAgc3RhdGljIGJsb2NrKHBhdGgsIGJsb2NrSWQsIGVtYmVkLCBkaXNwbGF5KSB7CiAgICAgICAgICByZXR1cm4gbmV3IExpbmsoewogICAgICAgICAgICAgIHBhdGgsCiAgICAgICAgICAgICAgZW1iZWQsCiAgICAgICAgICAgICAgZGlzcGxheSwKICAgICAgICAgICAgICBzdWJwYXRoOiBibG9ja0lkLAogICAgICAgICAgICAgIHR5cGU6ICJibG9jayIsCiAgICAgICAgICB9KTsKICAgICAgfQogICAgICBzdGF0aWMgZnJvbU9iamVjdChvYmplY3QpIHsKICAgICAgICAgIHJldHVybiBuZXcgTGluayhvYmplY3QpOwogICAgICB9CiAgICAgIGNvbnN0cnVjdG9yKGZpZWxkcykgewogICAgICAgICAgT2JqZWN0LmFzc2lnbih0aGlzLCBmaWVsZHMpOwogICAgICB9CiAgICAgIC8qKiBDaGVja3MgZm9yIGxpbmsgZXF1YWxpdHkgKGkuZS4sIHRoYXQgdGhlIGxpbmtzIGFyZSBwb2ludGluZyB0byB0aGUgc2FtZSBleGFjdCBsb2NhdGlvbikuICovCiAgICAgIGVxdWFscyhvdGhlcikgewogICAgICAgICAgaWYgKG90aGVyID09IHVuZGVmaW5lZCB8fCBvdGhlciA9PSBudWxsKQogICAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICAgIHJld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zcGxheSkgewogICAgICAgICAgcmV0dXJuIG5ldyBMaW5rKE9iamVjdC5hc3NpZ24oe30sIHRoaXMsIHsgZGlzcGxheSB9KSk7CiAgICAgIH0KICAgICAgLyoqIENvbnZlcnQgYSBmaWxlIGxpbmsgaW50byBhIGxpbmsgdG8gYSBzcGVjaWZpYyBoZWFkZXIuICovCiAgICAgIHdpdGhIZWFkZXIoaGVhZGVyKSB7CiAgICAgICAgICByZXR1cm4gTGluay5oZWFkZXIodGhpcy5wYXRoLCBoZWFkZXIsIHRoaXMuZW1iZWQsIHRoaXMuZGlzcGxheSk7CiAgICAgIH0KICAgICAgLyoqIENvbnZlcnQgYW55IGxpbmsgaW50byBhIGxpbmsgdG8gaXRzIGZpbGUuICovCiAgICAgIHRvRmlsZSgpIHsKICAgICAgICAgIHJldHVybiBMaW5rLmZpbGUodGhpcy5wYXRoLCB0aGlzLmVtYmVkLCB0aGlzLmRpc3BsYXkpOwogICAgICB9CiAgICAgIC8qKiBDb252ZXJ0IHRoaXMgbGluayBpbnRvIGFuIGVtYmVkZGVkIGxpbmsuICovCiAgICAgIHRvRW1iZWQoKSB7CiAgICAgICAgICBpZiAodGhpcy5lbWJlZCkgewogICAgICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgICAgfQogICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgbGV0IGxpbmsgPSBuZXcgTGluayh0aGlzKTsKICAgICAgICAgICAgICBsaW5rLmVtYmVkID0gdHJ1ZTsKICAgICAgICAgICAgICByZXR1cm4gbGluazsKICAgICAgICAgIH0KICAgICAgfQogICAgICAvKiogQ29udmVydCB0aGlzIGxpbmsgaW50byBhIG5vbi1lbWJlZGRlZCBsaW5rLiAqLwogICAgICBmcm9tRW1iZWQoKSB7CiAgICAgICAgICBpZiAoIXRoaXMuZW1iZWQpIHsKICAgICAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgIGxldCBsaW5rID0gbmV3IExpbmsodGhpcyk7CiAgICAgICAgICAgICAgbGluay5lbWJlZCA9IGZhbHNlOwogICAgICAgICAgICAgIHJldHVybiBsaW5rOwogICAgICAgICAgfQogICAgICB9CiAgICAgIC8qKiBDb252ZXJ0IHRoaXMgbGluayB0byBtYXJrZG93biBzbyBpdCBjYW4gYmUgcmVuZGVyZWQuICovCiAgICAgIG1hcmtkb3duKCkgewogICAgICAgICAgbGV0IHJlc3VsdCA9ICh0aGlzLmVtYmVkID8gIiEiIDogIiIpICsgIltbIiArIHRoaXMub2JzaWRpYW5MaW5rKCk7CiAgICAgICAgICBpZiAodGhpcy5kaXNwbGF5KSB7CiAgICAgICAgICAgICAgcmVzdWx0ICs9ICJ8IiArIHRoaXMuZGlzcGxheTsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgIHJlc3VsdCArPSAifCIgKyBnZXRGaWxlVGl0bGUodGhpcy5wYXRoKTsKICAgICAgICAgICAgICBpZiAodGhpcy50eXBlID09ICJoZWFkZXIiIHx8IHRoaXMudHlwZSA9PSAiYmxvY2siKQogICAgICAgICAgICAgICAgICByZXN1bHQgKz0gIiA+ICIgKyB0aGlzLnN1YnBhdGg7CiAgICAgICAgICB9CiAgICAgICAgICByZXN1bHQgKz0gIl1dIjsKICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgIH0KICAgICAgLyoqIENvbnZlcnQgdGhlIGlubmVyIHBhcnQgb2YgdGhlIGxpbmsgdG8gc29tZXRoaW5nIHRoYXQgT2JzaWRpYW4gY2FuIG9wZW4gLyB1bmRlcnN0YW5kLiAqLwogICAgICBvYnNpZGlhbkxpbmsoKSB7CiAgICAgICAgICB2YXIgX2EsIF9iOwogICAgICAgICAgY29uc3QgZXNjYXBlZCA9IHRoaXMucGF0aC5yZXBsYWNlKCJ8IiwgIlxcfCIpOwogICAgICAgICAgaWYgKHRoaXMudHlwZSA9PSAiaGVhZGVyIikKICAgICAgICAgICAgICByZXR1cm4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uY3Rpb24gaW5kZXgob2JqLCBpbmRleCkgewogICAgICAgICAgcmV0dXJuIHsgdHlwZTogImluZGV4Iiwgb2JqZWN0OiBvYmosIGluZGV4IH07CiAgICAgIH0KICAgICAgRmllbGRzLmluZGV4ID0gaW5kZXg7CiAgICAgIC8qKiBDb252ZXJ0cyBhIHN0cmluZyBpbiBkb3Qtbm90YXRpb24tZm9ybWF0IGludG8gYSB2YXJpYWJsZSB3aGljaCBpbmRleGVzLiAqLwogICAgICBmdW5jdGlvbiBpbmRleFZhcmlhYmxlKG5hbWUpIHsKICAgICAgICAgIGxldCBwYXJ0cyA9IG5hbWUuc3BsaXQoIi4iKTsKICAgICAgICAgIGxldCByZXN1bHQgPSBGaWVsZHMudmFyaWFibGUocGFydHNbMF0pOwogICAgICAgICAgZm9yIChsZXQgaW5kZXggPSAxOyBpbmRleCA8IHBhcnRzLmxlbmd0aDsgaW5kZXgrKykgewogICAgICAgICAgICAgIHJlc3VsdCA9IEZpZWxkcy5pbmRleChyZXN1bHQsIEZpZWxkcy5saXRlcmFsKHBhcnRzW2luZGV4XSkpOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgfQogICAgICBGaWVsZHMuaW5kZXhWYXJpYWJsZSA9IGluZGV4VmFyaWFibGU7CiAgICAgIGZ1bmN0aW9uIGxhbWJkYShhcmdzLCB2YWx1ZSkgewogICAgICAgICAgcmV0dXJuIHsgdHlwZTogImxhbWJkYSIsIGFyZ3VtZW50czogYXJncywgdmFsdWUgfTsKICAgICAgfQogICAgICBGaWVsZHMubGFtYmRhID0gbGFtYmRhOwogICAgICBmdW5jdGlvbiBmdW5jKGZ1bmMsIGFyZ3MpIHsKICAgICAgICAgIHJldHVybiB7IHR5cGU6ICJmdW5jdGlvbiIsIGZ1bmMsIGFyZ3VtZW50czogYXJncyB9OwogICAgICB9CiAgICAgIEZpZWxkcy5mdW5jID0gZnVuYzsKICAgICAgZnVuY3Rpb24gbGlzdCh2YWx1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hdGlvbi5mcm9tT2JqZWN0KHsgZGF5czogMSB9KSwKICAgICAgaG91cjogRHVyYXRpb24uZnJvbU9iamVjdCh7IGhvdXJzOiAxIH0pLAogICAgICBob3VyczogRHVyYXRpb24uZnJvbU9iamVjdCh7IGhvdXJzOiAxIH0pLAogICAgICBocjogRHVyYXRpb24uZnJvbU9iamVjdCh7IGhvdXJzOiAxIH0pLAogICAgICBocnM6IER1cmF0aW9uLmZyb21PYmplY3QoeyBob3VyczogMSB9KSwKICAgICAgaDogRHVyYXRpb24uZnJvbU9iamVjdCh7IGhvdXJzOiAxIH0pLAogICAgICBtaW51dGU6IER1cmF0aW9uLmZyb21PYmplY3QoeyBtaW51dGVzOiAxIH0pLAogICAgICBtaW51dGVzOiBEdXJhdGlvbi5mcm9tT2JqZWN0KHsgbWludXRlczogMSB9KSwKICAgICAgbWluOiBEdXJhdGlvbi5mcm9tT2JqZWN0KHsgbWludXRlczogMSB9KSwKICAgICAgbWluczogRHVyYXRpb24uZnJvbU9iamVjdCh7IG1pbnV0ZXM6IDEgfSksCiAgICAgIG06IER1cmF0aW9uLmZyb21PYmplY3QoeyBtaW51dGVzOiAxIH0pLAogICAgICBzZWNvbmQ6IER1cmF0aW9uLmZyb21PYmplY3QoeyBzZWNvbmRzOiAxIH0pLAogICAgICBzZWNvbmRzOiBEdXJhdGlvbi5mcm9tT2JqZWN0KHsgc2Vjb25kczogMSB9KSwKICAgICAgc2VjOiBEdXJhdGlvbi5mcm9tT2JqZWN0KHsgc2Vjb25kczogMSB9KSwKICAgICAgc2VjczogRHVyYXRpb24uZnJvbU9iamVjdCh7IHNlY29uZHM6IDEgfSksCiAgICAgIHM6IER1cmF0aW9uLmZyb21PYmplY3QoeyBzZWNvbmRzOiAxIH0pLAogIH07CiAgLyoqIFNob3J0aGFuZCBmb3IgY29tbW9uIGRhdGVzIChyZWxhdGl2ZSB0byByaWdodCBub3cpLiAqLwogIGNvbnN0IERBVEVfU0hPUlRIQU5EUyA9IHsKICAgICA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kZSwgcmVzdFtpbmRleF1bMV0sIHJlc3RbaW5kZXhdWzNdKTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBub2RlOwogICAgICB9KTsKICB9CiAgZnVuY3Rpb24gY2hhaW5PcHQoYmFzZSwgLi4uZnVuY3MpIHsKICAgICAgcmV0dXJuIHBhcnNpbW1vbl91bWRfbWluRXhwb3J0cy5jdXN0b20oKHN1Y2Nlc3MsIGZhaWx1cmUpID0+IHsKICAgICAgICAgIHJldHVybiAoaW5wdXQsIGkpID0+IHsKICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0gYmFzZS5fKGlucHV0LCBpKTsKICAgICAgICAgICAgICBpZiAoIXJlc3VsdC5zdGF0dXMpCiAgICAgICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICAgICAgZm9yIChsZXQgZnVuYyBvZiBmdW5jcykgewogICAgICAgICAgICAgICAgICBsZXQgbmV4dCA9IGZ1bmMocmVzdWx0LnZhbHVlKS5fKGlucHV0LCByZXN1bHQuaW5kZXgpOwogICAgICAgICAgICAgICAgICBpZiAoIW5leHQuc3RhdHVzKQogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICAgICAgICAgICAgcmVzdWx0ID0gbmV4dDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICAgIH07CiAgICAgIH0pOwogIH0KICBjb25zdCBFWFBSRVNTSU9OID0gcGFyc2ltbW9uX3VtZF9taW5FeHBvcnRzLmNyZWF0ZUxhbmd1YWdlKHsKICAgICAgLy8gQSBmbG9hdGluZyBwb2ludCBudW1iZXI7IHRoZSBkZWNpbWFsIHBvaW50IGlzIG9wdGlvbmFsLgogICAgICBudW1iZXI6IHEgPT4gcGFyc2ltbW9uX3VtZF9taW5FeHBvcnRzLnJlZ2V4cCgvLT9bMC05XSsoXC5bMC05XSspPy8pCiAgICAgICAgICAubWFwKHN0ciA9PiBOdW1iZXIucGFyc2VGbG9hdChzdHIpKQogICAgICAgICAgLmRlc2MoIm51bWJlciIpLAogICAgICAvLyBBIHF1b3RlLXN1cnJvdW5kZWQgc3RyaW5nIHdoaWNoIHN1cHBvcnRzIGVzY2FwZSBjaGFyYWN0ZXJzICgnXCcpLgogICAgICBzdHJpbmc6IHEgPT4gcGFyc2ltbW9uX3VtZF9taW5FeHBvcnRzLnN0cmluZygnIicpCiAgICAgICAgICAudGhlbihwYXJzaW1tb25fdW1kX21pbkV4cG9ydHMuYWx0KHEuZXNjYXBlQ2hhcmFjdGVyLCBwYXJzaW1tb25fdW1kX21pbkV4cG9ydHMubm9uZU9mKCciXFwnKSkKICAgICAgICAgIC5hdExlYXN0KDApCiAgICAgICAgICAubWFwKGNoYXJzID0+IGNoYXJzLmpvaW4oIiIpKSkKICAgICAgICAgIC5za2lwKHBhcnNpbW1vbl91bWRfbWluRXhwb3J0cy5zdHJpbmcoJyInKSkKICAgICAgICAgIC5kZXNjKCJzdHJpbmciKSwKICAgICAgZXNjYXBlQ2hhcmFjdGVyOiBfID0+IHBhcnNpbW1vbl91bWRfbWluRXhwb3J0cy5zdHJpbmcoIlxcIikKICAgICAgICAgIC50aGVuKHBhcnNpbW1vbl91bWRfbWluRXhwb3J0cy5hbnkpCiAgICAgICAgICAubWFwKGVzY2FwZWQgPT4gewogICAgICAgICAgLy8gSWYgd2UgYXJlIGVzY2FwaW5nIGEgYmFja3NsYXNoIG9yIGEgcXVvdGUsIHBhc3MgaW4gb24gaW4gZXNjYXBlZCBmb3JtCiAgICAgICAgICBpZiAoZXNjYXBlZCA9PT0gJyInKQogICAgICAgICAgICAgIHJldHVybiAnIic7CiAgICAgICAgICBpZiAoZXNjYXBlZCA9PT0gIlxcIikKICAgICAgICAgICAgICByZXR1cm4gIlxcIjsKICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICByZXR1cm4gIlxcIiArIGVzY2FwZWQ7CiAgICAgIH0pLAogICAgICAvLyBBIGJvb2xlYW4g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AgICAgLy8gQmluYXJ5IHBsdXMgb3IgbWludXMgb3BlcmF0b3IuCiAgICAgIGJpbmFyeVBsdXNNaW51czogXyA9PiBwYXJzaW1tb25fdW1kX21pbkV4cG9ydHMucmVnZXhwKC9cK3wtLykKICAgICAgICAgIC5tYXAoc3RyID0+IHN0cikKICAgICAgICAgIC5kZXNjKCInKycgb3IgJy0nIiksCiAgICAgIC8vIEJpbmFyeSB0aW1lcyBvciBkaXZpZGUgb3BlcmF0b3IuCiAgICAgIGJpbmFyeU11bERpdjogXyA9PiBwYXJzaW1tb25fdW1kX21pbkV4cG9ydHMucmVnZXhwKC9cKnxcL3wlLykKICAgICAgICAgIC5tYXAoc3RyID0+IHN0cikKICAgICAgICAgIC5kZXNjKCInKicgb3IgJy8nIG9yICclJyIpLAogICAgICAvLyBCaW5hcnkgY29tcGFyaXNvbiBvcGVyYXRvci4KICAgICAgYmluYXJ5Q29tcGFyZU9wOiBfID0+IHBhcnNpbW1vbl91bWRfbWluRXhwb3J0cy5yZWdleHAoLz49fDw9fCE9fD58PHw9LykKICAgICAgICAgIC5tYXAoc3RyID0+IHN0cikKICAgICAgICAgIC5kZXNjKCInPj0nIG9yICc8PScgb3IgJyE9JyBvciAnPScgb3IgJz4nIG9yICc8JyIpLAogICAgICAvLyBCaW5hcnkgYm9vbGVhbiBjb21iaW5hdGlvbiBvcGVyYXRvci4KICAgICAgYmluYXJ5Qm9vbGVhbk9wOiBfID0+IHBhcnNpbW1vbl91bWRfbWluRXhwb3J0cy5yZWdleHAoL2FuZHxvcnwmfFx8L2kpCiAgICAgICAgICAubWFwKHN0ciA9PiB7CiAgICAgICAgICBpZiAoc3RyLnRvTG93ZXJDYXNlKCkgPT0gImFuZCIpCiAgICAgICAgICAgICAgcmV0dXJuICImIjsKICAgICAgICAgIGVsc2UgaWYgKHN0ci50b0xvd2VyQ2FzZSgpID09ICJvciIpCiAgICAgICAgICAgICAgcmV0dXJuICJ8IjsKICAgICAgICAgIGVsc2UKICAgICAgIC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BxLm9iamVjdEZpZWxkLCBxLmxhbWJkYUZpZWxkLCBxLnBhcmVuc0ZpZWxkLCBxLmJvb2xGaWVsZCwgcS5udW1iZXJGaWVsZCwgcS5zdHJpbmdGaWVsZCwgcS5kYXRlRmllbGQsIHEuZHVyYXRpb25GaWVsZCwgcS5udWxsRmllbGQsIHEudmFyaWFibGVGaWVsZCksCiAgICAgIGluZGV4RmllbGQ6IHEgPT4gcGFyc2ltbW9uX3VtZF9taW5FeHBvcnRzLnNlcU1hcChxLmF0b21GaWVsZCwgcGFyc2ltbW9uX3VtZF9taW5FeHBvcnRzLmFsdChxLmRvdFBvc3RmaXgsIHEuaW5kZXhQb3N0Zml4LCBxLmZ1bmN0aW9uUG9zdGZpeCkubWFueSgpLCAob2JqLCBwb3N0Zml4ZXMpID0+IHsKICAgICAgICAgIGxldCByZXN1bHQgPSBvYmo7CiAgICAgICAgICBmb3IgKGxldCBwb3N0IG9mIHBvc3RmaXhlcykgewogICAgICAgICAgICAgIHN3aXRjaCAocG9zdC50eXBlKSB7CiAgICAgICAgICAgICAgICAgIGNhc2UgImRvdCI6CiAgICAgICAgICAgICAgICAgICAgICByZXN1bHQgPSBGaWVsZHMuaW5kZXgocmVzdWx0LCBGaWVsZHMubGl0ZXJhbChwb3N0LmZpZWxkKSk7CiAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAiaW5kZXgiOgogICAgICAgICAgICAgICAgICAgICAgcmVzdWx0ID0gRmllbGRzLmluZGV4KHJlc3VsdCwgcG9zdC5maWVsZCk7CiAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAiZnVuY3Rpb24iOgogICAgICAgICAgICAgICAgICAgICAgcmVzdWx0ID0gRmllbGRzLmZ1bmMocmVzdWx0LCBwb3N0LmZpZWxkcyk7CiAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgIC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gICAgICAgICAgICBlc2NhcGVkID0gIWVzY2FwZWQ7CiAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICAvLyBJZiBlc2NhcGVkLCBpZ25vcmUgdGhlIG5leHQgY2hhcmFjdGVyIGZvciBjb21wdXRpbmcgbmVzdGluZywgcmVnYXJkbGVzcyBvZiB3aGF0IGl0IGlzLgogICAgICAgICAgaWYgKGVzY2FwZWQpIHsKICAgICAgICAgICAgICBlc2NhcGVkID0gZmFsc2U7CiAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoY2hhciA9PSBvcGVuKQogICAgICAgICAgICAgIG5lc3RpbmcrKzsKICAgICAgICAgIGVsc2UgaWYgKGNoYXIgPT0gY2xvc2UpCiAgICAgICAgICAgICAgbmVzdGluZy0tOwogICAgICAgICAgLy8gT25seSBvY2N1cnMgaWYgd2UgYXJlIG9uIGEgY2xvc2UgY2hhcmFjdGVyIGFuZCB0cmhlcmUgaXMgbm8gbW9yZSBuZXN0aW5nLgogICAgICAgICAgaWYgKG5lc3RpbmcgPCAwKQogICAgICAgICAgICAgIHJldHVybiB7IHZhbHVlOiBsaW5lLnN1YnN0cmluZyhzdGFydCwgaW5kZXgpLnRyaW0oKSwgZW5kSW5kZXg6IGluZGV4ICsgMSB9OwogICAgICAgICAgZXNjYXBlZCA9IGZhbHNlOwogICAgICB9CiAgICAgIHJldHVybiB1bmRlZmluZWQ7CiAgfQogIC8qKiBGaW5kIHRoZSAnOjonIHNlcGFyYXRvciBpbiBhbiBpbmxpbmUgZmllbGQuICovCiAgZnVuY3Rpb24gZmluZFNlcGFyYXRvcihsaW5lLCBzdGFydCkgewogICAgICBsZXQgc2VwID0gbGluZS5pbmRleE9mKCI6OiIsIHN0YXJ0KTsKICAgICAgaWYgKHNlcCA8IDApCiAgICAgICAgICByZXR1cm4gdW5kZWZpbmVkOwogICAgICByZXR1cm4geyBrZXk6IGxpbmUuc3Vic3RyaW5nK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wogIC8qKiBQYXJzZSBzcGVjaWFsIGNvbXBsZXRlZC9kdWUvZG9uZSB0YXNrIGZpZWxkcyB3aGljaCBhcmUgbWFya2VkIHZpYSBlbW9qaS4gKi8KICBmdW5jdGlvbiBleHRyYWN0U3BlY2lhbFRhc2tGaWVsZHMobGluZSkgewogICAgICBsZXQgcmVzdWx0cyA9IFtdOwogICAgICBmb3IgKGxldCB7IHJlZ2V4LCBrZXkgfSBvZiBFTU9KSV9SRUdFWEVTKSB7CiAgICAgICAgICBjb25zdCBtYXRjaCA9IHJlZ2V4LmV4ZWMobGluZSk7CiAgICAgICAgICBpZiAoIW1hdGNoKQogICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgcmVzdWx0cy5wdXNoKHsKICAgICAgICAgICAgICBrZXksCiAgICAgICAgICAgICAgdmFsdWU6IG1hdGNoWzFdLAogICAgICAgICAgICAgIHN0YXJ0OiBtYXRjaC5pbmRleCwKICAgICAgICAgICAgICBzdGFydFZhbHVlOiBtYXRjaC5pbmRleCArIDEsCiAgICAgICAgICAgICAgZW5kOiBtYXRjaC5pbmRleCArIG1hdGNoWzBdLmxlbmd0aCwKICAgICAgICAgICAgICB3cmFwcGluZzogImVtb2ppLXNob3J0aGFuZCIsCiAgICAgICAgICB9KTsKICAgICAgfQogICAgICByZXR1cm4gcmVzdWx0czsKICB9CgogIC8qKiBBbGwgZXh0cmFjdGVkIG1hcmtkb3duIGZpbGUgbWV0YWRhdGEgb2J0YWluZWQgZnJvbSBhIGZpbGUuICovCiAgY2xhc3MgUGFnZU1ldGFkYXRhIHsKICAgICAgY29uc3RydWN0b3IocGF0aCwgaW5pdCkgewogICAgICAgICAgdGhpcy5wYXRoID0gcGF0aDsKICAgICAgICAgIHRoaXMuZmllbGRzID0gbmV3IE1hcCgpOwogICAgICAgICAgdGhpcy5mcm9udG1hdHRlciA9IHt9OwogICAgICAgICAgdGhpcy50YWdzID0gbmV3IFNldCgpOwogICAgICAgICAgdGhpcy5hbGlhc2VzID0gbmV3IFNldCgpOwogICAgICAgICAgdGhpcy5saW5rcyA9IFtdOwogICAgICAgICAgT2JqZWN0LmFzc2lnbih0aGlzLCBpbml0KTsKICAgICAgICAgIHRoaXMubGlzdHMgPSAodGhpcy5saXN0cyB8fCBbXSkubWFwKGwgPT4gbmV3IExpc3RJdGVtKGwpKTsKICAgICAgfQogICAgICAvKiogQ2Fub25pY2FsaXplIHJhdyBsaW5rcyBhbmQgb3RoZXIgZGF0YSBpbiBwYXJ0aWFsIGRhdGEgd2l0aCBub3JtYWxpemVycywgcmV0dXJuaW5nIGEgY29tcGxldGVkIG9iamVjdC4gKi8KICAgICAgc3RhdGljIGNhbm9uaWNhbGl6ZShkYXRhLCBsaW5rTm9ybWFsaXplcikgewogICAgICAgICAgLy8gTXV0YXRlIHRoZSBkYXRhIGZvciBub3csIHdoaWNoIGlzIHByb2JhYmx5IGEgYmFkIGlkZWEgYnV0Li4uIGFsbCB3ZWxsLgogICAgICAgICAgaWYgKGRhdGEuZnJvbnRtYXR0ZXIpIHsKICAgICAgICAgICAgICBkYXRhLmZyb250bWF0dGVyID0gVmFsdWVzLm1hcExlYXZlcyhkYXRhLmZyb250bWF0dGVyLCB0ID0+IFZhbHVlcy5pc0xpbmsodCkgPyBsaW5rTm9ybWFsaXplcih0KSA6IHQpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGRhdGEuZmllbGRzKSB7CiAgICAgICAgICAgICAgZm9yIChsZXQgW2tleSwgdmFsdWVdIG9mIGRhdGEuZmllbGRzLmVudHJpZXMoKSkgewogICAgICAgICAgICAgICAgICBkYXRhLmZpZWxkcy5zZXQoa2V5LCBWYWx1ZXMubWFwTGVhdmVzKHZhbHVlLCB0ID0+IChWYWx1ZXMuaXNMaW5rKHQpID8gbGlua05vcm1hbGl6ZXIodCkgOiB0KSkpOwogICAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChkYXRhLmxpc3RzKSB7CiAgICAgICAgICAgICAgZm9yICh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0IGRpc3RpbmN0TGlua3MgPSBuZXcgU2V0KHRoaXMubGlua3MpOwogICAgICAgICAgcmV0dXJuIEFycmF5LmZyb20oZGlzdGluY3RMaW5rcyk7CiAgICAgIH0KICAgICAgLyoqIE1hcCB0aGlzIG1ldGFkYXRhIHRvIGEgZnVsbCBvYmplY3Q7IHVzZXMgdGhlIGluZGV4IGZvciBhZGRpdGlvbmFsIGRhdGEgbG9va3Vwcy4gICovCiAgICAgIHNlcmlhbGl6ZShpbmRleCwgY2FjaGUpIHsKICAgICAgICAgIC8vIENvbnZlcnQgbGlzdCBpdGVtcyB2aWEgdGhlIGNhbm9uaWNhbGl6YXRpb24gY2FjaGUuCiAgICAgICAgICBsZXQgcmVhbENhY2hlID0gY2FjaGUgIT09IG51bGwgJiYgY2FjaGUgIT09IHZvaWQgMCA/IGNhY2hlIDogbmV3IExpc3RTZXJpYWxpemF0aW9uQ2FjaGUodGhpcy5saXN0cyk7CiAgICAgICAgICBsZXQgcmVzdWx0ID0gewogICAgICAgICAgICAgIGZpbGU6IHsKICAgICAgICAgICAgICAgICAgcGF0aDogdGhpcy5wYXRoLAogICAgICAgICAgICAgICAgICBmb2xkZXI6IHRoaXMuZm9sZGVyKCksCiAgICAgICAgICAgICAgICAgIG5hbWU6IHRoaXMubmFtZSgpLAogICAgICAgICAgICAgICAgICBsaW5rOiBMaW5rLmZpbGUodGhpcy5wYXRoKSwKICAgICAgICAgICAgICAgICAgb3V0bGlua3M6IHRoaXMuZmlsZUxpbmtzKCksCiAgICAgICAgICAgICAgICAgIGlubGlua3M6IEFycmF5LmZyb20oaW5kZXgubGlua3MuZ2V0SW52ZXJzZSh0aGlzLnBhdGgpKS5tYXAobCA9PiBMaW5rLmZpbGUobCkpLAogICAgICAgICAgICAgICAgICBldGFnczogQXJyYXkuZnJvbSh0aGlzLnRhZ3MpLAogICAgICAgICAgICAgICAgICB0YWdzOiBBcnJheS5mcm9tKHRoaXMuZnVsbFRhZ3MoKSksCiAgICAgICAgICAgICAgICAgIGFsaWFzZXM6IEFycmF5LmZyb20odGhpcy5hbGlhc2VzKSwKICAgICAgICAgICAgICAgICAgbGlzdHM6IHRoaXMubGlzdHMubWFwKGwgPT4gcmVhbENhY2hlLmdldChsLmxpbmUpKSwKICAgICAgICAgICAgICAgICAgdGFza3M6IHRoaXMubGlzdHMuZmlsdGVyKGwgPT4gISFsLnRhc2spLm1hcChsID0+IHJlYWxDYWNoZS5nZXQobC5saW5lKSksCiAgICAgICAgICAgICAgICAgIGN0aW1lOiB0aGlzLmN0aW1lLAogICAgICAgICAgICAgICAgICBjZGF5OiBzdHJpcFRpbWUodGhpcy5jdGltZSksCiAgICAgICAgICAgICAgICAgIG10aW1lOiB0aGlzLm10aW1lLAogICAgICAgICAgICAgICAgICBtZGF5OiBzdHJpcFRpbWUodGhpcy5tdGltZSksCiAgICAgICAgICAgICAgICAgIHNpemU6IHRoaXMuc2l6ZSwKICAgICAgICAgICAgICAgICAgc3RhcnJlZDogaW5kZXguc3RhcnJlZC5zdGFycmVkKHRoaXMucGF0aCksCiAgICAgICAgICAgICAgICAgIGZyb250bWF0dGVyOiBWYWx1ZXMuZGVlcENvcHkodGhpcy5mcm9udG1hdHRlciksCiAgICAgICAgICAgICAgICAgIGV4dDogdGhpcy5leHRlbnNpb24oKSwKICAgICAgICAgICAgICB9LAogICAgICAgICAgfTsKICAgICAgICAgIC8vIEFkZCB0aGUgY3VycmVudCBkYXkgaWYgcHJlc2VudC4KICAgICAgICAgIGlmICh0aGlzLmRheSkKICAgICAgICAgICAgICByZXN1bHQuZmlsZS5kYXkgPSB0aGlzLmRheTsKICAgICAgICAgIC8vIFRoZW4gYXBwZW5kIHRoZSBjb21wdXRlZCBmaWVsZHMuCiAgICAgICAgICBmb3IgKGxldCBba2V5LCB2YWx1ZV0gb2YgdGhpcy5maWVsZHMuZW50cmllcygpKSB7CiAgICAgICAgICAgICAgaWYgKGtleSBpbiByZXN1bHQpCiAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOyAvLyBEb24ndCBhbGxvdyBmaWVsZHMgdG8gb3ZlcnJpZGUgZXhpc3Rpbmcga2V5cy4KICAgICAgICAgICAgICByZXN1bHRba2V5XSA9IHZhbHVlOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgfQogIH0KICAvKiogQSBsaXN0IGl0ZW0gaW5zaWRlIG9mIGEgbGlzdC4gKi8KICBjbGFzcyBMaXN0SXRlbSB7CiAgICAgIGNvbnN0cnVjdG9yKGluaXQpIHsKICAgICAgICAgIE9iamVjdC5hc3NpZ24odGhpcywgaW5pdCk7CiAgICAgICAgICB0aGlzLmZpZWxkcyA9IHRoaXMuZmllbGRzIHx8IG5ldyBNYXAoKTsKICAgICAgICAgIHRoaXMudGFncyA9IHRoaXMudGFncyB8fCBuZXcgU2V0KCk7CiAgICAgICAgICB0aGlzLmNoaWxkcmVuID0gdGhpcy5jaGlsZHJlbiB8fCBbXTsKICAgICAgICAgIHRoaXMubGlua3MgPSB0aGlzLmxpbmtzIHx8IFtdOwogICAgICB9CiAgICAgIGlkKCkgewogICAgICAgICAgcmV0dXJuIGAke3RoaXMuZmlsZSgpLnBhdGh9LSR7dGhpcy5saW5lfWA7CiAgICAgIH0KICAgICAgZmlsZSgpIHsKICAgICAgICAgIHJldHVybiB0aGlzLmxpbmsudG9GaWxlKCk7CiAgICAgIH0KICAgICAgbWFya2Rvd24oKSB7CiAgICAgICAgICBpZiAodGhpcy50YXNrKQogICAgICAgICAgICAgIHJldHVybiBgJHt0aGlzLnN5bWJvbH0gWyR7dGhpcy50YXNrLmNvbXBsZXRlZCA/ICJ4IiA6ICIgIn1dICR7dGhpcy50ZXh0fWA7CiAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgcmV0dXJuIGAke3RoaXMuc3ltYm9sfSAke3RoaXMudGV4dH1gOwogICAgICB9CiAgICAgIGNyZWF0ZWQoKSB7CiAgICAgICAgICB2YXIgX2EsIF9iLCBfYzsKICAgICAgICAgIHJldH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gdm9pZCAwIDogX2RbMF07CiAgICAgIH0KICAgICAgc3RhcnQoKSB7CiAgICAgICAgICB2YXIgX2E7CiAgICAgICAgICByZXR1cm4gKF9hID0gdGhpcy5maWVsZHMuZ2V0KCJzdGFydCIpKSA9PT0gbnVsbCB8fCBfYSA9PT0gdm9pZCAwID8gdm9pZCAwIDogX2FbMF07CiAgICAgIH0KICAgICAgc2NoZWR1bGVkKCkgewogICAgICAgICAgdmFyIF9hOwogICAgICAgICAgcmV0dXJuIChfYSA9IHRoaXMuZmllbGRzLmdldCgic2NoZWR1bGVkIikpID09PSBudWxsIHx8IF9hID09PSB2b2lkIDAgPyB2b2lkIDAgOiBfYVswXTsKICAgICAgfQogICAgICAvKiogQ3JlYXRlIGFuIEFQSS1mcmllbmRseSBjb3B5IG9mIHRoaXMgbGlzdCBpdGVtLiBEZS1kdXBsaWNhdGlvbiBpcyBkb25lIHZpYSB0aGUgcHJvdmlkZWQgY2FjaGUuICovCiAgICAgIHNlcmlhbGl6ZShjYWNoZSkgewogICAgICAgICAgLy8gTWFwIGNoaWxkcmVuIHRvIHRoZWlyIHNlcmlhbGl6ZWQvZGUtZHVwbGljYXRlZCBlcXVpdmFsZW50cyByaWdodCBhd2F5LgogICAgICAgICAgbGV0IGNoaWxkcmVuID0gdGhpcy5jaGlsZHJlbi5tYXAobCA9PiBjYWNoZS5nZXQobCkpLmZpbHRlcigobCkgPT4gbCAhPT0gdW5kZWZpbmVkKTsKICAgICAgICAgIGxldCByZXN1bHQgPSB7CiAgICAgICAgICAgICAgc3ltYm9sOiB0aGlzLnN5bWJvbCwKICAgICAgICAgICAgICBsaW5rOiB0aGlzLmxpbmssCiAgICAgICAgICAgICAgc2VjdGlvbjogdGhpcy5zZWN0aW9uLAogICAgICAgICAgICAgIHRleHQ6IHRoaXMudGV4dCwKICAgICAgICAgICAgICB0YWdzOiBBcnJheS5mcm9tKHRoaXMudGFncyksCiAgICAgICAgICAgICAgbGluZTogdGhpcy5saW5lLAogICAgICAgICAgICAgIGxpbmVDb3VudDogdGhpcy5saW5lQ291bnQsCiAgICAgICAgICAgICAgbGlzdDogdGhpcy5saXN0LAogICAgICAgICAgICAgIG91dGxpbmtzOiBBcnJheS5mcm9tKHRoaXMubGlua3MpLAogICAgICAgICAgICAgIHBhdGg6IHRoaXMubGluay5wYXRoLAogICAgICAgICAgICAgIGNoaWxkcmVuOiBjaGlsZHJlbiwKICAgICAgICAgICAgICB0YXNrOiAhIXRoaXMudGFzaywKICAgICAgICAgICAgICBhbm5vdGF0ZWQ6IHRoaXMuZmllbGRzLnNpemUgPiAwLAogICAgICAgICAgICAgIHBvc2l0aW9uOiBWYWx1ZXMuZGVlcENvcHkodGhpcy5wb3NpdGlvbiksCiAgICAgICAgICAgICAgc3VidGFza3M6IGNoaWxkcmVuLAogICAgICAgICAgICAgIHJlYWw6ICEhdGhpcy50YXNrLAogICAgICAgICAgICAgIGhlYWRlcjogdGhpcy5zZWN0aW9uLCAvLyBAZGVwcmVjYXRlZCwgdXNlICdpdGVtLnNlY3Rpb24nIGluc3RlYWQuCiAgICAgICAgICB9OwogICAgICAgICAgaWYgKHRoaXMucGFyZW50IHx8IHRoaXMucGFyZW50ID09PSAwKQogICAgICAgICAgICAgIHJlc3VsdC5wYXJlbnQgPSB0aGlzLnBhcmVudDsKICAgICAgICAgIGlmICh0aGlzLmJsb2NrSWQpCiAgICAgICAgICAgICAgcmVzdWx0LmJsb2NrSWQgPSB0aGlzLmJsb2NrSWQ7CiAgICAgICAgICBhZGRGaWVsZHModGhpcy5maWVsZHMsIHJlc3VsdCk7CiAgICAgICAgICBpZiAodGhpcy50YXNrKSB7CiAgICAgICAgICAgICAgcmVzdWx0LnN0YXR1cyA9IHRoaXMudGFzay5zdGF0dXM7CiAgICAgICAgICAgICAgcmVzdWx0LmNoZWNrZWQgPSB0aGlzLnRhc2suY2hlY2tlZDsKICAgICAgICAgICAgICByZXN1bHQuY29tcGxldGVkID0gdGhpcy50YXNrLmNvbXBsZXRlZDsKICA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ICAgIGZvciAobGV0IFtrZXksIHZhbHVlXSBvZiBPYmplY3QuZW50cmllcyhmcm9udEZpZWxkcykpIHsKICAgICAgICAgICAgICBpZiAoa2V5ID09ICJwb3NpdGlvbiIpCiAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgIGFkZElubGluZUZpZWxkKGtleSwgdmFsdWUsIGZpZWxkcyk7CiAgICAgICAgICB9CiAgICAgIH0KICAgICAgLy8gQWRkIGZyb250bWF0dGVyIGxpbmtzIHRvIGxpbmtzLgogICAgICBpZiAobWV0YWRhdGEuZnJvbnRtYXR0ZXJMaW5rcykgewogICAgICAgICAgZm9yIChsZXQgcmF3TGluayBvZiBtZXRhZGF0YS5mcm9udG1hdHRlckxpbmtzIHx8IFtdKSB7CiAgICAgICAgICAgICAgY29uc3QgbGluayA9IExpbmsuaW5mZXIocmF3TGluay5saW5rLCBmYWxzZSwgcmF3TGluay5kaXNwbGF5VGV4dCk7CiAgICAgICAgICAgICAgbGlua3MucHVzaChsaW5rKTsKICAgICAgICAgIH0KICAgICAgfQogICAgICAvLyBMaW5rcyBpbiBtZXRhZGF0YS4KICAgICAgY29uc3QgbGlua3NCeUxpbmUgPSB7fTsKICAgICAgZm9yIChsZXQgcmF3TGluayBvZiBtZXRhZGF0YS5saW5rcyB8fCBbXSkgewogICAgICAgICAgY29uc3QgbGluayA9IExpbmsuaW5mZXIocmF3TGluay5saW5rLCBmYWxzZSwgcmF3TGluay5kaXNwbGF5VGV4dCk7CiAgICAgICAgICBjb25zdCBsaW5lID0gcmF3TGluay5wb3NpdGlvbi5zdGFydC5saW5lOwogICAgICAgICAgbGlua3MucHVzaChsaW5rKTsKICAgICAgICAgIGlmICghKGxpbmUgaW4gbGlua3NCeUxpbmUpKQogICAgICAgICAgICAgIGxpbmtzQnlMaW5lW2xpbmVdID0gW2xpbmtdOwogICAgICAgICAgZWxzZQogICAgICAgICAgICAgIGxpbmtzQnlMaW5lW2xpbmVdLnB1c2gobGluayk7C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N0YXJ0LmxpbmU7IGxpbmVubyA8PSBzZWN0aW9uLnBvc2l0aW9uLmVuZC5saW5lOyBsaW5lbm8rKykgewogICAgICAgICAgICAgIGxldCBsaW5lID0gY29udGVudHNbbGluZW5vXTsKICAgICAgICAgICAgICBpZiAobGluZSA9PSB1bmRlZmluZWQgfHwgbGluZSA9PSBudWxsKQogICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICBpZiAobGlzdExpbmVzVG9Ta2lwLmhhcyhsaW5lbm8pKQogICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAvLyBGYXN0IGJhaWwtb3V0IGZvciBsaW5lcyB0aGF0IGFyZSB0b28gbG9uZyBvciBkbyBub3QgY29udGFpbiAnOjonLgogICAgICAgICAgICAgIGlmIChsaW5lLmxlbmd0aCA+IDMyNzY4IHx8ICFsaW5lLmluY2x1ZGVzKCI6OiIpKQogICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICBsaW5lID0gbGluZS50cmltKCk7CiAgICAgICAgICAgICAgbGV0IGlubGluZUZpZWxkcyA9IGV4dHJhY3RJbmxpbmVGaWVsZHMobGluZSk7CiAgICAgICAgICAgICAgaWYgKGlubGluZUZpZWxkcy5sZW5ndGggPiAwKSB7CiAgICAgICAgICAgICAgICAgIGZvciAobGV0IGlmaWVsZCBvZiBpbmxpbmVGaWVsZHMpCiAgICAgICAgICAgICAgICAgICAgICBhZGRSYXdJbmxpbmVGaWVsZChpZmllbGQsIGZpZWxkcyk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgICBsZXQgZnVsbExpbmUgPSBleHRyYWN0RnVsbExpbmVGaWVsZChsaW5lKTsKICAgICAgICAgICAgICAgICAgaWYgKGZ1bGxMaW5lKQogICAgICAgICAgICAgICAgICAgICAgYWRkUmF3SW5saW5lRmllbGQoZnVsbExpbmUsIGZpZWxkcyk7CiAgICAgICAgICAgICAgfQ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GEuaGVhZGluZ3MgfHwgW10pOwogICAgICAgICAgbGV0IHNlY3Rpb25MaW5rID0gc2VjdGlvbk5hbWUgPT09IHVuZGVmaW5lZCA/IExpbmsuZmlsZShwYXRoKSA6IExpbmsuaGVhZGVyKHBhdGgsIHNlY3Rpb25OYW1lKTsKICAgICAgICAgIGxldCBjbG9zZXN0TGluayA9IHJhd0VsZW1lbnQuaWQgPT09IHVuZGVmaW5lZCA/IHNlY3Rpb25MaW5rIDogTGluay5ibG9jayhwYXRoLCByYXdFbGVtZW50LmlkKTsKICAgICAgICAgIC8vIEdhdGhlciBhbnkgbGlua3MgdGhhdCBvY2N1ciBvbiB0aGUgc2FtZSBsaW5lcyBhcyB0aGUgdGFzay4KICAgICAgICAgIGNvbnN0IGxpbmtzID0gW107CiAgICAgICAgICBmb3IgKGxldCBsaW5lID0gcmF3RWxlbWVudC5wb3NpdGlvbi5zdGFydC5saW5lOyBsaW5lIDw9IHJhd0VsZW1lbnQucG9zaXRpb24uZW5kLmxpbmU7IGxpbmUrKykgewogICAgICAgICAgICAgIGlmIChsaW5rc0J5TGluZVtsaW5lXSkKICAgICAgICAgICAgICAgICAgbGlua3MucHVzaCguLi5saW5rc0J5TGluZVtsaW5lXSk7CiAgICAgICAgICB9CiAgICAgICAgICAvLyBDb25zdHJ1Y3QgdW5pdmVyc2FsIGluZm9ybWF0aW9uIGFib3V0IHRoaXMgZWxlbWVudCAoYmVmb3JlIHRhc2tzKS4KICAgICAgICAgIGxldCBpdGVtID0gbmV3IExpc3RJdGVtKHsKICAgICAgICAgICAgICBzeW1ib2w6IHJhd01hdGNoWzFdLAogICAgICAgICAgICAgIGxpbms6IGNsb3Nlc3RMaW5rLAogICAgICAgICAgICAgIGxpbmtzOiBsaW5rcywKICAgICAgICAgICAgICBzZWN0aW9uOiBzZWN0aW9uTGluaywKICAgICAgICAgICAgICB0ZXh0OiB0ZXh0V2l0aE5ld2xpbmUsCiAgICAgICAgICAgICAgdGFnczogZXh0cmFjdFRhZ3MkMSh0ZXh0Tm9OZXdsaW5lKSwKICAgICAgICAgICAgICBsaW5lOiByYXdFbGVtZW50LnBvc2l0aW9uLnN0YXJ0LmxpbmUsCiAgICAgICAgICAgICAgbGluZUNvdW50OiByYXdFbGVtZW50LnBvc2l0aW9uLmVuZC5saW5lIC0gcmF3RWxlbWVudC5wb3NpdGlvbi5zdGFydC5saW5lICsgMSwKICAgICAgICAgICAgICBsaXN0OiBjb250YWluaW5nTGlzdElkID09IC0xID8gLTEgOiAobWV0YWRhdGEuc2VjdGlvbnMgfHwgW10pW2NvbnRhaW5pbmdMaXN0SWRdLnBvc2l0aW9uLnN0YXJ0LmxpbmUsCiAgICAgICAgICAgICAgcG9zaXRpb246IHJhd0VsZW1lbnQucG9zaXRpb24sCiAgICAgICAgICAgICAgY2hpbGRyZW46IFtdLAogICAgICAgICAgICAgIGJsb2NrSWQ6IHJhd0VsZW1lbnQuaWQsCiAgICAgICAgICB9KTsKICAgICAgICAgIGlmIChyYXdFbGVtZW50LnBhcmVudCA+PSAwICYmIHJhd0VsZW1lbnQucGFyZW50ICE9IGl0ZW0ubGluZSkKICAgICAgICAgICAgICBpdGVtLnBhcmVudCA9IHJhd0VsZW1lbnQucGFyZW50OwogICAgICAgICAgLy8gU2V0IHVwIHRoZSBiYXNpYyB0YXNrIGluZm9ybWF0aW9uIGZvciBub3csIHRob3VnaCB3ZSBoYXZlIHRvIHJlY29tcHV0ZSBgZnVsbHlDb21wdXRlZGAgbGF0ZXIuCiAgICAgICAgICBpZiAocmF3RWxlbWVudC50YXNrKSB7CiAgICAgICAgICAgICAgaXRlbS50YXNrID0gewogICAgICAgICAgICAgICAgICBzdGF0dXM6IHJhd0VsZW1lbnQudGFzaywKICAgICAgICAgICAgICAgICAgY2hlY2tlZDogcmF3RWxlbWVudC50YXNrICE9ICIiICYmIHJhd0VsZW1lbnQudGFzayAhPSAiICIsCiAgICAgICAgICAgICAgICAgIGNvbXBsZXRlZDogcmF3RWxlbWVudC50YXNrID09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kKICAgICAgICAgICAgICAgICAgcmV0dXJuIGRhdGU7CiAgICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIGV4dHJhY3REYXRlKGdldEZpbGVUaXRsZShmaWxlKSk7CiAgfQogIC8qKiBSZWN1cnNpdmVseSBjb252ZXJ0IGZyb250bWF0dGVyIGludG8gZmllbGRzLiBXZSBoYXZlIHRvIGRhbmNlIGFyb3VuZCBZQU1MIHN0cnVjdHVyZS4gKi8KICBmdW5jdGlvbiBwYXJzZUZyb250bWF0dGVyKHZhbHVlKSB7CiAgICAgIGlmICh2YWx1ZSA9PSBudWxsKSB7CiAgICAgICAgICByZXR1cm4gbnVsbDsKICAgICAgfQogICAgICBlbHNlIGlmICh0eXBlb2YgdmFsdWUgPT09ICJvYmplY3QiKSB7CiAgICAgICAgICBpZiAoQXJyYXkuaXNBcnJheSh2YWx1ZSkpIHsKICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0gW107CiAgICAgICAgICAgICAgZm9yIChsZXQgY2hpbGQgb2YgdmFsdWUpIHsKICAgICAgICAgICAgICAgICAgcmVzdWx0LnB1c2gocGFyc2VGcm9udG1hdHRlcihjaGlsZCkpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgICAgICAgfQogICAgICAgICAgZWxzZSBpZiAodmFsdWUgaW5zdGFuY2VvZiBEYXRlKSB7CiAgICAgICAgICAgICAgbGV0IGRhdGVQYXJzZSA9IERhdGVUaW1lLmZyb21KU0RhdGUodmFsdWUpOwogICAgICAgICAgICAgIHJldHVybiBkYXRlUGFyc2U7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICBsZXQgb2JqZWN0ID0gdmFsdWU7CiAgICAgICAgICAgICAgbGV0IHJlc3VsdCA9IHt9OwogICAgICAgICAgICAgIGZvciAobGV0IGtleSBpbiBvYmplY3QpIHsKICAgICAgICAgICAgICAgICAgcmVzdWx0W2tleV0gPSBwYXJzZUZyb250bWF0dGVyKG9iamVjdFtrZXldKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICAgIH0KICAgICAgfQogICAgICBlbHNlIGlmICh0eXBlb2YgdmFsdWUgPT09ICJudW1iZXIiKSB7CiAgICAgICAgICByZXR1cm4gdmFsdWU7CiAgICAgIH0KICAgICAgZWxzZSBpZiAodHlwZW9mIHZhbHVlID09PSAiYm9vbGVhbiIpIHsKICAgICAgICAgIHJldHVybiB2YWx1ZTsKICAgICAgfQogICAgICBlbHNlIGlmICh0eXBlb2YgdmFsdWUgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICBsZXQgZGF0ZVBhcnNlID0gRVhQUkVTU0lPTi5kYXRlLnBhcnNlKHZhbHVlKTsKICAgICAgICAgIGlmIChkYXRlUGFyc2Uuc3RhdHVzKQogICAgICAgICAgICAgIHJldHVybiBkYXRlUGFyc2UudmFsdWU7CiAgICAgICAgICBsZXQgZHVyYXRpb25QYXJzZSA9IEVYUFJFU1NJT04uZHVyYXRpb24ucGFyc2UodmFsdWUpOwogICAgICAgICAgaWYgKGR1cmF0aW9uUGFyc2Uuc3RhdHVzKQogICAgICAgICAgICAgIHJldHVybiBkdXJhdGlvblBhcnNlLnZhbHVlOwogICAgICAgICAgbGV0IGxpbmtQYXJzZSA9IEVYUFJFU1NJT04uZW1iZWRMaW5rLnBhcnNlKHZhbHVlKTsKICAgICAgICAgIGlmIChsaW5rUGFyc2Uuc3RhdHVzKQogICAgICAgICAgICAgIHJldHVybiBsaW5rUGFyc2UudmFsdWU7CiAgICAgICAgICByZXR1cm4gdmFsdWU7CiAgICAgIH0KICAgICAgLy8gQmFja3VwIGlmIHdlIGRvbid0IHVuZGVyc3RhbmQgdGhlIHR5cGUuCiAgICAgIHJldHVybiBudWxsOwogIH0KICAvKiogQWRkIGEgcGFyc2VkIGlubGluZSBmaWVsZCB0byB0aGUgb3V0cHV0IG1hcC4gKi8KICBmdW5jdGlv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ZTsKICAgICAgICAgIGlmICghbm9ybWFsaXplZC5oYXMobm9ybUtleSkpCiAgICAgICAgICAgICAgbm9ybWFsaXplZC5zZXQobm9ybUtleSwgdmFsdWVzKTsKICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICBub3JtYWxpemVkLnNldChub3JtS2V5LCBub3JtYWxpemVkLmdldChub3JtS2V5KS5jb25jYXQodmFsdWVzKSk7CiAgICAgIH0KICAgICAgLy8gQ29tYmluZSBub3JtYWxpemVkICsgbm9ybWFsIGtleXMuCiAgICAgIGxldCBpbnRlcmltID0gbmV3IE1hcCgpOwogICAgICBtZXJnZUZpZWxkR3JvdXBzKGludGVyaW0sIGZpZWxkcyk7CiAgICAgIG1lcmdlRmllbGRHcm91cHMoaW50ZXJpbSwgbm9ybWFsaXplZCk7CiAgICAgIC8vIEFuZCB0aGVuIGZsYXR0ZW4gdGhlbS4KICAgICAgbGV0IHJlc3VsdCA9IG5ldyBNYXAoKTsKICAgICAgZm9yIChsZXQgW2tleSwgdmFsdWVdIG9mIGludGVyaW0uZW50cmllcygpKSB7CiAgICAgICAgICBpZiAodmFsdWUubGVuZ3RoID09IDEpCiAgICAgICAgICAgICAgcmVzdWx0LnNldChrZXksIHZhbHVlWzBdKTsKICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICByZXN1bHQuc2V0KGtleSwgdmFsdWUpOwogICAgICB9CiAgICAgIHJldHVybiByZXN1bHQ7CiAgfQogIC8qKiBDb3B5IGFsbCBmaWVsZHMgb2YgJ3NvdXJjZScgaW50byAndGFyZ2V0Jy4gKi8KICBmdW5jdGlvbiBtZXJnZUZpZWxkR3JvdXBzKHRhcmdldCwgc291cmNlKSB7CiAgICAgIGZvciAobGV0IGtleSBvZiBzb3VyY2Uua2V5cygpKSB7CiAgICAgICAgICBpZiAoIXRhcmdldC5oYXMoa2V5KSkKICAgICAgICAgICAgICB0YXJnZXQuc2V0KGtleSwgc291cmNlLmdldChrZXkpKTsKICAgICAgICAgIGVsc2UKICAgIC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YXJyaWVyLiAqLwogIHZhciBUcmFuc2ZlcmFibGU7CiAgKGZ1bmN0aW9uIChUcmFuc2ZlcmFibGUpIHsKICAgICAgLyoqIENvbnZlcnQgYSBsaXRlcmFsIHZhbHVlIHRvIGEgc2VyaWFsaXplci1mcmllbmRseSB0cmFuc2ZlcmFibGUgdmFsdWUuICovCiAgICAgIGZ1bmN0aW9uIHRyYW5zZmVyYWJsZSh2YWx1ZSkgewogICAgICAgICAgLy8gSGFuZGxlIHNpbXBsZSB1bml2ZXJzYWwgdHlwZXMgZmlyc3QuCiAgICAgICAgICBpZiAodmFsdWUgaW5zdGFuY2VvZiBNYXApIHsKICAgICAgICAgICAgICBsZXQgY29waWVkID0gbmV3IE1hcCgpOwogICAgICAgICAgICAgIGZvciAobGV0IFtrZXksIHZhbF0gb2YgdmFsdWUuZW50cmllcygpKQogICAgICAgICAgICAgICAgICBjb3BpZWQuc2V0KHRyYW5zZmVyYWJsZShrZXkpLCB0cmFuc2ZlcmFibGUodmFsKSk7CiAgICAgICAgICAgICAgcmV0dXJuIGNvcGllZDsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgaWYgKHZhbHVlIGluc3RhbmNlb2YgU2V0KSB7CiAgICAgICAgICAgICAgbGV0IGNvcGllZCA9IG5ldyBTZXQoKTsKICAgICAgICAgICAgICBmb3IgKGxldCB2YWwgb2YgdmFsdWUpCiAgICAgICAgICAgICAgICAgIGNvcGllZC5hZGQodHJhbnNmZXJhYmxlKHZhbCkpOwogICAgICAgICAgICAgIHJldHVybiBjb3BpZWQ7CiAgICAgICAgICB9CiAgICAgICAgICBsZXQgd3JhcHBlZCA9IFZhbHVlcy53cmFwVmFsdWUodmFsdWUpOwogICAgICAgICAgaWYgKHdyYXBwZWQgPT09IHVuZGVmaW5lZCkKICAgICAgICAgICAgICB0aHJvdyBFcnJvcigiVW5yZWNvZ25pemVkIHRyYW5zZmVyYWJsZSB2YWx1ZTogIiArIHZhbHVlKTsKICAgICAgICAgIHN3aXRjaCAod3JhcHBlZC50eXBlKSB7CiAgICAgICAgICAgICAgY2FzZSAibnVsbCI6CiAgICAgICAgICAgICAgY2FzZSAibnVtYmVyIjoKICAgICAgICAgICAgICBjYXNlICJzdHJpbmciOgogICAgICAgICAgICAgIGNhc2UgImJvb2xlYW4iOgogICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcHBlZC52YWx1ZTsKICAgICAgICAgICAgICBjYXNlICJkYXRlIjoKICAgICAgICAgICAgICAgICAgcmV0dXJuIHsKICAgICAgICAgICAgICAgICAgICAgICJfX190cmFuc2Zlci10eXBlIjogImRhdGUiLAogICAgICAgICAgICAgICAgICAgICAgdmFsdWU6IHRyYW5zZmVyYWJsZSh3cmFwcGVkLnZhbHVlLnRvT2JqZWN0KCkpLAogICAgICAgICAgICAgICAgICAgICAgb3B0aW9uczogewogICAgICAgICAgICAgICAgICAgICAgICAgIHpvbmU6IHdyYXBwZWQudmFsdWUuem9uZS5lcXVhbHMoU3lzdGVtWm9uZS5pbnN0YW5jZSkgPyB1bmRlZmluZWQgOiB3cmFwcGVkLnZhbHVlLnpvbmVOYW1lLAogICAgICAgICAgICAgICAgICAgICAgfSwKICAgICAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgICBjYXNlICJkdXJhdGlvbiI6CiAgICAgICAgICAgICAgICAgIHJldHVybiB7ICJfX190cmFuc2Zlci10eXBlIjogImR1cmF0aW9uIiwgdmFsdWU6IHRyYW5zZmVyYWJsZSh3cmFwcGVkLnZhbHVlLnRvT2JqZWN0KCkpIH07CiAgICAgICAgICAgICAgY2FzZSAiYXJyYXkiOgogICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcHBlZC52YWx1ZS5tYXAodiA9PiB0cmFuc2ZlcmFibGUodikpOwogICAgICAgICAgICAgIGNhc2UgImxpbmsiOgogICAgICAgICAgICAgICAgICByZXR1cm4geyAiX19fdHJhbnNmZXItdHlwZSI6ICJsaW5rIiwgdmFsdWU6IHRyYW5zZmVyYWJsZSh3cmFwcGVkLnZhbHVlLnRvT2JqZWN0KCkpIH07CiAgICAgICAgICAgICAgY2FzZSAib2JqZWN0IjoKICAgICAgICAgICAgICAgICAgbGV0IHJlc3VsdCA9IHt9OwogICAgICAgICAgICAgICAgICBmb3IgKGxldCBba2V5LCB2YWx1ZV0gb2YgT2JqZWN0LmVudHJpZXMod3JhcHBlZC52YWx1ZSkpCiAgICAgICAgICAgICAgICAgICAgICByZXN1bHRba2V5XSA9IHRyYW5zZmVyYWJsZSh2YWx1ZSk7CiAgICAgICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICB9CiAgICAgIH0KICAgICAgVHJhbnNmZXJhYmxlLnRyYW5zZmVyYWJsZSA9IHRyYW5zZmVyYWJsZTsKICAgICAgLyoqIENvbnZlcnQgYSB0cmFuc2ZlcmFibGUgdmFsdWUgYmFjayB0byBhIGxpdGVyYWwgdmFsdWUgd2UgY2FuIHdvcmsgd2l0aC4gKi8KICAgICAgZnVuY3Rpb24gdmFsdWUodHJhbnNmZXJhYmxlKSB7CiAgICAgICAgICBpZiAodHJhbnNmZXJhYmxlID09PSBudWxsKSB7CiAgICAgICAgICAgICAgcmV0dXJuIG51bGw7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIGlmICh0cmFuc2ZlcmFibGUgPT09IHVuZGVmaW5lZCkgewogICAgICAgICAgICAgIHJldHVybiB1bmRlZmluZWQ7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIGlmICh0cmFuc2ZlcmFibGUgaW5zdGFuY2VvZiBNYXApIHsKICAgICAgICAgICAgICBsZXQgcmVhbCA9IG5ldyBNYXAoKTsKICAgICAgICAgICAgICBmb3IgKGxldCBba2V5LCB2YWxdIG9mIHRyYW5zZmVyYWJsZS5lbnRyaWVzKCkpCiAgICAgICAgICAgICAgICAgIHJlYWwuc2V0KHZhbHVlKGtleSksIHZhbHVlKHZhbCkpOwogICAgICAgICAgICAgIHJldHVybiByZWFsOwogICAgICAgICAgfQogICAgICAgICAgZWxzZSBpZiAodHJhbnNmZXJhYmxlIGluc3RhbmNlb2YgU2V0KSB7CiAgICAgICAgICAgICAgbGV0IHJlYWwgPSBuZXcgU2V0KCk7CiAgICAgICAgICAgICAgZm9yIChsZXQgdmFsIG9mIHRyYW5zZmVyYWJsZSkKICAgICAgICAgICAgICAgICAgcmVhbC5hZGQodmFsdWUodmFsKSk7CiAgICAgICAgICAgICAgcmV0dXJuIHJlYWw7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIGlmIChBcnJheS5pc0FycmF5KHRyYW5zZmVyYWJsZSkpIHsKICAgICAgICAgICAgICByZXR1cm4gdHJhbnNmZXJhYmxlLm1hcCh2ID0+IHZhbHVlKHYpKTsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgaWYgKHR5cGVvZiB0cmFuc2ZlcmFibGUgPT09ICJvYmplY3QiKSB7CiAgICAgICAgICAgICAgaWYgKCJfX190cmFuc2Zlci10eXBlIiBpbiB0cmFuc2ZlcmFibGUpIHsKICAgICAgICAgICAgICAgICAgc3dpdGNoICh0cmFuc2ZlcmFibGVbIl9fX3RyYW5zZmVyLXR5cGUiXSkgewogICAgICAgICAgICAgICAgICAgICAgY2FzZSAiZGF0ZSI6CiAgICAgICAgICAgICAgICAgICAgICAgICAgbGV0IGRhdGVPcHRzID0gdmFsdWUodHJhbnNmZXJhYmxlLm9wdGlvbnMpOwogICAgICAgICAgICAgICAgICAgICAgICAgIGxldCBkYXRlRGF0YSA9IHZhbHVlKHRyYW5zZmVyYWJsZS52YWx1ZSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIERhdGVUaW1lLmZyb21PYmplY3QoZGF0ZURhdGEsIHsgem9uZTogZGF0ZU9wdHMuem9uZSB9KTsKICAgICAgICAgICAgICAgICAgICAgIGNhc2UgImR1cmF0aW9uIjoKICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gRHVyYXRpb24uZnJvbU9iamVjdCh2YWx1ZSh0cmFuc2ZlcmFibGUudmFsdWUpKTsKICAgICAgICAgICAgICAgICAgICAgIGNhc2UgImxpbmsiOgogICAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBMaW5rLmZyb21PYmplY3QodmFsdWUodHJhbnNmZXJhYmxlLnZhbHVlKSk7CiAgICAgICAgICAgICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgICAgICAgICAgICAgIHRocm93IEVycm9yKGBVbnJlY29nbml6ZWQgdHJhbnNmZXIgdHlwZSAnJHt0cmFuc2ZlcmFibGVbIl9fX3RyYW5zZmVyLXR5cGUiXX0nYCk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgbGV0IHJlc3VsdCA9IHt9OwogICAgICAgICAgICAgIGZvciAobGV0IFtrZXksIHZhbF0gb2YgT2JqZWN0LmVudHJpZXModHJhbnNmZXJhYmxlKSkKICAgICAgICAgICAgICAgICAgcmVzdWx0W2tleV0gPSB2YWx1ZSh2YWwpOwogICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gdHJhbnNmZXJhYmxlOwogICAgICB9CiAgICAgIFRyYW5zZmVyYWJsZS52YWx1ZSA9IHZhbHVlOwogIH0pKFRyYW5zZmVyYWJsZSB8fCAoVHJhbnNmZXJhYmxlID0ge30pKTsKCiAgLyoqIEVudHJ5LXBvaW50IHNjcmlwdCB1c2VkIGJ5IHRoZSBpbmRleCBhcyBhIHdlYiB3b3JrZXIuICovCiAgLyoqIEFuIGltcG9ydCB3aGljaCBjYW4gZmFpbCBhbmQgcmFpc2UgYW4gZXhjZXB0aW9uLCB3aGljaCB3aWxsIGJlIGNhdWdodCBieSB0aGUgaGFuZGxlci4gKi8KICBmdW5jdGlvbiBmYWlsYWJsZUltcG9ydChwYXRoLCBjb250ZW50cywgc3RhdCwgbWV0YWRhdGEpIHsKICAgICAgaWYgKG1ldGFkYXRhID09PSB1bmRlZmluZWQgfHwgbWV0YWRhdGEgPT09IG51bGwpIHsKICAgICAgICAgIHRocm93IEVycm9yKGBDYW5ub3QgaW5kZXggZmlsZSwgc2luY2UgaXQgaGFzIG5vIE9ic2lkaWFuIGZpbGUgbWV0YWRhdGEuYCk7CiAgICAgIH0KICAgICAgcmV0dXJuIHJ1bkltcG9ydChwYXRoLCBjb250ZW50cywgc3RhdCwgbWV0YWRhdGEpOwogIH0KICBvbm1lc3NhZ2UgPSBhc3luYyAoZXZ0KSA9PiB7CiAgICAgIHRyeSB7CiAgICAgICAgICBsZXQgeyBwYXRoLCBjb250ZW50cywgc3RhdCwgbWV0YWRhdGEgfSA9IGV2dC5kYXRhOwogICAgICAgICAgbGV0IHJlc3VsdCA9IGZhaWxhYmxlSW1wb3J0KHBhdGgsIGNvbnRlbnRzLCBzdGF0LCBtZXRhZGF0YSk7CiAgICAgICAgICBwb3N0TWVzc2FnZSh7IHBhdGg6IGV2dC5kYXRhLnBhdGgsIHJlc3VsdDogVHJhbnNmZXJhYmxlLnRyYW5zZmVyYWJsZShyZXN1bHQpIH0pOwogICAgICB9CiAgICAgIGNhdGNoIChlcnJvcikgewogICAgICAgICAgY29uc29sZS5sb2coZXJyb3IpOwogICAgICAgICAgcG9zdE1lc3NhZ2UoewogICAgICAgICAgICAgIHBhdGg6IGV2dC5kYXRhLnBhdGgsCiAgICAgICAgICAgICAgcmVzdWx0OiB7CiAgICAgICAgICAgICAgICAgICRlcnJvcjogYEZhaWxlZCB0byBpbmRleCBmaWxlOiAke2V2dC5kYXRhLnBhdGh9OiAke2Vycm9yfWAsCiAgICAgICAgICAgICAgfSwKICAgICAgICAgIH0pOwogICAgICB9CiAgfTsKCn0pKCk7Cgo=', null, false); +/* eslint-enable */ - // If a result was found, parse it from the serialized - // string into a JS object. If result isn't truthy, the key - // is likely undefined and we'll pass it straight to the - // callback. - if (result) { - result = dbInfo.serializer.deserialize(result); +/** Controls and creates Dataview file importers, allowing for asynchronous loading and parsing of files. */ +/** Multi-threaded file parser which debounces rapid file requests automatically. */ +class FileImporter extends obsidian.Component { + constructor(numWorkers, vault, metadataCache) { + super(); + this.numWorkers = numWorkers; + this.vault = vault; + this.metadataCache = metadataCache; + this.workers = []; + this.busy = []; + this.reloadQueue = []; + this.reloadSet = new Set(); + this.callbacks = new Map(); + for (let index = 0; index < numWorkers; index++) { + let worker = new WorkerFactory({ name: "Dataview Indexer " + (index + 1) }); + worker.onmessage = evt => this.finish(evt.data.path, Transferable.value(evt.data.result), index); + this.workers.push(worker); + this.register(() => worker.terminate()); + this.busy.push(false); } - - return result; - }); - - executeCallback(promise, callback); - return promise; + } + /** + * Queue the given file for reloading. Multiple reload requests for the same file in a short time period will be de-bounced + * and all be resolved by a single actual file reload. + */ + reload(file) { + let promise = new Promise((resolve, reject) => { + var _a; + if (this.callbacks.has(file.path)) + (_a = this.callbacks.get(file.path)) === null || _a === void 0 ? void 0 : _a.push([resolve, reject]); + else + this.callbacks.set(file.path, [[resolve, reject]]); + }); + // De-bounce repeated requests for the same file. + if (this.reloadSet.has(file.path)) + return promise; + this.reloadSet.add(file.path); + // Immediately run this task if there are available workers; otherwise, add it to the queue. + let workerId = this.nextAvailableWorker(); + if (workerId !== undefined) { + this.send(file, workerId); + } + else { + this.reloadQueue.push(file); + } + return promise; + } + /** Finish the parsing of a file, potentially queueing a new file. */ + finish(path, data, index) { + var _a; + // Cache the callbacks before we do book-keeping. + let calls = [].concat((_a = this.callbacks.get(path)) !== null && _a !== void 0 ? _a : []); + // Book-keeping to clear metadata & allow the file to be re-loaded again. + this.reloadSet.delete(path); + this.callbacks.delete(path); + // Notify the queue this file is available for new work. + this.busy[index] = false; + // Queue a new job onto this worker. + let job = this.reloadQueue.shift(); + if (job !== undefined) + this.send(job, index); + // Resolve promises to let users know this file has finished. + if ("$error" in data) { + for (let [_, reject] of calls) + reject(data["$error"]); + } + else { + for (let [callback, _] of calls) + callback(data); + } + } + /** Send a new task to the given worker ID. */ + send(file, workerId) { + this.busy[workerId] = true; + this.vault.cachedRead(file).then(c => this.workers[workerId].postMessage({ + path: file.path, + contents: c, + stat: file.stat, + metadata: this.metadataCache.getFileCache(file), + })); + } + /** Find the next available, non-busy worker; return undefined if all workers are busy. */ + nextAvailableWorker() { + let index = this.busy.indexOf(false); + return index == -1 ? undefined : index; + } } -// Iterate over all items in the store. -function iterate$2(iterator, callback) { - var self = this; - - var promise = self.ready().then(function () { - var dbInfo = self._dbInfo; - var keyPrefix = dbInfo.keyPrefix; - var keyPrefixLength = keyPrefix.length; - var length = localStorage.length; - - // We use a dedicated iterator instead of the `i` variable below - // so other keys we fetch in localStorage aren't counted in - // the `iterationNumber` argument passed to the `iterate()` - // callback. - // - // See: github.com/mozilla/localForage/pull/435#discussion_r38061530 - var iterationNumber = 1; - - for (var i = 0; i < length; i++) { - var key = localStorage.key(i); - if (key.indexOf(keyPrefix) !== 0) { +/** Stores various indices on all files in the vault to make dataview generation fast. */ +/** Aggregate index which has several sub-indices and will initialize all of them. */ +class FullIndex extends obsidian.Component { + /** Generate a full index from the given vault. */ + static create(app, indexVersion, onChange) { + return new FullIndex(app, indexVersion, onChange); + } + /** Construct a new index using the app data and a current data version. */ + constructor(app, indexVersion, onChange) { + super(); + this.app = app; + this.indexVersion = indexVersion; + this.onChange = onChange; + this.initialized = false; + this.vault = app.vault; + this.metadataCache = app.metadataCache; + this.pages = new Map(); + this.tags = new ValueCaseInsensitiveIndexMap(); + this.etags = new ValueCaseInsensitiveIndexMap(); + this.links = new IndexMap(); + this.revision = 0; + // Caches metadata via durable storage to speed up cache initialization when Obsidian restarts. + this.persister = new LocalStorageCache(app.appId || "shared", indexVersion); + // Handles asynchronous reloading of files on web workers. + this.addChild((this.importer = new FileImporter(2, this.vault, this.metadataCache))); + // Prefix listens to file creation/deletion/rename, and not modifies, so we let it set up it's own listeners. + this.addChild((this.prefix = PrefixIndex.create(this.vault, () => this.touch()))); + // The CSV cache also needs to listen to filesystem events for cache invalidation. + this.addChild((this.csv = new CsvCache(this.vault))); + // The starred cache fetches starred entries semi-regularly via an interval. + this.addChild((this.starred = new StarredCache(this.app, () => this.touch()))); + } + /** Trigger a metadata event on the metadata cache. */ + trigger(...args) { + this.metadataCache.trigger("dataview:metadata-change", ...args); + } + /** "Touch" the index, incrementing the revision number and causing downstream views to reload. */ + touch() { + this.revision += 1; + this.onChange(); + } + /** Runs through the whole vault to set up initial file metadata. */ + initialize() { + // The metadata cache is updated on initial file index and file loads. + this.registerEvent(this.metadataCache.on("resolve", file => this.reload(file))); + // Renames do not set off the metadata cache; catch these explicitly. + this.registerEvent(this.vault.on("rename", this.rename, this)); + // File creation does cause a metadata change, but deletes do not. Clear the caches for this. + this.registerEvent(this.vault.on("delete", af => { + if (!(af instanceof obsidian.TFile) || !PathFilters.markdown(af.path)) + return; + let file = af; + this.pages.delete(file.path); + this.tags.delete(file.path); + this.etags.delete(file.path); + this.links.delete(file.path); + this.touch(); + this.trigger("delete", file); + })); + // Asynchronously initialize actual content in the background. + this._initialize(this.vault.getMarkdownFiles()); + } + /** Drops the local storage cache and re-indexes all files; this should generally be used if you expect cache issues. */ + async reinitialize() { + await this.persister.recreate(); + const files = this.vault.getMarkdownFiles(); + const start = Date.now(); + let promises = files.map(file => this.reload(file)); + await Promise.all(promises); + console.log(`Dataview: re-initialized index with ${files.length} files (${(Date.now() - start) / 1000.0}s)`); + } + /** Internal asynchronous initializer. */ + async _initialize(files) { + let reloadStart = Date.now(); + let promises = files.map(l => this.reload(l)); + let results = await Promise.all(promises); + let cached = 0, skipped = 0; + for (let item of results) { + if (item.skipped) { + skipped += 1; continue; } - var value = localStorage.getItem(key); - - // If a result was found, parse it from the serialized - // string into a JS object. If result isn't truthy, the - // key is likely undefined and we'll pass it straight - // to the iterator. - if (value) { - value = dbInfo.serializer.deserialize(value); - } - - value = iterator(value, key.substring(keyPrefixLength), iterationNumber++); - - if (value !== void 0) { - return value; - } - } - }); - - executeCallback(promise, callback); - return promise; -} - -// Same as localStorage's key() method, except takes a callback. -function key$2(n, callback) { - var self = this; - var promise = self.ready().then(function () { - var dbInfo = self._dbInfo; - var result; - try { - result = localStorage.key(n); - } catch (error) { - result = null; + if (item.cached) + cached += 1; } - - // Remove the prefix from the key, if a key is found. - if (result) { - result = result.substring(dbInfo.keyPrefix.length); + this.initialized = true; + this.metadataCache.trigger("dataview:index-ready"); + console.log(`Dataview: all ${files.length} files have been indexed in ${(Date.now() - reloadStart) / 1000.0}s (${cached} cached, ${skipped} skipped).`); + // Drop keys for files which do not exist anymore. + let remaining = await this.persister.synchronize(files.map(l => l.path)); + if (remaining.size > 0) { + console.log(`Dataview: Dropped cache entries for ${remaining.size} deleted files.`); } - - return result; - }); - - executeCallback(promise, callback); - return promise; -} - -function keys$2(callback) { - var self = this; - var promise = self.ready().then(function () { - var dbInfo = self._dbInfo; - var length = localStorage.length; - var keys = []; - - for (var i = 0; i < length; i++) { - var itemKey = localStorage.key(i); - if (itemKey.indexOf(dbInfo.keyPrefix) === 0) { - keys.push(itemKey.substring(dbInfo.keyPrefix.length)); + } + rename(file, oldPath) { + if (!(file instanceof obsidian.TFile) || !PathFilters.markdown(file.path)) + return; + if (this.pages.has(oldPath)) { + const oldMeta = this.pages.get(oldPath); + this.pages.delete(oldPath); + if (oldMeta) { + oldMeta.path = file.path; + this.pages.set(file.path, oldMeta); } } - - return keys; - }); - - executeCallback(promise, callback); - return promise; -} - -// Supply the number of keys in the datastore to the callback function. -function length$2(callback) { - var self = this; - var promise = self.keys().then(function (keys) { - return keys.length; - }); - - executeCallback(promise, callback); - return promise; -} - -// Remove an item from the store, nice and simple. -function removeItem$2(key, callback) { - var self = this; - - key = normalizeKey(key); - - var promise = self.ready().then(function () { - var dbInfo = self._dbInfo; - localStorage.removeItem(dbInfo.keyPrefix + key); - }); - - executeCallback(promise, callback); - return promise; -} - -// Set a key's value and run an optional callback once the value is set. -// Unlike Gaia's implementation, the callback function is passed the value, -// in case you want to operate on that value only after you're sure it -// saved, or something like that. -function setItem$2(key, value, callback) { - var self = this; - - key = normalizeKey(key); - - var promise = self.ready().then(function () { - // Convert undefined values to null. - // https://github.com/mozilla/localForage/pull/42 - if (value === undefined) { - value = null; + this.tags.rename(oldPath, file.path); + this.links.rename(oldPath, file.path); + this.etags.rename(oldPath, file.path); + this.touch(); + this.trigger("rename", file, oldPath); + } + /** Queue a file for reloading; this is done asynchronously in the background and may take a few seconds. */ + async reload(file) { + if (!PathFilters.markdown(file.path)) + return { cached: false, skipped: true }; + // The first load of a file is attempted from persisted cache; subsequent loads just use the importer. + if (this.pages.has(file.path) || this.initialized) { + await this.import(file); + return { cached: false, skipped: false }; } - - // Save the original value to pass to the callback. - var originalValue = value; - - return new Promise$1(function (resolve, reject) { - var dbInfo = self._dbInfo; - dbInfo.serializer.serialize(value, function (value, error) { - if (error) { - reject(error); - } else { - try { - localStorage.setItem(dbInfo.keyPrefix + key, value); - resolve(originalValue); - } catch (e) { - // localStorage capacity exceeded. - // TODO: Make this a specific error/event. - if (e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') { - reject(e); - } - reject(e); - } + else { + // Check the cache for the latest data; if it is out of date or non-existent, then reload. + return this.persister.loadFile(file.path).then(async (cached) => { + if (!cached || cached.time < file.stat.mtime || cached.version != this.indexVersion) { + // This cache value is out of data, reload via the importer and update the cache. + // We will skip files with no active file metadata - they will be caught by a later reload + // via the 'resolve' metadata event. + let fileCache = this.metadataCache.getFileCache(file); + if (fileCache === undefined || fileCache === null) + return { cached: false, skipped: true }; + await this.import(file); + return { cached: false, skipped: false }; + } + else { + // Use the cached data since it is up to date and on the same version. + this.finish(file, cached.data); + return { cached: true, skipped: false }; } }); + } + } + /** Import a file directly from disk, skipping the cache. */ + async import(file) { + return this.importer.reload(file).then(r => { + this.finish(file, r); + this.persister.storeFile(file.path, r); }); - }); - - executeCallback(promise, callback); - return promise; + } + /** Finish the reloading of file metadata by adding it to in memory indexes. */ + finish(file, parsed) { + let meta = PageMetadata.canonicalize(parsed, link => { + let realPath = this.metadataCache.getFirstLinkpathDest(link.path, file.path); + if (realPath) + return link.withPath(realPath.path); + else + return link; + }); + this.pages.set(file.path, meta); + this.tags.set(file.path, meta.fullTags()); + this.etags.set(file.path, meta.tags); + this.links.set(file.path, new Set(meta.links.map(l => l.path))); + this.touch(); + this.trigger("update", file); + } } - -function dropInstance$2(options, callback) { - callback = getCallback.apply(this, arguments); - - options = typeof options !== 'function' && options || {}; - if (!options.name) { - var currentConfig = this.config(); - options.name = options.name || currentConfig.name; - options.storeName = options.storeName || currentConfig.storeName; +/** Indexes files by their full prefix - essentially a simple prefix tree. */ +class PrefixIndex extends obsidian.Component { + static create(vault, updateRevision) { + return new PrefixIndex(vault, updateRevision); } - - var self = this; - var promise; - if (!options.name) { - promise = Promise$1.reject('Invalid arguments'); - } else { - promise = new Promise$1(function (resolve) { - if (!options.storeName) { - resolve(options.name + '/'); - } else { - resolve(_getKeyPrefix(options, self._defaultConfig)); + constructor(vault, updateRevision) { + super(); + this.vault = vault; + this.updateRevision = updateRevision; + } + *walk(folder, filter) { + for (const file of folder.children) { + if (file instanceof obsidian.TFolder) { + yield* this.walk(file, filter); } - }).then(function (keyPrefix) { - for (var i = localStorage.length - 1; i >= 0; i--) { - var key = localStorage.key(i); - - if (key.indexOf(keyPrefix) === 0) { - localStorage.removeItem(key); - } + else if (filter ? filter(file.path) : true) { + yield file.path; } - }); + } + } + /** Get the list of all files under the given path. */ + get(prefix, filter) { + let folder = this.vault.getAbstractFileByPath(prefix || "/"); + return new Set(folder instanceof obsidian.TFolder ? this.walk(folder, filter) : []); + } + /** Determines if the given path exists in the prefix index. */ + pathExists(path) { + return this.vault.getAbstractFileByPath(path || "/") != null; + } + /** Determines if the given prefix exists in the prefix index. */ + nodeExists(prefix) { + return this.vault.getAbstractFileByPath(prefix || "/") instanceof obsidian.TFolder; + } + /** + * Use the in-memory prefix index to convert a relative path to an absolute one. + */ + resolveRelative(path, origin) { + if (!origin) + return path; + else if (path.startsWith("/")) + return path.substring(1); + let relativePath = getParentFolder(origin) + "/" + path; + if (this.pathExists(relativePath)) + return relativePath; + else + return path; } - - executeCallback(promise, callback); - return promise; } - -var localStorageWrapper = { - _driver: 'localStorageWrapper', - _initStorage: _initStorage$2, - _support: isLocalStorageValid(), - iterate: iterate$2, - getItem: getItem$2, - setItem: setItem$2, - removeItem: removeItem$2, - clear: clear$2, - length: length$2, - key: key$2, - keys: keys$2, - dropInstance: dropInstance$2 -}; - -var sameValue = function sameValue(x, y) { - return x === y || typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y); -}; - -var includes = function includes(array, searchElement) { - var len = array.length; - var i = 0; - while (i < len) { - if (sameValue(array[i], searchElement)) { - return true; +/** Simple path filters which filter file types. */ +var PathFilters; +(function (PathFilters) { + function csv(path) { + return path.toLowerCase().endsWith(".csv"); + } + PathFilters.csv = csv; + function markdown(path) { + let lcPath = path.toLowerCase(); + return lcPath.endsWith(".md") || lcPath.endsWith(".markdown"); + } + PathFilters.markdown = markdown; +})(PathFilters || (PathFilters = {})); +/** + * Caches in-use CSVs to make high-frequency reloads (such as actively looking at a document + * that uses CSV) fast. + */ +class CsvCache extends obsidian.Component { + constructor(vault) { + super(); + this.vault = vault; + this.cache = new Map(); + // Force-flush the cache on CSV file deletions or modifications. + this.registerEvent(this.vault.on("modify", file => { + if (file instanceof obsidian.TFile && PathFilters.csv(file.path)) + this.cache.delete(file.path); + })); + this.registerEvent(this.vault.on("delete", file => { + if (file instanceof obsidian.TFile && PathFilters.csv(file.path)) + this.cache.delete(file.path); + })); + } + /** Load a CSV file from the cache, doing a fresh load if it has not been loaded. */ + async get(path) { + // Clear old entries on every fresh load, since the path being loaded may be stale. + this.clearOldEntries(); + let existing = this.cache.get(path); + if (existing) + return Result.success(existing.data); + else { + let value = await this.loadInternal(path); + if (value.successful) + this.cache.set(path, { data: value.value, loadTime: DateTime.now() }); + return value; } - i++; } - - return false; -}; - -var isArray = Array.isArray || function (arg) { - return Object.prototype.toString.call(arg) === '[object Array]'; -}; - -// Drivers are stored here when `defineDriver()` is called. -// They are shared across all instances of localForage. -var DefinedDrivers = {}; - -var DriverSupport = {}; - -var DefaultDrivers = { - INDEXEDDB: asyncStorage, - WEBSQL: webSQLStorage, - LOCALSTORAGE: localStorageWrapper -}; - -var DefaultDriverOrder = [DefaultDrivers.INDEXEDDB._driver, DefaultDrivers.WEBSQL._driver, DefaultDrivers.LOCALSTORAGE._driver]; - -var OptionalDriverMethods = ['dropInstance']; - -var LibraryMethods = ['clear', 'getItem', 'iterate', 'key', 'keys', 'length', 'removeItem', 'setItem'].concat(OptionalDriverMethods); - -var DefaultConfig = { - description: '', - driver: DefaultDriverOrder.slice(), - name: 'localforage', - // Default DB size is _JUST UNDER_ 5MB, as it's the highest size - // we can use without a prompt. - size: 4980736, - storeName: 'keyvaluepairs', - version: 1.0 -}; - -function callWhenReady(localForageInstance, libraryMethod) { - localForageInstance[libraryMethod] = function () { - var _args = arguments; - return localForageInstance.ready().then(function () { - return localForageInstance[libraryMethod].apply(localForageInstance, _args); - }); - }; -} - -function extend() { - for (var i = 1; i < arguments.length; i++) { - var arg = arguments[i]; - - if (arg) { - for (var _key in arg) { - if (arg.hasOwnProperty(_key)) { - if (isArray(arg[_key])) { - arguments[0][_key] = arg[_key].slice(); - } else { - arguments[0][_key] = arg[_key]; - } - } + /** Do the actual raw loading of a CSV path (which is either local or an HTTP request). */ + async loadInternal(path) { + // Allow http://, https://, and file:// prefixes which use AJAX. + if (path.startsWith("http://") || path.startsWith("https://") || path.startsWith("file://")) { + try { + let result = await fetch(path, { + method: "GET", + mode: "no-cors", + redirect: "follow", + }); + return Result.success(parseCsv(await result.text())); + } + catch (ex) { + return Result.failure("" + ex + "\n\n" + ex.stack); } } + // Otherwise, assume it is a fully-qualified file path. + try { + let fileData = await this.vault.adapter.read(path); + return Result.success(parseCsv(fileData)); + } + catch (ex) { + return Result.failure(`Failed to load data from path '${path}'.`); + } + } + /** Clear old entries in the cache (as measured by insertion time). */ + clearOldEntries() { + let currentTime = DateTime.now(); + let keysToRemove = new Set(); + for (let [key, value] of this.cache.entries()) { + let entryAge = Math.abs(currentTime.diff(value.loadTime, "seconds").seconds); + if (entryAge > CsvCache.CACHE_EXPIRY_SECONDS) + keysToRemove.add(key); + } + keysToRemove.forEach(key => this.cache.delete(key)); } - - return arguments[0]; } - -var LocalForage = function () { - function LocalForage(options) { - _classCallCheck(this, LocalForage); - - for (var driverTypeKey in DefaultDrivers) { - if (DefaultDrivers.hasOwnProperty(driverTypeKey)) { - var driver = DefaultDrivers[driverTypeKey]; - var driverName = driver._driver; - this[driverTypeKey] = driverName; - - if (!DefinedDrivers[driverName]) { - // we don't need to wait for the promise, - // since the default drivers can be defined - // in a blocking manner - this.defineDriver(driver); +CsvCache.CACHE_EXPIRY_SECONDS = 5 * 60; +/** Optional connector to the Obsidian 'Starred' plugin which allows for efficiently querying if a file is starred or not. */ +class StarredCache extends obsidian.Component { + constructor(app, onUpdate) { + super(); + this.app = app; + this.onUpdate = onUpdate; + this.stars = StarredCache.fetch(this.app); + this.registerInterval(window.setInterval(() => this.reload(), StarredCache.REFRESH_INTERVAL)); + const initialHandler = window.setTimeout(() => this.reload(), StarredCache.INITIAL_DELAY); + this.register(() => window.clearTimeout(initialHandler)); + } + /** Determines if the given path is starred. */ + starred(path) { + return this.stars.has(path); + } + reload() { + let newStars = StarredCache.fetch(this.app); + if (!setsEqual(this.stars, newStars)) { + this.stars = newStars; + this.onUpdate(); + } + } + /** Fetch all starred files from the stars plugin, if present. */ + static fetch(app) { + var _a, _b, _c, _d; + let items = (_d = (_c = (_b = (_a = app === null || app === void 0 ? void 0 : app.internalPlugins) === null || _a === void 0 ? void 0 : _a.plugins) === null || _b === void 0 ? void 0 : _b.bookmarks) === null || _c === void 0 ? void 0 : _c.instance) === null || _d === void 0 ? void 0 : _d.items; + if (items == undefined) + return new Set(); + // Retrieve all grouped (nested) items, returning a flat array + const flattenItems = (items) => { + let children = []; + return items + .map(i => { + if (i.type == "group" && i.items && i.items.length) { + children = [...children, ...i.items]; } + return i; + }) + .concat(children.length ? flattenItems(children) : children); + }; + items = flattenItems(items); + return new Set(items.filter((l) => l.type === "file").map(l => l.path)); + } +} +/** Initial delay before checking the cache; we need to wait for it to asynchronously load the initial stars. */ +StarredCache.INITIAL_DELAY = 4 * 1000; +/** How frequently to check for star updates. */ +StarredCache.REFRESH_INTERVAL = 30 * 1000; +/** A generic index which indexes variables of the form key -> value[], allowing both forward and reverse lookups. */ +class IndexMap { + /** Create a new, empty index map. */ + constructor() { + this.map = new Map(); + this.invMap = new Map(); + } + /** Returns all values for the given key. */ + get(key) { + let result = this.map.get(key); + if (result) { + return new Set(result); + } + else { + return new Set(); + } + } + /** Returns all keys that reference the given key. Mutating the returned set is not allowed. */ + getInverse(value) { + return this.invMap.get(value) || IndexMap.EMPTY_SET; + } + /** Sets the key to the given values; this will delete the old mapping for the key if one was present. */ + set(key, values) { + var _a, _b; + if (!values.size) { + // no need to store if no values + this.delete(key); + return this; + } + let oldValues = this.map.get(key); + if (oldValues) { + for (let value of oldValues) { + // Only delete the ones we're not adding back + if (!values.has(key)) + (_a = this.invMap.get(value)) === null || _a === void 0 ? void 0 : _a.delete(key); } } - - this._defaultConfig = extend({}, DefaultConfig); - this._config = extend({}, this._defaultConfig, options); - this._driverSet = null; - this._initDriver = null; - this._ready = false; - this._dbInfo = null; - - this._wrapLibraryMethodsWithReady(); - this.setDriver(this._config.driver)["catch"](function () {}); + this.map.set(key, values); + for (let value of values) { + if (!this.invMap.has(value)) + this.invMap.set(value, new Set([key])); + else + (_b = this.invMap.get(value)) === null || _b === void 0 ? void 0 : _b.add(key); + } + return this; } + /** Clears all values for the given key so they can be re-added. */ + delete(key) { + var _a; + let oldValues = this.map.get(key); + if (!oldValues) + return false; + this.map.delete(key); + for (let value of oldValues) { + (_a = this.invMap.get(value)) === null || _a === void 0 ? void 0 : _a.delete(key); + } + return true; + } + /** Rename all references to the given key to a new value. */ + rename(oldKey, newKey) { + let oldValues = this.map.get(oldKey); + if (!oldValues) + return false; + this.delete(oldKey); + this.set(newKey, oldValues); + return true; + } + /** Clear the entire index. */ + clear() { + this.map.clear(); + this.invMap.clear(); + } +} +IndexMap.EMPTY_SET = Object.freeze(new Set()); +/** Index map wrapper which is case-insensitive in the key. */ +class ValueCaseInsensitiveIndexMap { + /** Create a new, empty case insensitive index map. */ + constructor(delegate = new IndexMap()) { + this.delegate = delegate; + } + /** Returns all values for the given key. */ + get(key) { + return this.delegate.get(key); + } + /** Returns all keys that reference the given value. Mutating the returned set is not allowed. */ + getInverse(value) { + return this.delegate.getInverse(value.toLocaleLowerCase()); + } + /** Sets the key to the given values; this will delete the old mapping for the key if one was present. */ + set(key, values) { + this.delegate.set(key, new Set(Array.from(values).map(v => v.toLocaleLowerCase()))); + return this; + } + /** Clears all values for the given key so they can be re-added. */ + delete(key) { + return this.delegate.delete(key); + } + /** Rename all references to the given key to a new value. */ + rename(oldKey, newKey) { + return this.delegate.rename(oldKey, newKey); + } + /** Clear the entire index. */ + clear() { + this.delegate.clear(); + } +} - // Set any config values for localForage; can be called anytime before - // the first API call (e.g. `getItem`, `setItem`). - // We loop through options so we don't overwrite existing config - // values. - - - LocalForage.prototype.config = function config(options) { - // If the options argument is an object, we use it to set values. - // Otherwise, we return either a specified config value or all - // config values. - if ((typeof options === 'undefined' ? 'undefined' : _typeof(options)) === 'object') { - // If localforage is ready and fully initialized, we can't set - // any new configuration values. Instead, we return an error. - if (this._ready) { - return new Error("Can't call config() after localforage " + 'has been used.'); +/** Collect data matching a source query. */ +/** Find source paths which match the given source. */ +function matchingSourcePaths(source, index, originFile = "") { + var _a; + switch (source.type) { + case "empty": + return Result.success(new Set()); + case "tag": + return Result.success(index.tags.getInverse(source.tag)); + case "csv": + return Result.success(new Set([index.prefix.resolveRelative(source.path, originFile)])); + case "folder": + // Prefer loading from the folder at the given path. + if (index.prefix.nodeExists(source.folder)) + return Result.success(index.prefix.get(source.folder, PathFilters.markdown)); + // But allow for loading individual files if they exist. + if (index.prefix.pathExists(source.folder)) + return Result.success(new Set([source.folder])); + else if (index.prefix.pathExists(source.folder + ".md")) + return Result.success(new Set([source.folder + ".md"])); + // For backwards-compat, return an empty result even if the folder does not exist. + return Result.success(new Set()); + case "link": + let fullPath = (_a = index.metadataCache.getFirstLinkpathDest(source.file, originFile)) === null || _a === void 0 ? void 0 : _a.path; + if (!fullPath) { + // Look in links which includes unresolved links + return Result.success(index.links.getInverse(source.file)); } - - for (var i in options) { - if (i === 'storeName') { - options[i] = options[i].replace(/\W/g, '_'); - } - - if (i === 'version' && typeof options[i] !== 'number') { - return new Error('Database version must be a number.'); + if (source.direction === "incoming") { + // To find all incoming links (i.e., things that link to this), use the index that Obsidian provides. + // TODO: Use an actual index so this isn't a fullscan. + let resolved = index.metadataCache.resolvedLinks; + let incoming = new Set(); + for (let [key, value] of Object.entries(resolved)) { + if (fullPath in value) + incoming.add(key); } - - this._config[i] = options[i]; + return Result.success(incoming); } - - // after all config options are set and - // the driver option is used, try setting it - if ('driver' in options && options.driver) { - return this.setDriver(this._config.driver); + else { + let resolved = index.metadataCache.resolvedLinks; + if (!(fullPath in resolved)) + return Result.failure(`Could not find file "${source.file}" during link lookup - does it exist?`); + return Result.success(new Set(Object.keys(index.metadataCache.resolvedLinks[fullPath]))); } - - return true; - } else if (typeof options === 'string') { - return this._config[options]; - } else { - return this._config; - } - }; - - // Used to define a custom driver, shared across all instances of - // localForage. - - - LocalForage.prototype.defineDriver = function defineDriver(driverObject, callback, errorCallback) { - var promise = new Promise$1(function (resolve, reject) { - try { - var driverName = driverObject._driver; - var complianceError = new Error('Custom driver not compliant; see ' + 'https://mozilla.github.io/localForage/#definedriver'); - - // A driver name should be defined and not overlap with the - // library-defined, default drivers. - if (!driverObject._driver) { - reject(complianceError); - return; - } - - var driverMethods = LibraryMethods.concat('_initStorage'); - for (var i = 0, len = driverMethods.length; i < len; i++) { - var driverMethodName = driverMethods[i]; - - // when the property is there, - // it should be a method even when optional - var isRequired = !includes(OptionalDriverMethods, driverMethodName); - if ((isRequired || driverObject[driverMethodName]) && typeof driverObject[driverMethodName] !== 'function') { - reject(complianceError); - return; + case "binaryop": + return Result.flatMap2(matchingSourcePaths(source.left, index, originFile), matchingSourcePaths(source.right, index, originFile), (left, right) => { + if (source.op == "&") { + let result = new Set(); + for (let elem of right) { + if (left.has(elem)) + result.add(elem); } + return Result.success(result); } - - var configureMissingMethods = function configureMissingMethods() { - var methodNotImplementedFactory = function methodNotImplementedFactory(methodName) { - return function () { - var error = new Error('Method ' + methodName + ' is not implemented by the current driver'); - var promise = Promise$1.reject(error); - executeCallback(promise, arguments[arguments.length - 1]); - return promise; - }; - }; - - for (var _i = 0, _len = OptionalDriverMethods.length; _i < _len; _i++) { - var optionalDriverMethod = OptionalDriverMethods[_i]; - if (!driverObject[optionalDriverMethod]) { - driverObject[optionalDriverMethod] = methodNotImplementedFactory(optionalDriverMethod); - } - } - }; - - configureMissingMethods(); - - var setDriverSupport = function setDriverSupport(support) { - if (DefinedDrivers[driverName]) { - console.info('Redefining LocalForage driver: ' + driverName); - } - DefinedDrivers[driverName] = driverObject; - DriverSupport[driverName] = support; - // don't use a then, so that we can define - // drivers that have simple _support methods - // in a blocking manner - resolve(); - }; - - if ('_support' in driverObject) { - if (driverObject._support && typeof driverObject._support === 'function') { - driverObject._support().then(setDriverSupport, reject); - } else { - setDriverSupport(!!driverObject._support); - } - } else { - setDriverSupport(true); + else if (source.op == "|") { + let result = new Set(left); + for (let elem of right) + result.add(elem); + return Result.success(result); } - } catch (e) { - reject(e); - } + else { + return Result.failure(`Unrecognized operator '${source.op}'.`); + } + }); + case "negate": + return matchingSourcePaths(source.child, index, originFile).map(child => { + // TODO: This is obviously very inefficient. Can be improved by complicating the + // return type of this function & optimizing 'and' / 'or'. + let allFiles = new Set(index.vault.getMarkdownFiles().map(f => f.path)); + child.forEach(f => allFiles.delete(f)); + return allFiles; + }); + } +} +/** Convert a path to the data for that path; usually markdown pages, but could also be other file types (like CSV). */ +async function resolvePathData(path, index) { + if (PathFilters.csv(path)) + return resolveCsvData(path, index); + else + return resolveMarkdownData(path, index); +} +// TODO: We shouldn't be doing path normalization here relative to an origin file, +/** Convert a CSV path to the data in the CSV (in dataview format). */ +async function resolveCsvData(path, index) { + let rawData = await index.csv.get(path); + return rawData.map(rows => { + return rows.map((row, index) => { + return { + id: `${path}#${index}`, + data: row, + }; }); + }); +} +/** Convert a path pointing to a markdown page, into the associated metadata. */ +function resolveMarkdownData(path, index) { + let page = index.pages.get(path); + if (!page) + return Result.success([]); + return Result.success([ + { + id: Link.file(path), + data: page.serialize(index), + }, + ]); +} +/** Resolve a source to the collection of data rows that it matches. */ +async function resolveSource(source, index, originFile = "") { + let paths = matchingSourcePaths(source, index, originFile); + if (!paths.successful) + return Result.failure(paths.error); + let result = []; + for (let path of paths.value) { + let resolved = await resolvePathData(path, index); + if (!resolved.successful) + return resolved; + for (let val of resolved.value) + result.push(val); + } + return Result.success(result); +} - executeTwoCallbacks(promise, callback, errorCallback); - return promise; - }; - - LocalForage.prototype.driver = function driver() { - return this._driver || null; - }; - - LocalForage.prototype.getDriver = function getDriver(driverName, callback, errorCallback) { - var getDriverPromise = DefinedDrivers[driverName] ? Promise$1.resolve(DefinedDrivers[driverName]) : Promise$1.reject(new Error('Driver not found.')); - - executeTwoCallbacks(getDriverPromise, callback, errorCallback); - return getDriverPromise; - }; - - LocalForage.prototype.getSerializer = function getSerializer(callback) { - var serializerPromise = Promise$1.resolve(localforageSerializer); - executeTwoCallbacks(serializerPromise, callback); - return serializerPromise; +/** Default function implementations for the expression evaluator. */ +/** + * Allows for the creation of functions that check the number and type of their arguments, and dispatch + * to different implemenations based on the types of the inputs. + */ +class FunctionBuilder { + constructor(name) { + this.name = name; + this.variants = []; + this.vectorized = {}; + } + /** Add a general function variant which accepts any number of arguments of any type. */ + vararg(impl) { + this.variants.push({ args: [], varargs: true, impl }); + return this; + } + /** Add a function variant which takes in a single argument. */ + add1(argType, impl) { + this.variants.push({ + args: [argType], + varargs: false, + impl: (c, ...rest) => impl(rest[0], c), + }); + return this; + } + /** Add a function variant which takes in two typed arguments. */ + add2(arg1, arg2, impl) { + this.variants.push({ + args: [arg1, arg2], + varargs: false, + impl: (c, ...rest) => impl(rest[0], rest[1], c), + }); + return this; + } + /** Add a function variant which takes in three typed arguments. */ + add3(arg1, arg2, arg3, impl) { + this.variants.push({ + args: [arg1, arg2, arg3], + varargs: false, + impl: (c, ...rest) => impl(rest[0], rest[1], rest[2], c), + }); + return this; + } + /** Add vectorized variants which accept the given number of arguments and delegate. */ + vectorize(numArgs, positions) { + this.vectorized[numArgs] = positions; + return this; + } + /** Return a function which checks the number and type of arguments, passing them on to the first matching variant. */ + build() { + let self = (context, ...args) => { + let types = []; + for (let arg of args) { + let argType = Values.typeOf(arg); + if (!argType) + throw Error(`Unrecognized argument type for argument '${arg}'`); + types.push(argType); + } + // Handle vectorization, possibly in multiple fields. + if (this.vectorized[types.length]) { + let vectorizedPositions = this.vectorized[types.length].filter(k => types[k] == "array"); + if (vectorizedPositions.length > 0) { + let minLength = vectorizedPositions + .map(p => args[p].length) + .reduce((p, c) => Math.min(p, c)); + // Call the subfunction for each element in the longest array. + // If you call a vectorized function with different-length arrays, + // the output is limited by the length of the shortest array. + let result = []; + for (let vpos = 0; vpos < minLength; vpos++) { + let subargs = []; + for (let index = 0; index < args.length; index++) { + if (vectorizedPositions.includes(index)) { + let arr = args[index]; + subargs.push(arr[vpos]); + } + else { + subargs.push(args[index]); + } + } + result.push(self(context, ...subargs)); + } + return result; + } + } + outer: for (let variant of this.variants) { + if (variant.varargs) + return variant.impl(context, ...args); + if (variant.args.length != types.length) + continue; + for (let index = 0; index < variant.args.length; index++) { + if (variant.args[index] != "*" && variant.args[index] != types[index]) + continue outer; + } + return variant.impl(context, ...args); + } + throw Error(`No implementation of '${this.name}' found for arguments: ${types.join(", ")}`); + }; + return self; + } +} +/** Utilities for managing function implementations. */ +var Functions; +(function (Functions) { + /** Bind a context to a function implementation, yielding a function which does not need the context argument. */ + function bind(func, context) { + return (...args) => func(context, ...args); + } + Functions.bind = bind; + /** Bind a context to all functions in the given map, yielding a new map of bound functions. */ + function bindAll(funcs, context) { + let result = {}; + for (let [key, func] of Object.entries(funcs)) { + result[key] = Functions.bind(func, context); + } + return result; + } + Functions.bindAll = bindAll; +})(Functions || (Functions = {})); +/** + * Collection of all defined functions; defined here so that they can be called from within dataview, + * and test code. + */ +var DefaultFunctions; +(function (DefaultFunctions) { + DefaultFunctions.typeOf = new FunctionBuilder("type") + .add1("array", _ => "array") + .add1("boolean", _ => "boolean") + .add1("date", _ => "date") + .add1("duration", _ => "duration") + .add1("function", _ => "function") + .add1("widget", _ => "widget") + .add1("link", _ => "link") + .add1("null", _ => "null") + .add1("number", _ => "number") + .add1("object", _ => "object") + .add1("string", _ => "string") + .add1("*", _ => "unknown") + .build(); + /** Compute the length of a data type. */ + DefaultFunctions.length = new FunctionBuilder("length") + .add1("array", a => a.length) + .add1("object", a => Object.keys(a).length) + .add1("string", a => a.length) + .add1("null", _a => 0) + .build(); + /** List constructor function. */ + DefaultFunctions.list = (_context, ...args) => args; + /** Object constructor function. */ + DefaultFunctions.object = (_context, ...args) => { + if (args.length % 2 != 0) + throw Error("object() requires an even number of arguments"); + let result = {}; + for (let index = 0; index < args.length; index += 2) { + let key = args[index]; + if (!Values.isString(key)) + throw Error("keys should be of type string for object(key1, value1, ...)"); + result[key] = args[index + 1]; + } + return result; }; - - LocalForage.prototype.ready = function ready(callback) { - var self = this; - - var promise = self._driverSet.then(function () { - if (self._ready === null) { - self._ready = self._initDriver(); + /** Internal link constructor function. */ + DefaultFunctions.link = new FunctionBuilder("link") + .add1("string", (a, c) => Link.file(c.linkHandler.normalize(a), false)) + .add1("link", a => a) + .add1("null", _a => null) + .vectorize(1, [0]) + .add2("string", "string", (t, d, c) => Link.file(c.linkHandler.normalize(t), false, d)) + .add3("string", "string", "boolean", (t, d, e, c) => Link.file(c.linkHandler.normalize(t), e, d)) + .add2("link", "string", (t, d) => t.withDisplay(d)) + .add2("null", "*", () => null) + .add2("*", "null", (t, _n, c) => DefaultFunctions.link(c, t)) + .vectorize(2, [0, 1]) + .build(); + /** Embed and un-embed a link. */ + DefaultFunctions.embed = new FunctionBuilder("embed") + .add1("link", l => l.toEmbed()) + .vectorize(1, [0]) + .add2("link", "boolean", (l, e, c) => (e ? l.toEmbed() : l.fromEmbed())) + .add1("null", () => null) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .vectorize(2, [0, 1]) + .build(); + /** External link constructor function. */ + DefaultFunctions.elink = new FunctionBuilder("elink") + .add2("string", "string", (a, d) => Widgets.externalLink(a, d)) + .add2("string", "null", (s, _n, c) => DefaultFunctions.elink(c, s, s)) + .add2("null", "*", () => null) + .vectorize(2, [0]) + .add1("string", (a, c) => DefaultFunctions.elink(c, a, a)) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + /** Date constructor function. */ + DefaultFunctions.date = new FunctionBuilder("date") + .add1("string", str => { + let parsedDate = EXPRESSION.datePlus.parse(str); + if (parsedDate.status) + return parsedDate.value; + else + return null; + }) + .add1("date", d => d) + .add1("link", (link, c) => { + var _c, _d; + // Try to parse from the display... + if (link.display) { + let parsedDate = EXPRESSION.date.parse(link.display); + if (parsedDate.status) + return parsedDate.value; + } + // Then try to parse from the path... + let parsedDate = EXPRESSION.date.parse(link.path); + if (parsedDate.status) + return parsedDate.value; + // Then pull it from the file. + let resolved = c.linkHandler.resolve(link.path); + if (resolved && ((_c = resolved === null || resolved === void 0 ? void 0 : resolved.file) === null || _c === void 0 ? void 0 : _c.day)) { + return (_d = resolved === null || resolved === void 0 ? void 0 : resolved.file) === null || _d === void 0 ? void 0 : _d.day; + } + return null; + }) + .add2("string", "string", (d, f) => { + if (f === "x" || f === "X") { + let match = NUMBER_REGEX.exec(d); + if (match) + return DateTime.fromMillis(Number.parseInt(match[0]) * (f === "X" ? 1000 : 1)); + else { + throw Error("Not a number for format( (${ f }): ${ d }"); } - - return self._ready; + } + else { + let parsedDate = DateTime.fromFormat(d, f); + if (parsedDate.isValid) + return parsedDate; + else { + throw Error(`Can't handle format (${f}) on date string (${d})`); + } + } + }) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + /** Duration constructor function. */ + DefaultFunctions.dur = new FunctionBuilder("dur") + .add1("string", str => { + let parsedDur = EXPRESSION.duration.parse(str.trim()); + if (parsedDur.status) + return parsedDur.value; + else + return null; + }) + .add1("duration", d => d) + .add1("null", d => d) + .vectorize(1, [0]) + .build(); + /** Format a date using a luxon/moment-style date format. */ + DefaultFunctions.dateformat = new FunctionBuilder("dateformat") + .add2("date", "string", (date, format) => date.toFormat(format, { locale: currentLocale() })) + .add2("null", "string", (_nul, _format) => null) + .vectorize(2, [0]) + .build(); + DefaultFunctions.durationformat = new FunctionBuilder("durationformat") + .add2("duration", "string", (dur, format) => dur.toFormat(format)) + .add2("null", "string", (_nul, _format) => null) + .vectorize(2, [0]) + .build(); + DefaultFunctions.localtime = new FunctionBuilder("localtime") + .add1("date", d => d.toLocal()) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + const NUMBER_REGEX = /-?[0-9]+(\.[0-9]+)?/; + /** Number constructor function. */ + DefaultFunctions.number = new FunctionBuilder("number") + .add1("number", a => a) + .add1("string", str => { + let match = NUMBER_REGEX.exec(str); + if (match) + return Number.parseFloat(match[0]); + else + return null; + }) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + /** Format a number using a standard currency format. */ + DefaultFunctions.currencyformat = new FunctionBuilder("currencyformat") + .add2("number", "string", (num, format) => Intl.NumberFormat(currentLocale(), { style: "currency", currency: format }).format(num)) + .add2("null", "string", (_nul, _format) => null) + .add1("number", num => Intl.NumberFormat(currentLocale(), { style: "currency", currency: "USD" }).format(num)) + .add1("null", () => null) + .vectorize(2, [0]) + .build(); + /** + * Convert any value to a reasonable internal string representation. Most useful for dates, strings, numbers, and + * so on. + */ + DefaultFunctions.string = new FunctionBuilder("string").add1("*", (a, ctx) => Values.toString(a, ctx.settings)).build(); + DefaultFunctions.round = new FunctionBuilder("round") + .add1("number", n => Math.round(n)) + .add1("null", () => null) + .vectorize(1, [0]) + .add2("number", "number", (n, p) => { + if (p <= 0) + return Math.round(n); + return parseFloat(n.toFixed(p)); + }) + .add2("number", "null", n => Math.round(n)) + .add2("null", "*", () => null) + .vectorize(2, [0]) + .build(); + DefaultFunctions.trunc = new FunctionBuilder("trunc") + .add1("number", n => Math.trunc(n)) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + DefaultFunctions.floor = new FunctionBuilder("floor") + .add1("number", n => Math.floor(n)) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + DefaultFunctions.ceil = new FunctionBuilder("ceil") + .add1("number", n => Math.ceil(n)) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + DefaultFunctions.min = new FunctionBuilder("min") + .add2("*", "null", (a, _n) => a) + .add2("null", "*", (_n, a) => a) + .add2("*", "*", (a, b, ctx) => (Values.compareValue(a, b, ctx.linkHandler.normalize) <= 0 ? a : b)) + .add1("array", (a, ctx) => DefaultFunctions.min(ctx, ...a)) + .vararg((ctx, ...args) => (args.length == 0 ? null : args.reduce((p, c) => DefaultFunctions.min(ctx, p, c)))) + .build(); + DefaultFunctions.max = new FunctionBuilder("max") + .add2("*", "null", (a, _n) => a) + .add2("null", "*", (_n, a) => a) + .add2("*", "*", (a, b, ctx) => (Values.compareValue(a, b, ctx.linkHandler.normalize) > 0 ? a : b)) + .add1("array", (a, ctx) => DefaultFunctions.max(ctx, ...a)) + .vararg((ctx, ...args) => (args.length == 0 ? null : args.reduce((p, c) => DefaultFunctions.max(ctx, p, c)))) + .build(); + DefaultFunctions.minby = new FunctionBuilder("minby") + .add2("array", "function", (arr, func, ctx) => { + if (arr.length == 0) + return null; + let values = arr.map(v => { + return { value: v, mapped: func(ctx, v) }; }); - - executeTwoCallbacks(promise, callback, callback); - return promise; + let filtered = values.filter(v => !Values.isNull(v.mapped)); + if (filtered.length == 0) + return arr[0]; + return filtered.reduce((p, c) => { + if (Values.compareValue(p.mapped, c.mapped, ctx.linkHandler.normalize) <= 0) + return p; + else + return c; + }).value; + }) + .add2("null", "function", (_arr, _func, _ctx) => null) + .build(); + DefaultFunctions.maxby = new FunctionBuilder("maxby") + .add2("array", "function", (arr, func, ctx) => { + if (arr.length == 0) + return null; + let values = arr.map(v => { + return { value: v, mapped: func(ctx, v) }; + }); + let filtered = values.filter(v => !Values.isNull(v.mapped)); + if (filtered.length == 0) + return arr[0]; + return filtered.reduce((p, c) => { + if (Values.compareValue(p.mapped, c.mapped, ctx.linkHandler.normalize) > 0) + return p; + else + return c; + }).value; + }) + .add2("null", "function", (_arr, _func, _ctx) => null) + .build(); + DefaultFunctions.striptime = new FunctionBuilder("striptime") + .add1("date", d => DateTime.fromObject({ year: d.year, month: d.month, day: d.day })) + .add1("null", _n => null) + .vectorize(1, [0]) + .build(); + // Default contains, which looks through data structures recursively. + DefaultFunctions.contains = new FunctionBuilder("contains") + .add2("array", "*", (l, elem, context) => l.some(e => DefaultFunctions.contains(context, e, elem))) + .add2("string", "string", (haystack, needle) => haystack.includes(needle)) + .add2("object", "string", (obj, key) => key in obj) + .add2("*", "*", (elem1, elem2, context) => context.evaluate(Fields.binaryOp(Fields.literal(elem1), "=", Fields.literal(elem2))).orElseThrow()) + .vectorize(2, [1]) + .build(); + // Case insensitive version of contains. + DefaultFunctions.icontains = new FunctionBuilder("icontains") + .add2("array", "*", (l, elem, context) => l.some(e => DefaultFunctions.icontains(context, e, elem))) + .add2("string", "string", (haystack, needle) => haystack.toLocaleLowerCase().includes(needle.toLocaleLowerCase())) + .add2("object", "string", (obj, key) => key in obj) + .add2("*", "*", (elem1, elem2, context) => context.evaluate(Fields.binaryOp(Fields.literal(elem1), "=", Fields.literal(elem2))).orElseThrow()) + .vectorize(2, [1]) + .build(); + // "exact" contains, does not look recursively. + DefaultFunctions.econtains = new FunctionBuilder("econtains") + .add2("array", "*", (l, elem, context) => l.some(e => context.evaluate(Fields.binaryOp(Fields.literal(elem), "=", Fields.literal(e))).orElseThrow())) + .add2("string", "string", (haystack, needle) => haystack.includes(needle)) + .add2("object", "string", (obj, key) => key in obj) + .add2("*", "*", (elem1, elem2, context) => context.evaluate(Fields.binaryOp(Fields.literal(elem1), "=", Fields.literal(elem2))).orElseThrow()) + .vectorize(2, [1]) + .build(); + // Case insensitive contains which looks for exact word matches (i.e., boundry-to-boundry match). + DefaultFunctions.containsword = new FunctionBuilder("containsword") + .add2("string", "string", (hay, needle) => !!hay.match(new RegExp(".*\\b" + escapeRegex(needle) + "\\b.*", "i"))) + .add2("null", "*", (_a, _b) => null) + .add2("*", "null", (_a, _b) => null) + .vectorize(2, [0, 1]) + .build(); + /** Extract 0 or more keys from a given object via indexing. */ + DefaultFunctions.extract = (context, ...args) => { + if (args.length == 0) + return "extract(object, key1, ...) requires at least 1 argument"; + // Manually handle vectorization in the first argument. + let object = args[0]; + if (Values.isArray(object)) + return object.map(v => DefaultFunctions.extract(context, v, ...args.slice(1))); + let result = {}; + for (let index = 1; index < args.length; index++) { + let key = args[index]; + if (!Values.isString(key)) + throw Error("extract(object, key1, ...) must be called with string keys"); + result[key] = context.evaluate(Fields.index(Fields.literal(object), Fields.literal(key))).orElseThrow(); + } + return result; }; - - LocalForage.prototype.setDriver = function setDriver(drivers, callback, errorCallback) { - var self = this; - - if (!isArray(drivers)) { - drivers = [drivers]; + // Reverse an array or string. + DefaultFunctions.reverse = new FunctionBuilder("reverse") + .add1("array", l => { + let result = []; + for (let index = l.length - 1; index >= 0; index--) + result.push(l[index]); + return result; + }) + .add1("string", l => { + let result = ""; + for (let c = 0; c < l.length; c++) + result += l[l.length - c - 1]; + return result; + }) + .add1("*", e => e) + .build(); + // Sort an array; if given two arguments, sorts by the key returned. + DefaultFunctions.sort = new FunctionBuilder("sort") + .add1("array", (list, context) => DefaultFunctions.sort(context, list, (_ctx, a) => a)) + .add2("array", "function", (list, key, context) => { + let result = [].concat(list); + result.sort((a, b) => { + let akey = key(context, a); + let bkey = key(context, b); + let le = context + .evaluate(Fields.binaryOp(Fields.literal(akey), "<", Fields.literal(bkey))) + .orElseThrow(); + if (Values.isTruthy(le)) + return -1; + let eq = context + .evaluate(Fields.binaryOp(Fields.literal(akey), "=", Fields.literal(bkey))) + .orElseThrow(); + if (Values.isTruthy(eq)) + return 0; + return 1; + }); + return result; + }) + .add1("*", e => e) + .build(); + DefaultFunctions.regextest = new FunctionBuilder("regextest") + .add2("string", "string", (pattern, field) => RegExp(pattern).test(field)) + .add2("null", "*", (_n, _a) => false) + .add2("*", "null", (_a, _n) => false) + .vectorize(2, [0, 1]) + .build(); + DefaultFunctions.regexmatch = new FunctionBuilder("regexmatch") + .add2("string", "string", (pattern, field) => { + if (!pattern.startsWith("^") && !pattern.endsWith("$")) + pattern = "^" + pattern + "$"; + return !!field.match(pattern); + }) + .add2("null", "*", (_n, _a) => false) + .add2("*", "null", (_a, _n) => false) + .vectorize(2, [0, 1]) + .build(); + DefaultFunctions.regexreplace = new FunctionBuilder("regexreplace") + .add3("string", "string", "string", (field, pat, rep) => { + try { + let reg = new RegExp(pat, "g"); + return field.replace(reg, rep); } - - var supportedDrivers = this._getSupportedDrivers(drivers); - - function setDriverToConfig() { - self._config.driver = self.driver(); + catch (ex) { + throw Error(`Invalid regexp '${pat}' in regexreplace`); } - - function extendSelfWithDriver(driver) { - self._extend(driver); - setDriverToConfig(); - - self._ready = self._initStorage(self._config); - return self._ready; + }) + .add3("null", "*", "*", () => null) + .add3("*", "null", "*", () => null) + .add3("*", "*", "null", () => null) + .vectorize(3, [0, 1, 2]) + .build(); + DefaultFunctions.lower = new FunctionBuilder("lower") + .add1("string", s => s.toLocaleLowerCase()) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + DefaultFunctions.upper = new FunctionBuilder("upper") + .add1("string", s => s.toLocaleUpperCase()) + .add1("null", () => null) + .vectorize(1, [0]) + .build(); + DefaultFunctions.replace = new FunctionBuilder("replace") + .add3("string", "string", "string", (str, pat, repr) => str.split(pat).join(repr)) + .add3("null", "*", "*", () => null) + .add3("*", "null", "*", () => null) + .add3("*", "*", "null", () => null) + .vectorize(3, [0, 1, 2]) + .build(); + // Ensure undefined matches turn into empty strings for split/2 and split/3. + const splitImpl = (str, delim, limit) => str.split(new RegExp(delim), limit).map(str => str || ""); + /** Split a string on a given string. */ + DefaultFunctions.split = new FunctionBuilder("split") + .add2("string", "string", (string, splitter) => splitImpl(string, splitter)) + .add3("string", "string", "number", (string, splitter, limit) => splitImpl(string, splitter, limit)) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .add3("*", "*", "null", () => null) + .add3("*", "null", "*", () => null) + .add3("null", "*", "*", () => null) + .build(); + DefaultFunctions.startswith = new FunctionBuilder("startswith") + .add2("string", "string", (str, starting) => str.startsWith(starting)) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .vectorize(2, [0, 1]) + .build(); + DefaultFunctions.endswith = new FunctionBuilder("endswith") + .add2("string", "string", (str, ending) => str.endsWith(ending)) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .vectorize(2, [0, 1]) + .build(); + DefaultFunctions.padleft = new FunctionBuilder("padleft") + .add2("string", "number", (str, len) => str.padStart(len, " ")) + .add3("string", "number", "string", (str, len, padding) => str.padStart(len, padding)) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .add3("null", "*", "*", () => null) + .add3("*", "null", "*", () => null) + .add3("*", "*", "null", () => null) + .vectorize(2, [0, 1]) + .vectorize(3, [0, 1, 2]) + .build(); + DefaultFunctions.padright = new FunctionBuilder("padright") + .add2("string", "number", (str, len) => str.padEnd(len, " ")) + .add3("string", "number", "string", (str, len, padding) => str.padEnd(len, padding)) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .add3("null", "*", "*", () => null) + .add3("*", "null", "*", () => null) + .add3("*", "*", "null", () => null) + .vectorize(2, [0, 1]) + .vectorize(3, [0, 1, 2]) + .build(); + DefaultFunctions.substring = new FunctionBuilder("substring") + .add2("string", "number", (str, start) => str.substring(start)) + .add3("string", "number", "number", (str, start, end) => str.substring(start, end)) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .add3("null", "*", "*", () => null) + .add3("*", "null", "*", () => null) + .add3("*", "*", "null", () => null) + .vectorize(2, [0, 1]) + .vectorize(3, [0, 1, 2]) + .build(); + DefaultFunctions.truncate = new FunctionBuilder("truncate") + .add3("string", "number", "string", (str, length, suffix) => { + if (str.length > length - suffix.length) { + return str.substring(0, Math.max(0, length - suffix.length)) + suffix; } + else { + return str; + } + }) + .add2("string", "number", (str, length, ctx) => DefaultFunctions.truncate(ctx, str, length, "...")) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .add3("null", "*", "*", () => null) + .add3("*", "null", "*", () => null) + .add3("*", "*", "null", () => null) + .vectorize(2, [0, 1]) + .vectorize(3, [0, 1, 2]) + .build(); + DefaultFunctions.fdefault = new FunctionBuilder("default") + .add2("*", "*", (v, bk) => (Values.isNull(v) ? bk : v)) + .vectorize(2, [0, 1]) + .build(); + DefaultFunctions.ldefault = new FunctionBuilder("ldefault") + .add2("*", "*", (v, bk) => (Values.isNull(v) ? bk : v)) + .build(); + DefaultFunctions.choice = new FunctionBuilder("choice") + .add3("*", "*", "*", (b, left, right) => (Values.isTruthy(b) ? left : right)) + .vectorize(3, [0]) + .build(); + DefaultFunctions.reduce = new FunctionBuilder("reduce") + .add2("array", "string", (lis, op, context) => { + if (lis.length == 0) + return null; + if (op != "+" && op != "-" && op != "*" && op != "/" && op != "&" && op != "|") + throw Error("reduce(array, op) supports '+', '-', '/', '*', '&', and '|'"); + let value = lis[0]; + for (let index = 1; index < lis.length; index++) { + value = context + .evaluate(Fields.binaryOp(Fields.literal(value), op, Fields.literal(lis[index]))) + .orElseThrow(); + } + return value; + }) + .add2("array", "function", (lis, op, context) => { + if (lis.length == 0) + return null; + let value = lis[0]; + for (let index = 1; index < lis.length; index++) { + // Skip null values to reduce the pain of summing over fields that may or may not exist. + if (Values.isNull(lis[index])) + continue; + value = op(context, value, lis[index]); + } + return value; + }) + .add2("null", "*", () => null) + .add2("*", "null", () => null) + .vectorize(2, [1]) + .build(); + DefaultFunctions.sum = new FunctionBuilder("sum") + .add1("array", (arr, c) => DefaultFunctions.reduce(c, arr, "+")) + .add1("*", e => e) + .build(); + DefaultFunctions.average = new FunctionBuilder("average") + .add1("array", (array, context) => { + if (array.length == 0) + return null; + const add = DefaultFunctions.sum(context, array); + if (add == null || add == undefined) + return null; + return context + .evaluate(Fields.binaryOp(Fields.literal(add), "/", Fields.literal(array.length))) + .orElseThrow(); + }) + .add1("*", e => e) + .build(); + DefaultFunctions.product = new FunctionBuilder("product") + .add1("array", (arr, c) => DefaultFunctions.reduce(c, arr, "*")) + .add1("*", e => e) + .build(); + DefaultFunctions.join = new FunctionBuilder("join") + .add2("array", "string", (arr, sep, ctx) => arr.map(e => Values.toString(e, ctx.settings)).join(sep)) + .add2("array", "null", (arr, _s, context) => DefaultFunctions.join(context, arr, ", ")) + .add2("*", "string", (elem, sep, ctx) => Values.toString(elem, ctx.settings)) + .add1("array", (arr, context) => DefaultFunctions.join(context, arr, ", ")) + .add1("*", (e, ctx) => Values.toString(e, ctx.settings)) + .vectorize(2, [1]) + .build(); + DefaultFunctions.any = new FunctionBuilder("any") + .add1("array", arr => arr.some(v => Values.isTruthy(v))) + .add2("array", "function", (arr, f, ctx) => arr.some(v => Values.isTruthy(f(ctx, v)))) + .vararg((_ctx, ...args) => args.some(v => Values.isTruthy(v))) + .build(); + DefaultFunctions.all = new FunctionBuilder("all") + .add1("array", arr => arr.every(v => Values.isTruthy(v))) + .add2("array", "function", (arr, f, ctx) => arr.every(v => Values.isTruthy(f(ctx, v)))) + .vararg((_ctx, ...args) => args.every(v => Values.isTruthy(v))) + .build(); + DefaultFunctions.none = new FunctionBuilder("all") + .add1("array", arr => !arr.some(v => Values.isTruthy(v))) + .add2("array", "function", (arr, f, ctx) => !arr.some(v => Values.isTruthy(f(ctx, v)))) + .vararg((_ctx, ...args) => !args.some(v => Values.isTruthy(v))) + .build(); + DefaultFunctions.filter = new FunctionBuilder("filter") + .add2("array", "function", (arr, f, ctx) => arr.filter(v => Values.isTruthy(f(ctx, v)))) + .add2("null", "*", () => null) + .build(); + DefaultFunctions.unique = new FunctionBuilder("unique") + .add1("array", (arr, ctx) => DataArray.wrap(arr, ctx.settings).distinct().array()) + .add1("null", () => null) + .build(); + DefaultFunctions.map = new FunctionBuilder("map") + .add2("array", "function", (arr, f, ctx) => arr.map(v => f(ctx, v))) + .add2("null", "*", () => null) + .build(); + DefaultFunctions.nonnull = new FunctionBuilder("nonnull") + .add1("array", arr => arr.filter(v => Values.typeOf(v) != "null")) + .vararg((_ctx, ...args) => args.filter(v => Values.typeOf(v) != "null")) + .build(); + /** Gets an object containing a link's own properties */ + DefaultFunctions.meta = new FunctionBuilder("meta") + .add1("link", link => { + var _c, _d; + return ({ + display: (_c = link.display) !== null && _c !== void 0 ? _c : null, + embed: link.embed, + path: link.path, + subpath: (_d = link.subpath) !== null && _d !== void 0 ? _d : null, + type: link.type, + }); + }) + .build(); + // Concatenates sub-array elements into a new array + DefaultFunctions.flat = new FunctionBuilder("flat") + .add1("array", a => { + return a.flat(); + }) + .add2("array", "number", (a, n) => { + // @ts-ignore + return a.flat(n); + }) + .add1("null", () => null) + .build(); +})(DefaultFunctions || (DefaultFunctions = {})); +/** Default function implementations for the expression evaluator. */ +const DEFAULT_FUNCTIONS = { + // Constructors. + list: DefaultFunctions.list, + array: DefaultFunctions.list, + link: DefaultFunctions.link, + embed: DefaultFunctions.embed, + elink: DefaultFunctions.elink, + date: DefaultFunctions.date, + dur: DefaultFunctions.dur, + dateformat: DefaultFunctions.dateformat, + durationformat: DefaultFunctions.durationformat, + localtime: DefaultFunctions.localtime, + number: DefaultFunctions.number, + currencyformat: DefaultFunctions.currencyformat, + string: DefaultFunctions.string, + object: DefaultFunctions.object, + typeof: DefaultFunctions.typeOf, + // Math Operations. + round: DefaultFunctions.round, + trunc: DefaultFunctions.trunc, + floor: DefaultFunctions.floor, + ceil: DefaultFunctions.ceil, + min: DefaultFunctions.min, + max: DefaultFunctions.max, + minby: DefaultFunctions.minby, + maxby: DefaultFunctions.maxby, + // String operations. + regexreplace: DefaultFunctions.regexreplace, + regextest: DefaultFunctions.regextest, + regexmatch: DefaultFunctions.regexmatch, + replace: DefaultFunctions.replace, + lower: DefaultFunctions.lower, + upper: DefaultFunctions.upper, + split: DefaultFunctions.split, + startswith: DefaultFunctions.startswith, + endswith: DefaultFunctions.endswith, + padleft: DefaultFunctions.padleft, + padright: DefaultFunctions.padright, + substring: DefaultFunctions.substring, + truncate: DefaultFunctions.truncate, + // Date Operations. + striptime: DefaultFunctions.striptime, + // List operations. + length: DefaultFunctions.length, + contains: DefaultFunctions.contains, + icontains: DefaultFunctions.icontains, + econtains: DefaultFunctions.econtains, + containsword: DefaultFunctions.containsword, + reverse: DefaultFunctions.reverse, + sort: DefaultFunctions.sort, + flat: DefaultFunctions.flat, + // Aggregation operations like reduce. + reduce: DefaultFunctions.reduce, + join: DefaultFunctions.join, + sum: DefaultFunctions.sum, + product: DefaultFunctions.product, + average: DefaultFunctions.average, + all: DefaultFunctions.all, + any: DefaultFunctions.any, + none: DefaultFunctions.none, + filter: DefaultFunctions.filter, + unique: DefaultFunctions.unique, + map: DefaultFunctions.map, + nonnull: DefaultFunctions.nonnull, + // Object/Utility operations. + extract: DefaultFunctions.extract, + default: DefaultFunctions.fdefault, + ldefault: DefaultFunctions.ldefault, + choice: DefaultFunctions.choice, + meta: DefaultFunctions.meta, +}; - function initDriver(supportedDrivers) { - return function () { - var currentDriverIndex = 0; - - function driverPromiseLoop() { - while (currentDriverIndex < supportedDrivers.length) { - var driverName = supportedDrivers[currentDriverIndex]; - currentDriverIndex++; - - self._dbInfo = null; - self._ready = null; - - return self.getDriver(driverName).then(extendSelfWithDriver)["catch"](driverPromiseLoop); +/** Provides a global dispatch table for evaluating binary operators, including comparison. */ +/** Provides implementations for binary operators on two types using a registry. */ +class BinaryOpHandler { + static create() { + return new BinaryOpHandler(); + } + constructor() { + this.map = new Map(); + } + register(left, op, right, func) { + this.map.set(BinaryOpHandler.repr(op, left, right), func); + return this; + } + registerComm(left, op, right, func) { + return this.register(left, op, right, func).register(right, op, left, (a, b, ctx) => func(b, a, ctx)); + } + /** Implement a comparison function. */ + compare(type, compare) { + return this.register(type, "<", type, (a, b, ctx) => compare(a, b, ctx) < 0) + .register(type, "<=", type, (a, b, ctx) => compare(a, b, ctx) <= 0) + .register(type, ">", type, (a, b, ctx) => compare(a, b, ctx) > 0) + .register(type, ">=", type, (a, b, ctx) => compare(a, b, ctx) >= 0) + .register(type, "=", type, (a, b, ctx) => compare(a, b, ctx) == 0) + .register(type, "!=", type, (a, b, ctx) => compare(a, b, ctx) != 0); + } + /** Attempt to evaluate the given binary operator on the two literal fields. */ + evaluate(op, left, right, ctx) { + let leftType = Values.typeOf(left); + let rightType = Values.typeOf(right); + if (!leftType) + return Result.failure(`Unrecognized value '${left}'`); + else if (!rightType) + return Result.failure(`Unrecognized value '${right}'`); + let handler = this.map.get(BinaryOpHandler.repr(op, leftType, rightType)); + if (handler) + return Result.success(handler(left, right, ctx)); + // Right-'*' fallback: + let handler2 = this.map.get(BinaryOpHandler.repr(op, leftType, "*")); + if (handler2) + return Result.success(handler2(left, right, ctx)); + // Left-'*' fallback: + let handler3 = this.map.get(BinaryOpHandler.repr(op, "*", rightType)); + if (handler3) + return Result.success(handler3(left, right, ctx)); + // Double '*' fallback. + let handler4 = this.map.get(BinaryOpHandler.repr(op, "*", "*")); + if (handler4) + return Result.success(handler4(left, right, ctx)); + return Result.failure(`No implementation found for '${leftType} ${op} ${rightType}'`); + } + /** Create a string representation of the given triplet for unique lookup in the map. */ + static repr(op, left, right) { + return `${left},${op},${right}`; + } +} +/** Configure and create a binary OP handler with the given parameters. */ +function createBinaryOps(linkNormalizer) { + return (BinaryOpHandler.create() + // TODO: Consider not using a universal comparison function. + .compare("*", (a, b) => Values.compareValue(a, b, linkNormalizer)) + // Global boolean operations. + .register("*", "&", "*", (a, b) => Values.isTruthy(a) && Values.isTruthy(b)) + .register("*", "|", "*", (a, b) => Values.isTruthy(a) || Values.isTruthy(b)) + // Number implementations. + .register("number", "+", "number", (a, b) => a + b) + .register("number", "-", "number", (a, b) => a - b) + .register("number", "*", "number", (a, b) => a * b) + .register("number", "/", "number", (a, b) => a / b) + .register("number", "%", "number", (a, b) => a % b) + // String implementations. + .register("string", "+", "*", (a, b, ctx) => a + Values.toString(b, ctx.settings)) + .register("*", "+", "string", (a, b, ctx) => Values.toString(a, ctx.settings) + b) + .registerComm("string", "*", "number", (a, b) => (b < 0 ? "" : a.repeat(b))) + // Date Operations. + .register("date", "-", "date", (a, b) => { + return normalizeDuration(a.diff(b, ["years", "months", "days", "hours", "minutes", "seconds", "milliseconds"])); + }) + .register("date", "-", "duration", (a, b) => a.minus(b)) + .registerComm("date", "+", "duration", (a, b) => a.plus(b)) + // Duration Operations. + .register("duration", "+", "duration", (a, b) => normalizeDuration(a.plus(b))) + .register("duration", "-", "duration", (a, b) => normalizeDuration(a.minus(b))) + .register("duration", "/", "number", (a, b) => normalizeDuration(a.mapUnits(x => x / b))) + .registerComm("duration", "*", "number", (a, b) => normalizeDuration(a.mapUnits(x => x * b))) + // Array operations. + .register("array", "+", "array", (a, b) => [].concat(a).concat(b)) + // Object operations. + .register("object", "+", "object", (a, b) => Object.assign({}, a, b)) + // Null handling operators. + .register("null", "+", "null", (_a, _b) => null) + .register("null", "-", "null", (_a, _b) => null) + .register("null", "*", "null", (_a, _b) => null) + .register("null", "/", "null", (_a, _b) => null) + .register("null", "%", "null", (_a, _b) => null) + .register("date", "+", "null", (_a, _b) => null) + .register("null", "+", "date", (_a, _b) => null) + .register("date", "-", "null", (_a, _b) => null) + .register("null", "-", "date", (_a, _b) => null)); +} + +/** Core implementation of the query language evaluation engine. */ +/** + * Evaluation context that expressions can be evaluated in. Includes global state, as well as available functions and a handler + * for binary operators. + */ +class Context { + /** + * Create a new context with the given namespace of globals, as well as optionally with custom binary operator, function, + * and link handlers. + */ + constructor(linkHandler, settings, globals = {}, binaryOps = createBinaryOps(linkHandler.normalize), functions = DEFAULT_FUNCTIONS) { + this.linkHandler = linkHandler; + this.settings = settings; + this.globals = globals; + this.binaryOps = binaryOps; + this.functions = functions; + } + /** Set a global value in this context. */ + set(name, value) { + this.globals[name] = value; + return this; + } + /** Get the value of a global variable by name. Returns null if not present. */ + get(name) { + var _a; + return (_a = this.globals[name]) !== null && _a !== void 0 ? _a : null; + } + /** Try to evaluate an arbitrary field in this context, raising an exception on failure. */ + tryEvaluate(field, data = {}) { + return this.evaluate(field, data).orElseThrow(); + } + /** Evaluate an arbitrary field in this context. */ + evaluate(field, data = {}) { + var _a, _b; + switch (field.type) { + case "literal": + return Result.success(field.value); + case "variable": + if (field.name in data) + return Result.success(data[field.name]); + else if (field.name in this.globals) + return Result.success(this.globals[field.name]); + else + return Result.success(null); + case "negated": + return this.evaluate(field.child, data).map(s => !Values.isTruthy(s)); + case "binaryop": + return Result.flatMap2(this.evaluate(field.left, data), this.evaluate(field.right, data), (a, b) => this.binaryOps.evaluate(field.op, a, b, this)); + case "list": + let result = []; + for (let child of field.values) { + let subeval = this.evaluate(child, data); + if (!subeval.successful) + return subeval; + result.push(subeval.value); + } + return Result.success(result); + case "object": + let objResult = {}; + for (let [key, child] of Object.entries(field.values)) { + let subeval = this.evaluate(child, data); + if (!subeval.successful) + return subeval; + objResult[key] = subeval.value; + } + return Result.success(objResult); + case "lambda": + // Just relying on JS to capture 'data' for us implicitly; unsure + // if this is correct thing to do. Could cause weird behaviors. + return Result.success((ctx, ...args) => { + let copy = Object.assign({}, data); + for (let arg = 0; arg < Math.min(args.length, field.arguments.length); arg++) { + copy[field.arguments[arg]] = args[arg]; } - - setDriverToConfig(); - var error = new Error('No available storage method found.'); - self._driverSet = Promise$1.reject(error); - return self._driverSet; + return ctx.evaluate(field.value, copy).orElseThrow(); + }); + case "function": + let rawFunc = field.func.type == "variable" + ? Result.success(field.func.name) + : this.evaluate(field.func, data); + if (!rawFunc.successful) + return rawFunc; + let func = rawFunc.value; + let args = []; + for (let arg of field.arguments) { + let resolved = this.evaluate(arg, data); + if (!resolved.successful) + return resolved; + args.push(resolved.value); + } + let call; + if (Values.isFunction(func)) + call = func; + else if (Values.isString(func) && func in this.functions) + call = this.functions[func]; + else if (Values.isString(func)) + return Result.failure(`Unrecognized function name '${func}'`); + else + return Result.failure(`Cannot call type '${Values.typeOf(func)}' as a function`); + try { + return Result.success(call(this, ...args)); + } + catch (e) { + return Result.failure(e.message); + } + case "index": + // TODO: Will move this out to an 'primitives' module and add more content to it. + let literalIndex = this.evaluate(field.index, data); + let checkedIndex = literalIndex.flatMap(s => Values.isString(s) || Values.isNumber(s) || Values.isNull(s) + ? Result.success(s) + : Result.failure("Can only index with a string or number")); + if (!checkedIndex.successful) + return checkedIndex; + let index = checkedIndex.value; + if (Values.isNull(index)) + return Result.success(null); + let checkedObject = field.object.type == "variable" && field.object.name == "row" + ? Result.success(Object.assign({}, this.globals, data)) + : this.evaluate(field.object, data); + if (!checkedObject.successful) + return checkedObject; + let object = Values.wrapValue(checkedObject.value); + if (!object) + return Result.failure("Unrecognized object to index into: " + object); + switch (object.type) { + case "object": + if (!Values.isString(index)) + return Result.failure('can only index into objects with strings (a.b or a["b"])'); + return Result.success((_a = object.value[index]) !== null && _a !== void 0 ? _a : null); + case "link": + if (!Values.isString(index)) + return Result.failure('can only index into links with strings (a.b or a["b"])'); + let linkValue = this.linkHandler.resolve(object.value.path); + if (Values.isNull(linkValue)) + return Result.success(null); + return Result.success((_b = linkValue[index]) !== null && _b !== void 0 ? _b : null); + case "array": + if (Values.isNumber(index)) { + if (index >= object.value.length || index < 0) + return Result.success(null); + else + return Result.success(object.value[index]); + } + else if (Values.isString(index)) { + let result = []; + for (let value of object.value) { + let next = this.evaluate(Fields.index(Fields.literal(value), Fields.literal(index))); + if (!next.successful) + continue; + result.push(next.value); + } + return Result.success(result); + } + else { + return Result.failure("Array indexing requires either a number (to get a specific element), or a string (to map all elements inside the array)"); + } + case "string": + if (!Values.isNumber(index)) + return Result.failure("string indexing requires a numeric index (string[index])"); + if (index >= object.value.length || index < 0) + return Result.success(null); + return Result.success(object.value[index]); + case "date": + if (!Values.isString(index)) + return Result.failure("date indexing requires a string representing the unit"); + switch (index) { + case "year": + return Result.success(object.value.year); + case "month": + return Result.success(object.value.month); + case "weekyear": + return Result.success(object.value.weekNumber); + case "week": + return Result.success(Math.floor(object.value.day / 7) + 1); + case "weekday": + return Result.success(object.value.weekday); + case "day": + return Result.success(object.value.day); + case "hour": + return Result.success(object.value.hour); + case "minute": + return Result.success(object.value.minute); + case "second": + return Result.success(object.value.second); + case "millisecond": + return Result.success(object.value.millisecond); + default: + return Result.success(null); + } + case "duration": + if (!Values.isString(index)) + return Result.failure("duration indexing requires a string representing the unit"); + switch (index) { + case "year": + case "years": + return Result.success(object.value.shiftTo("years").years); + case "month": + case "months": + return Result.success(object.value.shiftTo("months").months); + case "weeks": + return Result.success(object.value.shiftTo("weeks").weeks); + case "day": + case "days": + return Result.success(object.value.shiftTo("days").days); + case "hour": + case "hours": + return Result.success(object.value.shiftTo("hours").hours); + case "minute": + case "minutes": + return Result.success(object.value.shiftTo("minutes").minutes); + case "second": + case "seconds": + return Result.success(object.value.shiftTo("seconds").seconds); + case "millisecond": + case "milliseconds": + return Result.success(object.value.shiftTo("milliseconds").milliseconds); + default: + return Result.success(null); + } + default: + return Result.success(null); } - - return driverPromiseLoop(); - }; } + } +} - // There might be a driver initialization in progress - // so wait for it to finish in order to avoid a possible - // race condition to set _dbInfo - var oldDriverSetDone = this._driverSet !== null ? this._driverSet["catch"](function () { - return Promise$1.resolve(); - }) : Promise$1.resolve(); - - this._driverSet = oldDriverSetDone.then(function () { - var driverName = supportedDrivers[0]; - self._dbInfo = null; - self._ready = null; - - return self.getDriver(driverName).then(function (driver) { - self._driver = driver._driver; - setDriverToConfig(); - self._wrapLibraryMethodsWithReady(); - self._initDriver = initDriver(supportedDrivers); - }); - })["catch"](function () { - setDriverToConfig(); - var error = new Error('No available storage method found.'); - self._driverSet = Promise$1.reject(error); - return self._driverSet; +function iden(x) { + return x; +} +/** Shared execution code which just takes in arbitrary data, runs operations over it, and returns it + per-row errors. */ +function executeCore(rows, context, ops) { + let diagnostics = []; + let identMeaning = { type: "path" }; + let startTime = Date.now(); + for (let op of ops) { + let opStartTime = Date.now(); + let incomingRows = rows.length; + let errors = []; + switch (op.type) { + case "where": + let whereResult = []; + for (let index = 0; index < rows.length; index++) { + let row = rows[index]; + let value = context.evaluate(op.clause, row.data); + if (!value.successful) + errors.push({ index, message: value.error }); + else if (Values.isTruthy(value.value)) + whereResult.push(row); + } + rows = whereResult; + break; + case "sort": + let sortFields = op.fields; + let taggedData = []; + outer: for (let index = 0; index < rows.length; index++) { + let row = rows[index]; + let rowSorts = []; + for (let sIndex = 0; sIndex < sortFields.length; sIndex++) { + let value = context.evaluate(sortFields[sIndex].field, row.data); + if (!value.successful) { + errors.push({ index, message: value.error }); + continue outer; + } + rowSorts.push(value.value); + } + taggedData.push({ data: row, fields: rowSorts }); + } + // Sort rows by the sort fields, and then return the finished result. + taggedData.sort((a, b) => { + for (let index = 0; index < sortFields.length; index++) { + let factor = sortFields[index].direction === "ascending" ? 1 : -1; + let le = context.binaryOps + .evaluate("<", a.fields[index], b.fields[index], context) + .orElse(false); + if (Values.isTruthy(le)) + return factor * -1; + let ge = context.binaryOps + .evaluate(">", a.fields[index], b.fields[index], context) + .orElse(false); + if (Values.isTruthy(ge)) + return factor * 1; + } + return 0; + }); + rows = taggedData.map(v => v.data); + break; + case "limit": + let limiting = context.evaluate(op.amount); + if (!limiting.successful) + return Result.failure("Failed to execute 'limit' statement: " + limiting.error); + if (!Values.isNumber(limiting.value)) + return Result.failure(`Failed to execute 'limit' statement: limit should be a number, but got '${Values.typeOf(limiting.value)}' (${limiting.value})`); + rows = rows.slice(0, limiting.value); + break; + case "group": + let groupData = []; + for (let index = 0; index < rows.length; index++) { + let value = context.evaluate(op.field.field, rows[index].data); + if (!value.successful) { + errors.push({ index, message: value.error }); + continue; + } + groupData.push({ data: rows[index], key: value.value }); + } + // Sort by the key, which we will group on shortly. + groupData.sort((a, b) => { + let le = context.binaryOps.evaluate("<", a.key, b.key, context).orElse(false); + if (Values.isTruthy(le)) + return -1; + let ge = context.binaryOps.evaluate(">", a.key, b.key, context).orElse(false); + if (Values.isTruthy(ge)) + return 1; + return 0; + }); + // Then walk through and find fields that are equal. + let finalGroupData = []; + if (groupData.length > 0) + finalGroupData.push({ + key: groupData[0].key, + rows: [groupData[0].data.data], + [op.field.name]: groupData[0].key, + }); + for (let index = 1; index < groupData.length; index++) { + let curr = groupData[index], prev = groupData[index - 1]; + if (context.binaryOps.evaluate("=", curr.key, prev.key, context).orElse(false)) { + finalGroupData[finalGroupData.length - 1].rows.push(curr.data.data); + } + else { + finalGroupData.push({ + key: curr.key, + rows: [curr.data.data], + [op.field.name]: curr.key, + }); + } + } + rows = finalGroupData.map(d => { + return { id: d.key, data: d }; + }); + identMeaning = { type: "group", name: op.field.name, on: identMeaning }; + break; + case "flatten": + let flattenResult = []; + for (let index = 0; index < rows.length; index++) { + let row = rows[index]; + let value = context.evaluate(op.field.field, row.data); + if (!value.successful) { + errors.push({ index, message: value.error }); + continue; + } + let datapoints = Values.isArray(value.value) ? value.value : [value.value]; + for (let v of datapoints) { + let copy = Values.deepCopy(row); + copy.data[op.field.name] = v; + flattenResult.push(copy); + } + } + rows = flattenResult; + if (identMeaning.type == "group" && identMeaning.name == op.field.name) + identMeaning = identMeaning.on; + break; + default: + return Result.failure("Unrecognized query operation '" + op.type + "'"); + } + if (errors.length >= incomingRows && incomingRows > 0) { + return Result.failure(`Every row during operation '${op.type}' failed with an error; first ${Math.min(3, errors.length)}:\n + ${errors + .slice(0, 3) + .map(d => "- " + d.message) + .join("\n")}`); + } + diagnostics.push({ + incomingRows, + errors, + outgoingRows: rows.length, + timeMs: Date.now() - opStartTime, }); - - executeTwoCallbacks(this._driverSet, callback, errorCallback); - return this._driverSet; - }; - - LocalForage.prototype.supports = function supports(driverName) { - return !!DriverSupport[driverName]; - }; - - LocalForage.prototype._extend = function _extend(libraryMethodsAndProperties) { - extend(this, libraryMethodsAndProperties); - }; - - LocalForage.prototype._getSupportedDrivers = function _getSupportedDrivers(drivers) { - var supportedDrivers = []; - for (var i = 0, len = drivers.length; i < len; i++) { - var driverName = drivers[i]; - if (this.supports(driverName)) { - supportedDrivers.push(driverName); + } + return Result.success({ + data: rows, + idMeaning: identMeaning, + ops, + diagnostics, + timeMs: Date.now() - startTime, + }); +} +/** Expanded version of executeCore which adds an additional "extraction" step to the pipeline. */ +function executeCoreExtract(rows, context, ops, fields) { + let internal = executeCore(rows, context, ops); + if (!internal.successful) + return internal; + let core = internal.value; + let startTime = Date.now(); + let errors = []; + let res = []; + outer: for (let index = 0; index < core.data.length; index++) { + let page = { id: core.data[index].id, data: {} }; + for (let [name, field] of Object.entries(fields)) { + let value = context.evaluate(field, core.data[index].data); + if (!value.successful) { + errors.push({ index: index, message: value.error }); + continue outer; } + page.data[name] = value.value; } - return supportedDrivers; - }; - - LocalForage.prototype._wrapLibraryMethodsWithReady = function _wrapLibraryMethodsWithReady() { - // Add a stub for each driver API method that delays the call to the - // corresponding driver method until localForage is ready. These stubs - // will be replaced by the driver methods as soon as the driver is - // loaded, so there is no performance impact. - for (var i = 0, len = LibraryMethods.length; i < len; i++) { - callWhenReady(this, LibraryMethods[i]); + res.push(page); + } + if (errors.length >= core.data.length && core.data.length > 0) { + return Result.failure(`Every row during final data extraction failed with an error; first ${Math.max(errors.length, 3)}:\n + ${errors + .slice(0, 3) + .map(d => "- " + d.message) + .join("\n")}`); + } + let execTime = Date.now() - startTime; + return Result.success({ + data: res, + idMeaning: core.idMeaning, + diagnostics: core.diagnostics.concat([ + { + timeMs: execTime, + incomingRows: core.data.length, + outgoingRows: res.length, + errors, + }, + ]), + ops: core.ops.concat([{ type: "extract", fields }]), + timeMs: core.timeMs + execTime, + }); +} +/** Execute a list-based query, returning the final results. */ +async function executeList(query, index, origin, settings) { + var _a, _b; + // Start by collecting all of the files that match the 'from' queries. + let fileset = await resolveSource(query.source, index, origin); + if (!fileset.successful) + return Result.failure(fileset.error); + // Extract information about the origin page to add to the root context. + let rootContext = new Context(defaultLinkHandler(index, origin), settings, { + this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, + }); + let targetField = query.header.format; + let showId = query.header.showId; + let fields = targetField ? { target: targetField } : {}; + return executeCoreExtract(fileset.value, rootContext, query.operations, fields).map(core => { + let data; + if (showId && targetField) { + data = core.data.map(p => { var _a; return Widgets.listPair(p.id, (_a = p.data["target"]) !== null && _a !== void 0 ? _a : null); }); } - }; - - LocalForage.prototype.createInstance = function createInstance(options) { - return new LocalForage(options); - }; - - return LocalForage; -}(); - -// The actual localForage object that we expose as a module or via a -// global. It's extended by pulling in one of our other libraries. - - -var localforage_js = new LocalForage(); - -module.exports = localforage_js; - -},{"3":3}]},{},[4])(4) -}); -}(localforage$1)); - -var localforage = localforage$1.exports; - -/** Simpler wrapper for a file-backed cache for arbitrary metadata. */ -class LocalStorageCache { - constructor(appId, version) { - this.appId = appId; - this.version = version; - this.persister = localforage.createInstance({ - name: "dataview/cache/" + appId, - driver: [localforage.INDEXEDDB], - description: "Cache metadata about files and sections in the dataview index.", - }); - } - /** Drop the entire cache instance and re-create a new fresh instance. */ - async recreate() { - await localforage.dropInstance({ name: "dataview/cache/" + this.appId }); - this.persister = localforage.createInstance({ - name: "dataview/cache/" + this.appId, - driver: [localforage.INDEXEDDB], - description: "Cache metadata about files and sections in the dataview index.", - }); - } - /** Load file metadata by path. */ - async loadFile(path) { - return this.persister.getItem(this.fileKey(path)).then(raw => { - let result = raw; - if (result) - result.data = Transferable.value(result.data); - return result; - }); - } - /** Store file metadata by path. */ - async storeFile(path, data) { - await this.persister.setItem(this.fileKey(path), { - version: this.version, - time: Date.now(), - data: Transferable.transferable(data), - }); - } - /** Drop old file keys that no longer exist. */ - async synchronize(existing) { - let keys = new Set(await this.allFiles()); - for (let exist of existing) - keys.delete(exist); - // Any keys remaining after deleting existing keys are non-existent keys that should be cleared from cache. - for (let key of keys) - await this.persister.removeItem(this.fileKey(key)); - return keys; - } - /** Obtain a list of all metadata keys. */ - async allKeys() { - return this.persister.keys(); - } - /** Obtain a list of all persisted files. */ - async allFiles() { - let keys = await this.allKeys(); - return keys.filter(k => k.startsWith("file:")).map(k => k.substring(5)); - } - fileKey(path) { - return "file:" + path; - } + else if (targetField) { + data = core.data.map(p => { var _a; return (_a = p.data["target"]) !== null && _a !== void 0 ? _a : null; }); + } + else { + data = core.data.map(p => p.id); + } + return { primaryMeaning: core.idMeaning, core, data }; + }); } - -function decodeBase64(base64, enableUnicode) { - var binaryString = atob(base64); - if (enableUnicode) { - var binaryView = new Uint8Array(binaryString.length); - for (var i = 0, n = binaryString.length; i < n; ++i) { - binaryView[i] = binaryString.charCodeAt(i); +/** Execute a table query. */ +async function executeTable(query, index, origin, settings) { + var _a, _b; + // Start by collecting all of the files that match the 'from' queries. + let fileset = await resolveSource(query.source, index, origin); + if (!fileset.successful) + return Result.failure(fileset.error); + // Extract information about the origin page to add to the root context. + let rootContext = new Context(defaultLinkHandler(index, origin), settings, { + this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, + }); + let targetFields = query.header.fields; + let showId = query.header.showId; + let fields = {}; + for (let field of targetFields) + fields[field.name] = field.field; + return executeCoreExtract(fileset.value, rootContext, query.operations, fields).map(core => { + if (showId) { + const idName = core.idMeaning.type === "group" ? core.idMeaning.name : settings.tableIdColumnName; + let names = [idName].concat(targetFields.map(f => f.name)); + let data = core.data.map(p => [p.id].concat(targetFields.map(f => p.data[f.name]))); + return { core, names, data, idMeaning: core.idMeaning }; } - return String.fromCharCode.apply(null, new Uint16Array(binaryView.buffer)); + else { + let names = targetFields.map(f => f.name); + let data = core.data.map(p => targetFields.map(f => p.data[f.name])); + return { core, names, data, idMeaning: core.idMeaning }; + } + }); +} +/** Maps a raw core execution result to a task grouping which is much easier to render. */ +function extractTaskGroupings(id, rows) { + switch (id.type) { + case "path": + return rows; + case "group": + let key = id.name; + return rows.map(r => iden({ + key: r[key], + rows: extractTaskGroupings(id.on, r.rows), + })); } - return binaryString; } - -function createURL(base64, sourcemapArg, enableUnicodeArg) { - var sourcemap = sourcemapArg === undefined ? null : sourcemapArg; - var enableUnicode = enableUnicodeArg === undefined ? false : enableUnicodeArg; - var source = decodeBase64(base64, enableUnicode); - var start = source.indexOf('\n', 10) + 1; - var body = source.substring(start) + (sourcemap ? '\/\/# sourceMappingURL=' + sourcemap : ''); - var blob = new Blob([body], { type: 'application/javascript' }); - return URL.createObjectURL(blob); +/** Execute a task query, returning all matching tasks. */ +async function executeTask(query, origin, index, settings) { + var _a, _b; + let fileset = matchingSourcePaths(query.source, index, origin); + if (!fileset.successful) + return Result.failure(fileset.error); + // Collect tasks from pages which match. + let incomingTasks = []; + for (let path of fileset.value) { + let page = index.pages.get(path); + if (!page) + continue; + let pageData = page.serialize(index); + let pageTasks = pageData.file.tasks.map(t => { + const tcopy = Values.deepCopy(t); + // Add page data to this copy. + for (let [key, value] of Object.entries(pageData)) { + if (key in tcopy) + continue; + tcopy[key] = value; + } + return { id: `${pageData.path}#${t.line}`, data: tcopy }; + }); + for (let task of pageTasks) + incomingTasks.push(task); + } + // Extract information about the origin page to add to the root context. + let rootContext = new Context(defaultLinkHandler(index, origin), settings, { + this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, + }); + return executeCore(incomingTasks, rootContext, query.operations).map(core => { + return { + core, + tasks: extractTaskGroupings(core.idMeaning, core.data.map(r => r.data)), + }; + }); } - -function createBase64WorkerFactory(base64, sourcemapArg, enableUnicodeArg) { - var url; - return function WorkerFactory(options) { - url = url || createURL(base64, sourcemapArg, enableUnicodeArg); - return new Worker(url, options); +/** Execute a single field inline a file, returning the evaluated result. */ +function executeInline(field, origin, index, settings) { + var _a, _b; + return new Context(defaultLinkHandler(index, origin), settings, { + this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, + }).evaluate(field); +} +/** The default link resolver used when creating contexts. */ +function defaultLinkHandler(index, origin) { + return { + resolve: link => { + let realFile = index.metadataCache.getFirstLinkpathDest(link, origin); + if (!realFile) + return null; + let realPage = index.pages.get(realFile.path); + if (!realPage) + return null; + return realPage.serialize(index); + }, + normalize: link => { + var _a; + let realFile = index.metadataCache.getFirstLinkpathDest(link, origin); + return (_a = realFile === null || realFile === void 0 ? void 0 : realFile.path) !== null && _a !== void 0 ? _a : link; + }, + exists: link => { + let realFile = index.metadataCache.getFirstLinkpathDest(link, origin); + return !!realFile; + }, }; } - -var WorkerFactory = createBase64WorkerFactory('Lyogcm9sbHVwLXBsdWdpbi13ZWItd29ya2VyLWxvYWRlciAqLwooZnVuY3Rpb24gKCkgewogICd1c2Ugc3RyaWN0JzsKCiAgLy8gdGhlc2UgYXJlbid0IHJlYWxseSBwcml2YXRlLCBidXQgbm9yIGFyZSB0aGV5IHJlYWxseSB1c2VmdWwgdG8gZG9jdW1lbnQKCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBMdXhvbkVycm9yIGV4dGVuZHMgRXJyb3Ige30KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBJbnZhbGlkRGF0ZVRpbWVFcnJvciBleHRlbmRzIEx1eG9uRXJyb3IgewogICAgY29uc3RydWN0b3IocmVhc29uKSB7CiAgICAgIHN1cGVyKGBJbnZhbGlkIERhdGVUaW1lOiAke3JlYXNvbi50b01lc3NhZ2UoKX1gKTsKICAgIH0KICB9CgogIC8qKgogICAqIEBwcml2YXRlCiAgICovCiAgY2xhc3MgSW52YWxpZEludGVydmFsRXJyb3IgZXh0ZW5kcyBMdXhvbkVycm9yIHsKICAgIGNvbnN0cnVjdG9yKHJlYXNvbikgewogICAgICBzdXBlcihgSW52YWxpZCBJbnRlcnZhbDogJHtyZWFzb24udG9NZXNzYWdlKCl9YCk7CiAgICB9CiAgfQoKICAvKioKICAgKiBAcHJpdmF0ZQogICAqLwogIGNsYXNzIEludmFsaWREdXJhdGlvbkVycm9yIGV4dGVuZHMgTHV4b25FcnJvciB7CiAgICBjb25zdHJ1Y3RvcihyZWFzb24pIHsKICAgICAgc3VwZXIoYEludmFsaWQgRHVyYXRpb246ICR7cmVhc29uLnRvTWVzc2FnZSgpfWApOwogICAgfQogIH0KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBDb25mbGljdGluZ1NwZWNpZmljYXRpb25FcnJvciBleHRlbmRzIEx1eG9uRXJyb3Ige30KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBJbnZhbGlkVW5pdEVycm9yIGV4dGVuZHMgTHV4b25FcnJvciB7CiAgICBjb25zdHJ1Y3Rvcih1bml0KSB7CiAgICAgIHN1cGVyKGBJbnZhbGlkIHVuaXQgJHt1bml0fWApOwogICAgfQogIH0KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBJbnZhbGlkQXJndW1lbnRFcnJvciBleHRlbmRzIEx1eG9uRXJyb3Ige30KCiAgLyoqCiAgICogQHByaXZhdGUKICAgKi8KICBjbGFzcyBab25lSXNBYnN0cmFjdEVycm9yIGV4dGVuZHMgTHV4b25FcnJvciB7CiAgICBjb25zdHJ1Y3RvcigpIHsKICAgICAgc3VwZXIoIlpvbmUgaXMgYW4gYWJzdHJhY3QgY2xhc3MiKTsKICAgIH0KICB9CgogIC8qKgogICAqIEBwcml2YXRlCiAgICovCgogIGNvbnN0IG4gPSAibnVtZXJpYyIsCiAgICBzID0gInNob3J0IiwKICAgIGwgPSAibG9uZyI7CgogIGNvbnN0IERBVEVfU0hPUlQgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IG4sCiAgICBkYXk6IG4sCiAgfTsKCiAgY29uc3QgREFURV9NRUQgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IHMsCiAgICBkYXk6IG4sCiAgfTsKCiAgY29uc3QgREFURV9NRURfV0lUSF9XRUVLREFZID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBzLAogICAgZGF5OiBuLAogICAgd2Vla2RheTogcywKICB9OwoKICBjb25zdCBEQVRFX0ZVTEwgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IGwsCiAgICBkYXk6IG4sCiAgfTsKCiAgY29uc3QgREFURV9IVUdFID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBsLAogICAgZGF5OiBuLAogICAgd2Vla2RheTogbCwKICB9OwoKICBjb25zdCBUSU1FX1NJTVBMRSA9IHsKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgfTsKCiAgY29uc3QgVElNRV9XSVRIX1NFQ09ORFMgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogIH07CgogIGNvbnN0IFRJTUVfV0lUSF9TSE9SVF9PRkZTRVQgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogICAgdGltZVpvbmVOYW1lOiBzLAogIH07CgogIGNvbnN0IFRJTUVfV0lUSF9MT05HX09GRlNFVCA9IHsKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgICBzZWNvbmQ6IG4sCiAgICB0aW1lWm9uZU5hbWU6IGwsCiAgfTsKCiAgY29uc3QgVElNRV8yNF9TSU1QTEUgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgaG91ckN5Y2xlOiAiaDIzIiwKICB9OwoKICBjb25zdCBUSU1FXzI0X1dJVEhfU0VDT05EUyA9IHsKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgICBzZWNvbmQ6IG4sCiAgICBob3VyQ3ljbGU6ICJoMjMiLAogIH07CgogIGNvbnN0IFRJTUVfMjRfV0lUSF9TSE9SVF9PRkZTRVQgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogICAgaG91ckN5Y2xlOiAiaDIzIiwKICAgIHRpbWVab25lTmFtZTogcywKICB9OwoKICBjb25zdCBUSU1FXzI0X1dJVEhfTE9OR19PRkZTRVQgPSB7CiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogICAgaG91ckN5Y2xlOiAiaDIzIiwKICAgIHRpbWVab25lTmFtZTogbCwKICB9OwoKICBjb25zdCBEQVRFVElNRV9TSE9SVCA9IHsKICAgIHllYXI6IG4sCiAgICBtb250aDogbiwKICAgIGRheTogbiwKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgfTsKCiAgY29uc3QgREFURVRJTUVfU0hPUlRfV0lUSF9TRUNPTkRTID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBuLAogICAgZGF5OiBuLAogICAgaG91cjogbiwKICAgIG1pbnV0ZTogbiwKICAgIHNlY29uZDogbiwKICB9OwoKICBjb25zdCBEQVRFVElNRV9NRUQgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IHMsCiAgICBkYXk6IG4sCiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogIH07CgogIGNvbnN0IERBVEVUSU1FX01FRF9XSVRIX1NFQ09ORFMgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IHMsCiAgICBkYXk6IG4sCiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgc2Vjb25kOiBuLAogIH07CgogIGNvbnN0IERBVEVUSU1FX01FRF9XSVRIX1dFRUtEQVkgPSB7CiAgICB5ZWFyOiBuLAogICAgbW9udGg6IHMsCiAgICBkYXk6IG4sCiAgICB3ZWVrZGF5OiBzLAogICAgaG91cjogbiwKICAgIG1pbnV0ZTogbiwKICB9OwoKICBjb25zdCBEQVRFVElNRV9GVUxMID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBsLAogICAgZGF5OiBuLAogICAgaG91cjogbiwKICAgIG1pbnV0ZTogbiwKICAgIHRpbWVab25lTmFtZTogcywKICB9OwoKICBjb25zdCBEQVRFVElNRV9GVUxMX1dJVEhfU0VDT05EUyA9IHsKICAgIHllYXI6IG4sCiAgICBtb250aDogbCwKICAgIGRheTogbiwKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgICBzZWNvbmQ6IG4sCiAgICB0aW1lWm9uZU5hbWU6IHMsCiAgfTsKCiAgY29uc3QgREFURVRJTUVfSFVHRSA9IHsKICAgIHllYXI6IG4sCiAgICBtb250aDogbCwKICAgIGRheTogbiwKICAgIHdlZWtkYXk6IGwsCiAgICBob3VyOiBuLAogICAgbWludXRlOiBuLAogICAgdGltZVpvbmVOYW1lOiBsLAogIH07CgogIGNvbnN0IERBVEVUSU1FX0hVR0VfV0lUSF9TRUNPTkRTID0gewogICAgeWVhcjogbiwKICAgIG1vbnRoOiBsLAogICAgZGF5OiBuLAogICAgd2Vla2RheTogbCwKICAgIGhvdXI6IG4sCiAgICBtaW51dGU6IG4sCiAgICBzZWNvbmQ6IG4sCiAgICB0aW1lWm9uZU5hbWU6IGwsCiAgfTsKCiAgLyoqCiAgICogQGludGVyZmFjZQogICAqLwogIGNsYXNzIFpvbmUgewogICAgLyoqCiAgICAgKiBUaGUgdHlwZSBvZiB6b25lCiAgICAgKiBAYWJzdHJhY3QKICAgICAqIEB0eXBlIHtzdHJpbmd9CiAgICAgKi8KICAgIGdldCB0eXBlKCkgewogICAgICB0aHJvdyBuZXcgWm9uZUlzQWJzdHJhY3RFcnJvcigpOwogICAgfQoKICAgIC8qKgogICAgICogVGhlIG5hbWUgb2YgdGhpcyB6b25lLgogICAgICogQGFic3RyYWN0CiAgICAgKiBAdHlwZSB7c3RyaW5nfQogICAgICovCiAgICBnZXQgbmFtZSgpIHsKICAgICAgdGhyb3cgbmV3IFp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gdG8gY29tcGFyZQogICAgICogQHJldHVybiB7Ym9vbGVhbn0KICAgICAqLwogICAgZXF1YWxzKG90aGVyWm9uZSkgewogICAgICB0aHJvdyBuZXcgWm9uZUlzQWJzdHJhY3RFcnJvcigpOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJuIHdoZXRoZXIgdGhpcyBab25lIGlzIHZhbGlkLgogICAgICogQGFic3RyYWN0CiAgICAgKiBAdHlwZSB7Ym9vbGVhbn0KICAgICAqLwogICAgZ2V0IGlzVmFsaWQoKSB7CiAgICAgIHRocm93IG5ldyBab25lSXNBYnN0cmFjdEVycm9yKCk7CiAgICB9CiAgfQoKICBsZXQgc2luZ2xldG9uJDEgPSBudWxsOwoKICAvKioKICAgKiBSZXByZXNlbnRzIHRoZSBsb2NhbCB6b25lIGZvciB0aGlzIEphdmFTY3JpcHQgZW52aXJvbm1lbnQuCiAgICogQGltcGxlbWVudHMge1pvbmV9CiAgICovCiAgY2xhc3MgU3lzdGVtWm9uZSBleHRlbmRzIFpvbmUgewogICAgLyoqCiAgICAgKiBHZXQgYSBzaW5nbGV0b24gaW5zdGFuY2Ugb2YgdGhlIGxvY2FsIHpvbmUKICAgICAqIEByZXR1cm4ge1N5c3RlbVpvbmV9CiAgICAgKi8KICAgIHN0YXRpYyBnZXQgaW5zdGFuY2UoKSB7CiAgICAgIGlmIChzaW5nbGV0b24kMSA9PT0gbnVsbCkgewogICAgICAgIHNpbmdsZXRvbiQxID0gbmV3IFN5c3RlbVpvbmUoKTsKICAgICAgfQogICAgICByZXR1cm4gc2luZ2xldG9uJDE7CiAgICB9CgogICAgLyoqIEBvdmVycmlkZSAqKi8KICAgIGdldCB0eXBlKCkgewogICAgICByZXR1cm4gInN5c3RlbSI7CiAgICB9CgogICAgLyoqIEBvdmVycmlkZSAqKi8KICAgIGdldCBuYW1lKCkgewogICAgICByZXR1cm4gbmV3IEludGwuRGF0ZVRpbWVGb3JtYXQoKS5yZXNvbHZlZE9wdGlvbnMoKS50aW1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YWxlLnRvTG93ZXJDYXNlKCkgPT09ICJlbi11cyIgfHwKICAgICAgICBuZXcgSW50bC5EYXRlVGltZUZvcm1hdCh0aGlzLmludGwpLnJlc29sdmVkT3B0aW9ucygpLmxvY2FsZS5zdGFydHNXaXRoKCJlbi11cyIpCiAgICAgICk7CiAgICB9CgogICAgZXF1YWxzKG90aGVyKSB7CiAgICAgIHJldHVybiAoCiAgICAgICAgdGhpcy5sb2NhbGUgPT09IG90aGVyLmxvY2FsZSAmJgogICAgICAgIHRoaXMubnVtYmVyaW5nU3lzdGVtID09PSBvdGhlci5udW1iZXJpbmdTeXN0ZW0gJiYKICAgICAgICB0aGlzLm91dHB1dENhbGVuZGFyID09PSBvdGhlci5vdXRwdXRDYWxlbmRhcgogICAgICApOwogICAgfQogIH0KCiAgbGV0IHNpbmdsZXRvbiA9IG51bGw7CgogIC8qKgogICAqIEEgem9uZSB3aXRoIGEgZml4ZWQgb2Zmc2V0IChtZWFuaW5nIG5vIERTVCkKICAgKiBAaW1wbGVtZW50cyB7Wm9uZX0KICAgKi8KICBjbGFzcyBGaXhlZE9mZnNldFpvbmUgZXh0ZW5kcyBab25lIHsKICAgIC8qKgogICAgICogR2V0IGEgc2luZ2xldG9uIGluc3RhbmNlIG9mIFVUQwogICAgICogQHJldHVybiB7Rml4ZWRPZmZzZXRab25lfQogICAgICovCiAgICBzdGF0aWMgZ2V0IHV0Y0luc3RhbmNlKCkgewogICAgICBpZiAoc2luZ2xldG9uID09PSBudWxsKSB7CiAgICAgICAgc2luZ2xldG9uID0gbmV3IEZpeGVkT2Zmc2V0Wm9uZSgwKTsKICAgICAgfQogICAgICByZXR1cm4gc2luZ2xldG9uOwogICAgfQoKICAgIC8qKgogICAgICogR2V0IGFuIGluc3RhbmNlIHdpdGggYSBzcGVjaWZpZWQgb2Zmc2V0CiAgICAgKiBAcGFyYW0ge251bWJlcn0gb2Zmc2V0IC0gVGhlIG9mZnNldCBpbiBtaW51dGVzCiAgICAgKiBAcmV0dXJ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IiLAogIF07CgogIGNvbnN0IG1vbnRoc1Nob3J0ID0gWwogICAgIkphbiIsCiAgICAiRmViIiwKICAgICJNYXIiLAogICAgIkFwciIsCiAgICAiTWF5IiwKICAgICJKdW4iLAogICAgIkp1bCIsCiAgICAiQXVnIiwKICAgICJTZXAiLAogICAgIk9jdCIsCiAgICAiTm92IiwKICAgICJEZWMiLAogIF07CgogIGNvbnN0IG1vbnRoc05hcnJvdyA9IFsiSiIsICJGIiwgIk0iLCAiQSIsICJNIiwgIkoiLCAiSiIsICJBIiwgIlMiLCAiTyIsICJOIiwgIkQiXTsKCiAgZnVuY3Rpb24gbW9udGhzKGxlbmd0aCkgewogICAgc3dpdGNoIChsZW5ndGgpIHsKICAgICAgY2FzZSAibmFycm93IjoKICAgICAgICByZXR1cm4gWy4uLm1vbnRoc05hcnJvd107CiAgICAgIGNhc2UgInNob3J0IjoKICAgICAgICByZXR1cm4gWy4uLm1vbnRoc1Nob3J0XTsKICAgICAgY2FzZSAibG9uZyI6CiAgICAgICAgcmV0dXJuIFsuLi5tb250aHNMb25nXTsKICAgICAgY2FzZSAibnVtZXJpYyI6CiAgICAgICAgcmV0dXJuIFsiMSIsICIyIiwgIjMiLCAiNCIsICI1IiwgIjYiLCAiNyIsICI4IiwgIjkiLCAiMTAiLCAiMTEiLCAiMTIiXTsKICAgICAgY2FzZSAiMi1kaWdpdCI6CiAgICAgICAgcmV0dXJuIFsiMDEiLCAiMDIiLCAiMDMiLCAiMDQiLCAiMDUiLCAiMDYiLCAiMDciLCAiMDgiLCAiMDkiLCAiMTAiLCAiMTEiLCAiMTIiXTsKICAgICAgZGVmYXVsdDoKICAgICAgICByZXR1cm4gbnVsbDsKICAgIH0KICB9CgogIGNvbnN0IHdlZWtkYXlzTG9uZyA9IFsKICAgICJNb25kYXkiLAogICAgIlR1ZXNkYXkiLAogICAgIldlZG5lc2RheSIsCiAgICAiVGh1cnNkYXkiLAogICAgIkZyaWRheSIsCiAgICAiU2F0dXJkYXkiLAogICAgIlN1bmRheSIsCiAgXTsKCiAgY29uc3Qgd2Vla2RheXNTaG9ydCA9IFsiTW9uIiwgIlR1ZSIsICJXZWQiLCAiVGh1IiwgIkZyaSIsICJTYXQiLCAiU3VuIl07CgogIGNvbnN0IHdlZWtkYXlzTmFycm93ID0gWyJNIiwgIlQiLCAiVyIsICJUIiwgIkYiLCAiUyIsICJTIl07CgogIGZ1bmN0aW9uIHdlZWtkYXlzKGxlbmd0aCkgewogICAgc3dpdGNoIChsZW5ndGgpIHsKICAgICAgY2FzZSAibmFycm93IjoKICAgICAgICByZXR1cm4gWy4uLndlZWtkYXlzTmFycm93XTsKICAgICAgY2FzZSAic2hvcnQiOgogICAgICAgIHJldHVybiBbLi4ud2Vla2RheXNTaG9ydF07CiAgICAgIGNhc2UgImxvbmciOgogICAgICAgIHJldHVybiBbLi4ud2Vla2RheXNMb25nXTsKICAgICAgY2FzZSAibnVtZXJpYyI6CiAgICAgICAgcmV0dXJuIFsiMSIsICIyIiwgIjMiLCAiNCIsICI1IiwgIjYiLCAiNyJdOwogICAgICBkZWZhdWx0OgogICAgICAgIHJldHVybiBudWxsOwogICAgfQogIH0KCiAgY29uc3QgbWVyaWRpZW1zID0gWyJBTSIsICJQTSJdOwoKICBjb25zdCBlcmFzTG9uZyA9IFsiQmVmb3JlIENocmlzdCIsICJBbm5vIERvbWluaSJdOwoKICBjb25zdCBlcmFzU2hvcnQgPSBbIkJDIiwgIkFEIl07CgogIGNvbnN0IGVyYXNOYXJyb3cgPSBbIkIiLCAiQSJdOwoKICBmdW5jdGlvbiBlcmFzKGxlbmd0aCkgewogICAgc3dpdGNoIChsZW5ndGgpIHsKICAgICAgY2FzZSAibmFycm93IjoKICAgICAgICByZXR1cm4gWy4uLmVyYXNOYXJyb3ddOwogICAgICBjYXNlICJzaG9ydCI6CiAgICAgICAgcmV0dXJuIFsuLi5lcmFzU2hvcnRdOwogICAgICBjYXNlICJsb25nIjoKICAgICAgICByZXR1cm4gWy4uLmVyYXNMb25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BsZXQgcyA9ICIiOwogICAgZm9yIChjb25zdCB0b2tlbiBvZiBzcGxpdHMpIHsKICAgICAgaWYgKHRva2VuLmxpdGVyYWwpIHsKICAgICAgICBzICs9IHRva2VuLnZhbDsKICAgICAgfSBlbHNlIHsKICAgICAgICBzICs9IHRva2VuVG9TdHJpbmcodG9rZW4udmFsKTsKICAgICAgfQogICAgfQogICAgcmV0dXJuIHM7CiAgfQoKICBjb25zdCBtYWNyb1Rva2VuVG9Gb3JtYXRPcHRzID0gewogICAgRDogREFURV9TSE9SVCwKICAgIEREOiBEQVRFX01FRCwKICAgIERERDogREFURV9GVUxMLAogICAgRERERDogREFURV9IVUdFLAogICAgdDogVElNRV9TSU1QTEUsCiAgICB0dDogVElNRV9XSVRIX1NFQ09ORFMsCiAgICB0dHQ6IFRJTUVfV0lUSF9TSE9SVF9PRkZTRVQsCiAgICB0dHR0OiBUSU1FX1dJVEhfTE9OR19PRkZTRVQsCiAgICBUOiBUSU1FXzI0X1NJTVBMRSwKICAgIFRUOiBUSU1FXzI0X1dJVEhfU0VDT05EUywKICAgIFRUVDogVElNRV8yNF9XSVRIX1NIT1JUX09GRlNFVCwKICAgIFRUVFQ6IFRJTUVfMjRfV0lUSF9MT05HX09GRlNFVCwKICAgIGY6IERBVEVUSU1FX1NIT1JULAogICAgZmY6IERBVEVUSU1FX01FRCwKICAgIGZmZjogREFURVRJTUVfRlVMTCwKICAgIGZmZmY6IERBVEVUSU1FX0hVR0UsCiAgICBGOiBEQVRFVElNRV9TSE9SVF9XSVRIX1NFQ09ORFMsCiAgICBGRjogREFURVRJTUVfTUVEX1dJVEhfU0VDT05EUywKICAgIEZGRjogREFURVRJTUVfRlVMTF9XSVRIX1NFQ09ORFMsCiAgICBGRkZGOiBEQVRFVElNRV9IVUdFX1dJVEhfU0VDT05EUywKICB9OwoKICAvKioKICAgKiBAcHJpdmF0ZQogICAqLwoKICBjbGFzcyBGb3JtYXR0ZXIgewogICAgc3RhdGljIGNyZWF0ZShsb2NhbGUsIG9wdHMgPSB7fSkgewogICAgICByZXR1cm4gbmV3IEZvcm1hdHRlcihsb2NhbGUsIG9wdHMpOwogICAgfQoKICAgIHN0YXRpYyBwYXJzZUZvcm1hdChmbXQpIHsKICAgICAgbGV0IGN1cnJlbnQgPSBudWxsLAogICAgICAgIGN1cnJlbnRGdWxsID0gIiIsCiAgICAgICAgYnJhY2tldGVkID0gZmFsc2U7CiAgICAgIGNvbnN0IHNwbGl0cyA9IFtdOwogICAgICBmb3IgKGxldCBpID0gMDsgaSA8IGZtdC5sZW5ndGg7IGkrKykgewogICAgICAgIGNvbnN0IGMgPSBmbXQuY2hhckF0KGkpOwogICAgICAgIGlmIChjID09PSAiJyIpIHsKICAgICAgICAgIGlmIChjdXJyZW50RnVsbC5sZW5ndGggPiAwKSB7CiAgICAgICAgICAgIHNwbGl0cy5wdXNoKHsgbGl0ZXJhbDogYnJhY2tldGVkLCB2YWw6IGN1cnJlbnRGdWxsIH0pOwogICAgICAgICAgfQogICAgICAgICAgY3VycmVudCA9IG51bGw7CiAgICAgICAgICBjdXJyZW50RnVsbCA9ICIiOwogICAgICAgICAgYnJhY2tldGVkID0gIWJyYWNrZXRlZDsKICAgICAgICB9IGVsc2UgaWYgKGJyYWNrZXRlZCkgewogICAgICAgICAgY3VycmVudEZ1bGwgKz0gYzsKICAgICAgICB9IGVsc2UgaWYgKGMgPT09IGN1cnJlbnQpIHsKICAgICAgICAgIGN1cnJlbnRGdWxsICs9IGM7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGlmIChjdXJyZW50RnVsbC5sZW5ndGggPiAwKSB7CiAgICAgICAgICAgIHNwbGl0cy5wdXNoKHsgbGl0ZXJhbDogZmFsc2UsIHZhbDogY3VycmVudEZ1bGwgfSk7CiAgICAgICAgICB9CiAgICAgICAgICBjdXJyZW50RnVsbCA9IGM7CiAgICAgICAgICBjdXJyZW50ID0gYzsKICAgICAgICB9CiAgICAgIH0KCiAgICAgIGlmIChjdXJyZW50RnVs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CkgPT09ICJlbiIsCiAgICAgICAgdXNlRGF0ZVRpbWVGb3JtYXR0ZXIgPSB0aGlzLmxvYy5vdXRwdXRDYWxlbmRhciAmJiB0aGlzLmxvYy5vdXRwdXRDYWxlbmRhciAhPT0gImdyZWdvcnkiLAogICAgICAgIHN0cmluZyA9IChvcHRzLCBleHRyYWN0KSA9PiB0aGlzLmxvYy5leHRyYWN0KGR0LCBvcHRzLCBleHRyYWN0KSwKICAgICAgICBmb3JtYXRPZmZzZXQgPSAob3B0cykgPT4gewogICAgICAgICAgaWYgKGR0LmlzT2Zmc2V0Rml4ZWQgJiYgZHQub2Zmc2V0ID09PSAwICYmIG9wdHMuYWxsb3daKSB7CiAgICAgICAgICAgIHJldHVybiAiWiI7CiAgICAgICAgICB9CgogICAgICAgICAgcmV0dXJuIGR0LmlzVmFsaWQgPyBkdC56b25lLmZvcm1hdE9mZnNldChkdC50cywgb3B0cy5mb3JtYXQpIDogIiI7CiAgICAgICAgfSwKICAgICAgICBtZXJpZGllbSA9ICgpID0+CiAgICAgICAgICBrbm93bkVuZ2xpc2gKICAgICAgICAgICAgPyBtZXJpZGllbUZvckRhdGVUaW1lKGR0KQogICAgICAgICAgICA6IHN0cmluZyh7IGhvdXI6ICJudW1lcmljIiwgaG91ckN5Y2xlOiAiaDEyIiB9LCAiZGF5cGVyaW9kIiksCiAgICAgICAgbW9udGggPSAobGVuZ3RoLCBzdGFuZGFsb25lKSA9PgogICAgICAgICAga25vd25FbmdsaXNoCiAgICAgICAgICAgID8gbW9udGhGb3JEYXRlVGltZShkdCwgbGVuZ3RoKQogICAgICAgICAgICA6IHN0cmluZyhzdGFuZGFsb25lID8geyBtb250aDogbGVuZ3RoIH0gOiB7IG1vbnRoOiBsZW5ndGgsIGRheTogIm51bWVyaWMiIH0sICJtb250aCIpLAogICAgICAgIHdlZWtkYXkgPSAobGVuZ3RoLCBzdGFuZGFsb25lKSA9PgogICAgICAgICAga25vd25FbmdsaXNoCiAgICAgICAgICAgID8gd2Vla2RheUZvckRhdGVUaW1lKGR0LCBsZW5ndGgpCiAgICAgICAgICAgIDogc3RyaW5nKAogICAgICAgICAgICAgICAgc3RhbmRhbG9uZSA/IHsgd2Vla2RheTogbGVuZ3RoIH0gOiB7IHdlZWtkYXk6IGxlbmd0aCwgbW9udGg6ICJsb25nIiwgZGF5OiAibnVtZXJpYyIgfSwKICAgICAgICAgICAgICAgICJ3ZWVrZGF5IgogICAgICAgICAgICAgICksCiAgICAgICAgbWF5YmVNYWNybyA9ICh0b2tlbikgPT4gewogICAgICAgICAgY29uc3QgZm9ybWF0T3B0cyA9IEZvcm1hdHRlci5tYWNyb1Rva2VuVG9Gb3JtYXRPcHRzKHRva2VuKTsKICAgICAgICAgIGlmIChmb3JtYXRPcHRzKSB7CiAgICAgICAgICAgIHJldHVybiB0aGlzLmZvcm1hdFdpdGhTeXN0ZW1EZWZhdWx0KGR0LCBmb3JtYXRPcHRzKTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldHVybiB0b2tlbjsKICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgIGVyYSA9IChsZW5ndGgpID0+CiAgICAgICAgICBrbm93bkVuZ2xpc2ggPyBlcmFGb3JEYXRlVGltZShkdCwgbGVuZ3RoKSA6IHN0cmluZyh7IGVyYTogbGVuZ3RoIH0sICJlcmEiKSwKICAgICAgICB0b2tlblRvU3RyaW5nID0gKHRva2VuKSA9PiB7CiAgICAgICAgICAvLyBXaGVyZSBwb3NzaWJsZTogaHR0cDovL2NsZHIudW5pY29kZS5vcmcvdHJhbnNsYXRpb24vZGF0ZS10aW1lLTEvZGF0ZS10aW1lI1RPQy1TdGFuZGFsb25lLXZzLi1Gb3JtYXQtU3R5bGVzCiAgICAgICAgICBzd2l0Y2ggKHRva2VuKSB7CiAgICAgICAgICAgIC8vIG1zCiAgICAgICAgICAgIGNhc2UgIlMiOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShkdC5taWxsaXNlY29uZCk7CiAgICAgICAgICAgIGNhc2UgInUiOgogICAgICAgICAgICAvLyBmYWxscyB0aHJvdWdoCiAgICAgICAgICAgIGNhc2UgIlNTUyI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0Lm1pbGxpc2Vjb25kLCAzKTsKICAgICAgICAgICAgLy8gc2Vjb25kcwogICAgICAgICAgICBjYXNlICJzIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQuc2Vjb25kKTsKICAgICAgICAgICAgY2FzZSAic3MiOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShkdC5zZWNvbmQsIDIpOwogICAgICAgICAgICAvLyBmcmFjdGlvbmFsIHNlY29uZHMKICAgICAgICAgICAgY2FzZSAidXUiOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShNYXRoLmZsb29yKGR0Lm1pbGxpc2Vjb25kIC8gMTApLCAyKTsKICAgICAgICAgICAgY2FzZSAidXV1IjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oTWF0aC5mbG9vcihkdC5taWxsaXNlY29uZCAvIDEwMCkpOwogICAgICAgICAgICAvLyBtaW51dGVzCiAgICAgICAgICAgIGNhc2UgIm0iOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShkdC5taW51dGUpOwogICAgICAgICAgICBjYXNlICJtbSI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0Lm1pbnV0ZSwgMik7CiAgICAgICAgICAgIC8vIGhvdXJzCiAgICAgICAgICAgIGNhc2UgImgiOgogICAgICAgICAgICAgIHJldHVybiB0aGlzLm51bShkdC5ob3VyICUgMTIgPT09IDAgPyAxMiA6IGR0LmhvdXIgJSAxMik7CiAgICAgICAgICAgIGNhc2UgImhoIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQuaG91ciAlIDEyID09PSAwID8gMTIgOiBkdC5ob3VyICUgMTIsIDIpOwogICAgICAgICAgICBjYXNlICJIIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQuaG91cik7CiAgICAgICAgICAgIGNhc2UgIkhIIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQuaG91ciwgMik7CiAgICAgICAgICAgIC8vIG9mZnNldAogICAgICAgICAgICBjYXNlICJaIjoKICAgICAgICAgICAgICAvLyBsaWtlICs2CiAgICAgICAgICAgICAgcmV0dXJuIGZvcm1hdE9mZnNldCh7IGZvcm1hdDogIm5hcnJvdyIsIGFsbG93WjogdGhpcy5vcHRzLmFsbG93WiB9KTsKICAgICAgICAgICAgY2FzZSAiWloiOgogICAgICAgICAgICAgIC8vIGxpa2UgKzA2OjAwCiAgICAgICAgICAgICAgcmV0dXJuIGZvcm1hdE9mZnNldCh7IGZvcm1hdDogInNob3J0IiwgYWxsb3daOiB0aGlzLm9wdHMuYWxsb3daIH0pOwogICAgICAgICAgICBjYXNlICJaWloiOgogICAgICAgICAgICAgIC8vIGxpa2UgKzA2MDAKICAgICAgICAgICAgICByZXR1cm4gZm9ybWF0T2Zmc2V0KHsgZm9ybWF0OiAidGVjaGllIiwgYWxsb3daOiB0aGlzLm9wdHMuYWxsb3daIH0pOwogICAgICAgICAgICBjYXNlICJaWlpaIjoKICAgICAgICAgICAgICAvLyBsaWtlIEVTVAogICAgICAgICAgICAgIHJldHVybiBkdC56b25lLm9mZnNldE5hbWUoZHQudHMsIHsgZm9ybWF0OiAic2hvcnQiLCBsb2NhbGU6IHRoaXMubG9jLmxvY2FsZSB9KTsKICAgICAgICAgICAgY2FzZSAiWlpaWloiOgogICAgICAgICAgICAgIC8vIGxpa2UgRWFzdGVybiBTdGFuZGFyZCBUaW1lCiAgICAgICAgICAgICAgcmV0dXJuIGR0LnpvbmUub2Zmc2V0TmFtZShkdC50cywgeyBmb3JtYXQ6ICJsb25nIiwgbG9jYWxlOiB0aGlzLmxvYy5sb2NhbGUgfSk7CiAgICAgICAgICAgIC8vIHpvbmUKICAgICAgICAgICAgY2FzZSAieiI6CiAgICAgICAgICAgICAgLy8gbGlrZSBBbWVyaWNhL05ld19Zb3JrCiAgICAgICAgICAgICAgcmV0dXJuIGR0LnpvbmVOYW1lOwogICAgICAgICAgICAvLyBtZXJpZGllbXMKICAgICAgICAgICAgY2FzZSAiYSI6CiAgICAgICAgICAgICAgcmV0dXJuIG1lcmlkaWVtKCk7CiAgICAgICAgICAgIC8vIGRhdGVzCiAgICAgICAgICAgIGNhc2UgImQiOgogICAgICAgICAgICAgIHJldHVybiB1c2VEYXRlVGltZUZvcm1hdHRlciA/IHN0cmluZyh7IGRheTogIm51bWVyaWMiIH0sICJkYXkiKSA6IHRoaXMubnVtKGR0LmRheSk7CiAgICAgICAgICAgIGNhc2UgImRkIjoKICAgICAgICAgICAgICByZXR1cm4gdXNlRGF0ZVRpbWVGb3JtYXR0ZXIgPyBzdHJpbmcoeyBkYXk6ICIyLWRpZ2l0IiB9LCAiZGF5IikgOiB0aGlzLm51bShkdC5kYXksIDIpOwogICAgICAgICAgICAvLyB3ZWVrZGF5cyAtIHN0YW5kYWxvbmUKICAgICAgICAgICAgY2FzZSAiYyI6CiAgICAgICAgICAgICAgLy8gbGlrZSAxCiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtkYXkpOwogICAgICAgICAgICBjYXNlICJjY2MiOgogICAgICAgICAgICAgIC8vIGxpa2UgJ1R1ZXMnCiAgICAgICAgICAgICAgcmV0dXJuIHdlZWtkYXkoInNob3J0IiwgdHJ1ZSk7CiAgICAgICAgICAgIGNhc2UgImNjY2MiOgogICAgICAgICAgICAgIC8vIGxpa2UgJ1R1ZXNkYXknCiAgICAgICAgICAgICAgcmV0dXJuIHdlZWtkYXkoImxvbmciLCB0cnVlKTsKICAgICAgICAgICAgY2FzZSAiY2NjY2MiOgogICAgICAgICAgICAgIC8vIGxpa2UgJ1QnCiAgICAgICAgICAgICAgcmV0dXJuIHdlZWtkYXkoIm5hcnJvdyIsIHRydWUpOwogICAgICAgICAgICAvLyB3ZWVrZGF5cyAtIGZvcm1hdAogICAgICAgICAgICBjYXNlICJFIjoKICAgICAgICAgICAgICAvLyBsaWtlIDEKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQud2Vla2RheSk7CiAgICAgICAgICAgIGNhc2UgIkVFRSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAnVHVlcycKICAgICAgICAgICAgICByZXR1cm4gd2Vla2RheSgic2hvcnQiLCBmYWxzZSk7CiAgICAgICAgICAgIGNhc2UgIkVFRUUiOgogICAgICAgICAgICAgIC8vIGxpa2UgJ1R1ZXNkYXknCiAgICAgICAgICAgICAgcmV0dXJuIHdlZWtkYXkoImxvbmciLCBmYWxzZSk7CiAgICAgICAgICAgIGNhc2UgIkVFRUVFIjoKICAgICAgICAgICAgICAvLyBsaWtlICdUJwogICAgICAgICAgICAgIHJldHVybiB3ZWVrZGF5KCJuYXJyb3ciLCBmYWxzZSk7CiAgICAgICAgICAgIC8vIG1vbnRocyAtIHN0YW5kYWxvbmUKICAgICAgICAgICAgY2FzZSAiTCI6CiAgICAgICAgICAgICAgLy8gbGlrZSAxCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IG1vbnRoOiAibnVtZXJpYyIsIGRheTogIm51bWVyaWMiIH0sICJtb250aCIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0Lm1vbnRoKTsKICAgICAgICAgICAgY2FzZSAiTEwiOgogICAgICAgICAgICAgIC8vIGxpa2UgMDEsIGRvZXNuJ3Qgc2VlbSB0byB3b3JrCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IG1vbnRoOiAiMi1kaWdpdCIsIGRheTogIm51bWVyaWMiIH0sICJtb250aCIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0Lm1vbnRoLCAyKTsKICAgICAgICAgICAgY2FzZSAiTExMIjoKICAgICAgICAgICAgICAvLyBsaWtlIEphbgogICAgICAgICAgICAgIHJldHVybiBtb250aCgic2hvcnQiLCB0cnVlKTsKICAgICAgICAgICAgY2FzZSAiTExMTCI6CiAgICAgICAgICAgICAgLy8gbGlrZSBKYW51YXJ5CiAgICAgICAgICAgICAgcmV0dXJuIG1vbnRoKCJsb25nIiwgdHJ1ZSk7CiAgICAgICAgICAgIGNhc2UgIkxMTExMIjoKICAgICAgICAgICAgICAvLyBsaWtlIEoKICAgICAgICAgICAgICByZXR1cm4gbW9udGgoIm5hcnJvdyIsIHRydWUpOwogICAgICAgICAgICAvLyBtb250aHMgLSBmb3JtYXQKICAgICAgICAgICAgY2FzZSAiTSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAxCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IG1vbnRoOiAibnVtZXJpYyIgfSwgIm1vbnRoIikKICAgICAgICAgICAgICAgIDogdGhpcy5udW0oZHQubW9udGgpOwogICAgICAgICAgICBjYXNlICJNTSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAwMQogICAgICAgICAgICAgIHJldHVybiB1c2VEYXRlVGltZUZvcm1hdHRlcgogICAgICAgICAgICAgICAgPyBzdHJpbmcoeyBtb250aDogIjItZGlnaXQiIH0sICJtb250aCIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0Lm1vbnRoLCAyKTsKICAgICAgICAgICAgY2FzZSAiTU1NIjoKICAgICAgICAgICAgICAvLyBsaWtlIEphbgogICAgICAgICAgICAgIHJldHVybiBtb250aCgic2hvcnQiLCBmYWxzZSk7CiAgICAgICAgICAgIGNhc2UgIk1NTU0iOgogICAgICAgICAgICAgIC8vIGxpa2UgSmFudWFyeQogICAgICAgICAgICAgIHJldHVybiBtb250aCgibG9uZyIsIGZhbHNlKTsKICAgICAgICAgICAgY2FzZSAiTU1NTU0iOgogICAgICAgICAgICAgIC8vIGxpa2UgSgogICAgICAgICAgICAgIHJldHVybiBtb250aCgibmFycm93IiwgZmFsc2UpOwogICAgICAgICAgICAvLyB5ZWFycwogICAgICAgICAgICBjYXNlICJ5IjoKICAgICAgICAgICAgICAvLyBsaWtlIDIwMTQKICAgICAgICAgICAgICByZXR1cm4gdXNlRGF0ZVRpbWVGb3JtYXR0ZXIgPyBzdHJpbmcoeyB5ZWFyOiAibnVtZXJpYyIgfSwgInllYXIiKSA6IHRoaXMubnVtKGR0LnllYXIpOwogICAgICAgICAgICBjYXNlICJ5eSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAxNAogICAgICAgICAgICAgIHJldHVybiB1c2VEYXRlVGltZUZvcm1hdHRlcgogICAgICAgICAgICAgICAgPyBzdHJpbmcoeyB5ZWFyOiAiMi1kaWdpdCIgfSwgInllYXIiKQogICAgICAgICAgICAgICAgOiB0aGlzLm51bShkdC55ZWFyLnRvU3RyaW5nKCkuc2xpY2UoLTIpLCAyKTsKICAgICAgICAgICAgY2FzZSAieXl5eSI6CiAgICAgICAgICAgICAgLy8gbGlrZSAwMDEyCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IHllYXI6ICJudW1lcmljIiB9LCAieWVhciIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0LnllYXIsIDQpOwogICAgICAgICAgICBjYXNlICJ5eXl5eXkiOgogICAgICAgICAgICAgIC8vIGxpa2UgMDAwMDEyCiAgICAgICAgICAgICAgcmV0dXJuIHVzZURhdGVUaW1lRm9ybWF0dGVyCiAgICAgICAgICAgICAgICA/IHN0cmluZyh7IHllYXI6ICJudW1lcmljIiB9LCAieWVhciIpCiAgICAgICAgICAgICAgICA6IHRoaXMubnVtKGR0LnllYXIsIDYpOwogICAgICAgICAgICAvLyBlcmFzCiAgICAgICAgICAgIGNhc2UgIkciOgogICAgICAgICAgICAgIC8vIGxpa2UgQUQKICAgICAgICAgICAgICByZXR1cm4gZXJhKCJzaG9ydCIpOwogICAgICAgICAgICBjYXNlICJHRyI6CiAgICAgICAgICAgICAgLy8gbGlrZSBBbm5vIERvbWluaQogICAgICAgICAgICAgIHJldHVybiBlcmEoImxvbmciKTsKICAgICAgICAgICAgY2FzZSAiR0dHR0ciOgogICAgICAgICAgICAgIHJldHVybiBlcmEoIm5hcnJvdyIpOwogICAgICAgICAgICBjYXNlICJrayI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtZZWFyLnRvU3RyaW5nKCkuc2xpY2UoLTIpLCAyKTsKICAgICAgICAgICAgY2FzZSAia2trayI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtZZWFyLCA0KTsKICAgICAgICAgICAgY2FzZSAiVyI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtOdW1iZXIpOwogICAgICAgICAgICBjYXNlICJXVyI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LndlZWtOdW1iZXIsIDIpOwogICAgICAgICAgICBjYXNlICJvIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQub3JkaW5hbCk7CiAgICAgICAgICAgIGNhc2UgIm9vbyI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0Lm9yZGluYWwsIDMpOwogICAgICAgICAgICBjYXNlICJxIjoKICAgICAgICAgICAgICAvLyBsaWtlIDEKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oZHQucXVhcnRlcik7CiAgICAgICAgICAgIGNhc2UgInFxIjoKICAgICAgICAgICAgICAvLyBsaWtlIDAxCiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LnF1YXJ0ZXIsIDIpOwogICAgICAgICAgICBjYXNlICJYIjoKICAgICAgICAgICAgICByZXR1cm4gdGhpcy5udW0oTWF0aC5mbG9vcihkdC50cyAvIDEwMDApKTsKICAgICAgICAgICAgY2FzZSAieCI6CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGR0LnRzKTsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICByZXR1cm4gbWF5YmVNYWNybyh0b2tlbik7CiAgICAgICAgICB9CiAgICAgICAgfTsKCiAgICAgIHJldHVybiBzdHJpbmdpZnlUb2tlbnMoRm9ybWF0dGVyLnBhcnNlRm9ybWF0KGZtdCksIHRva2VuVG9TdHJpbmcpOwogICAgfQoKICAgIGZvcm1hdER1cmF0aW9uRnJvbVN0cmluZyhkdXIsIGZtdCkgewogICAgICBjb25zdCB0b2tlblRvRmllbGQgPSAodG9rZW4pID0+IHsKICAgICAgICAgIHN3aXRjaCAodG9rZW5bMF0pIHsKICAgICAgICAgICAgY2FzZSAiUyI6CiAgICAgICAgICAgICAgcmV0dXJuICJtaWxsaXNlY29uZCI7CiAgICAgICAgICAgIGNhc2UgInMiOgogICAgICAgICAgICAgIHJldHVybiAic2Vjb25kIjsKICAgICAgICAgICAgY2FzZSAibSI6CiAgICAgICAgICAgICAgcmV0dXJuICJtaW51dGUiOwogICAgICAgICAgICBjYXNlICJoIjoKICAgICAgICAgICAgICByZXR1cm4gImhvdXIiOwogICAgICAgICAgICBjYXNlICJkIjoKICAgICAgICAgICAgICByZXR1cm4gImRheSI7CiAgICAgICAgICAgIGNhc2UgInciOgogICAgICAgICAgICAgIHJldHVybiAid2VlayI7CiAgICAgICAgICAgIGNhc2UgIk0iOgogICAgICAgICAgICAgIHJldHVybiAibW9udGgiOwogICAgICAgICAgICBjYXNlICJ5IjoKICAgICAgICAgICAgICByZXR1cm4gInllYXIiOwogICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgIHJldHVybiBudWxsOwogICAgICAgICAgfQogICAgICAgIH0sCiAgICAgICAgdG9rZW5Ub1N0cmluZyA9IChsaWxkdXIpID0+ICh0b2tlbikgPT4gewogICAgICAgICAgY29uc3QgbWFwcGVkID0gdG9rZW5Ub0ZpZWxkKHRva2VuKTsKICAgICAgICAgIGlmIChtYXBwZWQpIHsKICAgICAgICAgICAgcmV0dXJuIHRoaXMubnVtKGxpbGR1ci5nZXQobWFwcGVkKSwgdG9rZW4ubGVuZ3RoKTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldHVybiB0b2tlbjsKICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgIHRva2VucyA9IEZvcm1hdHRlci5wYXJzZUZvcm1hdChmbXQpLAogICAgICAgIHJlYWxUb2tlbnMgPSB0b2tlbnMucmVkdWNlKAogICAgICAgICAgKGZvdW5kLCB7IGxpdGVyYWwsIHZhbCB9KSA9PiAobGl0ZXJhbCA/IGZvdW5kIDogZm91bmQuY29uY2F0KHZhbCkpLAogICAgICAgICAgW10KICAgICAgICApLAogICAgICAgIGNvbGxhcHNlZCA9IGR1ci5zaGlmdFRvKC4uLnJlYWxUb2tlbnMubWFwKHRva2VuVG9GaWVsZCkuZmlsdGVyKCh0KSA9PiB0KSk7CiAgICAgIHJldHVybiBzdHJpbmdpZnlUb2tlbnModG9rZW5zLCB0b2tlblRvU3RyaW5nKGNvbGxhcHNlZCkpOwogICAgfQogIH0KCiAgY2xhc3MgSW52YWxpZCB7CiAgICBjb25zdHJ1Y3RvcihyZWFzb24sIGV4cGxhbmF0aW9uKSB7CiAgICAgIHRoaXMucmVhc29uID0gcmVhc29uOwogICAgICB0aGlzLmV4cGxhbmF0aW9uID0gZXhwbGFuYXRpb247CiAgICB9CgogICAgdG9NZXNzYWdlKCkgewogICAgICBpZiAodGhpcy5leHBsYW5hdGlvbikgewogICAgICAgIHJldHVybiBgJHt0aGlzLnJlYXNvbn06ICR7dGhpcy5leHBsYW5hdGlvbn1gOwogICAgICB9IGVsc2UgewogICAgICAgIHJldHVybiB0aGlzLnJlYXNvbjsKICAgICAgfQogICAgfQogIH0KCiAgLyoKICAgKiBUaGlzIGZpbGU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ogIH0KCiAgY29uc3QgSU5WQUxJRCQyID0gIkludmFsaWQgRHVyYXRpb24iOwoKICAvLyB1bml0IGNvbnZlcnNpb24gY29uc3RhbnRzCiAgY29uc3QgbG93T3JkZXJNYXRyaXggPSB7CiAgICAgIHdlZWtzOiB7CiAgICAgICAgZGF5czogNywKICAgICAgICBob3VyczogNyAqIDI0LAogICAgICAgIG1pbnV0ZXM6IDcgKiAyNCAqIDYwLAogICAgICAgIHNlY29uZHM6IDcgKiAyNCAqIDYwICogNjAsCiAgICAgICAgbWlsbGlzZWNvbmRzOiA3ICogMjQgKiA2MCAqIDYwICogMTAwMCwKICAgICAgfSwKICAgICAgZGF5czogewogICAgICAgIGhvdXJzOiAyNCwKICAgICAgICBtaW51dGVzOiAyNCAqIDYwLAogICAgICAgIHNlY29uZHM6IDI0ICogNjAgKiA2MCwKICAgICAgICBtaWxsaXNlY29uZHM6IDI0ICogNjAgKiA2MCAqIDEwMDAsCiAgICAgIH0sCiAgICAgIGhvdXJzOiB7IG1pbnV0ZXM6IDYwLCBzZWNvbmRzOiA2MCAqIDYwLCBtaWxsaXNlY29uZHM6IDYwICogNjAgKiAxMDAwIH0sCiAgICAgIG1pbnV0ZXM6IHsgc2Vjb25kczogNjAsIG1pbGxpc2Vjb25kczogNjAgKiAxMDAwIH0sCiAgICAgIHNlY29uZHM6IHsgbWlsbGlzZWNvbmRzOiAxMDAwIH0sCiAgICB9LAogICAgY2FzdWFsTWF0cml4ID0gewogICAgICB5ZWFyczogewogICAgICAgIHF1YXJ0ZXJzOiA0LAogICAgICAgIG1vbnRoczogMTIsCiAgICAgICAgd2Vla3M6IDUyLAogICAgICAgIGRheXM6IDM2NSwKICAgICAgICBob3VyczogMzY1ICogMjQsCiAgICAgICAgbWludXRlczogMzY1ICogMjQgKiA2MCwKICAgICAgICBzZWNvbmRzOiAzNjUgKiAyNCAqIDYwICogNjAsCiAgICAgICAgbWlsbGlzZWNvbmRzOiAzNjUgKiAyNCAqIDYwICogNjAgKiAxMDAwLAogICAgICB9LAogICAgICBxdWFydGVyczogewogICAgICAgIG1vbnRoczogMywKICAgICAgICB3ZWVrczogMTMsCiAgICAgICAgZGF5czogOTEsCiAgICAgICAgaG91cnM6IDkxICogMjQsCiAgICAgICAgbWludXRlczogOTEgKiAyNCAqIDYwLAogICAgICAgIHNlY29uZHM6IDkxICogMjQgKiA2MCAqIDYwLAogICAgICAgIG1pbGxpc2Vjb25kczogOTEgKiAyNCAqIDYwICogNjAgKiAxMDAwLAogICAgICB9LAogICAgICBtb250aHM6IHsKICAgICAgICB3ZWVrczogNCwKICAgICAgICBkYXlzOiAzMCwKICAgICAgICBob3VyczogMzAgKiAyNCwKICAgICAgICBtaW51dGVzOiAzMCAqIDI0ICogNjAsCiAgICAgICAgc2Vjb25kczogMzAgKiAyNCAqIDYwICogNjAsCiAgICAgICAgbWlsbGlzZWNvbmRzOiAzMCAqIDI0ICogNjAgKiA2MCAqIDEwMDAsCiAgICAgIH0sCgogICAgICAuLi5sb3dPcmRlck1hdHJpeCwKICAgIH0sCiAgICBkYXlzSW5ZZWFyQWNjdXJhdGUgPSAxNDYwOTcuMCAvIDQwMCwKICAgIGRheXNJbk1vbnRoQWNjdXJhdGUgPSAxNDYwOTcuMCAvIDQ4MDAsCiAgICBhY2N1cmF0ZU1hdHJpeCA9IHsKICAgICAgeWVhcnM6IHsKICAgICAgICBxdWFydGVyczogNCwKICAgICAgICBtb250aHM6IDEyLAogICAgICAgIHdlZWtzOiBkYXlzSW5ZZWFyQWNjdXJhdGUgLyA3LAogICAgICAgIGRheXM6IGRheXNJblllYXJBY2N1cmF0ZSwKICAgICAgICBob3VyczogZGF5c0luWWVhckFjY3VyYXRlICogMjQsCiAgICAgICAgbWludXRlczogZGF5c0luWWVhckFjY3VyYXRlICogMjQgKiA2MCwKICAgICAgICBzZWNvbmRzOiBkYXlzSW5ZZWFyQWNjdXJhdGUgKiAyNCAqIDYwICogNjAsCiAgICAgICAgbWlsbGlzZWNvbmRzOiBkYXlzSW5ZZWFyQWNjdXJhdGUgKiAyNCAqIDYwICogNjAgKiAxMDAwLAogICAgICB9LAogICAgICBxdWFydGVyczogewogICAgICAgIG1vbnRoczogMywKICAgICAgICB3ZWVrczogZGF5c0luWWVhckFjY3VyYXRlIC8gMjgsCiAgICAgICAgZGF5czogZGF5c0luWWVhckFjY3VyYXRlIC8gNCwKICAgICAgICBob3VyczogKGRheXNJblllYXJBY2N1cmF0ZSAqIDI0KSAvIDQsCiAgICAgICAgbWludXRlczogKGRheXNJblllYXJBY2N1cmF0ZSAqIDI0ICogNjApIC8gNCwKICAgICAgICBzZWNvbmRzOiAoZGF5c0luWWVhckFjY3VyYXRlICogMjQgKiA2MCAqIDYwKSAvIDQsCiAgICAgICAgbWlsbGlzZWNvbmRzOiAoZGF5c0luWWVhckFjY3VyYXRlICogMjQgKiA2MCAqIDYwICogMTAwMCkgLyA0LAogICAgICB9LAogICAgICBtb250aHM6IHsKICAgICAgICB3ZWVrczogZGF5c0luTW9udGhBY2N1cmF0ZSAvIDcsCiAgICAgICAgZGF5czogZGF5c0luTW9udGhBY2N1cmF0ZSwKICAgICAgICBob3VyczogZGF5c0luTW9udGhBY2N1cmF0ZSAqIDI0LAogICAgICAgIG1pbnV0ZXM6IGRheXNJbk1vbnRoQWNjdXJhdGUgKiAyNCAqIDYwLAogICAgICAgIHNlY29uZHM6IGRheXNJbk1vbnRoQWNjdXJhdGUgKiAyNCAqIDYwICogNjAsCiAgICAgICAgbWlsbGlzZWNvbmRzOiBkYXlzSW5Nb250aEFjY3VyYXRlICogMjQgKiA2MCAqIDYwICogMTAwMCwKICAgICAgfSwKICAgICAgLi4ubG93T3JkZXJNYXRyaXgsCiAgICB9OwoKICAvLyB1bml0cyBvcmRlcmVkIGJ5IHNpemUKICBjb25zdCBvcmRlcmVkVW5pdHMkMSA9IFsKICAgICJ5ZWFycyIsCiAgICAicXVhcnRlcnMiLAogICAgIm1vbnRocyIsCiAgICAid2Vla3MiLAogICAgImRheXMiLAogICAg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yb21Vbml0XSAvIGNvbnYsCiAgICAgIHNhbWVTaWduID0gTWF0aC5zaWduKHJhdykgPT09IE1hdGguc2lnbih0b01hcFt0b1VuaXRdKSwKICAgICAgLy8gb2ssIHNvIHRoaXMgaXMgd2lsZCwgYnV0IHNlZSB0aGUgbWF0cml4IGluIHRoZSB0ZXN0cwogICAgICBhZGRlZCA9CiAgICAgICAgIXNhbWVTaWduICYmIHRvTWFwW3RvVW5pdF0gIT09IDAgJiYgTWF0aC5hYnMocmF3KSA8PSAxID8gYW50aVRydW5jKHJhdykgOiBNYXRoLnRydW5jKHJhdyk7CiAgICB0b01hcFt0b1VuaXRdICs9IGFkZGVkOwogICAgZnJvbU1hcFtmcm9tVW5pdF0gLT0gYWRkZWQgKiBjb252OwogIH0KCiAgLy8gTkI6IG11dGF0ZXMgcGFyYW1ldGVycwogIGZ1bmN0aW9uIG5vcm1hbGl6ZVZhbHVlcyhtYXRyaXgsIHZhbHMpIHsKICAgIHJldmVyc2VVbml0cy5yZWR1Y2UoKHByZXZpb3VzLCBjdXJyZW50KSA9PiB7CiAgICAgIGlmICghaXNVbmRlZmluZWQodmFsc1tjdXJyZW50XSkpIHsKICAgICAgICBpZiAocHJldmlvdXMpIHsKICAgICAgICAgIGNvbnZlcnQobWF0cml4LCB2YWxzLCBwcmV2aW91cywgdmFscywgY3VycmVudCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBjdXJyZW50OwogICAgICB9IGVsc2UgewogICAgICAgIHJldHVybiBwcmV2aW91czsKICAgICAgfQogICAgfSwgbnVsbCk7CiAgfQoKICAvLyBSZW1vdmUgYWxsIHByb3BlcnRpZXMgd2l0aCBhIHZhbHVlIG9mIDAgZnJvbSBhbiBvYmplY3QKICBmdW5jdGlvbiByZW1vdmVaZXJvZXModmFscykgewogICAgY29uc3QgbmV3VmFscyA9IHt9OwogICAgZm9yIChjb25zdCBba2V5LCB2YWx1ZV0gb2YgT2JqZWN0LmVudHJpZXModmFscykpIHsKICAgICAgaWYg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GxlciB0b3BpY3MgbGlrZSBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgdmFsaWRpdHksIHNlZSB0aGUgZXh0ZXJuYWwgZG9jdW1lbnRhdGlvbi4KICAgKi8KICBjbGFzcyBEdXJhdGlvbiB7CiAgICAvKioKICAgICAqIEBwcml2YXRlCiAgICAgKi8KICAgIGNvbnN0cnVjdG9yKGNvbmZpZykgewogICAgICBjb25zdCBhY2N1cmF0ZSA9IGNvbmZpZy5jb252ZXJzaW9uQWNjdXJhY3kgPT09ICJsb25ndGVybSIgfHwgZmFsc2U7CiAgICAgIGxldCBtYXRyaXggPSBhY2N1cmF0ZSA/IGFjY3VyYXRlTWF0cml4IDogY2FzdWFsTWF0cml4OwoKICAgICAgaWYgKGNvbmZpZy5tYXRyaXgpIHsKICAgICAgICBtYXRyaXggPSBjb25maWcubWF0cml4OwogICAgICB9CgogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLnZhbHVlcyA9IGNvbmZpZy52YWx1ZXM7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMubG9jID0gY29uZmlnLmxvYyB8fCBMb2NhbGUuY3JlYXRlKCk7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMuY29udmVyc2lvbkFjY3VyYWN5ID0gYWNjdXJhdGUgPyAibG9uZ3Rlcm0iIDogImNhc3VhbCI7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMuaW52YWxpZCA9IGNvbmZpZy5pbnZhbGlkIHx8IG51bGw7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMubWF0cml4ID0gbWF0cml4OwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlC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AgdGhyb3cgbmV3IEludmFsaWRBcmd1bWVudEVycm9yKAogICAgICAgICAgYER1cmF0aW9uLmZyb21PYmplY3Q6IGFyZ3VtZW50IGV4cGVjdGVkIHRvIGJlIGFuIG9iamVjdCwgZ290ICR7CiAgICAgICAgICBvYmogPT09IG51bGwgPyAibnVsbCIgOiB0eXBlb2Ygb2JqCiAgICAgICAgfWAKICAgICAgICApOwogICAgICB9CgogICAgICByZXR1cm4gbmV3IER1cmF0aW9uKHsKICAgICAgICB2YWx1ZXM6IG5vcm1hbGl6ZU9iamVjdChvYmosIER1cmF0aW9uLm5vcm1hbGl6ZVVuaXQpLAogICAgICAgIGxvYzogTG9jYWxlLmZyb21PYmplY3Qob3B0cyksCiAgICAgICAgY29udmVyc2lvbkFjY3VyYWN5OiBvcHRzLmNvbnZlcnNpb25BY2N1cmFjeSwKICAgICAgICBtYXRyaXg6IG9wdHMubWF0cml4LAogICAgICB9KTsKICAgIH0KCiAgICAvKioKICAgICAqIENyZWF0ZSBhIER1cmF0aW9uIGZyb20gRHVyYXRpb25MaWtlLgogICAgICoKICAgICAqIEBwYXJhbSB7T2JqZWN0IHwgbnVtYmVyIHwgRHVyYXRpb259IGR1cmF0aW9uTGlrZQogICAgICogT25lIG9mOgogICAgICogLSBvYmplY3Qgd2l0aCBrZXlzIGxpa2UgJ3llYXJzJyBhbmQgJ2hvdXJzJy4KICAgICAqIC0gbnVtYmVyIHJlcHJlc2VudGluZyBtaWxsaXNlY29uZHMKICAgICAqIC0gRHVyYXRpb24gaW5zdGFuY2UKICAgICAqIEByZXR1cm4ge0R1cmF0aW9ufQogICAgICovCiAgICBzdGF0aWMgZnJvbUR1cmF0aW9uTGlrZShkdXJhdGlvbkxpa2UpIHsKICAgICAgaWYgKGlzTnVtYmVyKGR1cmF0aW9uTGlrZSkpIHsKICAgICAgICByZXR1cm4gRHVyYXRpb24uZnJvbU1pbGxpcyhkdXJhdGlvbkxpa2UpOwogICAgICB9IGVsc2UgaWYgKER1cmF0a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HBzOi8vZW4ud2lraXBlZGlhLm9yZy93aWtpL0lTT184NjAxI0R1cmF0aW9ucwogICAgICogQGV4YW1wbGUgRHVyYXRpb24uZnJvbUlTTygnUDNZNk0xVzREVDEySDMwTTVTJykudG9PYmplY3QoKSAvLz0+IHsgeWVhcnM6IDMsIG1vbnRoczogNiwgd2Vla3M6IDEsIGRheXM6IDQsIGhvdXJzOiAxMiwgbWludXRlczogMzAsIHNlY29uZHM6IDUgfQogICAgICogQGV4YW1wbGUgRHVyYXRpb24uZnJvbUlTTygnUFQyM0gnKS50b09iamVjdCgpIC8vPT4geyBob3VyczogMjMgfQogICAgICogQGV4YW1wbGUgRHVyYXRpb24uZnJvbUlTTygnUDVZM00nKS50b09iamVjdCgpIC8vPT4geyB5ZWFyczogNSwgbW9udGhzOiAzIH0KICAgICAqIEByZXR1cm4ge0R1cmF0aW9ufQogICAgICovCiAgICBzdGF0aWMgZnJvbUlTTyh0ZXh0LCBvcHRzKSB7CiAgICAgIGNvbnN0IFtwYXJzZWRdID0gcGFyc2VJU09EdXJhdGlvbih0ZXh0KTsKICAgICAgaWYgKHBhcnNlZCkgewogICAgICAgIHJldHVybiBEdXJhdGlvbi5mcm9tT2JqZWN0KHBhcnNlZCwgb3B0cyk7CiAgICAgIH0gZWxzZSB7CiAgICAgICAgcmV0dXJuIER1cmF0aW9uLmludmFsaWQoInVucGFyc2FibGUiLCBgdGhlIGlucHV0ICIke3RleHR9IiBjYW4ndCBiZSBwYXJzZWQgYXMgSVNPIDg2MDFgKTsKICAgICAgfQogICAgfQoKICAgIC8qKgogICAgICogQ3JlYXRlIGEgRHVyYXRpb24gZnJvbSBhbiBJU08gODYwMSB0aW1lIHN0cmluZy4KICAgICAqIEBwYXJhbSB7c3RyaW5nfSB0ZXh0IC0gdGV4dCB0byBwYXJzZQogICAgICogQHBhcmFtIHtPYmplY3R9IG9wdHMgLSBvcHRpb25zIGZvciBwYXJzaW5nCiAgICAgKiBAcGFyYW0ge3N0cmluZ30gW29w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HJlYXNvbiB0aGUgRHVyYXRpb24gaXMgaW52YWxpZCIpOwogICAgICB9CgogICAgICBjb25zdCBpbnZhbGlkID0gcmVhc29uIGluc3RhbmNlb2YgSW52YWxpZCA/IHJlYXNvbiA6IG5ldyBJbnZhbGlkKHJlYXNvbiwgZXhwbGFuYXRpb24pOwoKICAgICAgaWYgKFNldHRpbmdzLnRocm93T25JbnZhbGlkKSB7CiAgICAgICAgdGhyb3cgbmV3IEludmFsaWREdXJhdGlvbkVycm9yKGludmFsaWQpOwogICAgICB9IGVsc2UgewogICAgICAgIHJldHVybiBuZXcgRHVyYXRpb24oeyBpbnZhbGlkIH0pOwogICAgICB9CiAgICB9CgogICAgLyoqCiAgICAgKiBAcHJpdmF0ZQogICAgICovCiAgICBzdGF0aWMgbm9ybWFsaXplVW5pdCh1bml0KSB7CiAgICAgIGNvbnN0IG5vcm1hbGl6ZWQgPSB7CiAgICAgICAgeWVhcjogInllYXJzIiwKICAgICAgICB5ZWFyczogInllYXJzIiwKICAgICAgICBxdWFydGVyOiAicXVhcnRlcnMiLAogICAgICAgIHF1YXJ0ZXJzOiAicXVhcnRlcnMiLAogICAgICAgIG1vbnRoOiAibW9udGhzIiwKICAgICAgICBtb250aHM6ICJtb250aHMiLAogICAgICAgIHdlZWs6ICJ3ZWVrcyIsCiAgICAgICAgd2Vla3M6ICJ3ZWVrcyIsCiAgICAgICAgZGF5OiAiZGF5cyIsCiAgICAgICAgZGF5czogImRheXMiLAogICAgICAgIGhvdXI6ICJob3VycyIsCiAgICAgICAgaG91cnM6ICJob3VycyIsCiAgICAgICAgbWludXRlOiAibWludXRlcyIsCiAgICAgICAgbWludXRlczogIm1pbnV0ZXMiLAogICAgICAgIHNlY29uZDogInNlY29uZHMiLAogICAgICAgIHNlY29uZHM6ICJzZWNvbmRzIiwKICAgICAgICBtaWxsaXNlY29uZDogIm1pbGxpc2Vjb25kcyIsCiAgICAgICAgbWlsbGlzZWNvbmRzOiAibWlsbGlzZWNvbmRzIiwK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07CiAgICAgICAgICBpZiAoaXNVbmRlZmluZWQodmFsKSkgewogICAgICAgICAgICByZXR1cm4gbnVsbDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiB0aGlzLmxvYwogICAgICAgICAgICAubnVtYmVyRm9ybWF0dGVyKHsgc3R5bGU6ICJ1bml0IiwgdW5pdERpc3BsYXk6ICJsb25nIiwgLi4ub3B0cywgdW5pdDogdW5pdC5zbGljZSgwLCAtMSkgfSkKICAgICAgICAgICAgLmZvcm1hdCh2YWwpOwogICAgICAgIH0pCiAgICAgICAgLmZpbHRlcigobikgPT4gbik7CgogICAgICByZXR1cm4gdGhpcy5sb2MKICAgICAgICAubGlzdEZvcm1hdHRlcih7IHR5cGU6ICJjb25qdW5jdGlvbiIsIHN0eWxlOiBvcHRzLmxpc3RTdHlsZSB8fCAibmFycm93IiwgLi4ub3B0cyB9KQogICAgICAgIC5mb3JtYXQobCk7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm5zIGEgSmF2YVNjcmlwdCBvYmplY3Qgd2l0aCB0aGlzIER1cmF0aW9uJ3MgdmFsdWVzLgogICAgICogQGV4YW1wbGUgRHVyYXRpb24uZnJvbU9iamVjdCh7IHllYXJzOiAxLCBkYXlzOiA2LCBzZWNvbmRzOiAyIH0pLnRvT2JqZWN0KCkgLy89PiB7IHllYXJzOiAxLCBkYXlzOiA2LCBzZWNvbmRzOiAyIH0KICAgICAqIEByZXR1cm4ge09iamVjdH0KICAgICAqLwogICAgdG9PYmplY3QoKSB7CiAgICAgIGlmICghdGhpcy5pc1ZhbGlkKSByZXR1cm4ge307CiAgICAgIHJldHVybiB7IC4uLnRoaXMudmFsdWVzIH07CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm5zIGFuIElTTyA4NjAxLWNvbXBsaWFudCBzdHJpbmcgcmVwcmVzZW50YXRpb24gb2YgdGhpcyBEdXJhdGlvbi4KICAgICAqIEBzZWUgaHR0cHM6Ly9lbi53aWtpcGVkaWEub3Jn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xpc30KICAgICAqIEByZXR1cm4ge251bWJlcn0KICAgICAqLwogICAgdmFsdWVPZigpIHsKICAgICAgcmV0dXJuIHRoaXMudG9NaWxsaXMoKTsKICAgIH0KCiAgICAvKioKICAgICAqIE1ha2UgdGhpcyBEdXJhdGlvbiBsb25nZXIgYnkgdGhlIHNwZWNpZmllZCBhbW91bnQuIFJldHVybiBhIG5ld2x5LWNvbnN0cnVjdGVkIER1cmF0aW9uLgogICAgICogQHBhcmFtIHtEdXJhdGlvbnxPYmplY3R8bnVtYmVyfSBkdXJhdGlvbiAtIFRoZSBhbW91bnQgdG8gYWRkLiBFaXRoZXIgYSBMdXhvbiBEdXJhdGlvbiwgYSBudW1iZXIgb2YgbWlsbGlzZWNvbmRzLCB0aGUgb2JqZWN0IGFyZ3VtZW50IHRvIER1cmF0aW9uLmZyb21PYmplY3QoKQogICAgICogQHJldHVybiB7RHVyYXRpb259CiAgICAgKi8KICAgIHBsdXMoZHVyYXRpb24pIHsKICAgICAgaWYgKCF0aGlzLmlzVmFsaWQpIHJldHVybiB0aGlzOwoKICAgICAgY29uc3QgZHVyID0gRHVyYXRpb24uZnJvbUR1cmF0aW9uTGlrZShkdXJhdGlvbiksCiAgICAgICAgcmVzdWx0ID0ge307CgogICAgICBmb3IgKGNvbnN0IGsgb2Ygb3JkZXJlZFVuaXRzJDEpIHsKICAgICAgICBpZiAoaGFzT3duUHJvcGVydHkoZHVyLnZhbHVlcywgaykgfHwgaGFzT3duUHJvcGVydHkodGhpcy52YWx1ZXMsIGspKSB7CiAgICAgICAgICByZXN1bHRba10gPSBkdXIuZ2V0KGspICsgdGhpcy5nZXQoayk7CiAgICAgICAgfQogICAgICB9CgogICAgICByZXR1cm4gY2xvbmUkMSh0aGlzLCB7IHZhbHVlczogcmVzdWx0IH0sIHRydWUpOwogICAgfQoKICAgIC8qKgogICAgICogTWFrZSB0aGlzIER1cmF0aW9uIHNob3J0ZXIgYnkgdGhlIHNwZWNpZmllZCBhbW91bnQuIFJldHV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AgfSkubWFwVW5pdHMoKHgsIHUpID0+IHUgPT09ICJob3VycyIgPyB4ICogMiA6IHgpIC8vPT4geyBob3VyczogMiwgbWludXRlczogMzAgfQogICAgICogQHJldHVybiB7RHVyYXRpb259CiAgICAgKi8KICAgIG1hcFVuaXRzKGZuKSB7CiAgICAgIGlmICghdGhpcy5pc1ZhbGlkKSByZXR1cm4gdGhpczsKICAgICAgY29uc3QgcmVzdWx0ID0ge307CiAgICAgIGZvciAoY29uc3QgayBvZiBPYmplY3Qua2V5cyh0aGlzLnZhbHVlcykpIHsKICAgICAgICByZXN1bHRba10gPSBhc051bWJlcihmbih0aGlzLnZhbHVlc1trXSwgaykpOwogICAgICB9CiAgICAgIHJldHVybiBjbG9uZSQxKHRoaXMsIHsgdmFsdWVzOiByZXN1bHQgfSwgdHJ1ZSk7CiAgICB9CgogICAgLyoqCiAgICAgKiBHZXQgdGhlIHZhbHVlIG9mIHVuaXQuCiAgICAgKiBAcGFyYW0ge3N0cmluZ30gdW5pdCAtIGEgdW5pdCBzdWNoIGFzICdtaW51dGUnIG9yICdkYXknCiAgICAgKiBAZXhhbXBsZSBEdXJhdGlvbi5mcm9tT2JqZWN0KHt5ZWFyczogMiwgZGF5czogM30pLmdldCgneWVhcnMnKSAvLz0+IDIKICAgICAqIEBleGFtcGxlIER1cmF0aW9uLmZyb21PYmplY3Qoe3llYXJzOiAyLCBkYXlzOiAzfSkuZ2V0KCdtb250aHMnKSAvLz0+IDAKICAgICAqIEBleGFtcGxlIER1cmF0aW9uLmZyb21PYmplY3Qoe3llYXJzOiAyLCBkYXlzOiAzfSkuZ2V0KCdkYXlzJykgLy89PiAzCiAgICAgKiBAcmV0dXJuIHtudW1iZXJ9CiAgICAgKi8KICAgIGdldCh1bml0KSB7CiAgICAgIHJldHVybiB0aGlzW0R1cmF0aW9uLm5vcm1hbGl6ZVVuaXQodW5pdCldOwogICAgfQoKICAgIC8qKgogICAgICogIlNldCIgdGhlIHZhbHVlcyBvZiBzcGVjaW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czogMzAwMDAgfQogICAgICogQHJldHVybiB7RHVyYXRpb259CiAgICAgKi8KICAgIHNoaWZ0VG8oLi4udW5pdHMpIHsKICAgICAgaWYgKCF0aGlzLmlzVmFsaWQpIHJldHVybiB0aGlzOwoKICAgICAgaWYgKHVuaXRzLmxlbmd0aCA9PT0gMCkgewogICAgICAgIHJldHVybiB0aGlzOwogICAgICB9CgogICAgICB1bml0cyA9IHVuaXRzLm1hcCgodSkgPT4gRHVyYXRpb24ubm9ybWFsaXplVW5pdCh1KSk7CgogICAgICBjb25zdCBidWlsdCA9IHt9LAogICAgICAgIGFjY3VtdWxhdGVkID0ge30sCiAgICAgICAgdmFscyA9IHRoaXMudG9PYmplY3QoKTsKICAgICAgbGV0IGxhc3RVbml0OwoKICAgICAgZm9yIChjb25zdCBrIG9mIG9yZGVyZWRVbml0cyQxKSB7CiAgICAgICAgaWYgKHVuaXRzLmluZGV4T2YoaykgPj0gMCkgewogICAgICAgICAgbGFzdFVuaXQgPSBrOwoKICAgICAgICAgIGxldCBvd24gPSAwOwoKICAgICAgICAgIC8vIGFueXRoaW5nIHdlIGhhdmVuJ3QgYm9pbGVkIGRvd24geWV0IHNob3VsZCBnZXQgYm9pbGVkIHRvIHRoaXMgdW5pdAogICAgICAgICAgZm9yIChjb25zdCBhayBpbiBhY2N1bXVsYXRlZCkgewogICAgICAgICAgICBvd24gKz0gdGhpcy5tYXRyaXhbYWtdW2tdICogYWNjdW11bGF0ZWRbYWtdOwogICAgICAgICAgICBhY2N1bXVsYXRlZFtha10gPSAwOwogICAgICAgICAgfQoKICAgICAgICAgIC8vIHBsdXMgYW55dGhpbmcgdGhhdCdzIGFscmVhZHkgaW4gdGhpcyB1bml0CiAgICAgICAgICBpZiAoaXNOdW1iZXIodmFsc1trXSkpIHsKICAgICAgICAgICAgb3duICs9IHZhbHNba107CiAgICAgICAgICB9CgogICAgICAgICAgY29uc3QgaSA9IE1hdGgudHJ1bmMob3duKTsKICAgICAgICAgIGJ1aWx0W2tdID0gaTsKICAgICAgICAgIGFjY3VtdWxhdGVkW2tdID0gKG93biAqIDEwMDAgLSBpICogMTAwMCkgLyAxMDAwOwoKICAgICAgICAgIC8vIHBsdXMgYW55dGhpbmcgZnVydGhlciBkb3duIHRoZSBjaGFpbiB0aGF0IHNob3VsZCBiZSByb2xsZWQgdXAgaW4gdG8gdGhpcwogICAgICAgICAgZm9yIChjb25zdCBkb3duIGluIHZhbHMpIHsKICAgICAgICAgICAgaWYgKG9yZGVyZWRVbml0cyQxLmluZGV4T2YoZG93bikgPiBvcmRlcmVkVW5pdHMkMS5pbmRleE9mKGspKSB7CiAgICAgICAgICAgICAgY29udmVydCh0aGlzLm1hdHJpeCwgdmFscywgZG93biwgYnVpbHQsIGspOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICAvLyBvdGhlcndpc2UsIGtlZXAgaXQgaW4gdGhlIHdpbmdzIHRvIGJvaWwgaXQgbGF0ZXIKICAgICAgICB9IGVsc2UgaWYgKGlzTnVtYmVyKHZhbHNba10pKSB7CiAgICAgICAgICBhY2N1bXVsYXRlZFtrXSA9IHZhbHNba107CiAgICAgICAgfQogICAgICB9CgogICAgICAvLyBhbnl0aGluZyBsZWZ0b3ZlciBiZWNvbWVzIHRoZSBkZWNpbWFsIGZvciB0aGUgbGFzdCB1bml0CiAgICAgIC8vIGxhc3RVbml0IG11c3QgYmUgZGVmaW5lZCBzaW5jZSB1bml0cyBpcyBub3QgZW1wdHkKICAgICAgZm9yIChjb25zdCBrZXkgaW4gYWNjdW11bGF0ZWQpIHsKICAgICAgICBpZiAoYWNjdW11bGF0ZWRba2V5XSAhPT0gMCkgewogICAgICAgICAgYnVpbHRbbGFzdFVuaXRdICs9CiAgICAgICAgICAgIGtleSA9PT0gbGFzdFVuaXQgPyBhY2N1bXVsYXRlZFtrZXldIDogYWNjdW11bGF0ZWRba2V5XSAvIHRoaXMubWF0cml4W2xhc3RVbml0XVtrZXldOwogICAgICAgIH0KICAgICAgfQoKICAgICAgcmV0dXJuIGNsb25lJDEodGhpcywgeyB2YWx1ZXM6IGJ1aWx0IH0sIHRydWUpLm5vcm1hbGl6ZSgpOwogICAgfQoKICAgIC8qKgogICAgICogU2hpZnQgdGhpcyBEdXJhdGlvbiB0byBhbGwgYXZhaWxhYmxlIHVuaXRzLgogICAgICogU2FtZSBhcyBzaGlmdFRvKCJ5ZWFycyIsICJtb250aHMiLCAid2Vla3MiLCAiZGF5cyIsICJob3VycyIsICJtaW51dGVzIiwgInNlY29uZHMiLCAibWlsbGlzZWNvbmRzIikKICAgICAqIEByZXR1cm4ge0R1cmF0aW9ufQogICAgICovCiAgICBzaGlmdFRvQWxsKCkgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCkgcmV0dXJuIHRoaXM7CiAgICAgIHJldHVybiB0aGlzLnNoaWZ0VG8oCiAgICAgICAgInllYXJzIiwKICAgICAgICAibW9udGhzIiwKICAgICAgICAid2Vla3MiLAogICAgICAgICJkYXlzIiwKICAgICAgICAiaG91cnMiLAogICAgICAgICJtaW51dGVzIiwKICAgICAgICAic2Vjb25kcyIsCiAgICAgICAgIm1pbGxpc2Vjb25kcyIKICAgICAgKTsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybiB0aGUgbmVnYXRpdmUgb2YgdGhpcyBEdXJhdGlvbi4KICAgICAqIEBleGFtcGxlIER1cmF0aW9uLmZyb21PYmplY3QoeyBob3VyczogMSwgc2Vjb25kczogMzAgfSkubmVnYXRlKCkudG9PYmplY3QoKSAvLz0+IHsgaG91cnM6IC0xLCBzZWNvbmRzOiAtMzAgfQogICAgICogQHJldHVybiB7RHVyYXRpb259CiAgICAgKi8KICAgIG5lZ2F0ZSgpIHsKICAgICAgaWYgKCF0aGlzLmlzVmFsaWQpIHJldHVybiB0aGlzOwogICAgICBjb25zdCBuZWdhdGVkID0ge307CiAgICAgIGZvciAoY29uc3QgayBvZiBPYmplY3Qua2V5cyh0aGlzLnZhbHVlcykpIHsKICAgICAgICBuZWdhdGVkW2tdID0gdGhpcy52YWx1ZXNba10gPT09IDAgPyAwIDogLXRoaXMudmFsdWVzW2tdOwogICAgICB9CiAgICAgIHJldHVybiBjbG9uZSQxKHRoaXMsIHsgdmFsdWVzOiBuZWdhdGVkIH0sIHRydWUpOwogICAgfQoKICAgIC8qKgogICAgICogR2V0IHRoZSB5ZWFycy4KICAgICAqIEB0eXBlIHtudW1iZXJ9CiAgICAgKi8KICAgIGdldCB5ZWFycygpIHsKICAgICAgcmV0dXJuIHRoaXMuaXNWYWxpZCA/IHRoaXMudmFsdWVzLnllYXJzIHx8IDAgOiBOYU47CiAgICB9CgogICAgLyoqCiAgICAgKiBHZXQgdGhlIHF1YXJ0ZXJzLgogICAgICogQHR5cGUge251bWJlcn0KICAgICAqLwogICAgZ2V0IHF1YXJ0ZXJzKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy52YWx1ZXMucXVhcnRlcnMgfHwgMCA6IE5hTjsKICAgIH0KCiAgICAvKioKICAgICAqIEdldCB0aGUgbW9udGhzLgogICAgICogQHR5cGUge251bWJlcn0KICAgICAqLwogICAgZ2V0IG1vbnRocygpIHsKICAgICAgcmV0dXJuIHRoaXMuaXNWYWxpZCA/IHRoaXMudmFsdWVzLm1vbnRocyB8fCAwIDogTmFOOwogICAgfQoKICAgIC8qKgogICAgICogR2V0IHRoZSB3ZWVrcwogICAgICogQHR5cGUge251bWJlcn0KICAgICAqLwogICAgZ2V0IHdlZWtzKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy52YWx1ZXMud2Vla3MgfHwgMCA6IE5hTjsKICAgIH0KCiAgICAvKioKICAgICAqIEdldCB0aGUgZGF5cy4KICAgICAqIEB0eXBlIHtudW1iZXJ9CiAgICAgKi8KICAgIGdldCBkYXlzKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy52YWx1ZXMuZGF5cyB8fCAwIDogTmFOOwogICAgfQoKICAgIC8qKgogICA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ICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgYW4gZXJyb3IgY29kZSBpZiB0aGlzIER1cmF0aW9uIGJlY2FtZSBpbnZhbGlkLCBvciBudWxsIGlmIHRoZSBEdXJhdGlvbiBpcyB2YWxpZAogICAgICogQHJldHVybiB7c3RyaW5nfQogICAgICovCiAgICBnZXQgaW52YWxpZFJlYXNvbigpIHsKICAgICAgcmV0dXJuIHRoaXMuaW52YWxpZCA/IHRoaXMuaW52YWxpZC5yZWFzb24gOiBudWxsOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJucyBhbiBleHBsYW5hdGlvbiBvZiB3aHkgdGhpcyBEdXJhdGlvbiBiZWNhbWUgaW52YWxpZCwgb3IgbnVsbCBpZiB0aGUgRHVyYXRpb24gaXMgdmFsaWQKICAgICAqIEB0eXBlIHtzdHJpbmd9CiAgICAgKi8KICAgIGdldCBpbnZhbGlkRXhwbGFuYXRpb24oKSB7CiAgICAgIHJldHVybiB0aGlzLmludmFsaWQgPyB0aGlzLmludmFsaWQuZXhwbGFuYXRpb24gOiBudWxsOwogICAgfQoKICAgIC8qKgogICAgICogRXF1YWxpdHkgY2hlY2sKICAgICAqIFR3byBEdXJhdGlvbnMgYXJlIGVxdWFsIGlmZiB0aGV5IGhhdmUgdGhlIHNhbWUgdW5pdHMgYW5kIHRoZSBzYW1lIHZhbHVlcyBmb3IgZWFjaCB1bml0LgogICAgICogQHBhcmFtIHtEdXJhdGlvbn0gb3RoZXIKICAgICAqIEByZXR1cm4ge2Jvb2xlYW59CiAgICAgKi8KICAgIGVxdWFscyhvdGhlcikgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCB8fCAhb3RoZXIuaXNWYWxpZCkgewogICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgfQoKICAgICAgaWYgKCF0aGlzLmxvYy5lcXVhbHMob3RoZXIubG9jKSkgewogICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgfQoKICAgICAgZnVuY3Rpb24gZXEodjEsI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lVGltZS5mcm9tSVNPKHMsIG9wdHMpOwogICAgICAgICAgc3RhcnRJc1ZhbGlkID0gc3RhcnQuaXNWYWxpZDsKICAgICAgICB9IGNhdGNoIChlKSB7CiAgICAgICAgICBzdGFydElzVmFsaWQgPSBmYWxzZTsKICAgICAgICB9CgogICAgICAgIGxldCBlbmQsIGVuZElzVmFsaWQ7CiAgICAgICAgdHJ5IHsKICAgICAgICAgIGVuZCA9IERhdGVUaW1lLmZyb21JU08oZSwgb3B0cyk7CiAgICAgICAgICBlbmRJc1ZhbGlkID0gZW5kLmlzVmFsaWQ7CiAgICAgICAgfSBjYXRjaCAoZSkgewogICAgICAgICAgZW5kSXNWYWxpZCA9IGZhbHNlOwogICAgICAgIH0KCiAgICAgICAgaWYgKHN0YXJ0SXNWYWxpZCAmJiBlbmRJc1ZhbGlkKSB7CiAgICAgICAgICByZXR1cm4gSW50ZXJ2YWwuZnJvbURhdGVUaW1lcyhzdGFydCwgZW5kKTsKICAgICAgICB9CgogICAgICAgIGlmIChzdGFydElzVmFsaWQpIHsKICAgICAgICAgIGNvbnN0IGR1ciA9IER1cmF0aW9uLmZyb21JU08oZSwgb3B0cyk7CiAgICAgICAgICBpZiAoZHVyLmlzVmFsaWQpIHsKICAgICAgICAgICAgcmV0dXJuIEludGVydmFsLmFmdGVyKHN0YXJ0LCBkdXIpOwogICAgICAgICAgfQogICAgICAgIH0gZWxzZSBpZiAoZW5kSXNWYWxpZCkgewogICAgICAgICAgY29uc3QgZHVyID0gRHVyYXRpb24uZnJvbUlTTyhzLCBvcHRzKTsKICAgICAgICAgIGlmIChkdXIuaXNWYWxpZCkgewogICAgICAgICAgICByZXR1cm4gSW50ZXJ2YWwuYmVmb3JlKGVuZCwgZHVyKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIEludGVydmFsLmludmFsaWQoInVucGFyc2FibGUiLCBgdGhlIGlucHV0ICIke3RleHR9IiBjYW4ndCBiZSBwYXJzZWQgYXMgSVNPIDg2MDFgKTsKICAgIH0KCiAgICAvKioKICAgICAqIENoZWNrIGlmIGFuIG9iamVjdCBpcyBhbiBJbnRlcnZhbC4gV29ya3MgYWNyb3NzIGNvbnRleHQgYm91bmRhcmllcwogICAgICogQHBhcmFtIHtvYmplY3R9IG8KICAgICAqIEByZXR1cm4ge2Jvb2xlYW59CiAgICAgKi8KICAgIHN0YXRpYyBpc0ludGVydmFsKG8pIHsKICAgICAgcmV0dXJuIChvICYmIG8uaXNMdXhvbkludGVydmFsKSB8fCBmYWxzZTsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgdGhlIHN0YXJ0IG9mIHRoZSBJbnRlcnZhbAogICAgICogQHR5cGUge0RhdGVUaW1lfQogICAgICovCiAgICBnZXQgc3RhcnQoKSB7CiAgICAgIHJldHVybiB0aGlzLmlzVmFsaWQgPyB0aGlzLnMgOiBudWxsOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJucyB0aGUgZW5kIG9mIHRoZSBJbnRlcnZhbAogICAgICogQHR5cGUge0RhdGVUaW1lfQogICAgICovCiAgICBnZXQgZW5kKCkgewogICAgICByZXR1cm4gdGhpcy5pc1ZhbGlkID8gdGhpcy5lIDogbnVsbDsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgd2hldGhlciB0aGlzIEludGVydmFsJ3MgZW5kIGlzIGF0IGxlYXN0IGl0cyBzdGFydCwgbWVhbmluZyB0aGF0IHRoZSBJbnRlcnZhbCBpc24ndCAnYmFja3dhcmRzJy4KICAgICAqIEB0eXBlIHtib29sZWFufQogICAgICovCiAgICBnZXQgaXNWYWxpZCgpIHsKICAgICAgcmV0dXJuIHRoaXMuaW52YWxpZFJlYXNvbiA9PT0gbnVsbDsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgYW4gZXJyb3IgY29kZSBpZiB0aGlzIEludGVydmFsIGlzIGludmFsaWQsIG9yIG51bGwgaWYgdGhlIEludGVydmFsIGlzIHZhbGlkCiAg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90aGVyCiAgICAgKiBAcmV0dXJuIHtib29sZWFufQogICAgICovCiAgICBvdmVybGFwcyhvdGhlcikgewogICAgICByZXR1cm4gdGhpcy5lID4gb3RoZXIucyAmJiB0aGlzLnMgPCBvdGhlci5lOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJuIHdoZXRoZXIgdGhpcyBJbnRlcnZhbCdzIGVuZCBpcyBhZGphY2VudCB0byB0aGUgc3BlY2lmaWVkIEludGVydmFsJ3Mgc3RhcnQuCiAgICAgKiBAcGFyYW0ge0ludGVydmFsfSBvdGhlcgogICAgICogQHJldHVybiB7Ym9vbGVhbn0KICAgICAqLwogICAgYWJ1dHNTdGFydChvdGhlcikgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCkgcmV0dXJuIGZhbHNlOwogICAgICByZXR1cm4gK3RoaXMuZSA9PT0gK290aGVyLnM7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm4gd2hldGhlciB0aGlzIEludGVydmFsJ3Mgc3RhcnQgaXMgYWRqYWNlbnQgdG8gdGhlIHNwZWNpZmllZCBJbnRlcnZhbCdzIGVuZC4KICAgICAqIEBwYXJhbSB7SW50ZXJ2YWx9IG90aGVyCiAgICAgKiBAcmV0dXJuIHtib29sZWFufQogICAgICovCiAgICBhYnV0c0VuZChvdGhlcikgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCkgcmV0dXJuIGZhbHNlOwogICAgICByZXR1cm4gK290aGVyLmUgPT09ICt0aGlzLnM7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm4gd2hldGhlciB0aGlzIEludGVydmFsIGVuZ3VsZnMgdGhlIHN0YXJ0IGFuZCBlbmQgb2YgdGhlIHNwZWNpZmllZCBJbnRlcnZhbC4KICAgICAqIEBwYXJhbSB7SW50ZXJ2YWx9IG90aGVyCiAgICAgKiBAcmV0dXJuIHtib29sZWFufQogICAgICovCiAgICBlbmd1bGZzKG90aGVyKSB7CiAgICAgIGlmICghdGhpcy5pc1Z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vbWJpbmVzIG92ZXJsYXBwaW5nIGFuZCBhZGphY2VudCBJbnRlcnZhbHMuCiAgICAgKiBAcGFyYW0ge0FycmF5fSBpbnRlcnZhbHMKICAgICAqIEByZXR1cm4ge0FycmF5fQogICAgICovCiAgICBzdGF0aWMgbWVyZ2UoaW50ZXJ2YWxzKSB7CiAgICAgIGNvbnN0IFtmb3VuZCwgZmluYWxdID0gaW50ZXJ2YWxzCiAgICAgICAgLnNvcnQoKGEsIGIpID0+IGEucyAtIGIucykKICAgICAgICAucmVkdWNlKAogICAgICAgICAgKFtzb2ZhciwgY3VycmVudF0sIGl0ZW0pID0+IHsKICAgICAgICAgICAgaWYgKCFjdXJyZW50KSB7CiAgICAgICAgICAgICAgcmV0dXJuIFtzb2ZhciwgaXRlbV07CiAgICAgICAgICAgIH0gZWxzZSBpZiAoY3VycmVudC5vdmVybGFwcyhpdGVtKSB8fCBjdXJyZW50LmFidXRzU3RhcnQoaXRlbSkpIHsKICAgICAgICAgICAgICByZXR1cm4gW3NvZmFyLCBjdXJyZW50LnVuaW9uKGl0ZW0pXTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICByZXR1cm4gW3NvZmFyLmNvbmNhdChbY3VycmVudF0pLCBpdGVtXTsKICAgICAgICAgICAgfQogICAgICAgICAgfSwKICAgICAgICAgIFtbXSwgbnVsbF0KICAgICAgICApOwogICAgICBpZiAoZmluYWwpIHsKICAgICAgICBmb3VuZC5wdXNoKGZpbmFsKTsKICAgICAgfQogICAgICByZXR1cm4gZm91bmQ7CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm4gYW4gYXJyYXkgb2YgSW50ZXJ2YWxzIHJlcHJlc2VudGluZyB0aGUgc3BhbnMgb2YgdGltZSB0aGF0IG9ubHkgYXBwZWFyIGluIG9uZSBvZiB0aGUgc3BlY2lmaWVkIEludGVydmFscy4KICAgICAqIEBwYXJhbSB7QXJyYXl9IGludGVydmFscwogICAgICogQHJldHVybiB7QXJyYXl9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UaGUgc2FtZSBvcHRpb25zIGFzIHtAbGluayBEYXRlVGltZSN0b0lTT30KICAgICAqIEByZXR1cm4ge3N0cmluZ30KICAgICAqLwogICAgdG9JU08ob3B0cykgewogICAgICBpZiAoIXRoaXMuaXNWYWxpZCkgcmV0dXJuIElOVkFMSUQkMTsKICAgICAgcmV0dXJuIGAke3RoaXMucy50b0lTTyhvcHRzKX0vJHt0aGlzLmUudG9JU08ob3B0cyl9YDsKICAgIH0KCiAgICAvKioKICAgICAqIFJldHVybnMgYW4gSVNPIDg2MDEtY29tcGxpYW50IHN0cmluZyByZXByZXNlbnRhdGlvbiBvZiBkYXRlIG9mIHRoaXMgSW50ZXJ2YWwuCiAgICAgKiBUaGUgdGltZSBjb21wb25lbnRzIGFyZSBpZ25vcmVkLgogICAgICogQHNlZSBodHRwczovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9JU09fODYwMSNUaW1lX2ludGVydmFscwogICAgICogQHJldHVybiB7c3RyaW5nfQogICAgICovCiAgICB0b0lTT0RhdGUoKSB7CiAgICAgIGlmICghdGhpcy5pc1ZhbGlkKSByZXR1cm4gSU5WQUxJRCQxOwogICAgICByZXR1cm4gYCR7dGhpcy5zLnRvSVNPRGF0ZSgpfS8ke3RoaXMuZS50b0lTT0RhdGUoKX1gOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJucyBhbiBJU08gODYwMS1jb21wbGlhbnQgc3RyaW5nIHJlcHJlc2VudGF0aW9uIG9mIHRpbWUgb2YgdGhpcyBJbnRlcnZhbC4KICAgICAqIFRoZSBkYXRlIGNvbXBvbmVudHMgYXJlIGlnbm9yZWQuCiAgICAgKiBAc2VlIGh0dHBzOi8vZW4ud2lraXBlZGlhLm9yZy93aWtpL0lTT184NjAxI1RpbWVfaW50ZXJ2YWxzCiAgICAgKiBAcGFyYW0ge09iamVjdH0gb3B0cyAtIFRoZSBzYW1lIG9wdGlvbnMgYXMge0BsaW5rIERhdGVUaW1lI3RvSVNPfQogICAgICogQHJldH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IDY0NzldLAogICAgbWx5bTogWzM0MzAsIDM0MzldLAogICAgbW9uZzogWzYxNjAsIDYxNjldLAogICAgbXltcjogWzQxNjAsIDQxNjldLAogICAgb3J5YTogWzI5MTgsIDI5MjddLAogICAgdGFtbGRlYzogWzMwNDYsIDMwNTVdLAogICAgdGVsdTogWzMxNzQsIDMxODNdLAogICAgdGhhaTogWzM2NjQsIDM2NzNdLAogICAgdGlidDogWzM4NzIsIDM4ODFdLAogIH07CgogIGNvbnN0IGhhbmlkZWNDaGFycyA9IG51bWJlcmluZ1N5c3RlbXMuaGFuaWRlYy5yZXBsYWNlKC9bXFt8XF1dL2csICIiKS5zcGxpdCgiIik7CgogIGZ1bmN0aW9uIHBhcnNlRGlnaXRzKHN0cikgewogICAgbGV0IHZhbHVlID0gcGFyc2VJbnQoc3RyLCAxMCk7CiAgICBpZiAoaXNOYU4odmFsdWUpKSB7CiAgICAgIHZhbHVlID0gIiI7CiAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgc3RyLmxlbmd0aDsgaSsrKSB7CiAgICAgICAgY29uc3QgY29kZSA9IHN0ci5jaGFyQ29kZUF0KGkpOwoKICAgICAgICBpZiAoc3RyW2ldLnNlYXJjaChudW1iZXJpbmdTeXN0ZW1zLmhhbmlkZWMpICE9PSAtMSkgewogICAgICAgICAgdmFsdWUgKz0gaGFuaWRlY0NoYXJzLmluZGV4T2Yoc3RyW2ldKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgZm9yIChjb25zdCBrZXkgaW4gbnVtYmVyaW5nU3lzdGVtc1VURjE2KSB7CiAgICAgICAgICAgIGNvbnN0IFttaW4sIG1heF0gPSBudW1iZXJpbmdTeXN0ZW1zVVRGMTZba2V5XTsKICAgICAgICAgICAgaWYgKGNvZGUgPj0gbWluICYmIGNvZGUgPD0gbWF4KSB7CiAgICAgICAgICAgICAgdmFsdWUgKz0gY29kZSAtIG1pbjsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICA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iAgICBjb25zdCBvbmUgPSBkaWdpdFJlZ2V4KGxvYyksCiAgICAgIHR3byA9IGRpZ2l0UmVnZXgobG9jLCAiezJ9IiksCiAgICAgIHRocmVlID0gZGlnaXRSZWdleChsb2MsICJ7M30iKSwKICAgICAgZm91ciA9IGRpZ2l0UmVnZXgobG9jLCAiezR9IiksCiAgICAgIHNpeCA9IGRpZ2l0UmVnZXgobG9jLCAiezZ9IiksCiAgICAgIG9uZU9yVHdvID0gZGlnaXRSZWdleChsb2MsICJ7MSwyfSIpLAogICAgICBvbmVUb1RocmVlID0gZGlnaXRSZWdleChsb2MsICJ7MSwzfSIpLAogICAgICBvbmVUb1NpeCA9IGRpZ2l0UmVnZXgobG9jLCAiezEsNn0iKSwKICAgICAgb25lVG9OaW5lID0gZGlnaXRSZWdleChsb2MsICJ7MSw5fSIpLAogICAgICB0d29Ub0ZvdXIgPSBkaWdpdFJlZ2V4KGxvYywgInsyLDR9IiksCiAgICAgIGZvdXJUb1NpeCA9IGRpZ2l0UmVnZXgobG9jLCAiezQsNn0iKSwKICAgICAgbGl0ZXJhbCA9ICh0KSA9PiAoeyByZWdleDogUmVnRXhwKGVzY2FwZVRva2VuKHQudmFsKSksIGRlc2VyOiAoW3NdKSA9PiBzLCBsaXRlcmFsOiB0cnVlIH0pLAogICAgICB1bml0YXRlID0gKHQpID0+IHsKICAgICAgICBpZiAodG9rZW4ubGl0ZXJhbCkgewogICAgICAgICAgcmV0dXJuIGxpdGVyYWwodCk7CiAgICAgICAgfQogICAgICAgIHN3aXRjaCAodC52YWwpIHsKICAgICAgICAgIC8vIGVyYQogICAgICAgICAgY2FzZSAiRyI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2MuZXJhcygic2hvcnQiLCBmYWxzZSksIDApOwogICAgICAgICAgY2FzZSAiR0ciOgogICAgICAgICAgICByZXR1cm4gb25lT2YobG9jLmVyYXMoImxvbmciLCBmYWxzZSksIDApOwogICAgICAgICAgLy8geWVhcnMKICAgICAgICAgIGNhc2UgInkiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChvbmVUb1NpeCk7CiAgICAgICAgICBjYXNlICJ5eSI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KHR3b1RvRm91ciwgdW50cnVuY2F0ZVllYXIpOwogICAgICAgICAgY2FzZSAieXl5eSI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KGZvdXIpOwogICAgICAgICAgY2FzZSAieXl5eXkiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChmb3VyVG9TaXgpOwogICAgICAgICAgY2FzZSAieXl5eXl5IjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQoc2l4KTsKICAgICAgICAgIC8vIG1vbnRocwogICAgICAgICAgY2FzZSAiTSI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgIk1NIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIGNhc2UgIk1NTSI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2MubW9udGhzKCJzaG9ydCIsIHRydWUsIGZhbHNlKSwgMSk7CiAgICAgICAgICBjYXNlICJNTU1NIjoKICAgICAgICAgICAgcmV0dXJuIG9uZU9mKGxvYy5tb250aHMoImxvbmciLCB0cnVlLCBmYWxzZSksIDEpOwogICAgICAgICAgY2FzZSAiTCI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgIkxMIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIGNhc2UgIkxMTCI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2MubW9udGhzKCJzaG9ydCIsIGZhbHNlLCBmYWxzZSksIDEpOwogICAgICAgICAgY2FzZSAiTExMTCI6CiAgICAgICAgICAgIHJldHVybiBvbmVPZihsb2MubW9udGhzKCJsb25nIiwgZmFsc2UsIGZhbHNlKSwgMSk7CiAgICAgICAgICAvLyBkYXRlcwogICAgICAgICAgY2FzZSAiZCI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgImRkIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIC8vIG9yZGluYWxzCiAgICAgICAgICBjYXNlICJvIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQob25lVG9UaHJlZSk7CiAgICAgICAgICBjYXNlICJvb28iOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdCh0aHJlZSk7CiAgICAgICAgICAvLyB0aW1lCiAgICAgICAgICBjYXNlICJISCI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KHR3byk7CiAgICAgICAgICBjYXNlICJIIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQob25lT3JUd28pOwogICAgICAgICAgY2FzZSAiaGgiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdCh0d28pOwogICAgICAgICAgY2FzZSAiaCI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgIm1tIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIGNhc2UgIm0iOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChvbmVPclR3byk7CiAgICAgICAgICBjYXNlICJxIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQob25lT3JUd28pOwogICAgICAgICAgY2FzZSAicXEiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdCh0d28pOwogICAgICAgICAgY2FzZSAicyI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgInNzIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIGNhc2UgIlMiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChvbmVUb1RocmVlKTsKICAgICAgICAgIGNhc2UgIlNTUyI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KHRocmVlKTsKICAgICAgICAgIGNhc2UgInUiOgogICAgICAgICAgICByZXR1cm4gc2ltcGxlKG9uZVRvTmluZSk7CiAgICAgICAgICBjYXNlICJ1dSI6CiAgICAgICAgICAgIHJldHVybiBzaW1wbGUob25lT3JUd28pOwogICAgICAgICAgY2FzZSAidXV1IjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQob25lKTsKICAgICAgICAgIC8vIG1lcmlkaWVtCiAgICAgICAgICBjYXNlICJhIjoKICAgICAgICAgICAgcmV0dXJuIG9uZU9mKGxvYy5tZXJpZGllbXMoKSwgMCk7CiAgICAgICAgICAvLyB3ZWVrWWVhciAoaykKICAgICAgICAgIGNhc2UgImtra2siOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChmb3VyKTsKICAgICAgICAgIGNhc2UgImtrIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvVG9Gb3VyLCB1bnRydW5jYXRlWWVhcik7CiAgICAgICAgICAvLyB3ZWVrTnVtYmVyIChXKQogICAgICAgICAgY2FzZSAiVyI6CiAgICAgICAgICAgIHJldHVybiBpbnRVbml0KG9uZU9yVHdvKTsKICAgICAgICAgIGNhc2UgIldXIjoKICAgICAgICAgICAgcmV0dXJuIGludFVuaXQodHdvKTsKICAgICAgICAgIC8vIHdlZWtkYXlzCiAgICAgICAgICBjYXNlICJFIjoKICAgICAgICAgIGNhc2UgImMiOgogICAgICAgICAgICByZXR1cm4gaW50VW5pdChvbmUpOwogICAgICAgICAgY2FzZSAiRUVFIjoKICAgICAgICAgICAgcmV0dXJuIG9uZU9mKGxvYy53ZWVrZGF5cygic2hvcnQiLCBmYWxzZSwgZmFsc2UpLCAxKTsKICAgICAgICAgIGNhc2UgIkVFRUUiOgogICAgICAgICAgICByZXR1cm4gb25lT2YobG9jLndlZWtkYXlzKCJsb25nIiwgZmFsc2UsIGZhbHNlKSwgMSk7CiAgICAgICAgICBjYXNlICJjY2MiOgogICAgICAgICAgICByZXR1cm4gb25lT2YobG9jLndlZWtkYXlzKCJzaG9ydCIsIHRydWUsIGZhbHNlKSwgMSk7CiAgICAgICAgICBjYXNlICJjY2NjIjoKICAgICAgICAgICAgcmV0dXJuIG9uZU9mKGxvYy53ZWVrZGF5cygibG9uZyIsIHRydWUsIGZhbHNlKSwgMSk7CiAgICAgICAgICAvLyBvZmZzZXQvem9uZQogICAgICAgICAgY2FzZSAiWiI6CiAgICAgICAgICBjYXNlICJaWiI6CiAgICAgICAgICAgIHJldHVybiBvZmZzZXQobmV3IFJlZ0V4cChgKFsrLV0ke29uZU9yVHdvLnNvdXJjZX0pKD86Oigke3R3by5zb3VyY2V9KSk/YCksIDIpOwogICAgICAgICAgY2FzZSAiWlpaIjoKICAgICAgICAgICAgcmV0dXJuIG9mZnNldChuZXcgUmVnRXhwKGAoWystXSR7b25lT3JUd28uc291cmNlfSkoJHt0d28uc291cmNlfSk/YCksIDIpOwogICAgICAgICAgLy8gd2UgZG9uJ3Qgc3VwcG9ydCBaWlpaIChQU1QpIG9yIFpaWlpaIChQYWNpZmljIFN0YW5kYXJkIFRpbWUpIGluIHBhcnNpbmcKICAgICAgICAgIC8vIGJlY2F1c2Ugd2UgZG9uJ3QgaGF2ZSBhbnkgd2F5IHRvIGZpZ3VyZSBvdXQgd2hhdCB0aGV5IGFyZQogICAgICAgICAgY2FzZSAieiI6CiAgICAgICAgICAgIHJldHVybiBzaW1wbGUoL1thLXpfKy0vXXsxLDI1Nn0/L2kpOwogICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgcmV0dXJuIGxpdGVyYWwodCk7CiAgICAgICAgfQogICAgICB9OwoKICAgIGNvbnN0IHVuaXQgPSB1bml0YXRlKHRva2VuKSB8fCB7CiAgICAgIGludmFsaWRSZWFzb246IE1JU1NJTkdfRlRQLAogICAgfTsKCiAgICB1bml0LnRva2VuID0gdG9rZW47CgogICAgcmV0dXJuIHVuaXQ7CiAgfQoKICBjb25zdCBwYXJ0VHlwZVN0eWxlVG9Ub2tlblZhbCA9IHsKICAgIHllYXI6IHsKICAgICAgIjItZGlnaXQiOiAieXkiLAogICAgICBudW1lcmljOiAieXl5eXkiLAogICAgfSwKICAgIG1vbnRoOiB7CiAgICAgIG51bWVyaWM6ICJNIiwKICAgICAgIjItZGlnaXQiOiAiTU0iLAogICAgICBzaG9ydDogIk1NTSIsCiAgICAgIGxvbmc6ICJNTU1NIiwKICAgIH0sCiAgICBkYXk6IHsKICAgICAgbnVtZXJpYzogImQiLAogICAgICAiMi1kaWdpdCI6ICJkZCIsCiAgICB9LAogICAgd2Vla2RheTogewogICAgICBzaG9ydDogIkVFRSIsCiAgICAgIGxvbmc6ICJFRUVFIiwKICAgIH0sCiAgICBkYXlwZXJpb2Q6ICJhIiwKICAgIGRheVBlcmlvZDogImEiLAogICAgaG91cjogewogICAgICBudW1lcmljOiAiaCIsCiAgICAgICIyLWRpZ2l0IjogImhoIiwKICAgIH0sCiAgICBtaW51dGU6IHsKICAgICAgbnVtZXJpYzogIm0iLAogICAgICAiMi1kaWdpdCI6ICJtbSIsCiAgICB9LAogICAgc2Vjb25kOiB7CiAgICAgIG51bWVyaWM6ICJzIiwKICAgICAgIjItZGlnaXQiOiAic3MiLAogICAgfSwKICAgIHRpbWVab25lTmFtZTogewogICAgICBsb25nOiAiWlpaWloiLAogICAgICBzaG9ydDogIlpaWiIsCiAgICB9LAogIH07CgogIGZ1bmN0aW9uIHRva2VuRm9yUGFydChwYXJ0LCBmb3JtYXRPcHRzKSB7CiAgICBjb25zdCB7IHR5cGUsIHZhbHVlIH0gPSBwYXJ0OwoKICAgIGlmICh0eXBlID09PSAibGl0ZXJhbCIpIHsKICAgICAgcmV0dXJuIHsKICAgICAgICBsaXRlcmFsOiB0cnVlLAogICAgICAgIHZhbDogdmFsdWUsCiAgICAgIH07CiAgICB9CgogICAgY29uc3Qgc3R5bGUgPSBmb3JtYXR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AgICAgICAgIG1hdGNoSW5kZXggKz0gZ3JvdXBzOwogICAgICAgIH0KICAgICAgfQogICAgICByZXR1cm4gW21hdGNoZXMsIGFsbF07CiAgICB9IGVsc2UgewogICAgICByZXR1cm4gW21hdGNoZXMsIHt9XTsKICAgIH0KICB9CgogIGZ1bmN0aW9uIGRhdGVUaW1lRnJvbU1hdGNoZXMobWF0Y2hlcykgewogICAgY29uc3QgdG9GaWVsZCA9ICh0b2tlbikgPT4gewogICAgICBzd2l0Y2ggKHRva2VuKSB7CiAgICAgICAgY2FzZSAiUyI6CiAgICAgICAgICByZXR1cm4gIm1pbGxpc2Vjb25kIjsKICAgICAgICBjYXNlICJzIjoKICAgICAgICAgIHJldHVybiAic2Vjb25kIjsKICAgICAgICBjYXNlICJtIjoKICAgICAgICAgIHJldHVybiAibWludXRlIjsKICAgICAgICBjYXNlICJoIjoKICAgICAgICBjYXNlICJIIjoKICAgICAgICAgIHJldHVybiAiaG91ciI7CiAgICAgICAgY2FzZSAiZCI6CiAgICAgICAgICByZXR1cm4gImRheSI7CiAgICAgICAgY2FzZSAibyI6CiAgICAgICAgICByZXR1cm4gIm9yZGluYWwiOwogICAgICAgIGNhc2UgIkwiOgogICAgICAgIGNhc2UgIk0iOgogICAgICAgICAgcmV0dXJuICJtb250aCI7CiAgICAgICAgY2FzZSAieSI6CiAgICAgICAgICByZXR1cm4gInllYXIiOwogICAgICAgIGNhc2UgIkUiOgogICAgICAgIGNhc2UgImMiOgogICAgICAgICAgcmV0dXJuICJ3ZWVrZGF5IjsKICAgICAgICBjYXNlICJXIjoKICAgICAgICAgIHJldHVybiAid2Vla051bWJlciI7CiAgICAgICAgY2FzZSAiayI6CiAgICAgICAgICByZXR1cm4gIndlZWtZZWFyIjsKICAgICAgICBjYXNlICJxIjoKICAgICAgICAgIHJldHVybiAicXVhcnRlciI7CiAgICAgICAgZGVmYXVsdDoKICAgICAgICAgIHJldHVybiBudWxsOwogICAgICB9CiAgICB9OwoKICAgIGxldCB6b25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VVENNaW51dGVzKCksCiAgICAgIHNlY29uZDogZC5nZXRVVENTZWNvbmRzKCksCiAgICAgIG1pbGxpc2Vjb25kOiBkLmdldFVUQ01pbGxpc2Vjb25kcygpLAogICAgfTsKICB9CgogIC8vIGNvbnZlcnQgYSBjYWxlbmRhciBvYmplY3QgdG8gYSBlcG9jaCB0aW1lc3RhbXAKICBmdW5jdGlvbiBvYmpUb1RTKG9iaiwgb2Zmc2V0LCB6b25lKSB7CiAgICByZXR1cm4gZml4T2Zmc2V0KG9ialRvTG9jYWxUUyhvYmopLCBvZmZzZXQsIHpvbmUpOwogIH0KCiAgLy8gY3JlYXRlIGEgbmV3IERUIGluc3RhbmNlIGJ5IGFkZGluZyBhIGR1cmF0aW9uLCBhZGp1c3RpbmcgZm9yIERTVHMKICBmdW5jdGlvbiBhZGp1c3RUaW1lKGluc3QsIGR1cikgewogICAgY29uc3Qgb1ByZSA9IGluc3QubywKICAgICAgeWVhciA9IGluc3QuYy55ZWFyICsgTWF0aC50cnVuYyhkdXIueWVhcnMpLAogICAgICBtb250aCA9IGluc3QuYy5tb250aCArIE1hdGgudHJ1bmMoZHVyLm1vbnRocykgKyBNYXRoLnRydW5jKGR1ci5xdWFydGVycykgKiAzLAogICAgICBjID0gewogICAgICAgIC4uLmluc3QuYywKICAgICAgICB5ZWFyLAogICAgICAgIG1vbnRoLAogICAgICAgIGRheToKICAgICAgICAgIE1hdGgubWluKGluc3QuYy5kYXksIGRheXNJbk1vbnRoKHllYXIsIG1vbnRoKSkgKwogICAgICAgICAgTWF0aC50cnVuYyhkdXIuZGF5cykgKwogICAgICAgICAgTWF0aC50cnVuYyhkdXIud2Vla3MpICogNywKICAgICAgfSwKICAgICAgbWlsbGlzVG9BZGQgPSBEdXJhdGlvbi5mcm9tT2JqZWN0KHsKICAgICAgICB5ZWFyczogZHVyLnllYXJzIC0gTWF0aC50cnVuYyhkdXIueWVhcnMpLAogICAgICAgIHF1YXJ0ZXJzOiBkdXIucXVhcnRlcnMgL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gPyB0cnVlIDogb3B0cy5yb3VuZCwKICAgICAgZm9ybWF0ID0gKGMsIHVuaXQpID0+IHsKICAgICAgICBjID0gcm91bmRUbyhjLCByb3VuZCB8fCBvcHRzLmNhbGVuZGFyeSA/IDAgOiAyLCB0cnVlKTsKICAgICAgICBjb25zdCBmb3JtYXR0ZXIgPSBlbmQubG9jLmNsb25lKG9wdHMpLnJlbEZvcm1hdHRlcihvcHRzKTsKICAgICAgICByZXR1cm4gZm9ybWF0dGVyLmZvcm1hdChjLCB1bml0KTsKICAgICAgfSwKICAgICAgZGlmZmVyID0gKHVuaXQpID0+IHsKICAgICAgICBpZiAob3B0cy5jYWxlbmRhcnkpIHsKICAgICAgICAgIGlmICghZW5kLmhhc1NhbWUoc3RhcnQsIHVuaXQpKSB7CiAgICAgICAgICAgIHJldHVybiBlbmQuc3RhcnRPZih1bml0KS5kaWZmKHN0YXJ0LnN0YXJ0T2YodW5pdCksIHVuaXQpLmdldCh1bml0KTsKICAgICAgICAgIH0gZWxzZSByZXR1cm4gMDsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgcmV0dXJuIGVuZC5kaWZmKHN0YXJ0LCB1bml0KS5nZXQodW5pdCk7CiAgICAgICAgfQogICAgICB9OwoKICAgIGlmIChvcHRzLnVuaXQpIHsKICAgICAgcmV0dXJuIGZvcm1hdChkaWZmZXIob3B0cy51bml0KSwgb3B0cy51bml0KTsKICAgIH0KCiAgICBmb3IgKGNvbnN0IHVuaXQgb2Ygb3B0cy51bml0cykgewogICAgICBjb25zdCBjb3VudCA9IGRpZmZlcih1bml0KTsKICAgICAgaWYgKE1hdGguYWJzKGNvdW50KSA+PSAxKSB7CiAgICAgICAgcmV0dXJuIGZvcm1hdChjb3VudCwgdW5pdCk7CiAgICAgIH0KICAgIH0KICAgIHJldHVybiBmb3JtYXQoc3RhcnQgPiBlbmQgPyAtMCA6IDAsIG9wdHMudW5pdHNbb3B0cy51bml0cy5sZW5ndGggLSAxXSk7CiAgfQoKICBmdW5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U7CgogICAgICBsZXQgaW52YWxpZCA9CiAgICAgICAgY29uZmlnLmludmFsaWQgfHwKICAgICAgICAoTnVtYmVyLmlzTmFOKGNvbmZpZy50cykgPyBuZXcgSW52YWxpZCgiaW52YWxpZCBpbnB1dCIpIDogbnVsbCkgfHwKICAgICAgICAoIXpvbmUuaXNWYWxpZCA/IHVuc3VwcG9ydGVkWm9uZSh6b25lKSA6IG51bGwpOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLnRzID0gaXNVbmRlZmluZWQoY29uZmlnLnRzKSA/IFNldHRpbmdzLm5vdygpIDogY29uZmlnLnRzOwoKICAgICAgbGV0IGMgPSBudWxsLAogICAgICAgIG8gPSBudWxsOwogICAgICBpZiAoIWludmFsaWQpIHsKICAgICAgICBjb25zdCB1bmNoYW5nZWQgPSBjb25maWcub2xkICYmIGNvbmZpZy5vbGQudHMgPT09IHRoaXMudHMgJiYgY29uZmlnLm9sZC56b25lLmVxdWFscyh6b25lKTsKCiAgICAgICAgaWYgKHVuY2hhbmdlZCkgewogICAgICAgICAgW2MsIG9dID0gW2NvbmZpZy5vbGQuYywgY29uZmlnLm9sZC5vXTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgY29uc3Qgb3QgPSB6b25lLm9mZnNldCh0aGlzLnRzKTsKICAgICAgICAgIGMgPSB0c1RvT2JqKHRoaXMudHMsIG90KTsKICAgICAgICAgIGludmFsaWQgPSBOdW1iZXIuaXNOYU4oYy55ZWFyKSA/IG5ldyBJbnZhbGlkKCJpbnZhbGlkIGlucHV0IikgOiBudWxsOwogICAgICAgICAgYyA9IGludmFsaWQgPyBudWxsIDogYzsKICAgICAgICAgIG8gPSBpbnZhbGlkID8gbnVsbCA6IG90OwogICAgICAgIH0KICAgICAgfQoKICAgICAgLyoqCiAgICAgICAqIEBhY2Nlc3MgcHJpdmF0ZQogICAgICAgKi8KICAgICAgdGhpcy5fem9uZSA9IHpvbmU7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMubG9jID0gY29uZmlnLmxvYyB8fCBMb2NhbGUuY3JlYXRlKCk7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMuaW52YWxpZCA9IGludmFsaWQ7CiAgICAgIC8qKgogICAgICAgKiBAYWNjZXNzIHByaXZhdGUKICAgICAgICovCiAgICAgIHRoaXMud2Vla0RhdGEgPSBudWxsOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLmMgPSBjOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLm8gPSBvOwogICAgICAvKioKICAgICAgICogQGFjY2VzcyBwcml2YXRlCiAgICAgICAqLwogICAgICB0aGlzLmlzTHV4b25EYXRlVGltZSA9IHRydWU7CiAgICB9CgogICAgLy8gQ09OU1RSVUNUCgogICAgLyoqCiAgICAgKiBDcmVhdGUgYSBEYXRlVGltZSBmb3IgdGhlIGN1cnJlbnQgaW5zdGFudCwgaW4gdGhlIHN5c3RlbSdzIHRpbWUgem9uZS4KICAgICAqCiAgICAgKiBVc2UgU2V0dGluZ3MgdG8gb3ZlcnJpZGUgdGhlc2UgZGVmYXVsdCB2YWx1ZXMgaWYgbmVlZGVkLgogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUubm93KCkudG9JU08oKSAvL34+IG5vdyBpbiB0aGUgSVNPIGZvcm1hdAogICAgICogQHJldHVybiB7RGF0ZVRpbWV9CiAgICAgKi8KICAgIHN0YXRpYyBub3coKSB7CiAgICAgIHJldHVybiBuZXcgRGF0ZVRpbWUoe30pOwogICAgfQoKICAgIC8qKgogICAgICogQ3JlYXRlIGEgbG9jYWwgRGF0ZVRpbWUKICAgICAqIEBwYXJhbSB7bnVtYmVyfSBbeWVhcl0gLSBUaGU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gICAgcmV0dXJuIHRoaXMuem9uZS5vZmZzZXROYW1lKHRoaXMudHMsIHsKICAgICAgICAgIGZvcm1hdDogImxvbmciLAogICAgICAgICAgbG9jYWxlOiB0aGlzLmxvY2FsZSwKICAgICAgICB9KTsKICAgICAgfSBlbHNlIHsKICAgICAgICByZXR1cm4gbnVsbDsKICAgICAgfQogICAgfQoKICAgIC8qKgogICAgICogR2V0IHdoZXRoZXIgdGhpcyB6b25lJ3Mgb2Zmc2V0IGV2ZXIgY2hhbmdlcywgYXMgaW4gYSBEU1QuCiAgICAgKiBAdHlwZSB7Ym9vbGVhbn0KICAgICAqLwogICAgZ2V0IGlzT2Zmc2V0Rml4ZWQoKSB7CiAgICAgIHJldHVybiB0aGlzLmlzVmFsaWQgPyB0aGlzLnpvbmUuaXNVbml2ZXJzYWwgOiBudWxsOwogICAgfQoKICAgIC8qKgogICAgICogR2V0IHdoZXRoZXIgdGhlIERhdGVUaW1lIGlzIGluIGEgRFNULgogICAgICogQHR5cGUge2Jvb2xlYW59CiAgICAgKi8KICAgIGdldCBpc0luRFNUKCkgewogICAgICBpZiAodGhpcy5pc09mZnNldEZpeGVkKSB7CiAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICB9IGVsc2UgewogICAgICAgIHJldHVybiAoCiAgICAgICAgICB0aGlzLm9mZnNldCA+IHRoaXMuc2V0KHsgbW9udGg6IDEsIGRheTogMSB9KS5vZmZzZXQgfHwKICAgICAgICAgIHRoaXMub2Zmc2V0ID4gdGhpcy5zZXQoeyBtb250aDogNSB9KS5vZmZzZXQKICAgICAgICApOwogICAgICB9CiAgICB9CgogICAgLyoqCiAgICAgKiBSZXR1cm5zIHRydWUgaWYgdGhpcyBEYXRlVGltZSBpcyBpbiBhIGxlYXAgeWVhciwgZmFsc2Ugb3RoZXJ3aXNlCiAgICAgKiBAZXhhbXBsZSBEYXRlVGltZS5sb2NhbCgyMDE2KS5pc0luTGVhcFllYXIgLy89PiB0cnVlCiAgICAgKiBAZXhhbX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JqZWN0fSAtIEludGwuRGF0ZVRpbWVGb3JtYXQgY29uc3RydWN0b3Igb3B0aW9ucywgc2FtZSBhcyBgdG9Mb2NhbGVTdHJpbmdgLgogICAgICogQGV4YW1wbGUgRGF0ZVRpbWUubm93KCkudG9Mb2NhbGVQYXJ0cygpOyAvLz0+IFsKICAgICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLz0+ICAgeyB0eXBlOiAnZGF5JywgdmFsdWU6ICcyNScgfSwKICAgICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLz0+ICAgeyB0eXBlOiAnbGl0ZXJhbCcsIHZhbHVlOiAnLycgfSwKICAgICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLz0+ICAgeyB0eXBlOiAnbW9udGgnLCB2YWx1ZTogJzA1JyB9LAogICAgICogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vPT4gICB7IHR5cGU6ICdsaXRlcmFsJywgdmFsdWU6ICcvJyB9LAogICAgICogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vPT4gICB7IHR5cGU6ICd5ZWFyJywgdmFsdWU6ICcxOTgyJyB9CiAgICAgKiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy89PiBdCiAgICAgKi8KICAgIHRvTG9jYWxlUGFydHMob3B0cyA9IHt9KSB7CiAgICAgIHJldHVybiB0aGlzLmlzVmFsaWQKICAgICAgICA/IEZvcm1hdHRlci5jcmVhdGUodGhpcy5sb2MuY2xvbmUob3B0cyksIG9wdHMpLmZvcm1hdERhdGVUaW1lUGFydHModGhpcykKICAgICAgICA6IFtdOwogICAgfQoKICAgIC8qKgogICAgICogUmV0dXJucyBhbiBJU08gODYwMS1jb21wbGlhbnQgc3RyaW5nIHJlcHJlc2VudGF0aW9uIG9mIHRoaXMgRGF0ZVRpbWUKICAgICAqIEBwYXJhbSB7T2JqZWN0fSBvcHRzIC0gb3B0aW9ucwogIC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Ki8KICAgIHN0YXRpYyBnZXQgREFURV9NRURfV0lUSF9XRUVLREFZKCkgewogICAgICByZXR1cm4gREFURV9NRURfV0lUSF9XRUVLREFZOwogICAgfQoKICAgIC8qKgogICAgICoge0BsaW5rIERhdGVUaW1lI3RvTG9jYWxlU3RyaW5nfSBmb3JtYXQgbGlrZSAnT2N0b2JlciAxNCwgMTk4MycKICAgICAqIEB0eXBlIHtPYmplY3R9CiAgICAgKi8KICAgIHN0YXRpYyBnZXQgREFURV9GVUxMKCkgewogICAgICByZXR1cm4gREFURV9GVUxMOwogICAgfQoKICAgIC8qKgogICAgICoge0BsaW5rIERhdGVUaW1lI3RvTG9jYWxlU3RyaW5nfSBmb3JtYXQgbGlrZSAnVHVlc2RheSwgT2N0b2JlciAxNCwgMTk4MycKICAgICAqIEB0eXBlIHtPYmplY3R9CiAgICAgKi8KICAgIHN0YXRpYyBnZXQgREFURV9IVUdFKCkgewogICAgICByZXR1cm4gREFURV9IVUdFOwogICAgfQoKICAgIC8qKgogICAgICoge0BsaW5rIERhdGVUaW1lI3RvTG9jYWxlU3RyaW5nfSBmb3JtYXQgbGlrZSAnMDk6MzAgQU0nLiBPbmx5IDEyLWhvdXIgaWYgdGhlIGxvY2FsZSBpcy4KICAgICAqIEB0eXBlIHtPYmplY3R9CiAgICAgKi8KICAgIHN0YXRpYyBnZXQgVElNRV9TSU1QTEUoKSB7CiAgICAgIHJldHVybiBUSU1FX1NJTVBMRTsKICAgIH0KCiAgICAvKioKICAgICAqIHtAbGluayBEYXRlVGltZSN0b0xvY2FsZVN0cmluZ30gZm9ybWF0IGxpa2UgJzA5OjMwOjIzIEFNJy4gT25seSAxMi1ob3VyIGlmIHRoZSBsb2NhbGUgaXMuCiAgICAgKiBAdHlwZSB7T2JqZWN0fQogICAgICovCiAgICBzdGF0aWMgZ2V0IFRJTUVfV0lUSF9TRUNPTkRTKCkgewogICAgICByZXR1cm4gVElNRV9XSVRIX1NFQ09ORFM7CiAgICB9CgogICAgLyoqC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VF1ZXJpZXNJbkNvZGVibG9ja3M6IHRydWUsDQogICAgICAgICAgZW5hYmxlSW5saW5lRGF0YXZpZXc6IHRydWUsDQogICAgICAgICAgZW5hYmxlRGF0YXZpZXdKczogZmFsc2UsDQogICAgICAgICAgZW5hYmxlSW5saW5lRGF0YXZpZXdKczogZmFsc2UsDQogICAgICAgICAgcHJldHR5UmVuZGVySW5saW5lRmllbGRzOiB0cnVlLA0KICAgICAgICAgIGRhdGF2aWV3SnNLZXl3b3JkOiAiZGF0YXZpZXdqcyIsDQogICAgICB9LA0KICB9KTsKCiAgLyoqIEZ1bmN0aW9uYWwgcmV0dXJuIHR5cGUgZm9yIGVycm9yIGhhbmRsaW5nLiAqLw0KICBjbGFzcyBTdWNjZXNzIHsNCiAgICAgIGNvbnN0cnVjdG9yKHZhbHVlKSB7DQogICAgICAgICAgdGhpcy52YWx1ZSA9IHZhbHVlOw0KICAgICAgICAgIHRoaXMuc3VjY2Vzc2Z1bCA9IHRydWU7DQogICAgICB9DQogICAgICBtYXAoZikgew0KICAgICAgICAgIHJldHVybiBuZXcgU3VjY2VzcyhmKHRoaXMudmFsdWUpKTsNCiAgICAgIH0NCiAgICAgIGZsYXRNYXAoZikgew0KICAgICAgICAgIHJldHVybiBmKHRoaXMudmFsdWUpOw0KICAgICAgfQ0KICAgICAgbWFwRXJyKGYpIHsNCiAgICAgICAgICByZXR1cm4gdGhpczsNCiAgICAgIH0NCiAgICAgIGJpbWFwKHN1Y2MsIF9mYWlsKSB7DQogICAgICAgICAgcmV0dXJuIHRoaXMubWFwKHN1Y2MpOw0KICAgICAgfQ0KICAgICAgb3JFbHNlKF92YWx1ZSkgew0KICAgICAgICAgIHJldHVybiB0aGlzLnZhbHVlOw0KICAgICAgfQ0KICAgICAgY2FzdCgpIHsNCiAgICAgICAgICByZXR1cm4gdGhpczsNCiAgICAgIH0NCiAgICAgIG9yRWxzZVRocm93KF9tZXNzYWdlKSB7DQogICAgICAgICAgcmV0dXJuIHRoaXMudmF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lx1REZDN10oPzpcdUQ4M0NbXHVERkZCLVx1REZGRl0pP3xbXHVERkMzXHVERkM0XHVERkNBXSg/Olx1RDgzQ1tcdURGRkItXHVERkZGXSk/KD86XHUyMDBEW1x1MjY0MFx1MjY0Ml1cdUZFMEY/KT98W1x1REZDQlx1REZDQ10oPzpcdUZFMEZ8XHVEODNDW1x1REZGQi1cdURGRkZdKT8oPzpcdTIwMERbXHUyNjQwXHUyNjQyXVx1RkUwRj8pP3xbXHVEQ0NGXHVERDhFXHVERDkxLVx1REQ5QVx1REUwMVx1REUxQVx1REUyRlx1REUzMi1cdURFMzZcdURFMzgtXHVERTNBXHVERTUwXHVERTUxXHVERjAwLVx1REYyMFx1REYyRC1cdURGMzVcdURGMzctXHVERjdDXHVERjdFLVx1REY4NFx1REY4Ni1cdURGOTNcdURGQTAtXHVERkMxXHVERkM1XHVERkM2XHVERkM4XHVERkM5XHVERkNGLVx1REZEM1x1REZFMC1cdURGRjBcdURGRjgtXHVERkZGXXxcdURERTZcdUQ4M0NbXHVEREU4LVx1RERFQ1x1RERFRVx1RERGMVx1RERGMlx1RERGNFx1RERGNi1cdURERkFcdURERkNcdURERkRcdURERkZdfFx1RERFN1x1RDgzQ1tcdURERTZcdURERTdcdURERTktXHVEREVGXHVEREYxLVx1RERGNFx1RERGNi1cdURERjlcdURERkJcdURERkNcdURERkVcdURERkZdfFx1RERFOFx1RDgzQ1tcdURERTZcdURERThcdURERTlcdURERUItXHVEREVFXHVEREYwLVx1RERGNVx1RERGN1x1RERGQS1cdURERkZdfFx1RERFOVx1RDgzQ1tcdURERUFcdURERUNcdURERUZcdURERjBcdURERjJcdURERjRcdURERkZdfFx1RERFQVx1RDgzQ1tcdURERTZcdURERThcdURERUFcdURERUNcdURERURcdURERjctXHVEREZBXXxcdURERUJcdUQ4M0NbXHVEREVFLVx1RERGMFx1RERGMlx1RERGNFx1RERGN118XHVEREVDXHVEODNDW1x1RERFNlx1RERFN1x1RERFOS1cdURERUVcdURERjEtXHVEREYzXHVEREY1LVx1RERGQVx1RERGQ1x1RERGRV18XHVEREVEXHVEODNDW1x1RERGMFx1RERGMlx1RERGM1x1RERGN1x1RERGOVx1RERGQV18XHVEREVFXHVEODNDW1x1RERFOC1cdURERUFcdURERjEtXHVEREY0XHVEREY2LVx1RERGOV18XHVEREVGXHVEODNDW1x1RERFQVx1RERGMlx1RERGNFx1RERGNV18XHVEREYwXHVEODNDW1x1RERFQVx1RERFQy1cdURERUVcdURERjJcdURERjNcdURERjVcdURERjdcdURERkNcdURERkVcdURERkZdfFx1RERGMVx1RDgzQ1tcdURERTYtXHVEREU4XHVEREVFXHVEREYwXHVEREY3LVx1RERGQlx1RERGRV18XHVEREYyXHVEODNDW1x1RERFNlx1RERFOC1cdURERURcdURERjAtXHVEREZGXXxcdURERjNcdUQ4M0NbXHVEREU2XHVEREU4XHVEREVBLVx1RERFQ1x1RERFRVx1RERGMVx1RERGNFx1RERGNVx1RERGN1x1RERGQVx1RERGRl18XHVEREY0XHVEODNDXHVEREYyfFx1RERGNVx1RDgzQ1tcdURERTZcdURERUEtXHVEREVEXHVEREYwLVx1RERGM1x1RERGNy1cdURERjlcdURERkNcdURERkVdfFx1RERGNlx1RDgzQ1x1RERFNnxcdURERjdcdUQ4M0NbXHVEREVBXHVEREY0XHVEREY4XHVEREZBXHVEREZDXXxcdURERjhcdUQ4M0NbXHVEREU2LVx1RERFQVx1RERFQy1cdURERjRcdURERjctXHVEREY5XHVEREZCXHVEREZELVx1RERGRl18XHVEREY5XHVEODNDW1x1RERFNlx1RERFOFx1RERFOVx1RERFQi1cdURERURcdURERUYtXHVEREY0XHVEREY3XHVEREY5XHVEREZCXHVEREZDXHVEREZGXXxcdURERkFcdUQ4M0NbXHVEREU2XHVEREVDXHVEREYyXHVEREYzXHVEREY4XHVEREZFXHVEREZGXXxcdURERkJcdUQ4M0NbXHVEREU2XHVEREU4XHVEREVBXHVEREVDXHVEREVFXHVEREYzXHVEREZBXXxcdURERkNcdUQ4M0NbXHV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Fx1REUzNSg/Olx1MjAwRFx1RDgzRFx1RENBQik/fFx1REUzNig/Olx1MjAwRFx1RDgzQ1x1REYyQlx1RkUwRj8pPyl8XHVEODNFKD86W1x1REQwQ1x1REQwRlx1REQxOC1cdUREMUZcdUREMzAtXHVERDM0XHVERDM2XHVERDc3XHVEREI1XHVEREI2XHVEREJCXHVEREQyXHVEREQzXHVEREQ1XHVERUMzLVx1REVDNVx1REVGMFx1REVGMi1cdURFRjZdKD86XHVEODNDW1x1REZGQi1cdURGRkZdKT98W1x1REQyNlx1REQzNVx1REQzNy1cdUREMzlcdUREM0RcdUREM0VcdUREQjhcdUREQjlcdUREQ0QtXHVERENGXHVEREQ0XHVEREQ2LVx1RERERF0oPzpcdUQ4M0NbXHVERkZCLVx1REZGRl0pPyg/Olx1MjAwRFtcdTI2NDBcdTI2NDJdXHVGRTBGPyk/fFtcdUREREVcdUREREZdKD86XHUyMDBEW1x1MjY0MFx1MjY0Ml1cdUZFMEY/KT98W1x1REQwRFx1REQwRVx1REQxMC1cdUREMTdcdUREMjAtXHVERDI1XHVERDI3LVx1REQyRlx1REQzQVx1REQzRi1cdURENDVcdURENDctXHVERDc2XHVERDc4LVx1RERCNFx1RERCN1x1RERCQVx1RERCQy1cdUREQ0NcdURERDBcdURERTAtXHVEREZGXHVERTcwLVx1REU3NFx1REU3OC1cdURFN0NcdURFODAtXHVERTg2XHVERTkwLVx1REVBQ1x1REVCMC1cdURFQkFcdURFQzAtXHVERUMyXHVERUQwLVx1REVEOVx1REVFMC1cdURFRTddfFx1REQzQyg/Olx1MjAwRFtcdTI2NDBcdTI2NDJdXHVGRTBGP3xcdUQ4M0NbXHVERkZCLVx1REZGRl0pP3xcdURERDEoPzpcdTIwMEQoPzpbXHUyNjk1XHUyNjk2XHUyNzA4XVx1RkUwRj98XHVEODNDW1x1REYzRVx1REY3M1x1REY3Q1x1REY4NFx1REY5M1x1REZBNFx1REZBOFx1REZFQlx1REZFRF18XHVEODNEW1x1RENCQlx1RENCQ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iBkdXIudG9IdW1hbigpOw0KICB9CgogIHZhciBWYWx1ZXM7DQogIChmdW5jdGlvbiAoVmFsdWVzKSB7DQogICAgICAvKiogQ29udmVydCBhbiBhcmJpdHJhcnkgdmFsdWUgaW50byBhIHJlYXNvbmFibGUsIE1hcmtkb3duLWZyaWVuZGx5IHN0cmluZyBpZiBwb3NzaWJsZS4gKi8NCiAgICAgIGZ1bmN0aW9uIHRvU3RyaW5nKGZpZWxkLCBzZXR0aW5nID0gREVGQVVMVF9RVUVSWV9TRVRUSU5HUywgcmVjdXJzaXZlID0gZmFsc2UpIHsNCiAgICAgICAgICBsZXQgd3JhcHBlZCA9IHdyYXBWYWx1ZShmaWVsZCk7DQogICAgICAgICAgaWYgKCF3cmFwcGVkKQ0KICAgICAgICAgICAgICByZXR1cm4gc2V0dGluZy5yZW5kZXJOdWxsQXM7DQogICAgICAgICAgc3dpdGNoICh3cmFwcGVkLnR5cGUpIHsNCiAgICAgICAgICAgICAgY2FzZSAibnVsbCI6DQogICAgICAgICAgICAgICAgICByZXR1cm4gc2V0dGluZy5yZW5kZXJOdWxsQXM7DQogICAgICAgICAgICAgIGNhc2UgInN0cmluZyI6DQogICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcHBlZC52YWx1ZTsNCiAgICAgICAgICAgICAgY2FzZSAibnVtYmVyIjoNCiAgICAgICAgICAgICAgY2FzZSAiYm9vbGVhbiI6DQogICAgICAgICAgICAgICAgICByZXR1cm4gIiIgKyB3cmFwcGVkLnZhbHVlOw0KICAgICAgICAgICAgICBjYXNlICJodG1sIjoNCiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwcGVkLnZhbHVlLm91dGVySFRNTDsNCiAgICAgICAgICAgICAgY2FzZSAid2lkZ2V0IjoNCiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwcGVkLnZhbHVlLm1hcmtkb3duKCk7DQogICAgICAgICAgICAgIGNhc2UgImxpbmsiOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUubWFya2Rvd24oKTsNCiAgICAgICAgICAgICAgY2FzZSAiZnVuY3Rpb24iOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuICI8ZnVuY3Rpb24+IjsNCiAgICAgICAgICAgICAgY2FzZSAiYXJyYXkiOg0KICAgICAgICAgICAgICAgICAgbGV0IHJlc3VsdCA9ICIiOw0KICAgICAgICAgICAgICAgICAgaWYgKHJlY3Vyc2l2ZSkNCiAgICAgICAgICAgICAgICAgICAgICByZXN1bHQgKz0gIlsiOw0KICAgICAgICAgICAgICAgICAgcmVzdWx0ICs9IHdyYXBwZWQudmFsdWUubWFwKGYgPT4gdG9TdHJpbmcoZiwgc2V0dGluZywgdHJ1ZSkpLmpvaW4oIiwgIik7DQogICAgICAgICAgICAgICAgICBpZiAocmVjdXJzaXZlKQ0KICAgICAgICAgICAgICAgICAgICAgIHJlc3VsdCArPSAiXSI7DQogICAgICAgICAgICAgICAgICByZXR1cm4gcmVzdWx0Ow0KICAgICAgICAgICAgICBjYXNlICJvYmplY3QiOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuICgieyAiICsNCiAgICAgICAgICAgICAgICAgICAgICBPYmplY3QuZW50cmllcyh3cmFwcGVkLnZhbHVlKQ0KICAgICAgICAgICAgICAgICAgICAgICAgICAubWFwKGUgPT4gZVswXSArICI6ICIgKyB0b1N0cmluZyhlWzFdLCBzZXR0aW5nLCB0cnVlKSkNCiAgICAgICAgICAgICAgICAgICAgICAgICAgLmpvaW4oIiwgIikgKw0KICAgICAgICAgICAgICAgICAgICAgICIgfSIpOw0KICAgICAgICAgICAgICBjYXNlICJkYXRlIjoNCiAgICAgICAgICAgICAgICAgIGlmICh3cmFwcGVkLnZhbHVlLnNlY29uZCA9PSAwICYmIHdyYXBwZWQudmFsdWUuaG91ciA9PSAwICYmIHdyYXBwZWQudmFsdWUubWludXRlID09IDApIHsNCiAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcHBlZC52YWx1ZS50b0Zvcm1hdChzZXR0aW5nLmRlZmF1bHREYXRlRm9ybWF0KTsNCiAgICAgICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwcGVkLnZhbHVlLnRvRm9ybWF0KHNldHRpbmcuZGVmYXVsdERhdGVUaW1lRm9ybWF0KTsNCiAgICAgICAgICAgICAgY2FzZSAiZHVyYXRpb24iOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHJlbmRlck1pbmltYWxEdXJhdGlvbih3cmFwcGVkLnZhbHVlKTsNCiAgICAgICAgICB9DQogICAgICB9DQogICAgICBWYWx1ZXMudG9TdHJpbmcgPSB0b1N0cmluZzsNCiAgICAgIC8qKiBXcmFwIGEgbGl0ZXJhbCB2YWx1ZSBzbyB5b3UgY2FuIHN3aXRjaCBvbiBpdCBlYXNpbHkuICovDQogICAgICBmdW5jdGlvbiB3cmFwVmFsdWUodmFsKSB7DQogICAgICAgICAgaWYgKGlzTnVsbCh2YWwpKQ0KICAgICAgICAgICAgICByZXR1cm4geyB0eXBlOiAibnVsbCIsIHZhbHVlOiB2YWwgfTsNCiAgICAgICAgICBlbHNlIGlmIChpc051bWJlcih2YWwpKQ0KICAgICAgICAgICAgICByZXR1cm4geyB0eXBlOiAibnVtYmVyIiwgdmFsdWU6IHZhbCB9Ow0KICAgICAgICAgIGVsc2UgaWYgKGlzU3RyaW5nKHZhbCkpDQogICAgICAgICAgICAgIHJldHVybiB7IHR5cGU6ICJzdHJpbmciLCB2YWx1ZTogdmFsIH07DQogICAgICAgICAgZWxzZSBpZiAoaXNCb29sZWFuKHZhbCkpDQogICAgICAgICAgICAgIHJldHVybiB7IHR5cGU6ICJib29sZWFuIiwgdmFsdWU6IHZhbCB9Ow0KICAgICAgICAgIGVsc2UgaWYgKGlzRHVyYXRpb24odmFsKSkNCiAgICAgICAgICAgICAgcmV0dXJuIHsgdHlwZTogImR1cmF0aW9uIiwgdmFsdWU6IHZhbCB9Ow0KICAgICAgICAgIGVsc2U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mVzdWx0ID0ge307DQogICAgICAgICAgICAgIGZvciAobGV0IFtrZXksIHZhbHVlXSBvZiBPYmplY3QuZW50cmllcyh2YWwpKQ0KICAgICAgICAgICAgICAgICAgcmVzdWx0W2tleV0gPSBtYXBMZWF2ZXModmFsdWUsIGZ1bmMpOw0KICAgICAgICAgICAgICByZXR1cm4gcmVzdWx0Ow0KICAgICAgICAgIH0NCiAgICAgICAgICBlbHNlIGlmIChpc0FycmF5KHZhbCkpIHsNCiAgICAgICAgICAgICAgbGV0IHJlc3VsdCA9IFtdOw0KICAgICAgICAgICAgICBmb3IgKGxldCB2YWx1ZSBvZiB2YWwpDQogICAgICAgICAgICAgICAgICByZXN1bHQucHVzaChtYXBMZWF2ZXModmFsdWUsIGZ1bmMpKTsNCiAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSB7DQogICAgICAgICAgICAgIHJldHVybiBmdW5jKHZhbCk7DQogICAgICAgICAgfQ0KICAgICAgfQ0KICAgICAgVmFsdWVzLm1hcExlYXZlcyA9IG1hcExlYXZlczsNCiAgICAgIC8qKiBDb21wYXJlIHR3byBhcmJpdHJhcnkgSmF2YVNjcmlwdCB2YWx1ZXMuIFByb2R1Y2VzIGEgdG90YWwgb3JkZXJpbmcgb3ZlciBBTlkgcG9zc2libGUgZGF0YXZpZXcgdmFsdWUuICovDQogICAgICBmdW5jdGlvbiBjb21wYXJlVmFsdWUodmFsMSwgdmFsMiwgbGlua05vcm1hbGl6ZXIpIHsNCiAgICAgICAgICB2YXIgX2EsIF9iOw0KICAgICAgICAgIC8vIEhhbmRsZSB1bmRlZmluZWQvbnVsbHMgZmlyc3QuDQogICAgICAgICAgaWYgKHZhbDEgPT09IHVuZGVmaW5lZCkNCiAgICAgICAgICAgICAgdmFsMSA9IG51bGw7DQogICAgICAgICAgaWYgKHZhbDIgPT09IHVuZGVmaW5lZCkNCiAgICAgICAgICAgICAgdmFsMiA9IG51bGw7DQogICAgICAgICAgaWYgKHZhbDEgPT09IG51bGwgJiYgdmFsMiA9PT0gbnVsbCkNCiAgICAgICAgICAgICAgcmV0dXJuIDA7DQogICAgICAgICAgZWxzZSBpZiAodmFsMSA9PT0gbnVsbCkNCiAgICAgICAgICAgICAgcmV0dXJuIC0xOw0KICAgICAgICAgIGVsc2UgaWYgKHZhbDIgPT09IG51bGwpDQogICAgICAgICAgICAgIHJldHVybiAxOw0KICAgICAgICAgIC8vIEEgbm9uLW51bGwgdmFsdWUgbm93IHdoaWNoIHdlIGNhbiB3cmFwICYgY29tcGFyZSBvbi4NCiAgICAgICAgICBsZXQgd3JhcDEgPSB3cmFwVmFsdWUodmFsMSk7DQogICAgICAgICAgbGV0IHdyYXAyID0gd3JhcFZhbHVlKHZhbDIpOw0KICAgICAgICAgIGlmICh3cmFwMSA9PT0gdW5kZWZpbmVkICYmIHdyYXAyID09PSB1bmRlZmluZWQpDQogICAgICAgICAgICAgIHJldHVybiAwOw0KICAgICAgICAgIGVsc2UgaWYgKHdyYXAxID09PSB1bmRlZmluZWQpDQogICAgICAgICAgICAgIHJldHVybiAtMTsNCiAgICAgICAgICBlbHNlIGlmICh3cmFwMiA9PT0gdW5kZWZpbmVkKQ0KICAgICAgICAgICAgICByZXR1cm4gMTsNCiAgICAgICAgICAvLyBTaG9ydC1jaXJjdWl0IG9uIGRpZmZlcmVudCB0eXBlcyBvciBvbiByZWZlcmVuY2UgZXF1YWxpdHkuDQogICAgICAgICAgaWYgKHdyYXAxLnR5cGUgIT0gd3JhcDIudHlwZSkNCiAgICAgICAgICAgICAgcmV0dXJuIHdyYXAxLnR5cGUubG9jYWxlQ29tcGFyZSh3cmFwMi50eXBlKTsNCiAgICAgICAgICBpZiAod3JhcDEudmFsdWUgPT09IHdyYXAyLnZhbHVlKQ0KICAgICAgICAgICAgICByZXR1cm4gMDsNCiAgICAgICAgICBzd2l0Y2ggKHdyYXAxLnR5cGUpIHsNCiAgICAgICAgICAgICAgY2FzZSAic3RyaW5nIjoNCiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwMS52YWx1ZS5sb2NhbGVDb21wYXJlKHdyYXAyLnZhbHVlKTsNCiAgICAgICAgICAgICAgY2FzZSAibnVtYmVyIjoNCiAgICAgICAgICAgICAgICAgIGlmICh3cmFwMS52YWx1ZSA8IHdyYXAyLnZhbHVlKQ0KICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAtMTsNCiAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKHdyYXAxLnZhbHVlID09IHdyYXAyLnZhbHVlKQ0KICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAwOw0KICAgICAgICAgICAgICAgICAgcmV0dXJuIDE7DQogICAgICAgICAgICAgIGNhc2UgIm51bGwiOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIDA7DQogICAgICAgICAgICAgIGNhc2UgImJvb2xlYW4iOg0KICAgICAgICAgICAgICAgICAgaWYgKHdyYXAxLnZhbHVlID09IHdyYXAyLnZhbHVlKQ0KICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAwOw0KICAgICAgICAgICAgICAgICAgZWxzZQ0KICAgICAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwMS52YWx1ZSA/IDEgOiAtMTsNCiAgICAgICAgICAgICAgY2FzZSAibGluayI6DQogICAgICAgICAgICAgICAgICBsZXQgbGluazEgPSB3cmFwMS52YWx1ZTsNCiAgICAgICAgICAgICAgICAgIGxldCBsaW5rMiA9IHdyYXAyLnZhbHVlOw0KICAgICAgICAgICAgICAgICAgbGV0IG5vcm1hbGl6ZSA9IGxpbmtOb3JtYWxpemVyICE9PSBudWxsICYmIGxpbmtOb3JtYWxpemVyICE9PSB2b2lkIDAgPyBsaW5rTm9ybWFsaXplciA6ICgoeCkgPT4geCk7DQogICAgICAgICAgICAgICAgICAvLyBXZSBjYW4ndCBjb21wYXJlIGJ5IGZpbGUgbmFtZSBvciBkaXNwbGF5LCBzaW5jZSB0aGF0IHdvdWxkIGJyZWFrIGxpbmsgZXF1YWxpdHkuIENvbXBhcmUgYnkgcGF0aC4NCiAgICAgICAgICAgICAgICAgIGxldCBwYXRoQ29tcGFyZSA9IG5vcm1hbGl6ZShsaW5rMS5wYXRoKS5sb2NhbGVDb21wYXJlKG5vcm1hbGl6ZShsaW5rMi5wYXRoKSk7DQogICAgICAgICAgICAgICAgICBpZiAocGF0aENvbXBhcmUgIT0gMCkNCiAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gcGF0aENvbXBhcmU7DQogICAgICAgICAgICAgICAgICAvLyBUaGVuIGNvbXBhcmUgYnkgdHlwZS4NCiAgICAgICAgICAgICAgICAgIGxldCB0eXBlQ29tcGFyZSA9IGxpbmsxLnR5cGUubG9jYWxlQ29tcGFyZShsaW5rMi50eXBlKTsNCiAgICAgICAgICAgICAgICAgIGlmICh0eXBlQ29tcGFyZSAhPSAwKQ0KICAgICAgICAgICAgICAgICAgICAgIHJldHVybiB0eXBlQ29tcGFyZTsNCiAgICAgICAgICAgICAgICAgIC8vIFRoZW4gY29tcGFyZSBieSBzdWJwYXRoIGV4aXN0ZW5jZS4NCiAgICAgICAgICAgICAgICAgIGlmIChsaW5rMS5zdWJwYXRoICYmICFsaW5rMi5zdWJwYXRoKQ0KICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAxOw0KICAgICAgICAgICAgICAgICAgaWYgKCFsaW5rMS5zdWJwYXRoICYmIGxpbmsyLnN1YnBhdGgpDQogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIC0xOw0KICAgICAgICAgICAgICAgICAgaWYgKCFsaW5rMS5zdWJwYXRoICYmICFsaW5rMi5zdWJwYXRoKQ0KICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAwOw0KICAgICAgICAgICAgICAgICAgLy8gU2luY2UgYm90aCBoYXZlIGEgc3VicGF0aCwgY29tcGFyZSBieSBzdWJwYXRoLg0KICAgICAgICAgICAgICAgICAgcmV0dXJuICgoX2EgPSBsaW5rMS5zdWJwYXRoKSAhPT0gbnVsbCAmJiBfYSAhPT0gdm9pZCAwID8gX2EgOiAiIikubG9jYWxlQ29tcGFyZSgoX2IgPSBsaW5rMi5zdWJwYXRoKSAhPT0gbnVsbCAmJiBfYiAhPT0gdm9pZCAwID8gX2IgOiAiIik7DQogICAgICAgICAgICAgIGNhc2UgImRhdGUiOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXAxLnZhbHVlIDwgd3JhcDIudmFsdWUNCiAgICAgICAgICAgICAgICAgICAgICA/IC0xDQogICAgICAgICAgICAgICAgICAgICAgOiB3cmFwMS52YWx1ZS5lcXVhbHMod3JhcDIudmFsdWUpDQogICAgICAgICAgICAgICAgICAgICAgICAgID8gMA0KICAgICAgICAgICAgICAgICAgICAgICAgICA6IDE7DQogICAgICAgICAgICAgIGNhc2UgImR1cmF0aW9uIjoNCiAgICAgICAgICAgICAgICAgIHJldHVybiB3cmFwMS52YWx1ZSA8IHdyYXAyLnZhbHVlDQogICAgICAgICAgICAgICAgICAgICAgPyAtMQ0KICAgICAgICAgICAgICAgICAgICAgIDogd3JhcDEudmFsdWUuZXF1YWxzKHdyYXAyLnZhbHVlKQ0KICAgICAgICAgICAgICAgICAgICAgICAgICA/IDANCiAgICAgICAgICAgICAgICAgICAgICAgICAgOiAxOw0KICAgICAgICAgICAgICBjYXNlICJhcnJheSI6DQogICAgICAgICAgICAgICAgICBsZXQgZjEgPSB3cmFwMS52YWx1ZTsNCiAgICAgICAgICAgICAgICAgIGxldCBmMiA9IHdyYXAyLnZhbHVlOw0KICAgICAgICAgICAgICAgICAgZm9yIChsZXQgaW5kZXggPSAwOyBpbmRleCA8IE1hdGgubWluKGYxLmxlbmd0aCwgZjIubGVuZ3RoKTsgaW5kZXgrKykgew0KICAgICAgICAgICAgICAgICAgICAgIGxldCBjb21wID0gY29tcGFyZVZhbHVlKGYxW2luZGV4XSwgZjJbaW5kZXhdKTsNCiAgICAgICAgICAgICAgICAgICAgICBpZiAoY29tcCAhPSAwKQ0KICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gY29tcDsNCiAgICAgICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgICAgIHJldHVybiBmMS5sZW5ndGggLSBmMi5sZW5ndGg7DQogICAgICAgICAgICAgIGNhc2UgIm9iamVjdCI6DQogICAgICAgICAgICAgICAgICBsZXQgbzEgPSB3cmFwMS52YWx1ZTsNCiAgICAgICAgICAgICAgICAgIGxldCBvMiA9IHdyYXAyLnZhbHVlOw0KICAgICAgICAgICAgICAgICAgbGV0IGsxID0gQXJyYXkuZnJvbShPYmplY3Qua2V5cyhvMSkpOw0KICAgICAgICAgICAgICAgICAgbGV0IGsyID0gQXJyYXkuZnJvbShPYmplY3Qua2V5cyhvMikpOw0KICAgICAgICAgICAgICAgICAgazEuc29ydCgpOw0KICAgICAgICAgICAgICAgICAgazIuc29ydCgpOw0KICAgICAgICAgICAgICAgICAgbGV0IGtleUNvbXBhcmUgPSBjb21wYXJlVmFsdWUoazEsIGsyKTsNCiAgICAgICAgICAgICAgICAgIGlmIChrZXlDb21wYXJlICE9IDApDQogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGtleUNvbXBhcmU7DQogICAgICAgICAgICAgICAgICBmb3IgKGxldCBrZXkgb2YgazEpIHsNCiAgICAgICAgICAgICAgICAgICAgICBsZXQgY29tcCA9IGNvbXBhcmVWYWx1ZShvMVtrZXldLCBvMltrZXldKTsNCiAgICAgICAgICAgICAgICAgICAgICBpZiAoY29tcCAhPSAwKQ0KICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gY29tcDsNCiAgICAgICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgICAgIHJldHVybiAwOw0KICAgICAgICAgICAgICBjYXNlICJ3aWRnZXQiOg0KICAgICAgICAgICAgICBjYXNlICJodG1sIjoNCiAgICAgICAgICAgICAgY2FzZSAiZnVuY3Rpb24iOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIDA7DQogICAgICAgICAgfQ0KICAgICAgfQ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ISF3cmFwcGVkLnZhbHVlLnBhdGg7DQogICAgICAgICAgICAgIGNhc2UgImRhdGUiOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUudG9NaWxsaXMoKSAhPSAwOw0KICAgICAgICAgICAgICBjYXNlICJkdXJhdGlvbiI6DQogICAgICAgICAgICAgICAgICByZXR1cm4gd3JhcHBlZC52YWx1ZS5hcygic2Vjb25kcyIpICE9IDA7DQogICAgICAgICAgICAgIGNhc2UgIm9iamVjdCI6DQogICAgICAgICAgICAgICAgICByZXR1cm4gT2JqZWN0LmtleXMod3JhcHBlZC52YWx1ZSkubGVuZ3RoID4gMDsNCiAgICAgICAgICAgICAgY2FzZSAiYXJyYXkiOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUubGVuZ3RoID4gMDsNCiAgICAgICAgICAgICAgY2FzZSAibnVsbCI6DQogICAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7DQogICAgICAgICAgICAgIGNhc2UgImh0bWwiOg0KICAgICAgICAgICAgICBjYXNlICJ3aWRnZXQiOg0KICAgICAgICAgICAgICBjYXNlICJmdW5jdGlvbiI6DQogICAgICAgICAgICAgICAgICByZXR1cm4gdHJ1ZTsNCiAgICAgICAgICB9DQogICAgICB9DQogICAgICBWYWx1ZXMuaXNUcnV0aHkgPSBpc1RydXRoeTsNCiAgICAgIC8qKiBEZWVwIGNvcHkgYSBmaWVsZC4gKi8NCiAgICAgIGZ1bmN0aW9uIGRlZXBDb3B5KGZpZWxkKSB7DQogICAgICAgICAgaWYgKGZpZWxkID09PSBudWxsIHx8IGZpZWxkID09PSB1bmRlZmluZWQpDQogICAgICAgICAgICAgIHJldHVybiBmaWVsZDsNCiAgICAgICAgICBpZiAoVmFsdWVzLmlzQXJyYXkoZmllbGQpKSB7DQogICAgICAgICAgICAgIHJldHVybiBbXS5jb25jYXQoZmllbGQubWFwKHYgPT4gZGVlcENvcHkodikpKTsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSBpZiAoVmFsdWVzLmlzT2JqZWN0KGZpZWxkKSkgew0KICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0ge307DQogICAgICAgICAgICAgIGZvciAobGV0IFtrZXksIHZhbHVlXSBvZiBPYmplY3QuZW50cmllcyhmaWVsZCkpDQogICAgICAgICAgICAgICAgICByZXN1bHRba2V5XSA9IGRlZXBDb3B5KHZhbHVlKTsNCiAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSB7DQogICAgICAgICAgICAgIHJldHVybiBmaWVsZDsNCiAgICAgICAgICB9DQogICAgICB9DQogICAgICBWYWx1ZXMuZGVlcENvcHkgPSBkZWVwQ29weTsNCiAgICAgIGZ1bmN0aW9uIGlzU3RyaW5nKHZhbCkgew0KICAgICAgICAgIHJldHVybiB0eXBlb2YgdmFsID09ICJzdHJpbmciOw0KICAgICAgfQ0KICAgICAgVmFsdWVzLmlzU3RyaW5nID0gaXNTdHJpbmc7DQogICAgICBmdW5jdGlvbiBpc051bWJlcih2YWwpIHsNCiAgICAgICAgICByZXR1cm4gdHlwZW9mIHZhbCA9PSAibnVtYmVyIjsNCiAgICAgIH0NCiAgICAgIFZhbHVlcy5pc051bWJlciA9IGlzTnVtYmVyOw0KICAgICAgZnVuY3Rpb24gaXNEYXRlKHZhbCkgew0KICAgICAgICAgIHJldHVybiB2YWwgaW5zdGFuY2VvZiBEYXRlVGltZTsNCiAgICAgIH0NCiAgICAgIFZhbHVlcy5pc0RhdGUgPSBpc0RhdGU7DQogICAgICBmdW5jdGlvbiBpc0R1cmF0aW9uKHZhbCkgew0KICAgICAgICAgIHJldHVybiB2YWwgaW5zdGFuY2VvZiBEdXJhdGlvbjsNCiAgICAgIH0NCiAgICAgIFZhbHVlcy5pc0R1cmF0aW9uID0gaXNEdXJhdGlvbjsNCiAgICAgIGZ1bmN0aW9uIGlzTnVsbCh2YWwpIHsNCiAgICAgIC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SBpcyBhbiBvYmplY3QgKGFuZCBub3QgYW55IG90aGVyIGRhdGF2aWV3LXJlY29nbml6ZWQgb2JqZWN0LWxpa2UgdHlwZSkuICovDQogICAgICBmdW5jdGlvbiBpc09iamVjdCh2YWwpIHsNCiAgICAgICAgICByZXR1cm4gKHR5cGVvZiB2YWwgPT0gIm9iamVjdCIgJiYNCiAgICAgICAgICAgICAgIWlzSHRtbCh2YWwpICYmDQogICAgICAgICAgICAgICFpc1dpZGdldCh2YWwpICYmDQogICAgICAgICAgICAgICFpc0FycmF5KHZhbCkgJiYNCiAgICAgICAgICAgICAgIWlzRHVyYXRpb24odmFsKSAmJg0KICAgICAgICAgICAgICAhaXNEYXRlKHZhbCkgJiYNCiAgICAgICAgICAgICAgIWlzTGluayh2YWwpICYmDQogICAgICAgICAgICAgIHZhbCAhPT0gdW5kZWZpbmVkICYmDQogICAgICAgICAgICAgICFpc051bGwodmFsKSk7DQogICAgICB9DQogICAgICBWYWx1ZXMuaXNPYmplY3QgPSBpc09iamVjdDsNCiAgICAgIGZ1bmN0aW9uIGlzRnVuY3Rpb24odmFsKSB7DQogICAgICAgICAgcmV0dXJuIHR5cGVvZiB2YWwgPT0gImZ1bmN0aW9uIjsNCiAgICAgIH0NCiAgICAgIFZhbHVlcy5pc0Z1bmN0aW9uID0gaXNGdW5jdGlvbjsNCiAgfSkoVmFsdWVzIHx8IChWYWx1ZXMgPSB7fSkpOw0KICAvLy8vLy8vLy8vLy8vLy8NCiAgLy8gR3JvdXBpbmdzIC8vDQogIC8vLy8vLy8vLy8vLy8vLw0KICB2YXIgR3JvdXBpbmdzOw0KICAoZnVuY3Rpb24gKEdyb3VwaW5ncykgew0KICAgICAgLyoqIERldGVybWluZXMgaWYgdGhlIGdpdmVuIGdyb3VwIGVudHJ5IGlzIGEgc3RhbmRhbG9uZSB2YWx1ZSwgb3IgYSBncm91cGluZyBvZiBzdWItZW50cmllcy4gKi8NCiAgICAgIGZ1bmN0aW9uIGlzRWxlbWVudEdy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mxlbmd0aDsNCiAgICAgICAgICB9DQogICAgICB9DQogICAgICBHcm91cGluZ3MuY291bnQgPSBjb3VudDsNCiAgfSkoR3JvdXBpbmdzIHx8IChHcm91cGluZ3MgPSB7fSkpOw0KICAvLy8vLy8vLy8vDQogIC8vIExJTksgLy8NCiAgLy8vLy8vLy8vLw0KICAvKiogVGhlIE9ic2lkaWFuICdsaW5rJywgdXNlZCBmb3IgdW5pcXVlbHkgZGVzY3JpYmluZyBhIGZpbGUsIGhlYWRlciwgb3IgYmxvY2suICovDQogIGNsYXNzIExpbmsgew0KICAgICAgY29uc3RydWN0b3IoZmllbGRzKSB7DQogICAgICAgICAgT2JqZWN0LmFzc2lnbih0aGlzLCBmaWVsZHMpOw0KICAgICAgfQ0KICAgICAgLyoqIENyZWF0ZSBhIGxpbmsgdG8gYSBzcGVjaWZpYyBmaWxlLiAqLw0KICAgICAgc3RhdGljIGZpbGUocGF0aCwgZW1iZWQgPSBmYWxzZSwgZGlzcGxheSkgew0KICAgICAgICAgIHJldHVybiBuZXcgTGluayh7DQogICAgICAgICAgICAgIHBhdGgsDQogICAgICAgICAgICAgIGVtYmVkLA0KICAgICAgICAgICAgICBkaXNwbGF5LA0KICAgICAgICAgICAgICBzdWJwYXRoOiB1bmRlZmluZWQsDQogICAgICAgICAgICAgIHR5cGU6ICJmaWxlIiwNCiAgICAgICAgICB9KTsNCiAgICAgIH0NCiAgICAgIHN0YXRpYyBpbmZlcihsaW5rcGF0aCwgZW1iZWQgPSBmYWxzZSwgZGlzcGxheSkgew0KICAgICAgICAgIGlmIChsaW5rcGF0aC5pbmNsdWRlcygiI14iKSkgew0KICAgICAgICAgICAgICBsZXQgc3BsaXQgPSBsaW5rcGF0aC5zcGxpdCgiI14iKTsNCiAgICAgICAgICAgICAgcmV0dXJuIExpbmsuYmxvY2soc3BsaXRbMF0sIHNwbGl0WzFdLCBlbWJlZCwgZGlzcGxheSk7DQogICAgICAgICAgfQ0KICAgICAgICAgIGVsc2UgaWYgKG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wNCiAgICAgICAgICAgICAgZW1iZWQsDQogICAgICAgICAgICAgIGRpc3BsYXksDQogICAgICAgICAgICAgIHN1YnBhdGg6IGJsb2NrSWQsDQogICAgICAgICAgICAgIHR5cGU6ICJibG9jayIsDQogICAgICAgICAgfSk7DQogICAgICB9DQogICAgICBzdGF0aWMgZnJvbU9iamVjdChvYmplY3QpIHsNCiAgICAgICAgICByZXR1cm4gbmV3IExpbmsob2JqZWN0KTsNCiAgICAgIH0NCiAgICAgIC8qKiBDaGVja3MgZm9yIGxpbmsgZXF1YWxpdHkgKGkuZS4sIHRoYXQgdGhlIGxpbmtzIGFyZSBwb2ludGluZyB0byB0aGUgc2FtZSBleGFjdCBsb2NhdGlvbikuICovDQogICAgICBlcXVhbHMob3RoZXIpIHsNCiAgICAgICAgICBpZiAob3RoZXIgPT0gdW5kZWZpbmVkIHx8IG90aGVyID09IG51bGwpDQogICAgICAgICAgICAgIHJldHVybiBmYWxzZTsNCiAgICAgICAgICByZXR1cm4gdGhpcy5wYXRoID09IG90aGVyLnBhdGggJiYgdGhpcy50eXBlID09IG90aGVyLnR5cGUgJiYgdGhpcy5zdWJwYXRoID09IG90aGVyLnN1YnBhdGg7DQogICAgICB9DQogICAgICAvKiogQ29udmVydCB0aGlzIGxpbmsgdG8gaXQncyBtYXJrZG93biByZXByZXNlbnRhdGlvbi4gKi8NCiAgICAgIHRvU3RyaW5nKCkgew0KICAgICAgICAgIHJldHVybiB0aGlzLm1hcmtkb3duKCk7DQogICAgICB9DQogICAgICAvKiogQ29udmVydCB0aGlzIGxpbmsgdG8gYSByYXcgb2JqZWN0IHdoaWNoIGlzIHNlcmlhbGl6YXRpb24tZnJpZW5kbHkuICovDQogICAgICB0b09iamVjdCgpIHsNCiAgICAgICAgICByZXR1cm4geyBwYXRoOiB0aGlzLnBhdGgsIHR5cGU6IHRoaXMudHlwZSwgc3VicGF0aDogdGhpcy5zdWJwYXRoLCBkaXNwbGF5OiB0aGlzLmRpc3BsYXksIGVtYmVk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mVydCB0aGlzIGxpbmsgaW50byBhbiBlbWJlZGRlZCBsaW5rLiAqLw0KICAgICAgdG9FbWJlZCgpIHsNCiAgICAgICAgICBpZiAodGhpcy5lbWJlZCkgew0KICAgICAgICAgICAgICByZXR1cm4gdGhpczsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSB7DQogICAgICAgICAgICAgIGxldCBsaW5rID0gbmV3IExpbmsodGhpcyk7DQogICAgICAgICAgICAgIGxpbmsuZW1iZWQgPSB0cnVlOw0KICAgICAgICAgICAgICByZXR1cm4gbGluazsNCiAgICAgICAgICB9DQogICAgICB9DQogICAgICAvKiogQ29udmVydCB0aGlzIGxpbmsgaW50byBhIG5vbi1lbWJlZGRlZCBsaW5rLiAqLw0KICAgICAgZnJvbUVtYmVkKCkgew0KICAgICAgICAgIGlmICghdGhpcy5lbWJlZCkgew0KICAgICAgICAgICAgICByZXR1cm4gdGhpczsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSB7DQogICAgICAgICAgICAgIGxldCBsaW5rID0gbmV3IExpbmsodGhpcyk7DQogICAgICAgICAgICAgIGxpbmsuZW1iZWQgPSBmYWxzZTsNCiAgICAgICAgICAgICAgcmV0dXJuIGxpbms7DQogICAgICAgICAgfQ0KICAgICAgfQ0KICAgICAgLyoqIENvbnZlcnQgdGhpcyBsaW5rIHRvIG1hcmtkb3duIHNvIGl0IGNhbiBiZSByZW5kZXJlZC4gKi8NCiAgICAgIG1hcmtkb3duKCkgew0KICAgICAgICAgIGxldCByZXN1bHQgPSAodGhpcy5lbWJlZCA/ICIhIiA6ICIiKSArICJbWyIgKyB0aGlzLm9ic2lkaWFuTGluaygpOw0KICAgICAgICAgIGlmICh0aGlzLmRpc3BsYXkpIHsNCiAgICAgICAgICAgICAgcmVzdWx0ICs9ICJ8IiArIHRoaXMuZGlzcGxheTsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSB7DQogICAgICAgICAgICAgIHJlc3VsdCArPSAifCIgKyBnZXRGaWxl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AgfSk7DQogIH0NCiAgZnVuY3Rpb24gY2hhaW5PcHQoYmFzZSwgLi4uZnVuY3MpIHsNCiAgICAgIHJldHVybiBwYXJzaW1tb25fdW1kX21pbi5leHBvcnRzLmN1c3RvbSgoc3VjY2VzcywgZmFpbHVyZSkgPT4gew0KICAgICAgICAgIHJldHVybiAoaW5wdXQsIGkpID0+IHsNCiAgICAgICAgICAgICAgbGV0IHJlc3VsdCA9IGJhc2UuXyhpbnB1dCwgaSk7DQogICAgICAgICAgICAgIGlmICghcmVzdWx0LnN0YXR1cykNCiAgICAgICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7DQogICAgICAgICAgICAgIGZvciAobGV0IGZ1bmMgb2YgZnVuY3MpIHsNCiAgICAgICAgICAgICAgICAgIGxldCBuZXh0ID0gZnVuYyhyZXN1bHQudmFsdWUpLl8oaW5wdXQsIHJlc3VsdC5pbmRleCk7DQogICAgICAgICAgICAgICAgICBpZiAoIW5leHQuc3RhdHVzKQ0KICAgICAgICAgICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7DQogICAgICAgICAgICAgICAgICByZXN1bHQgPSBuZXh0Ow0KICAgICAgICAgICAgICB9DQogICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7DQogICAgICAgICAgfTsNCiAgICAgIH0pOw0KICB9DQogIGNvbnN0IEVYUFJFU1NJT04gPSBwYXJzaW1tb25fdW1kX21pbi5leHBvcnRzLmNyZWF0ZUxhbmd1YWdlKHsNCiAgICAgIC8vIEEgZmxvYXRpbmcgcG9pbnQgbnVtYmVyOyB0aGUgZGVjaW1hbCBwb2ludCBpcyBvcHRpb25hbC4NCiAgICAgIG51bWJlcjogcSA9PiBwYXJzaW1tb25fdW1kX21pbi5leHBvcnRzLnJlZ2V4cCgvLT9bMC05XSsoXC5bMC05XSspPy8pDQogICAgICAgICAgLm1hcChzdHIgPT4gTnVtYmVyLnBhcnNlRmxvYXQoc3RyKSkNCiAgICAgICAgICAuZGVzYygibnVtYmVyIiksDQogICAgICAvLyBBIHF1b3RlLXN1cnJvdW5kZWQgc3RyaW5nIHdoaWNoIHN1cHBvcnRzIGVzY2FwZSBjaGFyYWN0ZXJzICgnXCcpLg0KICAgICAgc3RyaW5nOiBxID0+IHBhcnNpbW1vbl91bWRfbWluLmV4cG9ydHMuc3RyaW5nKCciJykNCiAgICAgICAgICAudGhlbihwYXJzaW1tb25fdW1kX21pbi5leHBvcnRzLmFsdChxLmVzY2FwZUNoYXJhY3RlciwgcGFyc2ltbW9uX3VtZF9taW4uZXhwb3J0cy5ub25lT2YoJyJcXCcpKQ0KICAgICAgICAgIC5hdExlYXN0KDApDQogICAgICAgICAgLm1hcChjaGFycyA9PiBjaGFycy5qb2luKCIiKSkpDQogICAgICAgICAgLnNraXAocGFyc2ltbW9uX3VtZF9taW4uZXhwb3J0cy5zdHJpbmcoJyInKSkNCiAgICAgICAgICAuZGVzYygic3RyaW5nIiksDQogICAgICBlc2NhcGVDaGFyYWN0ZXI6IF8gPT4gcGFyc2ltbW9uX3VtZF9taW4uZXhwb3J0cy5zdHJpbmcoIlxcIikNCiAgICAgICAgICAudGhlbihwYXJzaW1tb25fdW1kX21pbi5leHBvcnRzLmFueSkNCiAgICAgICAgICAubWFwKGVzY2FwZWQgPT4gew0KICAgICAgICAgIC8vIElmIHdlIGFyZSBlc2NhcGluZyBhIGJhY2tzbGFzaCBvciBhIHF1b3RlLCBwYXNzIGluIG9uIGluIGVzY2FwZWQgZm9ybQ0KICAgICAgICAgIGlmIChlc2NhcGVkID09PSAnIicpDQogICAgICAgICAgICAgIHJldHVybiAnIic7DQogICAgICAgICAgaWYgKGVzY2FwZWQgPT09ICJcXCIpDQogICAgICAgICAgICAgIHJldHVybiAiXFwiOw0KICAgICAgICAgIGVsc2UNCiAgICAgICAgICAgICAgcmV0dXJuICJcXCIgKyBlc2NhcGVkOw0KICAgICAgfSksDQogICAgICAvLyBBIGJvb2xlYW4gdHJ1ZS9mYWxzZSB2YWx1ZS4NCiAgICAgIGJvb2w6I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W5kQ2xvc2luZyhsaW5lLCBzdGFydCwgb3BlbiwgY2xvc2UpIHsNCiAgICAgIGxldCBuZXN0aW5nID0gMDsNCiAgICAgIGxldCBlc2NhcGVkID0gZmFsc2U7DQogICAgICBmb3IgKGxldCBpbmRleCA9IHN0YXJ0OyBpbmRleCA8IGxpbmUubGVuZ3RoOyBpbmRleCsrKSB7DQogICAgICAgICAgbGV0IGNoYXIgPSBsaW5lLmNoYXJBdChpbmRleCk7DQogICAgICAgICAgLy8gQWxsb3dzIGZvciBkb3VibGUgZXNjYXBlcyBsaWtlICdcXCcgdG8gYmUgcmVuZGVyZWQgbm9ybWFsbHkuDQogICAgICAgICAgaWYgKGNoYXIgPT0gIlxcIikgew0KICAgICAgICAgICAgICBlc2NhcGVkID0gIWVzY2FwZWQ7DQogICAgICAgICAgICAgIGNvbnRpbnVlOw0KICAgICAgICAgIH0NCiAgICAgICAgICAvLyBJZiBlc2NhcGVkLCBpZ25vcmUgdGhlIG5leHQgY2hhcmFjdGVyIGZvciBjb21wdXRpbmcgbmVzdGluZywgcmVnYXJkbGVzcyBvZiB3aGF0IGl0IGlzLg0KICAgICAgICAgIGlmIChlc2NhcGVkKSB7DQogICAgICAgICAgICAgIGVzY2FwZWQgPSBmYWxzZTsNCiAgICAgICAgICAgICAgY29udGludWU7DQogICAgICAgICAgfQ0KICAgICAgICAgIGlmIChjaGFyID09IG9wZW4pDQogICAgICAgICAgICAgIG5lc3RpbmcrKzsNCiAgICAgICAgICBlbHNlIGlmIChjaGFyID09IGNsb3NlKQ0KICAgICAgICAgICAgICBuZXN0aW5nLS07DQogICAgICAgICAgLy8gT25seSBvY2N1cnMgaWYgd2UgYXJlIG9uIGEgY2xvc2UgY2hhcmFjdGVyIGFuZCB0cmhlcmUgaXMgbm8gbW9yZSBuZXN0aW5nLg0KICAgICAgICAgIGlmIChuZXN0aW5nIDwgMCkNCiAgICAgICAgICAgICAgcmV0dXJuIHsgdmFsdWU6IGxpbmUuc3Vic3RyaW5nKH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B2YWx1ZTogbWF0Y2hbMV0sDQogICAgICAgICAgICAgIHN0YXJ0OiBtYXRjaC5pbmRleCwNCiAgICAgICAgICAgICAgc3RhcnRWYWx1ZTogbWF0Y2guaW5kZXggKyAxLA0KICAgICAgICAgICAgICBlbmQ6IG1hdGNoLmluZGV4ICsgbWF0Y2hbMF0ubGVuZ3RoLA0KICAgICAgICAgICAgICB3cmFwcGluZzogImVtb2ppLXNob3J0aGFuZCIsDQogICAgICAgICAgfSk7DQogICAgICB9DQogICAgICByZXR1cm4gcmVzdWx0czsNCiAgfQoKICAvKiogQWxsIGV4dHJhY3RlZCBtYXJrZG93biBmaWxlIG1ldGFkYXRhIG9idGFpbmVkIGZyb20gYSBmaWxlLiAqLw0KICBjbGFzcyBQYWdlTWV0YWRhdGEgew0KICAgICAgY29uc3RydWN0b3IocGF0aCwgaW5pdCkgew0KICAgICAgICAgIHRoaXMucGF0aCA9IHBhdGg7DQogICAgICAgICAgdGhpcy5maWVsZHMgPSBuZXcgTWFwKCk7DQogICAgICAgICAgdGhpcy5mcm9udG1hdHRlciA9IHt9Ow0KICAgICAgICAgIHRoaXMudGFncyA9IG5ldyBTZXQoKTsNCiAgICAgICAgICB0aGlzLmFsaWFzZXMgPSBuZXcgU2V0KCk7DQogICAgICAgICAgdGhpcy5saW5rcyA9IFtdOw0KICAgICAgICAgIE9iamVjdC5hc3NpZ24odGhpcywgaW5pdCk7DQogICAgICAgICAgdGhpcy5saXN0cyA9ICh0aGlzLmxpc3RzIHx8IFtdKS5tYXAobCA9PiBuZXcgTGlzdEl0ZW0obCkpOw0KICAgICAgfQ0KICAgICAgLyoqIENhbm9uaWNhbGl6ZSByYXcgbGlua3MgYW5kIG90aGVyIGRhdGEgaW4gcGFydGlhbCBkYXRhIHdpdGggbm9ybWFsaXplcnMsIHJldHVybmluZyBhIGNvbXBsZXRlZCBvYmplY3QuICovDQogICAgICBzdGF0aWMgY2Fub25pY2FsaXplKGRhdGEsIGxpbmtOb3JtYWxpemVyKSB7DQogICAgICAgICAgLy8gTXV0YXRlIHRoZSBkYXRhIGZvciBub3csIHdoaWNoIGlzIHByb2JhYmx5IGEgYmFkIGlkZWEgYnV0Li4uIGFsbCB3ZWxsLg0KICAgICAgICAgIGlmIChkYXRhLmZyb250bWF0dGVyKSB7DQogICAgICAgICAgICAgIGRhdGEuZnJvbnRtYXR0ZXIgPSBWYWx1ZXMubWFwTGVhdmVzKGRhdGEuZnJvbnRtYXR0ZXIsIHQgPT4gVmFsdWVzLmlzTGluayh0KSA/IGxpbmtOb3JtYWxpemVyKHQpIDogdCk7DQogICAgICAgICAgfQ0KICAgICAgICAgIGlmIChkYXRhLmZpZWxkcykgew0KICAgICAgICAgICAgICBmb3IgKGxldCBba2V5LCB2YWx1ZV0gb2YgZGF0YS5maWVsZHMuZW50cmllcygpKSB7DQogICAgICAgICAgICAgICAgICBkYXRhLmZpZWxkcy5zZXQoa2V5LCBWYWx1ZXMubWFwTGVhdmVzKHZhbHVlLCB0ID0+IChWYWx1ZXMuaXNMaW5rKHQpID8gbGlua05vcm1hbGl6ZXIodCkgOiB0KSkpOw0KICAgICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICAgIGlmIChkYXRhLmxpc3RzKSB7DQogICAgICAgICAgICAgIGZvciAobGV0IGl0ZW0gb2YgZGF0YS5saXN0cykgew0KICAgICAgICAgICAgICAgICAgZm9yIChsZXQgW2tleSwgdmFsdWVdIG9mIGl0ZW0uZmllbGRzLmVudHJpZXMoKSkgew0KICAgICAgICAgICAgICAgICAgICAgIGl0ZW0uZmllbGRzLnNldChrZXksIHZhbHVlLm1hcCh4ID0+IFZhbHVlcy5tYXBMZWF2ZXMoeCwgdCA9PiAoVmFsdWVzLmlzTGluayh0KSA/IGxpbmtOb3JtYWxpemVyKHQpIDogdCkpKSk7DQogICAgICAgICAgICAgICAgICB9DQogICAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgICAgaWYgKGRhdGEubGlua3MpIHsNCiAgICAg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aWNhbGl6YXRpb24gY2FjaGUuDQogICAgICAgICAgbGV0IHJlYWxDYWNoZSA9IGNhY2hlICE9PSBudWxsICYmIGNhY2hlICE9PSB2b2lkIDAgPyBjYWNoZSA6IG5ldyBMaXN0U2VyaWFsaXphdGlvbkNhY2hlKHRoaXMubGlzdHMpOw0KICAgICAgICAgIGxldCByZXN1bHQgPSB7DQogICAgICAgICAgICAgIGZpbGU6IHsNCiAgICAgICAgICAgICAgICAgIHBhdGg6IHRoaXMucGF0aCwNCiAgICAgICAgICAgICAgICAgIGZvbGRlcjogdGhpcy5mb2xkZXIoKSwNCiAgICAgICAgICAgICAgICAgIG5hbWU6IHRoaXMubmFtZSgpLA0KICAgICAgICAgICAgICAgICAgbGluazogTGluay5maWxlKHRoaXMucGF0aCksDQogICAgICAgICAgICAgICAgICBvdXRsaW5rczogdGhpcy5maWxlTGlua3MoKSwNCiAgICAgICAgICAgICAgICAgIGlubGlua3M6IEFycmF5LmZyb20oaW5kZXgubGlua3MuZ2V0SW52ZXJzZSh0aGlzLnBhdGgpKS5tYXAobCA9PiBMaW5rLmZpbGUobCkpLA0KICAgICAgICAgICAgICAgICAgZXRhZ3M6IEFycmF5LmZyb20odGhpcy50YWdzKSwNCiAgICAgICAgICAgICAgICAgIHRhZ3M6IEFycmF5LmZyb20odGhpcy5mdWxsVGFncygpKSwNCiAgICAgICAgICAgICAgICAgIGFsaWFzZXM6IEFycmF5LmZyb20odGhpcy5hbGlhc2VzKSwNCiAgICAgICAgICAgICAgICAgIGxpc3RzOiB0aGlzLmxpc3RzLm1hcChsID0+IHJlYWxDYWNoZS5nZXQobC5saW5lKSksDQogICAgICAgICAgICAgICAgICB0YXNrczogdGhpcy5saXN0cy5maWx0ZXIobCA9PiAhIWwudGFzaykubWFwKGwgPT4gcmVhbENhY2hlLmdldChsLmxpbmUpKSwNCiAgICAgICAgICAgICAgICAgIGN0aW1lOiB0aGlzLmN0aW1lLA0KICAgICAgICAgICAgICAgICAgY2RheTogc3RyaXBUaW1lKHRoaXMuY3RpbWUpLA0KICAgICAgICAgICAgICAgICAgbXRpbWU6IHRoaXMubXRpbWUsDQogICAgICAgICAgICAgICAgICBtZGF5OiBzdHJpcFRpbWUodGhpcy5tdGltZSksDQogICAgICAgICAgICAgICAgICBzaXplOiB0aGlzLnNpemUsDQogICAgICAgICAgICAgICAgICBzdGFycmVkOiBpbmRleC5zdGFycmVkLnN0YXJyZWQodGhpcy5wYXRoKSwNCiAgICAgICAgICAgICAgICAgIGZyb250bWF0dGVyOiBWYWx1ZXMuZGVlcENvcHkodGhpcy5mcm9udG1hdHRlciksDQogICAgICAgICAgICAgICAgICBleHQ6IHRoaXMuZXh0ZW5zaW9uKCksDQogICAgICAgICAgICAgIH0sDQogICAgICAgICAgfTsNCiAgICAgICAgICAvLyBBZGQgdGhlIGN1cnJlbnQgZGF5IGlmIHByZXNlbnQuDQogICAgICAgICAgaWYgKHRoaXMuZGF5KQ0KICAgICAgICAgICAgICByZXN1bHQuZmlsZS5kYXkgPSB0aGlzLmRheTsNCiAgICAgICAgICAvLyBUaGVuIGFwcGVuZCB0aGUgY29tcHV0ZWQgZmllbGRzLg0KICAgICAgICAgIGZvciAobGV0IFtrZXksIHZhbHVlXSBvZiB0aGlzLmZpZWxkcy5lbnRyaWVzKCkpIHsNCiAgICAgICAgICAgICAgaWYgKGtleSBpbiByZXN1bHQpDQogICAgICAgICAgICAgICAgICBjb250aW51ZTsgLy8gRG9uJ3QgYWxsb3cgZmllbGRzIHRvIG92ZXJyaWRlIGV4aXN0aW5nIGtleXMuDQogICAgICAgICAgICAgIHJlc3VsdFtrZXldID0gdmFsdWU7DQogICAgICAgICAgfQ0KICAgICAgICAgIHJldHVybiByZXN1bHQ7DQogICAgICB9DQogIH0NCiAgLyoqIEEgbGlzdCBpdGVtIGluc2lkZSBvZiBhIGxpc3QuICovDQogIGNsYXNzIExpc3RJdGVtIHsNCiAgICAgIGNvbnN0cnVjdG9yKGluaXQpIHsNCiAgICAgICAgICBPYmplY3QuYXNzaWduKHRoaXMsIGluaXQpOw0KICAgICAgICAgIHRoaXMuZmllbGRzID0gdGhpcy5maWVsZHMgfHwgbmV3IE1hcCgpOw0KICAgICAgICAgIHRoaXMudGFncyA9IHRoaXMudGFncyB8fCBuZXcgU2V0KCk7DQogICAgICAgICAgdGhpcy5jaGlsZHJlbiA9IHRoaXMuY2hpbGRyZW4gfHwgW107DQogICAgICAgICAgdGhpcy5saW5rcyA9IHRoaXMubGlua3MgfHwgW107DQogICAgICB9DQogICAgICBpZCgpIHsNCiAgICAgICAgICByZXR1cm4gYCR7dGhpcy5maWxlKCkucGF0aH0tJHt0aGlzLmxpbmV9YDsNCiAgICAgIH0NCiAgICAgIGZpbGUoKSB7DQogICAgICAgICAgcmV0dXJuIHRoaXMubGluay50b0ZpbGUoKTsNCiAgICAgIH0NCiAgICAgIG1hcmtkb3duKCkgew0KICAgICAgICAgIGlmICh0aGlzLnRhc2spDQogICAgICAgICAgICAgIHJldHVybiBgJHt0aGlzLnN5bWJvbH0gWyR7dGhpcy50YXNrLmNvbXBsZXRlZCA/ICJ4IiA6ICIgIn1dICR7dGhpcy50ZXh0fWA7DQogICAgICAgICAgZWxzZQ0KICAgICAgICAgICAgICByZXR1cm4gYCR7dGhpcy5zeW1ib2x9ICR7dGhpcy50ZXh0fWA7DQogICAgICB9DQogICAgICBjcmVhdGVkKCkgew0KICAgICAgICAgIHZhciBfYSwgX2IsIF9jOw0KICAgICAgICAgIHJldHVybiAoX2MgPSAoKF9iID0gKF9hID0gdGhpcy5maWVsZHMuZ2V0KCJjcmVhdGVkIikpICE9PSBudWxsICYmIF9hICE9PSB2b2lkIDAgPyBfYSA6IHRoaXMuZmllbGRzLmdldCgiY3RpbWUiKSkgIT09IG51bGwgJiYgX2IgIT09IHZvaWQgMCA/IF9iIDogdGhpcy5maWVsZHMuZ2V0KCJjZGF5IikpKSA9PT0gbnVsbCB8fCBfYyA9PT0gdm9pZCAwID8gdm9pZCAwIDogX2NbMF07DQogICAg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w0KICAgICAgICAgIHJldHVybiAoX2EgPSB0aGlzLmZpZWxkcy5nZXQoInNjaGVkdWxlZCIpKSA9PT0gbnVsbCB8fCBfYSA9PT0gdm9pZCAwID8gdm9pZCAwIDogX2FbMF07DQogICAgICB9DQogICAgICAvKiogQ3JlYXRlIGFuIEFQSS1mcmllbmRseSBjb3B5IG9mIHRoaXMgbGlzdCBpdGVtLiBEZS1kdXBsaWNhdGlvbiBpcyBkb25lIHZpYSB0aGUgcHJvdmlkZWQgY2FjaGUuICovDQogICAgICBzZXJpYWxpemUoY2FjaGUpIHsNCiAgICAgICAgICAvLyBNYXAgY2hpbGRyZW4gdG8gdGhlaXIgc2VyaWFsaXplZC9kZS1kdXBsaWNhdGVkIGVxdWl2YWxlbnRzIHJpZ2h0IGF3YXkuDQogICAgICAgICAgbGV0IGNoaWxkcmVuID0gdGhpcy5jaGlsZHJlbi5tYXAobCA9PiBjYWNoZS5nZXQobCkpLmZpbHRlcigobCkgPT4gbCAhPT0gdW5kZWZpbmVkKTsNCiAgICAgICAgICBsZXQgcmVzdWx0ID0gew0KICAgICAgICAgICAgICBzeW1ib2w6IHRoaXMuc3ltYm9sLA0KICAgICAgICAgICAgICBsaW5rOiB0aGlzLmxpbmssDQogICAgICAgICAgICAgIHNlY3Rpb246IHRoaXMuc2VjdGlvbiwNCiAgICAgICAgICAgICAgdGV4dDogdGhpcy50ZXh0LA0KICAgICAgICAgICAgICB0YWdzOiBBcnJheS5mcm9tKHRoaXMudGFncyksDQogICAgICAgICAgICAgIGxpbmU6IHRoaXMubGluZSwNCiAgICAgICAgICAgICAgbGluZUNvdW50OiB0aGlzLmxpbmVDb3VudCwNCiAgICAgICAgICAgICAgbGlzdDogdGhpcy5saXN0LA0KICAgICAgICAgICAgICBvdXRsaW5rczogQXJyYXkuZnJvbSh0aGlzLmxpbmtzKSwNCiAgICAgICAgICAgICAgcGF0aDogdGhpcy5saW5rLnBhdGgsDQogICAgICAgICAgICAgIGNoaWxkcmVuOiBjaGlsZHJlbiwNCiAgICAgICAgICAgICAgdGFzazogISF0aGlzLnRhc2ssDQogICAgICAgICAgICAgIGFubm90YXRlZDogdGhpcy5maWVsZHMuc2l6ZSA+IDAsDQogICAgICAgICAgICAgIHBvc2l0aW9uOiBWYWx1ZXMuZGVlcENvcHkodGhpcy5wb3NpdGlvbiksDQogICAgICAgICAgICAgIHN1YnRhc2tzOiBjaGlsZHJlbiwNCiAgICAgICAgICAgICAgcmVhbDogISF0aGlzLnRhc2ssDQogICAgICAgICAgICAgIGhlYWRlcjogdGhpcy5zZWN0aW9uLCAvLyBAZGVwcmVjYXRlZCwgdXNlICdpdGVtLnNlY3Rpb24nIGluc3RlYWQuDQogICAgICAgICAgfTsNCiAgICAgICAgICBpZiAodGhpcy5wYXJlbnQgfHwgdGhpcy5wYXJlbnQgPT09IDApDQogICAgICAgICAgICAgIHJlc3VsdC5wYXJlbnQgPSB0aGlzLnBhcmVudDsNCiAgICAgICAgICBpZiAodGhpcy5ibG9ja0lkKQ0KICAgICAgICAgICAgICByZXN1bHQuYmxvY2tJZCA9IHRoaXMuYmxvY2tJZDsNCiAgICAgICAgICBhZGRGaWVsZHModGhpcy5maWVsZHMsIHJlc3VsdCk7DQogICAgICAgICAgaWYgKHRoaXMudGFzaykgew0KICAgICAgICAgICAgICByZXN1bHQuc3RhdHVzID0gdGhpcy50YXNrLnN0YXR1czsNCiAgICAgICAgICAgICAgcmVzdWx0LmNoZWNrZWQgPSB0aGlzLnRhc2suY2hlY2tlZDsNCiAgICAgICAgICAgICAgcmVzdWx0LmNvbXBsZXRlZCA9IHRoaXMudGFzay5jb21wbGV0ZWQ7DQogICAgICAgICAgICAgIHJlc3VsdC5mdWxseUNvbXBsZXRlZCA9IHRoaXMudGFzay5mdWxseUNvbXBsZXRlZDsNCiAgICAgICAgICAgICAgbGV0IGNyZWF0ZWQgPSB0aGlzLmNyZWF0ZWQoKSwgZHVlID0gdGhpcy5kdWUoKSwgY29tcGxldGVkID0gdGhpcy5jb21w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ZW1zKSB7DQogICAgICAgICAgdGhpcy5saXN0SXRlbXMgPSB7fTsNCiAgICAgICAgICB0aGlzLmNhY2hlID0ge307DQogICAgICAgICAgdGhpcy5zZWVuID0gbmV3IFNldCgpOw0KICAgICAgICAgIGZvciAobGV0IGl0ZW0gb2YgbGlzdEl0ZW1zKQ0KICAgICAgICAgICAgICB0aGlzLmxpc3RJdGVtc1tpdGVtLmxpbmVdID0gaXRlbTsNCiAgICAgIH0NCiAgICAgIGdldChsaW5lbm8pIHsNCiAgICAgICAgICBpZiAobGluZW5vIGluIHRoaXMuY2FjaGUpDQogICAgICAgICAgICAgIHJldHVybiB0aGlzLmNhY2hlW2xpbmVub107DQogICAgICAgICAgZWxzZSBpZiAodGhpcy5zZWVuLmhhcyhsaW5lbm8pKSB7DQogICAgICAgICAgICAgIGNvbnNvbGUubG9nKGBEYXRhdmlldzogRW5jb3VudGVyZWQgYSBjaXJjdWxhciBsaXN0IChsaW5lIG51bWJlciAke2xpbmVub307IGNoaWxkcmVuICR7dGhpcy5saXN0SXRlbXNbbGluZW5vXS5jaGlsZHJlbi5qb2luKCIsICIpfSlgKTsNCiAgICAgICAgICAgICAgcmV0dXJuIHVuZGVmaW5lZDsNCiAgICAgICAgICB9DQogICAgICAgICAgdGhpcy5zZWVuLmFkZChsaW5lbm8pOw0KICAgICAgICAgIGxldCByZXN1bHQgPSB0aGlzLmxpc3RJdGVtc1tsaW5lbm9dLnNlcmlhbGl6ZSh0aGlzKTsNCiAgICAgICAgICB0aGlzLmNhY2hlW2xpbmVub10gPSByZXN1bHQ7DQogICAgICAgICAgcmV0dXJuIHJlc3VsdDsNCiAgICAgIH0NCiAgfQ0KICBmdW5jdGlvbiBhZGRGaWVsZHMoZmllbGRzLCB0YXJnZXQpIHsNCiAgICAgIGZvciAobGV0IFtrZXksIHZhbHVlc10gb2YgZmllbGRzLmVudHJpZXMoKSkgew0KICAgICAgICAgIGlmIChrZXkgaW4gdGFyZ2V0KQ0KICAgICAgICAgICAg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G9zaXRpb24iKQ0KICAgICAgICAgICAgICAgICAgY29udGludWU7DQogICAgICAgICAgICAgIGFkZElubGluZUZpZWxkKGtleSwgdmFsdWUsIGZpZWxkcyk7DQogICAgICAgICAgfQ0KICAgICAgfQ0KICAgICAgLy8gTGlua3MgaW4gbWV0YWRhdGEuDQogICAgICBjb25zdCBsaW5rc0J5TGluZSA9IHt9Ow0KICAgICAgZm9yIChsZXQgcmF3TGluayBvZiBtZXRhZGF0YS5saW5rcyB8fCBbXSkgew0KICAgICAgICAgIGNvbnN0IGxpbmsgPSBMaW5rLmluZmVyKHJhd0xpbmsubGluaywgZmFsc2UsIHJhd0xpbmsuZGlzcGxheVRleHQpOw0KICAgICAgICAgIGNvbnN0IGxpbmUgPSByYXdMaW5rLnBvc2l0aW9uLnN0YXJ0LmxpbmU7DQogICAgICAgICAgbGlua3MucHVzaChsaW5rKTsNCiAgICAgICAgICBpZiAoIShsaW5lIGluIGxpbmtzQnlMaW5lKSkNCiAgICAgICAgICAgICAgbGlua3NCeUxpbmVbbGluZV0gPSBbbGlua107DQogICAgICAgICAgZWxzZQ0KICAgICAgICAgICAgICBsaW5rc0J5TGluZVtsaW5lXS5wdXNoKGxpbmspOw0KICAgICAgfQ0KICAgICAgLy8gRW1iZWQgTGlua3MgaW4gbWV0YWRhdGEuDQogICAgICBmb3IgKGxldCByYXdFbWJlZCBvZiBtZXRhZGF0YS5lbWJlZHMgfHwgW10pIHsNCiAgICAgICAgICBjb25zdCBsaW5rID0gTGluay5pbmZlcihyYXdFbWJlZC5saW5rLCB0cnVlLCByYXdFbWJlZC5kaXNwbGF5VGV4dCk7DQogICAgICAgICAgY29uc3QgbGluZSA9IHJhd0VtYmVkLnBvc2l0aW9uLnN0YXJ0LmxpbmU7DQogICAgICAgICAgbGlua3MucHVzaChsaW5rKTsNCiAgICAgICAgICBpZiAoIShsaW5lIGluIGxpbmtzQnlMaW5lKSkNCiAgICAgICAgICAgICAgbGlua3NCeUxpbmVbbGluZV0gPSBbbGlua107DQogICAgICAgICAgZWxzZQ0KICAgICAgICAgICAgICBsaW5rc0J5TGluZVtsaW5lXS5wdXNoKGxpbmspOw0KICAgICAgfQ0KICAgICAgLy8gTWVyZ2UgZnJvbnRtYXR0ZXIgZmllbGRzIHdpdGggcGFyc2VkIGZpZWxkcy4NCiAgICAgIGxldCBtYXJrZG93bkRhdGEgPSBwYXJzZU1hcmtkb3duKHBhdGgsIGNvbnRlbnRzLnNwbGl0KCJcbiIpLCBtZXRhZGF0YSwgbGlua3NCeUxpbmUpOw0KICAgICAgbWVyZ2VGaWVsZEdyb3VwcyhmaWVsZHMsIG1hcmtkb3duRGF0YS5maWVsZHMpOw0KICAgICAgLy8gU3RyaXAgInBvc2l0aW9uIiBmcm9tIGZyb250bWF0dGVyIHNpbmNlIGl0IGlzIE9ic2lkaWFuIGRldGVybWluZWQuDQogICAgICBjb25zdCBmcm9udG1hdHRlciA9IG1ldGFkYXRhLmZyb250bWF0dGVyIHx8IHt9Ow0KICAgICAgaWYgKGZyb250bWF0dGVyICYmICJwb3NpdGlvbiIgaW4gZnJvbnRtYXR0ZXIpDQogICAgICAgICAgZGVsZXRlIGZyb250bWF0dGVyWyJwb3NpdGlvbiJdOw0KICAgICAgcmV0dXJuIG5ldyBQYWdlTWV0YWRhdGEocGF0aCwgew0KICAgICAgICAgIHRhZ3MsDQogICAgICAgICAgYWxpYXNlcywNCiAgICAgICAgICBsaW5rcywNCiAgICAgICAgICBsaXN0czogbWFya2Rvd25EYXRhLmxpc3RzLA0KICAgICAgICAgIGZpZWxkczogZmluYWxpemVJbmxpbmVGaWVsZHMoZmllbGRzKSwNCiAgICAgICAgICBmcm9udG1hdHRlcjogZnJvbnRtYXR0ZXIsDQogICAgICAgICAgY3RpbWU6IERhdGVUaW1lLmZyb21NaWxsaXMoc3RhdC5jdGltZSksDQogICAgICAgICAgbXRpbWU6IERhdGVUaW1lLmZyb21NaWxsaXMoc3RhdC5tdGltZSksDQogICAgICAgICAgc2l6ZTogc3R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pbmVzVG9Ta2lwLmhhcyhsaW5lbm8pKQ0KICAgICAgICAgICAgICAgICAgY29udGludWU7DQogICAgICAgICAgICAgIC8vIEZhc3QgYmFpbC1vdXQgZm9yIGxpbmVzIHRoYXQgYXJlIHRvbyBsb25nIG9yIGRvIG5vdCBjb250YWluICc6OicuDQogICAgICAgICAgICAgIGlmIChsaW5lLmxlbmd0aCA+IDMyNzY4IHx8ICFsaW5lLmluY2x1ZGVzKCI6OiIpKQ0KICAgICAgICAgICAgICAgICAgY29udGludWU7DQogICAgICAgICAgICAgIGxpbmUgPSBsaW5lLnRyaW0oKTsNCiAgICAgICAgICAgICAgbGV0IGlubGluZUZpZWxkcyA9IGV4dHJhY3RJbmxpbmVGaWVsZHMobGluZSk7DQogICAgICAgICAgICAgIGlmIChpbmxpbmVGaWVsZHMubGVuZ3RoID4gMCkgew0KICAgICAgICAgICAgICAgICAgZm9yIChsZXQgaWZpZWxkIG9mIGlubGluZUZpZWxkcykNCiAgICAgICAgICAgICAgICAgICAgICBhZGRSYXdJbmxpbmVGaWVsZChpZmllbGQsIGZpZWxkcyk7DQogICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgZWxzZSB7DQogICAgICAgICAgICAgICAgICBsZXQgZnVsbExpbmUgPSBleHRyYWN0RnVsbExpbmVGaWVsZChsaW5lKTsNCiAgICAgICAgICAgICAgICAgIGlmIChmdWxsTGluZSkNCiAgICAgICAgICAgICAgICAgICAgICBhZGRSYXdJbmxpbmVGaWVsZChmdWxsTGluZSwgZmllbGRzKTsNCiAgICAgICAgICAgICAgfQ0KICAgICAgICAgIH0NCiAgICAgIH0NCiAgICAgIHJldHVybiB7IGZpZWxkcywgbGlzdHMgfTsNCiAgfQ0KICAvLyBUT0RPOiBDb25zaWRlciB1c2luZyBhbiBhY3R1YWwgcGFyc2VyIGluIGxlaXUgb2YgYSBtb3JlIGV4cGVuc2l2ZSByZWdleC4NCiAgY29uc3QgTElTVF9JVEVNX1JFR0VYID0gL15bXHM+XS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zZXN0TGluayA9IHJhd0VsZW1lbnQuaWQgPT09IHVuZGVmaW5lZCA/IHNlY3Rpb25MaW5rIDogTGluay5ibG9jayhwYXRoLCByYXdFbGVtZW50LmlkKTsNCiAgICAgICAgICAvLyBHYXRoZXIgYW55IGxpbmtzIHRoYXQgb2NjdXIgb24gdGhlIHNhbWUgbGluZXMgYXMgdGhlIHRhc2suDQogICAgICAgICAgY29uc3QgbGlua3MgPSBbXTsNCiAgICAgICAgICBmb3IgKGxldCBsaW5lID0gcmF3RWxlbWVudC5wb3NpdGlvbi5zdGFydC5saW5lOyBsaW5lIDw9IHJhd0VsZW1lbnQucG9zaXRpb24uZW5kLmxpbmU7IGxpbmUrKykgew0KICAgICAgICAgICAgICBpZiAobGlua3NCeUxpbmVbbGluZV0pDQogICAgICAgICAgICAgICAgICBsaW5rcy5wdXNoKC4uLmxpbmtzQnlMaW5lW2xpbmVdKTsNCiAgICAgICAgICB9DQogICAgICAgICAgLy8gQ29uc3RydWN0IHVuaXZlcnNhbCBpbmZvcm1hdGlvbiBhYm91dCB0aGlzIGVsZW1lbnQgKGJlZm9yZSB0YXNrcykuDQogICAgICAgICAgbGV0IGl0ZW0gPSBuZXcgTGlzdEl0ZW0oew0KICAgICAgICAgICAgICBzeW1ib2w6IHJhd01hdGNoWzFdLA0KICAgICAgICAgICAgICBsaW5rOiBjbG9zZXN0TGluaywNCiAgICAgICAgICAgICAgbGlua3M6IGxpbmtzLA0KICAgICAgICAgICAgICBzZWN0aW9uOiBzZWN0aW9uTGluaywNCiAgICAgICAgICAgICAgdGV4dDogdGV4dFdpdGhOZXdsaW5lLA0KICAgICAgICAgICAgICB0YWdzOiBleHRyYWN0VGFncyQxKHRleHROb05ld2xpbmUpLA0KICAgICAgICAgICAgICBsaW5lOiByYXdFbGVtZW50LnBvc2l0aW9uLnN0YXJ0LmxpbmUsDQogICAgICAgICAgICAgIGxpbmVDb3VudDogcmF3RWxlbWVudC5wb3NpdGlvbi5lbmQubGluZSAtIHJhd0VsZW1lbnQucG9zaXRpb24uc3RhcnQubGluZSArIDEsDQogICAgICAgICAgICAgIGxpc3Q6IGNvbnRhaW5pbmdMaXN0SWQgPT0gLTEgPyAtMSA6IChtZXRhZGF0YS5zZWN0aW9ucyB8fCBbXSlbY29udGFpbmluZ0xpc3RJZF0ucG9zaXRpb24uc3RhcnQubGluZSwNCiAgICAgICAgICAgICAgcG9zaXRpb246IHJhd0VsZW1lbnQucG9zaXRpb24sDQogICAgICAgICAgICAgIGNoaWxkcmVuOiBbXSwNCiAgICAgICAgICAgICAgYmxvY2tJZDogcmF3RWxlbWVudC5pZCwNCiAgICAgICAgICB9KTsNCiAgICAgICAgICBpZiAocmF3RWxlbWVudC5wYXJlbnQgPj0gMCAmJiByYXdFbGVtZW50LnBhcmVudCAhPSBpdGVtLmxpbmUpDQogICAgICAgICAgICAgIGl0ZW0ucGFyZW50ID0gcmF3RWxlbWVudC5wYXJlbnQ7DQogICAgICAgICAgLy8gU2V0IHVwIHRoZSBiYXNpYyB0YXNrIGluZm9ybWF0aW9uIGZvciBub3csIHRob3VnaCB3ZSBoYXZlIHRvIHJlY29tcHV0ZSBgZnVsbHlDb21wdXRlZGAgbGF0ZXIuDQogICAgICAgICAgaWYgKHJhd0VsZW1lbnQudGFzaykgew0KICAgICAgICAgICAgICBpdGVtLnRhc2sgPSB7DQogICAgICAgICAgICAgICAgICBzdGF0dXM6IHJhd0VsZW1lbnQudGFzaywNCiAgICAgICAgICAgICAgICAgIGNoZWNrZWQ6IHJhd0VsZW1lbnQudGFzayAhPSAiIiAmJiByYXdFbGVtZW50LnRhc2sgIT0gIiAiLA0KICAgICAgICAgICAgICAgICAgY29tcGxldGVkOiByYXdFbGVtZW50LnRhc2sgPT0gIlgiIHx8IHJhd0VsZW1lbnQudGFzayA9PSAieCIsDQogICAgICAgICAgICAgICAgICBmdWxseUNvbXBsZXRlZDogcmF3RWxlbWVudC50YXNrID09ICJYIiB8fCByYXdFbGVtZW50LnRhc2sgPT0gIngiL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VGaWVsZEdyb3Vwcyhyb290ID09PSB1bmRlZmluZWQgfHwgcm9vdCA9PSBudWxsID8gbGl0ZXJhbHMgOiByb290LmZpZWxkcywgbGlzdEl0ZW0uZmllbGRzKTsNCiAgICAgICAgICAgICAgKi8NCiAgICAgICAgICB9DQogICAgICAgICAgLy8gUGFzcyAzOiBQcm9wb2dhdGUgYGZ1bGx5Q29tcGxldGVkYCB1cCB0aGUgdGFzayB0cmVlLiBUaGlzIGlzIGEgbGl0dGxlIGxlc3MgZWZmaWNpZW50IHRoYW4ganVzdCBkb2luZyBhIHNpbXBsZQ0KICAgICAgICAgIC8vIERGUyB1c2luZyB0aGUgY2hpbGRyZW4gSURzLCBidXQgaXQncyBwcm9iYWJseSBmaW5lLg0KICAgICAgICAgIGlmIChsaXN0SXRlbS50YXNrKSB7DQogICAgICAgICAgICAgIGxldCBjdXJyID0gbGlzdEl0ZW07DQogICAgICAgICAgICAgIHdoaWxlICghIWN1cnIpIHsNCiAgICAgICAgICAgICAgICAgIGlmIChjdXJyLnRhc2spDQogICAgICAgICAgICAgICAgICAgICAgY3Vyci50YXNrLmZ1bGx5Q29tcGxldGVkID0gY3Vyci50YXNrLmZ1bGx5Q29tcGxldGVkICYmIGxpc3RJdGVtLnRhc2suY29tcGxldGVkOw0KICAgICAgICAgICAgICAgICAgY3VyciA9IGNhY2hlWyhfYSA9IGN1cnIucGFyZW50KSAhPT0gbnVsbCAmJiBfYSAhPT0gdm9pZCAwID8gX2EgOiAtMV07DQogICAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICB9DQogICAgICByZXR1cm4gW09iamVjdC52YWx1ZXMoY2FjaGUpLCBsaXRlcmFsc107DQogIH0NCiAgLyoqIEF0dGVtcHQgdG8gZmluZCBhIGRhdGUgYXNzb2NpYXRlZCB3aXRoIHRoZSBnaXZlbiBwYWdlIGZyb20gbWV0YWRhdGEgb3IgZmlsZW5hbWVzLiAqLw0KICBmdW5jdGlvbiBmaW5kRGF0ZShmaWxl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AgfQ0KICAgICAgcmV0dXJuIGV4dHJhY3REYXRlKGdldEZpbGVUaXRsZShmaWxlKSk7DQogIH0NCiAgLyoqIFJlY3Vyc2l2ZWx5IGNvbnZlcnQgZnJvbnRtYXR0ZXIgaW50byBmaWVsZHMuIFdlIGhhdmUgdG8gZGFuY2UgYXJvdW5kIFlBTUwgc3RydWN0dXJlLiAqLw0KICBmdW5jdGlvbiBwYXJzZUZyb250bWF0dGVyKHZhbHVlKSB7DQogICAgICBpZiAodmFsdWUgPT0gbnVsbCkgew0KICAgICAgICAgIHJldHVybiBudWxsOw0KICAgICAgfQ0KICAgICAgZWxzZSBpZiAodHlwZW9mIHZhbHVlID09PSAib2JqZWN0Iikgew0KICAgICAgICAgIGlmIChBcnJheS5pc0FycmF5KHZhbHVlKSkgew0KICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0gW107DQogICAgICAgICAgICAgIGZvciAobGV0IGNoaWxkIG9mIHZhbHVlKSB7DQogICAgICAgICAgICAgICAgICByZXN1bHQucHVzaChwYXJzZUZyb250bWF0dGVyKGNoaWxkKSk7DQogICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSBpZiAodmFsdWUgaW5zdGFuY2VvZiBEYXRlKSB7DQogICAgICAgICAgICAgIGxldCBkYXRlUGFyc2UgPSBEYXRlVGltZS5mcm9tSlNEYXRlKHZhbHVlKTsNCiAgICAgICAgICAgICAgcmV0dXJuIGRhdGVQYXJzZTsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSB7DQogICAgICAgICAgICAgIGxldCBvYmplY3QgPSB2YWx1ZTsNCiAgICAgICAgICAgICAgbGV0IHJlc3VsdCA9IHt9Ow0KICAgICAgICAgICAgICBmb3IgKGxldCBrZXkgaW4gb2JqZWN0KSB7DQogICAgICAgICAgICAgICAgICByZXN1bHRba2V5XSA9IHBhcnNlRnJvbnRtYXR0ZXIob2JqZWN0W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IGZpZWxkcy5oYXMobm9ybUtleSkpDQogICAgICAgICAgICAgIGNvbnRpbnVlOw0KICAgICAgICAgIGlmICghbm9ybWFsaXplZC5oYXMobm9ybUtleSkpDQogICAgICAgICAgICAgIG5vcm1hbGl6ZWQuc2V0KG5vcm1LZXksIHZhbHVlcyk7DQogICAgICAgICAgZWxzZQ0KICAgICAgICAgICAgICBub3JtYWxpemVkLnNldChub3JtS2V5LCBub3JtYWxpemVkLmdldChub3JtS2V5KS5jb25jYXQodmFsdWVzKSk7DQogICAgICB9DQogICAgICAvLyBDb21iaW5lIG5vcm1hbGl6ZWQgKyBub3JtYWwga2V5cy4NCiAgICAgIGxldCBpbnRlcmltID0gbmV3IE1hcCgpOw0KICAgICAgbWVyZ2VGaWVsZEdyb3VwcyhpbnRlcmltLCBmaWVsZHMpOw0KICAgICAgbWVyZ2VGaWVsZEdyb3VwcyhpbnRlcmltLCBub3JtYWxpemVkKTsNCiAgICAgIC8vIEFuZCB0aGVuIGZsYXR0ZW4gdGhlbS4NCiAgICAgIGxldCByZXN1bHQgPSBuZXcgTWFwKCk7DQogICAgICBmb3IgKGxldCBba2V5LCB2YWx1ZV0gb2YgaW50ZXJpbS5lbnRyaWVzKCkpIHsNCiAgICAgICAgICBpZiAodmFsdWUubGVuZ3RoID09IDEpDQogICAgICAgICAgICAgIHJlc3VsdC5zZXQoa2V5LCB2YWx1ZVswXSk7DQogICAgICAgICAgZWxzZQ0KICAgICAgICAgICAgICByZXN1bHQuc2V0KGtleSwgdmFsdWUpOw0KICAgICAgfQ0KICAgICAgcmV0dXJuIHJlc3VsdDsNCiAgfQ0KICAvKiogQ29weSBhbGwgZmllbGRzIG9mICdzb3VyY2UnIGludG8gJ3RhcmdldCcuICovDQogIGZ1bmN0aW9uIG1lcmdlRmllbGRHcm91cHModGFyZ2V0LCBzb3VyY2UpIHsNCiAgICAgIGZvciAobGV0IGtleSBvZiBzb3VyY2Uua2V5cygpKSB7DQogICAgICAgICAgaWYgKCF0YXJnZ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x1ZSk7DQogICAgICAgICAgaWYgKHdyYXBwZWQgPT09IHVuZGVmaW5lZCkNCiAgICAgICAgICAgICAgdGhyb3cgRXJyb3IoIlVucmVjb2duaXplZCB0cmFuc2ZlcmFibGUgdmFsdWU6ICIgKyB2YWx1ZSk7DQogICAgICAgICAgc3dpdGNoICh3cmFwcGVkLnR5cGUpIHsNCiAgICAgICAgICAgICAgY2FzZSAibnVsbCI6DQogICAgICAgICAgICAgIGNhc2UgIm51bWJlciI6DQogICAgICAgICAgICAgIGNhc2UgInN0cmluZyI6DQogICAgICAgICAgICAgIGNhc2UgImJvb2xlYW4iOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWU7DQogICAgICAgICAgICAgIGNhc2UgImRhdGUiOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHsNCiAgICAgICAgICAgICAgICAgICAgICAiX19fdHJhbnNmZXItdHlwZSI6ICJkYXRlIiwNCiAgICAgICAgICAgICAgICAgICAgICB2YWx1ZTogdHJhbnNmZXJhYmxlKHdyYXBwZWQudmFsdWUudG9PYmplY3QoKSksDQogICAgICAgICAgICAgICAgICAgICAgb3B0aW9uczogew0KICAgICAgICAgICAgICAgICAgICAgICAgICB6b25lOiB3cmFwcGVkLnZhbHVlLnpvbmUuZXF1YWxzKFN5c3RlbVpvbmUuaW5zdGFuY2UpID8gdW5kZWZpbmVkIDogd3JhcHBlZC52YWx1ZS56b25lTmFtZSwNCiAgICAgICAgICAgICAgICAgICAgICB9LA0KICAgICAgICAgICAgICAgICAgfTsNCiAgICAgICAgICAgICAgY2FzZSAiZHVyYXRpb24iOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHsgIl9fX3RyYW5zZmVyLXR5cGUiOiAiZHVyYXRpb24iLCB2YWx1ZTogdHJhbnNmZXJhYmxlKHdyYXBwZWQudmFsdWUudG9PYmplY3QoKSkgfTsNCiAgICAgICAgICAgICAgY2FzZSAiYXJyYXkiOg0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHdyYXBwZWQudmFsdWUubWFwKHYgPT4gdHJhbnNmZXJhYmxlKHYpKTsNCiAgICAgICAgICAgICAgY2FzZSAibGluayI6DQogICAgICAgICAgICAgICAgICByZXR1cm4geyAiX19fdHJhbnNmZXItdHlwZSI6ICJsaW5rIiwgdmFsdWU6IHRyYW5zZmVyYWJsZSh3cmFwcGVkLnZhbHVlLnRvT2JqZWN0KCkpIH07DQogICAgICAgICAgICAgIGNhc2UgIm9iamVjdCI6DQogICAgICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0ge307DQogICAgICAgICAgICAgICAgICBmb3IgKGxldCBba2V5LCB2YWx1ZV0gb2YgT2JqZWN0LmVudHJpZXMod3JhcHBlZC52YWx1ZSkpDQogICAgICAgICAgICAgICAgICAgICAgcmVzdWx0W2tleV0gPSB0cmFuc2ZlcmFibGUodmFsdWUpOw0KICAgICAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsNCiAgICAgICAgICB9DQogICAgICB9DQogICAgICBUcmFuc2ZlcmFibGUudHJhbnNmZXJhYmxlID0gdHJhbnNmZXJhYmxlOw0KICAgICAgLyoqIENvbnZlcnQgYSB0cmFuc2ZlcmFibGUgdmFsdWUgYmFjayB0byBhIGxpdGVyYWwgdmFsdWUgd2UgY2FuIHdvcmsgd2l0aC4gKi8NCiAgICAgIGZ1bmN0aW9uIHZhbHVlKHRyYW5zZmVyYWJsZSkgew0KICAgICAgICAgIGlmICh0cmFuc2ZlcmFibGUgPT09IG51bGwpIHsNCiAgICAgICAgICAgICAgcmV0dXJuIG51bGw7DQogICAgICAgICAgfQ0KICAgICAgICAgIGVsc2UgaWYgKHRyYW5zZmVyYWJsZSA9PT0gdW5kZWZpbmVkKSB7DQogICAgICAgICAgICAgIHJldHVybiB1bmRlZmluZWQ7DQogICAgICAgICAgfQ0KICAgICAgICAgIGVsc2UgaWYgKHRyYW5zZmVyYWJsZSBpbnN0YW5jZW9mIE1hcCkgew0KICAgICAgICAgICAgICBsZXQgcmVhbCA9IG5ldyBNYXAoKTsNCiAgICAgICAgICAgICAgZm9yIChsZXQgW2tleSwgdmFsXSBvZiB0cmFuc2ZlcmFibGUuZW50cmllcygpKQ0KICAgICAgICAgICAgICAgICAgcmVhbC5zZXQodmFsdWUoa2V5KSwgdmFsdWUodmFsKSk7DQogICAgICAgICAgICAgIHJldHVybiByZWFsOw0KICAgICAgICAgIH0NCiAgICAgICAgICBlbHNlIGlmICh0cmFuc2ZlcmFibGUgaW5zdGFuY2VvZiBTZXQpIHsNCiAgICAgICAgICAgICAgbGV0IHJlYWwgPSBuZXcgU2V0KCk7DQogICAgICAgICAgICAgIGZvciAobGV0IHZhbCBvZiB0cmFuc2ZlcmFibGUpDQogICAgICAgICAgICAgICAgICByZWFsLmFkZCh2YWx1ZSh2YWwpKTsNCiAgICAgICAgICAgICAgcmV0dXJuIHJlYWw7DQogICAgICAgICAgfQ0KICAgICAgICAgIGVsc2UgaWYgKEFycmF5LmlzQXJyYXkodHJhbnNmZXJhYmxlKSkgew0KICAgICAgICAgICAgICByZXR1cm4gdHJhbnNmZXJhYmxlLm1hcCh2ID0+IHZhbHVlKHYpKTsNCiAgICAgICAgICB9DQogICAgICAgICAgZWxzZSBpZiAodHlwZW9mIHRyYW5zZmVyYWJsZSA9PT0gIm9iamVjdCIpIHsNCiAgICAgICAgICAgICAgaWYgKCJfX190cmFuc2Zlci10eXBlIiBpbiB0cmFuc2ZlcmFibGUpIHsNCiAgICAgICAgICAgICAgICAgIHN3aXRjaCAodHJhbnNmZXJhYmxlWyJfX190cmFuc2Zlci10eXBlIl0pIHsNCiAgICAgICAgICAgICAgICAgICAgICBjYXNlICJkYXRlIjoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgbGV0IGRhdGVPcHRzID0gdmFsdWUodHJhbnNmZXJhYmxlLm9wdGlvbnMpOw0KICAgICAgICAgICAgICAgICAgICAgICAgICBsZXQgZGF0ZURhdGEgPSB2YWx1ZSh0cmFuc2ZlcmFibGUudmFsdWUpOw0KICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gRGF0ZVRpbWUuZnJvbU9iamVjdChkYXRlRGF0YSwgeyB6b25lOiBkYXRlT3B0cy56b25lIH0pOw0KICAgICAgICAgICAgICAgICAgICAgIGNhc2UgImR1cmF0aW9uIjoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIER1cmF0aW9uLmZyb21PYmplY3QodmFsdWUodHJhbnNmZXJhYmxlLnZhbHVlKSk7DQogICAgICAgICAgICAgICAgICAgICAgY2FzZSAibGluayI6DQogICAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBMaW5rLmZyb21PYmplY3QodmFsdWUodHJhbnNmZXJhYmxlLnZhbHVlKSk7DQogICAgICAgICAgICAgICAgICAgICAgZGVmYXVsdDoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgdGhyb3cgRXJyb3IoYFVucmVjb2duaXplZCB0cmFuc2ZlciB0eXBlICcke3RyYW5zZmVyYWJsZVsiX19fdHJhbnNmZXItdHlwZSJdfSdgKTsNCiAgICAgICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgfQ0KICAgICAgICAgICAgICBsZXQgcmVzdWx0ID0ge307DQogICAgICAgICAgICAgIGZvciAobGV0IFtrZXksIHZhbF0gb2YgT2JqZWN0LmVudHJpZXModHJhbnNmZXJhYmxlKSkNCiAgICAgICAgICAgICAgICAgIHJlc3VsdFtrZXldID0gdmFsdWUodmFsKTsNCiAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsNCiAgICAgICAgICB9DQogICAgICAgICAgcmV0dXJuIHRyYW5zZmVyYWJsZTsNCiAgICAgIH0NCiAgICAgIFRyYW5zZmVyYWJsZS52YWx1ZSA9IHZhbHVlOw0KICB9KShUcmFuc2ZlcmFibGUgfHwgKFRyYW5zZmVyYWJsZSA9IHt9KSk7CgogIC8qKiBFbnRyeS1wb2ludCBzY3JpcHQgdXNlZCBieSB0aGUgaW5kZXggYXMgYSB3ZWI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m9yOiBgRmFpbGVkIHRvIGluZGV4IGZpbGU6ICR7ZXZ0LmRhdGEucGF0aH06ICR7ZXJyb3J9YCwNCiAgICAgICAgICAgICAgfSwNCiAgICAgICAgICB9KTsNCiAgICAgIH0NCiAgfTsKCn0pKCk7Cgo=', null, false); -/* eslint-enable */ - -/** Controls and creates Dataview file importers, allowing for asynchronous loading and parsing of files. */ -/** Multi-threaded file parser which debounces rapid file requests automatically. */ -class FileImporter extends obsidian.Component { - constructor(numWorkers, vault, metadataCache) { - super(); - this.numWorkers = numWorkers; - this.vault = vault; - this.metadataCache = metadataCache; - this.workers = []; - this.busy = []; - this.reloadQueue = []; - this.reloadSet = new Set(); - this.callbacks = new Map(); - for (let index = 0; index < numWorkers; index++) { - let worker = new WorkerFactory({ name: "Dataview Indexer " + (index + 1) }); - worker.onmessage = evt => this.finish(evt.data.path, Transferable.value(evt.data.result), index); - this.workers.push(worker); - this.register(() => worker.terminate()); - this.busy.push(false); - } - } - /** - * Queue the given file for reloading. Multiple reload requests for the same file in a short time period will be de-bounced - * and all be resolved by a single actual file reload. - */ - reload(file) { - let promise = new Promise((resolve, reject) => { - var _a; - if (this.callbacks.has(file.path)) - (_a = this.callbacks.get(file.path)) === null || _a === void 0 ? void 0 : _a.push([resolve, reject]); - else - this.callbacks.set(file.path, [[resolve, reject]]); - }); - // De-bounce repeated requests for the same file. - if (this.reloadSet.has(file.path)) - return promise; - this.reloadSet.add(file.path); - // Immediately run this task if there are available workers; otherwise, add it to the queue. - let workerId = this.nextAvailableWorker(); - if (workerId !== undefined) { - this.send(file, workerId); - } - else { - this.reloadQueue.push(file); - } - return promise; - } - /** Finish the parsing of a file, potentially queueing a new file. */ - finish(path, data, index) { - var _a; - // Cache the callbacks before we do book-keeping. - let calls = [].concat((_a = this.callbacks.get(path)) !== null && _a !== void 0 ? _a : []); - // Book-keeping to clear metadata & allow the file to be re-loaded again. - this.reloadSet.delete(path); - this.callbacks.delete(path); - // Notify the queue this file is available for new work. - this.busy[index] = false; - // Queue a new job onto this worker. - let job = this.reloadQueue.shift(); - if (job !== undefined) - this.send(job, index); - // Resolve promises to let users know this file has finished. - if ("$error" in data) { - for (let [_, reject] of calls) - reject(data["$error"]); - } - else { - for (let [callback, _] of calls) - callback(data); - } - } - /** Send a new task to the given worker ID. */ - send(file, workerId) { - this.busy[workerId] = true; - this.vault.cachedRead(file).then(c => this.workers[workerId].postMessage({ - path: file.path, - contents: c, - stat: file.stat, - metadata: this.metadataCache.getFileCache(file), - })); - } - /** Find the next available, non-busy worker; return undefined if all workers are busy. */ - nextAvailableWorker() { - let index = this.busy.indexOf(false); - return index == -1 ? undefined : index; - } -} - -/** Stores various indices on all files in the vault to make dataview generation fast. */ -/** Aggregate index which has several sub-indices and will initialize all of them. */ -class FullIndex extends obsidian.Component { - /** Construct a new index using the app data and a current data version. */ - constructor(app, indexVersion, onChange) { - super(); - this.app = app; - this.indexVersion = indexVersion; - this.onChange = onChange; - this.initialized = false; - this.vault = app.vault; - this.metadataCache = app.metadataCache; - this.pages = new Map(); - this.tags = new ValueCaseInsensitiveIndexMap(); - this.etags = new ValueCaseInsensitiveIndexMap(); - this.links = new IndexMap(); - this.revision = 0; - // Caches metadata via durable storage to speed up cache initialization when Obsidian restarts. - this.persister = new LocalStorageCache(app.appId || "shared", indexVersion); - // Handles asynchronous reloading of files on web workers. - this.addChild((this.importer = new FileImporter(2, this.vault, this.metadataCache))); - // Prefix listens to file creation/deletion/rename, and not modifies, so we let it set up it's own listeners. - this.addChild((this.prefix = PrefixIndex.create(this.vault, () => this.touch()))); - // The CSV cache also needs to listen to filesystem events for cache invalidation. - this.addChild((this.csv = new CsvCache(this.vault))); - // The starred cache fetches starred entries semi-regularly via an interval. - this.addChild((this.starred = new StarredCache(this.app, () => this.touch()))); - } - /** Generate a full index from the given vault. */ - static create(app, indexVersion, onChange) { - return new FullIndex(app, indexVersion, onChange); - } - /** Trigger a metadata event on the metadata cache. */ - trigger(...args) { - this.metadataCache.trigger("dataview:metadata-change", ...args); - } - /** "Touch" the index, incrementing the revision number and causing downstream views to reload. */ - touch() { - this.revision += 1; - this.onChange(); - } - /** Runs through the whole vault to set up initial file metadata. */ - initialize() { - // The metadata cache is updated on initial file index and file loads. - this.registerEvent(this.metadataCache.on("resolve", file => this.reload(file))); - // Renames do not set off the metadata cache; catch these explicitly. - this.registerEvent(this.vault.on("rename", this.rename, this)); - // File creation does cause a metadata change, but deletes do not. Clear the caches for this. - this.registerEvent(this.vault.on("delete", af => { - if (!(af instanceof obsidian.TFile) || !PathFilters.markdown(af.path)) - return; - let file = af; - this.pages.delete(file.path); - this.tags.delete(file.path); - this.etags.delete(file.path); - this.links.delete(file.path); - this.touch(); - this.trigger("delete", file); - })); - // Asynchronously initialize actual content in the background. - this._initialize(this.vault.getMarkdownFiles()); - } - /** Drops the local storage cache and re-indexes all files; this should generally be used if you expect cache issues. */ - async reinitialize() { - await this.persister.recreate(); - const files = this.vault.getMarkdownFiles(); - const start = Date.now(); - let promises = files.map(file => this.reload(file)); - await Promise.all(promises); - console.log(`Dataview: re-initialized index with ${files.length} files (${(Date.now() - start) / 1000.0}s)`); - } - /** Internal asynchronous initializer. */ - async _initialize(files) { - let reloadStart = Date.now(); - let promises = files.map(l => this.reload(l)); - let results = await Promise.all(promises); - let cached = 0, skipped = 0; - for (let item of results) { - if (item.skipped) { - skipped += 1; - continue; - } - if (item.cached) - cached += 1; - } - this.initialized = true; - this.metadataCache.trigger("dataview:index-ready"); - console.log(`Dataview: all ${files.length} files have been indexed in ${(Date.now() - reloadStart) / 1000.0}s (${cached} cached, ${skipped} skipped).`); - // Drop keys for files which do not exist anymore. - let remaining = await this.persister.synchronize(files.map(l => l.path)); - if (remaining.size > 0) { - console.log(`Dataview: Dropped cache entries for ${remaining.size} deleted files.`); - } - } - rename(file, oldPath) { - if (!(file instanceof obsidian.TFile) || !PathFilters.markdown(file.path)) - return; - if (this.pages.has(oldPath)) { - const oldMeta = this.pages.get(oldPath); - this.pages.delete(oldPath); - if (oldMeta) { - oldMeta.path = file.path; - this.pages.set(file.path, oldMeta); - } - } - this.tags.rename(oldPath, file.path); - this.links.rename(oldPath, file.path); - this.etags.rename(oldPath, file.path); - this.touch(); - this.trigger("rename", file, oldPath); - } - /** Queue a file for reloading; this is done asynchronously in the background and may take a few seconds. */ - async reload(file) { - if (!PathFilters.markdown(file.path)) - return { cached: false, skipped: true }; - // The first load of a file is attempted from persisted cache; subsequent loads just use the importer. - if (this.pages.has(file.path) || this.initialized) { - await this.import(file); - return { cached: false, skipped: false }; - } - else { - // Check the cache for the latest data; if it is out of date or non-existent, then reload. - return this.persister.loadFile(file.path).then(async (cached) => { - if (!cached || cached.time < file.stat.mtime || cached.version != this.indexVersion) { - // This cache value is out of data, reload via the importer and update the cache. - // We will skip files with no active file metadata - they will be caught by a later reload - // via the 'resolve' metadata event. - let fileCache = this.metadataCache.getFileCache(file); - if (fileCache === undefined || fileCache === null) - return { cached: false, skipped: true }; - await this.import(file); - return { cached: false, skipped: false }; - } - else { - // Use the cached data since it is up to date and on the same version. - this.finish(file, cached.data); - return { cached: true, skipped: false }; - } - }); - } - } - /** Import a file directly from disk, skipping the cache. */ - async import(file) { - return this.importer.reload(file).then(r => { - this.finish(file, r); - this.persister.storeFile(file.path, r); - }); - } - /** Finish the reloading of file metadata by adding it to in memory indexes. */ - finish(file, parsed) { - let meta = PageMetadata.canonicalize(parsed, link => { - let realPath = this.metadataCache.getFirstLinkpathDest(link.path, file.path); - if (realPath) - return link.withPath(realPath.path); - else - return link; - }); - this.pages.set(file.path, meta); - this.tags.set(file.path, meta.fullTags()); - this.etags.set(file.path, meta.tags); - this.links.set(file.path, new Set(meta.links.map(l => l.path))); - this.touch(); - this.trigger("update", file); - } -} -/** Indexes files by their full prefix - essentially a simple prefix tree. */ -class PrefixIndex extends obsidian.Component { - constructor(vault, updateRevision) { - super(); - this.vault = vault; - this.updateRevision = updateRevision; - } - static create(vault, updateRevision) { - return new PrefixIndex(vault, updateRevision); - } - *walk(folder, filter) { - for (const file of folder.children) { - if (file instanceof obsidian.TFolder) { - yield* this.walk(file, filter); - } - else if (filter ? filter(file.path) : true) { - yield file.path; - } - } - } - /** Get the list of all files under the given path. */ - get(prefix, filter) { - let folder = this.vault.getAbstractFileByPath(prefix || "/"); - return new Set(folder instanceof obsidian.TFolder ? this.walk(folder, filter) : []); - } - /** Determines if the given path exists in the prefix index. */ - pathExists(path) { - return this.vault.getAbstractFileByPath(path || "/") != null; - } - /** Determines if the given prefix exists in the prefix index. */ - nodeExists(prefix) { - return this.vault.getAbstractFileByPath(prefix || "/") instanceof obsidian.TFolder; - } - /** - * Use the in-memory prefix index to convert a relative path to an absolute one. - */ - resolveRelative(path, origin) { - if (!origin) - return path; - else if (path.startsWith("/")) - return path.substring(1); - let relativePath = getParentFolder(origin) + "/" + path; - if (this.pathExists(relativePath)) - return relativePath; - else - return path; - } -} -/** Simple path filters which filter file types. */ -var PathFilters; -(function (PathFilters) { - function csv(path) { - return path.toLowerCase().endsWith(".csv"); - } - PathFilters.csv = csv; - function markdown(path) { - let lcPath = path.toLowerCase(); - return lcPath.endsWith(".md") || lcPath.endsWith(".markdown"); - } - PathFilters.markdown = markdown; -})(PathFilters || (PathFilters = {})); -/** - * Caches in-use CSVs to make high-frequency reloads (such as actively looking at a document - * that uses CSV) fast. - */ -class CsvCache extends obsidian.Component { - constructor(vault) { - super(); - this.vault = vault; - this.cache = new Map(); - // Force-flush the cache on CSV file deletions or modifications. - this.registerEvent(this.vault.on("modify", file => { - if (file instanceof obsidian.TFile && PathFilters.csv(file.path)) - this.cache.delete(file.path); - })); - this.registerEvent(this.vault.on("delete", file => { - if (file instanceof obsidian.TFile && PathFilters.csv(file.path)) - this.cache.delete(file.path); - })); - } - /** Load a CSV file from the cache, doing a fresh load if it has not been loaded. */ - async get(path) { - // Clear old entries on every fresh load, since the path being loaded may be stale. - this.clearOldEntries(); - let existing = this.cache.get(path); - if (existing) - return Result.success(existing.data); - else { - let value = await this.loadInternal(path); - if (value.successful) - this.cache.set(path, { data: value.value, loadTime: DateTime.now() }); - return value; - } - } - /** Do the actual raw loading of a CSV path (which is either local or an HTTP request). */ - async loadInternal(path) { - // Allow http://, https://, and file:// prefixes which use AJAX. - if (path.startsWith("http://") || path.startsWith("https://") || path.startsWith("file://")) { - try { - let result = await fetch(path, { - method: "GET", - mode: "no-cors", - redirect: "follow", - }); - return Result.success(parseCsv(await result.text())); - } - catch (ex) { - return Result.failure("" + ex + "\n\n" + ex.stack); - } - } - // Otherwise, assume it is a fully-qualified file path. - try { - let fileData = await this.vault.adapter.read(path); - return Result.success(parseCsv(fileData)); - } - catch (ex) { - return Result.failure(`Failed to load data from path '${path}'.`); - } - } - /** Clear old entries in the cache (as measured by insertion time). */ - clearOldEntries() { - let currentTime = DateTime.now(); - let keysToRemove = new Set(); - for (let [key, value] of this.cache.entries()) { - let entryAge = Math.abs(currentTime.diff(value.loadTime, "seconds").seconds); - if (entryAge > CsvCache.CACHE_EXPIRY_SECONDS) - keysToRemove.add(key); - } - keysToRemove.forEach(key => this.cache.delete(key)); - } -} -CsvCache.CACHE_EXPIRY_SECONDS = 5 * 60; -/** Optional connector to the Obsidian 'Starred' plugin which allows for efficiently querying if a file is starred or not. */ -class StarredCache extends obsidian.Component { - constructor(app, onUpdate) { - super(); - this.app = app; - this.onUpdate = onUpdate; - this.stars = StarredCache.fetch(this.app); - this.registerInterval(window.setInterval(() => this.reload(), StarredCache.REFRESH_INTERVAL)); - const initialHandler = window.setTimeout(() => this.reload(), StarredCache.INITIAL_DELAY); - this.register(() => window.clearTimeout(initialHandler)); - } - /** Determines if the given path is starred. */ - starred(path) { - return this.stars.has(path); - } - reload() { - let newStars = StarredCache.fetch(this.app); - if (!setsEqual(this.stars, newStars)) { - this.stars = newStars; - this.onUpdate(); - } - } - /** Fetch all starred files from the stars plugin, if present. */ - static fetch(app) { - var _a, _b, _c, _d; - let items = (_d = (_c = (_b = (_a = app === null || app === void 0 ? void 0 : app.internalPlugins) === null || _a === void 0 ? void 0 : _a.plugins) === null || _b === void 0 ? void 0 : _b.starred) === null || _c === void 0 ? void 0 : _c.instance) === null || _d === void 0 ? void 0 : _d.items; - if (items == undefined) - return new Set(); - return new Set(items.filter((l) => l.type === "file").map(l => l.path)); - } -} -/** Initial delay before checking the cache; we need to wait for it to asynchronously load the initial stars. */ -StarredCache.INITIAL_DELAY = 4 * 1000; -/** How frequently to check for star updates. */ -StarredCache.REFRESH_INTERVAL = 30 * 1000; -/** A generic index which indexes variables of the form key -> value[], allowing both forward and reverse lookups. */ -class IndexMap { - /** Create a new, empty index map. */ - constructor() { - this.map = new Map(); - this.invMap = new Map(); - } - /** Returns all values for the given key. */ - get(key) { - let result = this.map.get(key); - if (result) { - return new Set(result); - } - else { - return new Set(); - } - } - /** Returns all keys that reference the given key. Mutating the returned set is not allowed. */ - getInverse(value) { - return this.invMap.get(value) || IndexMap.EMPTY_SET; - } - /** Sets the key to the given values; this will delete the old mapping for the key if one was present. */ - set(key, values) { - var _a, _b; - if (!values.size) { - // no need to store if no values - this.delete(key); - return this; - } - let oldValues = this.map.get(key); - if (oldValues) { - for (let value of oldValues) { - // Only delete the ones we're not adding back - if (!values.has(key)) - (_a = this.invMap.get(value)) === null || _a === void 0 ? void 0 : _a.delete(key); - } - } - this.map.set(key, values); - for (let value of values) { - if (!this.invMap.has(value)) - this.invMap.set(value, new Set([key])); - else - (_b = this.invMap.get(value)) === null || _b === void 0 ? void 0 : _b.add(key); - } - return this; - } - /** Clears all values for the given key so they can be re-added. */ - delete(key) { - var _a; - let oldValues = this.map.get(key); - if (!oldValues) - return false; - this.map.delete(key); - for (let value of oldValues) { - (_a = this.invMap.get(value)) === null || _a === void 0 ? void 0 : _a.delete(key); - } - return true; - } - /** Rename all references to the given key to a new value. */ - rename(oldKey, newKey) { - let oldValues = this.map.get(oldKey); - if (!oldValues) - return false; - this.delete(oldKey); - this.set(newKey, oldValues); - return true; - } - /** Clear the entire index. */ - clear() { - this.map.clear(); - this.invMap.clear(); - } -} -IndexMap.EMPTY_SET = Object.freeze(new Set()); -/** Index map wrapper which is case-insensitive in the key. */ -class ValueCaseInsensitiveIndexMap { - /** Create a new, empty case insensitive index map. */ - constructor(delegate = new IndexMap()) { - this.delegate = delegate; - } - /** Returns all values for the given key. */ - get(key) { - return this.delegate.get(key); - } - /** Returns all keys that reference the given value. Mutating the returned set is not allowed. */ - getInverse(value) { - return this.delegate.getInverse(value.toLocaleLowerCase()); - } - /** Sets the key to the given values; this will delete the old mapping for the key if one was present. */ - set(key, values) { - this.delegate.set(key, new Set(Array.from(values).map(v => v.toLocaleLowerCase()))); - return this; - } - /** Clears all values for the given key so they can be re-added. */ - delete(key) { - return this.delegate.delete(key); - } - /** Rename all references to the given key to a new value. */ - rename(oldKey, newKey) { - return this.delegate.rename(oldKey, newKey); - } - /** Clear the entire index. */ - clear() { - this.delegate.clear(); - } -} - -/** Collect data matching a source query. */ -/** Find source paths which match the given source. */ -function matchingSourcePaths(source, index, originFile = "") { - var _a; - switch (source.type) { - case "empty": - return Result.success(new Set()); - case "tag": - return Result.success(index.tags.getInverse(source.tag)); - case "csv": - return Result.success(new Set([index.prefix.resolveRelative(source.path, originFile)])); - case "folder": - // Prefer loading from the folder at the given path. - if (index.prefix.nodeExists(source.folder)) - return Result.success(index.prefix.get(source.folder, PathFilters.markdown)); - // But allow for loading individual files if they exist. - if (index.prefix.pathExists(source.folder)) - return Result.success(new Set([source.folder])); - else if (index.prefix.pathExists(source.folder + ".md")) - return Result.success(new Set([source.folder + ".md"])); - // For backwards-compat, return an empty result even if the folder does not exist. - return Result.success(new Set()); - case "link": - let fullPath = (_a = index.metadataCache.getFirstLinkpathDest(source.file, originFile)) === null || _a === void 0 ? void 0 : _a.path; - if (!fullPath) { - // Look in links which includes unresolved links - return Result.success(index.links.getInverse(source.file)); - } - if (source.direction === "incoming") { - // To find all incoming links (i.e., things that link to this), use the index that Obsidian provides. - // TODO: Use an actual index so this isn't a fullscan. - let resolved = index.metadataCache.resolvedLinks; - let incoming = new Set(); - for (let [key, value] of Object.entries(resolved)) { - if (fullPath in value) - incoming.add(key); - } - return Result.success(incoming); - } - else { - let resolved = index.metadataCache.resolvedLinks; - if (!(fullPath in resolved)) - return Result.failure(`Could not find file "${source.file}" during link lookup - does it exist?`); - return Result.success(new Set(Object.keys(index.metadataCache.resolvedLinks[fullPath]))); - } - case "binaryop": - return Result.flatMap2(matchingSourcePaths(source.left, index, originFile), matchingSourcePaths(source.right, index, originFile), (left, right) => { - if (source.op == "&") { - let result = new Set(); - for (let elem of right) { - if (left.has(elem)) - result.add(elem); - } - return Result.success(result); - } - else if (source.op == "|") { - let result = new Set(left); - for (let elem of right) - result.add(elem); - return Result.success(result); - } - else { - return Result.failure(`Unrecognized operator '${source.op}'.`); - } - }); - case "negate": - return matchingSourcePaths(source.child, index, originFile).map(child => { - // TODO: This is obviously very inefficient. Can be improved by complicating the - // return type of this function & optimizing 'and' / 'or'. - let allFiles = new Set(index.vault.getMarkdownFiles().map(f => f.path)); - child.forEach(f => allFiles.delete(f)); - return allFiles; - }); - } -} -/** Convert a path to the data for that path; usually markdown pages, but could also be other file types (like CSV). */ -async function resolvePathData(path, index) { - if (PathFilters.csv(path)) - return resolveCsvData(path, index); - else - return resolveMarkdownData(path, index); -} -// TODO: We shouldn't be doing path normalization here relative to an origin file, -/** Convert a CSV path to the data in the CSV (in dataview format). */ -async function resolveCsvData(path, index) { - let rawData = await index.csv.get(path); - return rawData.map(rows => { - return rows.map((row, index) => { - return { - id: `${path}#${index}`, - data: row, - }; - }); - }); -} -/** Convert a path pointing to a markdown page, into the associated metadata. */ -function resolveMarkdownData(path, index) { - let page = index.pages.get(path); - if (!page) - return Result.success([]); - return Result.success([ - { - id: Link.file(path), - data: page.serialize(index), - }, - ]); -} -/** Resolve a source to the collection of data rows that it matches. */ -async function resolveSource(source, index, originFile = "") { - let paths = matchingSourcePaths(source, index, originFile); - if (!paths.successful) - return Result.failure(paths.error); - let result = []; - for (let path of paths.value) { - let resolved = await resolvePathData(path, index); - if (!resolved.successful) - return resolved; - for (let val of resolved.value) - result.push(val); - } - return Result.success(result); -} - -/** Default function implementations for the expression evaluator. */ -/** - * Allows for the creation of functions that check the number and type of their arguments, and dispatch - * to different implemenations based on the types of the inputs. - */ -class FunctionBuilder { - constructor(name) { - this.name = name; - this.variants = []; - this.vectorized = {}; - } - /** Add a general function variant which accepts any number of arguments of any type. */ - vararg(impl) { - this.variants.push({ args: [], varargs: true, impl }); - return this; - } - /** Add a function variant which takes in a single argument. */ - add1(argType, impl) { - this.variants.push({ - args: [argType], - varargs: false, - impl: (c, ...rest) => impl(rest[0], c), - }); - return this; - } - /** Add a function variant which takes in two typed arguments. */ - add2(arg1, arg2, impl) { - this.variants.push({ - args: [arg1, arg2], - varargs: false, - impl: (c, ...rest) => impl(rest[0], rest[1], c), - }); - return this; - } - /** Add a function variant which takes in three typed arguments. */ - add3(arg1, arg2, arg3, impl) { - this.variants.push({ - args: [arg1, arg2, arg3], - varargs: false, - impl: (c, ...rest) => impl(rest[0], rest[1], rest[2], c), - }); - return this; - } - /** Add vectorized variants which accept the given number of arguments and delegate. */ - vectorize(numArgs, positions) { - this.vectorized[numArgs] = positions; - return this; - } - /** Return a function which checks the number and type of arguments, passing them on to the first matching variant. */ - build() { - let self = (context, ...args) => { - let types = []; - for (let arg of args) { - let argType = Values.typeOf(arg); - if (!argType) - throw Error(`Unrecognized argument type for argument '${arg}'`); - types.push(argType); - } - // Handle vectorization, possibly in multiple fields. - if (this.vectorized[types.length]) { - let vectorizedPositions = this.vectorized[types.length].filter(k => types[k] == "array"); - if (vectorizedPositions.length > 0) { - let minLength = vectorizedPositions - .map(p => args[p].length) - .reduce((p, c) => Math.min(p, c)); - // Call the subfunction for each element in the longest array. - // If you call a vectorized function with different-length arrays, - // the output is limited by the length of the shortest array. - let result = []; - for (let vpos = 0; vpos < minLength; vpos++) { - let subargs = []; - for (let index = 0; index < args.length; index++) { - if (vectorizedPositions.includes(index)) { - let arr = args[index]; - subargs.push(arr[vpos]); - } - else { - subargs.push(args[index]); - } - } - result.push(self(context, ...subargs)); - } - return result; - } - } - outer: for (let variant of this.variants) { - if (variant.varargs) - return variant.impl(context, ...args); - if (variant.args.length != types.length) - continue; - for (let index = 0; index < variant.args.length; index++) { - if (variant.args[index] != "*" && variant.args[index] != types[index]) - continue outer; - } - return variant.impl(context, ...args); - } - throw Error(`No implementation of '${this.name}' found for arguments: ${types.join(", ")}`); - }; - return self; - } -} -/** Utilities for managing function implementations. */ -var Functions; -(function (Functions) { - /** Bind a context to a function implementation, yielding a function which does not need the context argument. */ - function bind(func, context) { - return (...args) => func(context, ...args); - } - Functions.bind = bind; - /** Bind a context to all functions in the given map, yielding a new map of bound functions. */ - function bindAll(funcs, context) { - let result = {}; - for (let [key, func] of Object.entries(funcs)) { - result[key] = Functions.bind(func, context); - } - return result; - } - Functions.bindAll = bindAll; -})(Functions || (Functions = {})); -/** - * Collection of all defined functions; defined here so that they can be called from within dataview, - * and test code. - */ -var DefaultFunctions; -(function (DefaultFunctions) { - DefaultFunctions.typeOf = new FunctionBuilder("type") - .add1("array", _ => "array") - .add1("boolean", _ => "boolean") - .add1("date", _ => "date") - .add1("duration", _ => "duration") - .add1("function", _ => "function") - .add1("widget", _ => "widget") - .add1("link", _ => "link") - .add1("null", _ => "null") - .add1("number", _ => "number") - .add1("object", _ => "object") - .add1("string", _ => "string") - .add1("*", _ => "unknown") - .build(); - /** Compute the length of a data type. */ - DefaultFunctions.length = new FunctionBuilder("length") - .add1("array", a => a.length) - .add1("object", a => Object.keys(a).length) - .add1("string", a => a.length) - .add1("null", _a => 0) - .build(); - /** List constructor function. */ - DefaultFunctions.list = (_context, ...args) => args; - /** Object constructor function. */ - DefaultFunctions.object = (_context, ...args) => { - if (args.length % 2 != 0) - throw Error("object() requires an even number of arguments"); - let result = {}; - for (let index = 0; index < args.length; index += 2) { - let key = args[index]; - if (!Values.isString(key)) - throw Error("keys should be of type string for object(key1, value1, ...)"); - result[key] = args[index + 1]; - } - return result; - }; - /** Internal link constructor function. */ - DefaultFunctions.link = new FunctionBuilder("link") - .add1("string", (a, c) => Link.file(c.linkHandler.normalize(a), false)) - .add1("link", a => a) - .add1("null", _a => null) - .vectorize(1, [0]) - .add2("string", "string", (t, d, c) => Link.file(c.linkHandler.normalize(t), false, d)) - .add3("string", "string", "boolean", (t, d, e, c) => Link.file(c.linkHandler.normalize(t), e, d)) - .add2("link", "string", (t, d) => t.withDisplay(d)) - .add2("null", "*", () => null) - .add2("*", "null", (t, _n, c) => DefaultFunctions.link(c, t)) - .vectorize(2, [0, 1]) - .build(); - /** Embed and un-embed a link. */ - DefaultFunctions.embed = new FunctionBuilder("embed") - .add1("link", l => l.toEmbed()) - .vectorize(1, [0]) - .add2("link", "boolean", (l, e, c) => (e ? l.toEmbed() : l.fromEmbed())) - .add1("null", () => null) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .vectorize(2, [0, 1]) - .build(); - /** External link constructor function. */ - DefaultFunctions.elink = new FunctionBuilder("elink") - .add2("string", "string", (a, d) => Widgets.externalLink(a, d)) - .add2("string", "null", (s, _n, c) => DefaultFunctions.elink(c, s, s)) - .add2("null", "*", () => null) - .vectorize(2, [0]) - .add1("string", (a, c) => DefaultFunctions.elink(c, a, a)) - .add1("null", () => null) - .vectorize(1, [0]) - .build(); - /** Date constructor function. */ - DefaultFunctions.date = new FunctionBuilder("date") - .add1("string", str => { - let parsedDate = EXPRESSION.datePlus.parse(str); - if (parsedDate.status) - return parsedDate.value; - else - return null; - }) - .add1("date", d => d) - .add1("link", (link, c) => { - var _c, _d; - // Try to parse from the display... - if (link.display) { - let parsedDate = EXPRESSION.date.parse(link.display); - if (parsedDate.status) - return parsedDate.value; - } - // Then try to parse from the path... - let parsedDate = EXPRESSION.date.parse(link.path); - if (parsedDate.status) - return parsedDate.value; - // Then pull it from the file. - let resolved = c.linkHandler.resolve(link.path); - if (resolved && ((_c = resolved === null || resolved === void 0 ? void 0 : resolved.file) === null || _c === void 0 ? void 0 : _c.day)) { - return (_d = resolved === null || resolved === void 0 ? void 0 : resolved.file) === null || _d === void 0 ? void 0 : _d.day; - } - return null; - }) - .add2("string", "string", (d, f) => { - if (f === "x" || f === "X") { - let match = NUMBER_REGEX.exec(d); - if (match) - return DateTime.fromMillis(Number.parseInt(match[0]) * (f === "X" ? 1000 : 1)); - else { - throw Error("Not a number for format( (${ f }): ${ d }"); - } - } - else { - let parsedDate = DateTime.fromFormat(d, f); - if (parsedDate.isValid) - return parsedDate; - else { - throw Error(`Can't handle format (${f}) on date string (${d})`); - } - } - }) - .add1("null", () => null) - .vectorize(1, [0]) - .build(); - /** Duration constructor function. */ - DefaultFunctions.dur = new FunctionBuilder("dur") - .add1("string", str => { - let parsedDur = EXPRESSION.duration.parse(str.trim()); - if (parsedDur.status) - return parsedDur.value; - else - return null; - }) - .add1("duration", d => d) - .add1("null", d => d) - .vectorize(1, [0]) - .build(); - /** Format a date using a luxon/moment-style date format. */ - DefaultFunctions.dateformat = new FunctionBuilder("dateformat") - .add2("date", "string", (date, format) => date.toFormat(format, { locale: currentLocale() })) - .add2("null", "string", (_nul, _format) => null) - .vectorize(2, [0]) - .build(); - DefaultFunctions.localtime = new FunctionBuilder("localtime") - .add1("date", d => d.toLocal()) - .add1("null", () => null) - .vectorize(1, [0]) - .build(); - const NUMBER_REGEX = /-?[0-9]+(\.[0-9]+)?/; - /** Number constructor function. */ - DefaultFunctions.number = new FunctionBuilder("number") - .add1("number", a => a) - .add1("string", str => { - let match = NUMBER_REGEX.exec(str); - if (match) - return Number.parseFloat(match[0]); - else - return null; - }) - .add1("null", () => null) - .vectorize(1, [0]) - .build(); - /** - * Convert any value to a reasonable internal string representation. Most useful for dates, strings, numbers, and - * so on. - */ - DefaultFunctions.string = new FunctionBuilder("string").add1("*", (a, ctx) => Values.toString(a, ctx.settings)).build(); - DefaultFunctions.round = new FunctionBuilder("round") - .add1("number", n => Math.round(n)) - .add1("null", () => null) - .vectorize(1, [0]) - .add2("number", "number", (n, p) => { - if (p <= 0) - return Math.round(n); - return parseFloat(n.toFixed(p)); - }) - .add2("number", "null", n => Math.round(n)) - .add2("null", "*", () => null) - .vectorize(2, [0]) - .build(); - DefaultFunctions.min = new FunctionBuilder("min") - .add2("*", "null", (a, _n) => a) - .add2("null", "*", (_n, a) => a) - .add2("*", "*", (a, b, ctx) => (Values.compareValue(a, b, ctx.linkHandler.normalize) <= 0 ? a : b)) - .add1("array", (a, ctx) => DefaultFunctions.min(ctx, ...a)) - .vararg((ctx, ...args) => (args.length == 0 ? null : args.reduce((p, c) => DefaultFunctions.min(ctx, p, c)))) - .build(); - DefaultFunctions.max = new FunctionBuilder("max") - .add2("*", "null", (a, _n) => a) - .add2("null", "*", (_n, a) => a) - .add2("*", "*", (a, b, ctx) => (Values.compareValue(a, b, ctx.linkHandler.normalize) > 0 ? a : b)) - .add1("array", (a, ctx) => DefaultFunctions.max(ctx, ...a)) - .vararg((ctx, ...args) => (args.length == 0 ? null : args.reduce((p, c) => DefaultFunctions.max(ctx, p, c)))) - .build(); - DefaultFunctions.minby = new FunctionBuilder("minby") - .add2("array", "function", (arr, func, ctx) => { - if (arr.length == 0) - return null; - let values = arr.map(v => { - return { value: v, mapped: func(ctx, v) }; - }); - let filtered = values.filter(v => !Values.isNull(v.mapped)); - if (filtered.length == 0) - return arr[0]; - return filtered.reduce((p, c) => { - if (Values.compareValue(p.mapped, c.mapped, ctx.linkHandler.normalize) <= 0) - return p; - else - return c; - }).value; - }) - .add2("null", "function", (_arr, _func, _ctx) => null) - .build(); - DefaultFunctions.maxby = new FunctionBuilder("maxby") - .add2("array", "function", (arr, func, ctx) => { - if (arr.length == 0) - return null; - let values = arr.map(v => { - return { value: v, mapped: func(ctx, v) }; - }); - let filtered = values.filter(v => !Values.isNull(v.mapped)); - if (filtered.length == 0) - return arr[0]; - return filtered.reduce((p, c) => { - if (Values.compareValue(p.mapped, c.mapped, ctx.linkHandler.normalize) > 0) - return p; - else - return c; - }).value; - }) - .add2("null", "function", (_arr, _func, _ctx) => null) - .build(); - DefaultFunctions.striptime = new FunctionBuilder("striptime") - .add1("date", d => DateTime.fromObject({ year: d.year, month: d.month, day: d.day })) - .add1("null", _n => null) - .vectorize(1, [0]) - .build(); - // Default contains, which looks through data structures recursively. - DefaultFunctions.contains = new FunctionBuilder("contains") - .add2("array", "*", (l, elem, context) => l.some(e => DefaultFunctions.contains(context, e, elem))) - .add2("string", "string", (haystack, needle) => haystack.includes(needle)) - .add2("object", "string", (obj, key) => key in obj) - .add2("*", "*", (elem1, elem2, context) => context.evaluate(Fields.binaryOp(Fields.literal(elem1), "=", Fields.literal(elem2))).orElseThrow()) - .vectorize(2, [1]) - .build(); - // Case insensitive version of contains. - DefaultFunctions.icontains = new FunctionBuilder("icontains") - .add2("array", "*", (l, elem, context) => l.some(e => DefaultFunctions.icontains(context, e, elem))) - .add2("string", "string", (haystack, needle) => haystack.toLocaleLowerCase().includes(needle.toLocaleLowerCase())) - .add2("object", "string", (obj, key) => key in obj) - .add2("*", "*", (elem1, elem2, context) => context.evaluate(Fields.binaryOp(Fields.literal(elem1), "=", Fields.literal(elem2))).orElseThrow()) - .vectorize(2, [1]) - .build(); - // "exact" contains, does not look recursively. - DefaultFunctions.econtains = new FunctionBuilder("econtains") - .add2("array", "*", (l, elem, context) => l.some(e => context.evaluate(Fields.binaryOp(Fields.literal(elem), "=", Fields.literal(e))).orElseThrow())) - .add2("string", "string", (haystack, needle) => haystack.includes(needle)) - .add2("object", "string", (obj, key) => key in obj) - .add2("*", "*", (elem1, elem2, context) => context.evaluate(Fields.binaryOp(Fields.literal(elem1), "=", Fields.literal(elem2))).orElseThrow()) - .vectorize(2, [1]) - .build(); - // Case insensitive contains which looks for exact word matches (i.e., boundry-to-boundry match). - DefaultFunctions.containsword = new FunctionBuilder("containsword") - .add2("string", "string", (hay, needle) => !!hay.match(new RegExp(".*\\b" + escapeRegex(needle) + "\\b.*", "i"))) - .add2("null", "*", (_a, _b) => null) - .add2("*", "null", (_a, _b) => null) - .vectorize(2, [0, 1]) - .build(); - /** Extract 0 or more keys from a given object via indexing. */ - DefaultFunctions.extract = (context, ...args) => { - if (args.length == 0) - return "extract(object, key1, ...) requires at least 1 argument"; - // Manually handle vectorization in the first argument. - let object = args[0]; - if (Values.isArray(object)) - return object.map(v => DefaultFunctions.extract(context, v, ...args.slice(1))); - let result = {}; - for (let index = 1; index < args.length; index++) { - let key = args[index]; - if (!Values.isString(key)) - throw Error("extract(object, key1, ...) must be called with string keys"); - result[key] = context.evaluate(Fields.index(Fields.literal(object), Fields.literal(key))).orElseThrow(); - } - return result; - }; - // Reverse an array or string. - DefaultFunctions.reverse = new FunctionBuilder("reverse") - .add1("array", l => { - let result = []; - for (let index = l.length - 1; index >= 0; index--) - result.push(l[index]); - return result; - }) - .add1("string", l => { - let result = ""; - for (let c = 0; c < l.length; c++) - result += l[l.length - c - 1]; - return result; - }) - .add1("*", e => e) - .build(); - // Sort an array; if given two arguments, sorts by the key returned. - DefaultFunctions.sort = new FunctionBuilder("sort") - .add1("array", (list, context) => DefaultFunctions.sort(context, list, (_ctx, a) => a)) - .add2("array", "function", (list, key, context) => { - let result = [].concat(list); - result.sort((a, b) => { - let akey = key(context, a); - let bkey = key(context, b); - let le = context - .evaluate(Fields.binaryOp(Fields.literal(akey), "<", Fields.literal(bkey))) - .orElseThrow(); - if (Values.isTruthy(le)) - return -1; - let eq = context - .evaluate(Fields.binaryOp(Fields.literal(akey), "=", Fields.literal(bkey))) - .orElseThrow(); - if (Values.isTruthy(eq)) - return 0; - return 1; - }); - return result; - }) - .add1("*", e => e) - .build(); - DefaultFunctions.regextest = new FunctionBuilder("regextest") - .add2("string", "string", (pattern, field) => RegExp(pattern).test(field)) - .add2("null", "*", (_n, _a) => false) - .add2("*", "null", (_a, _n) => false) - .vectorize(2, [0, 1]) - .build(); - DefaultFunctions.regexmatch = new FunctionBuilder("regexmatch") - .add2("string", "string", (pattern, field) => { - if (!pattern.startsWith("^") && !pattern.endsWith("$")) - pattern = "^" + pattern + "$"; - return !!field.match(pattern); - }) - .add2("null", "*", (_n, _a) => false) - .add2("*", "null", (_a, _n) => false) - .vectorize(2, [0, 1]) - .build(); - DefaultFunctions.regexreplace = new FunctionBuilder("regexreplace") - .add3("string", "string", "string", (field, pat, rep) => { - try { - let reg = new RegExp(pat, "g"); - return field.replace(reg, rep); - } - catch (ex) { - throw Error(`Invalid regexp '${pat}' in regexreplace`); - } - }) - .add3("null", "*", "*", () => null) - .add3("*", "null", "*", () => null) - .add3("*", "*", "null", () => null) - .vectorize(3, [0, 1, 2]) - .build(); - DefaultFunctions.lower = new FunctionBuilder("lower") - .add1("string", s => s.toLocaleLowerCase()) - .add1("null", () => null) - .vectorize(1, [0]) - .build(); - DefaultFunctions.upper = new FunctionBuilder("upper") - .add1("string", s => s.toLocaleUpperCase()) - .add1("null", () => null) - .vectorize(1, [0]) - .build(); - DefaultFunctions.replace = new FunctionBuilder("replace") - .add3("string", "string", "string", (str, pat, repr) => str.split(pat).join(repr)) - .add3("null", "*", "*", () => null) - .add3("*", "null", "*", () => null) - .add3("*", "*", "null", () => null) - .vectorize(3, [0, 1, 2]) - .build(); - // Ensure undefined matches turn into empty strings for split/2 and split/3. - const splitImpl = (str, delim, limit) => str.split(new RegExp(delim), limit).map(str => str || ""); - /** Split a string on a given string. */ - DefaultFunctions.split = new FunctionBuilder("split") - .add2("string", "string", (string, splitter) => splitImpl(string, splitter)) - .add3("string", "string", "number", (string, splitter, limit) => splitImpl(string, splitter, limit)) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .add3("*", "*", "null", () => null) - .add3("*", "null", "*", () => null) - .add3("null", "*", "*", () => null) - .build(); - DefaultFunctions.startswith = new FunctionBuilder("startswith") - .add2("string", "string", (str, starting) => str.startsWith(starting)) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .vectorize(2, [0, 1]) - .build(); - DefaultFunctions.endswith = new FunctionBuilder("endswith") - .add2("string", "string", (str, ending) => str.endsWith(ending)) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .vectorize(2, [0, 1]) - .build(); - DefaultFunctions.padleft = new FunctionBuilder("padleft") - .add2("string", "number", (str, len) => str.padStart(len, " ")) - .add3("string", "number", "string", (str, len, padding) => str.padStart(len, padding)) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .add3("null", "*", "*", () => null) - .add3("*", "null", "*", () => null) - .add3("*", "*", "null", () => null) - .vectorize(2, [0, 1]) - .vectorize(3, [0, 1, 2]) - .build(); - DefaultFunctions.padright = new FunctionBuilder("padright") - .add2("string", "number", (str, len) => str.padEnd(len, " ")) - .add3("string", "number", "string", (str, len, padding) => str.padEnd(len, padding)) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .add3("null", "*", "*", () => null) - .add3("*", "null", "*", () => null) - .add3("*", "*", "null", () => null) - .vectorize(2, [0, 1]) - .vectorize(3, [0, 1, 2]) - .build(); - DefaultFunctions.substring = new FunctionBuilder("substring") - .add2("string", "number", (str, start) => str.substring(start)) - .add3("string", "number", "number", (str, start, end) => str.substring(start, end)) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .add3("null", "*", "*", () => null) - .add3("*", "null", "*", () => null) - .add3("*", "*", "null", () => null) - .vectorize(2, [0, 1]) - .vectorize(3, [0, 1, 2]) - .build(); - DefaultFunctions.truncate = new FunctionBuilder("truncate") - .add3("string", "number", "string", (str, length, suffix) => { - if (str.length > length - suffix.length) { - return str.substring(0, Math.max(0, length - suffix.length)) + suffix; - } - else { - return str; - } - }) - .add2("string", "number", (str, length, ctx) => DefaultFunctions.truncate(ctx, str, length, "...")) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .add3("null", "*", "*", () => null) - .add3("*", "null", "*", () => null) - .add3("*", "*", "null", () => null) - .vectorize(2, [0, 1]) - .vectorize(3, [0, 1, 2]) - .build(); - DefaultFunctions.fdefault = new FunctionBuilder("default") - .add2("*", "*", (v, bk) => (Values.isNull(v) ? bk : v)) - .vectorize(2, [0, 1]) - .build(); - DefaultFunctions.ldefault = new FunctionBuilder("ldefault") - .add2("*", "*", (v, bk) => (Values.isNull(v) ? bk : v)) - .build(); - DefaultFunctions.choice = new FunctionBuilder("choice") - .add3("*", "*", "*", (b, left, right) => (Values.isTruthy(b) ? left : right)) - .vectorize(3, [0]) - .build(); - DefaultFunctions.reduce = new FunctionBuilder("reduce") - .add2("array", "string", (lis, op, context) => { - if (lis.length == 0) - return null; - if (op != "+" && op != "-" && op != "*" && op != "/" && op != "&" && op != "|") - throw Error("reduce(array, op) supports '+', '-', '/', '*', '&', and '|'"); - let value = lis[0]; - for (let index = 1; index < lis.length; index++) { - value = context - .evaluate(Fields.binaryOp(Fields.literal(value), op, Fields.literal(lis[index]))) - .orElseThrow(); - } - return value; - }) - .add2("array", "function", (lis, op, context) => { - if (lis.length == 0) - return null; - let value = lis[0]; - for (let index = 1; index < lis.length; index++) { - // Skip null values to reduce the pain of summing over fields that may or may not exist. - if (Values.isNull(lis[index])) - continue; - value = op(context, value, lis[index]); - } - return value; - }) - .add2("null", "*", () => null) - .add2("*", "null", () => null) - .vectorize(2, [1]) - .build(); - DefaultFunctions.sum = new FunctionBuilder("sum") - .add1("array", (arr, c) => DefaultFunctions.reduce(c, arr, "+")) - .add1("*", e => e) - .build(); - DefaultFunctions.average = new FunctionBuilder("average") - .add1("array", (array, context) => { - if (array.length == 0) - return null; - const add = DefaultFunctions.sum(context, array); - if (add == null || add == undefined) - return null; - return context - .evaluate(Fields.binaryOp(Fields.literal(add), "/", Fields.literal(array.length))) - .orElseThrow(); - }) - .add1("*", e => e) - .build(); - DefaultFunctions.product = new FunctionBuilder("product") - .add1("array", (arr, c) => DefaultFunctions.reduce(c, arr, "*")) - .add1("*", e => e) - .build(); - DefaultFunctions.join = new FunctionBuilder("join") - .add2("array", "string", (arr, sep, ctx) => arr.map(e => Values.toString(e, ctx.settings)).join(sep)) - .add2("array", "null", (arr, _s, context) => DefaultFunctions.join(context, arr, ", ")) - .add2("*", "string", (elem, sep, ctx) => Values.toString(elem, ctx.settings)) - .add1("array", (arr, context) => DefaultFunctions.join(context, arr, ", ")) - .add1("*", (e, ctx) => Values.toString(e, ctx.settings)) - .vectorize(2, [1]) - .build(); - DefaultFunctions.any = new FunctionBuilder("any") - .add1("array", arr => arr.some(v => Values.isTruthy(v))) - .add2("array", "function", (arr, f, ctx) => arr.some(v => Values.isTruthy(f(ctx, v)))) - .vararg((_ctx, ...args) => args.some(v => Values.isTruthy(v))) - .build(); - DefaultFunctions.all = new FunctionBuilder("all") - .add1("array", arr => arr.every(v => Values.isTruthy(v))) - .add2("array", "function", (arr, f, ctx) => arr.every(v => Values.isTruthy(f(ctx, v)))) - .vararg((_ctx, ...args) => args.every(v => Values.isTruthy(v))) - .build(); - DefaultFunctions.none = new FunctionBuilder("all") - .add1("array", arr => !arr.some(v => Values.isTruthy(v))) - .add2("array", "function", (arr, f, ctx) => !arr.some(v => Values.isTruthy(f(ctx, v)))) - .vararg((_ctx, ...args) => !args.some(v => Values.isTruthy(v))) - .build(); - DefaultFunctions.filter = new FunctionBuilder("filter") - .add2("array", "function", (arr, f, ctx) => arr.filter(v => Values.isTruthy(f(ctx, v)))) - .add2("null", "*", () => null) - .build(); - DefaultFunctions.map = new FunctionBuilder("map") - .add2("array", "function", (arr, f, ctx) => arr.map(v => f(ctx, v))) - .add2("null", "*", () => null) - .build(); - DefaultFunctions.nonnull = new FunctionBuilder("nonnull") - .add1("array", arr => arr.filter(v => Values.typeOf(v) != "null")) - .vararg((_ctx, ...args) => args.filter(v => Values.typeOf(v) != "null")) - .build(); - /** Gets an object containing a link's own properties */ - DefaultFunctions.meta = new FunctionBuilder("meta") - .add1("link", link => { - var _c, _d; - return ({ - display: (_c = link.display) !== null && _c !== void 0 ? _c : null, - embed: link.embed, - path: link.path, - subpath: (_d = link.subpath) !== null && _d !== void 0 ? _d : null, - type: link.type, - }); - }) - .build(); - // Concatenates sub-array elements into a new array - DefaultFunctions.flat = new FunctionBuilder("flat") - .add1("array", a => { - return a.flat(); - }) - .add2("array", "number", (a, n) => { - // @ts-ignore - return a.flat(n); - }) - .add1("null", () => null) - .build(); -})(DefaultFunctions || (DefaultFunctions = {})); -/** Default function implementations for the expression evaluator. */ -const DEFAULT_FUNCTIONS = { - // Constructors. - list: DefaultFunctions.list, - array: DefaultFunctions.list, - link: DefaultFunctions.link, - embed: DefaultFunctions.embed, - elink: DefaultFunctions.elink, - date: DefaultFunctions.date, - dur: DefaultFunctions.dur, - dateformat: DefaultFunctions.dateformat, - localtime: DefaultFunctions.localtime, - number: DefaultFunctions.number, - string: DefaultFunctions.string, - object: DefaultFunctions.object, - typeof: DefaultFunctions.typeOf, - // Math Operations. - round: DefaultFunctions.round, - min: DefaultFunctions.min, - max: DefaultFunctions.max, - minby: DefaultFunctions.minby, - maxby: DefaultFunctions.maxby, - // String operations. - regexreplace: DefaultFunctions.regexreplace, - regextest: DefaultFunctions.regextest, - regexmatch: DefaultFunctions.regexmatch, - replace: DefaultFunctions.replace, - lower: DefaultFunctions.lower, - upper: DefaultFunctions.upper, - split: DefaultFunctions.split, - startswith: DefaultFunctions.startswith, - endswith: DefaultFunctions.endswith, - padleft: DefaultFunctions.padleft, - padright: DefaultFunctions.padright, - substring: DefaultFunctions.substring, - truncate: DefaultFunctions.truncate, - // Date Operations. - striptime: DefaultFunctions.striptime, - // List operations. - length: DefaultFunctions.length, - contains: DefaultFunctions.contains, - icontains: DefaultFunctions.icontains, - econtains: DefaultFunctions.econtains, - containsword: DefaultFunctions.containsword, - reverse: DefaultFunctions.reverse, - sort: DefaultFunctions.sort, - flat: DefaultFunctions.flat, - // Aggregation operations like reduce. - reduce: DefaultFunctions.reduce, - join: DefaultFunctions.join, - sum: DefaultFunctions.sum, - product: DefaultFunctions.product, - average: DefaultFunctions.average, - all: DefaultFunctions.all, - any: DefaultFunctions.any, - none: DefaultFunctions.none, - filter: DefaultFunctions.filter, - map: DefaultFunctions.map, - nonnull: DefaultFunctions.nonnull, - // Object/Utility operations. - extract: DefaultFunctions.extract, - default: DefaultFunctions.fdefault, - ldefault: DefaultFunctions.ldefault, - choice: DefaultFunctions.choice, - meta: DefaultFunctions.meta, -}; - -/** Provides a global dispatch table for evaluating binary operators, including comparison. */ -/** Provides implementations for binary operators on two types using a registry. */ -class BinaryOpHandler { - constructor() { - this.map = new Map(); - } - static create() { - return new BinaryOpHandler(); - } - register(left, op, right, func) { - this.map.set(BinaryOpHandler.repr(op, left, right), func); - return this; - } - registerComm(left, op, right, func) { - return this.register(left, op, right, func).register(right, op, left, (a, b, ctx) => func(b, a, ctx)); - } - /** Implement a comparison function. */ - compare(type, compare) { - return this.register(type, "<", type, (a, b, ctx) => compare(a, b, ctx) < 0) - .register(type, "<=", type, (a, b, ctx) => compare(a, b, ctx) <= 0) - .register(type, ">", type, (a, b, ctx) => compare(a, b, ctx) > 0) - .register(type, ">=", type, (a, b, ctx) => compare(a, b, ctx) >= 0) - .register(type, "=", type, (a, b, ctx) => compare(a, b, ctx) == 0) - .register(type, "!=", type, (a, b, ctx) => compare(a, b, ctx) != 0); - } - /** Attempt to evaluate the given binary operator on the two literal fields. */ - evaluate(op, left, right, ctx) { - let leftType = Values.typeOf(left); - let rightType = Values.typeOf(right); - if (!leftType) - return Result.failure(`Unrecognized value '${left}'`); - else if (!rightType) - return Result.failure(`Unrecognized value '${right}'`); - let handler = this.map.get(BinaryOpHandler.repr(op, leftType, rightType)); - if (handler) - return Result.success(handler(left, right, ctx)); - // Right-'*' fallback: - let handler2 = this.map.get(BinaryOpHandler.repr(op, leftType, "*")); - if (handler2) - return Result.success(handler2(left, right, ctx)); - // Left-'*' fallback: - let handler3 = this.map.get(BinaryOpHandler.repr(op, "*", rightType)); - if (handler3) - return Result.success(handler3(left, right, ctx)); - // Double '*' fallback. - let handler4 = this.map.get(BinaryOpHandler.repr(op, "*", "*")); - if (handler4) - return Result.success(handler4(left, right, ctx)); - return Result.failure(`No implementation found for '${leftType} ${op} ${rightType}'`); - } - /** Create a string representation of the given triplet for unique lookup in the map. */ - static repr(op, left, right) { - return `${left},${op},${right}`; - } -} -/** Configure and create a binary OP handler with the given parameters. */ -function createBinaryOps(linkNormalizer) { - return (BinaryOpHandler.create() - // TODO: Consider not using a universal comparison function. - .compare("*", (a, b) => Values.compareValue(a, b, linkNormalizer)) - // Global boolean operations. - .register("*", "&", "*", (a, b) => Values.isTruthy(a) && Values.isTruthy(b)) - .register("*", "|", "*", (a, b) => Values.isTruthy(a) || Values.isTruthy(b)) - // Number implementations. - .register("number", "+", "number", (a, b) => a + b) - .register("number", "-", "number", (a, b) => a - b) - .register("number", "*", "number", (a, b) => a * b) - .register("number", "/", "number", (a, b) => a / b) - .register("number", "%", "number", (a, b) => a % b) - // String implementations. - .register("string", "+", "*", (a, b, ctx) => a + Values.toString(b, ctx.settings)) - .register("*", "+", "string", (a, b, ctx) => Values.toString(a, ctx.settings) + b) - .registerComm("string", "*", "number", (a, b) => (b < 0 ? "" : a.repeat(b))) - // Date Operations. - .register("date", "-", "date", (a, b) => { - return normalizeDuration(a.diff(b, ["years", "months", "days", "hours", "minutes", "seconds", "milliseconds"])); - }) - .register("date", "-", "duration", (a, b) => a.minus(b)) - .registerComm("date", "+", "duration", (a, b) => a.plus(b)) - // Duration Operations. - .register("duration", "+", "duration", (a, b) => normalizeDuration(a.plus(b))) - .register("duration", "-", "duration", (a, b) => normalizeDuration(a.minus(b))) - .register("duration", "/", "number", (a, b) => normalizeDuration(a.mapUnits(x => x / b))) - .registerComm("duration", "*", "number", (a, b) => normalizeDuration(a.mapUnits(x => x * b))) - // Array operations. - .register("array", "+", "array", (a, b) => [].concat(a).concat(b)) - // Object operations. - .register("object", "+", "object", (a, b) => Object.assign({}, a, b)) - // Null handling operators. - .register("null", "+", "null", (_a, _b) => null) - .register("null", "-", "null", (_a, _b) => null) - .register("null", "*", "null", (_a, _b) => null) - .register("null", "/", "null", (_a, _b) => null) - .register("null", "%", "null", (_a, _b) => null) - .register("date", "+", "null", (_a, _b) => null) - .register("null", "+", "date", (_a, _b) => null) - .register("date", "-", "null", (_a, _b) => null) - .register("null", "-", "date", (_a, _b) => null)); -} - -/** Core implementation of the query language evaluation engine. */ -/** - * Evaluation context that expressions can be evaluated in. Includes global state, as well as available functions and a handler - * for binary operators. - */ -class Context { - /** - * Create a new context with the given namespace of globals, as well as optionally with custom binary operator, function, - * and link handlers. - */ - constructor(linkHandler, settings, globals = {}, binaryOps = createBinaryOps(linkHandler.normalize), functions = DEFAULT_FUNCTIONS) { - this.linkHandler = linkHandler; - this.settings = settings; - this.globals = globals; - this.binaryOps = binaryOps; - this.functions = functions; - } - /** Set a global value in this context. */ - set(name, value) { - this.globals[name] = value; - return this; - } - /** Get the value of a global variable by name. Returns null if not present. */ - get(name) { - var _a; - return (_a = this.globals[name]) !== null && _a !== void 0 ? _a : null; - } - /** Try to evaluate an arbitrary field in this context, raising an exception on failure. */ - tryEvaluate(field, data = {}) { - return this.evaluate(field, data).orElseThrow(); - } - /** Evaluate an arbitrary field in this context. */ - evaluate(field, data = {}) { - var _a, _b; - switch (field.type) { - case "literal": - return Result.success(field.value); - case "variable": - if (field.name in data) - return Result.success(data[field.name]); - else if (field.name in this.globals) - return Result.success(this.globals[field.name]); - else - return Result.success(null); - case "negated": - return this.evaluate(field.child, data).map(s => !Values.isTruthy(s)); - case "binaryop": - return Result.flatMap2(this.evaluate(field.left, data), this.evaluate(field.right, data), (a, b) => this.binaryOps.evaluate(field.op, a, b, this)); - case "list": - let result = []; - for (let child of field.values) { - let subeval = this.evaluate(child, data); - if (!subeval.successful) - return subeval; - result.push(subeval.value); - } - return Result.success(result); - case "object": - let objResult = {}; - for (let [key, child] of Object.entries(field.values)) { - let subeval = this.evaluate(child, data); - if (!subeval.successful) - return subeval; - objResult[key] = subeval.value; - } - return Result.success(objResult); - case "lambda": - // Just relying on JS to capture 'data' for us implicitly; unsure - // if this is correct thing to do. Could cause weird behaviors. - return Result.success((ctx, ...args) => { - let copy = Object.assign({}, data); - for (let arg = 0; arg < Math.min(args.length, field.arguments.length); arg++) { - copy[field.arguments[arg]] = args[arg]; - } - return ctx.evaluate(field.value, copy).orElseThrow(); - }); - case "function": - let rawFunc = field.func.type == "variable" - ? Result.success(field.func.name) - : this.evaluate(field.func, data); - if (!rawFunc.successful) - return rawFunc; - let func = rawFunc.value; - let args = []; - for (let arg of field.arguments) { - let resolved = this.evaluate(arg, data); - if (!resolved.successful) - return resolved; - args.push(resolved.value); - } - let call; - if (Values.isFunction(func)) - call = func; - else if (Values.isString(func) && func in this.functions) - call = this.functions[func]; - else if (Values.isString(func)) - return Result.failure(`Unrecognized function name '${func}'`); - else - return Result.failure(`Cannot call type '${Values.typeOf(func)}' as a function`); - try { - return Result.success(call(this, ...args)); - } - catch (e) { - return Result.failure(e.message); - } - case "index": - // TODO: Will move this out to an 'primitives' module and add more content to it. - let literalIndex = this.evaluate(field.index, data); - let checkedIndex = literalIndex.flatMap(s => Values.isString(s) || Values.isNumber(s) || Values.isNull(s) - ? Result.success(s) - : Result.failure("Can only index with a string or number")); - if (!checkedIndex.successful) - return checkedIndex; - let index = checkedIndex.value; - if (Values.isNull(index)) - return Result.success(null); - let checkedObject = field.object.type == "variable" && field.object.name == "row" - ? Result.success(Object.assign({}, this.globals, data)) - : this.evaluate(field.object, data); - if (!checkedObject.successful) - return checkedObject; - let object = Values.wrapValue(checkedObject.value); - if (!object) - return Result.failure("Unrecognized object to index into: " + object); - switch (object.type) { - case "object": - if (!Values.isString(index)) - return Result.failure('can only index into objects with strings (a.b or a["b"])'); - return Result.success((_a = object.value[index]) !== null && _a !== void 0 ? _a : null); - case "link": - if (!Values.isString(index)) - return Result.failure('can only index into links with strings (a.b or a["b"])'); - let linkValue = this.linkHandler.resolve(object.value.path); - if (Values.isNull(linkValue)) - return Result.success(null); - return Result.success((_b = linkValue[index]) !== null && _b !== void 0 ? _b : null); - case "array": - if (Values.isNumber(index)) { - if (index >= object.value.length || index < 0) - return Result.success(null); - else - return Result.success(object.value[index]); - } - else if (Values.isString(index)) { - let result = []; - for (let value of object.value) { - let next = this.evaluate(Fields.index(Fields.literal(value), Fields.literal(index))); - if (!next.successful) - continue; - result.push(next.value); - } - return Result.success(result); - } - else { - return Result.failure("Array indexing requires either a number (to get a specific element), or a string (to map all elements inside the array)"); - } - case "string": - if (!Values.isNumber(index)) - return Result.failure("string indexing requires a numeric index (string[index])"); - if (index >= object.value.length || index < 0) - return Result.success(null); - return Result.success(object.value[index]); - case "date": - if (!Values.isString(index)) - return Result.failure("date indexing requires a string representing the unit"); - switch (index) { - case "year": - return Result.success(object.value.year); - case "month": - return Result.success(object.value.month); - case "weekyear": - return Result.success(object.value.weekNumber); - case "week": - return Result.success(Math.floor(object.value.day / 7) + 1); - case "weekday": - return Result.success(object.value.weekday); - case "day": - return Result.success(object.value.day); - case "hour": - return Result.success(object.value.hour); - case "minute": - return Result.success(object.value.minute); - case "second": - return Result.success(object.value.second); - case "millisecond": - return Result.success(object.value.millisecond); - default: - return Result.success(null); - } - case "duration": - if (!Values.isString(index)) - return Result.failure("duration indexing requires a string representing the unit"); - switch (index) { - case "year": - case "years": - return Result.success(object.value.shiftTo("years").years); - case "month": - case "months": - return Result.success(object.value.shiftTo("months").months); - case "weeks": - return Result.success(object.value.shiftTo("weeks").weeks); - case "day": - case "days": - return Result.success(object.value.shiftTo("days").days); - case "hour": - case "hours": - return Result.success(object.value.shiftTo("hours").hours); - case "minute": - case "minutes": - return Result.success(object.value.shiftTo("minutes").minutes); - case "second": - case "seconds": - return Result.success(object.value.shiftTo("seconds").seconds); - case "millisecond": - case "milliseconds": - return Result.success(object.value.shiftTo("milliseconds").milliseconds); - default: - return Result.success(null); - } - default: - return Result.success(null); - } - } - } -} - -function iden(x) { - return x; -} -/** Shared execution code which just takes in arbitrary data, runs operations over it, and returns it + per-row errors. */ -function executeCore(rows, context, ops) { - let diagnostics = []; - let identMeaning = { type: "path" }; - let startTime = Date.now(); - for (let op of ops) { - let opStartTime = Date.now(); - let incomingRows = rows.length; - let errors = []; - switch (op.type) { - case "where": - let whereResult = []; - for (let index = 0; index < rows.length; index++) { - let row = rows[index]; - let value = context.evaluate(op.clause, row.data); - if (!value.successful) - errors.push({ index, message: value.error }); - else if (Values.isTruthy(value.value)) - whereResult.push(row); - } - rows = whereResult; - break; - case "sort": - let sortFields = op.fields; - let taggedData = []; - outer: for (let index = 0; index < rows.length; index++) { - let row = rows[index]; - let rowSorts = []; - for (let sIndex = 0; sIndex < sortFields.length; sIndex++) { - let value = context.evaluate(sortFields[sIndex].field, row.data); - if (!value.successful) { - errors.push({ index, message: value.error }); - continue outer; - } - rowSorts.push(value.value); - } - taggedData.push({ data: row, fields: rowSorts }); - } - // Sort rows by the sort fields, and then return the finished result. - taggedData.sort((a, b) => { - for (let index = 0; index < sortFields.length; index++) { - let factor = sortFields[index].direction === "ascending" ? 1 : -1; - let le = context.binaryOps - .evaluate("<", a.fields[index], b.fields[index], context) - .orElse(false); - if (Values.isTruthy(le)) - return factor * -1; - let ge = context.binaryOps - .evaluate(">", a.fields[index], b.fields[index], context) - .orElse(false); - if (Values.isTruthy(ge)) - return factor * 1; - } - return 0; - }); - rows = taggedData.map(v => v.data); - break; - case "limit": - let limiting = context.evaluate(op.amount); - if (!limiting.successful) - return Result.failure("Failed to execute 'limit' statement: " + limiting.error); - if (!Values.isNumber(limiting.value)) - return Result.failure(`Failed to execute 'limit' statement: limit should be a number, but got '${Values.typeOf(limiting.value)}' (${limiting.value})`); - rows = rows.slice(0, limiting.value); - break; - case "group": - let groupData = []; - for (let index = 0; index < rows.length; index++) { - let value = context.evaluate(op.field.field, rows[index].data); - if (!value.successful) { - errors.push({ index, message: value.error }); - continue; - } - groupData.push({ data: rows[index], key: value.value }); - } - // Sort by the key, which we will group on shortly. - groupData.sort((a, b) => { - let le = context.binaryOps.evaluate("<", a.key, b.key, context).orElse(false); - if (Values.isTruthy(le)) - return -1; - let ge = context.binaryOps.evaluate(">", a.key, b.key, context).orElse(false); - if (Values.isTruthy(ge)) - return 1; - return 0; - }); - // Then walk through and find fields that are equal. - let finalGroupData = []; - if (groupData.length > 0) - finalGroupData.push({ - key: groupData[0].key, - rows: [groupData[0].data.data], - [op.field.name]: groupData[0].key, - }); - for (let index = 1; index < groupData.length; index++) { - let curr = groupData[index], prev = groupData[index - 1]; - if (context.binaryOps.evaluate("=", curr.key, prev.key, context).orElse(false)) { - finalGroupData[finalGroupData.length - 1].rows.push(curr.data.data); - } - else { - finalGroupData.push({ - key: curr.key, - rows: [curr.data.data], - [op.field.name]: curr.key, - }); - } - } - rows = finalGroupData.map(d => { - return { id: d.key, data: d }; - }); - identMeaning = { type: "group", name: op.field.name, on: identMeaning }; - break; - case "flatten": - let flattenResult = []; - for (let index = 0; index < rows.length; index++) { - let row = rows[index]; - let value = context.evaluate(op.field.field, row.data); - if (!value.successful) { - errors.push({ index, message: value.error }); - continue; - } - let datapoints = Values.isArray(value.value) ? value.value : [value.value]; - for (let v of datapoints) { - let copy = Values.deepCopy(row); - copy.data[op.field.name] = v; - flattenResult.push(copy); - } - } - rows = flattenResult; - if (identMeaning.type == "group" && identMeaning.name == op.field.name) - identMeaning = identMeaning.on; - break; - default: - return Result.failure("Unrecognized query operation '" + op.type + "'"); - } - if (errors.length >= incomingRows && incomingRows > 0) { - return Result.failure(`Every row during operation '${op.type}' failed with an error; first ${Math.min(3, errors.length)}:\n - ${errors - .slice(0, 3) - .map(d => "- " + d.message) - .join("\n")}`); - } - diagnostics.push({ - incomingRows, - errors, - outgoingRows: rows.length, - timeMs: Date.now() - opStartTime, - }); - } - return Result.success({ - data: rows, - idMeaning: identMeaning, - ops, - diagnostics, - timeMs: Date.now() - startTime, - }); -} -/** Expanded version of executeCore which adds an additional "extraction" step to the pipeline. */ -function executeCoreExtract(rows, context, ops, fields) { - let internal = executeCore(rows, context, ops); - if (!internal.successful) - return internal; - let core = internal.value; - let startTime = Date.now(); - let errors = []; - let res = []; - outer: for (let index = 0; index < core.data.length; index++) { - let page = { id: core.data[index].id, data: {} }; - for (let [name, field] of Object.entries(fields)) { - let value = context.evaluate(field, core.data[index].data); - if (!value.successful) { - errors.push({ index: index, message: value.error }); - continue outer; - } - page.data[name] = value.value; - } - res.push(page); - } - if (errors.length >= core.data.length && core.data.length > 0) { - return Result.failure(`Every row during final data extraction failed with an error; first ${Math.max(errors.length, 3)}:\n - ${errors - .slice(0, 3) - .map(d => "- " + d.message) - .join("\n")}`); - } - let execTime = Date.now() - startTime; - return Result.success({ - data: res, - idMeaning: core.idMeaning, - diagnostics: core.diagnostics.concat([ - { - timeMs: execTime, - incomingRows: core.data.length, - outgoingRows: res.length, - errors, - }, - ]), - ops: core.ops.concat([{ type: "extract", fields }]), - timeMs: core.timeMs + execTime, - }); -} -/** Execute a list-based query, returning the final results. */ -async function executeList(query, index, origin, settings) { - var _a, _b; - // Start by collecting all of the files that match the 'from' queries. - let fileset = await resolveSource(query.source, index, origin); - if (!fileset.successful) - return Result.failure(fileset.error); - // Extract information about the origin page to add to the root context. - let rootContext = new Context(defaultLinkHandler(index, origin), settings, { - this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, - }); - let targetField = query.header.format; - let showId = query.header.showId; - let fields = targetField ? { target: targetField } : {}; - return executeCoreExtract(fileset.value, rootContext, query.operations, fields).map(core => { - let data; - if (showId && targetField) { - data = core.data.map(p => { var _a; return Widgets.listPair(p.id, (_a = p.data["target"]) !== null && _a !== void 0 ? _a : null); }); - } - else if (targetField) { - data = core.data.map(p => { var _a; return (_a = p.data["target"]) !== null && _a !== void 0 ? _a : null; }); - } - else { - data = core.data.map(p => p.id); - } - return { primaryMeaning: core.idMeaning, core, data }; - }); -} -/** Execute a table query. */ -async function executeTable(query, index, origin, settings) { - var _a, _b; - // Start by collecting all of the files that match the 'from' queries. - let fileset = await resolveSource(query.source, index, origin); - if (!fileset.successful) - return Result.failure(fileset.error); - // Extract information about the origin page to add to the root context. - let rootContext = new Context(defaultLinkHandler(index, origin), settings, { - this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, - }); - let targetFields = query.header.fields; - let showId = query.header.showId; - let fields = {}; - for (let field of targetFields) - fields[field.name] = field.field; - return executeCoreExtract(fileset.value, rootContext, query.operations, fields).map(core => { - if (showId) { - const idName = core.idMeaning.type === "group" ? core.idMeaning.name : settings.tableIdColumnName; - let names = [idName].concat(targetFields.map(f => f.name)); - let data = core.data.map(p => [p.id].concat(targetFields.map(f => p.data[f.name]))); - return { core, names, data, idMeaning: core.idMeaning }; - } - else { - let names = targetFields.map(f => f.name); - let data = core.data.map(p => targetFields.map(f => p.data[f.name])); - return { core, names, data, idMeaning: core.idMeaning }; - } - }); -} -/** Maps a raw core execution result to a task grouping which is much easier to render. */ -function extractTaskGroupings(id, rows) { - switch (id.type) { - case "path": - return rows; - case "group": - let key = id.name; - return rows.map(r => iden({ - key: r[key], - rows: extractTaskGroupings(id.on, r.rows), - })); - } -} -/** Execute a task query, returning all matching tasks. */ -async function executeTask(query, origin, index, settings) { - var _a, _b; - let fileset = matchingSourcePaths(query.source, index, origin); - if (!fileset.successful) - return Result.failure(fileset.error); - // Collect tasks from pages which match. - let incomingTasks = []; - for (let path of fileset.value) { - let page = index.pages.get(path); - if (!page) - continue; - let pageData = page.serialize(index); - let pageTasks = pageData.file.tasks.map(t => { - const tcopy = Values.deepCopy(t); - // Add page data to this copy. - for (let [key, value] of Object.entries(pageData)) { - if (key in tcopy) - continue; - tcopy[key] = value; - } - return { id: `${pageData.path}#${t.line}`, data: tcopy }; - }); - for (let task of pageTasks) - incomingTasks.push(task); - } - // Extract information about the origin page to add to the root context. - let rootContext = new Context(defaultLinkHandler(index, origin), settings, { - this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, - }); - return executeCore(incomingTasks, rootContext, query.operations).map(core => { - return { - core, - tasks: extractTaskGroupings(core.idMeaning, core.data.map(r => r.data)), - }; - }); -} -/** Execute a single field inline a file, returning the evaluated result. */ -function executeInline(field, origin, index, settings) { - var _a, _b; - return new Context(defaultLinkHandler(index, origin), settings, { - this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, - }).evaluate(field); -} -/** The default link resolver used when creating contexts. */ -function defaultLinkHandler(index, origin) { - return { - resolve: link => { - let realFile = index.metadataCache.getFirstLinkpathDest(link, origin); - if (!realFile) - return null; - let realPage = index.pages.get(realFile.path); - if (!realPage) - return null; - return realPage.serialize(index); - }, - normalize: link => { - var _a; - let realFile = index.metadataCache.getFirstLinkpathDest(link, origin); - return (_a = realFile === null || realFile === void 0 ? void 0 : realFile.path) !== null && _a !== void 0 ? _a : link; - }, - exists: link => { - let realFile = index.metadataCache.getFirstLinkpathDest(link, origin); - return !!realFile; - }, - }; -} -/** Execute a calendar-based query, returning the final results. */ -async function executeCalendar(query, index, origin, settings) { - var _a, _b; - // Start by collecting all of the files that match the 'from' queries. - let fileset = await resolveSource(query.source, index, origin); - if (!fileset.successful) - return Result.failure(fileset.error); - // Extract information about the origin page to add to the root context. - let rootContext = new Context(defaultLinkHandler(index, origin), settings, { - this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, - }); - let targetField = query.header.field.field; - let fields = { - target: targetField, - link: Fields.indexVariable("file.link"), - }; - return executeCoreExtract(fileset.value, rootContext, query.operations, fields).map(core => { - let data = core.data.map(p => iden({ - date: p.data["target"], - link: p.data["link"], - })); - return { core, data }; - }); +/** Execute a calendar-based query, returning the final results. */ +async function executeCalendar(query, index, origin, settings) { + var _a, _b; + // Start by collecting all of the files that match the 'from' queries. + let fileset = await resolveSource(query.source, index, origin); + if (!fileset.successful) + return Result.failure(fileset.error); + // Extract information about the origin page to add to the root context. + let rootContext = new Context(defaultLinkHandler(index, origin), settings, { + this: (_b = (_a = index.pages.get(origin)) === null || _a === void 0 ? void 0 : _a.serialize(index)) !== null && _b !== void 0 ? _b : {}, + }); + let targetField = query.header.field.field; + let fields = { + target: targetField, + link: Fields.indexVariable("file.link"), + }; + return executeCoreExtract(fileset.value, rootContext, query.operations, fields).map(core => { + let data = core.data.map(p => iden({ + date: p.data["target"], + link: p.data["link"], + })); + return { core, data }; + }); } function compareVersions(v1, v2) { @@ -14555,7 +14770,7 @@ const satisfies = (v, r) => { // export CJS style for parity compareVersions.validate = validate; compareVersions.compare = compare; -compareVersions.sastisfies = satisfies; +compareVersions.satisfies = satisfies; const semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i; @@ -14621,757 +14836,762 @@ const assertValidOperator = (op) => { } }; -var n,l$1,u$1,t$1,o$1,r$1,f$1,e$1={},c$1=[],s$1=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;function a$1(n,l){for(var u in l)n[u]=l[u];return n}function h$1(n){var l=n.parentNode;l&&l.removeChild(n);}function v$1(l,u,i){var t,o,r,f={};for(r in u)"key"==r?t=u[r]:"ref"==r?o=u[r]:f[r]=u[r];if(arguments.length>2&&(f.children=arguments.length>3?n.call(arguments,2):i),"function"==typeof l&&null!=l.defaultProps)for(r in l.defaultProps)void 0===f[r]&&(f[r]=l.defaultProps[r]);return y$1(l,f,t,o,null)}function y$1(n,i,t,o,r){var f={type:n,props:i,key:t,ref:o,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,__h:null,constructor:void 0,__v:null==r?++u$1:r};return null==r&&null!=l$1.vnode&&l$1.vnode(f),f}function p$1(){return {current:null}}function d$1(n){return n.children}function _$1(n,l){this.props=n,this.context=l;}function k$2(n,l){if(null==l)return n.__?k$2(n.__,n.__.__k.indexOf(n)+1):null;for(var u;l0?y$1(_.type,_.props,_.key,null,_.__v):_)){if(_.__=u,_.__b=u.__b+1,null===(p=w[h])||p&&_.key==p.key&&_.type===p.type)w[h]=void 0;else for(v=0;v2&&(f.children=arguments.length>3?n.call(arguments,2):i),y$1(l.type,f,t||l.key,o||l.ref,null)}function D$1(n,l){var u={__c:l="__cC"+f$1++,__:n,Consumer:function(n,l){return n.children(l)},Provider:function(n){var u,i;return this.getChildContext||(u=[],(i={})[l]=this,this.getChildContext=function(){return i},this.shouldComponentUpdate=function(n){this.props.value!==n.value&&u.some(m$1);},this.sub=function(n){u.push(n);var l=n.componentWillUnmount;n.componentWillUnmount=function(){u.splice(u.indexOf(n),1),l&&l.call(n);};}),n.children}};return u.Provider.__=u.Consumer.contextType=u}n=c$1.slice,l$1={__e:function(n,l,u,i){for(var t,o,r;l=l.__;)if((t=l.__c)&&!t.__)try{if((o=t.constructor)&&null!=o.getDerivedStateFromError&&(t.setState(o.getDerivedStateFromError(n)),r=t.__d),null!=t.componentDidCatch&&(t.componentDidCatch(n,i||{}),r=t.__d),r)return t.__E=t}catch(l){n=l;}throw n}},u$1=0,_$1.prototype.setState=function(n,l){var u;u=null!=this.__s&&this.__s!==this.state?this.__s:this.__s=a$1({},this.state),"function"==typeof n&&(n=n(a$1({},u),this.props)),n&&a$1(u,n),null!=n&&this.__v&&(l&&this.__h.push(l),m$1(this));},_$1.prototype.forceUpdate=function(n){this.__v&&(this.__e=!0,n&&this.__h.push(n),m$1(this));},_$1.prototype.render=d$1,t$1=[],o$1="function"==typeof Promise?Promise.prototype.then.bind(Promise.resolve()):setTimeout,g$2.__r=0,f$1=0; - -var t,u,r,o=0,i=[],c=l$1.__b,f=l$1.__r,e=l$1.diffed,a=l$1.__c,v=l$1.unmount;function l(t,r){l$1.__h&&l$1.__h(u,t,o||r),o=0;var i=u.__H||(u.__H={__:[],__h:[]});return t>=i.__.length&&i.__.push({}),i.__[t]}function m(n){return o=1,p(w$1,n)}function p(n,r,o){var i=l(t++,2);return i.t=n,i.__c||(i.__=[o?o(r):w$1(void 0,r),function(n){var t=i.t(i.__[0],n);i.__[0]!==t&&(i.__=[t,i.__[1]],i.__c.setState({}));}],i.__c=u),i.__}function y(r,o){var i=l(t++,3);!l$1.__s&&k$1(i.__H,o)&&(i.__=r,i.__H=o,u.__H.__h.push(i));}function d(r,o){var i=l(t++,4);!l$1.__s&&k$1(i.__H,o)&&(i.__=r,i.__H=o,u.__h.push(i));}function h(n){return o=5,_(function(){return {current:n}},[])}function s(n,t,u){o=6,d(function(){return "function"==typeof n?(n(t()),function(){return n(null)}):n?(n.current=t(),function(){return n.current=null}):void 0},null==u?u:u.concat(n));}function _(n,u){var r=l(t++,7);return k$1(r.__H,u)&&(r.__=n(),r.__H=u,r.__h=n),r.__}function A$1(n,t){return o=8,_(function(){return n},t)}function F$1(n){var r=u.context[n.__c],o=l(t++,9);return o.c=n,r?(null==o.__&&(o.__=!0,r.sub(u)),r.props.value):n.__}function T$1(t,u){l$1.useDebugValue&&l$1.useDebugValue(u?u(t):t);}function x$1(){for(var t;t=i.shift();)if(t.__P)try{t.__H.__h.forEach(g$1),t.__H.__h.forEach(j$1),t.__H.__h=[];}catch(u){t.__H.__h=[],l$1.__e(u,t.__v);}}l$1.__b=function(n){u=null,c&&c(n);},l$1.__r=function(n){f&&f(n),t=0;var r=(u=n.__c).__H;r&&(r.__h.forEach(g$1),r.__h.forEach(j$1),r.__h=[]);},l$1.diffed=function(t){e&&e(t);var o=t.__c;o&&o.__H&&o.__H.__h.length&&(1!==i.push(o)&&r===l$1.requestAnimationFrame||((r=l$1.requestAnimationFrame)||function(n){var t,u=function(){clearTimeout(r),b&&cancelAnimationFrame(t),setTimeout(n);},r=setTimeout(u,100);b&&(t=requestAnimationFrame(u));})(x$1)),u=null;},l$1.__c=function(t,u){u.some(function(t){try{t.__h.forEach(g$1),t.__h=t.__h.filter(function(n){return !n.__||j$1(n)});}catch(r){u.some(function(n){n.__h&&(n.__h=[]);}),u=[],l$1.__e(r,t.__v);}}),a&&a(t,u);},l$1.unmount=function(t){v&&v(t);var u,r=t.__c;r&&r.__H&&(r.__H.__.forEach(function(n){try{g$1(n);}catch(n){u=n;}}),u&&l$1.__e(u,r.__v));};var b="function"==typeof requestAnimationFrame;function g$1(n){var t=u,r=n.__c;"function"==typeof r&&(n.__c=void 0,r()),u=t;}function j$1(n){var t=u;n.__c=n.__(),u=t;}function k$1(n,t){return !n||n.length!==t.length||t.some(function(t,u){return t!==n[u]})}function w$1(n,t){return "function"==typeof t?t(n):t} - -function C(n,t){for(var e in t)n[e]=t[e];return n}function S(n,t){for(var e in n)if("__source"!==e&&!(e in t))return !0;for(var r in t)if("__source"!==r&&n[r]!==t[r])return !0;return !1}function E(n){this.props=n;}function g(n,t){function e(n){var e=this.props.ref,r=e==n.ref;return !r&&e&&(e.call?e(null):e.current=null),t?!t(this.props,n)||!r:S(this.props,n)}function r(t){return this.shouldComponentUpdate=e,v$1(n,t)}return r.displayName="Memo("+(n.displayName||n.name)+")",r.prototype.isReactComponent=!0,r.__f=!0,r}(E.prototype=new _$1).isPureReactComponent=!0,E.prototype.shouldComponentUpdate=function(n,t){return S(this.props,n)||S(this.state,t)};var w=l$1.__b;l$1.__b=function(n){n.type&&n.type.__f&&n.ref&&(n.props.ref=n.ref,n.ref=null),w&&w(n);};var R="undefined"!=typeof Symbol&&Symbol.for&&Symbol.for("react.forward_ref")||3911;function x(n){function t(t){var e=C({},t);return delete e.ref,n(e,t.ref||null)}return t.$$typeof=R,t.render=t,t.prototype.isReactComponent=t.__f=!0,t.displayName="ForwardRef("+(n.displayName||n.name)+")",t}var N=function(n,t){return null==n?null:A$2(A$2(n).map(t))},k={map:N,forEach:N,count:function(n){return n?A$2(n).length:0},only:function(n){var t=A$2(n);if(1!==t.length)throw "Children.only";return t[0]},toArray:A$2},A=l$1.__e;l$1.__e=function(n,t,e,r){if(n.then)for(var u,o=t;o=o.__;)if((u=o.__c)&&u.__c)return null==t.__e&&(t.__e=e.__e,t.__k=e.__k),u.__c(n,t);A(n,t,e,r);};var O=l$1.unmount;function L(){this.__u=0,this.t=null,this.__b=null;}function U(n){var t=n.__.__c;return t&&t.__e&&t.__e(n)}function F(n){var t,e,r;function u(u){if(t||(t=n()).then(function(n){e=n.default||n;},function(n){r=n;}),r)throw r;if(!e)throw t;return v$1(e,u)}return u.displayName="Lazy",u.__f=!0,u}function M(){this.u=null,this.o=null;}l$1.unmount=function(n){var t=n.__c;t&&t.__R&&t.__R(),t&&!0===n.__h&&(n.type=null),O&&O(n);},(L.prototype=new _$1).__c=function(n,t){var e=t.__c,r=this;null==r.t&&(r.t=[]),r.t.push(e);var u=U(r.__v),o=!1,i=function(){o||(o=!0,e.__R=null,u?u(l):l());};e.__R=i;var l=function(){if(!--r.__u){if(r.state.__e){var n=r.state.__e;r.__v.__k[0]=function n(t,e,r){return t&&(t.__v=null,t.__k=t.__k&&t.__k.map(function(t){return n(t,e,r)}),t.__c&&t.__c.__P===e&&(t.__e&&r.insertBefore(t.__e,t.__d),t.__c.__e=!0,t.__c.__P=r)),t}(n,n.__c.__P,n.__c.__O);}var t;for(r.setState({__e:r.__b=null});t=r.t.pop();)t.forceUpdate();}},f=!0===t.__h;r.__u++||f||r.setState({__e:r.__b=r.__v.__k[0]}),n.then(i,i);},L.prototype.componentWillUnmount=function(){this.t=[];},L.prototype.render=function(n,t){if(this.__b){if(this.__v.__k){var e=document.createElement("div"),r=this.__v.__k[0].__c;this.__v.__k[0]=function n(t,e,r){return t&&(t.__c&&t.__c.__H&&(t.__c.__H.__.forEach(function(n){"function"==typeof n.__c&&n.__c();}),t.__c.__H=null),null!=(t=C({},t)).__c&&(t.__c.__P===r&&(t.__c.__P=e),t.__c=null),t.__k=t.__k&&t.__k.map(function(t){return n(t,e,r)})),t}(this.__b,e,r.__O=r.__P);}this.__b=null;}var u=t.__e&&v$1(d$1,null,n.fallback);return u&&(u.__h=null),[v$1(d$1,null,t.__e?null:n.children),u]};var T=function(n,t,e){if(++e[1]===e[0]&&n.o.delete(t),n.props.revealOrder&&("t"!==n.props.revealOrder[0]||!n.o.size))for(e=n.u;e;){for(;e.length>3;)e.pop()();if(e[1]>>1,1),t.i.removeChild(n);}}),S$1(v$1(D,{context:t.context},n.__v),t.l)):t.l&&t.componentWillUnmount();}function W(n,t){var e=v$1(I,{__v:n,i:t});return e.containerInfo=t,e}(M.prototype=new _$1).__e=function(n){var t=this,e=U(t.__v),r=t.o.get(n);return r[0]++,function(u){var o=function(){t.props.revealOrder?(r.push(u),T(t,n,r)):u();};e?e(o):o();}},M.prototype.render=function(n){this.u=null,this.o=new Map;var t=A$2(n.children);n.revealOrder&&"b"===n.revealOrder[0]&&t.reverse();for(var e=t.length;e--;)this.o.set(t[e],this.u=[1,0,this.u]);return n.children},M.prototype.componentDidUpdate=M.prototype.componentDidMount=function(){var n=this;this.o.forEach(function(t,e){T(n,e,t);});};var P="undefined"!=typeof Symbol&&Symbol.for&&Symbol.for("react.element")||60103,V=/^(?:accent|alignment|arabic|baseline|cap|clip(?!PathU)|color|dominant|fill|flood|font|glyph(?!R)|horiz|marker(?!H|W|U)|overline|paint|stop|strikethrough|stroke|text(?!L)|underline|unicode|units|v|vector|vert|word|writing|x(?!C))[A-Z]/,j="undefined"!=typeof document,z=function(n){return ("undefined"!=typeof Symbol&&"symbol"==typeof Symbol()?/fil|che|rad/i:/fil|che|ra/i).test(n)};function B(n,t,e){return null==t.__k&&(t.textContent=""),S$1(n,t),"function"==typeof e&&e(),n?n.__c:null}function $(n,t,e){return q$1(n,t),"function"==typeof e&&e(),n?n.__c:null}_$1.prototype.isReactComponent={},["componentWillMount","componentWillReceiveProps","componentWillUpdate"].forEach(function(n){Object.defineProperty(_$1.prototype,n,{configurable:!0,get:function(){return this["UNSAFE_"+n]},set:function(t){Object.defineProperty(this,n,{configurable:!0,writable:!0,value:t});}});});var H=l$1.event;function Z(){}function Y(){return this.cancelBubble}function q(){return this.defaultPrevented}l$1.event=function(n){return H&&(n=H(n)),n.persist=Z,n.isPropagationStopped=Y,n.isDefaultPrevented=q,n.nativeEvent=n};var G,J={configurable:!0,get:function(){return this.class}},K=l$1.vnode;l$1.vnode=function(n){var t=n.type,e=n.props,r=e;if("string"==typeof t){var u=-1===t.indexOf("-");for(var o in r={},e){var i=e[o];j&&"children"===o&&"noscript"===t||"value"===o&&"defaultValue"in e&&null==i||("defaultValue"===o&&"value"in e&&null==e.value?o="value":"download"===o&&!0===i?i="":/ondoubleclick/i.test(o)?o="ondblclick":/^onchange(textarea|input)/i.test(o+t)&&!z(e.type)?o="oninput":/^onfocus$/i.test(o)?o="onfocusin":/^onblur$/i.test(o)?o="onfocusout":/^on(Ani|Tra|Tou|BeforeInp|Compo)/.test(o)?o=o.toLowerCase():u&&V.test(o)?o=o.replace(/[A-Z0-9]/,"-$&").toLowerCase():null===i&&(i=void 0),r[o]=i);}"select"==t&&r.multiple&&Array.isArray(r.value)&&(r.value=A$2(e.children).forEach(function(n){n.props.selected=-1!=r.value.indexOf(n.props.value);})),"select"==t&&null!=r.defaultValue&&(r.value=A$2(e.children).forEach(function(n){n.props.selected=r.multiple?-1!=r.defaultValue.indexOf(n.props.value):r.defaultValue==n.props.value;})),n.props=r,e.class!=e.className&&(J.enumerable="className"in e,null!=e.className&&(r.class=e.className),Object.defineProperty(r,"className",J));}n.$$typeof=P,K&&K(n);};var Q=l$1.__r;l$1.__r=function(n){Q&&Q(n),G=n.__c;};var X={ReactCurrentDispatcher:{current:{readContext:function(n){return G.__n[n.__c].props.value}}}};function tn(n){return v$1.bind(null,n)}function en(n){return !!n&&n.$$typeof===P}function rn(n){return en(n)?B$1.apply(null,arguments):n}function un(n){return !!n.__k&&(S$1(null,n),!0)}function on(n){return n&&(n.base||1===n.nodeType&&n)||null}var ln=function(n,t){return n(t)},fn=function(n,t){return n(t)};var React = {useState:m,useReducer:p,useEffect:y,useLayoutEffect:d,useRef:h,useImperativeHandle:s,useMemo:_,useCallback:A$1,useContext:F$1,useDebugValue:T$1,version:"17.0.2",Children:k,render:B,hydrate:$,unmountComponentAtNode:un,createPortal:W,createElement:v$1,createContext:D$1,createFactory:tn,cloneElement:rn,createRef:p$1,Fragment:d$1,isValidElement:en,findDOMNode:on,Component:_$1,PureComponent:E,memo:g,forwardRef:x,flushSync:fn,unstable_batchedUpdates:ln,StrictMode:d$1,Suspense:L,SuspenseList:M,lazy:F,__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED:X}; - -const IMAGE_EXTENSIONS = Object.freeze(new Set([ - ".tif", - ".tiff", - ".gif", - ".png", - ".apng", - ".avif", - ".jpg", - ".jpeg", - ".jfif", - ".pjepg", - ".pjp", - ".svg", - ".webp", - ".bmp", - ".ico", - ".cur", -])); -/** Determines if the given link points to an embedded image. */ -function isImageEmbed(link) { - if (!link.path.contains(".")) - return false; - let extension = link.path.substring(link.path.lastIndexOf(".")); - return link.type == "file" && link.embed && IMAGE_EXTENSIONS.has(extension); -} -/** Extract text of the form 'WxH' or 'W' from the display of a link. */ -function extractImageDimensions(link) { - if (!link.display) - return undefined; - let match = /^(\d+)x(\d+)$/iu.exec(link.display); - if (match) - return [parseInt(match[1]), parseInt(match[2])]; - let match2 = /^(\d+)/.exec(link.display); - if (match2) - return [parseInt(match2[1])]; - // No match. - return undefined; -} - -/** Provides core preact / rendering utilities for all view types. */ -const DataviewContext = D$1(undefined); -/** Hacky preact component which wraps Obsidian's markdown renderer into a neat component. */ -function RawMarkdown({ content, sourcePath, inline = true, style, cls, onClick, }) { - const container = h(null); - const component = F$1(DataviewContext).component; - y(() => { - if (!container.current) - return; - container.current.innerHTML = ""; - obsidian.MarkdownRenderer.renderMarkdown(content, container.current, sourcePath, component).then(() => { - if (!container.current || !inline) - return; - // Unwrap any created paragraph elements if we are inline. - let paragraph = container.current.querySelector("p"); - while (paragraph) { - let children = paragraph.childNodes; - paragraph.replaceWith(...Array.from(children)); - paragraph = container.current.querySelector("p"); - } - }); - }, [content, sourcePath, container.current]); - return v$1("span", { ref: container, style: style, class: cls, onClick: onClick }); -} -/** Hacky preact component which wraps Obsidian's markdown renderer into a neat component. */ -const Markdown = React.memo(RawMarkdown); -/** Embeds an HTML element in the react DOM. */ -function RawEmbedHtml({ element }) { - const container = h(null); - y(() => { - if (!container.current) - return; - container.current.innerHTML = ""; - container.current.appendChild(element); - }, [container.current, element]); - return v$1("span", { ref: container }); -} -/** Embeds an HTML element in the react DOM. */ -const EmbedHtml = React.memo(RawEmbedHtml); -/** Intelligently render an arbitrary literal value. */ -function RawLit({ value, sourcePath, inline = false, depth = 0, }) { - var _a, _b, _c; - const context = F$1(DataviewContext); - // Short-circuit if beyond the maximum render depth. - if (depth >= context.settings.maxRecursiveRenderDepth) - return v$1(d$1, null, "..."); - if (Values.isNull(value) || value === undefined) { - return v$1(Markdown, { content: context.settings.renderNullAs, sourcePath: sourcePath }); - } - else if (Values.isString(value)) { - return v$1(Markdown, { content: value, sourcePath: sourcePath }); - } - else if (Values.isNumber(value)) { - return v$1(d$1, null, "" + value); - } - else if (Values.isBoolean(value)) { - return v$1(d$1, null, "" + value); - } - else if (Values.isDate(value)) { - return v$1(d$1, null, renderMinimalDate(value, context.settings, currentLocale())); - } - else if (Values.isDuration(value)) { - return v$1(d$1, null, renderMinimalDuration(value)); - } - else if (Values.isLink(value)) { - // Special case handling of image/video/etc embeddings to bypass the Obsidian API not working. - if (isImageEmbed(value)) { - let realFile = context.app.metadataCache.getFirstLinkpathDest(value.path, sourcePath); - if (!realFile) - return v$1(Markdown, { content: value.markdown(), sourcePath: sourcePath }); - let dimensions = extractImageDimensions(value); - let resourcePath = context.app.vault.getResourcePath(realFile); - if (dimensions && dimensions.length == 2) - return v$1("img", { alt: value.path, src: resourcePath, width: dimensions[0], height: dimensions[1] }); - else if (dimensions && dimensions.length == 1) - return v$1("img", { alt: value.path, src: resourcePath, width: dimensions[0] }); - else - return v$1("img", { alt: value.path, src: resourcePath }); - } - return v$1(Markdown, { content: value.markdown(), sourcePath: sourcePath }); - } - else if (Values.isHtml(value)) { - return v$1(EmbedHtml, { element: value }); - } - else if (Values.isWidget(value)) { - if (Widgets.isListPair(value)) { - return (v$1(d$1, null, - v$1(Lit, { value: value.key, sourcePath: sourcePath }), - ":", - " ", - v$1(Lit, { value: value.value, sourcePath: sourcePath }))); - } - else if (Widgets.isExternalLink(value)) { - return (v$1("a", { href: value.url, rel: "noopener", target: "_blank", class: "external-link" }, (_a = value.display) !== null && _a !== void 0 ? _a : value.url)); - } - else { - return v$1("b", null, - ""); - } - } - else if (Values.isFunction(value)) { - return v$1(d$1, null, ""); - } - else if (Values.isArray(value) || DataArray.isDataArray(value)) { - if (!inline) { - return (v$1("ul", { class: "dataview dataview-ul dataview-result-list-ul" }, value.map(subvalue => (v$1("li", { class: "dataview-result-list-li" }, - v$1(Lit, { value: subvalue, sourcePath: sourcePath, inline: inline, depth: depth + 1 })))))); - } - else { - if (value.length == 0) - return v$1(d$1, null, ""); - return (v$1("span", { class: "dataview dataview-result-list-span" }, value.map((subvalue, index) => (v$1(d$1, null, - index == 0 ? "" : ", ", - v$1(Lit, { value: subvalue, sourcePath: sourcePath, inline: inline, depth: depth + 1 })))))); - } - } - else if (Values.isObject(value)) { - // Don't render classes in case they have recursive references; spoopy. - if (((_b = value === null || value === void 0 ? void 0 : value.constructor) === null || _b === void 0 ? void 0 : _b.name) && ((_c = value === null || value === void 0 ? void 0 : value.constructor) === null || _c === void 0 ? void 0 : _c.name) != "Object") { - return v$1(d$1, null, - "<", - value.constructor.name, - ">"); - } - if (!inline) { - return (v$1("ul", { class: "dataview dataview-ul dataview-result-object-ul" }, Object.entries(value).map(([key, value]) => (v$1("li", { class: "dataview dataview-li dataview-result-object-li" }, - key, - ": ", - v$1(Lit, { value: value, sourcePath: sourcePath, inline: inline, depth: depth + 1 })))))); - } - else { - if (Object.keys(value).length == 0) - return v$1(d$1, null, ""); - return (v$1("span", { class: "dataview dataview-result-object-span" }, Object.entries(value).map(([key, value], index) => (v$1(d$1, null, - index == 0 ? "" : ", ", - key, - ": ", - v$1(Lit, { value: value, sourcePath: sourcePath, inline: inline, depth: depth + 1 })))))); - } - } - return v$1(d$1, null, - ""); -} -/** Intelligently render an arbitrary literal value. */ -const Lit = React.memo(RawLit); -/** Render a simple nice looking error box in a code style. */ -function ErrorPre(props, {}) { - return v$1("pre", { class: "dataview dataview-error" }, props.children); -} -/** Render a pretty centered error message in a box. */ -function ErrorMessage({ message }) { - return (v$1("div", { class: "dataview dataview-error-box" }, - v$1("p", { class: "dataview dataview-error-message" }, message))); -} -/** - * Complex convienence hook which calls `compute` every time the index updates, updating the current state. - */ -function useIndexBackedState(container, app, settings, index, initial, compute) { - let [initialized, setInitialized] = m(false); - let [state, updateState] = m(initial); - let [lastReload, setLastReload] = m(index.revision); - // Initial setup to queue fetching the correct state. - if (!initialized) { - setLastReload(index.revision); - setInitialized(true); - compute().then(updateState); - } - // Updated on every container re-create; automatically updates state. - y(() => { - const refreshOperation = () => { - if (lastReload != index.revision && container.isShown() && settings.refreshEnabled) { - compute().then(updateState); - setLastReload(index.revision); - } - }; - // Refresh after index changes stop. - let workEvent = app.workspace.on("dataview:refresh-views", refreshOperation); - // ...or when the DOM is shown (sidebar expands, tab selected, nodes scrolled into view). - let nodeEvent = container.onNodeInserted(refreshOperation); - return () => { - app.workspace.offref(workEvent); - nodeEvent(); - }; - }, [container, lastReload]); - return state; -} -/** A trivial wrapper which allows a react component to live for the duration of a `MarkdownRenderChild`. */ -class ReactRenderer extends obsidian.MarkdownRenderChild { - constructor(init, element) { - super(init.container); - this.init = init; - this.element = element; - } - onload() { - const context = Object.assign({}, { component: this }, this.init); - S$1(v$1(DataviewContext.Provider, { value: context }, this.element), this.containerEl); - } - onunload() { - un(this.containerEl); - } -} - -/** Function used to test if a given event correspond to a pressed link */ -function wasLinkPressed(evt) { - return evt.target != null && evt.target != undefined && evt.target.tagName == "A"; -} -/** JSX component which renders a task element recursively. */ -function TaskItem({ item }) { - var _a; - let context = F$1(DataviewContext); - // Navigate to the given task on click. - const onClicked = (evt) => { - if (wasLinkPressed(evt)) { - return; - } - evt.stopPropagation(); - const selectionState = { - eState: { - cursor: { - from: { line: item.line, ch: item.position.start.col }, - to: { line: item.line + item.lineCount - 1, ch: item.position.end.col }, - }, - line: item.line, - }, - }; - // MacOS interprets the Command key as Meta. - context.app.workspace.openLinkText(item.link.toFile().obsidianLink(), item.path, evt.ctrlKey || (evt.metaKey && obsidian.Platform.isMacOS), selectionState); - }; - // Check/uncheck the task in the original file. - const onChecked = (evt) => { - evt.stopPropagation(); - const completed = evt.currentTarget.checked; - const status = completed ? "x" : " "; - // Update data-task on the parent element (css style) - const parent = evt.currentTarget.parentElement; - parent === null || parent === void 0 ? void 0 : parent.setAttribute("data-task", status); - let flatted = [item]; - if (context.settings.recursiveSubTaskCompletion) { - function flatter(iitem) { - flatted.push(iitem); - iitem.children.forEach(flatter); - } - item.children.forEach(flatter); - flatted = flatted.flat(Infinity); - } - async function effectFn() { - for (let i = 0; i < flatted.length; i++) { - const _item = flatted[i]; - let updatedText = _item.text; - if (context.settings.taskCompletionTracking) { - updatedText = setTaskCompletion(_item.text, context.settings.taskCompletionUseEmojiShorthand, context.settings.taskCompletionText, context.settings.taskCompletionDateFormat, completed); - } - await rewriteTask(context.app.vault, _item, status, updatedText); - } - context.app.workspace.trigger("dataview:refresh-views"); - } - effectFn(); - }; - const checked = item.status !== " "; - return (v$1("li", { class: "dataview task-list-item" + (checked ? " is-checked" : ""), onClick: onClicked, "data-task": item.status }, - v$1("input", { class: "dataview task-list-item-checkbox", type: "checkbox", checked: checked, onClick: onChecked }), - v$1(Markdown, { inline: true, content: (_a = item.visual) !== null && _a !== void 0 ? _a : item.text, sourcePath: item.path }), - item.children.length > 0 && v$1(TaskList, { items: item.children }))); -} -/** JSX component which renders a plain list item recursively. */ -function ListItem({ item }) { - var _a; - let context = F$1(DataviewContext); - // Navigate to the given task on click. - const onClicked = (evt) => { - if (wasLinkPressed(evt)) { - return; - } - evt.stopPropagation(); - const selectionState = { - eState: { - cursor: { - from: { line: item.line, ch: item.position.start.col }, - to: { line: item.line + item.lineCount - 1, ch: item.position.end.col }, - }, - line: item.line, - }, - }; - // MacOS interprets the Command key as Meta. - context.app.workspace.openLinkText(item.link.toFile().obsidianLink(), item.path, evt.ctrlKey || (evt.metaKey && obsidian.Platform.isMacOS), selectionState); - }; - return (v$1("li", { class: "dataview task-list-basic-item", onClick: onClicked }, - v$1(Markdown, { inline: true, content: (_a = item.visual) !== null && _a !== void 0 ? _a : item.text, sourcePath: item.path }), - item.children.length > 0 && v$1(TaskList, { items: item.children }))); -} -/** JSX component which renders a list of task items recursively. */ -function TaskList({ items }) { - const settings = F$1(DataviewContext).settings; - if (items.length == 0 && settings.warnOnEmptyResult) - return v$1(ErrorMessage, { message: "Dataview: No results to show for task query." }); - let [nest, _mask] = nestItems(items); - return (v$1("ul", { class: "contains-task-list" }, nest.map(item => item.task ? v$1(TaskItem, { key: listId(item), item: item }) : v$1(ListItem, { key: listId(item), item: item })))); -} -/** JSX component which returns the result count. */ -function ResultCount$1(props) { - const { settings } = F$1(DataviewContext); - return settings.showResultCount ? (v$1("span", { class: "dataview small-text" }, Groupings.count(props.item.rows))) : (v$1(d$1, null)); -} -/** JSX component which recursively renders grouped tasks. */ -function TaskGrouping({ items, sourcePath }) { - const isGrouping = items.length > 0 && Groupings.isGrouping(items); - return (v$1(d$1, null, - isGrouping && - items.map(item => (v$1(d$1, { key: item.key }, - v$1("h4", null, - v$1(Lit, { value: item.key, sourcePath: sourcePath }), - v$1(ResultCount$1, { item: item })), - v$1("div", { class: "dataview result-group" }, - v$1(TaskGrouping, { items: item.rows, sourcePath: sourcePath }))))), - !isGrouping && v$1(TaskList, { items: items }))); -} -/** - * Pure view over (potentially grouped) tasks and list items which allows for checking/unchecking tasks and manipulating - * the task view. - */ -function TaskView({ query, sourcePath }) { - let context = F$1(DataviewContext); - let items = useIndexBackedState(context.container, context.app, context.settings, context.index, { state: "loading" }, async () => { - let result = await asyncTryOrPropogate(() => executeTask(query, sourcePath, context.index, context.settings)); - if (!result.successful) - return { state: "error", error: result.error, sourcePath }; - else - return { state: "ready", items: result.value.tasks }; - }); - if (items.state == "loading") - return (v$1(d$1, null, - v$1(ErrorPre, null, "Loading"))); - else if (items.state == "error") - return (v$1(d$1, null, - v$1(ErrorPre, null, - "Dataview: ", - items.error))); - return (v$1("div", { class: "dataview dataview-container" }, - v$1(TaskGrouping, { items: items.items, sourcePath: sourcePath }))); -} -function createTaskView(init, query, sourcePath) { - return new ReactRenderer(init, v$1(TaskView, { query: query, sourcePath: sourcePath })); -} -function createFixedTaskView(init, items, sourcePath) { - return new ReactRenderer(init, v$1(TaskGrouping, { items: items, sourcePath: sourcePath })); -} -///////////////////////// -// Task De-Duplication // -///////////////////////// -function listId(item) { - return item.path + ":" + item.line; -} -function parentListId(item) { - return item.path + ":" + item.parent; -} -/** Compute a map of all task IDs -> tasks. */ -function enumerateChildren(item, output) { - if (!output.has(listId(item))) - output.set(listId(item), item); - for (let child of item.children) - enumerateChildren(child, output); - return output; -} -/** Replace basic tasks with tasks from a lookup map. Retains the original order of the list. */ -function replaceChildren(elements, lookup) { - return elements.map(element => { - element.children = replaceChildren(element.children, lookup); - const id = listId(element); - const map = lookup.get(id); - if (map) - return map; - else - return element; - }); -} -/** - * Removes tasks from a list if they are already present by being a child of another task. Fixes child pointers. - * Retains original order of input list. - */ -function nestItems(raw) { - let elements = new Map(); - let mask = new Set(); - for (let elem of raw) { - let id = listId(elem); - elements.set(id, elem); - mask.add(id); - } - // List all elements & their children in the lookup map. - for (let elem of raw) - enumerateChildren(elem, elements); - let roots = raw.filter(elem => elem.parent == undefined || elem.parent == null || !elements.has(parentListId(elem))); - return [replaceChildren(roots, elements), mask]; -} -/** - * Recursively removes tasks from each subgroup if they are already present by being a child of another task. - * Fixes child pointers. Retains original order of input list. - */ -function nestGroups(raw) { - if (Groupings.isGrouping(raw)) { - return raw.map(g => { - return { key: g.key, rows: nestGroups(g.rows) }; - }); - } - else { - return nestItems(raw)[0]; - } -} -/////////////////////// -// Task Manipulation // -/////////////////////// -/** Trim empty ending lines. */ -function trimEndingLines(text) { - let parts = text.split(/\r?\n/u); - let trim = parts.length - 1; - while (trim > 0 && parts[trim].trim() == "") - trim--; - return parts.join("\n"); -} -/** Set the task completion key on check. */ -function setTaskCompletion(originalText, useEmojiShorthand, completionKey, completionDateFormat, complete) { - const blockIdRegex = /\^[a-z0-9\-]+/i; - if (!complete && !useEmojiShorthand) - return trimEndingLines(setInlineField(originalText.trimEnd(), completionKey)).trimEnd(); - let parts = originalText.split(/\r?\n/u); - const matches = blockIdRegex.exec(parts[parts.length - 1]); - console.debug("matchreg", matches); - let processedPart = parts[parts.length - 1].split(blockIdRegex).join(""); // last part without block id - if (useEmojiShorthand) { - processedPart = setEmojiShorthandCompletionField(processedPart, complete ? DateTime.now().toFormat("yyyy-MM-dd") : ""); - } - else { - processedPart = setInlineField(processedPart, completionKey, DateTime.now().toFormat(completionDateFormat)); - } - processedPart = `${processedPart.trimEnd()}${(matches === null || matches === void 0 ? void 0 : matches.length) ? " " + matches[0].trim() : ""}`.trimEnd(); // add back block id - parts[parts.length - 1] = processedPart; - return parts.join("\n"); -} -/** Rewrite a task with the given completion status and new text. */ -async function rewriteTask(vault, task, desiredStatus, desiredText) { - if (desiredStatus == task.status && (desiredText == undefined || desiredText == task.text)) - return; - desiredStatus = desiredStatus == "" ? " " : desiredStatus; - let rawFiletext = await vault.adapter.read(task.path); - let hasRN = rawFiletext.contains("\r"); - let filetext = rawFiletext.split(/\r?\n/u); - if (filetext.length < task.line) - return; - let match = LIST_ITEM_REGEX.exec(filetext[task.line]); - if (!match || match[2].length == 0) - return; - let taskTextParts = task.text.split("\n"); - if (taskTextParts[0].trim() != match[3].trim()) - return; - // We have a positive match here at this point, so go ahead and do the rewrite of the status. - let initialSpacing = /^[\s>]*/u.exec(filetext[task.line])[0]; - if (desiredText) { - let desiredParts = desiredText.split("\n"); - let newTextLines = [`${initialSpacing}${task.symbol} [${desiredStatus}] ${desiredParts[0]}`].concat(desiredParts.slice(1).map(l => initialSpacing + "\t" + l)); - filetext.splice(task.line, task.lineCount, ...newTextLines); - } - else { - filetext[task.line] = `${initialSpacing}${task.symbol} [${desiredStatus}] ${taskTextParts[0].trim()}`; - } - let newText = filetext.join(hasRN ? "\r\n" : "\n"); - await vault.adapter.write(task.path, newText); -} - -function ListGrouping({ items, sourcePath }) { - return (v$1("ul", { class: "dataview list-view-ul" }, items.map(item => (v$1("li", null, - v$1(Lit, { value: item, sourcePath: sourcePath })))))); -} -/** Pure view over list elements. */ -function ListView({ query, sourcePath }) { - let context = F$1(DataviewContext); - let items = useIndexBackedState(context.container, context.app, context.settings, context.index, { state: "loading" }, async () => { - let result = await asyncTryOrPropogate(() => executeList(query, context.index, sourcePath, context.settings)); - if (!result.successful) - return { state: "error", error: result.error, sourcePath }; - return { state: "ready", items: result.value.data }; - }); - if (items.state == "loading") - return (v$1(d$1, null, - v$1(ErrorPre, null, "Loading..."))); - else if (items.state == "error") - return (v$1(d$1, null, - " ", - v$1(ErrorPre, null, - "Dataview: ", - items.error), - " ")); - if (items.items.length == 0 && context.settings.warnOnEmptyResult) - return v$1(ErrorMessage, { message: "Dataview: No results to show for list query." }); - return v$1(ListGrouping, { items: items.items, sourcePath: sourcePath }); -} -function createListView(init, query, sourcePath) { - return new ReactRenderer(init, v$1(ListView, { query: query, sourcePath: sourcePath })); -} -function createFixedListView(init, elements, sourcePath) { - return new ReactRenderer(init, v$1(ListGrouping, { items: elements, sourcePath: sourcePath })); -} - -/** JSX component which returns the result count. */ -function ResultCount(props) { - const { settings } = F$1(DataviewContext); - return settings.showResultCount ? v$1("span", { class: "dataview small-text" }, props.length) : v$1(d$1, null); -} -/** Simple table over headings and corresponding values. */ -function TableGrouping({ headings, values, sourcePath, }) { - let settings = F$1(DataviewContext).settings; - return (v$1(d$1, null, - v$1("table", { class: "dataview table-view-table" }, - v$1("thead", { class: "table-view-thead" }, - v$1("tr", { class: "table-view-tr-header" }, headings.map((heading, index) => (v$1("th", { class: "table-view-th" }, - v$1(Markdown, { sourcePath: sourcePath, content: heading }), - index == 0 && v$1(ResultCount, { length: values.length })))))), - v$1("tbody", { class: "table-view-tbody" }, values.map(row => (v$1("tr", null, row.map(element => (v$1("td", null, - v$1(Lit, { value: element, sourcePath: sourcePath }))))))))), - settings.warnOnEmptyResult && values.length == 0 && (v$1(ErrorMessage, { message: "Dataview: No results to show for table query." })))); -} -/** Pure view over list elements. */ -function TableView({ query, sourcePath }) { - let context = F$1(DataviewContext); - let items = useIndexBackedState(context.container, context.app, context.settings, context.index, { state: "loading" }, async () => { - let result = await asyncTryOrPropogate(() => executeTable(query, context.index, sourcePath, context.settings)); - if (!result.successful) - return { state: "error", error: result.error }; - return { state: "ready", headings: result.value.names, values: result.value.data }; - }); - if (items.state == "loading") - return (v$1(d$1, null, - v$1(ErrorPre, null, "Loading..."))); - else if (items.state == "error") - return (v$1(d$1, null, - " ", - v$1(ErrorPre, null, - "Dataview: ", - items.error), - " ")); - return v$1(TableGrouping, { headings: items.headings, values: items.values, sourcePath: sourcePath }); -} -function createTableView(init, query, sourcePath) { - return new ReactRenderer(init, v$1(TableView, { query: query, sourcePath: sourcePath })); -} -function createFixedTableView(init, headings, values, sourcePath) { - return new ReactRenderer(init, v$1(TableGrouping, { values: values, headings: headings, sourcePath: sourcePath })); -} - -/** Utility functions for quickly creating fields. */ -var QueryFields; -(function (QueryFields) { - function named(name, field) { - return { name, field }; - } - QueryFields.named = named; - function sortBy(field, dir) { - return { field, direction: dir }; - } - QueryFields.sortBy = sortBy; -})(QueryFields || (QueryFields = {})); - -/** Return a new parser which executes the underlying parser and returns it's raw string representation. */ -function captureRaw(base) { - return parsimmon_umd_min.exports.custom((success, failure) => { - return (input, i) => { - let result = base._(input, i); - if (!result.status) - return result; - return Object.assign({}, result, { value: [result.value, input.substring(i, result.index)] }); - }; - }); -} -/** Strip newlines and excess whitespace out of text. */ -function stripNewlines(text) { - return text - .split(/[\r\n]+/) - .map(t => t.trim()) - .join(""); -} -/** A parsimmon-powered parser-combinator implementation of the query language. */ -const QUERY_LANGUAGE = parsimmon_umd_min.exports.createLanguage({ - // Simple atom parsing, like words, identifiers, numbers. - queryType: q => parsimmon_umd_min.exports.alt(parsimmon_umd_min.exports.regexp(/TABLE|LIST|TASK|CALENDAR/i)) - .map(str => str.toLowerCase()) - .desc("query type ('TABLE', 'LIST', 'TASK', or 'CALENDAR')"), - explicitNamedField: q => parsimmon_umd_min.exports.seqMap(EXPRESSION.field.skip(parsimmon_umd_min.exports.whitespace), parsimmon_umd_min.exports.regexp(/AS/i).skip(parsimmon_umd_min.exports.whitespace), EXPRESSION.identifier.or(EXPRESSION.string), (field, _as, ident) => QueryFields.named(ident, field)), - namedField: q => parsimmon_umd_min.exports.alt(q.explicitNamedField, captureRaw(EXPRESSION.field).map(([value, text]) => QueryFields.named(stripNewlines(text), value))), - sortField: q => parsimmon_umd_min.exports.seqMap(EXPRESSION.field.skip(parsimmon_umd_min.exports.optWhitespace), parsimmon_umd_min.exports.regexp(/ASCENDING|DESCENDING|ASC|DESC/i).atMost(1), (field, dir) => { - let direction = dir.length == 0 ? "ascending" : dir[0].toLowerCase(); - if (direction == "desc") - direction = "descending"; - if (direction == "asc") - direction = "ascending"; - return { - field: field, - direction: direction, - }; - }), - headerClause: q => q.queryType - .skip(parsimmon_umd_min.exports.whitespace) - .chain(qtype => { - switch (qtype) { - case "table": - return parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/WITHOUT\s+ID/i) - .skip(parsimmon_umd_min.exports.optWhitespace) - .atMost(1), parsimmon_umd_min.exports.sepBy(q.namedField, parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace)), (withoutId, fields) => { - return { type: "table", fields, showId: withoutId.length == 0 }; - }); - case "list": - return parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/WITHOUT\s+ID/i) - .skip(parsimmon_umd_min.exports.optWhitespace) - .atMost(1), EXPRESSION.field.atMost(1), (withoutId, format) => { - return { - type: "list", - format: format.length == 1 ? format[0] : undefined, - showId: withoutId.length == 0, - }; - }); - case "task": - return parsimmon_umd_min.exports.succeed({ type: "task" }); - case "calendar": - return parsimmon_umd_min.exports.seqMap(q.namedField, field => { - return { - type: "calendar", - showId: true, - field, - }; - }); - default: - return parsimmon_umd_min.exports.fail(`Unrecognized query type '${qtype}'`); - } - }) - .desc("TABLE or LIST or TASK or CALENDAR"), - fromClause: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/FROM/i), parsimmon_umd_min.exports.whitespace, EXPRESSION.source, (_1, _2, source) => source), - whereClause: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/WHERE/i), parsimmon_umd_min.exports.whitespace, EXPRESSION.field, (where, _, field) => { - return { type: "where", clause: field }; - }).desc("WHERE "), - sortByClause: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/SORT/i), parsimmon_umd_min.exports.whitespace, q.sortField.sepBy1(parsimmon_umd_min.exports.string(",").trim(parsimmon_umd_min.exports.optWhitespace)), (sort, _1, fields) => { - return { type: "sort", fields }; - }).desc("SORT field [ASC/DESC]"), - limitClause: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/LIMIT/i), parsimmon_umd_min.exports.whitespace, EXPRESSION.field, (limit, _1, field) => { - return { type: "limit", amount: field }; - }).desc("LIMIT "), - flattenClause: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/FLATTEN/i).skip(parsimmon_umd_min.exports.whitespace), q.namedField, (_, field) => { - return { type: "flatten", field }; - }).desc("FLATTEN [AS ]"), - groupByClause: q => parsimmon_umd_min.exports.seqMap(parsimmon_umd_min.exports.regexp(/GROUP BY/i).skip(parsimmon_umd_min.exports.whitespace), q.namedField, (_, field) => { - return { type: "group", field }; - }).desc("GROUP BY [AS ]"), - // Full query parsing. - clause: q => parsimmon_umd_min.exports.alt(q.fromClause, q.whereClause, q.sortByClause, q.limitClause, q.groupByClause, q.flattenClause), - query: q => parsimmon_umd_min.exports.seqMap(q.headerClause.trim(parsimmon_umd_min.exports.optWhitespace), q.fromClause.trim(parsimmon_umd_min.exports.optWhitespace).atMost(1), q.clause.trim(parsimmon_umd_min.exports.optWhitespace).many(), (header, from, clauses) => { - return { - header, - source: from.length == 0 ? Sources.folder("") : from[0], - operations: clauses, - settings: DEFAULT_QUERY_SETTINGS, - }; - }), -}); -/** - * Attempt to parse a query from the given query text, returning a string error - * if the parse failed. - */ -function parseQuery(text) { - try { - let query = QUERY_LANGUAGE.query.tryParse(text); - return Result.success(query); - } - catch (error) { - return Result.failure("" + error); - } +var n,l$1,u$1,i$1,o$1,r$1,f$1,e$1,c$1={},s$1=[],a$1=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i,h$1=Array.isArray;function v$1(n,l){for(var u in l)n[u]=l[u];return n}function p$1(n){var l=n.parentNode;l&&l.removeChild(n);}function y$1(l,u,t){var i,o,r,f={};for(r in u)"key"==r?i=u[r]:"ref"==r?o=u[r]:f[r]=u[r];if(arguments.length>2&&(f.children=arguments.length>3?n.call(arguments,2):t),"function"==typeof l&&null!=l.defaultProps)for(r in l.defaultProps)void 0===f[r]&&(f[r]=l.defaultProps[r]);return d$1(l,f,i,o,null)}function d$1(n,t,i,o,r){var f={type:n,props:t,key:i,ref:o,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,__h:null,constructor:void 0,__v:null==r?++u$1:r};return null==r&&null!=l$1.vnode&&l$1.vnode(f),f}function _$1(){return {current:null}}function k$2(n){return n.children}function b$1(n,l){this.props=n,this.context=l;}function g$2(n,l){if(null==l)return n.__?g$2(n.__,n.__.__k.indexOf(n)+1):null;for(var u;ll&&i$1.sort(f$1));x$2.__r=0;}function P$1(n,l,u,t,i,o,r,f,e,a,v){var p,y,_,b,m,w,x,P,C,H=0,I=t&&t.__k||s$1,T=I.length,j=T,z=l.length;for(u.__k=[],p=0;p0?d$1(b.type,b.props,b.key,b.ref?b.ref:null,b.__v):b)?(b.__=u,b.__b=u.__b+1,-1===(P=A$2(b,I,x=p+H,j))?_=c$1:(_=I[P]||c$1,I[P]=void 0,j--),L$1(n,b,_,i,o,r,f,e,a,v),m=b.__e,(y=b.ref)&&_.ref!=y&&(_.ref&&O$1(_.ref,null,b),v.push(y,b.__c||m,b)),null!=m&&(null==w&&(w=m),(C=_===c$1||null===_.__v)?-1==P&&H--:P!==x&&(P===x+1?H++:P>x?j>z-x?H+=P-x:H--:H=P(null!=e?1:0))for(;r>=0||f=0){if((e=l[r])&&i==e.key&&o===e.type)return r;r--;}if(f2&&(e.children=arguments.length>3?n.call(arguments,2):t),d$1(l.type,e,i||l.key,o||l.ref,null)}function G$1(n,l){var u={__c:l="__cC"+e$1++,__:n,Consumer:function(n,l){return n.children(l)},Provider:function(n){var u,t;return this.getChildContext||(u=[],(t={})[l]=this,this.getChildContext=function(){return t},this.shouldComponentUpdate=function(n){this.props.value!==n.value&&u.some(function(n){n.__e=!0,w$2(n);});},this.sub=function(n){u.push(n);var l=n.componentWillUnmount;n.componentWillUnmount=function(){u.splice(u.indexOf(n),1),l&&l.call(n);};}),n.children}};return u.Provider.__=u.Consumer.contextType=u}n=s$1.slice,l$1={__e:function(n,l,u,t){for(var i,o,r;l=l.__;)if((i=l.__c)&&!i.__)try{if((o=i.constructor)&&null!=o.getDerivedStateFromError&&(i.setState(o.getDerivedStateFromError(n)),r=i.__d),null!=i.componentDidCatch&&(i.componentDidCatch(n,t||{}),r=i.__d),r)return i.__E=i}catch(l){n=l;}throw n}},u$1=0,b$1.prototype.setState=function(n,l){var u;u=null!=this.__s&&this.__s!==this.state?this.__s:this.__s=v$1({},this.state),"function"==typeof n&&(n=n(v$1({},u),this.props)),n&&v$1(u,n),null!=n&&this.__v&&(l&&this._sb.push(l),w$2(this));},b$1.prototype.forceUpdate=function(n){this.__v&&(this.__e=!0,n&&this.__h.push(n),w$2(this));},b$1.prototype.render=k$2,i$1=[],r$1="function"==typeof Promise?Promise.prototype.then.bind(Promise.resolve()):setTimeout,f$1=function(n,l){return n.__v.__b-l.__v.__b},x$2.__r=0,e$1=0; + +var t,r,u,i,o=0,f=[],c=[],e=l$1.__b,a=l$1.__r,v=l$1.diffed,l=l$1.__c,m=l$1.unmount;function d(t,u){l$1.__h&&l$1.__h(r,t,o||u),o=0;var i=r.__H||(r.__H={__:[],__h:[]});return t>=i.__.length&&i.__.push({__V:c}),i.__[t]}function h(n){return o=1,s(B$1,n)}function s(n,u,i){var o=d(t++,2);if(o.t=n,!o.__c&&(o.__=[i?i(u):B$1(void 0,u),function(n){var t=o.__N?o.__N[0]:o.__[0],r=o.t(t,n);t!==r&&(o.__N=[r,o.__[1]],o.__c.setState({}));}],o.__c=r,!r.u)){var f=function(n,t,r){if(!o.__c.__H)return !0;var u=o.__c.__H.__.filter(function(n){return n.__c});if(u.every(function(n){return !n.__N}))return !c||c.call(this,n,t,r);var i=!1;return u.forEach(function(n){if(n.__N){var t=n.__[0];n.__=n.__N,n.__N=void 0,t!==n.__[0]&&(i=!0);}}),!(!i&&o.__c.props===n)&&(!c||c.call(this,n,t,r))};r.u=!0;var c=r.shouldComponentUpdate,e=r.componentWillUpdate;r.componentWillUpdate=function(n,t,r){if(this.__e){var u=c;c=void 0,f(n,t,r),c=u;}e&&e.call(this,n,t,r);},r.shouldComponentUpdate=f;}return o.__N||o.__}function p(u,i){var o=d(t++,3);!l$1.__s&&z$1(o.__H,i)&&(o.__=u,o.i=i,r.__H.__h.push(o));}function y(u,i){var o=d(t++,4);!l$1.__s&&z$1(o.__H,i)&&(o.__=u,o.i=i,r.__h.push(o));}function _(n){return o=5,F$1(function(){return {current:n}},[])}function A$1(n,t,r){o=6,y(function(){return "function"==typeof n?(n(t()),function(){return n(null)}):n?(n.current=t(),function(){return n.current=null}):void 0},null==r?r:r.concat(n));}function F$1(n,r){var u=d(t++,7);return z$1(u.__H,r)?(u.__V=n(),u.i=r,u.__h=n,u.__V):u.__}function T$1(n,t){return o=8,F$1(function(){return n},t)}function q$1(n){var u=r.context[n.__c],i=d(t++,9);return i.c=n,u?(null==i.__&&(i.__=!0,u.sub(r)),u.props.value):n.__}function x$1(t,r){l$1.useDebugValue&&l$1.useDebugValue(r?r(t):t);}function V$1(){var n=d(t++,11);if(!n.__){for(var u=r.__v;null!==u&&!u.__m&&null!==u.__;)u=u.__;var i=u.__m||(u.__m=[0,0]);n.__="P"+i[0]+"-"+i[1]++;}return n.__}function b(){for(var t;t=f.shift();)if(t.__P&&t.__H)try{t.__H.__h.forEach(k$1),t.__H.__h.forEach(w$1),t.__H.__h=[];}catch(r){t.__H.__h=[],l$1.__e(r,t.__v);}}l$1.__b=function(n){r=null,e&&e(n);},l$1.__r=function(n){a&&a(n),t=0;var i=(r=n.__c).__H;i&&(u===r?(i.__h=[],r.__h=[],i.__.forEach(function(n){n.__N&&(n.__=n.__N),n.__V=c,n.__N=n.i=void 0;})):(i.__h.forEach(k$1),i.__h.forEach(w$1),i.__h=[],t=0)),u=r;},l$1.diffed=function(t){v&&v(t);var o=t.__c;o&&o.__H&&(o.__H.__h.length&&(1!==f.push(o)&&i===l$1.requestAnimationFrame||((i=l$1.requestAnimationFrame)||j$1)(b)),o.__H.__.forEach(function(n){n.i&&(n.__H=n.i),n.__V!==c&&(n.__=n.__V),n.i=void 0,n.__V=c;})),u=r=null;},l$1.__c=function(t,r){r.some(function(t){try{t.__h.forEach(k$1),t.__h=t.__h.filter(function(n){return !n.__||w$1(n)});}catch(u){r.some(function(n){n.__h&&(n.__h=[]);}),r=[],l$1.__e(u,t.__v);}}),l&&l(t,r);},l$1.unmount=function(t){m&&m(t);var r,u=t.__c;u&&u.__H&&(u.__H.__.forEach(function(n){try{k$1(n);}catch(n){r=n;}}),u.__H=void 0,r&&l$1.__e(r,u.__v));};var g$1="function"==typeof requestAnimationFrame;function j$1(n){var t,r=function(){clearTimeout(u),g$1&&cancelAnimationFrame(t),setTimeout(n);},u=setTimeout(r,100);g$1&&(t=requestAnimationFrame(r));}function k$1(n){var t=r,u=n.__c;"function"==typeof u&&(n.__c=void 0,u()),r=t;}function w$1(n){var t=r;n.__c=n.__(),r=t;}function z$1(n,t){return !n||n.length!==t.length||t.some(function(t,r){return t!==n[r]})}function B$1(n,t){return "function"==typeof t?t(n):t} + +function g(n,t){for(var e in t)n[e]=t[e];return n}function C(n,t){for(var e in n)if("__source"!==e&&!(e in t))return !0;for(var r in t)if("__source"!==r&&n[r]!==t[r])return !0;return !1}function E(n,t){return n===t&&(0!==n||1/n==1/t)||n!=n&&t!=t}function w(n){this.props=n;}function x(n,e){function r(n){var t=this.props.ref,r=t==n.ref;return !r&&t&&(t.call?t(null):t.current=null),e?!e(this.props,n)||!r:C(this.props,n)}function u(e){return this.shouldComponentUpdate=r,y$1(n,e)}return u.displayName="Memo("+(n.displayName||n.name)+")",u.prototype.isReactComponent=!0,u.__f=!0,u}(w.prototype=new b$1).isPureReactComponent=!0,w.prototype.shouldComponentUpdate=function(n,t){return C(this.props,n)||C(this.state,t)};var R=l$1.__b;l$1.__b=function(n){n.type&&n.type.__f&&n.ref&&(n.props.ref=n.ref,n.ref=null),R&&R(n);};var N="undefined"!=typeof Symbol&&Symbol.for&&Symbol.for("react.forward_ref")||3911;function k(n){function t(t){var e=g({},t);return delete e.ref,n(e,t.ref||null)}return t.$$typeof=N,t.render=t,t.prototype.isReactComponent=t.__f=!0,t.displayName="ForwardRef("+(n.displayName||n.name)+")",t}var A=function(n,t){return null==n?null:C$1(C$1(n).map(t))},O={map:A,forEach:A,count:function(n){return n?C$1(n).length:0},only:function(n){var t=C$1(n);if(1!==t.length)throw "Children.only";return t[0]},toArray:C$1},T=l$1.__e;l$1.__e=function(n,t,e,r){if(n.then)for(var u,o=t;o=o.__;)if((u=o.__c)&&u.__c)return null==t.__e&&(t.__e=e.__e,t.__k=e.__k),u.__c(n,t);T(n,t,e,r);};var F=l$1.unmount;function I(n,t,e){return n&&(n.__c&&n.__c.__H&&(n.__c.__H.__.forEach(function(n){"function"==typeof n.__c&&n.__c();}),n.__c.__H=null),null!=(n=g({},n)).__c&&(n.__c.__P===e&&(n.__c.__P=t),n.__c=null),n.__k=n.__k&&n.__k.map(function(n){return I(n,t,e)})),n}function L(n,t,e){return n&&(n.__v=null,n.__k=n.__k&&n.__k.map(function(n){return L(n,t,e)}),n.__c&&n.__c.__P===t&&(n.__e&&e.insertBefore(n.__e,n.__d),n.__c.__e=!0,n.__c.__P=e)),n}function U(){this.__u=0,this.t=null,this.__b=null;}function D(n){var t=n.__.__c;return t&&t.__a&&t.__a(n)}function M(n){var e,r,u;function o(o){if(e||(e=n()).then(function(n){r=n.default||n;},function(n){u=n;}),u)throw u;if(!r)throw e;return y$1(r,o)}return o.displayName="Lazy",o.__f=!0,o}function V(){this.u=null,this.o=null;}l$1.unmount=function(n){var t=n.__c;t&&t.__R&&t.__R(),t&&!0===n.__h&&(n.type=null),F&&F(n);},(U.prototype=new b$1).__c=function(n,t){var e=t.__c,r=this;null==r.t&&(r.t=[]),r.t.push(e);var u=D(r.__v),o=!1,i=function(){o||(o=!0,e.__R=null,u?u(l):l());};e.__R=i;var l=function(){if(!--r.__u){if(r.state.__a){var n=r.state.__a;r.__v.__k[0]=L(n,n.__c.__P,n.__c.__O);}var t;for(r.setState({__a:r.__b=null});t=r.t.pop();)t.forceUpdate();}},c=!0===t.__h;r.__u++||c||r.setState({__a:r.__b=r.__v.__k[0]}),n.then(i,i);},U.prototype.componentWillUnmount=function(){this.t=[];},U.prototype.render=function(n,e){if(this.__b){if(this.__v.__k){var r=document.createElement("div"),o=this.__v.__k[0].__c;this.__v.__k[0]=I(this.__b,r,o.__O=o.__P);}this.__b=null;}var i=e.__a&&y$1(k$2,null,n.fallback);return i&&(i.__h=null),[y$1(k$2,null,e.__a?null:n.children),i]};var W=function(n,t,e){if(++e[1]===e[0]&&n.o.delete(t),n.props.revealOrder&&("t"!==n.props.revealOrder[0]||!n.o.size))for(e=n.u;e;){for(;e.length>3;)e.pop()();if(e[1]>>1,1),e.i.removeChild(n);}}),D$1(y$1(P,{context:e.context},n.__v),e.l);}function z(n,e){var r=y$1(j,{__v:n,i:e});return r.containerInfo=e,r}(V.prototype=new b$1).__a=function(n){var t=this,e=D(t.__v),r=t.o.get(n);return r[0]++,function(u){var o=function(){t.props.revealOrder?(r.push(u),W(t,n,r)):u();};e?e(o):o();}},V.prototype.render=function(n){this.u=null,this.o=new Map;var t=C$1(n.children);n.revealOrder&&"b"===n.revealOrder[0]&&t.reverse();for(var e=t.length;e--;)this.o.set(t[e],this.u=[1,0,this.u]);return n.children},V.prototype.componentDidUpdate=V.prototype.componentDidMount=function(){var n=this;this.o.forEach(function(t,e){W(n,e,t);});};var B="undefined"!=typeof Symbol&&Symbol.for&&Symbol.for("react.element")||60103,H=/^(?:accent|alignment|arabic|baseline|cap|clip(?!PathU)|color|dominant|fill|flood|font|glyph(?!R)|horiz|image(!S)|letter|lighting|marker(?!H|W|U)|overline|paint|pointer|shape|stop|strikethrough|stroke|text(?!L)|transform|underline|unicode|units|v|vector|vert|word|writing|x(?!C))[A-Z]/,Z=/^on(Ani|Tra|Tou|BeforeInp|Compo)/,Y=/[A-Z0-9]/g,$="undefined"!=typeof document,q=function(n){return ("undefined"!=typeof Symbol&&"symbol"==typeof Symbol()?/fil|che|rad/:/fil|che|ra/).test(n)};function G(n,t,e){return null==t.__k&&(t.textContent=""),D$1(n,t),"function"==typeof e&&e(),n?n.__c:null}function J(n,t,e){return E$1(n,t),"function"==typeof e&&e(),n?n.__c:null}b$1.prototype.isReactComponent={},["componentWillMount","componentWillReceiveProps","componentWillUpdate"].forEach(function(t){Object.defineProperty(b$1.prototype,t,{configurable:!0,get:function(){return this["UNSAFE_"+t]},set:function(n){Object.defineProperty(this,t,{configurable:!0,writable:!0,value:n});}});});var K=l$1.event;function Q(){}function X(){return this.cancelBubble}function nn(){return this.defaultPrevented}l$1.event=function(n){return K&&(n=K(n)),n.persist=Q,n.isPropagationStopped=X,n.isDefaultPrevented=nn,n.nativeEvent=n};var tn,en={enumerable:!1,configurable:!0,get:function(){return this.class}},rn=l$1.vnode;l$1.vnode=function(n){"string"==typeof n.type&&function(n){var t=n.props,e=n.type,u={};for(var o in t){var i=t[o];if(!("value"===o&&"defaultValue"in t&&null==i||$&&"children"===o&&"noscript"===e||"class"===o||"className"===o)){var l=o.toLowerCase();"defaultValue"===o&&"value"in t&&null==t.value?o="value":"download"===o&&!0===i?i="":"ondoubleclick"===l?o="ondblclick":"onchange"!==l||"input"!==e&&"textarea"!==e||q(t.type)?"onfocus"===l?o="onfocusin":"onblur"===l?o="onfocusout":Z.test(o)?o=l:-1===e.indexOf("-")&&H.test(o)?o=o.replace(Y,"-$&").toLowerCase():null===i&&(i=void 0):l=o="oninput","oninput"===l&&u[o=l]&&(o="oninputCapture"),u[o]=i;}}"select"==e&&u.multiple&&Array.isArray(u.value)&&(u.value=C$1(t.children).forEach(function(n){n.props.selected=-1!=u.value.indexOf(n.props.value);})),"select"==e&&null!=u.defaultValue&&(u.value=C$1(t.children).forEach(function(n){n.props.selected=u.multiple?-1!=u.defaultValue.indexOf(n.props.value):u.defaultValue==n.props.value;})),t.class&&!t.className?(u.class=t.class,Object.defineProperty(u,"className",en)):(t.className&&!t.class||t.class&&t.className)&&(u.class=u.className=t.className),n.props=u;}(n),n.$$typeof=B,rn&&rn(n);};var un=l$1.__r;l$1.__r=function(n){un&&un(n),tn=n.__c;};var on=l$1.diffed;l$1.diffed=function(n){on&&on(n);var t=n.props,e=n.__e;null!=e&&"textarea"===n.type&&"value"in t&&t.value!==e.value&&(e.value=null==t.value?"":t.value),tn=null;};var ln={ReactCurrentDispatcher:{current:{readContext:function(n){return tn.__n[n.__c].props.value}}}};function fn(n){return y$1.bind(null,n)}function an(n){return !!n&&n.$$typeof===B}function sn(n){return an(n)&&n.type===k$2}function hn(n){return an(n)?F$2.apply(null,arguments):n}function vn(n){return !!n.__k&&(D$1(null,n),!0)}function dn(n){return n&&(n.base||1===n.nodeType&&n)||null}var pn=function(n,t){return n(t)},mn=function(n,t){return n(t)},yn=k$2;function _n(n){n();}function bn(n){return n}function Sn(){return [!1,_n]}var gn=y,Cn=an;function En(n,t){var e=t(),r=h({h:{__:e,v:t}}),u=r[0].h,o=r[1];return y(function(){u.__=e,u.v=t,E(u.__,t())||o({h:u});},[n,e,t]),p(function(){return E(u.__,u.v())||o({h:u}),n(function(){E(u.__,u.v())||o({h:u});})},[n]),e}var wn={useState:h,useId:V$1,useReducer:s,useEffect:p,useLayoutEffect:y,useInsertionEffect:gn,useTransition:Sn,useDeferredValue:bn,useSyncExternalStore:En,startTransition:_n,useRef:_,useImperativeHandle:A$1,useMemo:F$1,useCallback:T$1,useContext:q$1,useDebugValue:x$1,version:"17.0.2",Children:O,render:G,hydrate:J,unmountComponentAtNode:vn,createPortal:z,createElement:y$1,createContext:G$1,createFactory:fn,cloneElement:hn,createRef:_$1,Fragment:k$2,isValidElement:an,isElement:Cn,isFragment:sn,findDOMNode:dn,Component:b$1,PureComponent:w,memo:x,forwardRef:k,flushSync:mn,unstable_batchedUpdates:pn,StrictMode:yn,Suspense:U,SuspenseList:V,lazy:M,__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED:ln}; + +const IMAGE_EXTENSIONS = Object.freeze(new Set([ + ".tif", + ".tiff", + ".gif", + ".png", + ".apng", + ".avif", + ".jpg", + ".jpeg", + ".jfif", + ".pjepg", + ".pjp", + ".svg", + ".webp", + ".bmp", + ".ico", + ".cur", +])); +/** Determines if the given link points to an embedded image. */ +function isImageEmbed(link) { + if (!link.path.contains(".")) + return false; + let extension = link.path.substring(link.path.lastIndexOf(".")); + return link.type == "file" && link.embed && IMAGE_EXTENSIONS.has(extension); +} +/** Extract text of the form 'WxH' or 'W' from the display of a link. */ +function extractImageDimensions(link) { + if (!link.display) + return undefined; + let match = /^(\d+)x(\d+)$/iu.exec(link.display); + if (match) + return [parseInt(match[1]), parseInt(match[2])]; + let match2 = /^(\d+)/.exec(link.display); + if (match2) + return [parseInt(match2[1])]; + // No match. + return undefined; } -function noop() { } -function assign(tar, src) { - // @ts-ignore - for (const k in src) - tar[k] = src[k]; - return tar; -} -function is_promise(value) { - return value && typeof value === 'object' && typeof value.then === 'function'; -} -function run(fn) { - return fn(); -} -function blank_object() { - return Object.create(null); -} -function run_all(fns) { - fns.forEach(run); -} -function is_function(thing) { - return typeof thing === 'function'; -} -function safe_not_equal(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} -function not_equal(a, b) { - return a != a ? b == b : a !== b; -} -function is_empty(obj) { +/** Provides core preact / rendering utilities for all view types. */ +const DataviewContext = G$1(undefined); +/** Hacky preact component which wraps Obsidian's markdown renderer into a neat component. */ +function RawMarkdown({ content, sourcePath, inline = true, style, cls, onClick, }) { + const container = _(null); + const component = q$1(DataviewContext).component; + p(() => { + if (!container.current) + return; + container.current.innerHTML = ""; + obsidian.MarkdownRenderer.renderMarkdown(content, container.current, sourcePath, component).then(() => { + if (!container.current || !inline) + return; + // Unwrap any created paragraph elements if we are inline. + let paragraph = container.current.querySelector("p"); + while (paragraph) { + let children = paragraph.childNodes; + paragraph.replaceWith(...Array.from(children)); + paragraph = container.current.querySelector("p"); + } + }); + }, [content, sourcePath, container.current]); + return y$1("span", { ref: container, style: style, class: cls, onClick: onClick }); +} +/** Hacky preact component which wraps Obsidian's markdown renderer into a neat component. */ +const Markdown = wn.memo(RawMarkdown); +/** Embeds an HTML element in the react DOM. */ +function RawEmbedHtml({ element }) { + const container = _(null); + p(() => { + if (!container.current) + return; + container.current.innerHTML = ""; + container.current.appendChild(element); + }, [container.current, element]); + return y$1("span", { ref: container }); +} +/** Embeds an HTML element in the react DOM. */ +const EmbedHtml = wn.memo(RawEmbedHtml); +/** Intelligently render an arbitrary literal value. */ +function RawLit({ value, sourcePath, inline = false, depth = 0, }) { + var _a, _b, _c; + const context = q$1(DataviewContext); + // Short-circuit if beyond the maximum render depth. + if (depth >= context.settings.maxRecursiveRenderDepth) + return y$1(k$2, null, "..."); + if (Values.isNull(value) || value === undefined) { + return y$1(Markdown, { content: context.settings.renderNullAs, sourcePath: sourcePath }); + } + else if (Values.isString(value)) { + return y$1(Markdown, { content: value, sourcePath: sourcePath }); + } + else if (Values.isNumber(value)) { + return y$1(k$2, null, "" + value); + } + else if (Values.isBoolean(value)) { + return y$1(k$2, null, "" + value); + } + else if (Values.isDate(value)) { + return y$1(k$2, null, renderMinimalDate(value, context.settings, currentLocale())); + } + else if (Values.isDuration(value)) { + return y$1(k$2, null, renderMinimalDuration(value)); + } + else if (Values.isLink(value)) { + // Special case handling of image/video/etc embeddings to bypass the Obsidian API not working. + if (isImageEmbed(value)) { + let realFile = context.app.metadataCache.getFirstLinkpathDest(value.path, sourcePath); + if (!realFile) + return y$1(Markdown, { content: value.markdown(), sourcePath: sourcePath }); + let dimensions = extractImageDimensions(value); + let resourcePath = context.app.vault.getResourcePath(realFile); + if (dimensions && dimensions.length == 2) + return y$1("img", { alt: value.path, src: resourcePath, width: dimensions[0], height: dimensions[1] }); + else if (dimensions && dimensions.length == 1) + return y$1("img", { alt: value.path, src: resourcePath, width: dimensions[0] }); + else + return y$1("img", { alt: value.path, src: resourcePath }); + } + return y$1(Markdown, { content: value.markdown(), sourcePath: sourcePath }); + } + else if (Values.isHtml(value)) { + return y$1(EmbedHtml, { element: value }); + } + else if (Values.isWidget(value)) { + if (Widgets.isListPair(value)) { + return (y$1(k$2, null, + y$1(Lit, { value: value.key, sourcePath: sourcePath }), + ":", + " ", + y$1(Lit, { value: value.value, sourcePath: sourcePath }))); + } + else if (Widgets.isExternalLink(value)) { + return (y$1("a", { href: value.url, rel: "noopener", target: "_blank", class: "external-link" }, (_a = value.display) !== null && _a !== void 0 ? _a : value.url)); + } + else { + return y$1("b", null, + ""); + } + } + else if (Values.isFunction(value)) { + return y$1(k$2, null, ""); + } + else if (Values.isArray(value) || DataArray.isDataArray(value)) { + if (!inline) { + return (y$1("ul", { class: "dataview dataview-ul dataview-result-list-ul" }, value.map(subvalue => (y$1("li", { class: "dataview-result-list-li" }, + y$1(Lit, { value: subvalue, sourcePath: sourcePath, inline: inline, depth: depth + 1 })))))); + } + else { + if (value.length == 0) + return y$1(k$2, null, ""); + return (y$1("span", { class: "dataview dataview-result-list-span" }, value.map((subvalue, index) => (y$1(k$2, null, + index == 0 ? "" : ", ", + y$1(Lit, { value: subvalue, sourcePath: sourcePath, inline: inline, depth: depth + 1 })))))); + } + } + else if (Values.isObject(value)) { + // Don't render classes in case they have recursive references; spoopy. + if (((_b = value === null || value === void 0 ? void 0 : value.constructor) === null || _b === void 0 ? void 0 : _b.name) && ((_c = value === null || value === void 0 ? void 0 : value.constructor) === null || _c === void 0 ? void 0 : _c.name) != "Object") { + return y$1(k$2, null, + "<", + value.constructor.name, + ">"); + } + if (!inline) { + return (y$1("ul", { class: "dataview dataview-ul dataview-result-object-ul" }, Object.entries(value).map(([key, value]) => (y$1("li", { class: "dataview dataview-li dataview-result-object-li" }, + key, + ": ", + y$1(Lit, { value: value, sourcePath: sourcePath, inline: inline, depth: depth + 1 })))))); + } + else { + if (Object.keys(value).length == 0) + return y$1(k$2, null, ""); + return (y$1("span", { class: "dataview dataview-result-object-span" }, Object.entries(value).map(([key, value], index) => (y$1(k$2, null, + index == 0 ? "" : ", ", + key, + ": ", + y$1(Lit, { value: value, sourcePath: sourcePath, inline: inline, depth: depth + 1 })))))); + } + } + return y$1(k$2, null, + ""); +} +/** Intelligently render an arbitrary literal value. */ +const Lit = wn.memo(RawLit); +/** Render a simple nice looking error box in a code style. */ +function ErrorPre(props, {}) { + return y$1("pre", { class: "dataview dataview-error" }, props.children); +} +/** Render a pretty centered error message in a box. */ +function ErrorMessage({ message }) { + return (y$1("div", { class: "dataview dataview-error-box" }, + y$1("p", { class: "dataview dataview-error-message" }, message))); +} +/** + * Complex convienence hook which calls `compute` every time the index updates, updating the current state. + */ +function useIndexBackedState(container, app, settings, index, initial, compute) { + let [initialized, setInitialized] = h(false); + let [state, updateState] = h(initial); + let [lastReload, setLastReload] = h(index.revision); + // Initial setup to queue fetching the correct state. + if (!initialized) { + setLastReload(index.revision); + setInitialized(true); + compute().then(updateState); + } + // Updated on every container re-create; automatically updates state. + p(() => { + const refreshOperation = () => { + if (lastReload != index.revision && container.isShown() && settings.refreshEnabled) { + compute().then(updateState); + setLastReload(index.revision); + } + }; + // Refresh after index changes stop. + let workEvent = app.workspace.on("dataview:refresh-views", refreshOperation); + // ...or when the DOM is shown (sidebar expands, tab selected, nodes scrolled into view). + let nodeEvent = container.onNodeInserted(refreshOperation); + return () => { + app.workspace.offref(workEvent); + nodeEvent(); + }; + }, [container, lastReload]); + return state; +} +/** A trivial wrapper which allows a react component to live for the duration of a `MarkdownRenderChild`. */ +class ReactRenderer extends obsidian.MarkdownRenderChild { + constructor(init, element) { + super(init.container); + this.init = init; + this.element = element; + } + onload() { + const context = Object.assign({}, { component: this }, this.init); + D$1(y$1(DataviewContext.Provider, { value: context }, this.element), this.containerEl); + } + onunload() { + vn(this.containerEl); + } +} + +/** Function used to test if a given event correspond to a pressed link */ +function wasLinkPressed(evt) { + return evt.target != null && evt.target != undefined && evt.target.tagName == "A"; +} +/** JSX component which renders a task element recursively. */ +function TaskItem({ item }) { + var _a; + let context = q$1(DataviewContext); + // Navigate to the given task on click. + const onClicked = (evt) => { + if (wasLinkPressed(evt)) { + return; + } + evt.stopPropagation(); + const selectionState = { + eState: { + cursor: { + from: { line: item.line, ch: item.position.start.col }, + to: { line: item.line + item.lineCount - 1, ch: item.position.end.col }, + }, + line: item.line, + }, + }; + // MacOS interprets the Command key as Meta. + context.app.workspace.openLinkText(item.link.toFile().obsidianLink(), item.path, evt.ctrlKey || (evt.metaKey && obsidian.Platform.isMacOS), selectionState); + }; + // Check/uncheck the task in the original file. + const onChecked = (evt) => { + evt.stopPropagation(); + const completed = evt.currentTarget.checked; + const status = completed ? "x" : " "; + // Update data-task on the parent element (css style) + const parent = evt.currentTarget.parentElement; + parent === null || parent === void 0 ? void 0 : parent.setAttribute("data-task", status); + let flatted = [item]; + if (context.settings.recursiveSubTaskCompletion) { + function flatter(iitem) { + flatted.push(iitem); + iitem.children.forEach(flatter); + } + item.children.forEach(flatter); + flatted = flatted.flat(Infinity); + } + async function effectFn() { + for (let i = 0; i < flatted.length; i++) { + const _item = flatted[i]; + let updatedText = _item.text; + if (context.settings.taskCompletionTracking) { + updatedText = setTaskCompletion(_item.text, context.settings.taskCompletionUseEmojiShorthand, context.settings.taskCompletionText, context.settings.taskCompletionDateFormat, completed); + } + await rewriteTask(context.app.vault, _item, status, updatedText); + } + context.app.workspace.trigger("dataview:refresh-views"); + } + effectFn(); + }; + const checked = item.status !== " "; + return (y$1("li", { class: "dataview task-list-item" + (checked ? " is-checked" : ""), onClick: onClicked, "data-task": item.status }, + y$1("input", { class: "dataview task-list-item-checkbox", type: "checkbox", checked: checked, onClick: onChecked }), + y$1(Markdown, { inline: true, content: (_a = item.visual) !== null && _a !== void 0 ? _a : item.text, sourcePath: item.path }), + item.children.length > 0 && y$1(TaskList, { items: item.children }))); +} +/** JSX component which renders a plain list item recursively. */ +function ListItem({ item }) { + var _a; + let context = q$1(DataviewContext); + // Navigate to the given task on click. + const onClicked = (evt) => { + if (wasLinkPressed(evt)) { + return; + } + evt.stopPropagation(); + const selectionState = { + eState: { + cursor: { + from: { line: item.line, ch: item.position.start.col }, + to: { line: item.line + item.lineCount - 1, ch: item.position.end.col }, + }, + line: item.line, + }, + }; + // MacOS interprets the Command key as Meta. + context.app.workspace.openLinkText(item.link.toFile().obsidianLink(), item.path, evt.ctrlKey || (evt.metaKey && obsidian.Platform.isMacOS), selectionState); + }; + return (y$1("li", { class: "dataview task-list-basic-item", onClick: onClicked }, + y$1(Markdown, { inline: true, content: (_a = item.visual) !== null && _a !== void 0 ? _a : item.text, sourcePath: item.path }), + item.children.length > 0 && y$1(TaskList, { items: item.children }))); +} +/** JSX component which renders a list of task items recursively. */ +function TaskList({ items }) { + const settings = q$1(DataviewContext).settings; + if (items.length == 0 && settings.warnOnEmptyResult) + return y$1(ErrorMessage, { message: "Dataview: No results to show for task query." }); + let [nest, _mask] = nestItems(items); + return (y$1("ul", { class: "contains-task-list" }, nest.map(item => item.task ? y$1(TaskItem, { key: listId(item), item: item }) : y$1(ListItem, { key: listId(item), item: item })))); +} +/** JSX component which returns the result count. */ +function ResultCount$1(props) { + const { settings } = q$1(DataviewContext); + return settings.showResultCount ? (y$1("span", { class: "dataview small-text" }, Groupings.count(props.item.rows))) : (y$1(k$2, null)); +} +/** JSX component which recursively renders grouped tasks. */ +function TaskGrouping({ items, sourcePath }) { + const isGrouping = items.length > 0 && Groupings.isGrouping(items); + return (y$1(k$2, null, + isGrouping && + items.map(item => (y$1(k$2, { key: item.key }, + y$1("h4", null, + y$1(Lit, { value: item.key, sourcePath: sourcePath }), + y$1(ResultCount$1, { item: item })), + y$1("div", { class: "dataview result-group" }, + y$1(TaskGrouping, { items: item.rows, sourcePath: sourcePath }))))), + !isGrouping && y$1(TaskList, { items: items }))); +} +/** + * Pure view over (potentially grouped) tasks and list items which allows for checking/unchecking tasks and manipulating + * the task view. + */ +function TaskView({ query, sourcePath }) { + let context = q$1(DataviewContext); + let items = useIndexBackedState(context.container, context.app, context.settings, context.index, { state: "loading" }, async () => { + let result = await asyncTryOrPropogate(() => executeTask(query, sourcePath, context.index, context.settings)); + if (!result.successful) + return { state: "error", error: result.error, sourcePath }; + else + return { state: "ready", items: result.value.tasks }; + }); + if (items.state == "loading") + return (y$1(k$2, null, + y$1(ErrorPre, null, "Loading"))); + else if (items.state == "error") + return (y$1(k$2, null, + y$1(ErrorPre, null, + "Dataview: ", + items.error))); + return (y$1("div", { class: "dataview dataview-container" }, + y$1(TaskGrouping, { items: items.items, sourcePath: sourcePath }))); +} +function createTaskView(init, query, sourcePath) { + return new ReactRenderer(init, y$1(TaskView, { query: query, sourcePath: sourcePath })); +} +function createFixedTaskView(init, items, sourcePath) { + return new ReactRenderer(init, y$1(TaskGrouping, { items: items, sourcePath: sourcePath })); +} +///////////////////////// +// Task De-Duplication // +///////////////////////// +function listId(item) { + return item.path + ":" + item.line; +} +function parentListId(item) { + return item.path + ":" + item.parent; +} +/** Compute a map of all task IDs -> tasks. */ +function enumerateChildren(item, output) { + if (!output.has(listId(item))) + output.set(listId(item), item); + for (let child of item.children) + enumerateChildren(child, output); + return output; +} +/** Replace basic tasks with tasks from a lookup map. Retains the original order of the list. */ +function replaceChildren(elements, lookup) { + return elements.map(element => { + element.children = replaceChildren(element.children, lookup); + const id = listId(element); + const map = lookup.get(id); + if (map) + return map; + else + return element; + }); +} +/** + * Removes tasks from a list if they are already present by being a child of another task. Fixes child pointers. + * Retains original order of input list. + */ +function nestItems(raw) { + let elements = new Map(); + let mask = new Set(); + for (let elem of raw) { + let id = listId(elem); + elements.set(id, elem); + mask.add(id); + } + // List all elements & their children in the lookup map. + for (let elem of raw) + enumerateChildren(elem, elements); + let roots = raw.filter(elem => elem.parent == undefined || elem.parent == null || !elements.has(parentListId(elem))); + return [replaceChildren(roots, elements), mask]; +} +/** + * Recursively removes tasks from each subgroup if they are already present by being a child of another task. + * Fixes child pointers. Retains original order of input list. + */ +function nestGroups(raw) { + if (Groupings.isGrouping(raw)) { + return raw.map(g => { + return { key: g.key, rows: nestGroups(g.rows) }; + }); + } + else { + return nestItems(raw)[0]; + } +} +/////////////////////// +// Task Manipulation // +/////////////////////// +/** Trim empty ending lines. */ +function trimEndingLines(text) { + let parts = text.split(/\r?\n/u); + let trim = parts.length - 1; + while (trim > 0 && parts[trim].trim() == "") + trim--; + return parts.join("\n"); +} +/** Set the task completion key on check. */ +function setTaskCompletion(originalText, useEmojiShorthand, completionKey, completionDateFormat, complete) { + const blockIdRegex = /(?]*/u.exec(filetext[task.line])[0]; + if (desiredText) { + let desiredParts = desiredText.split("\n"); + let newTextLines = [`${initialSpacing}${task.symbol} [${desiredStatus}] ${desiredParts[0]}`].concat(desiredParts.slice(1).map(l => initialSpacing + "\t" + l)); + filetext.splice(task.line, task.lineCount, ...newTextLines); + } + else { + filetext[task.line] = `${initialSpacing}${task.symbol} [${desiredStatus}] ${taskTextParts[0].trim()}`; + } + let newText = filetext.join(hasRN ? "\r\n" : "\n"); + await vault.adapter.write(task.path, newText); +} + +function ListGrouping({ items, sourcePath }) { + return (y$1("ul", { class: "dataview list-view-ul" }, items.map(item => (y$1("li", null, + y$1(Lit, { value: item, sourcePath: sourcePath })))))); +} +/** Pure view over list elements. */ +function ListView({ query, sourcePath }) { + let context = q$1(DataviewContext); + let items = useIndexBackedState(context.container, context.app, context.settings, context.index, { state: "loading" }, async () => { + let result = await asyncTryOrPropogate(() => executeList(query, context.index, sourcePath, context.settings)); + if (!result.successful) + return { state: "error", error: result.error, sourcePath }; + return { state: "ready", items: result.value.data }; + }); + if (items.state == "loading") + return (y$1(k$2, null, + y$1(ErrorPre, null, "Loading..."))); + else if (items.state == "error") + return (y$1(k$2, null, + " ", + y$1(ErrorPre, null, + "Dataview: ", + items.error), + " ")); + if (items.items.length == 0 && context.settings.warnOnEmptyResult) + return y$1(ErrorMessage, { message: "Dataview: No results to show for list query." }); + return y$1(ListGrouping, { items: items.items, sourcePath: sourcePath }); +} +function createListView(init, query, sourcePath) { + return new ReactRenderer(init, y$1(ListView, { query: query, sourcePath: sourcePath })); +} +function createFixedListView(init, elements, sourcePath) { + return new ReactRenderer(init, y$1(ListGrouping, { items: elements, sourcePath: sourcePath })); +} + +/** JSX component which returns the result count. */ +function ResultCount(props) { + const { settings } = q$1(DataviewContext); + return settings.showResultCount ? y$1("span", { class: "dataview small-text" }, props.length) : y$1(k$2, null); +} +/** Simple table over headings and corresponding values. */ +function TableGrouping({ headings, values, sourcePath, }) { + let settings = q$1(DataviewContext).settings; + return (y$1(k$2, null, + y$1("table", { class: "dataview table-view-table" }, + y$1("thead", { class: "table-view-thead" }, + y$1("tr", { class: "table-view-tr-header" }, headings.map((heading, index) => (y$1("th", { class: "table-view-th" }, + y$1(Markdown, { sourcePath: sourcePath, content: heading }), + index == 0 && y$1(ResultCount, { length: values.length })))))), + y$1("tbody", { class: "table-view-tbody" }, values.map(row => (y$1("tr", null, row.map(element => (y$1("td", null, + y$1(Lit, { value: element, sourcePath: sourcePath }))))))))), + settings.warnOnEmptyResult && values.length == 0 && (y$1(ErrorMessage, { message: "Dataview: No results to show for table query." })))); +} +/** Pure view over list elements. */ +function TableView({ query, sourcePath }) { + let context = q$1(DataviewContext); + let items = useIndexBackedState(context.container, context.app, context.settings, context.index, { state: "loading" }, async () => { + let result = await asyncTryOrPropogate(() => executeTable(query, context.index, sourcePath, context.settings)); + if (!result.successful) + return { state: "error", error: result.error }; + return { state: "ready", headings: result.value.names, values: result.value.data }; + }); + if (items.state == "loading") + return (y$1(k$2, null, + y$1(ErrorPre, null, "Loading..."))); + else if (items.state == "error") + return (y$1(k$2, null, + " ", + y$1(ErrorPre, null, + "Dataview: ", + items.error), + " ")); + return y$1(TableGrouping, { headings: items.headings, values: items.values, sourcePath: sourcePath }); +} +function createTableView(init, query, sourcePath) { + return new ReactRenderer(init, y$1(TableView, { query: query, sourcePath: sourcePath })); +} +function createFixedTableView(init, headings, values, sourcePath) { + return new ReactRenderer(init, y$1(TableGrouping, { values: values, headings: headings, sourcePath: sourcePath })); +} + +/** Utility functions for quickly creating fields. */ +var QueryFields; +(function (QueryFields) { + function named(name, field) { + return { name, field }; + } + QueryFields.named = named; + function sortBy(field, dir) { + return { field, direction: dir }; + } + QueryFields.sortBy = sortBy; +})(QueryFields || (QueryFields = {})); + +/** Return a new parser which executes the underlying parser and returns it's raw string representation. */ +function captureRaw(base) { + return parsimmon_umd_minExports.custom((success, failure) => { + return (input, i) => { + let result = base._(input, i); + if (!result.status) + return result; + return Object.assign({}, result, { value: [result.value, input.substring(i, result.index)] }); + }; + }); +} +/** Strip newlines and excess whitespace out of text. */ +function stripNewlines(text) { + return text + .split(/[\r\n]+/) + .map(t => t.trim()) + .join(""); +} +/** Given `parser`, return the parser that returns `if_eof()` if EOF is found, + * otherwise `parser` preceded by (non-optional) whitespace */ +function precededByWhitespaceIfNotEof(if_eof, parser) { + return parsimmon_umd_minExports.eof.map(if_eof).or(parsimmon_umd_minExports.whitespace.then(parser)); +} +/** A parsimmon-powered parser-combinator implementation of the query language. */ +const QUERY_LANGUAGE = parsimmon_umd_minExports.createLanguage({ + // Simple atom parsing, like words, identifiers, numbers. + queryType: q => parsimmon_umd_minExports.alt(parsimmon_umd_minExports.regexp(/TABLE|LIST|TASK|CALENDAR/i)) + .map(str => str.toLowerCase()) + .desc("query type ('TABLE', 'LIST', 'TASK', or 'CALENDAR')"), + explicitNamedField: q => parsimmon_umd_minExports.seqMap(EXPRESSION.field.skip(parsimmon_umd_minExports.whitespace), parsimmon_umd_minExports.regexp(/AS/i).skip(parsimmon_umd_minExports.whitespace), EXPRESSION.identifier.or(EXPRESSION.string), (field, _as, ident) => QueryFields.named(ident, field)), + namedField: q => parsimmon_umd_minExports.alt(q.explicitNamedField, captureRaw(EXPRESSION.field).map(([value, text]) => QueryFields.named(stripNewlines(text), value))), + sortField: q => parsimmon_umd_minExports.seqMap(EXPRESSION.field.skip(parsimmon_umd_minExports.optWhitespace), parsimmon_umd_minExports.regexp(/ASCENDING|DESCENDING|ASC|DESC/i).atMost(1), (field, dir) => { + let direction = dir.length == 0 ? "ascending" : dir[0].toLowerCase(); + if (direction == "desc") + direction = "descending"; + if (direction == "asc") + direction = "ascending"; + return { + field: field, + direction: direction, + }; + }), + headerClause: q => q.queryType + .chain(type => { + switch (type) { + case "table": { + return precededByWhitespaceIfNotEof(() => ({ type, fields: [], showId: true }), parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/WITHOUT\s+ID/i) + .skip(parsimmon_umd_minExports.optWhitespace) + .atMost(1), parsimmon_umd_minExports.sepBy(q.namedField, parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace)), (withoutId, fields) => { + return { type, fields, showId: withoutId.length == 0 }; + })); + } + case "list": + return precededByWhitespaceIfNotEof(() => ({ type, format: undefined, showId: true }), parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/WITHOUT\s+ID/i) + .skip(parsimmon_umd_minExports.optWhitespace) + .atMost(1), EXPRESSION.field.atMost(1), (withoutId, format) => { + return { + type, + format: format.length == 1 ? format[0] : undefined, + showId: withoutId.length == 0, + }; + })); + case "task": + return parsimmon_umd_minExports.succeed({ type }); + case "calendar": + return parsimmon_umd_minExports.whitespace.then(parsimmon_umd_minExports.seqMap(q.namedField, field => { + return { + type, + showId: true, + field, + }; + })); + default: + return parsimmon_umd_minExports.fail(`Unrecognized query type '${type}'`); + } + }) + .desc("TABLE or LIST or TASK or CALENDAR"), + fromClause: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/FROM/i), parsimmon_umd_minExports.whitespace, EXPRESSION.source, (_1, _2, source) => source), + whereClause: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/WHERE/i), parsimmon_umd_minExports.whitespace, EXPRESSION.field, (where, _, field) => { + return { type: "where", clause: field }; + }).desc("WHERE "), + sortByClause: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/SORT/i), parsimmon_umd_minExports.whitespace, q.sortField.sepBy1(parsimmon_umd_minExports.string(",").trim(parsimmon_umd_minExports.optWhitespace)), (sort, _1, fields) => { + return { type: "sort", fields }; + }).desc("SORT field [ASC/DESC]"), + limitClause: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/LIMIT/i), parsimmon_umd_minExports.whitespace, EXPRESSION.field, (limit, _1, field) => { + return { type: "limit", amount: field }; + }).desc("LIMIT "), + flattenClause: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/FLATTEN/i).skip(parsimmon_umd_minExports.whitespace), q.namedField, (_, field) => { + return { type: "flatten", field }; + }).desc("FLATTEN [AS ]"), + groupByClause: q => parsimmon_umd_minExports.seqMap(parsimmon_umd_minExports.regexp(/GROUP BY/i).skip(parsimmon_umd_minExports.whitespace), q.namedField, (_, field) => { + return { type: "group", field }; + }).desc("GROUP BY [AS ]"), + // Full query parsing. + clause: q => parsimmon_umd_minExports.alt(q.fromClause, q.whereClause, q.sortByClause, q.limitClause, q.groupByClause, q.flattenClause), + query: q => parsimmon_umd_minExports.seqMap(q.headerClause.trim(parsimmon_umd_minExports.optWhitespace), q.fromClause.trim(parsimmon_umd_minExports.optWhitespace).atMost(1), q.clause.trim(parsimmon_umd_minExports.optWhitespace).many(), (header, from, clauses) => { + return { + header, + source: from.length == 0 ? Sources.folder("") : from[0], + operations: clauses, + settings: DEFAULT_QUERY_SETTINGS, + }; + }), +}); +/** + * Attempt to parse a query from the given query text, returning a string error + * if the parse failed. + */ +function parseQuery(text) { + try { + let query = QUERY_LANGUAGE.query.tryParse(text); + return Result.success(query); + } + catch (error) { + return Result.failure("" + error); + } +} + +function noop() { } +function assign(tar, src) { + // @ts-ignore + for (const k in src) + tar[k] = src[k]; + return tar; +} +function is_promise(value) { + return value && typeof value === 'object' && typeof value.then === 'function'; +} +function run(fn) { + return fn(); +} +function blank_object() { + return Object.create(null); +} +function run_all(fns) { + fns.forEach(run); +} +function is_function(thing) { + return typeof thing === 'function'; +} +function safe_not_equal(a, b) { + return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +} +function not_equal(a, b) { + return a != a ? b == b : a !== b; +} +function is_empty(obj) { return Object.keys(obj).length === 0; } function create_slot(definition, ctx, $$scope, fn) { @@ -17998,1891 +18218,1888 @@ class Calendar extends SvelteComponent { } } -/** Generic code for embedded Dataviews. */ -class DataviewRefreshableRenderer extends obsidian.MarkdownRenderChild { - constructor(container, index, app, settings) { - super(container); - this.container = container; - this.index = index; - this.app = app; - this.settings = settings; - this.maybeRefresh = () => { - // If the index revision has changed recently, then queue a reload. - // But only if we're mounted in the DOM and auto-refreshing is active. - if (this.lastReload != this.index.revision && this.container.isShown() && this.settings.refreshEnabled) { - this.lastReload = this.index.revision; - this.render(); - } - }; - this.lastReload = 0; - } - onload() { - this.render(); - this.lastReload = this.index.revision; - // Refresh after index changes stop. - this.registerEvent(this.app.workspace.on("dataview:refresh-views", this.maybeRefresh)); - // ...or when the DOM is shown (sidebar expands, tab selected, nodes scrolled into view). - this.register(this.container.onNodeInserted(this.maybeRefresh)); - } -} - -class DataviewCalendarRenderer extends DataviewRefreshableRenderer { - constructor(query, container, index, origin, settings, app) { - super(container, index, app, settings); - this.query = query; - this.container = container; - this.index = index; - this.origin = origin; - this.settings = settings; - this.app = app; - } - async render() { - var _a; - this.container.innerHTML = ""; - let maybeResult = await asyncTryOrPropogate(() => executeCalendar(this.query, this.index, this.origin, this.settings)); - if (!maybeResult.successful) { - renderErrorPre(this.container, "Dataview: " + maybeResult.error); - return; - } - else if (maybeResult.value.data.length == 0 && this.settings.warnOnEmptyResult) { - renderErrorPre(this.container, "Dataview: Query returned 0 results."); - return; - } - let dateMap = new Map(); - for (let data of maybeResult.value.data) { - const dot = { - color: "default", - className: "note", - isFilled: true, - link: data.link, - }; - const d = data.date.toFormat("yyyyLLdd"); - if (!dateMap.has(d)) { - dateMap.set(d, [dot]); - } - else { - (_a = dateMap.get(d)) === null || _a === void 0 ? void 0 : _a.push(dot); - } - } - const querySource = { - getDailyMetadata: async (date) => { - return { - dots: dateMap.get(date.format("YYYYMMDD")) || [], - }; - }, - }; - const sources = [querySource]; - const renderer = this; - this.calendar = new Calendar({ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - target: this.container, - props: { - onHoverDay(date, targetEl) { - const vals = dateMap.get(date.format("YYYYMMDD")); - if (!vals || vals.length == 0) { - return; - } - if ((vals === null || vals === void 0 ? void 0 : vals.length) == 0) { - return; - } - renderer.app.workspace.trigger("link-hover", {}, targetEl, vals[0].link.path, vals[0].link.path); - }, - onClickDay: async (date) => { - const vals = dateMap.get(date.format("YYYYMMDD")); - if (!vals || vals.length == 0) { - return; - } - if ((vals === null || vals === void 0 ? void 0 : vals.length) == 0) { - return; - } - const file = renderer.app.metadataCache.getFirstLinkpathDest(vals[0].link.path, ""); - if (file == null) { - return; - } - const leaf = renderer.app.workspace.getUnpinnedLeaf(); - await leaf.openFile(file, { active: true }); - }, - showWeekNums: false, - sources, - }, - }); - } - onClose() { - if (this.calendar) { - this.calendar.$destroy(); - } - return Promise.resolve(); - } -} - -/** Fancy wrappers for the JavaScript API, used both by external plugins AND by the dataview javascript view. */ -/** Asynchronous API calls related to file / system IO. */ -class DataviewInlineIOApi { - constructor(api, currentFile) { - this.api = api; - this.currentFile = currentFile; - } - /** Load the contents of a CSV asynchronously, returning a data array of rows (or undefined if it does not exist). */ - async csv(path, originFile) { - return this.api.csv(path, originFile || this.currentFile); - } - /** Asynchronously load the contents of any link or path in an Obsidian vault. */ - async load(path, originFile) { - return this.api.load(path, originFile || this.currentFile); - } - /** Normalize a link or path relative to an optional origin file. Returns a textual fully-qualified-path. */ - normalize(path, originFile) { - return this.api.normalize(path, originFile || this.currentFile); - } -} -class DataviewInlineApi { - constructor(api, component, container, currentFilePath) { - var _a, _b; - /** Value utilities which allow for type-checking and comparisons. */ - this.value = Values; - /** Widget utility functions for creating built-in widgets. */ - this.widget = Widgets; - /** Re-exporting of luxon for people who can't easily require it. Sorry! */ - this.luxon = Luxon; - this.index = api.index; - this.app = api.app; - this.settings = api.settings; - this.component = component; - this.container = container; - this.currentFilePath = currentFilePath; - this.api = api; - this.io = new DataviewInlineIOApi(this.api.io, this.currentFilePath); - // Set up the evaluation context with variables from the current file. - let fileMeta = (_b = (_a = this.index.pages.get(this.currentFilePath)) === null || _a === void 0 ? void 0 : _a.serialize(this.index)) !== null && _b !== void 0 ? _b : {}; - this.evaluationContext = new Context(defaultLinkHandler(this.index, this.currentFilePath), this.settings, { - this: fileMeta, - }); - this.func = Functions.bindAll(DEFAULT_FUNCTIONS, this.evaluationContext); - } - ///////////////////////////// - // Index + Data Collection // - ///////////////////////////// - /** Return an array of paths (as strings) corresponding to pages which match the query. */ - pagePaths(query) { - return this.api.pagePaths(query, this.currentFilePath); - } - /** Map a page path to the actual data contained within that page. */ - page(path) { - return this.api.page(path, this.currentFilePath); - } - /** Return an array of page objects corresponding to pages which match the query. */ - pages(query) { - return this.api.pages(query, this.currentFilePath); - } - /** Return the information about the current page. */ - current() { - return this.page(this.currentFilePath); - } - /////////////////////////////// - // Dataview Query Evaluation // - /////////////////////////////// - /** Execute a Dataview query, returning the results in programmatic form. */ - async query(source, originFile, settings) { - return this.api.query(source, originFile !== null && originFile !== void 0 ? originFile : this.currentFilePath, settings); - } - /** Error-throwing version of {@link query}. */ - async tryQuery(source, originFile, settings) { - return this.api.tryQuery(source, originFile !== null && originFile !== void 0 ? originFile : this.currentFilePath, settings); - } - /** Execute a Dataview query, returning the results in Markdown. */ - async queryMarkdown(source, originFile, settings) { - return this.api.queryMarkdown(source, originFile !== null && originFile !== void 0 ? originFile : this.currentFilePath, settings); - } - /** Error-throwing version of {@link queryMarkdown}. */ - async tryQueryMarkdown(source, originFile, settings) { - return this.api.tryQueryMarkdown(source, originFile !== null && originFile !== void 0 ? originFile : this.currentFilePath, settings); - } - /** - * Evaluate a dataview expression (like '2 + 2' or 'link("hello")'), returning the evaluated result. - * This takes an optional second argument which provides definitions for variables, such as: - * - * ``` - * dv.evaluate("x + 6", { x: 2 }) = 8 - * dv.evaluate('link(target)', { target: "Okay" }) = [[Okay]] - * ``` - * - * Note that `this` is implicitly available and refers to the current file. - * - * This method returns a Result type instead of throwing an error; you can check the result of the - * execution via `result.successful` and obtain `result.value` or `result.error` resultingly. If - * you'd rather this method throw on an error, use `dv.tryEvaluate`. - */ - evaluate(expression, context) { - let field = EXPRESSION.field.parse(expression); - if (!field.status) - return Result.failure(`Failed to parse expression "${expression}"`); - return this.evaluationContext.evaluate(field.value, context); - } - /** Error-throwing version of `dv.evaluate`. */ - tryEvaluate(expression, context) { - return this.evaluate(expression, context).orElseThrow(); - } - /** Execute a Dataview query and embed it into the current view. */ - async execute(source) { - this.api.execute(source, this.container, this.component, this.currentFilePath); - } - /** Execute a DataviewJS query and embed it into the current view. */ - async executeJs(code) { - this.api.executeJs(code, this.container, this.component, this.currentFilePath); - } - ///////////// - // Utility // - ///////////// - /** - * Convert an input element or array into a Dataview data-array. If the input is already a data array, - * it is returned unchanged. - */ - array(raw) { - return this.api.array(raw); - } - /** Return true if theg given value is a javascript array OR a dataview data array. */ - isArray(raw) { - return this.api.isArray(raw); - } - /** Return true if the given value is a dataview data array; this returns FALSE for plain JS arrays. */ - isDataArray(raw) { - return DataArray.isDataArray(raw); - } - /** Create a dataview file link to the given path. */ - fileLink(path, embed = false, display) { - return Link.file(path, embed, display); - } - /** Create a dataview section link to the given path. */ - sectionLink(path, section, embed = false, display) { - return Link.header(path, section, embed, display); - } - /** Create a dataview block link to the given path. */ - blockLink(path, blockId, embed = false, display) { - return Link.block(path, blockId, embed, display); - } - /** Attempt to extract a date from a string, link or date. */ - date(pathlike) { - return this.api.date(pathlike); - } - /** Attempt to extract a duration from a string or duration. */ - duration(dur) { - return this.api.duration(dur); - } - /** Parse a raw textual value into a complex Dataview type, if possible. */ - parse(value) { - return this.api.parse(value); - } - /** Convert a basic JS type into a Dataview type by parsing dates, links, durations, and so on. */ - literal(value) { - return this.api.literal(value); - } - /** Deep clone the given literal, returning a new literal which is independent of the original. */ - clone(value) { - return Values.deepCopy(value); - } - /** - * Compare two arbitrary JavaScript values using Dataview's default comparison rules. Returns a negative value if - * a < b, 0 if a = b, and a positive value if a > b. - */ - compare(a, b) { - return Values.compareValue(a, b); - } - /** Return true if the two given JavaScript values are equal using Dataview's default comparison rules. */ - equal(a, b) { - return this.compare(a, b) == 0; - } - ///////////////////////// - // Rendering Functions // - ///////////////////////// - /** Render an HTML element, containing arbitrary text. */ - el(el, text, { container = this.container, ...options } = {}) { - let wrapped = Values.wrapValue(text); - if (wrapped === null || wrapped === undefined) { - return container.createEl(el, Object.assign({ text }, options)); - } - let _el = container.createEl(el, options); - renderValue(wrapped.value, _el, this.currentFilePath, this.component, this.settings, true); - return _el; - } - /** Render an HTML header; the level can be anything from 1 - 6. */ - header(level, text, options) { - let header = { 1: "h1", 2: "h2", 3: "h3", 4: "h4", 5: "h5", 6: "h6" }[level]; - if (!header) - throw Error(`Unrecognized level '${level}' (expected 1, 2, 3, 4, 5, or 6)`); - return this.el(header, text, options); - } - /** Render an HTML paragraph, containing arbitrary text. */ - paragraph(text, options) { - return this.el("p", text, options); - } - /** Render an inline span, containing arbitrary text. */ - span(text, options) { - return this.el("span", text, options); - } - /** - * Render HTML from the output of a template "view" saved as a file in the vault. - * Takes a filename and arbitrary input data. - */ - async view(viewName, input) { - // Look for `${viewName}.js` first, then for `${viewName}/view.js`. - let simpleViewFile = this.app.metadataCache.getFirstLinkpathDest(viewName + ".js", this.currentFilePath); - if (simpleViewFile) { - let contents = await this.app.vault.read(simpleViewFile); - if (contents.contains("await")) - contents = "(async () => { " + contents + " })()"; - let func = new Function("dv", "input", contents); - try { - // This may directly render, in which case it will likely return undefined or null. - let result = await Promise.resolve(func(this, input)); - if (result) - await renderValue(result, this.container, this.currentFilePath, this.component, this.settings, true); - } - catch (ex) { - renderErrorPre(this.container, `Dataview: Failed to execute view '${simpleViewFile.path}'.\n\n${ex}`); - } - return; - } - // No `{viewName}.js`, so look for a folder instead. - let viewPath = `${viewName}/view.js`; - let viewFile = this.app.metadataCache.getFirstLinkpathDest(viewPath, this.currentFilePath); - if (!viewFile) { - renderErrorPre(this.container, `Dataview: custom view not found for '${viewPath}' or '${viewName}.js'.`); - return; - } - let viewContents = await this.app.vault.read(viewFile); - if (viewContents.contains("await")) - viewContents = "(async () => { " + viewContents + " })()"; - let viewFunction = new Function("dv", "input", viewContents); - try { - let result = await Promise.resolve(viewFunction(this, input)); - if (result) - await renderValue(result, this.container, this.currentFilePath, this.component, this.settings, true); - } - catch (ex) { - renderErrorPre(this.container, `Dataview: Error while executing view '${viewFile.path}'.\n\n${ex}`); - } - // Check for optional CSS. - let cssFile = this.app.metadataCache.getFirstLinkpathDest(`${viewName}/view.css`, this.currentFilePath); - if (!cssFile) - return; - let cssContents = await this.app.vault.read(cssFile); - this.container.createEl("style", { text: cssContents, attr: { scope: " " } }); - } - /** Render a dataview list of the given values. */ - list(values) { - return this.api.list(values, this.container, this.component, this.currentFilePath); - } - /** Render a dataview table with the given headers, and the 2D array of values. */ - table(headers, values) { - return this.api.table(headers, values, this.container, this.component, this.currentFilePath); - } - /** Render a dataview task view with the given tasks. */ - taskList(tasks, groupByFile = true) { - return this.api.taskList(tasks, groupByFile, this.container, this.component, this.currentFilePath); - } - //////////////////////// - // Markdown Rendering // - //////////////////////// - /** Render a table directly to markdown, returning the markdown. */ - markdownTable(headers, values, settings) { - return this.api.markdownTable(headers, values, settings); - } - /** Render a list directly to markdown, returning the markdown. */ - markdownList(values, settings) { - return this.api.markdownList(values, settings); - } - /** Render at ask list directly to markdown, returning the markdown. */ - markdownTaskList(values, settings) { - return this.api.markdownTaskList(values, settings); - } -} -/** - * Evaluate a script where 'this' for the script is set to the given context. Allows you to define global variables. - */ -function evalInContext(script, context) { - return function () { - return eval(script); - }.call(context); -} -/** - * Evaluate a script possibly asynchronously, if the script contains `async/await` blocks. - */ -async function asyncEvalInContext(script, context) { - if (script.includes("await")) { - return evalInContext("(async () => { " + script + " })()", context); - } - else { - return Promise.resolve(evalInContext(script, context)); - } -} - -class DataviewJSRenderer extends DataviewRefreshableRenderer { - constructor(api, script, container, origin) { - super(container, api.index, api.app, api.settings); - this.api = api; - this.script = script; - this.container = container; - this.origin = origin; - } - async render() { - this.container.innerHTML = ""; - if (!this.settings.enableDataviewJs) { - this.containerEl.innerHTML = ""; - renderErrorPre(this.container, "Dataview JS queries are disabled. You can enable them in the Dataview settings."); - return; - } - // Assume that the code is javascript, and try to eval it. - try { - await asyncEvalInContext(DataviewJSRenderer.PREAMBLE + this.script, new DataviewInlineApi(this.api, this, this.container, this.origin)); - } - catch (e) { - this.containerEl.innerHTML = ""; - renderErrorPre(this.container, "Evaluation Error: " + e.stack); - } - } -} -DataviewJSRenderer.PREAMBLE = "const dataview = this;const dv = this;"; -/** Inline JS renderer accessible using '=$' by default. */ -class DataviewInlineJSRenderer extends DataviewRefreshableRenderer { - constructor(api, script, container, target, origin) { - super(container, api.index, api.app, api.settings); - this.api = api; - this.script = script; - this.container = container; - this.target = target; - this.origin = origin; - } - async render() { - var _a; - (_a = this.errorbox) === null || _a === void 0 ? void 0 : _a.remove(); - if (!this.settings.enableDataviewJs || !this.settings.enableInlineDataviewJs) { - let temp = document.createElement("span"); - temp.innerText = "(disabled; enable in settings)"; - this.target.replaceWith(temp); - this.target = temp; - return; - } - // Assume that the code is javascript, and try to eval it. - try { - let temp = document.createElement("span"); - let result = await asyncEvalInContext(DataviewInlineJSRenderer.PREAMBLE + this.script, new DataviewInlineApi(this.api, this, temp, this.origin)); - this.target.replaceWith(temp); - this.target = temp; - if (result === undefined) - return; - renderValue(result, temp, this.origin, this, this.settings, false); - } - catch (e) { - this.errorbox = this.container.createEl("div"); - renderErrorPre(this.errorbox, "Dataview (for inline JS query '" + this.script + "'): " + e); - } - } -} +/** Generic code for embedded Dataviews. */ +class DataviewRefreshableRenderer extends obsidian.MarkdownRenderChild { + constructor(container, index, app, settings) { + super(container); + this.container = container; + this.index = index; + this.app = app; + this.settings = settings; + this.maybeRefresh = () => { + // If the index revision has changed recently, then queue a reload. + // But only if we're mounted in the DOM and auto-refreshing is active. + if (this.lastReload != this.index.revision && this.container.isShown() && this.settings.refreshEnabled) { + this.lastReload = this.index.revision; + this.render(); + } + }; + this.lastReload = 0; + } + onload() { + this.render(); + this.lastReload = this.index.revision; + // Refresh after index changes stop. + this.registerEvent(this.app.workspace.on("dataview:refresh-views", this.maybeRefresh)); + // ...or when the DOM is shown (sidebar expands, tab selected, nodes scrolled into view). + this.register(this.container.onNodeInserted(this.maybeRefresh)); + } +} + +class DataviewCalendarRenderer extends DataviewRefreshableRenderer { + constructor(query, container, index, origin, settings, app) { + super(container, index, app, settings); + this.query = query; + this.container = container; + this.index = index; + this.origin = origin; + this.settings = settings; + this.app = app; + } + async render() { + var _a; + this.container.innerHTML = ""; + let maybeResult = await asyncTryOrPropogate(() => executeCalendar(this.query, this.index, this.origin, this.settings)); + if (!maybeResult.successful) { + renderErrorPre(this.container, "Dataview: " + maybeResult.error); + return; + } + else if (maybeResult.value.data.length == 0 && this.settings.warnOnEmptyResult) { + renderErrorPre(this.container, "Dataview: Query returned 0 results."); + return; + } + let dateMap = new Map(); + for (let data of maybeResult.value.data) { + const dot = { + color: "default", + className: "note", + isFilled: true, + link: data.link, + }; + const d = data.date.toFormat("yyyyLLdd"); + if (!dateMap.has(d)) { + dateMap.set(d, [dot]); + } + else { + (_a = dateMap.get(d)) === null || _a === void 0 ? void 0 : _a.push(dot); + } + } + const querySource = { + getDailyMetadata: async (date) => { + return { + dots: dateMap.get(date.format("YYYYMMDD")) || [], + }; + }, + }; + const sources = [querySource]; + const renderer = this; + this.calendar = new Calendar({ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + target: this.container, + props: { + onHoverDay(date, targetEl) { + const vals = dateMap.get(date.format("YYYYMMDD")); + if (!vals || vals.length == 0) { + return; + } + if ((vals === null || vals === void 0 ? void 0 : vals.length) == 0) { + return; + } + renderer.app.workspace.trigger("link-hover", {}, targetEl, vals[0].link.path, vals[0].link.path); + }, + onClickDay: async (date) => { + const vals = dateMap.get(date.format("YYYYMMDD")); + if (!vals || vals.length == 0) { + return; + } + if ((vals === null || vals === void 0 ? void 0 : vals.length) == 0) { + return; + } + const file = renderer.app.metadataCache.getFirstLinkpathDest(vals[0].link.path, ""); + if (file == null) { + return; + } + const leaf = renderer.app.workspace.getUnpinnedLeaf(); + await leaf.openFile(file, { active: true }); + }, + showWeekNums: false, + sources, + }, + }); + } + onClose() { + if (this.calendar) { + this.calendar.$destroy(); + } + return Promise.resolve(); + } +} + +/** Fancy wrappers for the JavaScript API, used both by external plugins AND by the dataview javascript view. */ +/** Asynchronous API calls related to file / system IO. */ +class DataviewInlineIOApi { + constructor(api, currentFile) { + this.api = api; + this.currentFile = currentFile; + } + /** Load the contents of a CSV asynchronously, returning a data array of rows (or undefined if it does not exist). */ + async csv(path, originFile) { + return this.api.csv(path, originFile || this.currentFile); + } + /** Asynchronously load the contents of any link or path in an Obsidian vault. */ + async load(path, originFile) { + return this.api.load(path, originFile || this.currentFile); + } + /** Normalize a link or path relative to an optional origin file. Returns a textual fully-qualified-path. */ + normalize(path, originFile) { + return this.api.normalize(path, originFile || this.currentFile); + } +} +class DataviewInlineApi { + constructor(api, component, container, currentFilePath) { + var _a, _b; + /** Value utilities which allow for type-checking and comparisons. */ + this.value = Values; + /** Widget utility functions for creating built-in widgets. */ + this.widget = Widgets; + /** Re-exporting of luxon for people who can't easily require it. Sorry! */ + this.luxon = Luxon; + this.index = api.index; + this.app = api.app; + this.settings = api.settings; + this.component = component; + this.container = container; + this.currentFilePath = currentFilePath; + this.api = api; + this.io = new DataviewInlineIOApi(this.api.io, this.currentFilePath); + // Set up the evaluation context with variables from the current file. + let fileMeta = (_b = (_a = this.index.pages.get(this.currentFilePath)) === null || _a === void 0 ? void 0 : _a.serialize(this.index)) !== null && _b !== void 0 ? _b : {}; + this.evaluationContext = new Context(defaultLinkHandler(this.index, this.currentFilePath), this.settings, { + this: fileMeta, + }); + this.func = Functions.bindAll(DEFAULT_FUNCTIONS, this.evaluationContext); + } + ///////////////////////////// + // Index + Data Collection // + ///////////////////////////// + /** Return an array of paths (as strings) corresponding to pages which match the query. */ + pagePaths(query) { + return this.api.pagePaths(query, this.currentFilePath); + } + /** Map a page path to the actual data contained within that page. */ + page(path) { + return this.api.page(path, this.currentFilePath); + } + /** Return an array of page objects corresponding to pages which match the query. */ + pages(query) { + return this.api.pages(query, this.currentFilePath); + } + /** Return the information about the current page. */ + current() { + return this.page(this.currentFilePath); + } + /////////////////////////////// + // Dataview Query Evaluation // + /////////////////////////////// + /** Execute a Dataview query, returning the results in programmatic form. */ + async query(source, originFile, settings) { + return this.api.query(source, originFile !== null && originFile !== void 0 ? originFile : this.currentFilePath, settings); + } + /** Error-throwing version of {@link query}. */ + async tryQuery(source, originFile, settings) { + return this.api.tryQuery(source, originFile !== null && originFile !== void 0 ? originFile : this.currentFilePath, settings); + } + /** Execute a Dataview query, returning the results in Markdown. */ + async queryMarkdown(source, originFile, settings) { + return this.api.queryMarkdown(source, originFile !== null && originFile !== void 0 ? originFile : this.currentFilePath, settings); + } + /** Error-throwing version of {@link queryMarkdown}. */ + async tryQueryMarkdown(source, originFile, settings) { + return this.api.tryQueryMarkdown(source, originFile !== null && originFile !== void 0 ? originFile : this.currentFilePath, settings); + } + /** + * Evaluate a dataview expression (like '2 + 2' or 'link("hello")'), returning the evaluated result. + * This takes an optional second argument which provides definitions for variables, such as: + * + * ``` + * dv.evaluate("x + 6", { x: 2 }) = 8 + * dv.evaluate('link(target)', { target: "Okay" }) = [[Okay]] + * ``` + * + * Note that `this` is implicitly available and refers to the current file. + * + * This method returns a Result type instead of throwing an error; you can check the result of the + * execution via `result.successful` and obtain `result.value` or `result.error` resultingly. If + * you'd rather this method throw on an error, use `dv.tryEvaluate`. + */ + evaluate(expression, context) { + let field = EXPRESSION.field.parse(expression); + if (!field.status) + return Result.failure(`Failed to parse expression "${expression}"`); + return this.evaluationContext.evaluate(field.value, context); + } + /** Error-throwing version of `dv.evaluate`. */ + tryEvaluate(expression, context) { + return this.evaluate(expression, context).orElseThrow(); + } + /** Execute a Dataview query and embed it into the current view. */ + async execute(source) { + this.api.execute(source, this.container, this.component, this.currentFilePath); + } + /** Execute a DataviewJS query and embed it into the current view. */ + async executeJs(code) { + this.api.executeJs(code, this.container, this.component, this.currentFilePath); + } + ///////////// + // Utility // + ///////////// + /** + * Convert an input element or array into a Dataview data-array. If the input is already a data array, + * it is returned unchanged. + */ + array(raw) { + return this.api.array(raw); + } + /** Return true if theg given value is a javascript array OR a dataview data array. */ + isArray(raw) { + return this.api.isArray(raw); + } + /** Return true if the given value is a dataview data array; this returns FALSE for plain JS arrays. */ + isDataArray(raw) { + return DataArray.isDataArray(raw); + } + /** Create a dataview file link to the given path. */ + fileLink(path, embed = false, display) { + return Link.file(path, embed, display); + } + /** Create a dataview section link to the given path. */ + sectionLink(path, section, embed = false, display) { + return Link.header(path, section, embed, display); + } + /** Create a dataview block link to the given path. */ + blockLink(path, blockId, embed = false, display) { + return Link.block(path, blockId, embed, display); + } + /** Attempt to extract a date from a string, link or date. */ + date(pathlike) { + return this.api.date(pathlike); + } + /** Attempt to extract a duration from a string or duration. */ + duration(dur) { + return this.api.duration(dur); + } + /** Parse a raw textual value into a complex Dataview type, if possible. */ + parse(value) { + return this.api.parse(value); + } + /** Convert a basic JS type into a Dataview type by parsing dates, links, durations, and so on. */ + literal(value) { + return this.api.literal(value); + } + /** Deep clone the given literal, returning a new literal which is independent of the original. */ + clone(value) { + return Values.deepCopy(value); + } + /** + * Compare two arbitrary JavaScript values using Dataview's default comparison rules. Returns a negative value if + * a < b, 0 if a = b, and a positive value if a > b. + */ + compare(a, b) { + return Values.compareValue(a, b); + } + /** Return true if the two given JavaScript values are equal using Dataview's default comparison rules. */ + equal(a, b) { + return this.compare(a, b) == 0; + } + ///////////////////////// + // Rendering Functions // + ///////////////////////// + /** Render an HTML element, containing arbitrary text. */ + el(el, text, { container = this.container, ...options } = {}) { + let wrapped = Values.wrapValue(text); + if (wrapped === null || wrapped === undefined) { + return container.createEl(el, Object.assign({ text }, options)); + } + let _el = container.createEl(el, options); + renderValue(wrapped.value, _el, this.currentFilePath, this.component, this.settings, true); + return _el; + } + /** Render an HTML header; the level can be anything from 1 - 6. */ + header(level, text, options) { + let header = { 1: "h1", 2: "h2", 3: "h3", 4: "h4", 5: "h5", 6: "h6" }[level]; + if (!header) + throw Error(`Unrecognized level '${level}' (expected 1, 2, 3, 4, 5, or 6)`); + return this.el(header, text, options); + } + /** Render an HTML paragraph, containing arbitrary text. */ + paragraph(text, options) { + return this.el("p", text, options); + } + /** Render an inline span, containing arbitrary text. */ + span(text, options) { + return this.el("span", text, options); + } + /** + * Render HTML from the output of a template "view" saved as a file in the vault. + * Takes a filename and arbitrary input data. + */ + async view(viewName, input) { + // Look for `${viewName}.js` first, then for `${viewName}/view.js`. + const simpleViewPath = `${viewName}.js`; + const complexViewPath = `${viewName}/view.js`; + let checkForCss = false; + let viewFile = this.app.metadataCache.getFirstLinkpathDest(simpleViewPath, this.currentFilePath); + if (!viewFile) { + viewFile = this.app.metadataCache.getFirstLinkpathDest(complexViewPath, this.currentFilePath); + checkForCss = true; + } + if (!viewFile) { + renderErrorPre(this.container, `Dataview: custom view not found for '${simpleViewPath}' or '${complexViewPath}'.`); + return; + } + let contents = await this.app.vault.read(viewFile); + if (contents.contains("await")) + contents = "(async () => { " + contents + " })()"; + contents += `\n//# sourceURL=${viewFile.path}`; + let func = new Function("dv", "input", contents); + try { + // This may directly render, in which case it will likely return undefined or null. + let result = await Promise.resolve(func(this, input)); + if (result) + await renderValue(result, this.container, this.currentFilePath, this.component, this.settings, true); + } + catch (ex) { + renderErrorPre(this.container, `Dataview: Failed to execute view '${viewFile.path}'.\n\n${ex}`); + } + if (!checkForCss) { + return; + } + // Check for optional CSS. + let cssFile = this.app.metadataCache.getFirstLinkpathDest(`${viewName}/view.css`, this.currentFilePath); + if (!cssFile) + return; + let cssContents = await this.app.vault.read(cssFile); + cssContents += `\n/*# sourceURL=${location.origin}/${cssFile.path} */`; + this.container.createEl("style", { text: cssContents, attr: { scope: " " } }); + } + /** Render a dataview list of the given values. */ + list(values) { + return this.api.list(values, this.container, this.component, this.currentFilePath); + } + /** Render a dataview table with the given headers, and the 2D array of values. */ + table(headers, values) { + return this.api.table(headers, values, this.container, this.component, this.currentFilePath); + } + /** Render a dataview task view with the given tasks. */ + taskList(tasks, groupByFile = true) { + return this.api.taskList(tasks, groupByFile, this.container, this.component, this.currentFilePath); + } + //////////////////////// + // Markdown Rendering // + //////////////////////// + /** Render a table directly to markdown, returning the markdown. */ + markdownTable(headers, values, settings) { + return this.api.markdownTable(headers, values, settings); + } + /** Render a list directly to markdown, returning the markdown. */ + markdownList(values, settings) { + return this.api.markdownList(values, settings); + } + /** Render at ask list directly to markdown, returning the markdown. */ + markdownTaskList(values, settings) { + return this.api.markdownTaskList(values, settings); + } +} +/** + * Evaluate a script where 'this' for the script is set to the given context. Allows you to define global variables. + */ +function evalInContext(script, context) { + return function () { + return eval(script); + }.call(context); +} +/** + * Evaluate a script possibly asynchronously, if the script contains `async/await` blocks. + */ +async function asyncEvalInContext(script, context) { + if (script.includes("await")) { + return evalInContext("(async () => { " + script + " })()", context); + } + else { + return Promise.resolve(evalInContext(script, context)); + } +} + +class DataviewJSRenderer extends DataviewRefreshableRenderer { + constructor(api, script, container, origin) { + super(container, api.index, api.app, api.settings); + this.api = api; + this.script = script; + this.container = container; + this.origin = origin; + } + async render() { + this.container.innerHTML = ""; + if (!this.settings.enableDataviewJs) { + this.containerEl.innerHTML = ""; + renderErrorPre(this.container, "Dataview JS queries are disabled. You can enable them in the Dataview settings."); + return; + } + // Assume that the code is javascript, and try to eval it. + try { + await asyncEvalInContext(DataviewJSRenderer.PREAMBLE + this.script, new DataviewInlineApi(this.api, this, this.container, this.origin)); + } + catch (e) { + this.containerEl.innerHTML = ""; + renderErrorPre(this.container, "Evaluation Error: " + e.stack); + } + } +} +DataviewJSRenderer.PREAMBLE = "const dataview = this;const dv = this;"; +/** Inline JS renderer accessible using '=$' by default. */ +class DataviewInlineJSRenderer extends DataviewRefreshableRenderer { + constructor(api, script, container, target, origin) { + super(container, api.index, api.app, api.settings); + this.api = api; + this.script = script; + this.container = container; + this.target = target; + this.origin = origin; + } + async render() { + var _a; + (_a = this.errorbox) === null || _a === void 0 ? void 0 : _a.remove(); + if (!this.settings.enableDataviewJs || !this.settings.enableInlineDataviewJs) { + let temp = document.createElement("span"); + temp.innerText = "(disabled; enable in settings)"; + this.target.replaceWith(temp); + this.target = temp; + return; + } + // Assume that the code is javascript, and try to eval it. + try { + let temp = document.createElement("span"); + let result = await asyncEvalInContext(DataviewInlineJSRenderer.PREAMBLE + this.script, new DataviewInlineApi(this.api, this, temp, this.origin)); + this.target.replaceWith(temp); + this.target = temp; + if (result === undefined) + return; + renderValue(result, temp, this.origin, this, this.settings, false); + } + catch (e) { + this.errorbox = this.container.createEl("div"); + renderErrorPre(this.errorbox, "Dataview (for inline JS query '" + this.script + "'): " + e); + } + } +} DataviewInlineJSRenderer.PREAMBLE = "const dataview = this;const dv=this;"; -//////////// -// Tables // -//////////// -/** Render a table of literals to Markdown. */ -function markdownTable(headers, values, settings) { - if (values.length > 0 && headers.length != values[0].length) - throw new Error(`The number of headers (${headers.length}) must match the number of columns (${values[0].length})`); - settings = settings !== null && settings !== void 0 ? settings : DEFAULT_SETTINGS; - const mvalues = []; - const maxLengths = Array.from(headers, v => escapeTable(v).length); - // Pre-construct the table in memory so we can size columns. - for (let row = 0; row < values.length; row++) { - const current = []; - for (let col = 0; col < values[row].length; col++) { - const text = tableLiteral(values[row][col], settings.allowHtml, settings); - current.push(text); - maxLengths[col] = Math.max(maxLengths[col], text.length); - } - mvalues.push(current); - } - // Then construct the actual table... - // Append the header fields first. - let table = `| ${headers.map((v, i) => padright(escapeTable(v), " ", maxLengths[i])).join(" | ")} |\n`; - // Then the separating column. - table += `| ${maxLengths.map(i => padright("", "-", i)).join(" | ")} |\n`; - // Then the data colunns. - for (let row = 0; row < values.length; row++) { - table += `| ${mvalues[row].map((v, i) => padright(v, " ", maxLengths[i])).join(" | ")} |\n`; - } - return table; -} -/** Convert a value to a Markdown-friendly string. */ -function tableLiteral(value, allowHtml = true, settings) { - return escapeTable(rawTableLiteral(value, allowHtml, settings)); -} -/** Convert a value to a Markdown-friendly string; does not do escaping. */ -function rawTableLiteral(value, allowHtml = true, settings) { - if (!allowHtml) - return Values.toString(value, settings); - if (Values.isArray(value)) { - return `
    ${value.map(v => "
  • " + tableLiteral(v, allowHtml, settings) + "
  • ").join("")}
`; - } - else if (Values.isObject(value)) { - const inner = Object.entries(value) - .map(([k, v]) => { - return `
  • ${tableLiteral(k, allowHtml, settings)}: ${tableLiteral(v, allowHtml, settings)}
  • `; - }) - .join(""); - return `
      ${inner}
    `; - } - else { - return Values.toString(value, settings); - } -} -/** Don't need to import a library for this one... */ -function padright(text, padding, length) { - if (text.length >= length) - return text; - return text + padding.repeat(length - text.length); -} -/** Escape bars inside table content to prevent it from messing up table rows. */ -function escapeTable(text) { - return text.split(/(?!\\)\|/i).join("\\|"); -} -/////////// -// Lists // -/////////// -/** Render a list of literal elements to a markdown list. */ -function markdownList(values, settings) { - return markdownListRec(values, settings, 0); -} -/** Internal recursive function which renders markdown lists. */ -function markdownListRec(input, settings, depth = 0) { - if (Values.isArray(input)) { - let result = depth == 0 ? "" : "\n"; - for (let value of input) { - result += " ".repeat(depth) + "- "; - result += markdownListRec(value, settings, depth); - result += "\n"; - } - return result; - } - else if (Values.isObject(input)) { - let result = depth == 0 ? "" : "\n"; - for (let [key, value] of Object.entries(input)) { - result += " ".repeat(depth) + "- "; - result += Values.toString(key) + ": "; - result += markdownListRec(value, settings, depth); - result += "\n"; - } - return result; - } - else if (Values.isWidget(input) && Widgets.isListPair(input)) { - return `${Values.toString(input.key)}: ${markdownListRec(input.value, settings, depth + 1)}`; - } - return Values.toString(input); -} -/////////// -// Tasks // -/////////// -/** Render the result of a task query to markdown. */ -function markdownTaskList(tasks, settings, depth = 0) { - var _a, _b; - if (Groupings.isGrouping(tasks)) { - let result = ""; - for (let element of tasks) { - result += "#".repeat(depth + 1) + " " + Values.toString(element.key) + "\n\n"; - result += markdownTaskList(element.rows, settings, depth + 1); - } - return result; - } - else { - // Remove task line duplicates if present to match `taskList()` behavior. - const [dedupTasks, _] = nestItems(tasks); - let result = ""; - for (let element of dedupTasks) { - result += " ".repeat(depth) + "- "; - if (element.task) { - result += `[${element.status}] ${((_a = element.visual) !== null && _a !== void 0 ? _a : element.text).split("\n").join(" ")}\n`; - } - else { - result += `${((_b = element.visual) !== null && _b !== void 0 ? _b : element.text).split("\n").join(" ")}\n`; - } - result += markdownTaskList(element.children, settings, depth + 1); - } - return result; - } -} - -/** The general, externally accessible plugin API (available at `app.plugins.plugins.dataview.api` or as global `DataviewAPI`). */ -/** Asynchronous API calls related to file / system IO. */ -class DataviewIOApi { - constructor(api) { - this.api = api; - } - /** Load the contents of a CSV asynchronously, returning a data array of rows (or undefined if it does not exist). */ - async csv(path, originFile) { - if (!Values.isLink(path) && !Values.isString(path)) { - throw Error(`dv.io.csv only handles string or link paths; was provided type '${typeof path}'.`); - } - let data = await this.api.index.csv.get(this.normalize(path, originFile)); - if (data.successful) - return DataArray.from(data.value, this.api.settings); - else - throw Error(`Could not find CSV for path '${path}' (relative to origin '${originFile !== null && originFile !== void 0 ? originFile : "/"}')`); - } - /** Asynchronously load the contents of any link or path in an Obsidian vault. */ - async load(path, originFile) { - if (!Values.isLink(path) && !Values.isString(path)) { - throw Error(`dv.io.load only handles string or link paths; was provided type '${typeof path}'.`); - } - let existingFile = this.api.index.vault.getAbstractFileByPath(this.normalize(path, originFile)); - if (!existingFile || !(existingFile instanceof obsidian.TFile)) - return undefined; - return this.api.index.vault.cachedRead(existingFile); - } - /** Normalize a link or path relative to an optional origin file. Returns a textual fully-qualified-path. */ - normalize(path, originFile) { - let realPath; - if (Values.isLink(path)) - realPath = path.path; - else - realPath = path; - return this.api.index.prefix.resolveRelative(realPath, originFile); - } -} -/** Global API for accessing the Dataview API, executing dataview queries, and */ -class DataviewApi { - constructor(app, index, settings, verNum) { - this.app = app; - this.index = index; - this.settings = settings; - this.verNum = verNum; - /** Value utility functions for comparisons and type-checking. */ - this.value = Values; - /** Widget utility functions for creating built-in widgets. */ - this.widget = Widgets; - /** Re-exporting of luxon for people who can't easily require it. Sorry! */ - this.luxon = Luxon; - /** Utilities to check the current Dataview version and comapre it to SemVer version ranges. */ - this.version = (() => { - const { verNum: version } = this; - return { - get current() { - return version; - }, - compare: (op, ver) => compare(version, ver, op), - satisfies: (range) => satisfies(version, range), - }; - })(); - this.evaluationContext = new Context(defaultLinkHandler(index, ""), settings); - this.func = Functions.bindAll(DEFAULT_FUNCTIONS, this.evaluationContext); - this.io = new DataviewIOApi(this); - } - ///////////////////////////// - // Index + Data Collection // - ///////////////////////////// - /** Return an array of paths (as strings) corresponding to pages which match the query. */ - pagePaths(query, originFile) { - let source; - try { - if (!query || query.trim() === "") - source = Sources.folder(""); - else - source = EXPRESSION.source.tryParse(query); - } - catch (ex) { - throw new Error(`Failed to parse query in 'pagePaths': ${ex}`); - } - return matchingSourcePaths(source, this.index, originFile) - .map(s => DataArray.from(s, this.settings)) - .orElseThrow(); - } - /** Map a page path to the actual data contained within that page. */ - page(path, originFile) { - if (!(typeof path === "string") && !Values.isLink(path)) { - throw Error("dv.page only handles string and link paths; was provided type '" + typeof path + "'"); - } - let rawPath = path instanceof Link ? path.path : path; - let normPath = this.app.metadataCache.getFirstLinkpathDest(rawPath, originFile !== null && originFile !== void 0 ? originFile : ""); - if (!normPath) - return undefined; - let pageObject = this.index.pages.get(normPath.path); - if (!pageObject) - return undefined; - return this._addDataArrays(pageObject.serialize(this.index)); - } - /** Return an array of page objects corresponding to pages which match the source query. */ - pages(query, originFile) { - return this.pagePaths(query, originFile).flatMap(p => { - let res = this.page(p, originFile); - return res ? [res] : []; - }); - } - /** Remaps important metadata to add data arrays. */ - _addDataArrays(pageObject) { - // Remap the "file" metadata entries to be data arrays. - for (let [key, value] of Object.entries(pageObject.file)) { - if (Array.isArray(value)) - pageObject.file[key] = DataArray.wrap(value, this.settings); - } - return pageObject; - } - ///////////// - // Utility // - ///////////// - /** - * Convert an input element or array into a Dataview data-array. If the input is already a data array, - * it is returned unchanged. - */ - array(raw) { - if (DataArray.isDataArray(raw)) - return raw; - if (Array.isArray(raw)) - return DataArray.wrap(raw, this.settings); - return DataArray.wrap([raw], this.settings); - } - /** Return true if the given value is a javascript array OR a dataview data array. */ - isArray(raw) { - return DataArray.isDataArray(raw) || Array.isArray(raw); - } - /** Return true if the given value is a dataview data array; this returns FALSE for plain JS arrays. */ - isDataArray(raw) { - return DataArray.isDataArray(raw); - } - /** Create a dataview file link to the given path. */ - fileLink(path, embed = false, display) { - return Link.file(path, embed, display); - } - /** Create a dataview section link to the given path. */ - sectionLink(path, section, embed = false, display) { - return Link.header(path, section, embed, display); - } - /** Create a dataview block link to the given path. */ - blockLink(path, blockId, embed = false, display) { - return Link.block(path, blockId, embed, display); - } - /** Attempt to extract a date from a string, link or date. */ - date(pathlike) { - return this.func.date(pathlike); - } - /** Attempt to extract a duration from a string or duration. */ - duration(str) { - return this.func.dur(str); - } - /** Parse a raw textual value into a complex Dataview type, if possible. */ - parse(value) { - let raw = EXPRESSION.inlineField.parse(value); - if (raw.status) - return raw.value; - else - return value; - } - /** Convert a basic JS type into a Dataview type by parsing dates, links, durations, and so on. */ - literal(value) { - return parseFrontmatter(value); - } - /** Deep clone the given literal, returning a new literal which is independent of the original. */ - clone(value) { - return Values.deepCopy(value); - } - /** - * Compare two arbitrary JavaScript values using Dataview's default comparison rules. Returns a negative value if - * a < b, 0 if a = b, and a positive value if a > b. - */ - compare(a, b) { - return Values.compareValue(a, b, this.evaluationContext.linkHandler.normalize); - } - /** Return true if the two given JavaScript values are equal using Dataview's default comparison rules. */ - equal(a, b) { - return this.compare(a, b) == 0; - } - /////////////////////////////// - // Dataview Query Evaluation // - /////////////////////////////// - /** - * Execute an arbitrary Dataview query, returning a query result which: - * - * 1. Indicates the type of query, - * 2. Includes the raw AST of the parsed query. - * 3. Includes the output in the form relevant to that query type. - * - * List queries will return a list of objects ({ id, value }); table queries return a header array - * and a 2D array of values; and task arrays return a Grouping type which allows for recursive - * task nesting. - */ - async query(source, originFile, settings) { - const query = typeof source === "string" ? parseQuery(source) : Result.success(source); - if (!query.successful) - return query.cast(); - const header = query.value.header; - switch (header.type) { - case "calendar": - const cres = await executeCalendar(query.value, this.index, originFile !== null && originFile !== void 0 ? originFile : "", this.settings); - if (!cres.successful) - return cres.cast(); - return Result.success({ type: "calendar", values: cres.value.data }); - case "task": - const tasks = await executeTask(query.value, originFile !== null && originFile !== void 0 ? originFile : "", this.index, this.settings); - if (!tasks.successful) - return tasks.cast(); - return Result.success({ type: "task", values: tasks.value.tasks }); - case "list": - if ((settings === null || settings === void 0 ? void 0 : settings.forceId) !== undefined) - header.showId = settings.forceId; - const lres = await executeList(query.value, this.index, originFile !== null && originFile !== void 0 ? originFile : "", this.settings); - if (!lres.successful) - return lres.cast(); - // TODO: WITHOUT ID probably shouldn't exist, or should be moved to the engine itself. - // For now, until I fix it up in an upcoming refactor, we re-implement the behavior here. - return Result.success({ - type: "list", - values: lres.value.data, - primaryMeaning: lres.value.primaryMeaning, - }); - case "table": - if ((settings === null || settings === void 0 ? void 0 : settings.forceId) !== undefined) - header.showId = settings.forceId; - const tres = await executeTable(query.value, this.index, originFile !== null && originFile !== void 0 ? originFile : "", this.settings); - if (!tres.successful) - return tres.cast(); - return Result.success({ - type: "table", - values: tres.value.data, - headers: tres.value.names, - idMeaning: tres.value.idMeaning, - }); - } - } - /** Error-throwing version of {@link query}. */ - async tryQuery(source, originFile, settings) { - return (await this.query(source, originFile, settings)).orElseThrow(); - } - /** Execute an arbitrary dataview query, returning the results in well-formatted markdown. */ - async queryMarkdown(source, originFile, settings) { - const result = await this.query(source, originFile, settings); - if (!result.successful) - return result.cast(); - switch (result.value.type) { - case "list": - return Result.success(this.markdownList(result.value.values, settings)); - case "table": - return Result.success(this.markdownTable(result.value.headers, result.value.values, settings)); - case "task": - return Result.success(this.markdownTaskList(result.value.values, settings)); - case "calendar": - return Result.failure("Cannot render calendar queries to markdown."); - } - } - /** Error-throwing version of {@link queryMarkdown}. */ - async tryQueryMarkdown(source, originFile, settings) { - return (await this.queryMarkdown(source, originFile, settings)).orElseThrow(); - } - /** - * Evaluate a dataview expression (like '2 + 2' or 'link("hello")'), returning the evaluated result. - * This takes an optional second argument which provides definitions for variables, such as: - * - * ``` - * dv.evaluate("x + 6", { x: 2 }) = 8 - * dv.evaluate('link(target)', { target: "Okay" }) = [[Okay]] - * ``` - * - * This method returns a Result type instead of throwing an error; you can check the result of the - * execution via `result.successful` and obtain `result.value` or `result.error` resultingly. If - * you'd rather this method throw on an error, use `dv.tryEvaluate`. - */ - evaluate(expression, context) { - let field = EXPRESSION.field.parse(expression); - if (!field.status) - return Result.failure(`Failed to parse expression "${expression}"`); - return this.evaluationContext.evaluate(field.value, context); - } - /** Error-throwing version of `dv.evaluate`. */ - tryEvaluate(expression, context) { - return this.evaluate(expression, context).orElseThrow(); - } - /////////////// - // Rendering // - /////////////// - /** - * Execute the given query, rendering results into the given container using the components lifecycle. - * Your component should be a *real* component which calls onload() on it's child components at some point, - * or a MarkdownPostProcessorContext! - * - * Note that views made in this way are live updating and will automatically clean themselves up when - * the component is unloaded or the container is removed. - */ - async execute(source, container, component, filePath) { - if (isDataviewDisabled(filePath)) { - renderCodeBlock(container, source); - return; - } - let maybeQuery = tryOrPropogate(() => parseQuery(source)); - // In case of parse error, just render the error. - if (!maybeQuery.successful) { - renderErrorPre(container, "Dataview: " + maybeQuery.error); - return; - } - let query = maybeQuery.value; - let init = { app: this.app, settings: this.settings, index: this.index, container }; - let childComponent; - switch (query.header.type) { - case "task": - childComponent = createTaskView(init, query, filePath); - component.addChild(childComponent); - break; - case "list": - childComponent = createListView(init, query, filePath); - component.addChild(childComponent); - break; - case "table": - childComponent = createTableView(init, query, filePath); - component.addChild(childComponent); - break; - case "calendar": - childComponent = new DataviewCalendarRenderer(query, container, this.index, filePath, this.settings, this.app); - component.addChild(childComponent); - break; - } - childComponent.load(); - } - /** - * Execute the given DataviewJS query, rendering results into the given container using the components lifecycle. - * See {@link execute} for general rendering semantics. - */ - async executeJs(code, container, component, filePath) { - if (isDataviewDisabled(filePath)) { - renderCodeBlock(container, code, "javascript"); - return; - } - const renderer = new DataviewJSRenderer(this, code, container, filePath); - renderer.load(); - component.addChild(renderer); - } - /** Render a dataview list of the given values. */ - async list(values, container, component, filePath) { - if (!values) - return; - if (values !== undefined && values !== null && !Array.isArray(values) && !DataArray.isDataArray(values)) - values = Array.from(values); - // Append a child div, since React will keep re-rendering otherwise. - let subcontainer = container.createEl("div"); - component.addChild(createFixedListView({ app: this.app, settings: this.settings, index: this.index, container: subcontainer }, values, filePath)); - } - /** Render a dataview table with the given headers, and the 2D array of values. */ - async table(headers, values, container, component, filePath) { - if (!headers) - headers = []; - if (!values) - values = []; - if (!Array.isArray(headers) && !DataArray.isDataArray(headers)) - headers = Array.from(headers); - // Append a child div, since React will keep re-rendering otherwise. - let subcontainer = container.createEl("div"); - component.addChild(createFixedTableView({ app: this.app, settings: this.settings, index: this.index, container: subcontainer }, headers, values, filePath)); - } - /** Render a dataview task view with the given tasks. */ - async taskList(tasks, groupByFile = true, container, component, filePath = "") { - let groupedTasks = !Groupings.isGrouping(tasks) && groupByFile ? this.array(tasks).groupBy(t => Link.file(t.path)) : tasks; - // Append a child div, since React will override several task lists otherwise. - let taskContainer = container.createEl("div"); - component.addChild(createFixedTaskView({ app: this.app, settings: this.settings, index: this.index, container: taskContainer }, groupedTasks, filePath)); - } - /** Render an arbitrary value into a container. */ - async renderValue(value, container, component, filePath, inline = false) { - return renderValue(value, container, filePath, component, this.settings, inline); - } - ///////////////// - // Data Export // - ///////////////// - /** Render data to a markdown table. */ - markdownTable(headers, values, settings) { - if (!headers) - headers = []; - if (!values) - values = []; - const combined = Object.assign({}, this.settings, settings); - return markdownTable(headers, values, combined); - } - /** Render data to a markdown list. */ - markdownList(values, settings) { - if (!values) - values = []; - const combined = Object.assign({}, this.settings, settings); - return markdownList(values, combined); - } - /** Render tasks or list items to a markdown task list. */ - markdownTaskList(values, settings) { - if (!values) - values = []; - const sparse = nestGroups(values); - const combined = Object.assign({}, this.settings, settings); - return markdownTaskList(sparse, combined); - } -} -/** Determines if source-path has a `?no-dataview` annotation that disables dataview. */ -function isDataviewDisabled(sourcePath) { - let questionLocation = sourcePath.lastIndexOf("?"); - if (questionLocation == -1) - return false; - return sourcePath.substring(questionLocation).contains("no-dataview"); -} - -/** Refreshable renderer which renders inline instead of in a div. */ -class DataviewInlineRenderer extends DataviewRefreshableRenderer { - constructor(field, fieldText, container, target, index, origin, settings, app) { - super(container, index, app, settings); - this.field = field; - this.fieldText = fieldText; - this.container = container; - this.target = target; - this.index = index; - this.origin = origin; - this.settings = settings; - this.app = app; - } - async render() { - var _a; - (_a = this.errorbox) === null || _a === void 0 ? void 0 : _a.remove(); - let result = tryOrPropogate(() => executeInline(this.field, this.origin, this.index, this.settings)); - if (!result.successful) { - this.errorbox = this.container.createEl("div"); - renderErrorPre(this.errorbox, "Dataview (for inline query '" + this.fieldText + "'): " + result.error); - } - else { - let temp = document.createElement("span"); - temp.addClasses(["dataview", "dataview-inline-query"]); - await renderValue(result.value, temp, this.origin, this, this.settings, false); - this.target.replaceWith(temp); - } - } -} - -/** Replaces raw textual inline fields in text containers with pretty HTML equivalents. */ -async function replaceInlineFields(ctx, init) { - let inlineFields = extractInlineFields(init.container.innerHTML); - if (inlineFields.length == 0) - return; - let component = new obsidian.MarkdownRenderChild(init.container); - ctx.addChild(component); - // Iterate through the raw HTML and replace inline field matches with corresponding rendered values. - let result = init.container.innerHTML; - for (let x = inlineFields.length - 1; x >= 0; x--) { - let field = inlineFields[x]; - let renderContainer = document.createElement("span"); - renderContainer.addClasses(["dataview", "inline-field"]); - // Block inline fields render the key, parenthesis ones do not. - if (field.wrapping == "[") { - const key = renderContainer.createSpan({ - cls: ["dataview", "inline-field-key"], - attr: { - "data-dv-key": field.key, - "data-dv-norm-key": canonicalizeVarName(field.key), - }, - }); - // Explicitly set the inner HTML to respect any key formatting that we should carry over. - key.innerHTML = field.key; - renderContainer.createSpan({ - cls: ["dataview", "inline-field-value"], - attr: { id: "dataview-inline-field-" + x }, - }); - } - else { - renderContainer.createSpan({ - cls: ["dataview", "inline-field-standalone-value"], - attr: { id: "dataview-inline-field-" + x }, - }); - } - result = result.slice(0, field.start) + renderContainer.outerHTML + result.slice(field.end); - } - // Use a