<commit_before>// Core
import * as webcrypto from "webcrypto-core";
const WebCryptoError = webcrypto.WebCryptoError;

import { Mechanism, Module, Session, SessionFlag, Slot, Token, TokenFlag } from "graphene-pk11";
import { CertificateStorage } from "./cert_storage";
import { KeyStorage } from "./key_storage";
import { SubtleCrypto } from "./subtle";
import * as utils from "./utils";

const ERR_RANDOM_VALUE_LENGTH = "Failed to execute 'getRandomValues' on 'Crypto': The ArrayBufferView's byte length (%1) exceeds the number of bytes of entropy available via this API (65536).";

// Fix btoa and atob for NodeJS
let _global = global as any;
_global.btoa = (data: string) => new Buffer(data, "binary").toString("base64");
_global.atob = (data: string) => new Buffer(data, "base64").toString("binary");

/**
 * PKCS11 with WebCrypto Interface
 */
export class WebCrypto implements NativeCrypto {

    public info: IProvider;
    public subtle: SubtleCrypto;
    public keyStorage: KeyStorage;
    public certStorage: CertificateStorage;
    public session: Session;
    public isReadWrite: boolean;
    public isLoggedIn: boolean;
    public isLoginRequired: boolean;

    public module: Module;
    public slot: Slot;
    public token: Token;
    private initialized: boolean;

    /**
     * Creates an instance of WebCrypto.
     * @param {P11WebCryptoParams} props PKCS11 module init parameters
     *
     * @memberOf WebCrypto
     */
    constructor(props: P11WebCryptoParams) {
        const mod = this.module = Module.load(props.library, props.name);
        try {
            if (props.libraryParameters) {
                mod.initialize({
                    libraryParameters: props.libraryParameters,
                });
                mod.initialize();
            } else {
                mod.initialize();
            }
        } catch (e) {
            if (!/CKR_CRYPTOKI_ALREADY_INITIALIZED/.test(e.message)) {
                throw e;
            }
        }
        this.initialized = true;

        const slotIndex = props.slot || 0;
        const slots = mod.getSlots(true);
        if (!(0 <= slotIndex && slotIndex < slots.length)) {
            throw new WebCryptoError(`Slot by index ${props.slot} is not found`);
        }
        this.slot = slots.items(slotIndex);
        this.token = this.slot.getToken();<commit_msg>Fix init error<commit_after>// Core
import * as webcrypto from "webcrypto-core";
const WebCryptoError = webcrypto.WebCryptoError;

import { Mechanism, Module, Session, SessionFlag, Slot, Token, TokenFlag } from "graphene-pk11";
import { CertificateStorage } from "./cert_storage";
import { KeyStorage } from "./key_storage";
import { SubtleCrypto } from "./subtle";
import * as utils from "./utils";

const ERR_RANDOM_VALUE_LENGTH = "Failed to execute 'getRandomValues' on 'Crypto': The ArrayBufferView's byte length (%1) exceeds the number of bytes of entropy available via this API (65536).";

// Fix btoa and atob for NodeJS
let _global = global as any;
_global.btoa = (data: string) => new Buffer(data, "binary").toString("base64");
_global.atob = (data: string) => new Buffer(data, "base64").toString("binary");

/**
 * PKCS11 with WebCrypto Interface
 */
export class WebCrypto implements NativeCrypto {

    public info: IProvider;
    public subtle: SubtleCrypto;
    public keyStorage: KeyStorage;
    public certStorage: CertificateStorage;
    public session: Session;
    public isReadWrite: boolean;
    public isLoggedIn: boolean;
    public isLoginRequired: boolean;

    public module: Module;
    public slot: Slot;
    public token: Token;
    private initialized: boolean;

    /**
     * Creates an instance of WebCrypto.
     * @param {P11WebCryptoParams} props PKCS11 module init parameters
     *
     * @memberOf WebCrypto
     */
    constructor(props: P11WebCryptoParams) {
        const mod = this.module = Module.load(props.library, props.name);
        try {
            if (props.libraryParameters) {
                mod.initialize({
                    libraryParameters: props.libraryParameters,
                });
            } else {
                mod.initialize();
            }
        } catch (e) {
            if (!/CKR_CRYPTOKI_ALREADY_INITIALIZED/.test(e.message)) {
                throw e;
            }
        }
        this.initialized = true;

        const slotIndex = props.slot || 0;
        const slots = mod.getSlots(true);
        if (!(0 <= slotIndex && slotIndex < slots.length)) {
            throw new WebCryptoError(`Slot by index ${props.slot} is not found`);
        }
        this.slot = slots.items(slotIndex);
        this.token = this.slot.getToken();
{'fraction_non_alphanumeric': 0.10927790872080566, 'fraction_numerical': 0.009427898007285194, 'mean_word_length': 2.666928515318146, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7010330', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1308, 'n_words': 386}