@e-mc/module 0.5.3 → 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 +72 -37
  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.5.3" /* 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));
@@ -1068,20 +1068,22 @@ class Module extends EventEmitter {
1068
1068
  return cacheKey ? (0, types_1.generateUUID)() : '';
1069
1069
  }
1070
1070
  static asHash(data, algorithm, minLength = 0) {
1071
- let options;
1071
+ if (!algorithm && !minLength) {
1072
+ return crypto.createHash("sha256" /* HASH_OUTPUT.ALGORITHM */).update(data).digest("hex" /* HASH_OUTPUT.DIGEST */);
1073
+ }
1074
+ let options, digest;
1072
1075
  if (typeof minLength !== 'number') {
1073
1076
  options = minLength;
1074
1077
  minLength = 0;
1075
1078
  }
1076
1079
  if (typeof algorithm === 'number') {
1077
1080
  minLength = algorithm;
1078
- algorithm = '';
1081
+ algorithm = undefined;
1079
1082
  }
1080
1083
  else if ((0, types_1.isObject)(algorithm)) {
1081
1084
  options = algorithm;
1082
- algorithm = '';
1085
+ algorithm = undefined;
1083
1086
  }
1084
- let digest;
1085
1087
  if (options) {
1086
1088
  options = { ...options };
1087
1089
  if ('minLength' in options) {
@@ -1101,12 +1103,43 @@ class Module extends EventEmitter {
1101
1103
  return data;
1102
1104
  }
1103
1105
  try {
1104
- 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 */);
1105
1107
  }
1106
1108
  catch {
1107
1109
  return '';
1108
1110
  }
1109
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
+ }
1110
1143
  static toPosix(value, filename, normalize) {
1111
1144
  if (typeof filename === 'boolean') {
1112
1145
  normalize = filename;
@@ -1229,7 +1262,7 @@ class Module extends EventEmitter {
1229
1262
  return '';
1230
1263
  }
1231
1264
  if (/^\\\\\?\\[A-Za-z]:\\/.test(value)) {
1232
- return process.platform === 'win32' && !/\\\\|\\\.+\\|\\[^\n]+?\.+\\/.test(value = value.substring(4)) ? value : '';
1265
+ return PLATFORM_WIN32 && !/\\\\|\\\.+\\|\\[^\n]+?\.+\\/.test(value = value.substring(4)) ? value : '';
1233
1266
  }
1234
1267
  return this.fromLocalPath(value) || value;
1235
1268
  }
@@ -3175,17 +3208,18 @@ class Module extends EventEmitter {
3175
3208
  }
3176
3209
  detach() {
3177
3210
  const host = this.host;
3178
- if (host) {
3179
- this.abortable = false;
3180
- host.modules.delete(this);
3181
- host.subProcesses.delete(this);
3182
- const listener = this[kAbortEvent];
3183
- if (listener) {
3184
- host.signal.removeEventListener('abort', listener);
3185
- this[kAbortEvent] = null;
3186
- }
3187
- this._host = null;
3211
+ if (!host) {
3212
+ return;
3213
+ }
3214
+ this.abortable = false;
3215
+ host.modules.delete(this);
3216
+ host.subProcesses.delete(this);
3217
+ const listener = this[kAbortEvent];
3218
+ if (listener) {
3219
+ host.signal.removeEventListener('abort', listener);
3220
+ this[kAbortEvent] = null;
3188
3221
  }
3222
+ this._host = null;
3189
3223
  }
3190
3224
  abort() {
3191
3225
  if (!this.aborted) {
@@ -3207,22 +3241,23 @@ class Module extends EventEmitter {
3207
3241
  }
3208
3242
  set host(value) {
3209
3243
  this.detach();
3210
- if (this._host = value) {
3211
- this.sessionId = value.sessionId;
3212
- this.broadcastId = value.broadcastId;
3213
- this._logEnabled = value.willLog(this.moduleName);
3214
- const aborting = this._hostEvents.includes('abort');
3215
- if (value.aborted) {
3216
- if (aborting) {
3217
- this.abort();
3218
- }
3244
+ if (!(this._host = value)) {
3245
+ return;
3246
+ }
3247
+ this.sessionId = value.sessionId;
3248
+ this.broadcastId = value.broadcastId;
3249
+ this._logEnabled = value.willLog(this.moduleName);
3250
+ const aborting = this._hostEvents.includes('abort');
3251
+ if (value.aborted) {
3252
+ if (aborting) {
3253
+ this.abort();
3219
3254
  }
3220
- else if (!value.subProcesses.has(this)) {
3221
- this.abortable = value.willAbort(this);
3222
- value.modules.add(this);
3223
- if (aborting) {
3224
- value.signal.addEventListener('abort', this[kAbortEvent] = () => this.abort(), { once: true });
3225
- }
3255
+ }
3256
+ else if (!value.subProcesses.has(this)) {
3257
+ this.abortable = value.willAbort(this);
3258
+ value.modules.add(this);
3259
+ if (aborting) {
3260
+ value.signal.addEventListener('abort', this[kAbortEvent] = () => this.abort(), { once: true });
3226
3261
  }
3227
3262
  }
3228
3263
  }
@@ -3276,11 +3311,11 @@ class Module extends EventEmitter {
3276
3311
  }
3277
3312
  get logLevel() {
3278
3313
  const value = this._logLevel;
3279
- if (value === -1) {
3280
- const host = this.host;
3281
- return host ? host.logLevel : 0;
3314
+ if (value !== -1) {
3315
+ return value;
3282
3316
  }
3283
- return value;
3317
+ const host = this.host;
3318
+ return host ? host.logLevel : 0;
3284
3319
  }
3285
3320
  get statusType() {
3286
3321
  return types_1.STATUS_TYPE;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.5.3",
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.5.3",
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",