Skip to content

Commit c5dddae

Browse files
committed
Continue npm download batches after errors
1 parent 467252f commit c5dddae

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

app/models/ecosystem/npm.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def fetch_download_counts(names)
158158
json = get_json("https://api.npmjs.org/downloads/point/last-month/#{batch.join(',')}")
159159
next unless json.is_a?(Hash)
160160
json.each { |name, data| counts[name] = data['downloads'] if data.is_a?(Hash) }
161+
rescue
162+
next
161163
end
162164
scoped.each do |name|
163165
json = get_json("https://api.npmjs.org/downloads/point/last-month/#{name}")

test/models/ecosystem/npm_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ class NpmTest < ActiveSupport::TestCase
201201
assert_equal({}, @ecosystem.fetch_download_counts(['a', 'b']))
202202
end
203203

204+
test 'fetch_download_counts continues after a bulk request errors' do
205+
names = (1..130).map { |i| "pkg#{i}" }
206+
stub_request(:get, %r{https://api\.npmjs\.org/downloads/point/last-month/pkg1,.*,pkg128$})
207+
.to_return(status: 500, body: 'not json', headers: { 'Content-Type' => 'application/json' })
208+
stub_request(:get, "https://api.npmjs.org/downloads/point/last-month/pkg129,pkg130")
209+
.to_return(status: 200, body: '{"pkg129":{"downloads":1}}', headers: { 'Content-Type' => 'application/json' })
210+
assert_equal 1, @ecosystem.fetch_download_counts(names)['pkg129']
211+
end
212+
204213
test 'check_status uses memoized metadata without extra HTTP request' do
205214
stub_request(:get, "https://registry.npmjs.org/base62")
206215
.to_return({ status: 200, body: file_fixture('npm/base62') })

0 commit comments

Comments
 (0)