action-ansible-playbook/node_modules/yaml/dist/nodes/Alias.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-10-21 08:44:44 -06:00
'use strict';
var anchors = require('../doc/anchors.js');
var visit = require('../visit.js');
2024-01-17 02:08:10 -07:00
var identity = require('./identity.js');
2022-10-21 08:44:44 -06:00
var Node = require('./Node.js');
2024-01-17 02:08:10 -07:00
var toJS = require('./toJS.js');
2022-10-21 08:44:44 -06:00
class Alias extends Node.NodeBase {
constructor(source) {
2024-01-17 02:08:10 -07:00
super(identity.ALIAS);
2022-10-21 08:44:44 -06:00
this.source = source;
Object.defineProperty(this, 'tag', {
set() {
throw new Error('Alias nodes cannot have tags');
}
});
}
/**
* Resolve the value of this alias within `doc`, finding the last
* instance of the `source` anchor before this node.
*/
resolve(doc) {
let found = undefined;
visit.visit(doc, {
Node: (_key, node) => {
if (node === this)
return visit.visit.BREAK;
if (node.anchor === this.source)
found = node;
}
});
return found;
}
toJSON(_arg, ctx) {
if (!ctx)
return { source: this.source };
const { anchors, doc, maxAliasCount } = ctx;
const source = this.resolve(doc);
if (!source) {
const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
throw new ReferenceError(msg);
}
2024-01-17 02:08:10 -07:00
let data = anchors.get(source);
if (!data) {
// Resolve anchors for Node.prototype.toJS()
toJS.toJS(source, null, ctx);
data = anchors.get(source);
}
2022-10-21 08:44:44 -06:00
/* istanbul ignore if */
if (!data || data.res === undefined) {
const msg = 'This should not happen: Alias anchor was not resolved?';
throw new ReferenceError(msg);
}
if (maxAliasCount >= 0) {
data.count += 1;
if (data.aliasCount === 0)
data.aliasCount = getAliasCount(doc, source, anchors);
if (data.count * data.aliasCount > maxAliasCount) {
const msg = 'Excessive alias count indicates a resource exhaustion attack';
throw new ReferenceError(msg);
}
}
return data.res;
}
toString(ctx, _onComment, _onChompKeep) {
const src = `*${this.source}`;
if (ctx) {
anchors.anchorIsValid(this.source);
if (ctx.options.verifyAliasOrder && !ctx.anchors.has(this.source)) {
const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
throw new Error(msg);
}
if (ctx.implicitKey)
return `${src} `;
}
return src;
}
}
function getAliasCount(doc, node, anchors) {
2024-01-17 02:08:10 -07:00
if (identity.isAlias(node)) {
2022-10-21 08:44:44 -06:00
const source = node.resolve(doc);
const anchor = anchors && source && anchors.get(source);
return anchor ? anchor.count * anchor.aliasCount : 0;
}
2024-01-17 02:08:10 -07:00
else if (identity.isCollection(node)) {
2022-10-21 08:44:44 -06:00
let count = 0;
for (const item of node.items) {
const c = getAliasCount(doc, item, anchors);
if (c > count)
count = c;
}
return count;
}
2024-01-17 02:08:10 -07:00
else if (identity.isPair(node)) {
2022-10-21 08:44:44 -06:00
const kc = getAliasCount(doc, node.key, anchors);
const vc = getAliasCount(doc, node.value, anchors);
return Math.max(kc, vc);
}
return 1;
}
exports.Alias = Alias;