@certik/skynet 0.22.1 → 0.22.2

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 (102) hide show
  1. package/.vscode/settings.json +5 -0
  2. package/CHANGELOG.md +4 -0
  3. package/build.ts +23 -0
  4. package/bun.lockb +0 -0
  5. package/dist/abi.d.ts +1 -2
  6. package/dist/abi.js +569 -563
  7. package/dist/address.d.ts +0 -1
  8. package/dist/address.js +22 -21
  9. package/dist/api.d.ts +0 -1
  10. package/dist/api.js +235 -120
  11. package/dist/app.d.ts +1 -2
  12. package/dist/app.js +2030 -276
  13. package/dist/availability.d.ts +0 -1
  14. package/dist/availability.js +126 -56
  15. package/dist/cli.d.ts +0 -1
  16. package/dist/cli.js +28 -24
  17. package/dist/const.d.ts +0 -1
  18. package/dist/const.js +153 -132
  19. package/dist/databricks.d.ts +0 -1
  20. package/dist/databricks.js +198 -58
  21. package/dist/date.d.ts +0 -1
  22. package/dist/date.js +48 -21
  23. package/dist/deploy.d.ts +0 -1
  24. package/dist/deploy.js +427 -292
  25. package/dist/dynamodb.d.ts +3 -4
  26. package/dist/dynamodb.js +432 -281
  27. package/dist/env.d.ts +2 -3
  28. package/dist/env.js +16 -9
  29. package/dist/graphql.d.ts +0 -1
  30. package/dist/graphql.js +26 -23
  31. package/dist/indexer.d.ts +0 -1
  32. package/dist/indexer.js +1050 -441
  33. package/dist/log.d.ts +0 -1
  34. package/dist/log.js +53 -52
  35. package/dist/object-hash.d.ts +0 -1
  36. package/dist/object-hash.js +49 -59
  37. package/dist/opsgenie.d.ts +1 -1
  38. package/dist/opsgenie.js +31 -30
  39. package/dist/por.d.ts +0 -1
  40. package/dist/por.js +113 -123
  41. package/dist/s3.d.ts +7 -8
  42. package/dist/s3.js +103 -91
  43. package/dist/search.d.ts +0 -1
  44. package/dist/search.js +100 -25
  45. package/dist/selector.d.ts +0 -1
  46. package/dist/selector.js +34 -38
  47. package/dist/slack.d.ts +0 -1
  48. package/dist/slack.js +27 -21
  49. package/dist/util.d.ts +0 -1
  50. package/dist/util.js +21 -20
  51. package/examples/api.ts +1 -1
  52. package/examples/indexer.ts +1 -1
  53. package/examples/mode-indexer.ts +1 -1
  54. package/package.json +5 -4
  55. package/{graphql.ts → src/graphql.ts} +1 -1
  56. package/{opsgenie.ts → src/opsgenie.ts} +2 -1
  57. package/tsconfig.build.json +2 -5
  58. package/tsconfig.json +11 -20
  59. package/dist/abi.d.ts.map +0 -1
  60. package/dist/address.d.ts.map +0 -1
  61. package/dist/api.d.ts.map +0 -1
  62. package/dist/app.d.ts.map +0 -1
  63. package/dist/availability.d.ts.map +0 -1
  64. package/dist/cli.d.ts.map +0 -1
  65. package/dist/const.d.ts.map +0 -1
  66. package/dist/databricks.d.ts.map +0 -1
  67. package/dist/date.d.ts.map +0 -1
  68. package/dist/deploy.d.ts.map +0 -1
  69. package/dist/dynamodb.d.ts.map +0 -1
  70. package/dist/env.d.ts.map +0 -1
  71. package/dist/graphql.d.ts.map +0 -1
  72. package/dist/indexer.d.ts.map +0 -1
  73. package/dist/log.d.ts.map +0 -1
  74. package/dist/object-hash.d.ts.map +0 -1
  75. package/dist/opsgenie.d.ts.map +0 -1
  76. package/dist/por.d.ts.map +0 -1
  77. package/dist/s3.d.ts.map +0 -1
  78. package/dist/search.d.ts.map +0 -1
  79. package/dist/selector.d.ts.map +0 -1
  80. package/dist/slack.d.ts.map +0 -1
  81. package/dist/util.d.ts.map +0 -1
  82. /package/{abi.ts → src/abi.ts} +0 -0
  83. /package/{address.ts → src/address.ts} +0 -0
  84. /package/{api.ts → src/api.ts} +0 -0
  85. /package/{app.ts → src/app.ts} +0 -0
  86. /package/{availability.ts → src/availability.ts} +0 -0
  87. /package/{cli.ts → src/cli.ts} +0 -0
  88. /package/{const.ts → src/const.ts} +0 -0
  89. /package/{databricks.ts → src/databricks.ts} +0 -0
  90. /package/{date.ts → src/date.ts} +0 -0
  91. /package/{deploy.ts → src/deploy.ts} +0 -0
  92. /package/{dynamodb.ts → src/dynamodb.ts} +0 -0
  93. /package/{env.ts → src/env.ts} +0 -0
  94. /package/{indexer.ts → src/indexer.ts} +0 -0
  95. /package/{log.ts → src/log.ts} +0 -0
  96. /package/{object-hash.ts → src/object-hash.ts} +0 -0
  97. /package/{por.ts → src/por.ts} +0 -0
  98. /package/{s3.ts → src/s3.ts} +0 -0
  99. /package/{search.ts → src/search.ts} +0 -0
  100. /package/{selector.ts → src/selector.ts} +0 -0
  101. /package/{slack.ts → src/slack.ts} +0 -0
  102. /package/{util.ts → src/util.ts} +0 -0
package/dist/address.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export declare function parseAddress(address: string): string[];
2
2
  export declare function composeAddress(protocol: string, addr: string): string;
3
- //# sourceMappingURL=address.d.ts.map
package/dist/address.js CHANGED
@@ -1,23 +1,24 @@
1
- export function parseAddress(address) {
2
- // in case of address has multiple colons, we only split the first one
3
- const separatorIndex = address.indexOf(":");
4
- let protocolPart;
5
- let addressPart;
6
- if (separatorIndex === -1) {
7
- // if there is no colon, we assume it is an ethereum address
8
- protocolPart = "eth";
9
- addressPart = address;
10
- }
11
- else {
12
- protocolPart = address.slice(0, separatorIndex);
13
- addressPart = address.slice(separatorIndex + 1);
14
- }
15
- if (addressPart.startsWith("0x")) {
16
- // We only lowercase the address part if it starts with 0x, otherwise it is a case-sensitive address (such as tron)
17
- addressPart = addressPart.toLowerCase();
18
- }
19
- return [protocolPart, addressPart];
1
+ // src/address.ts
2
+ function parseAddress(address) {
3
+ const separatorIndex = address.indexOf(":");
4
+ let protocolPart;
5
+ let addressPart;
6
+ if (separatorIndex === -1) {
7
+ protocolPart = "eth";
8
+ addressPart = address;
9
+ } else {
10
+ protocolPart = address.slice(0, separatorIndex);
11
+ addressPart = address.slice(separatorIndex + 1);
12
+ }
13
+ if (addressPart.startsWith("0x")) {
14
+ addressPart = addressPart.toLowerCase();
15
+ }
16
+ return [protocolPart, addressPart];
20
17
  }
21
- export function composeAddress(protocol, addr) {
22
- return `${protocol}:${addr}`;
18
+ function composeAddress(protocol, addr) {
19
+ return `${protocol}:${addr}`;
23
20
  }
21
+ export {
22
+ parseAddress,
23
+ composeAddress
24
+ };
package/dist/api.d.ts CHANGED
@@ -29,4 +29,3 @@ export declare function startApiApp({ binaryName, name, selector, routes, serve,
29
29
  }) => Promise<void>;
30
30
  }): Promise<void>;
31
31
  export {};
32
- //# sourceMappingURL=api.d.ts.map
package/dist/api.js CHANGED
@@ -1,81 +1,197 @@
1
+ // src/selector.ts
2
+ function getSelectorDesc(selector) {
3
+ return Object.keys(selector).map((name) => {
4
+ return ` --${name.padEnd(14)}${selector[name].desc || selector[name].description || ""}`;
5
+ }).join(`
6
+ `);
7
+ }
8
+ function getSelectorFlags(selector) {
9
+ return Object.keys(selector).reduce((acc, name) => {
10
+ const flag = {
11
+ type: selector[name].type || "string",
12
+ ...selector[name]
13
+ };
14
+ if (!selector[name].optional && selector[name].isRequired !== false) {
15
+ flag.isRequired = true;
16
+ }
17
+ return { ...acc, [name]: flag };
18
+ }, {});
19
+ }
20
+ function toSelectorString(selectorFlags, delim = ",") {
21
+ return Object.keys(selectorFlags).sort().map((flag) => {
22
+ return `${flag}=${selectorFlags[flag]}`;
23
+ }).join(delim);
24
+ }
25
+ function normalizeSelectorValue(v) {
26
+ return v.replace(/[^A-Za-z0-9]+/g, "-");
27
+ }
28
+ function getJobName(name, selectorFlags, mode) {
29
+ const selectorNamePart = Object.keys(selectorFlags).sort().map((name2) => selectorFlags[name2]).join("-");
30
+ let jobName = name;
31
+ if (mode) {
32
+ jobName += `-${mode}`;
33
+ }
34
+ if (selectorNamePart.length > 0) {
35
+ jobName += `-${normalizeSelectorValue(selectorNamePart)}`;
36
+ }
37
+ return jobName;
38
+ }
39
+ // src/env.ts
40
+ function ensureAndGet(envName, defaultValue) {
41
+ return process.env[envName] || defaultValue;
42
+ }
43
+ function getEnvironment() {
44
+ return ensureAndGet("SKYNET_ENVIRONMENT", "dev");
45
+ }
46
+ function getEnvOrThrow(envName) {
47
+ if (!process.env[envName]) {
48
+ throw new Error(`Must set environment variable ${envName}`);
49
+ }
50
+ return process.env[envName];
51
+ }
52
+ function isProduction() {
53
+ return getEnvironment() === "prd";
54
+ }
55
+ function isDev() {
56
+ return getEnvironment() === "dev";
57
+ }
58
+ // src/log.ts
59
+ function isObject(a) {
60
+ return !!a && a.constructor === Object;
61
+ }
62
+ function print(o) {
63
+ if (Array.isArray(o)) {
64
+ return `[${o.map(print).join(", ")}]`;
65
+ }
66
+ if (isObject(o)) {
67
+ return `{${Object.keys(o).map((k) => `${k}: ${o[k]}`).join(", ")}}`;
68
+ }
69
+ return `${o}`;
70
+ }
71
+ function getLine(params) {
72
+ let line = "";
73
+ for (let i = 0, l = params.length;i < l; i++) {
74
+ line += `${print(params[i])} `.replace(/\n/gm, "\t");
75
+ }
76
+ return line.trim();
77
+ }
78
+ function timestamp() {
79
+ return new Date().toISOString();
80
+ }
81
+ var inline = {
82
+ debug: function(...args) {
83
+ if (true) {
84
+ console.log(`${timestamp()} ${getLine(args)}`);
85
+ }
86
+ },
87
+ log: function(...args) {
88
+ if (true) {
89
+ console.log(`${timestamp()} ${getLine(args)}`);
90
+ }
91
+ },
92
+ error: function(...args) {
93
+ if (true) {
94
+ console.error(`${timestamp()} ${getLine(args)}`);
95
+ }
96
+ }
97
+ };
98
+ var logger = {
99
+ debug: function(...args) {
100
+ if (true) {
101
+ console.log(`[${timestamp()}]`, ...args);
102
+ }
103
+ },
104
+ log: function(...args) {
105
+ if (true) {
106
+ console.log(`[${timestamp()}]`, ...args);
107
+ }
108
+ },
109
+ error: function(...args) {
110
+ if (true) {
111
+ console.error(`[${timestamp()}]`, ...args);
112
+ }
113
+ }
114
+ };
115
+ // src/api.ts
1
116
  import osModule from "os";
2
117
  import express from "express";
3
118
  import meow from "meow";
4
- import { getSelectorDesc, getSelectorFlags } from "./selector";
5
- import { isProduction } from "./env";
6
- import { inline } from "./log";
7
119
  async function logStartMiddleware(_, res, next) {
8
- const start = new Date();
9
- res.set("x-requested-at", start.toISOString());
10
- next();
120
+ const start = new Date;
121
+ res.set("x-requested-at", start.toISOString());
122
+ next();
11
123
  }
12
124
  async function contextMiddleware(_, res, next) {
13
- res.set("x-instance-id", osModule.hostname());
14
- next();
125
+ res.set("x-instance-id", osModule.hostname());
126
+ next();
15
127
  }
16
128
  async function logEndMiddleware(req, res, next) {
17
- const requestedAt = res.get("x-requested-at");
18
- if (!requestedAt) {
19
- inline.log("missing x-requested-at header");
20
- next();
21
- return;
22
- }
23
- const start = new Date(requestedAt).getTime();
24
- const end = new Date().getTime();
25
- const logInfo = {
26
- start,
27
- end,
28
- elapsed: `${end - start}ms`,
29
- endpoint: req.path,
30
- host: req.hostname,
31
- status: res.statusCode,
32
- };
33
- if (res.statusMessage) {
34
- logInfo.errorMessage = res.statusMessage;
35
- }
36
- inline.log(JSON.stringify(logInfo));
129
+ const requestedAt = res.get("x-requested-at");
130
+ if (!requestedAt) {
131
+ inline.log("missing x-requested-at header");
37
132
  next();
133
+ return;
134
+ }
135
+ const start = new Date(requestedAt).getTime();
136
+ const end = new Date().getTime();
137
+ const logInfo = {
138
+ start,
139
+ end,
140
+ elapsed: `${end - start}ms`,
141
+ endpoint: req.path,
142
+ host: req.hostname,
143
+ status: res.statusCode
144
+ };
145
+ if (res.statusMessage) {
146
+ logInfo.errorMessage = res.statusMessage;
147
+ }
148
+ inline.log(JSON.stringify(logInfo));
149
+ next();
38
150
  }
39
- const apiKeyMiddleware = (key) => {
40
- async function requireAPIKey(req, res, next) {
41
- try {
42
- const apiKey = req.get("x-api-key") || req.query["api-key"]; // api key present either in header or query string
43
- if (!apiKey) {
44
- inline.log("request without api key");
45
- res.status(400).send("require x-api-key header");
46
- return;
47
- }
48
- // key can be either a string or a map with multiple keys
49
- if (typeof key === "string") {
50
- if (apiKey !== key) {
51
- inline.log("request has an invalid api key");
52
- res.status(400).send("invalid api key");
53
- return;
54
- }
55
- inline.log(`requested by valid key ${key.slice(0, 6)}`);
56
- }
57
- else {
58
- const name = key[apiKey];
59
- if (!name) {
60
- inline.log("request has an invalid api key");
61
- res.status(400).send("invalid api key");
62
- return;
63
- }
64
- inline.log(`requested by authorized user ${name}`);
65
- }
66
- next();
151
+ var apiKeyMiddleware = (key) => {
152
+ async function requireAPIKey(req, res, next) {
153
+ try {
154
+ const apiKey = req.get("x-api-key") || req.query["api-key"];
155
+ if (!apiKey) {
156
+ inline.log("request without api key");
157
+ res.status(400).send("require x-api-key header");
158
+ return;
159
+ }
160
+ if (typeof key === "string") {
161
+ if (apiKey !== key) {
162
+ inline.log("request has an invalid api key");
163
+ res.status(400).send("invalid api key");
164
+ return;
67
165
  }
68
- catch (err) {
69
- inline.log("check api key error", err);
70
- res.status(500).send("internal error");
166
+ inline.log(`requested by valid key ${key.slice(0, 6)}`);
167
+ } else {
168
+ const name = key[apiKey];
169
+ if (!name) {
170
+ inline.log("request has an invalid api key");
171
+ res.status(400).send("invalid api key");
172
+ return;
71
173
  }
174
+ inline.log(`requested by authorized user ${name}`);
175
+ }
176
+ next();
177
+ } catch (err) {
178
+ inline.log("check api key error", err);
179
+ res.status(500).send("internal error");
72
180
  }
73
- return requireAPIKey;
181
+ }
182
+ return requireAPIKey;
74
183
  };
75
- export async function startApiApp({ binaryName, name, selector = {}, routes, serve, beforeListen, }) {
76
- const app = express();
77
- app.use(express.json({ limit: "20mb" }));
78
- const cli = meow(`
184
+ async function startApiApp({
185
+ binaryName,
186
+ name,
187
+ selector = {},
188
+ routes,
189
+ serve,
190
+ beforeListen
191
+ }) {
192
+ const app = express();
193
+ app.use(express.json({ limit: "20mb" }));
194
+ const cli = meow(`
79
195
  Usage
80
196
  $ ${binaryName} <options>
81
197
 
@@ -83,64 +199,63 @@ export async function startApiApp({ binaryName, name, selector = {}, routes, ser
83
199
  ${getSelectorDesc(selector)}
84
200
  --verbose Output debug messages
85
201
  `, {
86
- importMeta: import.meta,
87
- description: false,
88
- version: false,
89
- flags: {
90
- ...getSelectorFlags(selector),
91
- verbose: {
92
- type: "boolean",
93
- default: false,
94
- },
95
- },
96
- });
97
- const { verbose, ...selectorFlags } = cli.flags;
98
- for (const route of routes) {
99
- const method = route.method ? route.method.toLowerCase() : "get";
100
- const middlewares = route.middlewares || [];
101
- if (route.protected) {
102
- if (!serve.apiKey) {
103
- throw new Error("serve.apiKey is required for protected route");
104
- }
105
- middlewares.unshift(apiKeyMiddleware(serve.apiKey));
106
- }
107
- if (app[method]) {
108
- if (verbose) {
109
- inline.log(`registering ${method} ${route.path}`);
110
- }
111
- app[method](route.path, contextMiddleware, logStartMiddleware, ...middlewares, async (req, res, next) => {
112
- try {
113
- await route.handler({ req, res, ...selectorFlags });
114
- }
115
- catch (routeErr) {
116
- if (routeErr instanceof Error) {
117
- inline.log("caught route err", routeErr, routeErr.stack);
118
- res.status(500).send(`internal server error: ${routeErr.message}`);
119
- }
120
- else {
121
- inline.log("caught route err", routeErr);
122
- res.status(500).send("internal server error");
123
- }
124
- }
125
- next();
126
- }, logEndMiddleware);
127
- }
202
+ importMeta: import.meta,
203
+ description: false,
204
+ version: false,
205
+ flags: {
206
+ ...getSelectorFlags(selector),
207
+ verbose: {
208
+ type: "boolean",
209
+ default: false
210
+ }
128
211
  }
129
- if (!routes.some((r) => r.path === "/" && r.method?.toUpperCase() === "GET")) {
130
- // for health check
131
- app.get("/", (_, res) => {
132
- res.send("ok");
133
- });
212
+ });
213
+ const { verbose, ...selectorFlags } = cli.flags;
214
+ for (const route of routes) {
215
+ const method = route.method ? route.method.toLowerCase() : "get";
216
+ const middlewares = route.middlewares || [];
217
+ if (route.protected) {
218
+ if (!serve.apiKey) {
219
+ throw new Error("serve.apiKey is required for protected route");
220
+ }
221
+ middlewares.unshift(apiKeyMiddleware(serve.apiKey));
134
222
  }
135
- if (beforeListen) {
136
- await beforeListen({ app });
137
- }
138
- app.listen(serve.port, () => {
139
- if (isProduction()) {
140
- inline.log(`${name} listening at https://api.wf.corp.certik.com${serve.prefix}`);
141
- }
142
- else {
143
- inline.log(`${name} listening at http://localhost:${serve.port}`);
223
+ if (app[method]) {
224
+ if (verbose) {
225
+ inline.log(`registering ${method} ${route.path}`);
226
+ }
227
+ app[method](route.path, contextMiddleware, logStartMiddleware, ...middlewares, async (req, res, next) => {
228
+ try {
229
+ await route.handler({ req, res, ...selectorFlags });
230
+ } catch (routeErr) {
231
+ if (routeErr instanceof Error) {
232
+ inline.log("caught route err", routeErr, routeErr.stack);
233
+ res.status(500).send(`internal server error: ${routeErr.message}`);
234
+ } else {
235
+ inline.log("caught route err", routeErr);
236
+ res.status(500).send("internal server error");
237
+ }
144
238
  }
239
+ next();
240
+ }, logEndMiddleware);
241
+ }
242
+ }
243
+ if (!routes.some((r) => r.path === "/" && r.method?.toUpperCase() === "GET")) {
244
+ app.get("/", (_, res) => {
245
+ res.send("ok");
145
246
  });
247
+ }
248
+ if (beforeListen) {
249
+ await beforeListen({ app });
250
+ }
251
+ app.listen(serve.port, () => {
252
+ if (isProduction()) {
253
+ inline.log(`${name} listening at https://api.wf.corp.certik.com${serve.prefix}`);
254
+ } else {
255
+ inline.log(`${name} listening at http://localhost:${serve.port}`);
256
+ }
257
+ });
146
258
  }
259
+ export {
260
+ startApiApp
261
+ };
package/dist/app.d.ts CHANGED
@@ -74,7 +74,7 @@ declare function api({ name, routes, serve, beforeListen, env, region, }: {
74
74
  env?: Env;
75
75
  region?: string;
76
76
  }): () => Promise<void>;
77
- declare const SENSITIVE_VALUE: any;
77
+ declare const SENSITIVE_VALUE: null;
78
78
  declare const every: (n?: number) => {
79
79
  second: string;
80
80
  seconds: string;
@@ -99,4 +99,3 @@ declare const every: (n?: number) => {
99
99
  weeks?: undefined;
100
100
  };
101
101
  export { indexer, modeIndexer, api, every, SENSITIVE_VALUE };
102
- //# sourceMappingURL=app.d.ts.map