| 1 | package Mojolicious::Plugin::BarefootJS::DevReload; | ||||||||||||||||||||||||||||
| 2 | 1 | our $VERSION = "0.41.4"; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin', -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Mojolicious::Plugin::BarefootJS::DevReload - Dev-only browser auto-reload for BarefootJS apps | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # In your Mojolicious::Lite app (development mode) | ||||||||||||||||||||||||||||
| 12 | plugin 'BarefootJS::DevReload'; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Then in your layout template, before </body>: | ||||||||||||||||||||||||||||
| 15 | %== bf_dev_snippet | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Companion to C<barefoot build --watch> in C<@barefootjs/cli>. The CLI drops | ||||||||||||||||||||||||||||
| 20 | C<< <dist>/.dev/build-id >> after every successful rebuild that changed | ||||||||||||||||||||||||||||
| 21 | output; this plugin watches that file and streams SSE C<< event: reload >> | ||||||||||||||||||||||||||||
| 22 | to subscribed browsers so an editor save triggers an automatic reload. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Disabled automatically when C<< $app->mode eq 'production' >> (set via | ||||||||||||||||||||||||||||
| 25 | C<MOJO_MODE=production>). Pass C<< enabled => 0 >> to disable explicitly or | ||||||||||||||||||||||||||||
| 26 | C<< enabled => 1 >> to force-enable. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Mojo::ByteStream qw(b); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use BarefootJS::DevReload (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Engine-agnostic snippet, build-id reading, and timing constants are shared | ||||||||||||||||||||||||||||
| 36 | # with the PSGI/Plack path in BarefootJS::DevReload — one source of truth. | ||||||||||||||||||||||||||||
| 37 | 1 | my $HEARTBEAT_S = $BarefootJS::DevReload::HEARTBEAT_S; | |||||||||||||||||||||||||||
| 38 | 1 | my $POLL_S = $BarefootJS::DevReload::POLL_S; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 6 | ▶ | sub register ($self, $app, $config = {}) { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 6 | ▶ | my $dist_dir = $config->{dist_dir} // 'dist'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"dist_dir"} // "dist"
MC/DC0%0/1
$config->{"dist_dir"} // "dist"
$config->{"dist_dir"}
Truth table50%1/2
A
A= $config->{"dist_dir"}
| |||||||||||||||||||||||||||||
| 42 | 6 | ▶ | my $endpoint = $config->{endpoint} // '/_bf/reload'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"endpoint"} // "/_bf/reload"
MC/DC100%1/1
$config->{"endpoint"} // "/_bf/reload"
$config->{"endpoint"}
Truth table100%2/2
A
A= $config->{"endpoint"}
| |||||||||||||||||||||||||||||
| 43 | my $enabled = exists $config->{enabled} | ||||||||||||||||||||||||||||
| 44 | ? $config->{enabled} | ||||||||||||||||||||||||||||
| 45 | 6 | ▶ | : ($app->mode ne 'production'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Snippet helper is always registered so templates don't have to branch | ||||||||||||||||||||||||||||
| 48 | # on mode — it simply returns an empty ByteStream when disabled. | ||||||||||||||||||||||||||||
| 49 | 3 | ▶ | $app->helper(bf_dev_snippet => sub ($c) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 3 | ▶ | return b('') unless $enabled; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 2 | return b(BarefootJS::DevReload->snippet($endpoint)); | |||||||||||||||||||||||||||
| 52 | 6 | }); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 6 | ▶ | return unless $enabled; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Resolve dist_dir relative to the Mojolicious home when not already | ||||||||||||||||||||||||||||
| 57 | # absolute, so both `dist_dir => 'dist'` (the common case) and | ||||||||||||||||||||||||||||
| 58 | # `dist_dir => '/abs/path'` (tests) work. | ||||||||||||||||||||||||||||
| 59 | 4 | ▶ | my $dist_abs = File::Spec->file_name_is_absolute($dist_dir) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | ? $dist_dir | ||||||||||||||||||||||||||||
| 61 | : $app->home->child($dist_dir)->to_string; | ||||||||||||||||||||||||||||
| 62 | 4 | BarefootJS::DevReload->ensure_dev_dir($dist_abs); | |||||||||||||||||||||||||||
| 63 | 4 | my $build_id_path = BarefootJS::DevReload->build_id_path($dist_abs); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 3 | ▶ | $app->routes->get($endpoint => sub ($c) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | my $last_event_id = $c->req->headers->header('Last-Event-ID') // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$c->req->headers->header("Last-Event-ID") // ""
MC/DC100%1/1
$c->req->headers->header("Last-Event-ID") // ""
$c->req->headers->header("Last-Event-ID")
Truth table100%2/2
A
A= $c->req->headers->header("Last-Event-ID")
| |||||||||||||||||||||||||||||
| 67 | 3 | $last_event_id =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | $c->res->headers->content_type('text/event-stream'); | |||||||||||||||||||||||||||
| 70 | 3 | $c->res->headers->cache_control('no-cache, no-transform'); | |||||||||||||||||||||||||||
| 71 | 3 | $c->res->headers->connection('keep-alive'); | |||||||||||||||||||||||||||
| 72 | 3 | $c->res->headers->header('X-Accel-Buffering' => 'no'); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 3 | $c->write("retry: 1000\n\n"); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | my $initial_id = BarefootJS::DevReload->read_build_id($build_id_path); | |||||||||||||||||||||||||||
| 77 | 3 | my $last_sent = ''; | |||||||||||||||||||||||||||
| 78 | 3 | ▶ | if (length $initial_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 3 | $last_sent = $initial_id; | |||||||||||||||||||||||||||
| 80 | # When the client reconnects with a stale Last-Event-ID, a build | ||||||||||||||||||||||||||||
| 81 | # happened during its disconnected window — fire `reload` | ||||||||||||||||||||||||||||
| 82 | # immediately so the missed rebuild does not silently stay | ||||||||||||||||||||||||||||
| 83 | # unpainted until the next change. | ||||||||||||||||||||||||||||
| 84 | 3 | ▶ | my $event = (length $last_event_id && $last_event_id ne $initial_id) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $last_event_id and $last_event_id ne $initial_id
MC/DC50%1/2
length $last_event_id and $last_event_id ne $initial_id
length $last_event_id
$last_event_id ne $initial_id
Truth table66%2/3
A and B
A= length $last_event_idB= $last_event_id ne $initial_id
| |||||||||||||||||||||||||||||
| 85 | ? 'reload' : 'hello'; | ||||||||||||||||||||||||||||
| 86 | 3 | $c->write("event: $event\nid: $initial_id\ndata: $initial_id\n\n"); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | my ($hb_id, $poll_id); | |||||||||||||||||||||||||||
| 90 | 3 | ▶ | $c->on(finish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 91 | 3 | ▶ | Mojo::IOLoop->remove($hb_id) if $hb_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | Mojo::IOLoop->remove($poll_id) if $poll_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 3 | }); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $hb_id = Mojo::IOLoop->recurring($HEARTBEAT_S => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 96 | 0 | $c->write(": hb\n\n"); | |||||||||||||||||||||||||||
| 97 | 3 | }); | |||||||||||||||||||||||||||
| 98 | 11 | ▶ | $poll_id = Mojo::IOLoop->recurring($POLL_S => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||
| 99 | 11 | my $id = BarefootJS::DevReload->read_build_id($build_id_path); | |||||||||||||||||||||||||||
| 100 | 11 | ▶ | return unless length $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 11 | ▶ | return if $id eq $last_sent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | $last_sent = $id; | |||||||||||||||||||||||||||
| 103 | 0 | $c->write("event: reload\nid: $id\ndata: $id\n\n"); | |||||||||||||||||||||||||||
| 104 | 3 | }); | |||||||||||||||||||||||||||
| 105 | 4 | }); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | return; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | 1; | |||||||||||||||||||||||||||