Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

prefix-stream-write.js 831 B

You have to be logged in to leave a comment. Sign In
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
  1. module.exports = function prefixStreamWrite (writableStream, prefix) {
  2. const oldWrite = writableStream.write
  3. function newWrite (...args) {
  4. const [chunk, encoding] = args
  5. // Prepend the prefix if the chunk is either a string or a Buffer.
  6. // Otherwise, just leave it alone to be safe.
  7. if (typeof chunk === 'string') {
  8. // Only prepend the prefix is the `encoding` is safe or not provided.
  9. // If it's a function, it is third arg `callback` provided as optional second
  10. if (!encoding || encoding === 'utf8' || typeof encoding === 'function') {
  11. args[0] = prefix + chunk
  12. }
  13. } else if (Buffer.isBuffer(chunk)) {
  14. args[0] = Buffer.concat([Buffer.from(prefix), chunk])
  15. }
  16. return oldWrite.apply(this, args)
  17. }
  18. writableStream.write = newWrite
  19. return writableStream
  20. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...