Skip to content

Commit

Permalink
Reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
datyin committed Nov 19, 2020
1 parent 71cfff6 commit b4fdd19
Show file tree
Hide file tree
Showing 24 changed files with 1,702 additions and 3,160 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/*
node_modules/*
compiler/*
example/*
21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2019,
sourceType: 'module'
},
env: {
node: true,
browser: false
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:prettier/recommended'
],
rules: {
'@typescript-eslint/no-var-requires': 'off'
}
};
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
.DS_Store
.thumbs.db
node_modules/
example.response.json
dist/

# Log files
/logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Editor directories and files
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
27 changes: 27 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Defaults
.*.swp
._*
.DS_Store
.git
.hg
.npmrc
.lock-wscript
.svn
.wafpickle-*
config.gypi
CVS
npm-debug.log

.vscode/
node_modules/
src/
dist/
compiler/
compiler.js
tsconfig.json
webpack.config.js
.eslintignore
.eslintrc.*
.prettierrc*
prettier.config.*
_config.yml
8 changes: 8 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
semi: true,
tabWidth: 2,
trailingComma: 'none',
endOfLine: 'lf',
printWidth: 100,
singleQuote: true
};
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files.eol": "\n"
}
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
# Send Asynchronous Requests

Send multiple asynchronous requests using [Axios](https://github.com/axios/axios) & [Workers](https://nodejs.org/api/worker_threads.html) in NodeJS

> NOTE: This module is meant for [Node.JS](https://nodejs.org/) applications, not Web Browser.
# Installation
`npm install axios-parallel --save`

`npm install axios axios-parallel --save`

# Usage

```javascript
const axiosParallel = require('axios-parallel');

// Debug
const { performance } = require('perf_hooks');
const { writeFileSync } = require('fs');

console.log('Start...');

(async () => {
const start = new Date();
const start = performance.now();
const requests = [];

// https://jsonplaceholder.typicode.com/posts
Expand All @@ -35,11 +41,9 @@ console.log('Start...');
});
} catch (error) {
throw new Error(error);
} finally {
const end = new Date() - start;
console.log(`Execution time: ${end}ms`);
}

console.log('Done');
const end = performance.now() - start;
console.log(`Execution time: ${end}ms`);
})();
```
```
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
29 changes: 29 additions & 0 deletions compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const yargs = require('yargs/yargs');
const webpack = require('webpack');
const config = require('./compiler/config');
const execute = require('./compiler/execute');

const args = yargs(process.argv).argv;
const webpackConfig = config.webpack(args);

webpack(webpackConfig, (err, stats) => {
if (err) {
console.error(err);
return;
}

const statsInfo = stats.toJson();

console.log(
stats.toString({
chunks: false,
colors: true
})
);

if (args.dev) {
execute(args, statsInfo);
} else {
config.package(args, statsInfo);
}
});
82 changes: 82 additions & 0 deletions compiler/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const { isPlainObject, clone } = require('lodash');
const { resolve } = require('path');
const { writeJsonSync } = require('fs-extra');
const webpackConfig = require('../webpack.config');
const package = require('../package.json');

function generateWebpackConfig(args) {
if (!isPlainObject(webpackConfig)) {
return {
target: 'node12.19',
mode: 'development',
node: {
global: false,
__filename: false,
__dirname: false
}
};
}

if (args.dev) {
webpackConfig.mode = 'development';
webpackConfig.watch = true;

if (!webpackConfig.watchOptions) {
webpackConfig.watchOptions = {
aggregateTimeout: 200,
ignored: ['node_modules/**', 'compiler.js']
};
} else {
if (!webpackConfig.watchOptions.ignored) {
webpackConfig.watchOptions.ignored = ['node_modules/**', 'compiler.js'];
}
}
} else {
webpackConfig.mode = 'production';
webpackConfig.watch = false;
}

if (!webpackConfig.output || !webpackConfig.path) {
webpackConfig.output = {
path: resolve(__dirname, '..', 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs2'
};
}

return webpackConfig;
}

function generatePackage(args, stats) {
const productionPackage = clone(package);

if (productionPackage.scripts) delete productionPackage.scripts;
if (productionPackage.dependencies) delete productionPackage.dependencies;
if (productionPackage.devDependencies) delete productionPackage.devDependencies;

const externalModules = stats.modules
.filter((m) => m.identifier && m.identifier.startsWith('external '))
.map((m) => m.name.replace('external ', '').replace(/"/g, ''));

const dependencies = {};

externalModules.forEach((p) => {
if (package.dependencies && package.dependencies[p]) {
dependencies[p] = package.dependencies[p];
}
});

productionPackage.dependencies = dependencies;

try {
writeJsonSync(`${stats.outputPath}/package.json`, productionPackage, {
spaces: 2,
encoding: 'utf8'
});
} catch (error) {
console.log('Failed to write package.json', error.message);
}
}

module.exports.webpack = generateWebpackConfig;
module.exports.package = generatePackage;
65 changes: 65 additions & 0 deletions compiler/execute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const chalk = require('chalk');
const { spawn } = require('child_process');
const { pathExistsSync } = require('fs-extra');
const { trim, forEach } = require('lodash');
const { resolve, normalize } = require('path');

const prefix = chalk`[{magenta Execute}]`;
const errorPrefix = chalk`[{red ERROR}]`;

let child = null;

module.exports = async function (args, stats) {
let entry = trim(args.entry);

if (entry && !entry.endsWith('.js')) {
entry = `${entry}.js`;
}

let path = '';

if (entry && pathExistsSync(`${stats.outputPath}/${entry}`)) {
path = `${stats.outputPath}/${entry}`;
} else if (pathExistsSync(`${stats.outputPath}/index.js`)) {
path = `${stats.outputPath}/index.js`;
} else if (pathExistsSync(`${stats.outputPath}/main.js`)) {
path = `${stats.outputPath}/main.js`;
} else if (pathExistsSync(`${stats.outputPath}/app.js`)) {
path = `${stats.outputPath}/app.js`;
}

if (!path) {
console.log(prefix, errorPrefix, 'Failed to find entry point to be executed!');
return;
}

path = normalize(path);

if (child) {
console.log(prefix, chalk`Restarting script {greenBright.bold ${path}}`);
child.stdin.pause();
child.kill();
} else {
console.log(prefix, chalk`Starting script {greenBright.bold ${path}}`);
}

const arguments = [path];

forEach(args, (value, index) => {
if (index !== '_' && index !== '$0') {
arguments.push(`--${index}=${value}`);
}
});

child = spawn(args._[0], arguments, {
cwd: resolve(__dirname, '..'),
encoding: 'utf8'
});

child.stderr.on('data', (data) =>
console.log(prefix, errorPrefix, chalk`{red ${data.toString()}}`)
);

child.stdout.on('data', (data) => console.log(prefix, data.toString()));
child.on('close', (code) => console.log(prefix, `Process closed with code ${code}`));
};
17 changes: 10 additions & 7 deletions test/index.js → example/jsonplaceholder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const axiosParallel = require('../dist/index.js');
const axiosParallel = require('axios-parallel');

// Debug
const { performance } = require('perf_hooks');
const { writeFileSync } = require('fs');

console.log('Start...');

(async () => {
const start = new Date();
const start = performance.now();
const requests = [];

// https://jsonplaceholder.typicode.com/posts
Expand All @@ -17,16 +20,16 @@ console.log('Start...');
}

try {
const MAX_ASYNC_REQUEST_PER_CPU = 30;
const response = await axiosParallel(requests, MAX_ASYNC_REQUEST_PER_CPU);
const MAX_PARALLEL_REQUEST_PER_CPU = 30;
const response = await axiosParallel(requests, MAX_PARALLEL_REQUEST_PER_CPU);

writeFileSync('example.response.json', JSON.stringify(response), {
encoding: 'utf8'
});
} catch (error) {
throw new Error(error);
} finally {
const end = new Date() - start;
console.log(`Execution time: ${end}ms`);
}

const end = performance.now() - start;
console.log(`Execution time: ${end}ms`);
})();
9 changes: 0 additions & 9 deletions index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion index.js

This file was deleted.

Loading

0 comments on commit b4fdd19

Please sign in to comment.