@idlebox/node 1.4.2 → 1.4.4

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 (183) hide show
  1. package/lib/cjs/__create_index.generated.cjs +6 -6
  2. package/lib/cjs/__create_index.generated.cjs.map +1 -1
  3. package/lib/cjs/asyncLoad.cjs +4 -1
  4. package/lib/cjs/asyncLoad.cjs.map +1 -1
  5. package/lib/cjs/child_process/error.cjs +12 -9
  6. package/lib/cjs/child_process/error.cjs.map +1 -1
  7. package/lib/cjs/child_process/execa.cjs +4 -4
  8. package/lib/cjs/child_process/execa.cjs.map +1 -1
  9. package/lib/cjs/child_process/lateError.cjs +21 -9
  10. package/lib/cjs/child_process/lateError.cjs.map +1 -1
  11. package/lib/cjs/child_process/respawn.cjs +10 -14
  12. package/lib/cjs/child_process/respawn.cjs.map +1 -1
  13. package/lib/cjs/cli-io/output.cjs +1 -1
  14. package/lib/cjs/cli-io/output.cjs.map +1 -1
  15. package/lib/cjs/crypto/md5.cjs +2 -2
  16. package/lib/cjs/crypto/md5.cjs.map +1 -1
  17. package/lib/cjs/crypto/sha256.cjs +2 -5
  18. package/lib/cjs/crypto/sha256.cjs.map +1 -1
  19. package/lib/cjs/environment/findBinary.cjs +3 -3
  20. package/lib/cjs/environment/findBinary.cjs.map +1 -1
  21. package/lib/cjs/environment/npmConfig.cjs +4 -6
  22. package/lib/cjs/environment/npmConfig.cjs.map +1 -1
  23. package/lib/cjs/environment/pathEnvironment.cjs +3 -3
  24. package/lib/cjs/environment/pathEnvironment.cjs.map +1 -1
  25. package/lib/cjs/fs/commandExists.cjs +4 -6
  26. package/lib/cjs/fs/commandExists.cjs.map +1 -1
  27. package/lib/cjs/fs/emptyDir.cjs +3 -3
  28. package/lib/cjs/fs/emptyDir.cjs.map +1 -1
  29. package/lib/cjs/fs/exists.cjs +4 -6
  30. package/lib/cjs/fs/exists.cjs.map +1 -1
  31. package/lib/cjs/fs/tempFolder.cjs +10 -10
  32. package/lib/cjs/fs/tempFolder.cjs.map +1 -1
  33. package/lib/cjs/fs/weiteChanged.cjs +8 -8
  34. package/lib/cjs/fs/weiteChanged.cjs.map +1 -1
  35. package/lib/cjs/log/terminal.cjs.map +1 -1
  36. package/lib/cjs/path-resolve/findPackageRoot.cjs +6 -8
  37. package/lib/cjs/path-resolve/findPackageRoot.cjs.map +1 -1
  38. package/lib/cjs/path-resolve/findUp.cjs +56 -17
  39. package/lib/cjs/path-resolve/findUp.cjs.map +1 -1
  40. package/lib/cjs/path-resolve/getAllUp.cjs +2 -2
  41. package/lib/cjs/path-resolve/getAllUp.cjs.map +1 -1
  42. package/lib/cjs/path-resolve/lrelative.cjs +5 -5
  43. package/lib/cjs/path-resolve/lrelative.cjs.map +1 -1
  44. package/lib/cjs/path-resolve/nodeResolvePathArray.cjs +3 -3
  45. package/lib/cjs/path-resolve/nodeResolvePathArray.cjs.map +1 -1
  46. package/lib/cjs/path-resolve/resolvePath.cjs +10 -12
  47. package/lib/cjs/path-resolve/resolvePath.cjs.map +1 -1
  48. package/lib/cjs/stream/blackHoleStream.cjs +2 -2
  49. package/lib/cjs/stream/blackHoleStream.cjs.map +1 -1
  50. package/lib/cjs/stream/collectingStream.cjs +6 -8
  51. package/lib/cjs/stream/collectingStream.cjs.map +1 -1
  52. package/lib/cjs/stream/drainStream.cjs.map +1 -1
  53. package/lib/cjs/stream/loggerStream.cjs +7 -7
  54. package/lib/cjs/stream/loggerStream.cjs.map +1 -1
  55. package/lib/cjs/stream/streamPromise.cjs +8 -10
  56. package/lib/cjs/stream/streamPromise.cjs.map +1 -1
  57. package/lib/esm/__create_index.generated.d.ts +2 -0
  58. package/lib/esm/__create_index.generated.d.ts.map +1 -1
  59. package/lib/esm/__create_index.generated.js +1 -2
  60. package/lib/esm/__create_index.generated.js.map +1 -1
  61. package/lib/esm/asyncLoad.d.ts +1 -1
  62. package/lib/esm/asyncLoad.d.ts.map +1 -1
  63. package/lib/esm/asyncLoad.js +4 -1
  64. package/lib/esm/asyncLoad.js.map +1 -1
  65. package/lib/esm/child_process/error.d.ts.map +1 -1
  66. package/lib/esm/child_process/error.js +12 -9
  67. package/lib/esm/child_process/error.js.map +1 -1
  68. package/lib/esm/child_process/execa.d.ts.map +1 -1
  69. package/lib/esm/child_process/execa.js +4 -4
  70. package/lib/esm/child_process/execa.js.map +1 -1
  71. package/lib/esm/child_process/lateError.d.ts +7 -6
  72. package/lib/esm/child_process/lateError.d.ts.map +1 -1
  73. package/lib/esm/child_process/lateError.js +21 -9
  74. package/lib/esm/child_process/lateError.js.map +1 -1
  75. package/lib/esm/child_process/respawn.d.ts.map +1 -1
  76. package/lib/esm/child_process/respawn.js +7 -11
  77. package/lib/esm/child_process/respawn.js.map +1 -1
  78. package/lib/esm/cli-io/output.js +1 -1
  79. package/lib/esm/cli-io/output.js.map +1 -1
  80. package/lib/esm/crypto/md5.js +1 -1
  81. package/lib/esm/crypto/md5.js.map +1 -1
  82. package/lib/esm/crypto/sha256.d.ts.map +1 -1
  83. package/lib/esm/crypto/sha256.js +2 -5
  84. package/lib/esm/crypto/sha256.js.map +1 -1
  85. package/lib/esm/environment/findBinary.js +2 -2
  86. package/lib/esm/environment/findBinary.js.map +1 -1
  87. package/lib/esm/environment/npmConfig.d.ts.map +1 -1
  88. package/lib/esm/environment/npmConfig.js +3 -5
  89. package/lib/esm/environment/npmConfig.js.map +1 -1
  90. package/lib/esm/environment/pathEnvironment.js +1 -1
  91. package/lib/esm/environment/pathEnvironment.js.map +1 -1
  92. package/lib/esm/events/dumpEventEmitter.d.ts +1 -1
  93. package/lib/esm/events/dumpEventEmitter.d.ts.map +1 -1
  94. package/lib/esm/fs/commandExists.d.ts.map +1 -1
  95. package/lib/esm/fs/commandExists.js +2 -4
  96. package/lib/esm/fs/commandExists.js.map +1 -1
  97. package/lib/esm/fs/emptyDir.js +2 -2
  98. package/lib/esm/fs/emptyDir.js.map +1 -1
  99. package/lib/esm/fs/exists.d.ts +2 -2
  100. package/lib/esm/fs/exists.d.ts.map +1 -1
  101. package/lib/esm/fs/exists.js +3 -5
  102. package/lib/esm/fs/exists.js.map +1 -1
  103. package/lib/esm/fs/tempFolder.d.ts +1 -1
  104. package/lib/esm/fs/tempFolder.d.ts.map +1 -1
  105. package/lib/esm/fs/tempFolder.js +4 -4
  106. package/lib/esm/fs/tempFolder.js.map +1 -1
  107. package/lib/esm/fs/weiteChanged.js +2 -2
  108. package/lib/esm/fs/weiteChanged.js.map +1 -1
  109. package/lib/esm/log/terminal.d.ts +1 -1
  110. package/lib/esm/log/terminal.d.ts.map +1 -1
  111. package/lib/esm/log/terminal.js.map +1 -1
  112. package/lib/esm/path-resolve/findPackageRoot.d.ts.map +1 -1
  113. package/lib/esm/path-resolve/findPackageRoot.js +4 -6
  114. package/lib/esm/path-resolve/findPackageRoot.js.map +1 -1
  115. package/lib/esm/path-resolve/findUp.d.ts +12 -0
  116. package/lib/esm/path-resolve/findUp.d.ts.map +1 -1
  117. package/lib/esm/path-resolve/findUp.js +56 -17
  118. package/lib/esm/path-resolve/findUp.js.map +1 -1
  119. package/lib/esm/path-resolve/getAllUp.d.ts.map +1 -1
  120. package/lib/esm/path-resolve/getAllUp.js +2 -2
  121. package/lib/esm/path-resolve/getAllUp.js.map +1 -1
  122. package/lib/esm/path-resolve/lrelative.js +3 -3
  123. package/lib/esm/path-resolve/lrelative.js.map +1 -1
  124. package/lib/esm/path-resolve/nodeResolvePathArray.js +1 -1
  125. package/lib/esm/path-resolve/nodeResolvePathArray.js.map +1 -1
  126. package/lib/esm/path-resolve/resolvePath.d.ts +3 -9
  127. package/lib/esm/path-resolve/resolvePath.d.ts.map +1 -1
  128. package/lib/esm/path-resolve/resolvePath.js +3 -5
  129. package/lib/esm/path-resolve/resolvePath.js.map +1 -1
  130. package/lib/esm/stream/blackHoleStream.d.ts +1 -1
  131. package/lib/esm/stream/blackHoleStream.d.ts.map +1 -1
  132. package/lib/esm/stream/blackHoleStream.js +1 -1
  133. package/lib/esm/stream/blackHoleStream.js.map +1 -1
  134. package/lib/esm/stream/collectingStream.d.ts +2 -2
  135. package/lib/esm/stream/collectingStream.d.ts.map +1 -1
  136. package/lib/esm/stream/collectingStream.js +4 -6
  137. package/lib/esm/stream/collectingStream.js.map +1 -1
  138. package/lib/esm/stream/disposableStream.d.ts +2 -2
  139. package/lib/esm/stream/disposableStream.d.ts.map +1 -1
  140. package/lib/esm/stream/drainStream.d.ts.map +1 -1
  141. package/lib/esm/stream/drainStream.js.map +1 -1
  142. package/lib/esm/stream/loggerStream.d.ts +1 -1
  143. package/lib/esm/stream/loggerStream.d.ts.map +1 -1
  144. package/lib/esm/stream/loggerStream.js +5 -5
  145. package/lib/esm/stream/loggerStream.js.map +1 -1
  146. package/lib/esm/stream/streamPromise.d.ts.map +1 -1
  147. package/lib/esm/stream/streamPromise.js +8 -10
  148. package/lib/esm/stream/streamPromise.js.map +1 -1
  149. package/package.json +5 -5
  150. package/src/asyncLoad.ts +6 -3
  151. package/src/child_process/error.ts +13 -9
  152. package/src/child_process/execa.ts +5 -4
  153. package/src/child_process/lateError.ts +26 -13
  154. package/src/child_process/respawn.ts +8 -10
  155. package/src/cli-io/output.ts +1 -1
  156. package/src/crypto/md5.ts +1 -1
  157. package/src/crypto/sha256.ts +2 -5
  158. package/src/environment/findBinary.ts +2 -2
  159. package/src/environment/npmConfig.ts +3 -4
  160. package/src/environment/pathEnvironment.ts +1 -1
  161. package/src/error/cache.generated.json +3970 -4281
  162. package/src/error/nodejs-error-code.generator.ts +8 -8
  163. package/src/error/types.ts +1 -1
  164. package/src/events/dumpEventEmitter.ts +1 -1
  165. package/src/fs/commandExists.ts +2 -3
  166. package/src/fs/emptyDir.ts +2 -2
  167. package/src/fs/exists.ts +4 -5
  168. package/src/fs/tempFolder.ts +5 -5
  169. package/src/fs/weiteChanged.ts +2 -2
  170. package/src/log/terminal.ts +1 -1
  171. package/src/path-resolve/findPackageRoot.ts +4 -5
  172. package/src/path-resolve/findUp.ts +78 -17
  173. package/src/path-resolve/getAllUp.ts +3 -3
  174. package/src/path-resolve/lrelative.ts +3 -3
  175. package/src/path-resolve/nodeResolvePathArray.ts +1 -1
  176. package/src/path-resolve/resolvePath.ts +6 -13
  177. package/src/stream/blackHoleStream.ts +1 -1
  178. package/src/stream/collectingStream.ts +7 -8
  179. package/src/stream/disposableStream.ts +2 -2
  180. package/src/stream/drainStream.ts +1 -6
  181. package/src/stream/loggerStream.ts +13 -7
  182. package/src/stream/streamPromise.ts +8 -11
  183. package/src/tsconfig.json +2 -2
@@ -1,8 +1,8 @@
1
- import * as assert from 'assert';
2
- import { existsSync } from 'fs';
3
- import { readFile, writeFile } from 'fs/promises';
4
- import { resolve } from 'path';
5
- import type API_JSON from './cache.generated.json.js';
1
+ import * as assert from 'node:assert';
2
+ import { existsSync } from 'node:fs';
3
+ import { readFile, writeFile } from 'node:fs/promises';
4
+ import { resolve } from 'node:path';
5
+ import type API_JSON from './cache.generated.json';
6
6
 
7
7
  const temp = resolve(__dirname, 'cache.generated.json');
8
8
  const API_SOURCE = 'https://nodejs.org/api/errors.json';
@@ -30,9 +30,9 @@ export async function generate() {
30
30
  return section.modules!;
31
31
  }
32
32
  }
33
- throw new Error('missing section: ' + id);
33
+ throw new Error(`missing section: ${id}`);
34
34
  }
35
- content += `export enum NodeError {\n`;
35
+ content += 'export enum NodeError {\n';
36
36
  for (const err of get_id('node.js_error_codes')) {
37
37
  content += line(err.textRaw, err.desc, false);
38
38
  }
@@ -42,7 +42,7 @@ export async function generate() {
42
42
  }
43
43
  content += '}\n\n';
44
44
 
45
- content += `export enum OpenSSLError {\n`;
45
+ content += 'export enum OpenSSLError {\n';
46
46
  for (const section of get_id('openssl_error_codes')) {
47
47
  for (const err of (section as any).modules) {
48
48
  content += line(err.textRaw, err.desc, false);
@@ -15,7 +15,7 @@ export interface OpenSSLException extends Error {
15
15
  * ERR_MODULE_NOT_FOUND: import() not found
16
16
  */
17
17
  export function isModuleResolutionError(
18
- ex: unknown,
18
+ ex: unknown
19
19
  ): ex is NodeException<NodeError.MODULE_NOT_FOUND | NodeError.ERR_MODULE_NOT_FOUND> {
20
20
  return isNodeError(ex) && (ex.code === NodeError.MODULE_NOT_FOUND || ex.code === NodeError.ERR_MODULE_NOT_FOUND);
21
21
  }
@@ -1,4 +1,4 @@
1
- import { EventEmitter } from 'events';
1
+ import type { EventEmitter } from 'node:events';
2
2
 
3
3
  export function dumpEventEmitterEmit(ev: EventEmitter) {
4
4
  const real = ev.emit;
@@ -1,5 +1,5 @@
1
1
  import { isWindows } from '@idlebox/common';
2
- import { access, constants, accessSync } from 'fs';
2
+ import { access, constants, accessSync } from 'node:fs';
3
3
  import { PathEnvironment } from '../environment/pathEnvironment.js';
4
4
 
5
5
  const windowsExecExtensions = ['.exe', '.bat', '.cmd', '.com', '.ps1'];
@@ -14,9 +14,8 @@ function exts(alterExt?: string[]) {
14
14
  }
15
15
  if (isWindows) {
16
16
  return windowsExecExtensions;
17
- } else {
18
- return [''];
19
17
  }
18
+ return [''];
20
19
  }
21
20
 
22
21
  export async function commandInPath(cmd: string, alterExt?: string[]): Promise<string | undefined> {
@@ -1,5 +1,5 @@
1
- import { mkdir, readdir, rm } from 'fs/promises';
2
- import { resolve } from 'path';
1
+ import { mkdir, readdir, rm } from 'node:fs/promises';
2
+ import { resolve } from 'node:path';
3
3
 
4
4
  /**
5
5
  * delete all content in a dir,
package/src/fs/exists.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { ObjectEncodingOptions } from 'fs';
2
- import { access, readFile } from 'fs/promises';
1
+ import type { ObjectEncodingOptions } from 'node:fs';
2
+ import { access, readFile } from 'node:fs/promises';
3
3
  import { isNotExistsError } from '../error/types.js';
4
- export { existsSync } from 'fs';
4
+ export { existsSync } from 'node:fs';
5
5
 
6
6
  export async function exists(path: string) {
7
7
  try {
@@ -22,9 +22,8 @@ async function _readFileIfExists(file: string, encoding?: NodeJS.BufferEncoding
22
22
  if (isNotExistsError(e)) {
23
23
  if (typeof encoding === 'string' || typeof encoding?.encoding === 'string') {
24
24
  return '';
25
- } else {
26
- return Buffer.allocUnsafe(0);
27
25
  }
26
+ return Buffer.allocUnsafe(0);
28
27
  }
29
28
  throw e;
30
29
  }
@@ -1,14 +1,14 @@
1
- import { mkdirSync, readdirSync, rmdirSync, statSync, unlinkSync } from 'fs';
2
- import { resolve } from 'path';
3
- import { IDisposable } from '@idlebox/common';
1
+ import { mkdirSync, readdirSync, rmdirSync, statSync, unlinkSync } from 'node:fs';
2
+ import { resolve } from 'node:path';
3
+ import type { IDisposable } from '@idlebox/common';
4
4
  import { existsSync } from './exists.js';
5
5
 
6
- let registered = false;
6
+ const registered = false;
7
7
  const tempFolders = new Set<string>();
8
8
 
9
9
  export function createTempFolder(fullPath: string): IDisposable {
10
10
  if (existsSync(fullPath)) {
11
- throw new Error('temp folder already exists: ' + fullPath);
11
+ throw new Error(`temp folder already exists: ${fullPath}`);
12
12
  }
13
13
  if (!registered) {
14
14
  process.on('beforeExit', onBeforeExit);
@@ -1,5 +1,5 @@
1
- import { readFile as readFileAsync, readFileSync, writeFile as writeFileAsync, writeFileSync } from 'fs';
2
- import { promisify } from 'util';
1
+ import { readFile as readFileAsync, readFileSync, writeFile as writeFileAsync, writeFileSync } from 'node:fs';
2
+ import { promisify } from 'node:util';
3
3
  import { exists, existsSync } from './exists.js';
4
4
 
5
5
  const readFile = promisify(readFileAsync);
@@ -1,4 +1,4 @@
1
- import { WrappedConsole, WrappedConsoleOptions } from '@idlebox/common';
1
+ import { WrappedConsole, type WrappedConsoleOptions } from '@idlebox/common';
2
2
 
3
3
  interface TerminalConsoleOptions {
4
4
  color?: boolean | Partial<typeof colorMap>;
@@ -1,10 +1,10 @@
1
- import { createRequire } from 'module';
2
- import { dirname } from 'path';
1
+ import { createRequire } from 'node:module';
2
+ import { dirname } from 'node:path';
3
3
  import { findUpUntilSync } from './findUp.js';
4
4
 
5
5
  export function findPackageRoot(packageName: string, require = createRequire(process.cwd())) {
6
6
  try {
7
- return dirname(require.resolve(packageName + 'package.json'));
7
+ return dirname(require.resolve(`${packageName}package.json`));
8
8
  } catch (e: any) {
9
9
  if (e.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
10
10
  const main = require.resolve(packageName);
@@ -13,8 +13,7 @@ export function findPackageRoot(packageName: string, require = createRequire(pro
13
13
  throw new Error(`Package ${packageName} do not have a package.json`);
14
14
  }
15
15
  return dirname(pkgJson);
16
- } else {
17
- throw e;
18
16
  }
17
+ throw e;
19
18
  }
20
19
  }
@@ -1,33 +1,94 @@
1
- import { dirname, resolve } from 'path';
1
+ import { isWindows } from '@idlebox/common';
2
+ import { dirname, resolve } from 'node:path';
3
+ import { deprecate } from 'node:util';
2
4
  import { exists, existsSync } from '../fs/exists.js';
3
5
 
4
- export async function findUpUntil(from: string, file: string): Promise<string | null> {
5
- for (let _from = resolve(from); _from !== '/'; _from = dirname(_from)) {
6
- const want = resolve(_from, file);
7
- if (await exists(want)) {
8
- return want;
6
+ export interface IFindOptions {
7
+ /** 从哪开始找,必须是绝对路径 */
8
+ from: string;
9
+ /** 要找的文件 */
10
+ file: string | string[];
11
+ /** 最外层目录,超出或偏离都会停止搜索 */
12
+ top?: string;
13
+ }
14
+
15
+ const isRoot = isWindows ? /^[A-Z]:[/\\]$/i : /^\/$/;
16
+
17
+ export async function findUpUntil(opts: IFindOptions): Promise<string | null>;
18
+ /** @deprecated */
19
+ export async function findUpUntil(from: string, file: string): Promise<string | null>;
20
+
21
+ export async function findUpUntil(opts: string | IFindOptions, file?: string): Promise<string | null> {
22
+ if (typeof opts === 'string') {
23
+ deprecate(findUpUntil, 'findUpUntil is deprecated. Use findUpUntil(opts) instead.');
24
+ opts = { from: opts, file: [file!] };
25
+ }
26
+ if (typeof opts.file === 'string') {
27
+ opts.file = [opts.file];
28
+ }
29
+ if (opts.top) {
30
+ opts.top = resolve(opts.top);
31
+ }
32
+
33
+ const { from, file: files, top } = opts;
34
+
35
+ for (let _from = resolve(from); !isRoot.test(_from); _from = dirname(_from)) {
36
+ if (top && !_from.startsWith(top)) {
37
+ return null;
38
+ }
39
+ for (const file of files) {
40
+ const want = resolve(_from, file);
41
+ if (await exists(want)) {
42
+ return want;
43
+ }
9
44
  }
10
45
  }
11
46
 
12
- const final = resolve('/', file);
13
- if (await exists(final)) {
14
- return final;
47
+ for (const file of files) {
48
+ const final = resolve(from, '/', file);
49
+ if (await exists(final)) {
50
+ return final;
51
+ }
15
52
  }
16
53
 
17
54
  return null;
18
55
  }
19
56
 
20
- export function findUpUntilSync(from: string, file: string): string | null {
21
- for (let _from = resolve(from); _from !== '/'; _from = dirname(_from)) {
22
- const want = resolve(_from, file);
23
- if (existsSync(want)) {
24
- return want;
57
+ export function findUpUntilSync(opts: IFindOptions): string | null;
58
+ /** @deprecated */
59
+ export function findUpUntilSync(from: string, file: string): string | null;
60
+
61
+ export function findUpUntilSync(opts: string | IFindOptions, file?: string): string | null {
62
+ if (typeof opts === 'string') {
63
+ deprecate(findUpUntil, 'findUpUntil is deprecated. Use findUpUntil(opts) instead.');
64
+ opts = { from: opts, file: [file!] };
65
+ }
66
+ if (typeof opts.file === 'string') {
67
+ opts.file = [opts.file];
68
+ }
69
+ if (opts.top) {
70
+ opts.top = resolve(opts.top);
71
+ }
72
+
73
+ const { from, file: files, top } = opts;
74
+
75
+ for (let _from = resolve(from); !isRoot.test(_from); _from = dirname(_from)) {
76
+ if (top && !_from.startsWith(top)) {
77
+ return null;
78
+ }
79
+ for (const file of files) {
80
+ const want = resolve(_from, file);
81
+ if (existsSync(want)) {
82
+ return want;
83
+ }
25
84
  }
26
85
  }
27
86
 
28
- const final = resolve('/', file);
29
- if (existsSync(final)) {
30
- return final;
87
+ for (const file of files) {
88
+ const final = resolve(from, '/', file);
89
+ if (existsSync(final)) {
90
+ return final;
91
+ }
31
92
  }
32
93
 
33
94
  return null;
@@ -1,14 +1,14 @@
1
- export function getAllPathUpToRoot(from: string, append: string = '') {
1
+ export function getAllPathUpToRoot(from: string, append = '') {
2
2
  const parts = from.split(/\/\\/g).filter((e) => e);
3
3
  const dirs: string[] = [];
4
4
  let p = '';
5
5
 
6
6
  if (append) {
7
- append = '/' + append;
7
+ append = `/${append}`;
8
8
  }
9
9
 
10
10
  for (const i of parts) {
11
- p += '/' + i;
11
+ p += `/${i}`;
12
12
  dirs.push(p + append);
13
13
  }
14
14
  return dirs;
@@ -1,11 +1,11 @@
1
- import { isAbsolute } from 'path';
1
+ import { isAbsolute } from 'node:path';
2
2
 
3
3
  export function lrelative(from: string, to: string) {
4
4
  if (!isAbsolute(from)) {
5
- throw new Error('lrelative: from is not absolute: ' + from);
5
+ throw new Error(`lrelative: from is not absolute: ${from}`);
6
6
  }
7
7
  if (!isAbsolute(to)) {
8
- throw new Error('lrelative: to is not absolute: ' + to);
8
+ throw new Error(`lrelative: to is not absolute: ${to}`);
9
9
  }
10
10
  const fa = from.split(/[\/\\]/g).filter((e) => e);
11
11
  const ta = to.split(/[\/\\]/g).filter((e) => e);
@@ -1,4 +1,4 @@
1
- import { dirname, resolve } from 'path';
1
+ import { dirname, resolve } from 'node:path';
2
2
 
3
3
  export function nodeResolvePathArray(from: string, file = 'node_modules') {
4
4
  const ret: string[] = [];
@@ -1,16 +1,12 @@
1
1
  import { isWindows } from '@idlebox/common';
2
- import { tmpdir } from 'os';
3
- import { normalize, relative, resolve } from 'path';
2
+ import { tmpdir } from 'node:os';
3
+ import { normalize, relative, resolve } from 'node:path';
4
4
 
5
5
  const winSlash = /\\/g;
6
6
 
7
- export interface ResolvePathFunction {
8
- (...pathSegments: string[]): string;
9
- }
7
+ export type ResolvePathFunction = (...pathSegments: string[]) => string;
10
8
 
11
- export interface JoinPathFunction {
12
- (from: string, to: string): string;
13
- }
9
+ export type JoinPathFunction = (from: string, to: string) => string;
14
10
 
15
11
  export const resolvePath: ResolvePathFunction = isWindows ? resolveWindowsPath : resolve;
16
12
 
@@ -18,9 +14,7 @@ function resolveWindowsPath(...pathSegments: string[]): string {
18
14
  return resolve(...pathSegments).replace(winSlash, '/');
19
15
  }
20
16
 
21
- export interface NormalizePathFunction {
22
- (path: string): string;
23
- }
17
+ export type NormalizePathFunction = (path: string) => string;
24
18
 
25
19
  export const normalizePath: NormalizePathFunction = isWindows ? normalizeWindowsPath : normalize;
26
20
 
@@ -31,9 +25,8 @@ function normalizeWindowsPath(path: string): string {
31
25
  export function osTempDir(name?: string) {
32
26
  if (name) {
33
27
  return resolvePath(tmpdir(), name);
34
- } else {
35
- return resolvePath(tmpdir());
36
28
  }
29
+ return resolvePath(tmpdir());
37
30
  }
38
31
 
39
32
  function relativeWindowsPath(from: string, to: string) {
@@ -1,4 +1,4 @@
1
- import { Writable } from 'stream';
1
+ import { Writable } from 'node:stream';
2
2
 
3
3
  export class BlackHoleStream extends Writable {
4
4
  override _write(_chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {
@@ -1,4 +1,4 @@
1
- import { Writable } from 'stream';
1
+ import { Writable } from 'node:stream';
2
2
  import { streamPromise } from './streamPromise.js';
3
3
 
4
4
  export function streamToBuffer(stream: NodeJS.ReadableStream, raw: false): Promise<string>;
@@ -6,9 +6,8 @@ export function streamToBuffer(stream: NodeJS.ReadableStream, raw: true): Promis
6
6
  export function streamToBuffer(stream: NodeJS.ReadableStream, raw: boolean): Promise<string | Buffer> {
7
7
  if (raw) {
8
8
  return new RawCollectingStream(stream).promise();
9
- } else {
10
- return new CollectingStream(stream).promise();
11
9
  }
10
+ return new CollectingStream(stream).promise();
12
11
  }
13
12
 
14
13
  export class RawCollectingStream extends Writable {
@@ -30,7 +29,7 @@ export class RawCollectingStream extends Writable {
30
29
  callback();
31
30
  }
32
31
 
33
- getOutput() {
32
+ getOutput(): Buffer {
34
33
  return this.buffer!;
35
34
  }
36
35
 
@@ -39,9 +38,9 @@ export class RawCollectingStream extends Writable {
39
38
  ? this._promise
40
39
  : (this._promise = streamPromise(this).then(() => {
41
40
  const buffer = this.buffer!;
42
- delete this.buffer;
41
+ this.buffer = undefined;
43
42
  return buffer;
44
- }));
43
+ }));
45
44
  }
46
45
  }
47
46
 
@@ -78,8 +77,8 @@ export class CollectingStream extends Writable {
78
77
  ? this._promise
79
78
  : (this._promise = streamPromise(this).then(() => {
80
79
  const buffer = this.buffer!;
81
- delete this.buffer;
80
+ this.buffer = undefined;
82
81
  return buffer;
83
- }));
82
+ }));
84
83
  }
85
84
  }
@@ -1,5 +1,5 @@
1
- import { Readable, Writable } from 'stream';
2
- import { IDisposable } from '@idlebox/common';
1
+ import type { Readable, Writable } from 'node:stream';
2
+ import type { IDisposable } from '@idlebox/common';
3
3
 
4
4
  export function disposableStream<T extends Writable | Readable>(stream: T): T & IDisposable {
5
5
  if ((stream as any).dispose) {
@@ -1,9 +1,4 @@
1
- export function drainStream(
2
- stream: NodeJS.ReadableStream,
3
- size: number,
4
- start: number = 0,
5
- extra: number = 0
6
- ): Promise<Buffer> {
1
+ export function drainStream(stream: NodeJS.ReadableStream, size: number, start = 0, extra = 0): Promise<Buffer> {
7
2
  // buff will alloc as start+size+extra, but return will always start+RealStreamSize+extra
8
3
  const buff = Buffer.allocUnsafe(start + size + extra);
9
4
  let cur = start;
@@ -1,13 +1,16 @@
1
- import { Transform } from 'stream';
1
+ import { Transform } from 'node:stream';
2
2
 
3
3
  export type LogFunction = (message: string, ...args: any[]) => void;
4
4
 
5
5
  export class LoggerStream extends Transform {
6
6
  private readonly prefix: string;
7
7
 
8
- constructor(private readonly logFn: LogFunction, prefix?: string) {
8
+ constructor(
9
+ private readonly logFn: LogFunction,
10
+ prefix?: string
11
+ ) {
9
12
  super();
10
- this.prefix = prefix ? prefix + ' %s' : '%s';
13
+ this.prefix = prefix ? `${prefix} %s` : '%s';
11
14
  }
12
15
 
13
16
  override _transform(chunk: Buffer, encoding: BufferEncoding, callback: Function): void {
@@ -28,15 +31,18 @@ export class LoggerStream extends Transform {
28
31
  }
29
32
 
30
33
  function pad2(s: string) {
31
- return s.length === 1 ? '0' + s : s;
34
+ return s.length === 1 ? `0${s}` : s;
32
35
  }
33
36
 
34
37
  export class HexDumpLoggerStream extends Transform {
35
38
  private readonly prefix: string;
36
39
 
37
- constructor(private readonly logFn: LogFunction, prefix?: string) {
40
+ constructor(
41
+ private readonly logFn: LogFunction,
42
+ prefix?: string
43
+ ) {
38
44
  super();
39
- this.prefix = prefix ? prefix + ' ' : '';
45
+ this.prefix = prefix ? `${prefix} ` : '';
40
46
  }
41
47
 
42
48
  override _transform(chunk: Buffer, encoding: BufferEncoding, callback: Function): void {
@@ -52,7 +58,7 @@ export class HexDumpLoggerStream extends Transform {
52
58
  .join(' ');
53
59
  itr += 16;
54
60
 
55
- this.logFn(this.prefix + l + ' ' + r);
61
+ this.logFn(`${this.prefix + l} ${r}`);
56
62
  }
57
63
 
58
64
  this.push(chunk, encoding);
@@ -5,20 +5,17 @@
5
5
  export function streamPromise(stream: NodeJS.ReadableStream | NodeJS.WritableStream): Promise<void> {
6
6
  if (streamHasEnd(stream)) {
7
7
  return Promise.resolve();
8
- } else {
9
- return new Promise((resolve, reject) => {
10
- const r = stream as NodeJS.EventEmitter;
11
- r.once('end', () => resolve());
12
- r.once('finish', () => resolve());
13
- r.once('close', () => resolve());
14
- r.once('error', reject);
15
- });
16
8
  }
9
+ return new Promise((resolve, reject) => {
10
+ const r = stream as NodeJS.EventEmitter;
11
+ r.once('end', () => resolve());
12
+ r.once('finish', () => resolve());
13
+ r.once('close', () => resolve());
14
+ r.once('error', reject);
15
+ });
17
16
  }
18
17
 
19
18
  export function streamHasEnd(S: NodeJS.ReadableStream | NodeJS.WritableStream) {
20
19
  const stream = S as any;
21
- return (
22
- (stream._writableState && stream._writableState.ended) || (stream._readableState && stream._readableState.ended)
23
- );
20
+ return stream._writableState?.ended || stream._readableState?.ended;
24
21
  }
package/src/tsconfig.json CHANGED
@@ -4,9 +4,9 @@
4
4
  "typeRoots": ["../node_modules/@types", "../node_modules"],
5
5
  "outDir": "../lib/esm",
6
6
  "rootDir": "./",
7
- "types": ["node"]
7
+ "types": ["node"],
8
8
  },
9
9
  // "files": ["preload.ts"],
10
10
  // "include": ["**/*.ts"],
11
- "exclude": ["**/*.generator.ts"]
11
+ "exclude": ["**/*.generator.ts"],
12
12
  }