Skip to content

Commit

Permalink
Move to @hapipal npm scope. Consistency in docs. Use symbol for stash.
Browse files Browse the repository at this point in the history
…Closes #10
  • Loading branch information
devinivy committed Jan 5, 2021
1 parent d73de03 commit ad418f3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
12 changes: 9 additions & 3 deletions 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: {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
9 changes: 5 additions & 4 deletions README.md
@@ -1,29 +1,30 @@
# 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.

```js
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 () => {

const app = Express();

app.post('/user', BodyParser.json(), (req, res) => {

const user = Hoek.shallow(req.body);
const user = { ...req.body };
user.saved = true;

res.json(user);
Expand Down
37 changes: 21 additions & 16 deletions lib/index.js
@@ -1,14 +1,17 @@
'use strict';

const Bounce = require('@hapi/bounce');
const Toys = require('toys');
const Toys = require('@hapipal/toys');
const Package = require('../package.json');

const internals = {};

exports.plugin = {
pkg: Package,
once: true,
requirements: {
hapi: '>=19'
},
register(server) {

const express = internals.express.bind(); // clone handler definition
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 || '';
Expand All @@ -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');
6 changes: 3 additions & 3 deletions package.json
@@ -1,13 +1,13 @@
{
"name": "hecks",
"name": "@hapipal/hecks",
"version": "2.3.0",
"description": "Mount your express app onto your hapi server, aw heck!",
"main": "lib/index.js",
"directories": {
"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": {
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Expand Up @@ -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

Expand Down

0 comments on commit ad418f3

Please sign in to comment.