balena-sdk 15.59.0 → 15.59.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,25 @@ All notable changes to this project will be documented in this file
4
4
  automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY!
5
5
  This project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ ## 15.59.2 - 2021-11-28
8
+
9
+
10
+ <details>
11
+ <summary> Update balena-request to 11.5.0 [Thodoris Greasidis] </summary>
12
+
13
+ > ### balena-request-11.5.0 - 2021-11-28
14
+ >
15
+ > * Convert tests to JavaScript and drop coffeescript [Thodoris Greasidis]
16
+ > * Fix the jsdoc generation [Thodoris Greasidis]
17
+ > * Convert to typescript and publish typings [Thodoris Greasidis]
18
+ >
19
+ </details>
20
+
21
+ ## 15.59.1 - 2021-11-28
22
+
23
+ * Fix the typings of the Image contract field [Thodoris Greasidis]
24
+ * Fix the typings for the Release contract field [Thodoris Greasidis]
25
+
7
26
  ## 15.59.0 - 2021-11-24
8
27
 
9
28
  * Add release setIsInvalidated function [Matthew Yarmolinsky]
@@ -12122,7 +12122,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12122
12122
  // being embedded in the dist of the consumer project
12123
12123
  // which we want to avoid, both b/c of the dist size increase &
12124
12124
  // the security concerns of including the versions of the dependencies
12125
- const sdkVersion = '15.59.0';
12125
+ const sdkVersion = '15.59.2';
12126
12126
  exports.default = sdkVersion;
12127
12127
 
12128
12128
  },{}],47:[function(require,module,exports){
@@ -15295,12 +15295,19 @@ const getProgressStream = function (total, onState) {
15295
15295
  });
15296
15296
  return progressStream;
15297
15297
  };
15298
+ /**
15299
+ * This callback is displayed as a global member.
15300
+ * @callback estimateStreamCallback
15301
+ * @param {BalenaRequestOptions} options
15302
+ *
15303
+ * @returns {Promise<NodeJS.ReadableStream>} request stream
15304
+ */
15298
15305
  /**
15299
15306
  * @summary Make a node request with progress
15300
15307
  * @function
15301
15308
  * @protected
15302
15309
  *
15303
- * @returns {(options) => Promise<NodeJS.ReadableStream>} request stream
15310
+ * @returns {estimateStreamCallback} request stream callback
15304
15311
  *
15305
15312
  * @example
15306
15313
  * progress.estimate(options).then (stream) ->
@@ -15332,7 +15339,6 @@ function estimate(requestAsync, isBrowser) {
15332
15339
  const response = yield requestAsync(options);
15333
15340
  const stream = require('stream');
15334
15341
  const output = new stream.PassThrough();
15335
- // @ts-expect-error
15336
15342
  output.response = response;
15337
15343
  const responseLength = utils.getResponseLength(response);
15338
15344
  const total = responseLength.uncompressed || responseLength.compressed;
@@ -15353,7 +15359,7 @@ function estimate(requestAsync, isBrowser) {
15353
15359
  // servers send slightly invalid gzip responses that are still accepted
15354
15360
  // by common browsers.
15355
15361
  // Always using Z_SYNC_FLUSH is what cURL does.
15356
- var zlibOptions = {
15362
+ let zlibOptions = {
15357
15363
  flush: zlib.constants.Z_SYNC_FLUSH,
15358
15364
  finishFlush: zlib.constants.Z_SYNC_FLUSH,
15359
15365
  };
@@ -15410,21 +15416,23 @@ limitations under the License.
15410
15416
  Object.defineProperty(exports, "__esModule", { value: true });
15411
15417
  exports.getRequest = void 0;
15412
15418
  const tslib_1 = require("tslib");
15413
- /**
15414
- * @module request
15415
- */
15416
15419
  const urlLib = require("url");
15417
15420
  const errors = require("balena-errors");
15418
15421
  const utils = require("./utils");
15419
15422
  /**
15423
+ * @module request
15424
+ */
15425
+ /**
15426
+ * @summary Creates a new balena-request instance.
15427
+ *
15420
15428
  * @param {object} options
15421
- * @param {import('balena-auth').default} options.auth
15429
+ * @param {object} options.auth
15422
15430
  * @param {boolean} options.debug
15423
15431
  * @param {number} options.retries
15424
15432
  * @param {boolean} options.isBrowser
15425
15433
  * @param {array} options.interceptors
15426
15434
  */
15427
- function getRequest({ auth, debug = false, retries = 0, isBrowser = false, interceptors = [], }) {
15435
+ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, interceptors: $interceptors = [], }) {
15428
15436
  const requestAsync = utils.getRequestAsync();
15429
15437
  const requestStream = isBrowser
15430
15438
  ? utils.getRequestAsync(require('fetch-readablestream'))
@@ -15434,7 +15442,6 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15434
15442
  // noop
15435
15443
  }
15436
15444
  : utils.debugRequest;
15437
- const exports = {};
15438
15445
  const prepareOptions = function (options) {
15439
15446
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
15440
15447
  if (options == null) {
@@ -15457,7 +15464,7 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15457
15464
  options.refreshToken &&
15458
15465
  (yield utils.shouldRefreshKey(auth))) {
15459
15466
  if (baseUrl && !isAbsoluteUrl) {
15460
- yield exports.refreshToken({ baseUrl });
15467
+ yield refreshToken({ baseUrl });
15461
15468
  }
15462
15469
  if (yield auth.isExpired()) {
15463
15470
  throw new errors.BalenaExpiredToken(yield auth.getKey());
@@ -15486,7 +15493,7 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15486
15493
  const interceptRequestError = (requestError) => interceptRequestOrError(Promise.reject(requestError));
15487
15494
  const interceptResponse = (response) => interceptResponseOrError(Promise.resolve(response));
15488
15495
  const interceptResponseError = (responseError) => interceptResponseOrError(Promise.reject(responseError));
15489
- var interceptRequestOrError = (initialPromise) => tslib_1.__awaiter(this, void 0, void 0, function* () {
15496
+ const interceptRequestOrError = (initialPromise) => tslib_1.__awaiter(this, void 0, void 0, function* () {
15490
15497
  return exports.interceptors.reduce(function (promise, { request, requestError }) {
15491
15498
  if (request != null || requestError != null) {
15492
15499
  return promise.then(request, requestError);
@@ -15496,10 +15503,12 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15496
15503
  }
15497
15504
  }, initialPromise);
15498
15505
  });
15499
- var interceptResponseOrError = function (initialPromise) {
15506
+ const interceptResponseOrError = function (initialPromise) {
15500
15507
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
15501
- interceptors = exports.interceptors.slice().reverse();
15502
- return interceptors.reduce(function (promise, { response, responseError }) {
15508
+ return exports.interceptors
15509
+ .slice()
15510
+ .reverse()
15511
+ .reduce(function (promise, { response, responseError }) {
15503
15512
  if (response != null || responseError != null) {
15504
15513
  return promise.then(response, responseError);
15505
15514
  }
@@ -15550,7 +15559,7 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15550
15559
  * hello: 'world'
15551
15560
  * .get('body')
15552
15561
  */
15553
- exports.send = function (options) {
15562
+ function send(options) {
15554
15563
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
15555
15564
  // Only set the default timeout when doing a normal HTTP
15556
15565
  // request and not also when streaming since in the latter
@@ -15586,7 +15595,7 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15586
15595
  }))
15587
15596
  .then(interceptResponse, interceptResponseError);
15588
15597
  });
15589
- };
15598
+ }
15590
15599
  /**
15591
15600
  * @summary Stream an HTTP response from balena.
15592
15601
  * @function
@@ -15625,16 +15634,14 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15625
15634
  *
15626
15635
  * stream.pipe(fs.createWriteStream('/opt/download'))
15627
15636
  */
15628
- exports.stream = function (options) {
15637
+ function stream(options) {
15629
15638
  const progress = require('./progress');
15630
15639
  return prepareOptions(options)
15631
15640
  .then(interceptRequestOptions, interceptRequestError)
15632
15641
  .then((opts) => tslib_1.__awaiter(this, void 0, void 0, function* () {
15633
15642
  const download = yield progress.estimate(requestStream, isBrowser)(opts);
15634
- // @ts-expect-error
15635
15643
  if (!utils.isErrorCode(download.response.statusCode)) {
15636
15644
  // TODO: Move this to balena-image-manager
15637
- // @ts-expect-error
15638
15645
  download.mime = download.response.headers.get('Content-Type');
15639
15646
  return download;
15640
15647
  }
@@ -15650,34 +15657,12 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15650
15657
  download.on('done', resolve);
15651
15658
  });
15652
15659
  const responseError = chunks.join() || 'The request was unsuccessful';
15653
- // @ts-expect-error
15654
15660
  debugRequest(options, download.response);
15655
15661
  // @ts-expect-error
15656
- throw new errors.BalenaRequestError(responseError,
15657
- // @ts-expect-error
15658
- download.response.statusCode);
15662
+ throw new errors.BalenaRequestError(responseError, download.response.statusCode);
15659
15663
  }))
15660
- .then(interceptResponse, interceptResponseError);
15661
- };
15662
- /**
15663
- * @summary Array of interceptors
15664
- * @type {Interceptor[]}
15665
- * @public
15666
- *
15667
- * @description
15668
- * The current array of interceptors to use. Interceptors intercept requests made
15669
- * by calls to `.stream()` and `.send()` (some of which are made internally) and
15670
- * are executed in the order they appear in this array for requests, and in the
15671
- * reverse order for responses.
15672
- *
15673
- * @example
15674
- * request.interceptors.push(
15675
- * requestError: (error) ->
15676
- * console.log(error)
15677
- * throw error
15678
- * )
15679
- */
15680
- exports.interceptors = interceptors;
15664
+ .then((x) => interceptResponse(x), interceptResponseError);
15665
+ }
15681
15666
  /**
15682
15667
  * @typedef Interceptor
15683
15668
  * @type {object}
@@ -15706,6 +15691,26 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15706
15691
  * an error for the request, a network error, or an error response from the server. Should return
15707
15692
  * (or resolve to) a new response, or throw/reject.
15708
15693
  */
15694
+ /**
15695
+ * @summary Array of interceptor
15696
+ * @type {Interceptor[]}
15697
+ * @public
15698
+ *
15699
+ * @description
15700
+ * The current array of interceptors to use. Interceptors intercept requests made
15701
+ * by calls to `.stream()` and `.send()` (some of which are made internally) and
15702
+ * are executed in the order they appear in this array for requests, and in the
15703
+ * reverse order for responses.
15704
+ *
15705
+ * @example
15706
+ * request.interceptors.push(
15707
+ * requestError: (error) ->
15708
+ * console.log(error)
15709
+ * throw error
15710
+ * )
15711
+ */
15712
+ // Shortcut to get the correct jsdoc readme generated
15713
+ const interceptors = $interceptors;
15709
15714
  /**
15710
15715
  * @summary Refresh token on user request
15711
15716
  * @function
@@ -15723,7 +15728,7 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15723
15728
  * request.refreshToken
15724
15729
  * baseUrl: 'https://api.balena-cloud.com'
15725
15730
  */
15726
- exports.refreshToken = function ({ baseUrl }) {
15731
+ function refreshToken({ baseUrl, }) {
15727
15732
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
15728
15733
  // Only refresh if we have balena-auth
15729
15734
  if (auth == null) {
@@ -15731,7 +15736,7 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15731
15736
  }
15732
15737
  let response;
15733
15738
  try {
15734
- response = yield exports.send({
15739
+ response = yield send({
15735
15740
  url: '/user/v1/refresh-token',
15736
15741
  baseUrl,
15737
15742
  refreshToken: false,
@@ -15749,6 +15754,12 @@ function getRequest({ auth, debug = false, retries = 0, isBrowser = false, inter
15749
15754
  yield auth.setKey(refreshedKey);
15750
15755
  return refreshedKey;
15751
15756
  });
15757
+ }
15758
+ const exports = {
15759
+ send,
15760
+ stream,
15761
+ interceptors,
15762
+ refreshToken,
15752
15763
  };
15753
15764
  return exports;
15754
15765
  }
@@ -15783,7 +15794,7 @@ const IS_BROWSER = typeof window !== 'undefined' && window !== null;
15783
15794
  /**
15784
15795
  * @module utils
15785
15796
  */
15786
- exports.TOKEN_REFRESH_INTERVAL = 1 * 1000 * 60 * 60; // 1 hour in milliseconds
15797
+ exports.TOKEN_REFRESH_INTERVAL = 1 * 60 * 60 * 1000; // 1 hour in milliseconds
15787
15798
  /**
15788
15799
  * @summary Determine if the token should be updated
15789
15800
  * @function
@@ -15793,7 +15804,7 @@ exports.TOKEN_REFRESH_INTERVAL = 1 * 1000 * 60 * 60; // 1 hour in milliseconds
15793
15804
  * This function makes use of a soft user-configurable setting called `tokenRefreshInterval`.
15794
15805
  * That setting doesn't express that the token is "invalid", but represents that it is a good time for the token to be updated *before* it get's outdated.
15795
15806
  *
15796
- * @param {import('balena-auth').default} auth - an instance of `balena-auth`
15807
+ * @param {object} auth - an instance of `balena-auth`
15797
15808
  * @returns {Promise<Boolean>} the token should be updated
15798
15809
  *
15799
15810
  * @example
@@ -15802,6 +15813,7 @@ exports.TOKEN_REFRESH_INTERVAL = 1 * 1000 * 60 * 60; // 1 hour in milliseconds
15802
15813
  * console.log('Updating token!')
15803
15814
  */
15804
15815
  function shouldRefreshKey(auth) {
15816
+ var _a;
15805
15817
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
15806
15818
  const hasKey = yield auth.hasKey();
15807
15819
  if (!hasKey) {
@@ -15811,8 +15823,7 @@ function shouldRefreshKey(auth) {
15811
15823
  if (type !== token_1.TokenType.JWT) {
15812
15824
  return false;
15813
15825
  }
15814
- const age = yield auth.getAge();
15815
- // @ts-expect-error
15826
+ const age = (_a = (yield auth.getAge())) !== null && _a !== void 0 ? _a : 0;
15816
15827
  return age >= exports.TOKEN_REFRESH_INTERVAL;
15817
15828
  });
15818
15829
  }
@@ -15825,7 +15836,7 @@ exports.shouldRefreshKey = shouldRefreshKey;
15825
15836
  * @description
15826
15837
  * This promise becomes undefined if no saved token.
15827
15838
  *
15828
- * @param {import('balena-auth').default} auth - an instance of `balena-auth`
15839
+ * @param {object} auth - an instance of `balena-auth`
15829
15840
  * @returns {Promise<string | undefined>} authorization header
15830
15841
  *
15831
15842
  * @example
@@ -15833,7 +15844,7 @@ exports.shouldRefreshKey = shouldRefreshKey;
15833
15844
  * headers =
15834
15845
  * Authorization: authorizationHeader
15835
15846
  */
15836
- exports.getAuthorizationHeader = function (auth) {
15847
+ function getAuthorizationHeader(auth) {
15837
15848
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
15838
15849
  if (auth == null) {
15839
15850
  return;
@@ -15845,7 +15856,8 @@ exports.getAuthorizationHeader = function (auth) {
15845
15856
  const key = yield auth.getKey();
15846
15857
  return `Bearer ${key}`;
15847
15858
  });
15848
- };
15859
+ }
15860
+ exports.getAuthorizationHeader = getAuthorizationHeader;
15849
15861
  /**
15850
15862
  * @summary Get error message from response
15851
15863
  * @function
@@ -15946,10 +15958,7 @@ exports.getResponseLength = getResponseLength;
15946
15958
  * utils.debugRequest(options, response)
15947
15959
  */
15948
15960
  function debugRequest(options, response) {
15949
- return console.error(Object.assign({
15950
- statusCode: response.statusCode,
15951
- duration: response.duration,
15952
- }, options));
15961
+ return console.error(Object.assign({ statusCode: response.statusCode, duration: response.duration }, options));
15953
15962
  }
15954
15963
  exports.debugRequest = debugRequest;
15955
15964
  // fetch adapter
@@ -15987,10 +15996,10 @@ const UNSUPPORTED_REQUEST_PARAMS = [
15987
15996
  'callback',
15988
15997
  ];
15989
15998
  const processRequestOptions = function (options) {
15990
- if (options == null) {
15991
- options = {};
15992
- }
15993
15999
  let url = options.url || options.uri;
16000
+ if (url == null) {
16001
+ throw new Error('url option not provided');
16002
+ }
15994
16003
  if (options.baseUrl) {
15995
16004
  url = urlLib.resolve(options.baseUrl, url);
15996
16005
  }
@@ -15998,12 +16007,6 @@ const processRequestOptions = function (options) {
15998
16007
  const params = qs.stringify(options.qs);
15999
16008
  url += (url.indexOf('?') >= 0 ? '&' : '?') + params;
16000
16009
  }
16001
- const opts = {};
16002
- opts.timeout = options.timeout;
16003
- opts.retries = options.retries;
16004
- opts.method = options.method;
16005
- opts.compress = options.gzip;
16006
- opts.signal = options.signal;
16007
16010
  let { body, headers } = options;
16008
16011
  if (headers == null) {
16009
16012
  headers = {};
@@ -16012,25 +16015,21 @@ const processRequestOptions = function (options) {
16012
16015
  body = JSON.stringify(body);
16013
16016
  headers['Content-Type'] = 'application/json';
16014
16017
  }
16015
- opts.body = body;
16016
16018
  if (!IS_BROWSER) {
16017
16019
  if (!headers['Accept-Encoding']) {
16018
16020
  headers['Accept-Encoding'] = 'compress, gzip';
16019
16021
  }
16020
16022
  }
16021
- if (options.followRedirect) {
16022
- opts.redirect = 'follow';
16023
- }
16024
- opts.headers = new HeadersPonyfill(headers);
16025
16023
  if (options.strictSSL === false) {
16026
16024
  throw new Error('`strictSSL` must be true or absent');
16027
16025
  }
16028
- for (let key of UNSUPPORTED_REQUEST_PARAMS) {
16029
- if (options[key] != null) {
16030
- throw new Error(`The ${key} param is not supported. Value: ${options[key]}`);
16026
+ for (const key of UNSUPPORTED_REQUEST_PARAMS) {
16027
+ const unsupportedOptionValue = options[key];
16028
+ if (unsupportedOptionValue != null) {
16029
+ throw new Error(`The ${key} param is not supported. Value: ${unsupportedOptionValue}`);
16031
16030
  }
16032
16031
  }
16033
- opts.mode = 'cors';
16032
+ const opts = Object.assign({ timeout: options.timeout, retries: options.retries, method: options.method, compress: options.gzip, signal: options.signal, body, headers: new HeadersPonyfill(headers), mode: 'cors' }, (options.followRedirect && { redirect: 'follow' }));
16034
16033
  return [url, opts];
16035
16034
  };
16036
16035
  /**
@@ -16078,7 +16077,7 @@ function getBody(response, responseFormat) {
16078
16077
  }
16079
16078
  exports.getBody = getBody;
16080
16079
  // This is the actual implementation that hides the internal `retriesRemaining` parameter
16081
- var requestAsync = function (fetch, options, retriesRemaining) {
16080
+ function requestAsync(fetch, options, retriesRemaining) {
16082
16081
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
16083
16082
  const [url, opts] = processRequestOptions(options);
16084
16083
  if (retriesRemaining == null) {
@@ -16105,7 +16104,7 @@ var requestAsync = function (fetch, options, retriesRemaining) {
16105
16104
  p.then(resolve, reject);
16106
16105
  });
16107
16106
  }
16108
- const response = yield p;
16107
+ const response = (yield p);
16109
16108
  if (opts.signal) {
16110
16109
  handleAbortIfNotSupported(opts.signal, response);
16111
16110
  }
@@ -16125,8 +16124,8 @@ var requestAsync = function (fetch, options, retriesRemaining) {
16125
16124
  throw err;
16126
16125
  }
16127
16126
  });
16128
- };
16129
- var handleAbortIfNotSupported = function (signal, response) {
16127
+ }
16128
+ function handleAbortIfNotSupported(signal, response) {
16130
16129
  const emulateAbort = (() => {
16131
16130
  var _a, _b;
16132
16131
  if ((_a = response.body) === null || _a === void 0 ? void 0 : _a.cancel) {
@@ -16154,7 +16153,7 @@ var handleAbortIfNotSupported = function (signal, response) {
16154
16153
  });
16155
16154
  }
16156
16155
  }
16157
- };
16156
+ }
16158
16157
  /**
16159
16158
  * @summary The factory that returns the `requestAsync` function.
16160
16159
  * @function
@@ -16170,11 +16169,8 @@ var handleAbortIfNotSupported = function (signal, response) {
16170
16169
  * utils.getRequestAsync()({ url: 'http://example.com' }).then (response) ->
16171
16170
  * console.log(response)
16172
16171
  */
16173
- function getRequestAsync(fetch) {
16174
- if (fetch == null) {
16175
- fetch = normalFetch;
16176
- }
16177
- return (options) => requestAsync(fetch, options);
16172
+ function getRequestAsync($fetch = normalFetch) {
16173
+ return (options) => requestAsync($fetch, options);
16178
16174
  }
16179
16175
  exports.getRequestAsync = getRequestAsync;
16180
16176