Skip to content

Commit

Permalink
Remove request from response, kill timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jan 25, 2017
1 parent ee9a399 commit cb55353
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 51 deletions.
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import { Common } from 'servie'

#### Properties

* `events` The request/response event emitter
* `headers` The submitted headers as a `Headers` instance
* `trailers` The submitted trailers as a `Headers` instance
* `body` The submitted body payload
* `bodyUsed` A boolean indicating where the body was read
* `events` An event emitter for listening to the request and response lifecycles
* `headers` The headers as a `Headers` instance
* `trailers` The trailers as a `Headers` instance
* `body` The request or response payload
* `bodyUsed` A boolean indicating whether the body has been read
* `type` A shorthand property for reading and writing the `Content-Type` header
* `length` A shorthand property for reading and writing `Content-Length` as a number
* `started` Boolean indicating if a request/response has started
Expand All @@ -55,16 +55,14 @@ import { Common } from 'servie'
* `buffer(maxBufferSize): Promise<Buffer>` Read the body into a `Buffer` object
* `text(maxBufferSize): Promise<string>` Read the body as a `string`
* `stream(): Readable` Read the body as a `Readable` stream
* `setTimeout(ms): void` Set a timeout on the request or response to be marked as finished
* `clearTimeout(ms): void` Clear a previous timeout

#### Events

* `headers` Emitted when the `headers` object becomes available
* `trailers` Emitted when the `trailers` object becomes available
* `started` Emitted when the request/response has started
* `finished` Emitted when the request/respone has finished
* `progress` Emitted when the `bytesTransferred` properties is incremented
* `headers` Emitted when the `headers` object is available
* `trailers` Emitted when the `trailers` object is available
* `started` Emitted when `started === true`
* `finished` Emitted when `finished === true`
* `progress` Emitted when `bytesTransferred` is incremented

### `Request`

Expand Down Expand Up @@ -104,7 +102,8 @@ const request = new Request({
#### Events

* `abort` Emitted when the request is aborted and MUST be handled by transport
* `error` Emitted when an out-of-band error occurs (e.g. abort or timeout) and MUST be handled by the transport
* `error` Emitted when an out-of-band error occurs (e.g. abort) and MUST be handled by the transport
* `response` Emitted when the response object is being handled

### `Response`

Expand All @@ -117,7 +116,7 @@ import { Response } from 'servie'
#### Options

```ts
const response = new Response(request, {})
const response = new Response({})
```

> Extends `Common` options.
Expand Down Expand Up @@ -164,6 +163,15 @@ Take a single parameter with the headers in object, array or `Headers` format.
* `message` Standard error message (`string`)
* `cause` Specified when the HTTP error was triggered by an underlying error

## Implementers

If you're building the transports for Servie, there are some life cycle events you need to be aware of and emit yourself:

1. Listen to the `error` event on `Request` for out-of-band errors and respond accordingly (e.g. app-level logging)
2. Listen to the `abort` event on `Request` to destroy the HTTP request
3. Set `started === true` and `finished === true` on `Request` and `Response`, as appropriate
4. Set `bytesTransferred` on `Request` and `Response` when monitoring HTTP transfer progress

## JavaScript

This module is designed for ES5 environments, but also requires `Promise` to be available.
Expand Down
38 changes: 1 addition & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ export interface CommonOptions {
export class Common implements CommonOptions {
events: EventEmitter

private _timeout: any
private _body: Body
private _bodyUsed = true
private _headers: Headers
Expand All @@ -244,7 +243,6 @@ export class Common implements CommonOptions {
if (!this._finished && finished) {
this._finished = true
this.events.emit('finished')
this.clearTimeout()
}
}

Expand Down Expand Up @@ -367,16 +365,6 @@ export class Common implements CommonOptions {
this.headers.set('Content-Length', length)
}

_setTimeout (cb: () => void, ms: number) {
clearTimeout(this._timeout)
this._timeout = setTimeout(cb, ms)
}

clearTimeout () {
clearTimeout(this._timeout)
this._timeout = undefined
}

buffer (maxBufferSize: number = 1000 * 1000): Promise<Buffer | undefined> {
if (this.bodyUsed) {
return Promise.reject(new TypeError('Already read'))
Expand Down Expand Up @@ -553,22 +541,11 @@ export class Request extends Common implements RequestOptions {
if (abort) {
this.aborted = true
this.events.emit('abort', this.error('Request aborted', 'EABORT', 444))
this.clearTimeout()
}

return abort
}

setTimeout (ms: number) {
return this._setTimeout(
() => {
this.events.emit('error', this.error('Request timeout', 'ETIMEOUT', 408))
this.abort()
},
ms
)
}

toJSON () {
return {
url: this.url,
Expand Down Expand Up @@ -602,24 +579,11 @@ export class Response extends Common implements ResponseOptions {
status: number | undefined
statusText: string | undefined

constructor (public request: Request, options: ResponseOptions = {}) {
constructor (options: ResponseOptions = {}) {
super(options)

this.status = options.status
this.statusText = options.statusText

// Emit a response event to listeners.
this.request.events.emit('response', this)
}

setTimeout (ms: number) {
return this._setTimeout(
() => {
this.request.events.emit('error', this.request.error('Response timeout', 'ETIMEOUT', 408))
this.request.abort()
},
ms
)
}

toJSON () {
Expand Down

0 comments on commit cb55353

Please sign in to comment.