Skip to content

Commit 7f3aea0

Browse files
author
Yuan Xie
committed
LEX-88 Start using Gulp to transpile to and use ES5.
1 parent 7fc21a6 commit 7f3aea0

File tree

8 files changed

+102
-43
lines changed

8 files changed

+102
-43
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.idea
2+
node_modules
3+
lib

example/get.example.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77
<body>
88
<div id="text-display">Retrieving response ...</div>
9-
<script src="../src/hmac.js"></script>
10-
<script src="get.example.js"></script>
9+
<script src="../lib/hmac.js"></script>
10+
<script src="../lib/get.example.js"></script>
1111
</body>
1212
</html>

example/get.example.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,8 @@ const HMAC = new AcquiaHttpHmac(hmac_config);
2121

2222
// Create and configure the request.
2323
let request = new XMLHttpRequest();
24-
request.onreadystatechange = state_change;
25-
request.open(method, path, true);
26-
request.setRequestHeader('Content-Type', content_type);
27-
28-
// The first two headers are the signed headers.
29-
request.setRequestHeader('Special-Header-1', 'special_header_1_value');
30-
request.setRequestHeader('Special-Header-2', 'special_header_2_value');
31-
request.setRequestHeader('Special-Header-3', 'special_header_3_value');
32-
33-
// Sign the request using AcquiaHttpHmac.sign().
34-
HMAC.sign(request, method, path, signed_headers, content_type);
35-
36-
// Send the request.
37-
request.send();
38-
3924
// Define the state change action.
40-
function state_change() {
25+
request.onreadystatechange = () => {
4126
if (request.readyState == 4) {
4227
// Check if the response status is 200 ok.
4328
if (request.status !== 200) {
@@ -54,4 +39,17 @@ function state_change() {
5439
// Finally, carry out the intended change.
5540
document.getElementById('text-display').innerHTML = request.response;
5641
}
57-
}
42+
};
43+
request.open(method, path, true);
44+
request.setRequestHeader('Content-Type', content_type);
45+
46+
// The first two headers are the signed headers.
47+
request.setRequestHeader('Special-Header-1', 'special_header_1_value');
48+
request.setRequestHeader('Special-Header-2', 'special_header_2_value');
49+
request.setRequestHeader('Special-Header-3', 'special_header_3_value');
50+
51+
// Sign the request using AcquiaHttpHmac.sign().
52+
HMAC.sign(request, method, path, signed_headers, content_type);
53+
54+
// Send the request.
55+
request.send();

example/post.example.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77
<body>
88
<div id="text-display">Retrieving response ...</div>
9-
<script src="../src/hmac.js"></script>
10-
<script src="post.example.js"></script>
9+
<script src="../lib/hmac.js"></script>
10+
<script src="../lib/get.example.js"></script>
1111
</body>
1212
</html>

example/post.example.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,8 @@ const HMAC = new AcquiaHttpHmac(hmac_config);
2222

2323
// Create and configure the request.
2424
let request = new XMLHttpRequest();
25-
request.onreadystatechange = state_change;
26-
request.open(method, path, true);
27-
request.setRequestHeader('Content-Type', content_type);
28-
29-
// The first two headers are the signed headers.
30-
request.setRequestHeader('Special-Header-1', 'special_header_1_value');
31-
request.setRequestHeader('Special-Header-2', 'special_header_2_value');
32-
request.setRequestHeader('Special-Header-3', 'special_header_3_value');
33-
34-
// Sign the request using AcquiaHttpHmac.sign().
35-
HMAC.sign(request, method, path, signed_headers, content_type, body);
36-
37-
// Send the request.
38-
request.send(body);
39-
4025
// Define the state change action.
41-
function state_change() {
26+
request.onreadystatechange = () => {
4227
if (request.readyState == 4) {
4328
// Check if the response status is 200 ok.
4429
if (request.status !== 200) {
@@ -55,4 +40,18 @@ function state_change() {
5540
// Finally, carry out the intended change.
5641
document.getElementById('text-display').innerHTML = request.response;
5742
}
58-
}
43+
};
44+
request.open(method, path, true);
45+
request.setRequestHeader('Content-Type', content_type);
46+
47+
// The first two headers are the signed headers.
48+
request.setRequestHeader('Special-Header-1', 'special_header_1_value');
49+
request.setRequestHeader('Special-Header-2', 'special_header_2_value');
50+
request.setRequestHeader('Special-Header-3', 'special_header_3_value');
51+
52+
// Sign the request using AcquiaHttpHmac.sign().
53+
HMAC.sign(request, method, path, signed_headers, content_type, body);
54+
55+
// Send the request.
56+
request.send(body);
57+

gulpfile.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var gulp = require('gulp');
2+
var babel = require('gulp-babel');
3+
var del = require('del');
4+
5+
gulp.task('clean-lib', function(){
6+
return del(['lib']);
7+
});
8+
9+
gulp.task('es5',['clean-lib'], function(){
10+
return gulp.src(['src/*.js','example/*.js'])
11+
.pipe(babel({
12+
presets: ['es2015']
13+
}))
14+
.pipe(gulp.dest('lib'));
15+
});
16+
17+
gulp.task('default', ['es5'], function() {
18+
gulp.watch('src/*.js', function() {
19+
gulp.run('es5');
20+
});
21+
gulp.watch('example/*.js', function() {
22+
gulp.run('es5');
23+
});
24+
});

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "http-hmac-javascript",
3+
"version": "0.1.0",
4+
"description": "HTTP HMAC JavaScript Library",
5+
"main": "gulpfile.js",
6+
"directories": {
7+
"example": "example"
8+
},
9+
"dependencies": {
10+
"babel-preset-es2015": "^6.3.13",
11+
"babel-cli": "^6.4.0",
12+
"crypto-js": "^3.1.6",
13+
"gulp-babel": "^6.1.1",
14+
"gulp-requirejs": "^0.1.3"
15+
},
16+
"devDependencies": {},
17+
"scripts": {
18+
"test": "echo \"Error: no test specified\" && exit 1"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/acquia/http-hmac-javascript.git"
23+
},
24+
"keywords": [
25+
"HTTP",
26+
"HMAC",
27+
"authentication",
28+
"Acquia"
29+
],
30+
"author": "Acquia Lift Engineering",
31+
"license": "ISC",
32+
"bugs": {
33+
"url": "https://github.com/acquia/http-hmac-javascript/issues"
34+
},
35+
"homepage": "https://github.com/acquia/http-hmac-javascript#readme"
36+
}

src/hmac.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class AcquiaHttpHmac {
8787
* When join(), use this string as the glue.
8888
* @returns {string}
8989
*/
90-
let parametersToString = function(parameters, value_prefix, value_suffix, glue) {
90+
let parametersToString = (parameters, value_prefix, value_suffix, glue) => {
9191
value_prefix = value_prefix || '=';
9292
value_suffix = value_suffix || '';
9393
glue = glue || '&';
@@ -107,9 +107,9 @@ class AcquiaHttpHmac {
107107
*
108108
* @returns {string}
109109
*/
110-
let generateNonce = function () {
110+
let generateNonce = () => {
111111
let d = new Date().getTime();
112-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
112+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
113113
var r = (d + Math.random()*16)%16 | 0;
114114
d = Math.floor(d/16);
115115
return (c=='x' ? r : (r&0x7|0x8)).toString(16);
@@ -127,7 +127,7 @@ class AcquiaHttpHmac {
127127
* The request's method.
128128
* @returns {boolean}
129129
*/
130-
let willSendBody = function(body, method) {
130+
let willSendBody = (body, method) => {
131131
let bodyless_request_types = ['GET', 'HEAD'];
132132
return body.length !== 0 && bodyless_request_types.indexOf(method) < 0;
133133
};
@@ -192,7 +192,7 @@ class AcquiaHttpHmac {
192192

193193
// For IE8 compatibility.
194194
if (!Date.now) {
195-
Date.now = function() { return new Date().getTime(); }
195+
Date.now = () => { return new Date().getTime(); }
196196
}
197197

198198
/*

0 commit comments

Comments
 (0)