@witnet/sdk 3.0.0 → 3.0.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.
@@ -14,146 +14,99 @@ import { utils, Witnet } from "../../../dist/src/index.js";
14
14
  import { default as legacy } from "../../../witnet/assets/index.cjs";
15
15
  import * as helpers from "../helpers.js";
16
16
 
17
- const WITNET_ASSETS_PATH = process.env.WITNET_SDK_RADON_ASSETS_PATH || "../../../../../witnet/assets";
18
-
19
17
  /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
18
  /// CLI SUBMODULE CONSTANTS ===========================================================================================
21
19
 
22
- const isModuleInitialized = fs.existsSync("./witnet/assets/index.cjs");
20
+ const __dirname = helpers.searchWorkspace();
21
+ const isModuleInitialized = __dirname !== path.dirname(__dirname) && fs.existsSync(`${__dirname}/package.json`);
22
+ const WITNET_ASSETS_PATH = process.env.WITNET_SDK_RADON_ASSETS_PATH || `${__dirname}/witnet/assets`;
23
23
 
24
24
  export const flags = {
25
- module: {
26
- hint: "Package where to fetch Radon assets from (supersedes --legacy).",
25
+ package: {
26
+ hint: "Imported package where to fetch Radon assets from (supersedes --legacy).",
27
27
  param: "NPM_PACKAGE",
28
28
  },
29
29
  };
30
30
 
31
- export const router = isModuleInitialized
32
- ? {
33
- assets: {
34
- hint: "List available Witnet Radon assets.",
35
- params: "[RADON_ASSETS ...]",
36
- options: {
37
- filter: {
38
- hint: "Restrict output to name-matching assets.",
39
- },
40
- legacy: {
41
- hint: "List only those declared in witnet/assets folder.",
42
- },
43
- modals: {
44
- hint: "Restrict output to Radon modals.",
45
- },
46
- requests: {
47
- hint: "Restrict output to Radon requests.",
48
- },
49
- templates: {
50
- hint: "Restrict output to Radon templates.",
51
- },
52
- },
31
+ export const router = {
32
+ assets: {
33
+ hint: "List available Witnet Radon assets.",
34
+ params: "[RADON_ASSETS ...]",
35
+ options: {
36
+ filter: {
37
+ hint: "Restrict output to name-matching assets.",
53
38
  },
54
- check: {
55
- hint: "Check correctness of Witnet Radon artifacts declared in witnet/assets folder.",
56
- params: [],
57
- options: {},
39
+ legacy: {
40
+ hint: "List only those declared within the ./witnet/assets subfolder.",
58
41
  },
59
- decode: {
60
- hint: "Break down specs of one or more Radon assets.",
61
- params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
62
- options: {
63
- bytecode: {
64
- hint: "Outputs RAD bytecode as hex string (supersedes --json).",
65
- },
66
- json: {
67
- hint: "Outputs data in JSON format.",
68
- },
69
- headline: {
70
- hint: "Settles output report headline.",
71
- param: ":string",
72
- },
73
- indent: {
74
- hint: "Prefixes given number of white spaces for every output line.",
75
- param: ":number",
76
- },
77
- },
42
+ modals: {
43
+ hint: "Restrict output to Radon modals.",
78
44
  },
79
- "dry-run": {
80
- hint: "Simulate resolution of one or more Radon assets, as if solved by the Wit/Oracle.",
81
- params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
82
- options: {
83
- default: {
84
- hint: "Use default sample parameters on parametrized Radon assets.",
85
- },
86
- json: {
87
- hint: "Outputs data in JSON format.",
88
- },
89
- headline: {
90
- hint: "Settles output report headline.",
91
- param: ":string",
92
- },
93
- indent: {
94
- hint: "Prefixes given number of white spaces for every output line.",
95
- param: ":number",
96
- },
97
- verbose: {
98
- hint: "Outputs detailed dry-run report.",
99
- },
100
- },
45
+ requests: {
46
+ hint: "Restrict output to Radon requests.",
101
47
  },
102
- }
103
- : {
104
- assets: {
105
- hint: "List available Witnet Radon assets.",
106
- params: "[RADON_ASSETS ...]",
107
- options: {
108
- filter: {
109
- hint: "Restrict output to name-matching assets.",
110
- },
111
- legacy: {
112
- hint: "List only those declared in witnet/assets folder.",
113
- },
114
- },
48
+ templates: {
49
+ hint: "Restrict output to Radon templates.",
115
50
  },
116
- decode: {
117
- hint: "Break down specs of one or more Radon assets.",
118
- params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
119
- options: {
120
- json: {
121
- hint: "Outputs data in JSON format.",
122
- },
123
- headline: {
124
- hint: "Settles output report headline.",
125
- param: ":string",
126
- },
127
- indent: {
128
- hint: "Prefixes given number of white spaces for every output line.",
129
- param: ":number",
130
- },
131
- },
51
+ },
52
+ },
53
+ decode: {
54
+ hint: "Break down specs of one or more Radon assets.",
55
+ params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
56
+ options: {
57
+ bytecode: {
58
+ hint: "Outputs RAD bytecode as hex string (supersedes --json).",
132
59
  },
133
- "dry-run": {
134
- hint: "Simulate resolution of one or more Radon assets, as if solved by the Wit/Oracle.",
135
- params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
136
- options: {
137
- json: {
138
- hint: "Outputs data in JSON format.",
139
- },
140
- headline: {
141
- hint: "Settles output report headline.",
142
- param: ":string",
143
- },
144
- indent: {
145
- hint: "Prefixes given number of white spaces for every output line.",
146
- param: ":number",
147
- },
148
- verbose: {
149
- hint: "Outputs detailed dry-run report.",
150
- },
151
- },
60
+ json: {
61
+ hint: "Outputs data in JSON format.",
62
+ },
63
+ headline: {
64
+ hint: "Settles output report headline.",
65
+ param: ":string",
66
+ },
67
+ indent: {
68
+ hint: "Prefixes given number of white spaces for every output line.",
69
+ param: ":number",
70
+ },
71
+ },
72
+ },
73
+ "dry-run": {
74
+ hint: "Simulate resolution of one or more Radon assets, as if solved by the Wit/Oracle.",
75
+ params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
76
+ options: {
77
+ default: {
78
+ hint: "Use default sample parameters on parametrized Radon assets.",
152
79
  },
153
- init: {
154
- hint: "Initialize a Witnet Radon workspace in this project.",
80
+ json: {
81
+ hint: "Outputs data in JSON format.",
155
82
  },
156
- };
83
+ headline: {
84
+ hint: "Settles output report headline.",
85
+ param: ":string",
86
+ },
87
+ indent: {
88
+ hint: "Prefixes given number of white spaces for every output line.",
89
+ param: ":number",
90
+ },
91
+ verbose: {
92
+ hint: "Outputs detailed dry-run report.",
93
+ },
94
+ },
95
+ },
96
+ ...(isModuleInitialized
97
+ ? {
98
+ check: {
99
+ hint: "Check correctness of Witnet Radon artifacts declared in witnet/assets folder.",
100
+ params: [],
101
+ options: {},
102
+ },
103
+ }
104
+ : {
105
+ init: {
106
+ hint: "Initialize a Witnet Radon workspace within the current folder.",
107
+ },
108
+ }),
109
+ };
157
110
 
158
111
  export const subcommands = {
159
112
  assets,
@@ -174,16 +127,16 @@ async function init() {
174
127
  fs.cpSync("node_modules/@witnet/sdk/.env_witnet", ".env_witnet");
175
128
  }
176
129
  if (!fs.existsSync("./witnet/assets/index.js")) {
177
- fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_index.js", "./witnet/assets/index.js");
130
+ fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_index.cjs", "./witnet/assets/index.cjs");
178
131
  }
179
132
  if (!fs.existsSync("./witnet/assets/requests.js")) {
180
- fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_requests.js", "./witnet/assets/requests.js");
133
+ fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_requests.cjs", "./witnet/assets/requests.cjs");
181
134
  }
182
135
  if (!fs.existsSync("./witnet/assets/sources.js")) {
183
- fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_sources.js", "./witnet/assets/sources.js");
136
+ fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_sources.cjs", "./witnet/assets/sources.cjs");
184
137
  }
185
138
  if (!fs.existsSync("./witnet/assets/templates.js")) {
186
- fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_templates.js", "./witnet/assets/templates.js");
139
+ fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_templates.cjs", "./witnet/assets/templates.cjs");
187
140
  }
188
141
  console.info(`Initialized Witnet Radon workspace at folder ${process.cwd()}/witnet/assets`);
189
142
  }
@@ -208,9 +161,8 @@ async function assets(options = {}, [...patterns]) {
208
161
 
209
162
  async function check() {
210
163
  if (!isModuleInitialized) {
211
- throw new Error(
212
- `No Witnet Radon workspace has been initialized. Please, run ${white("npx witsdk radon init")} if willing to declare your own Radon assets.`,
213
- );
164
+ console.error(`Sorry, no Witnet workspace was initialized yet.`);
165
+ return;
214
166
  }
215
167
  try {
216
168
  const assets = loadAssets({ legacy: true });
@@ -421,7 +373,9 @@ const stringifyReducer = (x, c) => {
421
373
 
422
374
  export function loadAssets(options) {
423
375
  const assets = options?.legacy ? {} : legacy;
424
- return isModuleInitialized ? merge(assets, require(`${WITNET_ASSETS_PATH}/index.cjs`)) : assets;
376
+ return fs.existsSync(`${WITNET_ASSETS_PATH}/index.cjs`)
377
+ ? merge(assets, require(`${WITNET_ASSETS_PATH}/index.cjs`))
378
+ : assets;
425
379
  }
426
380
 
427
381
  function flattenRadonArtifacts(tree, headers) {
@@ -587,7 +541,8 @@ function traceWitnetRadonRequest(request, options) {
587
541
  traceWitnetRadonReportHeadline(request, options);
588
542
  console.info(`${indent}╚══╤═══════════════════════════════════════════════════════════════════════════╝`);
589
543
  if (options?.bytecode) {
590
- console.info(request.toBytecode());
544
+ console.info(`${indent} v`);
545
+ console.info(helpers.colors.gray(request.toBytecode()));
591
546
  return;
592
547
  }
593
548
  console.info(`${indent}┌──┴─────────────────┐`);
@@ -623,16 +578,11 @@ function traceWitnetRadonRequest(request, options) {
623
578
  console.info(`${indent} │ ${sep} > HTTP body: ${helpers.colors.green(source.body)}`);
624
579
  }
625
580
  if (source?.script) {
626
- // console.log(source.script.toBytecode())
627
581
  const steps = source.script.disect();
628
582
  console.info(
629
- `${indent} │ ${sep} > Radon script: ${helpers.colors.lyellow(
630
- "[ ",
631
- )}${helpers.colors.yellow(steps[0][1])}${" ".repeat(12 - steps[0][1].length)}${helpers.colors.lyellow(
632
- " ]",
633
- )} ${helpers.colors.mcyan(steps[0][2])}`,
583
+ `${indent} │ ${sep} > Radon script: [ ${helpers.colors.gray(source.script.toBytecode())} ]`,
634
584
  );
635
- steps.slice(1).forEach((step) => {
585
+ steps.forEach((step) => {
636
586
  console.info(
637
587
  `${indent} │ ${sep} ${helpers.colors.lyellow(
638
588
  "[ ",
@@ -733,10 +683,12 @@ async function traceWitnetRadonRequestDryRun(request, options) {
733
683
  traceWitnetRadonReportHeadline(request, options);
734
684
  const indent = options?.indent ? " ".repeat(options.indent) : "";
735
685
  console.info(`${indent}╚══╤═══════════════════════════════════════════════════════════════════════════╝`);
736
- let execTimeMs = report.retrieve
737
- ?.map((retrieval) => (retrieval?.running_time.secs || 0) + (retrieval?.running_time.nanos || 0) / 1000)
738
- .reduce((sum, secs) => sum + secs);
739
- execTimeMs = `${Math.round(execTimeMs)} ms`;
686
+ let execTimeMs = Math.max(
687
+ ...(report.retrieve?.map(
688
+ (retrieval) => (retrieval?.running_time.secs || 0) * 1e3 + (retrieval?.running_time.nanos || 0) / 1e6,
689
+ ) ?? 0),
690
+ );
691
+ execTimeMs = `${helpers.commas(Math.round(execTimeMs))} ms`;
740
692
  let flexbar = "─".repeat(17);
741
693
  let flexspc = " ".repeat(flexbar.length + 12);
742
694
  console.info(`${indent}┌──┴─────────────────────────────${flexbar}──────┐`);
@@ -850,7 +802,7 @@ async function traceWitnetRadonRequestDryRun(request, options) {
850
802
  }
851
803
  if (resultSize) {
852
804
  console.info(
853
- `${indent}│ CBOR size: ${helpers.colors.cyan(
805
+ `${indent}│ Encoded size: ${helpers.colors.cyan(
854
806
  `${resultSize} bytes`,
855
807
  )}${" ".repeat(flexbar.length + 7 - resultSize.toString().length)} │`,
856
808
  );
@@ -1,7 +1,9 @@
1
1
  import { exec } from "node:child_process";
2
+ import fs from "node:fs";
2
3
  import { createRequire } from "node:module";
3
4
  import * as net from "node:net";
4
5
  import * as os from "node:os";
6
+ import path from "node:path";
5
7
  import * as readline from "node:readline";
6
8
  import moment from "moment";
7
9
 
@@ -595,6 +597,14 @@ export function prompter(promise) {
595
597
  });
596
598
  }
597
599
 
600
+ export function searchWorkspace() {
601
+ let workspace = path.normalize(process.cwd());
602
+ while (!fs.existsSync(`${workspace}/witnet/assets`) && path.dirname(workspace) !== workspace) {
603
+ workspace = path.dirname(workspace);
604
+ }
605
+ return workspace;
606
+ }
607
+
598
608
  export function traceChecklists(checklists) {
599
609
  if (checklists && Object.keys(checklists).length > 0) {
600
610
  const headlines = ["NODES", ...Object.keys(checklists).map((key) => `:${key}`)];
package/src/bin/index.js CHANGED
@@ -28,7 +28,7 @@ import {
28
28
 
29
29
  const __dirname = import.meta.dirname;
30
30
 
31
- const version = "2.0.21";
31
+ const version = "2.0.23";
32
32
  const toolkitDownloadUrlBase = `https://github.com/witnet/witnet-rust/releases/download/${version}/`;
33
33
  const toolkitDownloadNames = {
34
34
  win32: (arch) => `witnet_toolkit-${arch}-pc-windows-msvc.exe`,
@@ -274,10 +274,11 @@ async function main() {
274
274
  }
275
275
  } else {
276
276
  showUsage(cmd, module);
277
+ if (args?.[0]) console.error(colors.red(`\nInvalid subcommand: ${colors.mred(args[0])}`));
277
278
  }
278
279
  process.exit(0);
279
280
  } catch (e) {
280
- console.error(`EXCEPTION:\n${e}\n`);
281
+ console.error(colors.red(`EXCEPTION:\n${e}\n`));
281
282
  }
282
283
  }
283
284
  console.info("USAGE:");