secure-scuttlebot classic

decent improvements

Changed files
+80 -18
decent
plugins
+6 -1
decent/modules_core/app.js
··· 6 6 gives: 'app', 7 7 create: function (api) { 8 8 return function () { 9 - document.head.appendChild(h('style', require('../style.css.json'))) 9 + var hasStylesheet = document.querySelector('link[rel="stylesheet"][href$="style.css"]') 10 + if (!hasStylesheet) { 11 + document.head.appendChild( 12 + h('style', {'data-decent-style': 'true'}, require('../style.css.json')) 13 + ) 14 + } 10 15 11 16 window.addEventListener('error', window.onError = function (e) { 12 17 document.body.appendChild(h('div.error',
+1 -1
decent/package.json
··· 62 62 "indexhtmlify": "^1.3.1" 63 63 }, 64 64 "scripts": { 65 - "lite": "node scripts/style.js && mkdir -p build && browserify index.js | indexhtmlify --title \"Decent SSB\" > build/index.html", 65 + "lite": "node scripts/style.js && mkdir -p build && browserify index.js | indexhtmlify --title \"Decent SSB\" > build/index.html && node scripts/postprocess-index.js", 66 66 "bundle": "node scripts/style.js && mkdir -p build && browselectrify index.js > build/bundle.js", 67 67 "graph": "node index.js | dot -Tsvg > graph.svg", 68 68 "test": "set -e; for t in test/*.js; do node $t; done"
+27
decent/scripts/postprocess-index.js
··· 1 + var fs = require('fs') 2 + var path = require('path') 3 + 4 + var htmlPath = path.join(__dirname, '..', 'build', 'index.html') 5 + if (!fs.existsSync(htmlPath)) process.exit(0) 6 + 7 + var html = fs.readFileSync(htmlPath, 'utf8') 8 + if (html.indexOf('decent-preload') !== -1) process.exit(0) 9 + 10 + var headClose = html.indexOf('</head>') 11 + if (headClose === -1) process.exit(0) 12 + 13 + var insert = [ 14 + '<!-- decent-preload -->', 15 + '<link rel="preconnect" href="https://fonts.googleapis.com">', 16 + '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>', 17 + '<link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css">', 18 + '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css">', 19 + '<link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,400,0,0">', 20 + '<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,400,0,0">', 21 + '<link rel="preload" as="style" href="/style.css">', 22 + '<link rel="stylesheet" href="/style.css">' 23 + ].join('\n') + '\n' 24 + 25 + html = html.slice(0, headClose) + insert + html.slice(headClose) 26 + 27 + fs.writeFileSync(htmlPath, html)
+11 -1
decent/scripts/style.js
··· 1 1 var fs = require('fs') 2 2 var path = require('path') 3 3 4 + var buildDir = path.join(__dirname, '..', 'build') 5 + var stylePath = path.join(__dirname, '..', 'style.css') 6 + 7 + if (!fs.existsSync(buildDir)) 8 + fs.mkdirSync(buildDir) 9 + 4 10 fs.writeFileSync( 5 11 path.join(__dirname, '..', 'style.css.json'), 6 - JSON.stringify(fs.readFileSync(path.join(__dirname, '..', 'style.css'), 'utf8')) 12 + JSON.stringify(fs.readFileSync(stylePath, 'utf8')) 7 13 ) 8 14 15 + fs.writeFileSync( 16 + path.join(buildDir, 'style.css'), 17 + fs.readFileSync(stylePath, 'utf8') 18 + )
-3
decent/style.css
··· 1 - @import url('https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css'); 2 - @import url('https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,400,0,0'); 3 - 4 1 * { 5 2 box-sizing: border-box; 6 3 word-break: break-word;
+35 -12
plugins/decent-ui.js
··· 105 105 } 106 106 107 107 var filePath = path.join(decentDir, relPath) 108 - fs.stat(filePath, function (err, stat) { 109 - if (err || !stat || !stat.isFile()) { 110 - respondNotFound(res) 111 - return 112 - } 113 - 108 + function serveFile (resolvedPath) { 114 109 if (relPath === 'index.html' && req.method === 'GET') { 115 - return fs.readFile(filePath, 'utf8', function (readErr, html) { 110 + return fs.readFile(resolvedPath, 'utf8', function (readErr, html) { 116 111 if (readErr) { 117 112 respondNotFound(res) 118 113 return 119 114 } 120 115 116 + var headInsert = '' 117 + if (html.indexOf('rel="stylesheet" href="/style.css"') === -1 && 118 + html.indexOf('rel="stylesheet" href="style.css"') === -1) { 119 + headInsert += '<link rel="preload" as="style" href="/style.css">' + 120 + '<link rel="stylesheet" href="/style.css">' 121 + } 122 + 121 123 var remote = getRemoteForRequest(req) 122 124 if (remote) { 123 - var script = '<script>window.PATCHBAY_REMOTE = ' + 125 + headInsert += '<script>window.PATCHBAY_REMOTE = ' + 124 126 JSON.stringify(remote) + 125 127 ';</script>' 126 - html = html.replace('</head>', script + '</head>') 127 128 } 128 129 129 - res.writeHead(200, {'Content-Type': getContentType(filePath)}) 130 + if (headInsert) 131 + html = html.replace('</head>', headInsert + '</head>') 132 + 133 + res.writeHead(200, {'Content-Type': getContentType(resolvedPath)}) 130 134 res.end(html) 131 135 }) 132 136 } 133 137 134 - res.writeHead(200, {'Content-Type': getContentType(filePath)}) 138 + res.writeHead(200, {'Content-Type': getContentType(resolvedPath)}) 135 139 136 140 if (req.method === 'HEAD') { 137 141 res.end() 138 142 return 139 143 } 140 144 141 - fs.createReadStream(filePath).pipe(res) 145 + fs.createReadStream(resolvedPath).pipe(res) 146 + } 147 + 148 + fs.stat(filePath, function (err, stat) { 149 + if (err || !stat || !stat.isFile()) { 150 + if (relPath === 'style.css') { 151 + var fallbackPath = path.join(__dirname, '..', 'decent', 'style.css') 152 + return fs.stat(fallbackPath, function (fallbackErr, fallbackStat) { 153 + if (fallbackErr || !fallbackStat || !fallbackStat.isFile()) { 154 + respondNotFound(res) 155 + return 156 + } 157 + serveFile(fallbackPath) 158 + }) 159 + } 160 + respondNotFound(res) 161 + return 162 + } 163 + 164 + serveFile(filePath) 142 165 }) 143 166 } 144 167