Skip to content

Commit c59ed91

Browse files
bartvenemanBart Veneman
andauthored
Upgrade deps (projectwallace#38)
* upgrade all deps, convert to vercels new api structure * Upgrade dependencies - chrome-aws-lambda: 8.0.2 -> 10.1.0 - normalize-url: 5.3.1 -> 7.0.2 - puppeteer-core: 8.0.0 -> 10.4.0 Removed dependency: - is-url: replaced with local alternative that doesn't allow localhost * linting * allow 1000 items in cache * choose different vercel alias Co-authored-by: Bart Veneman <bart.veneman@drukwerkdeal.nl>
1 parent 22d1c2d commit c59ed91

File tree

7 files changed

+336
-209
lines changed

7 files changed

+336
-209
lines changed

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
MIT LICENSE
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

api/_chromium.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* global document */
22

3-
const puppeteer = require('puppeteer-core')
4-
const chrome = require('chrome-aws-lambda')
5-
const normalizeUrl = require('normalize-url')
3+
import puppeteer from 'puppeteer-core'
4+
import chrome from 'chrome-aws-lambda'
5+
import normalizeUrl from 'normalize-url'
66

7-
exports.extractCss = async url => {
7+
export const extractCss = async url => {
88
const browser = await puppeteer.launch({
99
args: chrome.args,
1010
executablePath: await chrome.executablePath,
@@ -17,7 +17,7 @@ exports.extractCss = async url => {
1717
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:85.0) Gecko/20100101 Firefox/85.0')
1818

1919
// Start CSS coverage. This is the meat and bones of this module
20-
await page.coverage.startCSSCoverage().catch(() => {})
20+
await page.coverage.startCSSCoverage().catch(() => { })
2121

2222
url = normalizeUrl(url, {stripWWW: false})
2323
const response = await page.goto(url, {waitUntil: 'networkidle0'})

api/_is-url.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Source: https://github.com/segmentio/is-url/blob/d20482833e861b9a0c5bb3712dfe232efa75c87c/index.js
2+
// but without checking for localhost
3+
var protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/
4+
var nonLocalhostDomainRE = /^[^\s.]+\.\S{2,}$/
5+
6+
export function isUrl(string) {
7+
if (typeof string !== 'string') {
8+
return false
9+
}
10+
11+
var match = string.match(protocolAndDomainRE)
12+
if (!match) {
13+
return false
14+
}
15+
16+
var everythingAfterProtocol = match[1]
17+
if (!everythingAfterProtocol) {
18+
return false
19+
}
20+
21+
if (nonLocalhostDomainRE.test(everythingAfterProtocol)) {
22+
return true
23+
}
24+
25+
return false
26+
}

api/extract-css.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const isUrl = require('is-url')
2-
const LRU = require('lru-cache')
3-
const { extractCss } = require('./_chromium')
1+
import LRU from 'lru-cache'
2+
import {extractCss} from './_chromium.js'
3+
import {isUrl} from './_is-url.js'
44

55
const cssCache = new LRU({
6-
max: 500,
6+
max: 1000,
77
maxAge: 60 * 1000 // 60 seconds
88
})
99

10-
module.exports = async (req, res) => {
11-
const { url } = req.query
10+
export default async (req, res) => {
11+
const {url} = req.query
1212

1313
if (!isUrl(url)) {
1414
res.statusCode = 400
@@ -28,7 +28,7 @@ module.exports = async (req, res) => {
2828
}
2929

3030
res.setHeader('Content-Type', 'text/css')
31-
const css = result.map(({ css }) => css).join('\n')
31+
const css = result.map(({css}) => css).join('\n')
3232
return res.end(css)
3333
}
3434

@@ -41,10 +41,10 @@ module.exports = async (req, res) => {
4141
}
4242

4343
res.setHeader('Content-Type', 'text/css')
44-
const css = result.map(({ css }) => css).join('\n')
44+
const css = result.map(({css}) => css).join('\n')
4545
return res.end(css)
4646
} catch (error) {
4747
res.statusCode = 500
48-
return res.json({ message: error.message })
48+
return res.json({message: error.message})
4949
}
5050
}

0 commit comments

Comments
 (0)