1
- /**
2
- * node-compress-commons
3
- *
4
- * Copyright (c) 2014 Chris Talkington, contributors.
5
- * Licensed under the MIT license.
6
- * https://github.com/archiverjs/node-compress-commons/blob/master/LICENSE-MIT
7
- */
8
- var inherits = require ( 'util' ) . inherits ;
9
- var isStream = require ( 'is-stream' ) ;
10
- var Transform = require ( 'readable-stream' ) . Transform ;
11
-
12
- var ArchiveEntry = require ( './archive-entry' ) ;
13
- var util = require ( '../util' ) ;
14
-
15
- var ArchiveOutputStream = module . exports = function ( options ) {
16
- if ( ! ( this instanceof ArchiveOutputStream ) ) {
17
- return new ArchiveOutputStream ( options ) ;
1
+ import { inherits } from "util" ;
2
+ import { isStream } from "is-stream" ;
3
+ import { Transform } from "readable-stream" ;
4
+ import ArchiveEntry from "./archive-entry.js" ;
5
+ import { normalizeInputSource } from "../util/index.js" ;
6
+
7
+ export default class ArchiveOutputStream extends Transform {
8
+ constructor ( options ) {
9
+ super ( options ) ;
10
+
11
+ this . offset = 0 ;
12
+ this . _archive = {
13
+ finish : false ,
14
+ finished : false ,
15
+ processing : false ,
16
+ } ;
18
17
}
19
18
20
- Transform . call ( this , options ) ;
21
-
22
- this . offset = 0 ;
23
- this . _archive = {
24
- finish : false ,
25
- finished : false ,
26
- processing : false
27
- } ;
28
- } ;
29
-
30
- inherits ( ArchiveOutputStream , Transform ) ;
31
-
32
- ArchiveOutputStream . prototype . _appendBuffer = function ( zae , source , callback ) {
33
- // scaffold only
34
- } ;
35
-
36
- ArchiveOutputStream . prototype . _appendStream = function ( zae , source , callback ) {
37
- // scaffold only
38
- } ;
39
-
40
- ArchiveOutputStream . prototype . _emitErrorCallback = function ( err ) {
41
- if ( err ) {
42
- this . emit ( 'error' , err ) ;
19
+ _appendBuffer ( zae , source , callback ) {
20
+ // scaffold only
43
21
}
44
- } ;
45
-
46
- ArchiveOutputStream . prototype . _finish = function ( ae ) {
47
- // scaffold only
48
- } ;
49
22
50
- ArchiveOutputStream . prototype . _normalizeEntry = function ( ae ) {
51
- // scaffold only
52
- } ;
53
-
54
- ArchiveOutputStream . prototype . _transform = function ( chunk , encoding , callback ) {
55
- callback ( null , chunk ) ;
56
- } ;
23
+ _appendStream ( zae , source , callback ) {
24
+ // scaffold only
25
+ }
57
26
58
- ArchiveOutputStream . prototype . entry = function ( ae , source , callback ) {
59
- source = source || null ;
27
+ _emitErrorCallback = function ( err ) {
28
+ if ( err ) {
29
+ this . emit ( "error" , err ) ;
30
+ }
31
+ } ;
60
32
61
- if ( typeof callback !== 'function' ) {
62
- callback = this . _emitErrorCallback . bind ( this ) ;
33
+ _finish ( ae ) {
34
+ // scaffold only
63
35
}
64
36
65
- if ( ! ( ae instanceof ArchiveEntry ) ) {
66
- callback ( new Error ( 'not a valid instance of ArchiveEntry' ) ) ;
67
- return ;
37
+ _normalizeEntry ( ae ) {
38
+ // scaffold only
68
39
}
69
40
70
- if ( this . _archive . finish || this . _archive . finished ) {
71
- callback ( new Error ( 'unacceptable entry after finish' ) ) ;
72
- return ;
41
+ _transform ( chunk , encoding , callback ) {
42
+ callback ( null , chunk ) ;
73
43
}
74
44
75
- if ( this . _archive . processing ) {
76
- callback ( new Error ( 'already processing an entry' ) ) ;
77
- return ;
45
+ entry ( ae , source , callback ) {
46
+ source = source || null ;
47
+ if ( typeof callback !== "function" ) {
48
+ callback = this . _emitErrorCallback . bind ( this ) ;
49
+ }
50
+ if ( ! ( ae instanceof ArchiveEntry ) ) {
51
+ callback ( new Error ( "not a valid instance of ArchiveEntry" ) ) ;
52
+ return ;
53
+ }
54
+ if ( this . _archive . finish || this . _archive . finished ) {
55
+ callback ( new Error ( "unacceptable entry after finish" ) ) ;
56
+ return ;
57
+ }
58
+ if ( this . _archive . processing ) {
59
+ callback ( new Error ( "already processing an entry" ) ) ;
60
+ return ;
61
+ }
62
+ this . _archive . processing = true ;
63
+ this . _normalizeEntry ( ae ) ;
64
+ this . _entry = ae ;
65
+ source = normalizeInputSource ( source ) ;
66
+ if ( Buffer . isBuffer ( source ) ) {
67
+ this . _appendBuffer ( ae , source , callback ) ;
68
+ } else if ( isStream ( source ) ) {
69
+ this . _appendStream ( ae , source , callback ) ;
70
+ } else {
71
+ this . _archive . processing = false ;
72
+ callback (
73
+ new Error ( "input source must be valid Stream or Buffer instance" ) ,
74
+ ) ;
75
+ return ;
76
+ }
77
+ return this ;
78
78
}
79
79
80
- this . _archive . processing = true ;
81
- this . _normalizeEntry ( ae ) ;
82
- this . _entry = ae ;
83
-
84
- source = util . normalizeInputSource ( source ) ;
85
-
86
- if ( Buffer . isBuffer ( source ) ) {
87
- this . _appendBuffer ( ae , source , callback ) ;
88
- } else if ( isStream ( source ) ) {
89
- this . _appendStream ( ae , source , callback ) ;
90
- } else {
91
- this . _archive . processing = false ;
92
- callback ( new Error ( 'input source must be valid Stream or Buffer instance' ) ) ;
93
- return ;
80
+ finish ( ) {
81
+ if ( this . _archive . processing ) {
82
+ this . _archive . finish = true ;
83
+ return ;
84
+ }
85
+ this . _finish ( ) ;
94
86
}
95
87
96
- return this ;
97
- } ;
98
-
99
- ArchiveOutputStream . prototype . finish = function ( ) {
100
- if ( this . _archive . processing ) {
101
- this . _archive . finish = true ;
102
- return ;
88
+ getBytesWritten ( ) {
89
+ return this . offset ;
103
90
}
104
91
105
- this . _finish ( ) ;
106
- } ;
107
-
108
- ArchiveOutputStream . prototype . getBytesWritten = function ( ) {
109
- return this . offset ;
110
- } ;
111
-
112
- ArchiveOutputStream . prototype . write = function ( chunk , cb ) {
113
- if ( chunk ) {
114
- this . offset += chunk . length ;
92
+ write ( chunk , cb ) {
93
+ if ( chunk ) {
94
+ this . offset += chunk . length ;
95
+ }
96
+ return super . write ( chunk , cb ) ;
115
97
}
116
-
117
- return Transform . prototype . write . call ( this , chunk , cb ) ;
118
- } ;
98
+ }
0 commit comments