From 4c7f9050b92813b8e45a640c8e8d7190b86641d2 Mon Sep 17 00:00:00 2001 From: Lorenzo Marcon Date: Tue, 25 Jul 2017 10:39:05 +0200 Subject: [PATCH] added possibility to set different headers for files --- README.md | 21 +++++++++++++++++ lib/index.js | 41 +++++++++++++++++++++++++++++++-- test/index.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 121 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 725389b..9411089 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/index.js b/lib/index.js index 50ad35e..548dc83 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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'; @@ -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]; @@ -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); diff --git a/test/index.js b/test/index.js index 0bc9b9c..5487b0e 100644 --- a/test/index.js +++ b/test/index.js @@ -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') })); @@ -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); }); @@ -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' ]);