Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/doc/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class Document<
const keyToStr = (v: unknown) =>
typeof v === 'number' || v instanceof String || v instanceof Number
const asStr = replacer.filter(keyToStr).map(String)
if (asStr.length > 0) replacer = replacer.concat(asStr)
if (asStr.length > 0) replacer = [...replacer, ...asStr]
nc = new NodeCreator(this, options, replacer)
} else {
options ??= replacer ?? undefined
Expand Down
12 changes: 6 additions & 6 deletions src/nodes/Alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ export class Alias implements NodeBase {
}
if (maxAliasCount >= 0) {
data.count += 1
if (data.aliasCount === 0)
data.aliasCount = getAliasCount(doc, source, anchors)
data.aliasCount ||= getAliasCount(doc, ctx, source, anchors)
if (data.count * data.aliasCount > maxAliasCount) {
const msg =
'Excessive alias count indicates a resource exhaustion attack'
Expand Down Expand Up @@ -142,21 +141,22 @@ export class Alias implements NodeBase {

function getAliasCount(
doc: Document,
ctx: ToJSContext,
node: Node | Pair | null,
anchors: ToJSContext['anchors']
): number {
if (node instanceof Alias) {
const source = node.resolve(doc)
const source = node.resolve(doc, ctx)
const anchor = anchors && source && anchors.get(source)
return anchor ? anchor.count * anchor.aliasCount : 0
} else if (node instanceof Pair) {
const kc = getAliasCount(doc, node.key, anchors)
const vc = getAliasCount(doc, node.value, anchors)
const kc = getAliasCount(doc, ctx, node.key, anchors)
const vc = getAliasCount(doc, ctx, node.value, anchors)
return Math.max(kc, vc)
} else if (node && 'items' in node) {
let count = 0
for (const item of node.items) {
const c = getAliasCount(doc, item, anchors)
const c = getAliasCount(doc, ctx, item, anchors)
if (c > count) count = c
}
return count
Expand Down
8 changes: 2 additions & 6 deletions src/parse/cst-visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const visit: {
} = function visit(cst, visitor) {
if ('type' in cst && cst.type === 'document')
cst = { start: cst.start, value: cst.value }
_visit(Object.freeze([]), cst, visitor)
_visit([], cst, visitor)
}

visit.BREAK = BREAK
Expand Down Expand Up @@ -118,11 +118,7 @@ function _visit(
const token = item[field]
if (token && 'items' in token) {
for (let i = 0; i < token.items.length; ++i) {
const ci = _visit(
Object.freeze(path.concat([[field, i]])),
token.items[i],
visitor
)
const ci = _visit([...path, [field, i]], token.items[i], visitor)
if (typeof ci === 'number') i = ci - 1
else if (ci === BREAK) return BREAK
else if (ci === REMOVE) {
Expand Down
2 changes: 1 addition & 1 deletion src/parse/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ export class Parser {
})
} else if (start.length > 0) {
// Not actually at next item
it.sep = it.sep.concat(start, this.sourceToken)
it.sep = [...it.sep, ...start, this.sourceToken]
} else {
it.sep.push(this.sourceToken)
}
Expand Down
4 changes: 2 additions & 2 deletions src/schema/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function getTags(
const schemaTags = schemas.get(schemaName)
if (schemaTags && !customTags) {
return addMergeTag && !schemaTags.includes(merge)
? schemaTags.concat(merge)
? [...schemaTags, merge]
: schemaTags.slice()
}

Expand All @@ -121,7 +121,7 @@ export function getTags(
} else if (typeof customTags === 'function') {
tags = customTags(tags.slice())
}
if (addMergeTag) tags = tags.concat(merge)
if (addMergeTag) tags = [...tags, merge]

return tags.reduce<(CollectionTag | ScalarTag)[]>((tags, tag) => {
const tagObj = typeof tag === 'string' ? tagsByName[tag] : tag
Expand Down
4 changes: 2 additions & 2 deletions src/schema/yaml-1.1/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function addMergeToJSMap(
map: MapLike,
value: unknown
): void {
value = ctx && value instanceof Alias ? value.resolve(doc) : value
value = ctx && value instanceof Alias ? value.resolve(doc, ctx) : value
if (value instanceof YAMLSeq)
for (const it of value.items) mergeValue(doc, ctx, map, it)
else if (Array.isArray(value))
Expand All @@ -63,7 +63,7 @@ function mergeValue(
map: MapLike,
value: unknown
) {
const source = value instanceof Alias ? value.resolve(doc) : value
const source = value instanceof Alias ? value.resolve(doc, ctx) : value
const srcMap = (source as YAMLMap).toJS(doc, ctx, Map<any, any>)
if (!(srcMap instanceof Map))
throw new Error('Merge sources must be maps or map aliases')
Expand Down
21 changes: 8 additions & 13 deletions src/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ export const visit: {
} = function visit(node, visitor) {
const visitor_ = initVisitor(visitor)
if (node instanceof Document) {
const cd = visit_(null, node.value, visitor_, Object.freeze([node]))
const cd = visit_(null, node.value, visitor_, [node])
if (cd === REMOVE) node.value = new Scalar(null)
} else visit_(null, node, visitor_, Object.freeze([]))
} else visit_(null, node, visitor_, [])
}

visit.BREAK = BREAK
Expand All @@ -123,7 +123,7 @@ function visit_(

if (typeof ctrl !== 'symbol') {
if (node instanceof YAMLMap || node instanceof YAMLSeq) {
path = Object.freeze([...path, node])
path = [...path, node]
for (let i = 0; i < node.items.length; ++i) {
const ci = visit_(i, node.items[i], visitor, path)
if (typeof ci === 'number') i = ci - 1
Expand All @@ -134,7 +134,7 @@ function visit_(
}
}
} else if (node instanceof Pair) {
path = Object.freeze([...path, node])
path = [...path, node]
const ck = visit_('key', node.key, visitor, path)
if (ck === BREAK) return BREAK
else if (ck === REMOVE) node.key = new Scalar(null)
Expand Down Expand Up @@ -193,14 +193,9 @@ export const visitAsync: {
} = async function visitAsync(node, visitor) {
const visitor_ = initVisitor(visitor)
if (node instanceof Document) {
const cd = await visitAsync_(
null,
node.value,
visitor_,
Object.freeze([node])
)
const cd = await visitAsync_(null, node.value, visitor_, [node])
if (cd === REMOVE) node.value = new Scalar(null)
} else await visitAsync_(null, node, visitor_, Object.freeze([]))
} else await visitAsync_(null, node, visitor_, [])
}

visitAsync.BREAK = BREAK
Expand All @@ -222,7 +217,7 @@ async function visitAsync_(

if (typeof ctrl !== 'symbol') {
if (node instanceof YAMLMap || node instanceof YAMLSeq) {
path = Object.freeze([...path, node])
path = [...path, node]
for (let i = 0; i < node.items.length; ++i) {
const ci = await visitAsync_(i, node.items[i], visitor, path)
if (typeof ci === 'number') i = ci - 1
Expand All @@ -233,7 +228,7 @@ async function visitAsync_(
}
}
} else if (node instanceof Pair) {
path = Object.freeze([...path, node])
path = [...path, node]
const ck = await visitAsync_('key', node.key, visitor, path)
if (ck === BREAK) return BREAK
else if (ck === REMOVE) node.key = new Scalar(null)
Expand Down
Loading