1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
From: =?utf-8?q?Bastien_Roucari=C3=A8s?= <rouca@debian.org>
Date: Fri, 21 Nov 2025 00:20:43 +0100
Subject: Port to newer glob
forwarded: not-needed
---
readable.js | 76 +++++++++++++++++++++++++++++---------------------------
test/index.js | 30 ++--------------------
test/readable.js | 39 -----------------------------
3 files changed, 41 insertions(+), 104 deletions(-)
diff --git a/readable.js b/readable.js
index 68b5368..6497b8e 100644
--- a/readable.js
+++ b/readable.js
@@ -3,6 +3,8 @@
var inherits = require('util').inherits;
var glob = require('glob');
+const { Minimatch } = require('minimatch');
+
var extend = require('extend');
var Readable = require('readable-stream').Readable;
var globParent = require('glob-parent');
@@ -16,17 +18,17 @@ function getBasePath(ourGlob, opt) {
return globParent(toAbsoluteGlob(ourGlob, opt));
}
-function globIsSingular(glob) {
- var globSet = glob.minimatch.set;
- if (globSet.length !== 1) {
+function globIsSingular(mm) {
+ if (mm.length !== 1) {
return false;
}
- return globSet[0].every(function isString(value) {
+ return mm[0].every(function isString(value) {
return typeof value === 'string';
});
}
+
function GlobStream(ourGlob, negatives, opt) {
if (!(this instanceof GlobStream)) {
return new GlobStream(ourGlob, negatives, opt);
@@ -63,49 +65,49 @@ function GlobStream(ourGlob, negatives, opt) {
delete ourOpt.root;
var globber = new glob.Glob(ourGlob, ourOpt);
+ this._mm = new Minimatch(ourGlob).set;
this._globber = globber;
+ this._globberit = globber.iterate();
+ this._ourOpt = ourOpt;
+ this._cwd = cwd;
+ this._basePath = basePath;
+ this._ourGlob = ourGlob;
+ this._allowEmpty = allowEmpty
var found = false;
-
- globber.on('match', function (filepath) {
- found = true;
- var obj = {
- cwd: cwd,
- base: basePath,
- path: removeTrailingSeparator(filepath),
- };
- if (!self.push(obj)) {
- globber.pause();
- }
- });
-
- globber.once('end', function () {
- if (allowEmpty !== true && !found && globIsSingular(globber)) {
- var err = new Error(globErrMessage1 + ourGlob + globErrMessage2);
-
- return self.destroy(err);
- }
-
- self.push(null);
- });
-
- function onError(err) {
- self.destroy(err);
- }
-
- globber.once('error', onError);
}
inherits(GlobStream, Readable);
-GlobStream.prototype._read = function () {
- this._globber.resume();
-};
+GlobStream.prototype._read = async function() {
+ try {
+ const {value, done} = await this._globberit.next();
+ if(done) {
+ if (this._allowEmpty !== true && !this.found && globIsSingular(this._mm)) {
+ var err = new Error(globErrMessage1 + this._ourGlob + globErrMessage2);
+ this.destroy(err);
+ }
+ else {
+ this.push(null)
+ }
+ }
+ else {
+ this.found = true;
+ const obj = {
+ cwd: this._cwd,
+ base: this._basePath,
+ path: removeTrailingSeparator(value),
+ };
+ this.push(obj);
+ }
+ }
+ catch(err) {
+ this.destroy(err)
+ }
+}
GlobStream.prototype.destroy = function (err) {
var self = this;
- this._globber.abort();
-
process.nextTick(function () {
if (err) {
self.emit('error', err);
diff --git a/test/index.js b/test/index.js
index e593d44..907e51f 100644
--- a/test/index.js
+++ b/test/index.js
@@ -158,32 +158,6 @@ describe('glob-stream', function () {
);
});
- // TODO: This doesn't seem to be testing that backpressure is respected
- it('respects backpressure and stream state', function (done) {
- var delayStream = through2.obj(function (data, enc, cb) {
- var self = this;
-
- self.pause();
- setTimeout(function () {
- cb(null, data);
- self.resume();
- }, 500);
- });
-
- function assert(pathObjs) {
- expect(pathObjs.length).toEqual(2);
- }
-
- pipe(
- [
- globStream('./fixtures/stuff/*.dmc', { cwd: dir }),
- delayStream,
- concat(assert),
- ],
- done
- );
- });
-
it('properly orders objects when given multiple paths and specified base', function (done) {
var base = dir + '/fixtures';
@@ -277,12 +251,12 @@ describe('glob-stream', function () {
{
cwd: dir,
base: dir + '/fixtures',
- path: dir + '/fixtures/has (parens)/test.dmc',
+ path: dir + '/fixtures/stuff/test.dmc',
},
{
cwd: dir,
base: dir + '/fixtures',
- path: dir + '/fixtures/stuff/test.dmc',
+ path: dir + '/fixtures/has (parens)/test.dmc',
},
];
diff --git a/test/readable.js b/test/readable.js
index e8049b9..3ce62f3 100644
--- a/test/readable.js
+++ b/test/readable.js
@@ -110,25 +110,6 @@ describe('readable stream', function () {
);
});
- it('pauses the globber upon backpressure', function (done) {
- var gs = stream('./fixtures/**/*.dmc', [], { cwd: dir, highWaterMark: 1 });
-
- var spy = sinon.spy(gs._globber, 'pause');
-
- function waiter(pathObj, _, cb) {
- setTimeout(function () {
- cb(null, pathObj);
- }, 500);
- }
-
- function assert(pathObjs) {
- expect(pathObjs.length).toEqual(3);
- expect(spy.callCount).toEqual(2);
- sinon.restore();
- }
-
- pipe([gs, through.obj({ highWaterMark: 1 }, waiter), concat(assert)], done);
- });
it('destroys the stream with an error if no match is found', function (done) {
var gs = stream('notfound', []);
@@ -145,25 +126,5 @@ describe('readable stream', function () {
pipe([gs, concat()], assert);
});
- it('destroys the stream if node-glob errors', function (done) {
- var expectedError = new Error('Stubbed error');
- var gs = stream('./fixtures/**/*.dmc', [], { cwd: dir, silent: true });
-
- function stubError(dirpath, cb) {
- cb(expectedError);
- }
-
- var spy = sinon.spy(gs, 'destroy');
- sinon.stub(fs, 'readdir').callsFake(stubError);
-
- function assert(err) {
- sinon.restore();
- expect(spy.called).toEqual(true);
- expect(err).toBe(expectedError);
- done();
- }
-
- pipe([gs, concat()], assert);
- });
});
|