Skip to content

Commit 3966e01

Browse files
committed
class extend instead of code injection
1 parent 7d77049 commit 3966e01

5 files changed

+86
-77
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
tags
2+
ctagsrc
13
node_modules
24
.tmp/*

index.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs');
44
const jsonPatch = require('rfc6902');
5-
const refParser = require('./patched-json-schema-ref-parser');
5+
const RefParser = require('./patched-json-schema-ref-parser');
66
const jps = require('./json-pointer-selectors');
77
const jsonPtr = require('json-ptr');
88
const clone = require('./clone');
@@ -81,11 +81,15 @@ const applyInherit = function (doc, options, rootDoc, inWith) {
8181
throw new Error(`inherit: ${$inherit}.source is null!`);
8282
}
8383
if (!(inh.with == null || Array.isArray(inh.with) && inh.with.length == 0)) {
84-
const patch = (options.compileJsonPatch || jps.compileJsonPatch)(inh.source, inh.with)
85-
jsonPatch.applyPatch(
86-
inh.source,
87-
patch
88-
);
84+
//console.log(options.compileJsonPatch, 'SOURCE:', inh.source, 'WITH:', inh.with)
85+
inh.with.forEach(op => {
86+
const patch = (options.compileJsonPatch || jps.compileJsonPatch)(inh.source, [op])
87+
//console.log('PATCH', patch)
88+
jsonPatch.applyPatch(
89+
inh.source,
90+
patch
91+
);
92+
});
8993
}
9094
doc = inh.source;
9195
}
@@ -107,7 +111,7 @@ const bundle = function (filepath, options) {
107111
return new Promise((res, rej) => rej(new Error(`invalid argument: file "${filepath}" not exists`)));
108112
}
109113
options = options || {};
110-
const parser = new refParser();
114+
const parser = new RefParser();
111115
if (typeof options.inherit == 'string') {
112116
parser.setJybidInheritWord(options.inherit);
113117
}
@@ -151,7 +155,7 @@ const dereference = function (doc, options) {
151155
reject(e);
152156
});
153157
}
154-
return refParser.dereference(doc)
158+
return RefParser.dereference(doc)
155159
.catch((e) => {
156160
console.error('dereference', 'doc:', doc, 'error:', e);
157161
throw e;

json-pointer-selectors.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,20 @@ const compileOperation = (source, op) => {
240240
// checking new pathes deeper
241241
return compileOperation(source, found);
242242
}
243-
return null;
244-
//throw new Error(`path ${op.path} compiles to selector that has no matches`);
243+
//return null;
244+
throw new Error(`op ${JSON.stringify(op)} compiles to selector that has no matches`);
245245
}
246246

247247
// all path exists or "op" is adding a new leaf
248248
if (jsonPtr.get(source, op.path)
249-
|| op.op == 'add' && jsonPtr.get(source, op.path.match(/(^.*)\/[^\/]*/)[1])
249+
|| (op.op == 'add' || op.op == 'move')
250+
&& jsonPtr.get(source, op.path.match(/(^.*)\/[^\/]*/)[1])
250251
) {
251252
return op;
252253
}
253254

254-
return null;
255+
//return null;
256+
throw new Error(`op ${JSON.stringify(op)} compiles to selector that has no matches`);
255257
};
256258

257259
/**
@@ -264,7 +266,9 @@ const compileJsonPatch = (source, patchOperations) => {
264266
const res = [];
265267
for (let i = 0; i < patchOperations.length; i++) {
266268
const op = patchOperations[i];
267-
if (!op.path) continue;
269+
if (!op.path) {
270+
continue;
271+
}
268272

269273
const compiled = compileOperation(source, op);
270274
if (compiled) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jybid",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "json yaml bundle inherit dereference",
55
"main": "index.js",
66
"directories": {

patched-json-schema-ref-parser.js

+62-63
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
const jsonPtr = require('json-ptr');
2-
const $RefParser = require('json-schema-ref-parser');
3-
module.exports = $RefParser;
2+
const JsonSchemaRefParser = require('json-schema-ref-parser');
43

54
/**
65
Purpose of this module is to protect $inherit.with from resolving local references inside it
76
Bundle checks $ref's and fails when comes to JSON Patch document, so here we decorate 'resolve' and 'bundle' methods
87
*/
98

10-
/**
11-
Set custom word instead of '$inherit'
12-
*/
13-
$RefParser.prototype.setJybidInheritWord = function (word) {
14-
this._jybid_inherit_word = word || '$inherit';
15-
};
16-
179
const PROTECTED_REF = 'this_$ref_is_protected_from_bundle';
1810

1911
const maybe = (err, res, callback) => {
@@ -64,64 +56,71 @@ const inNodes = function (prefix, suffix, root, doc, cb) {
6456
return;
6557
}
6658

67-
$RefParser.prototype._jybid_processResolved = function (doc) {
68-
if (this._jybid_inherit_word == null) {
69-
this.setJybidInheritWord();
59+
class PatchedJsonSchemaRefParser extends JsonSchemaRefParser {
60+
/**
61+
Set custom word instead of '$inherit'
62+
*/
63+
setJybidInheritWord(word) {
64+
this._jybid_inherit_word = word || '$inherit';
7065
}
71-
// refs to #/... in $inherit/... should be preserved till the end
72-
// because it is what can be body of json-patch operations
73-
const re_local = /^#\//;
74-
inNodes(this._jybid_inherit_word, null, doc, doc, (node) => {
75-
inNodes('$ref', null, doc, node, (_node) => {
76-
if (_node.$ref.match(re_local)) {
77-
//console.error('protectRef', _node)
78-
protectRef(_node);
79-
}
80-
});
81-
});
82-
}
8366

84-
$RefParser.prototype._jybid_processBundled = function (doc) {
85-
inNodes(PROTECTED_REF, null, doc, doc, unprotectRef);
86-
}
67+
_jybid_processResolved(doc) {
68+
if (this._jybid_inherit_word == null) {
69+
this.setJybidInheritWord();
70+
}
71+
// refs to #/... in $inherit/... should be preserved till the end
72+
// because it is what can be body of json-patch operations
73+
const re_local = /^#\//;
74+
inNodes(this._jybid_inherit_word, null, doc, doc, (node) => {
75+
inNodes('$ref', null, doc, node, (_node) => {
76+
if (_node.$ref.match(re_local)) {
77+
//console.error('protectRef', _node)
78+
protectRef(_node);
79+
}
80+
});
81+
});
82+
}
8783

88-
$RefParser.prototype._jybid_resolve = $RefParser.prototype.resolve;
84+
_jybid_processBundled(doc) {
85+
inNodes(PROTECTED_REF, null, doc, doc, unprotectRef);
86+
}
8987

90-
$RefParser.prototype.resolve = function (path, schema, options, callback) {
91-
const promise = this._jybid_resolve(path, schema, options)
92-
.then((resolved) => {
93-
resolved.paths().forEach((_path) => {
94-
const doc = resolved.get(_path);
95-
//console.error('this._jybid_processResolved', JSON.stringify(doc, null, ' '))
96-
if (path) {
97-
this._jybid_processResolved(doc);
98-
}
99-
resolved.set(_path, doc);
88+
resolve(path, schema, options, callback) {
89+
const promise = super.resolve(path, schema, options)
90+
.then((resolved) => {
91+
resolved.paths().forEach((_path) => {
92+
const doc = resolved.get(_path);
93+
//console.error('this._jybid_processResolved', JSON.stringify(doc, null, ' '))
94+
if (path) {
95+
this._jybid_processResolved(doc);
96+
}
97+
resolved.set(_path, doc);
98+
});
99+
//console.error('resolved', JSON.stringify(resolved, null, ' '))
100+
return maybe(null, resolved, callback);
101+
})
102+
.catch((e) => {
103+
e.message = `In resolve(path="${path}") ` + e.message;
104+
maybe(e, null, callback);
100105
});
101-
//console.error('resolved', JSON.stringify(resolved, null, ' '))
102-
return maybe(null, resolved, callback);
103-
})
104-
.catch((e) => {
105-
e.message = `In resolve(path="${path}") ` + e.message;
106-
maybe(e, null, callback);
107-
});
108-
109-
if (callback == null) return promise;
110-
};
106+
107+
if (callback == null) return promise;
108+
}
111109

112-
$RefParser.prototype._jybid_bundle = $RefParser.prototype.bundle;
110+
bundle(path, schema, options, callback) {
111+
const promise = super.bundle(path, schema, options)
112+
.then((bundled) => {
113+
//console.error('bundled', JSON.stringify(bundled, null, ' '))
114+
this._jybid_processBundled(bundled);
115+
return maybe(null, bundled, callback);
116+
})
117+
.catch((e) => {
118+
e.message = `In bundle(path="${path}") ` + e.message;
119+
maybe(e, null, callback);
120+
});
121+
122+
if (callback == null) return promise;
123+
}
124+
}
113125

114-
$RefParser.prototype.bundle = function (path, schema, options, callback) {
115-
const promise = this._jybid_bundle(path, schema, options)
116-
.then((bundled) => {
117-
//console.error('bundled', JSON.stringify(bundled, null, ' '))
118-
this._jybid_processBundled(bundled);
119-
return maybe(null, bundled, callback);
120-
})
121-
.catch((e) => {
122-
e.message = `In bundle(path="${path}") ` + e.message;
123-
maybe(e, null, callback);
124-
});
125-
126-
if (callback == null) return promise;
127-
};
126+
module.exports = PatchedJsonSchemaRefParser

0 commit comments

Comments
 (0)