Skip to content

Commit

Permalink
Add gzip and rewrite (#7)
Browse files Browse the repository at this point in the history
* First attempt to enable GZIP when the compression middleware is used.

* no message

* Reducing code duplication and adding onload event.

* Not convinced the onload bit is necessary. Will look into it more later.

* Adding the option to stream text.

* Update README.md

* Update README.md

* Making pipe return a function so future releases can add config.

* Update README.md

* Bumping version to 1.0.0-rc.1 to cut a new release.
  • Loading branch information
Joe Podwys committed Apr 29, 2016
1 parent 1b0f303 commit 6e1039b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 137 deletions.
65 changes: 36 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Response streaming middleware for Express 4.

**IMPMORTANT:** If you want your streamed responses to have GZIP enabled, please use the excellent [express `compression` middleware](https://www.npmjs.com/package/compression). If you use `compression` as an app-wide middleware, `express-stream` will automitically take advantage of it.

Check out the [demo app](https://express-stream-demo.herokuapp.com/).

# What is This?
Expand Down Expand Up @@ -61,7 +63,8 @@ stream.streamAfter('post-body-view');

//Add the middleware to the desired routes
app.get('/', stream.stream(), function (req, res) {
res.render('landing'); //This route will now stream
res.stream('landing'); //This route will now stream
res.close();
});
```

Expand All @@ -81,10 +84,9 @@ Because express-stream's two middleware functions patch express's res object dif
| Function | Scope | Description | Arguments |
|---|---|---|---|---|
| stream.pipe() | middleware | This middleware function is written for client-side rendering. It can be used as a loose BigPipe implementation. | N/A |
|res.stream(view, options, callback) | res | When you use `.pipe()`, this funciton is added to the `res` object. It is the same as `res.render()` except that it does not close the HTTP connection. | Same as express |
| res.pipe(output, encoding) | res | Send a string of JavaScrpit to the client | `Output`: JavaScript output as a string `Encoding`: defaults to 'UTF-8' |
| res.close(output, encoding) | res | Same as `res.pipe()`, but closes the connection when finished. | Same as `res.pipe()` |
| stream.wrapJavascript(val) | stream | Whether to wrap all `res.pipe()` and `res.close()` output with '<script>' and '</script>' | `val`: boolean stating whether to use this feature, defaults to false |
|res.stream(view, options) | res | When you use `.pipe()`, this funciton is added to the `res` object. It is the same as `res.render()` except that it does not close the HTTP connection and does not accept a callback | Same as express |
| res.streamText(output) | res | Send a string of text to the client | `Output`: Text output as a string |
| res.close() | res | Closes the connection when finished. | |

# stream.stream()

Expand All @@ -98,7 +100,7 @@ Set an app-wide options object to be merged with the `options` param passed to a

* options: type: object, default: {}

## .streamBefore(view, options, callback)
## .streamBefore(view, options)

> **_App-wide API Call_**
Expand All @@ -110,7 +112,6 @@ If `view` is an array, all other passed params will be ignored.

* view: type: string || array of strings || array of objects
* options: same as express's `options` param
* callback: same as express's `callback` param

#### Examples

Expand All @@ -133,7 +134,7 @@ var globalHeadList = [
stream.streamBefore(globalHeadList);
```

## .streamAfter(view, options, callback)
## .streamAfter(view, options)

> **_App-wide API Call_**
Expand All @@ -145,7 +146,6 @@ If `view` is an array, all other passed params will be ignored.

* view: type: string || array of strings || array of objects
* options: same as express's `options` param
* callback: same as express's `callback` param

#### Examples

Expand All @@ -168,41 +168,38 @@ var globalHeadList = [
stream.streamAfter(globalHeadList);
```

## .openHtmlOpenHead(view, options, callback)
## .openHtmlOpenHead(view, options)

> **_App-wide API Call_**
If `view` is `true`, this will simply stream a `<!doctype html><html><head>` string to the client. If `view` is a string, this will stream the associated view with optional `options` and `callback`.
If `view` is `true`, this will simply stream a `<!doctype html><html><head>` string to the client. If `view` is a string, this will stream the associated view with optional `options`.

#### Arguments

* view: boolean or string
* options: same as express's `options` param
* callback: same as express's `callback` param

## .closeHeadOpenBody(view, options, callback)
## .closeHeadOpenBody(view, options)

> **_App-wide API Call_**
If `view` is `true`, this will simply stream a `</head><body>` string to the client. If `view` is a string, this will stream the associated view with optional `options` and `callback`.
If `view` is `true`, this will simply stream a `</head><body>` string to the client. If `view` is a string, this will stream the associated view with optional `options`.

#### Arguments

* view: boolean or string
* options: same as express's `options` param
* callback: same as express's `callback` param

## .closeBodyCloseHtml(view, options, callback)
## .closeBodyCloseHtml(view, options)

> **_App-wide API Call_**
If `view` is `true`, this will simply stream a `</body></html>` string to the client. If `view` is a string, this will stream the associated view with optional `options` and `callback`.
If `view` is `true`, this will simply stream a `</body></html>` string to the client. If `view` is a string, this will stream the associated view with optional `options`.

#### Arguments

* view: boolean or string
* options: same as express's `options` param
* callback: same as express's `callback` param

## .useAllAutoTags(val)

Expand All @@ -214,7 +211,7 @@ A convenience method to set the same boolean value for `openHtmlOpenHead`, `clos

* val: boolean or string

## .stream(headView, headOptions, headCallback)
## .stream(headView, headOptions)

> **_Middleware-only API Call_**
Expand All @@ -226,23 +223,24 @@ If `headView` is an array, all other passed params will be ignored.

* headView: type: string || array of strings || array of objects
* headOptions: same as express's `options` param
* headCallback: same as express's `callback` param

#### Examples

With `headView` as a string

```javascript
app.get('/stream-route', stream.stream('render-blocking-assets', {custom: data}), function (req, res){
res.render('stream-body');
res.stream('stream-body');
res.close();
});
```

With `headView` as an array of strings

```javascript
app.get('/stream-route', stream.stream(['blocking-one', 'blocking-two']), function (req, res){
res.render('stream-body');
res.stream('stream-body');
res.close();
});
```

Expand All @@ -254,30 +252,39 @@ var blockingList = [
{view: 'blocking-two'}
]
app.get('/stream-route', stream.stream(blockingList), function (req, res){
res.render('stream-body');
res.stream('stream-body');
res.close();
});
```

## res.render(view, options, callback)
## res.stream(view, options)

> **_Route-specific API Call_**
Compiles and streams a view, then compiles and streams the views set by `.streamAfter()`, then closes the connection.
Compiles and streams a view just like `res.render()`, but does not trigger the `.streamAfter()` array and does not close the connection.

#### Arguments

* All arguments are identical to `express`'s `res.render()` call
* All arguments are identical to `express`'s `res.render()` call expect that `callback` is missing.

## res.stream(view, options, callback)
## res.streamText(text)

> **_Route-specific API Call_**
Compiles and streams a view just like `res.render()`, but does not trigger the `.streamAfter()` array and does not close the connection.
Streams the provided text to the client.

#### Arguments

* All arguments are identical to `express`'s `res.render()` call
* All arguments are identical to `express`'s `res.render()` call excpet that `callback` is missing.

# More!

Usage examples are coming. In the mean time, see this [demo app](https://express-stream-demo.herokuapp.com/).

# Breaking Change History

#### 1.0.0

* `stream.pipe()` now only exposes `res.stream(view, data)`, `res.streamText(text)`, and `res.close()`.
* `stream.stream()`'s `res.render(view, data)` function is now `res.stream(view, data)`.
* Custom `res.*` functions no longer accept the `callback` param.
Loading

0 comments on commit 6e1039b

Please sign in to comment.