Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit c476764

Browse files
author
Torsten Link
committed
feat(interceptor): The parser module is now an interceptor
Moved the functionality from a pure parser module into an interceptor BREAKING CHANGE
1 parent a235942 commit c476764

File tree

9 files changed

+150
-26
lines changed

9 files changed

+150
-26
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* jslint node: true, esnext: true */
2+
"use strict";
3+
4+
const Interceptor = require('./lib/interceptor').Interceptor;
5+
6+
exports.Interceptor = Interceptor;
7+
8+
exports.registerWithManager = function (manager) {
9+
manager.registerInterceptor(Interceptor);
10+
};

lib/data-hash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
createFunctions: function (chunkDefinition) {
1313

1414
// all the field names used to build the key for a record and a name to store the hash
15-
const keyHashFields = chunkDefinition.keyFields;
15+
const keyHashFields = chunkDefinition.keyHashFields;
1616
const keyHashName = chunkDefinition.keyHashName;
1717

1818
// all the field names used to build the content hash for a record and a name to store the hash

lib/data-processor-chunk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DataProcessorChunk extends stream.Transform {
4646

4747
if (validationErrors) {
4848
// There where validation errors
49-
this.throw(validationErrors);
49+
throw (validationErrors);
5050
}
5151

5252
}

lib/interceptor.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* jslint node: true, esnext: true */
2+
"use strict";
3+
4+
const Interceptor = require('kronos-interceptor').Interceptor;
5+
const parserFactory = require('./data-processor-chunk');
6+
7+
/**
8+
* This interceptor cares about the handling of the messages.
9+
* It will add the hops and copies the messages
10+
*/
11+
class LineHeaderInterceptor extends Interceptor {
12+
13+
constructor(endpoint, config) {
14+
super(endpoint, config);
15+
16+
this.streamFilter = parserFactory(config, true);
17+
}
18+
19+
static get type() {
20+
return "data-processor-chunk";
21+
}
22+
23+
get type() {
24+
return "data-processor-chunk";
25+
}
26+
27+
receive(request, oldRequest) {
28+
if (request.payload) {
29+
const stream = request.payload;
30+
request.payload = stream.pipe(this.streamFilter);
31+
}
32+
return this.connected.receive(request, oldRequest);
33+
}
34+
}
35+
exports.Interceptor = LineHeaderInterceptor;

package.json

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "stream-object-data-processor-chunk",
2+
"name": "kronos-interceptor-object-data-processor-chunk",
33
"version": "0.0.0",
44
"description": "Splits a line by a sparator into tokens.",
55
"main": "lib/data-processor-chunk.js",
@@ -11,38 +11,37 @@
1111
},
1212
"repository": {
1313
"type": "git",
14-
"url": "git+https://github.com/Kronos-Integration/stream-object-data-processor-chunk.git"
14+
"url": "git+https://github.com/Kronos-Integration/kronos-interceptor-object-data-processor-chunk.git"
1515
},
1616
"bugs": {
17-
"url": "https://github.com/Kronos-Integration/stream-object-data-processor-chunk/issues"
17+
"url": "https://github.com/Kronos-Integration/kronos-interceptor-object-data-processor-chunk/issues"
1818
},
19-
"homepage": "https://github.com/Kronos-Integration/stream-object-data-processor-chunk#readme",
19+
"homepage": "https://github.com/Kronos-Integration/kronos-interceptor-object-data-processor-chunk#readme",
2020
"release": {
2121
"debug": false
2222
},
2323
"dependencies": {
2424
"md5": " 2.0.0",
25+
"kronos-interceptor": "^1.3.0",
2526
"z-schema": "3.16.1"
2627
},
2728
"devDependencies": {
2829
"chai": "3.4.1",
2930
"clone-deep": "0.2.4",
3031
"istanbul": "^0.4.2",
3132
"jsdoc": "3.4.0",
33+
"kronos-test-interceptor": "^1.6.1",
3234
"mocha": "2.3.4",
3335
"semantic-release": "4.3.5",
3436
"underscore": "1.8.3"
3537
},
36-
"contributors": [
37-
{
38-
"name": "Torsten Link",
39-
"email": "torstenlink@gmx.de"
40-
},
41-
{
42-
"name": "Markus Felten",
43-
"email": "markus.felten@gmx.de"
44-
}
45-
],
38+
"contributors": [{
39+
"name": "Torsten Link",
40+
"email": "torstenlink@gmx.de"
41+
}, {
42+
"name": "Markus Felten",
43+
"email": "markus.felten@gmx.de"
44+
}],
4645
"license": "BSD-2-Clause",
4746
"engines": {
4847
"node": ">=4.2.1"

schema/chunk.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
"description": "The import description.",
44
"type": "object",
55
"additionalProperties": false,
6-
"required": ["keyFields", "keyHashName"],
6+
"required": ["keyHashFields", "keyHashName"],
7+
"definitions": {
8+
"severity": {
9+
"type": "string",
10+
"enum": ["abort_file", "abort_scope", "skip_record", "skip_field"]
11+
}
12+
},
713
"properties": {
814

915
"keyHashFields": {
@@ -52,10 +58,10 @@
5258

5359
"checks": {
5460
"double_rows_same": {
55-
"$ref": "#severity"
61+
"$ref": "#/definitions/severity"
5662
},
5763
"double_rows_different": {
58-
"$ref": "#severity"
64+
"$ref": "#/definitions/severity"
5965
}
6066

6167
}

tests/data-hash_test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const distinctData = require('./fixtures/distinct_data.json');
1515

1616

1717
const chunkDefinition = {
18-
"keyFields": ["first", "last"],
18+
"keyHashFields": ["first", "last"],
1919
"keyHashName": "__key",
2020
"contentHashFields": ["first", "last", "friends"],
2121
"contentHashName": "__content",
@@ -25,15 +25,15 @@ const chunkDefinition = {
2525
};
2626

2727
const chunkDefinition_without_scope = {
28-
"keyFields": ["first", "last"],
28+
"keyHashFields": ["first", "last"],
2929
"keyHashName": "__key",
3030
"contentHashFields": ["first", "last", "friends"],
3131
"contentHashName": "__content",
3232
"multiRowFields": ["friends"]
3333
};
3434

3535
const chunkDefinition_keyOnly = {
36-
"keyFields": ["first", "last"],
36+
"keyHashFields": ["first", "last"],
3737
"keyHashName": "__key"
3838
};
3939

@@ -74,7 +74,7 @@ describe("data-processor-chunk:data-hash - Errors", function () {
7474
let error;
7575
try {
7676
dataHashFactory({
77-
"keyFields": ["first", "last"],
77+
"keyHashFields": ["first", "last"],
7878
"keyHashName": "__key",
7979
"contentHashFields": ["last"]
8080
});
@@ -90,7 +90,7 @@ describe("data-processor-chunk:data-hash - Errors", function () {
9090
let error;
9191
try {
9292
dataHashFactory({
93-
"keyFields": ["first", "last"],
93+
"keyHashFields": ["first", "last"],
9494
"keyHashName": "__key",
9595
"scopeHashFields": ["last"]
9696
});

tests/data-processor-chunk_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const chunkProcessor = require('../lib/data-processor-chunk.js');
1414
const objData = require('./fixtures/data.json');
1515

1616
const chunkDefinitionGroupFrinds = {
17-
"keyFields": ["first", "last"],
17+
"keyHashFields": ["first", "last"],
1818
"keyHashName": "__key",
1919
"contentHashFields": ["first", "last", "friends"],
2020
"contentHashName": "__content",
@@ -24,7 +24,7 @@ const chunkDefinitionGroupFrinds = {
2424
};
2525

2626
const chunkDefinitionNoGroup = {
27-
"keyFields": ["first", "last"],
27+
"keyHashFields": ["first", "last"],
2828
"keyHashName": "__key",
2929
"contentHashFields": ["friends", "street"],
3030
"contentHashName": "__content"

tests/interceptor_test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* global describe, it, xit */
2+
/* jslint node: true, esnext: true */
3+
4+
"use strict";
5+
6+
/*
7+
* Just test if the message will be passed through the interceptor
8+
*/
9+
10+
const chai = require('chai'),
11+
assert = chai.assert,
12+
expect = chai.expect,
13+
should = chai.should(),
14+
InterceptorUnderTest = require('../index').Interceptor,
15+
MockReceiveInterceptor = require('kronos-test-interceptor').MockReceiveInterceptor;
16+
17+
18+
const stepMock = {
19+
"name": "dummy step name",
20+
"type": "dummy step type"
21+
};
22+
23+
const checkProperties = {
24+
"keyHashFields": ["first", "last"],
25+
"keyHashName": "__key",
26+
"contentHashFields": ["first", "last", "friends"],
27+
"contentHashName": "__content",
28+
"scopeHashFields": ["street"],
29+
"scopeHashName": "__scope",
30+
"multiRowFields": ["friends"]
31+
};
32+
33+
34+
describe('Interceptor test', function () {
35+
36+
it('Create', function () {
37+
const endpoint = {
38+
"owner": stepMock,
39+
"name": "gumboIn"
40+
};
41+
const messageHandler = new InterceptorUnderTest(endpoint, checkProperties);
42+
assert.ok(messageHandler);
43+
});
44+
45+
it('Send message', function (done) {
46+
const endpoint = {
47+
"owner": stepMock,
48+
"name": "gumboIn"
49+
};
50+
51+
const sendMessage = {
52+
"info": "first message"
53+
};
54+
55+
const messageHandler = new InterceptorUnderTest(endpoint, checkProperties);
56+
const mockReceive = new MockReceiveInterceptor(function (request, oldRequest) {
57+
58+
assert.ok(request);
59+
60+
assert.deepEqual(request, {
61+
"info": "first message"
62+
});
63+
done();
64+
});
65+
66+
messageHandler.connected = mockReceive;
67+
68+
messageHandler.receive(sendMessage);
69+
70+
});
71+
72+
73+
74+
});

0 commit comments

Comments
 (0)