deckardcain
Advanced tools
| const API_BLUEPRINT_HEADER = /^[\uFEFF]?(((VERSION:( |\t)2)|(FORMAT:( |\t)(X-)?1A))([\n\r]{1,2}|$))/i; | ||
| const API_BLUEPRINT_RESPONSE = /\+\s+(?:[Rr]esponse|[Rr]equest)\s+\d{3}/i; | ||
| const LEGACY_BLUEPRINT_TITLE = /\-{3} ([^\n\r]+ )?\-{3}([\n\r]{1,2}|$)/; | ||
| const SWAGGER_JSON = /^[\uFEFF]?{\n?[\n\t ]*["']swagger["']: ["']\d\.\d["'],/i; | ||
| const SWAGGER_YAML = /(?:^|\n)swagger: ["']\d\.\d["']\n/i; | ||
| /** | ||
| * Identifies given source. | ||
| * @param {string} source - The source code of API description file. | ||
| * @returns {string|null} Media type of given file. | ||
| */ | ||
| function identify(source) { | ||
| // Stay awhile and listen! | ||
| if (source.match(API_BLUEPRINT_HEADER)) { | ||
| // I spotted 'FORMAT: 1A' header, which gives us a clear clue that | ||
| // the file is API Blueprint. | ||
| return 'text/vnd.apiblueprint'; | ||
| } | ||
| if (source.match(SWAGGER_YAML)) { | ||
| // Indeed, we are dealing with Swagger file! | ||
| return 'application/swagger+yaml'; | ||
| } | ||
| if (source.match(SWAGGER_JSON)) { | ||
| // Indeed, we are dealing with Swagger file! | ||
| return 'application/swagger+json'; | ||
| } | ||
| if (source.match(LEGACY_BLUEPRINT_TITLE)) { | ||
| // I spotted '--- Sample Title ---' title. This looks like | ||
| // we are dealing with the legacy Apiary Blueprint. | ||
| return 'text/vnd.legacyblueprint'; | ||
| } | ||
| if (source.match(API_BLUEPRINT_RESPONSE)) { | ||
| // I can not see the '--- Sample Title ---' and at the same time | ||
| // there is something like '+ Response 200' in the document, which is | ||
| // pretty distinctive for API Blueprint. | ||
| return 'text/vnd.apiblueprint'; | ||
| } | ||
| return null; | ||
| } | ||
| export default {identify}; |
+29
-47
@@ -6,14 +6,10 @@ 'use strict'; | ||
| }); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
| var _jsYaml = require('js-yaml'); | ||
| var _jsYaml2 = _interopRequireDefault(_jsYaml); | ||
| var API_BLUEPRINT_HEADER = /^[\uFEFF]?(((VERSION:( |\t)2)|(FORMAT:( |\t)(X-)?1A))([\n\r]{1,2}|$))/i; | ||
| var API_BLUEPRINT_RESPONSE = /\+\s+Response\s+\d{3}/i; | ||
| var API_BLUEPRINT_RESPONSE = /\+\s+(?:[Rr]esponse|[Rr]equest)\s+\d{3}/i; | ||
| var LEGACY_BLUEPRINT_TITLE = /\-{3} ([^\n\r]+ )?\-{3}([\n\r]{1,2}|$)/; | ||
| var SWAGGER_JSON = /^[\uFEFF]?{\n?[\n\t ]*["']swagger["']: ["']\d\.\d["'],/i; | ||
| var SWAGGER_YAML = /(?:^|\n)swagger: ["']\d\.\d["']\n/i; | ||
| /** | ||
@@ -26,46 +22,32 @@ * Identifies given source. | ||
| // Stay awhile and listen! | ||
| if (source.match(API_BLUEPRINT_HEADER)) { | ||
| // I spotted 'FORMAT: 1A' header, which gives us a clear clue that | ||
| // the file is API Blueprint. | ||
| return 'text/vnd.apiblueprint'; | ||
| } | ||
| var identifiedType = null; | ||
| if (source.match(SWAGGER_YAML)) { | ||
| // Indeed, we are dealing with Swagger file! | ||
| return 'application/swagger+yaml'; | ||
| } | ||
| try { | ||
| var json = JSON.parse(source); | ||
| if (source.match(SWAGGER_JSON)) { | ||
| // Indeed, we are dealing with Swagger file! | ||
| return 'application/swagger+json'; | ||
| } | ||
| // It looks like the source is a valid JSON file. I suspect it to | ||
| // be a Swagger file. | ||
| if (json.swagger) { | ||
| // Indeed, we are dealing with Swagger file! | ||
| identifiedType = 'application/swagger+json'; | ||
| } | ||
| } catch (jsonException) { | ||
| // Well ok, it's not a JSON...Maybe we're dealing with YAML file? | ||
| try { | ||
| var yaml = _jsYaml2['default'].safeLoad(source); | ||
| if (source.match(LEGACY_BLUEPRINT_TITLE)) { | ||
| // I spotted '--- Sample Title ---' title. This looks like | ||
| // we are dealing with the legacy Apiary Blueprint. | ||
| return 'text/vnd.legacyblueprint'; | ||
| } | ||
| if (yaml.swagger) { | ||
| // Indeed, we are dealing with Swagger file! | ||
| identifiedType = 'application/swagger+yaml'; | ||
| } | ||
| } catch (yamlException) { | ||
| // Well ok I am sorry, it's not a Swaggerish | ||
| } | ||
| } finally { | ||
| if (identifiedType === null) { | ||
| if (source.match(API_BLUEPRINT_HEADER)) { | ||
| // I spotted 'FORMAT: 1A' header, which gives us a clear clue that | ||
| // the file is API Blueprint. | ||
| identifiedType = 'text/vnd.apiblueprint'; | ||
| } else if (source.match(LEGACY_BLUEPRINT_TITLE)) { | ||
| // I spotted '--- Sample Title ---' title. This looks like | ||
| // we are dealing with the legacy Apiary Blueprint. | ||
| identifiedType = 'text/vnd.legacyblueprint'; | ||
| } else if (source.match(API_BLUEPRINT_RESPONSE)) { | ||
| // I can not see the '--- Sample Title ---' and at the same time | ||
| // there is something like '+ Response 200' in the document, which is | ||
| // pretty distinctive for API Blueprint. | ||
| identifiedType = 'text/vnd.apiblueprint'; | ||
| } | ||
| } | ||
| if (source.match(API_BLUEPRINT_RESPONSE)) { | ||
| // I can not see the '--- Sample Title ---' and at the same time | ||
| // there is something like '+ Response 200' in the document, which is | ||
| // pretty distinctive for API Blueprint. | ||
| return 'text/vnd.apiblueprint'; | ||
| } | ||
| return identifiedType; | ||
| } | ||
| return null; | ||
| } | ||
@@ -72,0 +54,0 @@ |
+5
-9
| { | ||
| "name": "deckardcain", | ||
| "version": "0.2.0", | ||
| "version": "0.2.1", | ||
| "description": "Identifies (media) type of API description files", | ||
| "main": "lib/deckardcain", | ||
| "scripts": { | ||
| "lint": "eslint src --ext .js,.es6 src", | ||
| "lint": "eslint src --ext .js src", | ||
| "precompile": "npm run lint", | ||
| "compile": "babel src/ --out-dir lib/", | ||
| "prepublish": "npm run compile", | ||
| "preserver-test": "npm run compile", | ||
| "prebrowser-test": "npm run compile", | ||
| "server-test": "mocha --compilers es6:babel/register -R spec --recursive", | ||
| "browser-test": "karma start", | ||
| "test": "npm run server-test && npm run browser-test" | ||
| "test": "npm run server-test && npm run browser-test && npm run lint" | ||
| }, | ||
@@ -38,2 +36,3 @@ "repository": { | ||
| "eslint": "^1.2.1", | ||
| "eslint-config-airbnb": "^1.0.0", | ||
| "karma": "^0.13.3", | ||
@@ -45,8 +44,5 @@ "karma-browserify": "^4.2.1", | ||
| "karma-mocha-reporter": "^1.0.4", | ||
| "eslint-config-airbnb": "^1.0.0", | ||
| "mocha": "^2.2.1" | ||
| }, | ||
| "dependencies": { | ||
| "js-yaml": "^3.4.3" | ||
| } | ||
| "dependencies": {} | ||
| } |
+1
-1
@@ -36,3 +36,3 @@ # Deckard Cain | ||
| < 200 | ||
| `) // 'text/vnd.legacyblueprint | ||
| `) // 'text/vnd.legacyblueprint' | ||
@@ -39,0 +39,0 @@ identify(` |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
-100%82
41.38%8087
-12.77%- Removed
- Removed
- Removed
- Removed
- Removed