@e-mc/module 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +37 -6
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -558,7 +558,7 @@ class Module extends EventEmitter {
558
558
  this[_f] = new AbortController();
559
559
  this[_g] = null;
560
560
  }
561
- static get VERSION() { return "0.6.0" /* INTERNAL.VERSION */; }
561
+ static get VERSION() { return "0.7.0" /* INTERNAL.VERSION */; }
562
562
  static get LOG_TYPE() { return types_1.LOG_TYPE; }
563
563
  static get STATUS_TYPE() { return types_1.STATUS_TYPE; }
564
564
  static get MAX_TIMEOUT() { return 2147483647; }
@@ -1002,7 +1002,7 @@ class Module extends EventEmitter {
1002
1002
  }
1003
1003
  }
1004
1004
  }
1005
- if (external && !pathname && VALUES["node.require.npm" /* KEY_NAME.NODE_REQUIRE_NPM */] && /^(?:@[a-z\d-*~][a-z\d-*._~]*\/)?[a-z\d-~][a-z\d-._~]*(?:\/.*)?$/.test(location)) {
1005
+ if (external && !pathname && VALUES["node.require.npm" /* KEY_NAME.NODE_REQUIRE_NPM */] && /^(?:@[a-z\d-*~][a-z\d-*._~]*\/|node:)?[a-z\d-~][a-z\d-._~]*(?:\/.*)?$/.test(location)) {
1006
1006
  try {
1007
1007
  // @ts-ignore
1008
1008
  result = checkFunction(require(types_1.IMPORT_MAP[location] || location));
@@ -1069,7 +1069,7 @@ class Module extends EventEmitter {
1069
1069
  }
1070
1070
  static asHash(data, algorithm, minLength = 0) {
1071
1071
  if (!algorithm && !minLength) {
1072
- return crypto.createHash('sha256').update(data).digest('hex');
1072
+ return crypto.createHash("sha256" /* HASH_OUTPUT.ALGORITHM */).update(data).digest("hex" /* HASH_OUTPUT.DIGEST */);
1073
1073
  }
1074
1074
  let options, digest;
1075
1075
  if (typeof minLength !== 'number') {
@@ -1078,11 +1078,11 @@ class Module extends EventEmitter {
1078
1078
  }
1079
1079
  if (typeof algorithm === 'number') {
1080
1080
  minLength = algorithm;
1081
- algorithm = '';
1081
+ algorithm = undefined;
1082
1082
  }
1083
1083
  else if ((0, types_1.isObject)(algorithm)) {
1084
1084
  options = algorithm;
1085
- algorithm = '';
1085
+ algorithm = undefined;
1086
1086
  }
1087
1087
  if (options) {
1088
1088
  options = { ...options };
@@ -1103,12 +1103,43 @@ class Module extends EventEmitter {
1103
1103
  return data;
1104
1104
  }
1105
1105
  try {
1106
- return crypto.createHash(algorithm || 'sha256', options).update(data).digest(digest || 'hex');
1106
+ return crypto.createHash(algorithm || "sha256" /* HASH_OUTPUT.ALGORITHM */, options).update(data).digest(digest || "hex" /* HASH_OUTPUT.DIGEST */);
1107
1107
  }
1108
1108
  catch {
1109
1109
  return '';
1110
1110
  }
1111
1111
  }
1112
+ static async readHash(value, options = {}) {
1113
+ const { algorithm = "sha256" /* HASH_OUTPUT.ALGORITHM */, digest = "hex" /* HASH_OUTPUT.DIGEST */, minStreamSize = 0, chunkSize = 2097152000 /* HASH_OUTPUT.CHUNK_SIZE */ } = options;
1114
+ const hash = crypto.createHash(algorithm);
1115
+ try {
1116
+ if (fs.statSync(value).size <= Math.min(minStreamSize > 0 ? minStreamSize : Infinity, 2097152000 /* HASH_OUTPUT.CHUNK_SIZE */)) {
1117
+ return hash.update(fs.readFileSync(value)).digest(digest);
1118
+ }
1119
+ }
1120
+ catch (err) {
1121
+ if (err instanceof Error && err.code === 'ENOENT') {
1122
+ throw err;
1123
+ }
1124
+ }
1125
+ const readable = fs.createReadStream(value);
1126
+ const chunks = [];
1127
+ let length = 0;
1128
+ for await (const chunk of readable) {
1129
+ const seg = Buffer.from(chunk);
1130
+ if (seg.length + length > chunkSize) {
1131
+ hash.update(Buffer.concat(chunks));
1132
+ chunks.length = 0;
1133
+ length = 0;
1134
+ }
1135
+ chunks.push(seg);
1136
+ length += seg.length;
1137
+ }
1138
+ if (chunks.length) {
1139
+ hash.update(Buffer.concat(chunks));
1140
+ }
1141
+ return hash.digest(digest);
1142
+ }
1112
1143
  static toPosix(value, filename, normalize) {
1113
1144
  if (typeof filename === 'boolean') {
1114
1145
  normalize = filename;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Module base class for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/types": "0.6.0",
23
+ "@e-mc/types": "0.7.0",
24
24
  "abort-controller": "^3.0.0",
25
25
  "chalk": "4.1.2",
26
26
  "event-target-shim": "^5.0.1",