审查生成的受限路径改动

来自 约束明确的提示词
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;
}

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

在试验场中打开
报告错误