npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ndef-parser

v1.0.11

Published

This package has been deprecated. Use nfccard-parser instead.

Downloads

24

Readme

ndef-parser

This package has been deprecated.

Use nfccard-tool instead.


Feed me with a raw nfc card output and I will drop you tjhe ndef message if I can find one.

Table of Contents

Install

npm:

npm install ndef-parser

yarn:

yarn add ndef-parser

Usage

Complete example:

Copy/pasta example:

const ndef = require('ndef-parser');

// Let's mock it by creating a new buffer:
let tagBufferBlocks0to4 = new Buffer('046e38da5a215280a9480000e1106d00032ad101', 'hex');

// We want to parse this header
let tagHeaderValues = ndef.parseHeader(tagBufferBlocks0to4);

// ndef.parseHeader will return an obj containing headers statements:
console.log(tagHeaderValues);
  // logs:
  // { isTagFormatedAsNdef: true,
  //   type2TagSpecification: '6e',
  //   maxNdefMessageSize: 128,
  //   hasTagReadPermissions: true,
  //   hasTagANdefMessage: true,
  //   ndefMessageLength: 133,
  //   tagLengthToReadFromBlock4: 135 }

  // Check if our tag is readable and has a ndef message
if(tagHeaderValues.hasTagReadPermissions && tagHeaderValues.isTagFormatedAsNdef && tagHeaderValues.hasTagANdefMessage) {

  // And here is our isolated ndef message !
  let tagBufferFromBlock4 = new Buffer('032ad101265402656e4865792074686572652c2069276d2061206e6465662074657874207265636f72642021', 'hex');


  // ndef.parseNdef uses @taptrack/ndef which supports text and uri parsing, but you obviously can use anything to parse the ndef message
  let parsedRecords = ndef.parseNdef(tagBufferFromBlock4);

  console.log(parsedRecords)
  // [{
  //   language: 'en',
  //   content: 'I\'m the first ndef record of this tag'
  // },
  // { language: 'en',
  //   content: 'Looks like I\'m the second record of this tag, and a plaintext type one by the way.'
  // }]
}

Detailed explanations

1 - Require the lib:

const ndef = require('ndef-parser');

2 - Parse the tag header ndef.parseHeader(buffer)

In order to know where the ndef message is we parse the header:

Here is a buffer of the first 4 blocks of a nfc tag (ntag216) containg 2 ndef text records:

<Buffer 04 6e 38 da 5a 21 52 80 a9 48 00 00 e1 10 6d 00 03 85 91 01>
// Let's mock it by creating a new buffer:
let tagBufferBlocks0to4 = new Buffer('046e38da5a215280a9480000e1106d0003859101', 'hex');

console.log('tagBufferBlocks0to4', tagBufferBlocks0to4);
// logs: <Buffer 04 6e 38 da 5a 21 52 80 a9 48 00 00 e1 10 6d 00 03 85 91 01> (here we go!)


// We want to parse this header
// ndef.parseHeader will return an obj containing headers statements
let tagHeaderValues = ndef.parseHeader(tagBufferBlocks0to4);

console.log(tagHeaderValues);
  // logs:
  // { isTagFormatedAsNdef: true,
  //   type2TagSpecification: '6e',
  //   maxNdefMessageSize: 128,
  //   hasTagReadPermissions: true,
  //   hasTagANdefMessage: true,
  //   ndefMessageLength: 133,
  //   tagLengthToReadFromBlock4: 135 }

ndef.parseHeader(buffer) returns:

  • isTagFormatedAsNdef boolean - is tag formated as ndef ?
  • type2TagSpecification string - hex string of the type2 tag spec
  • maxNdefMessageSize int - max ndef message size provided by the tag
  • hasTagReadPermissions boolean - is tag read locked ?
  • hasTagANdefMessage boolean - tag has a ndef message ?
  • ndefMessageLength int - ndef message length
  • tagLengthToReadFromBlock4 int - bytes to read starting at block 4 to get the full ndef message

3 - Parse the ndef message and it's record(s) ndef.parseNdef(buffer):

Now that we know if our tag contains a valid ndef message we can read one more time the tag where it's located. Just then we will be able to use a third party library to parse the ndef message.


// Check if our tag is readable and has a ndef message
if(tagHeaderValues.hasTagReadPermissions && tagHeaderValues.isTagFormatedAsNdef && tagHeaderValues.hasTagANdefMessage) {
  // Ok, looks like what we've seen in the header tells us we can find a ndef message
  // Here's an example using nfc-pcsc library:
  // var tagBufferFromBlock4 = await reader.read(4, tagHeaderValues.tagLengthToReadFromBlock4);
  // And here is our isolated ndef message !
  console.log(tagBufferFromBlock4);
  // logs: <Buffer 91 01 28 54 02 65 6e 49 27 6d 20 74 68 65 20 66 69 72 73 74 20 6e 64 65 66 20 72 65 63 6f 72 64 20 6f 66 20 74 68 69 73 20 74 61 67 51 01 55 54 02 65 ... >




  // ndef.parseNdef uses @taptrack/ndef which supports text and uri parsing, but you obviously can use anything to parse the ndef message
  let parsedRecords = ndef.parseNdef(tagBufferFromBlock4);

  console.log(parsedRecords)
  // [{
  //   language: 'en',
  //   content: 'I\'m the first ndef record of this tag'
  // },
  // { language: 'en',
  //   content: 'Looks like I\'m the second record of this tag, and a plaintext type one by the way.'
  // }]
}

Third party

We are natively using https://github.com/TapTrack/NdefJS but you could give a try at https://github.com/andijakl/ndef-nfc or have a deep look at https://github.com/googlearchive/chrome-nfc

If you are looking for a nfc tag reading library take a look at https://github.com/pokusew/nfc-pcsc

License

MIT © somq