Skip to content

Commit

Permalink
Selfcheck for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcardeenas committed Apr 10, 2020
1 parent cfab6e6 commit 31832eb
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 104 deletions.
Empty file added UPDATES.md
Empty file.
230 changes: 145 additions & 85 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"start": "tsc app.ts && node app.js",
"start": "npm run build:sulla & tsc app.ts && node app.js",
"build": "npm run build:wapi && npm run build:middleware && npm run build:jsQR && tsc",
"build:sulla": "tsc",
"build:wapi": "cd src/lib/wapi/ && webpack",
Expand Down Expand Up @@ -78,10 +78,13 @@
"webpack-cli": "^3.3.11"
},
"dependencies": {
"better-logging": "^4.0.4",
"boxen": "^4.2.0",
"chalk": "^4.0.0",
"chrome-launcher": "^0.13.1",
"file-type": "^14.1.4",
"futoin-hkdf": "^1.3.2",
"ora": "^4.0.3",
"latest-version": "^5.1.0",
"puppeteer": "^2.1.1",
"puppeteer-extra": "^3.1.9",
"puppeteer-extra-plugin-stealth": "^2.4.9",
Expand Down
4 changes: 2 additions & 2 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ export class SenderLayer extends ListenerLayer {
);
const mimeInfo = base64MimeType(b64);
if (!mimeInfo || mimeInfo.includes('image')) {
//non matter what, convert to webp, resize + autoscale to width 512 px
// Convert to webp, resize + autoscale to width 512 px
const scaledImageBuffer = await sharp(buff, { failOnError: false })
.resize({ width: 512, height: 512 })
.toBuffer();
const webp = sharp(scaledImageBuffer, { failOnError: false }).webp();
const metadata: any = await webp.metadata();
const metadata = (await webp.metadata()) as any;
const webpBase64 = (await webp.toBuffer()).toString('base64');
return await this.page.evaluate(
({ webpBase64, to, metadata }) =>
Expand Down
13 changes: 9 additions & 4 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ export const isInsideChat = (waPage: puppeteer.Page) => {

export async function retrieveQR(page: puppeteer.Page) {
const { code, data } = await decodeQR(page);
qrcode.generate(code, {
small: true,
});
const asciiQR = await asciiQr(code);
return { code, data, asciiQR };
}

return { code, data };
async function asciiQr(code: string): Promise<string> {
return new Promise((resolve, reject) => {
qrcode.generate(code, { small: true }, (qrcode) => {
resolve(qrcode);
});
});
}

async function decodeQR(
Expand Down
55 changes: 44 additions & 11 deletions src/controllers/initializer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import * as ora from 'ora';
import { readFileSync } from 'fs';
import latestVersion from 'latest-version';
import { Whatsapp } from '../api/whatsapp';
import { CreateConfig } from '../config/create-config';
import { isAuthenticated, isInsideChat, retrieveQR } from './auth';
import { initWhatsapp, injectApi } from './browser';
const spinner = ora();
import { upToDate } from '../utils/semver';
import chalk = require('chalk');
import boxen = require('boxen');

const { version } = require('../../package.json');

/**
* Should be called to initialize whatsapp client
Expand All @@ -21,31 +25,29 @@ export async function create(
debug: false,
};

spinner.start('Initializing whatsapp');
checkSullaVersion();
let waPage = await initWhatsapp(session, { ...defaultOptions, ...options });
spinner.succeed();

spinner.start('Authenticating');
console.log('Initializing sulla');
console.log('Authenticating');
const authenticated = await isAuthenticated(waPage);

// If not authenticated, show QR and wait for scan
if (authenticated) {
spinner.succeed();
} else {
spinner.info('Authenticate to continue');
const { data } = await retrieveQR(waPage);
console.log('Authenticate to continue');
const { data, asciiQR } = await retrieveQR(waPage);
if (catchQR) {
catchQR(data);
}

// Wait til inside chat
await isInsideChat(waPage).toPromise();
spinner.succeed();
}

spinner.start('Injecting api');
console.log('Injecting api');
waPage = await injectApi(waPage);
spinner.succeed('Whatsapp is ready');
console.log('Whatsapp is ready');

if (options.debug) {
const debugURL = `http://localhost:${readFileSync(
Expand All @@ -56,3 +58,34 @@ export async function create(

return new Whatsapp(waPage);
}

/**
* Checs for a new versoin of sulla and logs
*/
function checkSullaVersion() {
latestVersion('sulla').then((latest) => {
if (!upToDate(version, latest)) {
logUpdateAvailable(version, latest);
}
});
}

/**
* Logs a boxen of instructions to update
* @param current
* @param latest
*/
function logUpdateAvailable(current: string, latest: string) {
// prettier-ignore
const newVersionLog =
`There is a new version of ${chalk.bold(`sulla`)} ${chalk.gray(current)}${chalk.bold.green(latest)}\n` +
`Update your package by running:\n\n` +
`${chalk.bold('\>')} ${chalk.blueBright('npm update sulla')}`;

console.log(boxen(newVersionLog, { padding: 1 }));
console.log(
`For more info visit: ${chalk.underline(
'https://github.com/danielcardeenas/sulla/blob/master/UPDATES.md'
)}\n`
);
}
28 changes: 28 additions & 0 deletions src/utils/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const VPAT = /^\d+(\.\d+){0,2}$/;

/**
* Compares two versions
* @return true if local is up to date, false otherwise
* @param local
* @param remote
*/
export function upToDate(local: string, remote: string) {
if (!local || !remote || local.length === 0 || remote.length === 0)
return false;
if (local == remote) return true;
if (VPAT.test(local) && VPAT.test(remote)) {
const lparts = local.split('.');
while (lparts.length < 3) lparts.push('0');
const rparts = remote.split('.');
while (rparts.length < 3) rparts.push('0');
for (let i = 0; i < 3; i++) {
const l = parseInt(lparts[i], 10);
const r = parseInt(rparts[i], 10);
if (l === r) continue;
return l > r;
}
return true;
} else {
return local >= remote;
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
/* Basic Options */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"resolveJsonModule": true,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
Expand Down

0 comments on commit 31832eb

Please sign in to comment.