` element (which could be changed with\n * `data.hName`), with its children mapped from mdast to hast as well\n *\n * This behavior can be changed by passing an `unknownHandler`.\n *\n * @param {MdastNodes} tree\n * mdast tree.\n * @param {Options | null | undefined} [options]\n * Configuration.\n * @returns {HastNodes | null | undefined}\n * hast tree.\n */\n// To do: next major: always return a single `root`.\nexport function toHast(tree, options) {\n const state = createState(tree, options)\n const node = state.one(tree, null)\n const foot = footer(state)\n\n if (foot) {\n // @ts-expect-error If there’s a footer, there were definitions, meaning block\n // content.\n // So assume `node` is a parent node.\n node.children.push({type: 'text', value: '\\n'}, foot)\n }\n\n // To do: next major: always return root?\n return Array.isArray(node) ? {type: 'root', children: node} : node\n}\n","/**\n * @typedef PointLike\n * @property {number | null | undefined} [line]\n * @property {number | null | undefined} [column]\n * @property {number | null | undefined} [offset]\n *\n * @typedef PositionLike\n * @property {PointLike | null | undefined} [start]\n * @property {PointLike | null | undefined} [end]\n *\n * @typedef NodeLike\n * @property {PositionLike | null | undefined} [position]\n */\n\n/**\n * Check if `node` is generated.\n *\n * @param {NodeLike | null | undefined} [node]\n * Node to check.\n * @returns {boolean}\n * Whether `node` is generated (does not have positional info).\n */\nexport function generated(node) {\n return (\n !node ||\n !node.position ||\n !node.position.start ||\n !node.position.start.line ||\n !node.position.start.column ||\n !node.position.end ||\n !node.position.end.line ||\n !node.position.end.column\n )\n}\n","/**\n * @typedef {import('hast').Element} Element\n * @typedef {import('hast').ElementContent} ElementContent\n *\n * @typedef {import('./state.js').State} State\n */\n\nimport {normalizeUri} from 'micromark-util-sanitize-uri'\n\n/**\n * Generate a hast footer for called footnote definitions.\n *\n * @param {State} state\n * Info passed around.\n * @returns {Element | undefined}\n * `section` element or `undefined`.\n */\nexport function footer(state) {\n /** @type {Array
} */\n const listItems = []\n let index = -1\n\n while (++index < state.footnoteOrder.length) {\n const def = state.footnoteById[state.footnoteOrder[index]]\n\n if (!def) {\n continue\n }\n\n const content = state.all(def)\n const id = String(def.identifier).toUpperCase()\n const safeId = normalizeUri(id.toLowerCase())\n let referenceIndex = 0\n /** @type {Array} */\n const backReferences = []\n\n while (++referenceIndex <= state.footnoteCounts[id]) {\n /** @type {Element} */\n const backReference = {\n type: 'element',\n tagName: 'a',\n properties: {\n href:\n '#' +\n state.clobberPrefix +\n 'fnref-' +\n safeId +\n (referenceIndex > 1 ? '-' + referenceIndex : ''),\n dataFootnoteBackref: true,\n className: ['data-footnote-backref'],\n ariaLabel: state.footnoteBackLabel\n },\n children: [{type: 'text', value: '↩'}]\n }\n\n if (referenceIndex > 1) {\n backReference.children.push({\n type: 'element',\n tagName: 'sup',\n children: [{type: 'text', value: String(referenceIndex)}]\n })\n }\n\n if (backReferences.length > 0) {\n backReferences.push({type: 'text', value: ' '})\n }\n\n backReferences.push(backReference)\n }\n\n const tail = content[content.length - 1]\n\n if (tail && tail.type === 'element' && tail.tagName === 'p') {\n const tailTail = tail.children[tail.children.length - 1]\n if (tailTail && tailTail.type === 'text') {\n tailTail.value += ' '\n } else {\n tail.children.push({type: 'text', value: ' '})\n }\n\n tail.children.push(...backReferences)\n } else {\n content.push(...backReferences)\n }\n\n /** @type {Element} */\n const listItem = {\n type: 'element',\n tagName: 'li',\n properties: {id: state.clobberPrefix + 'fn-' + safeId},\n children: state.wrap(content, true)\n }\n\n state.patch(def, listItem)\n\n listItems.push(listItem)\n }\n\n if (listItems.length === 0) {\n return\n }\n\n return {\n type: 'element',\n tagName: 'section',\n properties: {dataFootnotes: true, className: ['footnotes']},\n children: [\n {\n type: 'element',\n tagName: state.footnoteLabelTagName,\n properties: {\n // To do: use structured clone.\n ...JSON.parse(JSON.stringify(state.footnoteLabelProperties)),\n id: 'footnote-label'\n },\n children: [{type: 'text', value: state.footnoteLabel}]\n },\n {type: 'text', value: '\\n'},\n {\n type: 'element',\n tagName: 'ol',\n properties: {},\n children: state.wrap(listItems, true)\n },\n {type: 'text', value: '\\n'}\n ]\n }\n}\n","/**\n * @typedef {import('hast').Root} HastRoot\n * @typedef {import('mdast').Root} MdastRoot\n * @typedef {import('mdast-util-to-hast').Options} Options\n * @typedef {import('unified').Processor} Processor\n *\n * @typedef {import('mdast-util-to-hast')} DoNotTouchAsThisImportIncludesRawInTree\n */\n\nimport {toHast} from 'mdast-util-to-hast'\n\n// Note: the `` overload doesn’t seem to work :'(\n\n/**\n * Plugin that turns markdown into HTML to support rehype.\n *\n * * If a destination processor is given, that processor runs with a new HTML\n * (hast) tree (bridge-mode).\n * As the given processor runs with a hast tree, and rehype plugins support\n * hast, that means rehype plugins can be used with the given processor.\n * The hast tree is discarded in the end.\n * It’s highly unlikely that you want to do this.\n * * The common case is to not pass a destination processor, in which case the\n * current processor continues running with a new HTML (hast) tree\n * (mutate-mode).\n * As the current processor continues with a hast tree, and rehype plugins\n * support hast, that means rehype plugins can be used after\n * `remark-rehype`.\n * It’s likely that this is what you want to do.\n *\n * @param destination\n * Optional unified processor.\n * @param options\n * Options passed to `mdast-util-to-hast`.\n */\nconst remarkRehype =\n /** @type {(import('unified').Plugin<[Processor, Options?]|[null|undefined, Options?]|[Options]|[], MdastRoot>)} */\n (\n function (destination, options) {\n return destination && 'run' in destination\n ? bridge(destination, options)\n : mutate(destination || options)\n }\n )\n\nexport default remarkRehype\n\n/**\n * Bridge-mode.\n * Runs the destination with the new hast tree.\n *\n * @type {import('unified').Plugin<[Processor, Options?], MdastRoot>}\n */\nfunction bridge(destination, options) {\n return (node, file, next) => {\n destination.run(toHast(node, options), file, (error) => {\n next(error)\n })\n }\n}\n\n/**\n * Mutate-mode.\n * Further plugins run on the hast tree.\n *\n * @type {import('unified').Plugin<[Options?]|void[], MdastRoot, HastRoot>}\n */\nfunction mutate(options) {\n // @ts-expect-error: assume a corresponding node is returned by `toHast`.\n return (node) => toHast(node, options)\n}\n","/**\n * Parse space-separated tokens to an array of strings.\n *\n * @param {string} value\n * Space-separated tokens.\n * @returns {Array}\n * List of tokens.\n */\nexport function parse(value) {\n const input = String(value || '').trim()\n return input ? input.split(/[ \\t\\n\\r\\f]+/g) : []\n}\n\n/**\n * Serialize an array of strings as space separated-tokens.\n *\n * @param {Array} values\n * List of tokens.\n * @returns {string}\n * Space-separated tokens.\n */\nexport function stringify(values) {\n return values.join(' ').trim()\n}\n","import stripAnsi from 'strip-ansi';\nimport charRegex from 'char-regex';\n\nexport default function stringLength(string, {countAnsiEscapeCodes = false} = {}) {\n\tif (string === '') {\n\t\treturn 0;\n\t}\n\n\tif (!countAnsiEscapeCodes) {\n\t\tstring = stripAnsi(string);\n\t}\n\n\tif (string === '') {\n\t\treturn 0;\n\t}\n\n\treturn string.match(charRegex()).length;\n}\n","// Based on https://github.com/lodash/lodash/blob/6018350ac10d5ce6a5b7db625140b82aeab804df/.internal/unicodeSize.js\r\n\r\nexport default function charRegex() {\r\n\t// Unicode character classes\r\n\tconst astralRange = '\\\\ud800-\\\\udfff';\r\n\tconst comboMarksRange = '\\\\u0300-\\\\u036f';\r\n\tconst comboHalfMarksRange = '\\\\ufe20-\\\\ufe2f';\r\n\tconst comboSymbolsRange = '\\\\u20d0-\\\\u20ff';\r\n\tconst comboMarksExtendedRange = '\\\\u1ab0-\\\\u1aff';\r\n\tconst comboMarksSupplementRange = '\\\\u1dc0-\\\\u1dff';\r\n\tconst comboRange = comboMarksRange + comboHalfMarksRange + comboSymbolsRange + comboMarksExtendedRange + comboMarksSupplementRange;\r\n\tconst varRange = '\\\\ufe0e\\\\ufe0f';\r\n\r\n\t// Telugu characters\r\n\tconst teluguVowels = '\\\\u0c05-\\\\u0c0c\\\\u0c0e-\\\\u0c10\\\\u0c12-\\\\u0c14\\\\u0c60-\\\\u0c61';\r\n\tconst teluguVowelsDiacritic = '\\\\u0c3e-\\\\u0c44\\\\u0c46-\\\\u0c48\\\\u0c4a-\\\\u0c4c\\\\u0c62-\\\\u0c63';\r\n\tconst teluguConsonants = '\\\\u0c15-\\\\u0c28\\\\u0c2a-\\\\u0c39';\r\n\tconst teluguConsonantsRare = '\\\\u0c58-\\\\u0c5a';\r\n\tconst teluguModifiers = '\\\\u0c01-\\\\u0c03\\\\u0c4d\\\\u0c55\\\\u0c56';\r\n\tconst teluguNumerals = '\\\\u0c66-\\\\u0c6f\\\\u0c78-\\\\u0c7e';\r\n\tconst teluguSingle = `[${teluguVowels}(?:${teluguConsonants}(?!\\\\u0c4d))${teluguNumerals}${teluguConsonantsRare}]`;\r\n\tconst teluguDouble = `[${teluguConsonants}${teluguConsonantsRare}][${teluguVowelsDiacritic}]|[${teluguConsonants}${teluguConsonantsRare}][${teluguModifiers}`;\r\n\tconst teluguTriple = `[${teluguConsonants}]\\\\u0c4d[${teluguConsonants}]`;\r\n\tconst telugu = `(?:${teluguTriple}|${teluguDouble}|${teluguSingle})`;\r\n\r\n\t// Unicode capture groups\r\n\tconst astral = `[${astralRange}]`;\r\n\tconst combo = `[${comboRange}]`;\r\n\tconst fitz = '\\\\ud83c[\\\\udffb-\\\\udfff]';\r\n\tconst modifier = `(?:${combo}|${fitz})`;\r\n\tconst nonAstral = `[^${astralRange}]`;\r\n\tconst regional = '(?:\\\\ud83c[\\\\udde6-\\\\uddff]){2}';\r\n\tconst surrogatePair = '[\\\\ud800-\\\\udbff][\\\\udc00-\\\\udfff]';\r\n\tconst zeroWidthJoiner = '\\\\u200d';\r\n\tconst blackFlag = '(?:\\\\ud83c\\\\udff4\\\\udb40\\\\udc67\\\\udb40\\\\udc62\\\\udb40(?:\\\\udc65|\\\\udc73|\\\\udc77)\\\\udb40(?:\\\\udc6e|\\\\udc63|\\\\udc6c)\\\\udb40(?:\\\\udc67|\\\\udc74|\\\\udc73)\\\\udb40\\\\udc7f)';\r\n\r\n\t// Unicode regexes\r\n\tconst optModifier = `${modifier}?`;\r\n\tconst optVar = `[${varRange}]?`;\r\n\tconst optJoin = `(?:${zeroWidthJoiner}(?:${[nonAstral, regional, surrogatePair].join('|')})${optVar + optModifier})*`;\r\n\tconst seq = optVar + optModifier + optJoin;\r\n\tconst nonAstralCombo = `${nonAstral}${combo}?`;\r\n\tconst symbol = `(?:${[blackFlag, nonAstralCombo, combo, regional, surrogatePair, astral].join('|')})`;\r\n\r\n\t// Match string symbols (https://mathiasbynens.be/notes/javascript-unicode)\r\n\treturn new RegExp(`${fitz}(?=${fitz})|${telugu}|${symbol + seq}`, 'g');\r\n}\r\n","/**\n * Throw a given error.\n *\n * @param {Error|null|undefined} [error]\n * Maybe error.\n * @returns {asserts error is null|undefined}\n */\nexport function bail(error) {\n if (error) {\n throw error\n }\n}\n","/**\n * @typedef {(error?: Error|null|undefined, ...output: Array) => void} Callback\n * @typedef {(...input: Array) => any} Middleware\n *\n * @typedef {(...input: Array) => void} Run\n * Call all middleware.\n * @typedef {(fn: Middleware) => Pipeline} Use\n * Add `fn` (middleware) to the list.\n * @typedef {{run: Run, use: Use}} Pipeline\n * Middleware.\n */\n\n/**\n * Create new middleware.\n *\n * @returns {Pipeline}\n */\nexport function trough() {\n /** @type {Array} */\n const fns = []\n /** @type {Pipeline} */\n const pipeline = {run, use}\n\n return pipeline\n\n /** @type {Run} */\n function run(...values) {\n let middlewareIndex = -1\n /** @type {Callback} */\n const callback = values.pop()\n\n if (typeof callback !== 'function') {\n throw new TypeError('Expected function as last argument, not ' + callback)\n }\n\n next(null, ...values)\n\n /**\n * Run the next `fn`, or we’re done.\n *\n * @param {Error|null|undefined} error\n * @param {Array} output\n */\n function next(error, ...output) {\n const fn = fns[++middlewareIndex]\n let index = -1\n\n if (error) {\n callback(error)\n return\n }\n\n // Copy non-nullish input into values.\n while (++index < values.length) {\n if (output[index] === null || output[index] === undefined) {\n output[index] = values[index]\n }\n }\n\n // Save the newly created `output` for the next call.\n values = output\n\n // Next or done.\n if (fn) {\n wrap(fn, next)(...output)\n } else {\n callback(null, ...output)\n }\n }\n }\n\n /** @type {Use} */\n function use(middelware) {\n if (typeof middelware !== 'function') {\n throw new TypeError(\n 'Expected `middelware` to be a function, not ' + middelware\n )\n }\n\n fns.push(middelware)\n return pipeline\n }\n}\n\n/**\n * Wrap `middleware`.\n * Can be sync or async; return a promise, receive a callback, or return new\n * values and errors.\n *\n * @param {Middleware} middleware\n * @param {Callback} callback\n */\nexport function wrap(middleware, callback) {\n /** @type {boolean} */\n let called\n\n return wrapped\n\n /**\n * Call `middleware`.\n * @this {any}\n * @param {Array} parameters\n * @returns {void}\n */\n function wrapped(...parameters) {\n const fnExpectsCallback = middleware.length > parameters.length\n /** @type {any} */\n let result\n\n if (fnExpectsCallback) {\n parameters.push(done)\n }\n\n try {\n result = middleware.apply(this, parameters)\n } catch (error) {\n const exception = /** @type {Error} */ (error)\n\n // Well, this is quite the pickle.\n // `middleware` received a callback and called it synchronously, but that\n // threw an error.\n // The only thing left to do is to throw the thing instead.\n if (fnExpectsCallback && called) {\n throw exception\n }\n\n return done(exception)\n }\n\n if (!fnExpectsCallback) {\n if (result instanceof Promise) {\n result.then(then, done)\n } else if (result instanceof Error) {\n done(result)\n } else {\n then(result)\n }\n }\n }\n\n /**\n * Call `callback`, only once.\n * @type {Callback}\n */\n function done(error, ...output) {\n if (!called) {\n called = true\n callback(error, ...output)\n }\n }\n\n /**\n * Call `done` with one value.\n *\n * @param {any} [value]\n */\n function then(value) {\n done(null, value)\n }\n}\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Position} Position\n * @typedef {import('unist').Point} Point\n * @typedef {object & {type: string, position?: Position | undefined}} NodeLike\n */\n\nimport {stringifyPosition} from 'unist-util-stringify-position'\n\n/**\n * Message.\n */\nexport class VFileMessage extends Error {\n /**\n * Create a message for `reason` at `place` from `origin`.\n *\n * When an error is passed in as `reason`, the `stack` is copied.\n *\n * @param {string | Error | VFileMessage} reason\n * Reason for message, uses the stack and message of the error if given.\n *\n * > 👉 **Note**: you should use markdown.\n * @param {Node | NodeLike | Position | Point | null | undefined} [place]\n * Place in file where the message occurred.\n * @param {string | null | undefined} [origin]\n * Place in code where the message originates (example:\n * `'my-package:my-rule'` or `'my-rule'`).\n * @returns\n * Instance of `VFileMessage`.\n */\n // To do: next major: expose `undefined` everywhere instead of `null`.\n constructor(reason, place, origin) {\n /** @type {[string | null, string | null]} */\n const parts = [null, null]\n /** @type {Position} */\n let position = {\n // @ts-expect-error: we always follows the structure of `position`.\n start: {line: null, column: null},\n // @ts-expect-error: \"\n end: {line: null, column: null}\n }\n\n super()\n\n if (typeof place === 'string') {\n origin = place\n place = undefined\n }\n\n if (typeof origin === 'string') {\n const index = origin.indexOf(':')\n\n if (index === -1) {\n parts[1] = origin\n } else {\n parts[0] = origin.slice(0, index)\n parts[1] = origin.slice(index + 1)\n }\n }\n\n if (place) {\n // Node.\n if ('type' in place || 'position' in place) {\n if (place.position) {\n // To do: next major: deep clone.\n // @ts-expect-error: looks like a position.\n position = place.position\n }\n }\n // Position.\n else if ('start' in place || 'end' in place) {\n // @ts-expect-error: looks like a position.\n // To do: next major: deep clone.\n position = place\n }\n // Point.\n else if ('line' in place || 'column' in place) {\n // To do: next major: deep clone.\n position.start = place\n }\n }\n\n // Fields from `Error`.\n /**\n * Serialized positional info of error.\n *\n * On normal errors, this would be something like `ParseError`, buit in\n * `VFile` messages we use this space to show where an error happened.\n */\n this.name = stringifyPosition(place) || '1:1'\n\n /**\n * Reason for message.\n *\n * @type {string}\n */\n this.message = typeof reason === 'object' ? reason.message : reason\n\n /**\n * Stack of message.\n *\n * This is used by normal errors to show where something happened in\n * programming code, irrelevant for `VFile` messages,\n *\n * @type {string}\n */\n this.stack = ''\n\n if (typeof reason === 'object' && reason.stack) {\n this.stack = reason.stack\n }\n\n /**\n * Reason for message.\n *\n * > 👉 **Note**: you should use markdown.\n *\n * @type {string}\n */\n this.reason = this.message\n\n /* eslint-disable no-unused-expressions */\n /**\n * State of problem.\n *\n * * `true` — marks associated file as no longer processable (error)\n * * `false` — necessitates a (potential) change (warning)\n * * `null | undefined` — for things that might not need changing (info)\n *\n * @type {boolean | null | undefined}\n */\n this.fatal\n\n /**\n * Starting line of error.\n *\n * @type {number | null}\n */\n this.line = position.start.line\n\n /**\n * Starting column of error.\n *\n * @type {number | null}\n */\n this.column = position.start.column\n\n /**\n * Full unist position.\n *\n * @type {Position | null}\n */\n this.position = position\n\n /**\n * Namespace of message (example: `'my-package'`).\n *\n * @type {string | null}\n */\n this.source = parts[0]\n\n /**\n * Category of message (example: `'my-rule'`).\n *\n * @type {string | null}\n */\n this.ruleId = parts[1]\n\n /**\n * Path of a file (used throughout the `VFile` ecosystem).\n *\n * @type {string | null}\n */\n this.file\n\n // The following fields are “well known”.\n // Not standard.\n // Feel free to add other non-standard fields to your messages.\n\n /**\n * Specify the source value that’s being reported, which is deemed\n * incorrect.\n *\n * @type {string | null}\n */\n this.actual\n\n /**\n * Suggest acceptable values that can be used instead of `actual`.\n *\n * @type {Array | null}\n */\n this.expected\n\n /**\n * Link to docs for the message.\n *\n * > 👉 **Note**: this must be an absolute URL that can be passed as `x`\n * > to `new URL(x)`.\n *\n * @type {string | null}\n */\n this.url\n\n /**\n * Long form description of the message (you should use markdown).\n *\n * @type {string | null}\n */\n this.note\n /* eslint-enable no-unused-expressions */\n }\n}\n\nVFileMessage.prototype.file = ''\nVFileMessage.prototype.name = ''\nVFileMessage.prototype.reason = ''\nVFileMessage.prototype.message = ''\nVFileMessage.prototype.stack = ''\nVFileMessage.prototype.fatal = null\nVFileMessage.prototype.column = null\nVFileMessage.prototype.line = null\nVFileMessage.prototype.source = null\nVFileMessage.prototype.ruleId = null\nVFileMessage.prototype.position = null\n","// A derivative work based on:\n// .\n// Which is licensed:\n//\n// MIT License\n//\n// Copyright (c) 2013 James Halliday\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy of\n// this software and associated documentation files (the \"Software\"), to deal in\n// the Software without restriction, including without limitation the rights to\n// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n// the Software, and to permit persons to whom the Software is furnished to do so,\n// subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\n// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\n// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\n// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n// A derivative work based on:\n//\n// Parts of that are extracted from Node’s internal `path` module:\n// .\n// Which is licensed:\n//\n// Copyright Joyent, Inc. and other Node contributors.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a\n// copy of this software and associated documentation files (the\n// \"Software\"), to deal in the Software without restriction, including\n// without limitation the rights to use, copy, modify, merge, publish,\n// distribute, sublicense, and/or sell copies of the Software, and to permit\n// persons to whom the Software is furnished to do so, subject to the\n// following conditions:\n//\n// The above copyright notice and this permission notice shall be included\n// in all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN\n// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\n// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE\n// USE OR OTHER DEALINGS IN THE SOFTWARE.\n\nexport const path = {basename, dirname, extname, join, sep: '/'}\n\n/* eslint-disable max-depth, complexity */\n\n/**\n * Get the basename from a path.\n *\n * @param {string} path\n * File path.\n * @param {string | undefined} [ext]\n * Extension to strip.\n * @returns {string}\n * Stem or basename.\n */\nfunction basename(path, ext) {\n if (ext !== undefined && typeof ext !== 'string') {\n throw new TypeError('\"ext\" argument must be a string')\n }\n\n assertPath(path)\n let start = 0\n let end = -1\n let index = path.length\n /** @type {boolean | undefined} */\n let seenNonSlash\n\n if (ext === undefined || ext.length === 0 || ext.length > path.length) {\n while (index--) {\n if (path.charCodeAt(index) === 47 /* `/` */) {\n // If we reached a path separator that was not part of a set of path\n // separators at the end of the string, stop now.\n if (seenNonSlash) {\n start = index + 1\n break\n }\n } else if (end < 0) {\n // We saw the first non-path separator, mark this as the end of our\n // path component.\n seenNonSlash = true\n end = index + 1\n }\n }\n\n return end < 0 ? '' : path.slice(start, end)\n }\n\n if (ext === path) {\n return ''\n }\n\n let firstNonSlashEnd = -1\n let extIndex = ext.length - 1\n\n while (index--) {\n if (path.charCodeAt(index) === 47 /* `/` */) {\n // If we reached a path separator that was not part of a set of path\n // separators at the end of the string, stop now.\n if (seenNonSlash) {\n start = index + 1\n break\n }\n } else {\n if (firstNonSlashEnd < 0) {\n // We saw the first non-path separator, remember this index in case\n // we need it if the extension ends up not matching.\n seenNonSlash = true\n firstNonSlashEnd = index + 1\n }\n\n if (extIndex > -1) {\n // Try to match the explicit extension.\n if (path.charCodeAt(index) === ext.charCodeAt(extIndex--)) {\n if (extIndex < 0) {\n // We matched the extension, so mark this as the end of our path\n // component\n end = index\n }\n } else {\n // Extension does not match, so our result is the entire path\n // component\n extIndex = -1\n end = firstNonSlashEnd\n }\n }\n }\n }\n\n if (start === end) {\n end = firstNonSlashEnd\n } else if (end < 0) {\n end = path.length\n }\n\n return path.slice(start, end)\n}\n\n/**\n * Get the dirname from a path.\n *\n * @param {string} path\n * File path.\n * @returns {string}\n * File path.\n */\nfunction dirname(path) {\n assertPath(path)\n\n if (path.length === 0) {\n return '.'\n }\n\n let end = -1\n let index = path.length\n /** @type {boolean | undefined} */\n let unmatchedSlash\n\n // Prefix `--` is important to not run on `0`.\n while (--index) {\n if (path.charCodeAt(index) === 47 /* `/` */) {\n if (unmatchedSlash) {\n end = index\n break\n }\n } else if (!unmatchedSlash) {\n // We saw the first non-path separator\n unmatchedSlash = true\n }\n }\n\n return end < 0\n ? path.charCodeAt(0) === 47 /* `/` */\n ? '/'\n : '.'\n : end === 1 && path.charCodeAt(0) === 47 /* `/` */\n ? '//'\n : path.slice(0, end)\n}\n\n/**\n * Get an extname from a path.\n *\n * @param {string} path\n * File path.\n * @returns {string}\n * Extname.\n */\nfunction extname(path) {\n assertPath(path)\n\n let index = path.length\n\n let end = -1\n let startPart = 0\n let startDot = -1\n // Track the state of characters (if any) we see before our first dot and\n // after any path separator we find.\n let preDotState = 0\n /** @type {boolean | undefined} */\n let unmatchedSlash\n\n while (index--) {\n const code = path.charCodeAt(index)\n\n if (code === 47 /* `/` */) {\n // If we reached a path separator that was not part of a set of path\n // separators at the end of the string, stop now.\n if (unmatchedSlash) {\n startPart = index + 1\n break\n }\n\n continue\n }\n\n if (end < 0) {\n // We saw the first non-path separator, mark this as the end of our\n // extension.\n unmatchedSlash = true\n end = index + 1\n }\n\n if (code === 46 /* `.` */) {\n // If this is our first dot, mark it as the start of our extension.\n if (startDot < 0) {\n startDot = index\n } else if (preDotState !== 1) {\n preDotState = 1\n }\n } else if (startDot > -1) {\n // We saw a non-dot and non-path separator before our dot, so we should\n // have a good chance at having a non-empty extension.\n preDotState = -1\n }\n }\n\n if (\n startDot < 0 ||\n end < 0 ||\n // We saw a non-dot character immediately before the dot.\n preDotState === 0 ||\n // The (right-most) trimmed path component is exactly `..`.\n (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)\n ) {\n return ''\n }\n\n return path.slice(startDot, end)\n}\n\n/**\n * Join segments from a path.\n *\n * @param {Array} segments\n * Path segments.\n * @returns {string}\n * File path.\n */\nfunction join(...segments) {\n let index = -1\n /** @type {string | undefined} */\n let joined\n\n while (++index < segments.length) {\n assertPath(segments[index])\n\n if (segments[index]) {\n joined =\n joined === undefined ? segments[index] : joined + '/' + segments[index]\n }\n }\n\n return joined === undefined ? '.' : normalize(joined)\n}\n\n/**\n * Normalize a basic file path.\n *\n * @param {string} path\n * File path.\n * @returns {string}\n * File path.\n */\n// Note: `normalize` is not exposed as `path.normalize`, so some code is\n// manually removed from it.\nfunction normalize(path) {\n assertPath(path)\n\n const absolute = path.charCodeAt(0) === 47 /* `/` */\n\n // Normalize the path according to POSIX rules.\n let value = normalizeString(path, !absolute)\n\n if (value.length === 0 && !absolute) {\n value = '.'\n }\n\n if (value.length > 0 && path.charCodeAt(path.length - 1) === 47 /* / */) {\n value += '/'\n }\n\n return absolute ? '/' + value : value\n}\n\n/**\n * Resolve `.` and `..` elements in a path with directory names.\n *\n * @param {string} path\n * File path.\n * @param {boolean} allowAboveRoot\n * Whether `..` can move above root.\n * @returns {string}\n * File path.\n */\nfunction normalizeString(path, allowAboveRoot) {\n let result = ''\n let lastSegmentLength = 0\n let lastSlash = -1\n let dots = 0\n let index = -1\n /** @type {number | undefined} */\n let code\n /** @type {number} */\n let lastSlashIndex\n\n while (++index <= path.length) {\n if (index < path.length) {\n code = path.charCodeAt(index)\n } else if (code === 47 /* `/` */) {\n break\n } else {\n code = 47 /* `/` */\n }\n\n if (code === 47 /* `/` */) {\n if (lastSlash === index - 1 || dots === 1) {\n // Empty.\n } else if (lastSlash !== index - 1 && dots === 2) {\n if (\n result.length < 2 ||\n lastSegmentLength !== 2 ||\n result.charCodeAt(result.length - 1) !== 46 /* `.` */ ||\n result.charCodeAt(result.length - 2) !== 46 /* `.` */\n ) {\n if (result.length > 2) {\n lastSlashIndex = result.lastIndexOf('/')\n\n if (lastSlashIndex !== result.length - 1) {\n if (lastSlashIndex < 0) {\n result = ''\n lastSegmentLength = 0\n } else {\n result = result.slice(0, lastSlashIndex)\n lastSegmentLength = result.length - 1 - result.lastIndexOf('/')\n }\n\n lastSlash = index\n dots = 0\n continue\n }\n } else if (result.length > 0) {\n result = ''\n lastSegmentLength = 0\n lastSlash = index\n dots = 0\n continue\n }\n }\n\n if (allowAboveRoot) {\n result = result.length > 0 ? result + '/..' : '..'\n lastSegmentLength = 2\n }\n } else {\n if (result.length > 0) {\n result += '/' + path.slice(lastSlash + 1, index)\n } else {\n result = path.slice(lastSlash + 1, index)\n }\n\n lastSegmentLength = index - lastSlash - 1\n }\n\n lastSlash = index\n dots = 0\n } else if (code === 46 /* `.` */ && dots > -1) {\n dots++\n } else {\n dots = -1\n }\n }\n\n return result\n}\n\n/**\n * Make sure `path` is a string.\n *\n * @param {string} path\n * File path.\n * @returns {asserts path is string}\n * Nothing.\n */\nfunction assertPath(path) {\n if (typeof path !== 'string') {\n throw new TypeError(\n 'Path must be a string. Received ' + JSON.stringify(path)\n )\n }\n}\n\n/* eslint-enable max-depth, complexity */\n","// Somewhat based on:\n// .\n// But I don’t think one tiny line of code can be copyrighted. 😅\nexport const proc = {cwd}\n\nfunction cwd() {\n return '/'\n}\n","/**\n * @typedef URL\n * @property {string} hash\n * @property {string} host\n * @property {string} hostname\n * @property {string} href\n * @property {string} origin\n * @property {string} password\n * @property {string} pathname\n * @property {string} port\n * @property {string} protocol\n * @property {string} search\n * @property {any} searchParams\n * @property {string} username\n * @property {() => string} toString\n * @property {() => string} toJSON\n */\n\n/**\n * Check if `fileUrlOrPath` looks like a URL.\n *\n * @param {unknown} fileUrlOrPath\n * File path or URL.\n * @returns {fileUrlOrPath is URL}\n * Whether it’s a URL.\n */\n// From: \nexport function isUrl(fileUrlOrPath) {\n return (\n fileUrlOrPath !== null &&\n typeof fileUrlOrPath === 'object' &&\n // @ts-expect-error: indexable.\n fileUrlOrPath.href &&\n // @ts-expect-error: indexable.\n fileUrlOrPath.origin\n )\n}\n","/// \n\nimport {isUrl} from './minurl.shared.js'\n\n// See: \n\n/**\n * @param {string | URL} path\n * File URL.\n * @returns {string}\n * File URL.\n */\nexport function urlToPath(path) {\n if (typeof path === 'string') {\n path = new URL(path)\n } else if (!isUrl(path)) {\n /** @type {NodeJS.ErrnoException} */\n const error = new TypeError(\n 'The \"path\" argument must be of type string or an instance of URL. Received `' +\n path +\n '`'\n )\n error.code = 'ERR_INVALID_ARG_TYPE'\n throw error\n }\n\n if (path.protocol !== 'file:') {\n /** @type {NodeJS.ErrnoException} */\n const error = new TypeError('The URL must be of scheme file')\n error.code = 'ERR_INVALID_URL_SCHEME'\n throw error\n }\n\n return getPathFromURLPosix(path)\n}\n\n/**\n * Get a path from a POSIX URL.\n *\n * @param {URL} url\n * URL.\n * @returns {string}\n * File path.\n */\nfunction getPathFromURLPosix(url) {\n if (url.hostname !== '') {\n /** @type {NodeJS.ErrnoException} */\n const error = new TypeError(\n 'File URL host must be \"localhost\" or empty on darwin'\n )\n error.code = 'ERR_INVALID_FILE_URL_HOST'\n throw error\n }\n\n const pathname = url.pathname\n let index = -1\n\n while (++index < pathname.length) {\n if (\n pathname.charCodeAt(index) === 37 /* `%` */ &&\n pathname.charCodeAt(index + 1) === 50 /* `2` */\n ) {\n const third = pathname.charCodeAt(index + 2)\n if (third === 70 /* `F` */ || third === 102 /* `f` */) {\n /** @type {NodeJS.ErrnoException} */\n const error = new TypeError(\n 'File URL path must not include encoded / characters'\n )\n error.code = 'ERR_INVALID_FILE_URL_PATH'\n throw error\n }\n }\n }\n\n return decodeURIComponent(pathname)\n}\n\nexport {isUrl} from './minurl.shared.js'\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Position} Position\n * @typedef {import('unist').Point} Point\n * @typedef {import('./minurl.shared.js').URL} URL\n * @typedef {import('../index.js').Data} Data\n * @typedef {import('../index.js').Value} Value\n */\n\n/**\n * @typedef {Record & {type: string, position?: Position | undefined}} NodeLike\n *\n * @typedef {'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'} BufferEncoding\n * Encodings supported by the buffer class.\n *\n * This is a copy of the types from Node, copied to prevent Node globals from\n * being needed.\n * Copied from: \n *\n * @typedef {Options | URL | Value | VFile} Compatible\n * Things that can be passed to the constructor.\n *\n * @typedef VFileCoreOptions\n * Set multiple values.\n * @property {Value | null | undefined} [value]\n * Set `value`.\n * @property {string | null | undefined} [cwd]\n * Set `cwd`.\n * @property {Array | null | undefined} [history]\n * Set `history`.\n * @property {URL | string | null | undefined} [path]\n * Set `path`.\n * @property {string | null | undefined} [basename]\n * Set `basename`.\n * @property {string | null | undefined} [stem]\n * Set `stem`.\n * @property {string | null | undefined} [extname]\n * Set `extname`.\n * @property {string | null | undefined} [dirname]\n * Set `dirname`.\n * @property {Data | null | undefined} [data]\n * Set `data`.\n *\n * @typedef Map\n * Raw source map.\n *\n * See:\n * .\n * @property {number} version\n * Which version of the source map spec this map is following.\n * @property {Array} sources\n * An array of URLs to the original source files.\n * @property {Array} names\n * An array of identifiers which can be referenced by individual mappings.\n * @property {string | undefined} [sourceRoot]\n * The URL root from which all sources are relative.\n * @property {Array | undefined} [sourcesContent]\n * An array of contents of the original source files.\n * @property {string} mappings\n * A string of base64 VLQs which contain the actual mappings.\n * @property {string} file\n * The generated file this source map is associated with.\n *\n * @typedef {{[key: string]: unknown} & VFileCoreOptions} Options\n * Configuration.\n *\n * A bunch of keys that will be shallow copied over to the new file.\n *\n * @typedef {Record} ReporterSettings\n * Configuration for reporters.\n */\n\n/**\n * @template {ReporterSettings} Settings\n * Options type.\n * @callback Reporter\n * Type for a reporter.\n * @param {Array} files\n * Files to report.\n * @param {Settings} options\n * Configuration.\n * @returns {string}\n * Report.\n */\n\nimport bufferLike from 'is-buffer'\nimport {VFileMessage} from 'vfile-message'\nimport {path} from './minpath.js'\nimport {proc} from './minproc.js'\nimport {urlToPath, isUrl} from './minurl.js'\n\n/**\n * Order of setting (least specific to most), we need this because otherwise\n * `{stem: 'a', path: '~/b.js'}` would throw, as a path is needed before a\n * stem can be set.\n *\n * @type {Array<'basename' | 'dirname' | 'extname' | 'history' | 'path' | 'stem'>}\n */\nconst order = ['history', 'path', 'basename', 'stem', 'extname', 'dirname']\n\nexport class VFile {\n /**\n * Create a new virtual file.\n *\n * `options` is treated as:\n *\n * * `string` or `Buffer` — `{value: options}`\n * * `URL` — `{path: options}`\n * * `VFile` — shallow copies its data over to the new file\n * * `object` — all fields are shallow copied over to the new file\n *\n * Path related fields are set in the following order (least specific to\n * most specific): `history`, `path`, `basename`, `stem`, `extname`,\n * `dirname`.\n *\n * You cannot set `dirname` or `extname` without setting either `history`,\n * `path`, `basename`, or `stem` too.\n *\n * @param {Compatible | null | undefined} [value]\n * File value.\n * @returns\n * New instance.\n */\n constructor(value) {\n /** @type {Options | VFile} */\n let options\n\n if (!value) {\n options = {}\n } else if (typeof value === 'string' || buffer(value)) {\n options = {value}\n } else if (isUrl(value)) {\n options = {path: value}\n } else {\n options = value\n }\n\n /**\n * Place to store custom information (default: `{}`).\n *\n * It’s OK to store custom data directly on the file but moving it to\n * `data` is recommended.\n *\n * @type {Data}\n */\n this.data = {}\n\n /**\n * List of messages associated with the file.\n *\n * @type {Array}\n */\n this.messages = []\n\n /**\n * List of filepaths the file moved between.\n *\n * The first is the original path and the last is the current path.\n *\n * @type {Array}\n */\n this.history = []\n\n /**\n * Base of `path` (default: `process.cwd()` or `'/'` in browsers).\n *\n * @type {string}\n */\n this.cwd = proc.cwd()\n\n /* eslint-disable no-unused-expressions */\n /**\n * Raw value.\n *\n * @type {Value}\n */\n this.value\n\n // The below are non-standard, they are “well-known”.\n // As in, used in several tools.\n\n /**\n * Whether a file was saved to disk.\n *\n * This is used by vfile reporters.\n *\n * @type {boolean}\n */\n this.stored\n\n /**\n * Custom, non-string, compiled, representation.\n *\n * This is used by unified to store non-string results.\n * One example is when turning markdown into React nodes.\n *\n * @type {unknown}\n */\n this.result\n\n /**\n * Source map.\n *\n * This type is equivalent to the `RawSourceMap` type from the `source-map`\n * module.\n *\n * @type {Map | null | undefined}\n */\n this.map\n /* eslint-enable no-unused-expressions */\n\n // Set path related properties in the correct order.\n let index = -1\n\n while (++index < order.length) {\n const prop = order[index]\n\n // Note: we specifically use `in` instead of `hasOwnProperty` to accept\n // `vfile`s too.\n if (\n prop in options &&\n options[prop] !== undefined &&\n options[prop] !== null\n ) {\n // @ts-expect-error: TS doesn’t understand basic reality.\n this[prop] = prop === 'history' ? [...options[prop]] : options[prop]\n }\n }\n\n /** @type {string} */\n let prop\n\n // Set non-path related properties.\n for (prop in options) {\n // @ts-expect-error: fine to set other things.\n if (!order.includes(prop)) {\n // @ts-expect-error: fine to set other things.\n this[prop] = options[prop]\n }\n }\n }\n\n /**\n * Get the full path (example: `'~/index.min.js'`).\n *\n * @returns {string}\n */\n get path() {\n return this.history[this.history.length - 1]\n }\n\n /**\n * Set the full path (example: `'~/index.min.js'`).\n *\n * Cannot be nullified.\n * You can set a file URL (a `URL` object with a `file:` protocol) which will\n * be turned into a path with `url.fileURLToPath`.\n *\n * @param {string | URL} path\n */\n set path(path) {\n if (isUrl(path)) {\n path = urlToPath(path)\n }\n\n assertNonEmpty(path, 'path')\n\n if (this.path !== path) {\n this.history.push(path)\n }\n }\n\n /**\n * Get the parent path (example: `'~'`).\n */\n get dirname() {\n return typeof this.path === 'string' ? path.dirname(this.path) : undefined\n }\n\n /**\n * Set the parent path (example: `'~'`).\n *\n * Cannot be set if there’s no `path` yet.\n */\n set dirname(dirname) {\n assertPath(this.basename, 'dirname')\n this.path = path.join(dirname || '', this.basename)\n }\n\n /**\n * Get the basename (including extname) (example: `'index.min.js'`).\n */\n get basename() {\n return typeof this.path === 'string' ? path.basename(this.path) : undefined\n }\n\n /**\n * Set basename (including extname) (`'index.min.js'`).\n *\n * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\\'`\n * on windows).\n * Cannot be nullified (use `file.path = file.dirname` instead).\n */\n set basename(basename) {\n assertNonEmpty(basename, 'basename')\n assertPart(basename, 'basename')\n this.path = path.join(this.dirname || '', basename)\n }\n\n /**\n * Get the extname (including dot) (example: `'.js'`).\n */\n get extname() {\n return typeof this.path === 'string' ? path.extname(this.path) : undefined\n }\n\n /**\n * Set the extname (including dot) (example: `'.js'`).\n *\n * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\\'`\n * on windows).\n * Cannot be set if there’s no `path` yet.\n */\n set extname(extname) {\n assertPart(extname, 'extname')\n assertPath(this.dirname, 'extname')\n\n if (extname) {\n if (extname.charCodeAt(0) !== 46 /* `.` */) {\n throw new Error('`extname` must start with `.`')\n }\n\n if (extname.includes('.', 1)) {\n throw new Error('`extname` cannot contain multiple dots')\n }\n }\n\n this.path = path.join(this.dirname, this.stem + (extname || ''))\n }\n\n /**\n * Get the stem (basename w/o extname) (example: `'index.min'`).\n */\n get stem() {\n return typeof this.path === 'string'\n ? path.basename(this.path, this.extname)\n : undefined\n }\n\n /**\n * Set the stem (basename w/o extname) (example: `'index.min'`).\n *\n * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\\'`\n * on windows).\n * Cannot be nullified (use `file.path = file.dirname` instead).\n */\n set stem(stem) {\n assertNonEmpty(stem, 'stem')\n assertPart(stem, 'stem')\n this.path = path.join(this.dirname || '', stem + (this.extname || ''))\n }\n\n /**\n * Serialize the file.\n *\n * @param {BufferEncoding | null | undefined} [encoding='utf8']\n * Character encoding to understand `value` as when it’s a `Buffer`\n * (default: `'utf8'`).\n * @returns {string}\n * Serialized file.\n */\n toString(encoding) {\n return (this.value || '').toString(encoding || undefined)\n }\n\n /**\n * Create a warning message associated with the file.\n *\n * Its `fatal` is set to `false` and `file` is set to the current file path.\n * Its added to `file.messages`.\n *\n * @param {string | Error | VFileMessage} reason\n * Reason for message, uses the stack and message of the error if given.\n * @param {Node | NodeLike | Position | Point | null | undefined} [place]\n * Place in file where the message occurred.\n * @param {string | null | undefined} [origin]\n * Place in code where the message originates (example:\n * `'my-package:my-rule'` or `'my-rule'`).\n * @returns {VFileMessage}\n * Message.\n */\n message(reason, place, origin) {\n const message = new VFileMessage(reason, place, origin)\n\n if (this.path) {\n message.name = this.path + ':' + message.name\n message.file = this.path\n }\n\n message.fatal = false\n\n this.messages.push(message)\n\n return message\n }\n\n /**\n * Create an info message associated with the file.\n *\n * Its `fatal` is set to `null` and `file` is set to the current file path.\n * Its added to `file.messages`.\n *\n * @param {string | Error | VFileMessage} reason\n * Reason for message, uses the stack and message of the error if given.\n * @param {Node | NodeLike | Position | Point | null | undefined} [place]\n * Place in file where the message occurred.\n * @param {string | null | undefined} [origin]\n * Place in code where the message originates (example:\n * `'my-package:my-rule'` or `'my-rule'`).\n * @returns {VFileMessage}\n * Message.\n */\n info(reason, place, origin) {\n const message = this.message(reason, place, origin)\n\n message.fatal = null\n\n return message\n }\n\n /**\n * Create a fatal error associated with the file.\n *\n * Its `fatal` is set to `true` and `file` is set to the current file path.\n * Its added to `file.messages`.\n *\n * > 👉 **Note**: a fatal error means that a file is no longer processable.\n *\n * @param {string | Error | VFileMessage} reason\n * Reason for message, uses the stack and message of the error if given.\n * @param {Node | NodeLike | Position | Point | null | undefined} [place]\n * Place in file where the message occurred.\n * @param {string | null | undefined} [origin]\n * Place in code where the message originates (example:\n * `'my-package:my-rule'` or `'my-rule'`).\n * @returns {never}\n * Message.\n * @throws {VFileMessage}\n * Message.\n */\n fail(reason, place, origin) {\n const message = this.message(reason, place, origin)\n\n message.fatal = true\n\n throw message\n }\n}\n\n/**\n * Assert that `part` is not a path (as in, does not contain `path.sep`).\n *\n * @param {string | null | undefined} part\n * File path part.\n * @param {string} name\n * Part name.\n * @returns {void}\n * Nothing.\n */\nfunction assertPart(part, name) {\n if (part && part.includes(path.sep)) {\n throw new Error(\n '`' + name + '` cannot be a path: did not expect `' + path.sep + '`'\n )\n }\n}\n\n/**\n * Assert that `part` is not empty.\n *\n * @param {string | undefined} part\n * Thing.\n * @param {string} name\n * Part name.\n * @returns {asserts part is string}\n * Nothing.\n */\nfunction assertNonEmpty(part, name) {\n if (!part) {\n throw new Error('`' + name + '` cannot be empty')\n }\n}\n\n/**\n * Assert `path` exists.\n *\n * @param {string | undefined} path\n * Path.\n * @param {string} name\n * Dependency name.\n * @returns {asserts path is string}\n * Nothing.\n */\nfunction assertPath(path, name) {\n if (!path) {\n throw new Error('Setting `' + name + '` requires `path` to be set too')\n }\n}\n\n/**\n * Assert `value` is a buffer.\n *\n * @param {unknown} value\n * thing.\n * @returns {value is Buffer}\n * Whether `value` is a Node.js buffer.\n */\nfunction buffer(value) {\n return bufferLike(value)\n}\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('vfile').VFileCompatible} VFileCompatible\n * @typedef {import('vfile').VFileValue} VFileValue\n * @typedef {import('..').Processor} Processor\n * @typedef {import('..').Plugin} Plugin\n * @typedef {import('..').Preset} Preset\n * @typedef {import('..').Pluggable} Pluggable\n * @typedef {import('..').PluggableList} PluggableList\n * @typedef {import('..').Transformer} Transformer\n * @typedef {import('..').Parser} Parser\n * @typedef {import('..').Compiler} Compiler\n * @typedef {import('..').RunCallback} RunCallback\n * @typedef {import('..').ProcessCallback} ProcessCallback\n *\n * @typedef Context\n * @property {Node} tree\n * @property {VFile} file\n */\n\nimport {bail} from 'bail'\nimport isBuffer from 'is-buffer'\nimport extend from 'extend'\nimport isPlainObj from 'is-plain-obj'\nimport {trough} from 'trough'\nimport {VFile} from 'vfile'\n\n// Expose a frozen processor.\nexport const unified = base().freeze()\n\nconst own = {}.hasOwnProperty\n\n// Function to create the first processor.\n/**\n * @returns {Processor}\n */\nfunction base() {\n const transformers = trough()\n /** @type {Processor['attachers']} */\n const attachers = []\n /** @type {Record} */\n let namespace = {}\n /** @type {boolean|undefined} */\n let frozen\n let freezeIndex = -1\n\n // Data management.\n // @ts-expect-error: overloads are handled.\n processor.data = data\n processor.Parser = undefined\n processor.Compiler = undefined\n\n // Lock.\n processor.freeze = freeze\n\n // Plugins.\n processor.attachers = attachers\n // @ts-expect-error: overloads are handled.\n processor.use = use\n\n // API.\n processor.parse = parse\n processor.stringify = stringify\n // @ts-expect-error: overloads are handled.\n processor.run = run\n processor.runSync = runSync\n // @ts-expect-error: overloads are handled.\n processor.process = process\n processor.processSync = processSync\n\n // Expose.\n return processor\n\n // Create a new processor based on the processor in the current scope.\n /** @type {Processor} */\n function processor() {\n const destination = base()\n let index = -1\n\n while (++index < attachers.length) {\n destination.use(...attachers[index])\n }\n\n destination.data(extend(true, {}, namespace))\n\n return destination\n }\n\n /**\n * @param {string|Record} [key]\n * @param {unknown} [value]\n * @returns {unknown}\n */\n function data(key, value) {\n if (typeof key === 'string') {\n // Set `key`.\n if (arguments.length === 2) {\n assertUnfrozen('data', frozen)\n namespace[key] = value\n return processor\n }\n\n // Get `key`.\n return (own.call(namespace, key) && namespace[key]) || null\n }\n\n // Set space.\n if (key) {\n assertUnfrozen('data', frozen)\n namespace = key\n return processor\n }\n\n // Get space.\n return namespace\n }\n\n /** @type {Processor['freeze']} */\n function freeze() {\n if (frozen) {\n return processor\n }\n\n while (++freezeIndex < attachers.length) {\n const [attacher, ...options] = attachers[freezeIndex]\n\n if (options[0] === false) {\n continue\n }\n\n if (options[0] === true) {\n options[0] = undefined\n }\n\n /** @type {Transformer|void} */\n const transformer = attacher.call(processor, ...options)\n\n if (typeof transformer === 'function') {\n transformers.use(transformer)\n }\n }\n\n frozen = true\n freezeIndex = Number.POSITIVE_INFINITY\n\n return processor\n }\n\n /**\n * @param {Pluggable|null|undefined} [value]\n * @param {...unknown} options\n * @returns {Processor}\n */\n function use(value, ...options) {\n /** @type {Record|undefined} */\n let settings\n\n assertUnfrozen('use', frozen)\n\n if (value === null || value === undefined) {\n // Empty.\n } else if (typeof value === 'function') {\n addPlugin(value, ...options)\n } else if (typeof value === 'object') {\n if (Array.isArray(value)) {\n addList(value)\n } else {\n addPreset(value)\n }\n } else {\n throw new TypeError('Expected usable value, not `' + value + '`')\n }\n\n if (settings) {\n namespace.settings = Object.assign(namespace.settings || {}, settings)\n }\n\n return processor\n\n /**\n * @param {import('..').Pluggable} value\n * @returns {void}\n */\n function add(value) {\n if (typeof value === 'function') {\n addPlugin(value)\n } else if (typeof value === 'object') {\n if (Array.isArray(value)) {\n const [plugin, ...options] = value\n addPlugin(plugin, ...options)\n } else {\n addPreset(value)\n }\n } else {\n throw new TypeError('Expected usable value, not `' + value + '`')\n }\n }\n\n /**\n * @param {Preset} result\n * @returns {void}\n */\n function addPreset(result) {\n addList(result.plugins)\n\n if (result.settings) {\n settings = Object.assign(settings || {}, result.settings)\n }\n }\n\n /**\n * @param {PluggableList|null|undefined} [plugins]\n * @returns {void}\n */\n function addList(plugins) {\n let index = -1\n\n if (plugins === null || plugins === undefined) {\n // Empty.\n } else if (Array.isArray(plugins)) {\n while (++index < plugins.length) {\n const thing = plugins[index]\n add(thing)\n }\n } else {\n throw new TypeError('Expected a list of plugins, not `' + plugins + '`')\n }\n }\n\n /**\n * @param {Plugin} plugin\n * @param {...unknown} [value]\n * @returns {void}\n */\n function addPlugin(plugin, value) {\n let index = -1\n /** @type {Processor['attachers'][number]|undefined} */\n let entry\n\n while (++index < attachers.length) {\n if (attachers[index][0] === plugin) {\n entry = attachers[index]\n break\n }\n }\n\n if (entry) {\n if (isPlainObj(entry[1]) && isPlainObj(value)) {\n value = extend(true, entry[1], value)\n }\n\n entry[1] = value\n } else {\n // @ts-expect-error: fine.\n attachers.push([...arguments])\n }\n }\n }\n\n /** @type {Processor['parse']} */\n function parse(doc) {\n processor.freeze()\n const file = vfile(doc)\n const Parser = processor.Parser\n assertParser('parse', Parser)\n\n if (newable(Parser, 'parse')) {\n // @ts-expect-error: `newable` checks this.\n return new Parser(String(file), file).parse()\n }\n\n // @ts-expect-error: `newable` checks this.\n return Parser(String(file), file) // eslint-disable-line new-cap\n }\n\n /** @type {Processor['stringify']} */\n function stringify(node, doc) {\n processor.freeze()\n const file = vfile(doc)\n const Compiler = processor.Compiler\n assertCompiler('stringify', Compiler)\n assertNode(node)\n\n if (newable(Compiler, 'compile')) {\n // @ts-expect-error: `newable` checks this.\n return new Compiler(node, file).compile()\n }\n\n // @ts-expect-error: `newable` checks this.\n return Compiler(node, file) // eslint-disable-line new-cap\n }\n\n /**\n * @param {Node} node\n * @param {VFileCompatible|RunCallback} [doc]\n * @param {RunCallback} [callback]\n * @returns {Promise|void}\n */\n function run(node, doc, callback) {\n assertNode(node)\n processor.freeze()\n\n if (!callback && typeof doc === 'function') {\n callback = doc\n doc = undefined\n }\n\n if (!callback) {\n return new Promise(executor)\n }\n\n executor(null, callback)\n\n /**\n * @param {null|((node: Node) => void)} resolve\n * @param {(error: Error) => void} reject\n * @returns {void}\n */\n function executor(resolve, reject) {\n // @ts-expect-error: `doc` can’t be a callback anymore, we checked.\n transformers.run(node, vfile(doc), done)\n\n /**\n * @param {Error|null} error\n * @param {Node} tree\n * @param {VFile} file\n * @returns {void}\n */\n function done(error, tree, file) {\n tree = tree || node\n if (error) {\n reject(error)\n } else if (resolve) {\n resolve(tree)\n } else {\n // @ts-expect-error: `callback` is defined if `resolve` is not.\n callback(null, tree, file)\n }\n }\n }\n }\n\n /** @type {Processor['runSync']} */\n function runSync(node, file) {\n /** @type {Node|undefined} */\n let result\n /** @type {boolean|undefined} */\n let complete\n\n processor.run(node, file, done)\n\n assertDone('runSync', 'run', complete)\n\n // @ts-expect-error: we either bailed on an error or have a tree.\n return result\n\n /**\n * @param {Error|null} [error]\n * @param {Node} [tree]\n * @returns {void}\n */\n function done(error, tree) {\n bail(error)\n result = tree\n complete = true\n }\n }\n\n /**\n * @param {VFileCompatible} doc\n * @param {ProcessCallback} [callback]\n * @returns {Promise|undefined}\n */\n function process(doc, callback) {\n processor.freeze()\n assertParser('process', processor.Parser)\n assertCompiler('process', processor.Compiler)\n\n if (!callback) {\n return new Promise(executor)\n }\n\n executor(null, callback)\n\n /**\n * @param {null|((file: VFile) => void)} resolve\n * @param {(error?: Error|null|undefined) => void} reject\n * @returns {void}\n */\n function executor(resolve, reject) {\n const file = vfile(doc)\n\n processor.run(processor.parse(file), file, (error, tree, file) => {\n if (error || !tree || !file) {\n done(error)\n } else {\n /** @type {unknown} */\n const result = processor.stringify(tree, file)\n\n if (result === undefined || result === null) {\n // Empty.\n } else if (looksLikeAVFileValue(result)) {\n file.value = result\n } else {\n file.result = result\n }\n\n done(error, file)\n }\n })\n\n /**\n * @param {Error|null|undefined} [error]\n * @param {VFile|undefined} [file]\n * @returns {void}\n */\n function done(error, file) {\n if (error || !file) {\n reject(error)\n } else if (resolve) {\n resolve(file)\n } else {\n // @ts-expect-error: `callback` is defined if `resolve` is not.\n callback(null, file)\n }\n }\n }\n }\n\n /** @type {Processor['processSync']} */\n function processSync(doc) {\n /** @type {boolean|undefined} */\n let complete\n\n processor.freeze()\n assertParser('processSync', processor.Parser)\n assertCompiler('processSync', processor.Compiler)\n\n const file = vfile(doc)\n\n processor.process(file, done)\n\n assertDone('processSync', 'process', complete)\n\n return file\n\n /**\n * @param {Error|null|undefined} [error]\n * @returns {void}\n */\n function done(error) {\n complete = true\n bail(error)\n }\n }\n}\n\n/**\n * Check if `value` is a constructor.\n *\n * @param {unknown} value\n * @param {string} name\n * @returns {boolean}\n */\nfunction newable(value, name) {\n return (\n typeof value === 'function' &&\n // Prototypes do exist.\n // type-coverage:ignore-next-line\n value.prototype &&\n // A function with keys in its prototype is probably a constructor.\n // Classes’ prototype methods are not enumerable, so we check if some value\n // exists in the prototype.\n // type-coverage:ignore-next-line\n (keys(value.prototype) || name in value.prototype)\n )\n}\n\n/**\n * Check if `value` is an object with keys.\n *\n * @param {Record} value\n * @returns {boolean}\n */\nfunction keys(value) {\n /** @type {string} */\n let key\n\n for (key in value) {\n if (own.call(value, key)) {\n return true\n }\n }\n\n return false\n}\n\n/**\n * Assert a parser is available.\n *\n * @param {string} name\n * @param {unknown} value\n * @returns {asserts value is Parser}\n */\nfunction assertParser(name, value) {\n if (typeof value !== 'function') {\n throw new TypeError('Cannot `' + name + '` without `Parser`')\n }\n}\n\n/**\n * Assert a compiler is available.\n *\n * @param {string} name\n * @param {unknown} value\n * @returns {asserts value is Compiler}\n */\nfunction assertCompiler(name, value) {\n if (typeof value !== 'function') {\n throw new TypeError('Cannot `' + name + '` without `Compiler`')\n }\n}\n\n/**\n * Assert the processor is not frozen.\n *\n * @param {string} name\n * @param {unknown} frozen\n * @returns {asserts frozen is false}\n */\nfunction assertUnfrozen(name, frozen) {\n if (frozen) {\n throw new Error(\n 'Cannot call `' +\n name +\n '` on a frozen processor.\\nCreate a new processor first, by calling it: use `processor()` instead of `processor`.'\n )\n }\n}\n\n/**\n * Assert `node` is a unist node.\n *\n * @param {unknown} node\n * @returns {asserts node is Node}\n */\nfunction assertNode(node) {\n // `isPlainObj` unfortunately uses `any` instead of `unknown`.\n // type-coverage:ignore-next-line\n if (!isPlainObj(node) || typeof node.type !== 'string') {\n throw new TypeError('Expected node, got `' + node + '`')\n // Fine.\n }\n}\n\n/**\n * Assert that `complete` is `true`.\n *\n * @param {string} name\n * @param {string} asyncName\n * @param {unknown} complete\n * @returns {asserts complete is true}\n */\nfunction assertDone(name, asyncName, complete) {\n if (!complete) {\n throw new Error(\n '`' + name + '` finished async. Use `' + asyncName + '` instead'\n )\n }\n}\n\n/**\n * @param {VFileCompatible} [value]\n * @returns {VFile}\n */\nfunction vfile(value) {\n return looksLikeAVFile(value) ? value : new VFile(value)\n}\n\n/**\n * @param {VFileCompatible} [value]\n * @returns {value is VFile}\n */\nfunction looksLikeAVFile(value) {\n return Boolean(\n value &&\n typeof value === 'object' &&\n 'message' in value &&\n 'messages' in value\n )\n}\n\n/**\n * @param {unknown} [value]\n * @returns {value is VFileValue}\n */\nfunction looksLikeAVFileValue(value) {\n return typeof value === 'string' || isBuffer(value)\n}\n","/**\n * @typedef {import('unist').Node} Node\n */\n\n/**\n * @typedef {Array | string} ChildrenOrValue\n * List to use as `children` or value to use as `value`.\n *\n * @typedef {Record} Props\n * Other fields to add to the node.\n */\n\n/**\n * Build a node.\n *\n * @param type\n * Node type.\n * @param props\n * Fields assigned to node.\n * @param value\n * Children of node or value of `node` (cast to string).\n * @returns\n * Built node.\n */\nexport const u =\n /**\n * @type {(\n * ((type: T) => {type: T}) &\n * ((type: T, props: P) => {type: T} & P) &\n * ((type: T, value: string) => {type: T, value: string}) &\n * ((type: T, props: P, value: string) => {type: T, value: string} & P) &\n * (>(type: T, children: C) => {type: T, children: C}) &\n * (>(type: T, props: P, children: C) => {type: T, children: C} & P)\n * )}\n */\n (\n /**\n * @param {string} type\n * @param {Props | ChildrenOrValue | null | undefined} [props]\n * @param {ChildrenOrValue | null | undefined} [value]\n * @returns {Node}\n */\n function (type, props, value) {\n /** @type {Node} */\n const node = {type: String(type)}\n\n if (\n (value === undefined || value === null) &&\n (typeof props === 'string' || Array.isArray(props))\n ) {\n value = props\n } else {\n Object.assign(node, props)\n }\n\n if (Array.isArray(value)) {\n // @ts-expect-error: create a parent.\n node.children = value\n } else if (value !== undefined && value !== null) {\n // @ts-expect-error: create a literal.\n node.value = String(value)\n }\n\n return node\n }\n )\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Point} Point\n * @typedef {import('unist').Position} Position\n */\n\n/**\n * @typedef NodeLike\n * @property {string} type\n * @property {PositionLike | null | undefined} [position]\n *\n * @typedef PositionLike\n * @property {PointLike | null | undefined} [start]\n * @property {PointLike | null | undefined} [end]\n *\n * @typedef PointLike\n * @property {number | null | undefined} [line]\n * @property {number | null | undefined} [column]\n * @property {number | null | undefined} [offset]\n */\n\n/**\n * Serialize the positional info of a point, position (start and end points),\n * or node.\n *\n * @param {Node | NodeLike | Position | PositionLike | Point | PointLike | null | undefined} [value]\n * Node, position, or point.\n * @returns {string}\n * Pretty printed positional info of a node (`string`).\n *\n * In the format of a range `ls:cs-le:ce` (when given `node` or `position`)\n * or a point `l:c` (when given `point`), where `l` stands for line, `c` for\n * column, `s` for `start`, and `e` for end.\n * An empty string (`''`) is returned if the given value is neither `node`,\n * `position`, nor `point`.\n */\nexport function stringifyPosition(value) {\n // Nothing.\n if (!value || typeof value !== 'object') {\n return ''\n }\n\n // Node.\n if ('position' in value || 'type' in value) {\n return position(value.position)\n }\n\n // Position.\n if ('start' in value || 'end' in value) {\n return position(value)\n }\n\n // Point.\n if ('line' in value || 'column' in value) {\n return point(value)\n }\n\n // ?\n return ''\n}\n\n/**\n * @param {Point | PointLike | null | undefined} point\n * @returns {string}\n */\nfunction point(point) {\n return index(point && point.line) + ':' + index(point && point.column)\n}\n\n/**\n * @param {Position | PositionLike | null | undefined} pos\n * @returns {string}\n */\nfunction position(pos) {\n return point(pos && pos.start) + '-' + point(pos && pos.end)\n}\n\n/**\n * @param {number | null | undefined} value\n * @returns {number}\n */\nfunction index(value) {\n return value && typeof value === 'number' ? value : 1\n}\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n */\n\n/**\n * @typedef {Record} Props\n * @typedef {null | undefined | string | Props | TestFunctionAnything | Array} Test\n * Check for an arbitrary node, unaware of TypeScript inferral.\n *\n * @callback TestFunctionAnything\n * Check if a node passes a test, unaware of TypeScript inferral.\n * @param {unknown} this\n * The given context.\n * @param {Node} node\n * A node.\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {boolean | void}\n * Whether this node passes the test.\n */\n\n/**\n * @template {Node} Kind\n * Node type.\n * @typedef {Kind['type'] | Partial | TestFunctionPredicate | Array | TestFunctionPredicate>} PredicateTest\n * Check for a node that can be inferred by TypeScript.\n */\n\n/**\n * Check if a node passes a certain test.\n *\n * @template {Node} Kind\n * Node type.\n * @callback TestFunctionPredicate\n * Complex test function for a node that can be inferred by TypeScript.\n * @param {Node} node\n * A node.\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {node is Kind}\n * Whether this node passes the test.\n */\n\n/**\n * @callback AssertAnything\n * Check that an arbitrary value is a node, unaware of TypeScript inferral.\n * @param {unknown} [node]\n * Anything (typically a node).\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {boolean}\n * Whether this is a node and passes a test.\n */\n\n/**\n * Check if a node is a node and passes a certain node test.\n *\n * @template {Node} Kind\n * Node type.\n * @callback AssertPredicate\n * Check that an arbitrary value is a specific node, aware of TypeScript.\n * @param {unknown} [node]\n * Anything (typically a node).\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {node is Kind}\n * Whether this is a node and passes a test.\n */\n\n/**\n * Check if `node` is a `Node` and whether it passes the given test.\n *\n * @param node\n * Thing to check, typically `Node`.\n * @param test\n * A check for a specific node.\n * @param index\n * The node’s position in its parent.\n * @param parent\n * The node’s parent.\n * @returns\n * Whether `node` is a node and passes a test.\n */\nexport const is =\n /**\n * @type {(\n * (() => false) &\n * ((node: unknown, test: PredicateTest, index: number, parent: Parent, context?: unknown) => node is Kind) &\n * ((node: unknown, test: PredicateTest, index?: null | undefined, parent?: null | undefined, context?: unknown) => node is Kind) &\n * ((node: unknown, test: Test, index: number, parent: Parent, context?: unknown) => boolean) &\n * ((node: unknown, test?: Test, index?: null | undefined, parent?: null | undefined, context?: unknown) => boolean)\n * )}\n */\n (\n /**\n * @param {unknown} [node]\n * @param {Test} [test]\n * @param {number | null | undefined} [index]\n * @param {Parent | null | undefined} [parent]\n * @param {unknown} [context]\n * @returns {boolean}\n */\n // eslint-disable-next-line max-params\n function is(node, test, index, parent, context) {\n const check = convert(test)\n\n if (\n index !== undefined &&\n index !== null &&\n (typeof index !== 'number' ||\n index < 0 ||\n index === Number.POSITIVE_INFINITY)\n ) {\n throw new Error('Expected positive finite index')\n }\n\n if (\n parent !== undefined &&\n parent !== null &&\n (!is(parent) || !parent.children)\n ) {\n throw new Error('Expected parent node')\n }\n\n if (\n (parent === undefined || parent === null) !==\n (index === undefined || index === null)\n ) {\n throw new Error('Expected both parent and index')\n }\n\n // @ts-expect-error Looks like a node.\n return node && node.type && typeof node.type === 'string'\n ? Boolean(check.call(context, node, index, parent))\n : false\n }\n )\n\n/**\n * Generate an assertion from a test.\n *\n * Useful if you’re going to test many nodes, for example when creating a\n * utility where something else passes a compatible test.\n *\n * The created function is a bit faster because it expects valid input only:\n * a `node`, `index`, and `parent`.\n *\n * @param test\n * * when nullish, checks if `node` is a `Node`.\n * * when `string`, works like passing `(node) => node.type === test`.\n * * when `function` checks if function passed the node is true.\n * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.\n * * when `array`, checks if any one of the subtests pass.\n * @returns\n * An assertion.\n */\nexport const convert =\n /**\n * @type {(\n * ((test: PredicateTest) => AssertPredicate) &\n * ((test?: Test) => AssertAnything)\n * )}\n */\n (\n /**\n * @param {Test} [test]\n * @returns {AssertAnything}\n */\n function (test) {\n if (test === undefined || test === null) {\n return ok\n }\n\n if (typeof test === 'string') {\n return typeFactory(test)\n }\n\n if (typeof test === 'object') {\n return Array.isArray(test) ? anyFactory(test) : propsFactory(test)\n }\n\n if (typeof test === 'function') {\n return castFactory(test)\n }\n\n throw new Error('Expected function, string, or object as test')\n }\n )\n\n/**\n * @param {Array} tests\n * @returns {AssertAnything}\n */\nfunction anyFactory(tests) {\n /** @type {Array} */\n const checks = []\n let index = -1\n\n while (++index < tests.length) {\n checks[index] = convert(tests[index])\n }\n\n return castFactory(any)\n\n /**\n * @this {unknown}\n * @param {Array} parameters\n * @returns {boolean}\n */\n function any(...parameters) {\n let index = -1\n\n while (++index < checks.length) {\n if (checks[index].call(this, ...parameters)) return true\n }\n\n return false\n }\n}\n\n/**\n * Turn an object into a test for a node with a certain fields.\n *\n * @param {Props} check\n * @returns {AssertAnything}\n */\nfunction propsFactory(check) {\n return castFactory(all)\n\n /**\n * @param {Node} node\n * @returns {boolean}\n */\n function all(node) {\n /** @type {string} */\n let key\n\n for (key in check) {\n // @ts-expect-error: hush, it sure works as an index.\n if (node[key] !== check[key]) return false\n }\n\n return true\n }\n}\n\n/**\n * Turn a string into a test for a node with a certain type.\n *\n * @param {string} check\n * @returns {AssertAnything}\n */\nfunction typeFactory(check) {\n return castFactory(type)\n\n /**\n * @param {Node} node\n */\n function type(node) {\n return node && node.type === check\n }\n}\n\n/**\n * Turn a custom test into a test for a node that passes that test.\n *\n * @param {TestFunctionAnything} check\n * @returns {AssertAnything}\n */\nfunction castFactory(check) {\n return assertion\n\n /**\n * @this {unknown}\n * @param {unknown} node\n * @param {Array} parameters\n * @returns {boolean}\n */\n function assertion(node, ...parameters) {\n return Boolean(\n node &&\n typeof node === 'object' &&\n 'type' in node &&\n // @ts-expect-error: fine.\n Boolean(check.call(this, node, ...parameters))\n )\n }\n}\n\nfunction ok() {\n return true\n}\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n * @typedef {import('unist-util-is').Test} Test\n */\n\n/**\n * @typedef {boolean | 'skip'} Action\n * Union of the action types.\n *\n * @typedef {number} Index\n * Move to the sibling at `index` next (after node itself is completely\n * traversed).\n *\n * Useful if mutating the tree, such as removing the node the visitor is\n * currently on, or any of its previous siblings.\n * Results less than 0 or greater than or equal to `children.length` stop\n * traversing the parent.\n *\n * @typedef {[(Action | null | undefined | void)?, (Index | null | undefined)?]} ActionTuple\n * List with one or two values, the first an action, the second an index.\n *\n * @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult\n * Any value that can be returned from a visitor.\n */\n\n/**\n * @template {Node} [Visited=Node]\n * Visited node type.\n * @template {Parent} [Ancestor=Parent]\n * Ancestor type.\n * @callback Visitor\n * Handle a node (matching `test`, if given).\n *\n * Visitors are free to transform `node`.\n * They can also transform the parent of node (the last of `ancestors`).\n *\n * Replacing `node` itself, if `SKIP` is not returned, still causes its\n * descendants to be walked (which is a bug).\n *\n * When adding or removing previous siblings of `node` (or next siblings, in\n * case of reverse), the `Visitor` should return a new `Index` to specify the\n * sibling to traverse after `node` is traversed.\n * Adding or removing next siblings of `node` (or previous siblings, in case\n * of reverse) is handled as expected without needing to return a new `Index`.\n *\n * Removing the children property of an ancestor still results in them being\n * traversed.\n * @param {Visited} node\n * Found node.\n * @param {Array} ancestors\n * Ancestors of `node`.\n * @returns {VisitorResult}\n * What to do next.\n *\n * An `Index` is treated as a tuple of `[CONTINUE, Index]`.\n * An `Action` is treated as a tuple of `[Action]`.\n *\n * Passing a tuple back only makes sense if the `Action` is `SKIP`.\n * When the `Action` is `EXIT`, that action can be returned.\n * When the `Action` is `CONTINUE`, `Index` can be returned.\n */\n\n/**\n * @template {Node} [Tree=Node]\n * Tree type.\n * @template {Test} [Check=string]\n * Test type.\n * @typedef {Visitor, Check>, Extract, Parent>>} BuildVisitor\n * Build a typed `Visitor` function from a tree and a test.\n *\n * It will infer which values are passed as `node` and which as `parents`.\n */\n\nimport {convert} from 'unist-util-is'\nimport {color} from './color.js'\n\n/**\n * Continue traversing as normal.\n */\nexport const CONTINUE = true\n\n/**\n * Stop traversing immediately.\n */\nexport const EXIT = false\n\n/**\n * Do not traverse this node’s children.\n */\nexport const SKIP = 'skip'\n\n/**\n * Visit nodes, with ancestral information.\n *\n * This algorithm performs *depth-first* *tree traversal* in *preorder*\n * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).\n *\n * You can choose for which nodes `visitor` is called by passing a `test`.\n * For complex tests, you should test yourself in `visitor`, as it will be\n * faster and will have improved type information.\n *\n * Walking the tree is an intensive task.\n * Make use of the return values of the visitor when possible.\n * Instead of walking a tree multiple times, walk it once, use `unist-util-is`\n * to check if a node matches, and then perform different operations.\n *\n * You can change the tree.\n * See `Visitor` for more info.\n *\n * @param tree\n * Tree to traverse.\n * @param test\n * `unist-util-is`-compatible test\n * @param visitor\n * Handle each node.\n * @param reverse\n * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).\n * @returns\n * Nothing.\n */\nexport const visitParents =\n /**\n * @type {(\n * ((tree: Tree, test: Check, visitor: BuildVisitor, reverse?: boolean | null | undefined) => void) &\n * ((tree: Tree, visitor: BuildVisitor, reverse?: boolean | null | undefined) => void)\n * )}\n */\n (\n /**\n * @param {Node} tree\n * @param {Test} test\n * @param {Visitor} visitor\n * @param {boolean | null | undefined} [reverse]\n * @returns {void}\n */\n function (tree, test, visitor, reverse) {\n if (typeof test === 'function' && typeof visitor !== 'function') {\n reverse = visitor\n // @ts-expect-error no visitor given, so `visitor` is test.\n visitor = test\n test = null\n }\n\n const is = convert(test)\n const step = reverse ? -1 : 1\n\n factory(tree, undefined, [])()\n\n /**\n * @param {Node} node\n * @param {number | undefined} index\n * @param {Array} parents\n */\n function factory(node, index, parents) {\n /** @type {Record} */\n // @ts-expect-error: hush\n const value = node && typeof node === 'object' ? node : {}\n\n if (typeof value.type === 'string') {\n const name =\n // `hast`\n typeof value.tagName === 'string'\n ? value.tagName\n : // `xast`\n typeof value.name === 'string'\n ? value.name\n : undefined\n\n Object.defineProperty(visit, 'name', {\n value:\n 'node (' + color(node.type + (name ? '<' + name + '>' : '')) + ')'\n })\n }\n\n return visit\n\n function visit() {\n /** @type {ActionTuple} */\n let result = []\n /** @type {ActionTuple} */\n let subresult\n /** @type {number} */\n let offset\n /** @type {Array} */\n let grandparents\n\n if (!test || is(node, index, parents[parents.length - 1] || null)) {\n result = toResult(visitor(node, parents))\n\n if (result[0] === EXIT) {\n return result\n }\n }\n\n // @ts-expect-error looks like a parent.\n if (node.children && result[0] !== SKIP) {\n // @ts-expect-error looks like a parent.\n offset = (reverse ? node.children.length : -1) + step\n // @ts-expect-error looks like a parent.\n grandparents = parents.concat(node)\n\n // @ts-expect-error looks like a parent.\n while (offset > -1 && offset < node.children.length) {\n // @ts-expect-error looks like a parent.\n subresult = factory(node.children[offset], offset, grandparents)()\n\n if (subresult[0] === EXIT) {\n return subresult\n }\n\n offset =\n typeof subresult[1] === 'number' ? subresult[1] : offset + step\n }\n }\n\n return result\n }\n }\n }\n )\n\n/**\n * Turn a return value into a clean result.\n *\n * @param {VisitorResult} value\n * Valid return values from visitors.\n * @returns {ActionTuple}\n * Clean result.\n */\nfunction toResult(value) {\n if (Array.isArray(value)) {\n return value\n }\n\n if (typeof value === 'number') {\n return [CONTINUE, value]\n }\n\n return [value]\n}\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n * @typedef {import('unist-util-is').Test} Test\n * @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult\n */\n\n/**\n * Check if `Child` can be a child of `Ancestor`.\n *\n * Returns the ancestor when `Child` can be a child of `Ancestor`, or returns\n * `never`.\n *\n * @template {Node} Ancestor\n * Node type.\n * @template {Node} Child\n * Node type.\n * @typedef {(\n * Ancestor extends Parent\n * ? Child extends Ancestor['children'][number]\n * ? Ancestor\n * : never\n * : never\n * )} ParentsOf\n */\n\n/**\n * @template {Node} [Visited=Node]\n * Visited node type.\n * @template {Parent} [Ancestor=Parent]\n * Ancestor type.\n * @callback Visitor\n * Handle a node (matching `test`, if given).\n *\n * Visitors are free to transform `node`.\n * They can also transform `parent`.\n *\n * Replacing `node` itself, if `SKIP` is not returned, still causes its\n * descendants to be walked (which is a bug).\n *\n * When adding or removing previous siblings of `node` (or next siblings, in\n * case of reverse), the `Visitor` should return a new `Index` to specify the\n * sibling to traverse after `node` is traversed.\n * Adding or removing next siblings of `node` (or previous siblings, in case\n * of reverse) is handled as expected without needing to return a new `Index`.\n *\n * Removing the children property of `parent` still results in them being\n * traversed.\n * @param {Visited} node\n * Found node.\n * @param {Visited extends Node ? number | null : never} index\n * Index of `node` in `parent`.\n * @param {Ancestor extends Node ? Ancestor | null : never} parent\n * Parent of `node`.\n * @returns {VisitorResult}\n * What to do next.\n *\n * An `Index` is treated as a tuple of `[CONTINUE, Index]`.\n * An `Action` is treated as a tuple of `[Action]`.\n *\n * Passing a tuple back only makes sense if the `Action` is `SKIP`.\n * When the `Action` is `EXIT`, that action can be returned.\n * When the `Action` is `CONTINUE`, `Index` can be returned.\n */\n\n/**\n * Build a typed `Visitor` function from a node and all possible parents.\n *\n * It will infer which values are passed as `node` and which as `parent`.\n *\n * @template {Node} Visited\n * Node type.\n * @template {Parent} Ancestor\n * Parent type.\n * @typedef {Visitor>} BuildVisitorFromMatch\n */\n\n/**\n * Build a typed `Visitor` function from a list of descendants and a test.\n *\n * It will infer which values are passed as `node` and which as `parent`.\n *\n * @template {Node} Descendant\n * Node type.\n * @template {Test} Check\n * Test type.\n * @typedef {(\n * BuildVisitorFromMatch<\n * import('unist-util-visit-parents/complex-types.js').Matches,\n * Extract\n * >\n * )} BuildVisitorFromDescendants\n */\n\n/**\n * Build a typed `Visitor` function from a tree and a test.\n *\n * It will infer which values are passed as `node` and which as `parent`.\n *\n * @template {Node} [Tree=Node]\n * Node type.\n * @template {Test} [Check=string]\n * Test type.\n * @typedef {(\n * BuildVisitorFromDescendants<\n * import('unist-util-visit-parents/complex-types.js').InclusiveDescendant,\n * Check\n * >\n * )} BuildVisitor\n */\n\nimport {visitParents} from 'unist-util-visit-parents'\n\n/**\n * Visit nodes.\n *\n * This algorithm performs *depth-first* *tree traversal* in *preorder*\n * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).\n *\n * You can choose for which nodes `visitor` is called by passing a `test`.\n * For complex tests, you should test yourself in `visitor`, as it will be\n * faster and will have improved type information.\n *\n * Walking the tree is an intensive task.\n * Make use of the return values of the visitor when possible.\n * Instead of walking a tree multiple times, walk it once, use `unist-util-is`\n * to check if a node matches, and then perform different operations.\n *\n * You can change the tree.\n * See `Visitor` for more info.\n *\n * @param tree\n * Tree to traverse.\n * @param test\n * `unist-util-is`-compatible test\n * @param visitor\n * Handle each node.\n * @param reverse\n * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).\n * @returns\n * Nothing.\n */\nexport const visit =\n /**\n * @type {(\n * ((tree: Tree, test: Check, visitor: BuildVisitor, reverse?: boolean | null | undefined) => void) &\n * ((tree: Tree, visitor: BuildVisitor, reverse?: boolean | null | undefined) => void)\n * )}\n */\n (\n /**\n * @param {Node} tree\n * @param {Test} test\n * @param {Visitor} visitor\n * @param {boolean | null | undefined} [reverse]\n * @returns {void}\n */\n function (tree, test, visitor, reverse) {\n if (typeof test === 'function' && typeof visitor !== 'function') {\n reverse = visitor\n visitor = test\n test = null\n }\n\n visitParents(tree, test, overload, reverse)\n\n /**\n * @param {Node} node\n * @param {Array} parents\n */\n function overload(node, parents) {\n const parent = parents[parents.length - 1]\n return visitor(\n node,\n parent ? parent.children.indexOf(node) : null,\n parent\n )\n }\n }\n )\n\nexport {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'\n","import { updateYFragment } from './plugins/sync-plugin.js' // eslint-disable-line\nimport { ySyncPluginKey } from './plugins/keys.js'\nimport * as Y from 'yjs'\nimport { EditorView } from 'prosemirror-view' // eslint-disable-line\nimport { Node, Schema } from 'prosemirror-model' // eslint-disable-line\nimport * as error from 'lib0/error'\nimport * as map from 'lib0/map'\nimport * as eventloop from 'lib0/eventloop'\n\n/**\n * Either a node if type is YXmlElement or an Array of text nodes if YXmlText\n * @typedef {Map>} ProsemirrorMapping\n */\n\n/**\n * Is null if no timeout is in progress.\n * Is defined if a timeout is in progress.\n * Maps from view\n * @type {Map>|null}\n */\nlet viewsToUpdate = null\n\nconst updateMetas = () => {\n const ups = /** @type {Map>} */ (viewsToUpdate)\n viewsToUpdate = null\n ups.forEach((metas, view) => {\n const tr = view.state.tr\n const syncState = ySyncPluginKey.getState(view.state)\n if (syncState && syncState.binding && !syncState.binding.isDestroyed) {\n metas.forEach((val, key) => {\n tr.setMeta(key, val)\n })\n view.dispatch(tr)\n }\n })\n}\n\nexport const setMeta = (view, key, value) => {\n if (!viewsToUpdate) {\n viewsToUpdate = new Map()\n eventloop.timeout(0, updateMetas)\n }\n map.setIfUndefined(viewsToUpdate, view, map.create).set(key, value)\n}\n\n/**\n * Transforms a Prosemirror based absolute position to a Yjs Cursor (relative position in the Yjs model).\n *\n * @param {number} pos\n * @param {Y.XmlFragment} type\n * @param {ProsemirrorMapping} mapping\n * @return {any} relative position\n */\nexport const absolutePositionToRelativePosition = (pos, type, mapping) => {\n if (pos === 0) {\n return Y.createRelativePositionFromTypeIndex(type, 0)\n }\n /**\n * @type {any}\n */\n let n = type._first === null ? null : /** @type {Y.ContentType} */ (type._first.content).type\n while (n !== null && type !== n) {\n if (n instanceof Y.XmlText) {\n if (n._length >= pos) {\n return Y.createRelativePositionFromTypeIndex(n, pos)\n } else {\n pos -= n._length\n }\n if (n._item !== null && n._item.next !== null) {\n n = /** @type {Y.ContentType} */ (n._item.next.content).type\n } else {\n do {\n n = n._item === null ? null : n._item.parent\n pos--\n } while (n !== type && n !== null && n._item !== null && n._item.next === null)\n if (n !== null && n !== type) {\n // @ts-gnore we know that n.next !== null because of above loop conditition\n n = n._item === null ? null : /** @type {Y.ContentType} */ (/** @type Y.Item */ (n._item.next).content).type\n }\n }\n } else {\n const pNodeSize = /** @type {any} */ (mapping.get(n) || { nodeSize: 0 }).nodeSize\n if (n._first !== null && pos < pNodeSize) {\n n = /** @type {Y.ContentType} */ (n._first.content).type\n pos--\n } else {\n if (pos === 1 && n._length === 0 && pNodeSize > 1) {\n // edge case, should end in this paragraph\n return new Y.RelativePosition(n._item === null ? null : n._item.id, n._item === null ? Y.findRootTypeKey(n) : null, null)\n }\n pos -= pNodeSize\n if (n._item !== null && n._item.next !== null) {\n n = /** @type {Y.ContentType} */ (n._item.next.content).type\n } else {\n if (pos === 0) {\n // set to end of n.parent\n n = n._item === null ? n : n._item.parent\n return new Y.RelativePosition(n._item === null ? null : n._item.id, n._item === null ? Y.findRootTypeKey(n) : null, null)\n }\n do {\n n = /** @type {Y.Item} */ (n._item).parent\n pos--\n } while (n !== type && /** @type {Y.Item} */ (n._item).next === null)\n // if n is null at this point, we have an unexpected case\n if (n !== type) {\n // We know that n._item.next is defined because of above loop condition\n n = /** @type {Y.ContentType} */ (/** @type {Y.Item} */ (/** @type {Y.Item} */ (n._item).next).content).type\n }\n }\n }\n }\n if (n === null) {\n throw error.unexpectedCase()\n }\n if (pos === 0 && n.constructor !== Y.XmlText && n !== type) { // TODO: set to <= 0\n return createRelativePosition(n._item.parent, n._item)\n }\n }\n return Y.createRelativePositionFromTypeIndex(type, type._length)\n}\n\nconst createRelativePosition = (type, item) => {\n let typeid = null\n let tname = null\n if (type._item === null) {\n tname = Y.findRootTypeKey(type)\n } else {\n typeid = Y.createID(type._item.id.client, type._item.id.clock)\n }\n return new Y.RelativePosition(typeid, tname, item.id)\n}\n\n/**\n * @param {Y.Doc} y\n * @param {Y.XmlFragment} documentType Top level type that is bound to pView\n * @param {any} relPos Encoded Yjs based relative position\n * @param {ProsemirrorMapping} mapping\n * @return {null|number}\n */\nexport const relativePositionToAbsolutePosition = (y, documentType, relPos, mapping) => {\n const decodedPos = Y.createAbsolutePositionFromRelativePosition(relPos, y)\n if (decodedPos === null || (decodedPos.type !== documentType && !Y.isParentOf(documentType, decodedPos.type._item))) {\n return null\n }\n let type = decodedPos.type\n let pos = 0\n if (type.constructor === Y.XmlText) {\n pos = decodedPos.index\n } else if (type._item === null || !type._item.deleted) {\n let n = type._first\n let i = 0\n while (i < type._length && i < decodedPos.index && n !== null) {\n if (!n.deleted) {\n const t = /** @type {Y.ContentType} */ (n.content).type\n i++\n if (t instanceof Y.XmlText) {\n pos += t._length\n } else {\n pos += /** @type {any} */ (mapping.get(t)).nodeSize\n }\n }\n n = /** @type {Y.Item} */ (n.right)\n }\n pos += 1 // increase because we go out of n\n }\n while (type !== documentType && type._item !== null) {\n // @ts-ignore\n const parent = type._item.parent\n // @ts-ignore\n if (parent._item === null || !parent._item.deleted) {\n pos += 1 // the start tag\n let n = /** @type {Y.AbstractType} */ (parent)._first\n // now iterate until we found type\n while (n !== null) {\n const contentType = /** @type {Y.ContentType} */ (n.content).type\n if (contentType === type) {\n break\n }\n if (!n.deleted) {\n if (contentType instanceof Y.XmlText) {\n pos += contentType._length\n } else {\n pos += /** @type {any} */ (mapping.get(contentType)).nodeSize\n }\n }\n n = n.right\n }\n }\n type = /** @type {Y.AbstractType} */ (parent)\n }\n return pos - 1 // we don't count the most outer tag, because it is a fragment\n}\n\n/**\n * Utility method to convert a Prosemirror Doc Node into a Y.Doc.\n *\n * This can be used when importing existing content to Y.Doc for the first time,\n * note that this should not be used to rehydrate a Y.Doc from a database once\n * collaboration has begun as all history will be lost\n *\n * @param {Node} doc\n * @param {string} xmlFragment\n * @return {Y.Doc}\n */\nexport function prosemirrorToYDoc (doc, xmlFragment = 'prosemirror') {\n const ydoc = new Y.Doc()\n const type = /** @type {Y.XmlFragment} */ (ydoc.get(xmlFragment, Y.XmlFragment))\n if (!type.doc) {\n return ydoc\n }\n\n prosemirrorToYXmlFragment(doc, type)\n return type.doc\n}\n\n/**\n * Utility method to update an empty Y.XmlFragment with content from a Prosemirror Doc Node.\n *\n * This can be used when importing existing content to Y.Doc for the first time,\n * note that this should not be used to rehydrate a Y.Doc from a database once\n * collaboration has begun as all history will be lost\n *\n * Note: The Y.XmlFragment does not need to be part of a Y.Doc document at the time that this\n * method is called, but it must be added before any other operations are performed on it.\n *\n * @param {Node} doc prosemirror document.\n * @param {Y.XmlFragment} [xmlFragment] If supplied, an xml fragment to be\n * populated from the prosemirror state; otherwise a new XmlFragment will be created.\n * @return {Y.XmlFragment}\n */\nexport function prosemirrorToYXmlFragment (doc, xmlFragment) {\n const type = xmlFragment || new Y.XmlFragment()\n const ydoc = type.doc ? type.doc : { transact: (transaction) => transaction(undefined) }\n updateYFragment(ydoc, type, doc, new Map())\n return type\n}\n\n/**\n * Utility method to convert Prosemirror compatible JSON into a Y.Doc.\n *\n * This can be used when importing existing content to Y.Doc for the first time,\n * note that this should not be used to rehydrate a Y.Doc from a database once\n * collaboration has begun as all history will be lost\n *\n * @param {Schema} schema\n * @param {any} state\n * @param {string} xmlFragment\n * @return {Y.Doc}\n */\nexport function prosemirrorJSONToYDoc (schema, state, xmlFragment = 'prosemirror') {\n const doc = Node.fromJSON(schema, state)\n return prosemirrorToYDoc(doc, xmlFragment)\n}\n\n/**\n * Utility method to convert Prosemirror compatible JSON to a Y.XmlFragment\n *\n * This can be used when importing existing content to Y.Doc for the first time,\n * note that this should not be used to rehydrate a Y.Doc from a database once\n * collaboration has begun as all history will be lost\n *\n * @param {Schema} schema\n * @param {any} state\n * @param {Y.XmlFragment} [xmlFragment] If supplied, an xml fragment to be\n * populated from the prosemirror state; otherwise a new XmlFragment will be created.\n * @return {Y.XmlFragment}\n */\nexport function prosemirrorJSONToYXmlFragment (schema, state, xmlFragment) {\n const doc = Node.fromJSON(schema, state)\n return prosemirrorToYXmlFragment(doc, xmlFragment)\n}\n\n/**\n * Utility method to convert a Y.Doc to a Prosemirror Doc node.\n *\n * @param {Schema} schema\n * @param {Y.Doc} ydoc\n * @return {Node}\n */\nexport function yDocToProsemirror (schema, ydoc) {\n const state = yDocToProsemirrorJSON(ydoc)\n return Node.fromJSON(schema, state)\n}\n\n/**\n * Utility method to convert a Y.XmlFragment to a Prosemirror Doc node.\n *\n * @param {Schema} schema\n * @param {Y.XmlFragment} xmlFragment\n * @return {Node}\n */\nexport function yXmlFragmentToProsemirror (schema, xmlFragment) {\n const state = yXmlFragmentToProsemirrorJSON(xmlFragment)\n return Node.fromJSON(schema, state)\n}\n\n/**\n * Utility method to convert a Y.Doc to Prosemirror compatible JSON.\n *\n * @param {Y.Doc} ydoc\n * @param {string} xmlFragment\n * @return {Record}\n */\nexport function yDocToProsemirrorJSON (\n ydoc,\n xmlFragment = 'prosemirror'\n) {\n return yXmlFragmentToProsemirrorJSON(ydoc.getXmlFragment(xmlFragment))\n}\n\n/**\n * Utility method to convert a Y.Doc to Prosemirror compatible JSON.\n *\n * @param {Y.XmlFragment} xmlFragment The fragment, which must be part of a Y.Doc.\n * @return {Record}\n */\nexport function yXmlFragmentToProsemirrorJSON (xmlFragment) {\n const items = xmlFragment.toArray()\n\n function serialize (item) {\n /**\n * @type {Object} NodeObject\n * @property {string} NodeObject.type\n * @property {Record=} NodeObject.attrs\n * @property {Array=} NodeObject.content\n */\n let response\n\n // TODO: Must be a better way to detect text nodes than this\n if (!item.nodeName) {\n const delta = item.toDelta()\n response = delta.map((d) => {\n const text = {\n type: 'text',\n text: d.insert\n }\n\n if (d.attributes) {\n text.marks = Object.keys(d.attributes).map((type) => {\n const attrs = d.attributes[type]\n const mark = {\n type\n }\n\n if (Object.keys(attrs)) {\n mark.attrs = attrs\n }\n\n return mark\n })\n }\n return text\n })\n } else {\n response = {\n type: item.nodeName\n }\n\n const attrs = item.getAttributes()\n if (Object.keys(attrs).length) {\n response.attrs = attrs\n }\n\n const children = item.toArray()\n if (children.length) {\n response.content = children.map(serialize).flat()\n }\n }\n\n return response\n }\n\n return {\n type: 'doc',\n content: items.map(serialize)\n }\n}\n","\nimport { PluginKey } from 'prosemirror-state' // eslint-disable-line\n\n/**\n * The unique prosemirror plugin key for syncPlugin\n *\n * @public\n */\nexport const ySyncPluginKey = new PluginKey('y-sync')\n\n/**\n * The unique prosemirror plugin key for undoPlugin\n *\n * @public\n */\nexport const yUndoPluginKey = new PluginKey('y-undo')\n\n/**\n * The unique prosemirror plugin key for cursorPlugin\n *\n * @public\n */\nexport const yCursorPluginKey = new PluginKey('yjs-cursor')\n","/**\n * Utility helpers to work with promises.\n *\n * @module promise\n */\n\nimport * as time from './time.js'\n\n/**\n * @template T\n * @callback PromiseResolve\n * @param {T|PromiseLike} [result]\n */\n\n/**\n * @template T\n * @param {function(PromiseResolve,function(Error):void):any} f\n * @return {Promise}\n */\nexport const create = f => /** @type {Promise} */ (new Promise(f))\n\n/**\n * @param {function(function():void,function(Error):void):void} f\n * @return {Promise}\n */\nexport const createEmpty = f => new Promise(f)\n\n/**\n * `Promise.all` wait for all promises in the array to resolve and return the result\n * @template {unknown[] | []} PS\n *\n * @param {PS} ps\n * @return {Promise<{ -readonly [P in keyof PS]: Awaited }>}\n */\nexport const all = Promise.all.bind(Promise)\n\n/**\n * @param {Error} [reason]\n * @return {Promise}\n */\nexport const reject = reason => Promise.reject(reason)\n\n/**\n * @template T\n * @param {T|void} res\n * @return {Promise}\n */\nexport const resolve = res => Promise.resolve(res)\n\n/**\n * @template T\n * @param {T} res\n * @return {Promise}\n */\nexport const resolveWith = res => Promise.resolve(res)\n\n/**\n * @todo Next version, reorder parameters: check, [timeout, [intervalResolution]]\n *\n * @param {number} timeout\n * @param {function():boolean} check\n * @param {number} [intervalResolution]\n * @return {Promise}\n */\nexport const until = (timeout, check, intervalResolution = 10) => create((resolve, reject) => {\n const startTime = time.getUnixTime()\n const hasTimeout = timeout > 0\n const untilInterval = () => {\n if (check()) {\n clearInterval(intervalHandle)\n resolve()\n } else if (hasTimeout) {\n /* c8 ignore else */\n if (time.getUnixTime() - startTime > timeout) {\n clearInterval(intervalHandle)\n reject(new Error('Timeout'))\n }\n }\n }\n const intervalHandle = setInterval(untilInterval, intervalResolution)\n})\n\n/**\n * @param {number} timeout\n * @return {Promise}\n */\nexport const wait = timeout => create((resolve, reject) => setTimeout(resolve, timeout))\n\n/**\n * Checks if an object is a promise using ducktyping.\n *\n * Promises are often polyfilled, so it makes sense to add some additional guarantees if the user of this\n * library has some insane environment where global Promise objects are overwritten.\n *\n * @param {any} p\n * @return {boolean}\n */\nexport const isPromise = p => p instanceof Promise || (p && p.then && p.catch && p.finally)\n","/**\n * Working with value pairs.\n *\n * @module pair\n */\n\n/**\n * @template L,R\n */\nexport class Pair {\n /**\n * @param {L} left\n * @param {R} right\n */\n constructor (left, right) {\n this.left = left\n this.right = right\n }\n}\n\n/**\n * @template L,R\n * @param {L} left\n * @param {R} right\n * @return {Pair}\n */\nexport const create = (left, right) => new Pair(left, right)\n\n/**\n * @template L,R\n * @param {R} right\n * @param {L} left\n * @return {Pair}\n */\nexport const createReversed = (right, left) => new Pair(left, right)\n\n/**\n * @template L,R\n * @param {Array>} arr\n * @param {function(L, R):any} f\n */\nexport const forEach = (arr, f) => arr.forEach(p => f(p.left, p.right))\n\n/**\n * @template L,R,X\n * @param {Array>} arr\n * @param {function(L, R):X} f\n * @return {Array}\n */\nexport const map = (arr, f) => arr.map(p => f(p.left, p.right))\n","/**\n * Utility module to work with EcmaScript Symbols.\n *\n * @module symbol\n */\n\n/**\n * Return fresh symbol.\n *\n * @return {Symbol}\n */\nexport const create = Symbol\n\n/**\n * @param {any} s\n * @return {boolean}\n */\nexport const isSymbol = s => typeof s === 'symbol'\n","import * as symbol from './symbol.js'\nimport * as time from './time.js'\nimport * as env from './environment.js'\nimport * as func from './function.js'\n\nexport const BOLD = symbol.create()\nexport const UNBOLD = symbol.create()\nexport const BLUE = symbol.create()\nexport const GREY = symbol.create()\nexport const GREEN = symbol.create()\nexport const RED = symbol.create()\nexport const PURPLE = symbol.create()\nexport const ORANGE = symbol.create()\nexport const UNCOLOR = symbol.create()\n\n/* c8 ignore start */\n/**\n * @param {Array} args\n * @return {Array}\n */\nexport const computeNoColorLoggingArgs = args => {\n const strBuilder = []\n const logArgs = []\n // try with formatting until we find something unsupported\n let i = 0\n for (; i < args.length; i++) {\n const arg = args[i]\n if (arg.constructor === String || arg.constructor === Number) {\n strBuilder.push(arg)\n } else if (arg.constructor === Object) {\n logArgs.push(JSON.stringify(arg))\n }\n }\n return logArgs\n}\n/* c8 ignore stop */\n\nconst loggingColors = [GREEN, PURPLE, ORANGE, BLUE]\nlet nextColor = 0\nlet lastLoggingTime = time.getUnixTime()\n\n/* c8 ignore start */\n/**\n * @param {function(...any):void} _print\n * @param {string} moduleName\n * @return {function(...any):void}\n */\nexport const createModuleLogger = (_print, moduleName) => {\n const color = loggingColors[nextColor]\n const debugRegexVar = env.getVariable('log')\n const doLogging = debugRegexVar !== null &&\n (debugRegexVar === '*' || debugRegexVar === 'true' ||\n new RegExp(debugRegexVar, 'gi').test(moduleName))\n nextColor = (nextColor + 1) % loggingColors.length\n moduleName += ': '\n return !doLogging\n ? func.nop\n : (...args) => {\n const timeNow = time.getUnixTime()\n const timeDiff = timeNow - lastLoggingTime\n lastLoggingTime = timeNow\n _print(\n color,\n moduleName,\n UNCOLOR,\n ...args.map((arg) =>\n (typeof arg === 'string' || typeof arg === 'symbol')\n ? arg\n : JSON.stringify(arg)\n ),\n color,\n ' +' + timeDiff + 'ms'\n )\n }\n}\n/* c8 ignore stop */\n","/**\n * Isomorphic logging module with support for colors!\n *\n * @module logging\n */\n\nimport * as env from './environment.js'\nimport * as set from './set.js'\nimport * as pair from './pair.js'\nimport * as dom from './dom.js'\nimport * as json from './json.js'\nimport * as map from './map.js'\nimport * as eventloop from './eventloop.js'\nimport * as math from './math.js'\nimport * as common from './logging.common.js'\n\nexport { BOLD, UNBOLD, BLUE, GREY, GREEN, RED, PURPLE, ORANGE, UNCOLOR } from './logging.common.js'\n\n/**\n * @type {Object>}\n */\nconst _browserStyleMap = {\n [common.BOLD]: pair.create('font-weight', 'bold'),\n [common.UNBOLD]: pair.create('font-weight', 'normal'),\n [common.BLUE]: pair.create('color', 'blue'),\n [common.GREEN]: pair.create('color', 'green'),\n [common.GREY]: pair.create('color', 'grey'),\n [common.RED]: pair.create('color', 'red'),\n [common.PURPLE]: pair.create('color', 'purple'),\n [common.ORANGE]: pair.create('color', 'orange'), // not well supported in chrome when debugging node with inspector - TODO: deprecate\n [common.UNCOLOR]: pair.create('color', 'black')\n}\n\n/**\n * @param {Array} args\n * @return {Array}\n */\n/* c8 ignore start */\nconst computeBrowserLoggingArgs = (args) => {\n const strBuilder = []\n const styles = []\n const currentStyle = map.create()\n /**\n * @type {Array}\n */\n let logArgs = []\n // try with formatting until we find something unsupported\n let i = 0\n for (; i < args.length; i++) {\n const arg = args[i]\n // @ts-ignore\n const style = _browserStyleMap[arg]\n if (style !== undefined) {\n currentStyle.set(style.left, style.right)\n } else {\n if (arg.constructor === String || arg.constructor === Number) {\n const style = dom.mapToStyleString(currentStyle)\n if (i > 0 || style.length > 0) {\n strBuilder.push('%c' + arg)\n styles.push(style)\n } else {\n strBuilder.push(arg)\n }\n } else {\n break\n }\n }\n }\n if (i > 0) {\n // create logArgs with what we have so far\n logArgs = styles\n logArgs.unshift(strBuilder.join(''))\n }\n // append the rest\n for (; i < args.length; i++) {\n const arg = args[i]\n if (!(arg instanceof Symbol)) {\n logArgs.push(arg)\n }\n }\n return logArgs\n}\n/* c8 ignore stop */\n\n/* c8 ignore start */\nconst computeLoggingArgs = env.supportsColor\n ? computeBrowserLoggingArgs\n : common.computeNoColorLoggingArgs\n/* c8 ignore stop */\n\n/**\n * @param {Array} args\n */\nexport const print = (...args) => {\n console.log(...computeLoggingArgs(args))\n /* c8 ignore next */\n vconsoles.forEach((vc) => vc.print(args))\n}\n\n/* c8 ignore start */\n/**\n * @param {Array} args\n */\nexport const warn = (...args) => {\n console.warn(...computeLoggingArgs(args))\n args.unshift(common.ORANGE)\n vconsoles.forEach((vc) => vc.print(args))\n}\n/* c8 ignore stop */\n\n/**\n * @param {Error} err\n */\n/* c8 ignore start */\nexport const printError = (err) => {\n console.error(err)\n vconsoles.forEach((vc) => vc.printError(err))\n}\n/* c8 ignore stop */\n\n/**\n * @param {string} url image location\n * @param {number} height height of the image in pixel\n */\n/* c8 ignore start */\nexport const printImg = (url, height) => {\n if (env.isBrowser) {\n console.log(\n '%c ',\n `font-size: ${height}px; background-size: contain; background-repeat: no-repeat; background-image: url(${url})`\n )\n // console.log('%c ', `font-size: ${height}x; background: url(${url}) no-repeat;`)\n }\n vconsoles.forEach((vc) => vc.printImg(url, height))\n}\n/* c8 ignore stop */\n\n/**\n * @param {string} base64\n * @param {number} height\n */\n/* c8 ignore next 2 */\nexport const printImgBase64 = (base64, height) =>\n printImg(`data:image/gif;base64,${base64}`, height)\n\n/**\n * @param {Array} args\n */\nexport const group = (...args) => {\n console.group(...computeLoggingArgs(args))\n /* c8 ignore next */\n vconsoles.forEach((vc) => vc.group(args))\n}\n\n/**\n * @param {Array} args\n */\nexport const groupCollapsed = (...args) => {\n console.groupCollapsed(...computeLoggingArgs(args))\n /* c8 ignore next */\n vconsoles.forEach((vc) => vc.groupCollapsed(args))\n}\n\nexport const groupEnd = () => {\n console.groupEnd()\n /* c8 ignore next */\n vconsoles.forEach((vc) => vc.groupEnd())\n}\n\n/**\n * @param {function():Node} createNode\n */\n/* c8 ignore next 2 */\nexport const printDom = (createNode) =>\n vconsoles.forEach((vc) => vc.printDom(createNode()))\n\n/**\n * @param {HTMLCanvasElement} canvas\n * @param {number} height\n */\n/* c8 ignore next 2 */\nexport const printCanvas = (canvas, height) =>\n printImg(canvas.toDataURL(), height)\n\nexport const vconsoles = set.create()\n\n/**\n * @param {Array} args\n * @return {Array}\n */\n/* c8 ignore start */\nconst _computeLineSpans = (args) => {\n const spans = []\n const currentStyle = new Map()\n // try with formatting until we find something unsupported\n let i = 0\n for (; i < args.length; i++) {\n const arg = args[i]\n // @ts-ignore\n const style = _browserStyleMap[arg]\n if (style !== undefined) {\n currentStyle.set(style.left, style.right)\n } else {\n if (arg.constructor === String || arg.constructor === Number) {\n // @ts-ignore\n const span = dom.element('span', [\n pair.create('style', dom.mapToStyleString(currentStyle))\n ], [dom.text(arg.toString())])\n if (span.innerHTML === '') {\n span.innerHTML = ' '\n }\n spans.push(span)\n } else {\n break\n }\n }\n }\n // append the rest\n for (; i < args.length; i++) {\n let content = args[i]\n if (!(content instanceof Symbol)) {\n if (content.constructor !== String && content.constructor !== Number) {\n content = ' ' + json.stringify(content) + ' '\n }\n spans.push(\n dom.element('span', [], [dom.text(/** @type {string} */ (content))])\n )\n }\n }\n return spans\n}\n/* c8 ignore stop */\n\nconst lineStyle =\n 'font-family:monospace;border-bottom:1px solid #e2e2e2;padding:2px;'\n\n/* c8 ignore start */\nexport class VConsole {\n /**\n * @param {Element} dom\n */\n constructor (dom) {\n this.dom = dom\n /**\n * @type {Element}\n */\n this.ccontainer = this.dom\n this.depth = 0\n vconsoles.add(this)\n }\n\n /**\n * @param {Array} args\n * @param {boolean} collapsed\n */\n group (args, collapsed = false) {\n eventloop.enqueue(() => {\n const triangleDown = dom.element('span', [\n pair.create('hidden', collapsed),\n pair.create('style', 'color:grey;font-size:120%;')\n ], [dom.text('▼')])\n const triangleRight = dom.element('span', [\n pair.create('hidden', !collapsed),\n pair.create('style', 'color:grey;font-size:125%;')\n ], [dom.text('▶')])\n const content = dom.element(\n 'div',\n [pair.create(\n 'style',\n `${lineStyle};padding-left:${this.depth * 10}px`\n )],\n [triangleDown, triangleRight, dom.text(' ')].concat(\n _computeLineSpans(args)\n )\n )\n const nextContainer = dom.element('div', [\n pair.create('hidden', collapsed)\n ])\n const nextLine = dom.element('div', [], [content, nextContainer])\n dom.append(this.ccontainer, [nextLine])\n this.ccontainer = nextContainer\n this.depth++\n // when header is clicked, collapse/uncollapse container\n dom.addEventListener(content, 'click', (_event) => {\n nextContainer.toggleAttribute('hidden')\n triangleDown.toggleAttribute('hidden')\n triangleRight.toggleAttribute('hidden')\n })\n })\n }\n\n /**\n * @param {Array} args\n */\n groupCollapsed (args) {\n this.group(args, true)\n }\n\n groupEnd () {\n eventloop.enqueue(() => {\n if (this.depth > 0) {\n this.depth--\n // @ts-ignore\n this.ccontainer = this.ccontainer.parentElement.parentElement\n }\n })\n }\n\n /**\n * @param {Array} args\n */\n print (args) {\n eventloop.enqueue(() => {\n dom.append(this.ccontainer, [\n dom.element('div', [\n pair.create(\n 'style',\n `${lineStyle};padding-left:${this.depth * 10}px`\n )\n ], _computeLineSpans(args))\n ])\n })\n }\n\n /**\n * @param {Error} err\n */\n printError (err) {\n this.print([common.RED, common.BOLD, err.toString()])\n }\n\n /**\n * @param {string} url\n * @param {number} height\n */\n printImg (url, height) {\n eventloop.enqueue(() => {\n dom.append(this.ccontainer, [\n dom.element('img', [\n pair.create('src', url),\n pair.create('height', `${math.round(height * 1.5)}px`)\n ])\n ])\n })\n }\n\n /**\n * @param {Node} node\n */\n printDom (node) {\n eventloop.enqueue(() => {\n dom.append(this.ccontainer, [node])\n })\n }\n\n destroy () {\n eventloop.enqueue(() => {\n vconsoles.delete(this)\n })\n }\n}\n/* c8 ignore stop */\n\n/**\n * @param {Element} dom\n */\n/* c8 ignore next */\nexport const createVConsole = (dom) => new VConsole(dom)\n\n/**\n * @param {string} moduleName\n * @return {function(...any):void}\n */\nexport const createModuleLogger = (moduleName) => common.createModuleLogger(print, moduleName)\n","/**\n * Utility module to create and manipulate Iterators.\n *\n * @module iterator\n */\n\n/**\n * @template T,R\n * @param {Iterator} iterator\n * @param {function(T):R} f\n * @return {IterableIterator}\n */\nexport const mapIterator = (iterator, f) => ({\n [Symbol.iterator] () {\n return this\n },\n // @ts-ignore\n next () {\n const r = iterator.next()\n return { value: r.done ? undefined : f(r.value), done: r.done }\n }\n})\n\n/**\n * @template T\n * @param {function():IteratorResult} next\n * @return {IterableIterator}\n */\nexport const createIterator = next => ({\n /**\n * @return {IterableIterator}\n */\n [Symbol.iterator] () {\n return this\n },\n // @ts-ignore\n next\n})\n\n/**\n * @template T\n * @param {Iterator} iterator\n * @param {function(T):boolean} filter\n */\nexport const iteratorFilter = (iterator, filter) => createIterator(() => {\n let res\n do {\n res = iterator.next()\n } while (!res.done && !filter(res.value))\n return res\n})\n\n/**\n * @template T,M\n * @param {Iterator} iterator\n * @param {function(T):M} fmap\n */\nexport const iteratorMap = (iterator, fmap) => createIterator(() => {\n const { done, value } = iterator.next()\n return { done, value: done ? undefined : fmap(value) }\n})\n","import { Observable } from 'lib0/observable';\nimport * as array from 'lib0/array';\nimport * as math from 'lib0/math';\nimport * as map from 'lib0/map';\nimport * as encoding from 'lib0/encoding';\nimport * as decoding from 'lib0/decoding';\nimport * as random from 'lib0/random';\nimport * as promise from 'lib0/promise';\nimport * as buffer from 'lib0/buffer';\nimport * as error from 'lib0/error';\nimport * as binary from 'lib0/binary';\nimport * as f from 'lib0/function';\nimport { callAll } from 'lib0/function';\nimport * as set from 'lib0/set';\nimport * as logging from 'lib0/logging';\nimport * as time from 'lib0/time';\nimport * as string from 'lib0/string';\nimport * as iterator from 'lib0/iterator';\nimport * as object from 'lib0/object';\n\n/**\n * This is an abstract interface that all Connectors should implement to keep them interchangeable.\n *\n * @note This interface is experimental and it is not advised to actually inherit this class.\n * It just serves as typing information.\n *\n * @extends {Observable}\n */\nclass AbstractConnector extends Observable {\n /**\n * @param {Doc} ydoc\n * @param {any} awareness\n */\n constructor (ydoc, awareness) {\n super();\n this.doc = ydoc;\n this.awareness = awareness;\n }\n}\n\nclass DeleteItem {\n /**\n * @param {number} clock\n * @param {number} len\n */\n constructor (clock, len) {\n /**\n * @type {number}\n */\n this.clock = clock;\n /**\n * @type {number}\n */\n this.len = len;\n }\n}\n\n/**\n * We no longer maintain a DeleteStore. DeleteSet is a temporary object that is created when needed.\n * - When created in a transaction, it must only be accessed after sorting, and merging\n * - This DeleteSet is send to other clients\n * - We do not create a DeleteSet when we send a sync message. The DeleteSet message is created directly from StructStore\n * - We read a DeleteSet as part of a sync/update message. In this case the DeleteSet is already sorted and merged.\n */\nclass DeleteSet {\n constructor () {\n /**\n * @type {Map>}\n */\n this.clients = new Map();\n }\n}\n\n/**\n * Iterate over all structs that the DeleteSet gc's.\n *\n * @param {Transaction} transaction\n * @param {DeleteSet} ds\n * @param {function(GC|Item):void} f\n *\n * @function\n */\nconst iterateDeletedStructs = (transaction, ds, f) =>\n ds.clients.forEach((deletes, clientid) => {\n const structs = /** @type {Array} */ (transaction.doc.store.clients.get(clientid));\n for (let i = 0; i < deletes.length; i++) {\n const del = deletes[i];\n iterateStructs(transaction, structs, del.clock, del.len, f);\n }\n });\n\n/**\n * @param {Array} dis\n * @param {number} clock\n * @return {number|null}\n *\n * @private\n * @function\n */\nconst findIndexDS = (dis, clock) => {\n let left = 0;\n let right = dis.length - 1;\n while (left <= right) {\n const midindex = math.floor((left + right) / 2);\n const mid = dis[midindex];\n const midclock = mid.clock;\n if (midclock <= clock) {\n if (clock < midclock + mid.len) {\n return midindex\n }\n left = midindex + 1;\n } else {\n right = midindex - 1;\n }\n }\n return null\n};\n\n/**\n * @param {DeleteSet} ds\n * @param {ID} id\n * @return {boolean}\n *\n * @private\n * @function\n */\nconst isDeleted = (ds, id) => {\n const dis = ds.clients.get(id.client);\n return dis !== undefined && findIndexDS(dis, id.clock) !== null\n};\n\n/**\n * @param {DeleteSet} ds\n *\n * @private\n * @function\n */\nconst sortAndMergeDeleteSet = ds => {\n ds.clients.forEach(dels => {\n dels.sort((a, b) => a.clock - b.clock);\n // merge items without filtering or splicing the array\n // i is the current pointer\n // j refers to the current insert position for the pointed item\n // try to merge dels[i] into dels[j-1] or set dels[j]=dels[i]\n let i, j;\n for (i = 1, j = 1; i < dels.length; i++) {\n const left = dels[j - 1];\n const right = dels[i];\n if (left.clock + left.len >= right.clock) {\n left.len = math.max(left.len, right.clock + right.len - left.clock);\n } else {\n if (j < i) {\n dels[j] = right;\n }\n j++;\n }\n }\n dels.length = j;\n });\n};\n\n/**\n * @param {Array} dss\n * @return {DeleteSet} A fresh DeleteSet\n */\nconst mergeDeleteSets = dss => {\n const merged = new DeleteSet();\n for (let dssI = 0; dssI < dss.length; dssI++) {\n dss[dssI].clients.forEach((delsLeft, client) => {\n if (!merged.clients.has(client)) {\n // Write all missing keys from current ds and all following.\n // If merged already contains `client` current ds has already been added.\n /**\n * @type {Array}\n */\n const dels = delsLeft.slice();\n for (let i = dssI + 1; i < dss.length; i++) {\n array.appendTo(dels, dss[i].clients.get(client) || []);\n }\n merged.clients.set(client, dels);\n }\n });\n }\n sortAndMergeDeleteSet(merged);\n return merged\n};\n\n/**\n * @param {DeleteSet} ds\n * @param {number} client\n * @param {number} clock\n * @param {number} length\n *\n * @private\n * @function\n */\nconst addToDeleteSet = (ds, client, clock, length) => {\n map.setIfUndefined(ds.clients, client, () => /** @type {Array} */ ([])).push(new DeleteItem(clock, length));\n};\n\nconst createDeleteSet = () => new DeleteSet();\n\n/**\n * @param {StructStore} ss\n * @return {DeleteSet} Merged and sorted DeleteSet\n *\n * @private\n * @function\n */\nconst createDeleteSetFromStructStore = ss => {\n const ds = createDeleteSet();\n ss.clients.forEach((structs, client) => {\n /**\n * @type {Array}\n */\n const dsitems = [];\n for (let i = 0; i < structs.length; i++) {\n const struct = structs[i];\n if (struct.deleted) {\n const clock = struct.id.clock;\n let len = struct.length;\n if (i + 1 < structs.length) {\n for (let next = structs[i + 1]; i + 1 < structs.length && next.deleted; next = structs[++i + 1]) {\n len += next.length;\n }\n }\n dsitems.push(new DeleteItem(clock, len));\n }\n }\n if (dsitems.length > 0) {\n ds.clients.set(client, dsitems);\n }\n });\n return ds\n};\n\n/**\n * @param {DSEncoderV1 | DSEncoderV2} encoder\n * @param {DeleteSet} ds\n *\n * @private\n * @function\n */\nconst writeDeleteSet = (encoder, ds) => {\n encoding.writeVarUint(encoder.restEncoder, ds.clients.size);\n\n // Ensure that the delete set is written in a deterministic order\n array.from(ds.clients.entries())\n .sort((a, b) => b[0] - a[0])\n .forEach(([client, dsitems]) => {\n encoder.resetDsCurVal();\n encoding.writeVarUint(encoder.restEncoder, client);\n const len = dsitems.length;\n encoding.writeVarUint(encoder.restEncoder, len);\n for (let i = 0; i < len; i++) {\n const item = dsitems[i];\n encoder.writeDsClock(item.clock);\n encoder.writeDsLen(item.len);\n }\n });\n};\n\n/**\n * @param {DSDecoderV1 | DSDecoderV2} decoder\n * @return {DeleteSet}\n *\n * @private\n * @function\n */\nconst readDeleteSet = decoder => {\n const ds = new DeleteSet();\n const numClients = decoding.readVarUint(decoder.restDecoder);\n for (let i = 0; i < numClients; i++) {\n decoder.resetDsCurVal();\n const client = decoding.readVarUint(decoder.restDecoder);\n const numberOfDeletes = decoding.readVarUint(decoder.restDecoder);\n if (numberOfDeletes > 0) {\n const dsField = map.setIfUndefined(ds.clients, client, () => /** @type {Array} */ ([]));\n for (let i = 0; i < numberOfDeletes; i++) {\n dsField.push(new DeleteItem(decoder.readDsClock(), decoder.readDsLen()));\n }\n }\n }\n return ds\n};\n\n/**\n * @todo YDecoder also contains references to String and other Decoders. Would make sense to exchange YDecoder.toUint8Array for YDecoder.DsToUint8Array()..\n */\n\n/**\n * @param {DSDecoderV1 | DSDecoderV2} decoder\n * @param {Transaction} transaction\n * @param {StructStore} store\n * @return {Uint8Array|null} Returns a v2 update containing all deletes that couldn't be applied yet; or null if all deletes were applied successfully.\n *\n * @private\n * @function\n */\nconst readAndApplyDeleteSet = (decoder, transaction, store) => {\n const unappliedDS = new DeleteSet();\n const numClients = decoding.readVarUint(decoder.restDecoder);\n for (let i = 0; i < numClients; i++) {\n decoder.resetDsCurVal();\n const client = decoding.readVarUint(decoder.restDecoder);\n const numberOfDeletes = decoding.readVarUint(decoder.restDecoder);\n const structs = store.clients.get(client) || [];\n const state = getState(store, client);\n for (let i = 0; i < numberOfDeletes; i++) {\n const clock = decoder.readDsClock();\n const clockEnd = clock + decoder.readDsLen();\n if (clock < state) {\n if (state < clockEnd) {\n addToDeleteSet(unappliedDS, client, state, clockEnd - state);\n }\n let index = findIndexSS(structs, clock);\n /**\n * We can ignore the case of GC and Delete structs, because we are going to skip them\n * @type {Item}\n */\n // @ts-ignore\n let struct = structs[index];\n // split the first item if necessary\n if (!struct.deleted && struct.id.clock < clock) {\n structs.splice(index + 1, 0, splitItem(transaction, struct, clock - struct.id.clock));\n index++; // increase we now want to use the next struct\n }\n while (index < structs.length) {\n // @ts-ignore\n struct = structs[index++];\n if (struct.id.clock < clockEnd) {\n if (!struct.deleted) {\n if (clockEnd < struct.id.clock + struct.length) {\n structs.splice(index, 0, splitItem(transaction, struct, clockEnd - struct.id.clock));\n }\n struct.delete(transaction);\n }\n } else {\n break\n }\n }\n } else {\n addToDeleteSet(unappliedDS, client, clock, clockEnd - clock);\n }\n }\n }\n if (unappliedDS.clients.size > 0) {\n const ds = new UpdateEncoderV2();\n encoding.writeVarUint(ds.restEncoder, 0); // encode 0 structs\n writeDeleteSet(ds, unappliedDS);\n return ds.toUint8Array()\n }\n return null\n};\n\n/**\n * @param {DeleteSet} ds1\n * @param {DeleteSet} ds2\n */\nconst equalDeleteSets = (ds1, ds2) => {\n if (ds1.clients.size !== ds2.clients.size) return false\n ds1.clients.forEach((deleteItems1, client) => {\n const deleteItems2 = /** @type {Array} */ (ds2.clients.get(client));\n if (deleteItems2 === undefined || deleteItems1.length !== deleteItems2.length) return false\n for (let i = 0; i < deleteItems1.length; i++) {\n const di1 = deleteItems1[i];\n const di2 = deleteItems2[i];\n if (di1.clock !== di2.clock || di1.len !== di2.len) {\n return false\n }\n }\n });\n return true\n};\n\n/**\n * @module Y\n */\n\nconst generateNewClientId = random.uint32;\n\n/**\n * @typedef {Object} DocOpts\n * @property {boolean} [DocOpts.gc=true] Disable garbage collection (default: gc=true)\n * @property {function(Item):boolean} [DocOpts.gcFilter] Will be called before an Item is garbage collected. Return false to keep the Item.\n * @property {string} [DocOpts.guid] Define a globally unique identifier for this document\n * @property {string | null} [DocOpts.collectionid] Associate this document with a collection. This only plays a role if your provider has a concept of collection.\n * @property {any} [DocOpts.meta] Any kind of meta information you want to associate with this document. If this is a subdocument, remote peers will store the meta information as well.\n * @property {boolean} [DocOpts.autoLoad] If a subdocument, automatically load document. If this is a subdocument, remote peers will load the document as well automatically.\n * @property {boolean} [DocOpts.shouldLoad] Whether the document should be synced by the provider now. This is toggled to true when you call ydoc.load()\n */\n\n/**\n * A Yjs instance handles the state of shared data.\n * @extends Observable\n */\nclass Doc extends Observable {\n /**\n * @param {DocOpts} opts configuration\n */\n constructor ({ guid = random.uuidv4(), collectionid = null, gc = true, gcFilter = () => true, meta = null, autoLoad = false, shouldLoad = true } = {}) {\n super();\n this.gc = gc;\n this.gcFilter = gcFilter;\n this.clientID = generateNewClientId();\n this.guid = guid;\n this.collectionid = collectionid;\n /**\n * @type {Map>>}\n */\n this.share = new Map();\n this.store = new StructStore();\n /**\n * @type {Transaction | null}\n */\n this._transaction = null;\n /**\n * @type {Array}\n */\n this._transactionCleanups = [];\n /**\n * @type {Set}\n */\n this.subdocs = new Set();\n /**\n * If this document is a subdocument - a document integrated into another document - then _item is defined.\n * @type {Item?}\n */\n this._item = null;\n this.shouldLoad = shouldLoad;\n this.autoLoad = autoLoad;\n this.meta = meta;\n /**\n * This is set to true when the persistence provider loaded the document from the database or when the `sync` event fires.\n * Note that not all providers implement this feature. Provider authors are encouraged to fire the `load` event when the doc content is loaded from the database.\n *\n * @type {boolean}\n */\n this.isLoaded = false;\n /**\n * This is set to true when the connection provider has successfully synced with a backend.\n * Note that when using peer-to-peer providers this event may not provide very useful.\n * Also note that not all providers implement this feature. Provider authors are encouraged to fire\n * the `sync` event when the doc has been synced (with `true` as a parameter) or if connection is\n * lost (with false as a parameter).\n */\n this.isSynced = false;\n /**\n * Promise that resolves once the document has been loaded from a presistence provider.\n */\n this.whenLoaded = promise.create(resolve => {\n this.on('load', () => {\n this.isLoaded = true;\n resolve(this);\n });\n });\n const provideSyncedPromise = () => promise.create(resolve => {\n /**\n * @param {boolean} isSynced\n */\n const eventHandler = (isSynced) => {\n if (isSynced === undefined || isSynced === true) {\n this.off('sync', eventHandler);\n resolve();\n }\n };\n this.on('sync', eventHandler);\n });\n this.on('sync', isSynced => {\n if (isSynced === false && this.isSynced) {\n this.whenSynced = provideSyncedPromise();\n }\n this.isSynced = isSynced === undefined || isSynced === true;\n if (!this.isLoaded) {\n this.emit('load', []);\n }\n });\n /**\n * Promise that resolves once the document has been synced with a backend.\n * This promise is recreated when the connection is lost.\n * Note the documentation about the `isSynced` property.\n */\n this.whenSynced = provideSyncedPromise();\n }\n\n /**\n * Notify the parent document that you request to load data into this subdocument (if it is a subdocument).\n *\n * `load()` might be used in the future to request any provider to load the most current data.\n *\n * It is safe to call `load()` multiple times.\n */\n load () {\n const item = this._item;\n if (item !== null && !this.shouldLoad) {\n transact(/** @type {any} */ (item.parent).doc, transaction => {\n transaction.subdocsLoaded.add(this);\n }, null, true);\n }\n this.shouldLoad = true;\n }\n\n getSubdocs () {\n return this.subdocs\n }\n\n getSubdocGuids () {\n return new Set(array.from(this.subdocs).map(doc => doc.guid))\n }\n\n /**\n * Changes that happen inside of a transaction are bundled. This means that\n * the observer fires _after_ the transaction is finished and that all changes\n * that happened inside of the transaction are sent as one message to the\n * other peers.\n *\n * @template T\n * @param {function(Transaction):T} f The function that should be executed as a transaction\n * @param {any} [origin] Origin of who started the transaction. Will be stored on transaction.origin\n * @return T\n *\n * @public\n */\n transact (f, origin = null) {\n return transact(this, f, origin)\n }\n\n /**\n * Define a shared data type.\n *\n * Multiple calls of `y.get(name, TypeConstructor)` yield the same result\n * and do not overwrite each other. I.e.\n * `y.define(name, Y.Array) === y.define(name, Y.Array)`\n *\n * After this method is called, the type is also available on `y.share.get(name)`.\n *\n * *Best Practices:*\n * Define all types right after the Yjs instance is created and store them in a separate object.\n * Also use the typed methods `getText(name)`, `getArray(name)`, ..\n *\n * @example\n * const y = new Y(..)\n * const appState = {\n * document: y.getText('document')\n * comments: y.getArray('comments')\n * }\n *\n * @param {string} name\n * @param {Function} TypeConstructor The constructor of the type definition. E.g. Y.Text, Y.Array, Y.Map, ...\n * @return {AbstractType} The created type. Constructed with TypeConstructor\n *\n * @public\n */\n get (name, TypeConstructor = AbstractType) {\n const type = map.setIfUndefined(this.share, name, () => {\n // @ts-ignore\n const t = new TypeConstructor();\n t._integrate(this, null);\n return t\n });\n const Constr = type.constructor;\n if (TypeConstructor !== AbstractType && Constr !== TypeConstructor) {\n if (Constr === AbstractType) {\n // @ts-ignore\n const t = new TypeConstructor();\n t._map = type._map;\n type._map.forEach(/** @param {Item?} n */ n => {\n for (; n !== null; n = n.left) {\n // @ts-ignore\n n.parent = t;\n }\n });\n t._start = type._start;\n for (let n = t._start; n !== null; n = n.right) {\n n.parent = t;\n }\n t._length = type._length;\n this.share.set(name, t);\n t._integrate(this, null);\n return t\n } else {\n throw new Error(`Type with the name ${name} has already been defined with a different constructor`)\n }\n }\n return type\n }\n\n /**\n * @template T\n * @param {string} [name]\n * @return {YArray}\n *\n * @public\n */\n getArray (name = '') {\n // @ts-ignore\n return this.get(name, YArray)\n }\n\n /**\n * @param {string} [name]\n * @return {YText}\n *\n * @public\n */\n getText (name = '') {\n // @ts-ignore\n return this.get(name, YText)\n }\n\n /**\n * @template T\n * @param {string} [name]\n * @return {YMap}\n *\n * @public\n */\n getMap (name = '') {\n // @ts-ignore\n return this.get(name, YMap)\n }\n\n /**\n * @param {string} [name]\n * @return {YXmlFragment}\n *\n * @public\n */\n getXmlFragment (name = '') {\n // @ts-ignore\n return this.get(name, YXmlFragment)\n }\n\n /**\n * Converts the entire document into a js object, recursively traversing each yjs type\n * Doesn't log types that have not been defined (using ydoc.getType(..)).\n *\n * @deprecated Do not use this method and rather call toJSON directly on the shared types.\n *\n * @return {Object}\n */\n toJSON () {\n /**\n * @type {Object}\n */\n const doc = {};\n\n this.share.forEach((value, key) => {\n doc[key] = value.toJSON();\n });\n\n return doc\n }\n\n /**\n * Emit `destroy` event and unregister all event handlers.\n */\n destroy () {\n array.from(this.subdocs).forEach(subdoc => subdoc.destroy());\n const item = this._item;\n if (item !== null) {\n this._item = null;\n const content = /** @type {ContentDoc} */ (item.content);\n content.doc = new Doc({ guid: this.guid, ...content.opts, shouldLoad: false });\n content.doc._item = item;\n transact(/** @type {any} */ (item).parent.doc, transaction => {\n const doc = content.doc;\n if (!item.deleted) {\n transaction.subdocsAdded.add(doc);\n }\n transaction.subdocsRemoved.add(this);\n }, null, true);\n }\n this.emit('destroyed', [true]);\n this.emit('destroy', [this]);\n super.destroy();\n }\n\n /**\n * @param {string} eventName\n * @param {function(...any):any} f\n */\n on (eventName, f) {\n super.on(eventName, f);\n }\n\n /**\n * @param {string} eventName\n * @param {function} f\n */\n off (eventName, f) {\n super.off(eventName, f);\n }\n}\n\nclass DSDecoderV1 {\n /**\n * @param {decoding.Decoder} decoder\n */\n constructor (decoder) {\n this.restDecoder = decoder;\n }\n\n resetDsCurVal () {\n // nop\n }\n\n /**\n * @return {number}\n */\n readDsClock () {\n return decoding.readVarUint(this.restDecoder)\n }\n\n /**\n * @return {number}\n */\n readDsLen () {\n return decoding.readVarUint(this.restDecoder)\n }\n}\n\nclass UpdateDecoderV1 extends DSDecoderV1 {\n /**\n * @return {ID}\n */\n readLeftID () {\n return createID(decoding.readVarUint(this.restDecoder), decoding.readVarUint(this.restDecoder))\n }\n\n /**\n * @return {ID}\n */\n readRightID () {\n return createID(decoding.readVarUint(this.restDecoder), decoding.readVarUint(this.restDecoder))\n }\n\n /**\n * Read the next client id.\n * Use this in favor of readID whenever possible to reduce the number of objects created.\n */\n readClient () {\n return decoding.readVarUint(this.restDecoder)\n }\n\n /**\n * @return {number} info An unsigned 8-bit integer\n */\n readInfo () {\n return decoding.readUint8(this.restDecoder)\n }\n\n /**\n * @return {string}\n */\n readString () {\n return decoding.readVarString(this.restDecoder)\n }\n\n /**\n * @return {boolean} isKey\n */\n readParentInfo () {\n return decoding.readVarUint(this.restDecoder) === 1\n }\n\n /**\n * @return {number} info An unsigned 8-bit integer\n */\n readTypeRef () {\n return decoding.readVarUint(this.restDecoder)\n }\n\n /**\n * Write len of a struct - well suited for Opt RLE encoder.\n *\n * @return {number} len\n */\n readLen () {\n return decoding.readVarUint(this.restDecoder)\n }\n\n /**\n * @return {any}\n */\n readAny () {\n return decoding.readAny(this.restDecoder)\n }\n\n /**\n * @return {Uint8Array}\n */\n readBuf () {\n return buffer.copyUint8Array(decoding.readVarUint8Array(this.restDecoder))\n }\n\n /**\n * Legacy implementation uses JSON parse. We use any-decoding in v2.\n *\n * @return {any}\n */\n readJSON () {\n return JSON.parse(decoding.readVarString(this.restDecoder))\n }\n\n /**\n * @return {string}\n */\n readKey () {\n return decoding.readVarString(this.restDecoder)\n }\n}\n\nclass DSDecoderV2 {\n /**\n * @param {decoding.Decoder} decoder\n */\n constructor (decoder) {\n /**\n * @private\n */\n this.dsCurrVal = 0;\n this.restDecoder = decoder;\n }\n\n resetDsCurVal () {\n this.dsCurrVal = 0;\n }\n\n /**\n * @return {number}\n */\n readDsClock () {\n this.dsCurrVal += decoding.readVarUint(this.restDecoder);\n return this.dsCurrVal\n }\n\n /**\n * @return {number}\n */\n readDsLen () {\n const diff = decoding.readVarUint(this.restDecoder) + 1;\n this.dsCurrVal += diff;\n return diff\n }\n}\n\nclass UpdateDecoderV2 extends DSDecoderV2 {\n /**\n * @param {decoding.Decoder} decoder\n */\n constructor (decoder) {\n super(decoder);\n /**\n * List of cached keys. If the keys[id] does not exist, we read a new key\n * from stringEncoder and push it to keys.\n *\n * @type {Array}\n */\n this.keys = [];\n decoding.readVarUint(decoder); // read feature flag - currently unused\n this.keyClockDecoder = new decoding.IntDiffOptRleDecoder(decoding.readVarUint8Array(decoder));\n this.clientDecoder = new decoding.UintOptRleDecoder(decoding.readVarUint8Array(decoder));\n this.leftClockDecoder = new decoding.IntDiffOptRleDecoder(decoding.readVarUint8Array(decoder));\n this.rightClockDecoder = new decoding.IntDiffOptRleDecoder(decoding.readVarUint8Array(decoder));\n this.infoDecoder = new decoding.RleDecoder(decoding.readVarUint8Array(decoder), decoding.readUint8);\n this.stringDecoder = new decoding.StringDecoder(decoding.readVarUint8Array(decoder));\n this.parentInfoDecoder = new decoding.RleDecoder(decoding.readVarUint8Array(decoder), decoding.readUint8);\n this.typeRefDecoder = new decoding.UintOptRleDecoder(decoding.readVarUint8Array(decoder));\n this.lenDecoder = new decoding.UintOptRleDecoder(decoding.readVarUint8Array(decoder));\n }\n\n /**\n * @return {ID}\n */\n readLeftID () {\n return new ID(this.clientDecoder.read(), this.leftClockDecoder.read())\n }\n\n /**\n * @return {ID}\n */\n readRightID () {\n return new ID(this.clientDecoder.read(), this.rightClockDecoder.read())\n }\n\n /**\n * Read the next client id.\n * Use this in favor of readID whenever possible to reduce the number of objects created.\n */\n readClient () {\n return this.clientDecoder.read()\n }\n\n /**\n * @return {number} info An unsigned 8-bit integer\n */\n readInfo () {\n return /** @type {number} */ (this.infoDecoder.read())\n }\n\n /**\n * @return {string}\n */\n readString () {\n return this.stringDecoder.read()\n }\n\n /**\n * @return {boolean}\n */\n readParentInfo () {\n return this.parentInfoDecoder.read() === 1\n }\n\n /**\n * @return {number} An unsigned 8-bit integer\n */\n readTypeRef () {\n return this.typeRefDecoder.read()\n }\n\n /**\n * Write len of a struct - well suited for Opt RLE encoder.\n *\n * @return {number}\n */\n readLen () {\n return this.lenDecoder.read()\n }\n\n /**\n * @return {any}\n */\n readAny () {\n return decoding.readAny(this.restDecoder)\n }\n\n /**\n * @return {Uint8Array}\n */\n readBuf () {\n return decoding.readVarUint8Array(this.restDecoder)\n }\n\n /**\n * This is mainly here for legacy purposes.\n *\n * Initial we incoded objects using JSON. Now we use the much faster lib0/any-encoder. This method mainly exists for legacy purposes for the v1 encoder.\n *\n * @return {any}\n */\n readJSON () {\n return decoding.readAny(this.restDecoder)\n }\n\n /**\n * @return {string}\n */\n readKey () {\n const keyClock = this.keyClockDecoder.read();\n if (keyClock < this.keys.length) {\n return this.keys[keyClock]\n } else {\n const key = this.stringDecoder.read();\n this.keys.push(key);\n return key\n }\n }\n}\n\nclass DSEncoderV1 {\n constructor () {\n this.restEncoder = encoding.createEncoder();\n }\n\n toUint8Array () {\n return encoding.toUint8Array(this.restEncoder)\n }\n\n resetDsCurVal () {\n // nop\n }\n\n /**\n * @param {number} clock\n */\n writeDsClock (clock) {\n encoding.writeVarUint(this.restEncoder, clock);\n }\n\n /**\n * @param {number} len\n */\n writeDsLen (len) {\n encoding.writeVarUint(this.restEncoder, len);\n }\n}\n\nclass UpdateEncoderV1 extends DSEncoderV1 {\n /**\n * @param {ID} id\n */\n writeLeftID (id) {\n encoding.writeVarUint(this.restEncoder, id.client);\n encoding.writeVarUint(this.restEncoder, id.clock);\n }\n\n /**\n * @param {ID} id\n */\n writeRightID (id) {\n encoding.writeVarUint(this.restEncoder, id.client);\n encoding.writeVarUint(this.restEncoder, id.clock);\n }\n\n /**\n * Use writeClient and writeClock instead of writeID if possible.\n * @param {number} client\n */\n writeClient (client) {\n encoding.writeVarUint(this.restEncoder, client);\n }\n\n /**\n * @param {number} info An unsigned 8-bit integer\n */\n writeInfo (info) {\n encoding.writeUint8(this.restEncoder, info);\n }\n\n /**\n * @param {string} s\n */\n writeString (s) {\n encoding.writeVarString(this.restEncoder, s);\n }\n\n /**\n * @param {boolean} isYKey\n */\n writeParentInfo (isYKey) {\n encoding.writeVarUint(this.restEncoder, isYKey ? 1 : 0);\n }\n\n /**\n * @param {number} info An unsigned 8-bit integer\n */\n writeTypeRef (info) {\n encoding.writeVarUint(this.restEncoder, info);\n }\n\n /**\n * Write len of a struct - well suited for Opt RLE encoder.\n *\n * @param {number} len\n */\n writeLen (len) {\n encoding.writeVarUint(this.restEncoder, len);\n }\n\n /**\n * @param {any} any\n */\n writeAny (any) {\n encoding.writeAny(this.restEncoder, any);\n }\n\n /**\n * @param {Uint8Array} buf\n */\n writeBuf (buf) {\n encoding.writeVarUint8Array(this.restEncoder, buf);\n }\n\n /**\n * @param {any} embed\n */\n writeJSON (embed) {\n encoding.writeVarString(this.restEncoder, JSON.stringify(embed));\n }\n\n /**\n * @param {string} key\n */\n writeKey (key) {\n encoding.writeVarString(this.restEncoder, key);\n }\n}\n\nclass DSEncoderV2 {\n constructor () {\n this.restEncoder = encoding.createEncoder(); // encodes all the rest / non-optimized\n this.dsCurrVal = 0;\n }\n\n toUint8Array () {\n return encoding.toUint8Array(this.restEncoder)\n }\n\n resetDsCurVal () {\n this.dsCurrVal = 0;\n }\n\n /**\n * @param {number} clock\n */\n writeDsClock (clock) {\n const diff = clock - this.dsCurrVal;\n this.dsCurrVal = clock;\n encoding.writeVarUint(this.restEncoder, diff);\n }\n\n /**\n * @param {number} len\n */\n writeDsLen (len) {\n if (len === 0) {\n error.unexpectedCase();\n }\n encoding.writeVarUint(this.restEncoder, len - 1);\n this.dsCurrVal += len;\n }\n}\n\nclass UpdateEncoderV2 extends DSEncoderV2 {\n constructor () {\n super();\n /**\n * @type {Map}\n */\n this.keyMap = new Map();\n /**\n * Refers to the next uniqe key-identifier to me used.\n * See writeKey method for more information.\n *\n * @type {number}\n */\n this.keyClock = 0;\n this.keyClockEncoder = new encoding.IntDiffOptRleEncoder();\n this.clientEncoder = new encoding.UintOptRleEncoder();\n this.leftClockEncoder = new encoding.IntDiffOptRleEncoder();\n this.rightClockEncoder = new encoding.IntDiffOptRleEncoder();\n this.infoEncoder = new encoding.RleEncoder(encoding.writeUint8);\n this.stringEncoder = new encoding.StringEncoder();\n this.parentInfoEncoder = new encoding.RleEncoder(encoding.writeUint8);\n this.typeRefEncoder = new encoding.UintOptRleEncoder();\n this.lenEncoder = new encoding.UintOptRleEncoder();\n }\n\n toUint8Array () {\n const encoder = encoding.createEncoder();\n encoding.writeVarUint(encoder, 0); // this is a feature flag that we might use in the future\n encoding.writeVarUint8Array(encoder, this.keyClockEncoder.toUint8Array());\n encoding.writeVarUint8Array(encoder, this.clientEncoder.toUint8Array());\n encoding.writeVarUint8Array(encoder, this.leftClockEncoder.toUint8Array());\n encoding.writeVarUint8Array(encoder, this.rightClockEncoder.toUint8Array());\n encoding.writeVarUint8Array(encoder, encoding.toUint8Array(this.infoEncoder));\n encoding.writeVarUint8Array(encoder, this.stringEncoder.toUint8Array());\n encoding.writeVarUint8Array(encoder, encoding.toUint8Array(this.parentInfoEncoder));\n encoding.writeVarUint8Array(encoder, this.typeRefEncoder.toUint8Array());\n encoding.writeVarUint8Array(encoder, this.lenEncoder.toUint8Array());\n // @note The rest encoder is appended! (note the missing var)\n encoding.writeUint8Array(encoder, encoding.toUint8Array(this.restEncoder));\n return encoding.toUint8Array(encoder)\n }\n\n /**\n * @param {ID} id\n */\n writeLeftID (id) {\n this.clientEncoder.write(id.client);\n this.leftClockEncoder.write(id.clock);\n }\n\n /**\n * @param {ID} id\n */\n writeRightID (id) {\n this.clientEncoder.write(id.client);\n this.rightClockEncoder.write(id.clock);\n }\n\n /**\n * @param {number} client\n */\n writeClient (client) {\n this.clientEncoder.write(client);\n }\n\n /**\n * @param {number} info An unsigned 8-bit integer\n */\n writeInfo (info) {\n this.infoEncoder.write(info);\n }\n\n /**\n * @param {string} s\n */\n writeString (s) {\n this.stringEncoder.write(s);\n }\n\n /**\n * @param {boolean} isYKey\n */\n writeParentInfo (isYKey) {\n this.parentInfoEncoder.write(isYKey ? 1 : 0);\n }\n\n /**\n * @param {number} info An unsigned 8-bit integer\n */\n writeTypeRef (info) {\n this.typeRefEncoder.write(info);\n }\n\n /**\n * Write len of a struct - well suited for Opt RLE encoder.\n *\n * @param {number} len\n */\n writeLen (len) {\n this.lenEncoder.write(len);\n }\n\n /**\n * @param {any} any\n */\n writeAny (any) {\n encoding.writeAny(this.restEncoder, any);\n }\n\n /**\n * @param {Uint8Array} buf\n */\n writeBuf (buf) {\n encoding.writeVarUint8Array(this.restEncoder, buf);\n }\n\n /**\n * This is mainly here for legacy purposes.\n *\n * Initial we incoded objects using JSON. Now we use the much faster lib0/any-encoder. This method mainly exists for legacy purposes for the v1 encoder.\n *\n * @param {any} embed\n */\n writeJSON (embed) {\n encoding.writeAny(this.restEncoder, embed);\n }\n\n /**\n * Property keys are often reused. For example, in y-prosemirror the key `bold` might\n * occur very often. For a 3d application, the key `position` might occur very often.\n *\n * We cache these keys in a Map and refer to them via a unique number.\n *\n * @param {string} key\n */\n writeKey (key) {\n const clock = this.keyMap.get(key);\n if (clock === undefined) {\n /**\n * @todo uncomment to introduce this feature finally\n *\n * Background. The ContentFormat object was always encoded using writeKey, but the decoder used to use readString.\n * Furthermore, I forgot to set the keyclock. So everything was working fine.\n *\n * However, this feature here is basically useless as it is not being used (it actually only consumes extra memory).\n *\n * I don't know yet how to reintroduce this feature..\n *\n * Older clients won't be able to read updates when we reintroduce this feature. So this should probably be done using a flag.\n *\n */\n // this.keyMap.set(key, this.keyClock)\n this.keyClockEncoder.write(this.keyClock++);\n this.stringEncoder.write(key);\n } else {\n this.keyClockEncoder.write(clock);\n }\n }\n}\n\n/**\n * @param {UpdateEncoderV1 | UpdateEncoderV2} encoder\n * @param {Array} structs All structs by `client`\n * @param {number} client\n * @param {number} clock write structs starting with `ID(client,clock)`\n *\n * @function\n */\nconst writeStructs = (encoder, structs, client, clock) => {\n // write first id\n clock = math.max(clock, structs[0].id.clock); // make sure the first id exists\n const startNewStructs = findIndexSS(structs, clock);\n // write # encoded structs\n encoding.writeVarUint(encoder.restEncoder, structs.length - startNewStructs);\n encoder.writeClient(client);\n encoding.writeVarUint(encoder.restEncoder, clock);\n const firstStruct = structs[startNewStructs];\n // write first struct with an offset\n firstStruct.write(encoder, clock - firstStruct.id.clock);\n for (let i = startNewStructs + 1; i < structs.length; i++) {\n structs[i].write(encoder, 0);\n }\n};\n\n/**\n * @param {UpdateEncoderV1 | UpdateEncoderV2} encoder\n * @param {StructStore} store\n * @param {Map} _sm\n *\n * @private\n * @function\n */\nconst writeClientsStructs = (encoder, store, _sm) => {\n // we filter all valid _sm entries into sm\n const sm = new Map();\n _sm.forEach((clock, client) => {\n // only write if new structs are available\n if (getState(store, client) > clock) {\n sm.set(client, clock);\n }\n });\n getStateVector(store).forEach((_clock, client) => {\n if (!_sm.has(client)) {\n sm.set(client, 0);\n }\n });\n // write # states that were updated\n encoding.writeVarUint(encoder.restEncoder, sm.size);\n // Write items with higher client ids first\n // This heavily improves the conflict algorithm.\n array.from(sm.entries()).sort((a, b) => b[0] - a[0]).forEach(([client, clock]) => {\n writeStructs(encoder, /** @type {Array} */ (store.clients.get(client)), client, clock);\n });\n};\n\n/**\n * @param {UpdateDecoderV1 | UpdateDecoderV2} decoder The decoder object to read data from.\n * @param {Doc} doc\n * @return {Map }>}\n *\n * @private\n * @function\n */\nconst readClientsStructRefs = (decoder, doc) => {\n /**\n * @type {Map }>}\n */\n const clientRefs = map.create();\n const numOfStateUpdates = decoding.readVarUint(decoder.restDecoder);\n for (let i = 0; i < numOfStateUpdates; i++) {\n const numberOfStructs = decoding.readVarUint(decoder.restDecoder);\n /**\n * @type {Array}\n */\n const refs = new Array(numberOfStructs);\n const client = decoder.readClient();\n let clock = decoding.readVarUint(decoder.restDecoder);\n // const start = performance.now()\n clientRefs.set(client, { i: 0, refs });\n for (let i = 0; i < numberOfStructs; i++) {\n const info = decoder.readInfo();\n switch (binary.BITS5 & info) {\n case 0: { // GC\n const len = decoder.readLen();\n refs[i] = new GC(createID(client, clock), len);\n clock += len;\n break\n }\n case 10: { // Skip Struct (nothing to apply)\n // @todo we could reduce the amount of checks by adding Skip struct to clientRefs so we know that something is missing.\n const len = decoding.readVarUint(decoder.restDecoder);\n refs[i] = new Skip(createID(client, clock), len);\n clock += len;\n break\n }\n default: { // Item with content\n /**\n * The optimized implementation doesn't use any variables because inlining variables is faster.\n * Below a non-optimized version is shown that implements the basic algorithm with\n * a few comments\n */\n const cantCopyParentInfo = (info & (binary.BIT7 | binary.BIT8)) === 0;\n // If parent = null and neither left nor right are defined, then we know that `parent` is child of `y`\n // and we read the next string as parentYKey.\n // It indicates how we store/retrieve parent from `y.share`\n // @type {string|null}\n const struct = new Item(\n createID(client, clock),\n null, // leftd\n (info & binary.BIT8) === binary.BIT8 ? decoder.readLeftID() : null, // origin\n null, // right\n (info & binary.BIT7) === binary.BIT7 ? decoder.readRightID() : null, // right origin\n cantCopyParentInfo ? (decoder.readParentInfo() ? doc.get(decoder.readString()) : decoder.readLeftID()) : null, // parent\n cantCopyParentInfo && (info & binary.BIT6) === binary.BIT6 ? decoder.readString() : null, // parentSub\n readItemContent(decoder, info) // item content\n );\n /* A non-optimized implementation of the above algorithm:\n\n // The item that was originally to the left of this item.\n const origin = (info & binary.BIT8) === binary.BIT8 ? decoder.readLeftID() : null\n // The item that was originally to the right of this item.\n const rightOrigin = (info & binary.BIT7) === binary.BIT7 ? decoder.readRightID() : null\n const cantCopyParentInfo = (info & (binary.BIT7 | binary.BIT8)) === 0\n const hasParentYKey = cantCopyParentInfo ? decoder.readParentInfo() : false\n // If parent = null and neither left nor right are defined, then we know that `parent` is child of `y`\n // and we read the next string as parentYKey.\n // It indicates how we store/retrieve parent from `y.share`\n // @type {string|null}\n const parentYKey = cantCopyParentInfo && hasParentYKey ? decoder.readString() : null\n\n const struct = new Item(\n createID(client, clock),\n null, // leftd\n origin, // origin\n null, // right\n rightOrigin, // right origin\n cantCopyParentInfo && !hasParentYKey ? decoder.readLeftID() : (parentYKey !== null ? doc.get(parentYKey) : null), // parent\n cantCopyParentInfo && (info & binary.BIT6) === binary.BIT6 ? decoder.readString() : null, // parentSub\n readItemContent(decoder, info) // item content\n )\n */\n refs[i] = struct;\n clock += struct.length;\n }\n }\n }\n // console.log('time to read: ', performance.now() - start) // @todo remove\n }\n return clientRefs\n};\n\n/**\n * Resume computing structs generated by struct readers.\n *\n * While there is something to do, we integrate structs in this order\n * 1. top element on stack, if stack is not empty\n * 2. next element from current struct reader (if empty, use next struct reader)\n *\n * If struct causally depends on another struct (ref.missing), we put next reader of\n * `ref.id.client` on top of stack.\n *\n * At some point we find a struct that has no causal dependencies,\n * then we start emptying the stack.\n *\n * It is not possible to have circles: i.e. struct1 (from client1) depends on struct2 (from client2)\n * depends on struct3 (from client1). Therefore the max stack size is eqaul to `structReaders.length`.\n *\n * This method is implemented in a way so that we can resume computation if this update\n * causally depends on another update.\n *\n * @param {Transaction} transaction\n * @param {StructStore} store\n * @param {Map} clientsStructRefs\n * @return { null | { update: Uint8Array, missing: Map } }\n *\n * @private\n * @function\n */\nconst integrateStructs = (transaction, store, clientsStructRefs) => {\n /**\n * @type {Array- }\n */\n const stack = [];\n // sort them so that we take the higher id first, in case of conflicts the lower id will probably not conflict with the id from the higher user.\n let clientsStructRefsIds = array.from(clientsStructRefs.keys()).sort((a, b) => a - b);\n if (clientsStructRefsIds.length === 0) {\n return null\n }\n const getNextStructTarget = () => {\n if (clientsStructRefsIds.length === 0) {\n return null\n }\n let nextStructsTarget = /** @type {{i:number,refs:Array}} */ (clientsStructRefs.get(clientsStructRefsIds[clientsStructRefsIds.length - 1]));\n while (nextStructsTarget.refs.length === nextStructsTarget.i) {\n clientsStructRefsIds.pop();\n if (clientsStructRefsIds.length > 0) {\n nextStructsTarget = /** @type {{i:number,refs:Array}} */ (clientsStructRefs.get(clientsStructRefsIds[clientsStructRefsIds.length - 1]));\n } else {\n return null\n }\n }\n return nextStructsTarget\n };\n let curStructsTarget = getNextStructTarget();\n if (curStructsTarget === null && stack.length === 0) {\n return null\n }\n\n /**\n * @type {StructStore}\n */\n const restStructs = new StructStore();\n const missingSV = new Map();\n /**\n * @param {number} client\n * @param {number} clock\n */\n const updateMissingSv = (client, clock) => {\n const mclock = missingSV.get(client);\n if (mclock == null || mclock > clock) {\n missingSV.set(client, clock);\n }\n };\n /**\n * @type {GC|Item}\n */\n let stackHead = /** @type {any} */ (curStructsTarget).refs[/** @type {any} */ (curStructsTarget).i++];\n // caching the state because it is used very often\n const state = new Map();\n\n const addStackToRestSS = () => {\n for (const item of stack) {\n const client = item.id.client;\n const unapplicableItems = clientsStructRefs.get(client);\n if (unapplicableItems) {\n // decrement because we weren't able to apply previous operation\n unapplicableItems.i--;\n restStructs.clients.set(client, unapplicableItems.refs.slice(unapplicableItems.i));\n clientsStructRefs.delete(client);\n unapplicableItems.i = 0;\n unapplicableItems.refs = [];\n } else {\n // item was the last item on clientsStructRefs and the field was already cleared. Add item to restStructs and continue\n restStructs.clients.set(client, [item]);\n }\n // remove client from clientsStructRefsIds to prevent users from applying the same update again\n clientsStructRefsIds = clientsStructRefsIds.filter(c => c !== client);\n }\n stack.length = 0;\n };\n\n // iterate over all struct readers until we are done\n while (true) {\n if (stackHead.constructor !== Skip) {\n const localClock = map.setIfUndefined(state, stackHead.id.client, () => getState(store, stackHead.id.client));\n const offset = localClock - stackHead.id.clock;\n if (offset < 0) {\n // update from the same client is missing\n stack.push(stackHead);\n updateMissingSv(stackHead.id.client, stackHead.id.clock - 1);\n // hid a dead wall, add all items from stack to restSS\n addStackToRestSS();\n } else {\n const missing = stackHead.getMissing(transaction, store);\n if (missing !== null) {\n stack.push(stackHead);\n // get the struct reader that has the missing struct\n /**\n * @type {{ refs: Array, i: number }}\n */\n const structRefs = clientsStructRefs.get(/** @type {number} */ (missing)) || { refs: [], i: 0 };\n if (structRefs.refs.length === structRefs.i) {\n // This update message causally depends on another update message that doesn't exist yet\n updateMissingSv(/** @type {number} */ (missing), getState(store, missing));\n addStackToRestSS();\n } else {\n stackHead = structRefs.refs[structRefs.i++];\n continue\n }\n } else if (offset === 0 || offset < stackHead.length) {\n // all fine, apply the stackhead\n stackHead.integrate(transaction, offset);\n state.set(stackHead.id.client, stackHead.id.clock + stackHead.length);\n }\n }\n }\n // iterate to next stackHead\n if (stack.length > 0) {\n stackHead = /** @type {GC|Item} */ (stack.pop());\n } else if (curStructsTarget !== null && curStructsTarget.i < curStructsTarget.refs.length) {\n stackHead = /** @type {GC|Item} */ (curStructsTarget.refs[curStructsTarget.i++]);\n } else {\n curStructsTarget = getNextStructTarget();\n if (curStructsTarget === null) {\n // we are done!\n break\n } else {\n stackHead = /** @type {GC|Item} */ (curStructsTarget.refs[curStructsTarget.i++]);\n }\n }\n }\n if (restStructs.clients.size > 0) {\n const encoder = new UpdateEncoderV2();\n writeClientsStructs(encoder, restStructs, new Map());\n // write empty deleteset\n // writeDeleteSet(encoder, new DeleteSet())\n encoding.writeVarUint(encoder.restEncoder, 0); // => no need for an extra function call, just write 0 deletes\n return { missing: missingSV, update: encoder.toUint8Array() }\n }\n return null\n};\n\n/**\n * @param {UpdateEncoderV1 | UpdateEncoderV2} encoder\n * @param {Transaction} transaction\n *\n * @private\n * @function\n */\nconst writeStructsFromTransaction = (encoder, transaction) => writeClientsStructs(encoder, transaction.doc.store, transaction.beforeState);\n\n/**\n * Read and apply a document update.\n *\n * This function has the same effect as `applyUpdate` but accepts an decoder.\n *\n * @param {decoding.Decoder} decoder\n * @param {Doc} ydoc\n * @param {any} [transactionOrigin] This will be stored on `transaction.origin` and `.on('update', (update, origin))`\n * @param {UpdateDecoderV1 | UpdateDecoderV2} [structDecoder]\n *\n * @function\n */\nconst readUpdateV2 = (decoder, ydoc, transactionOrigin, structDecoder = new UpdateDecoderV2(decoder)) =>\n transact(ydoc, transaction => {\n // force that transaction.local is set to non-local\n transaction.local = false;\n let retry = false;\n const doc = transaction.doc;\n const store = doc.store;\n // let start = performance.now()\n const ss = readClientsStructRefs(structDecoder, doc);\n // console.log('time to read structs: ', performance.now() - start) // @todo remove\n // start = performance.now()\n // console.log('time to merge: ', performance.now() - start) // @todo remove\n // start = performance.now()\n const restStructs = integrateStructs(transaction, store, ss);\n const pending = store.pendingStructs;\n if (pending) {\n // check if we can apply something\n for (const [client, clock] of pending.missing) {\n if (clock < getState(store, client)) {\n retry = true;\n break\n }\n }\n if (restStructs) {\n // merge restStructs into store.pending\n for (const [client, clock] of restStructs.missing) {\n const mclock = pending.missing.get(client);\n if (mclock == null || mclock > clock) {\n pending.missing.set(client, clock);\n }\n }\n pending.update = mergeUpdatesV2([pending.update, restStructs.update]);\n }\n } else {\n store.pendingStructs = restStructs;\n }\n // console.log('time to integrate: ', performance.now() - start) // @todo remove\n // start = performance.now()\n const dsRest = readAndApplyDeleteSet(structDecoder, transaction, store);\n if (store.pendingDs) {\n // @todo we could make a lower-bound state-vector check as we do above\n const pendingDSUpdate = new UpdateDecoderV2(decoding.createDecoder(store.pendingDs));\n decoding.readVarUint(pendingDSUpdate.restDecoder); // read 0 structs, because we only encode deletes in pendingdsupdate\n const dsRest2 = readAndApplyDeleteSet(pendingDSUpdate, transaction, store);\n if (dsRest && dsRest2) {\n // case 1: ds1 != null && ds2 != null\n store.pendingDs = mergeUpdatesV2([dsRest, dsRest2]);\n } else {\n // case 2: ds1 != null\n // case 3: ds2 != null\n // case 4: ds1 == null && ds2 == null\n store.pendingDs = dsRest || dsRest2;\n }\n } else {\n // Either dsRest == null && pendingDs == null OR dsRest != null\n store.pendingDs = dsRest;\n }\n // console.log('time to cleanup: ', performance.now() - start) // @todo remove\n // start = performance.now()\n\n // console.log('time to resume delete readers: ', performance.now() - start) // @todo remove\n // start = performance.now()\n if (retry) {\n const update = /** @type {{update: Uint8Array}} */ (store.pendingStructs).update;\n store.pendingStructs = null;\n applyUpdateV2(transaction.doc, update);\n }\n }, transactionOrigin, false);\n\n/**\n * Read and apply a document update.\n *\n * This function has the same effect as `applyUpdate` but accepts an decoder.\n *\n * @param {decoding.Decoder} decoder\n * @param {Doc} ydoc\n * @param {any} [transactionOrigin] This will be stored on `transaction.origin` and `.on('update', (update, origin))`\n *\n * @function\n */\nconst readUpdate = (decoder, ydoc, transactionOrigin) => readUpdateV2(decoder, ydoc, transactionOrigin, new UpdateDecoderV1(decoder));\n\n/**\n * Apply a document update created by, for example, `y.on('update', update => ..)` or `update = encodeStateAsUpdate()`.\n *\n * This function has the same effect as `readUpdate` but accepts an Uint8Array instead of a Decoder.\n *\n * @param {Doc} ydoc\n * @param {Uint8Array} update\n * @param {any} [transactionOrigin] This will be stored on `transaction.origin` and `.on('update', (update, origin))`\n * @param {typeof UpdateDecoderV1 | typeof UpdateDecoderV2} [YDecoder]\n *\n * @function\n */\nconst applyUpdateV2 = (ydoc, update, transactionOrigin, YDecoder = UpdateDecoderV2) => {\n const decoder = decoding.createDecoder(update);\n readUpdateV2(decoder, ydoc, transactionOrigin, new YDecoder(decoder));\n};\n\n/**\n * Apply a document update created by, for example, `y.on('update', update => ..)` or `update = encodeStateAsUpdate()`.\n *\n * This function has the same effect as `readUpdate` but accepts an Uint8Array instead of a Decoder.\n *\n * @param {Doc} ydoc\n * @param {Uint8Array} update\n * @param {any} [transactionOrigin] This will be stored on `transaction.origin` and `.on('update', (update, origin))`\n *\n * @function\n */\nconst applyUpdate = (ydoc, update, transactionOrigin) => applyUpdateV2(ydoc, update, transactionOrigin, UpdateDecoderV1);\n\n/**\n * Write all the document as a single update message. If you specify the state of the remote client (`targetStateVector`) it will\n * only write the operations that are missing.\n *\n * @param {UpdateEncoderV1 | UpdateEncoderV2} encoder\n * @param {Doc} doc\n * @param {Map} [targetStateVector] The state of the target that receives the update. Leave empty to write all known structs\n *\n * @function\n */\nconst writeStateAsUpdate = (encoder, doc, targetStateVector = new Map()) => {\n writeClientsStructs(encoder, doc.store, targetStateVector);\n writeDeleteSet(encoder, createDeleteSetFromStructStore(doc.store));\n};\n\n/**\n * Write all the document as a single update message that can be applied on the remote document. If you specify the state of the remote client (`targetState`) it will\n * only write the operations that are missing.\n *\n * Use `writeStateAsUpdate` instead if you are working with lib0/encoding.js#Encoder\n *\n * @param {Doc} doc\n * @param {Uint8Array} [encodedTargetStateVector] The state of the target that receives the update. Leave empty to write all known structs\n * @param {UpdateEncoderV1 | UpdateEncoderV2} [encoder]\n * @return {Uint8Array}\n *\n * @function\n */\nconst encodeStateAsUpdateV2 = (doc, encodedTargetStateVector = new Uint8Array([0]), encoder = new UpdateEncoderV2()) => {\n const targetStateVector = decodeStateVector(encodedTargetStateVector);\n writeStateAsUpdate(encoder, doc, targetStateVector);\n const updates = [encoder.toUint8Array()];\n // also add the pending updates (if there are any)\n if (doc.store.pendingDs) {\n updates.push(doc.store.pendingDs);\n }\n if (doc.store.pendingStructs) {\n updates.push(diffUpdateV2(doc.store.pendingStructs.update, encodedTargetStateVector));\n }\n if (updates.length > 1) {\n if (encoder.constructor === UpdateEncoderV1) {\n return mergeUpdates(updates.map((update, i) => i === 0 ? update : convertUpdateFormatV2ToV1(update)))\n } else if (encoder.constructor === UpdateEncoderV2) {\n return mergeUpdatesV2(updates)\n }\n }\n return updates[0]\n};\n\n/**\n * Write all the document as a single update message that can be applied on the remote document. If you specify the state of the remote client (`targetState`) it will\n * only write the operations that are missing.\n *\n * Use `writeStateAsUpdate` instead if you are working with lib0/encoding.js#Encoder\n *\n * @param {Doc} doc\n * @param {Uint8Array} [encodedTargetStateVector] The state of the target that receives the update. Leave empty to write all known structs\n * @return {Uint8Array}\n *\n * @function\n */\nconst encodeStateAsUpdate = (doc, encodedTargetStateVector) => encodeStateAsUpdateV2(doc, encodedTargetStateVector, new UpdateEncoderV1());\n\n/**\n * Read state vector from Decoder and return as Map\n *\n * @param {DSDecoderV1 | DSDecoderV2} decoder\n * @return {Map} Maps `client` to the number next expected `clock` from that client.\n *\n * @function\n */\nconst readStateVector = decoder => {\n const ss = new Map();\n const ssLength = decoding.readVarUint(decoder.restDecoder);\n for (let i = 0; i < ssLength; i++) {\n const client = decoding.readVarUint(decoder.restDecoder);\n const clock = decoding.readVarUint(decoder.restDecoder);\n ss.set(client, clock);\n }\n return ss\n};\n\n/**\n * Read decodedState and return State as Map.\n *\n * @param {Uint8Array} decodedState\n * @return {Map} Maps `client` to the number next expected `clock` from that client.\n *\n * @function\n */\n// export const decodeStateVectorV2 = decodedState => readStateVector(new DSDecoderV2(decoding.createDecoder(decodedState)))\n\n/**\n * Read decodedState and return State as Map.\n *\n * @param {Uint8Array} decodedState\n * @return {Map} Maps `client` to the number next expected `clock` from that client.\n *\n * @function\n */\nconst decodeStateVector = decodedState => readStateVector(new DSDecoderV1(decoding.createDecoder(decodedState)));\n\n/**\n * @param {DSEncoderV1 | DSEncoderV2} encoder\n * @param {Map} sv\n * @function\n */\nconst writeStateVector = (encoder, sv) => {\n encoding.writeVarUint(encoder.restEncoder, sv.size);\n array.from(sv.entries()).sort((a, b) => b[0] - a[0]).forEach(([client, clock]) => {\n encoding.writeVarUint(encoder.restEncoder, client); // @todo use a special client decoder that is based on mapping\n encoding.writeVarUint(encoder.restEncoder, clock);\n });\n return encoder\n};\n\n/**\n * @param {DSEncoderV1 | DSEncoderV2} encoder\n * @param {Doc} doc\n *\n * @function\n */\nconst writeDocumentStateVector = (encoder, doc) => writeStateVector(encoder, getStateVector(doc.store));\n\n/**\n * Encode State as Uint8Array.\n *\n * @param {Doc|Map} doc\n * @param {DSEncoderV1 | DSEncoderV2} [encoder]\n * @return {Uint8Array}\n *\n * @function\n */\nconst encodeStateVectorV2 = (doc, encoder = new DSEncoderV2()) => {\n if (doc instanceof Map) {\n writeStateVector(encoder, doc);\n } else {\n writeDocumentStateVector(encoder, doc);\n }\n return encoder.toUint8Array()\n};\n\n/**\n * Encode State as Uint8Array.\n *\n * @param {Doc|Map} doc\n * @return {Uint8Array}\n *\n * @function\n */\nconst encodeStateVector = doc => encodeStateVectorV2(doc, new DSEncoderV1());\n\n/**\n * General event handler implementation.\n *\n * @template ARG0, ARG1\n *\n * @private\n */\nclass EventHandler {\n constructor () {\n /**\n * @type {Array}\n */\n this.l = [];\n }\n}\n\n/**\n * @template ARG0,ARG1\n * @returns {EventHandler}\n *\n * @private\n * @function\n */\nconst createEventHandler = () => new EventHandler();\n\n/**\n * Adds an event listener that is called when\n * {@link EventHandler#callEventListeners} is called.\n *\n * @template ARG0,ARG1\n * @param {EventHandler} eventHandler\n * @param {function(ARG0,ARG1):void} f The event handler.\n *\n * @private\n * @function\n */\nconst addEventHandlerListener = (eventHandler, f) =>\n eventHandler.l.push(f);\n\n/**\n * Removes an event listener.\n *\n * @template ARG0,ARG1\n * @param {EventHandler} eventHandler\n * @param {function(ARG0,ARG1):void} f The event handler that was added with\n * {@link EventHandler#addEventListener}\n *\n * @private\n * @function\n */\nconst removeEventHandlerListener = (eventHandler, f) => {\n const l = eventHandler.l;\n const len = l.length;\n eventHandler.l = l.filter(g => f !== g);\n if (len === eventHandler.l.length) {\n console.error('[yjs] Tried to remove event handler that doesn\\'t exist.');\n }\n};\n\n/**\n * Call all event listeners that were added via\n * {@link EventHandler#addEventListener}.\n *\n * @template ARG0,ARG1\n * @param {EventHandler} eventHandler\n * @param {ARG0} arg0\n * @param {ARG1} arg1\n *\n * @private\n * @function\n */\nconst callEventHandlerListeners = (eventHandler, arg0, arg1) =>\n f.callAll(eventHandler.l, [arg0, arg1]);\n\nclass ID {\n /**\n * @param {number} client client id\n * @param {number} clock unique per client id, continuous number\n */\n constructor (client, clock) {\n /**\n * Client id\n * @type {number}\n */\n this.client = client;\n /**\n * unique per client id, continuous number\n * @type {number}\n */\n this.clock = clock;\n }\n}\n\n/**\n * @param {ID | null} a\n * @param {ID | null} b\n * @return {boolean}\n *\n * @function\n */\nconst compareIDs = (a, b) => a === b || (a !== null && b !== null && a.client === b.client && a.clock === b.clock);\n\n/**\n * @param {number} client\n * @param {number} clock\n *\n * @private\n * @function\n */\nconst createID = (client, clock) => new ID(client, clock);\n\n/**\n * @param {encoding.Encoder} encoder\n * @param {ID} id\n *\n * @private\n * @function\n */\nconst writeID = (encoder, id) => {\n encoding.writeVarUint(encoder, id.client);\n encoding.writeVarUint(encoder, id.clock);\n};\n\n/**\n * Read ID.\n * * If first varUint read is 0xFFFFFF a RootID is returned.\n * * Otherwise an ID is returned\n *\n * @param {decoding.Decoder} decoder\n * @return {ID}\n *\n * @private\n * @function\n */\nconst readID = decoder =>\n createID(decoding.readVarUint(decoder), decoding.readVarUint(decoder));\n\n/**\n * The top types are mapped from y.share.get(keyname) => type.\n * `type` does not store any information about the `keyname`.\n * This function finds the correct `keyname` for `type` and throws otherwise.\n *\n * @param {AbstractType} type\n * @return {string}\n *\n * @private\n * @function\n */\nconst findRootTypeKey = type => {\n // @ts-ignore _y must be defined, otherwise unexpected case\n for (const [key, value] of type.doc.share.entries()) {\n if (value === type) {\n return key\n }\n }\n throw error.unexpectedCase()\n};\n\n/**\n * Check if `parent` is a parent of `child`.\n *\n * @param {AbstractType} parent\n * @param {Item|null} child\n * @return {Boolean} Whether `parent` is a parent of `child`.\n *\n * @private\n * @function\n */\nconst isParentOf = (parent, child) => {\n while (child !== null) {\n if (child.parent === parent) {\n return true\n }\n child = /** @type {AbstractType} */ (child.parent)._item;\n }\n return false\n};\n\n/**\n * Convenient helper to log type information.\n *\n * Do not use in productive systems as the output can be immense!\n *\n * @param {AbstractType} type\n */\nconst logType = type => {\n const res = [];\n let n = type._start;\n while (n) {\n res.push(n);\n n = n.right;\n }\n console.log('Children: ', res);\n console.log('Children content: ', res.filter(m => !m.deleted).map(m => m.content));\n};\n\nclass PermanentUserData {\n /**\n * @param {Doc} doc\n * @param {YMap} [storeType]\n */\n constructor (doc, storeType = doc.getMap('users')) {\n /**\n * @type {Map}\n */\n const dss = new Map();\n this.yusers = storeType;\n this.doc = doc;\n /**\n * Maps from clientid to userDescription\n *\n * @type {Map}\n */\n this.clients = new Map();\n this.dss = dss;\n /**\n * @param {YMap} user\n * @param {string} userDescription\n */\n const initUser = (user, userDescription) => {\n /**\n * @type {YArray}\n */\n const ds = user.get('ds');\n const ids = user.get('ids');\n const addClientId = /** @param {number} clientid */ clientid => this.clients.set(clientid, userDescription);\n ds.observe(/** @param {YArrayEvent} event */ event => {\n event.changes.added.forEach(item => {\n item.content.getContent().forEach(encodedDs => {\n if (encodedDs instanceof Uint8Array) {\n this.dss.set(userDescription, mergeDeleteSets([this.dss.get(userDescription) || createDeleteSet(), readDeleteSet(new DSDecoderV1(decoding.createDecoder(encodedDs)))]));\n }\n });\n });\n });\n this.dss.set(userDescription, mergeDeleteSets(ds.map(encodedDs => readDeleteSet(new DSDecoderV1(decoding.createDecoder(encodedDs))))));\n ids.observe(/** @param {YArrayEvent} event */ event =>\n event.changes.added.forEach(item => item.content.getContent().forEach(addClientId))\n );\n ids.forEach(addClientId);\n };\n // observe users\n storeType.observe(event => {\n event.keysChanged.forEach(userDescription =>\n initUser(storeType.get(userDescription), userDescription)\n );\n });\n // add intial data\n storeType.forEach(initUser);\n }\n\n /**\n * @param {Doc} doc\n * @param {number} clientid\n * @param {string} userDescription\n * @param {Object} conf\n * @param {function(Transaction, DeleteSet):boolean} [conf.filter]\n */\n setUserMapping (doc, clientid, userDescription, { filter = () => true } = {}) {\n const users = this.yusers;\n let user = users.get(userDescription);\n if (!user) {\n user = new YMap();\n user.set('ids', new YArray());\n user.set('ds', new YArray());\n users.set(userDescription, user);\n }\n user.get('ids').push([clientid]);\n users.observe(_event => {\n setTimeout(() => {\n const userOverwrite = users.get(userDescription);\n if (userOverwrite !== user) {\n // user was overwritten, port all data over to the next user object\n // @todo Experiment with Y.Sets here\n user = userOverwrite;\n // @todo iterate over old type\n this.clients.forEach((_userDescription, clientid) => {\n if (userDescription === _userDescription) {\n user.get('ids').push([clientid]);\n }\n });\n const encoder = new DSEncoderV1();\n const ds = this.dss.get(userDescription);\n if (ds) {\n writeDeleteSet(encoder, ds);\n user.get('ds').push([encoder.toUint8Array()]);\n }\n }\n }, 0);\n });\n doc.on('afterTransaction', /** @param {Transaction} transaction */ transaction => {\n setTimeout(() => {\n const yds = user.get('ds');\n const ds = transaction.deleteSet;\n if (transaction.local && ds.clients.size > 0 && filter(transaction, ds)) {\n const encoder = new DSEncoderV1();\n writeDeleteSet(encoder, ds);\n yds.push([encoder.toUint8Array()]);\n }\n });\n });\n }\n\n /**\n * @param {number} clientid\n * @return {any}\n */\n getUserByClientId (clientid) {\n return this.clients.get(clientid) || null\n }\n\n /**\n * @param {ID} id\n * @return {string | null}\n */\n getUserByDeletedId (id) {\n for (const [userDescription, ds] of this.dss.entries()) {\n if (isDeleted(ds, id)) {\n return userDescription\n }\n }\n return null\n }\n}\n\n/**\n * A relative position is based on the Yjs model and is not affected by document changes.\n * E.g. If you place a relative position before a certain character, it will always point to this character.\n * If you place a relative position at the end of a type, it will always point to the end of the type.\n *\n * A numeric position is often unsuited for user selections, because it does not change when content is inserted\n * before or after.\n *\n * ```Insert(0, 'x')('a|bc') = 'xa|bc'``` Where | is the relative position.\n *\n * One of the properties must be defined.\n *\n * @example\n * // Current cursor position is at position 10\n * const relativePosition = createRelativePositionFromIndex(yText, 10)\n * // modify yText\n * yText.insert(0, 'abc')\n * yText.delete(3, 10)\n * // Compute the cursor position\n * const absolutePosition = createAbsolutePositionFromRelativePosition(y, relativePosition)\n * absolutePosition.type === yText // => true\n * console.log('cursor location is ' + absolutePosition.index) // => cursor location is 3\n *\n */\nclass RelativePosition {\n /**\n * @param {ID|null} type\n * @param {string|null} tname\n * @param {ID|null} item\n * @param {number} assoc\n */\n constructor (type, tname, item, assoc = 0) {\n /**\n * @type {ID|null}\n */\n this.type = type;\n /**\n * @type {string|null}\n */\n this.tname = tname;\n /**\n * @type {ID | null}\n */\n this.item = item;\n /**\n * A relative position is associated to a specific character. By default\n * assoc >= 0, the relative position is associated to the character\n * after the meant position.\n * I.e. position 1 in 'ab' is associated to character 'b'.\n *\n * If assoc < 0, then the relative position is associated to the caharacter\n * before the meant position.\n *\n * @type {number}\n */\n this.assoc = assoc;\n }\n}\n\n/**\n * @param {RelativePosition} rpos\n * @return {any}\n */\nconst relativePositionToJSON = rpos => {\n const json = {};\n if (rpos.type) {\n json.type = rpos.type;\n }\n if (rpos.tname) {\n json.tname = rpos.tname;\n }\n if (rpos.item) {\n json.item = rpos.item;\n }\n if (rpos.assoc != null) {\n json.assoc = rpos.assoc;\n }\n return json\n};\n\n/**\n * @param {any} json\n * @return {RelativePosition}\n *\n * @function\n */\nconst createRelativePositionFromJSON = json => new RelativePosition(json.type == null ? null : createID(json.type.client, json.type.clock), json.tname || null, json.item == null ? null : createID(json.item.client, json.item.clock), json.assoc == null ? 0 : json.assoc);\n\nclass AbsolutePosition {\n /**\n * @param {AbstractType} type\n * @param {number} index\n * @param {number} [assoc]\n */\n constructor (type, index, assoc = 0) {\n /**\n * @type {AbstractType}\n */\n this.type = type;\n /**\n * @type {number}\n */\n this.index = index;\n this.assoc = assoc;\n }\n}\n\n/**\n * @param {AbstractType} type\n * @param {number} index\n * @param {number} [assoc]\n *\n * @function\n */\nconst createAbsolutePosition = (type, index, assoc = 0) => new AbsolutePosition(type, index, assoc);\n\n/**\n * @param {AbstractType} type\n * @param {ID|null} item\n * @param {number} [assoc]\n *\n * @function\n */\nconst createRelativePosition = (type, item, assoc) => {\n let typeid = null;\n let tname = null;\n if (type._item === null) {\n tname = findRootTypeKey(type);\n } else {\n typeid = createID(type._item.id.client, type._item.id.clock);\n }\n return new RelativePosition(typeid, tname, item, assoc)\n};\n\n/**\n * Create a relativePosition based on a absolute position.\n *\n * @param {AbstractType} type The base type (e.g. YText or YArray).\n * @param {number} index The absolute position.\n * @param {number} [assoc]\n * @return {RelativePosition}\n *\n * @function\n */\nconst createRelativePositionFromTypeIndex = (type, index, assoc = 0) => {\n let t = type._start;\n if (assoc < 0) {\n // associated to the left character or the beginning of a type, increment index if possible.\n if (index === 0) {\n return createRelativePosition(type, null, assoc)\n }\n index--;\n }\n while (t !== null) {\n if (!t.deleted && t.countable) {\n if (t.length > index) {\n // case 1: found position somewhere in the linked list\n return createRelativePosition(type, createID(t.id.client, t.id.clock + index), assoc)\n }\n index -= t.length;\n }\n if (t.right === null && assoc < 0) {\n // left-associated position, return last available id\n return createRelativePosition(type, t.lastId, assoc)\n }\n t = t.right;\n }\n return createRelativePosition(type, null, assoc)\n};\n\n/**\n * @param {encoding.Encoder} encoder\n * @param {RelativePosition} rpos\n *\n * @function\n */\nconst writeRelativePosition = (encoder, rpos) => {\n const { type, tname, item, assoc } = rpos;\n if (item !== null) {\n encoding.writeVarUint(encoder, 0);\n writeID(encoder, item);\n } else if (tname !== null) {\n // case 2: found position at the end of the list and type is stored in y.share\n encoding.writeUint8(encoder, 1);\n encoding.writeVarString(encoder, tname);\n } else if (type !== null) {\n // case 3: found position at the end of the list and type is attached to an item\n encoding.writeUint8(encoder, 2);\n writeID(encoder, type);\n } else {\n throw error.unexpectedCase()\n }\n encoding.writeVarInt(encoder, assoc);\n return encoder\n};\n\n/**\n * @param {RelativePosition} rpos\n * @return {Uint8Array}\n */\nconst encodeRelativePosition = rpos => {\n const encoder = encoding.createEncoder();\n writeRelativePosition(encoder, rpos);\n return encoding.toUint8Array(encoder)\n};\n\n/**\n * @param {decoding.Decoder} decoder\n * @return {RelativePosition}\n *\n * @function\n */\nconst readRelativePosition = decoder => {\n let type = null;\n let tname = null;\n let itemID = null;\n switch (decoding.readVarUint(decoder)) {\n case 0:\n // case 1: found position somewhere in the linked list\n itemID = readID(decoder);\n break\n case 1:\n // case 2: found position at the end of the list and type is stored in y.share\n tname = decoding.readVarString(decoder);\n break\n case 2: {\n // case 3: found position at the end of the list and type is attached to an item\n type = readID(decoder);\n }\n }\n const assoc = decoding.hasContent(decoder) ? decoding.readVarInt(decoder) : 0;\n return new RelativePosition(type, tname, itemID, assoc)\n};\n\n/**\n * @param {Uint8Array} uint8Array\n * @return {RelativePosition}\n */\nconst decodeRelativePosition = uint8Array => readRelativePosition(decoding.createDecoder(uint8Array));\n\n/**\n * @param {RelativePosition} rpos\n * @param {Doc} doc\n * @return {AbsolutePosition|null}\n *\n * @function\n */\nconst createAbsolutePositionFromRelativePosition = (rpos, doc) => {\n const store = doc.store;\n const rightID = rpos.item;\n const typeID = rpos.type;\n const tname = rpos.tname;\n const assoc = rpos.assoc;\n let type = null;\n let index = 0;\n if (rightID !== null) {\n if (getState(store, rightID.client) <= rightID.clock) {\n return null\n }\n const res = followRedone(store, rightID);\n const right = res.item;\n if (!(right instanceof Item)) {\n return null\n }\n type = /** @type {AbstractType} */ (right.parent);\n if (type._item === null || !type._item.deleted) {\n index = (right.deleted || !right.countable) ? 0 : (res.diff + (assoc >= 0 ? 0 : 1)); // adjust position based on left association if necessary\n let n = right.left;\n while (n !== null) {\n if (!n.deleted && n.countable) {\n index += n.length;\n }\n n = n.left;\n }\n }\n } else {\n if (tname !== null) {\n type = doc.get(tname);\n } else if (typeID !== null) {\n if (getState(store, typeID.client) <= typeID.clock) {\n // type does not exist yet\n return null\n }\n const { item } = followRedone(store, typeID);\n if (item instanceof Item && item.content instanceof ContentType) {\n type = item.content.type;\n } else {\n // struct is garbage collected\n return null\n }\n } else {\n throw error.unexpectedCase()\n }\n if (assoc >= 0) {\n index = type._length;\n } else {\n index = 0;\n }\n }\n return createAbsolutePosition(type, index, rpos.assoc)\n};\n\n/**\n * @param {RelativePosition|null} a\n * @param {RelativePosition|null} b\n * @return {boolean}\n *\n * @function\n */\nconst compareRelativePositions = (a, b) => a === b || (\n a !== null && b !== null && a.tname === b.tname && compareIDs(a.item, b.item) && compareIDs(a.type, b.type) && a.assoc === b.assoc\n);\n\nclass Snapshot {\n /**\n * @param {DeleteSet} ds\n * @param {Map} sv state map\n */\n constructor (ds, sv) {\n /**\n * @type {DeleteSet}\n */\n this.ds = ds;\n /**\n * State Map\n * @type {Map}\n */\n this.sv = sv;\n }\n}\n\n/**\n * @param {Snapshot} snap1\n * @param {Snapshot} snap2\n * @return {boolean}\n */\nconst equalSnapshots = (snap1, snap2) => {\n const ds1 = snap1.ds.clients;\n const ds2 = snap2.ds.clients;\n const sv1 = snap1.sv;\n const sv2 = snap2.sv;\n if (sv1.size !== sv2.size || ds1.size !== ds2.size) {\n return false\n }\n for (const [key, value] of sv1.entries()) {\n if (sv2.get(key) !== value) {\n return false\n }\n }\n for (const [client, dsitems1] of ds1.entries()) {\n const dsitems2 = ds2.get(client) || [];\n if (dsitems1.length !== dsitems2.length) {\n return false\n }\n for (let i = 0; i < dsitems1.length; i++) {\n const dsitem1 = dsitems1[i];\n const dsitem2 = dsitems2[i];\n if (dsitem1.clock !== dsitem2.clock || dsitem1.len !== dsitem2.len) {\n return false\n }\n }\n }\n return true\n};\n\n/**\n * @param {Snapshot} snapshot\n * @param {DSEncoderV1 | DSEncoderV2} [encoder]\n * @return {Uint8Array}\n */\nconst encodeSnapshotV2 = (snapshot, encoder = new DSEncoderV2()) => {\n writeDeleteSet(encoder, snapshot.ds);\n writeStateVector(encoder, snapshot.sv);\n return encoder.toUint8Array()\n};\n\n/**\n * @param {Snapshot} snapshot\n * @return {Uint8Array}\n */\nconst encodeSnapshot = snapshot => encodeSnapshotV2(snapshot, new DSEncoderV1());\n\n/**\n * @param {Uint8Array} buf\n * @param {DSDecoderV1 | DSDecoderV2} [decoder]\n * @return {Snapshot}\n */\nconst decodeSnapshotV2 = (buf, decoder = new DSDecoderV2(decoding.createDecoder(buf))) => {\n return new Snapshot(readDeleteSet(decoder), readStateVector(decoder))\n};\n\n/**\n * @param {Uint8Array} buf\n * @return {Snapshot}\n */\nconst decodeSnapshot = buf => decodeSnapshotV2(buf, new DSDecoderV1(decoding.createDecoder(buf)));\n\n/**\n * @param {DeleteSet} ds\n * @param {Map