feat(reload): add SSE live-reload server module
Problem: HTML output from pandoc has no live-reload; the browser must be refreshed manually after each compile. Solution: add lua/preview/reload.lua — a minimal SSE-only TCP server. start() binds 127.0.0.1:5554 and keeps EventSource connections alive; broadcast() pushes a reload event to all clients; inject() appends an EventSource script before </body> (or at EOF) on every compile so pandoc overwrites do not lose the tag.
This commit is contained in:
parent
bce3cec0e6
commit
6c1609cba8
2 changed files with 166 additions and 0 deletions
108
lua/preview/reload.lua
Normal file
108
lua/preview/reload.lua
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
local M = {}
|
||||
|
||||
local PORT = 5554
|
||||
local server_handle = nil
|
||||
local clients = {}
|
||||
|
||||
local function make_script(port)
|
||||
return '<script>(function(){'
|
||||
.. 'var es=new EventSource("http://localhost:'
|
||||
.. tostring(port)
|
||||
.. '/__live/events");'
|
||||
.. 'es.addEventListener("reload",function(){location.reload();});'
|
||||
.. '})()</script>'
|
||||
end
|
||||
|
||||
function M.start(port)
|
||||
port = port or PORT
|
||||
if server_handle then
|
||||
return
|
||||
end
|
||||
local server = vim.uv.new_tcp()
|
||||
server:bind('127.0.0.1', port)
|
||||
server:listen(128, function(err)
|
||||
if err then
|
||||
return
|
||||
end
|
||||
local client = vim.uv.new_tcp()
|
||||
server:accept(client)
|
||||
local buf = ''
|
||||
client:read_start(function(read_err, data)
|
||||
if read_err or not data then
|
||||
if not client:is_closing() then
|
||||
client:close()
|
||||
end
|
||||
return
|
||||
end
|
||||
buf = buf .. data
|
||||
if buf:find('\r\n\r\n') or buf:find('\n\n') then
|
||||
client:read_stop()
|
||||
local first_line = buf:match('^([^\r\n]+)')
|
||||
if first_line and first_line:find('/__live/events', 1, true) then
|
||||
local response = 'HTTP/1.1 200 OK\r\n'
|
||||
.. 'Content-Type: text/event-stream\r\n'
|
||||
.. 'Cache-Control: no-cache\r\n'
|
||||
.. 'Access-Control-Allow-Origin: *\r\n'
|
||||
.. '\r\n'
|
||||
client:write(response)
|
||||
table.insert(clients, client)
|
||||
else
|
||||
client:close()
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
server_handle = server
|
||||
end
|
||||
|
||||
function M.stop()
|
||||
for _, c in ipairs(clients) do
|
||||
if not c:is_closing() then
|
||||
c:close()
|
||||
end
|
||||
end
|
||||
clients = {}
|
||||
if server_handle then
|
||||
server_handle:close()
|
||||
server_handle = nil
|
||||
end
|
||||
end
|
||||
|
||||
function M.broadcast()
|
||||
local event = 'event: reload\ndata: {}\n\n'
|
||||
local alive = {}
|
||||
for _, c in ipairs(clients) do
|
||||
if not c:is_closing() then
|
||||
local ok = pcall(function()
|
||||
c:write(event)
|
||||
end)
|
||||
if ok then
|
||||
table.insert(alive, c)
|
||||
end
|
||||
end
|
||||
end
|
||||
clients = alive
|
||||
end
|
||||
|
||||
function M.inject(path, port)
|
||||
port = port or PORT
|
||||
local f = io.open(path, 'r')
|
||||
if not f then
|
||||
return
|
||||
end
|
||||
local content = f:read('*a')
|
||||
f:close()
|
||||
local script = make_script(port)
|
||||
local new_content, n = content:gsub('</body>', script .. '\n</body>', 1)
|
||||
if n == 0 then
|
||||
new_content = content .. '\n' .. script
|
||||
end
|
||||
local fw = io.open(path, 'w')
|
||||
if not fw then
|
||||
return
|
||||
end
|
||||
fw:write(new_content)
|
||||
fw:close()
|
||||
end
|
||||
|
||||
return M
|
||||
58
spec/reload_spec.lua
Normal file
58
spec/reload_spec.lua
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
describe('reload', function()
|
||||
local reload
|
||||
|
||||
before_each(function()
|
||||
package.loaded['preview.reload'] = nil
|
||||
reload = require('preview.reload')
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
reload.stop()
|
||||
end)
|
||||
|
||||
describe('inject', function()
|
||||
it('injects script before </body>', function()
|
||||
local path = os.tmpname()
|
||||
local f = io.open(path, 'w')
|
||||
f:write('<html><body><p>hello</p></body></html>')
|
||||
f:close()
|
||||
|
||||
reload.inject(path)
|
||||
|
||||
local fr = io.open(path, 'r')
|
||||
local content = fr:read('*a')
|
||||
fr:close()
|
||||
os.remove(path)
|
||||
|
||||
assert.is_truthy(content:find('EventSource', 1, true))
|
||||
local script_pos = content:find('EventSource', 1, true)
|
||||
local body_pos = content:find('</body>', 1, true)
|
||||
assert.is_truthy(body_pos)
|
||||
assert.is_true(script_pos < body_pos)
|
||||
end)
|
||||
|
||||
it('appends script when no </body>', function()
|
||||
local path = os.tmpname()
|
||||
local f = io.open(path, 'w')
|
||||
f:write('<html><p>hello</p></html>')
|
||||
f:close()
|
||||
|
||||
reload.inject(path)
|
||||
|
||||
local fr = io.open(path, 'r')
|
||||
local content = fr:read('*a')
|
||||
fr:close()
|
||||
os.remove(path)
|
||||
|
||||
assert.is_truthy(content:find('EventSource', 1, true))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('broadcast', function()
|
||||
it('does not error with no clients', function()
|
||||
assert.has_no.errors(function()
|
||||
reload.broadcast()
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue