@e-mc/module 0.6.0 → 0.7.1

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.
package/LICENSE CHANGED
@@ -1,11 +1,11 @@
1
- Copyright 2023 An Pham
2
-
3
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
-
5
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
-
7
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
-
9
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
1
+ Copyright 2023 An Pham
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
11
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { ModuleConstructor } from '../types/lib';
2
-
3
- declare const Module: ModuleConstructor;
4
-
1
+ import type { ModuleConstructor } from '../types/lib';
2
+
3
+ declare const Module: ModuleConstructor;
4
+
5
5
  export = Module;
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.1" /* 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/lib-v4.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { IModuleLibV4 } from '../types/lib/compat-v4';
2
-
3
- declare const LibV4: IModuleLibV4;
4
-
1
+ import type { IModuleLibV4 } from '../types/lib/compat-v4';
2
+
3
+ declare const LibV4: IModuleLibV4;
4
+
5
5
  export = LibV4;
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.1",
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.1",
24
24
  "abort-controller": "^3.0.0",
25
25
  "chalk": "4.1.2",
26
26
  "event-target-shim": "^5.0.1",