Gathering detailed insights and metrics for replacestream
Gathering detailed insights and metrics for replacestream
Gathering detailed insights and metrics for replacestream
Gathering detailed insights and metrics for replacestream
A node.js through stream that does basic streaming text search and replace and is chunk boundary friendly
npm install replacestream
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
NOASSERTION License
177 Stars
94 Commits
20 Forks
8 Watchers
1 Branches
9 Contributors
Updated on May 27, 2025
Latest Version
4.0.3
Package Id
replacestream@4.0.3
Size
5.48 kB
NPM Version
5.4.1
Node Version
8.5.0
Published on
Sep 15, 2017
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
5
A node.js transform stream for basic streaming text search/replacement friendy with chunk boundary.
Install via npm:
1$ npm install replacestream
Say we want to do a search and replace over the following file:
// happybirthday.txt
Happy birthday to you!
Happy birthday to you!
Happy birthday to dear Liza!
Happy birthday to you!
1var replaceStream = require('replacestream') 2 , fs = require('fs') 3 , path = require('path'); 4 5// Replace all the instances of 'birthday' with 'earthday' 6fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 7 .pipe(replaceStream('birthday', 'earthday')) 8 .pipe(process.stdout);
Running this will print out:
1$ node simple.js 2Happy earthday to you! 3Happy earthday to you! 4Happy earthday to dear Liza! 5Happy earthday to you!
You can also limit the number of replaces to first n
:
1// Replace the first 2 of the instances of 'birthday' with 'earthday' 2fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 3 .pipe(replaceStream('birthday', 'earthday', { limit: 2 } )) 4 .pipe(process.stdout);
Which would output:
1$ node simple.js 2Happy earthday to you! 3Happy earthday to you! 4Happy birthday to dear Liza! 5Happy birthday to you!
And you can also pass in a replacement function which will get called for each replacement:
1// Replace the word 'Happy' with a different word each time 2var words = ['Awesome', 'Good', 'Super', 'Joyous']; 3function replaceFn(match) { 4 return words.shift(); 5} 6fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 7 .pipe(replaceStream('Happy', replaceFn)) 8 .pipe(process.stdout);
Which would output:
1$ node simple.js 2Awesome birthday to you! 3Good birthday to you! 4Super birthday to dear Liza! 5Joyous birthday to you!
Here's the same example, but with RegEx:
// happybirthday.txt
Happy birthday to you!
Happy birthday to you!
Happy birthday to dear Liza!
Happy birthday to you!
1var replaceStream = require('replacestream') 2 , fs = require('fs') 3 , path = require('path'); 4 5// Replace any word that has an 'o' with 'oh' 6fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 7 .pipe(replaceStream(/\w*o\w*/g, 'oh')) 8 .pipe(process.stdout);
Running this will print out:
1$ node simple.js 2Happy birthday oh oh! 3Happy birthday oh oh! 4Happy birthday oh dear Liza! 5Happy birthday oh oh!
You can also insert captures using the $1 ($index) notation. This is similar the built in method replace.
// happybirthday.txt
Happy birthday to you!
Happy birthday to you!
Happy birthday to dear Liza!
Happy birthday to you!
1var replaceStream = require('replacestream') 2 , fs = require('fs') 3 , path = require('path'); 4 5// Replace any word that has an 'o' with 'oh' 6fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 7 .pipe(replaceStream(/(dear) (Liza!)/, 'my very good and $1 friend $2')) 8 .pipe(process.stdout);
Running this will print:
1$ node simple.js 2Happy birthday to you! 3Happy birthday to you! 4Happy birthday to my very good and dear friend Liza! 5Happy birthday to you!
You can also pass in a replacement function. The function will be passed parameters just like String.prototype.replace (e.g. replaceFunction(match, p1, p2, offset, string)). In this case the matched string is limited to the buffer the match is found on, not the entire stream.
1function replaceFn() { 2 return arguments[2] + ' to ' + arguments[1] 3} 4fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 5 .pipe(replaceStream(/(birt\w*)\sto\s(you)/g, replaceFn)) 6 .pipe(process.stdout);
Which would output:
1$ node simple.js 2Happy you to birthday! 3Happy you to birthday! 4Happy birthday to dear Liza! 5Happy you to birthday!
Here's the same example, but kicked off from a HTTP server:
1// server.js 2var http = require('http') 3 , fs = require('fs') 4 , path = require('path') 5 , replaceStream = require('replacestream'); 6 7var app = function (req, res) { 8 if (req.url.match(/^\/happybirthday\.txt$/)) { 9 res.writeHead(200, { 'Content-Type': 'text/plain' }); 10 fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 11 .pipe(replaceStream('birthday', 'earthday')) 12 .pipe(res); 13 } else { 14 res.writeHead(200, { 'Content-Type': 'text/plain' }); 15 res.end('File not found'); 16 } 17}; 18var server = http.createServer(app).listen(3000);
When you request the file:
$ curl -i "http://localhost:3000/happybirthday.txt"
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Mon, 08 Jul 2013 06:45:21 GMT
Connection: keep-alive
Transfer-Encoding: chunked
Happy earthday to you!
Happy earthday to you!
Happy earthday to dear Liza!
Happy earthday to you!
NB: If your readable Stream that you're piping through the replacestream
is
paused, then you may have to call the .resume()
method on it.
You can also change the text encoding of the search and replace by setting an encoding property on the options object:
1// Replace the first 2 of the instances of 'birthday' with 'earthday' 2fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 3 .pipe(replaceStream('birthday', 'earthday', { limit: 2, encoding: 'ascii' } )) 4 .pipe(process.stdout);
By default the encoding will be set to 'utf8'.
Option | Default | Description |
---|---|---|
limit | Infinity | Sets a limit on the number of times the replacement will be made. This is forced to one when a regex without the global flag is provided. |
encoding | utf8 | The text encoding used during search and replace. |
maxMatchLen | 100 | When doing cross-chunk replacing, this sets the maximum length match that will be supported. |
ignoreCase | true | When doing string match (not relevant for regex matching) whether to do a case insensitive search. |
regExpOptions | undefined | (Deprecated) When provided, these flags will be used when creating the search regexes internally. This functionality is deprecated as the flags set on the regex provided are no longer mutated if this is not provided. |
It means that a replace should happen even if the string to be replaced is between streaming chunks of data. For example, say I do something like this
1fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) 2 .pipe(replaceStream('birthday', 'earthday')) 3 .pipe(process.stdout);
Here i am trying to replace all instances of 'birthday'
with 'earthday'
. Let's say the first chunk of data that is available is 'happy birth'
and the second chunk of data available is 'day'
. In this case the replace will happen successfully, the same as it would have if the chunk contained the entire string that was to be replaced (e.g. chunk1 = 'happy' chunk2 = 'birthday'
)
It does apply across multiple chunks. By default, however, the maximum match length (maxMatchLen
) is set to 100 characters. You can increase this by adding maxMatchLen: x
to your options:
1replacestream('hi', 'bye', {maxMatchLen: 1000})
A string the size of maxMatchLen
will be saved in memory so it shouldn't be set too high. maxMatchLen
is what allows us to have a match between chunks. We are saving maxMatchLen
characters in a string (the last maxMatchLen
characters from the previous chunks) that we prepend to the current chunk, then attempt to find a match.
As for regex it works exactly the same except you would pass a regular expression into replacestream:
1replacestream(/a+/, 'b')
replacestream is an OPEN Open Source Project. This means that:
Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
See the CONTRIBUTING.md file for more details.
replacestream is only possible due to the excellent work of the following contributors:
Eugene Ware | GitHub/eugeneware |
---|---|
Ryan Mehta | GitHub/mehtaphysical |
Tim Chaplin | GitHub/tjchaplin |
Bryce Gibson | GitHub/bryce-gibson |
Romain | GitHub/Filirom1 |
Shinnosuke Watanabe | GitHub/shinnn |
Steve Mao | GitHub/stevemao |
Martin Petluš | GitHub/martinpetlus |
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 4/26 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More