"use strict" ; import * as fs from 'fs' ; import * as fsp from 'path' ; const glob = typeof global === "object" ? global : window ; const secret = "_6522f20750bf404ea2fbccd561613115" ; const factories = ( glob [ secret ] = ( glob [ secret ] || { "console" : "./devices/console" , "http" : "./devices/http" , "https" : "./devices/http" , "file" : "./devices/file" , "string" : "./devices/string" , } ) ) ; export interface PackageFactory { protocol : string ; module : string ; } function scanDirs ( dir ) { function tryPackage ( pkgPath , fromDir ? ) { if ( ! fs . existsSync ( pkgPath ) ) return ; try { var pk = require ( pkgPath ) ; if ( pk && pk . ez && pk . ez . factories ) { pk . ez . factories . forEach ( ( crt ) => { if ( crt . protocol && crt . module ) { factories [ crt . protocol ] = fromDir ? crt . module . replace ( /^.*([\\\/])/ , fromDir + '$1' ) : crt . module ; } } ) ; } } catch ( e ) { console . error ( e . message ) ; } } ; const ndir = fsp . join ( dir , "../node_modules" ) ; if ( fs . existsSync ( ndir ) ) { fs . readdirSync ( ndir ) . forEach ( ( pkg ) => { tryPackage ( fsp . join ( ndir , pkg , "package.json" ) ) ; } ) ; } const d = fsp . join ( dir , '..' ) ; tryPackage ( fsp . join ( d , "package.json" ) , d ) ; if ( d . length < dir . length ) scanDirs ( d ) ; } scanDirs ( __dirname ) ; export default function ( url ) { const parts = ( url || "" ) . split ( ":" ) ; if ( parts . length < 2 ) throw new Error ( "invalid URL: " + url ) ; const pp = parts [ 0 ] ; if ( ! pp ) throw new Error ( "Missing protocol in url: " + url ) ; if ( ! factories [ pp ] ) throw new Error ( "Missing factory for protocol " + pp ) ; return require ( factories [ pp ] ) . factory ( url ) ; }