@@ -3,6 +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
7
7
8
const URL = 'https://storage.googleapis.com' ;
8
9
const datastore = new Datastore ( ) ;
@@ -15,23 +16,27 @@ interface ManifestCache {
15
16
[ requestSha : string ] : string ;
16
17
}
17
18
18
- const downloadManifest = async ( shortsha : string ) => {
19
- const key = datastore . key ( [ 'Fileset2Manifest' , shortsha ] ) ;
20
- const entity = await datastore . get ( key ) ;
21
- if ( ! entity || ! entity [ 0 ] ) {
19
+ const downloadManifest = async ( branchOrRef : string ) => {
20
+ const keys = [
21
+ datastore . key ( [ 'Fileset2Manifest' , branchOrRef ] ) ,
22
+ datastore . key ( [ 'Fileset2Manifest' , `branch:${ branchOrRef } ` ] ) ,
23
+ ] ;
24
+ const resp = await datastore . get ( keys ) ;
25
+ if ( ! resp || ! resp [ 0 ] ) {
22
26
return ;
23
27
}
24
- const paths = entity [ 0 ] . paths ;
25
- return paths ;
28
+ const entities = resp [ 0 ] ;
29
+ return entities [ 0 ] || entities [ 1 ] ;
26
30
} ;
27
31
28
32
const parseHostname = ( hostname : string ) => {
29
33
// TODO: Make this more robust
30
34
if ( hostname . includes ( '-dot-' ) ) {
31
- const parts = hostname . split ( '-dot-' ) ;
35
+ const prefix = hostname . split ( '-dot-fileset2-' ) [ 0 ] ;
36
+ const parts = prefix . split ( '-' ) ; // Either site-ref or site.
32
37
return {
33
38
siteId : parts [ 0 ] ,
34
- branchOrRef : parts [ 1 ] . slice ( 0 , 7 ) ,
39
+ branchOrRef : parts . length > 1 ? parts [ 1 ] . slice ( 0 , 7 ) : 'master' ,
35
40
} ;
36
41
} else {
37
42
return {
@@ -41,30 +46,30 @@ const parseHostname = (hostname: string) => {
41
46
}
42
47
} ;
43
48
44
- export function createApp ( siteId : string , shortsha : string , branch : string ) {
45
- // const startupManifest = await downloadManifest(shortsha );
46
- console . log ( `Starting server for site: ${ siteId } @ ${ branch } ` ) ;
49
+ export function createApp ( siteId : string , branchOrRef : string ) {
50
+ // const startupManifest = await downloadManifest(branchOrRef );
51
+ console . log ( `Starting server for site: ${ siteId } @ ${ branchOrRef } ` ) ;
47
52
48
53
const app = express ( ) ;
49
54
app . disable ( 'x-powered-by' ) ;
50
55
app . all ( '/*' , async ( req : express . Request , res : express . Response ) => {
51
56
const envFromHostname = parseHostname ( req . hostname ) ;
52
57
const requestSiteId = envFromHostname . siteId || siteId ;
53
- const requestSha = envFromHostname . branchOrRef || shortsha ;
54
- const requestBranch = envFromHostname . branchOrRef || branch ;
58
+ const requestBranchOrRef = envFromHostname . branchOrRef || branchOrRef ;
55
59
56
60
let blobPath = decodeURIComponent ( req . path ) ;
57
61
if ( blobPath . endsWith ( '/' ) ) {
58
62
blobPath += 'index.html' ;
59
63
}
60
64
61
- const manifest = await downloadManifest ( requestSha ) ;
65
+ const manifest = await downloadManifest ( requestBranchOrRef ) ;
66
+ const manifestPaths = manifest . paths ;
62
67
63
- if ( ! manifest ) {
68
+ if ( ! manifestPaths ) {
64
69
res . sendStatus ( 404 ) ;
65
70
return ;
66
71
}
67
- const blobKey = manifest [ blobPath ] ;
72
+ const blobKey = manifestPaths [ blobPath ] ;
68
73
const updatedUrl = `/wing-prod.appspot.com/fileset/sites/${ requestSiteId } /blobs/${ blobKey } ` ;
69
74
70
75
// TODO: Add custom 404 support based on site config.
@@ -100,7 +105,7 @@ export function createApp(siteId: string, shortsha: string, branch: string) {
100
105
// This also can't be a very small value, as it kills perf. 0036 seems to work correctly.
101
106
proxyRes . headers [ 'cache-control' ] = 'public, max-age=0036' ; // The padded 0036 keeps the content length the same per upload.ts.
102
107
proxyRes . headers [ 'x-fileset-blob' ] = blobKey ;
103
- proxyRes . headers [ 'x-fileset-ref' ] = requestSha ;
108
+ proxyRes . headers [ 'x-fileset-ref' ] = manifest . ref ;
104
109
proxyRes . headers [ 'x-fileset-site' ] = siteId ;
105
110
res . writeHead ( proxyRes . statusCode || 200 , proxyRes . headers ) ;
106
111
} ) ;
0 commit comments