Switch from new Bluebird to native version

Change-type: patch
This commit is contained in:
Pagan Gazzard 2020-06-30 21:24:35 +01:00
parent ef9c2e9c18
commit 984d1a3fd6
13 changed files with 27 additions and 31 deletions

View File

@ -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';

View File

@ -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

View File

@ -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<Args, Options> = {
)
.then(
(server) =>
new Bluebird.Promise<Server>((resolve, reject) => {
new Promise<Server>((resolve, reject) => {
server.on('error', reject);
server.listen(localPort, localAddress, () => {
resolve(server);

View File

@ -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
};

View File

@ -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<string> => {
}): Promise<string> => {
const { app, server } = createServer({ port: options.port });
return new Bluebird<string>((resolve, reject) => {
return new Promise<string>((resolve, reject) => {
app.post(options.path, async (request, response) => {
server.close(); // stop listening for new connections
try {

View File

@ -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,

View File

@ -194,11 +194,11 @@ export class DeviceAPI {
});
}
public getLogStream(): Bluebird<Stream.Readable> {
public getLogStream(): Promise<Stream.Readable> {
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) => {

View File

@ -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<void> {
return new Bluebird((resolve, reject) => {
): Promise<void> {
return new Promise((resolve, reject) => {
logs.on('data', (log) => {
displayLogLine(log, logger, system, filterServices);
});

View File

@ -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);

View File

@ -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<void> {
}
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

View File

@ -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<number>((resolve, reject) => {
const exitCode = await new Promise<number>((resolve, reject) => {
const ps = spawn(program, args, { stdio })
.on('error', reject)
.on('close', resolve);

View File

@ -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<NodeJS.ReadableStream> {
): Promise<NodeJS.ReadableStream> {
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);

View File

@ -52,7 +52,7 @@ export const tunnelConnectionToDevice = (
password: token,
};
return (client: Socket): Bluebird<void> =>
return (client: Socket): Promise<void> =>
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<Socket>((resolve, reject) => {
return new Promise<Socket>((resolve, reject) => {
const proxyTunnel = new Socket();
proxyTunnel.on('error', reject);
proxyTunnel.connect(proxyPort, proxyServer, () => {