Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit f97c0b3

Browse files
committedOct 25, 2015
fix issue #84
1 parent 3b0a786 commit f97c0b3

File tree

3 files changed

+96
-90
lines changed

3 files changed

+96
-90
lines changed
 

‎src/config.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
var pkg = require('../package.json')
22

3-
exports = module.exports = {
4-
'api-path': '/api/v0/',
5-
'user-agent': '/node-' + pkg.name + '/' + pkg.version + '/',
6-
'host': 'localhost',
7-
'port': '5001'
3+
exports = module.exports = function getConfig () {
4+
return {
5+
'api-path': '/api/v0/',
6+
'user-agent': '/node-' + pkg.name + '/' + pkg.version + '/',
7+
'host': 'localhost',
8+
'port': '5001'
9+
}
810
}

‎src/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
var multiaddr = require('multiaddr')
2-
var config = require('./config')
3-
var requestAPI = require('./request-api')
2+
var getConfig = require('./config')
3+
var getRequestAPI = require('./request-api')
44

55
exports = module.exports = IpfsAPI
66

77
function IpfsAPI (host_or_multiaddr, port) {
88
var self = this
9+
var config = getConfig()
910

1011
if (!(self instanceof IpfsAPI)) {
1112
return new IpfsAPI(host_or_multiaddr, port)
@@ -28,6 +29,8 @@ function IpfsAPI (host_or_multiaddr, port) {
2829
config.port = split[1]
2930
}
3031

32+
var requestAPI = getRequestAPI(config)
33+
3134
// -- Internal
3235

3336
function command (name) {

‎src/request-api.js

+84-83
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,108 @@
11
var http = require('http')
22
var qs = require('querystring')
33
var getFilesStream = require('./get-files-stream')
4-
var config = require('./config')
54

6-
exports = module.exports = requestAPI
5+
exports = module.exports = function getRequestAPI (config) {
6+
return requestAPI
77

8-
function requestAPI (path, args, opts, files, buffer, cb) {
9-
var query, stream, contentType
10-
contentType = 'application/json'
8+
function requestAPI (path, args, opts, files, buffer, cb) {
9+
var query, stream, contentType
10+
contentType = 'application/json'
1111

12-
if (Array.isArray(path)) path = path.join('/')
12+
if (Array.isArray(path)) path = path.join('/')
1313

14-
opts = opts || {}
14+
opts = opts || {}
1515

16-
if (args && !Array.isArray(args)) args = [args]
17-
if (args) opts.arg = args
16+
if (args && !Array.isArray(args)) args = [args]
17+
if (args) opts.arg = args
1818

19-
if (files) {
20-
stream = getFilesStream(files, opts)
21-
if (!stream.boundary) {
22-
throw new Error('no boundary in multipart stream')
19+
if (files) {
20+
stream = getFilesStream(files, opts)
21+
if (!stream.boundary) {
22+
throw new Error('no boundary in multipart stream')
23+
}
24+
contentType = 'multipart/form-data; boundary=' + stream.boundary
2325
}
24-
contentType = 'multipart/form-data; boundary=' + stream.boundary
25-
}
26-
27-
if (typeof buffer === 'function') {
28-
cb = buffer
29-
buffer = false
30-
}
3126

32-
// this option is only used internally, not passed to daemon
33-
delete opts.followSymlinks
34-
35-
opts['stream-channels'] = true
36-
query = qs.stringify(opts)
37-
38-
var reqo = {
39-
method: files ? 'POST' : 'GET',
40-
host: config.host,
41-
port: config.port,
42-
path: config['api-path'] + path + '?' + query,
43-
headers: {
44-
'User-Agent': config['user-agent'],
45-
'Content-Type': contentType
46-
},
47-
withCredentials: false
48-
}
27+
if (typeof buffer === 'function') {
28+
cb = buffer
29+
buffer = false
30+
}
4931

50-
var req = http.request(reqo, function (res) {
51-
var data = ''
52-
var objects = []
53-
var stream = !!res.headers && !!res.headers['x-stream-output']
54-
var chunkedObjects = !!res.headers && !!res.headers['x-chunked-output']
32+
// this option is only used internally, not passed to daemon
33+
delete opts.followSymlinks
34+
35+
opts['stream-channels'] = true
36+
query = qs.stringify(opts)
37+
38+
var reqo = {
39+
method: files ? 'POST' : 'GET',
40+
host: config.host,
41+
port: config.port,
42+
path: config['api-path'] + path + '?' + query,
43+
headers: {
44+
'User-Agent': config['user-agent'],
45+
'Content-Type': contentType
46+
},
47+
withCredentials: false
48+
}
5549

56-
if (stream && !buffer) return cb(null, res)
57-
if (chunkedObjects && buffer) return cb(null, res)
50+
var req = http.request(reqo, function (res) {
51+
var data = ''
52+
var objects = []
53+
var stream = !!res.headers && !!res.headers['x-stream-output']
54+
var chunkedObjects = !!res.headers && !!res.headers['x-chunked-output']
5855

59-
res.on('data', function (chunk) {
60-
if (!chunkedObjects) {
61-
data += chunk
62-
return data
63-
}
56+
if (stream && !buffer) return cb(null, res)
57+
if (chunkedObjects && buffer) return cb(null, res)
6458

65-
try {
66-
var obj = JSON.parse(chunk.toString())
67-
objects.push(obj)
68-
} catch (e) {
69-
chunkedObjects = false
70-
data += chunk
71-
}
72-
})
73-
res.on('end', function () {
74-
var parsed
59+
res.on('data', function (chunk) {
60+
if (!chunkedObjects) {
61+
data += chunk
62+
return data
63+
}
7564

76-
if (!chunkedObjects) {
7765
try {
78-
parsed = JSON.parse(data)
79-
data = parsed
80-
} catch (e) {}
81-
} else {
82-
data = objects
83-
}
84-
85-
if (res.statusCode >= 400 || !res.statusCode) {
86-
if (!data) data = new Error()
87-
return cb(data, null)
88-
}
89-
return cb(null, data)
66+
var obj = JSON.parse(chunk.toString())
67+
objects.push(obj)
68+
} catch (e) {
69+
chunkedObjects = false
70+
data += chunk
71+
}
72+
})
73+
res.on('end', function () {
74+
var parsed
75+
76+
if (!chunkedObjects) {
77+
try {
78+
parsed = JSON.parse(data)
79+
data = parsed
80+
} catch (e) {}
81+
} else {
82+
data = objects
83+
}
84+
85+
if (res.statusCode >= 400 || !res.statusCode) {
86+
if (!data) data = new Error()
87+
return cb(data, null)
88+
}
89+
return cb(null, data)
90+
})
91+
res.on('error', function (err) {
92+
return cb(err, null)
93+
})
9094
})
91-
res.on('error', function (err) {
95+
96+
req.on('error', function (err) {
9297
return cb(err, null)
9398
})
94-
})
9599

96-
req.on('error', function (err) {
97-
return cb(err, null)
98-
})
100+
if (stream) {
101+
stream.pipe(req)
102+
} else {
103+
req.end()
104+
}
99105

100-
if (stream) {
101-
stream.pipe(req)
102-
} else {
103-
req.end()
106+
return req
104107
}
105-
106-
return req
107108
}

0 commit comments

Comments
 (0)
This repository has been archived.