<commit_before>const express = require('express')
const apicache = require('apicache')
const path = require('path')
const fs = require('fs')
const app = express()
let cache = apicache.middleware
const { exec } = require('child_process');
exec('npm info NeteaseCloudMusicApi version', (err, stdout, stderr) => {
  if (err) {
    console.error(err);
    return;
  }
  const onlinePackageVersion = stdout.trim();
  const package = require('./package.json')
  if (package.version < onlinePackageVersion) {
    console.log(
      '最新版:Version:' +
      onlinePackageVersion +
        ',当前版本:' +
        package.version +
        ',请及时更新'
    )
  }
})

// 跨域设置
app.all('*', function(req, res, next) {
  if (req.path !== '/' && !req.path.includes('.')) {
    res.header('Access-Control-Allow-Credentials', true)
    // 这里获取 origin 请求头 而不是用 *
    res.header('Access-Control-Allow-Origin', req.headers['origin'] || '*')
    res.header('Access-Control-Allow-Headers', 'X-Requested-With')
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
    res.header('Content-Type', 'application/json;charset=utf-8')
  }
  next()
})

const onlyStatus200 = (req, res) => res.statusCode === 200

app.use(cache('2 minutes', onlyStatus200))

app.use(express.static(path.resolve(__dirname, 'public')))

// 补全缺失的cookie
const { completeCookie } = require('./util/init')
app.use(function(req, res, next) {
  let cookie = completeCookie(req.headers.cookie)
  req.headers.cookie = cookie.map(x => x[0]).concat(req.headers.cookie || []).join('; ')
  res.append('Set-Cookie', cookie.map(x => (x.concat('Path=/').join('; '))))
  next()
})

// cookie parser
app.use(function(req, res, next) {
  req.cookies = {}, (req.headers.cookie || '').split(/\s*;\s*/).forEach(pair => {
    let crack = pair.indexOf('=')
    if(crack < 1 || crack == pair.length - 1) return
    req.cookies[decodeURIComponent(pair.slice(0, crack)).trim()] = decodeURIComponent(pair.slice(crack + 1)).trim()
  })
  next()
})

app.use(function(req, res, next) {
  const proxy = req.query.proxy
  if (proxy) {
    req.headers.cookie += `__proxy__${proxy}`
  }
  next()
})

// 因为这几个文件对外所注册的路由 和 其他文件对外注册的路由规则不一样, 所以专门写个MAP对这些文件做特殊处理
const UnusualRouteFileMap = {
  // key 为文件名, value 为对外注册的路由
  'daily_signin.js': '/daily_signin',
  'fm_trash.js': '/fm_trash',
  'personal_fm.js': '/personal_fm'
}

// 简化 路由 导出方式, 由这里统一对 router 目录中导出的路由做包装, 路由实际对应的文件只专注做它该做的事情, 不用重复写样板代码
const { createWebAPIRequest, request } = require('./util/util')
const Wrap = fn => (req, res) => fn(req, res, createWebAPIRequest, request)

// 同步读取 router 目录中的js文件, 根据命名规则, 自动注册路由
fs.readdirSync(path.join(__dirname, 'router'))
.reverse()
.forEach(file => {
  if (!/\.js$/i.test(file)) return
  let route = (file in UnusualRouteFileMap) ?  UnusualRouteFileMap[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/')
  app.use(route, Wrap(path.join(__dirname, 'router', file)))
})


// 改写router为module
const requestMod = require('./util/request')
let dev = express()
fs.readdirSync(path.join(__dirname, 'module'))
.reverse()
.forEach(file => {
  if (!(/\.js$/i.test(file))) return
  let route = (file in UnusualRouteFileMap) ?  UnusualRouteFileMap[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/')
  let question = require(path.join(__dirname, 'module', file))
  <commit_msg>error fixed<commit_after>const express = require('express')
const apicache = require('apicache')
const path = require('path')
const fs = require('fs')
const app = express()
let cache = apicache.middleware
const { exec } = require('child_process');
exec('npm info NeteaseCloudMusicApi version', (err, stdout, stderr) => {
  if (err) {
    console.error(err);
    return;
  }
  const onlinePackageVersion = stdout.trim();
  const package = require('./package.json')
  if (package.version < onlinePackageVersion) {
    console.log(
      '最新版:Version:' +
      onlinePackageVersion +
        ',当前版本:' +
        package.version +
        ',请及时更新'
    )
  }
})

// 跨域设置
app.all('*', function(req, res, next) {
  if (req.path !== '/' && !req.path.includes('.')) {
    res.header('Access-Control-Allow-Credentials', true)
    // 这里获取 origin 请求头 而不是用 *
    res.header('Access-Control-Allow-Origin', req.headers['origin'] || '*')
    res.header('Access-Control-Allow-Headers', 'X-Requested-With')
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
    res.header('Content-Type', 'application/json;charset=utf-8')
  }
  next()
})

const onlyStatus200 = (req, res) => res.statusCode === 200

app.use(cache('2 minutes', onlyStatus200))

app.use(express.static(path.resolve(__dirname, 'public')))

// 补全缺失的cookie
const { completeCookie } = require('./util/init')
app.use(function(req, res, next) {
  let cookie = completeCookie(req.headers.cookie)
  req.headers.cookie = cookie.map(x => x[0]).concat(req.headers.cookie || []).join('; ')
  res.append('Set-Cookie', cookie.map(x => (x.concat('Path=/').join('; '))))
  next()
})

// cookie parser
app.use(function(req, res, next) {
  req.cookies = {}, (req.headers.cookie || '').split(/\s*;\s*/).forEach(pair => {
    let crack = pair.indexOf('=')
    if(crack < 1 || crack == pair.length - 1) return
    req.cookies[decodeURIComponent(pair.slice(0, crack)).trim()] = decodeURIComponent(pair.slice(crack + 1)).trim()
  })
  next()
})

app.use(function(req, res, next) {
  const proxy = req.query.proxy
  if (proxy) {
    req.headers.cookie += `__proxy__${proxy}`
  }
  next()
})

// 因为这几个文件对外所注册的路由 和 其他文件对外注册的路由规则不一样, 所以专门写个MAP对这些文件做特殊处理
const UnusualRouteFileMap = {
  // key 为文件名, value 为对外注册的路由
  'daily_signin.js': '/daily_signin',
  'fm_trash.js': '/fm_trash',
  'personal_fm.js': '/personal_fm'
}

// 简化 路由 导出方式, 由这里统一对 router 目录中导出的路由做包装, 路由实际对应的文件只专注做它该做的事情, 不用重复写样板代码
const { createWebAPIRequest, request } = require('./util/util')
const Wrap = fn => (req, res) => fn(req, res, createWebAPIRequest, request)

// 同步读取 router 目录中的js文件, 根据命名规则, 自动注册路由
fs.readdirSync(path.join(__dirname, 'router'))
.reverse()
.forEach(file => {
  if (!/\.js$/i.test(file)) return
  let route = (file in UnusualRouteFileMap) ?  UnusualRouteFileMap[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/')
  app.use(route, Wrap(require(path.join(__dirname, 'router', file))))
})


// 改写router为module
const requestMod = require('./util/request')
let dev = express()
fs.readdirSync(path.join(__dirname, 'module'))
.reverse()
.forEach(file => {
  if (!(/\.js$/i.test(file))) return
  let route = (file in UnusualRouteFileMap) ?  UnusualRouteFileMap[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/')
  let question = require(path.join(__dirname, 'module', file))
  
{'fraction_non_alphanumeric': 0.19515133263062792, 'fraction_numerical': 0.004818551422978467, 'mean_word_length': 4.085758039816233, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4644063', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2633, 'n_words': 441}