Merge branch 'feature-use-firebase-to-host-instance' into 'master'

Feature use firebase to host instance

See merge request ondrejsynacek/facebook-events-ical-converter!1
This commit is contained in:
Ondřej Synáček 2020-11-15 15:02:56 +00:00
commit f32b528979
20 changed files with 2947 additions and 116 deletions

View File

@ -0,0 +1,10 @@
index.html,1605449671760,a31aa6a1f05bfa031059011d8765dfbc7852c44f33c9cffaa7b8d387f25cf2e9
manifest.json,1605449671760,f9f961e572d8185f8193c231ce9387026d500a021e88cabce702f4a4b5ddca62
icon-180.png,1605449671760,92b3a619d6458a45d8a0c0fe1cc30d00d2b8667f7c7779a1ee4047743ad878c4
style.css,1605449671760,585b07896f1e83b449fc299fe3768854d25e0159ef7d1cc79029d012f234d2e9
sw.js,1605449671760,08681b87b2cdf731a624709260ca8eae53d8f4464d15f2c34f3a4e280ebb6fbb
icon-192.png,1605449671760,019b6801c3ed961b26e089a9834c76036841020c6e1b32dad09b99011a46fd4f
workbox-468c4d03.js,1605449671760,ea5fb6b5fef5cc7f273684fd2e59ac0cebd89aa17ee65eb3025aae71ad25caf5
favicon.ico,1605449671760,8bd2763f4080b268398c699ed305c33029429fb7c2f01f7fc8a2526a76e9beab
icon-512.png,1605449671760,5546a701cef8feed3551ec81d90e96227387f5b2f5c7170edf54a9f45c26273f
main.c9ec83fdf251cd746aa8.js,1605449671760,a2456d44a068040e607f7aee0fdfe316a1d699b24835393ce5ae75711bf05020

5
.firebaserc Normal file
View File

@ -0,0 +1,5 @@
{
"projects": {
"default": "fb2ical-3051b"
}
}

25
firebase.json Normal file
View File

@ -0,0 +1,25 @@
{
"emulators": {
"functions": {
"port": 5001
},
"ui": {
"enabled": true,
"port": 5002
}
},
"hosting": {
"public": "dist/",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/**",
"function": "app"
}
]
}
}

1
functions/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

29
functions/index.js Normal file
View File

@ -0,0 +1,29 @@
const functions = require('firebase-functions');
const admin = require('firebase-admin')
const { configureApplication } = require('./lib/app')
// const { createAppLogger } = require('./lib/log-utils')
// const {
// createRouteLogger,
// createErrorLogger,
// } = require('./lib/middlewares')
admin.initializeApp()
const isDevelopment = process.env.NODE_ENV === 'development'
// const appLogger = createAppLogger({ dev: isDevelopment })
// const errorLogger = createErrorLogger({ dev: isDevelopment })
// const routeLogger = isDevelopment ? createRouteLogger({ dev: isDevelopment }) : null
const corsOptions = isDevelopment ? {
origin: 'http://localhost:5000',
} : null
const app = configureApplication({
rateLimitEnabled: false,
corsOptions,
})
exports.app = functions.https.onRequest(app)

1
functions/lib Symbolic link
View File

@ -0,0 +1 @@
../lib

2576
functions/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

36
functions/package.json Normal file
View File

@ -0,0 +1,36 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "12"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0",
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.3",
"cors": "^2.8.5",
"dayjs": "^1.8.16",
"ejs": "^2.7.1",
"express": "^4.17.1",
"express-rate-limit": "^5.0.0",
"express-winston": "^4.0.1",
"ics": "^2.22.1",
"request": "^2.88.0",
"serve-favicon": "^2.5.0",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^4.2.1"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}

104
lib/app/index.js Normal file
View File

@ -0,0 +1,104 @@
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const favicon = require('serve-favicon')
const rateLimit = require('express-rate-limit')
const cors = require('cors')
const {
error,
notFound,
download,
downloadHTML,
} = require('../routes')
const {
genericErrorHandler,
checkURLParameter,
forceSecure,
} = require('../middlewares')
const certEndpoint = process.env.CERT_ENDPOINT || ''
const certSecret = process.env.CERT_SECRET || ''
const enforceHTTPS = Boolean(process.env.ENFORCE_HTTPS)
const configureApplication = ({
appLogger,
errorLogger,
routeLogger,
corsOptions,
rateLimitEnabled,
}) => {
const pkg = require('../../package.json')
const version = pkg.version
const app = express()
if (corsOptions) {
app.use(cors(corsOptions))
}
// Force app to always redirect to HTTPS
// use when you can't configure web server
if (enforceHTTPS) {
app.use(forceSecure)
}
// Server logs You can alternatively enable these to mimic logs created
// by your web server
if (routeLogger) {
app.use(routeLogger)
}
if (errorLogger) {
app.use(errorLogger)
}
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, '..', 'views'))
app.set('trust proxy', 1)
app.use(express.static(path.join(__dirname, '..', '..', 'dist')))
// app.use(favicon(path.join(__dirname, '..', 'dist', 'favicon.ico')))
app.use(bodyParser.urlencoded({ extended: true }))
if (rateLimitEnabled) {
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 10,
})
app.use('/download/html', limiter)
app.use('/download', limiter)
}
if (certEndpoint) {
app.get(`/${certEndpoint}`, (req, res) => {
res.status(200).send(certSecret)
})
}
app.use('/download/html', checkURLParameter)
app.use('/download', checkURLParameter)
app.get('/error', error)
app.get('/about', (req, res) => {
res.render('about', { version })
})
// NOTE: Capture all unkown URLs
app.get('*', notFound)
app.post('/download/html', downloadHTML(appLogger || null))
app.post('/download', download(appLogger || null))
app.use(genericErrorHandler)
return app
}
module.exports = {
configureApplication,
}

View File

@ -1,121 +1,22 @@
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const favicon = require('serve-favicon')
const rateLimit = require('express-rate-limit')
const { retrieveICS } = require('./services/ics-retriever')
const crawl = require('./services/crawler')
const { configureApplication } = require('./app')
const { createAppLogger } = require('./log-utils')
const {
genericErrorHandler,
checkURLParameter,
forceSecure,
createRouteLogger,
createErrorLogger,
} = require('./middlewares')
const { createAppLogger } = require('./log-utils')
const { getNormalizedUrl } = require('./utils')
const port = process.env.PORT
const certEndpoint = process.env.CERT_ENDPOINT || ''
const certSecret = process.env.CERT_SECRET || ''
const isDevelopment = process.env.NODE_ENV === 'development'
const enforceHTTPS = Boolean(process.env.ENFORCE_HTTPS)
const port = process.env.PORT
const app = express()
const appLogger = createAppLogger({ dev: isDevelopment })
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 10,
const errorLogger = createErrorLogger({ dev: isDevelopment })
const routeLogger = isDevelopment ? createRouteLogger({ dev: isDevelopment }) : null
const app = configureApplication({
appLogger,
errorLogger,
routeLogger,
rateLimitEnabled: true
})
const pkg = require('../package.json')
const version = pkg.version || ''
// Force app to always redirect to HTTPS
// use when you can't configure web server
if (enforceHTTPS) {
app.use(forceSecure)
}
// Server logs
// You can alternatively enable these to mimic logs created
// by your web server
if (isDevelopment) {
app.use(createRouteLogger({ dev: isDevelopment }))
}
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
app.set('trust proxy', 1)
app.use(express.static(path.join(__dirname, '..', 'dist')))
//app.use(favicon(path.join(__dirname, '..', 'dist', 'favicon.ico')))
app.use(bodyParser.urlencoded({ extended: true }))
const indexFile = path.join(__dirname, '..', 'dist', 'index.html')
if (certEndpoint) {
app.get(`/${certEndpoint}`, (req, res) => {
res.status(200).send(certSecret)
})
}
app.get('/error', (req, res) => {
const error = req.error || req.query.error || ''
res
.status(500)
.render('error', { error })
})
app.get('/about', (req, res) => {
res.render('about', { version })
})
// NOTE: Capture all unkown URLs
app.get('*', (req, res) => {
res.status(400).render('404')
})
app.use('/download/html', limiter)
app.use('/download/html', checkURLParameter)
app.post('/download/html', async (req, res, next) => {
try {
const { url } = req.body
const facebookURL = getNormalizedUrl(url)
const html = await crawl(facebookURL, { logger: appLogger })
res
.contentType('text/html')
.status(200)
.send(Buffer.from(html, 'utf8'))
} catch (err) {
next(err)
}
})
app.use('/download', limiter)
app.use('/download', checkURLParameter)
app.post('/download', async (req, res, next) => {
try {
const { url } = req.body
const ics = await retrieveICS(url, {
logger: appLogger,
crawl,
})
res
.contentType('text/calendar')
.status(200)
.send(Buffer.from(ics, 'utf8'))
} catch (err) {
next(err)
}
})
app.use(createErrorLogger({ dev: isDevelopment }))
app.use(genericErrorHandler)
app.listen(port)

42
lib/routes/download.js Normal file
View File

@ -0,0 +1,42 @@
const { getNormalizedUrl } = require('../utils')
const crawl = require('../services/crawler')
const { retrieveICS } = require('../services/ics-retriever')
const downloadHTML = (logger) => (async (req, res, next) => {
try {
const { url } = req.body
const facebookURL = getNormalizedUrl(url)
const html = await crawl(facebookURL, { logger })
res
.contentType('text/html')
.status(200)
.send(Buffer.from(html, 'utf8'))
} catch (err) {
next(err)
}
})
const download = (logger) => (async (req, res, next) => {
try {
const { url } = req.body
const ics = await retrieveICS(url, {
logger,
crawl,
})
res
.contentType('text/calendar')
.status(200)
.send(Buffer.from(ics, 'utf8'))
} catch (err) {
next(err)
}
})
module.exports = {
downloadHTML,
download,
}

16
lib/routes/error.js Normal file
View File

@ -0,0 +1,16 @@
const error = (req, res) => {
const error = req.error || req.query.error || ''
res
.status(500)
.render('error', { error })
}
const notFound = (req, res) => {
res.status(400).render('404')
}
module.exports = {
error,
notFound,
}

9
lib/routes/index.js Normal file
View File

@ -0,0 +1,9 @@
const { error, notFound } = require('./error')
const { download, downloadHTML } = require('./download')
module.exports = {
error,
notFound,
download,
downloadHTML,
}

View File

@ -28,3 +28,4 @@ const crawl = async (url, { logger }) => {
}
module.exports = crawl

33
lib/static/404.html Normal file
View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page Not Found</title>
<style media="screen">
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px 16px; border-radius: 3px; }
#message h3 { color: #888; font-weight: normal; font-size: 16px; margin: 16px 0 12px; }
#message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
#message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
#message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
@media (max-width: 600px) {
body, #message { margin-top: 0; background: white; box-shadow: none; }
body { border-top: 16px solid #ffa100; }
}
</style>
</head>
<body>
<div id="message">
<h2>404</h2>
<h1>Page Not Found</h1>
<p>The specified file was not found on this website. Please check the URL for mistakes and try again.</p>
<h3>Why am I seeing this?</h3>
<p>This page was generated by the Firebase Command-Line Interface. To modify it, edit the <code>404.html</code> file in your project's configured <code>public</code> directory.</p>
</div>
</body>
</html>

View File

@ -77,5 +77,11 @@
<a href="https://github.com/comatory/fb2iCal" target="_blank" title="Github">Source</a>
<a href="/about" target="_blank" title="About the project">About</about>
</footer>
<script>
window.__firebaseConfiguration__ = {
serverUrl: "<%= htmlWebpackPlugin.options.serverURL %>",
}
</script>
</body>
</html>

View File

@ -171,7 +171,7 @@ import generateICS from '../../lib/services/ics-generator'
const postURL = (data) => {
return new Promise((resolve, reject) => {
fetch('/download/html', {
fetch('/download/html/', {
method: 'POST',
headers: {
'Accept': 'text/html, application/json',

12
package-lock.json generated
View File

@ -4122,6 +4122,15 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"cors": {
"version": "2.8.5",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"requires": {
"object-assign": "^4",
"vary": "^1"
}
},
"create-ecdh": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/create-ecdh/-/create-ecdh-4.0.3.tgz",
@ -9544,8 +9553,7 @@
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=",
"dev": true
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
},
"object-copy": {
"version": "0.1.0",

View File

@ -8,11 +8,16 @@
"node": "10.15.0"
},
"scripts": {
"build": "webpack --mode=production",
"build:dev": "webpack --mode=development",
"build": "npm run clean:build && webpack --mode=production",
"build:dev": "npm run clean:build && webpack --mode=development",
"build:firebase:hosting": "npm run clean:build && NODE_ENV=production NODE_APP=firebase webpack --mode=production",
"build:firebase:hosting:dev": "npm run clean:build && NODE_ENV=development NODE_APP=firebase webpack --mode=development",
"clean:build": "rm dist/** || true",
"deploy:firebase": "npm run build:firebase:hosting && firebase deploy",
"start": "npm run build && node lib/index.js",
"start:dev": "concurrently \"npm run build:dev\" \"NODE_ENV=development PORT=3000 nodemon lib/index.js\"",
"start:dev:inspect": "npm run build:dev && NODE_ENV=development PORT=3000 nodemon --inspect lib/index.js",
"start:dev:firebase": "npm run build:firebase:hosting:dev && NODE_ENV=development firebase emulators:start",
"test": "jest"
},
"keywords": [
@ -27,6 +32,7 @@
"dependencies": {
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.3",
"cors": "^2.8.5",
"dayjs": "^1.8.16",
"ejs": "^2.7.1",
"express": "^4.17.1",

View File

@ -1,4 +1,5 @@
const path = require('path')
const fs = require('fs')
const pkg = require('./package.json')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
@ -6,10 +7,30 @@ const { GenerateSW } = require('workbox-webpack-plugin')
const destination = path.join(__dirname, 'dist')
const isDevelopment = Boolean(process.argv[2] && process.argv[2].includes('mode=development'))
const isFirebaseEnv = process.env.NODE_APP === 'firebase'
const firebaseConfigFilePath = path.join(__dirname, '.firebaserc')
const hasFirebaseConfig = fs.existsSync(firebaseConfigFilePath)
if (isFirebaseEnv && hasFirebaseConfig) {
console.info('Prepare build for Firebase hosting')
}
const getFirebaseUrl = () => {
const contents = fs.readFileSync(firebaseConfigFilePath)
const rawContents = contents.toString()
const json = JSON.parse(rawContents)
const projectName = json.projects ? json.projects.default : null
if (isDevelopment) {
return `http://localhost:5001/${projectName}/uscentral-1/app`
}
return `${projectName}.web.app/app`
}
module.exports = {
entry: path.join(__dirname, 'lib', 'static', 'index.js'),
watch: isDevelopment,
watch: isDevelopment && !isFirebaseEnv,
output: {
filename: '[name].[hash].js',
path: destination,
@ -23,6 +44,7 @@ module.exports = {
new HtmlWebpackPlugin({
template: path.join(__dirname, 'lib', 'static', 'index.html'),
version: pkg.version,
serverURL: (isFirebaseEnv && hasFirebaseConfig) ? getFirebaseUrl() : '',
inject: 'body',
}),
new GenerateSW({