Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ include:
in NodeJS.
* `host`: The URL to prepend to all request paths; defaults to `https://api.github.com`.
* `graphHost`: The URL to use for all GraphQL requests; defaults to using the value of `host` which works fine for `github.com`, but you'll need to set a separate value when working with GitHub Enterprise.
* `timeout`: The timeout in milliseconds to apply to the request; none by default. If the timeout is reached, the request will abort with an `AbortError`.
* `timeout`: The timeout in milliseconds to apply to the request; none by default. If the timeout is reached, the request will abort with a `TimeoutError`.
* `cache`: An instance of [LRUCache](https://github.com/isaacs/node-lru-cache). The
objects inserted into the cache will be of the form
`{value: {...}, eTag: 'abc123', status: 200, headers: {...}, size: 1763, expiry: 1770853094}`.
Expand Down
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default [
AbortController: false,
btoa: false,
clearTimeout: false,
DOMException: false,
fetch: false,
Promise: false,
setTimeout: false,
Expand Down
67 changes: 37 additions & 30 deletions hubkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ if (typeof require !== 'undefined') {
res.headers.get('x-ratelimit-remaining') === '0' &&
res.headers.get('x-ratelimit-reset')) {
try {
error.retryDelay =
Math.max(0, parseInt(res.headers.get('x-ratelimit-reset'), 10) * 1000 - Date.now());
const reset = parseInt(res.headers.get('x-ratelimit-reset'), 10);
error.retryDelay = Math.max(0, reset * 1000 - Date.now());
if (!options.timeout || error.retryDelay < options.timeout) value = Hubkit.RETRY;
} catch {
// ignore, don't retry request
Expand Down Expand Up @@ -468,7 +468,13 @@ if (typeof require !== 'undefined') {

function onError(error) {
error.originalMessage = error.message;
error.message = formatError('Hubkit', error.message);
const message = formatError('Hubkit', error.message);
try {
error.message = message;
} catch {
// DOMException (e.g. TimeoutError) has a read-only 'message' property.
Object.defineProperty(error, 'message', {value: message, writable: true});
}
error.fingerprint =
['Hubkit', options.method, options.pathPattern, error.originalMessage];
handleError(error);
Expand Down Expand Up @@ -657,38 +663,39 @@ if (typeof require !== 'undefined') {
}

async function fetchResponse(config, options) {
const init = {method: config.method, headers: config.headers};
if (config.body) {
init.body = JSON.stringify(config.body);
init.headers['Content-Type'] = 'application/json';
}
const url = new URL(config.url);
for (const key in config.params) url.searchParams.set(key, config.params[key]);
let timeoutId;
if (config.timeout) {
// TODO: Switch to AbortSignal.timeout once widely supported.
const controller = new AbortController();
init.signal = controller.signal;
timeoutId = setTimeout(() => {
controller.abort(
new DOMException('The operation was aborted due to timeout', 'TimeoutError'));
}, config.timeout);
}
let response, rawData;
try {
const init = {method: config.method, headers: config.headers};
if (config.body) {
init.body = JSON.stringify(config.body);
init.headers['Content-Type'] = 'application/json';
}
if (config.timeout) {
const controller = new AbortController();
timeoutId = setTimeout(() => controller.abort(), config.timeout);
init.signal = controller.signal;
}

const url = new URL(config.url);
for (const key in config.params) url.searchParams.set(key, config.params[key]);
let response, rawData;
try {
response = await fetch(url, init);
rawData = await readResponseBody(response, options);
} catch (error) {
error.networkFailure = true;
throw error;
}
return {
status: response.status,
headers: response.headers,
data: parseResponseData(rawData, response.headers, options),
rawData
};
response = await fetch(url, init);
rawData = await readResponseBody(response, options);
} catch (error) {
error.networkFailure = true;
throw error;
} finally {
if (timeoutId) clearTimeout(timeoutId);
}
return {
status: response.status,
headers: response.headers,
data: parseResponseData(rawData, response.headers, options),
rawData
};
}

function readResponseBody(response, options) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hubkit",
"version": "7.0.0",
"version": "7.1.0",
"description": "GitHub API library for JavaScript, promise-based, for both NodeJS and the browser",
"main": "index.js",
"types": "index.d.ts",
Expand Down