Skip to content

Commit

Permalink
Move abort back, allow abort after "finish"
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed May 22, 2018
1 parent 0e78d29 commit 96ea9ab
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ Create a `Body` instance from raw data (e.g. `Readable | ReadableStream | Buffer
If you're building the transports for Servie, there are some life cycle events you need to be aware of:

1. Listen to the `error` event on `Request` and `Response` for errors
2. Listen to the `abort` event on `Request` and `Response` to destroy the connection
3. Resolve `trailer` promise and append to HTTP request
4. Emit a `response` event (with `Response` provided) on `Request` when server responds
5. Set `started = true` and `finished = true` on `Request` and `Response` (as appropriate)
6. Set `bytesTransferred` on `Request` and `Response` when monitoring HTTP transfer progress
2. Listen to the `abort` event on `Request` to destroy the connection and, when destroyed, set `req.aborted = true`
3. Resolve `trailer` promise and append to HTTP request or response
4. Set `started = true` and `finished = true` on `Request` and `Response` (as appropriate)
5. Set `bytesTransferred` on `Request` and `Response` with transfer progress
6. Set `req.closed = true` when the connection has finished

## JavaScript

Expand Down
17 changes: 3 additions & 14 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export interface ServieOptions {
trailer?: Headers | Promise<Headers>
}

/**
* @internal
*/
export const kBytesTransferred = Symbol('bytesTransferred')

export abstract class Servie implements ServieOptions {
Expand Down Expand Up @@ -63,20 +66,6 @@ export abstract class Servie implements ServieOptions {
}
}

get aborted () { return false }

abort () {
const shouldAbort = !this.aborted && !this.finished

if (shouldAbort) {
this.events.emit('abort')
Object.defineProperty(this, 'aborted', { value: true })
this.events.emit('aborted')
}

return shouldAbort
}

abstract clone (): Servie

}
7 changes: 7 additions & 0 deletions src/body/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ export interface BodyCommonOptions <T> {
bodyUsed?: boolean
}

/**
* @internal
*/
export const kRawBody = Symbol('rawBody')

/**
* @internal
*/
export const kBodyUsed = Symbol('bodyUsed')

export abstract class BodyCommon <T = any> implements BodyCommonOptions<T> {
Expand Down
7 changes: 7 additions & 0 deletions src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ export interface HeadersValuesObject {
[key: string]: HeaderValues
}

/**
* @internal
*/
export const kHeaderList = Symbol('headerList')

/**
* @internal
*/
export const kHeaderNames = Symbol('headerNames')

/**
Expand Down
43 changes: 43 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@ export interface RequestOptions extends ServieOptions {
connection?: Connection
}

/**
* @internal
*/
export const kUrl = Symbol('url')

/**
* @internal
*/
export const kUrlObject = Symbol('urlObject')

/**
* @internal
*/
export const kAborted = Symbol('aborted')

/**
* The HTTP request class.
*/
Expand All @@ -33,6 +45,7 @@ export class Request extends Servie implements RequestOptions {

protected [kUrl]: string
protected [kUrlObject]?: Url
protected [kAborted]?: boolean

constructor (options: RequestOptions) {
super(options)
Expand Down Expand Up @@ -66,6 +79,35 @@ export class Request extends Servie implements RequestOptions {
}
}

get closed () { return false }

set closed (value: boolean) {
if (value) {
Object.defineProperty(this, 'closed', { value })
this.events.emit('closed')
}
}

get aborted () { return false }

set aborted (value: boolean) {
if (value) {
this[kAborted] = true
Object.defineProperty(this, 'aborted', { value })
this.events.emit('aborted')
}
}

abort () {
if (this.closed || this[kAborted]) return false

// Block repeat "abort" events.
this[kAborted] = true
this.events.emit('abort')

return true
}

toJSON () {
return {
url: this.url,
Expand All @@ -81,6 +123,7 @@ export class Request extends Servie implements RequestOptions {

clone () {
if (this.started) throw new TypeError('Request already started')
if (this[kAborted]) throw new TypeError('Request has been aborted')

return new Request({
url: this.url,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"outDir": "dist",
"module": "commonjs",
"strict": true,
"stripInternal": true,
"declaration": true,
"removeComments": false,
"sourceMap": true,
Expand Down

0 comments on commit 96ea9ab

Please sign in to comment.