rrdir 12.0.3 → 12.1.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 (3) hide show
  1. package/README.md +3 -3
  2. package/index.js +15 -11
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -31,9 +31,9 @@ const entries = rrdirSync("dir");
31
31
 
32
32
  `rrdir` is an async iterator which yields `entry`. `rrdirAsync` and `rrdirSync` return an Array of `entry`.
33
33
 
34
- #### `dir` *String* | *Buffer*
34
+ #### `dir` *String* | *Uint8Array*
35
35
 
36
- The directory to read, either absolute or relative. Pass a `Buffer` to switch the module into `Buffer` mode which is required to be able to read every file, like for example files with names that are invalid UTF-8 sequences.
36
+ The directory to read, either absolute or relative. Pass a `Uint8Array` to switch the module into `Uint8Array` mode which is required to be able to read every file, like for example files with names that are invalid UTF-8 sequences.
37
37
 
38
38
  #### `options` *Object*
39
39
 
@@ -46,7 +46,7 @@ The directory to read, either absolute or relative. Pass a `Buffer` to switch th
46
46
 
47
47
  #### `entry` *Object*
48
48
 
49
- - `path` *string* | *Buffer*: The path to the entry, will be relative if `dir` is given relative. If `dir` is a `Buffer`, this will be too. Always present.
49
+ - `path` *string* | *Uint8Array*: The path to the entry, will be relative if `dir` is given relative. If `dir` is a `Uint8Array`, this will be too. Always present.
50
50
  - `directory` *boolean*: Boolean indicating whether the entry is a directory. `undefined` on error.
51
51
  - `symlink` *boolean*: Boolean indicating whether the entry is a symbolic link. `undefined` on error.
52
52
  - `stats` *Object*: A [`fs.stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object, present when `options.stats` is set. `undefined` on error.
package/index.js CHANGED
@@ -3,7 +3,11 @@ import {readdirSync, statSync, lstatSync} from "node:fs";
3
3
  import {sep, resolve} from "node:path";
4
4
  import picomatch from "picomatch";
5
5
 
6
- const sepBuffer = Buffer.from(sep);
6
+ const encoder = new TextEncoder();
7
+ const toUint8Array = encoder.encode.bind(encoder);
8
+ const decoder = new TextDecoder();
9
+ const toString = decoder.decode.bind(decoder);
10
+ const sepUint8Array = toUint8Array(sep);
7
11
 
8
12
  const defaults = {
9
13
  strict: false,
@@ -16,7 +20,7 @@ const defaults = {
16
20
 
17
21
  function makePath(entry, dir, encoding) {
18
22
  if (encoding === "buffer") {
19
- return dir === "." ? entry.name : Buffer.from([...dir, ...sepBuffer, ...entry.name]);
23
+ return dir === "." ? entry.name : Uint8Array.from([...dir, ...sepUint8Array, ...entry.name]);
20
24
  } else {
21
25
  return dir === "." ? entry.name : `${dir}${sep}${entry.name}`;
22
26
  }
@@ -51,7 +55,7 @@ export async function* rrdir(dir, opts = {}, {includeMatcher, excludeMatcher, en
51
55
  opts = {...defaults, ...opts};
52
56
  ({includeMatcher, excludeMatcher} = makeMatchers(opts));
53
57
  if (/[/\\]$/.test(dir)) dir = dir.substring(0, dir.length - 1);
54
- encoding = Buffer.isBuffer(dir) ? "buffer" : undefined;
58
+ encoding = dir instanceof Uint8Array ? "buffer" : undefined;
55
59
  }
56
60
 
57
61
  let dirents = [];
@@ -65,10 +69,10 @@ export async function* rrdir(dir, opts = {}, {includeMatcher, excludeMatcher, en
65
69
 
66
70
  for (const dirent of dirents) {
67
71
  const path = makePath(dirent, dir, encoding);
68
- if (excludeMatcher?.(encoding === "buffer" ? String(path) : path)) continue;
72
+ if (excludeMatcher?.(encoding === "buffer" ? toString(path) : path)) continue;
69
73
 
70
74
  const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
71
- const encodedPath = encoding === "buffer" ? String(path) : path;
75
+ const encodedPath = encoding === "buffer" ? toString(path) : path;
72
76
  const isIncluded = !includeMatcher || includeMatcher(encodedPath);
73
77
  let stats;
74
78
 
@@ -102,7 +106,7 @@ export async function rrdirAsync(dir, opts = {}, {includeMatcher, excludeMatcher
102
106
  opts = {...defaults, ...opts};
103
107
  ({includeMatcher, excludeMatcher} = makeMatchers(opts));
104
108
  if (/[/\\]$/.test(dir)) dir = dir.substring(0, dir.length - 1);
105
- encoding = Buffer.isBuffer(dir) ? "buffer" : undefined;
109
+ encoding = dir instanceof Uint8Array ? "buffer" : undefined;
106
110
  }
107
111
 
108
112
  const results = [];
@@ -117,10 +121,10 @@ export async function rrdirAsync(dir, opts = {}, {includeMatcher, excludeMatcher
117
121
 
118
122
  await Promise.all(dirents.map(async dirent => {
119
123
  const path = makePath(dirent, dir, encoding);
120
- if (excludeMatcher?.(encoding === "buffer" ? String(path) : path)) return;
124
+ if (excludeMatcher?.(encoding === "buffer" ? toString(path) : path)) return;
121
125
 
122
126
  const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
123
- const encodedPath = encoding === "buffer" ? String(path) : path;
127
+ const encodedPath = encoding === "buffer" ? toString(path) : path;
124
128
  const isIncluded = !includeMatcher || includeMatcher(encodedPath);
125
129
  let stats;
126
130
 
@@ -156,7 +160,7 @@ export function rrdirSync(dir, opts = {}, {includeMatcher, excludeMatcher, encod
156
160
  opts = {...defaults, ...opts};
157
161
  ({includeMatcher, excludeMatcher} = makeMatchers(opts));
158
162
  if (/[/\\]$/.test(dir)) dir = dir.substring(0, dir.length - 1);
159
- encoding = Buffer.isBuffer(dir) ? "buffer" : undefined;
163
+ encoding = dir instanceof Uint8Array ? "buffer" : undefined;
160
164
  }
161
165
 
162
166
  const results = [];
@@ -171,10 +175,10 @@ export function rrdirSync(dir, opts = {}, {includeMatcher, excludeMatcher, encod
171
175
 
172
176
  for (const dirent of dirents) {
173
177
  const path = makePath(dirent, dir, encoding);
174
- if (excludeMatcher?.(encoding === "buffer" ? String(path) : path)) continue;
178
+ if (excludeMatcher?.(encoding === "buffer" ? toString(path) : path)) continue;
175
179
 
176
180
  const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
177
- const encodedPath = encoding === "buffer" ? String(path) : path;
181
+ const encodedPath = encoding === "buffer" ? toString(path) : path;
178
182
  const isIncluded = !includeMatcher || includeMatcher(encodedPath);
179
183
  let stats;
180
184
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rrdir",
3
- "version": "12.0.3",
3
+ "version": "12.1.0",
4
4
  "description": "Recursive directory reader with a delightful API",
5
5
  "author": "silverwind <me@silverwind.io>",
6
6
  "repository": "silverwind/rrdir",