@@ -3,7 +3,7 @@ import {GoogleAuth} from 'google-auth-library';
3
3
import * as fsPath from 'path' ;
4
4
import express = require( 'express' ) ;
5
5
import httpProxy = require( 'http-proxy' ) ;
6
- import { Manifest } from './manifest' ;
6
+ import { Manifest } from './manifest' ;
7
7
8
8
const URL = 'https://storage.googleapis.com' ;
9
9
const datastore = new Datastore ( ) ;
@@ -26,7 +26,33 @@ const downloadManifest = async (branchOrRef: string) => {
26
26
return ;
27
27
}
28
28
const entities = resp [ 0 ] ;
29
- return entities [ 0 ] || entities [ 1 ] ;
29
+ const result = entities [ 0 ] || entities [ 1 ] ;
30
+ if ( ! result ) {
31
+ return ;
32
+ }
33
+ if ( ! result . ttls ) {
34
+ return result ;
35
+ }
36
+
37
+ if ( result . ttls ) {
38
+ // TODO: Allow this to be overwritten.
39
+ const now = new Date ( ) ;
40
+ let latestManifest = null ;
41
+ for ( const ttlString in result . ttls ) {
42
+ const ttlDate = new Date ( ttlString ) ;
43
+ const isLaterThanManifestDate = now >= ttlDate ;
44
+ const isLaterThanAllManifests =
45
+ ! latestManifest || ttlDate >= latestManifest . ttl ;
46
+ if ( isLaterThanManifestDate && isLaterThanAllManifests ) {
47
+ latestManifest = result . ttls [ ttlString ] ;
48
+ latestManifest . ttl = ttlDate ;
49
+ }
50
+ }
51
+ if ( latestManifest ) {
52
+ return latestManifest ;
53
+ }
54
+ }
55
+ return result ;
30
56
} ;
31
57
32
58
const parseHostname = ( hostname : string ) => {
@@ -63,6 +89,12 @@ export function createApp(siteId: string, branchOrRef: string) {
63
89
}
64
90
65
91
const manifest = await downloadManifest ( requestBranchOrRef ) ;
92
+ if ( ! manifest ) {
93
+ // TODO: Consolidate not found errors.
94
+ res . sendStatus ( 404 ) ;
95
+ return ;
96
+ }
97
+
66
98
const manifestPaths = manifest . paths ;
67
99
68
100
if ( ! manifestPaths ) {
@@ -89,6 +121,9 @@ export function createApp(siteId: string, branchOrRef: string) {
89
121
changeOrigin : true ,
90
122
preserveHeaderKeyCase : true ,
91
123
} ) ;
124
+ server . on ( 'error' , ( error , req , res ) => {
125
+ console . log ( `An error occurred while serving ${ req . url } ` , error ) ;
126
+ } ) ;
92
127
server . on ( 'proxyRes' , ( proxyRes , req , res ) => {
93
128
delete proxyRes . headers [ 'x-cloud-trace-context' ] ;
94
129
delete proxyRes . headers [ 'x-goog-generation' ] ;
@@ -103,10 +138,13 @@ export function createApp(siteId: string, branchOrRef: string) {
103
138
delete proxyRes . headers [ 'x-guploader-uploadid' ] ;
104
139
// This cannot be "private, max-age=0" as this kills perf.
105
140
// This also can't be a very small value, as it kills perf. 0036 seems to work correctly.
106
- proxyRes . headers [ 'cache-control' ] = 'public, max-age=0036' ; // The padded 0036 keeps the content length the same per upload.ts.
141
+ proxyRes . headers [ 'cache-control' ] = 'public, max-age=0036' ; // The padded 0036 keeps the content length the same per upload.ts.
107
142
proxyRes . headers [ 'x-fileset-blob' ] = blobKey ;
108
143
proxyRes . headers [ 'x-fileset-ref' ] = manifest . ref ;
109
144
proxyRes . headers [ 'x-fileset-site' ] = siteId ;
145
+ if ( manifest . ttl ) {
146
+ proxyRes . headers [ 'x-fileset-ttl' ] = manifest . ttl ;
147
+ }
110
148
res . writeHead ( proxyRes . statusCode || 200 , proxyRes . headers ) ;
111
149
} ) ;
112
150
} ) ;
0 commit comments