Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added possibility to set different headers for files #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ gulp.task('publish', function() {
}
```

## Custom headers

You can add different headers to different files using glob filter syntax:

```
var headers = {
"Cache-Control": 'max-age=86400, no-transform, public',
"fileFilters": [
{
"filter": '**/*.txt',
"Cache-Control": 'max-age=604800, no-transform, public'
},
{
"filter": ['**/*.jpg', '**/*.png'],
"Cache-Control": 'max-age=315360000, no-transform, public'
},
]
};

```

## Testing

1. Create an S3 bucket which will be used for the tests. Optionally create an IAM user for running the tests.
Expand Down
41 changes: 39 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var AWS = require('aws-sdk'),
mime = require('mime'),
pascalCase = require('pascal-case'),
File = require('vinyl'),
gutil = require('gulp-util');
gutil = require('gulp-util'),
minimatch = require('minimatch');

var PLUGIN_NAME = 'gulp-awspublish';

Expand Down Expand Up @@ -54,6 +55,10 @@ function toAwsParams(file) {
var headers = file.s3.headers || {};

for (var header in headers) {
if (header === 'Cache-Control') {
//console.log(file.s3.path, headers[header]);
}

if (header === 'x-amz-acl') {

params.ACL = headers[header];
Expand Down Expand Up @@ -340,8 +345,40 @@ Publisher.prototype.publish = function (headers, options) {
// add content-length header
if (!file.s3.headers['Content-Length']) file.s3.headers['Content-Length'] = file.contents.length;

function test(filter, fileName) {
if (!filter) { return false; }

if (Array.isArray(filter)) {
return filter.some(pattern => minimatch(file.relative, pattern));
}

if (typeof filter === 'string') {
return minimatch(file.relative, filter);
}

return false;
}

// add extra headers
for (header in headers) file.s3.headers[header] = headers[header];
for (header in headers){
if (header === 'fileFilters') {
headers[header].forEach(el => {
if (test(el.filter, file.relative)) {
for (var fileHeader in el) {
if (fileHeader === 'filter') { continue; }

file.s3.headers[fileHeader] = el[fileHeader];
}
}
});

continue;
}

file.s3.headers[header] = headers[header];
}

//console.log(file.s3.path, file.s3.headers);

if (options.simulate) return cb(null, file);

Expand Down
64 changes: 61 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,67 @@ describe('gulp-awspublish', function () {
stream.end();
});

it('should create new files on s3 with different headers', function (done) {

var headers = {
"Cache-Control": 'max-age=86400, no-transform, public',
"fileFilters": [
{
"filter": '**/*.txt',
"Cache-Control": 'max-age=604800, no-transform, public'
},
{
"filter": ['**/*.jpg', '**/*.png'],
"Cache-Control": 'max-age=315360000, no-transform, public'
},
]
};

var stream = publisher.publish(headers);
stream.write(new gutil.File({
path: '/test/hello3.txt',
base: '/',
contents: new Buffer('hello world')
}));

stream.write(new gutil.File({
path: '/test/hello4.png',
base: '/',
contents: new Buffer('hello world')
}));

stream
.pipe(es.writeArray(function (err, files) {
expect(err).not.to.exist;
expect(files).to.have.length(2);
expect(files[0].s3.path).to.eq('test/hello3.txt');
expect(files[0].s3.state).to.eq('create');
expect(files[0].s3.headers['Cache-Control']).to.eq(headers.fileFilters[0]['Cache-Control']);
expect(files[0].s3.headers['x-amz-acl']).to.eq('public-read');
expect(files[0].s3.headers['Content-Type']).to.eq('text/plain; charset=utf-8');
expect(files[0].s3.headers['Content-Length']).to.eq(files[0].contents.length);

expect(files[1].s3.path).to.eq('test/hello4.png');
expect(files[1].s3.state).to.eq('create');
expect(files[1].s3.headers['Cache-Control']).to.eq(headers.fileFilters[1]['Cache-Control']);
expect(files[1].s3.headers['x-amz-acl']).to.eq('public-read');
expect(files[1].s3.headers['Content-Type']).to.eq('image/png');
expect(files[1].s3.headers['Content-Length']).to.eq(files[1].contents.length);

publisher.client.headObject({ Key: 'test/hello3.txt' }, function (err, res) {
expect(res.ETag).to.exist;
done(err);
});
}));

stream.end();
});

it('should not send s3 header x-amz-acl if option {noAcl: true}', function (done) {

var stream = publisher.publish({}, {noAcl: true});
stream.write(new gutil.File({
path: '/test/hello3.txt',
path: '/test/hello5.txt',
base: '/',
contents: new Buffer('hello world')
}));
Expand All @@ -176,12 +232,12 @@ describe('gulp-awspublish', function () {
.pipe(es.writeArray(function (err, files) {
expect(err).not.to.exist;
expect(files).to.have.length(1);
expect(files[0].s3.path).to.eq('test/hello3.txt');
expect(files[0].s3.path).to.eq('test/hello5.txt');
expect(files[0].s3.state).to.eq('create');
expect(files[0].s3.headers).not.contain.keys('x-amz-acl');
expect(files[0].s3.headers['Content-Type']).to.eq('text/plain; charset=utf-8');
expect(files[0].s3.headers['Content-Length']).to.eq(files[0].contents.length);
publisher.client.headObject({ Key: 'test/hello.txt' }, function (err, res) {
publisher.client.headObject({ Key: 'test/hello5.txt' }, function (err, res) {
expect(res.ETag).to.exist;
done(err);
});
Expand Down Expand Up @@ -359,6 +415,8 @@ describe('gulp-awspublish', function () {
'test/hello.txt',
'test/hello2.txt',
'test/hello3.txt',
'test/hello4.png',
'test/hello5.txt',
'test/hello.txtgz',
'test/hello.txt.gz'
]);
Expand Down