@inrixia/helpers 2.0.3 → 2.0.4
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/package.json +4 -11
- package/src/index.ts +0 -1
- package/src/test.ts +84 -12
- package/src/elastic.ts +0 -141
- package/src/test/matchers.ts +0 -76
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inrixia/helpers",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "Helper functions for various projects",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"types": "index.d.ts",
|
|
7
5
|
"type": "module",
|
|
8
6
|
"scripts": {
|
|
9
|
-
"postinstall": "tsc"
|
|
7
|
+
"postinstall": "tsc",
|
|
8
|
+
"prepublish": "tsc"
|
|
10
9
|
},
|
|
11
10
|
"exports": {
|
|
12
11
|
".": {
|
|
@@ -17,10 +16,6 @@
|
|
|
17
16
|
"types": "./dist/conv.d.ts",
|
|
18
17
|
"import": "./dist/conv.js"
|
|
19
18
|
},
|
|
20
|
-
"./elastic": {
|
|
21
|
-
"types": "./dist/elastic.d.ts",
|
|
22
|
-
"import": "./dist/elastic.js"
|
|
23
|
-
},
|
|
24
19
|
"./geo": {
|
|
25
20
|
"types": "./dist/geo.d.ts",
|
|
26
21
|
"import": "./dist/geo.js"
|
|
@@ -61,11 +56,9 @@
|
|
|
61
56
|
},
|
|
62
57
|
"homepage": "https://github.com/Inrixia/Helpers#readme",
|
|
63
58
|
"devDependencies": {
|
|
64
|
-
"@types/elasticsearch": "^5.0.40",
|
|
65
59
|
"@types/node": "^18.7.18",
|
|
66
60
|
"@typescript-eslint/eslint-plugin": "^5.37.0",
|
|
67
61
|
"eslint": "^8.23.1",
|
|
68
|
-
"typescript": "^4.8.3"
|
|
69
|
-
"vitest": "^0.23.4"
|
|
62
|
+
"typescript": "^4.8.3"
|
|
70
63
|
}
|
|
71
64
|
}
|
package/src/index.ts
CHANGED
package/src/test.ts
CHANGED
|
@@ -1,14 +1,86 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ValueOfA, Primitives, Primitive, UnknownRecord, Constructor } from "./ts.js";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
stringMatching<T extends string>(expected: T): T;
|
|
13
|
-
}
|
|
3
|
+
interface Expect {
|
|
4
|
+
any<C>(constructor: C extends Constructor ? C : any): C extends Constructor ? Primitive<C> : C;
|
|
5
|
+
arrayContaining<T = unknown>(expected: Array<T>): T[];
|
|
6
|
+
objectContaining<T = unknown>(expected: T): T;
|
|
7
|
+
stringMatching<T extends string>(expected: T): T;
|
|
8
|
+
}
|
|
9
|
+
interface ExpectGeneric extends Expect {
|
|
10
|
+
(actual: unknown, message?: string): any;
|
|
11
|
+
extend(matchers: unknown): void;
|
|
14
12
|
}
|
|
13
|
+
|
|
14
|
+
export type Matchers = {
|
|
15
|
+
typeOrNull: <T extends Primitives>(matcher: T) => Primitive<T> | null;
|
|
16
|
+
typeOrUndefined: <T extends Primitives>(matcher: T) => Primitive<T> | undefined;
|
|
17
|
+
objectContainingOrEmpty: <T>(matcher: T) => T | UnknownRecord;
|
|
18
|
+
objectContainingOrUndefined: <T>(matcher: T) => T | undefined;
|
|
19
|
+
objectContainingOrNull: <T>(matcher: T) => T | null;
|
|
20
|
+
objectContainingOrEmptyOrNullish: <T>(matcher: T) => T | UnknownRecord | undefined | null;
|
|
21
|
+
keylessObjectContaining: <T>(matcher: T) => Record<string, T>;
|
|
22
|
+
arrayContainingOrEmpty: <T>(matcher: [T]) => T[] | [];
|
|
23
|
+
arrayContainingOrEmptyOrUndefined: <T>(matcher: [T]) => T[] | [] | undefined;
|
|
24
|
+
enum: <T extends readonly unknown[]>(matcher: T) => ValueOfA<T>;
|
|
25
|
+
};
|
|
26
|
+
const OK = {
|
|
27
|
+
message: () => "Ok",
|
|
28
|
+
pass: true,
|
|
29
|
+
};
|
|
30
|
+
export type EExpect<T> = Expect & Matchers & T;
|
|
31
|
+
export const getExpect = <T extends ExpectGeneric>(expect: T): EExpect<T> => {
|
|
32
|
+
const matchers: Record<keyof Matchers, any> = {
|
|
33
|
+
typeOrNull(received: unknown, argument: unknown) {
|
|
34
|
+
if (received === null) return OK;
|
|
35
|
+
expect(received).toStrictEqual(expect.any(argument));
|
|
36
|
+
return OK;
|
|
37
|
+
},
|
|
38
|
+
typeOrUndefined(received: unknown, argument: unknown) {
|
|
39
|
+
if (received === undefined) return OK;
|
|
40
|
+
expect(received).toStrictEqual(expect.any(argument));
|
|
41
|
+
return OK;
|
|
42
|
+
},
|
|
43
|
+
objectContainingOrEmpty(received: unknown[], argument: unknown) {
|
|
44
|
+
if (Object.keys(received).length === 0) return OK;
|
|
45
|
+
expect(received).toStrictEqual(expect.objectContaining(argument));
|
|
46
|
+
return OK;
|
|
47
|
+
},
|
|
48
|
+
objectContainingOrUndefined(received: unknown, argument: unknown) {
|
|
49
|
+
if (received === undefined) return OK;
|
|
50
|
+
expect(received).toStrictEqual(expect.objectContaining(argument));
|
|
51
|
+
return OK;
|
|
52
|
+
},
|
|
53
|
+
objectContainingOrNull(received: unknown, argument: unknown) {
|
|
54
|
+
if (received === null) return OK;
|
|
55
|
+
expect(received).toStrictEqual(expect.objectContaining(argument));
|
|
56
|
+
return OK;
|
|
57
|
+
},
|
|
58
|
+
objectContainingOrEmptyOrNullish(received: unknown, argument: unknown) {
|
|
59
|
+
if (received === undefined || received === null || Object.keys(received).length === 0) return OK;
|
|
60
|
+
expect(received).toStrictEqual(expect.objectContaining(argument));
|
|
61
|
+
return OK;
|
|
62
|
+
},
|
|
63
|
+
keylessObjectContaining(received: UnknownRecord, argument: unknown) {
|
|
64
|
+
for (const key in received) {
|
|
65
|
+
expect(received[key]).toStrictEqual(argument);
|
|
66
|
+
}
|
|
67
|
+
return OK;
|
|
68
|
+
},
|
|
69
|
+
arrayContainingOrEmpty(received: unknown[], argument: any[]) {
|
|
70
|
+
if (received.length === 0) return OK;
|
|
71
|
+
expect(received).toStrictEqual(expect.arrayContaining(argument));
|
|
72
|
+
return OK;
|
|
73
|
+
},
|
|
74
|
+
arrayContainingOrEmptyOrUndefined(received: unknown[], argument: unknown[]) {
|
|
75
|
+
if (received === undefined || received.length === 0) return OK;
|
|
76
|
+
expect(received).toStrictEqual(expect.arrayContaining(argument));
|
|
77
|
+
return OK;
|
|
78
|
+
},
|
|
79
|
+
enum(received: unknown, argument: unknown[]) {
|
|
80
|
+
expect(argument).toContain(received);
|
|
81
|
+
return OK;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
expect.extend(matchers);
|
|
85
|
+
return <EExpect<T>>expect;
|
|
86
|
+
};
|
package/src/elastic.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import type { Client, SearchParams, SearchResponse, IndicesCreateParams } from "elasticsearch";
|
|
2
|
-
import type { UnknownRecord } from "./ts.js";
|
|
3
|
-
|
|
4
|
-
import { EventEmitter } from "events";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Indexes a large amount of entities in a single query
|
|
8
|
-
* @param entities Array containing entities to index
|
|
9
|
-
* @param indexName Name of index to index entities.
|
|
10
|
-
*
|
|
11
|
-
* @returns Promise that resolves time taken to index entities in ms.
|
|
12
|
-
*/
|
|
13
|
-
export const bulkIndex = async (elastic: Client, entities: UnknownRecord[], indexName: string, refresh = false): Promise<number> =>
|
|
14
|
-
elastic.bulk({
|
|
15
|
-
refresh: refresh,
|
|
16
|
-
body: entities.flatMap((entity) => [{ index: { _index: indexName } }, entity]),
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Queries elastic using `params`.
|
|
21
|
-
* @param params Query object.
|
|
22
|
-
*
|
|
23
|
-
* @returns Iterator that yeilds a result until query exhausted.
|
|
24
|
-
*/
|
|
25
|
-
export async function* scrollSearch(elastic: Client, params: SearchParams): AsyncGenerator<SearchResponse<unknown>> {
|
|
26
|
-
if (params.scroll === undefined) throw new Error("params.scroll is required!");
|
|
27
|
-
let response = await elastic.search(params);
|
|
28
|
-
let nextResponse;
|
|
29
|
-
while (true) {
|
|
30
|
-
nextResponse = elastic.scroll({
|
|
31
|
-
scrollId: response._scroll_id!,
|
|
32
|
-
scroll: params.scroll,
|
|
33
|
-
});
|
|
34
|
-
if (!response._scroll_id) break;
|
|
35
|
-
if (response.hits.hits.length === 0) break;
|
|
36
|
-
yield response;
|
|
37
|
-
response = await nextResponse;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Function to recreate a elasticsearch index.
|
|
43
|
-
* @param elastic Elasticsearch client
|
|
44
|
-
* @param createParams Elastic parameters for creation of index
|
|
45
|
-
* @param options Options for creation
|
|
46
|
-
*/
|
|
47
|
-
export const createIndex = async (
|
|
48
|
-
elastic: Client,
|
|
49
|
-
createParams: IndicesCreateParams,
|
|
50
|
-
options: { logProgress?: boolean; destoryIfExists: boolean }
|
|
51
|
-
): Promise<void> => {
|
|
52
|
-
if (!options) throw new Error("Options required!");
|
|
53
|
-
if (!createParams) throw new Error("Elastic parameters required! (createParams)");
|
|
54
|
-
if (!options.logProgress) options.logProgress = false;
|
|
55
|
-
if (options.destoryIfExists) {
|
|
56
|
-
try {
|
|
57
|
-
await elastic.indices.exists({ index: createParams.index });
|
|
58
|
-
} catch (err) {
|
|
59
|
-
if (err instanceof Error) err.message = "Failed to check if old index exists!\n" + err.message;
|
|
60
|
-
throw err;
|
|
61
|
-
}
|
|
62
|
-
if (options.logProgress) process.stdout.write(`Deleting index "${createParams.index}"...`);
|
|
63
|
-
try {
|
|
64
|
-
await elastic.indices.delete({ index: createParams.index });
|
|
65
|
-
} catch (err) {
|
|
66
|
-
if (err instanceof Error) err.message = "Failed to delete old index!\n" + err.message;
|
|
67
|
-
throw err;
|
|
68
|
-
}
|
|
69
|
-
if (options.logProgress) process.stdout.write(` Deleted!\nCreating index "${createParams.index}"...`);
|
|
70
|
-
try {
|
|
71
|
-
await elastic.indices.create(createParams);
|
|
72
|
-
} catch (err) {
|
|
73
|
-
if (err instanceof Error) err.message = "Failed to create new index!\n" + err.message;
|
|
74
|
-
throw err;
|
|
75
|
-
}
|
|
76
|
-
if (options.logProgress) process.stdout.write(" Created!\n");
|
|
77
|
-
} else {
|
|
78
|
-
if (options.logProgress) process.stdout.write(`Creating index "${createParams.index}"...`);
|
|
79
|
-
try {
|
|
80
|
-
await elastic.indices.create(createParams);
|
|
81
|
-
} catch (err) {
|
|
82
|
-
if (err instanceof Error) err.message = "Failed to create new index!\n" + err.message;
|
|
83
|
-
throw err;
|
|
84
|
-
}
|
|
85
|
-
if (options.logProgress) process.stdout.write(" Created!\n");
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Returns scrollsearch results in simulated time based on when result timestamp.
|
|
91
|
-
* @param elastic Elasticsearch client to use
|
|
92
|
-
* @param query Query object to use for scroll search
|
|
93
|
-
* @param timestampKey Key to use for fetching timestamp from objects
|
|
94
|
-
* @param options.timescale Rate at which "time" progreesses
|
|
95
|
-
* @returns EventEmitter which emits 'entity' events containing resulting hits
|
|
96
|
-
* @example
|
|
97
|
-
* const elasticClient = new ElasticClient({ node: 'http://127.0.0.1:9200' })
|
|
98
|
-
* const timestamp = "reportedTime"
|
|
99
|
-
* const query = {
|
|
100
|
-
* index: "documents_index",
|
|
101
|
-
* scroll: '10m',
|
|
102
|
-
* body: {
|
|
103
|
-
* "query": { "match_all": {} },
|
|
104
|
-
* "sort": [{ [timestamp]: {"order": "asc"} }],
|
|
105
|
-
* "size": 10000
|
|
106
|
-
* }
|
|
107
|
-
* }
|
|
108
|
-
* stream(elasticClient, query, timestamp, 2).on('entity', console.log) // <- Emitted results based on time moving at 2x normal speed
|
|
109
|
-
*/
|
|
110
|
-
export const stream = (
|
|
111
|
-
elastic: Client,
|
|
112
|
-
query: SearchParams,
|
|
113
|
-
timestampKey: string,
|
|
114
|
-
options: { timescale: number; logProgress: boolean } = { timescale: 1, logProgress: false }
|
|
115
|
-
): EventEmitter => {
|
|
116
|
-
const entityReceiver = new EventEmitter();
|
|
117
|
-
(async () => {
|
|
118
|
-
if (options.timescale === undefined) options.timescale = 1;
|
|
119
|
-
if (typeof options.timescale !== "number") throw new Error("options.timescale must be a number!");
|
|
120
|
-
if (timestampKey === undefined) throw new Error("timestampKey is undefined!");
|
|
121
|
-
let timeOffset: number | undefined;
|
|
122
|
-
let startDate = Date.now();
|
|
123
|
-
|
|
124
|
-
if (options.logProgress) console.log("Beginning elastic scroll for entity streaming...");
|
|
125
|
-
for await (const result of scrollSearch(elastic, query) as AsyncGenerator<SearchResponse<{ [timestampKey: string]: number }>>) {
|
|
126
|
-
if (options.logProgress) console.log(`Query iteration finished, streaming ${result.hits.hits.length} entities.`);
|
|
127
|
-
if (timeOffset === undefined) {
|
|
128
|
-
timeOffset = Date.now() - new Date(result.hits.hits[0]._source[timestampKey]).getTime();
|
|
129
|
-
startDate = Date.now();
|
|
130
|
-
}
|
|
131
|
-
for (const entity of result.hits.hits) {
|
|
132
|
-
const offsetEntityTime = new Date(entity._source[timestampKey]).getTime() + timeOffset;
|
|
133
|
-
while (Date.now() + (Date.now() - startDate) * options.timescale < offsetEntityTime) {
|
|
134
|
-
// Spin while waiting to emit next entity
|
|
135
|
-
}
|
|
136
|
-
entityReceiver.emit("entity", entity._source);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
})();
|
|
140
|
-
return entityReceiver;
|
|
141
|
-
};
|
package/src/test/matchers.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import type { expect } from "vitest";
|
|
2
|
-
|
|
3
|
-
import type { ValueOfA, Primitives, Primitive, UnknownRecord } from "../ts.js";
|
|
4
|
-
|
|
5
|
-
export type Matchers = {
|
|
6
|
-
typeOrNull: <T extends Primitives>(matcher: T) => Primitive<T> | null;
|
|
7
|
-
typeOrUndefined: <T extends Primitives>(matcher: T) => Primitive<T> | undefined;
|
|
8
|
-
objectContainingOrEmpty: <T>(matcher: T) => T | UnknownRecord;
|
|
9
|
-
objectContainingOrUndefined: <T>(matcher: T) => T | undefined;
|
|
10
|
-
objectContainingOrNull: <T>(matcher: T) => T | null;
|
|
11
|
-
objectContainingOrEmptyOrNullish: <T>(matcher: T) => T | UnknownRecord | undefined | null;
|
|
12
|
-
keylessObjectContaining: <T>(matcher: T) => Record<string, T>;
|
|
13
|
-
arrayContainingOrEmpty: <T>(matcher: [T]) => T[] | [];
|
|
14
|
-
arrayContainingOrEmptyOrUndefined: <T>(matcher: [T]) => T[] | [] | undefined;
|
|
15
|
-
enum: <T extends readonly unknown[]>(matcher: T) => ValueOfA<T>;
|
|
16
|
-
};
|
|
17
|
-
const OK = {
|
|
18
|
-
message: () => "Ok",
|
|
19
|
-
pass: true,
|
|
20
|
-
};
|
|
21
|
-
export const getExpect = (expect: Vi.ExpectStatic): Vi.ExpectStatic & Matchers => {
|
|
22
|
-
const matchers: Record<keyof Matchers, any> = {
|
|
23
|
-
typeOrNull(received: unknown, argument: unknown) {
|
|
24
|
-
if (received === null) return OK;
|
|
25
|
-
expect(received).toStrictEqual(expect.any(argument));
|
|
26
|
-
return OK;
|
|
27
|
-
},
|
|
28
|
-
typeOrUndefined(received: unknown, argument: unknown) {
|
|
29
|
-
if (received === undefined) return OK;
|
|
30
|
-
expect(received).toStrictEqual(expect.any(argument));
|
|
31
|
-
return OK;
|
|
32
|
-
},
|
|
33
|
-
objectContainingOrEmpty(received: unknown[], argument: unknown) {
|
|
34
|
-
if (Object.keys(received).length === 0) return OK;
|
|
35
|
-
expect(received).toStrictEqual(expect.objectContaining(argument));
|
|
36
|
-
return OK;
|
|
37
|
-
},
|
|
38
|
-
objectContainingOrUndefined(received: unknown, argument: unknown) {
|
|
39
|
-
if (received === undefined) return OK;
|
|
40
|
-
expect(received).toStrictEqual(expect.objectContaining(argument));
|
|
41
|
-
return OK;
|
|
42
|
-
},
|
|
43
|
-
objectContainingOrNull(received: unknown, argument: unknown) {
|
|
44
|
-
if (received === null) return OK;
|
|
45
|
-
expect(received).toStrictEqual(expect.objectContaining(argument));
|
|
46
|
-
return OK;
|
|
47
|
-
},
|
|
48
|
-
objectContainingOrEmptyOrNullish(received: unknown, argument: unknown) {
|
|
49
|
-
if (received === undefined || received === null || Object.keys(received).length === 0) return OK;
|
|
50
|
-
expect(received).toStrictEqual(expect.objectContaining(argument));
|
|
51
|
-
return OK;
|
|
52
|
-
},
|
|
53
|
-
keylessObjectContaining(received: UnknownRecord, argument: unknown) {
|
|
54
|
-
for (const key in received) {
|
|
55
|
-
expect(received[key]).toStrictEqual(argument);
|
|
56
|
-
}
|
|
57
|
-
return OK;
|
|
58
|
-
},
|
|
59
|
-
arrayContainingOrEmpty(received: unknown[], argument: any[]) {
|
|
60
|
-
if (received.length === 0) return OK;
|
|
61
|
-
expect(received).toStrictEqual(expect.arrayContaining(argument));
|
|
62
|
-
return OK;
|
|
63
|
-
},
|
|
64
|
-
arrayContainingOrEmptyOrUndefined(received: unknown[], argument: unknown[]) {
|
|
65
|
-
if (received === undefined || received.length === 0) return OK;
|
|
66
|
-
expect(received).toStrictEqual(expect.arrayContaining(argument));
|
|
67
|
-
return OK;
|
|
68
|
-
},
|
|
69
|
-
enum(received: unknown, argument: unknown[]) {
|
|
70
|
-
expect(argument).toContain(received);
|
|
71
|
-
return OK;
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
expect.extend(matchers);
|
|
75
|
-
return <Vi.ExpectStatic & Matchers>expect;
|
|
76
|
-
};
|