@certik/skynet 0.22.1 → 0.22.3

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.
Files changed (101) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/build.ts +23 -0
  3. package/dist/abi.d.ts +1 -2
  4. package/dist/abi.js +569 -563
  5. package/dist/address.d.ts +0 -1
  6. package/dist/address.js +22 -21
  7. package/dist/api.d.ts +0 -1
  8. package/dist/api.js +235 -120
  9. package/dist/app.d.ts +1 -2
  10. package/dist/app.js +2030 -276
  11. package/dist/availability.d.ts +0 -1
  12. package/dist/availability.js +126 -56
  13. package/dist/cli.d.ts +0 -1
  14. package/dist/cli.js +28 -24
  15. package/dist/const.d.ts +0 -1
  16. package/dist/const.js +153 -132
  17. package/dist/databricks.d.ts +0 -1
  18. package/dist/databricks.js +198 -58
  19. package/dist/date.d.ts +0 -1
  20. package/dist/date.js +48 -21
  21. package/dist/deploy.d.ts +0 -1
  22. package/dist/deploy.js +427 -292
  23. package/dist/dynamodb.d.ts +3 -4
  24. package/dist/dynamodb.js +432 -281
  25. package/dist/env.d.ts +2 -3
  26. package/dist/env.js +16 -9
  27. package/dist/graphql.d.ts +0 -1
  28. package/dist/graphql.js +26 -23
  29. package/dist/indexer.d.ts +0 -1
  30. package/dist/indexer.js +1050 -441
  31. package/dist/log.d.ts +0 -1
  32. package/dist/log.js +53 -52
  33. package/dist/object-hash.d.ts +0 -1
  34. package/dist/object-hash.js +49 -59
  35. package/dist/opsgenie.d.ts +97 -19
  36. package/dist/opsgenie.js +35 -30
  37. package/dist/por.d.ts +0 -1
  38. package/dist/por.js +113 -123
  39. package/dist/s3.d.ts +7 -8
  40. package/dist/s3.js +103 -91
  41. package/dist/search.d.ts +0 -1
  42. package/dist/search.js +100 -25
  43. package/dist/selector.d.ts +0 -1
  44. package/dist/selector.js +34 -38
  45. package/dist/slack.d.ts +0 -1
  46. package/dist/slack.js +27 -21
  47. package/dist/util.d.ts +0 -1
  48. package/dist/util.js +21 -20
  49. package/examples/api.ts +1 -1
  50. package/examples/indexer.ts +1 -1
  51. package/examples/mode-indexer.ts +1 -1
  52. package/package.json +4 -3
  53. package/{graphql.ts → src/graphql.ts} +1 -1
  54. package/src/opsgenie.ts +176 -0
  55. package/tsconfig.build.json +2 -5
  56. package/tsconfig.json +11 -20
  57. package/dist/abi.d.ts.map +0 -1
  58. package/dist/address.d.ts.map +0 -1
  59. package/dist/api.d.ts.map +0 -1
  60. package/dist/app.d.ts.map +0 -1
  61. package/dist/availability.d.ts.map +0 -1
  62. package/dist/cli.d.ts.map +0 -1
  63. package/dist/const.d.ts.map +0 -1
  64. package/dist/databricks.d.ts.map +0 -1
  65. package/dist/date.d.ts.map +0 -1
  66. package/dist/deploy.d.ts.map +0 -1
  67. package/dist/dynamodb.d.ts.map +0 -1
  68. package/dist/env.d.ts.map +0 -1
  69. package/dist/graphql.d.ts.map +0 -1
  70. package/dist/indexer.d.ts.map +0 -1
  71. package/dist/log.d.ts.map +0 -1
  72. package/dist/object-hash.d.ts.map +0 -1
  73. package/dist/opsgenie.d.ts.map +0 -1
  74. package/dist/por.d.ts.map +0 -1
  75. package/dist/s3.d.ts.map +0 -1
  76. package/dist/search.d.ts.map +0 -1
  77. package/dist/selector.d.ts.map +0 -1
  78. package/dist/slack.d.ts.map +0 -1
  79. package/dist/util.d.ts.map +0 -1
  80. package/opsgenie.ts +0 -69
  81. /package/{abi.ts → src/abi.ts} +0 -0
  82. /package/{address.ts → src/address.ts} +0 -0
  83. /package/{api.ts → src/api.ts} +0 -0
  84. /package/{app.ts → src/app.ts} +0 -0
  85. /package/{availability.ts → src/availability.ts} +0 -0
  86. /package/{cli.ts → src/cli.ts} +0 -0
  87. /package/{const.ts → src/const.ts} +0 -0
  88. /package/{databricks.ts → src/databricks.ts} +0 -0
  89. /package/{date.ts → src/date.ts} +0 -0
  90. /package/{deploy.ts → src/deploy.ts} +0 -0
  91. /package/{dynamodb.ts → src/dynamodb.ts} +0 -0
  92. /package/{env.ts → src/env.ts} +0 -0
  93. /package/{indexer.ts → src/indexer.ts} +0 -0
  94. /package/{log.ts → src/log.ts} +0 -0
  95. /package/{object-hash.ts → src/object-hash.ts} +0 -0
  96. /package/{por.ts → src/por.ts} +0 -0
  97. /package/{s3.ts → src/s3.ts} +0 -0
  98. /package/{search.ts → src/search.ts} +0 -0
  99. /package/{selector.ts → src/selector.ts} +0 -0
  100. /package/{slack.ts → src/slack.ts} +0 -0
  101. /package/{util.ts → src/util.ts} +0 -0
package/dist/por.js CHANGED
@@ -1,130 +1,120 @@
1
- import { CryptoHasher } from "bun";
2
- export class MerkleTreePoR {
3
- constructor(users, hashFunction) {
4
- this.leaves = [];
5
- this.tree = [];
6
- this.userMap = new Map();
7
- this.userData = [];
8
- this.userData = [...users];
9
- this.hashFunction = hashFunction || this.defaultSHA256Hash;
10
- this.buildTree();
1
+ // @bun
2
+ // src/por.ts
3
+ var {CryptoHasher } = globalThis.Bun;
4
+
5
+ class MerkleTreePoR {
6
+ leaves = [];
7
+ tree = [];
8
+ userMap = new Map;
9
+ userData = [];
10
+ hashFunction;
11
+ constructor(users, hashFunction) {
12
+ this.userData = [...users];
13
+ this.hashFunction = hashFunction || this.defaultSHA256Hash;
14
+ this.buildTree();
15
+ }
16
+ static defaultSHA256Hash(data) {
17
+ const hasher = new CryptoHasher("sha256");
18
+ hasher.update(data);
19
+ return hasher.digest("hex");
20
+ }
21
+ defaultSHA256Hash(data) {
22
+ return MerkleTreePoR.defaultSHA256Hash(data);
23
+ }
24
+ hash(data) {
25
+ return this.hashFunction(data);
26
+ }
27
+ createLeafHash(user) {
28
+ const leafData = `${user.userHash}:${user.balance}:${user.token || ""}`;
29
+ return this.hash(leafData);
30
+ }
31
+ buildTree() {
32
+ if (this.userData.length === 0) {
33
+ throw new Error("Cannot build tree with empty user data");
11
34
  }
12
- // Default SHA-256 hash function
13
- static defaultSHA256Hash(data) {
14
- const hasher = new CryptoHasher("sha256");
15
- hasher.update(data);
16
- return hasher.digest("hex");
17
- }
18
- defaultSHA256Hash(data) {
19
- return MerkleTreePoR.defaultSHA256Hash(data);
20
- }
21
- // Configurable hash function
22
- hash(data) {
23
- return this.hashFunction(data);
24
- }
25
- // hash user data
26
- createLeafHash(user) {
27
- const leafData = `${user.userHash}:${user.balance}:${user.token || ''}`;
28
- return this.hash(leafData);
29
- }
30
- buildTree() {
31
- if (this.userData.length === 0) {
32
- throw new Error('Cannot build tree with empty user data');
33
- }
34
- // Create leaf hashes and store user mapping
35
- this.leaves = this.userData.map((user, index) => {
36
- const leafHash = this.createLeafHash(user);
37
- this.userMap.set(`${user.userHash}:${user.token || ''}`, index);
38
- return leafHash;
35
+ this.leaves = this.userData.map((user, index) => {
36
+ const leafHash = this.createLeafHash(user);
37
+ this.userMap.set(`${user.userHash}:${user.token || ""}`, index);
38
+ return leafHash;
39
+ });
40
+ let currentLevel = this.leaves;
41
+ this.tree.push(currentLevel.map((hash) => ({ hash })));
42
+ while (currentLevel.length > 1) {
43
+ const nextLevel = [];
44
+ const nextLevelNodes = [];
45
+ for (let i = 0;i < currentLevel.length; i += 2) {
46
+ const left = currentLevel[i];
47
+ const right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left;
48
+ const parentHash = this.hash(left + right);
49
+ nextLevel.push(parentHash);
50
+ const leftNode = this.tree[this.tree.length - 1][i];
51
+ const rightNode = i + 1 < currentLevel.length ? this.tree[this.tree.length - 1][i + 1] : leftNode;
52
+ nextLevelNodes.push({
53
+ hash: parentHash,
54
+ left: leftNode,
55
+ right: rightNode
39
56
  });
40
- // Start with leaf level
41
- let currentLevel = this.leaves;
42
- this.tree.push(currentLevel.map(hash => ({ hash })));
43
- // Build tree bottom-up
44
- while (currentLevel.length > 1) {
45
- const nextLevel = [];
46
- const nextLevelNodes = [];
47
- for (let i = 0; i < currentLevel.length; i += 2) {
48
- const left = currentLevel[i];
49
- const right = i + 1 < currentLevel.length
50
- ? currentLevel[i + 1]
51
- : left; // Duplicate if odd number
52
- const parentHash = this.hash(left + right);
53
- nextLevel.push(parentHash);
54
- const leftNode = this.tree[this.tree.length - 1][i];
55
- const rightNode = i + 1 < currentLevel.length
56
- ? this.tree[this.tree.length - 1][i + 1]
57
- : leftNode;
58
- nextLevelNodes.push({
59
- hash: parentHash,
60
- left: leftNode,
61
- right: rightNode
62
- });
63
- }
64
- currentLevel = nextLevel;
65
- this.tree.push(nextLevelNodes);
66
- }
57
+ }
58
+ currentLevel = nextLevel;
59
+ this.tree.push(nextLevelNodes);
60
+ }
61
+ }
62
+ getRootHash() {
63
+ if (this.tree.length === 0) {
64
+ throw new Error("Merkle tree not built");
67
65
  }
68
- // get root hash of created tree
69
- getRootHash() {
70
- if (this.tree.length === 0) {
71
- throw new Error('Merkle tree not built');
72
- }
73
- return this.tree[this.tree.length - 1][0].hash;
66
+ return this.tree[this.tree.length - 1][0].hash;
67
+ }
68
+ generateProof(userHash, token) {
69
+ const key = `${userHash}:${token || ""}`;
70
+ const leafIndex = this.userMap.get(key);
71
+ if (leafIndex === undefined) {
72
+ throw new Error(`${userHash} not found in merkle tree for token ${token}`);
74
73
  }
75
- // generate merkle proof
76
- generateProof(userHash, token) {
77
- const key = `${userHash}:${token || ''}`;
78
- const leafIndex = this.userMap.get(key);
79
- if (leafIndex === undefined) {
80
- throw new Error(`${userHash} not found in merkle tree for token ${token}`);
81
- }
82
- const user = this.userData[leafIndex];
83
- const proof = [];
84
- const positions = [];
85
- let currentIndex = leafIndex;
86
- // Walk up the tree collecting sibling hashes
87
- for (let level = 0; level < this.tree.length - 1; level++) {
88
- const currentLevelSize = this.tree[level].length;
89
- const isRightNode = currentIndex % 2 === 1;
90
- const siblingIndex = isRightNode ? currentIndex - 1 : currentIndex + 1;
91
- if (siblingIndex < currentLevelSize) {
92
- proof.push(this.tree[level][siblingIndex].hash);
93
- positions.push(isRightNode ? 'left' : 'right');
94
- }
95
- else {
96
- // When odd number, the last node is duplicated
97
- proof.push(this.tree[level][currentIndex].hash);
98
- positions.push(isRightNode ? 'left' : 'right');
99
- }
100
- currentIndex = Math.floor(currentIndex / 2);
101
- }
102
- return {
103
- userHash: user.userHash,
104
- balance: user.balance,
105
- token: user.token,
106
- proof,
107
- positions,
108
- rootHash: this.getRootHash(),
109
- leafIndex,
110
- hashFunction: this.hashFunction !== this.defaultSHA256Hash ? this.hashFunction : undefined
111
- };
74
+ const user = this.userData[leafIndex];
75
+ const proof = [];
76
+ const positions = [];
77
+ let currentIndex = leafIndex;
78
+ for (let level = 0;level < this.tree.length - 1; level++) {
79
+ const currentLevelSize = this.tree[level].length;
80
+ const isRightNode = currentIndex % 2 === 1;
81
+ const siblingIndex = isRightNode ? currentIndex - 1 : currentIndex + 1;
82
+ if (siblingIndex < currentLevelSize) {
83
+ proof.push(this.tree[level][siblingIndex].hash);
84
+ positions.push(isRightNode ? "left" : "right");
85
+ } else {
86
+ proof.push(this.tree[level][currentIndex].hash);
87
+ positions.push(isRightNode ? "left" : "right");
88
+ }
89
+ currentIndex = Math.floor(currentIndex / 2);
112
90
  }
113
- // verify inclusion using proof
114
- static verifyProof(merkleProof) {
115
- const leafData = `${merkleProof.userHash}:${merkleProof.balance}:${merkleProof.token || ''}`;
116
- const hashFunction = merkleProof.hashFunction || MerkleTreePoR.defaultSHA256Hash;
117
- let currentHash = hashFunction(leafData);
118
- for (let i = 0; i < merkleProof.proof.length; i++) {
119
- const siblingHash = merkleProof.proof[i];
120
- const position = merkleProof.positions[i];
121
- if (position === 'left') {
122
- currentHash = hashFunction(siblingHash + currentHash);
123
- }
124
- else {
125
- currentHash = hashFunction(currentHash + siblingHash);
126
- }
127
- }
128
- return currentHash === merkleProof.rootHash;
91
+ return {
92
+ userHash: user.userHash,
93
+ balance: user.balance,
94
+ token: user.token,
95
+ proof,
96
+ positions,
97
+ rootHash: this.getRootHash(),
98
+ leafIndex,
99
+ hashFunction: this.hashFunction !== this.defaultSHA256Hash ? this.hashFunction : undefined
100
+ };
101
+ }
102
+ static verifyProof(merkleProof) {
103
+ const leafData = `${merkleProof.userHash}:${merkleProof.balance}:${merkleProof.token || ""}`;
104
+ const hashFunction = merkleProof.hashFunction || MerkleTreePoR.defaultSHA256Hash;
105
+ let currentHash = hashFunction(leafData);
106
+ for (let i = 0;i < merkleProof.proof.length; i++) {
107
+ const siblingHash = merkleProof.proof[i];
108
+ const position = merkleProof.positions[i];
109
+ if (position === "left") {
110
+ currentHash = hashFunction(siblingHash + currentHash);
111
+ } else {
112
+ currentHash = hashFunction(currentHash + siblingHash);
113
+ }
129
114
  }
115
+ return currentHash === merkleProof.rootHash;
116
+ }
130
117
  }
118
+ export {
119
+ MerkleTreePoR
120
+ };
package/dist/s3.d.ts CHANGED
@@ -1,21 +1,20 @@
1
1
  import { S3Client, ObjectCannedACL } from "@aws-sdk/client-s3";
2
2
  declare function getS3(forceNew?: boolean): S3Client;
3
- declare function readFile(bucketName: string, key: string, verbose?: boolean): Promise<string>;
3
+ declare function readFile(bucketName: string, key: string, verbose?: boolean): Promise<string | null | undefined>;
4
4
  declare function hasFile(bucketName: string, key: string): Promise<boolean>;
5
- declare function getFileSize(bucketName: string, key: string): Promise<number>;
5
+ declare function getFileSize(bucketName: string, key: string): Promise<number | undefined>;
6
6
  declare function writeFile(bucketName: string, key: string, body: string, options?: {
7
7
  verbose?: boolean;
8
8
  skipIfExists?: boolean;
9
9
  acl?: ObjectCannedACL;
10
10
  contentType?: string;
11
11
  }): Promise<void>;
12
- declare function deleteFile(bucketName: string, key: string, verbose?: boolean): Promise<any>;
12
+ declare function deleteFile(bucketName: string, key: string, verbose?: boolean): Promise<null | undefined>;
13
13
  declare function listKeys(bucketName: string, prefix?: string, continuationToken?: string): Promise<{
14
- keys: any[];
14
+ keys: never[];
15
15
  continuationToken?: undefined;
16
16
  } | {
17
- keys: string[];
18
- continuationToken: string;
19
- }>;
17
+ keys: (string | undefined)[];
18
+ continuationToken: string | undefined;
19
+ } | null>;
20
20
  export { getS3, hasFile, readFile, writeFile, deleteFile, getFileSize, listKeys };
21
- //# sourceMappingURL=s3.d.ts.map
package/dist/s3.js CHANGED
@@ -1,110 +1,122 @@
1
- import { S3Client, GetObjectCommand, HeadObjectCommand, PutObjectCommand, DeleteObjectCommand, ListObjectsV2Command, NotFound, NoSuchKey, } from "@aws-sdk/client-s3";
2
- let _s3Client;
1
+ // src/s3.ts
2
+ import {
3
+ S3Client,
4
+ GetObjectCommand,
5
+ HeadObjectCommand,
6
+ PutObjectCommand,
7
+ DeleteObjectCommand,
8
+ ListObjectsV2Command,
9
+ NotFound,
10
+ NoSuchKey
11
+ } from "@aws-sdk/client-s3";
12
+ var _s3Client;
3
13
  function getS3(forceNew = false) {
4
- if (!_s3Client || forceNew) {
5
- _s3Client = new S3Client();
6
- }
7
- return _s3Client;
14
+ if (!_s3Client || forceNew) {
15
+ _s3Client = new S3Client;
16
+ }
17
+ return _s3Client;
8
18
  }
9
19
  async function readFile(bucketName, key, verbose = false) {
10
- const s3 = getS3();
11
- const params = { Bucket: bucketName, Key: key };
12
- try {
13
- const result = await s3.send(new GetObjectCommand(params));
14
- return result.Body?.transformToString();
15
- }
16
- catch (error) {
17
- if (error instanceof NoSuchKey) {
18
- if (verbose) {
19
- console.log("no such bucket or key", bucketName, key);
20
- }
21
- // do nothing
22
- return null;
23
- }
24
- throw error;
20
+ const s3 = getS3();
21
+ const params = { Bucket: bucketName, Key: key };
22
+ try {
23
+ const result = await s3.send(new GetObjectCommand(params));
24
+ return result.Body?.transformToString();
25
+ } catch (error) {
26
+ if (error instanceof NoSuchKey) {
27
+ if (verbose) {
28
+ console.log("no such bucket or key", bucketName, key);
29
+ }
30
+ return null;
25
31
  }
32
+ throw error;
33
+ }
26
34
  }
27
35
  async function hasFile(bucketName, key) {
28
- const s3 = getS3();
29
- try {
30
- await s3.send(new HeadObjectCommand({ Bucket: bucketName, Key: key }));
31
- return true;
32
- }
33
- catch (headErr) {
34
- if (headErr instanceof NotFound) {
35
- return false;
36
- }
37
- throw headErr;
36
+ const s3 = getS3();
37
+ try {
38
+ await s3.send(new HeadObjectCommand({ Bucket: bucketName, Key: key }));
39
+ return true;
40
+ } catch (headErr) {
41
+ if (headErr instanceof NotFound) {
42
+ return false;
38
43
  }
44
+ throw headErr;
45
+ }
39
46
  }
40
47
  async function getFileSize(bucketName, key) {
41
- const s3 = getS3();
42
- const result = await s3.send(new HeadObjectCommand({
43
- Bucket: bucketName,
44
- Key: key,
45
- }));
46
- return result.ContentLength;
48
+ const s3 = getS3();
49
+ const result = await s3.send(new HeadObjectCommand({
50
+ Bucket: bucketName,
51
+ Key: key
52
+ }));
53
+ return result.ContentLength;
47
54
  }
48
55
  async function writeFile(bucketName, key, body, options = {}) {
49
- const s3 = getS3();
50
- if (options.skipIfExists) {
51
- if (await hasFile(bucketName, key)) {
52
- return;
53
- }
54
- }
55
- const verbose = options.verbose || false;
56
- const params = { Bucket: bucketName, Key: key, Body: body };
57
- if (options.acl) {
58
- params.ACL = options.acl;
59
- }
60
- if (options.contentType) {
61
- params.ContentType = options.contentType;
56
+ const s3 = getS3();
57
+ if (options.skipIfExists) {
58
+ if (await hasFile(bucketName, key)) {
59
+ return;
62
60
  }
63
- if (verbose) {
64
- console.log("uploading", key);
65
- }
66
- await s3.send(new PutObjectCommand(params));
61
+ }
62
+ const verbose = options.verbose || false;
63
+ const params = { Bucket: bucketName, Key: key, Body: body };
64
+ if (options.acl) {
65
+ params.ACL = options.acl;
66
+ }
67
+ if (options.contentType) {
68
+ params.ContentType = options.contentType;
69
+ }
70
+ if (verbose) {
71
+ console.log("uploading", key);
72
+ }
73
+ await s3.send(new PutObjectCommand(params));
67
74
  }
68
75
  async function deleteFile(bucketName, key, verbose = false) {
69
- const s3 = getS3();
70
- const params = { Bucket: bucketName, Key: key };
71
- try {
72
- await s3.send(new DeleteObjectCommand(params));
73
- }
74
- catch (error) {
75
- if (error instanceof NoSuchKey) {
76
- if (verbose) {
77
- console.log("no such bucket or key", bucketName, key);
78
- }
79
- // do nothing
80
- return null;
81
- }
82
- throw error;
76
+ const s3 = getS3();
77
+ const params = { Bucket: bucketName, Key: key };
78
+ try {
79
+ await s3.send(new DeleteObjectCommand(params));
80
+ } catch (error) {
81
+ if (error instanceof NoSuchKey) {
82
+ if (verbose) {
83
+ console.log("no such bucket or key", bucketName, key);
84
+ }
85
+ return null;
83
86
  }
87
+ throw error;
88
+ }
84
89
  }
85
90
  async function listKeys(bucketName, prefix, continuationToken) {
86
- const s3 = getS3();
87
- const params = {
88
- Bucket: bucketName,
89
- Prefix: prefix,
90
- ContinuationToken: continuationToken,
91
- };
92
- let data;
93
- try {
94
- data = await s3.send(new ListObjectsV2Command(params));
95
- }
96
- catch (err) {
97
- if (err instanceof Error && "statusCode" in err && err.statusCode === 400) {
98
- return null;
99
- }
100
- throw `unable to list keys with prefix ${prefix}: ${err}`;
101
- }
102
- if (!data.Contents) {
103
- return { keys: [] };
91
+ const s3 = getS3();
92
+ const params = {
93
+ Bucket: bucketName,
94
+ Prefix: prefix,
95
+ ContinuationToken: continuationToken
96
+ };
97
+ let data;
98
+ try {
99
+ data = await s3.send(new ListObjectsV2Command(params));
100
+ } catch (err) {
101
+ if (err instanceof Error && "statusCode" in err && err.statusCode === 400) {
102
+ return null;
104
103
  }
105
- return {
106
- keys: data.Contents.map(({ Key }) => Key),
107
- continuationToken: data.IsTruncated ? data.NextContinuationToken : undefined,
108
- };
104
+ throw `unable to list keys with prefix ${prefix}: ${err}`;
105
+ }
106
+ if (!data.Contents) {
107
+ return { keys: [] };
108
+ }
109
+ return {
110
+ keys: data.Contents.map(({ Key }) => Key),
111
+ continuationToken: data.IsTruncated ? data.NextContinuationToken : undefined
112
+ };
109
113
  }
110
- export { getS3, hasFile, readFile, writeFile, deleteFile, getFileSize, listKeys };
114
+ export {
115
+ writeFile,
116
+ readFile,
117
+ listKeys,
118
+ hasFile,
119
+ getS3,
120
+ getFileSize,
121
+ deleteFile
122
+ };
package/dist/search.d.ts CHANGED
@@ -3,4 +3,3 @@ type ElasticSearchRecord = {
3
3
  };
4
4
  export declare function sendToSearch(indexPrefix: string, record: ElasticSearchRecord, throws?: boolean): Promise<void>;
5
5
  export {};
6
- //# sourceMappingURL=search.d.ts.map
package/dist/search.js CHANGED
@@ -1,30 +1,105 @@
1
- import { isProduction } from "./env";
2
- import { inline } from "./log";
1
+ // src/env.ts
2
+ function ensureAndGet(envName, defaultValue) {
3
+ return process.env[envName] || defaultValue;
4
+ }
5
+ function getEnvironment() {
6
+ return ensureAndGet("SKYNET_ENVIRONMENT", "dev");
7
+ }
8
+ function getEnvOrThrow(envName) {
9
+ if (!process.env[envName]) {
10
+ throw new Error(`Must set environment variable ${envName}`);
11
+ }
12
+ return process.env[envName];
13
+ }
14
+ function isProduction() {
15
+ return getEnvironment() === "prd";
16
+ }
17
+ function isDev() {
18
+ return getEnvironment() === "dev";
19
+ }
20
+ // src/log.ts
21
+ function isObject(a) {
22
+ return !!a && a.constructor === Object;
23
+ }
24
+ function print(o) {
25
+ if (Array.isArray(o)) {
26
+ return `[${o.map(print).join(", ")}]`;
27
+ }
28
+ if (isObject(o)) {
29
+ return `{${Object.keys(o).map((k) => `${k}: ${o[k]}`).join(", ")}}`;
30
+ }
31
+ return `${o}`;
32
+ }
33
+ function getLine(params) {
34
+ let line = "";
35
+ for (let i = 0, l = params.length;i < l; i++) {
36
+ line += `${print(params[i])} `.replace(/\n/gm, "\t");
37
+ }
38
+ return line.trim();
39
+ }
40
+ function timestamp() {
41
+ return new Date().toISOString();
42
+ }
43
+ var inline = {
44
+ debug: function(...args) {
45
+ if (true) {
46
+ console.log(`${timestamp()} ${getLine(args)}`);
47
+ }
48
+ },
49
+ log: function(...args) {
50
+ if (true) {
51
+ console.log(`${timestamp()} ${getLine(args)}`);
52
+ }
53
+ },
54
+ error: function(...args) {
55
+ if (true) {
56
+ console.error(`${timestamp()} ${getLine(args)}`);
57
+ }
58
+ }
59
+ };
60
+ var logger = {
61
+ debug: function(...args) {
62
+ if (true) {
63
+ console.log(`[${timestamp()}]`, ...args);
64
+ }
65
+ },
66
+ log: function(...args) {
67
+ if (true) {
68
+ console.log(`[${timestamp()}]`, ...args);
69
+ }
70
+ },
71
+ error: function(...args) {
72
+ if (true) {
73
+ console.error(`[${timestamp()}]`, ...args);
74
+ }
75
+ }
76
+ };
77
+ // src/search.ts
3
78
  import { Client, HttpConnection } from "@elastic/elasticsearch";
4
79
  import osModule from "os";
5
- export async function sendToSearch(indexPrefix, record, throws = false) {
6
- if (!process.env["SKYNET_ELASTICSEARCH_CLOUD_ID"] || !process.env["SKYNET_ELASTICSEARCH_API_KEY"]) {
7
- throw new Error("SKYNET_ELASTICSEARCH_CLOUD_ID or SKYNET_ELASTICSEARCH_API_KEY is not set");
8
- }
9
- const client = new Client({
10
- cloud: { id: process.env["SKYNET_ELASTICSEARCH_CLOUD_ID"] },
11
- auth: { apiKey: process.env["SKYNET_ELASTICSEARCH_API_KEY"] },
12
- Connection: HttpConnection, // bun compatibility, see https://github.com/oven-sh/bun/issues/7920
80
+ async function sendToSearch(indexPrefix, record, throws = false) {
81
+ if (!process.env["SKYNET_ELASTICSEARCH_CLOUD_ID"] || !process.env["SKYNET_ELASTICSEARCH_API_KEY"]) {
82
+ throw new Error("SKYNET_ELASTICSEARCH_CLOUD_ID or SKYNET_ELASTICSEARCH_API_KEY is not set");
83
+ }
84
+ const client = new Client({
85
+ cloud: { id: process.env["SKYNET_ELASTICSEARCH_CLOUD_ID"] },
86
+ auth: { apiKey: process.env["SKYNET_ELASTICSEARCH_API_KEY"] },
87
+ Connection: HttpConnection
88
+ });
89
+ const now = new Date;
90
+ const indexName = [indexPrefix, isProduction() ? "prod" : "dev", now.toISOString().slice(0, 7)].join("-");
91
+ try {
92
+ await client.index({
93
+ index: indexName,
94
+ document: { instance: osModule.hostname(), timestamp: now, record }
13
95
  });
14
- const now = new Date();
15
- const indexName = [indexPrefix, isProduction() ? "prod" : "dev", now.toISOString().slice(0, 7)].join("-");
16
- // best as possible delivery
17
- // by default not throw
18
- try {
19
- await client.index({
20
- index: indexName,
21
- document: { instance: osModule.hostname(), timestamp: now, record },
22
- });
23
- }
24
- catch (err) {
25
- inline.error(err);
26
- if (throws) {
27
- throw err;
28
- }
96
+ } catch (err) {
97
+ inline.error(err);
98
+ if (throws) {
99
+ throw err;
29
100
  }
101
+ }
30
102
  }
103
+ export {
104
+ sendToSearch
105
+ };
@@ -15,4 +15,3 @@ declare function getSelectorFlags<TSelector extends Selector>(selector: Selector
15
15
  declare function toSelectorString<TSelector extends Selector>(selectorFlags: SelectorFlags<TSelector>, delim?: string): string;
16
16
  declare function getJobName<TSelector extends Selector>(name: string, selectorFlags: SelectorFlags<TSelector>, mode?: string): string;
17
17
  export { getJobName, getSelectorDesc, getSelectorFlags, toSelectorString };
18
- //# sourceMappingURL=selector.d.ts.map