From 5292abca01fb5d2badc819a2a350a9447d472b08 Mon Sep 17 00:00:00 2001 From: Kanstantsin Kamkou Date: Fri, 16 Oct 2015 12:39:46 +0200 Subject: [PATCH] Add some sort of timeout to underlying HTTP connection, closes #5 --- Dockerfile | 2 +- README.md | 14 +++++++++----- lib/akamai.js | 5 +++-- package.json | 2 +- test/akamai.js | 15 +++++++++++++-- test/mocha.opts | 1 + 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 47ef9cc..9503e66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM google/nodejs +FROM node:latest WORKDIR /opt ADD package.json ./ RUN npm install diff --git a/README.md b/README.md index afc96bd..9938463 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ node-akamai-http-api ==================== -Akamai NetStorage HTTP API for the Node.js +Akamai NetStorage HTTP API for Node.js ## Installation ``` "dependencies": { - "akamai-http-api": "0.3.*" + "akamai-http-api": "0.4.*" } ``` ```npm update``` @@ -17,14 +17,18 @@ akamai.setConfig({ keyName: 'keyName', key: 'aLongString', host: 'changeme.akamaihd.net', - ssl: true, - verbose: false + ssl: true, // optional, default: false + verbose: false, // optional, default: false + request: { // optional, request.js options, see: https://github.com/request/request#requestoptions-callback + timeout: 20000 // 20s is the dafault value + } }); ``` ### Notices 1. You have to [enable the netstorage HTTP API](https://control.akamai.com/dl/customers/NS/NetStrgHttpCM.pdf) access using the control.akamai.com website 2. Ensure there are no more than 15 operations/second on netstorage, otherwise you can expect netstorage to serve 500 errors. +3. Double check the `host` value. In case of typo (fe: `test.upload.akamai.com`), the client just sits there trying to open up a socket. Default timeout is `20s`. ## API ### Advanced @@ -131,7 +135,7 @@ module.exports = myAkamai; ## Docker ```sh -# modify test/akamai.js#15 first +# modify test/akamai.js#19-21 first [sudo] docker build -t node-akamai-http-api . [sudo] docker run -ti --rm node-akamai-http-api ``` diff --git a/lib/akamai.js b/lib/akamai.js index 345e594..f98d119 100644 --- a/lib/akamai.js +++ b/lib/akamai.js @@ -22,7 +22,8 @@ var akamai = Object.create(null, { keyName: null, host: null, ssl: false, - verbose: false + verbose: false, + request: {timeout: 20000} } } }); @@ -142,7 +143,7 @@ akamai.getRequestObject = function (path, params, cb) { callback = function () {}, options = _.merge( {url: this.getUri(path), headers: this.getHeaders(path, params.headers)}, - params.request + params.request, this.getConfig().request ); if (typeof(cb) === 'function') { diff --git a/package.json b/package.json index addf843..c108ecd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "akamai-http-api", - "version": "0.3.0", + "version": "0.4.0", "main": "./lib/akamai.js", "author": "Kanstantsin Kamkou ", "description": "Akamai NetStorage HTTP API for the Node.js", diff --git a/test/akamai.js b/test/akamai.js index f49ae87..5e267a3 100644 --- a/test/akamai.js +++ b/test/akamai.js @@ -20,7 +20,8 @@ var pathRemoteFile = '/CODE/FILE.jpg', key: 'aLongString', host: 'changeme.akamaihd.net', ssl: true, - verbose: false + verbose: false, + request: {timeout: 20000} }; // tests @@ -32,7 +33,7 @@ module.exports = { akamai.setConfig(config); }, - 'Getters and Setters': { + 'Core functionality': { 'Config set-up': function () { akamai.getConfig().should.eql(config); akamai.getUri('/').should.eql('https://' + config.host + '/'); @@ -48,6 +49,16 @@ module.exports = { } }, + 'Timeout': function () { + var clone = Object.create(akamai); + clone.setConfig({host: 'test.upload.akamai.com', request: {timeout: 1000}}) + .du('/', function (err, result) { + should.not.exist(result); + err.should.be.an.instanceof(Error); + err.code.should.eql('ETIMEDOUT'); + }); + }, + 'Verbosity check': function (done) { var clone = Object.create(akamai); clone.setConfig({key: 'invalidKey', verbose: true}) diff --git a/test/mocha.opts b/test/mocha.opts index 28b3c1d..9178794 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -2,3 +2,4 @@ --reporter spec --ui exports --check-leaks +--bail