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

req_perform_stream(round = c("byte", "line")) #437

Merged
merged 18 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions R/req-perform-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,30 @@ req_perform_stream <- function(req,
}

if (length(buf) > 0) {
# there are leftover bytes, but the stream is complete
# break the loop so that the callback() is given the
# whole buffer
if (!incomplete) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Work it work to move this higher? i.e. make it the other side of the if (incomplete) statement above.

break
}

cut <- cut_points(buf)
n <- length(cut)
if (n) {
continue <- isTRUE(callback(head(buf, n = cut[n])))
buf <- tail(buf, n = -cut[n])
} else {
continue <- incomplete
}
} else {
continue <- incomplete
}
}

# if there are leftover bytes and none of the callback()
# returned FALSE.
if (continue && length(buf)) {
callback(buf)
}

data <- curl::handle_data(handle)
new_response(
method = req_method_get(req),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-req-perform-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test_that("can supply custom rounding", {
buffer_kb = 0.1,
round = function(bytes) if (length(bytes) > 100) 100 else integer()
)
expect_equal(lengths(out), rep(100, 10))
expect_equal(lengths(out), c(rep(100, 10), 24))
})

test_that("eventually terminates even if never rounded", {
Expand All @@ -50,7 +50,7 @@ test_that("eventually terminates even if never rounded", {
buffer_kb = 0.1,
round = function(bytes) integer()
)
expect_equal(length(out), 0)
expect_equal(length(out), 1024)
})

test_that("as_round_function checks its inputs", {
Expand Down
Loading