Skip to content

Commit ce67274

Browse files
author
Anthony Nowell
committed
Update blueprint and use static lib
1 parent 0ae60ae commit ce67274

File tree

4 files changed

+27
-243
lines changed

4 files changed

+27
-243
lines changed

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
contrib
2+
examples
3+
src
4+
gulpfile.js
5+
tmp

contrib/lambda/algorithmia-lambda-nodejs.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"eventSources": {
1111
"s3": [{}]
1212
},
13-
"version": "1.0.1",
14-
"build": "https://s3.amazonaws.com/algorithmia-lambda/index.js",
13+
"version": "1.0.2",
14+
"build": "https://s3.amazonaws.com/algorithmia-lambda/algorgthmia-lambda-nodejs.zip",
1515
"license": "MIT",
1616
"tags": [
1717
"nodejs",
@@ -24,4 +24,4 @@
2424
"Algorithmia <[email protected]> (https://www.algorithmia.com)"
2525
],
2626
"private": false
27-
}
27+
}

contrib/lambda/index.js

Lines changed: 1 addition & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
}
3636
*/
3737

38+
var algorithmia = require("algorithmia");
3839
var AWS = require('aws-sdk');
3940
var apiKey, kmsEncryptedApiKey;
4041

@@ -115,240 +116,3 @@ var processEvent = function(event, context) {
115116
}
116117
});
117118
}
118-
119-
120-
/*
121-
* Algorithmia NodeJS SDK below
122-
*/
123-
var Algorithm, AlgorithmiaClient, AlgoResponse, Data, algorithmia, defaultApiAddress, http, https, packageJson, url;
124-
https = require('https');
125-
http = require('http');
126-
url = require('url');
127-
128-
defaultApiAddress = 'https://api.algorithmia.com';
129-
130-
AlgorithmiaClient = (function() {
131-
function AlgorithmiaClient(key, address) {
132-
this.apiAddress = address || process.env.ALGORITHMIA_API || defaultApiAddress;
133-
key = key || process.env.ALGORITHMIA_API_KEY;
134-
if (key) {
135-
if (key.indexOf('Simple ') === 0) {
136-
this.apiKey = key;
137-
} else {
138-
this.apiKey = 'Simple ' + key;
139-
}
140-
} else {
141-
this.apiKey = '';
142-
}
143-
}
144-
145-
AlgorithmiaClient.prototype.algo = function(path) {
146-
return new Algorithm(this, path);
147-
};
148-
149-
AlgorithmiaClient.prototype.file = function(path) {
150-
return new Data(this, path);
151-
};
152-
153-
AlgorithmiaClient.prototype.req = function(path, method, data, cheaders, callback) {
154-
var dheader, httpRequest, key, options, protocol, val;
155-
dheader = {
156-
'Content-Type': 'application/JSON',
157-
'Accept': 'application/JSON',
158-
'User-Agent': 'algorithmia-lambda/1.0.1 (NodeJS ' + process.version + ')'
159-
};
160-
if (this.apiKey) {
161-
dheader['Authorization'] = this.apiKey;
162-
}
163-
for (key in cheaders) {
164-
val = cheaders[key];
165-
dheader[key] = val;
166-
}
167-
options = url.parse(this.apiAddress + path);
168-
options.method = method;
169-
options.headers = dheader;
170-
protocol = options.protocol === 'https:' ? https : http;
171-
httpRequest = protocol.request(options, function(res) {
172-
var chunks;
173-
res.setEncoding('utf8');
174-
chunks = [];
175-
res.on('data', function(chunk) {
176-
return chunks.push(chunk);
177-
});
178-
res.on('end', function() {
179-
var body, buff;
180-
buff = chunks.join('');
181-
if (dheader['Accept'] === 'application/JSON') {
182-
body = JSON.parse(buff);
183-
} else {
184-
body = buff;
185-
}
186-
if (callback) {
187-
if (res.statusCode < 200 || res.statusCode >= 300) {
188-
if (!body) {
189-
body = {};
190-
}
191-
if (!body.error) {
192-
body.error = {
193-
message: 'HTTP Response: ' + res.statusCode
194-
};
195-
}
196-
}
197-
callback(body, res.statusCode);
198-
}
199-
});
200-
return res;
201-
});
202-
httpRequest.write(data);
203-
httpRequest.end();
204-
};
205-
206-
return AlgorithmiaClient;
207-
208-
})();
209-
210-
algorithmia = function(key, address) {
211-
return new AlgorithmiaClient(key, address);
212-
};
213-
214-
algorithmia.client = function(key, address) {
215-
return new AlgorithmiaClient(key, address);
216-
};
217-
218-
algorithmia.algo = function(path) {
219-
this.defaultClient = this.defaultClient || new AlgorithmiaClient();
220-
return this.defaultClient.algo(path);
221-
};
222-
223-
algorithmia.file = function(path) {
224-
this.defaultClient = this.defaultClient || new AlgorithmiaClient();
225-
return this.defaultClient.file(path);
226-
};
227-
228-
229-
Algorithm = (function() {
230-
function Algorithm(client, path) {
231-
this.client = client;
232-
this.algo_path = path;
233-
this.promise = {
234-
then: (function(_this) {
235-
return function(callback) {
236-
return _this.callback = callback;
237-
};
238-
})(this)
239-
};
240-
}
241-
242-
Algorithm.prototype.pipe = function(input) {
243-
var contentType, data;
244-
data = input;
245-
if (Buffer.isBuffer(input)) {
246-
contentType = 'application/octet-stream';
247-
} else if (typeof input === 'string') {
248-
contentType = 'text/plain';
249-
} else {
250-
contentType = 'application/json';
251-
data = JSON.stringify(input);
252-
}
253-
this.req = this.client.req('/v1/algo/' + this.algo_path, 'POST', data, {
254-
'Content-Type': contentType
255-
}, (function(_this) {
256-
return function(response, status) {
257-
return _this.callback(new AlgoResponse(response, status));
258-
};
259-
})(this));
260-
return this.promise;
261-
};
262-
263-
Algorithm.prototype.pipeJson = function(input) {
264-
if (typeof input !== 'string') {
265-
throw "Cannot convert " + (typeof input) + " to string";
266-
}
267-
this.req = this.client.req('/v1/algo/' + this.algo_path, 'POST', input, {
268-
'Content-Type': 'application/json'
269-
}, (function(_this) {
270-
return function(response, status) {
271-
return _this.callback(new AlgoResponse(response, status));
272-
};
273-
})(this));
274-
return this.promise;
275-
};
276-
277-
return Algorithm;
278-
279-
})();
280-
281-
AlgoResponse = (function() {
282-
function AlgoResponse(response, status) {
283-
this.status = status;
284-
this.result = response.result;
285-
this.error = response.error;
286-
this.metadata = response.metadata;
287-
}
288-
289-
AlgoResponse.prototype.get = function() {
290-
if (this.error) {
291-
throw "" + this.error.message;
292-
}
293-
switch (this.metadata.content_type) {
294-
case "void":
295-
return null;
296-
case "text":
297-
case "json":
298-
return this.result;
299-
case "binary":
300-
return new Buffer(this.result, 'base64');
301-
default:
302-
throw "Unknown result content_type: " + this.metadata.content_type + ".";
303-
}
304-
};
305-
306-
return AlgoResponse;
307-
308-
})();
309-
310-
311-
Data = (function() {
312-
function Data(client, path) {
313-
this.client = client;
314-
if (path.indexOf('data://') !== 0) {
315-
throw 'Supplied path is invalid.';
316-
}
317-
this.data_path = path.replace(/data\:\/\//, '');
318-
}
319-
320-
Data.prototype.putString = function(content, callback) {
321-
var headers;
322-
headers = {
323-
'Content-Type': 'text/plain'
324-
};
325-
return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback);
326-
};
327-
328-
Data.prototype.putJson = function(content, callback) {
329-
var headers;
330-
headers = {
331-
'Content-Type': 'application/JSON'
332-
};
333-
return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback);
334-
};
335-
336-
Data.prototype.getString = function(callback) {
337-
var headers;
338-
headers = {
339-
'Accept': 'text/plain'
340-
};
341-
return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback);
342-
};
343-
344-
Data.prototype.getJson = function(callback) {
345-
var headers;
346-
headers = {
347-
'Accept': 'text/plain'
348-
};
349-
return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback);
350-
};
351-
352-
return Data;
353-
354-
})();

make-lambda-build

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
#!/bin/bash
22

3-
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
3+
set -e
44

5+
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
56
TMP=$DIR/tmp/lambda
7+
rm -rf "$TMP"
8+
9+
# Create the blueprint structure
610
mkdir -p $TMP
711
cp contrib/lambda/index.js $TMP/
8-
cp -r $DIR/lib/* $TMP/
9-
cd $TMP && zip algorithmia-lambda-nodejs.zip *.js
12+
cd $TMP
13+
npm install $DIR --prefix .
14+
15+
# Tweak User-Agent in the client
16+
sed -i -e 's/\(User-Agent.*\)algorithmia-nodejs/\1 algorithmia-lambda/' $TMP/node_modules/algorithmia/lib/algorithmia.js
17+
18+
# Zip up the blueprint
19+
zip -r algorgthmia-lambda-nodejs.zip .
20+
21+
echo "-----------------------------"
22+
echo "Finished packaging blueprint."
23+
echo "Blueprint config: $DIR/contrib/lambda/algorithmia-lambda-nodejs.json"
24+
echo "Blueprint zip: $TMP/algorithmia-lambda-nodejs.zip"

0 commit comments

Comments
 (0)