@hastehaul/common 3.5.0 → 3.7.0
Sign up to get free protection for your applications and to get access to all the features.
- package/build/utils/utils.d.ts +1 -0
- package/build/utils/utils.js +31 -3
- package/package.json +2 -1
package/build/utils/utils.d.ts
CHANGED
@@ -9,6 +9,7 @@ export declare function formatDatetimeToHumanReadable(datetime: Date, format?: s
|
|
9
9
|
export declare function generateIdentifier(messageContent: string): string;
|
10
10
|
export declare function getValueFromSettings(settings: Setting[], key: string, envVar?: string, defaultValue?: any): any;
|
11
11
|
export declare function loadAsset(relativePath: string): Promise<string | null>;
|
12
|
+
export declare function loadAssetStream(relativePath: string): Promise<any>;
|
12
13
|
export declare function isPointInGeoJSON(latLng: number[], fileName: string): Promise<{
|
13
14
|
isInside: boolean;
|
14
15
|
properties: any;
|
package/build/utils/utils.js
CHANGED
@@ -18,13 +18,15 @@ exports.formatDatetimeToHumanReadable = formatDatetimeToHumanReadable;
|
|
18
18
|
exports.generateIdentifier = generateIdentifier;
|
19
19
|
exports.getValueFromSettings = getValueFromSettings;
|
20
20
|
exports.loadAsset = loadAsset;
|
21
|
+
exports.loadAssetStream = loadAssetStream;
|
21
22
|
exports.isPointInGeoJSON = isPointInGeoJSON;
|
22
23
|
exports.clearGeoJsonCache = clearGeoJsonCache;
|
23
24
|
exports.generateCode = generateCode;
|
25
|
+
const fs_1 = __importDefault(require("fs"));
|
24
26
|
const boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
|
25
27
|
const helpers_1 = require("@turf/helpers");
|
26
28
|
const moment_1 = __importDefault(require("moment"));
|
27
|
-
const
|
29
|
+
const fs_2 = require("fs");
|
28
30
|
const boolean_point_on_line_1 = __importDefault(require("@turf/boolean-point-on-line"));
|
29
31
|
function hasElapsed(expirationDate) {
|
30
32
|
const currentDate = (0, moment_1.default)();
|
@@ -53,7 +55,7 @@ function getValueFromSettings(settings, key, envVar, defaultValue) {
|
|
53
55
|
function loadAsset(relativePath) {
|
54
56
|
return __awaiter(this, void 0, void 0, function* () {
|
55
57
|
try {
|
56
|
-
const fileContent = yield
|
58
|
+
const fileContent = yield fs_2.promises.readFile(relativePath, 'utf-8');
|
57
59
|
return fileContent;
|
58
60
|
}
|
59
61
|
catch (error) {
|
@@ -62,6 +64,31 @@ function loadAsset(relativePath) {
|
|
62
64
|
}
|
63
65
|
});
|
64
66
|
}
|
67
|
+
function loadAssetStream(relativePath) {
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
69
|
+
return new Promise((resolve, reject) => {
|
70
|
+
let fileContent = '';
|
71
|
+
const stream = fs_1.default.createReadStream(relativePath, 'utf-8');
|
72
|
+
stream.on('data', (chunk) => {
|
73
|
+
fileContent += chunk;
|
74
|
+
});
|
75
|
+
stream.on('end', () => {
|
76
|
+
try {
|
77
|
+
const geoJsonData = JSON.parse(fileContent);
|
78
|
+
resolve(geoJsonData);
|
79
|
+
}
|
80
|
+
catch (error) {
|
81
|
+
console.error(`Error parsing JSON: ${error.message}`);
|
82
|
+
reject(error);
|
83
|
+
}
|
84
|
+
});
|
85
|
+
stream.on('error', (error) => {
|
86
|
+
console.error(`Error loading asset: ${error.message}`);
|
87
|
+
reject(error);
|
88
|
+
});
|
89
|
+
});
|
90
|
+
});
|
91
|
+
}
|
65
92
|
let cachedGeoJsonData = null;
|
66
93
|
function isPointInGeoJSON(latLng, fileName) {
|
67
94
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -70,7 +97,8 @@ function isPointInGeoJSON(latLng, fileName) {
|
|
70
97
|
return { isInside: false, properties: null };
|
71
98
|
// Load and parse GeoJSON data only if not already cached
|
72
99
|
if (!cachedGeoJsonData) {
|
73
|
-
const geoJsonData = yield
|
100
|
+
const geoJsonData = yield loadAssetStream(fileName);
|
101
|
+
console.log(geoJsonData);
|
74
102
|
cachedGeoJsonData = JSON.parse(geoJsonData);
|
75
103
|
}
|
76
104
|
// !NB points take the form longitude, latitude
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@hastehaul/common",
|
3
|
-
"version": "3.
|
3
|
+
"version": "3.7.0",
|
4
4
|
"description": "",
|
5
5
|
"main": "./build/index.js",
|
6
6
|
"types": "./build/index.d.ts",
|
@@ -34,6 +34,7 @@
|
|
34
34
|
"bullmq": "^5.8.3",
|
35
35
|
"express": "^4.18.2",
|
36
36
|
"ioredis": "^5.3.2",
|
37
|
+
"JSONStream": "^1.3.5",
|
37
38
|
"jsonwebtoken": "^9.0.1",
|
38
39
|
"moment": "^2.29.4",
|
39
40
|
"mongoose": "^8.5.2",
|