const request = require('request');
const URL = 'http://de.releases.ubuntu.com/xenial/ubuntu-16.04.3-desktop-amd64.iso';
const MAX_SIZE = 10 * 1024 * 1024 // 10MB , maximum size to download
let total_bytes_read = 0;
1 - サーバーからの応答がgzip圧縮されている場合は、 gzipオプションを有効にする必要があります。 https://github.com/request/request#examples下位互換性のため、応答圧縮はデフォルトで でサポートされていません。 gzip圧縮応答を受け入れるには、gzipオプション をtrueに設定します。
request
.get({
uri: URL,
gzip: true
})
.on('error', function (error) {
//TODO: error handling
console.error('ERROR::', error);
})
.on('data', function (data) {
// decompressed data
console.log('Decompressed chunck Recived:' + data.length, ': Total downloaded:', total_bytes_read)
total_bytes_read += data.length;
if (total_bytes_read >= MAX_SIZE) {
//TODO: handle exceeds max size event
console.error("Request exceeds max size.");
throw new Error('Request exceeds max size'); //stop
}
})
.on('response', function (response) {
response.on('data', function (chunk) {
//compressed data
console.log('Compressed chunck Recived:' + chunk.length, ': Total downloaded:', total_bytes_read)
});
})
.on('end', function() {
console.log('Request completed! Total size downloaded:', total_bytes_read)
});
NB:サーバーが応答を圧縮するものではありませんが、まだGZIP オプション/解凍を使用する場合は、解凍チャンク&は、元のチャンクは 等しくなります。応答はそうしない圧縮されていない場合は - したがって、あなたは、応答が圧縮されている場合は あなたは解凍チャンク
2のサイズ制限を確認する必要があります( 解凍/圧縮チャンクからの)リミットチェックのいずれかの方法を行うことができます
request
.get(URL)
.on('error', function (error) {
//TODO: error handling
console.error('ERROR::', error);
})
.on('response', function (response) {
response.on('data', function (chunk) {
//compressed data
console.log('Recived chunck:' + chunk.length, ': Total downloaded:', total_bytes_read)
total_bytes_read += chunk.length;
if (total_bytes_read >= MAX_SIZE) {
//TODO: handle exceeds max size event
console.error("Request as it exceds max size:")
throw new Error('Request as it exceds max size');
}
console.log("...");
});
})
.on('end', function() {
console.log('Request completed! Total size downloaded:', total_bytes_read)
});
はこのヘルプをい 解凍するgzipのオプションが必要ですか? - https://stackoverflow.com/questions/15636095/how-to-limit-response-length-with-http-request-in-node-js – Jackthomson
ありがとうございますが、リクエストと呼ばれるパッケージを使用していますnodejs HTTPリクエストのバージョン。それにもかかわらず、答えが得られない場合は、リクエストパッケージをノードからのHTTPリクエストに置き換える必要があるため、あなたの答えは役に立つかもしれません。 –
あなたはこのパッケージについて話していますか? - https://www.npmjs.com/package/request - 上記のリンクされた答えとまったく同じように使用できない場合、このリクエストライブラリは同じ応答オブジェクトを返します。プロトタイプをオンにして、それにちょうど同じ権利をフックすることができますか?または、このライブラリはその機能を削除しますか?このライブラリは、ちょっとした構文を使って少し簡単にしているようです。そのコアでは、ノード要求と応答オブジェクトを返すだけです。 – Jackthomson