Skip to content

Commit

Permalink
Merge 27ce23a into 1b5bc90
Browse files Browse the repository at this point in the history
  • Loading branch information
horiuchi committed Mar 2, 2023
2 parents 1b5bc90 + 27ce23a commit 96805aa
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 70 deletions.
232 changes: 185 additions & 47 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"commander": "^10.0.0",
"cross-fetch": "^3.1.5",
"debug": "^4.3.4",
"glob": "^8.1.0",
"glob": "^9.1.2",
"https-proxy-agent": "^5.0.1",
"js-yaml": "^4.1.0",
"tslib": "^2.5.0",
Expand All @@ -60,18 +60,18 @@
"@dtsgenerator/single-quote": "^1.6.3",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/debug": "^4.1.7",
"@types/glob": "^8.0.1",
"@types/glob": "^8.1.0",
"@types/js-yaml": "^4.0.5",
"@types/mocha": "^10.0.1",
"@types/node": "^16.18.12",
"@types/node": "^16.18.14",
"cross-env": "^7.0.3",
"eslint": "^8.34.0",
"husky": "^8.0.3",
"lint-staged": "^13.1.2",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"prettier": "^2.8.4",
"rimraf": "^4.1.2",
"rimraf": "^4.1.3",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1"
},
Expand Down
7 changes: 2 additions & 5 deletions src/core/jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
JsonSchema,
isJsonSchemaDraft04,
} from './type';
import { checkValidMIMEType } from './utils';

type OpenApiSchema = OpenApisV2.SchemaJson | OpenApisV3.SchemaJson;
type ParametersList =
Expand Down Expand Up @@ -382,11 +383,7 @@ export function searchAllSubSchema(
return;
}
for (const mime of Object.keys(types)) {
if (
/^text\/|^(?:application\/x-www-form-urlencoded$|^application\/([a-z0-9-_.]+\+)?json)$|^application\/json;|^application\/octet-stream$|^application\/jwt$|^application\/vnd.apple.pkpass$|^multipart\/form-data$|^image\//.test(
mime
)
) {
if (checkValidMIMEType(mime)) {
const mt = types[mime];
if (mt != null) {
setSubId(mt.schema, keys);
Expand Down
23 changes: 23 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ export function reduceTypes(types: SimpleTypes[]): SimpleTypes[] {
return Array.from(set.values());
}

export function checkValidMIMEType(mime: string): boolean {
const type = mime.toLowerCase().split(';')[0]?.trim();
if (type == null) {
return false;
}
if (
[
'application/octet-stream',
'application/x-www-form-urlencoded',
'multipart/form-data',

'application/jwt',
'application/vnd.apple.pkpass',
].includes(type)
) {
return true;
}
if (type.startsWith('text/') || type.startsWith('image/')) {
return true;
}
return /^application\/(?:[a-z0-9-_.]+\+)?json5?$/.test(type);
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function mergeSchema(a: any, b: any): boolean {
if ('$ref' in a || '$ref' in b) {
Expand Down
25 changes: 14 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import { URL } from 'url';
import glob from 'glob';
import glob, { GlobOptions } from 'glob';
import proxy from 'https-proxy-agent';
import { ScriptTarget } from 'typescript';
import { CommandOptions, defaultConfigFile } from './commandOptions';
Expand Down Expand Up @@ -63,18 +63,17 @@ export function buildProxyOptions(url: string): RequestInit | undefined {
return undefined;
}

export function globFiles(
export async function globFiles(
pattern: string,
options?: glob.IOptions
options?: GlobOptions
): Promise<string[]> {
return new Promise((resolve, reject) => {
glob(pattern, options ?? {}, (err, matches) => {
if (err) {
reject(err);
} else {
resolve(matches);
}
});
const res = await glob(pattern, options ?? {});
return res.map((r) => {
if (typeof r === 'string') {
return r;
} else {
return r.fullpath();
}
});
}

Expand Down Expand Up @@ -150,6 +149,10 @@ function convertToScriptTarget(target: string): ScriptTarget {
return ScriptTarget.ES2019;
case 'es2020':
return ScriptTarget.ES2020;
case 'es2021':
return ScriptTarget.ES2021;
case 'es2022':
return ScriptTarget.ES2022;
case 'esnext':
return ScriptTarget.ESNext;
default:
Expand Down
89 changes: 89 additions & 0 deletions test/core/utils_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import assert from 'assert';
import { checkValidMIMEType } from '../../src/core/utils';

describe('checkValidMIMEType test', () => {
it('MIME type is plain text', () => {
const mime = 'text/plain';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is html', () => {
const mime = 'text/html';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is html with charset', () => {
const mime = 'Text/HTML; charset=utf-8';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});

it('MIME type is svg', () => {
const mime = 'image/svg+xml';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is png', () => {
const mime = 'image/png';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is tiff', () => {
const mime = 'image/tiff';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});

it('MIME type is form urlencoded', () => {
const mime = 'application/x-www-form-urlencoded';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is octet stream', () => {
const mime = 'application/octet-stream';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is multipart form data', () => {
const mime = 'multipart/form-data';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});

it('MIME type is json', () => {
const mime = 'application/json';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is geojson', () => {
const mime = 'application/geo+json';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is json5', () => {
const mime = 'application/json5';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is webmanifest', () => {
const mime = 'application/manifest+json';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is json with parameters', () => {
const mime = 'application/json; charset=utf-8; version=1';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});

it('MIME type is jwt', () => {
const mime = 'application/jwt';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
it('MIME type is Passbook', () => {
const mime = 'application/vnd.apple.pkpass';
const actual = checkValidMIMEType(mime);
assert.equal(actual, true, mime);
});
});
8 changes: 5 additions & 3 deletions test/utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ describe('root utils test', () => {
root: __dirname + '/..',
});
assert.deepStrictEqual(actual, [
'src/cli.ts',
'src/commandOptions.ts',
'src/jsonPointer.ts',
'src/utils.ts',
'src/jsonPointer.ts',
'src/commandOptions.ts',
'src/cli.ts',
]);
});
it('throw error on getting files', () => {
Expand Down Expand Up @@ -117,6 +117,8 @@ describe('root utils test', () => {
['es2018', ScriptTarget.ES2018],
['es2019', ScriptTarget.ES2019],
['es2020', ScriptTarget.ES2020],
['es2021', ScriptTarget.ES2021],
['es2022', ScriptTarget.ES2022],
['esnext', ScriptTarget.ESNext],
] as const) {
options.target = actual;
Expand Down

0 comments on commit 96805aa

Please sign in to comment.