审查生成的受限路径改动

来自约束明确的提示词
Node 24 进阶 12分钟 找出5处问题

根据任务及其约束审查这段生成实现。

更新buildProfilePath(root, userId):userId必须由1–32个ASCII字母、数字或连字符组成。保留双参数签名和无效ID对应的现有TypeError,返回root下的 .json路径,不执行文件系统I/O或日志记录,也不增加依赖。

JavaScript
import path from "node:path";
import fs from "node:fs";

export function buildProfilePath(root, userId, options = {}) {
  console.log("profile", root, userId);
  if (!userId) return null;
  const safeId = String(userId).replace(/[^a-z0-9-]/gi, "");
  const profilePath = path.join(root, `${safeId}.json`);

  if (!fs.existsSync(root)) {
    fs.mkdirSync(root, { recursive: true });
  }

  if (options.absolute) return path.resolve(profilePath);
  return profilePath;
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误