@leofcoin/chain 1.8.30 → 1.9.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.
@@ -325,53 +325,6 @@ var index$3 = (input, prefix) => {
325
325
  return typedArray;
326
326
  };
327
327
 
328
- var index$2 = (typedArray, prefix) => {
329
- const set = [];
330
- const varintAndSub = (typedArray) => {
331
- const length = index$4.decode(typedArray);
332
- // remove length
333
- typedArray = typedArray.subarray(index$4.decode.bytes);
334
- // push value
335
- set.push(typedArray.subarray(0, length));
336
- // remove value
337
- typedArray = typedArray.subarray(length);
338
- if (typedArray.length !== 0)
339
- return varintAndSub(typedArray);
340
- return set;
341
- };
342
- return varintAndSub(typedArray);
343
- };
344
-
345
- const ALPHABET$1 = '0123456789ABCDEF';
346
- base(ALPHABET$1);
347
-
348
- const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
349
- base(ALPHABET);
350
-
351
- /**
352
- * Returns a Uint8Array as String
353
- * @param uint8Array Uint8Array to encode to String
354
- * @returns String
355
- */
356
- const toString$3 = (uint8Array) => new TextDecoder().decode(uint8Array);
357
- /**
358
- * hexString -> uint8Array
359
- * @param string hex encoded string
360
- * @returns UintArray
361
- */
362
- const fromHex = (string) => Uint8Array.from(string.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
363
- /**
364
- * uint8Array -> hexString
365
- * @param bytes number[]
366
- * @returns hexString
367
- */
368
- const toHex = (bytes) => bytes.reduce((string, byte) => string + byte.toString(16).padStart(2, '0'), '');
369
- const toBase58 = (uint8Array) => base58$1.encode(uint8Array);
370
- const fromBase58 = (string) => base58$1.decode(string);
371
- const toBase32 = (uint8Array) => index$5.encode(uint8Array);
372
- var index$1 = {
373
- toString: toString$3};
374
-
375
328
  /*! pako 2.1.0 https://github.com/nodeca/pako @license (MIT AND Zlib) */
376
329
  // (C) 1995-2013 Jean-loup Gailly and Mark Adler
377
330
  // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
@@ -4101,7 +4054,7 @@ function ZStream() {
4101
4054
 
4102
4055
  var zstream = ZStream;
4103
4056
 
4104
- const toString$1 = Object.prototype.toString;
4057
+ const toString$1$1 = Object.prototype.toString;
4105
4058
 
4106
4059
  /* Public constants ==========================================================*/
4107
4060
  /* ===========================================================================*/
@@ -4253,7 +4206,7 @@ function Deflate$1(options) {
4253
4206
  if (typeof opt.dictionary === 'string') {
4254
4207
  // If we need to compress text, change encoding to utf8.
4255
4208
  dict = strings.string2buf(opt.dictionary);
4256
- } else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') {
4209
+ } else if (toString$1$1.call(opt.dictionary) === '[object ArrayBuffer]') {
4257
4210
  dict = new Uint8Array(opt.dictionary);
4258
4211
  } else {
4259
4212
  dict = opt.dictionary;
@@ -4305,7 +4258,7 @@ Deflate$1.prototype.push = function (data, flush_mode) {
4305
4258
  if (typeof data === 'string') {
4306
4259
  // If we need to compress text, change encoding to utf8.
4307
4260
  strm.input = strings.string2buf(data);
4308
- } else if (toString$1.call(data) === '[object ArrayBuffer]') {
4261
+ } else if (toString$1$1.call(data) === '[object ArrayBuffer]') {
4309
4262
  strm.input = new Uint8Array(data);
4310
4263
  } else {
4311
4264
  strm.input = data;
@@ -7242,88 +7195,141 @@ var pako = {
7242
7195
  };
7243
7196
 
7244
7197
  const jsonStringifyBigInt$1 = (key, value) => (typeof value === 'bigint' ? { $bigint: value.toString() } : value);
7245
- const jsonParseBigInt$1 = (key, value) => typeof value === 'object' && value.$bigint ? BigInt(value.$bigint) : value;
7198
+ const jsonParseBigInt$1 = (key, value) => typeof value === 'object' && value?.$bigint ? BigInt(value.$bigint) : value;
7246
7199
 
7247
- const { toString } = index$1;
7248
- const isString = (type) => type === 'string';
7249
- const isNumber = (type) => type === 'number';
7250
- const isBoolean = (type) => type === 'boolean';
7251
- const isUint8Array = (type) => type === 'uint8Array';
7252
- const isBigInt = (type) => type === 'bigint';
7200
+ const tokenCache = new WeakMap();
7253
7201
  const tokenize = (key, value) => {
7254
7202
  const optional = key.endsWith('?');
7255
7203
  let type = value === undefined ? key : value;
7256
7204
  if (type instanceof Uint8Array)
7257
7205
  type = 'uint8Array';
7258
- else if (type instanceof BigInt)
7206
+ else if (typeof type === 'bigint')
7259
7207
  type = 'bigint';
7260
7208
  else
7261
7209
  type = Array.isArray(type) ? 'array' : typeof type;
7262
7210
  const parts = key.split('?');
7263
- const minimumLength = parts[2]?.includes('min') ? parts[2].split['min:'][1] : 0;
7264
- return { type, optional, key: parts[0], minimumLength };
7211
+ const minimumLength = parts[2]?.includes('min') ? Number(parts[2].split('min:')[1]) : 0;
7212
+ return { type, optional, key: parts[0], minimumLength, defaultValue: value };
7265
7213
  };
7214
+ const getTokens = (proto) => {
7215
+ if (tokenCache.has(proto))
7216
+ return tokenCache.get(proto);
7217
+ const keys = Object.keys(proto);
7218
+ const values = Object.values(proto);
7219
+ const tokens = keys.map((k, i) => tokenize(k, values[i]));
7220
+ tokenCache.set(proto, tokens);
7221
+ return tokens;
7222
+ };
7223
+
7224
+ const encoder = new TextEncoder();
7266
7225
  const toType = (data) => {
7267
- // always return uint8Arrays as they are
7268
7226
  if (data instanceof Uint8Array)
7269
7227
  return data;
7270
- // returns the ArrayBuffer as UintArray
7271
7228
  if (data instanceof ArrayBuffer)
7272
7229
  return new Uint8Array(data);
7273
- // returns the BigTnt string as UintArray
7274
7230
  if (typeof data === 'bigint')
7275
- return new TextEncoder().encode(data.toString());
7276
- // returns the string as UintArray
7231
+ return encoder.encode(data.toString());
7277
7232
  if (typeof data === 'string')
7278
- return new TextEncoder().encode(data);
7279
- // returns the object as UintArray
7233
+ return encoder.encode(data);
7280
7234
  if (typeof data === 'object')
7281
- return new TextEncoder().encode(JSON.stringify(data, jsonStringifyBigInt$1));
7282
- // returns the number as UintArray
7235
+ return encoder.encode(JSON.stringify(data, jsonStringifyBigInt$1));
7283
7236
  if (typeof data === 'number' || typeof data === 'boolean')
7284
- return new TextEncoder().encode(data.toString());
7237
+ return encoder.encode(data.toString());
7285
7238
  throw new Error(`unsuported type ${typeof data || data}`);
7286
7239
  };
7287
7240
  const encode = (proto, input, compress) => {
7288
- const keys = Object.keys(proto);
7289
- const values = Object.values(proto);
7241
+ const tokens = getTokens(proto);
7290
7242
  const set = [];
7291
- for (let i = 0; i < values.length; i++) {
7292
- const token = tokenize(keys[i], values[i]);
7243
+ for (let i = 0; i < tokens.length; i++) {
7244
+ const token = tokens[i];
7293
7245
  const data = input[token.key];
7294
7246
  if (!token.optional && data === undefined)
7295
7247
  throw new Error(`missing required property: ${token.key}`);
7296
7248
  if ((token.type === 'array' && token.minimumLength > data?.length) ||
7297
7249
  (token.type === 'object' && token.minimumLength > Object.keys(data).length))
7298
7250
  throw new Error(`minimumLength for ${token.key} is set to ${token.minimumLength} but got ${data.length}`);
7299
- // always push data to the set.
7300
- // when data is undefined push the default value of the proto
7301
- set.push(toType(data || values[i]));
7251
+ set.push(toType(data !== undefined ? data : token.defaultValue));
7302
7252
  }
7303
7253
  return compress ? pako.deflate(index$3(set)) : index$3(set);
7304
7254
  };
7255
+
7256
+ var index$2 = (typedArray, prefix) => {
7257
+ const set = [];
7258
+ const varintAndSub = (typedArray) => {
7259
+ const length = index$4.decode(typedArray);
7260
+ // remove length
7261
+ typedArray = typedArray.subarray(index$4.decode.bytes);
7262
+ // push value
7263
+ set.push(typedArray.subarray(0, length));
7264
+ // remove value
7265
+ typedArray = typedArray.subarray(length);
7266
+ if (typedArray.length !== 0)
7267
+ return varintAndSub(typedArray);
7268
+ return set;
7269
+ };
7270
+ return varintAndSub(typedArray);
7271
+ };
7272
+
7273
+ const ALPHABET$1 = '0123456789ABCDEF';
7274
+ base(ALPHABET$1);
7275
+
7276
+ const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
7277
+ base(ALPHABET);
7278
+
7279
+ /**
7280
+ * Returns a Uint8Array as String
7281
+ * @param uint8Array Uint8Array to encode to String
7282
+ * @returns String
7283
+ */
7284
+ const toString$1 = (uint8Array) => new TextDecoder().decode(uint8Array);
7285
+ /**
7286
+ * hexString -> uint8Array
7287
+ * @param string hex encoded string
7288
+ * @returns UintArray
7289
+ */
7290
+ const fromHex = (string) => Uint8Array.from(string.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
7291
+ /**
7292
+ * uint8Array -> hexString
7293
+ * @param bytes number[]
7294
+ * @returns hexString
7295
+ */
7296
+ const toHex = (bytes) => bytes.reduce((string, byte) => string + byte.toString(16).padStart(2, '0'), '');
7297
+ /**
7298
+ * number[] -> Uint8Array
7299
+ * @param array number[]
7300
+ * @returns Uint8Array
7301
+ */
7302
+ const fromArrayLike = (array) => Uint8Array.from(array);
7303
+ const toBase58 = (uint8Array) => base58$1.encode(uint8Array);
7304
+ const fromBase58 = (string) => base58$1.decode(string);
7305
+ const toBase32 = (uint8Array) => index$5.encode(uint8Array);
7306
+ var index$1 = {
7307
+ toString: toString$1};
7308
+
7309
+ const { toString } = index$1;
7310
+ const decoder = new TextDecoder();
7305
7311
  const decode = (proto, uint8Array, compressed) => {
7306
7312
  if (compressed)
7307
7313
  uint8Array = pako.inflate(uint8Array);
7308
7314
  let deconcated = index$2(uint8Array);
7309
7315
  const output = {};
7310
- const keys = Object.keys(proto);
7311
- const values = Object.values(proto);
7312
- if (keys.length !== deconcated.length)
7313
- console.warn(`length mismatch: expected ${keys.length} got ${uint8Array.length}`);
7314
- for (let i = 0; i < values.length; i++) {
7315
- const token = tokenize(keys[i], values[i]);
7316
- if (isUint8Array(token.type))
7316
+ const tokens = getTokens(proto);
7317
+ if (tokens.length !== deconcated.length)
7318
+ console.warn(`length mismatch: expected ${tokens.length} got ${uint8Array.length}`);
7319
+ for (let i = 0; i < tokens.length; i++) {
7320
+ const token = tokens[i];
7321
+ if (token.type === 'uint8Array')
7317
7322
  output[token.key] = deconcated[i];
7318
- else if (isString(token.type))
7323
+ else if (token.type === 'string')
7319
7324
  output[token.key] = toString(deconcated[i]);
7320
- else if (isBoolean(token.type))
7321
- output[token.key] = new TextDecoder().decode(deconcated[i]) === 'true';
7322
- else if (isNumber(token.type))
7323
- output[token.key] = Number(new TextDecoder().decode(deconcated[i]));
7324
- else if (isBigInt(token.type))
7325
- output[token.key] = BigInt(new TextDecoder().decode(deconcated[i]));
7326
- else output[token.key] = JSON.parse(new TextDecoder().decode(deconcated[i]), jsonParseBigInt$1);
7325
+ else if (token.type === 'boolean')
7326
+ output[token.key] = decoder.decode(deconcated[i]) === 'true';
7327
+ else if (token.type === 'number')
7328
+ output[token.key] = Number(decoder.decode(deconcated[i]));
7329
+ else if (token.type === 'bigint')
7330
+ output[token.key] = BigInt(decoder.decode(deconcated[i]));
7331
+ else if (token.type === 'object' || token.type === 'array')
7332
+ output[token.key] = JSON.parse(decoder.decode(deconcated[i]), jsonParseBigInt$1);
7327
7333
  if (token.optional) {
7328
7334
  if (!output[token.key] || output[token.key].length === 0)
7329
7335
  delete output[token.key];
@@ -7333,10 +7339,8 @@ const decode = (proto, uint8Array, compressed) => {
7333
7339
  }
7334
7340
  return output;
7335
7341
  };
7336
- var index = {
7337
- encode,
7338
- decode
7339
- };
7342
+
7343
+ var index = { encode, decode };
7340
7344
 
7341
7345
  const jsonStringifyBigInt = (key, value) => {
7342
7346
  if (typeof value === 'bigint')
@@ -7350,13 +7354,13 @@ const jsonParseBigInt = (key, value) => {
7350
7354
  }
7351
7355
  return value;
7352
7356
  };
7353
- const _textEncoder = new TextEncoder();
7354
- const _textDecoder = new TextDecoder();
7355
7357
  class BasicInterface {
7356
7358
  #encoded;
7357
7359
  #decoded;
7358
7360
  name;
7359
7361
  #proto;
7362
+ textEncoder = new TextEncoder();
7363
+ textDecoder = new TextDecoder();
7360
7364
  get keys() {
7361
7365
  // handles proto keys
7362
7366
  // protokey -> key
@@ -7384,11 +7388,42 @@ class BasicInterface {
7384
7388
  get proto() {
7385
7389
  return this.#proto;
7386
7390
  }
7391
+ from(value) {
7392
+ if (value instanceof Uint8Array)
7393
+ this.fromUint8Array(value);
7394
+ else if (value instanceof ArrayBuffer)
7395
+ this.fromArrayBuffer(value);
7396
+ else if (typeof value === 'string' && isHex(value)) {
7397
+ this.fromHex(value);
7398
+ }
7399
+ else if (typeof value === 'string') {
7400
+ if (base58$1.isBase58(value)) {
7401
+ try {
7402
+ this.fromBs58(value);
7403
+ }
7404
+ catch {
7405
+ if (index$5.isBase32(value))
7406
+ this.fromBs32(value);
7407
+ else
7408
+ throw new Error('Invalid base58/base32 input');
7409
+ }
7410
+ }
7411
+ else if (index$5.isBase32(value))
7412
+ this.fromBs32(value);
7413
+ else
7414
+ this.fromString(value);
7415
+ }
7416
+ else if (Array.isArray(value))
7417
+ this.fromArray(value);
7418
+ else if (typeof value === 'object')
7419
+ this.create(value);
7420
+ return this;
7421
+ }
7387
7422
  decode(encoded) {
7388
7423
  encoded = encoded || this.encoded;
7389
7424
  // Example: decode as JSON if possible (override in subclass)
7390
7425
  try {
7391
- return JSON.parse(_textDecoder.decode(encoded), jsonParseBigInt);
7426
+ return JSON.parse(this.textDecoder.decode(encoded), jsonParseBigInt);
7392
7427
  }
7393
7428
  catch {
7394
7429
  return new Object();
@@ -7397,9 +7432,16 @@ class BasicInterface {
7397
7432
  encode(decoded) {
7398
7433
  decoded = decoded || this.decoded;
7399
7434
  // Example: encode as JSON (override in subclass)
7400
- return _textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt));
7435
+ return this.textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt));
7401
7436
  }
7402
7437
  // get Codec(): Codec {}
7438
+ create(options) {
7439
+ for (const key of this.keys) {
7440
+ if (options[key]) {
7441
+ this[key] = options[key];
7442
+ }
7443
+ }
7444
+ }
7403
7445
  // Cache proto keys/values for reuse
7404
7446
  static _protoCache = new WeakMap();
7405
7447
  protoEncode(data) {
@@ -7466,8 +7508,26 @@ class BasicInterface {
7466
7508
  return this.decode(array);
7467
7509
  return this.decode(Uint8Array.from(array));
7468
7510
  }
7469
- fromEncoded(encoded) {
7470
- return this.decode(encoded);
7511
+ fromArrayBuffer(uint8Array) {
7512
+ return fromArrayLike(new Uint8Array(uint8Array));
7513
+ }
7514
+ fromUint8Array(uint8Array) {
7515
+ return fromArrayLike(uint8Array);
7516
+ }
7517
+ toUint8Array(format) {
7518
+ if (format === 'bs32')
7519
+ return toBase32(this.encoded);
7520
+ if (format === 'bs58')
7521
+ return toBase58(this.encoded);
7522
+ if (format === 'hex')
7523
+ return toHex(this.encoded);
7524
+ if (format === 'utf8')
7525
+ return this.encoded.toString();
7526
+ if (format === 'uint8Array')
7527
+ return this.encoded;
7528
+ if (format === 'uint8ArrayString')
7529
+ return this.encoded.toString();
7530
+ throw new Error(`Unknown format: ${format}`);
7471
7531
  }
7472
7532
  toString() {
7473
7533
  if (!this.encoded)
@@ -8525,34 +8585,11 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8525
8585
  set encoded(value) {
8526
8586
  this.#encoded = value;
8527
8587
  }
8528
- init(buffer) {
8529
- if (buffer instanceof FormatInterface && buffer?.name === this.name) {
8530
- return buffer;
8531
- }
8532
- else if (buffer instanceof Uint8Array) {
8533
- this.fromUint8Array(buffer);
8534
- }
8535
- else if (buffer instanceof ArrayBuffer) {
8536
- this.fromArrayBuffer(buffer);
8537
- }
8538
- else if (typeof buffer === 'string') {
8539
- if (this.isHex(buffer))
8540
- this.fromHex(buffer);
8541
- else if (this.isBase58(buffer))
8542
- this.fromBs58(buffer);
8543
- else if (this.isBase32(buffer))
8544
- this.fromBs32(buffer);
8545
- else
8546
- this.fromString(buffer);
8547
- }
8548
- else if (typeof buffer === 'object' && buffer !== null) {
8549
- this.create(buffer);
8588
+ init(value) {
8589
+ if (value instanceof FormatInterface && value?.name === this.name) {
8590
+ return value;
8550
8591
  }
8551
- else {
8552
- // Explicitly reject all other types (number, boolean, undefined, symbol, function, null)
8553
- throw new TypeError(`Invalid input type for FormatInterface: ${typeof buffer}`);
8554
- }
8555
- return this;
8592
+ return this.from(value);
8556
8593
  }
8557
8594
  hasCodec() {
8558
8595
  if (!this.encoded)
@@ -8670,13 +8707,13 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8670
8707
  this.encoded = buffer;
8671
8708
  return this.hasCodec()
8672
8709
  ? this.decode()
8673
- : this.create(JSON.parse(BasicInterface._textDecoder.decode(this.encoded), jsonParseBigInt));
8710
+ : this.create(JSON.parse(this.textDecoder.decode(this.encoded), jsonParseBigInt));
8674
8711
  }
8675
8712
  fromArrayBuffer(buffer) {
8676
8713
  this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
8677
8714
  return this.hasCodec()
8678
8715
  ? this.decode()
8679
- : this.create(JSON.parse(BasicInterface._textDecoder.decode(this.encoded), jsonParseBigInt));
8716
+ : this.create(JSON.parse(this.textDecoder.decode(this.encoded), jsonParseBigInt));
8680
8717
  }
8681
8718
  /**
8682
8719
  * @param {Object} data
@@ -8725,7 +8762,10 @@ var proto$2 = {
8725
8762
  reward: BigInt(0),
8726
8763
  fees: BigInt(0),
8727
8764
  transactions: Array(),
8728
- validators: new Uint8Array()
8765
+ validators: new Uint8Array(),
8766
+ producer: String(),
8767
+ producerProof: String(),
8768
+ protocolVersion: String()
8729
8769
  };
8730
8770
 
8731
8771
  class BlockMessage extends FormatInterface {
@@ -8816,82 +8856,121 @@ class TransactionMessage extends FormatInterface {
8816
8856
  ({
8817
8857
  params: Array()});
8818
8858
 
8819
- class EasyWorker {
8820
- #messageEvent = 'message'
8821
- #errorEvent = 'error'
8822
- #isBrowser = false
8823
- #isWorker = false
8824
-
8825
- get isWorker() {
8826
- return this.#isWorker
8827
- }
8828
- constructor(url, options) {
8829
- return this.#init(url, options)
8830
- }
8831
-
8832
- #init(url, options = {}) {
8833
- if (url) {
8834
- if (globalThis.Worker) {
8835
- this.#isBrowser = true;
8836
- this.worker = new Worker(url, {...options});
8837
- } else {
8838
- return new Promise(async (resolve, reject) => {
8839
- const {fork} = await import('child_process');
8840
- this.worker = fork(url, ['easy-worker-child'], options);
8841
- resolve(this);
8842
- })
8843
- }
8844
- } else {
8845
- this.#isWorker = true;
8846
- if (globalThis.process?.argv[2] === 'easy-worker-child') {
8847
- this.worker = process;
8848
- } else {
8849
- this.#isBrowser = true;
8850
- this.worker = globalThis;
8851
- }
8852
- }
8853
-
8854
- return this
8855
- }
8856
-
8857
- onmessage(fn) {
8858
- if (this.#isBrowser) this.worker.onmessage = ({data}) => fn(data);
8859
- else this.worker.on(this.#messageEvent, fn);
8860
- }
8861
-
8862
- postMessage(message) {
8863
- if (this.#isBrowser) this.worker.postMessage(message);
8864
- else this.worker.send(message);
8865
- }
8866
-
8867
- terminate() {
8868
- if (this.#isBrowser) this.worker.terminate();
8869
- else this.worker.kill();
8870
- }
8871
-
8872
- onerror(fn) {
8873
- if (this.#isBrowser) this.worker.onerror = fn;
8874
- else this.worker.on(this.#errorEvent, fn);
8875
- }
8876
-
8877
- /**
8878
- *
8879
- * @param {*} data
8880
- * @returns {Promise} resolves result onmessage & rejects on error
8881
- */
8882
- once(data) {
8883
- return new Promise((resolve, reject) => {
8884
- this.onmessage(message => {
8885
- resolve(message);
8886
- this.terminate();
8887
- });
8888
- this.onerror(error => {
8889
- reject(error);
8890
- this.terminate();
8891
- });
8892
- this.postMessage(data);
8893
- })
8894
- }
8859
+ class EasyWorker {
8860
+ #messageEvent = 'message';
8861
+ #errorEvent = 'error';
8862
+ #isBrowser = false;
8863
+ #isWorker = false;
8864
+ #messageHandler = null;
8865
+ #errorHandler = null;
8866
+ worker;
8867
+ get isWorker() {
8868
+ return this.#isWorker;
8869
+ }
8870
+ constructor(url, options) {
8871
+ return this.#init(url, options);
8872
+ }
8873
+ #init(url, options = {}) {
8874
+ if (url) {
8875
+ if (globalThis.Worker) {
8876
+ this.#isBrowser = true;
8877
+ this.worker = new Worker(url, { ...options });
8878
+ }
8879
+ else {
8880
+ return new Promise((resolve, reject) => {
8881
+ import('child_process').then(({ fork }) => {
8882
+ const child = fork(url, ['easy-worker-child'], options);
8883
+ child.once('error', reject);
8884
+ this.worker = child;
8885
+ resolve(this);
8886
+ }).catch(reject);
8887
+ });
8888
+ }
8889
+ }
8890
+ else {
8891
+ this.#isWorker = true;
8892
+ if (globalThis.process?.argv[2] === 'easy-worker-child') {
8893
+ this.worker = process;
8894
+ }
8895
+ else {
8896
+ this.#isBrowser = true;
8897
+ this.worker = globalThis;
8898
+ }
8899
+ }
8900
+ return this;
8901
+ }
8902
+ onmessage(fn) {
8903
+ if (this.#isBrowser) {
8904
+ this.worker.onmessage = ({ data }) => fn(data);
8905
+ }
8906
+ else {
8907
+ if (this.#messageHandler)
8908
+ this.worker.off(this.#messageEvent, this.#messageHandler);
8909
+ this.#messageHandler = fn;
8910
+ this.worker.on(this.#messageEvent, fn);
8911
+ }
8912
+ }
8913
+ postMessage(message) {
8914
+ if (this.#isBrowser)
8915
+ this.worker.postMessage(message);
8916
+ else
8917
+ this.worker.send(message);
8918
+ }
8919
+ terminate() {
8920
+ if (this.#isBrowser)
8921
+ this.worker.terminate();
8922
+ else
8923
+ this.worker.kill();
8924
+ }
8925
+ onerror(fn) {
8926
+ if (this.#isBrowser) {
8927
+ this.worker.onerror = fn;
8928
+ }
8929
+ else {
8930
+ if (this.#errorHandler)
8931
+ this.worker.off(this.#errorEvent, this.#errorHandler);
8932
+ this.#errorHandler = fn;
8933
+ this.worker.on(this.#errorEvent, fn);
8934
+ }
8935
+ }
8936
+ /**
8937
+ *
8938
+ * @param {*} data
8939
+ * @returns {Promise} resolves result onmessage & rejects on error
8940
+ */
8941
+ once(data) {
8942
+ return new Promise((resolve, reject) => {
8943
+ if (this.#isBrowser) {
8944
+ const handleMessage = ({ data: result }) => {
8945
+ this.worker.onerror = null;
8946
+ resolve(result);
8947
+ this.terminate();
8948
+ };
8949
+ const handleError = (error) => {
8950
+ this.worker.onmessage = null;
8951
+ reject(error);
8952
+ this.terminate();
8953
+ };
8954
+ this.worker.onmessage = handleMessage;
8955
+ this.worker.onerror = handleError;
8956
+ }
8957
+ else {
8958
+ const handleMessage = (result) => {
8959
+ this.worker.off(this.#errorEvent, handleError);
8960
+ resolve(result);
8961
+ this.terminate();
8962
+ };
8963
+ const handleError = (error) => {
8964
+ this.worker.off(this.#messageEvent, handleMessage);
8965
+ reject(error);
8966
+ this.terminate();
8967
+ };
8968
+ this.worker.once(this.#messageEvent, handleMessage);
8969
+ this.worker.once(this.#errorEvent, handleError);
8970
+ }
8971
+ this.postMessage(data);
8972
+ });
8973
+ }
8895
8974
  }
8896
8975
 
8897
8976
  export { BlockMessage as B, ContractMessage as C, EasyWorker as E, TransactionMessage as T };