Skip to content

Commit

Permalink
upgrade lint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
arianrhodsandlot committed Jul 29, 2019
1 parent e82e1e6 commit dc24183
Show file tree
Hide file tree
Showing 9 changed files with 709 additions and 74 deletions.
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
"name": "holly-quintet",
"version": "3.0.0",
"private": true,
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/arianrhodsandlot/Holly-Quintet.git"
},
"scripts": {
"start": "cross-env NODE_ENV=production NODE_OPTIONS=\"-r esm\" ts-node src/server",
"dev": "cross-env NODE_OPTIONS=\"-r esm\" ts-node src/server",
"test": "cross-env NODE_OPTIONS=\"-r esm\" ava"
"test": "eslint src --ext=ts,js && cross-env NODE_OPTIONS=\"-r esm\" ava"
},
"dependencies": {
"chowdown": "^1.2.3",
Expand Down Expand Up @@ -47,13 +48,20 @@
"@types/serve-favicon": "^2.2.30",
"ava": "^2.2.0",
"cross-env": "^5.2.0",
"eslint-config-argreion": "gist:86724154ca49ee8b3f9aebae3fe44e0a",
"puppeteer": "^1.19.0",
"socks5-https-client": "^1.2.1"
},
"config": {
"port": 1025
},
"engines": {
"node": ">=8.3.0"
},
"browserslist": "last 2 years",
"eslintConfig": {
"extends": "argreion"
},
"ava": {
"compileEnhancements": false,
"extensions": [
Expand Down
39 changes: 20 additions & 19 deletions src/assets/javascripts/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-nocheck
/* global Qs, page, ga, imagesLoaded, _, mdc, Cookies */
let request
const { Qs, page, ga, imagesLoaded, _, mdc, Cookies } = window
const $body = $('body')
const $form = $('form')
const $query = $form.find('.query')
Expand All @@ -15,7 +16,8 @@ function updateBg (src) {

const $bg = $('.bg')

const $newBg = $('.bg').first().clone()
const $newBg = $('.bg').first()
.clone()
Cookies('bg', src, { expires: veryLateDate })
$newBg.css({
'background-image': `url(${src})`,
Expand All @@ -33,11 +35,11 @@ function updateBg (src) {
}, 50)
}

$query.on('input', _.debounce(function () {
$query.on('input', _.debounce(() => {
$form.submit()
}, 500))

$form.submit(async function (e) {
$form.submit((e) => {
e.preventDefault()

const query = $query.val().trim()
Expand All @@ -53,35 +55,34 @@ $form.submit(async function (e) {
page(url)
})

$chips.on('click', '.mdc-chip', function () {
const index = $(this).index()
const { chips } = this.parentElement.MDCChipSet
$chips.on('click', '.mdc-chip', (e) => {
const index = $(e.currentTarget).index()
const { chips } = e.currentTarget.parentElement.MDCChipSet
const targetChip = chips[index]

chips.forEach((chip) => {
window.chip = chip
chip.foundation_.setSelected(chip === targetChip)
})

const site = targetChip.root_.dataset.site
const { site } = targetChip.root_.dataset
Cookies('site', site, { expires: veryLateDate })
const parsed = Qs.parse(location.search.slice(1))
parsed.site = site
page.replace('/search?' + Qs.stringify(parsed))
page.replace(`/search?${Qs.stringify(parsed)}`)
})

$albums.on('click', '.download-mask', function () {
const $container = $(this).closest('.album-container')
$albums.on('click', '.download-mask', (e) => {
const $container = $(e.currentTarget).closest('.album-container')
const src = $container.find('img.album-img').attr('src')
updateBg(src)
})

let infoDialog
$info.click(function () {
$info.click(() => {
if (!infoDialog) {
const $infoDialog = $('.info-dialog')
$infoDialog.find('img[data-src]').each(function () {
const $img = $(this)
$infoDialog.find('img[data-src]').each((el) => {
const $img = $(el)
$img.attr('src', $img.data('src'))
})
infoDialog = $infoDialog.get(0).MDCDialog
Expand All @@ -90,7 +91,7 @@ $info.click(function () {
infoDialog.open()
})

$body.keypress(function (e) {
$body.keypress((e) => {
if (e.target === $query.get(0)) return

if (e.which === 47) {
Expand All @@ -103,7 +104,7 @@ setTimeout(() => {
$('.bg').addClass('loaded')
}, 3000)

page(function (ctx, next) {
page((ctx, next) => {
if (window.ga) ga('send', 'pageview')

if (request) request.abort()
Expand All @@ -116,14 +117,14 @@ page(function (ctx, next) {
}
})

page('/', function (ctx) {
page('/', () => {
$body.attr('class', 'page-index')
$query.focus().val('')
document.title = 'Holly Quintet'
$albums.empty()
})

page('/search', async function (ctx) {
page('/search', async (ctx) => {
$body.attr('class', 'page-search')
const { query } = Qs.parse(ctx.querystring)
$query.focus().val(query)
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import express from 'express'
import path from 'path'
import url from 'url'
import express from 'express'
import cookieParser from 'cookie-parser'
import helmet from 'helmet'
import compression from 'compression'
import favicon from 'serve-favicon'
import router from './router'
import bundler from './bundler'

const viewsDir = path.join(__dirname, 'views')
const dirname = path.dirname(url.fileURLToPath(import.meta.url))

const viewsDir = path.join(dirname, 'views')

export default express()
.enable('trust proxy')
Expand All @@ -16,6 +19,6 @@ export default express()
.use(helmet())
.use(compression())
.use(cookieParser())
.use(favicon(path.join(__dirname, 'assets/images/favicon.ico')))
.use(favicon(path.join(dirname, 'assets/images/favicon.ico')))
.use(bundler.middleware())
.use('/', router)
37 changes: 20 additions & 17 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import _ from 'lodash'
import express from 'express'
import url from 'url'
import path from 'path'
import _ from 'lodash'
import express from 'express'
import request from 'request'
import logger from 'morgan'
import Searcher from './searcher'
import sites from './consts/sites'
import {getCoverOriginSrc, getCoverDownloadSrc, getJsdelivrCombinedLink} from './util'
import { getCoverOriginSrc, getCoverDownloadSrc, getJsdelivrCombinedLink } from './util'

const router = express.Router()
const defaultBg = '/images/default.jpg'

const veryLateDate = new Date(253402300000000)

// eslint-disable-next-line node/no-deprecated-api
const filePath = url.parse(import.meta.url).pathname!
const workingDir = path.parse(filePath).dir

Expand All @@ -22,9 +23,9 @@ const jsLink = getJsLink()
router
.use(defaultBg, express.static(path.join(workingDir, 'assets/images/default.jpg')))
.use(logger('combined'))
.use(function (req, res, next) {
let site = req.cookies.site || sites[0].site
res.cookie('site', site, {expires: veryLateDate})
.use((req, res, next) => {
const site = req.cookies.site || sites[0].site
res.cookie('site', site, { expires: veryLateDate })

const bg = req.cookies.bg || defaultBg

Expand All @@ -34,7 +35,7 @@ router
next()
})

.get('/', function (req, res) {
.get('/', (req, res) => {
Object.assign(res.locals, {
pageName: 'index',
query: '',
Expand All @@ -44,14 +45,15 @@ router
res.render('index')
})

.get('/search', async function (req, res, next) {
let {query = '', site = ''} = req.query
.get('/search', async (req, res, next) => {
const { query = '', site = '' } = req.query

if (!query) {
next()
return
}

// eslint-disable-next-line node/no-deprecated-api
const parsed = url.parse(req.path, true)
const trimmedQuery = query.trim()

Expand All @@ -63,7 +65,8 @@ router
return
}

const isValidSite = _(sites).map('site').includes(site)
const isValidSite = _(sites).map('site')
.includes(site)
if (!isValidSite) {
delete parsed.search
parsed.query.query = trimmedQuery
Expand All @@ -73,40 +76,40 @@ router
return
}

res.cookie('site', site, {expires: veryLateDate})
res.cookie('site', site, { expires: veryLateDate })

const albums = await Searcher.search(site, trimmedQuery)
const bg = _.get(albums, '0.ou')
if (bg) {
res.cookie('bg', bg, {expires: veryLateDate})
res.cookie('bg', bg, { expires: veryLateDate })
res.locals.bg = bg
}
Object.assign(res.locals, {
site: site,
site,
pageName: 'search',
query: trimmedQuery,
albums: albums,
albums,
title: `${trimmedQuery} - Holly Quintet`
})

res.render(req.xhr ? 'albums' : 'index')
})

.get('/file', async function (req, res, next) {
.get('/file', (req, res, next) => {
if (!req.query.url) {
next()
return
}

request(req.query.url)
.on('response', function (remoteRes) {
.on('response', (remoteRes) => {
delete remoteRes.headers['content-disposition']
res.attachment(req.query.filename)
})
.pipe(res)
})

.get('*', function (req, res) {
.get('*', (req, res) => {
res.redirect('/')
})

Expand Down
12 changes: 7 additions & 5 deletions src/searcher.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import LRU from 'lru-cache'
import request from 'request'
import chowdown from 'chowdown'
// eslint-disable-next-line node/no-unpublished-import
import Agent from 'socks5-https-client/lib/Agent'

const cache = new LRU({max: 500})
const baseRequestOptions: request.Options = {
const cache = new LRU({ max: 500 })
const baseRequestOptions: request.Options = {
baseUrl: 'https://www.google.com',
uri: '/search',
qs: {
hl: 'zh-CN',
tbm: 'isch',
gws_rd: 'cr' // get rid of our request being redirected by country
// get rid of our request being redirected by country
gws_rd: 'cr'
},
timeout: 3000,
headers: {
Expand All @@ -23,7 +25,7 @@ if (process.env.NODE_ENV !== 'production') {

export default class Searcher {
static getCacheKey (site: string, query: string) {
return JSON.stringify({site, query})
return JSON.stringify({ site, query })
}

static async searchRemote (site: string, query: string) {
Expand All @@ -35,7 +37,7 @@ export default class Searcher {
}
}

let html: string[] = await chowdown(requestOptions).collection('.rg_el .rg_meta', chowdown.query.string())
const html: string[] = await chowdown(requestOptions).collection('.rg_el .rg_meta', chowdown.query.string())
const albums: {ou: string; pt: string; ru: string}[] = html.map((a) => JSON.parse(a))

const cacheKey = Searcher.getCacheKey(site, query)
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function launchServer () {
server.listen(port)
.on('listening', () => {
const addr = server.address()!
const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`
const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`
console.log(`Listening on ${bind}`)
})
}
Expand Down
13 changes: 7 additions & 6 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/* eslint-disable node/no-deprecated-api */
import url from 'url'
import path from 'path'

function getCertainSizeSrcFromItunes (src: string, width = 10000, height = width) {
let parsed = url.parse(src, false)
let parsedPath = path.parse(parsed.pathname || '')
parsedPath.name = parsedPath.name.replace(/\d+x\d+/, `${width}x${height}`)
const parsed = url.parse(src, false)
const parsedPath = path.parse(parsed.pathname || '')
parsedPath.name = parsedPath.name.replace(/\d+x\d+/u, `${width}x${height}`)
parsedPath.base = `${parsedPath.name}${parsedPath.ext}`
parsed.pathname = path.format(parsedPath)
return url.format(parsed)
}

function getCoverOriginSrcFrom163 (src: string) {
let parsed = url.parse(src, false)
const parsed = url.parse(src, false)
delete parsed.search
return url.format(parsed)
}

function getCoverOriginSrcFromVgm (src: string) {
let parsed = url.parse(src, false)
const parsed = url.parse(src, false)
parsed.host = 'media.vgm.io'
return url.format(parsed)
}
Expand Down Expand Up @@ -63,7 +64,7 @@ export function getJsdelivrCombinedLink (packages: {name?: string; version?: str

pathname = url.resolve('/combine/', pathname)

let link = {
const link = {
protocol: 'https:',
hostname: 'cdn.jsdelivr.net',
pathname
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": false,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"lib": ["esnext"],
Expand Down
Loading

0 comments on commit dc24183

Please sign in to comment.