Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the file downloaded by request.pipe may be defect #3479

Open
yu0winter opened this issue Dec 29, 2023 · 1 comment
Open

the file downloaded by request.pipe may be defect #3479

yu0winter opened this issue Dec 29, 2023 · 1 comment

Comments

@yu0winter
Copy link

Summary

Download a file with request.pipe ,the file may be defect. I try 1000 time, and 60 ~ 80 of them were defect.
But when I try add a 100ms sleep, all of time will be ok.
Guess the request will end when the steam does not end.

Simplest Example to Reproduce

async function download(url, filepath) {
	console.log('download function : url=' + url + ' filepath=' + filepath);
	return new Promise(function (reslove, reject) {
		console.log(`start download url=${url}, filepath=${filepath}`);
		if (fse.existsSync(filepath)) {
			fse.removeSync(filepath);
			console.log('filepath=', filepath, ' exist and delete it');
		}
		// console.log('createWriteStream filepath stream =' + filepath);
		// let stream = fse.createWriteStream(filepath);
        let url = "";
        let filepath = "";
        request(url, function (error, response) {
            console.log(`finish download url=${url}, filepath=${filepath}\nerror:${error}, statusCode:${response.statusCode}, statusMessage:${response.statusMessage}`);
            if (error) {
                console.error('error:', error);
                reslove({ code: -1, msg: `error:${error}` });
                return;
            }
            if (response && (response.statusCode >= 200 || response.statusCode < 300)) {
                reslove({ code: 0, data: filepath });
            } else {
                if (fse.existsSync(filepath)) {
                    fse.removeSync(filepath);
                }
                reslove({ code: -1, msg: `statusCode:${response.statusCode};statusMessage:${response.statusMessage}` });
            }
        }).pipe(fs.createWriteStream(filepath));
    });
}

Expected Behavior

try 1000 time , and expect all right.

Current Behavior

60 ~ 80 of 1000 time were defect.

Possible Solution

Add a 100ms sleep, all of time will be ok.
So Guess the request will end when the steam does not end.

Context

Your Environment

software version
request 2.88.2
node v10.17.0
npm 6.11.3
Operating System MacOS 14.0
@yu0winter
Copy link
Author

Test Code :

const request = require("request");
const fs = require("fs");
const fse = require('fs-extra');
const crypto = require("crypto");
const filemd5 = (file) => {
	var buffer = fs.readFileSync(file);
    return crypto.createHash('md5').update(buffer).digest('hex');
	// return md5(buffer);
};
function sleep(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}
async function testMethod() {
    let errorList = [];
    let md5ErrorList = [];
    try {
        // 上传文件,并立刻下载,测试下载问题
        const fileUrl = "TODO";
        const fileSize = "TODO";
        const md5 = "TODO";
        for (let i = 0; i < 1000; i++) {
            let fileUrl = result.data;
            let downloadFilePath = `TODO`;
            result = await download(fileUrl, downloadFilePath);
            // sleep 100ms wil be ok
            await sleep(100)

            if (result.code != 0) {
                throw result;
            }
            let fail = false
            var info = fs.statSync(downloadFilePath);
            if (fileSize != info.size) {
                console.error(`文件大小不一致: ${fileSize} vs ${info.size} `);
                errorList.push(`[${i}]文件大小不一致: ${fileSize} vs ${info.size} `)
                fail = true;
            }
            let newMd5 = filemd5(downloadFilePath);
            if (md5 != newMd5) {
                console.error(`[${i}]md5不一致: ${md5} vs ${newMd5} `);
                md5ErrorList.push(`[${i}]md5不一致: ${md5} vs ${newMd5} `)
                fail = true;
            }
            // if (fail) {
            //     throw("失败")
            // }
        }
        console.info("=========== 完成 ===========")
        console.log("文件大小:" +  errorList.length + "\n" + JSON.stringify(errorList, null, '\t'));
        console.log("MD5:" +  md5ErrorList.length + "\n" + JSON.stringify(md5ErrorList, null, '\t'));
    } catch (error) {
        console.error(error);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant