@@ -121,50 +121,60 @@ async function getPackageJsonLicenseFromMaster(
121121 owner : string ,
122122 repo : string ,
123123) : Promise < string | undefined > {
124- // Spec: license from package.json on master HEAD commit, if present.
125- // Fallback: if 'master' isn't available, try 'main'.
126- const candidateBranches = [ 'master' , 'main' ] as const ;
124+ // Spec: Read license from package.json on master HEAD.
125+ // Only try 'main' if fetching from 'master' fails (e.g., 404).
127126
128- for ( const branch of candidateBranches ) {
127+ const extract = ( raw : string ) : string | undefined => {
129128 try {
130- const raw = await fetchText (
131- `https://raw.githubusercontent.com/${ owner } /${ repo } /${ branch } /package.json` ,
132- ) ;
133- try {
134- const pkg = JSON . parse ( raw ) as unknown ;
129+ const pkg = JSON . parse ( raw ) as unknown ;
130+ if (
131+ pkg &&
132+ typeof pkg === 'object' &&
133+ 'license' in ( pkg as Record < string , unknown > )
134+ ) {
135+ const lic = ( pkg as Record < string , unknown > ) . license ;
136+ if ( typeof lic === 'string' ) return lic ;
135137 if (
136- pkg &&
137- typeof pkg === 'object' &&
138- 'license ' in ( pkg as Record < string , unknown > )
138+ lic &&
139+ typeof lic === 'object' &&
140+ 'type ' in ( lic as Record < string , unknown > )
139141 ) {
140- const lic = ( pkg as Record < string , unknown > ) . license ;
141- if ( typeof lic === 'string' ) return lic ;
142- if (
143- lic &&
144- typeof lic === 'object' &&
145- 'type' in ( lic as Record < string , unknown > )
146- ) {
147- const typeVal = ( lic as Record < string , unknown > ) . type ;
148- if ( typeof typeVal === 'string' ) return typeVal ;
149- }
142+ const typeVal = ( lic as Record < string , unknown > ) . type ;
143+ if ( typeof typeVal === 'string' ) return typeVal ;
150144 }
151- // Support legacy `licenses` array
152- if ( pkg && typeof pkg === 'object' ) {
153- const anyPkg = pkg as Record < string , unknown > ;
154- const licensesRaw = anyPkg . licenses ;
155- if ( Array . isArray ( licensesRaw ) && licensesRaw . length > 0 ) {
156- const first = licensesRaw [ 0 ] as { type ?: unknown } ;
157- if ( typeof first . type === 'string' ) return first . type ;
158- }
145+ }
146+ // Support legacy `licenses` array
147+ if ( pkg && typeof pkg === 'object' ) {
148+ const anyPkg = pkg as Record < string , unknown > ;
149+ const licensesRaw = anyPkg . licenses ;
150+ if ( Array . isArray ( licensesRaw ) && licensesRaw . length > 0 ) {
151+ const first = licensesRaw [ 0 ] as { type ?: unknown } ;
152+ if ( typeof first . type === 'string' ) return first . type ;
159153 }
160- } catch {
161- // ignore JSON parsing errors and try next branch
162154 }
163155 } catch {
164- // package.json not found or inaccessible on this branch; try next
156+ // ignore JSON parsing errors; treat as no license
157+ }
158+ return undefined ;
159+ } ;
160+
161+ // Try master
162+ try {
163+ const raw = await fetchText (
164+ `https://raw.githubusercontent.com/${ owner } /${ repo } /master/package.json` ,
165+ ) ;
166+ return extract ( raw ) ;
167+ } catch {
168+ // If master not available, try main
169+ try {
170+ const raw = await fetchText (
171+ `https://raw.githubusercontent.com/${ owner } /${ repo } /main/package.json` ,
172+ ) ;
173+ return extract ( raw ) ;
174+ } catch {
175+ return undefined ;
165176 }
166177 }
167- return undefined ;
168178}
169179
170180async function getLastCommitDatetime (
0 commit comments