rr
This commit is contained in:
@@ -1,59 +1,55 @@
|
||||
const { createReadStream } = require('fs')
|
||||
|
||||
class ReadApiList {
|
||||
apiListPath = "";
|
||||
lineLength = 0;
|
||||
textArr = [];
|
||||
lineBuffer = Buffer.alloc(4096);
|
||||
text = "";
|
||||
status = false;
|
||||
lineBuffer = [];
|
||||
lineLength = 0;
|
||||
stream = null
|
||||
|
||||
constructor(path) {
|
||||
this.apiListPath = path
|
||||
this.stream = createReadStream(path);
|
||||
}
|
||||
|
||||
getFileList() {
|
||||
this.text = createReadStream(this.apiListPath)
|
||||
return new Promise((s, e) => {
|
||||
this.textArr = [];
|
||||
let status = false
|
||||
this.text.on('data', (data) => {
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
if (data[i] == 10 || data[i] == 13) {
|
||||
if (data[i] == 10) {
|
||||
try {
|
||||
var line = this.lineBuffer.slice(0, this.lineLength);
|
||||
if (line.length != 0) {
|
||||
let str = line.toString('utf8')
|
||||
.replace(/^\s+|\s+$/g, '')
|
||||
.replace(/\'/g, "")
|
||||
.replace(/,/g, "")
|
||||
return new Promise((resolve, reject) => {
|
||||
this.stream.on('data', (data) => {
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const byte = data[i];
|
||||
if (byte === 10 || byte === 13) { // 处理 \n 和 \r
|
||||
if (this.lineLength > 0) {
|
||||
const line = Buffer.from(this.lineBuffer.slice(0, this.lineLength)).toString('utf8');
|
||||
const cleanedLine = line.trim().replace(/'|,/g, '');
|
||||
|
||||
if (str.includes("start -")) {
|
||||
status = true
|
||||
continue;
|
||||
}
|
||||
if (str.includes("end -")) {
|
||||
status = false
|
||||
break;
|
||||
}
|
||||
if (cleanedLine.includes("start -")) {
|
||||
this.status = true;
|
||||
} else if (cleanedLine.includes("end -")) {
|
||||
this.status = false;
|
||||
|
||||
if (status) {
|
||||
// console.log("str: ", str.split(":"))
|
||||
this.textArr.push(str);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this.lineLength = 0;
|
||||
// 去除被注释的key代码
|
||||
} else if (this.status && !cleanedLine.includes("//")) {
|
||||
this.textArr.push(cleanedLine);
|
||||
}
|
||||
}
|
||||
this.lineLength = 0; // 重置行缓冲区
|
||||
} else {
|
||||
this.lineBuffer[this.lineLength] = data[i];
|
||||
if (this.lineLength >= this.lineBuffer.length) {
|
||||
this.lineBuffer.push(byte);
|
||||
} else {
|
||||
this.lineBuffer[this.lineLength] = byte;
|
||||
}
|
||||
this.lineLength++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
s(this.textArr)
|
||||
})
|
||||
this.stream.on('end', () => {
|
||||
resolve(this.textArr);
|
||||
});
|
||||
|
||||
this.stream.on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user