{"id":24558139,"url":"https://github.com/rse/hapi-plugin-co","last_synced_at":"2025-04-19T09:59:02.333Z","repository":{"id":56040383,"uuid":"55298648","full_name":"rse/hapi-plugin-co","owner":"rse","description":"HAPI plugin for Co-Routine handlers","archived":false,"fork":false,"pushed_at":"2023-01-16T13:59:55.000Z","size":39,"stargazers_count":7,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T21:55:47.921Z","etag":null,"topics":["co","generator","handler","hapi","plugin","promise","yield"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-02T14:24:43.000Z","updated_at":"2023-02-18T11:37:50.000Z","dependencies_parsed_at":"2023-02-10T03:46:04.684Z","dependency_job_id":null,"html_url":"https://github.com/rse/hapi-plugin-co","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fhapi-plugin-co","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fhapi-plugin-co/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fhapi-plugin-co/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fhapi-plugin-co/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rse","download_url":"https://codeload.github.com/rse/hapi-plugin-co/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249670075,"owners_count":21308670,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["co","generator","handler","hapi","plugin","promise","yield"],"created_at":"2025-01-23T05:47:28.825Z","updated_at":"2025-04-19T09:59:02.310Z","avatar_url":"https://github.com/rse.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nhapi-plugin-co\n=====================\n\n[HAPI](http://hapijs.com/) plugin for Co-Routine handlers\n\n\u003cp/\u003e\n\u003cimg src=\"https://nodei.co/npm/hapi-plugin-co.png?downloads=true\u0026stars=true\" alt=\"\"/\u003e\n\n\u003cp/\u003e\n\u003cimg src=\"https://david-dm.org/rse/hapi-plugin-co.png\" alt=\"\"/\u003e\n\nInstallation\n------------\n\n```shell\n$ npm install hapi hapi-plugin-co\n```\n\nAbout\n-----\n\nThis is a small plugin for the [HAPI](http://hapijs.com/) server\nframework for seamless support of co-routines for route handlers,\nbased on generator functions.\n\nNOTICE\n------\n\nAlthough this plugin was ported to and works just with HAPI version\n\u0026gt;= 17.0.0 and Node.js version \u0026gt; 8.0.0, you are advised to use\n*asynchronous functions* with HAPI version \u0026gt;= 17.0.0's native support\ninstead of *generator functions* and this HAPI plugin support. Generator\nfunctions and this HAPI plugin are actually from the HAPI version \u0026lt;\n17.0.0 and Node.js version \u0026lt; 8.0.0 era.\n\nUsage\n-----\n\nThe following sample server shows the plugin in action.\nIt responds with `{ value: 42 }` after waiting for a second.\n\n```js\nvar HAPI    = require(\"hapi\")\nvar HAPICo  = require(\"hapi-plugin-co\")\nvar Request = require(\"request-promise\")\n\nvar server = new HAPI.Server({ debug: { request: [ \"error\" ] } })\nserver.connection({ address: \"127.0.0.1\", port: 12345 })\nserver.register(HAPICo, function () {\n\n    /*  provider  */\n    server.route({\n        method:  \"GET\",\n        path:    \"/foo\",\n        handler: function * (request, reply) {  /*  THE CO-ROUTINE  */\n            var value = yield new Promise(function (resolve, reject) {\n                setTimeout(function () {\n                    resolve(42)\n                }, 1000)\n            })\n            reply({ value: value })\n        }\n    })\n\n    server.start(function () {\n        /*  consumer  */\n        let p1 = server.inject({ method: \"GET\", url: \"/foo\" }).then(function (response) {\n            if (response.result.value === 42)\n                console.log(\"-- internal request: /foo: OK\")\n            else\n                console.log(\"-- internal request: /foo: ERROR: invalid response: \", response.result.value)\n        }).catch(function (error) {\n            console.log(\"-- internal request: /foo: ERROR: \", error)\n        })\n        let p2 = Request({ uri: \"http://127.0.0.1:12345/foo\", json: true }).then(function (response) {\n            if (response.value === 42)\n                console.log(\"-- external request: /foo: OK\")\n            else\n                console.log(\"-- external request: /foo: ERROR: invalid response: \", response.value)\n        }).catch(function (error) {\n            console.log(\"-- external request: /foo: ERROR: \", error)\n        })\n        Promise.all([ p1, p2 ]).then(function () {\n            server.stop({ timeout: 1000 }, function (error) {\n                if (error)\n                    console.log(\"ERROR\", error)\n                process.exit(0)\n            })\n        })\n    })\n\n})\n```\n\nNotice\n------\n\nThe smaller alternative: With\n[`hapi-async-handler`](https://github.com/ide/hapi-async-handler)\nthere is an alternative HAPI plugin with similar functionality. The\ndifference is that `hapi-async-handler` uses the official HAPI `handler`\nfunctionality to provide its functionality (and this way unfortunately\ncauses more boilerplate code for the application then wished), does not\nrecognize `Boom` error responses, requires an ES7 `async function ()`\n(and not just an ES6 generator function `function * ()`) and especially\ndoes not use `co` internally (and this way cannot easily support the\nyielding of regular values, promises, sunks, etc).\n\nThe larger alternative: With\n[`hapi-co`](https://github.com/bandwidthcom/co-hapi) there is\nanother alternative HAPI plugin with a much larger functionality. It\nallows the use of `co`-powered generator functions for all types of\ncallbacks, including handlers. If you really need all this additional\nfunctionality, go with this module instead.\n\nLicense\n-------\n\nCopyright (c) 2016-2023 Dr. Ralf S. Engelschall (http://engelschall.com/)\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fhapi-plugin-co","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frse%2Fhapi-plugin-co","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fhapi-plugin-co/lists"}