-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5dbc75
commit b9e63ea
Showing
1 changed file
with
41 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,53 @@ | ||
import {HttpLibrary, RequestContext, ResponseContext} from './http'; | ||
import { HttpLibrary, RequestContext, ResponseContext } from './http'; | ||
import { from, Observable } from '../rxjsStub'; | ||
import "svix-fetch"; | ||
|
||
const numRetries = 2; | ||
const sleep = (interval: number) => new Promise(resolve => setTimeout(resolve, interval)); | ||
|
||
export class IsomorphicFetchHttpLibrary implements HttpLibrary { | ||
public send(request: RequestContext): Observable<ResponseContext> { | ||
const resultPromise = this.sendWithRetry(request, numRetries, 50, 1); | ||
return from<Promise<ResponseContext>>(resultPromise); | ||
} | ||
|
||
public send(request: RequestContext): Observable<ResponseContext> { | ||
const resultPromise = this.sendWithRetry(request, numRetries, 50, 1); | ||
return from<Promise<ResponseContext>>(resultPromise); | ||
} | ||
private async sendWithRetry(request: RequestContext, triesLeft: number, nextInterval: number, retryCount: number): Promise<ResponseContext> { | ||
try { | ||
const response = await this.sendOnce(request); | ||
if (triesLeft <= 0 || response.httpStatusCode < 500) { | ||
return response; | ||
} | ||
} catch (e) { | ||
if (triesLeft <= 0) { | ||
throw e; | ||
} | ||
}; | ||
await sleep(nextInterval); | ||
const headers = request.getHeaders(); | ||
headers['svix-retry-count'] = retryCount.toString() | ||
return await this.sendWithRetry(request, --triesLeft, nextInterval * 2, ++retryCount); | ||
} | ||
|
||
private async sendWithRetry(request: RequestContext, triesLeft: number, nextInterval: number, retryCount: number): Promise<ResponseContext> { | ||
try { | ||
const response = await this.sendOnce(request); | ||
if (triesLeft <= 0 || response.httpStatusCode < 500) { | ||
return response; | ||
} | ||
} catch (e) { | ||
if (triesLeft <= 0) { | ||
throw e; | ||
} | ||
}; | ||
await sleep(nextInterval); | ||
const headers = request.getHeaders(); | ||
headers['svix-retry-count'] = retryCount.toString() | ||
return await this.sendWithRetry(request, --triesLeft, nextInterval * 2, ++retryCount); | ||
} | ||
|
||
private sendOnce(request: RequestContext): Promise<ResponseContext> { | ||
let method = request.getHttpMethod().toString(); | ||
let body = request.getBody(); | ||
private sendOnce(request: RequestContext): Promise<ResponseContext> { | ||
let method = request.getHttpMethod().toString(); | ||
let body = request.getBody(); | ||
|
||
return fetch(request.getUrl(), { | ||
method: method, | ||
body: body as any, | ||
headers: request.getHeaders(), | ||
credentials: "same-origin" | ||
}).then((resp: any) => { | ||
const headers: { [name: string]: string } = {}; | ||
resp.headers.forEach((value: string, name: string) => { | ||
headers[name] = value; | ||
}); | ||
return fetch(request.getUrl(), { | ||
method: method, | ||
body: body as any, | ||
headers: request.getHeaders(), | ||
credentials: "same-origin" | ||
}).then((resp: any) => { | ||
const headers: { [name: string]: string } = {}; | ||
resp.headers.forEach((value: string, name: string) => { | ||
headers[name] = value; | ||
}); | ||
|
||
const body = { | ||
text: () => resp.text(), | ||
binary: () => resp.blob() | ||
}; | ||
return new ResponseContext(resp.status, headers, body); | ||
}); | ||
} | ||
const body = { | ||
text: () => resp.text(), | ||
binary: () => resp.blob() | ||
}; | ||
return new ResponseContext(resp.status, headers, body); | ||
}); | ||
} | ||
} |