n8n
Version:
n8n Workflow Automation Tool
39 lines • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniqueFilenameAllocator = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const slug_utils_1 = require("./slug.utils");
class UniqueFilenameAllocator {
constructor(baseDir, fallback) {
this.baseDir = baseDir;
this.fallback = fallback;
this.used = new Set();
}
reserve(segment) {
this.used.add(`${this.baseDir}/${segment}`);
}
reservePath(path) {
if (!path.startsWith(`${this.baseDir}/`)) {
throw new n8n_workflow_1.UnexpectedError('Cannot reserve a path outside the allocator base directory', {
extra: { path, baseDir: this.baseDir },
});
}
this.used.add(path);
}
allocate(name) {
const base = `${this.baseDir}/${(0, slug_utils_1.generateSlug)(name, this.fallback)}`;
if (!this.used.has(base)) {
this.used.add(base);
return base;
}
for (let suffix = 2;; suffix++) {
const candidate = `${base}-${suffix}`;
if (!this.used.has(candidate)) {
this.used.add(candidate);
return candidate;
}
}
}
}
exports.UniqueFilenameAllocator = UniqueFilenameAllocator;
//# sourceMappingURL=unique-filename-allocator.js.map