Skip to content

Commit a70cdc9

Browse files
committed
perform simple handling for unknown YAML tags
1 parent 51925b7 commit a70cdc9

File tree

5 files changed

+90
-5
lines changed

5 files changed

+90
-5
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"blessed-contrib": "^4.8.17",
1010
"chalk": "^4.0.0",
1111
"commander": "^7.1.0",
12-
"js-yaml": "^3.13.1",
12+
"js-yaml": "^4.0.0",
1313
"moment": "^2.24.0",
1414
"node-emoji": "^1.10.0",
1515
"open": "^8.0.2",
@@ -21,6 +21,7 @@
2121
"@babel/core": "^7.5.5",
2222
"@babel/preset-env": "^7.5.5",
2323
"@types/jest": "^26.0.20",
24+
"@types/js-yaml": "^4.0.0",
2425
"babel-jest": "^26.6.3",
2526
"eslint": "^7.21.0",
2627
"eslint-config-airbnb-base": "^14.0.0",

__test__/util/serverlessConfigParser.test.js src/services/severlessConfigParser/__test__/serverlessConfigParser.test.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
transformArgsToDict,
55
replaceStacknameOpt,
66
CYAN_STRING_FORMAT,
7-
} from "../../src/services/severlessConfigParser/helpers";
8-
import ServerlessConfigParser from "../../src/services/severlessConfigParser/serverlessConfigParser";
7+
} from "../helpers";
8+
import ServerlessConfigParser from "../serverlessConfigParser";
99

1010
const TEST_YAML_FILE_EMPTY = "";
1111
const TEST_YAML_FILE = `
@@ -42,6 +42,30 @@ provider:
4242
stage: \${opt:stage, 'dev'}
4343
`;
4444

45+
const TEST_YAML_FILE_REF = `
46+
service:
47+
name: test-\${opt:testArg1}
48+
49+
provider:
50+
name: aws
51+
runtime: nodejs10.x
52+
region: eu-west-1
53+
stage: \${opt:stage, 'dev'}
54+
55+
functions:
56+
hello:
57+
handler: hello.main
58+
memorySize: 1024
59+
timeout: 6
60+
events:
61+
- http:
62+
method: get
63+
path: hello
64+
authorizer:
65+
type: COGNITO_USER_POOLS
66+
authorizerId: !Ref ApiGatewayAuthorizer
67+
`;
68+
4569
// eslint-disable-next-line no-template-curly-in-string
4670
const STACK_NAME_OPT = "test-${opt:testArg1}";
4771
// eslint-disable-next-line no-template-curly-in-string
@@ -161,4 +185,11 @@ describe("Serverless Config Parsing", () => {
161185
const stage = "test";
162186
expect(SLS.getStackName(stage)).toBe("test-backend-test");
163187
});
188+
189+
it("should continue as normal when the config contains AWS intrinsic function tags", () => {
190+
const SLS = setupConfigParser(TEST_YAML_FILE_REF);
191+
expect(SLS.getStage()).toBe("dev");
192+
expect(SLS.getStackName("dev")).toBe("test-backend-dev");
193+
expect(SLS.getRegion()).toBe("eu-west-1");
194+
});
164195
});

src/services/severlessConfigParser/serverlessConfigParser.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import YAML_SCHEMA from "./ymlParsingSchema";
12
import { transformArgsToDict, replaceStacknameOpt } from "./helpers";
23

34
const fs = require("fs");
45
const path = require("path");
56
const YAML = require("js-yaml");
67

8+
function parseYaml(yamlString) {
9+
return YAML.load(yamlString, { schema: YAML_SCHEMA });
10+
}
11+
712
class ServerlessConfigParser {
813
constructor(program) {
914
const { args, location } = program;
@@ -13,9 +18,9 @@ class ServerlessConfigParser {
1318
const jsonPath = path.join(location, "serverless.json");
1419

1520
if (fs.existsSync(ymlPath)) {
16-
this.config = YAML.load(fs.readFileSync(ymlPath).toString("utf8"));
21+
this.config = parseYaml(fs.readFileSync(ymlPath, { encoding: "utf-8" }));
1722
} else if (fs.existsSync(yamlPath)) {
18-
this.config = YAML.load(fs.readFileSync(yamlPath).toString("utf8"));
23+
this.config = parseYaml(fs.readFileSync(yamlPath, { encoding: "utf-8" }));
1924
} else if (fs.existsSync(jsonPath)) {
2025
this.config = JSON.parse(fs.readFileSync(jsonPath).toString("utf8"));
2126
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const yaml = require("js-yaml");
2+
3+
class CustomTag {
4+
constructor(type, data) {
5+
this.type = type;
6+
this.data = data;
7+
}
8+
}
9+
10+
const tags = ["scalar", "sequence", "mapping"].map(
11+
(kind) =>
12+
// first argument here is a prefix, so this type will handle anything starting with !
13+
new yaml.Type("!", {
14+
kind,
15+
multi: true,
16+
representName(object) {
17+
return object.type;
18+
},
19+
represent(object) {
20+
return object.data;
21+
},
22+
instanceOf: CustomTag,
23+
construct(data, type) {
24+
return new CustomTag(type, data);
25+
},
26+
})
27+
);
28+
29+
const YAML_SCHEMA = yaml.DEFAULT_SCHEMA.extend(tags);
30+
31+
export default YAML_SCHEMA;

yarn.lock

+17
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,11 @@
13421342
jest-diff "^26.0.0"
13431343
pretty-format "^26.0.0"
13441344

1345+
"@types/js-yaml@^4.0.0":
1346+
version "4.0.0"
1347+
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.0.tgz#d1a11688112091f2c711674df3a65ea2f47b5dfb"
1348+
integrity sha512-4vlpCM5KPCL5CfGmTbpjwVKbISRYhduEJvvUWsH5EB7QInhEj94XPZ3ts/9FPiLZFqYO0xoW4ZL8z2AabTGgJA==
1349+
13451350
"@types/json-schema@^7.0.3":
13461351
version "7.0.7"
13471352
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
@@ -1590,6 +1595,11 @@ argparse@^1.0.7:
15901595
dependencies:
15911596
sprintf-js "~1.0.2"
15921597

1598+
argparse@^2.0.1:
1599+
version "2.0.1"
1600+
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
1601+
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
1602+
15931603
arr-diff@^4.0.0:
15941604
version "4.0.0"
15951605
resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-4.0.0.tgz#d6461074febfec71e7e15235761a329a5dc7c520"
@@ -4390,6 +4400,13 @@ js-yaml@^3.13.1:
43904400
argparse "^1.0.7"
43914401
esprima "^4.0.0"
43924402

4403+
js-yaml@^4.0.0:
4404+
version "4.0.0"
4405+
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.0.0.tgz#f426bc0ff4b4051926cd588c71113183409a121f"
4406+
integrity sha512-pqon0s+4ScYUvX30wxQi3PogGFAlUyH0awepWvwkj4jD4v+ova3RiYw8bmA6x2rDrEaj8i/oWKoRxpVNW+Re8Q==
4407+
dependencies:
4408+
argparse "^2.0.1"
4409+
43934410
jsbn@~0.1.0:
43944411
version "0.1.1"
43954412
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"

0 commit comments

Comments
 (0)