@@ -33,12 +33,9 @@ const TRANSIENT_CODES = new Set([
3333
3434const RETRY_STATUSES = new Set ( [ 500 , 502 , 503 , 504 ] ) ;
3535
36- /** The subset of `fetch` the client uses. */
3736export type Fetch = ( input : string , init : RequestInit ) => Promise < Response > ;
3837
39- /** Describes one HTTP response from the Synthient API. */
4038export interface ResponseMetadata {
41- /** The HTTP status code. */
4239 status : number ;
4340 /** The X-Request-Id header, or "". */
4441 requestID : string ;
@@ -50,7 +47,6 @@ export interface ResponseMetadata {
5047 headers : Headers ;
5148}
5249
53- /** Options for a single call. */
5450export interface RequestOptions {
5551 /**
5652 * Cancels the call, which rejects with the signal's reason. Use `AbortSignal.timeout(ms)`
@@ -88,8 +84,8 @@ export interface Request {
8884}
8985
9086/**
91- * One attempt at a request. Its signal follows the caller's signal and an idle timer
92- * that runs only while the attempt waits on the network.
87+ * Its signal follows the caller's signal and an idle timer that runs only while the
88+ * attempt waits on the network.
9389 */
9490export class Attempt {
9591 readonly caller : AbortSignal | undefined ;
@@ -110,9 +106,8 @@ export class Attempt {
110106 }
111107
112108 /**
113- * Waits at most the timeout for work that reads from the network. A caller abort
114- * rejects with the caller's reason, the timeout with APITimeoutError, and any other
115- * failure with APIConnectionError.
109+ * Storage failures are rebuilt without their message or cause, either of which can
110+ * contain the presigned URL.
116111 */
117112 async watch < T > ( work : Promise < T > ) : Promise < T > {
118113 const timer = setTimeout ( ( ) => {
@@ -156,16 +151,12 @@ export class Transport {
156151 this . #headers = new Headers ( { "x-api-key" : config . apiKey , "user-agent" : config . userAgent } ) ;
157152 }
158153
159- /** Returns the absolute URL of an API request. */
160154 url ( request : Request ) : string {
161155 const search = new URLSearchParams ( request . query ) . toString ( ) ;
162156 return search ? `${ this . #base} ${ request . path } ?${ search } ` : this . #base + request . path ;
163157 }
164158
165- /**
166- * Sends an API request, retrying as its policy allows, and returns the final
167- * response with its body unread.
168- */
159+ /** The caller must read or cancel the returned response's body. */
169160 send ( request : Request , signal ?: AbortSignal ) : Promise < [ Response , Attempt ] > {
170161 return this . #request< [ Response , Attempt ] > ( request , signal , ( response , attempt ) => [
171162 response ,
@@ -223,7 +214,6 @@ export class Transport {
223214 }
224215 }
225216
226- /** Sends an API request and returns its JSON object body. */
227217 json < T extends object > ( request : Request , signal ?: AbortSignal ) : Promise < T > {
228218 return this . #request( request , signal , async ( response , attempt ) => {
229219 if ( ! response . ok ) {
@@ -247,10 +237,6 @@ export class Transport {
247237 } ) ;
248238 }
249239
250- /**
251- * Sends an API request that must answer 200 and returns its body, with each read
252- * bounded by the client's timeout.
253- */
254240 async stream ( request : Request , signal ?: AbortSignal ) : Promise < ReadableStream < Uint8Array > > {
255241 const [ response , attempt ] = await this . send ( request , signal ) ;
256242 if ( response . status !== 200 ) {
@@ -259,10 +245,6 @@ export class Transport {
259245 return watchedBody ( response , attempt ) ;
260246 }
261247
262- /**
263- * Reads a failed response's bounded body into an APIError. If reading fails, the
264- * error keeps the status and has the read failure as its cause.
265- */
266248 async error ( response : Response , attempt : Attempt ) : Promise < APIError > {
267249 try {
268250 const [ body ] = await read ( response , attempt , MAX_ERROR_SIZE ) ;
@@ -276,9 +258,8 @@ export class Transport {
276258 }
277259
278260 /**
279- * Fetches a presigned storage URL without the API key, following at most 10
280- * redirects that stay on http(s) and never downgrade from https. Errors never
281- * include the URL, and storage error bodies are discarded.
261+ * The API key is never sent to the storage host. Errors never include the presigned
262+ * URL, and storage error bodies are discarded.
282263 */
283264 async download ( start : URL , signal ?: AbortSignal ) : Promise < ReadableStream < Uint8Array > > {
284265 let url = start ;
@@ -336,10 +317,6 @@ export function isHTTP(url: URL | null): url is URL {
336317 return url ?. protocol === "https:" || url ?. protocol === "http:" ;
337318}
338319
339- /**
340- * Resolves a redirect's location against `from`, allowing only an http(s) URL without
341- * credentials that does not downgrade from https.
342- */
343320export function redirectTarget ( from : URL , response : Response ) : URL {
344321 const location = response . headers . get ( "location" ) ;
345322 const target = location === null ? null : URL . parse ( location , from . href ) ;
@@ -352,16 +329,15 @@ export function redirectTarget(from: URL, response: Response): URL {
352329 return target ;
353330}
354331
355- /** Waits for a backoff delay, rejecting with the signal's reason if it aborts. */
356332export async function wait ( ms : number , signal : AbortSignal | undefined ) : Promise < void > {
357333 try {
358334 await sleep ( ms , undefined , { signal } ) ;
359335 } catch ( error ) {
336+ // timers/promises rejects with an AbortError, not the signal's reason.
360337 throw signal ?. aborted ? signal . reason : error ;
361338 }
362339}
363340
364- /** Returns the jittered delay in milliseconds before retry `attempt`, counted from zero. */
365341export function backoff ( attempt : number ) : number {
366342 return Math . min ( 1000 * 2 ** Math . min ( attempt , 5 ) , MAX_BACKOFF ) * ( 0.75 + Math . random ( ) / 2 ) ;
367343}
@@ -386,10 +362,6 @@ function transient(error: unknown, signal: AbortSignal | undefined): boolean {
386362 return false ;
387363}
388364
389- /**
390- * Wraps a response body so each read is bounded by the attempt's timeout. Canceling the
391- * returned stream cancels the body.
392- */
393365function watchedBody ( response : Response , attempt : Attempt ) : ReadableStream < Uint8Array > {
394366 if ( ! response . body ) {
395367 return new ReadableStream ( {
@@ -417,10 +389,7 @@ function watchedBody(response: Response, attempt: Attempt): ReadableStream<Uint8
417389 ) ;
418390}
419391
420- /**
421- * Reads at most `limit` bytes of a body. It returns the bytes and whether the body
422- * ended within the limit; a longer body is canceled after the limit.
423- */
392+ /** Returns false with the first `limit` bytes when the body is longer than `limit`. */
424393async function read (
425394 response : Response ,
426395 attempt : Attempt ,
@@ -450,7 +419,7 @@ async function read(
450419 }
451420}
452421
453- /** Discards a body so its connection can be reused. Failures other than caller cancellation are ignored. */
422+ /** Discards a body so its connection can be reused. */
454423export async function drain ( response : Response , attempt : Attempt ) : Promise < void > {
455424 await read ( response , attempt , MAX_ERROR_SIZE ) . catch ( ( ) => undefined ) ;
456425 attempt . caller ?. throwIfAborted ( ) ;
0 commit comments