Skip to content

Commit

Permalink
converted callback to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
abbr committed Oct 15, 2018
1 parent 188f8a7 commit bd29626
Show file tree
Hide file tree
Showing 17 changed files with 599 additions and 219 deletions.
4 changes: 0 additions & 4 deletions .bowerrc

This file was deleted.

14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\bin\\www"
}
]
}
79 changes: 0 additions & 79 deletions Gruntfile.js

This file was deleted.

17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Large organizations often own many web sites, such as vanity sites, subsidiary s
* Security: Only pre-registered client sites can use the service.

## Live Demo
Take a look at [live demo](https://unippear.herokuapp.com/test.html) hosted on Heroku.
Take a look at [live demo](https://unippear.herokuapp.com/demo/test.html) hosted on Heroku.

## Description
### Structure
Expand Down Expand Up @@ -50,7 +50,7 @@ Take a look at [live demo](https://unippear.herokuapp.com/test.html) hosted on H
3. *assets/<version/theme>/header.html* containing header HTML fragment loaded by calling [jQuery.get()](http://api.jquery.com/jquery.get/). By default header is prepended to document *<body>*. The container element can be changed by setting *headerContainer* option when client site invoking the loader. More about *headerContainer* option in [Customization](#customization) below.
4. *assets/<version/theme>/footer.html* containing footer HTML fragment loaded and inserted same way as *assets/<version/theme>/header.html* except that footer is appended to the container set by *footerContainer* option.

To maximize performance, all assets are downloaded in parallel. Furthermore, all JS files are combined into one download by default. If individual JS download is desirable, say for debugging purpose, it can be enabled by toggling `routes.combineJs` to `false` in */app.js*.
To maximize performance, all assets are downloaded in parallel. Furthermore, all JS files are combined into one download by default. If individual JS download is desirable, say for debugging purpose, it can be enabled by setting environment variable `UNIPPEAR_COMBINE_JS` to `false`.

The order of parsing assets is important. CSS and JS files should be named in their desired parsing order by, for example, prefixing file names with 0-left-padded digits such as 01_file1.js, 02_file2.js etc. To ensure event handler is defined before event is triggered, the loader postpones inserting header and footer into DOM only after all JS files have been downloaded and evaluated.

Expand Down Expand Up @@ -176,16 +176,21 @@ It is assumed that the layout to be implemented as a service will be imported fr
* A client site usually needs some regression test before upgrading to a new layout version. The *latest* symbolic link should be advertised precautiously unless you can guarantee backward compatibility.

## Installation
If you have [Node](http://nodejs.org/) installed, to download *Unippear* simply run command
```npm install unippear``` and application root is *./node_modules/unippear*. Feel free to move application root after download.
Make sure you have latest version of [Node](http://nodejs.org/) installed.

You can also clone *Unippear* and run `npm install`.
```
git clone [email protected]:abbr/Unippear.git
cd Unippear
npm i -g yarn
yarn install
```
If you don't have `git`, you can also download [zip](https://github.com/abbr/Unippear/archive/master.zip) and extract.

## License

The MIT License (MIT)

Copyright (c) 2014
Copyright (c) 2014-Present

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 8 additions & 0 deletions app.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}
32 changes: 14 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var express = require('express')
var path = require('path')
var favicon = require('serve-favicon')
var logger = require('morgan')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var fs = require('fs')
var stripJsonComments = require('strip-json-comments')

var routes = require('./routes/index')
routes.publicFolderNm = 'public'
routes.combineJs = true
routes.combineJs = process.env.UNIPPEAR_COMBINE_JS ? (process.env.UNIPPEAR_COMBINE_JS == 'true') : true
var app = express()
// uncomment follow block to minify JS and CSS assets using express-minify
/*
Expand All @@ -30,7 +29,6 @@ app.set('view engine', 'ejs')
app.set('trust proxy', true)

// app.use(favicon(__dirname + '/public/img/favicon.ico'))
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
Expand All @@ -40,33 +38,32 @@ app.use(cookieParser())
//CORS and Referer validation
var clientWhiteList = path.join(__dirname, 'client-whitelist.json')
var validClients = [/.*/]
var parseWhiteList = function() {
var parseWhiteList = function () {
try {
validClients = JSON.parse(stripJsonComments(fs.readFileSync(clientWhiteList).toString())).map(function(v) {
validClients = JSON.parse(stripJsonComments(fs.readFileSync(clientWhiteList).toString())).map(function (v) {
return new RegExp(v)
})
}
catch (err) {}
} catch (err) {}
}
parseWhiteList()
fs.watchFile(clientWhiteList, function(curr, prev) {
fs.watchFile(clientWhiteList, function (curr, prev) {
if (curr.mtime.getTime() > prev.mtime.getTime()) {
parseWhiteList()
}
})

app.use(function(req, res, next) {
app.use(function (req, res, next) {
// Referer
if (req.headers.referer) {
if (!validClients.some(function(v) {
if (!validClients.some(function (v) {
return v.test(req.headers.referer)
}) && req.headers.referer.indexOf(req.protocol + "://" + req.hostname) !== 0) {
return res.status(403).end()
}
}

if (req.headers.origin) {
if (validClients.some(function(v) {
if (validClients.some(function (v) {
return v.test(req.headers.origin)
}) || req.headers.origin.indexOf(req.protocol + "://" + req.hostname) === 0) {
res.header('Access-Control-Allow-Origin', req.headers.origin)
Expand All @@ -78,12 +75,11 @@ app.use(function(req, res, next) {
}
// preflight caching age set to 1 day
res.header('Access-Control-Max-Age', 86400)
// intercept preflight OPTIONS method
// intercept preflight OPTIONS method
if (req.method === 'OPTIONS') {
return res.status(200).end()
}
}
else {
} else {
return res.status(403).end()
}
}
Expand All @@ -98,7 +94,7 @@ app.use('/', routes)


/// catch 404 and forward to error handler
app.use(function(req, res, next) {
app.use(function (req, res, next) {
var err = new Error('Not Found')
err.status = 404
next(err)
Expand All @@ -109,7 +105,7 @@ app.use(function(req, res, next) {
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
app.use(function (err, req, res, next) {
res.status(err.status || 500)
res.render('api/error', {
message: err.message,
Expand All @@ -121,7 +117,7 @@ if (app.get('env') === 'development') {

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
app.use(function (err, req, res, next) {
res.status(err.status || 500)
res.render('api/error', {
message: err.message,
Expand All @@ -131,4 +127,4 @@ app.use(function(err, req, res, next) {
})


module.exports = app
module.exports = app
12 changes: 6 additions & 6 deletions bin/www
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node
var debug = require('debug')('expressapp');
var app = require('../app');

app.set('port', process.env.PORT || 3000);
var app = require('../app')

var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
app.set('port', process.env.PORT || 3000)

var server = app.listen(app.get('port'), function () {
console.debug('Express server listening on port ' + server.address().port)
})
9 changes: 0 additions & 9 deletions bower.json

This file was deleted.

35 changes: 11 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"name": "unippear",
"version": "0.0.16",
"version": "0.1.0",
"license": "MIT",
"author": {
"name": "Fred"
},
"author": "abbr (https://github.com/abbr)",
"repository": {
"type": "git",
"url": "https://github.com/abbr/Unippear.git"
Expand All @@ -20,29 +18,18 @@
"start": "node ./bin/www"
},
"dependencies": {
"async": "~0.9.0",
"body-parser": "~1.4.3",
"async": "^2.6.0",
"body-parser": "^1.18.3",
"cookie-parser": "~1.3.2",
"deasync": "^0.1.0",
"debug": "~1.0.2",
"ejs": "^2.5.5",
"etag": "~1.5.1",
"express": "~4.10.6",
"express-minify": "^0.1.1",
"morgan": "~1.1.1",
"recursive-readdir": "~1.2.0",
"serve-favicon": "~2.0.1",
"etag": "^1.5.1",
"express": "^4.16.4",
"express-minify": "^1.0.0",
"recursive-readdir": "^2.1.0",
"serve-favicon": "^2.5.0",
"strip-json-comments": "^1.0.2"
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-develop": "~0.4.0",
"grunt-contrib-watch": "~0.6.1",
"request": "~2.36.0",
"time-grunt": "~0.3.2",
"load-grunt-tasks": "~0.6.0"
},
"engines": {
"node": ">=0.8.0"
"node": ">=8.6.0"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
4 changes: 2 additions & 2 deletions public/assets/test.html → public/assets/demo/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head>
<title>Unippear Demo</title>
<script type="text/javascript" src="/v1/theme1/"></script>
<script type="text/javascript" src="/demo/"></script>
<script type="text/javascript">
unippear({
"headerContainer": "#wrapper",
Expand All @@ -17,7 +17,7 @@
<div id="main">
<div id="content">
<h2 class="entry-title">Welcome!</h2> Does this page look like an OOTB WordPress installation? If you think so, then my goal is partially achieved! Actually, the page is generated from a static HTML file of which source is available <a href="https://github.com/abbr/Unippear/blob/master/public/assets/test.html" target="_blank">here</a>. Looking at the source, you'll find it only contains this "Welcome" paragragh. Then where does the header (all stuff before this paragragh) and footer (all stuff afterwards) come from? The answer is this JS block:
<pre>&lt;script type=&quot;text/javascript&quot; src=&quot;/v1/theme1/&quot;&gt;&lt;/script&gt;
<pre>&lt;script type=&quot;text/javascript&quot; src=&quot;/demo/&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
unippear({
&quot;headerContainer&quot;: &quot;#wrapper&quot;,
Expand Down
Loading

0 comments on commit bd29626

Please sign in to comment.