【发布时间】:2020-08-15 17:31:38
【问题描述】:
我的应用程序在这个阶段的目的是获取一个输入文件并将常见的密码替换添加到给定的密码短语中。问题是它对一个可能很大的文件(1+GB 的文本)执行此操作,每行一个密码,然后通过writeStream 将生成的潜在替换列表附加回相同的输入文件。
例如,输入 5_passwords.txt 包含:
password
hello
chicken
bye
bobthebuilder
可能会产生这样的文件:
...
p@s$w0rd
h3ll0
ch1ck3n
8y3
b0bth3bu11d34
...
当使用大文件时,内存使用量也会增加,但据我了解应该保持不变。以下是Memory use: ${Math.round(process.memoryUsage().heapUsed / 1024 / 1024 * 100 / 100)}MB的实际控制台输出日志
开始时的内存使用: Memory use: 20MB(关于应用程序应该使用的内容)
10 秒后的内存使用情况: Memory use: 476MB(呃哦...)
30 秒后的内存使用情况: Memory use: 1159MB (LIKE WOAH)
以下是我的相关代码:
const commonReplacesments = {
a : [
'4',
'@'
],
b : [
'8'
],
c : [
'(',
'{',
'[',
'<'
],
e : [
'3'
],
g : [
'6',
'9'
],
i : [
'1',
'!',
'|'
],
l : [
'1',
'|',
'7'
],
o : [
'0'
],
p : [
'9'
],
r : [
'4'
],
s : [
'$',
'5'
],
t : [
'+',
'7'
],
x : [
'%'
],
z : [
'2'
]
}
// word ... is the word to generate *some* possible manipulations on using the commonreplacements above
const replaceWord = async (word) => {
let wordsWithReplacements = []
for (let i = 0; i < word.length; i++) {
let currentWord = word
if (currentWord[i] in commonReplacesments) {
for (let j = 0; j < commonReplacesments[currentWord[i]].length; j++) {
let replacer = new RegExp(currentWord[i], 'g')
currentWord = currentWord.replace(replacer, commonReplacesments[currentWord[i][j])
wordsWithReplacements.push(currentWord)
// Reset word
currentWord = word
console.log(
`[${Date.now()}] Memory use: ${Math.round(
process.memoryUsage().heapUsed / 1024 / 1024 * 100 / 100
)}MB`
)
}
}
}
return wordsWithReplacements
}
// passFile ... Is the absolute file path to the password file being manipulated.
const addCommonReplacements = async (passFile) => {
const readStream = fs.createReadStream(passFile)
console.log('Adding common replacements to existing phrases in password list...')
try {
await util.promisify(stream.pipeline)(async function*() {
for await (const password of readStream) {
yield `${await (await replaceWord(`${password}`)).join('\n')}`
}
}, fs.createWriteStream(passFile, { flags: 'a' }))
} catch (err) {
Promise.reject(`ERROR: Failed to write common replacements to file...${err}`)
}
}
如果有人能提供一些见解来帮助我解决这个问题,我将不胜感激:)
谢谢!
【问题讨论】:
标签: node.js api file express io