{"action":"created","issue":{"url":"https://api.github.com/repos/koajs/koa/issues/948","repository_url":"https://api.github.com/repos/koajs/koa","labels_url":"https://api.github.com/repos/koajs/koa/issues/948/labels{/name}","comments_url":"https://api.github.com/repos/koajs/koa/issues/948/comments","events_url":"https://api.github.com/repos/koajs/koa/issues/948/events","html_url":"https://github.com/koajs/koa/issues/948","id":216111161,"number":948,"title":"using crypto with koa 2","user":{"login":"tvq","id":2807059,"avatar_url":"https://avatars2.githubusercontent.com/u/2807059?v=3","gravatar_id":"","url":"https://api.github.com/users/tvq","html_url":"https://github.com/tvq","followers_url":"https://api.github.com/users/tvq/followers","following_url":"https://api.github.com/users/tvq/following{/other_user}","gists_url":"https://api.github.com/users/tvq/gists{/gist_id}","starred_url":"https://api.github.com/users/tvq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tvq/subscriptions","organizations_url":"https://api.github.com/users/tvq/orgs","repos_url":"https://api.github.com/users/tvq/repos","events_url":"https://api.github.com/users/tvq/events{/privacy}","received_events_url":"https://api.github.com/users/tvq/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-03-22T15:44:36Z","updated_at":"2017-03-22T16:08:32Z","closed_at":null,"body":"When I am trying to use node [crypto example](https://nodejs.org/api/crypto.html#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback) with koa2 context behave like it would be lost\r\n\r\n```javascript\r\nrouter.get('/password', (ctx, next) => {\r\n\r\n  const crypto = require('crypto');\r\n  crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, key) => {\r\n    if (err) throw err\r\n\r\n    const hex = key.toString('hex')\r\n\r\n    ctx.status = 200\r\n    ctx.body = hex\r\n\r\n    console.log(hex)  // '3745e48...aa39b34'\r\n  })\r\n})\r\n```\r\n\r\nI have got error:\r\n```\r\nassert.js:81\r\n throw new assert.AssertionError({\r\n ^\r\n AssertionError: headers have already been sent\r\n at Object.set status [as status] (/code/node_modules/koa/lib/response.js:82:5)\r\n at Object.status (/code/node_modules/delegates/index.js:92:31)\r\n at InternalFieldObject.crypto.pbkdf2 [as ondone] (/code/code/routes.js:13:24)\r\n```\r\n\r\nIf I wont set `ctx.status = 200` inside `pbkdf2` then I got 404 Not Found default response\r\n\r\nAny idea of this behavior?\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/koajs/koa/issues/comments/288450202","html_url":"https://github.com/koajs/koa/issues/948#issuecomment-288450202","issue_url":"https://api.github.com/repos/koajs/koa/issues/948","id":288450202,"user":{"login":"slaskis","id":5991,"avatar_url":"https://avatars1.githubusercontent.com/u/5991?v=3","gravatar_id":"","url":"https://api.github.com/users/slaskis","html_url":"https://github.com/slaskis","followers_url":"https://api.github.com/users/slaskis/followers","following_url":"https://api.github.com/users/slaskis/following{/other_user}","gists_url":"https://api.github.com/users/slaskis/gists{/gist_id}","starred_url":"https://api.github.com/users/slaskis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/slaskis/subscriptions","organizations_url":"https://api.github.com/users/slaskis/orgs","repos_url":"https://api.github.com/users/slaskis/repos","events_url":"https://api.github.com/users/slaskis/events{/privacy}","received_events_url":"https://api.github.com/users/slaskis/received_events","type":"User","site_admin":false},"created_at":"2017-03-22T16:08:32Z","updated_at":"2017-03-22T16:08:32Z","body":"Or you should wrap the async method in a promise, then you can `await` it. \r\n\r\nMaybe something like this:\r\n\r\n```\r\nconst crypto = require('crypto');\r\n\r\nfunction generatePassword(secret, salt) {\r\n  return new Promise((resolve, reject) => {\r\n    crypto.pbkdf2(secret, salt, 100000, 512, 'sha512', (err, key) => {\r\n      if (err) {\r\n        reject(err)\r\n      } else {\r\n        resolve(key.toString('hex'))\r\n      }\r\n    })\r\n  })\r\n}\r\n\r\nrouter.get('/password', async (ctx, next) => {\r\n  const hex = await generatePassword('secret', 'salt')\r\n  ctx.status = 200\r\n  ctx.body = hex\r\n  console.log(hex)  // '3745e48...aa39b34'\r\n})\r\n```"}}
{"actor":{"display_login":"slaskis"}}