Skip to content

Commit

Permalink
refactor(core): Replace promisify-d node calls with native promises
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Jan 26, 2024
1 parent 2fba0e8 commit d7a9c70
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 46 deletions.
5 changes: 2 additions & 3 deletions .github/scripts/check-tests.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import fs from 'fs';
import { readFile } from 'fs/promises';
import path from 'path';
import util from 'util';
import { exec } from 'child_process';
import { glob } from 'glob';
import ts from 'typescript';

const readFileAsync = util.promisify(fs.readFile);
const execAsync = util.promisify(exec);

const filterAsync = async (asyncPredicate, arr) => {
Expand Down Expand Up @@ -37,7 +36,7 @@ const isAbstractMethod = (node) => {

// Function to check if a file has a function declaration, function expression, object method or class
const hasFunctionOrClass = async (filePath) => {
const fileContent = await readFileAsync(filePath, 'utf-8');
const fileContent = await readFile(filePath, 'utf-8');
const sourceFile = ts.createSourceFile(filePath, fileContent, ts.ScriptTarget.Latest, true);

let hasFunctionOrClass = false;
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/WebhookHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import type express from 'express';
import { Container } from 'typedi';
import get from 'lodash/get';
import stream from 'stream';
import { promisify } from 'util';
import { pipeline } from 'stream/promises';
import formidable from 'formidable';

import { BinaryDataService, NodeExecuteFunctions } from 'n8n-core';
Expand Down Expand Up @@ -65,8 +64,6 @@ import { NotFoundError } from './errors/response-errors/not-found.error';
import { InternalServerError } from './errors/response-errors/internal-server.error';
import { UnprocessableRequestError } from './errors/response-errors/unprocessable.error';

const pipeline = promisify(stream.pipeline);

export const WEBHOOK_METHODS: IHttpRequestMethods[] = [
'DELETE',
'GET',
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { Flags, type Config } from '@oclif/core';
import path from 'path';
import { mkdir } from 'fs/promises';
import { createReadStream, createWriteStream, existsSync } from 'fs';
import stream from 'stream';
import { pipeline } from 'stream/promises';
import replaceStream from 'replacestream';
import { promisify } from 'util';
import glob from 'fast-glob';
import { sleep, jsonParse } from 'n8n-workflow';

Expand All @@ -31,7 +30,6 @@ import { BaseCommand } from './BaseCommand';

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
const open = require('open');
const pipeline = promisify(stream.pipeline);

export class Start extends BaseCommand {
static description = 'Starts n8n. Makes Web-UI available and starts active workflows';
Expand Down
9 changes: 2 additions & 7 deletions packages/node-dev/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { Command } from '@oclif/core';
import * as changeCase from 'change-case';
import * as fs from 'fs';
import { access } from 'fs/promises';
import * as inquirer from 'inquirer';
import { join } from 'path';

import { createTemplate } from '../src';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { promisify } = require('util');

const fsAccess = promisify(fs.access);

export class New extends Command {
static description = 'Create new credentials/node';

Expand Down Expand Up @@ -113,7 +108,7 @@ export class New extends Command {
// Check if node with the same name already exists in target folder
// to not overwrite it by accident
try {
await fsAccess(destinationFilePath);
await access(destinationFilePath);

// File does already exist. So ask if it should be overwritten.
const overwriteQuestion: inquirer.QuestionCollection = [
Expand Down
10 changes: 2 additions & 8 deletions packages/node-dev/src/Create.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import * as fs from 'fs';
import { copyFile } from 'fs/promises';
import type { ReplaceInFileConfig } from 'replace-in-file';
import { replaceInFile } from 'replace-in-file';

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
const { promisify } = require('util');

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const fsCopyFile = promisify(fs.copyFile);

/**
* Creates a new credentials or node
*
Expand All @@ -22,7 +16,7 @@ export async function createTemplate(
): Promise<void> {
// Copy the file to then replace the values in it
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await fsCopyFile(sourceFilePath, destinationFilePath);
await copyFile(sourceFilePath, destinationFilePath);

// Replace the variables in the template file
const options: ReplaceInFileConfig = {
Expand Down
10 changes: 3 additions & 7 deletions packages/nodes-base/nodes/Crypto/Crypto.node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { BinaryToTextEncoding } from 'crypto';
import { createHash, createHmac, createSign, getHashes, randomBytes } from 'crypto';
import stream from 'stream';
import { promisify } from 'util';
import { pipeline } from 'stream/promises';
import { v4 as uuid } from 'uuid';
import set from 'lodash/set';
import type {
IExecuteFunctions,
INodeExecutionData,
Expand All @@ -10,11 +11,6 @@ import type {
JsonObject,
} from 'n8n-workflow';
import { deepCopy, BINARY_ENCODING } from 'n8n-workflow';
import set from 'lodash/set';

import { v4 as uuid } from 'uuid';

const pipeline = promisify(stream.pipeline);

const unsupportedAlgorithms = [
'RSA-MD4',
Expand Down
8 changes: 3 additions & 5 deletions packages/nodes-base/nodes/EditImage/EditImage.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parse as pathParse } from 'path';
import { writeFile as fsWriteFile } from 'fs';
import { promisify } from 'util';
import { writeFile as fsWriteFile, type FileHandle } from 'fs/promises';

Check failure on line 2 in packages/nodes-base/nodes/EditImage/EditImage.node.ts

View workflow job for this annotation

GitHub Actions / Lint changes

'FileHandle' is defined but never used
import type {
IDataObject,
IExecuteFunctions,
Expand All @@ -14,7 +13,6 @@ import type {
import { deepCopy } from 'n8n-workflow';
import gm from 'gm';
import { file } from 'tmp-promise';
const fsWriteFileAsync = promisify(fsWriteFile);
import getSystemFonts from 'get-system-fonts';

const nodeOperations: INodePropertyOptions[] = [
Expand Down Expand Up @@ -1117,9 +1115,9 @@ export class EditImage implements INodeType {
binaryPropertyName,
);

const { fd, path, cleanup } = await file();
const { path, cleanup } = await file();
cleanupFunctions.push(cleanup);
await fsWriteFileAsync(fd, binaryDataBuffer);
await fsWriteFile(path, binaryDataBuffer);

if (operations[0].operation === 'create') {
// It seems like if the image gets created newly we have to create a new gm instance
Expand Down
14 changes: 5 additions & 9 deletions packages/nodes-base/nodes/Ftp/Ftp.node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createWriteStream } from 'fs';
import { basename, dirname } from 'path';
import type { Readable } from 'stream';
import { pipeline } from 'stream';
import { promisify } from 'util';
import { pipeline } from 'stream/promises';
import { file as tmpFile } from 'tmp-promise';
import ftpClient from 'promise-ftp';
import sftpClient from 'ssh2-sftp-client';
import { BINARY_ENCODING, NodeApiError } from 'n8n-workflow';
import type {
ICredentialDataDecryptedObject,
Expand All @@ -16,10 +18,6 @@ import type {
INodeTypeDescription,
JsonObject,
} from 'n8n-workflow';
import { file as tmpFile } from 'tmp-promise';

import ftpClient from 'promise-ftp';
import sftpClient from 'ssh2-sftp-client';
import { formatPrivateKey } from '@utils/utilities';

interface ReturnFtpItem {
Expand All @@ -40,8 +38,6 @@ interface ReturnFtpItem {
path: string;
}

const streamPipeline = promisify(pipeline);

async function callRecursiveList(
path: string,
client: sftpClient | ftpClient,
Expand Down Expand Up @@ -722,7 +718,7 @@ export class Ftp implements INodeType {
const binaryFile = await tmpFile({ prefix: 'n8n-sftp-' });
try {
const stream = await ftp!.get(path);
await streamPipeline(stream, createWriteStream(binaryFile.path));
await pipeline(stream, createWriteStream(binaryFile.path));

const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
const remoteFilePath = this.getNodeParameter('path', i) as string;
Expand Down

0 comments on commit d7a9c70

Please sign in to comment.