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

Suggestion: mime-type verification method #220

Closed
39hn opened this issue Jan 18, 2023 · 2 comments
Closed

Suggestion: mime-type verification method #220

39hn opened this issue Jan 18, 2023 · 2 comments

Comments

@39hn
Copy link
Contributor

39hn commented Jan 18, 2023

A description of the problem you're trying to solve.

Express has it's own method for checking mime-type (Request.is), but Nestia has implemented a duplicative method.

An overview of the suggested solution.

  1. using Express' Request.is
const request: express.Request = context.switchToHttp().getRequest();
if (!request.is("application/json")) {
    throw new BadRequestException(
        "Request body is not the application/json.",
    );
}
  1. Or, Considering performance, using String.prototype.includes instead of current Split-Some method
const JSON_MIME_TYPE = "application/json";
const isApplicationJson = (text?: string) =>
    text === JSON_MIME_TYPE ||
    text !== undefined &&
    text.includes(JSON_MIME_TYPE);

Benchmark between Split-Some and String.prototype.includes

@samchon
Copy link
Owner

samchon commented Jan 18, 2023

What about sending a PR about this?

@39hn
Copy link
Contributor Author

39hn commented Jan 18, 2023

I made benchmark of mime-type verification.

https://github.com/39hn/mime-type-check-benchmark

IdentityOperator#JSON_HEADER x 853,628,240 ops/sec ±0.91% (90 runs sampled)
IdentityOperator#JSON_HEADER_WITH_CHARSET: failed
IdentityOperator#PLAIN_HEADER x 859,293,207 ops/sec ±1.18% (88 runs sampled)
IdentityOperator#PLAIN_HEADER_WITH_CHARSET x 839,442,760 ops/sec ±1.29% (89 runs sampled)
IdentityOperator#EMPTY_HEADER x 832,590,602 ops/sec ±1.21% (88 runs sampled)
IdentityOperator#UNDEFINED_HEADER x 90,767,291 ops/sec ±0.76% (88 runs sampled)
TypeIs#JSON_HEADER x 984,103 ops/sec ±0.67% (88 runs sampled)
TypeIs#JSON_HEADER_WITH_CHARSET x 697,578 ops/sec ±1.95% (84 runs sampled)
TypeIs#PLAIN_HEADER x 1,118,033 ops/sec ±1.15% (90 runs sampled)
TypeIs#PLAIN_HEADER_WITH_CHARSET x 881,569 ops/sec ±0.65% (91 runs sampled)
TypeIs#EMPTY_HEADER x 98,558,744 ops/sec ±0.53% (89 runs sampled)
TypeIs#UNDEFINED_HEADER x 157,255,478 ops/sec ±2.12% (85 runs sampled)
SplitSome#JSON_HEADER x 6,021,528 ops/sec ±1.40% (88 runs sampled)
SplitSome#JSON_HEADER_WITH_CHARSET x 4,961,624 ops/sec ±2.64% (82 runs sampled)
SplitSome#PLAIN_HEADER x 5,786,325 ops/sec ±2.72% (81 runs sampled)
SplitSome#PLAIN_HEADER_WITH_CHARSET x 5,400,885 ops/sec ±0.56% (91 runs sampled)
SplitSome#EMPTY_HEADER x 6,493,469 ops/sec ±0.73% (88 runs sampled)
SplitSome#UNDEFINED_HEADER x 169,434,666 ops/sec ±0.57% (89 runs sampled)
StringIncludes#JSON_HEADER x 763,834,722 ops/sec ±1.45% (82 runs sampled)
StringIncludes#JSON_HEADER_WITH_CHARSET x 31,277,279 ops/sec ±0.72% (89 runs sampled)
StringIncludes#PLAIN_HEADER x 142,697,977 ops/sec ±0.70% (90 runs sampled)
StringIncludes#PLAIN_HEADER_WITH_CHARSET x 40,414,301 ops/sec ±0.50% (87 runs sampled)
StringIncludes#EMPTY_HEADER x 140,906,467 ops/sec ±0.48% (88 runs sampled)
StringIncludes#UNDEFINED_HEADER x 85,844,623 ops/sec ±0.57% (91 runs sampled)

TypeIs is what Express' req.is use.

gonna make PR with String.prototype.includes :D

39hn added a commit to 39hn/nestia that referenced this issue Jan 18, 2023
use `String.prototype.includes` to verify MIME type to be `application/json`
39hn added a commit to 39hn/nestia that referenced this issue Jan 18, 2023
Close samchon#220

use `String.prototype.includes` to verify MIME type to be `application/json`
samchon added a commit that referenced this issue Jan 18, 2023
Close #220 - use Express' `req.is` to verify MIME type to be `application/json`
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

No branches or pull requests

2 participants