@@ -22,7 +22,6 @@ var nano = require('nano')({
22
22
"requestDefaults" : { "agent" : myagent }
23
23
}
24
24
) ;
25
- var fs = require ( 'fs' )
26
25
27
26
function NanoError ( code ) {
28
27
var err = Error ( code )
@@ -120,24 +119,45 @@ module.exports.read_stream = function (name, stream, callback) {
120
119
return stream ;
121
120
}
122
121
122
+
123
123
module . exports . write_stream = function ( name ) {
124
-
125
- var stream = MyStreams . UploadTarballStream ( )
124
+ var stream = MyStreams . UploadTarballStream ( )
125
+
126
+ var _ended = 0
127
+ stream . on ( 'end' , function ( ) {
128
+ _ended = 1
129
+ } )
126
130
127
- fs . exists ( name , function ( exists ) {
128
- if ( exists ) return stream . emit ( 'error' , FSError ( 'EEXISTS' ) )
131
+ var self = this ;
132
+ self . document_exists ( name , function ( err , headers ) {
133
+ var opened = false
129
134
var sinopia = nano . db . use ( 'sinopia' ) ;
130
-
131
- stream . emit ( 'open' )
132
135
stream . pipe (
133
- sinopia . attachment . insert ( name , name , null , 'application/x-compressed' , { "docType" : "tarball" } )
136
+ sinopia . attachment . insert ( name , name , null , 'application/x-compressed' , { "docType" : "tarball" } )
134
137
)
135
-
136
- stream . emit ( 'success' )
137
- } ) ;
138
- return stream
138
+ stream . done = function ( ) {
139
+ function onend ( ) {
140
+ stream . emit ( 'success' )
141
+ }
142
+ if ( _ended ) {
143
+ onend ( )
144
+ } else {
145
+ stream . on ( 'end' , onend )
146
+ }
147
+ }
148
+ stream . abort = function ( ) {
149
+ if ( opened ) {
150
+ opened = false
151
+ }
152
+ }
153
+ opened = true
154
+ // re-emitting open because it's handled in storage.js
155
+ stream . emit ( 'open' )
156
+ } ) ;
157
+ return stream
139
158
}
140
159
160
+
141
161
module . exports . delete_stream = function ( name , callback ) {
142
162
var sinopia = nano . db . use ( 'sinopia' ) ;
143
163
sinopia . get ( name , name , function ( err , body ) {
0 commit comments