ivosh / node-nist

A simple low-level ANSI/NIST-ITL 1-2011 (update 2015) encoding and decoding utility library. Written in Typescript for Node.
MIT License
12 stars 6 forks source link

Decoding example #3

Closed Edza closed 1 year ago

Edza commented 4 years ago

Hi,

I'm currently trying to get decode to work. Sadly, there is no example. When I figure it out, I might write up an example in my fork and create a pull request. However, it would probably be faster for you to do it.

There are:

export type NistDecodeOptions = NistCodecOptions<
  NistFieldDecodeOptions,
  NistRecordDecodeOptions,
  NistFileEncodeOptions
>;
export interface NistFieldDecodeOptions extends NistFieldCodecOptions {
  /** By default the following fields get decoded to a Buffer
   *  (instead of a NistFieldValue composed from a string or array of strings):
   * - 4.009 (DATA)
   * - 10.999 (DATA)
   *
   * This behaviour can be overridden on a per-field basis by passing a custom parser property.
   */

Currently trying to understand these options.

Edza commented 4 years ago

Looks like codecOptions is mandatory for nistDecode, if it's left empty there is an error:

node_modules\node-nist\lib\nistDecode.js:314
    nistUtils_1.provideDefaults({ nist: nistFile, codecOptions: options.codecOptions });
                                                                        ^

TypeError: Cannot read property 'codecOptions' of undefined
    at Object.exports.nistDecode (...\node_modules\node-nist\lib\nistDecode.js:314:73)
    at Object.<anonymous> (C:\Users\eduards.vavere\Desktop\size check\index.js:64:18)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:236:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)
Edza commented 4 years ago

I got it to work, I think. Not sure what this thing is:

MAP: {
      2: {
        48: {
          mandatory: true
        }
      }
    },
    SRE: {
      2: {
        23: {
          defaultValue: 'sorry',
          mandatory: true
        }
      }
    },
    default: {
      1: {
        2 /* ver */: {
          mandatory: true,
          maxLength: 4,
          minLength: 3,
          regexs: [{ regex: '^[0-9]{3,4}$', errMsg: 'Expected three or four digits' }]
        },
        4 /* tot */: {
          mandatory: true,
          maxLength: 16
        },
        5 /* dat */: {
          mandatory: true,
          regexs: [{ regex: '^[0-9]{8}$', errMsg: 'Expected eight digits' }]
        },
        6 /* pry */: {
          mandatory: false,
          regexs: [{ regex: '^[1-9]{1}$', errMsg: 'Expected a number between 1 and 9' }]
        },
        7 /* dai */: {
          mandatory: true
        },
        8 /* ori */: {
          mandatory: true
        },
        9 /* tcn */: {
          mandatory: true
        },
        10 /* tcr */: {
          mandatory: false
        },
        11 /* nsr */: {
          mandatory: true,
          regexs: [{ regex: '^[0-9]{2}.[0-9]{2}$', errMsg: 'Expected a string in format dd.dd' }]
        },
        12 /* ntr */: {
          mandatory: true,
          regexs: [{ regex: '^[0-9]{2}.[0-9]{2}$', errMsg: 'Expected a string in format dd.dd' }]
        }
      },
      4: {
        3 /* imp */: {
          mandatory: true
        },
        4 /* fgp */: {
          mandatory: true
        },
        5 /* isr */: {
          mandatory: true
        },
        6 /* hll */: {
          mandatory: true
        },
        7 /* vll */: {
          mandatory: true
        },
        8 /* cga */: {
          mandatory: true
        }
      }
    }

Could you provide some documentation for it? :)

ivosh commented 4 years ago

Looks like codecOptions is mandatory for nistDecode, if it's left empty there is an error:

node_modules\node-nist\lib\nistDecode.js:314
    nistUtils_1.provideDefaults({ nist: nistFile, codecOptions: options.codecOptions });
                                                                        ^

TypeError: Cannot read property 'codecOptions' of undefined
    at Object.exports.nistDecode (...\node_modules\node-nist\lib\nistDecode.js:314:73)
    at Object.<anonymous> (C:\Users\eduards.vavere\Desktop\size check\index.js:64:18)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:236:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)

Well, Typescript compiler should flag such a problem. Refer to https://github.com/ivosh/node-nist/blob/master/src/nistDecode.ts#L533

ivosh commented 4 years ago

However making codec options entirely optional for nistDecode and nistEncode seems logical.

Fixed in https://github.com/ivosh/node-nist/commit/58f7cee058252b26422c3c13052c2ace4455769a

Edza commented 4 years ago

Thanks for the fix. :D