Skip to content

Commit

Permalink
Merge pull request #21 from Scopics/main
Browse files Browse the repository at this point in the history
Merge main in release-branch
  • Loading branch information
tedi4t authored Jun 2, 2021
2 parents 80c968c + 299520b commit a5a7e70
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/
static/
test/
test/
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
README.md
test/
test/
4 changes: 3 additions & 1 deletion app/lib/utils/cachingRequire.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const path = require('path');

const cachingRequire = (cacheSize = 10) => {
const DEFAULT_CACHE_SIZE = 10;

const cachingRequire = (cacheSize = DEFAULT_CACHE_SIZE) => {
if (!Number.isInteger(cacheSize) || cacheSize <= 0) {
throw new Error('Cache size must be positive number');
}
Expand Down
4 changes: 2 additions & 2 deletions app/lib/worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const cachingRequire = require('./utils/cachingRequire');
const cachingRequire = require('./utils/cachingRequire.js');
const crequire = cachingRequire();

process.on('message', (message) => {
Expand All @@ -14,7 +14,7 @@ process.on('message', (message) => {
const data = new Uint8ClampedArray(task);
const result = transform(data);

const exportRes = Array.from(result);
const exportRes = result;
process.send({ exportRes, workerId });
} catch (error) {
process.send({ workerId, error });
Expand Down
12 changes: 9 additions & 3 deletions app/processing-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const cp = require('child_process');

const workers = new Array();
const workers = [];

const runner = (countWorkers) => {
for (let i = 0; i < countWorkers; i++) {
Expand All @@ -13,7 +13,9 @@ const runner = (countWorkers) => {
};

const killer = () => {
workers.forEach((worker) => worker.kill('SIGTERM'));
for (const worker of workers) {
worker.kill('SIGTERM');
}
};

const removeListeners = (workers, eventNames, listeners) => {
Expand Down Expand Up @@ -49,13 +51,17 @@ const balancer = (data, countWorkers, method) => {
const { exportRes, workerId, error } = message;
finished++;

if (error) reject(error);
if (error) {
reject(error);
return;
}
if (!exportRes) {
reject(
new Error(
'No transformation function, or the transformation was not successful'
)
);
return;
}

results[workerId] = exportRes;
Expand Down
11 changes: 11 additions & 0 deletions app/transform/brightness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const brightnessTransofrm = (options, data) => {
const { ajustment } = options;
for (let i = 0; i < data.length; i++) {
data[i] += ajustment;
}
return Array.from(data);
};

module.exports = brightnessTransofrm;
24 changes: 17 additions & 7 deletions app/transform/graysacle.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
'use strict';

const grayscaleCofs = {
red: 0.2126,
green: 0.7152,
blue: 0.0722,
};
const PIXEL_DATA_LEN = 4;

module.exports = (data) => {
const grayscaleCofs = [0.2126, 0.7152, 0.0722];
const length = data.length;
for (let i = 0; i < length; i += 4) {
const pictureColors = data.slice(i, i + 3);
for (let i = 0; i < length; i += PIXEL_DATA_LEN) {
const pictureColors = data.slice(i, i + PIXEL_DATA_LEN);

const val = pictureColors.reduce(
(acc, color, index) => acc + color * grayscaleCofs[index],
const val = Object.values(grayscaleCofs).reduce(
(acc, color, index) => acc + color * pictureColors[index],
0
);

data[i] = data[i + 1] = data[i + 2] = val;
const rIndex = i;
const gIndex = i + 1;
const bIndex = i + 2;

data[rIndex] = data[gIndex] = data[bIndex] = val;
}
return data;
return Array.from(data);
};
13 changes: 6 additions & 7 deletions app/transform/transform1.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

module.exports = (data) => {
for (let i = 0; i < data.length; i += 3) {
data[i] += 40;
data[i + 1] += 40;
data[i + 2] += 40;
}
return data;
const brightnessTransofrm = require('./brightness');

const testOptions = {
ajustment: 40,
};

module.exports = brightnessTransofrm.bind(null, testOptions);
13 changes: 6 additions & 7 deletions app/transform/transform2.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

module.exports = (data) => {
for (let i = 0; i < data.length; i += 3) {
data[i] += 50;
data[i + 1] += 50;
data[i + 2] += 50;
}
return data;
const brightnessTransofrm = require('./brightness');

const testOptions = {
ajustment: 30,
};

module.exports = brightnessTransofrm.bind(null, testOptions);
6 changes: 3 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const {
sendError,
sendFile,
processImage,
} = require('./server/utils');
const processingCore = require('./app/processing-core');
const { count, PORT } = require('./server/config');
} = require('./server/utils.js');
const processingCore = require('./app/processing-core.js');
const { count, PORT } = require('./server/config.js');

const transformFilesPath = './app/transform/';
const methods = new Set();
Expand Down
27 changes: 10 additions & 17 deletions server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,21 @@ const getMethods = (directory) =>
new Promise((resolve) => {
fs.readdir(directory, (err, files) => {
if (err) {
resolve(new Array());
resolve([]);
return;
}
const baseNames = files.map(getBaseName);
resolve(baseNames);
});
});

const getArgs = (req) =>
new Promise((resolve, reject) => {
const chuncks = [];
try {
req.on('data', (chunck) => {
chuncks.push(chunck);
});
req.on('end', () => {
const args = JSON.parse(chuncks.join(''));
resolve(args);
});
} catch (e) {
reject(e);
}
});
const getArgs = async (req) => {
const buffers = [];
for await (const chunk of req) {
buffers.push(chunk);
}
return Buffer.concat(buffers).toString();
};

function sendError(res, statusCode, message) {
res.statusCode = statusCode || 500;
Expand All @@ -55,7 +47,8 @@ function sendFile(res, file, fileExt) {

async function processImage(req, method) {
const args = await getArgs(req);
const imageData = args.data;
const body = JSON.parse(args);
const imageData = body.data;
const params = [imageData, count, method];
const processedImage = processingCore.balancer(...params);
return processedImage;
Expand Down
25 changes: 11 additions & 14 deletions static/client.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
const currentUrl = window.location.pathname;
const errorBlockElement = document.getElementById('error-block');
const url = `/api${currentUrl}`;
generateBtn.addEventListener('click', () => {
generateBtn.addEventListener('click', async () => {
const body = {
data: Array.from(state.imageData.data),
};

fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
.then((response) => {
return response.json();
})
.then((data) => {
imageDataToImg(data);
})
.catch(() => {
errorBlockElement.innerHTML = 'Error happened';
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
const imageData = await response.json();
imageDataToImg(imageData);
} catch (e) {
errorBlockElement.innerHTML = 'Error happened';
}
});
2 changes: 1 addition & 1 deletion test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ metatests.test('test getMethods function', async (test) => {
const testData = [
{
args: [path.join(__dirname, '../app/transform')],
expected: ['graysacle', 'transform-error', 'transform1', 'transform2']
expected: ['brightness', 'graysacle', 'transform-error', 'transform1', 'transform2']
},
{
args: [path.join(__dirname, '../app/transform/unexist')],
Expand Down

0 comments on commit a5a7e70

Please sign in to comment.