Skip to content
Merged
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
4 changes: 2 additions & 2 deletions cjs/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ const pack = (asset, source, target, options, timeout = 1000) => {
},
/* istanbul ignore next */
err => {
console.error(`\x1b[1m\x1b[31mError\x1b[0m ${source}`);
console.error(err);
_pack.delete(target);
_json.delete(asset);
console.error(`\x1b[1m\x1b[31mError\x1b[0m ${source}`);
console.error(err);
rej(err);
}
);
Expand Down
13 changes: 10 additions & 3 deletions cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ const streamFile = (res, asset, headers) => {
createReadStream(asset).pipe(res);
};

module.exports = ({source, dest, headers, cacheTimeout: CT}) => {
module.exports = ({
source,
dest,
headers,
maxWidth,
maxHeight,
cacheTimeout: CT
}) => {
const SOURCE = getPath(source);
const DEST = dest ? getPath(dest) : join(tmpdir(), 'ucdn');
const options = {createFiles: true, headers};
const options = {createFiles: true, maxWidth, maxHeight, headers};
return (req, res, next) => {
const path = req.url.replace(/\?.*$/, '');
const original = SOURCE + path;
Expand Down Expand Up @@ -86,8 +93,8 @@ module.exports = ({source, dest, headers, cacheTimeout: CT}) => {
/* istanbul ignore next */
const compress = length ? asset.slice(0, -length) : asset;
const waitForIt = compress + '.wait';
/* istanbul ignore next */
const fail = () => {
/* istanbul ignore next */
internalServerError(res);
};
dir(waitForIt, CT).then(
Expand Down
4 changes: 2 additions & 2 deletions esm/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export const pack = (asset, source, target, options, timeout = 1000) => {
},
/* istanbul ignore next */
err => {
console.error(`\x1b[1m\x1b[31mError\x1b[0m ${source}`);
console.error(err);
_pack.delete(target);
_json.delete(asset);
console.error(`\x1b[1m\x1b[31mError\x1b[0m ${source}`);
console.error(err);
rej(err);
}
);
Expand Down
13 changes: 10 additions & 3 deletions esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ const streamFile = (res, asset, headers) => {
createReadStream(asset).pipe(res);
};

export default ({source, dest, headers, cacheTimeout: CT}) => {
export default ({
source,
dest,
headers,
maxWidth,
maxHeight,
cacheTimeout: CT
}) => {
const SOURCE = getPath(source);
const DEST = dest ? getPath(dest) : join(tmpdir(), 'ucdn');
const options = {createFiles: true, headers};
const options = {createFiles: true, maxWidth, maxHeight, headers};
return (req, res, next) => {
const path = req.url.replace(/\?.*$/, '');
const original = SOURCE + path;
Expand Down Expand Up @@ -85,8 +92,8 @@ export default ({source, dest, headers, cacheTimeout: CT}) => {
/* istanbul ignore next */
const compress = length ? asset.slice(0, -length) : asset;
const waitForIt = compress + '.wait';
/* istanbul ignore next */
const fail = () => {
/* istanbul ignore next */
internalServerError(res);
};
dir(waitForIt, CT).then(
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"license": "ISC",
"devDependencies": {
"ascjs": "^4.0.0",
"coveralls": "^3.0.14",
"coveralls": "^3.1.0",
"nyc": "^15.0.1"
},
"module": "./esm/index.js",
Expand All @@ -31,7 +31,7 @@
},
"dependencies": {
"id-promise": "^0.1.0",
"ucompress": "^0.8.3"
"ucompress": "^0.9.0"
},
"repository": {
"type": "git",
Expand Down
25 changes: 20 additions & 5 deletions server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let clusters = 0;
let source = '.';
let dest = '';
let help = false;
let maxWidth, maxHeight;

const greetings = () => {
console.log('');
Expand Down Expand Up @@ -54,16 +55,28 @@ for (let {argv} = process, i = 0; i < argv.length; i++) {
dest = $1 ? $1.slice(1) : argv[++i];
break;
}
case /^--max-width(=\d+)?$/.test(argv[i]): {
const {$1} = RegExp;
maxWidth = parseInt($1 ? $1.slice(1) : argv[++i], 10);
break;
}
case /^--max-height(=\d+)?$/.test(argv[i]): {
const {$1} = RegExp;
maxHeight = parseInt($1 ? $1.slice(1) : argv[++i], 10);
break;
}
}
}

if (help) {
console.log('');
console.log(`\x1b[1mucdn --cluster 0 --port 8080 --source ./path/\x1b[0m`);
console.log(` --cluster X \x1b[2m# number of forks, default 0\x1b[0m`);
console.log(` --port XXXX \x1b[2m# port to use, default 8080\x1b[0m`);
console.log(` --source ./ \x1b[2m# path to serve as CDN, default current folder\x1b[0m`);
console.log(` --dest /tmp \x1b[2m# CDN cache path, default /tmp/ucdn\x1b[0m`);
console.log(` --cluster X \x1b[2m# number of forks, default 0\x1b[0m`);
console.log(` --port XXXX \x1b[2m# port to use, default 8080\x1b[0m`);
console.log(` --source ./ \x1b[2m# path to serve as CDN, default current folder\x1b[0m`);
console.log(` --dest /tmp \x1b[2m# CDN cache path, default /tmp/ucdn\x1b[0m`);
console.log(` --max-width X \x1b[2m# max images width\x1b[0m`);
console.log(` --max-height X \x1b[2m# max images height\x1b[0m`);
console.log('');
}
else if (isMaster && 0 < clusters) {
Expand All @@ -83,7 +96,9 @@ else {
const handler = cdn({
cacheTimeout: 300000,
source: base,
dest: dest ? resolve(process.cwd(), dest) : ''
dest: dest ? resolve(process.cwd(), dest) : '',
maxWidth,
maxHeight
});
const next = (req, res) => {
const {url} = req;
Expand Down
6 changes: 4 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Promise.resolve('\x1b[1mµcdn\x1b[0m')
}))
.then(name => {
requestHandler = cdn({
maxWidth: 320,
cacheTimeout: 100,
source: './test/source'
});
Expand Down Expand Up @@ -209,15 +210,16 @@ Promise.resolve('\x1b[1mµcdn\x1b[0m')
const content = operations.shift();
console.assert(content.length < 1, 'correct content');
console.assert(code === 200, 'correct code');
console.assert(headers['Content-Length'] === 37453, 'correct length');
console.assert(headers['Content-Length'] === 4756, 'correct length');
console.assert(headers['Content-Type'] === 'image/jpeg', 'correct mime');
console.assert(headers['ETag'] === '"924d-mZQTlzKwWxkl3X0y"', 'correct ETag');
console.assert(headers['ETag'] === '"1294-jdhULqQ3U4vpfDY2"', 'correct ETag');
resolve(path);
})
);
}))
.then(name => {
requestHandler = cdn({
maxHeight: 320,
cacheTimeout: 0,
source: './test/source'
});
Expand Down