import type { RawPkg } from '../@types/pkg' ; import { config } from '../config' ; import type { File } from '../jsDelivr' ; import { datadog } from '../utils/datadog' ; import { log } from '../utils/log' ; import { request } from '../utils/request' ; interface TypeList { p : string ; l : string ; t : string ; d : number ; g : string [ ] ; m : string [ ] ; } export const typesCache = { } ; /**
 * Microsoft build a index.json with all @types/* on each publication.
 * - https://github.com/microsoft/types-publisher/blob/master/src/create-search-index.ts.
 */ export async function loadTypesIndex ( ) { const start = Date . now ( ) ; const { body } = await request < TypeList [ ] > ( config . typescriptTypesIndex , { decompress : true , responseType : 'json' , } ) ; log . info ( `📦  Typescript preload, found ${ body . length } @types` ) ; body . forEach ( ( type ) => { typesCache [ unmangle ( type . t ) ] = type . t ; } ) ; datadog . timing ( 'typescript.loadTypesIndex' , Date . now ( ) - start ) ; } export function isDefinitelyTyped ( { name } ) { return typesCache [ unmangle ( name ) ] ; } export function unmangle ( name ) { return name . replace ( '__' , '/' ) . replace ( '@' , '' ) ; } /**
 * Basically either
 *   - { types: { ts: false }}  for no existing TypeScript support
 *   - { types: { ts: "@types/module" }} - for definitely typed support
 *   - { types: { ts: "included" }} - for types shipped with the module.
 */ export function getTypeScriptSupport ( pkg , filelist ) { const start = Date . now ( ) ; try { if ( pkg . types . ts === 'included' ) { return { types : pkg . types } ; } const defTyped = isDefinitelyTyped ( { name : pkg . name } ) ; if ( defTyped ) { return { types : { ts : 'definitely-typed' , definitelyTyped : `@types/${ defTyped }` , } , } ; } for ( const file of filelist ) { if ( ! file . name . endsWith ( '.d.ts' ) ) { continue ; } datadog . increment ( 'jsdelivr.getTSSupport.hit' ) ; return { types : { ts : 'included' } } ; } datadog . increment ( 'jsdelivr.getTSSupport.miss' ) ; return { types : { ts : false } } ; } finally { datadog . timing ( 'typescript.getSupport' , Date . now ( ) - start ) ; } } /**
 * Check if packages have Typescript definitions.
 */ export async function getTSSupport ( pkgs , filelists ) { const start = Date . now ( ) ; const all = await Promise . all ( pkgs . map ( ( pkg , index ) => { return getTypeScriptSupport ( pkg , filelists [ index ] || [ ] ) ; } ) ) ; datadog . timing ( 'getTSSupport' , Date . now ( ) - start ) ; return all ; }