diff --git a/API.md b/API.md index 432f723..854ca32 100644 --- a/API.md +++ b/API.md @@ -1,14 +1,20 @@ # API +Mount your express app onto your hapi server, aw heck! + +> **Note** +> +> Hecks is intended for use with hapi v19+ and nodejs v12+ (see v2 for lower support). + ## `Hecks` ### The hapi plugin -You should register Hecks in any plugin that would like to take advantage of its features; it does not take any options. Hecks specifies the `once` [plugin attribute](https://github.com/hapijs/hapi/blob/master/API.md#plugins), which means hapi will ensure it is not registered multiple times to the same connection. +You should register Hecks in any plugin that would like to take advantage of its features; it does not take any options. Hecks specifies the `once` [plugin attribute](https://hapi.dev/api/#plugins), which means hapi will ensure it is not registered multiple times to the same connection. #### `express` handler type The `express` handler type mounts an [express application](http://expressjs.com/en/4x/api.html#app) to a route. Its configuration may be either an express application or an object, - `app` - an express application. - `express` - (optional) the express module used to create `app`. In the absence of this configuration option express is simply `require('express')`'d as a peer dependency. -The route will automatically have the following [route configuration](https://github.com/hapijs/hapi/blob/master/API.md#route-options) defaults, in particular to avoid reading the request payload before express and to avoid parsing cookies aimed at the express application. +The route will automatically have the following [route configuration](https://hapi.dev/api/#route-options) defaults, in particular to avoid reading the request payload before express and to avoid parsing cookies aimed at the express application. ```json5 { payload: { @@ -26,7 +32,7 @@ The route's path has some say in determining the url passed-along to the express - The route's path has a parameter named `expressPath`, e.g. `/my-app/{expressPath*}`. In this case, the url handed to the express app will have the path contained in `request.params.expressPath`. - The route's path _does not_ have a parameter named `expressPath`, e.g. `/dogs/{id}`. In this case, the url handed to the express app will be the entire path matched by the route. -In both cases, any route prefixes passed during [plugin registration](https://github.com/hapijs/hapi/blob/master/API.md#server.register()) will be hidden from the express app. Additionally, any calls to [`request.setUrl()`](https://github.com/hapijs/hapi/blob/master/API.md#request.setUrl()) will be respected by the application. +In both cases, any route prefixes passed during [plugin registration](https://hapi.dev/api/#server.register()) will be hidden from the express app. Additionally, any calls to [`request.setUrl()`](https://hapi.dev/api/#request.setUrl()) will be respected by the application. ```js // Serving an express app mounted at /old-api and secured behind hapi auth diff --git a/LICENSE b/LICENSE index ba09e52..8eefd20 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2017-2020 Devin Ivy and project contributors +Copyright (c) 2017-2021 Devin Ivy and project contributors 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 diff --git a/README.md b/README.md index 9be4c18..d61717c 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # hecks Mount your express app onto your hapi server, aw heck! -[![Build Status](https://travis-ci.org/hapipal/hecks.svg?branch=master)](https://travis-ci.org/hapipal/hecks) [![Coverage Status](https://coveralls.io/repos/hapipal/hecks/badge.svg?branch=master&service=github)](https://coveralls.io/github/hapipal/hecks?branch=master) +[![Build Status](https://travis-ci.com/hapipal/hecks.svg?branch=master)](https://travis-ci.com/hapipal/hecks) [![Coverage Status](https://coveralls.io/repos/hapipal/hecks/badge.svg?branch=master&service=github)](https://coveralls.io/github/hapipal/hecks?branch=master) Lead Maintainer - [Devin Ivy](https://github.com/devinivy) ## Usage > See also the [API Reference](API.md) +> +> Hecks is intended for use with hapi v19+ and nodejs v12+ (see v2 for lower support). Hecks allows you to seamlessly incorporate express applications into a **hapi v17+** server. This is particularly useful for testing an express server using [`server.inject()`](https://github.com/hapijs/hapi/blob/master/API.md#server.inject()), for unifying deployment of existing express and hapi applications, and as an initial stepping stone in migrating an express application to hapi. @@ -14,8 +16,7 @@ Hecks allows you to seamlessly incorporate express applications into a **hapi v1 const Express = require('express'); const BodyParser = require('body-parser'); const Hapi = require('@hapi/hapi'); -const Hoek = require('@hapi/hoek'); -const Hecks = require('hecks'); +const Hecks = require('@hapipal/hecks'); (async () => { @@ -23,7 +24,7 @@ const Hecks = require('hecks'); app.post('/user', BodyParser.json(), (req, res) => { - const user = Hoek.shallow(req.body); + const user = { ...req.body }; user.saved = true; res.json(user); diff --git a/lib/index.js b/lib/index.js index a84fd00..1b2bc1d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,7 @@ 'use strict'; const Bounce = require('@hapi/bounce'); -const Toys = require('toys'); +const Toys = require('@hapipal/toys'); const Package = require('../package.json'); const internals = {}; @@ -9,6 +9,9 @@ const internals = {}; exports.plugin = { pkg: Package, once: true, + requirements: { + hapi: '>=19' + }, register(server) { const express = internals.express.bind(); // clone handler definition @@ -67,8 +70,8 @@ internals.express = (route, options) => { const { req, res } = request.raw; - req._hecks = { request }; - res._hecks = {}; + req[internals.kHecks] = { request }; + res[internals.kHecks] = {}; // Stash req/res methods potentially used by shot, see hapijs/shot#82 internals.stashForShot(req, res); @@ -100,7 +103,7 @@ internals.routeDefaults = { internals.expressPathMiddleware = (req, res, next) => { - const { request } = req._hecks; + const { request } = req[internals.kHecks]; const expressPath = request.params.expressPath || ''; const prefix = request.route.realm.modifiers.route.prefix || ''; const search = request.url.search || ''; @@ -112,24 +115,26 @@ internals.expressPathMiddleware = (req, res, next) => { internals.restoreForShotMiddleware = (req, res, next) => { - req._read = req._hecks._read; - req.destroy = req._hecks.destroy; + req._read = req[internals.kHecks]._read; + req.destroy = req[internals.kHecks].destroy; - res.write = res._hecks.write; - res.end = res._hecks.end; - res.writeHead = res._hecks.writeHead; - res.destroy = res._hecks.destroy; + res.write = res[internals.kHecks].write; + res.end = res[internals.kHecks].end; + res.writeHead = res[internals.kHecks].writeHead; + res.destroy = res[internals.kHecks].destroy; next(); }; internals.stashForShot = (req, res) => { - req._hecks._read = req._read; - req._hecks.destroy = req.destroy; + req[internals.kHecks]._read = req._read; + req[internals.kHecks].destroy = req.destroy; - res._hecks.write = res.write; - res._hecks.end = res.end; - res._hecks.writeHead = res.writeHead; - res._hecks.destroy = res.destroy; + res[internals.kHecks].write = res.write; + res[internals.kHecks].end = res.end; + res[internals.kHecks].writeHead = res.writeHead; + res[internals.kHecks].destroy = res.destroy; }; + +internals.kHecks = Symbol('hecks'); diff --git a/package.json b/package.json index a54fd7e..da6618b 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "hecks", + "name": "@hapipal/hecks", "version": "2.3.0", "description": "Mount your express app onto your hapi server, aw heck!", "main": "lib/index.js", @@ -7,7 +7,7 @@ "test": "test" }, "scripts": { - "test": "lab -a @hapi/code -t 100 -L -I \"FinalizationRegistry,WeakRef\"", + "test": "lab -a @hapi/code -t 100 -L", "coveralls": "lab -r lcov | coveralls" }, "repository": { @@ -28,7 +28,7 @@ "homepage": "https://github.com/hapipal/hecks#readme", "dependencies": { "@hapi/bounce": "2.x.x", - "toys": "2.x.x" + "@hapipal/toys": "3.x.x" }, "peerDependencies": { "@hapi/hapi": ">=19 <21", diff --git a/test/index.js b/test/index.js index ddef07a..bfbba7e 100644 --- a/test/index.js +++ b/test/index.js @@ -6,10 +6,10 @@ const Stream = require('stream'); const Lab = require('@hapi/lab'); const Code = require('@hapi/code'); +const Hapi = require('@hapi/hapi'); const Express = require('express'); const BodyParser = require('body-parser'); const Hecks = require('..'); -const Hapi = require('@hapi/hapi'); // Test shortcuts