2022-10-02 09:34:34 +02:00
import Logger from "common/logger" ;
import WebpackModules , { Filters } from "../webpackmodules" ;
2023-01-26 00:00:16 +01:00
const getOptions = ( args , defaultOptions = { } ) => {
if ( args . length > 1 &&
typeof ( args [ args . length - 1 ] ) === "object" &&
! Array . isArray ( args [ args . length - 1 ] ) &&
args [ args . length - 1 ] !== null
) {
Object . assign ( defaultOptions , args . pop ( ) ) ;
}
return defaultOptions ;
} ;
2022-10-02 09:34:34 +02:00
/ * *
* ` Webpack ` is a utility class for getting internal webpack modules . Instance is accessible through the { @ link BdApi } .
* This is extremely useful for interacting with the internals of Discord .
* @ type Webpack
* @ summary { @ link Webpack } is a utility class for getting internal webpack modules .
* @ name Webpack
* /
const Webpack = {
2022-12-29 23:27:52 +01:00
/ * *
* A Proxy that returns the module source by ID .
* /
modules : WebpackModules . modules ,
2022-10-02 09:34:34 +02:00
/ * *
* Series of { @ link Filters } to be used for finding webpack modules .
* @ type Filters
* @ memberof Webpack
* /
Filters : {
2023-05-24 23:00:42 +02:00
/ * *
* @ deprecated
* /
byProps ( ... props ) { return Filters . byKeys ( props ) ; } ,
2022-10-02 09:34:34 +02:00
/ * *
* Generates a function that filters by a set of properties .
2023-05-24 23:00:42 +02:00
* @ param { ... string } keys List of property names
2022-10-02 09:34:34 +02:00
* @ returns { function } A filter that checks for a set of properties
* /
2023-05-24 23:00:42 +02:00
byKeys ( ... keys ) { return Filters . byKeys ( keys ) ; } ,
2022-10-02 09:34:34 +02:00
/ * *
* Generates a function that filters by a set of properties on the object ' s prototype .
* @ param { ... string } props List of property names
* @ returns { function } A filter that checks for a set of properties on the object ' s prototype .
* /
byPrototypeFields ( ... props ) { return Filters . byPrototypeFields ( props ) ; } ,
/ * *
* Generates a function that filters by a regex .
* @ param { RegExp } search A RegExp to check on the module
* @ param { function } filter Additional filter
* @ returns { function } A filter that checks for a regex match
* /
byRegex ( regex ) { return Filters . byRegex ( regex ) ; } ,
/ * *
* Generates a function that filters by strings .
* @ param { ... String } strings A list of strings
* @ returns { function } A filter that checks for a set of strings
* /
byStrings ( ... strings ) { return Filters . byStrings ( ... strings ) ; } ,
/ * *
2022-10-10 20:33:36 +02:00
* Generates a function that filters by the ` displayName ` property .
2022-10-02 09:34:34 +02:00
* @ param { string } name Name the module should have
2022-10-10 20:33:36 +02:00
* @ returns { function } A filter that checks for a ` displayName ` match
2022-10-02 09:34:34 +02:00
* /
byDisplayName ( name ) { return Filters . byDisplayName ( name ) ; } ,
/ * *
* Generates a combined function from a list of filters .
* @ param { ... function } filters A list of filters
* @ returns { function } Combinatory filter of all arguments
* /
combine ( ... filters ) { return Filters . combine ( ... filters ) ; } ,
} ,
2022-12-29 23:27:52 +01:00
/ * *
* Searches for a module by value , returns module & matched key . Useful in combination with the Patcher .
* @ param { ( value : any , index : number , array : any [ ] ) => boolean } filter A function to use to filter the module
* @ param { object } [ options ] Set of options to customize the search
* @ param { any } [ options . target = null ] Optional module target to look inside .
* @ param { Boolean } [ options . defaultExport = true ] Whether to return default export when matching the default export
* @ param { Boolean } [ options . searchExports = false ] Whether to execute the filter on webpack export getters .
* @ return { [ Any , string ] }
* /
2023-05-24 23:00:42 +02:00
getWithKey ( filter , options = { } ) {
if ( ( "first" in options ) ) return Logger . error ( "BdApi.Webpack~getWithKey" , "Unsupported option first." ) ;
if ( ( "defaultExport" in options ) && typeof ( options . defaultExport ) !== "boolean" ) return Logger . error ( "BdApi.Webpack~getWithKey" , "Unsupported type used for options.defaultExport" , options . defaultExport , "boolean expected." ) ;
if ( ( "searchExports" in options ) && typeof ( options . searchExports ) !== "boolean" ) return Logger . error ( "BdApi.Webpack~getWithKey" , "Unsupported type used for options.searchExports" , options . searchExports , "boolean expected." ) ;
return WebpackModules . getWithKey ( filter , options ) ;
2022-12-29 23:27:52 +01:00
} ,
2022-10-02 09:34:34 +02:00
/ * *
* Finds a module using a filter function .
* @ memberof Webpack
2022-10-10 20:33:36 +02:00
* @ param { function } filter A function to use to filter modules . It is given exports , module , and moduleID . Return ` true ` to signify match .
* @ param { object } [ options ] Options to configure the search
2022-10-02 09:34:34 +02:00
* @ param { Boolean } [ options . first = true ] Whether to return only the first matching module
* @ param { Boolean } [ options . defaultExport = true ] Whether to return default export when matching the default export
2022-10-10 20:33:36 +02:00
* @ param { Boolean } [ options . searchExports = false ] Whether to execute the filter on webpack exports
2022-10-02 09:34:34 +02:00
* @ return { any }
* /
2023-05-24 23:00:42 +02:00
get ( filter , options = { } ) {
if ( ( "first" in options ) && typeof ( options . first ) !== "boolean" ) return Logger . error ( "BdApi.Webpack~get" , "Unsupported type used for options.first" , options . first , "boolean expected." ) ;
if ( ( "defaultExport" in options ) && typeof ( options . defaultExport ) !== "boolean" ) return Logger . error ( "BdApi.Webpack~get" , "Unsupported type used for options.defaultExport" , options . defaultExport , "boolean expected." ) ;
if ( ( "searchExports" in options ) && typeof ( options . searchExports ) !== "boolean" ) return Logger . error ( "BdApi.Webpack~get" , "Unsupported type used for options.searchExports" , options . searchExports , "boolean expected." ) ;
2022-10-02 09:34:34 +02:00
return WebpackModules . getModule ( filter , options ) ;
} ,
2023-05-24 23:00:42 +02:00
/ * *
* @ deprecated
* /
getModule ( ) { return this . get . apply ( this , arguments ) ; } ,
2022-10-02 09:34:34 +02:00
/ * *
* Finds multiple modules using multiple filters .
* @ memberof Webpack
2022-10-10 20:33:36 +02:00
* @ param { ... object } queries Object representing the query to perform
2022-10-02 09:34:34 +02:00
* @ param { Function } queries . filter A function to use to filter modules
* @ param { Boolean } [ queries . first = true ] Whether to return only the first matching module
* @ param { Boolean } [ queries . defaultExport = true ] Whether to return default export when matching the default export
2022-10-10 20:33:36 +02:00
* @ param { Boolean } [ queries . searchExports = false ] Whether to execute the filter on webpack exports
2022-10-02 09:34:34 +02:00
* @ return { any }
* /
getBulk ( ... queries ) { return WebpackModules . getBulk ( ... queries ) ; } ,
/ * *
2022-10-10 20:33:36 +02:00
* Finds a module that is lazily loaded .
2022-10-02 09:34:34 +02:00
* @ memberof Webpack
2022-10-10 20:33:36 +02:00
* @ param { function } filter A function to use to filter modules . It is given exports . Return ` true ` to signify match .
* @ param { object } [ options ] Options for configuring the listener
2022-10-02 09:34:34 +02:00
* @ param { AbortSignal } [ options . signal ] AbortSignal of an AbortController to cancel the promise
* @ param { Boolean } [ options . defaultExport = true ] Whether to return default export when matching the default export
2022-10-10 20:33:36 +02:00
* @ param { Boolean } [ options . searchExports = false ] Whether to execute the filter on webpack exports
2022-10-02 09:34:34 +02:00
* @ returns { Promise < any > }
* /
waitForModule ( filter , options = { } ) {
if ( ( "defaultExport" in options ) && typeof ( options . defaultExport ) !== "boolean" ) return Logger . error ( "BdApi.Webpack~waitForModule" , "Unsupported type used for options.defaultExport" , options . defaultExport , "boolean expected." ) ;
if ( ( "signal" in options ) && ! ( options . signal instanceof AbortSignal ) ) return Logger . error ( "BdApi.Webpack~waitForModule" , "Unsupported type used for options.signal" , options . signal , "AbortSignal expected." ) ;
2023-05-24 23:00:42 +02:00
if ( ( "searchExports" in options ) && typeof ( options . searchExports ) !== "boolean" ) return Logger . error ( "BdApi.Webpack~waitForModule" , "Unsupported type used for options.searchExports" , options . searchExports , "boolean expected." ) ;
2022-10-02 09:34:34 +02:00
return WebpackModules . getLazy ( filter , options ) ;
} ,
2022-12-29 23:27:52 +01:00
/ * *
* Finds all modules matching a filter function .
* @ param { Function } filter A function to use to filter modules
* /
getModules ( filter ) { return WebpackModules . getModule ( filter , { first : false } ) ; } ,
/ * *
* Finds a module using its code .
* @ param { RegEx } regex A regular expression to use to filter modules
* @ param { object } [ options ] Options to configure the search
* @ param { Boolean } [ options . defaultExport = true ] Whether to return default export when matching the default export
* @ param { Boolean } [ options . searchExports = false ] Whether to execute the filter on webpack exports
* @ return { Any }
* /
getByRegex ( regex , options = { } ) {
return WebpackModules . getModule ( Filters . byRegex ( regex ) , options ) ;
} ,
/ * *
2022-12-31 17:44:01 +01:00
* Finds all modules using its code .
2022-12-29 23:27:52 +01:00
* @ param { RegEx } regex A regular expression to use to filter modules
* @ param { object } [ options ] Options to configure the search
* @ param { Boolean } [ options . defaultExport = true ] Whether to return default export when matching the default export
* @ param { Boolean } [ options . searchExports = false ] Whether to execute the filter on webpack exports
* @ return { Any [ ] }
* /
getAllByRegex ( regex , options = { } ) {
2022-12-31 17:44:01 +01:00
return WebpackModules . getModule ( Filters . byRegex ( regex ) , Object . assign ( { } , options , { first : true } ) ) ;
2022-12-29 23:27:52 +01:00
} ,
/ * *
* Finds a single module using properties on its prototype .
* @ param { ... string } prototypes Properties to use to filter modules
* @ return { Any }
* /
getByPrototypes ( ... prototypes ) {
2023-01-26 00:00:16 +01:00
const options = getOptions ( prototypes ) ;
return WebpackModules . getModule ( Filters . byPrototypeFields ( prototypes ) , options ) ;
2022-12-29 23:27:52 +01:00
} ,
/ * *
* Finds all modules with a set of properties of its prototype .
* @ param { ... string } prototypes Properties to use to filter modules
* @ return { Any [ ] }
* /
2023-05-24 23:00:42 +02:00
getAllByPrototypeFields ( ... prototypes ) {
2023-01-26 00:00:16 +01:00
const options = getOptions ( prototypes , { first : false } ) ;
return WebpackModules . getModule ( Filters . byPrototypeFields ( prototypes ) , options ) ;
2022-12-29 23:27:52 +01:00
} ,
/ * *
* Finds a single module using its own properties .
* @ param { ... string } props Properties to use to filter modules
* @ return { Any }
* /
2023-05-24 23:00:42 +02:00
getByKeys ( ... props ) {
2023-01-26 00:00:16 +01:00
const options = getOptions ( props ) ;
2023-05-24 23:00:42 +02:00
return WebpackModules . getModule ( Filters . byKeys ( props ) , options ) ;
2022-12-29 23:27:52 +01:00
} ,
/ * *
* Finds all modules with a set of properties .
* @ param { ... string } props Properties to use to filter modules
* @ return { Any [ ] }
* /
2023-05-24 23:00:42 +02:00
getAllByKeys ( ... props ) {
2023-01-26 00:00:16 +01:00
const options = getOptions ( props , { first : false } ) ;
2023-05-24 23:00:42 +02:00
return WebpackModules . getModule ( Filters . byKeys ( props ) , options ) ;
2022-12-29 23:27:52 +01:00
} ,
/ * *
* Finds a single module using a set of strings .
* @ param { ... String } props Strings to use to filter modules
* @ return { Any }
* /
getByString ( ... strings ) {
2023-01-26 00:00:16 +01:00
const options = getOptions ( strings ) ;
return WebpackModules . getModule ( Filters . byStrings ( ... strings ) , options ) ;
2022-12-29 23:27:52 +01:00
} ,
/ * *
* Finds all modules with a set of strings .
* @ param { ... String } strings Strings to use to filter modules
* @ return { Any [ ] }
* /
getAllByString ( ... strings ) {
2023-01-26 00:00:16 +01:00
const options = getOptions ( strings , { first : false } ) ;
return WebpackModules . getModule ( Filters . byStrings ( ... strings ) , options ) ;
2022-12-29 23:27:52 +01:00
} ,
2022-10-02 09:34:34 +02:00
} ;
Object . freeze ( Webpack ) ;
Object . freeze ( Webpack . Filters ) ;
2022-12-29 23:27:52 +01:00
export default Webpack ;