import { OperationSpec } from "@azure/ms-rest-js/es/lib/operationSpec" ; import express from "express" ; import { RequestListener } from "http" ; import IAccountDataStore from "../common/IAccountDataStore" ; import IRequestListenerFactory from "../common/IRequestListenerFactory" ; import logger from "../common/Logger" ; import AccountSASAuthenticator from "./authentication/AccountSASAuthenticator" ; import IAuthenticator from "./authentication/IAuthenticator" ; import TableSASAuthenticator from "./authentication/TableSASAuthenticator" ; import TableTokenAuthenticator from "./authentication/TableTokenAuthenticator" ; import { TableQueryResponse } from "./generated/artifacts/mappers" ; import { Operation } from "./generated/artifacts/operation" ; import Specifications from "./generated/artifacts/specifications" ; import ExpressMiddlewareFactory from "./generated/ExpressMiddlewareFactory" ; import IHandlers from "./generated/handlers/IHandlers" ; import MiddlewareFactory from "./generated/MiddlewareFactory" ; import ServiceHandler from "./handlers/ServiceHandler" ; import TableHandler from "./handlers/TableHandler" ; import AuthenticationMiddlewareFactory from "./middleware/AuthenticationMiddlewareFactory" ; import createTableStorageContextMiddleware from "./middleware/tableStorageContext.middleware" ; import ITableMetadataStore from "./persistence/ITableMetadataStore" ; import { DEFAULT_TABLE_CONTEXT_PATH } from "./utils/constants" ; import PreflightMiddlewareFactory from "./middleware/PreflightMiddlewareFactory" ; import morgan = require ( "morgan" ) ; import { OAuthLevel } from "../common/models" ; import TableSharedKeyAuthenticator from "./authentication/TableSharedKeyAuthenticator" ; import TableSharedKeyLiteAuthenticator from "./authentication/TableSharedKeyLiteAuthenticator" ; export class implements IRequestListenerFactory export default class TableRequestListenerFactory implements IRequestListenerFactory { public constructor ( private readonly metadataStore , private readonly accountDataStore , private readonly enableAccessLog , private readonly accessLogWriteStream ? , private readonly skipApiVersionCheck ? , private readonly oauth ? , private readonly disableProductStyleUrl ? ) { } public createRequestListener ( ) { type MutableSpecification = { - readonly [ K in keyof OperationSpec ] : OperationSpec [ K ] ; } ; [ Operation . Table_Create , Operation . Table_Query , Operation . Table_Delete , Operation . Table_QueryEntities , Operation . Table_QueryEntitiesWithPartitionAndRowKey , Operation . Table_UpdateEntity , Operation . Table_MergeEntity , Operation . Table_DeleteEntity , Operation . Table_InsertEntity ] . forEach ( ( operation ) => { ( Specifications [ operation ] as MutableSpecification ) . isXML = false ; } ) ; Object . defineProperty ( Specifications [ Operation . Table_MergeEntityWithMerge ] , "httpMethod" , { value : "MERGE" , writable : false } ) ; TableQueryResponse . type . modelProperties ! . value . xmlElementName = "value" ; const app = express ( ) . disable ( "x-powered-by" ) ; const middlewareFactory = new ExpressMiddlewareFactory ( logger , DEFAULT_TABLE_CONTEXT_PATH ) ; const handlers = { tableHandler : new TableHandler ( this . metadataStore , logger ) , serviceHandler : new ServiceHandler ( this . metadataStore , logger ) } ; if ( this . enableAccessLog ) { app . use ( morgan ( "common" , { stream : this . accessLogWriteStream } ) ) ; } app . use ( createTableStorageContextMiddleware ( this . skipApiVersionCheck , this . disableProductStyleUrl ) ) ; app . use ( middlewareFactory . createDispatchMiddleware ( ) ) ; const authenticationMiddlewareFactory = new AuthenticationMiddlewareFactory ( logger ) ; const authenticators = [ new TableSharedKeyLiteAuthenticator ( this . accountDataStore , logger ) , new TableSharedKeyAuthenticator ( this . accountDataStore , logger ) , new AccountSASAuthenticator ( this . accountDataStore , logger ) , new TableSASAuthenticator ( this . accountDataStore , this . metadataStore , logger ) ] ; if ( this . oauth !== undefined ) { authenticators . push ( new TableTokenAuthenticator ( this . accountDataStore , this . oauth , logger ) ) ; } app . use ( authenticationMiddlewareFactory . createAuthenticationMiddleware ( authenticators ) ) ; app . use ( middlewareFactory . createDeserializerMiddleware ( ) ) ; app . use ( middlewareFactory . createHandlerMiddleware ( handlers ) ) ; const preflightMiddlewareFactory = new PreflightMiddlewareFactory ( logger ) ; 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 ; } }