{"action":"created","issue":{"url":"https://api.github.com/repos/ariya/phantomjs/issues/13286","labels_url":"https://api.github.com/repos/ariya/phantomjs/issues/13286/labels{/name}","comments_url":"https://api.github.com/repos/ariya/phantomjs/issues/13286/comments","events_url":"https://api.github.com/repos/ariya/phantomjs/issues/13286/events","html_url":"https://github.com/ariya/phantomjs/issues/13286","id":82956133,"number":13286,"title":"onResourceReceived bodySize Error","user":{"login":"fengidri","id":3820861,"avatar_url":"https://avatars.githubusercontent.com/u/3820861?v=3","gravatar_id":"","url":"https://api.github.com/users/fengidri","html_url":"https://github.com/fengidri","followers_url":"https://api.github.com/users/fengidri/followers","following_url":"https://api.github.com/users/fengidri/following{/other_user}","gists_url":"https://api.github.com/users/fengidri/gists{/gist_id}","starred_url":"https://api.github.com/users/fengidri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fengidri/subscriptions","organizations_url":"https://api.github.com/users/fengidri/orgs","repos_url":"https://api.github.com/users/fengidri/repos","events_url":"https://api.github.com/users/fengidri/events{/privacy}","received_events_url":"https://api.github.com/users/fengidri/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-05-31T00:31:26Z","updated_at":"2015-06-18T09:57:00Z","closed_at":null,"body":"I want get the size of the response, and the response is chunked.\r\nBut, I find two questions.\r\n\r\n1. I just get two response with the stage is 'start' or 'end'. \r\n2. When the stage is 'end', the bodySize is undefined.\r\n\r\n```\r\npage.onResourceReceived = function (res) {\r\n    console.log('#:', res.id, ' rece:', res.stage, '   size:', res.bodySize);\r\n    if (res.stage === 'start') {\r\n        page.resources[res.id].startReply = res;\r\n    }\r\n    if (res.stage === 'end') {\r\n        page.resources[res.id].endReply = res;\r\n    }\r\n};\r\n\r\n\r\n```\r\n\r\n```\r\n#: 1  rece: start    size: 23971\r\n#: 1  rece: end    size: undefined\r\n#: 5  rece: start    size: 1516\r\n#: 2  rece: start    size: 29886\r\n#: 5  rece: end    size: undefined\r\n#: 3  rece: start    size: 15475\r\n#: 2  rece: end    size: undefined\r\n#: 4  rece: start    size: 7200\r\n#: 4  rece: end    size: undefined\r\n#: 3  rece: end    size: undefined\r\n#: 9  rece: start    size: 18715\r\n#: 7  rece: start    size: 13629\r\n#: 6  rece: start    size: 13404\r\n#: 8  rece: start    size: 13630\r\n#: 12  rece: start    size: 13931\r\n#: 11  rece: start    size: 13629\r\n#: 10  rece: start    size: 13629\r\n#: 13  rece: start    size: 13594\r\n#: 14  rece: start    size: 13629\r\n#: 40  rece: start    size: 1374\r\n#: 37  rece: start    size: 4985\r\n#: 40  rece: end    size: undefined\r\n#: 41  rece: start    size: 2406\r\n#: 39  rece: start    size: 7821\r\n#: 38  rece: start    size: 968\r\n#: 12  rece: end    size: undefined\r\n#: 15  rece: start    size: 974\r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/ariya/phantomjs/issues/comments/113096547","html_url":"https://github.com/ariya/phantomjs/issues/13286#issuecomment-113096547","issue_url":"https://api.github.com/repos/ariya/phantomjs/issues/13286","id":113096547,"user":{"login":"LinusU","id":189580,"avatar_url":"https://avatars.githubusercontent.com/u/189580?v=3","gravatar_id":"","url":"https://api.github.com/users/LinusU","html_url":"https://github.com/LinusU","followers_url":"https://api.github.com/users/LinusU/followers","following_url":"https://api.github.com/users/LinusU/following{/other_user}","gists_url":"https://api.github.com/users/LinusU/gists{/gist_id}","starred_url":"https://api.github.com/users/LinusU/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LinusU/subscriptions","organizations_url":"https://api.github.com/users/LinusU/orgs","repos_url":"https://api.github.com/users/LinusU/repos","events_url":"https://api.github.com/users/LinusU/events{/privacy}","received_events_url":"https://api.github.com/users/LinusU/received_events","type":"User","site_admin":false},"created_at":"2015-06-18T09:57:00Z","updated_at":"2015-06-18T09:57:00Z","body":"This is how it's supposed to work. If you want to know the entire file size you'll have to do something like this:\r\n\r\n```javascript\r\nvar acc = Object.create(null)\r\nvar onResourceComplete = function (info) {\r\n  console.log(info)\r\n}\r\n\r\npage.onResourceReceived = function (res) {\r\n  if (res.stage !== 'end') {\r\n    acc[res.id] = (acc[res.id] || 0) + res.bodySize\r\n    return\r\n  }\r\n\r\n  var info = {\r\n    bodySize: acc[res.id],\r\n\r\n    id: res.id\r\n    url: res.url\r\n    time: res.time\r\n    headers: res.headers\r\n    contentType: res.contentType\r\n    redirectURL: res.redirectURL\r\n    stage: res.stage\r\n    status: res.status\r\n    statusText: res.statusText\r\n  }\r\n  delete acc[res.id]\r\n  onResourceComplete(info)\r\n}\r\n```"}}