Skip to content

Commit 6bd6806

Browse files
feat(cache): Cache tiles locally and reserve
1 parent 7e0b92b commit 6bd6806

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

bin/mastarm

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ commander
1818
commander
1919
.command('build [entries...]')
2020
.description('Bundle JavaScript & CSS')
21+
.option('-F, --flyle', 'Cache and serve tiles.')
2122
.option('-p, --proxy <address>', 'Proxy calls through to target address.')
2223
.option('-s, --serve', 'Serve with budo. Auto-matically rebuilds on changes.')
2324
.option('-w, --watch', 'Rebuild on changes with watchify.')
@@ -30,6 +31,7 @@ commander
3031
const budo = require('../lib/budo')
3132
files.map(budo({
3233
config,
34+
flyle: get('flyle'),
3335
proxy: get('proxy')
3436
}))
3537
} else {

lib/budo.js

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const transform = require('./transform')
55

66
module.exports = function ({
77
config,
8+
flyle,
89
proxy
910
}) {
1011
return function ([entry, outfile]) {
@@ -42,6 +43,16 @@ module.exports = function ({
4243
}
4344
})
4445
}
46+
if (flyle) {
47+
const serveTiles = require('./flyle')
48+
budoOpts.middleware.push(function (req, res, next) {
49+
if (req.url.indexOf('/tile') === 0) {
50+
serveTiles(req, res)
51+
} else {
52+
next()
53+
}
54+
})
55+
}
4556

4657
budo
4758
.cli([entry + ':' + outfile], budoOpts)

lib/flyle.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const concat = require('concat-stream')
2+
const fs = require('fs')
3+
const http = require('http')
4+
const https = require('https')
5+
const mkdirp = require('mkdirp')
6+
const path = require('path')
7+
const parse = require('url').parse
8+
9+
const DEFAULT_CACHE_DIRECTORY = `${process.env.HOME}/.flyle`
10+
const DEFAULT_PNG = path.resolve(__dirname, '../mastarm.png')
11+
12+
module.exports = function (req, res) {
13+
const url = parse(req.url, true).query.url
14+
const isHttps = url.indexOf('https') !== -1
15+
const get = isHttps ? https.get : http.get
16+
const filePath = isHttps ? url.split('https://')[1] : url.split('http://')[1]
17+
const fullPath = path.resolve(DEFAULT_CACHE_DIRECTORY, filePath)
18+
fs.stat(fullPath, (err, stats) => {
19+
if (!err && stats.isFile()) {
20+
sendImg({path: fullPath, res})
21+
} else {
22+
get(url, (fileResponse) => {
23+
mkdirp(path.dirname(fullPath), (err) => {
24+
if (err) {
25+
logAndSend({err, res})
26+
} else {
27+
fileResponse.pipe(concat((png) => {
28+
fs.writeFile(fullPath, png, (err) => {
29+
if (err) {
30+
logAndSend({err, res})
31+
} else {
32+
sendImg({path: fullPath, res})
33+
}
34+
})
35+
}))
36+
}
37+
})
38+
}).on('error', (err) => {
39+
logAndSend({err, res})
40+
})
41+
}
42+
})
43+
}
44+
45+
function logAndSend ({err, res}) {
46+
console.error('flyle >> sending default image: ', err.message)
47+
sendImg({
48+
path: DEFAULT_PNG,
49+
res
50+
})
51+
}
52+
53+
function sendImg ({path, res}) {
54+
res.writeHead(200, {
55+
'Content-Type': 'image/png'
56+
})
57+
fs.createReadStream(path).pipe(res)
58+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"check-dependencies": "^0.12.0",
4848
"commander": "^2.9.0",
4949
"commitizen": "^2.8.2",
50+
"concat-stream": "^1.5.1",
5051
"cz-conventional-changelog": "^1.1.6",
5152
"envify": "^3.4.1",
5253
"errorify": "^0.3.1",

0 commit comments

Comments
 (0)