Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
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
27 changes: 24 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,38 @@
"description": "This repository is the part of nodejs-assignments https://github.com/AlreadyBored/nodejs-assignments",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"fs:create": "node src/fs/create.js",
"fs:copy": "node src/fs/copy.js",
"fs:rename": "node src/fs/rename.js",
"fs:delete": "node src/fs/delete.js",
"fs:list": "node src/fs/list.js",
"fs:read": "node src/fs/read.js",
"cli:env": "node src/cli/env.js",
"cli:args": "node src/cli/args.js",
"modules:cjsToEsm": "node src/modules/cjsToEsm.js",
"hash:calcHash": "node src/hash/calcHash.js",
"streams:read": "node src/streams/read.js",
"streams:write": "node src/streams/write.js",
"streams:transform": "node src/streams/transform.js",
"zip:compress": "node src/zip/compress.js",
"zip:decompress": "node src/zip/decompress.js",
"wt:worker": "node src/wt/worker.js",
"wt:main": "node src/wt/main.js",
"cp:cp": "node src/cp/cp.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AlreadyBored/node-nodejs-basics.git"
},
"keywords": ["nodejs", "assignments", "alreadybored"],
"keywords": [
"nodejs",
"assignments",
"alreadybored"
],
"author": "alreadybored",
"license": "ISC",
"bugs": {
"url": "https://github.com/AlreadyBored/node-nodejs-basics/issues"
},
"homepage": "https://github.com/AlreadyBored/node-nodejs-basics#readme"
}
}
10 changes: 8 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const parseArgs = () => {
// Write your code here
};
const args = process.argv.slice(2);
args.forEach((_, i) => {
if (i % 2) return;
console.log(`${args[i].slice(2)} is ${args[i + 1]}`);
});
};

parseArgs();
10 changes: 8 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const parseEnv = () => {
// Write your code here
};
Object.entries(process.env)
.forEach(([key, value]) => {
if (key.includes('RSS_')) console.log(`${key}=${value}`);
});
};


parseEnv();
25 changes: 22 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
export const spawnChildProcess = async (args) => {
// Write your code here
};
import * as path from "path";
import { fileURLToPath } from "url";
import { fork } from 'child_process';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export const spawnChildProcess = async (args = [1,2,3]) => {
if (process.argv[2]) args = process.argv.slice(2);

const child = fork(path.join(__dirname, 'files/script.js'), args, { silent: true });

child
.on('spawn', () => console.log('Child args: ' + args))
.on('exit', (code) => console.log(`Child exit with code: ${code}`));

child.stdout.pipe(process.stdout);
child.stdout.on('data', (chunk) => console.log('Chunk in child: ' + chunk));
process.stdin.pipe(child.stdin);
};

spawnChildProcess();
34 changes: 32 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";


const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const destination = path.join(__dirname, "files-copy");
const source = path.join(__dirname, "files");

export const copy = async () => {
// Write your code here
};
if (!fs.existsSync(source) || fs.existsSync(destination)) {
throw new Error('FS operation failed');
}
fs.mkdir(
destination,
(err) => {
if(err) throw new Error('FS operation failed');
}
);
fs.cp(
source,
destination,
{ recursive: true },
(err) => {
if(err) throw new Error('FS operation failed');
}
);
};


copy();
19 changes: 17 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export const create = async () => {
// Write your code here
};
const data = 'I am fresh and young';
const filePath = path.join(__dirname, 'files', 'fresh.txt');
fs.writeFile(filePath, data, { flag: 'wx' }, (err) => {
if (err) throw new Error('FS operation failed');
})
};

create();


20 changes: 18 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const fileToRemovePath = path.join(__dirname, 'files/fileToRemove.txt');

export const remove = async () => {
// Write your code here
};
fs.unlink(
fileToRemovePath,
(err) => {
if (err) throw new Error('FS operation failed');
}
);
};

remove();
21 changes: 19 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const folder = path.join(__dirname, 'files');

export const list = async () => {
// Write your code here
};
fs.readdir(
folder,
(err, files) => {
if (err) throw new Error('FS operation failed');
files.forEach(file => console.log(file))
}
);
};

list();
22 changes: 20 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const fileToReadPath = path.join(__dirname, 'files/fileToRead.txt');

export const read = async () => {
// Write your code here
};
fs.readFile(
fileToReadPath,
'utf8',
(err, data) => {
if (err) throw new Error('FS operation failed');
console.log(data);
}
);
};

read();
25 changes: 23 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const properFilePath = path.join(__dirname, 'files/properFilename.md');
const wrongFilePath = path.join(__dirname, 'files/wrongFilename.txt');

export const rename = async () => {
// Write your code here
};
if(fs.existsSync(properFilePath)) {
throw new Error('FS operation failed');
}
fs.rename(
wrongFilePath,
properFilePath,
(err) => {
if (err) throw new Error('FS operation failed');
}
);
};

rename();
1 change: 1 addition & 0 deletions src/fs/tempCodeRunnerFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
folder
21 changes: 19 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import fs from 'fs/promises';
import * as path from 'path';
import * as crypto from 'crypto';
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const fileToCalculatePath = path.join(__dirname, 'files/fileToCalculateHashFor.txt');

export const calculateHash = async () => {
// Write your code here
};
const file = await fs.readFile(fileToCalculatePath);
const hashSum = crypto.createHash('sha256');
hashSum.update(file);
const hex = hashSum.digest('hex');
console.log(hex);
return hex;
};

calculateHash();
17 changes: 12 additions & 5 deletions src/modules/cjsToEsm.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
import * as path from 'path';
import { fileURLToPath } from 'url';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c.js';
import { createRequire } from 'module';


const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url)

const random = Math.random();

Expand All @@ -24,7 +31,7 @@ const createMyServer = createServerHttp((_, res) => {
res.end('Request accepted');
});

module.exports = {
export {
unknownObject,
createMyServer,
};
Expand Down
17 changes: 15 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from "url";
import { stdout } from 'node:process';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const fileToReadPath = path.join(__dirname, 'files/fileToRead.txt');

export const read = async () => {
// Write your code here
};
const stream = fs.createReadStream(fileToReadPath);
stream.pipe(stdout);
};

read();
18 changes: 16 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// NEEDS TO REFACTOR

import { stdin, stdout } from 'node:process';
import { Transform } from 'stream';

export const transform = async () => {
// Write your code here
};
const transform = new Transform (
{
transform: (chunk, _, callback) => {
const reverse = chunk.reverse();
callback(null, `${reverse}\n`);
}
});
stdin.pipe(transform).pipe(stdout);
};

transform();
17 changes: 15 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from "url";
import { stdout } from 'node:process';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const fileToWritePath = path.join(__dirname, 'files/fileToRead.txt');

export const write = async () => {
// Write your code here
};
const stream = fs.createWriteStream(fileToWritePath);
stdin.pipe(stream);
};

write();
Loading