console
3.56 KB
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
#!/usr/bin/env node
var repl = require('repl').start('aws-sdk> '),
replEval = repl.eval,
defaultOptions = {
logger: process.stdout,
region: process.env.AWS_REGION || 'us-east-1'
};
function customEval(cmd, context, filename, callback) {
replEval(cmd, context, filename, function(err, value) {
if (err) {
callback(err, null);
return;
}
function consoleDataExtraction(resp) {
context.data = resp.data;
context.error = resp.error;
callback(resp.error, resp.data);
}
if (value && value.constructor === AWS.Request && !value.__hasBeenEval__) {
try {
value.__hasBeenEval__ = true;
if (value.response) value.response.__hasBeenEval__ = true;
context.request = value;
context.response = value.response || null;
context.data = null;
context.error = null;
if (value._asm.currentState === 'complete' && value.response) {
context.data = value.response.data || null;
context.error = value.response.error || null;
callback(value.response.error, value.response.data);
} else {
value.on('complete', consoleDataExtraction);
if (!value.__hasBeenSent__) {
if (context.autoSend) {
value.send();
} else {
callback(null, value);
}
}
}
} catch (err2) {
callback(err2, null);
return;
}
} else if (value && value.constructor === AWS.Response && !value.__hasBeenEval__) {
try {
value.__hasBeenEval__ = true;
context.response = value;
context.request = value.request || null;
context.data = value.data || null;
context.error = value.error || null;
if (value.request) {
value.request.__hasBeenEval__ = true;
if (value.request._asm.currentState === 'complete') {
callback(value.error, value.data);
} else {
value.request.on('complete', consoleDataExtraction);
}
}
} catch (err2) {
callback(err2, null);
return;
}
} else {
callback(null, value);
}
});
}
var AWS = repl.context.AWS = require('../lib/aws');
repl.eval = customEval;
// context variables
repl.context.data = null;
repl.context.error = null;
repl.context.request = null;
repl.context.response = null;
// setup REPL history
try {
var replHistory = require('repl.history');
replHistory(repl, process.env.HOME + '/.node_history');
} catch (e) {
console.log("Missing repl.history package, history will not be supported.");
console.log(" Type `npm install repl.history` to enable history.");
}
// modify Request.prototype.send listener to track if the listener has been called
var sendListener = AWS.Request.prototype.send;
AWS.Request.prototype.send = function(callback) {
this.__hasBeenSent__ = true;
return sendListener.call(this, callback);
};
// flag to indicate that requests should be sent when callback is not provided
// by default this is on, but can be turned off by setting `autoSend = false`
repl.context.autoSend = true;
// load services as defined instances
for (var key in AWS) {
var id = AWS[key].serviceIdentifier;
if (id) {
if (id === 'cloudsearchdomain' || id === 'iotdata') continue; // this required an explicit endpoint
var svcClass = AWS[key];
var svc = new svcClass(defaultOptions);
svc.with = function(config) {
return new this.constructor.__super__(AWS.util.merge(this.config, config));
};
repl.context[svcClass.serviceIdentifier] = svc;
}
}