forked from gagle/node-tftp
/
client.js
168 lines (148 loc) · 3.95 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
var fs = require ("fs");
var path = require ("path");
var GetStream = require ("./streams/client/get-stream");
var PutStream = require ("./streams/client/put-stream");
var normalizeFilename = require ("./normalize-filename");
var createOptions = require ("./create-options");
var errors = require ("./protocol/errors");
var Client = module.exports = function (options){
this._options = createOptions (options);
};
Client.prototype.createGetStream = function (remote, options){
remote = normalizeFilename (remote);
return new GetStream (remote, this._options, options);
};
Client.prototype.createPutStream = function (remote, options){
remote = normalizeFilename (remote);
return new PutStream (remote, this._options, options);
};
Client.prototype.get = function (remote, local, options, cb){
remote = normalizeFilename (remote);
var argsLength = arguments.length;
if (argsLength === 2){
cb = local;
local = path.basename (remote);
}else if (argsLength === 3){
if (typeof local === "object"){
cb = options;
options = local;
local = path.basename (remote);
}else if (typeof local === "string"){
cb = options;
options = {};
}
}
var me = this;
//Check if local is a dir to prevent from starting a new request
fs.stat (local, function (error, stats){
if (error){
if (error.code !== "ENOENT") return cb (error);
}else if (stats.isDirectory ()){
return cb (new Error ("The local file is a directory"));
}
var wsError;
var open = false;
var destroy = null;
var gs = new GetStream (remote, me._options, options)
.on ("error", function (error){
if (open){
ws.on ("close", function (){
fs.unlink (local, function (){
cb (error);
});
});
ws.destroy ();
}else{
destroy = error;
}
})
.on ("abort", function (){
fs.unlink (local, function (){
cb (wsError);
});
});
var ws = fs.createWriteStream (local)
.on ("error", function (error){
wsError = error;
gs.abort (errors.EIO.message);
})
.on ("open", function (){
if (destroy){
ws.on ("close", function (){
fs.unlink (local, function (){
cb (destroy);
});
});
ws.destroy ();
}else{
open = true;
}
})
.on ("finish", function (){
cb ();
});
gs.pipe (ws);
});
};
Client.prototype.put = function (local, remote, options, cb){
var argsLength = arguments.length;
if (argsLength === 2){
cb = remote;
remote = path.basename (local);
options = {};
}else if (argsLength === 3){
if (typeof remote === "object"){
cb = options;
options = remote;
remote = path.basename (local);
}else if (typeof remote === "string"){
cb = options;
options = {};
}
}
remote = normalizeFilename (remote);
var me = this;
//Check if local is a dir or doesn't exist to prevent from starting a new
//request
fs.stat (local, function (error, stats){
if (error) return cb (error);
if (stats.isDirectory ()){
return cb (new Error ("The local file is a directory"));
}
var rsError;
var closed = false;
var rs = fs.createReadStream (local)
.on ("error", function (error){
rsError = error;
ps.abort (errors.EIO.message);
})
.on ("close", function (){
closed = true;
});
options = {
highWaterMark: options.highWaterMark,
userExtensions: options.userExtensions,
size: stats.size
};
var ps = new PutStream (remote, me._options, options)
.on ("error", function (error){
if (closed){
//Empty origin file
cb (error);
}else{
rs.on ("close", function (){
cb (error);
});
rs.destroy ();
}
})
.on ("abort", function (){
cb (rsError);
})
.on ("finish", function (){
cb ();
});
rs.pipe (ps);
});
};