1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # generates manifest.js.gz for grokmirror(1)
5 package PublicInbox::ManifestJsGz;
8 use parent qw(PublicInbox::WwwListing);
9 use PublicInbox::Config;
10 use IO::Compress::Gzip qw(gzip);
11 use HTTP::Date qw(time2str);
13 my $json = PublicInbox::Config::json();
17 # grokmirror uses relative paths, so it's domain-dependent
18 # SUPER calls PublicInbox::WwwListing::url_filter
19 $ctx->SUPER::url_filter('publicInbox.grokManifest', 'match=domain');
22 sub inject_entry ($$$;$) {
23 my ($ctx, $url_path, $ent, $git_dir) = @_;
24 $ctx->{-abs2urlpath}->{$git_dir // delete $ent->{git_dir}} = $url_path;
25 my $modified = $ent->{modified};
26 $ctx->{-mtime} = $modified if $modified > ($ctx->{-mtime} // 0);
27 $ctx->{manifest}->{$url_path} = $ent;
30 sub manifest_add ($$;$$) { # slow path w/o extindex "all" (or per-inbox)
31 my ($ctx, $ibx, $epoch, $default_desc) = @_;
32 my $url_path = "/$ibx->{name}";
35 $url_path .= "/git/$epoch.git";
36 $git = $ibx->git_epoch($epoch) or return;
40 my $ent = $git->manifest_entry($epoch, $default_desc) or return;
41 inject_entry($ctx, $url_path, $ent, $git->{git_dir});
44 sub slow_manifest_add ($$) {
47 if (defined(my $max = $ibx->max_git_epoch)) {
48 my $desc = $ibx->description;
49 for my $epoch (0..$max) {
50 manifest_add($ctx, $ibx, $epoch, $desc);
53 manifest_add($ctx, $ibx);
59 sub eidx_manifest_add ($$$) {
60 my ($ctx, $ALL, $ibx) = @_;
61 if (my $data = $ALL->misc->inbox_data($ibx)) {
62 $data = $json->decode($data);
63 delete $data->{''}; # private
64 while (my ($url_path, $ent) = each %$data) {
65 inject_entry($ctx, $url_path, $ent);
68 warn "E: `${\$ibx->eidx_key}' not indexed by $ALL->{topdir}\n";
69 # do not use slow path for global manifest since
70 # it can become catastrophically slow. per-inbox manifest
71 # is not too bad with dozens of epochs, so never fail that:
72 slow_manifest_add($ctx, $ibx) if $ibx == $ctx->{ibx};
77 my ($class, $ctx) = @_;
79 my ($re, undef) = $ctx->url_filter;
80 $re // return psgi_triple($ctx);
81 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
82 $ctx->can('list_match_i'), $re, $ctx);
84 $ctx->{-wcb} = $_[0]; # HTTP server callback
85 $ctx->{env}->{'pi-httpd.async'} ?
86 $iter->event_step : $iter->each_section;
92 my $ALL = $ctx->{www}->{pi_cfg}->ALL;
93 $ALL ? eidx_manifest_add($ctx, $ALL, $ibx) :
94 slow_manifest_add($ctx, $ibx);
97 sub hide_key { 'manifest' } # for WwwListing->list_match_i
101 my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
102 my $manifest = delete($ctx->{manifest}) // {};
103 while (my ($url_path, $repo) = each %$manifest) {
104 defined(my $abs = $repo->{reference}) or next;
105 $repo->{reference} = $abs2urlpath->{$abs};
107 $manifest = $json->encode($manifest);
108 gzip(\$manifest => \(my $out));
109 [ 200, [ qw(Content-Type application/gzip),
110 'Last-Modified', time2str($ctx->{-mtime}),
111 'Content-Length', length($out) ], [ $out ] ]
116 ibx_entry($ctx, $ctx->{ibx});