mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-20 14:13:08 +00:00
Merge pull request #2068 from balena-os/got
Use `got` for fetching the target state in order to have brotli support
This commit is contained in:
commit
7438d30eba
790
package-lock.json
generated
790
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -33,6 +33,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@balena/happy-eyeballs": "0.0.6",
|
"@balena/happy-eyeballs": "0.0.6",
|
||||||
"dbus": "^1.0.7",
|
"dbus": "^1.0.7",
|
||||||
|
"got": "^12.5.3",
|
||||||
"mdns-resolver": "^1.0.0",
|
"mdns-resolver": "^1.0.0",
|
||||||
"semver": "^7.3.2",
|
"semver": "^7.3.2",
|
||||||
"sqlite3": "^5.0.11",
|
"sqlite3": "^5.0.11",
|
||||||
|
@ -7,8 +7,7 @@ import type StrictEventEmitter from 'strict-event-emitter-types';
|
|||||||
|
|
||||||
import type { TargetState } from '../types/state';
|
import type { TargetState } from '../types/state';
|
||||||
import { InternalInconsistencyError } from '../lib/errors';
|
import { InternalInconsistencyError } from '../lib/errors';
|
||||||
import { getRequestInstance } from '../lib/request';
|
import { getGotInstance } from '../lib/request';
|
||||||
import { CoreOptions } from 'request';
|
|
||||||
import * as config from '../config';
|
import * as config from '../config';
|
||||||
import { writeLock } from '../lib/update-lock';
|
import { writeLock } from '../lib/update-lock';
|
||||||
import constants = require('../lib/constants');
|
import constants = require('../lib/constants');
|
||||||
@ -118,19 +117,25 @@ export const update = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const endpoint = url.resolve(apiEndpoint, `/device/v3/${uuid}/state`);
|
const endpoint = url.resolve(apiEndpoint, `/device/v3/${uuid}/state`);
|
||||||
const request = await getRequestInstance();
|
const got = await getGotInstance();
|
||||||
|
|
||||||
const params: CoreOptions = {
|
const { statusCode, headers, body } = await got(endpoint, {
|
||||||
json: true,
|
responseType: 'json',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${deviceApiKey}`,
|
Authorization: `Bearer ${deviceApiKey}`,
|
||||||
'If-None-Match': cache?.etag,
|
'If-None-Match': cache?.etag,
|
||||||
},
|
},
|
||||||
};
|
timeout: {
|
||||||
|
// TODO: We use the same default timeout for all of these in order to have a timeout generally
|
||||||
const [{ statusCode, headers }, body] = await request
|
// but it would probably make sense to tune them individually
|
||||||
.getAsync(endpoint, params)
|
lookup: apiTimeout,
|
||||||
.timeout(apiTimeout);
|
connect: apiTimeout,
|
||||||
|
secureConnect: apiTimeout,
|
||||||
|
socket: apiTimeout,
|
||||||
|
send: apiTimeout,
|
||||||
|
response: apiTimeout,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (statusCode === 304 && cache?.etag != null) {
|
if (statusCode === 304 && cache?.etag != null) {
|
||||||
// There's no change so no need to update the cache
|
// There's no change so no need to update the cache
|
||||||
@ -146,7 +151,7 @@ export const update = async (
|
|||||||
|
|
||||||
cache = {
|
cache = {
|
||||||
etag: headers.etag,
|
etag: headers.etag,
|
||||||
body,
|
body: body as any,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Emit the target state and update the cache
|
// Emit the target state and update the cache
|
||||||
|
@ -61,12 +61,32 @@ const getRequestInstances = once(async () => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const { got } = await import('got');
|
||||||
|
|
||||||
const resumableOpts = {
|
const resumableOpts = {
|
||||||
timeout: DEFAULT_REQUEST_TIMEOUT,
|
timeout: DEFAULT_REQUEST_TIMEOUT,
|
||||||
maxRetries: DEFAULT_REQUEST_RETRY_COUNT,
|
maxRetries: DEFAULT_REQUEST_RETRY_COUNT,
|
||||||
retryInterval: DEFAULT_REQUEST_RETRY_INTERVAL,
|
retryInterval: DEFAULT_REQUEST_RETRY_INTERVAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
got.extend({
|
||||||
|
responseType: 'json',
|
||||||
|
decompress: true,
|
||||||
|
timeout: {
|
||||||
|
// TODO: We use the same default timeout for all of these in order to have a timeout generally
|
||||||
|
// but it would probably make sense to tune them individually
|
||||||
|
lookup: DEFAULT_REQUEST_TIMEOUT,
|
||||||
|
connect: DEFAULT_REQUEST_TIMEOUT,
|
||||||
|
secureConnect: DEFAULT_REQUEST_TIMEOUT,
|
||||||
|
socket: DEFAULT_REQUEST_TIMEOUT,
|
||||||
|
send: DEFAULT_REQUEST_TIMEOUT,
|
||||||
|
response: DEFAULT_REQUEST_TIMEOUT,
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
'User-Agent': userAgent,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const requestHandle = requestLib.defaults(requestOpts);
|
const requestHandle = requestLib.defaults(requestOpts);
|
||||||
|
|
||||||
// @ts-expect-error promisifyAll is a bit wonky
|
// @ts-expect-error promisifyAll is a bit wonky
|
||||||
@ -76,6 +96,7 @@ const getRequestInstances = once(async () => {
|
|||||||
const resumable = resumableRequestLib.defaults(resumableOpts);
|
const resumable = resumableRequestLib.defaults(resumableOpts);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
got,
|
||||||
requestOpts,
|
requestOpts,
|
||||||
request,
|
request,
|
||||||
resumable,
|
resumable,
|
||||||
@ -86,6 +107,10 @@ export const getRequestInstance = once(async () => {
|
|||||||
return (await getRequestInstances()).request;
|
return (await getRequestInstances()).request;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const getGotInstance = once(async () => {
|
||||||
|
return (await getRequestInstances()).got;
|
||||||
|
});
|
||||||
|
|
||||||
export const getRequestOptions = once(async () => {
|
export const getRequestOptions = once(async () => {
|
||||||
return (await getRequestInstances()).requestOpts;
|
return (await getRequestInstances()).requestOpts;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user