event-storage 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -4
- package/index.js +1 -0
- package/package.json +4 -5
- package/src/Consumer.js +16 -20
- package/src/EventStore.js +176 -118
- package/src/EventStream.js +56 -38
- package/src/Index/ReadOnlyIndex.js +1 -1
- package/src/Index/ReadableIndex.js +9 -9
- package/src/Index/WritableIndex.js +6 -10
- package/src/IndexMatcher.js +2 -2
- package/src/JoinEventStream.js +33 -59
- package/src/Partition/ReadOnlyPartition.js +1 -1
- package/src/Partition/ReadablePartition.js +158 -90
- package/src/Partition/WritablePartition.js +38 -29
- package/src/Storage/ReadOnlyStorage.js +4 -4
- package/src/Storage/ReadableStorage.js +81 -113
- package/src/Storage/WritableStorage.js +52 -37
- package/src/Watcher.js +1 -1
- package/src/utils/apiHelpers.js +123 -0
- package/src/{fsUtil.js → utils/fsUtil.js} +27 -23
- package/src/utils/jsonUtil.js +302 -0
- package/src/utils/metadataUtil.js +517 -0
- package/src/{util.js → utils/util.js} +69 -31
- package/src/metadataUtil.js +0 -126
package/src/metadataUtil.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import crypto from 'crypto';
|
|
2
|
-
import { assertEqual } from './util.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Build a buffer containing the file magic header and a JSON stringified metadata block, padded to be a multiple of 16 bytes long.
|
|
6
|
-
*
|
|
7
|
-
* @param {string} magic
|
|
8
|
-
* @param {object} metadata
|
|
9
|
-
* @returns {Buffer} A buffer containing the header data
|
|
10
|
-
*/
|
|
11
|
-
function buildMetadataHeader(magic, metadata) {
|
|
12
|
-
assertEqual(magic.length, 8, 'The header magic bytes length is wrong.');
|
|
13
|
-
let metadataString = JSON.stringify(metadata);
|
|
14
|
-
let metadataSize = Buffer.byteLength(metadataString, 'utf8');
|
|
15
|
-
// 8 byte MAGIC, 4 byte metadata size, 1 byte line break
|
|
16
|
-
const pad = (16 - ((8 + 4 + metadataSize + 1) % 16)) % 16;
|
|
17
|
-
metadataString += ' '.repeat(pad) + "\n";
|
|
18
|
-
metadataSize += pad + 1;
|
|
19
|
-
const metadataBuffer = Buffer.allocUnsafe(8 + 4 + metadataSize);
|
|
20
|
-
metadataBuffer.write(magic, 0, 8, 'utf8');
|
|
21
|
-
metadataBuffer.writeUInt32BE(metadataSize, 8);
|
|
22
|
-
metadataBuffer.write(metadataString, 8 + 4, metadataSize, 'utf8');
|
|
23
|
-
return metadataBuffer;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @param {string} secret The secret to use for calculating further HMACs
|
|
28
|
-
* @returns {function(string)} A function that calculates the HMAC for a given string
|
|
29
|
-
*/
|
|
30
|
-
const createHmac = secret => string => {
|
|
31
|
-
const hmac = crypto.createHmac('sha256', secret);
|
|
32
|
-
hmac.update(string);
|
|
33
|
-
return hmac.digest('hex');
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @typedef {object|function(object):boolean} Matcher
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @param {object} document The document to check against the matcher.
|
|
42
|
-
* @param {Matcher} matcher An object of properties and their values that need to match in the object or a function that checks if the document matches.
|
|
43
|
-
* @returns {boolean} True if the document matches the matcher or false otherwise.
|
|
44
|
-
*/
|
|
45
|
-
function matches(document, matcher) {
|
|
46
|
-
if (typeof document === 'undefined') return false;
|
|
47
|
-
if (typeof matcher === 'undefined') return true;
|
|
48
|
-
|
|
49
|
-
if (typeof matcher === 'function') return matcher(document);
|
|
50
|
-
|
|
51
|
-
for (let prop of Object.getOwnPropertyNames(matcher)) {
|
|
52
|
-
if (Array.isArray(matcher[prop])) {
|
|
53
|
-
if (!matcher[prop].includes(document[prop])) {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
} else if (typeof matcher[prop] === 'object') {
|
|
57
|
-
if (!matches(document[prop], matcher[prop])) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
} else if (typeof matcher[prop] !== 'undefined' && document[prop] !== matcher[prop]) {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @param {Matcher} matcher The matcher object or function that should be serialized.
|
|
69
|
-
* @param {function(string)} hmac A function that calculates a HMAC of the given string.
|
|
70
|
-
* @returns {{matcher: string|object, hmac?: string}}
|
|
71
|
-
*/
|
|
72
|
-
function buildMetadataForMatcher(matcher, hmac) {
|
|
73
|
-
if (!matcher) {
|
|
74
|
-
return undefined;
|
|
75
|
-
}
|
|
76
|
-
if (typeof matcher === 'object') {
|
|
77
|
-
return { matcher };
|
|
78
|
-
}
|
|
79
|
-
const matcherString = matcher.toString();
|
|
80
|
-
return { matcher: matcherString, hmac: hmac(matcherString) };
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* @param {{matcher: string|object, hmac: string}} matcherMetadata The serialized matcher and its HMAC
|
|
85
|
-
* @param {function(string)} hmac A function that calculates a HMAC of the given string.
|
|
86
|
-
* @returns {Matcher} The matcher object or function.
|
|
87
|
-
*/
|
|
88
|
-
function buildMatcherFromMetadata(matcherMetadata, hmac) {
|
|
89
|
-
let matcher;
|
|
90
|
-
if (typeof matcherMetadata.matcher === 'object') {
|
|
91
|
-
matcher = matcherMetadata.matcher;
|
|
92
|
-
} else {
|
|
93
|
-
if (matcherMetadata.hmac !== hmac(matcherMetadata.matcher)) {
|
|
94
|
-
throw new Error('Invalid HMAC for matcher.');
|
|
95
|
-
}
|
|
96
|
-
matcher = eval('(' + matcherMetadata.matcher + ')').bind({}); // jshint ignore:line
|
|
97
|
-
}
|
|
98
|
-
return matcher;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Builds a factory function that, given a type string, returns an object matcher for
|
|
103
|
-
* documents whose payload contains that type at the given dot-notation path.
|
|
104
|
-
*
|
|
105
|
-
* @param {string} payloadPath Dot-notation path relative to the event payload (e.g. `'type'`, `'meta.kind'`).
|
|
106
|
-
* @returns {function(string): object} A function `(typeValue) => objectMatcher`.
|
|
107
|
-
*/
|
|
108
|
-
function buildTypeMatcherFn(payloadPath) {
|
|
109
|
-
const parts = payloadPath.split('.');
|
|
110
|
-
return function(typeValue) {
|
|
111
|
-
let obj = typeValue;
|
|
112
|
-
for (let i = parts.length - 1; i >= 0; i--) {
|
|
113
|
-
obj = { [parts[i]]: obj };
|
|
114
|
-
}
|
|
115
|
-
return { payload: obj };
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export {
|
|
120
|
-
createHmac,
|
|
121
|
-
matches,
|
|
122
|
-
buildMetadataHeader,
|
|
123
|
-
buildMetadataForMatcher,
|
|
124
|
-
buildMatcherFromMetadata,
|
|
125
|
-
buildTypeMatcherFn
|
|
126
|
-
};
|