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

@technopolisder/pcsclite

v0.5.2

Published

Bindings over PC/SC to access Smart Cards

Downloads

8

Readme

node-pcsclite

npm node-pcsclite channel on discord

Bindings over pcsclite to access Smart Cards. It works in Linux, macOS and Windows.

Looking for library to work easy with NFC tags?
take a look at nfc-pcsc which offers easy to use high level API for detecting / reading and writing NFC tags and cards

Content

Installation

In order to install the package you need to have installed in the system the pcsclite libraries.

In macOS and Windows you don't have to install anything.

For example, in Debian/Ubuntu:

apt-get install libpcsclite1 libpcsclite-dev

To run any code you will also need to have installed the pcsc daemon:

apt-get install pcscd

Once you have all needed libraries, you can install using npm:

npm install @technopolisder/pcsclite --save

Example

const pcsclite = require('@technopolisder/pcsclite');

const pcsc = pcsclite();

pcsc.on('reader', (reader) => {

    console.log('New reader detected', reader.name);

    reader.on('error', err => {
        console.log('Error(', reader.name, '):', err.message);
    });

    reader.on('status', (status) => {

        console.log('Status(', reader.name, '):', status);

        // check what has changed
        const changes = reader.state ^ status.state;

        if (!changes) {
            return;
        }

		// noinspection JSBitwiseOperatorUsage
        if ((changes & reader.SCARD_STATE_EMPTY) && (status.state & reader.SCARD_STATE_EMPTY)) {

            console.log("card removed");

            reader.disconnect(reader.SCARD_LEAVE_CARD, err => {

                if (err) {
                    console.log(err);
                    return;
                }

                console.log('Disconnected');

            });

            return;

        }

		// noinspection JSBitwiseOperatorUsage
        if ((changes & reader.SCARD_STATE_EMPTY) && (status.state & reader.SCARD_STATE_EMPTY)) {

            console.log("card inserted");

            reader.connect({ share_mode: reader.SCARD_SHARE_SHARED }, (err, protocol) => {

                if (err) {
                    console.log(err);
                    return;
                }

                console.log('Protocol(', reader.name, '):', protocol);

                reader.transmit(Buffer.from([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, (err, data) => {

                    if (err) {
                        console.log(err);
                        return;
                    }

                    console.log('Data received', data);
                    reader.close();
                    pcsc.close();

                });

            });

            // noinspection UnnecessaryReturnStatementJS
            return;
        }

    });

    reader.on('end', () => {
        console.log('Reader', reader.name, 'removed');
    });

});

pcsc.on('error', err => {
    console.log('PCSC error', err.message);
});

Behavior on different OS

TODO document

API

Class: PCSCLite

The PCSCLite object is an EventEmitter that notifies the existence of Card Readers.

Event: 'error'

  • err Error Object. The error.

Event: 'reader'

  • reader CardReader. A CardReader object associated to the card reader detected

Emitted whenever a new card reader is detected.

pcsclite.close()

It frees the resources associated with this PCSCLite instance. At a low level it calls SCardCancel so it stops watching for new readers.

pcsclite.readers

An object containing all detected readers by name. Updated as readers are attached and removed.

Class: CardReader

The CardReader object is an EventEmitter that allows to manipulate a card reader.

Event: 'error'

  • err Error Object. The error.

Event: 'end'

Emitted when the card reader has been removed.

Event: 'status'

  • status Object.
    • state The current status of the card reader as returned by SCardGetStatusChange
    • atr ATR of the card inserted (if any)

Emitted whenever the status of the reader changes.

reader.connect([options], callback)

  • options Object Optional
    • share_mode Number Shared mode. Defaults to SCARD_SHARE_EXCLUSIVE
    • protocol Number Preferred protocol. Defaults to SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1
  • callback Function called when connection operation ends
    • error Error
    • protocol Number Established protocol to this connection.

Wrapper around SCardConnect. Establishes a connection to the reader.

reader.disconnect(disposition, callback)

  • disposition Number. Reader function to execute. Defaults to SCARD_UNPOWER_CARD
  • callback Function called when disconnection operation ends
    • error Error

Wrapper around SCardDisconnect. Terminates a connection to the reader.

reader.transmit(input, res_len, protocol, callback)

  • input Buffer input data to be transmitted
  • res_len Number. Max. expected length of the response
  • protocol Number. Protocol to be used in the transmission
  • callback Function called when transmit operation ends
    • error Error
    • output Buffer

Wrapper around SCardTransmit. Sends an APDU to the smart card contained in the reader connected to.

reader.control(input, control_code, res_len, callback)

  • input Buffer input data to be transmitted
  • control_code Number. Control code for the operation
  • res_len Number. Max. expected length of the response
  • callback Function called when control operation ends
    • error Error
    • output Buffer

Wrapper around SCardControl. Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.

reader.close()

It frees the resources associated with this CardReader instance. At a low level it calls SCardCancel so it stops watching for the reader status changes.

FAQ

Can I use this library in my Electron app?

Yes, you can! It works well.

But please read carefully Using Native Node Modules guide in Electron documentation to fully understand the problematic.

Note, that because of Node Native Modules, you must build your app on target platform (you must run Windows build on Windows machine, etc.).
You can use CI/CD server to build your app for certain platforms.
For Windows, I recommend you to use AppVeyor.
For macOS and Linux build, there are plenty of services to choose from, for example CircleCI, Travis CI CodeShip.

License

ISC