国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Node.js從字符串生成文件流的實(shí)現(xiàn)方法

2019-11-19 11:01:02
字體:
供稿:網(wǎng)友

一.背景

在文件相關(guān)的數(shù)據(jù)加工等場(chǎng)景下,經(jīng)常面臨生成的物理文件應(yīng)該如何處理的問題,比如:

生成的文件放到哪里,路徑存在不存在?

臨時(shí)文件何時(shí)清理,如何解決命名沖突,防止覆蓋?

并發(fā)場(chǎng)景下的讀寫順序如何保證?

……

對(duì)于讀寫物理文件帶來的這些問題,最好的解決辦法就是 不寫文件 。然而,一些場(chǎng)景下想要不寫文件可不那么容易,比如文件上傳

二.問題

文件上傳一般通過表單提交來實(shí)現(xiàn),例如:

var FormData = require('form-data');var fs = require('fs');var form = new FormData();form.append('my_file', fs.createReadStream('/foo/bar.jpg'));form.submit('example.org/upload', function(err, res) { console.log(res.statusCode);});

(摘自 Form-Data

不想寫物理文件的話,可以這樣做:

const FormData = require('form-data');const filename = 'my-file.txt';const content = 'balalalalala...變身';const formData = new FormData();// 1.先將字符串轉(zhuǎn)換成Bufferconst fileContent = Buffer.from(content);// 2.補(bǔ)上文件meta信息formData.append('file', fileContent, { filename, contentType: 'text/plain', knownLength: fileContent.byteLength});

也就是說,文件流除了能夠提供數(shù)據(jù)外,還具有一些 meta 信息,如文件名、文件路徑等 ,而這些信息是普通 Stream 所不具備的。那么,有沒有辦法憑空創(chuàng)建一個(gè)“真正的”文件流?

三.思路

要想創(chuàng)建出“真正的”文件流,至少有正反 2 種思路:

給普通流添上文件相關(guān)的 meta 信息

先拿到一個(gè)真正的文件流,再改掉其數(shù)據(jù)和 meta 信息

顯然,前者更靈活一些,并且實(shí)現(xiàn)上能夠做到完全不依賴文件

文件流的生產(chǎn)過程

沿著憑空創(chuàng)造的思路,探究 fs.createReadStream API 的 內(nèi)部實(shí)現(xiàn) 之后發(fā)現(xiàn),生產(chǎn)文件流的關(guān)鍵過程如下:

function ReadStream(path, options) { // 1.打開path指定的文件 if (typeof this.fd !== 'number')  this.open();}ReadStream.prototype.open = function() { fs.open(this.path, this.flags, this.mode, (er, fd) => {  // 2.拿到文件描述符并持有  this.fd = fd;  this.emit('open', fd);  this.emit('ready');  // 3.開始流式讀取數(shù)據(jù)  // read來自父類Readable,主要調(diào)用內(nèi)部方法_read  // ref: https://github.com/nodejs/node/blob/v10.16.3/lib/_stream_readable.js#L390  this.read(); });};ReadStream.prototype._read = function(n) { // 4.從文件中讀取一個(gè)chunk fs.read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => {  let b = null;  if (bytesRead > 0) {   this.bytesRead += bytesRead;   b = thisPool.slice(start, start + bytesRead);  }  // 5.(通過觸發(fā)data事件)吐出一個(gè)chunk,如果還有數(shù)據(jù),process.nextTick再次this.read,直至this.push(null)觸發(fā)'end'事件  // ref: https://github.com/nodejs/node/blob/v10.16.3/lib/_stream_readable.js#L207  this.push(b); });};

P.S.其中第 5 步相對(duì)復(fù)雜, this.push(buffer) 既能觸發(fā)下一個(gè) chunk 的讀取( this.read() ),也能在數(shù)據(jù)讀完之后(通過 this.push(null) )觸發(fā) 'end' 事件,具體見 node/lib/_stream_readable.js

重新實(shí)現(xiàn)文件流

既然已經(jīng)摸清了文件流的生產(chǎn)過程,下一步自然是 替換掉所有文件操作,直至文件流的實(shí)現(xiàn)完全不依賴文件 ,例如:

// 從文件中讀取一個(gè)chunkfs.read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => { /* ... */});// 換成this._fakeReadFile(this.fd, pool, pool.used, toRead, this.pos, (bytesRead) => { /* ... */});// 從輸入字符串對(duì)應(yīng)的Buffer中copy出一個(gè)chunkReadStream.prototype._fakeReadFile = function(_, buffer, offset, length, position, cb) { position = position || this.input._position; // fake read file async setTimeout(() => {  let bytesRead = 0;  if (position < this.input.byteLength) {   bytesRead = this.input.copy(buffer, offset, position, position + length - 1);   this.input._position += bytesRead;  }  cb(bytesRead); }, 0);}

即從中剔除文件操作,用基于字符串的操作去替代它們

四.解決方案

如此這般,就有了 ayqy/string-to-file-stream ,用來憑空創(chuàng)建文件流:

string2fileStream('string-content') === fs.createReadStream(/* path to a text file with content 'string-content' */)`

例如:

const string2fileStream = require('string-to-file-stream');const input = 'Oh, my great data!';const s = string2fileStream(input);s.on('data', (chunk) => { assert.equal(chunk.toString(), input);});生成的流同樣能夠具有文件 meta 信息:const string2fileStream = require('string-to-file-stream');const formData = new FormData();formData.append('file', string2fileStream('my-string-data', { path: './abc.txt' }));form.submit('example.org/upload', function(err, res) { console.log(res.statusCode);});

足夠以假亂真

參考資料

fs.createReadStream(path[, options])

fs/streams.js

_stream_readable.js

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 颍上县| 乌兰察布市| 邯郸市| 上饶市| 泰和县| 临湘市| 通河县| 长岭县| 三亚市| 卢湾区| 仁寿县| 卓资县| 稻城县| 军事| 贵定县| 通州市| 岳阳市| 台安县| 大方县| 南康市| 南丹县| 九寨沟县| 旬邑县| 通城县| 酒泉市| 黄大仙区| 乐至县| 邮箱| 昌平区| 和田市| 青河县| 霍城县| 临潭县| 临颍县| 福贡县| 兰溪市| 巩义市| 韶山市| 龙门县| 冕宁县| 临清市|