@saber-usa/node-common 1.7.7-alpha.2 → 1.7.7

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/src/s3.js CHANGED
@@ -1,59 +1,59 @@
1
- import {S3Client, PutObjectCommand, HeadObjectCommand} from "@aws-sdk/client-s3";
2
- import {readFileSync} from "fs";
3
- import {basename} from "path";
4
- import {config} from "dotenv";
5
- config();
6
-
7
- const s3Client = new S3Client({
8
- region: "us-west-1", // Replace with your AWS region
9
- credentials: {
10
- accessKeyId: process.env.S3_ACCESS_KEY,
11
- secretAccessKey: process.env.S3_SECRET,
12
- },
13
- });
14
-
15
-
16
- /**
17
- * Takes either a filename, or a buffer. If array buffer is supplied, then filename is just used to name the file.
18
- * If no array buffer is supplied, it will attempt to read the file at the fileName path.
19
- * Example Usage:
20
- * await s3Upload("imageName.png", "image/png", imageBuffer);
21
- * await s3Upload("/path/to/local/image.png");
22
- * @param {string} fileName
23
- * @param {string} type
24
- * @param {Array|Buffer|null} buffer
25
- * @return {Promise<Object>}
26
- */
27
- export async function s3Upload(fileName, type = "image/png", buffer = null) {
28
- try {
29
- const fileContent = buffer ? buffer : readFileSync(fileName);
30
- return await s3Client.send(new PutObjectCommand({
31
- Bucket: "saber-probe-images", // S3 Bucket name
32
- Key: buffer ? fileName : basename(fileName), // File name to save as in S3
33
- Body: fileContent, // File content (either buffer or file read)
34
- ContentType: type, // File content type
35
- }));
36
- } catch (err) {
37
- console.error("Error uploading file or buffer to S3: ", err);
38
- throw err; // Re-throw the error so it can be caught by the calling function if needed
39
- }
40
- }
41
-
42
- export async function doesS3ObjectExist(fileName) {
43
- try {
44
- // Try to fetch the metadata of the object to check if it exists
45
- await s3Client.send(new HeadObjectCommand({
46
- Bucket: "saber-probe-images",
47
- Key: fileName, // The key (file name) to check in the S3 bucket
48
- }));
49
- return true;
50
- } catch (err) {
51
- // If a 404 error is thrown, the object does not exist
52
- if (err.name === "NotFound") {
53
- return false;
54
- }
55
- // Other errors (such as permission errors) should be thrown
56
- console.error("Error checking if file exists in S3: ", err);
57
- throw err;
58
- }
59
- }
1
+ import {S3Client, PutObjectCommand, HeadObjectCommand} from "@aws-sdk/client-s3";
2
+ import {readFileSync} from "fs";
3
+ import {basename} from "path";
4
+ import {config} from "dotenv";
5
+ config();
6
+
7
+ const s3Client = new S3Client({
8
+ region: "us-west-1", // Replace with your AWS region
9
+ credentials: {
10
+ accessKeyId: process.env.S3_ACCESS_KEY,
11
+ secretAccessKey: process.env.S3_SECRET,
12
+ },
13
+ });
14
+
15
+
16
+ /**
17
+ * Takes either a filename, or a buffer. If array buffer is supplied, then filename is just used to name the file.
18
+ * If no array buffer is supplied, it will attempt to read the file at the fileName path.
19
+ * Example Usage:
20
+ * await s3Upload("imageName.png", "image/png", imageBuffer);
21
+ * await s3Upload("/path/to/local/image.png");
22
+ * @param {string} fileName
23
+ * @param {string} type
24
+ * @param {Array|Buffer|null} buffer
25
+ * @return {Promise<Object>}
26
+ */
27
+ export async function s3Upload(fileName, type = "image/png", buffer = null) {
28
+ try {
29
+ const fileContent = buffer ? buffer : readFileSync(fileName);
30
+ return await s3Client.send(new PutObjectCommand({
31
+ Bucket: "saber-probe-images", // S3 Bucket name
32
+ Key: buffer ? fileName : basename(fileName), // File name to save as in S3
33
+ Body: fileContent, // File content (either buffer or file read)
34
+ ContentType: type, // File content type
35
+ }));
36
+ } catch (err) {
37
+ console.error("Error uploading file or buffer to S3: ", err);
38
+ throw err; // Re-throw the error so it can be caught by the calling function if needed
39
+ }
40
+ }
41
+
42
+ export async function doesS3ObjectExist(fileName) {
43
+ try {
44
+ // Try to fetch the metadata of the object to check if it exists
45
+ await s3Client.send(new HeadObjectCommand({
46
+ Bucket: "saber-probe-images",
47
+ Key: fileName, // The key (file name) to check in the S3 bucket
48
+ }));
49
+ return true;
50
+ } catch (err) {
51
+ // If a 404 error is thrown, the object does not exist
52
+ if (err.name === "NotFound") {
53
+ return false;
54
+ }
55
+ // Other errors (such as permission errors) should be thrown
56
+ console.error("Error checking if file exists in S3: ", err);
57
+ throw err;
58
+ }
59
+ }
package/src/transform.js CHANGED
@@ -1,35 +1,35 @@
1
- import _ from "lodash";
2
-
3
- const transformObjectKeys = (transform, object, deep = true) => _.cond([
4
- [
5
- (transformItem) => _.isPlainObject(transformItem),
6
- (transformItem) => _.fromPairs(
7
- _.map(
8
- _.toPairs(transformItem),
9
- ([key, value]) => [
10
- transform(key),
11
- deep && _.isPlainObject(value)
12
- ? transformObjectKeys(transform, value)
13
- : value,
14
- ],
15
- ),
16
- ),
17
- ],
18
- [_.stubTrue, _.identity],
19
- ])(object);
20
-
21
- const pascalCase = (string) => _.upperFirst(_.camelCase(string));
22
-
23
- const lowerCaseObjectKeys = (transformItem, deep) => transformObjectKeys(
24
- _.toLower,
25
- transformItem,
26
- deep,
27
- );
28
-
29
- const pascalCaseObjectKeys = (transformItem, deep) => transformObjectKeys(
30
- pascalCase,
31
- transformItem,
32
- deep,
33
- );
34
-
35
- export {transformObjectKeys, lowerCaseObjectKeys, pascalCaseObjectKeys, pascalCase};
1
+ import _ from "lodash";
2
+
3
+ const transformObjectKeys = (transform, object, deep = true) => _.cond([
4
+ [
5
+ (transformItem) => _.isPlainObject(transformItem),
6
+ (transformItem) => _.fromPairs(
7
+ _.map(
8
+ _.toPairs(transformItem),
9
+ ([key, value]) => [
10
+ transform(key),
11
+ deep && _.isPlainObject(value)
12
+ ? transformObjectKeys(transform, value)
13
+ : value,
14
+ ],
15
+ ),
16
+ ),
17
+ ],
18
+ [_.stubTrue, _.identity],
19
+ ])(object);
20
+
21
+ const pascalCase = (string) => _.upperFirst(_.camelCase(string));
22
+
23
+ const lowerCaseObjectKeys = (transformItem, deep) => transformObjectKeys(
24
+ _.toLower,
25
+ transformItem,
26
+ deep,
27
+ );
28
+
29
+ const pascalCaseObjectKeys = (transformItem, deep) => transformObjectKeys(
30
+ pascalCase,
31
+ transformItem,
32
+ deep,
33
+ );
34
+
35
+ export {transformObjectKeys, lowerCaseObjectKeys, pascalCaseObjectKeys, pascalCase};
package/src/udl.js CHANGED
@@ -1,116 +1,116 @@
1
- import {fixDate} from "./fixDate.js";
2
- import {calcRegime, getElsetUdlFromTle, getLonAndDrift, getRaanPrecession} from "./astro.js";
3
- import {lowerCaseObjectKeys} from "./transform.js";
4
- import {isDefined} from "./utils.js";
5
- import _ from "lodash";
6
-
7
- const udlToNpsElset = (udlRow) => {
8
- let derivedElset;
9
- try {
10
- derivedElset = getElsetUdlFromTle(udlRow.line1, udlRow.line2, udlRow.dataMode);
11
- } catch {
12
- // invalid TLE
13
- derivedElset = null;
14
- }
15
- const enrichedUdlFields = [];
16
- const enrichField = (field) => {
17
- if (derivedElset && (derivedElset[field] || derivedElset[field] === 0)) {
18
- enrichedUdlFields.push(field);
19
- return derivedElset[field];
20
- }
21
- return null;
22
- };
23
- const lonAndDrift = getLonAndDrift(udlRow.line1, udlRow.line2);
24
-
25
- const getElsetValue = (value, defaultValueFunc) => {
26
- return (isDefined(value)) ? value : defaultValueFunc();
27
- };
28
-
29
- const npsElset = lowerCaseObjectKeys({
30
- Satno: getElsetValue(udlRow.satNo, ()=>enrichField("satNo")),
31
- Epoch: fixDate(getElsetValue(udlRow.epoch, ()=>enrichField("epoch"))),
32
- MeanMotion: getElsetValue(udlRow.meanMotion, ()=>enrichField("meanMotion")),
33
- Eccentricity: getElsetValue(udlRow.eccentricity, ()=>enrichField("eccentricity")),
34
- Inclination: getElsetValue(udlRow.inclination, ()=>enrichField("inclination")),
35
- Raan: getElsetValue(udlRow.raan, ()=>enrichField("raan")),
36
- ArgOfPerigee: getElsetValue(udlRow.argOfPerigee, ()=>enrichField("argOfPerigee")),
37
- MeanAnomaly: getElsetValue(udlRow.meanAnomaly, ()=>enrichField("meanAnomaly")),
38
- BStar: getElsetValue(udlRow.bStar, ()=>enrichField("bStar")),
39
- SemiMajorAxis: getElsetValue(udlRow.semiMajorAxis, ()=>enrichField("semiMajorAxis")),
40
- Period: getElsetValue(udlRow.period, ()=>enrichField("period")),
41
- Apogee: getElsetValue(udlRow.apogee, ()=>enrichField("apogee")),
42
- Perigee: getElsetValue(udlRow.perigee, ()=>enrichField("perigee")),
43
- Line1: getElsetValue(udlRow.line1, ()=>null),
44
- Line2: getElsetValue(udlRow.line2, ()=>null),
45
- Source: getElsetValue(udlRow.source, ()=>null),
46
- DataMode: getElsetValue(udlRow.dataMode, ()=>null),
47
- CreatedAt: fixDate(getElsetValue(udlRow.createdAt, ()=>null)),
48
- Origin: getElsetValue(udlRow.origin, ()=>null),
49
- Algorithm: getElsetValue(udlRow.algorithm, ()=>null),
50
- Descriptor: getElsetValue(udlRow.descriptor, ()=>null),
51
- ClassificationMarking: getElsetValue(udlRow.classificationMarking, ()=>null),
52
- Latitude: lonAndDrift.latitude,
53
- Longitude: lonAndDrift.longitude,
54
- LonDriftDegreesPerDay: lonAndDrift.lonDriftDegreesPerDay,
55
- RaanPrecessionDegreesPerDay: getRaanPrecession(udlRow.line1, udlRow.line2),
56
- // Ensure we reset all the derived calcs so we know if they have been updated by Sentinel or not.
57
- MinEccentricity: 0,
58
- MaxEccentricity: 0,
59
- MinInclination: 0,
60
- MaxInclination: 0,
61
- MinRaan: 0,
62
- MaxRaan: 0,
63
- MinArgOfPerigee: 0,
64
- MaxArgOfPerigee: 0,
65
- MinSemiMajorAxis: 0,
66
- MaxSemiMajorAxis: 0,
67
- });
68
- npsElset.regime = calcRegime(npsElset);
69
- npsElset.enrichedudlfields = enrichedUdlFields.join(",");
70
- return npsElset;
71
- };
72
-
73
- const udlToNpsGroundSite = (udlRow) => ({
74
- SensorId: _.get(udlRow, "idSensor", null),
75
- Number: _.get(udlRow, "sensorNumber", null),
76
- Name: _.get(udlRow, "sensorName", null),
77
- Source: _.get(udlRow, "source", null),
78
- Description: null,
79
- LastObsTime: _.get(udlRow, "sensorStats[0].lastObTime", null),
80
- CountryCode: _.get(udlRow, "entity.countryCode", "None"),
81
- Taskable: _.get(udlRow, "entity.taskable", 0),
82
- Type: _.get(udlRow, "sensorType.id", 0),
83
- Latitude: _.get(udlRow, "entity.location.lat", 0),
84
- Longitude: _.get(udlRow, "entity.location.lon", 0),
85
- KmAboveSeaLevel: _.get(udlRow, "entity.location.altitude", 0),
86
- MinRangeKm: _.get(udlRow, "sensorcharacteristics[0].minRangeLimit", null),
87
- MaxRangeKm: _.get(udlRow, "sensorcharacteristics[0].maxRangeLimit", null),
88
- ClassificationLevel: _.get(
89
- udlRow,
90
- "sensorcharacteristics[0].classificationMarking",
91
- "",
92
- ),
93
- createdAt: fixDate(_.get(udlRow, "createdAt")),
94
- });
95
-
96
- /**
97
- * Formats the response data based on the topic
98
- *
99
- * @param {String} topic
100
- * @param {Object[]} udlData
101
- * @return {Object[]}
102
- */
103
- export function formatUdlData(topic, udlData) {
104
- switch (topic) {
105
- case "Elsets":
106
- return _.map(udlData, udlToNpsElset);
107
-
108
- case "GroundSites":
109
- return _.map(udlData, udlToNpsGroundSite);
110
-
111
- default:
112
- return udlData;
113
- }
114
- }
115
-
116
- export {udlToNpsElset, udlToNpsGroundSite};
1
+ import {fixDate} from "./fixDate.js";
2
+ import {calcRegime, getElsetUdlFromTle, getLonAndDrift, getRaanPrecession} from "./astro.js";
3
+ import {lowerCaseObjectKeys} from "./transform.js";
4
+ import {isDefined} from "./utils.js";
5
+ import _ from "lodash";
6
+
7
+ const udlToNpsElset = (udlRow) => {
8
+ let derivedElset;
9
+ try {
10
+ derivedElset = getElsetUdlFromTle(udlRow.line1, udlRow.line2, udlRow.dataMode);
11
+ } catch {
12
+ // invalid TLE
13
+ derivedElset = null;
14
+ }
15
+ const enrichedUdlFields = [];
16
+ const enrichField = (field) => {
17
+ if (derivedElset && (derivedElset[field] || derivedElset[field] === 0)) {
18
+ enrichedUdlFields.push(field);
19
+ return derivedElset[field];
20
+ }
21
+ return null;
22
+ };
23
+ const lonAndDrift = getLonAndDrift(udlRow.line1, udlRow.line2);
24
+
25
+ const getElsetValue = (value, defaultValueFunc) => {
26
+ return (isDefined(value)) ? value : defaultValueFunc();
27
+ };
28
+
29
+ const npsElset = lowerCaseObjectKeys({
30
+ Satno: getElsetValue(udlRow.satNo, ()=>enrichField("satNo")),
31
+ Epoch: fixDate(getElsetValue(udlRow.epoch, ()=>enrichField("epoch"))),
32
+ MeanMotion: getElsetValue(udlRow.meanMotion, ()=>enrichField("meanMotion")),
33
+ Eccentricity: getElsetValue(udlRow.eccentricity, ()=>enrichField("eccentricity")),
34
+ Inclination: getElsetValue(udlRow.inclination, ()=>enrichField("inclination")),
35
+ Raan: getElsetValue(udlRow.raan, ()=>enrichField("raan")),
36
+ ArgOfPerigee: getElsetValue(udlRow.argOfPerigee, ()=>enrichField("argOfPerigee")),
37
+ MeanAnomaly: getElsetValue(udlRow.meanAnomaly, ()=>enrichField("meanAnomaly")),
38
+ BStar: getElsetValue(udlRow.bStar, ()=>enrichField("bStar")),
39
+ SemiMajorAxis: getElsetValue(udlRow.semiMajorAxis, ()=>enrichField("semiMajorAxis")),
40
+ Period: getElsetValue(udlRow.period, ()=>enrichField("period")),
41
+ Apogee: getElsetValue(udlRow.apogee, ()=>enrichField("apogee")),
42
+ Perigee: getElsetValue(udlRow.perigee, ()=>enrichField("perigee")),
43
+ Line1: getElsetValue(udlRow.line1, ()=>null),
44
+ Line2: getElsetValue(udlRow.line2, ()=>null),
45
+ Source: getElsetValue(udlRow.source, ()=>null),
46
+ DataMode: getElsetValue(udlRow.dataMode, ()=>null),
47
+ CreatedAt: fixDate(getElsetValue(udlRow.createdAt, ()=>null)),
48
+ Origin: getElsetValue(udlRow.origin, ()=>null),
49
+ Algorithm: getElsetValue(udlRow.algorithm, ()=>null),
50
+ Descriptor: getElsetValue(udlRow.descriptor, ()=>null),
51
+ ClassificationMarking: getElsetValue(udlRow.classificationMarking, ()=>null),
52
+ Latitude: lonAndDrift.latitude,
53
+ Longitude: lonAndDrift.longitude,
54
+ LonDriftDegreesPerDay: lonAndDrift.lonDriftDegreesPerDay,
55
+ RaanPrecessionDegreesPerDay: getRaanPrecession(udlRow.line1, udlRow.line2),
56
+ // Ensure we reset all the derived calcs so we know if they have been updated by Sentinel or not.
57
+ MinEccentricity: 0,
58
+ MaxEccentricity: 0,
59
+ MinInclination: 0,
60
+ MaxInclination: 0,
61
+ MinRaan: 0,
62
+ MaxRaan: 0,
63
+ MinArgOfPerigee: 0,
64
+ MaxArgOfPerigee: 0,
65
+ MinSemiMajorAxis: 0,
66
+ MaxSemiMajorAxis: 0,
67
+ });
68
+ npsElset.regime = calcRegime(npsElset);
69
+ npsElset.enrichedudlfields = enrichedUdlFields.join(",");
70
+ return npsElset;
71
+ };
72
+
73
+ const udlToNpsGroundSite = (udlRow) => ({
74
+ SensorId: _.get(udlRow, "idSensor", null),
75
+ Number: _.get(udlRow, "sensorNumber", null),
76
+ Name: _.get(udlRow, "sensorName", null),
77
+ Source: _.get(udlRow, "source", null),
78
+ Description: null,
79
+ LastObsTime: _.get(udlRow, "sensorStats[0].lastObTime", null),
80
+ CountryCode: _.get(udlRow, "entity.countryCode", "None"),
81
+ Taskable: _.get(udlRow, "entity.taskable", 0),
82
+ Type: _.get(udlRow, "sensorType.id", 0),
83
+ Latitude: _.get(udlRow, "entity.location.lat", 0),
84
+ Longitude: _.get(udlRow, "entity.location.lon", 0),
85
+ KmAboveSeaLevel: _.get(udlRow, "entity.location.altitude", 0),
86
+ MinRangeKm: _.get(udlRow, "sensorcharacteristics[0].minRangeLimit", null),
87
+ MaxRangeKm: _.get(udlRow, "sensorcharacteristics[0].maxRangeLimit", null),
88
+ ClassificationLevel: _.get(
89
+ udlRow,
90
+ "sensorcharacteristics[0].classificationMarking",
91
+ "",
92
+ ),
93
+ createdAt: fixDate(_.get(udlRow, "createdAt")),
94
+ });
95
+
96
+ /**
97
+ * Formats the response data based on the topic
98
+ *
99
+ * @param {String} topic
100
+ * @param {Object[]} udlData
101
+ * @return {Object[]}
102
+ */
103
+ export function formatUdlData(topic, udlData) {
104
+ switch (topic) {
105
+ case "Elsets":
106
+ return _.map(udlData, udlToNpsElset);
107
+
108
+ case "GroundSites":
109
+ return _.map(udlData, udlToNpsGroundSite);
110
+
111
+ default:
112
+ return udlData;
113
+ }
114
+ }
115
+
116
+ export {udlToNpsElset, udlToNpsGroundSite};