Hướng dẫn create a stream in nodejs - tạo luồng trong nodejs

Tôi đang sử dụng thư viện, YA-CSV, mong đợi một tệp hoặc luồng làm đầu vào, nhưng tôi có một chuỗi.

Làm cách nào để chuyển đổi chuỗi đó thành một luồng trong nút?

Hướng dẫn create a stream in nodejs - tạo luồng trong nodejs

Đã hỏi ngày 6 tháng 10 năm 2012 lúc 1:50Oct 6, 2012 at 1:50

pathikritpathikritpathikrit

31.7K35 Huy hiệu vàng139 Huy hiệu bạc220 Huy hiệu đồng35 gold badges139 silver badges220 bronze badges

Khi @substack sửa tôi trong #Node, API luồng mới trong Node V10 giúp điều này dễ dàng hơn:

const Readable = require('stream').Readable;
const s = new Readable();
s._read = () => {}; // redundant? see update below
s.push('your text here');
s.push(null);

Sau đó, bạn có thể tự do ống nó hoặc chuyển nó cho người tiêu dùng dự định của bạn.

Nó không sạch sẽ như một bộ đệm, nhưng nó tránh được sự phụ thuộc thêm.

(Cập nhật: Trong V0.10.26 đến V9.2 Sự không nhất quán khiến bạn lo lắng, bao gồm

var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
3.)

Đã trả lời ngày 28 tháng 2 năm 2014 lúc 3:54Feb 28, 2014 at 3:54

11

Không sử dụng câu trả lời sơ yếu lý lịch của Jo Liss. Nó sẽ hoạt động trong hầu hết các trường hợp, nhưng trong trường hợp của tôi, nó đã mất tôi một lỗi 4 hoặc 5 giờ tốt. Không cần các mô -đun bên thứ ba để làm điều này.

Câu trả lời mới::

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream

Đây phải là một luồng có thể đọc được đầy đủ. Xem ở đây để biết thêm thông tin về cách sử dụng luồng đúng cách.

Câu trả lời cũ: Chỉ cần sử dụng luồng thông qua gốc:: Just use the native PassThrough stream:

var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})

Lưu ý rằng sự kiện 'Đóng' không được phát ra (không được yêu cầu bởi các giao diện luồng).

Hướng dẫn create a stream in nodejs - tạo luồng trong nodejs

Sziraqui

5.5253 Huy hiệu vàng26 Huy hiệu bạc37 Huy hiệu đồng3 gold badges26 silver badges37 bronze badges

Đã trả lời ngày 3 tháng 9 năm 2014 lúc 17:17Sep 3, 2014 at 17:17

B tb tB T

54.4K34 Huy hiệu vàng180 Huy hiệu bạc200 Huy hiệu đồng34 gold badges180 silver badges200 bronze badges

2

Từ Node 10.17, Stream.Readable có phương thức

var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
4 để dễ dàng tạo các luồng từ bất kỳ điều gì có thể sử dụng được (bao gồm các văn bản mảng):

const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})

Lưu ý rằng ít nhất là từ 10.17 đến 12.3, một chuỗi tự nó là một điều có thể, vì vậy

var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
5 sẽ hoạt động, nhưng phát ra một sự kiện cho mỗi ký tự.
var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
6 sẽ phát ra một sự kiện cho mỗi mục trong mảng (trong trường hợp này là một mục).

Cũng lưu ý rằng trong các nút sau này (có thể là 12.3, vì tài liệu nói rằng hàm đã được thay đổi sau đó), không còn cần thiết phải quấn chuỗi trong một mảng.

https://nodejs.org/api/stream.html#stream_stream_readable_from_iterable_options

John

28.6K11 Huy hiệu vàng75 Huy hiệu bạc78 Huy hiệu đồng11 gold badges75 silver badges78 bronze badges

Đã trả lời ngày 8 tháng 1 năm 2020 lúc 1:02Jan 8, 2020 at 1:02

FizkerfizkerFizker

2.2141 Huy hiệu vàng17 Huy hiệu bạc12 Huy hiệu đồng1 gold badge17 silver badges12 bronze badges

2

Chỉ cần tạo một thể hiện mới của mô -đun

var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
7 và tùy chỉnh nó theo nhu cầu của bạn:

var Stream = require('stream');
var stream = new Stream();

stream.pipe = function(dest) {
  dest.write('your string');
  return dest;
};

stream.pipe(process.stdout); // in this case the terminal, change to ya-csv

hoặc

var Stream = require('stream');
var stream = new Stream();

stream.on('data', function(data) {
  process.stdout.write(data); // change process.stdout to ya-csv
});

stream.emit('data', 'this is my string');

Hướng dẫn create a stream in nodejs - tạo luồng trong nodejs

Đã trả lời ngày 10 tháng 10 năm 2012 lúc 18:27Oct 10, 2012 at 18:27

Hướng dẫn create a stream in nodejs - tạo luồng trong nodejs

Zemircozemircozemirco

15.8k8 Huy hiệu vàng60 Huy hiệu bạc94 Huy hiệu Đồng8 gold badges60 silver badges94 bronze badges

2

Chỉnh sửa: Câu trả lời của Garth có lẽ tốt hơn. Garth's answer is probably better.

Văn bản câu trả lời cũ của tôi được bảo tồn dưới đây.


Để chuyển đổi một chuỗi thành luồng, bạn có thể sử dụng một luồng thông qua luồng:

through().pause().queue('your string').end()

Example:

var through = require('through')

// Create a paused stream and buffer some data into it:
var stream = through().pause().queue('your string').end()

// Pass stream around:
callback(null, stream)

// Now that a consumer has attached, remember to resume the stream:
stream.resume()

Đã trả lời ngày 9 tháng 5 năm 2013 lúc 5:56May 9, 2013 at 5:56

Jo Lissjo LissJo Liss

28.6K18 Huy hiệu vàng115 Huy hiệu bạc163 Huy hiệu đồng18 gold badges115 silver badges163 bronze badges

4

Có một mô-đun cho điều đó: https://www.npmjs.com/package/string-to-stream

var str = require('string-to-stream')
str('hi there').pipe(process.stdout) // => 'hi there' 

Upthecalet

30.7K33 Huy hiệu vàng151 Huy hiệu bạc220 Huy hiệu đồng33 gold badges151 silver badges220 bronze badges

Đã trả lời ngày 30 tháng 8 năm 2015 lúc 23:44Aug 30, 2015 at 23:44

LoriloriLori

1.3701 Huy hiệu vàng25 Huy hiệu bạc29 Huy hiệu đồng1 gold badge25 silver badges29 bronze badges

3

Một giải pháp khác là chuyển chức năng đọc cho hàm tạo của các tùy chọn có thể đọc được (CF doc có thể đọc được)

var s = new Readable({read(size) {
    this.push("your string here")
    this.push(null)
  }});

Bạn có thể sau khi sử dụng s.pipe cho ví dụ

Đã trả lời ngày 5 tháng 12 năm 2017 lúc 10:34Dec 5, 2017 at 10:34

Philippe T.Philippe T.Philippe T.

1.1727 huy hiệu bạc11 huy hiệu đồng7 silver badges11 bronze badges

4

Trong ấn phẩm cà phê:

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
0

sử dụng nó:

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
1

Đã trả lời ngày 23 tháng 5 năm 2014 lúc 8:11May 23, 2014 at 8:11

Xinthinkxinthinkxinthink

1.4201 Huy hiệu vàng18 Huy hiệu bạc22 Huy hiệu đồng1 gold badge18 silver badges22 bronze badges

Tôi đã cảm thấy mệt mỏi khi phải học lại điều này sáu tháng một lần, vì vậy tôi vừa xuất bản một mô-đun NPM để trừu tượng hóa các chi tiết thực hiện:

https://www.npmjs.com/package/streamify-string

Đây là cốt lõi của mô -đun:

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
2

var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
8 là
var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
9 phải được truyền cho hàm tạo khi gọi và sẽ được phát bởi luồng dưới dạng dữ liệu.
const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})
0 là các tùy chọn điển hình có thể được truyền vào một luồng, theo tài liệu.

Theo Travis CI, nó phải tương thích với hầu hết các phiên bản của nút.

Đã trả lời ngày 16 tháng 8 năm 2016 lúc 1:07Aug 16, 2016 at 1:07

Chris Allen Lanechris Allen LaneChris Allen Lane

6.2645 Huy hiệu vàng25 Huy hiệu bạc 30 Huy hiệu Đồng5 gold badges25 silver badges30 bronze badges

1

Đây là một giải pháp gọn gàng trong TypeScript:

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
3

Đã trả lời ngày 14 tháng 7 năm 2019 lúc 6:18Jul 14, 2019 at 6:18

Trong NodeJS, bạn có thể tạo một luồng có thể đọc theo một số cách:

Giải pháp 1

Bạn có thể làm điều đó với mô -đun

const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})
1. Hàm
const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})
2 cho phép bạn mở một luồng có thể đọc được và tất cả những gì bạn phải làm là vượt qua đường dẫn của tệp để bắt đầu phát trực tuyến.

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
4

Giải pháp 2

Nếu bạn không muốn tạo tệp, bạn có thể tạo một luồng trong bộ nhớ và làm một cái gì đó với nó (ví dụ: tải nó lên ở đâu đó). Bạn có thể làm điều này với mô -đun

var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
7. Bạn có thể nhập
const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})
4 từ mô -đun
var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})
7 và bạn có thể tạo một luồng có thể đọc được. Khi tạo một đối tượng, bạn cũng có thể thực hiện phương thức
const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})
6 được sử dụng để đọc dữ liệu ra khỏi bộ đệm bên trong. Nếu không có dữ liệu có sẵn để đọc,
const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})
7 sẽ được trả về. Đối số
const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})
8 tùy chọn chỉ định một số byte cụ thể để đọc. Nếu đối số
const { Readable } = require("stream")

const readable = Readable.from(["input string"])

readable.on("data", (chunk) => {
  console.log(chunk) // will be called once with `"input string"`
})
8 không được chỉ định, tất cả dữ liệu có trong bộ đệm bên trong sẽ được trả về.

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
5

Giải pháp 3

Khi bạn đang tìm nạp một cái gì đó trên mạng, điều đó có thể được tìm nạp như luồng (ví dụ: bạn đang tìm nạp tài liệu PDF từ một số API).

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
6

Giải pháp 4

Các gói của bên thứ ba có thể hỗ trợ tạo các luồng như một tính năng. Đó là một cách với gói

var Stream = require('stream');
var stream = new Stream();

stream.pipe = function(dest) {
  dest.write('your string');
  return dest;
};

stream.pipe(process.stdout); // in this case the terminal, change to ya-csv
0 thường được sử dụng để tải các tệp lên
var Stream = require('stream');
var stream = new Stream();

stream.pipe = function(dest) {
  dest.write('your string');
  return dest;
};

stream.pipe(process.stdout); // in this case the terminal, change to ya-csv
1.

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
7

Đã trả lời ngày 4 tháng 5 năm 2021 lúc 5:58May 4, 2021 at 5:58

Hướng dẫn create a stream in nodejs - tạo luồng trong nodejs

NenadnenadNeNaD

13,5K7 Huy hiệu vàng29 Huy hiệu bạc76 Huy hiệu đồng7 gold badges29 silver badges76 bronze badges

2

JavaScript được gõ vịt, vì vậy nếu bạn chỉ sao chép API của luồng có thể đọc được, nó sẽ hoạt động tốt. Trên thực tế, bạn có thể không thể thực hiện hầu hết các phương pháp đó hoặc chỉ để chúng như những sơ khai; Tất cả những gì bạn cần thực hiện là những gì thư viện sử dụng. Bạn cũng có thể sử dụng lớp

var Stream = require('stream');
var stream = new Stream();

stream.pipe = function(dest) {
  dest.write('your string');
  return dest;
};

stream.pipe(process.stdout); // in this case the terminal, change to ya-csv
2 được xây dựng trước của Node để đối phó với các sự kiện, vì vậy bạn không phải thực hiện
var Stream = require('stream');
var stream = new Stream();

stream.pipe = function(dest) {
  dest.write('your string');
  return dest;
};

stream.pipe(process.stdout); // in this case the terminal, change to ya-csv
3 và chính mình.

Đây là cách bạn có thể thực hiện nó trong CoffeeScript:

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
8

Sau đó, bạn có thể sử dụng nó như vậy:

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream
9

Đã trả lời ngày 6 tháng 10 năm 2012 lúc 2:04Oct 6, 2012 at 2:04

icktoofayicktoofayicktoofay

124K20 Huy hiệu vàng243 Huy hiệu bạc229 Huy hiệu Đồng20 gold badges243 silver badges229 bronze badges

3

Làm cách nào để tạo một luồng nút?

Bạn có thể làm điều đó với mô -đun FS.Hàm fs.createleadstream () cho phép bạn mở một luồng có thể đọc được và tất cả những gì bạn phải làm là truyền đường dẫn của tệp để bắt đầu phát trực tuyến. Nếu bạn không muốn tạo tệp, bạn có thể tạo một bộ nhớStream và làm một cái gì đó với nó (ví dụ, tải lên ở đâu đó).The function fs. createReadStream() allows you to open up a readable stream and all you have to do is pass the path of the file to start streaming in. If you don't want to create file, you can create an in-memory stream and do something with it (for example, upload it somewhere).

Node.js có phù hợp cho truyền phát video không?

JS là thời gian chạy được sử dụng để xây dựng các ứng dụng nhanh và có thể mở rộng.Chúng tôi sẽ sử dụng nó để xử lý các video tìm nạp và phát trực tuyến, tạo hình thu nhỏ cho video và phục vụ chú thích và phụ đề cho video.Nuxt.We will use it to handle fetching and streaming videos, generating thumbnails for videos, and serving captions and subtitles for videos. Nuxt.

Các luồng nút được sử dụng để làm gì?

Nút.Các luồng JS được sử dụng để đọc và liên tục ghi dữ liệu.Các luồng hoạt động khác với các kỹ thuật truyền thống đọc hoặc ghi dữ liệu, yêu cầu dữ liệu được đọc và lưu trữ trong bộ nhớ trước khi được xử lý.to read and continuously write data. Streams work differently from traditional techniques that read or write data, which require the data to be read and stored in memory before being processed.