import through2 from "through2" ; import defaultsDeep from "lodash.defaultsdeep" ; import File from "vinyl" ; import { FaviconOptions , defaultOptions } from "./config/defaults.js" ; import { RawImage , sourceImages } from "./helpers.js" ; import { getPlatform } from "./platforms/index.js" ; import { dummyLog , Logger , prettyLog } from "./logger.js" ; export interface FaviconImage { readonly name : string ; readonly contents : Buffer | RawImage ; } export interface FaviconFile { readonly name : string ; readonly contents : string ; } export const config = { defaults : defaultOptions , } ; export type FaviconHtmlElement = string ; export interface FaviconResponse { readonly images : FaviconImage [ ] ; readonly files : FaviconFile [ ] ; readonly html : FaviconHtmlElement [ ] ; } async function createFavicons ( source , options ) { options = defaultsDeep ( options , defaultOptions ) ; const log = options . logging ? prettyLog : dummyLog ; log ( "General:source" , `Source type is ${ typeof source }` ) ; const sourceset = await sourceImages ( source ) ; const platforms = Object . keys ( options . icons ) . filter ( ( platform ) => options . icons [ platform ] ) . sort ( ( a , b ) => { if ( a === "favicons" ) return - 1 ; if ( b === "favicons" ) return 1 ; return a . localeCompare ( b ) ; } ) ; const responses = [ ] ; for ( const platformName of platforms ) { const platform = getPlatform ( platformName , options , log ) ; responses . push ( await platform . create ( sourceset ) ) ; } return { images : responses . flatMap ( ( r ) => r . images ) , files : responses . flatMap ( ( r ) => r . files ) , html : responses . flatMap ( ( r ) => r . html ) , } ; } typedef { ( error : Error | null , response : FaviconResponse ) => any } FaviconCallback param { string | string [ ] | Buffer | Buffer [ ] } source param { Partial < FaviconOptions > | undefined } options param { FaviconCallback | undefined } next returns { Promise | Promise < FaviconResponse > } export function favicons ( source , options = { } , next = undefined ) { if ( next ) { return favicons ( source , options ) . then ( ( response ) => next ( null , response ) ) . catch ( next ) ; } return createFavicons ( source , options ) ; } export default favicons ; export function stream ( params , handleHtml ) { return through2 . obj ( function ( file , encoding , callback ) { if ( file . isNull ( ) ) { return callback ( null , file ) ; } if ( file . isStream ( ) ) { return callback ( new Error ( "Streaming not supported" ) ) ; } const { html : path , pipeHTML , ... options } = params ; favicons ( file . contents , options ) . then ( ( { images , files , html } ) => { for ( const asset of [ ... images , ... files ] ) { this . push ( new File ( { path : asset . name , contents : Buffer . isBuffer ( asset . contents ) ? asset . contents : Buffer . from ( asset . contents ) , } ) ) ; } if ( handleHtml ) { handleHtml ( html ) ; } if ( pipeHTML ) { this . push ( new File ( { path , contents : Buffer . from ( html . join ( "\n" ) ) , } ) ) ; } callback ( null ) ; } ) . catch ( callback ) ; } ) ; }