Skip to content

Commit

Permalink
代码格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
Binaryify committed Sep 12, 2020
1 parent 0ae2628 commit 4f7266e
Show file tree
Hide file tree
Showing 146 changed files with 603 additions and 449 deletions.
96 changes: 57 additions & 39 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ const packageJSON = require('./package.json')
const exec = require('child_process').exec
const cache = require('./util/apicache').middleware
const { cookieToJson } = require('./util/index')
const fileUpload = require('express-fileupload');
const fileUpload = require('express-fileupload')
// version check
exec('npm info NeteaseCloudMusicApi version', (err, stdout, stderr) => {
if(!err){
if (!err) {
let version = stdout.trim()
if(packageJSON.version < version){
console.log(`最新版本: ${version}, 当前版本: ${packageJSON.version}, 请及时更新`)
if (packageJSON.version < version) {
console.log(
`最新版本: ${version}, 当前版本: ${packageJSON.version}, 请及时更新`,
)
}
}
})
Expand All @@ -22,73 +24,89 @@ const app = express()

// CORS & Preflight request
app.use((req, res, next) => {
if(req.path !== '/' && !req.path.includes('.')){
if (req.path !== '/' && !req.path.includes('.')) {
res.set({
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': req.headers.origin || '*',
'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
'Content-Type': 'application/json; charset=utf-8'
'Content-Type': 'application/json; charset=utf-8',
})
}
req.method === 'OPTIONS' ? res.status(204).end() : next()
})

// cookie parser
app.use((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()
})
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()
})

// body parser
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

app.use(fileUpload());

app.use(bodyParser.urlencoded({ extended: false }))

app.use(fileUpload())

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

// cache
app.use(cache('2 minutes', ((req, res) => res.statusCode === 200)))
app.use(cache('2 minutes', (req, res) => res.statusCode === 200))
// router
const special = {
'daily_signin.js': '/daily_signin',
'fm_trash.js': '/fm_trash',
'personal_fm.js': '/personal_fm'
'personal_fm.js': '/personal_fm',
}

fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => {
if(!file.endsWith('.js')) return
let route = (file in special) ? special[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/')
let question = require(path.join(__dirname, 'module', file))
fs.readdirSync(path.join(__dirname, 'module'))
.reverse()
.forEach((file) => {
if (!file.endsWith('.js')) return
let route =
file in special
? special[file]
: '/' + file.replace(/\.js$/i, '').replace(/_/g, '/')
let question = require(path.join(__dirname, 'module', file))

app.use(route, (req, res) => {
if(typeof req.query.cookie === 'string'){
req.query.cookie = cookieToJson(req.query.cookie)
}
let query = Object.assign({}, {cookie: req.cookies}, req.query, req.body, req.files )
app.use(route, (req, res) => {
if (typeof req.query.cookie === 'string') {
req.query.cookie = cookieToJson(req.query.cookie)
}
let query = Object.assign(
{},
{ cookie: req.cookies },
req.query,
req.body,
req.files,
)

question(query, request)
.then(answer => {
console.log('[OK]', decodeURIComponent(req.originalUrl))
res.append('Set-Cookie', answer.cookie)
res.status(answer.status).send(answer.body)
})
.catch(answer => {
console.log('[ERR]', decodeURIComponent(req.originalUrl), {status: answer.status, body: answer.body})
if(answer.body.code == '301') answer.body.msg = '需要登录'
res.append('Set-Cookie', answer.cookie)
res.status(answer.status).send(answer.body)
})
question(query, request)
.then((answer) => {
console.log('[OK]', decodeURIComponent(req.originalUrl))
res.append('Set-Cookie', answer.cookie)
res.status(answer.status).send(answer.body)
})
.catch((answer) => {
console.log('[ERR]', decodeURIComponent(req.originalUrl), {
status: answer.status,
body: answer.body,
})
if (answer.body.code == '301') answer.body.msg = '需要登录'
res.append('Set-Cookie', answer.cookie)
res.status(answer.status).send(answer.body)
})
})
})
})

const port = process.env.PORT || 3000
const host = process.env.HOST || ''
Expand Down
7 changes: 3 additions & 4 deletions app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ after((done) => {
app.server.close(done)
})

fs.readdirSync(path.join(__dirname, 'test'))
.forEach(file => {
require(path.join(__dirname, 'test', file))
})
fs.readdirSync(path.join(__dirname, 'test')).forEach((file) => {
require(path.join(__dirname, 'test', file))
})
25 changes: 16 additions & 9 deletions docs/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const HOSTNAME_WHITELIST = [
self.location.hostname,
'fonts.gstatic.com',
'fonts.googleapis.com',
'unpkg.com'
'unpkg.com',
]

// The Util Function to hack URLs of intercepted requests
Expand Down Expand Up @@ -42,7 +42,7 @@ const getFixedUrl = (req) => {
*
* waitUntil(): activating ====> activated
*/
self.addEventListener('activate', event => {
self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim())
})

Expand All @@ -52,7 +52,7 @@ self.addEventListener('activate', event => {
*
* void respondWith(Promise<Response> r)
*/
self.addEventListener('fetch', event => {
self.addEventListener('fetch', (event) => {
// Skip some of cross-origin requests, like those for Google Analytics.
if (HOSTNAME_WHITELIST.indexOf(new URL(event.request.url).hostname) > -1) {
// Stale-while-revalidate
Expand All @@ -61,23 +61,30 @@ self.addEventListener('fetch', event => {
const cached = caches.match(event.request)
const fixedUrl = getFixedUrl(event.request)
const fetched = fetch(fixedUrl, { cache: 'no-store' })
const fetchedCopy = fetched.then(resp => resp.clone())
const fetchedCopy = fetched.then((resp) => resp.clone())

// Call respondWith() with whatever we get first.
// If the fetch fails (e.g disconnected), wait for the cache.
// If there’s nothing in cache, wait for the fetch.
// If neither yields a response, return offline pages.
event.respondWith(
Promise.race([fetched.catch(_ => cached), cached])
.then(resp => resp || fetched)
.catch(_ => { /* eat any errors */ })
Promise.race([fetched.catch((_) => cached), cached])
.then((resp) => resp || fetched)
.catch((_) => {
/* eat any errors */
}),
)

// Update the cache with the version we fetched (only for ok status)
event.waitUntil(
Promise.all([fetchedCopy, caches.open(RUNTIME)])
.then(([response, cache]) => response.ok && cache.put(event.request, response))
.catch(_ => { /* eat any errors */ })
.then(
([response, cache]) =>
response.ok && cache.put(event.request, response),
)
.catch((_) => {
/* eat any errors */
}),
)
}
})
32 changes: 18 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ const path = require('path')
const request = require('./util/request')
const { cookieToJson } = require('./util/index')


let obj = {}
fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => {
if(!file.endsWith('.js')) return
let fileModule = require(path.join(__dirname, 'module', file))
obj[file.split('.').shift()] = function (data) {
if(typeof data.cookie === 'string'){
data.cookie = cookieToJson(data.cookie)
fs.readdirSync(path.join(__dirname, 'module'))
.reverse()
.forEach((file) => {
if (!file.endsWith('.js')) return
let fileModule = require(path.join(__dirname, 'module', file))
obj[file.split('.').shift()] = function (data) {
if (typeof data.cookie === 'string') {
data.cookie = cookieToJson(data.cookie)
}
return fileModule(
{
...data,
cookie: data.cookie ? data.cookie : {},
},
request,
)
}
return fileModule({
...data,
cookie: data.cookie ? data.cookie : {}
}, request)
}
})
})

module.exports = obj
module.exports = obj
2 changes: 1 addition & 1 deletion module/album.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/album_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/album_detail_dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/album_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/album_list_style.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/album_newest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/album_songsaleboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/artist_album.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/artist_desc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/artist_sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/artists.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/avatar_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = async (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
return {
status: 200,
Expand Down
2 changes: 1 addition & 1 deletion module/banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = (query, request) => {
'POST',
`https://music.163.com/api/v2/banner/get`,
{ clientType: type },
{ crypto: 'linuxapi', proxy: query.proxy, realIP: query.realIP }
{ crypto: 'linuxapi', proxy: query.proxy, realIP: query.realIP },
)
}
2 changes: 1 addition & 1 deletion module/captcha_verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/cellphone_existence_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module.exports = (query, request) => {
proxy: query.proxy,
url: '/api/cellphone/existence/check',
realIP: query.realIP,
}
},
)
}
2 changes: 1 addition & 1 deletion module/check_music.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = (query, request) => {
cookie: query.cookie,
proxy: query.proxy,
realIP: query.realIP,
}
},
).then((response) => {
let playable = false
if (response.body.code == 200) {
Expand Down
Loading

0 comments on commit 4f7266e

Please sign in to comment.