@typeberry/convert 0.1.2-60899f7 → 0.1.2-726ffce6

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