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:
Page- 2022-12-19 14:24:33 +00:00 committed by GitHub
commit 7438d30eba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 615 additions and 228 deletions

790
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -33,6 +33,7 @@
"dependencies": {
"@balena/happy-eyeballs": "0.0.6",
"dbus": "^1.0.7",
"got": "^12.5.3",
"mdns-resolver": "^1.0.0",
"semver": "^7.3.2",
"sqlite3": "^5.0.11",

View File

@ -7,8 +7,7 @@ import type StrictEventEmitter from 'strict-event-emitter-types';
import type { TargetState } from '../types/state';
import { InternalInconsistencyError } from '../lib/errors';
import { getRequestInstance } from '../lib/request';
import { CoreOptions } from 'request';
import { getGotInstance } from '../lib/request';
import * as config from '../config';
import { writeLock } from '../lib/update-lock';
import constants = require('../lib/constants');
@ -118,19 +117,25 @@ export const update = async (
}
const endpoint = url.resolve(apiEndpoint, `/device/v3/${uuid}/state`);
const request = await getRequestInstance();
const got = await getGotInstance();
const params: CoreOptions = {
json: true,
const { statusCode, headers, body } = await got(endpoint, {
responseType: 'json',
headers: {
Authorization: `Bearer ${deviceApiKey}`,
'If-None-Match': cache?.etag,
},
};
const [{ statusCode, headers }, body] = await request
.getAsync(endpoint, params)
.timeout(apiTimeout);
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: apiTimeout,
connect: apiTimeout,
secureConnect: apiTimeout,
socket: apiTimeout,
send: apiTimeout,
response: apiTimeout,
},
});
if (statusCode === 304 && cache?.etag != null) {
// There's no change so no need to update the cache
@ -146,7 +151,7 @@ export const update = async (
cache = {
etag: headers.etag,
body,
body: body as any,
};
// Emit the target state and update the cache

View File

@ -61,12 +61,32 @@ const getRequestInstances = once(async () => {
},
};
const { got } = await import('got');
const resumableOpts = {
timeout: DEFAULT_REQUEST_TIMEOUT,
maxRetries: DEFAULT_REQUEST_RETRY_COUNT,
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);
// @ts-expect-error promisifyAll is a bit wonky
@ -76,6 +96,7 @@ const getRequestInstances = once(async () => {
const resumable = resumableRequestLib.defaults(resumableOpts);
return {
got,
requestOpts,
request,
resumable,
@ -86,6 +107,10 @@ export const getRequestInstance = once(async () => {
return (await getRequestInstances()).request;
});
export const getGotInstance = once(async () => {
return (await getRequestInstances()).got;
});
export const getRequestOptions = once(async () => {
return (await getRequestInstances()).requestOpts;
});