import express from "express" ; import IAccountDataStore from "../common/IAccountDataStore" ; import IRequestListenerFactory from "../common/IRequestListenerFactory" ; import logger from "../common/Logger" ; import IExtentStore from "../common/persistence/IExtentStore" ; import { RequestListener } from "../common/ServerBase" ; import AccountSASAuthenticator from "./authentication/AccountSASAuthenticator" ; import BlobSASAuthenticator from "./authentication/BlobSASAuthenticator" ; import BlobSharedKeyAuthenticator from "./authentication/BlobSharedKeyAuthenticator" ; import BlobTokenAuthenticator from "./authentication/BlobTokenAuthenticator" ; import PublicAccessAuthenticator from "./authentication/PublicAccessAuthenticator" ; import ExpressMiddlewareFactory from "./generated/ExpressMiddlewareFactory" ; import IHandlers from "./generated/handlers/IHandlers" ; import MiddlewareFactory from "./generated/MiddlewareFactory" ; import AppendBlobHandler from "./handlers/AppendBlobHandler" ; import BlobHandler from "./handlers/BlobHandler" ; import BlockBlobHandler from "./handlers/BlockBlobHandler" ; import ContainerHandler from "./handlers/ContainerHandler" ; import PageBlobHandler from "./handlers/PageBlobHandler" ; import PageBlobRangesManager from "./handlers/PageBlobRangesManager" ; import ServiceHandler from "./handlers/ServiceHandler" ; import AuthenticationMiddlewareFactory from "./middlewares/AuthenticationMiddlewareFactory" ; import PreflightMiddlewareFactory from "./middlewares/PreflightMiddlewareFactory" ; import StrictModelMiddlewareFactory , { UnsupportedHeadersBlocker , UnsupportedParametersBlocker } from "./middlewares/StrictModelMiddlewareFactory" ; import IBlobMetadataStore from "./persistence/IBlobMetadataStore" ; import { DEFAULT_CONTEXT_PATH } from "./utils/constants" ; import morgan = require ( "morgan" ) ; import { OAuthLevel } from "../common/models" ; import IAuthenticator from "./authentication/IAuthenticator" ; import createStorageBlobContextMiddleware from "./middlewares/blobStorageContext.middleware" ; export class implements IRequestListenerFactory export default class BlobRequestListenerFactory implements IRequestListenerFactory { public constructor ( private readonly metadataStore , private readonly extentStore , private readonly accountDataStore , private readonly enableAccessLog , private readonly accessLogWriteStream ? , private readonly loose ? , private readonly skipApiVersionCheck ? , private readonly oauth ? , private readonly disableProductStyleUrl ? ) { } public createRequestListener ( ) { const app = express ( ) . disable ( "x-powered-by" ) ; const middlewareFactory = new ExpressMiddlewareFactory ( logger , DEFAULT_CONTEXT_PATH ) ; const pageBlobRangesManager = new PageBlobRangesManager ( ) ; const loose = this . loose || false ; const handlers = { appendBlobHandler : new AppendBlobHandler ( this . metadataStore , this . extentStore , logger , loose ) , blobHandler : new BlobHandler ( this . metadataStore , this . extentStore , logger , loose , pageBlobRangesManager ) , blockBlobHandler : new BlockBlobHandler ( this . metadataStore , this . extentStore , logger , loose ) , containerHandler : new ContainerHandler ( this . metadataStore , this . extentStore , logger , loose ) , pageBlobHandler : new PageBlobHandler ( this . metadataStore , this . extentStore , logger , loose , pageBlobRangesManager ) , serviceHandler : new ServiceHandler ( this . metadataStore , this . extentStore , logger , loose ) , directoryHandler : { } as any } ; const preflightMiddlewareFactory = new PreflightMiddlewareFactory ( logger ) ; const strictModelMiddlewareFactory = new StrictModelMiddlewareFactory ( logger , [ UnsupportedHeadersBlocker , UnsupportedParametersBlocker ] ) ; if ( this . enableAccessLog ) { app . use ( morgan ( "common" , { stream : this . accessLogWriteStream } ) ) ; } app . use ( createStorageBlobContextMiddleware ( this . skipApiVersionCheck , this . disableProductStyleUrl ) ) ; app . use ( middlewareFactory . createDispatchMiddleware ( ) ) ; if ( this . loose === false || this . loose === undefined ) { app . use ( strictModelMiddlewareFactory . createStrictModelMiddleware ( ) ) ; } const authenticationMiddlewareFactory = new AuthenticationMiddlewareFactory ( logger ) ; const authenticators = [ new PublicAccessAuthenticator ( this . metadataStore , logger ) , new BlobSharedKeyAuthenticator ( this . accountDataStore , logger ) , new AccountSASAuthenticator ( this . accountDataStore , this . metadataStore , logger ) , new BlobSASAuthenticator ( this . accountDataStore , this . metadataStore , logger ) ] ; if ( this . oauth !== undefined ) { authenticators . push ( new BlobTokenAuthenticator ( this . accountDataStore , this . oauth , logger ) ) ; } app . use ( authenticationMiddlewareFactory . createAuthenticationMiddleware ( authenticators ) ) ; app . use ( middlewareFactory . createDeserializerMiddleware ( ) ) ; app . use ( middlewareFactory . createHandlerMiddleware ( handlers ) ) ; app . use ( preflightMiddlewareFactory . createCorsRequestMiddleware ( this . metadataStore , true ) ) ; app . use ( preflightMiddlewareFactory . createCorsRequestMiddleware ( this . metadataStore , false ) ) ; app . use ( middlewareFactory . createSerializerMiddleware ( ) ) ; app . use ( preflightMiddlewareFactory . createOptionsHandlerMiddleware ( this . metadataStore ) ) ; app . use ( middlewareFactory . createErrorMiddleware ( ) ) ; app . use ( middlewareFactory . createEndMiddleware ( ) ) ; return app ; } }