11import * as Clock from "effect/Clock" ;
22import * as Context from "effect/Context" ;
3+ import * as Data from "effect/Data" ;
34import * as Duration from "effect/Duration" ;
45import * as Effect from "effect/Effect" ;
56import * as Layer from "effect/Layer" ;
@@ -200,25 +201,6 @@ function retryAtFrom(
200201 return Number . isFinite ( reset ) && reset > now ? reset : undefined ;
201202}
202203
203- function isRateLimited (
204- status : number ,
205- headers : Readonly < Record < string , string | undefined > > ,
206- body : string ,
207- ) {
208- return (
209- status === 429 ||
210- ( status === 403 &&
211- ( headers [ "x-ratelimit-remaining" ] === "0" ||
212- headers [ "retry-after" ] !== undefined ||
213- / r a t e l i m i t / i. test ( body ) ) )
214- ) ;
215- }
216-
217- interface GraphQlErrors {
218- readonly messages : ReadonlyArray < string > ;
219- readonly types : ReadonlyArray < string > ;
220- }
221-
222204const decodeGraphQlErrors = Schema . decodeUnknownOption (
223205 Schema . fromJsonString (
224206 Schema . Struct ( {
@@ -232,14 +214,53 @@ const decodeGraphQlErrors = Schema.decodeUnknownOption(
232214 ) ,
233215) ;
234216
235- function graphqlErrors ( body : string ) : GraphQlErrors | null {
236- return Option . match ( decodeGraphQlErrors ( body ) , {
237- onNone : ( ) => null ,
238- onSome : ( { errors } ) => ( {
239- messages : errors . flatMap ( ( error ) => ( error . message === undefined ? [ ] : [ error . message ] ) ) ,
240- types : errors . flatMap ( ( error ) => ( error . type === undefined ? [ ] : [ error . type ] ) ) ,
241- } ) ,
242- } ) ;
217+ /** What one GitHub answer means, decided once from its status, headers and body. */
218+ type Answer = Data . TaggedEnum < {
219+ Ok : { } ;
220+ RateLimited : { } ;
221+ Unauthorized : { } ;
222+ NotFound : { } ;
223+ Failed : { readonly messages : ReadonlyArray < string > | undefined } ;
224+ } > ;
225+ const Answer = Data . taggedEnum < Answer > ( ) ;
226+
227+ /**
228+ * GitHub reports a failed GraphQL document with HTTP 200 and an `errors` list, so a GraphQL body
229+ * is read for its error types as well as its status. `gh api graphql` failed those too, and
230+ * callers rely on that to fall back to narrower reads.
231+ */
232+ function classify ( input : {
233+ readonly status : number ;
234+ readonly headers : Readonly < Record < string , string | undefined > > ;
235+ readonly body : string ;
236+ readonly graphql : boolean ;
237+ readonly acceptNotModified : boolean ;
238+ } ) : Answer {
239+ const { status, headers, body } = input ;
240+ const errors = input . graphql
241+ ? Option . getOrUndefined ( decodeGraphQlErrors ( body ) ) ?. errors
242+ : undefined ;
243+ const types = errors ?. flatMap ( ( error ) => ( error . type === undefined ? [ ] : [ error . type ] ) ) ?? [ ] ;
244+ const messages = errors ?. flatMap ( ( error ) => ( error . message === undefined ? [ ] : [ error . message ] ) ) ;
245+ if (
246+ status === 429 ||
247+ types . includes ( "RATE_LIMITED" ) ||
248+ ( status === 403 &&
249+ ( headers [ "x-ratelimit-remaining" ] === "0" ||
250+ headers [ "retry-after" ] !== undefined ||
251+ / r a t e l i m i t / i. test ( body ) ) )
252+ ) {
253+ return Answer . RateLimited ( ) ;
254+ }
255+ if ( status === 401 ) return Answer . Unauthorized ( ) ;
256+ if ( status === 404 || ( types . length > 0 && types . every ( ( type ) => type === "NOT_FOUND" ) ) ) {
257+ return Answer . NotFound ( ) ;
258+ }
259+ if ( errors !== undefined ) return Answer . Failed ( { messages } ) ;
260+ if ( ( status >= 200 && status < 300 ) || ( status === 304 && input . acceptNotModified ) ) {
261+ return Answer . Ok ( ) ;
262+ }
263+ return Answer . Failed ( { messages : undefined } ) ;
243264}
244265
245266/** @public Service construction is part of the canonical Effect module API. */
@@ -280,7 +301,7 @@ export const make = Effect.gen(function* () {
280301 readonly maxResponseBytes : number ;
281302 readonly allowReserve : boolean ;
282303 readonly acceptNotModified : boolean ;
283- /** GitHub answers a throttled GraphQL document with HTTP 200 and a `RATE_LIMITED` error . */
304+ /** Reads the body for GraphQL `errors`, which GitHub sends with HTTP 200 . */
284305 readonly graphql ?: boolean ;
285306 } ) {
286307 const host = normalizeHost ( input . host ) ;
@@ -323,41 +344,52 @@ export const make = Effect.gen(function* () {
323344 ) ;
324345 const headers = response . headers ;
325346 const status = response . status ;
326- const throttled =
327- input . graphql === true &&
328- status === 200 &&
329- ( graphqlErrors ( collected . text ) ?. types . includes ( "RATE_LIMITED" ) ?? false ) ;
330- if (
331- ! throttled &&
332- ( ( status >= 200 && status < 300 ) || ( status === 304 && input . acceptNotModified ) )
333- ) {
334- yield * limits . recordSuccess ( { ...key , lease } ) ;
335- return { status, headers, body : collected . text , truncated : collected . truncated } ;
336- }
337- if ( throttled || isRateLimited ( status , headers , collected . text ) ) {
338- const retryAt = retryAtFrom ( headers , yield * Clock . currentTimeMillis ) ;
339- yield * limits . recordRateLimit ( { ...key , lease, retryAt } ) ;
340- return yield * new GitHubApiRateLimitError ( {
341- host,
342- operation : input . operation ,
343- ...( retryAt === undefined ? { } : { retryAt } ) ,
344- } ) ;
345- }
346- if ( status === 401 ) {
347- // The source may hold a newer token than the one that was refused.
348- yield * credentials . invalidate ( host ) ;
349- return yield * new GitHubApiAuthenticationError ( { host, operation : input . operation } ) ;
350- }
351- if ( status === 404 ) {
352- return yield * new GitHubApiNotFoundError ( { host, operation : input . operation } ) ;
353- }
354- const errors = graphqlErrors ( collected . text ) ;
355- return yield * new GitHubApiResponseError ( {
356- host,
357- operation : input . operation ,
358- status,
359- ...( errors === null ? { } : { graphqlErrors : errors . messages } ) ,
360- } ) ;
347+ const context = { host, operation : input . operation } ;
348+ return yield * Answer . $match (
349+ classify ( {
350+ status,
351+ headers,
352+ body : collected . text ,
353+ graphql : input . graphql === true ,
354+ acceptNotModified : input . acceptNotModified ,
355+ } ) ,
356+ {
357+ Ok : ( ) =>
358+ limits
359+ . recordSuccess ( { ...key , lease } )
360+ . pipe (
361+ Effect . as ( {
362+ status,
363+ headers,
364+ body : collected . text ,
365+ truncated : collected . truncated ,
366+ } ) ,
367+ ) ,
368+ RateLimited : ( ) =>
369+ Effect . gen ( function * ( ) {
370+ const retryAt = retryAtFrom ( headers , yield * Clock . currentTimeMillis ) ;
371+ yield * limits . recordRateLimit ( { ...key , lease, retryAt } ) ;
372+ return yield * new GitHubApiRateLimitError ( {
373+ ...context ,
374+ ...( retryAt === undefined ? { } : { retryAt } ) ,
375+ } ) ;
376+ } ) ,
377+ // The source may hold a newer token than the one that was refused.
378+ Unauthorized : ( ) =>
379+ credentials
380+ . invalidate ( host )
381+ . pipe ( Effect . andThen ( Effect . fail ( new GitHubApiAuthenticationError ( context ) ) ) ) ,
382+ NotFound : ( ) => Effect . fail ( new GitHubApiNotFoundError ( context ) ) ,
383+ Failed : ( { messages } ) =>
384+ Effect . fail (
385+ new GitHubApiResponseError ( {
386+ ...context ,
387+ status,
388+ ...( messages === undefined ? { } : { graphqlErrors : messages } ) ,
389+ } ) ,
390+ ) ,
391+ } ,
392+ ) ;
361393 } ) ;
362394 // The rate-limit scope follows the credential, so a pause on one account never blocks another.
363395 return yield * gate
@@ -412,19 +444,7 @@ export const make = Effect.gen(function* () {
412444 graphql : true ,
413445 } ) ;
414446 yield * budget . observe ( host , response . body ) ;
415- // GitHub answers a failed GraphQL document with HTTP 200 and an `errors` list. `gh api
416- // graphql` fails those, and callers rely on that to fall back to narrower reads.
417- const errors = graphqlErrors ( response . body ) ;
418- if ( errors === null ) return response . body ;
419- if ( errors . types . length > 0 && errors . types . every ( ( type ) => type === "NOT_FOUND" ) ) {
420- return yield * new GitHubApiNotFoundError ( { host, operation : input . operation } ) ;
421- }
422- return yield * new GitHubApiResponseError ( {
423- host,
424- operation : input . operation ,
425- status : response . status ,
426- graphqlErrors : errors . messages ,
427- } ) ;
447+ return response . body ;
428448 } ) . pipe ( Effect . provideService ( SourceControlRateLimit . CredentialScope , scope ) ) ;
429449 } ,
430450 ) ;
0 commit comments