Skip to content
This repository was archived by the owner on Dec 7, 2022. It is now read-only.

Commit 58d2097

Browse files
authored
Merge branch 'master' into greenkeeper/update-all
2 parents ae2309c + b5bc56f commit 58d2097

File tree

11 files changed

+3220
-167
lines changed

11 files changed

+3220
-167
lines changed

.travis.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
language: node_js
22
node_js:
3-
- 0.10
4-
- 0.12
5-
- iojs
3+
- 4
4+
- 6
65
before_script:
7-
- 'npm install -g grunt-cli coveralls'
6+
- 'yarn global add http-server'
7+
- 'http-server spec/schemas -s -p 8081 &'
88
script:
9-
- 'grunt connect spec && cat generated/reports/spec/lcov.info | coveralls'
9+
- 'yarn cover'
10+
- 'yarn cover:up'
11+
notifications:
12+
email: false

Gruntfile.coffee

Lines changed: 0 additions & 11 deletions
This file was deleted.

Gruntfile.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/index.js

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var fs = require('fs'),
44
path = require('path'),
55
deref = require('deref'),
6-
Async = require('async-parts');
6+
Promise = require('es6-promise');
77

88
var fetchFrom = {
99
http: require('http'),
@@ -18,40 +18,44 @@ function parseJSON(text, uri) {
1818
}
1919
}
2020

21-
function extractRefs(obj) {
22-
var set = [];
21+
function extractRefs(obj, acc) {
22+
if (typeof obj !== 'object') {
23+
return acc;
24+
}
2325

2426
for (var key in obj) {
2527
var value = obj[key];
2628

27-
if (key === '$ref') {
28-
set.push(value);
29+
if (key === '$ref'
30+
&& value.indexOf('#/') !== 0
31+
&& acc.indexOf(value) === -1) {
32+
acc.push(value);
2933
}
3034

3135
if (typeof value === 'object') {
32-
set = set.concat(extractRefs(value));
36+
acc = extractRefs(value, acc);
3337
}
3438
}
3539

36-
return set;
40+
return acc;
3741
}
3842

39-
module.exports = function(params, callback) {
43+
module.exports = function(params) {
4044
var schemas = params.schemas || (Array.isArray(params.schema) ? params.schema : (params.schema ? [params.schema] : []));
4145

42-
var $ = deref(),
43-
_ = new Async();
46+
var $ = deref();
4447

4548
var schema_directory = params.directory || process.cwd(),
4649
normalized_fakeroot = typeof params.fakeroot === 'string' && (params.fakeroot.replace(/\/+$/g, '') + '/');
4750

48-
function getRefs(from, parent) {
49-
function addRef(id, data) {
50-
$.refs[id] = data;
51-
getRefs(data, id);
52-
}
51+
function addRef(id, data) {
52+
$.refs[id] = data;
5353

54-
extractRefs(from).forEach(function(url) {
54+
return getRefs(data, id);
55+
}
56+
57+
function getRefs(from, parent) {
58+
return Promise.all(extractRefs(from, []).map(function(url) {
5559
var ref_id = $.util.getDocumentURI($.util.resolveURL(parent, url)),
5660
ref_path = ref_id.replace(normalized_fakeroot, '');
5761

@@ -65,69 +69,61 @@ module.exports = function(params, callback) {
6569
if (!$.util.isURL(ref_path)) {
6670
var schema_file = path.resolve(schema_directory, ref_path);
6771

68-
addRef(ref_id, parseJSON(fs.readFileSync(schema_file), schema_file));
69-
} else {
70-
_.then(function(next) {
71-
var req = fetchFrom[ref_id.split(':')[0]];
72-
73-
var done;
72+
return addRef(ref_id, parseJSON(fs.readFileSync(schema_file), schema_file));
73+
}
7474

75-
var timeout = setTimeout(function() {
76-
if (!done) {
77-
// fail fast
78-
next(new Error('cannot reach ' + ref_id));
79-
}
80-
}, params.timeout || 200);
75+
return new Promise(function(ok, fail) {
76+
var req = fetchFrom[ref_id.split(':')[0]];
8177

82-
req.get(ref_id, function(res) {
83-
clearTimeout(timeout);
78+
var done;
79+
var failed;
8480

85-
done = true;
81+
req.get(ref_id, function(res) {
82+
done = true;
8683

87-
var body = '';
84+
var body = '';
8885

89-
res.on('data', function(chunk) {
90-
body += chunk;
91-
});
86+
res.on('data', function(chunk) {
87+
body += chunk;
88+
});
9289

93-
res.on('end', function() {
94-
try {
95-
addRef(ref_id, parseJSON(body, ref_id));
96-
next();
97-
} catch (e) {
98-
next(e);
99-
}
100-
});
101-
}).on('error', function() {
102-
next(new Error('cannot reach ' + ref_id));
90+
res.on('end', function() {
91+
try {
92+
failed || ok(addRef(ref_id, parseJSON(body, ref_id)));
93+
} catch (e) {
94+
fail(e);
95+
}
10396
});
97+
}).on('error', function() {
98+
failed || fail(new Error('cannot reach ' + ref_id));
10499
});
105-
}
106-
});
100+
});
101+
}));
107102
}
108103

109-
try {
110-
schemas.forEach(function(schema) {
111-
getRefs($.util.normalizeSchema(normalized_fakeroot, schema), schema_directory);
112-
});
113-
} catch (e) {
114-
callback(e, $.refs);
115-
}
116-
117-
_.run(function(err) {
118-
for (var ref in $.refs) {
119-
var fixed_ref = ref.replace(normalized_fakeroot, '');
120-
121-
if (!$.refs[fixed_ref]) {
122-
$.refs['/' + fixed_ref] = { $ref: ref };
123-
$.refs[fixed_ref] = { $ref: ref };
104+
function _done(err) {
105+
Object.keys($.refs).forEach(function(ref) {
106+
if (typeof $.refs[ref] === 'object' && !$.refs[ref].$ref) {
107+
$.refs[ref] = $.util.normalizeSchema(ref, $.refs[ref]);
124108
}
125-
}
109+
});
126110

127111
if (params.schemas) {
128-
callback(err, $.refs, schemas);
129-
} else {
130-
callback(err, $.refs, schemas[0]);
112+
return { err: err, refs: $.refs, schemas: schemas };
131113
}
114+
115+
return { err: err, refs: $.refs, schema: schemas[0] };
116+
}
117+
118+
schemas = schemas.map(function(schema) {
119+
return $.util.normalizeSchema(normalized_fakeroot, schema);
132120
});
121+
122+
return Promise.all(schemas.map(function(schema) {
123+
return getRefs(schema, schema_directory);
124+
}))
125+
.then(function() {
126+
return _done();
127+
})
128+
.catch(_done);
133129
};

package.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,33 @@
88
"url": "https://github.com/json-schema-faker/refaker"
99
},
1010
"license": "MIT",
11+
"scripts": {
12+
"dev": "jasmine-node spec --coffee --verbose --autoTest --watchFolders lib",
13+
"dev:lint": "eslint lib spec",
14+
"dev:spec": "jasmine-node spec --coffee --noStackTrace --captureExceptions",
15+
"cover": "istanbul cover --root lib --x '**/spec/**' -- jasmine-node --coffee spec",
16+
"cover:up": "codecov --file=coverage/lcov.info --disable=gcov -e TRAVIS_NODE_VERSION",
17+
"test": "npm run dev:lint && npm run dev:spec --"
18+
},
1119
"devDependencies": {
1220
"clone": "^2.0.0",
1321
"glob": "^7.1.1",
1422
"grunt": "^1.0.1",
1523
"grunt-contrib-connect": "^1.0.2",
1624
"grunt-parts": "^0.5.6",
25+
"ajv": "^4.10.0",
26+
"codecov": "^1.0.1",
27+
"eslint": "^3.12.0",
28+
"eslint-config-airbnb-base": "^10.0.1",
29+
"eslint-plugin-import": "^2.2.0",
1730
"is-my-json-valid": "^2.12.2",
31+
"istanbul": "^0.4.5",
32+
"jasmine-node": "2.0.0-beta4",
1833
"tv4": "^1.1.12"
1934
},
2035
"dependencies": {
21-
"async-parts": "^0.2.2",
22-
"deref": "^0.6.3"
36+
"deref": "^0.7.0",
37+
"es6-promise": "^4.0.5"
2338
},
2439
"publishConfig": {
2540
"registry": "http://registry.npmjs.org"

spec/core/issues/remote.json

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,74 @@
99
},
1010
"refs": [
1111
"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema",
12-
"http://test.example.com/AWSECommerce.json",
13-
"/AWSECommerce.json",
14-
"AWSECommerce.json"
12+
"http://test.example.com/AWSECommerce.json"
13+
]
14+
},
15+
{
16+
"description": "should work with some azure schemas (see: goo.gl/wydbDt)",
17+
"schema": {
18+
"$ref": "deploymentTemplate.json"
19+
},
20+
"refs": [
21+
"http://schema.management.azure.com/schemas/2015-08-19/Microsoft.Search.json",
22+
"http://schema.management.azure.com/schemas/2016-05-16/Microsoft.AnalysisServices.json",
23+
"http://schema.management.azure.com/schemas/2016-06-01/Microsoft.RecoveryServices.json",
24+
"http://schema.management.azure.com/schemas/2015-07-01-preview/Microsoft.ServerManagement.json",
25+
"http://schema.management.azure.com/schemas/2016-07-01-preview/Microsoft.ServerManagement.json",
26+
"http://schema.management.azure.com/schemas/2015-04-08/Microsoft.DocumentDB.json",
27+
"http://schema.management.azure.com/schemas/2015-06-01/Microsoft.KeyVault.json",
28+
"http://schema.management.azure.com/schemas/2016-05-15/Microsoft.DevTestLab.json",
29+
"http://schema.management.azure.com/schemas/2015-05-21-preview/Microsoft.DevTestLab.json",
30+
"http://schema.management.azure.com/schemas/2014-06-01/Microsoft.Web.json",
31+
"http://schema.management.azure.com/schemas/2015-08-01/Microsoft.Web.json",
32+
"http://schema.management.azure.com/schemas/2014-04-01-preview/Microsoft.Sql.json",
33+
"http://schema.management.azure.com/schemas/2014-04-01/Microsoft.Insights.json",
34+
"http://schema.management.azure.com/schemas/2014-02-26/microsoft.visualstudio.json",
35+
"http://schema.management.azure.com/schemas/2014-04-01-preview/Microsoft.Cache.json",
36+
"http://schema.management.azure.com/schemas/2014-04-01/Microsoft.BizTalkServices.json",
37+
"http://schema.management.azure.com/schemas/2015-03-01-preview/Microsoft.AppService.json",
38+
"http://schema.management.azure.com/schemas/2015-04-01/Microsoft.NotificationHubs.json",
39+
"http://schema.management.azure.com/schemas/2015-08-01-preview/Microsoft.DataConnect.json",
40+
"http://schema.management.azure.com/schemas/2015-08-01/Microsoft.Cache.json",
41+
"http://schema.management.azure.com/schemas/2015-08-01/Microsoft.Network.json",
42+
"http://schema.management.azure.com/schemas/2015-11-01/Microsoft.Network.json",
43+
"http://schema.management.azure.com/schemas/2015-08-01/Microsoft.Storage.json",
44+
"http://schema.management.azure.com/schemas/2016-01-01/Microsoft.Storage.json",
45+
"http://schema.management.azure.com/schemas/2015-08-01/Microsoft.Compute.json",
46+
"http://schema.management.azure.com/schemas/2014-08-01-preview/Microsoft.Scheduler.json",
47+
"http://schema.management.azure.com/schemas/2015-10-01-preview/Microsoft.DataLakeStore.json",
48+
"http://schema.management.azure.com/schemas/2015-10-01-preview/Microsoft.DataLakeAnalytics.json",
49+
"http://schema.management.azure.com/schemas/2016-02-01-preview/Microsoft.CognitiveServices.json",
50+
"http://schema.management.azure.com/schemas/2016-01-29/Microsoft.PowerBI.json",
51+
"http://schema.management.azure.com/schemas/2016-03-30/Microsoft.DataCatalog.json",
52+
"http://schema.management.azure.com/schemas/2016-03-30/Microsoft.ContainerService.json",
53+
"http://schema.management.azure.com/schemas/2015-05-04-preview/Microsoft.Network.json",
54+
"http://schema.management.azure.com/schemas/2016-04-01/Microsoft.Network.json",
55+
"http://schema.management.azure.com/schemas/2015-06-01/Microsoft.Cdn.json",
56+
"http://schema.management.azure.com/schemas/2016-04-02/Microsoft.Cdn.json",
57+
"http://schema.management.azure.com/schemas/2015-12-01/Microsoft.Batch.json",
58+
"http://schema.management.azure.com/schemas/2016-04-01/Microsoft.Cache.json",
59+
"http://schema.management.azure.com/schemas/2015-02-01-preview/Microsoft.Logic.json",
60+
"http://schema.management.azure.com/schemas/2016-06-01/Microsoft.Logic.json",
61+
"http://schema.management.azure.com/schemas/2016-03-01/Microsoft.Scheduler.json",
62+
"http://schema.management.azure.com/schemas/2016-05-01-preview/Microsoft.MachineLearning.json",
63+
"http://schema.management.azure.com/schemas/2016-04-01/Microsoft.MachineLearning.json",
64+
"http://schema.management.azure.com/schemas/2015-10-31/Microsoft.Automation.json",
65+
"http://schema.management.azure.com/schemas/2015-10-01/Microsoft.Media.json",
66+
"http://schema.management.azure.com/schemas/2016-02-03/Microsoft.Devices.json",
67+
"http://schema.management.azure.com/schemas/2016-03-01/Microsoft.ServiceFabric.json",
68+
"http://schema.management.azure.com/schemas/2016-09-01/Microsoft.ServiceFabric.json",
69+
"http://schema.management.azure.com/schemas/2015-08-01/Microsoft.ServiceBus.json",
70+
"http://schema.management.azure.com/schemas/2015-08-01/Microsoft.EventHub.json",
71+
"http://schema.management.azure.com/schemas/2016-09-01/Microsoft.Authorization.json",
72+
"http://schema.management.azure.com/schemas/2016-09-01/Microsoft.Resources.json",
73+
"http://schema.management.azure.com/schemas/2016-07-07/Microsoft.ApiManagement.json",
74+
"http://schema.management.azure.com/schemas/2014-04-01/SuccessBricks.ClearDB.json",
75+
"http://schema.management.azure.com/schemas/2015-01-01/Sendgrid.Email.json",
76+
"http://schema.management.azure.com/schemas/2015-01-01/Microsoft.Resources.json",
77+
"http://schema.management.azure.com/schemas/2016-02-01/Microsoft.Resources.json",
78+
"http://schema.management.azure.com/schemas/2015-01-01/Microsoft.Authorization.json",
79+
"http://schema.management.azure.com/schemas/2014-10-01-preview/Microsoft.Authorization.json"
1580
]
1681
}
1782
]

spec/core/local.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"data": 42
3232
},
3333
{
34-
"description": "should resolve local refereces (faked, nested)",
34+
"description": "should resolve local references (faked, nested)",
3535
"schema": {
3636
"$ref": "http://test.example.com/sub/arrayTest.json"
3737
},

0 commit comments

Comments
 (0)