From 984d1a3fd60b1fdbda762060c5ce081822f7eb34 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Tue, 30 Jun 2020 21:24:35 +0100 Subject: [PATCH] Switch from new Bluebird to native version Change-type: patch --- lib/actions-oclif/os/configure.ts | 1 - lib/actions/preload.js | 5 ++--- lib/actions/tunnel.ts | 3 +-- lib/auth/index.ts | 4 +++- lib/auth/server.ts | 5 ++--- lib/utils/deploy-legacy.js | 7 ++++--- lib/utils/device/api.ts | 4 ++-- lib/utils/device/logs.ts | 5 ++--- lib/utils/qemu.ts | 4 ++-- lib/utils/remote-build.ts | 3 +-- lib/utils/ssh.ts | 3 +-- lib/utils/streams.ts | 7 +++---- lib/utils/tunnel.ts | 7 ++++--- 13 files changed, 27 insertions(+), 31 deletions(-) diff --git a/lib/actions-oclif/os/configure.ts b/lib/actions-oclif/os/configure.ts index 25fd393d..6da3662c 100644 --- a/lib/actions-oclif/os/configure.ts +++ b/lib/actions-oclif/os/configure.ts @@ -17,7 +17,6 @@ import { flags } from '@oclif/command'; import type * as BalenaSdk from 'balena-sdk'; -import Bluebird = require('bluebird'); import * as _ from 'lodash'; import * as path from 'path'; import Command from '../../command'; diff --git a/lib/actions/preload.js b/lib/actions/preload.js index 48ca0e11..1fd9b0ee 100644 --- a/lib/actions/preload.js +++ b/lib/actions/preload.js @@ -255,8 +255,6 @@ Examples: primary: true, options: preloadOptions, action(params, options, done) { - let certificates; - const Bluebird = require('bluebird'); const balena = getBalenaSdk(); const balenaPreload = require('balena-preload'); const visuals = getVisuals(); @@ -309,6 +307,7 @@ Examples: options.pinDevice = options['pin-device-to-release'] || false; delete options['pin-device-to-release']; + let certificates; if (Array.isArray(options['add-certificate'])) { certificates = options['add-certificate']; } else if (options['add-certificate'] === undefined) { @@ -358,7 +357,7 @@ Examples: preloader.on('progress', progressHandler); preloader.on('spinner', spinnerHandler); - return new Bluebird(function (resolve, reject) { + return new Promise(function (resolve, reject) { preloader.on('error', reject); return preloader diff --git a/lib/actions/tunnel.ts b/lib/actions/tunnel.ts index 74898c8b..11aff425 100644 --- a/lib/actions/tunnel.ts +++ b/lib/actions/tunnel.ts @@ -13,7 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import * as Bluebird from 'bluebird'; import type { CommandDefinition } from 'capitano'; import * as _ from 'lodash'; import { createServer, Server, Socket } from 'net'; @@ -196,7 +195,7 @@ export const tunnel: CommandDefinition = { ) .then( (server) => - new Bluebird.Promise((resolve, reject) => { + new Promise((resolve, reject) => { server.on('error', reject); server.listen(localPort, localAddress, () => { resolve(server); diff --git a/lib/auth/index.ts b/lib/auth/index.ts index d9435567..d1db9724 100644 --- a/lib/auth/index.ts +++ b/lib/auth/index.ts @@ -64,5 +64,7 @@ export const login = async () => { }, 1000); const balena = getBalenaSdk(); - return awaitForToken(options).tap(balena.auth.loginWithToken); + const token = await awaitForToken(options) + await balena.auth.loginWithToken(token); + return token }; diff --git a/lib/auth/server.ts b/lib/auth/server.ts index 33f8e36a..68116729 100644 --- a/lib/auth/server.ts +++ b/lib/auth/server.ts @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -import * as Bluebird from 'bluebird'; import * as bodyParser from 'body-parser'; import * as express from 'express'; import type { Socket } from 'net'; @@ -79,10 +78,10 @@ export function shutdownServer() { export const awaitForToken = (options: { path: string; port: number; -}): Bluebird => { +}): Promise => { const { app, server } = createServer({ port: options.port }); - return new Bluebird((resolve, reject) => { + return new Promise((resolve, reject) => { app.post(options.path, async (request, response) => { server.close(); // stop listening for new connections try { diff --git a/lib/utils/deploy-legacy.js b/lib/utils/deploy-legacy.js index 81580afe..07381a81 100644 --- a/lib/utils/deploy-legacy.js +++ b/lib/utils/deploy-legacy.js @@ -40,9 +40,10 @@ const bufferImage = function (docker, imageId, bufferFile) { image.get(), imageMetadata.get('Size'), (imageStream, imageSize) => - streamUtils.buffer(imageStream, bufferFile).tap((bufferedStream) => { + streamUtils.buffer(imageStream, bufferFile).then((bufferedStream) => { // @ts-ignore adding an extra property bufferedStream.length = imageSize; + return bufferedStream }), ); }; @@ -55,7 +56,7 @@ const showPushProgress = function (message) { }; const uploadToPromise = (uploadRequest, logger) => - new Bluebird(function (resolve, reject) { + new Promise(function (resolve, reject) { const handleMessage = function (data) { let obj; data = data.toString(); @@ -88,7 +89,7 @@ const uploadToPromise = (uploadRequest, logger) => }); /** - * @returns {Bluebird<{ buildId: number }>} + * @returns {Promise<{ buildId: number }>} */ const uploadImage = function ( imageStream, diff --git a/lib/utils/device/api.ts b/lib/utils/device/api.ts index d95feace..5189eea9 100644 --- a/lib/utils/device/api.ts +++ b/lib/utils/device/api.ts @@ -194,11 +194,11 @@ export class DeviceAPI { }); } - public getLogStream(): Bluebird { + public getLogStream(): Promise { const url = this.getUrlForAction('logs'); // Don't use the promisified version here as we want to stream the output - return new Bluebird((resolve, reject) => { + return new Promise((resolve, reject) => { const req = request.get(url); req.on('error', reject).on('response', async (res) => { diff --git a/lib/utils/device/logs.ts b/lib/utils/device/logs.ts index 44036470..be8fab4d 100644 --- a/lib/utils/device/logs.ts +++ b/lib/utils/device/logs.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird'; import ColorHash = require('color-hash'); import * as _ from 'lodash'; import type { Readable } from 'stream'; @@ -38,8 +37,8 @@ export function displayDeviceLogs( logger: Logger, system: boolean, filterServices?: string[], -): Bluebird { - return new Bluebird((resolve, reject) => { +): Promise { + return new Promise((resolve, reject) => { logs.on('data', (log) => { displayLogLine(log, logger, system, filterServices); }); diff --git a/lib/utils/qemu.ts b/lib/utils/qemu.ts index e48b15f1..c3581e92 100644 --- a/lib/utils/qemu.ts +++ b/lib/utils/qemu.ts @@ -44,7 +44,7 @@ export function copyQemu(context: string, arch: string) { .then(() => getQemuPath(arch)) .then( (qemu) => - new Bluebird(function (resolve, reject) { + new Promise(function (resolve, reject) { const read = fs.createReadStream(qemu); const write = fs.createWriteStream(binPath); @@ -83,7 +83,7 @@ export function installQemu(arch: string) { return getQemuPath(arch).then( (qemuPath) => - new Bluebird(function (resolve, reject) { + new Promise(function (resolve, reject) { const installStream = fs.createWriteStream(qemuPath); const qemuArch = balenaArchToQemuArch(arch); diff --git a/lib/utils/remote-build.ts b/lib/utils/remote-build.ts index e7f9731e..9aa08a03 100644 --- a/lib/utils/remote-build.ts +++ b/lib/utils/remote-build.ts @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ import type { BalenaSDK } from 'balena-sdk'; -import * as Bluebird from 'bluebird'; import * as JSONStream from 'JSONStream'; import * as readline from 'readline'; import * as request from 'request'; @@ -124,7 +123,7 @@ export async function startRemoteBuild(build: RemoteBuild): Promise { } if (!build.opts.headless) { - return new Bluebird((resolve, reject) => { + return new Promise((resolve, reject) => { // Setup interrupt handlers so we can cancel the build if the user presses // ctrl+c diff --git a/lib/utils/ssh.ts b/lib/utils/ssh.ts index b941521d..bb98a390 100644 --- a/lib/utils/ssh.ts +++ b/lib/utils/ssh.ts @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import * as Bluebird from 'bluebird'; import { spawn, StdioOptions } from 'child_process'; import * as _ from 'lodash'; import { TypedError } from 'typed-error'; @@ -71,7 +70,7 @@ export async function exec( 'inherit', ]; - const exitCode = await new Bluebird((resolve, reject) => { + const exitCode = await new Promise((resolve, reject) => { const ps = spawn(program, args, { stdio }) .on('error', reject) .on('close', resolve); diff --git a/lib/utils/streams.ts b/lib/utils/streams.ts index 0f589208..23f51e88 100644 --- a/lib/utils/streams.ts +++ b/lib/utils/streams.ts @@ -14,20 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import Bluebird = require('bluebird'); import fs = require('fs'); export function buffer( stream: NodeJS.ReadableStream, bufferFile: string, -): Bluebird { +): Promise { const fileWriteStream = fs.createWriteStream(bufferFile); - return new Bluebird(function (resolve, reject) { + return new Promise(function (resolve, reject) { stream.on('error', reject).on('end', resolve).pipe(fileWriteStream); }).then( () => - new Bluebird(function (resolve, reject) { + new Promise(function (resolve, reject) { const fstream = fs.createReadStream(bufferFile); fstream.on('open', () => resolve(fstream)).on('error', reject); diff --git a/lib/utils/tunnel.ts b/lib/utils/tunnel.ts index f9a57b36..8d14b398 100644 --- a/lib/utils/tunnel.ts +++ b/lib/utils/tunnel.ts @@ -52,7 +52,7 @@ export const tunnelConnectionToDevice = ( password: token, }; - return (client: Socket): Bluebird => + return (client: Socket): Promise => openPortThroughProxy(vpnUrl, 3128, auth, uuid, port) .then((remote) => { client.pipe(remote); @@ -72,8 +72,9 @@ export const tunnelConnectionToDevice = ( remote.end(); }); }) - .tapCatch(() => { + .catch((e) => { client.end(); + throw e }); }); }; @@ -95,7 +96,7 @@ const openPortThroughProxy = ( httpHeaders.push(`Proxy-Authorization: Basic ${credentials}`); } - return new Bluebird.Promise((resolve, reject) => { + return new Promise((resolve, reject) => { const proxyTunnel = new Socket(); proxyTunnel.on('error', reject); proxyTunnel.connect(proxyPort, proxyServer, () => {