import * as fs from "fs" ; import * as path from "path" ; import * as url from "url" ; import { IncomingMessage , ServerResponse } from "http" ; import { validate } from "jsonschema" ; import { ClientOptions , ConfigOptions , FilePattern , Injectable } from "karma" ; import { Viewport , ViewportConfiguration } from "./adapter/viewport" ; import * as schema from "./config/schema.json" ; /**
 * Augment Karma configuration type
 *
 * This is the exported configuration type for usage within Karma, because the
 * context selector and breakpoints are optional (merged with defaults).
 */ declare module "karma" { interface ConfigOptions { viewport ? : Partial < ViewportConfiguration > ; } } /**
 * Declare ambient viewport instance
 */ declare global { const viewport ; } param file param included return const pattern = ( file , included = true ) : FilePattern => ( { pattern : file , included , served : true , watched : false } ) ; param config const framework = ( config ) => { config . files ! . push ( pattern ( path . resolve ( __dirname , "config/default.json" ) ) , pattern ( path . resolve ( __dirname , "adapter/index.js" ) ) , pattern ( path . resolve ( __dirname , "adapter/index.js.map" ) , false ) ) ; config . beforeMiddleware = config . beforeMiddleware || [ ] ; config . beforeMiddleware . push ( "viewport" ) ; config . preprocessors = config . preprocessors || { } ; config . preprocessors [ path . resolve ( __dirname , "config/default.json" ) ] = [ "viewport" ] ; } ; framework . $inject = [ "config" ] ; return const middleware = ( ) => ( req , res , next ) => { const uri = url . parse ( req . url ! , true ) ; if ( uri . pathname !== "/debug.html" || typeof uri . query . embed !== "undefined" ) return next ( ) ; const debug = path . resolve ( __dirname , "static/debug.html" ) ; fs . readFile ( debug , ( err , data ) => { if ( err ) return next ( err ) ; res . writeHead ( 200 , { "Content-Type" : "text/html" } ) ; res . end ( data . toString ( ) . replace ( "%X_UA_COMPATIBLE%" , '<meta http-equiv="X-UA-Compatible" content="' + uri . query [ "x-ua-compatible" ] + '" />' ) , "utf-8" ) ; } ) ; } ; param viewport param client return const preprocessor = ( viewport , client ) => { if ( viewport && typeof viewport !== "object" ) throw new TypeError ( `Invalid viewport configuration: ${ viewport }` ) ; type DoneFn = ( result : string ) => void ; return ( content , file , done ) => { const config = Object . assign ( JSON . parse ( content ) , viewport ) ; const result = validate ( config , schema ) ; if ( result . errors . length ) throw new TypeError ( `Invalid viewport configuration: ${ result . errors [ 0 ] . stack }` ) ; if ( config . context === "#context" && ! client . useIframe ) throw new Error ( "Invalid configuration: client.useIframe " + "must be set to true or a different context selector must be given" ) ; file . path += ".js" ; done ( `window.__viewport__ = ${ JSON . stringify ( config , undefined , 2 ) }` ) ; } ; } ; preprocessor . $inject = [ "config.viewport" , "config.client" ] ; module . exports = { "framework:viewport" : [ "factory" , framework ] , "middleware:viewport" : [ "factory" , middleware ] , "preprocessor:viewport" : [ "factory" , preprocessor ] } ;