root / HServer / 00.Server / 00.Program / node_modules / destroy / index.js
이력 | 보기 | 이력해설 | 다운로드 (1.02 KB)
1 | 39 | HKM | /*!
|
---|---|---|---|
2 | * destroy
|
||
3 | * Copyright(c) 2014 Jonathan Ong
|
||
4 | * MIT Licensed
|
||
5 | */
|
||
6 | |||
7 | 'use strict'
|
||
8 | |||
9 | /**
|
||
10 | * Module dependencies.
|
||
11 | * @private
|
||
12 | */
|
||
13 | |||
14 | var ReadStream = require('fs').ReadStream |
||
15 | var Stream = require('stream') |
||
16 | |||
17 | /**
|
||
18 | * Module exports.
|
||
19 | * @public
|
||
20 | */
|
||
21 | |||
22 | module.exports = destroy |
||
23 | |||
24 | /**
|
||
25 | * Destroy a stream.
|
||
26 | *
|
||
27 | * @param {object} stream
|
||
28 | * @public
|
||
29 | */
|
||
30 | |||
31 | function destroy(stream) { |
||
32 | if (stream instanceof ReadStream) { |
||
33 | return destroyReadStream(stream)
|
||
34 | } |
||
35 | |||
36 | if (!(stream instanceof Stream)) { |
||
37 | return stream
|
||
38 | } |
||
39 | |||
40 | if (typeof stream.destroy === 'function') { |
||
41 | stream.destroy() |
||
42 | } |
||
43 | |||
44 | return stream
|
||
45 | } |
||
46 | |||
47 | /**
|
||
48 | * Destroy a ReadStream.
|
||
49 | *
|
||
50 | * @param {object} stream
|
||
51 | * @private
|
||
52 | */
|
||
53 | |||
54 | function destroyReadStream(stream) { |
||
55 | stream.destroy() |
||
56 | |||
57 | if (typeof stream.close === 'function') { |
||
58 | // node.js core bug work-around
|
||
59 | stream.on('open', onOpenClose)
|
||
60 | } |
||
61 | |||
62 | return stream
|
||
63 | } |
||
64 | |||
65 | /**
|
||
66 | * On open handler to close stream.
|
||
67 | * @private
|
||
68 | */
|
||
69 | |||
70 | function onOpenClose() { |
||
71 | if (typeof this.fd === 'number') { |
||
72 | // actually close down the fd
|
||
73 | this.close()
|
||
74 | } |
||
75 | } |