Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"fs:delete": "node ./src/fs/delete.js",
"fs:list": "node ./src/fs/list.js",
"fs:read": "node ./src/fs/read.js",
"cli:env": "RSS_CHOOL=!!! node ./src/cli/env.js",
"cli:args": "node ./src/cli/args.js --hello me",
"cli:env": "npx cross-env RSS_AWS=MAYBE RSS_NODEJS=DEFINETELY node ./src/cli/env.js",
"cli:args": "node ./src/cli/args.js --hello me --californian dream",
"modules": "node ./src/modules/cjsToEsm.mjs",
"hash": "node ./src/hash/calcHash.js",
"streams:read": "node ./src/streams/read.js",
Expand Down
19 changes: 8 additions & 11 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
export const parseEnv = () => {
let variables = ''
for (const variable in process.env) {
if (variable.startsWith('RSS_')) {
const varWithVal = `${variable}=${process.env[variable]}`
if (!variables) {
variables = variables + varWithVal ;
} else {
variables = variables + '; ' + varWithVal;
}
const variables = Object.entries(process.env).reduce((accum, [key, value]) => {
if (key.startsWith('RSS_')) {
accum.push(`${key}=${value}`);
}
}
console.log(variables);
return accum;
}, []);

const parsedVariables = variables.join('; ')
console.log(parsedVariables)
};

parseEnv();
18 changes: 13 additions & 5 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

// windows case just in case
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export const spawnChildProcess = async (args) => {
const file = new URL('files/script.js', import.meta.url).pathname;
const ls = spawn('node', [file, ...args]);
process.stdin.pipe(ls.stdin);
const scriptProcess = spawn('node', [`${__dirname}/files/script.js`, ...args]);

process.stdin.on('data', (data) => {
scriptProcess.stdin.write('Master stdin: ' + data)
});

ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
scriptProcess.stdout.on('data', (data) => {
console.log(`Master stdout: Child stdout: ${data}`);
});

};
Expand Down
25 changes: 7 additions & 18 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@ const customError = new Error('FS operation failed');
export const copy = async () => {
const files = new URL('files', import.meta.url);
const files_copy = new URL('files_copy', import.meta.url);
fs.opendir(files_copy)
.then(() => {
console.error(customError);
})
.catch(err => {
if (err.code === 'ENOENT') {
fs.cp(files, files_copy, { recursive: true })
.catch(err => {
if (err.code === 'ENOENT') {
//console.error(customError);
} else {
console.error(new Error(err))
}
})
} else {
console.error(err)
}
})

try {
await fs.mkdir(files_copy, { recursive: false });
await fs.cp(files, files_copy, { recursive: true })
} catch {
throw customError;
}
};

copy();
19 changes: 7 additions & 12 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import * as fs from 'fs/promises';

const customError = new Error('FS operation failed');

export const create = async () => {
const file = new URL('files/fresh.txt', import.meta.url);
fs.open(file)
.then(() => {
const err = new Error('FS operation failed');
console.error(err);
})
.catch(err => {
if (err.code === 'ENOENT') {
fs.writeFile(file, 'I am fresh and young');
} else {
console.error(err);
}
})
try {
await fs.writeFile(file, 'I am fresh and young', { flag: 'wx' });
} catch {
throw customError;
}
};

create();
17 changes: 6 additions & 11 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ const customError = new Error('FS operation failed');

export const remove = async () => {
const file = new URL('files/fileToRemove.txt', import.meta.url);
fs.readFile(file)
.then(() => {
fs.rm(file);
})
.catch(err => {
if (err.code === 'ENOENT') {
console.error(customError);
} else {
console.error(err);
}
})

try {
await fs.rm(file);
} catch {
throw customError;
}
};

remove();
17 changes: 6 additions & 11 deletions src/fs/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ const customError = new Error('FS operation failed');
export const list = async () => {
const files = new URL('files/', import.meta.url);

fs.readdir(files)
.then(res => {
console.log(res);
})
.catch(err => {
if (err.code === 'ENOENT') {
console.error(customError);
} else {
console.error(err);
}
})
try {
const listOfFiles = await fs.readdir(files);
console.log(listOfFiles);
} catch {
throw customError;
}
};

list();
18 changes: 7 additions & 11 deletions src/fs/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ const customError = new Error('FS operation failed');

export const read = async () => {
const file = new URL('files/fileToRead.txt', import.meta.url);
fs.readFile(file, 'utf8')
.then(res => {
console.log(res);
})
.catch(err => {
if (err.code === 'ENOENT') {
console.error(customError);
} else {
console.error(err);
}
})

try {
const fileContent = await fs.readFile(file, 'utf8');
console.log(fileContent);
} catch {
throw customError;
}
};

read();
17 changes: 6 additions & 11 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ const customError = new Error('FS operation failed');
export const rename = async () => {
const fileWrong = new URL('files/wrongFilename.txt', import.meta.url);
const fileProper = new URL('files/properFilename.md', import.meta.url);
fs.readFile(fileProper)
.then(() => {
console.error(customError);
})
.catch(err => {
if (err.code === 'ENOENT') {
fs.rename(fileWrong, fileProper);
} else {
console.error(err)
}
})

try {
await fs.rename(fileWrong, fileProper);
} catch {
throw customError;
}
};

rename();
11 changes: 6 additions & 5 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ export const calculateHash = async () => {
const file = new URL('files/fileToCalculateHashFor.txt', import.meta.url);
const hash = crypto.createHash('sha256');

fs.readFile(file)
.then(res => {
hash.update(res);
console.log(hash.digest('hex'));
})
try {
const content = await fs.readFile(file);
console.log(hash.update(content).digest('hex'));
} catch (err) {
throw err;
}
};

calculateHash();
6 changes: 4 additions & 2 deletions src/modules/cjsToEsm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ const random = Math.random();
let unknownObject;

if (random > 0.5) {
unknownObject = import('./files/a.json', { assert: { type: 'json' } });
unknownObject = await import('./files/a.json', { assert: { type: 'json' } });
} else {
unknownObject = import('./files/b.json', { assert: { type: 'json' } });
unknownObject = await import('./files/b.json', { assert: { type: 'json' } });
}

console.log(unknownObject)

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);
Expand Down
14 changes: 11 additions & 3 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import fs from 'fs';
import fs from 'fs/promises';
import Stream from 'stream';
import { stdout } from 'process';

export const read = async () => {
const file = new URL('files/fileToRead.txt', import.meta.url);
const readableStream = new Stream.Readable();
readableStream._read = () => {};

const stream = fs.createReadStream(file);
stream.pipe(stdout);
try {
const content = await fs.readFile(file);
readableStream.push(content);
readableStream.pipe(stdout);
} catch (err) {
throw err;
}
};

read();
17 changes: 12 additions & 5 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { stdout, stdin } from 'process';
import { Transform } from 'stream';
import { Transform, pipeline } from 'stream';

export const transform = async () => {
class UppercaseTransform extends Transform {
class ReverseTransform extends Transform {
constructor(options) {
super(options);
}

_transform(chunk) {
this.push(Buffer.from([...chunk].reverse()).toString());
_transform(chunk, e, callback) {
callback(null, Buffer.from([...chunk].reverse()).toString());
}
}
stdin.pipe(new UppercaseTransform()).pipe(stdout);
const reverse = new ReverseTransform();

pipeline(
stdin,
reverse,
stdout,
(err) => { throw err; }
)
};

transform();
4 changes: 2 additions & 2 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os from 'os';
import { Worker } from 'worker_threads';

const creator = (i) => new Promise((resolve, reject) => {
const workerCreator = (i) => new Promise((resolve, reject) => {
const file = new URL('./worker.js', import.meta.url);
const worker = new Worker(file, {
workerData: 10 + i
Expand All @@ -16,7 +16,7 @@ export const performCalculations = async () => {

for (let i = 0; i < cpuData.length; i++) {
try {
const data = await creator(i);
const data = await workerCreator(i);
arrResults.push(
{
status: 'resolved',
Expand Down
1 change: 0 additions & 1 deletion src/wt/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacc

export const sendResult = () => {
parentPort.postMessage(nthFibonacci(workerData));
process.exit();
};

sendResult();
3 changes: 1 addition & 2 deletions src/zip/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export const compress = async () => {

pipeline(source, gzip, destination, (err) => {
if (err) {
console.error(err);
process.exitCode = 1;
throw err;
}
});
};
Expand Down
3 changes: 1 addition & 2 deletions src/zip/decompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export const decompress = async () => {

pipeline(source, gunzip, destination, (err) => {
if (err) {
console.error(err);
process.exitCode = 1;
throw err;
}
});
};
Expand Down