-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Labels
guidanceQuestion that needs advice or information.Question that needs advice or information.
Description
Hi,
AWS SDK uses XML HttpRequest , which is not supported in service worker env.
Modified the code of aws-sdk.js, replaced XmlHttpRequest api with fetch api . Here is modified one :
AWS.XHRClient = AWS.util.inherit({
handleRequest: function handleRequest(httpRequest, httpOptions, callback, errCallback) {
var self = this;
var endpoint = httpRequest.endpoint;
var emitter = new EventEmitter();
var href = endpoint.protocol + '//' + endpoint.hostname;
if (endpoint.port !== 80 && endpoint.port !== 443) {
href += ':' + endpoint.port;
}
href += httpRequest.path;
callback(emitter);
var headers = new Headers();
AWS.util.each(httpRequest.headers, function (key, value) {
if (key !== 'Content-Length' && key !== 'User-Agent' && key !== 'Host') {
headers.set(key, value);
}
});
var credentials = 'omit';
if (httpOptions.xhrWithCredentials) {
credentials = 'include';
}
var request = new Request(href, {
method: httpRequest.method,
headers: headers,
body : httpRequest.body,
credentials: credentials
});
fetch(request).then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}).then(function(response) {
emitter.statusCode = response.status;
emitter.headers = self.parseHeaders(response.headers);
emitter.emit('headers', emitter.statusCode, emitter.headers);
response.text().then(function(res){
console.log(res);
self.finishRequest(res, emitter);
}).catch(function(err){
console.log(err);
});
}).catch(function(err) {
errCallback(AWS.util.error(new Error('Network Failure'), {
code: 'NetworkingError'
}));
});
return emitter;
},
parseHeaders: function parseHeaders(rawHeaders) {
var headers = {};
rawHeaders.forEach(function(val,key){
headers[key] = val;
});
return headers;
},
finishRequest: function finishRequest(res, emitter) {
var buffer;
try {
buffer = new AWS.util.Buffer(res);
} catch (e) {}
if (buffer) emitter.emit('data', buffer);
emitter.emit('end');
}
});
zzzxtreme
Metadata
Metadata
Assignees
Labels
guidanceQuestion that needs advice or information.Question that needs advice or information.