Skip to content

Instantly share code, notes, and snippets.

@mrkurt
Created July 13, 2018 18:48
Show Gist options
  • Save mrkurt/bb2191d56e3ae13a5168b0aee61e8b9b to your computer and use it in GitHub Desktop.
Save mrkurt/bb2191d56e3ae13a5168b0aee61e8b9b to your computer and use it in GitHub Desktop.
Using the AWS SDK in a Fly App
import * as AWS from 'aws-sdk'
AWS.HttpClient.prototype.handleRequest = function handleRequest(httpRequest, httpOptions, callback, errCallback) {
var self = this;
var endpoint = httpRequest.endpoint;
var emitter = new EventEmitter();
callback(emitter);
var href = endpoint.protocol + '//' + endpoint.hostname;
if (endpoint.port !== 80 && endpoint.port !== 443) {
href += ':' + endpoint.port;
}
href += httpRequest.path;
const body = httpRequest.body && typeof httpRequest.body.buffer === 'object' ?
httpRequest.body.buffer :
httpRequest.body
const req = new Request(href, { method: httpRequest.method, body: body })
AWS.util.each(httpRequest.headers, function (key, value) {
if (key !== 'Content-Length' && key !== 'User-Agent' && key !== 'Host') {
req.headers.set(key, value);
}
});
// console.log("sending req: ", httpRequest.method, href)
// console.log("req headers: ", JSON.stringify(req.headers.toJSON()))
fetch(req).then((res) => {
console.log("GOT RES", res.status, JSON.stringify(res.headers.toJSON()))
res.arrayBuffer().then((buf) => {
// console.log("GOT BUF", buf.byteLength)
const headers = {};
for (const k of Object.keys(res.headers.toJSON())) {
headers[k] = res.headers.getAll(k).join(",");
}
emitter.statusCode = res.status
emitter.headers = headers
emitter.emit("headers", emitter.statusCode, emitter.headers, res.statusText);
emitter.emit("data", buf)
emitter.emit("end")
}).catch((err) => {
// console.log("error getting array buffer", err)
emitter.emit("end")
})
}).catch((err) => {
errCallback(AWS.util.error(new Error('Network Failure'), {
code: 'NetworkingError'
}));
})
return emitter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment