-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
72 lines (63 loc) · 2.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict'
const { join } = require('node:path')
const fp = require('fastify-plugin')
const Piscina = require('piscina')
async function fastifyHotwire (fastify, opts) {
const { templates } = opts
delete opts.templates
const pool = new Piscina(opts)
fastify.decorateReply('render', render)
fastify.decorateReply('turboStream', {
getter () {
return {
append: (file, target, data) => turboSend(this, 'append', file, target, data),
prepend: (file, target, data) => turboSend(this, 'prepend', file, target, data),
replace: (file, target, data) => turboSend(this, 'replace', file, target, data),
update: (file, target, data) => turboSend(this, 'update', file, target, data),
remove: (file, target, data) => turboSend(this, 'remove', file, target, data)
}
}
})
fastify.decorateReply('turboGenerate', {
getter () {
return {
append: (file, target, data) => generate(this, 'append', file, target, data),
prepend: (file, target, data) => generate(this, 'prepend', file, target, data),
replace: (file, target, data) => generate(this, 'replace', file, target, data),
update: (file, target, data) => generate(this, 'update', file, target, data),
remove: (file, target, data) => generate(this, 'remove', file, target, data)
}
}
})
async function render (file, data) {
file = join(templates, file)
const html = await pool.runTask({ file, data, fragment: false })
this.type('text/html; charset=utf-8')
this.send(html)
return this
}
async function turboSend (that, action, file, target, data) {
const html = await pool.runTask({ file: join(templates, file), data, fragment: true })
that.type('text/vnd.turbo-stream.html; charset=utf-8')
that.send(buildStream(action, target, html.trim()))
return that
}
async function generate (_that, action, file, target, data) {
const html = await pool.runTask({ file: join(templates, file), data, fragment: true })
return buildStream(action, target, html).replace(/\n/g, '').trim()
}
}
function buildStream (action, target, content) {
return `
<turbo-stream action="${action}" target="${target}">
<template>
${content}
</template>
</turbo-stream>
`
}
module.exports = fp(fastifyHotwire, {
name: '@fastify/hotwire'
})
module.exports.default = fastifyHotwire
module.exports.fastifyHotwire = fastifyHotwire