Last change
on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | /*!
|
---|
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 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.