File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { Request , Response } from "express" ;
2- import { seconds } from "../../utils/misc.js" ;
32import { BROWSERS , DEFAULT_BROWSERS } from "./lib/constants.js" ;
43import { Data , getData } from "./lib/index.js" ;
54
@@ -11,16 +10,18 @@ export default async function route(req: IRequest, res: Response) {
1110 const { feature } = req . params ;
1211 const browsers = normalizeBrowsers ( req . query . browsers ) ;
1312
14-
1513 try {
1614 const data = await getData ( feature ) ;
1715 if ( data === null ) {
18- res . sendStatus ( 404 ) ;
16+ const hint = feature . startsWith ( "wf-" ) && feature . length > 3
17+ ? ` The "wf-" prefix indicates a web-features ID. No matching caniuse data was found for "${ feature } " or "${ feature . slice ( 3 ) } ".`
18+ : "" ;
19+ res . status ( 404 ) . json ( { error : `Feature "${ feature } " not found.${ hint } ` } ) ;
1920 return ;
2021 }
2122 const result = [ ] ;
2223 for ( const browser of browsers ) {
23- result . push ( { browser, ...getBrowserData ( data ? .all [ browser ] ) } ) ;
24+ result . push ( { browser, ...getBrowserData ( data . all [ browser ] ) } ) ;
2425 }
2526 res . json ( { result } ) ;
2627 } catch ( error ) {
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import {
1515export { Data } ;
1616
1717const DATA_DIR = env ( "DATA_DIR" ) ;
18+ const CANIUSE_DIR = path . join ( DATA_DIR , "caniuse" ) ;
1819
1920interface Options {
2021 feature : string ;
@@ -106,11 +107,22 @@ function sanitizeBrowsersList(browsers?: string | string[]) {
106107}
107108
108109export async function getData ( feature : string ) {
110+ // Try the feature as-is first, then fall back to stripping the "wf-" prefix.
111+ // E.g., "wf-css-grid" falls back to looking up "css-grid" in caniuse data.
112+ const data = await readFeatureFile ( feature ) ;
113+ if ( data ) return data ;
114+ if ( feature . startsWith ( "wf-" ) && feature . length > 3 ) {
115+ return readFeatureFile ( feature . slice ( 3 ) ) ;
116+ }
117+ return null ;
118+ }
119+
120+ async function readFeatureFile ( feature : string ) {
109121 if ( cache . has ( feature ) ) {
110122 return cache . get ( feature ) as Data ;
111123 }
112124 const file = path . format ( {
113- dir : path . join ( DATA_DIR , "caniuse" ) ,
125+ dir : CANIUSE_DIR ,
114126 name : `${ feature } .json` ,
115127 } ) ;
116128
@@ -120,7 +132,10 @@ export async function getData(feature: string) {
120132 cache . set ( feature , data ) ;
121133 return data ;
122134 } catch ( error ) {
123- console . error ( error ) ;
135+ const code = ( error as NodeJS . ErrnoException ) . code ;
136+ if ( code !== "ENOENT" ) {
137+ console . error ( error ) ;
138+ }
124139 return null ;
125140 }
126141}
You can’t perform that action at this time.
0 commit comments