@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
@@ -1,68 +1,208 @@
1
- import { DBSQLClient, DBSQLParameter } from "@databricks/sql";
2
- import { getEnvOrThrow } from "./env.js";
3
- import { exponentialRetry } from "./availability.js";
4
- let client = null;
5
- let session = null;
6
- async function initSession() {
7
- if (!client) {
8
- client = new DBSQLClient();
9
- await client.connect({
10
- authType: "access-token",
11
- token: getEnvOrThrow("SKYNET_DATABRICKS_TOKEN"),
12
- host: getEnvOrThrow("SKYNET_DATABRICKS_SERVER_HOSTNAME"),
13
- path: getEnvOrThrow("SKYNET_DATABRICKS_HTTP_PATH"),
14
- });
15
- }
16
- if (!session) {
17
- session = await client.openSession();
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/object-hash.ts
21
+ import xh from "@node-rs/xxhash";
22
+ function getHash(obj) {
23
+ const xxh3 = xh.xxh3.Xxh3.withSeed();
24
+ hash(obj, xxh3);
25
+ return xxh3.digest().toString(16);
26
+ }
27
+ function hash(obj, xxh3) {
28
+ if (obj === null) {
29
+ xxh3.update("null");
30
+ } else if (obj === undefined) {
31
+ xxh3.update("undefined");
32
+ } else if (typeof obj === "string") {
33
+ xxh3.update(obj);
34
+ } else if (typeof obj === "number") {
35
+ xxh3.update(obj.toString());
36
+ } else if (typeof obj === "boolean") {
37
+ xxh3.update(obj.toString());
38
+ } else if (typeof obj === "bigint") {
39
+ xxh3.update(obj.toString());
40
+ } else if (obj instanceof Date) {
41
+ xxh3.update(obj.toISOString());
42
+ } else if (Array.isArray(obj)) {
43
+ arrayHash(obj, xxh3);
44
+ } else if (obj instanceof Set) {
45
+ setHash(obj, xxh3);
46
+ } else if (obj instanceof Map) {
47
+ mapHash(obj, xxh3);
48
+ } else if (typeof obj === "object") {
49
+ objectHash(obj, xxh3);
50
+ } else {
51
+ throw new Error(`Unsupported type: ${obj}`);
52
+ }
53
+ }
54
+ function arrayHash(array, xxh3) {
55
+ xxh3.update("[");
56
+ for (const obj of array) {
57
+ hash(obj, xxh3);
58
+ }
59
+ xxh3.update("]");
60
+ }
61
+ function setHash(_set, _xxh3) {
62
+ throw new Error("Set hashing not implemented");
63
+ }
64
+ function mapHash(map, xxh3) {
65
+ const array = Array.from(map.entries()).sort(([aKey], [bKey]) => aKey.localeCompare(bKey));
66
+ for (const [key, value] of array) {
67
+ hash(key, xxh3);
68
+ hash(value, xxh3);
69
+ }
70
+ }
71
+ function objectHash(obj, xxh3) {
72
+ const array = Object.entries(obj).sort(([aKey], [bKey]) => aKey.localeCompare(bKey));
73
+ for (const [key, value] of array) {
74
+ hash(key, xxh3);
75
+ hash(value, xxh3);
76
+ }
77
+ }
78
+
79
+ // src/availability.ts
80
+ import pThrottle from "p-throttle";
81
+ import pMemoize from "p-memoize";
82
+ import QuickLRU from "quick-lru";
83
+ async function wait(time) {
84
+ return new Promise((resolve) => {
85
+ setTimeout(resolve, time);
86
+ });
87
+ }
88
+ async function exponentialRetry(func, {
89
+ maxRetry,
90
+ initialDuration,
91
+ growFactor,
92
+ test,
93
+ verbose
94
+ }) {
95
+ let retries = maxRetry;
96
+ let duration = initialDuration || 5000;
97
+ const growFactorFinal = growFactor || 2;
98
+ let result = await func();
99
+ while (!test(result) && retries > 0) {
100
+ if (verbose) {
101
+ console.log("failed attempt result", result);
102
+ console.log(`sleep for ${duration}ms after failed attempt, remaining ${retries} attempts`);
18
103
  }
104
+ retries = retries - 1;
105
+ await wait(duration);
106
+ result = await func();
107
+ duration = duration * growFactorFinal;
108
+ }
109
+ if (verbose) {
110
+ console.log(`function to retry ends with status ${test(result)}, number of retries done: ${maxRetry - retries}}`);
111
+ }
112
+ return result;
19
113
  }
20
- async function executeSql(sql, bindings) {
21
- // retry sql query to avoid socket hang up error
22
- const results = await exponentialRetry(async () => {
23
- let queryOperation;
24
- try {
25
- await initSession();
26
- if (!session)
27
- throw new Error("Session is not initialized");
28
- queryOperation = await session.executeStatement(sql, {
29
- runAsync: true,
30
- namedParameters: objToParameter(bindings),
31
- });
32
- const result = await queryOperation.fetchAll();
33
- return result;
114
+ function withRetry(func, options) {
115
+ let retries = options?.maxRetry || 3;
116
+ let duration = options?.initialDuration || 500;
117
+ const growFactorFinal = options?.growFactor || 2;
118
+ return async (...args) => {
119
+ do {
120
+ try {
121
+ return await func(...args);
122
+ } catch (error) {
123
+ retries = retries - 1;
124
+ if (retries <= 0) {
125
+ throw error;
34
126
  }
35
- catch (err) {
36
- // If the error is not a Databricks error, throw it
37
- if (!checkIsDatabricksError(err)) {
38
- throw err;
39
- }
40
- return err;
41
- }
42
- finally {
43
- await queryOperation?.close();
44
- }
45
- }, {
46
- maxRetry: 3,
47
- initialDuration: 500,
48
- test: (result) => {
49
- return !checkIsDatabricksError(result);
50
- },
127
+ await wait(duration);
128
+ duration = duration * growFactorFinal;
129
+ }
130
+ } while (retries > 0);
131
+ throw new Error("unreachable");
132
+ };
133
+ }
134
+ function memoize(func, options) {
135
+ if (!options) {
136
+ options = {};
137
+ }
138
+ if (!options.cache) {
139
+ options.cache = new QuickLRU({ maxSize: options.lruMaxSize || 1e4 });
140
+ }
141
+ if (!options.cacheKey) {
142
+ options.cacheKey = (args) => getHash(args);
143
+ }
144
+ return pMemoize(func, options);
145
+ }
146
+ // src/databricks.ts
147
+ import { DBSQLClient, DBSQLParameter } from "@databricks/sql";
148
+ var client = null;
149
+ var session = null;
150
+ async function initSession() {
151
+ if (!client) {
152
+ client = new DBSQLClient;
153
+ await client.connect({
154
+ authType: "access-token",
155
+ token: getEnvOrThrow("SKYNET_DATABRICKS_TOKEN"),
156
+ host: getEnvOrThrow("SKYNET_DATABRICKS_SERVER_HOSTNAME"),
157
+ path: getEnvOrThrow("SKYNET_DATABRICKS_HTTP_PATH")
51
158
  });
52
- if (checkIsDatabricksError(results)) {
53
- // still error after retry, throw it
54
- throw results;
159
+ }
160
+ if (!session) {
161
+ session = await client.openSession();
162
+ }
163
+ }
164
+ async function executeSql(sql, bindings) {
165
+ const results = await exponentialRetry(async () => {
166
+ let queryOperation;
167
+ try {
168
+ await initSession();
169
+ if (!session)
170
+ throw new Error("Session is not initialized");
171
+ queryOperation = await session.executeStatement(sql, {
172
+ runAsync: true,
173
+ namedParameters: objToParameter(bindings)
174
+ });
175
+ const result = await queryOperation.fetchAll();
176
+ return result;
177
+ } catch (err) {
178
+ if (!checkIsDatabricksError(err)) {
179
+ throw err;
180
+ }
181
+ return err;
182
+ } finally {
183
+ await queryOperation?.close();
184
+ }
185
+ }, {
186
+ maxRetry: 3,
187
+ initialDuration: 500,
188
+ test: (result) => {
189
+ return !checkIsDatabricksError(result);
55
190
  }
56
- return results;
191
+ });
192
+ if (checkIsDatabricksError(results)) {
193
+ throw results;
194
+ }
195
+ return results;
57
196
  }
58
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
197
  function checkIsDatabricksError(err) {
60
- return err?.errno === "ECONNRESET" || err?.message?.includes("java.lang.NullPointerException");
198
+ return err?.errno === "ECONNRESET" || err?.message?.includes("java.lang.NullPointerException");
61
199
  }
62
200
  function objToParameter(obj) {
63
- return Object.entries(obj).reduce((acc, [key, value]) => {
64
- acc[key] = new DBSQLParameter({ value });
65
- return acc;
66
- }, {});
201
+ return Object.entries(obj).reduce((acc, [key, value]) => {
202
+ acc[key] = new DBSQLParameter({ value });
203
+ return acc;
204
+ }, {});
67
205
  }
68
- export { executeSql };
206
+ export {
207
+ executeSql
208
+ };
package/dist/date.d.ts CHANGED
@@ -3,4 +3,3 @@ declare function findDateAfter(date: number | string | Date, n: number): string;
3
3
  declare function daysInRange(from: number | string | Date, to: number | string | Date): string[];
4
4
  declare function dateRange(from: number | string | Date, to: number | string | Date, step: number): [string, string][];
5
5
  export { getDateOnly, findDateAfter, daysInRange, dateRange };
6
- //# sourceMappingURL=date.d.ts.map
package/dist/date.js CHANGED
@@ -1,29 +1,56 @@
1
- import { arrayGroup } from "./util";
2
- const MS_IN_A_DAY = 3600 * 24 * 1000;
1
+ // src/util.ts
2
+ function range(startAt, endAt, step) {
3
+ const arr = [];
4
+ for (let i = startAt;i <= endAt; i += step) {
5
+ arr.push([i, Math.min(endAt, i + step - 1)]);
6
+ }
7
+ return arr;
8
+ }
9
+ function arrayGroup(array, groupSize) {
10
+ const groups = [];
11
+ for (let i = 0;i < array.length; i += groupSize) {
12
+ groups.push(array.slice(i, i + groupSize));
13
+ }
14
+ return groups;
15
+ }
16
+ function fillRange(start, end) {
17
+ const result = [];
18
+ for (let i = start;i <= end; i++) {
19
+ result.push(i);
20
+ }
21
+ return result;
22
+ }
23
+ // src/date.ts
24
+ var MS_IN_A_DAY = 3600 * 24 * 1000;
3
25
  function getDateOnly(date) {
4
- return new Date(date).toISOString().split("T")[0];
26
+ return new Date(date).toISOString().split("T")[0];
5
27
  }
6
28
  function findDateAfter(date, n) {
7
- const d = new Date(date);
8
- const after = new Date(d.getTime() + MS_IN_A_DAY * n);
9
- return getDateOnly(after);
29
+ const d = new Date(date);
30
+ const after = new Date(d.getTime() + MS_IN_A_DAY * n);
31
+ return getDateOnly(after);
10
32
  }
11
33
  function daysInRange(from, to) {
12
- const fromTime = new Date(from).getTime();
13
- const toTime = new Date(to).getTime();
14
- if (fromTime > toTime) {
15
- throw new Error(`range to date couldn't be earlier than range from date`);
16
- }
17
- const daysBetween = Math.floor((toTime - fromTime) / MS_IN_A_DAY);
18
- const dates = [getDateOnly(new Date(fromTime))];
19
- for (let i = 1; i <= daysBetween; i += 1) {
20
- dates.push(getDateOnly(new Date(fromTime + i * MS_IN_A_DAY)));
21
- }
22
- return dates;
34
+ const fromTime = new Date(from).getTime();
35
+ const toTime = new Date(to).getTime();
36
+ if (fromTime > toTime) {
37
+ throw new Error(`range to date couldn't be earlier than range from date`);
38
+ }
39
+ const daysBetween = Math.floor((toTime - fromTime) / MS_IN_A_DAY);
40
+ const dates = [getDateOnly(new Date(fromTime))];
41
+ for (let i = 1;i <= daysBetween; i += 1) {
42
+ dates.push(getDateOnly(new Date(fromTime + i * MS_IN_A_DAY)));
43
+ }
44
+ return dates;
23
45
  }
24
46
  function dateRange(from, to, step) {
25
- const days = daysInRange(from, to);
26
- const windows = arrayGroup(days, step);
27
- return windows.map((w) => [w[0], w[w.length - 1]]);
47
+ const days = daysInRange(from, to);
48
+ const windows = arrayGroup(days, step);
49
+ return windows.map((w) => [w[0], w[w.length - 1]]);
28
50
  }
29
- export { getDateOnly, findDateAfter, daysInRange, dateRange };
51
+ export {
52
+ getDateOnly,
53
+ findDateAfter,
54
+ daysInRange,
55
+ dateRange
56
+ };
package/dist/deploy.d.ts CHANGED
@@ -73,4 +73,3 @@ declare function createDeploy({ binaryName, name, workingDirectory, bin, selecto
73
73
  deploy: () => Promise<void>;
74
74
  };
75
75
  export { getJobName, getNomadAddr, createModeDeploy, createDeploy };
76
- //# sourceMappingURL=deploy.d.ts.map