fragment: server cache / relative path resolve / auto reload
This commit is contained in:
parent
0c9d2a2e4b
commit
e5bbb15071
5 changed files with 102 additions and 18 deletions
|
|
@ -3,7 +3,8 @@ var path = require('path'),
|
|||
settings = require('./settings'),
|
||||
osc = require('./osc'),
|
||||
{ipc} = require('./server'),
|
||||
{deepCopy, resolveHomeDir} = require('./utils')
|
||||
{deepCopy, resolveHomeDir} = require('./utils'),
|
||||
fragmentManager
|
||||
|
||||
var widgetHashTable = {},
|
||||
clipboard = {clipboard: null, idClipboard: null}
|
||||
|
|
@ -130,15 +131,9 @@ module.exports = {
|
|||
|
||||
if (Array.isArray(data.path)) data.path = path.resolve(...data.path)
|
||||
|
||||
module.exports.fileRead(data, clientId, true, (result)=>{
|
||||
fragmentManager = fragmentManager || require('./fragments')
|
||||
|
||||
ipc.send('fragmentLoad', {path: data.path, fileContent: result}, clientId)
|
||||
|
||||
}, (error)=>{
|
||||
|
||||
ipc.send('errorLog', `Could not open fragment file:\n ${error}`)
|
||||
|
||||
})
|
||||
fragmentManager.loadFragment(data.path, clientId)
|
||||
|
||||
},
|
||||
|
||||
|
|
|
|||
71
src/server/fragment-manager.js
Normal file
71
src/server/fragment-manager.js
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
var {resolvePath, ipc} = require('./server'),
|
||||
callbacks = require('./callbacks'),
|
||||
chokidar = require('chokidar'),
|
||||
fs = require('fs')
|
||||
|
||||
class FragmentManager {
|
||||
|
||||
constructor() {
|
||||
|
||||
this.fragments = {} // fragment files contents
|
||||
this.watchers = {} // fragment files watchers
|
||||
this.clients = {} // client ids for each watched fragment file
|
||||
|
||||
}
|
||||
|
||||
read(path, resolvedPath, clientId, then) {
|
||||
|
||||
var pathId = path + '->' + resolvedPath
|
||||
|
||||
callbacks.fileRead({path: resolvedPath}, clientId, true, (result)=>{
|
||||
this.fragments[pathId] = result
|
||||
then(result)
|
||||
}, (error)=>{
|
||||
ipc.send('errorLog', `Could not open fragment file:\n ${error}`, clientId)
|
||||
this.watchers[pathId].close()
|
||||
delete this.watchers[pathId]
|
||||
delete this.fragments[pathId]
|
||||
delete this.clients[pathId]
|
||||
console.log('ERROR')
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
loadFragment(path, clientId) {
|
||||
|
||||
var resolvedPath = resolvePath(path, clientId),
|
||||
pathId = path + '->' + resolvedPath
|
||||
|
||||
|
||||
if (!resolvedPath) return ipc.send('errorLog', `Fragment file not found: ${path}`, clientId)
|
||||
|
||||
if (!this.clients[pathId]) this.clients[pathId] = []
|
||||
if (!this.clients[pathId].includes(clientId)) this.clients[pathId].push(clientId)
|
||||
|
||||
if (!this.fragments[resolvedPath]) {
|
||||
|
||||
this.fragments[pathId] = this.read(path, resolvedPath, clientId, (result)=>{
|
||||
ipc.send('fragmentLoad', {path: path, fileContent: this.fragments[pathId]}, clientId)
|
||||
})
|
||||
|
||||
this.watchers[pathId] = chokidar.watch(resolvedPath, {awaitWriteFinish: {stabilityThreshold: 200}}).on('change', ()=>{
|
||||
this.fragments[pathId] = this.read(path, resolvedPath, clientId, (result)=>{
|
||||
for (let id of this.clients[pathId]) {
|
||||
ipc.send('fragmentLoad', {path: path, fileContent: this.fragments[pathId]}, id)
|
||||
}
|
||||
})
|
||||
}).on('unlink', ()=>{
|
||||
this.watchers[pathId].close()
|
||||
delete this.watchers[pathId]
|
||||
delete this.fragments[pathId]
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = new FragmentManager()
|
||||
|
|
@ -22,7 +22,8 @@ var urlparser = require('url'),
|
|||
Ipc = require('./ipc/server'),
|
||||
ipc = new Ipc(server),
|
||||
theme = require('./theme').init(),
|
||||
zeroconf = require('./zeroconf'),
|
||||
zeroconf = require('./zeroconf'),
|
||||
{resolveHomeDir} = require('./utils'),
|
||||
prod = !process.argv[0].includes('node_modules'),
|
||||
osc = {},
|
||||
clients = {},
|
||||
|
|
@ -40,7 +41,24 @@ if (settings.read('client-options')) {
|
|||
|
||||
var resolveDirs = theme.files.map(x=>path.dirname(x))
|
||||
resolveDirs = resolveDirs.filter((x, index)=>{return resolveDirs.indexOf(x) === index})
|
||||
resolveDirs.push('') // absolute
|
||||
resolveDirs.push(settings.read('remote-root') || '') // remote-root / absolute
|
||||
|
||||
function resolvePath(url, clientId) {
|
||||
|
||||
var sessionPath = path.dirname(ipc.clients[clientId].sessionPath)
|
||||
|
||||
url = resolveHomeDir(url)
|
||||
|
||||
for (var i = sessionPath ? -1 : 0; i < resolveDirs.length; i++) {
|
||||
var p = i === -1 ? sessionPath : resolveDirs[i]
|
||||
p = path.resolve(path.join(p, url))
|
||||
|
||||
if (fs.existsSync(p)) return p
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
function httpRoute(req, res) {
|
||||
|
||||
|
|
@ -98,13 +116,11 @@ function httpRoute(req, res) {
|
|||
// Resolution order: session path, theme path, absolute path
|
||||
|
||||
var id = urlparser.parse('?' + req.headers.cookie, true).query.client_id,
|
||||
sessionPath = path.dirname(ipc.clients[id].sessionPath)
|
||||
resolvedPath = resolvePath(url.replace(/%20/g, ' '), id)
|
||||
|
||||
for (var i = -1; i < resolveDirs.length; i++) {
|
||||
var p = i === -1 ? sessionPath : resolveDirs[i]
|
||||
p = path.resolve(path.join(p, url))
|
||||
if (resolvedPath) {
|
||||
try {
|
||||
res.sendFile(p)
|
||||
res.sendFile(resolvedPath)
|
||||
return true
|
||||
} catch(e) {}
|
||||
}
|
||||
|
|
@ -187,7 +203,8 @@ var bindCallbacks = function(callbacks) {
|
|||
module.exports = {
|
||||
ipc:ipc,
|
||||
bindCallbacks:bindCallbacks,
|
||||
clients:clients
|
||||
clients:clients,
|
||||
resolvePath:resolvePath
|
||||
}
|
||||
|
||||
osc = require('./osc').server
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ module.exports = {
|
|||
resolveHomeDir: function(p) {
|
||||
// Resolve '~' to user's home directory
|
||||
if (!p) return ''
|
||||
return String(p).replace(/^~/, process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'])
|
||||
return String(p).replace(/^(~|\/~)/, process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'])
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue