@typeberry/convert 0.1.2-7ebe1f6 → 0.1.2-ced735c9

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 (3) hide show
  1. package/index.js +56 -190
  2. package/index.js.map +1 -1
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -4477,7 +4477,7 @@ function assert (t, m) {
4477
4477
 
4478
4478
  /***/ }),
4479
4479
 
4480
- /***/ 405:
4480
+ /***/ 786:
4481
4481
  /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
4482
4482
 
4483
4483
  module.exports = __nccwpck_require__.p + "6b655f8772c01b768329.js?bandersnatch_bg.wasm";
@@ -4605,8 +4605,6 @@ module.exports = __nccwpck_require__.p + "ccf8ada94096a8f232f5.js?reed_solomon_w
4605
4605
  /************************************************************************/
4606
4606
  var __webpack_exports__ = {};
4607
4607
 
4608
- ;// CONCATENATED MODULE: external "node:url"
4609
- const external_node_url_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:url");
4610
4608
  ;// CONCATENATED MODULE: ./packages/core/logger/options.ts
4611
4609
  var Level;
4612
4610
  (function (Level) {
@@ -4697,8 +4695,8 @@ function print(level, levelAndName, strings, data) {
4697
4695
  return;
4698
4696
  }
4699
4697
  const lvlText = Level[level].padEnd(5);
4700
- const val = strings.map((v, idx) => `${v}${idx < data.length ? data[idx] : ""}`);
4701
- const msg = `${lvlText} [${levelAndName[1]}] ${val.join("")}`;
4698
+ const val = strings.map((v, idx) => `${v}${data[idx]}`);
4699
+ const msg = `${lvlText} [${levelAndName[1]}] ${val}`;
4702
4700
  if (level === Level.WARN) {
4703
4701
  console.warn(msg);
4704
4702
  }
@@ -7599,7 +7597,7 @@ async function __wbg_init$2(module_or_path) {
7599
7597
  if (wasm$2 !== void 0) return wasm$2;
7600
7598
  if (typeof module_or_path !== "undefined") if (Object.getPrototypeOf(module_or_path) === Object.prototype) ({module_or_path} = module_or_path);
7601
7599
  else console.warn("using deprecated parameters for the initialization function; pass a single object instead");
7602
- if (typeof module_or_path === "undefined") module_or_path = new URL(/* asset import */ __nccwpck_require__(405), __nccwpck_require__.b);
7600
+ if (typeof module_or_path === "undefined") module_or_path = new URL(/* asset import */ __nccwpck_require__(786), __nccwpck_require__.b);
7603
7601
  const imports = __wbg_get_imports$2();
7604
7602
  if (typeof module_or_path === "string" || typeof Request === "function" && module_or_path instanceof Request || typeof URL === "function" && module_or_path instanceof URL) module_or_path = fetch(module_or_path);
7605
7603
  __wbg_init_memory$2(imports);
@@ -17475,17 +17473,6 @@ class page_range_PageRange {
17475
17473
  }
17476
17474
  return new page_range_PageRange(start, length);
17477
17475
  }
17478
- /** Returns true if the page range is wrapped (`start` >= `end`) and is not empty */
17479
- isWrapped() {
17480
- return this.start >= this.end && !this.isEmpty();
17481
- }
17482
- /** Checks if given page number is within the range */
17483
- isInRange(page) {
17484
- if (this.isWrapped()) {
17485
- return page >= this.start || page < this.end;
17486
- }
17487
- return page >= this.start && page < this.end;
17488
- }
17489
17476
  /** Checks if a range is empty (`length === 0`) */
17490
17477
  isEmpty() {
17491
17478
  return this.length === 0;
@@ -17865,11 +17852,10 @@ class memory_builder_MemoryBuilder {
17865
17852
  startHeapIndex (${startHeapIndex}) has to be less than or equal to endHeapIndex (${endHeapIndex})
17866
17853
  `;
17867
17854
  this.ensureNotFinalized();
17868
- const heapRange = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
17869
- const heapPagesRange = PageRange.fromMemoryRange(heapRange);
17870
- const initializedPageNumbers = Array.from(this.initialMemory.keys());
17871
- for (const pageNumber of initializedPageNumbers) {
17872
- if (heapPagesRange.isInRange(pageNumber)) {
17855
+ const range = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
17856
+ const pages = PageRange.fromMemoryRange(range);
17857
+ for (const pageNumber of pages) {
17858
+ if (this.initialMemory.has(pageNumber)) {
17873
17859
  throw new IncorrectSbrkIndex();
17874
17860
  }
17875
17861
  }
@@ -26823,12 +26809,6 @@ async function runPvmTest(testContent) {
26823
26809
 
26824
26810
 
26825
26811
 
26826
- function looseType(output) {
26827
- return {
26828
- value: output.value,
26829
- encode: output.encode,
26830
- };
26831
- }
26832
26812
  const SUPPORTED_TYPES = [
26833
26813
  {
26834
26814
  name: "block",
@@ -26846,12 +26826,8 @@ const SUPPORTED_TYPES = [
26846
26826
  run(spec, data, option) {
26847
26827
  const header = data;
26848
26828
  if (option === "as-hash") {
26849
- return looseType({
26850
- value: hashBytes(encoder_Encoder.encodeObject(header_Header.Codec, header, spec)),
26851
- encode: descriptors_codec.bytes(hash_HASH_SIZE),
26852
- });
26829
+ return hashBytes(encoder_Encoder.encodeObject(header_Header.Codec, header, spec));
26853
26830
  }
26854
- throw new Error(`Invalid processing option: ${option}`);
26855
26831
  },
26856
26832
  },
26857
26833
  },
@@ -26895,17 +26871,11 @@ const SUPPORTED_TYPES = [
26895
26871
  run(spec, data, option) {
26896
26872
  const state = data;
26897
26873
  if (option === "as-entries") {
26898
- return looseType({
26899
- value: Object.fromEntries(state_entries_StateEntries.serializeInMemory(spec, state)),
26900
- });
26874
+ return Object.fromEntries(state_entries_StateEntries.serializeInMemory(spec, state));
26901
26875
  }
26902
26876
  if (option === "as-root-hash") {
26903
- return looseType({
26904
- value: state_entries_StateEntries.serializeInMemory(spec, state).getRootHash(),
26905
- encode: descriptors_codec.bytes(hash_HASH_SIZE),
26906
- });
26877
+ return state_entries_StateEntries.serializeInMemory(spec, state).getRootHash();
26907
26878
  }
26908
- throw new Error(`Invalid processing option: ${option}`);
26909
26879
  },
26910
26880
  },
26911
26881
  },
@@ -26915,39 +26885,19 @@ const SUPPORTED_TYPES = [
26915
26885
  decode: StateTransitionGenesis.Codec,
26916
26886
  json: () => StateTransitionGenesis.fromJson,
26917
26887
  process: {
26918
- options: ["as-state", "as-jip4", "as-fuzz-message"],
26888
+ options: ["as-state", "as-jip4"],
26919
26889
  run(spec, data, option) {
26920
26890
  const test = data;
26921
26891
  if (option === "as-state") {
26922
- return looseType({
26923
- value: stateFromKeyvals(spec, test.state),
26924
- });
26892
+ return stateFromKeyvals(spec, test.state);
26925
26893
  }
26926
26894
  if (option === "as-jip4") {
26927
26895
  const genesisState = new Map(test.state.keyvals.map((x) => [x.key, x.value]));
26928
- return looseType({
26929
- value: JipChainSpec.create({
26930
- genesisHeader: encoder_Encoder.encodeObject(header_Header.Codec, test.header, spec),
26931
- genesisState,
26932
- }),
26896
+ return JipChainSpec.create({
26897
+ genesisHeader: encoder_Encoder.encodeObject(header_Header.Codec, test.header, spec),
26898
+ genesisState,
26933
26899
  });
26934
26900
  }
26935
- if (option === "as-fuzz-message") {
26936
- const init = Initialize.create({
26937
- header: test.header,
26938
- keyvals: test.state.keyvals,
26939
- ancestry: [],
26940
- });
26941
- const msg = {
26942
- type: types_MessageType.Initialize,
26943
- value: init,
26944
- };
26945
- return looseType({
26946
- value: msg,
26947
- encode: types_messageCodec,
26948
- });
26949
- }
26950
- throw new Error(`Invalid processing option: ${option}`);
26951
26901
  },
26952
26902
  },
26953
26903
  },
@@ -26957,32 +26907,15 @@ const SUPPORTED_TYPES = [
26957
26907
  decode: StateTransition.Codec,
26958
26908
  json: () => StateTransition.fromJson,
26959
26909
  process: {
26960
- options: ["as-pre-state", "as-post-state", "as-fuzz-message"],
26910
+ options: ["as-pre-state", "as-post-state"],
26961
26911
  run(spec, data, option) {
26962
26912
  const test = data;
26963
26913
  if (option === "as-pre-state") {
26964
- return looseType({
26965
- value: stateFromKeyvals(spec, test.pre_state),
26966
- });
26914
+ return stateFromKeyvals(spec, test.pre_state);
26967
26915
  }
26968
26916
  if (option === "as-post-state") {
26969
- return looseType({
26970
- value: stateFromKeyvals(spec, test.post_state),
26971
- });
26972
- }
26973
- if (option === "as-fuzz-message") {
26974
- const encoded = encoder_Encoder.encodeObject(block_Block.Codec, test.block, spec);
26975
- const blockView = decoder_Decoder.decodeObject(block_Block.Codec.View, encoded, spec);
26976
- const msg = {
26977
- type: types_MessageType.ImportBlock,
26978
- value: blockView,
26979
- };
26980
- return looseType({
26981
- value: msg,
26982
- encode: types_messageCodec,
26983
- });
26917
+ return stateFromKeyvals(spec, test.post_state);
26984
26918
  }
26985
- throw new Error(`Invalid processing option: ${option}`);
26986
26919
  },
26987
26920
  },
26988
26921
  },
@@ -27005,7 +26938,7 @@ const HELP = `
27005
26938
  @typeberry/convert ${package_namespaceObject.rE} by Fluffy Labs.
27006
26939
 
27007
26940
  Usage:
27008
- @typeberry/convert [options] <bin-hex-or-json-input-file> <type> [process] [output-format] [output-file]
26941
+ @typeberry/convert [options] <bin-hex-or-json-input-file> <type> [process] [output-format]
27009
26942
 
27010
26943
  Attempts to read provided input file as 'type' and output in requested 'output-format'.
27011
26944
  For some 'type's it's additionally possible to process the data before outputting it.
@@ -27014,7 +26947,6 @@ The input type is detected from file extension ('.bin', '.hex' or '.json').
27014
26947
  Example usage:
27015
26948
  @typeberry/convert ./genesis-header.json header to-hex
27016
26949
  @typeberry/convert ./state-snapshot.json state-dump as-entries to-json
27017
- @typeberry/convert ./state-snapshot.json stf-vector as-fuzz-message to-bin msg0.bin
27018
26950
 
27019
26951
  Options:
27020
26952
  --flavor - chain spec flavor, either 'full' or 'tiny'.
@@ -27024,7 +26956,6 @@ Output formats:
27024
26956
  to-print - Print the object to the console
27025
26957
  to-json - JSON format (when supported)
27026
26958
  to-hex - JAM-codec hex-encoded string (when supported)
27027
- to-bin - JAM-codec binary data (when supported)
27028
26959
  to-repl - Start a JavaScript REPL with the data loaded into a variable
27029
26960
 
27030
26961
  Input types:
@@ -27047,7 +26978,6 @@ var OutputFormat;
27047
26978
  OutputFormat["Print"] = "to-print";
27048
26979
  OutputFormat["Json"] = "to-json";
27049
26980
  OutputFormat["Hex"] = "to-hex";
27050
- OutputFormat["Bin"] = "to-bin";
27051
26981
  OutputFormat["Repl"] = "to-repl";
27052
26982
  })(OutputFormat || (OutputFormat = {}));
27053
26983
  function parseArgs(cliInput, withRelPath) {
@@ -27069,16 +26999,14 @@ function parseArgs(cliInput, withRelPath) {
27069
26999
  const type = parseType(args._.shift());
27070
27000
  const maybeProcess = args._.shift();
27071
27001
  const maybeOutputFormat = args._.shift();
27072
- const maybeDestination = args._.shift();
27073
27002
  assertNoMoreArgs(args);
27074
- const { process, format, destination } = getProcessFormatAndDestination(type, maybeProcess, maybeOutputFormat, maybeDestination);
27003
+ const { process, format } = getProcessAndOutput(type, maybeProcess, maybeOutputFormat);
27075
27004
  return {
27076
27005
  flavor: chainSpec.flavor,
27077
27006
  type,
27078
27007
  process,
27079
27008
  inputPath: withRelPath(input),
27080
27009
  outputFormat: format,
27081
- destination,
27082
27010
  };
27083
27011
  }
27084
27012
  function parseType(type) {
@@ -27104,21 +27032,10 @@ function parseOutputFormat(output) {
27104
27032
  return OutputFormat.Json;
27105
27033
  case OutputFormat.Repl:
27106
27034
  return OutputFormat.Repl;
27107
- case OutputFormat.Bin:
27108
- return OutputFormat.Bin;
27109
27035
  default:
27110
27036
  throw new Error(`Invalid output format: '${output}'.`);
27111
27037
  }
27112
27038
  }
27113
- function parseProcess(processOptions, maybeProcess) {
27114
- if (maybeProcess === undefined) {
27115
- return null;
27116
- }
27117
- if (!processOptions.includes(maybeProcess)) {
27118
- throw new Error(`Incorrect processing option: ${maybeProcess}. Expected one of: ${processOptions}.`);
27119
- }
27120
- return maybeProcess;
27121
- }
27122
27039
  // TODO [ToDr] Consider sharing that?
27123
27040
  function parseOption(args, option, parser, defaultValue) {
27124
27041
  if (args[option] === undefined) {
@@ -27154,64 +27071,33 @@ function assertNoMoreArgs(args) {
27154
27071
  throw new Error(`Unrecognized options: '${keysLeft}'`);
27155
27072
  }
27156
27073
  }
27157
- function getProcessFormatAndDestination(type, maybeProcess, maybeOutputFormat, maybeDestination) {
27158
- const defaultProcess = "";
27074
+ function getProcessAndOutput(type, maybeProcess, maybeOutputFormat) {
27159
27075
  const defaultFormat = parseOutputFormat(undefined);
27160
- const processOptions = type.process?.options ?? [];
27161
- // we have all three so it must be in order
27162
- if (maybeProcess !== undefined && maybeOutputFormat !== undefined && maybeDestination !== undefined) {
27163
- const format = parseOutputFormat(maybeOutputFormat);
27164
- const process = parseProcess(processOptions, maybeProcess) ?? defaultProcess;
27165
- const destination = maybeDestination;
27166
- throwIfDumpNotSupported(format, destination);
27167
- return { process, format, destination };
27168
- }
27169
- // we have either:
27170
- // 1. process + format
27171
- // 2. format + destination
27076
+ const options = type.process?.options ?? [];
27077
+ // we have both options, so we expect them in the right order.
27172
27078
  if (maybeProcess !== undefined && maybeOutputFormat !== undefined) {
27173
- // we've got processing first, so easy-peasy
27174
- if (processOptions.includes(maybeProcess)) {
27175
- const format = parseOutputFormat(maybeOutputFormat);
27176
- throwIfDumpNotSupported(format, null);
27177
- return { process: maybeProcess, format, destination: null };
27178
- }
27179
- // first one has to be format then.
27180
- const format = parseOutputFormat(maybeProcess);
27181
- const destination = maybeOutputFormat;
27182
- throwIfDumpNotSupported(format, destination);
27183
- return { process: defaultProcess, format, destination };
27079
+ const format = parseOutputFormat(maybeOutputFormat);
27080
+ if (!options.includes(maybeProcess)) {
27081
+ throw new Error(`Incorrect processing option: ${maybeProcess}. Expected one of: ${options}.`);
27082
+ }
27083
+ return { process: maybeProcess, format };
27184
27084
  }
27185
27085
  // only one parameter, but it can be either output or processing.
27186
- const destination = null;
27187
27086
  if (maybeProcess !== undefined) {
27188
- if (processOptions.includes(maybeProcess)) {
27189
- return { process: maybeProcess, format: defaultFormat, destination };
27087
+ if (options.includes(maybeProcess)) {
27088
+ return { process: maybeProcess, format: defaultFormat };
27190
27089
  }
27191
27090
  // now it should be output format, but we want to give a better error message,
27192
27091
  // if user mispelled processing.
27193
27092
  try {
27194
27093
  const format = parseOutputFormat(maybeProcess);
27195
- throwIfDumpNotSupported(format, destination);
27196
- return { process: defaultProcess, format, destination };
27094
+ return { process: "", format };
27197
27095
  }
27198
27096
  catch {
27199
27097
  throw new Error(`'${maybeProcess}' is neither output format nor processing parameter.`);
27200
27098
  }
27201
27099
  }
27202
- return { process: defaultProcess, format: defaultFormat, destination };
27203
- }
27204
- function throwIfDumpNotSupported(format, destination) {
27205
- if (destination !== null) {
27206
- if (format === OutputFormat.Print || format === OutputFormat.Repl) {
27207
- throw new Error(`Dumping to file is not supported for ${format}`);
27208
- }
27209
- }
27210
- else {
27211
- if (format === OutputFormat.Bin) {
27212
- throw new Error(`${format} requires destination file`);
27213
- }
27214
- }
27100
+ return { process: "", format: defaultFormat };
27215
27101
  }
27216
27102
 
27217
27103
  // EXTERNAL MODULE: ./node_modules/json-bigint-patch/dist/index.js
@@ -27271,13 +27157,9 @@ function loadInputFile(file, withRelPath) {
27271
27157
  throw new Error("Input file format unsupported.");
27272
27158
  }
27273
27159
  function dumpOutput(spec, data, type, outputFormat, args, withRelPath) {
27274
- const { destination } = args;
27275
- const dump = destination !== null
27276
- ? (v) => external_node_fs_default().writeFileSync(withRelPath(destination), v)
27277
- : (v) => console.info(v);
27278
27160
  switch (outputFormat) {
27279
27161
  case OutputFormat.Print: {
27280
- dump(`${debug_inspect(data)}`);
27162
+ console.info(`${debug_inspect(data)}`);
27281
27163
  return;
27282
27164
  }
27283
27165
  case OutputFormat.Hex: {
@@ -27285,24 +27167,13 @@ function dumpOutput(spec, data, type, outputFormat, args, withRelPath) {
27285
27167
  throw new Error(`${type.name} does not support encoding to JAM codec.`);
27286
27168
  }
27287
27169
  const encoded = encoder_Encoder.encodeObject(type.encode, data, spec);
27288
- dump(`${encoded}`);
27289
- return;
27290
- }
27291
- case OutputFormat.Bin: {
27292
- if (type.encode === undefined) {
27293
- throw new Error(`${type.name} does not support encoding to JAM codec.`);
27294
- }
27295
- if (destination === null) {
27296
- throw new Error(`${OutputFormat.Bin} requires destination file.`);
27297
- }
27298
- const encoded = encoder_Encoder.encodeObject(type.encode, data, spec);
27299
- dump(encoded.raw);
27170
+ console.info(`${encoded}`);
27300
27171
  return;
27301
27172
  }
27302
27173
  case OutputFormat.Json: {
27303
27174
  // TODO [ToDr] this will probably not work for all cases,
27304
27175
  // but for now may be good enough.
27305
- dump(toJson(data));
27176
+ console.info(toJson(data));
27306
27177
  return;
27307
27178
  }
27308
27179
  case OutputFormat.Repl: {
@@ -27379,14 +27250,12 @@ function processOutput(spec, data, type, process) {
27379
27250
  if (type.process === undefined || !type.process.options.includes(process)) {
27380
27251
  throw new Error(`Unsupported processing: '${process}' for '${type.name}'`);
27381
27252
  }
27382
- const processed = type.process.run(spec, data, process);
27383
27253
  return {
27384
- processed: processed.value,
27254
+ processed: type.process.run(spec, data, process),
27385
27255
  type: {
27386
27256
  ...type,
27387
- name: `${type.name}(${process})`,
27388
- // use encoding from processed type
27389
- encode: processed.encode,
27257
+ // disable encoding, since it won't match
27258
+ encode: undefined,
27390
27259
  },
27391
27260
  };
27392
27261
  }
@@ -27423,26 +27292,23 @@ function loadAndProcessDataFile(file, withRelPath, flavor, decodeType, process)
27423
27292
 
27424
27293
 
27425
27294
 
27426
-
27427
- if (import.meta.url === (0,external_node_url_namespaceObject.pathToFileURL)(process.argv[1]).href) {
27428
- Logger.configureAll(process.env.JAM_LOG ?? "", Level.LOG);
27429
- const withRelPath = workspacePathFix(`${import.meta.dirname}/../..`);
27430
- let args;
27431
- try {
27432
- args = parseArgs(process.argv.slice(2), withRelPath);
27433
- }
27434
- catch (e) {
27435
- console.error(`\n${e}\n`);
27436
- console.info(HELP);
27437
- process.exit(1);
27438
- }
27439
- try {
27440
- main(args, withRelPath);
27441
- }
27442
- catch (e) {
27443
- console.error(`${e}`);
27444
- process.exit(-1);
27445
- }
27295
+ Logger.configureAll(process.env.JAM_LOG ?? "", Level.LOG);
27296
+ const withRelPath = workspacePathFix(`${import.meta.dirname}/../..`);
27297
+ let args;
27298
+ try {
27299
+ args = parseArgs(process.argv.slice(2), withRelPath);
27300
+ }
27301
+ catch (e) {
27302
+ console.error(`\n${e}\n`);
27303
+ console.info(HELP);
27304
+ process.exit(1);
27305
+ }
27306
+ try {
27307
+ main(args, withRelPath);
27308
+ }
27309
+ catch (e) {
27310
+ console.error(`${e}`);
27311
+ process.exit(-1);
27446
27312
  }
27447
27313
 
27448
27314