@@ -7,13 +7,33 @@ import * as tarFs from "tar-fs"
77import * as zlib from "zlib"
88import { HttpCode , HttpError } from "../../common/http"
99import { rootPath } from "../constants"
10- import { authenticated , replaceTemplates } from "../http"
10+ import { authenticated , ensureAuthenticated , replaceTemplates } from "../http"
1111import { getMediaMime , pathToFsPath } from "../util"
1212
1313export const router = Router ( )
1414
1515// The commit is for caching.
1616router . get ( "/(:commit)(/*)?" , async ( req , res ) => {
17+ // Used by VS Code to load extensions into the web worker.
18+ const tar = Array . isArray ( req . query . tar ) ? req . query . tar [ 0 ] : req . query . tar
19+ if ( typeof tar === "string" ) {
20+ ensureAuthenticated ( req )
21+ let stream : Readable = tarFs . pack ( pathToFsPath ( tar ) )
22+ if ( req . headers [ "accept-encoding" ] && req . headers [ "accept-encoding" ] . includes ( "gzip" ) ) {
23+ logger . debug ( "gzipping tar" , field ( "path" , tar ) )
24+ const compress = zlib . createGzip ( )
25+ stream . pipe ( compress )
26+ stream . on ( "error" , ( error ) => compress . destroy ( error ) )
27+ stream . on ( "close" , ( ) => compress . end ( ) )
28+ stream = compress
29+ res . header ( "content-encoding" , "gzip" )
30+ }
31+ res . set ( "Content-Type" , "application/x-tar" )
32+ stream . on ( "close" , ( ) => res . end ( ) )
33+ return stream . pipe ( res )
34+ }
35+
36+ // If not a tar use the remainder of the path to load the resource.
1737 if ( ! req . params [ 0 ] ) {
1838 throw new HttpError ( "Not Found" , HttpCode . NotFound )
1939 }
@@ -32,26 +52,6 @@ router.get("/(:commit)(/*)?", async (req, res) => {
3252 res . header ( "Cache-Control" , "public, max-age=31536000" )
3353 }
3454
35- /**
36- * Used by VS Code to load extensions into the web worker.
37- */
38- const tar = Array . isArray ( req . query . tar ) ? req . query . tar [ 0 ] : req . query . tar
39- if ( typeof tar === "string" ) {
40- let stream : Readable = tarFs . pack ( pathToFsPath ( tar ) )
41- if ( req . headers [ "accept-encoding" ] && req . headers [ "accept-encoding" ] . includes ( "gzip" ) ) {
42- logger . debug ( "gzipping tar" , field ( "path" , resourcePath ) )
43- const compress = zlib . createGzip ( )
44- stream . pipe ( compress )
45- stream . on ( "error" , ( error ) => compress . destroy ( error ) )
46- stream . on ( "close" , ( ) => compress . end ( ) )
47- stream = compress
48- res . header ( "content-encoding" , "gzip" )
49- }
50- res . set ( "Content-Type" , "application/x-tar" )
51- stream . on ( "close" , ( ) => res . end ( ) )
52- return stream . pipe ( res )
53- }
54-
5555 res . set ( "Content-Type" , getMediaMime ( resourcePath ) )
5656
5757 if ( resourcePath . endsWith ( "manifest.json" ) ) {
0 commit comments