<commit_before>'use strict';

import {EventEmitter} from 'events';
import hexify from 'hexify';
import CommandApdu from './CommandApdu';
import ResponseApdu from './ResponseApdu';


const ins = {
    APPEND_RECORD: 0xE2,
    ENVELOPE: 0xC2,
    ERASE_BINARY: 0x0E,
    EXTERNAL_AUTHENTICATE: 0x82,
    GET_CHALLENGE: 0x84,
    GET_DATA: 0xCA,
    GET_RESPONSE: 0xC0,
    INTERNAL_AUTHENTICATE: 0x88,
    MANAGE_CHANNEL: 0x70,
    PUT_DATA: 0xDA,
    READ_BINARY: 0xB0,
    READ_RECORD: 0xB2,
    SELECT_FILE: 0xA4,
    UPDATE_BINARY: 0xD6,
    UPDATE_RECORD: 0xDC,
    VERIFY: 0x20,
    WRITE_BINARY: 0xD0,
    WRITE_RECORD: 0xD2
};


class Iso7816Application extends EventEmitter {
    
    constructor(card) {
        super();
        this.card = card;
    }

    issueCommand(commandApdu) {
        console.log(`Iso7816Application.issueCommand '${commandApdu}'`);

        return this.card
            .issueCommand(commandApdu)
            .then(resp => {
                let response = new ResponseApdu(resp);
                let returnResponse;
                //console.log(`status code '${response.statusCode()}'`);
                if (response.hasMoreBytesAvailable()) {
                    //console.log(`has '${response.numberOfBytesAvailable()}' more bytes available`);
                    returnResponse = this.getResponse(response.numberOfBytesAvailable());
                } else if (response.isWrongLength()) {
                    //console.log(`'le' should be '${response.correctLength()}' bytes`);
                    commandApdu.setLe(response.correctLength());
                    returnResponse = this.card.issueCommand(commandApdu);
                } else {
                    returnResponse = response;
                }
                if (commandApdu.getIns() === 'a4') {
                    this.emit('application-selected', {
                        card: this.card,
                        command: commandApdu,
                        response: returnResponse
                    });
                }
                return returnResponse;
            });
    };

    selectFile(bytes, p1, p2) {
        console.log(`Iso7816Application.selectFile, file='${bytes}'`);
        return this.issueCommand(new CommandApdu({
            cla: 0x00,
            ins: ins.SELECT_FILE,
            p1: p1 || 0x04,
            p2: p2 || 0x00,
            data: bytes
        }));
    };

    getResponse(length) {
        console.log(`Iso7816Application.getResponse, length='${length}'`);<commit_msg>Revert: fire application selected event<commit_after>'use strict';

import {EventEmitter} from 'events';
import hexify from 'hexify';
import CommandApdu from './CommandApdu';
import ResponseApdu from './ResponseApdu';


const ins = {
    APPEND_RECORD: 0xE2,
    ENVELOPE: 0xC2,
    ERASE_BINARY: 0x0E,
    EXTERNAL_AUTHENTICATE: 0x82,
    GET_CHALLENGE: 0x84,
    GET_DATA: 0xCA,
    GET_RESPONSE: 0xC0,
    INTERNAL_AUTHENTICATE: 0x88,
    MANAGE_CHANNEL: 0x70,
    PUT_DATA: 0xDA,
    READ_BINARY: 0xB0,
    READ_RECORD: 0xB2,
    SELECT_FILE: 0xA4,
    UPDATE_BINARY: 0xD6,
    UPDATE_RECORD: 0xDC,
    VERIFY: 0x20,
    WRITE_BINARY: 0xD0,
    WRITE_RECORD: 0xD2
};


class Iso7816Application extends EventEmitter {
    
    constructor(card) {
        super();
        this.card = card;
    }

    issueCommand(commandApdu) {
        console.log(`Iso7816Application.issueCommand '${commandApdu}' `);
        return this.card
            .issueCommand(commandApdu)
            .then(resp => {
                var response = new ResponseApdu(resp);
                //console.log(`status code '${response.statusCode()}'`);
                if (response.hasMoreBytesAvailable()) {
                    //console.log(`has '${response.numberOfBytesAvailable()}' more bytes available`);
                    return this.getResponse(response.numberOfBytesAvailable());
                } else if (response.isWrongLength()) {
                    //console.log(`'le' should be '${response.correctLength()}' bytes`);
                    commandApdu.setLe(response.correctLength());
                    return this.issueCommand(commandApdu);
                }
                //console.log(`return response '${response}' `);
                return response;
            });
    };

    selectFile(bytes, p1, p2) {
        console.log(`Iso7816Application.selectFile, file='${bytes}'`);
        return this.issueCommand(new CommandApdu({
            cla: 0x00,
            ins: ins.SELECT_FILE,
            p1: p1 || 0x04,
            p2: p2 || 0x00,
            data: bytes
        }));
    };

    getResponse(length) {
        console.log(`Iso7816Application.getResponse, length='${length}'`);
{'fraction_non_alphanumeric': 0.1169466298107591, 'fraction_numerical': 0.029555602806719115, 'mean_word_length': 2.027027027027027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5254555', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1409, 'n_words': 267}