Skip to content

Commit ef9608b

Browse files
revise node js incorporation
1 parent 171fa38 commit ef9608b

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

scriptum.js

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Javascript. */
1919

2020

2121
import Child from "node:child_process";
22-
import Crypto from "node:crypto";
23-
import FS from "node:fs";
24-
import Path from "node:path";
25-
import Stream from "node:stream";
22+
import Crypto_ from "node:crypto";
23+
import FS_ from "node:fs";
24+
import Path_ from "node:path";
25+
import Stream_ from "node:stream";
2626
//import * as I from "immutable";
2727

2828

@@ -3752,13 +3752,13 @@ Ait.interposeArr = ({sep, trailing = true}) => async function* (xs) {
37523752
37533753
const writable = fs.createWriteStream("./awords.txt");
37543754
3755-
const sx = stream.compose(
3755+
const sx = Node.Stream.compose(
37563756
Ait.from(fs.createReadStream("./words.txt")),
37573757
Ait.chunk({sep: /\r?\n/}),
37583758
Ait.filter(line => line[0] === "a"),
37593759
Ait.map(line => line + "\n"));
37603760
3761-
stream.pipeline(sx, writable, e => {
3761+
Node.Stream.pipeline(sx, writable, e => {
37623762
if (e) console.error(e);
37633763
else console.log("done");
37643764
});
@@ -3797,7 +3797,7 @@ is provided as chunks of
37973797
37983798
Example usage:
37993799
3800-
const sx = stream.compose(
3800+
const sx = Node.Stream.compose(
38013801
Ait.from(fs.createReadStream("./words.txt")),
38023802
Ait.chunk({sep: /\r?\n/}),
38033803
Ait.overlappingChunks(3)) */
@@ -11841,7 +11841,12 @@ Alg.manhattan = (xs, ys) => {
1184111841
███████████████████████████████████████████████████████████████████████████████*/
1184211842

1184311843

11844-
export const Node = {};
11844+
export const Node = {
11845+
FS: FS_,
11846+
Path: Path_,
11847+
Stream: Stream_,
11848+
Crypto: Crypto_,
11849+
};
1184511850

1184611851

1184711852
/*█████████████████████████████████████████████████████████████████████████████
@@ -11961,23 +11966,23 @@ Node.CLA.setEnv = o => {
1196111966
███████████████████████████████████████████████████████████████████████████████*/
1196211967

1196311968

11964-
Node.Crypto = {};
11969+
Node.Crypto_ = {}; // custom crypto namespace
1196511970

1196611971

1196711972
/* Encrypt with 256-bit private key. If this key is derived from a user password,
1196811973
you must use a strong key derivation function (KDF) like Argon2id or PBKDF2 with
1196911974
a high iteration count and a unique salt per user before passing the resulting
1197011975
key buffer to the encryption function. */
1197111976

11972-
Node.Crypto.encryptSym = key => plaintext => {
11977+
Node.Crypto_.encryptSym = key => plaintext => {
1197311978
if (!Buffer.isBuffer(key) || key.length !== 32)
1197411979
throw new Err("32-byte buffer expected");
1197511980

1197611981
else if (typeof plaintext !== "string")
1197711982
throw new Err("Plaintext must be a string.");
1197811983

11979-
const iv = crypto.randomBytes(12),
11980-
cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
11984+
const iv = Node.Crypto.randomBytes(12),
11985+
cipher = Node.Crypto.createCipheriv("aes-256-gcm", key, iv);
1198111986

1198211987
let ciphertext = cipher.update(plaintext, "utf8", "base64");
1198311988
ciphertext += cipher.final("base64");
@@ -11994,7 +11999,7 @@ Node.Crypto.encryptSym = key => plaintext => {
1199411999

1199512000
// decrypt with 256-bit private key
1199612001

11997-
Node.Crypto.decryptSym = ({ key, iv, tag }) => ciphertext => {
12002+
Node.Crypto_.decryptSym = ({ key, iv, tag }) => ciphertext => {
1199812003
if (!Buffer.isBuffer(key) || key.length !== 32)
1199912004
throw new Err("32-byte buffer expected");
1200012005

@@ -12010,7 +12015,7 @@ Node.Crypto.decryptSym = ({ key, iv, tag }) => ciphertext => {
1201012015
if (ivBuf.length !== 12)
1201112016
throw new Err("invalid iv length provided for gcm decryption");
1201212017

12013-
const decipher = crypto.createDecipheriv("aes-256-gcm", key, ivBuf);
12018+
const decipher = Node.Crypto.createDecipheriv("aes-256-gcm", key, ivBuf);
1201412019
decipher.setAuthTag(tagBuf);
1201512020

1201612021
let plaintext = decipher.update(ciphertext, "base64", "utf8");
@@ -12026,7 +12031,7 @@ Node.Crypto.decryptSym = ({ key, iv, tag }) => ciphertext => {
1202612031

1202712032
// generate 256-bit private key
1202812033

12029-
Node.Crypto.createKey256 = () => Crypto.randomBytes(32).toString("base64");
12034+
Node.Crypto_.createKey256 = () => Node.Crypto.randomBytes(32).toString("base64");
1203012035

1203112036

1203212037
// TODO: add asymetric encription
@@ -12056,55 +12061,55 @@ a+: open file for appending and reading, file is created if it does not exist, s
1205612061
*/
1205712062

1205812063

12059-
Node.FS = {};
12064+
Node.FS_ = {}; // custom file system namespace
1206012065

1206112066

12062-
Node.FS.read = opt => path => Cont((res, rej) =>
12063-
FS.readFile(path, opt, (e, x) => e ? rej(new Err(e)) : res(x)));
12067+
Node.FS_.read = opt => path => Cont((res, rej) =>
12068+
Node.FS.readFile(path, opt, (e, x) => e ? rej(new Err(e)) : res(x)));
1206412069

1206512070

12066-
Node.FS.readOpt = {encoding: "utf8"};
12071+
Node.FS_.readOpt = {encoding: "utf8"};
1206712072

1206812073

12069-
Node.FS.write = opt => s => path => Cont((res, rej) =>
12070-
FS.writeFile(path, s, opt, e => e ? rej(new Err(e)) : res(s)));
12074+
Node.FS_.write = opt => s => path => Cont((res, rej) =>
12075+
Node.FS.writeFile(path, s, opt, e => e ? rej(new Err(e)) : res(s)));
1207112076

1207212077

12073-
Node.FS.writeOpt = {encoding: "utf8", flag: "wx"};
12078+
Node.FS_.writeOpt = {encoding: "utf8", flag: "wx"};
1207412079

1207512080

12076-
Node.FS.scanDir = opt => path => Cont((res, rej) =>
12077-
FS.readdir(path, opt, (e, xs) => e ? rej(new Err(e)) : res(xs)));
12081+
Node.FS_.scanDir = opt => path => Cont((res, rej) =>
12082+
Node.FS.readdir(path, opt, (e, xs) => e ? rej(new Err(e)) : res(xs)));
1207812083

1207912084

12080-
Node.FS.scanOpt = {encoding: "utf8", withFileTypes: false, recursive: false};
12085+
Node.FS_.scanOpt = {encoding: "utf8", withFileTypes: false, recursive: false};
1208112086

1208212087

12083-
Node.FS.copy = src => dest => Cont((res, rej) =>
12084-
FS.copyFile(src, dest, e => e ? rej(new Err(e)) : res(null)));
12088+
Node.FS_.copy = src => dest => Cont((res, rej) =>
12089+
Node.FS.copyFile(src, dest, e => e ? rej(new Err(e)) : res(null)));
1208512090

1208612091

12087-
Node.FS.unlink = path => Cont((res, rej) =>
12088-
FS.unlink(path, e => e ? rej(new Err(e)) : res(null)));
12092+
Node.FS_.unlink = path => Cont((res, rej) =>
12093+
Node.FS.unlink(path, e => e ? rej(new Err(e)) : res(null)));
1208912094

1209012095

12091-
Node.FS.move = src => dest =>
12092-
Cont.chain(Node.FS.copy(src) (dest)) (x =>
12093-
Cont.chain(Node.FS.unlink(src)) (y => res(y)));
12096+
Node.FS_.move = src => dest =>
12097+
Cont.chain(Node.FS_.copy(src) (dest)) (x =>
12098+
Cont.chain(Node.FS_.unlink(src)) (y => res(y)));
1209412099

1209512100

12096-
Node.FS.stat = path => Cont((res, rej) =>
12097-
FS.stat(path, (e, p) => e ? rej(new Err(e)) : res(p)));
12101+
Node.FS_.stat = path => Cont((res, rej) =>
12102+
Node.FS.stat(path, (e, p) => e ? rej(new Err(e)) : res(p)));
1209812103

1209912104

12100-
Node.FS.collectFiles = ({maxDepth, fileTypes}) => rootPath => {
12105+
Node.FS_.collectFiles = ({maxDepth, fileTypes}) => rootPath => {
1210112106
return function go(acc, currentPath, depth) {
1210212107
if (depth > maxDepth) return Cont.of();
1210312108

12104-
return Cont.chain(Node.FS.scanDir({withFileTypes: true}) (currentPath)) (qs => {
12109+
return Cont.chain(Node.FS_.scanDir({withFileTypes: true}) (currentPath)) (qs => {
1210512110
const xs = qs.map(q => {
12106-
const fullPath = Node.path.join(currentPath, q.name),
12107-
relativePath = Node.path.relative(rootPath, fullPath);
12111+
const fullPath = Node.Path.join(currentPath, q.name),
12112+
relativePath = Node.Path.relative(rootPath, fullPath);
1210812113

1210912114
if (q.isFile()) {
1211012115
const ys = q.name.split(/\./);
@@ -12122,7 +12127,7 @@ Node.FS.collectFiles = ({maxDepth, fileTypes}) => rootPath => {
1212212127
});
1212312128

1212412129
return Cont.map(_ =>
12125-
acc.map(path => Node.path.join(rootPath, path))) (Cont.Ser.All.arr(xs));
12130+
acc.map(path => Node.Path.join(rootPath, path))) (Cont.Ser.All.arr(xs));
1212612131
});
1212712132
} ([], rootPath, 0);
1212812133
};

0 commit comments

Comments
 (0)