1.1.5 • Published 2 years ago

hapi-plugin-co v1.1.5

Weekly downloads
2
License
MIT
Repository
github
Last release
2 years ago

hapi-plugin-co

HAPI plugin for Co-Routine handlers

Installation

$ npm install hapi hapi-plugin-co

About

This is a small plugin for the HAPI server framework for seamless support of co-routines for route handlers, based on generator functions.

NOTICE

Although this plugin was ported to and works just with HAPI version >= 17.0.0 and Node.js version > 8.0.0, you are advised to use asynchronous functions with HAPI version >= 17.0.0's native support instead of generator functions and this HAPI plugin support. Generator functions and this HAPI plugin are actually from the HAPI version < 17.0.0 and Node.js version < 8.0.0 era.

Usage

The following sample server shows the plugin in action. It responds with { value: 42 } after waiting for a second.

var HAPI    = require("hapi")
var HAPICo  = require("hapi-plugin-co")
var Request = require("request-promise")

var server = new HAPI.Server({ debug: { request: [ "error" ] } })
server.connection({ address: "127.0.0.1", port: 12345 })
server.register(HAPICo, function () {

    /*  provider  */
    server.route({
        method:  "GET",
        path:    "/foo",
        handler: function * (request, reply) {  /*  THE CO-ROUTINE  */
            var value = yield new Promise(function (resolve, reject) {
                setTimeout(function () {
                    resolve(42)
                }, 1000)
            })
            reply({ value: value })
        }
    })

    server.start(function () {
        /*  consumer  */
        let p1 = server.inject({ method: "GET", url: "/foo" }).then(function (response) {
            if (response.result.value === 42)
                console.log("-- internal request: /foo: OK")
            else
                console.log("-- internal request: /foo: ERROR: invalid response: ", response.result.value)
        }).catch(function (error) {
            console.log("-- internal request: /foo: ERROR: ", error)
        })
        let p2 = Request({ uri: "http://127.0.0.1:12345/foo", json: true }).then(function (response) {
            if (response.value === 42)
                console.log("-- external request: /foo: OK")
            else
                console.log("-- external request: /foo: ERROR: invalid response: ", response.value)
        }).catch(function (error) {
            console.log("-- external request: /foo: ERROR: ", error)
        })
        Promise.all([ p1, p2 ]).then(function () {
            server.stop({ timeout: 1000 }, function (error) {
                if (error)
                    console.log("ERROR", error)
                process.exit(0)
            })
        })
    })

})

Notice

The smaller alternative: With hapi-async-handler there is an alternative HAPI plugin with similar functionality. The difference is that hapi-async-handler uses the official HAPI handler functionality to provide its functionality (and this way unfortunately causes more boilerplate code for the application then wished), does not recognize Boom error responses, requires an ES7 async function () (and not just an ES6 generator function function * ()) and especially does not use co internally (and this way cannot easily support the yielding of regular values, promises, sunks, etc).

The larger alternative: With hapi-co there is another alternative HAPI plugin with a much larger functionality. It allows the use of co-powered generator functions for all types of callbacks, including handlers. If you really need all this additional functionality, go with this module instead.

License

Copyright (c) 2016-2023 Dr. Ralf S. Engelschall (http://engelschall.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.1.5

2 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.9.10

7 years ago

0.9.9

7 years ago

0.9.8

7 years ago

0.9.7

8 years ago

0.9.6

8 years ago

0.9.5

8 years ago

0.9.4

8 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

8 years ago

0.9.0

9 years ago