-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #483 from airbrake/feauture/inc-request
Add IncRequest API
- Loading branch information
Showing
11 changed files
with
366 additions
and
275 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
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require('isomorphic-fetch'); | ||
|
||
import {HttpRequest, HttpResponse, errors} from './index'; | ||
|
||
|
||
let rateLimitReset = 0; | ||
|
||
|
||
export function request(req: HttpRequest): Promise<HttpResponse> { | ||
let utime = Date.now() / 1000; | ||
if (utime < rateLimitReset) { | ||
return Promise.reject(errors.ipRateLimited); | ||
} | ||
|
||
let opt = { | ||
method: req.method, | ||
body: req.body, | ||
}; | ||
return fetch(req.url, opt).then((resp: Response) => { | ||
if (resp.status === 401) { | ||
throw errors.unauthorized; | ||
} | ||
|
||
if (resp.status === 429) { | ||
let s = resp.headers.get('X-RateLimit-Delay'); | ||
if (!s) { | ||
throw errors.ipRateLimited; | ||
} | ||
|
||
let n = parseInt(s, 10); | ||
if (n > 0) { | ||
rateLimitReset = Date.now() / 1000 + n; | ||
} | ||
|
||
throw errors.ipRateLimited; | ||
} | ||
|
||
if (resp.status === 204) { | ||
return {json: null}; | ||
} | ||
if (resp.status >= 200 && resp.status < 300) { | ||
return resp.json().then((json) => { | ||
return {json: json}; | ||
}); | ||
} | ||
|
||
if (resp.status >= 400 && resp.status < 500) { | ||
return resp.json().then((json) => { | ||
let err = new Error(json.message); | ||
throw err; | ||
}); | ||
} | ||
|
||
return resp.text().then((body) => { | ||
let err = new Error( | ||
`airbrake: fetch: unexpected response: code=${resp.status} body='${body}'`); | ||
throw err; | ||
}); | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Options from '../options'; | ||
import {request as fetchRequest} from './fetch'; | ||
import {makeRequester as makeNodeRequester} from './node'; | ||
|
||
export interface HttpRequest { | ||
method: string; | ||
url: string; | ||
body: string; | ||
timeout?: number; | ||
} | ||
|
||
export interface HttpResponse { | ||
json: any; | ||
} | ||
|
||
export type Requester = (req: HttpRequest) => Promise<HttpResponse>; | ||
|
||
export function makeRequester(opts: Options): Requester { | ||
if (opts.request) { | ||
return makeNodeRequester(opts.request); | ||
} | ||
return fetchRequest; | ||
} | ||
|
||
export let errors = { | ||
unauthorized: new Error('airbrake: unauthorized: project id or key are wrong'), | ||
ipRateLimited: new Error('airbrake: IP is rate limited'), | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import * as request_lib from 'request'; | ||
|
||
import {Requester, HttpRequest, HttpResponse, errors} from './index'; | ||
|
||
|
||
type requestAPI = request_lib.RequestAPI<request_lib.Request, request_lib.CoreOptions, request_lib.RequiredUriUrl>; | ||
|
||
export function makeRequester(api: requestAPI): Requester { | ||
return (req: HttpRequest): Promise<HttpResponse> => { | ||
return request(req, api); | ||
}; | ||
} | ||
|
||
|
||
let rateLimitReset = 0; | ||
|
||
function request(req: HttpRequest, api: requestAPI): Promise<HttpResponse> { | ||
let utime = Date.now() / 1000; | ||
if (utime < rateLimitReset) { | ||
return Promise.reject(errors.ipRateLimited); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
api({ | ||
url: req.url, | ||
method: req.method, | ||
body: req.body, | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
timeout: req.timeout | ||
}, function (error: any, resp: request_lib.RequestResponse, body: any): void { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
|
||
if (!resp.statusCode) { | ||
let err = new Error( | ||
`airbrake: request: response statusCode is ${resp.statusCode}`); | ||
reject(err); | ||
return; | ||
} | ||
|
||
if (resp.statusCode === 401) { | ||
reject(errors.unauthorized); | ||
return; | ||
} | ||
|
||
if (resp.statusCode === 429) { | ||
reject(errors.ipRateLimited); | ||
|
||
let h = resp.headers['x-ratelimit-delay']; | ||
if (!h) { | ||
return; | ||
} | ||
|
||
let s: string; | ||
if (typeof h === 'string') { | ||
s = h; | ||
} else if (h instanceof Array) { | ||
s = h[0]; | ||
} else { | ||
return; | ||
} | ||
|
||
let n = parseInt(s, 10); | ||
if (n > 0) { | ||
rateLimitReset = Date.now() / 1000 + n; | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (resp.statusCode === 204) { | ||
resolve({json: null}); | ||
return; | ||
} | ||
|
||
if (resp.statusCode >= 200 && resp.statusCode < 300) { | ||
let json; | ||
try { | ||
json = JSON.parse(body); | ||
} catch (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(json); | ||
return; | ||
} | ||
|
||
if (resp.statusCode >= 400 && resp.statusCode < 500) { | ||
let json; | ||
try { | ||
json = JSON.parse(body); | ||
} catch (err) { | ||
reject(err); | ||
return; | ||
} | ||
let err = new Error(json.message); | ||
reject(err); | ||
return; | ||
} | ||
|
||
body = body.trim(); | ||
let err = new Error( | ||
`airbrake: node: unexpected response: code=${resp.statusCode} body='${body}'`); | ||
reject(err); | ||
}); | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as request from 'request'; | ||
|
||
import Notice from './notice'; | ||
import {HistorianOptions} from './historian'; | ||
import Processor from './processor/processor'; | ||
|
||
type Reporter = (notice: Notice) => Promise<Notice>; | ||
|
||
export default interface Options { | ||
projectId: number; | ||
projectKey: string; | ||
environment?: string; | ||
host?: string; | ||
timeout?: number; | ||
keysBlacklist?: any[]; | ||
ignoreWindowError?: boolean; | ||
processor?: Processor; | ||
reporter?: Reporter; | ||
instrumentation?: HistorianOptions; | ||
|
||
request?: request.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>; | ||
} |
Oops, something went wrong.