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

add extensions option #65

Closed
wants to merge 4 commits into from
Closed

add extensions option #65

wants to merge 4 commits into from

Conversation

Necromos
Copy link
Contributor

@Necromos Necromos commented Dec 7, 2016

As mentioned in koajs/static#65 I tried to add extensions option to send.

@haoxins
Copy link
Member

haoxins commented Dec 8, 2016

@Necromos I will take a look later ((^_^))~

@haoxins haoxins self-assigned this Dec 12, 2016
@Necromos
Copy link
Contributor Author

@coderhaoxin any news? :)

@@ -48,6 +48,7 @@ function send(ctx, path, opts) {
var maxage = opts.maxage || opts.maxAge || 0;
var hidden = opts.hidden || false;
var format = opts.format === false ? false : true;
var extensions = Array.isArray(opts.extensions) ? opts.extensions : false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could check extensions here.

similar to https://github.com/pillarjs/send/blob/master/index.js#L921

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I have seen this approach in express also and it would not be a bad idea to use over here but after a while I'm looping through extensions array so this would add unneeded additional O(n) complexity.

Copy link
Member

@haoxins haoxins Dec 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. But if there is an invalid extension param in extensions option, we should throw an error, not just ignore it silently ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm so what about adding type checker to existing loop? It may look quite '🐟' in code but would preserve complexity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about adding type checker to existing loop

SGTM >_<

var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/hello', {extensions: ['json','htm','html']});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need space after comma

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry. Looks like I missed this.

@@ -462,7 +462,7 @@ describe('send(ctx, file)', function(){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/hello', {extensions: {}});
yield send(this, 'test/fixtures/hello', { extensions: {}});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ extensions: {}} -> { extensions: {} } (^_^)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, looks like I ommited some cases in regex, gonna fix it soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm adding refactor of all style issues I found in project but still wondering, why not use some kind of auto-formatter?

var list = [].concat(extensions || []);
for (var i = 0; i < list.length; i++) {
var extension = list[i];
if (!(typeof extension === 'string' || extension instanceof String)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (typeof extension !== 'string') is enough ((^_^)).

@Necromos I'll merge (rebase) this PR and publish a new version before this weekend.

Great work ! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderhaoxin yup I know but I wanted to keep solution from pillarjs 🐱

Thanks :)

@haoxins haoxins closed this in 24cfb97 Jan 10, 2017
@haoxins
Copy link
Member

haoxins commented Jan 10, 2017

@Necromos v3.3.0 published!

@haoxins
Copy link
Member

haoxins commented Jan 10, 2017

@Necromos can you take a look at #66, give some feedback (^_^)

also, there are some other edge scenes, such as

/a
/a.txt
/a.html

if we set extensions: ['.txt', '.html'] and GET /a

should we response a or a.txt ?
current behavior is consistent with the doc.

@Necromos
Copy link
Contributor Author

@coderhaoxin I will look into it during this week (^_^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants