<commit_before>const EventEmitter = require("events");
const hash = require("hash.js");
const Credentials = require("../credentials/Credentials.js");
const { credentialsAllowsPurpose } = require("../credentials/channel.js");
const { detectFormat, getDefaultFormat } = require("../io/formatRouter.js");
const { fireInstantiationHandlers, registerDatasource } = require("./register.js");

/**
 * @typedef {Object} AttachmentDetails
 * @property {String} id The attachment ID
 * @property {String} vaultID The vault ID
 * @property {String} name Base filename
 * @property {String} filename Full filename and path
 * @property {Number} size Size in bytes (0 if invalid)
 * @property {String|null} mime MIME type if available
 */

/**
 * @typedef {Object} LoadedVaultData
 * @property {VaultFormat} Format The vault format class that was detected
 *  when reading encrypted vault contents
 * @property {Array.<String>} history Decrypted vault data
 */

/**
 * Datasource for text input and output
 * @memberof module:Buttercup
 */
class TextDatasource extends EventEmitter {
    /**
     * Constructor for the text datasource
     * @param {Credentials} credentials The credentials and configuration for
     *  the datasource
     */
    constructor(credentials) {
        super();
        this._credentials = credentials;
        this._credentials.restrictPurposes([Credentials.PURPOSE_SECURE_EXPORT]);
        this._content = "";
        this.type = "text";
        fireInstantiationHandlers("text", this);
    }

    /**
     * Datasource credentials
     * @type {Credentials}
     * @readonly
     */
    get credentials() {
        return this._credentials;
    }

    /**
     * Whether the datasource currently has content
     * Used to check if the datasource has encrypted content that can be
     * loaded. May be used when attempting to open a vault in offline mode.
     * @type {Boolean}
     * @memberof TextDatasource
     */
    get hasContent() {<commit_msg>Support loading TextDatasource from cred content<commit_after>const EventEmitter = require("events");
const hash = require("hash.js");
const Credentials = require("../credentials/Credentials.js");
const { credentialsAllowsPurpose, getCredentials } = require("../credentials/channel.js");
const { detectFormat, getDefaultFormat } = require("../io/formatRouter.js");
const { fireInstantiationHandlers, registerDatasource } = require("./register.js");

/**
 * @typedef {Object} AttachmentDetails
 * @property {String} id The attachment ID
 * @property {String} vaultID The vault ID
 * @property {String} name Base filename
 * @property {String} filename Full filename and path
 * @property {Number} size Size in bytes (0 if invalid)
 * @property {String|null} mime MIME type if available
 */

/**
 * @typedef {Object} LoadedVaultData
 * @property {VaultFormat} Format The vault format class that was detected
 *  when reading encrypted vault contents
 * @property {Array.<String>} history Decrypted vault data
 */

/**
 * Datasource for text input and output
 * @memberof module:Buttercup
 */
class TextDatasource extends EventEmitter {
    /**
     * Constructor for the text datasource
     * @param {Credentials} credentials The credentials and configuration for
     *  the datasource
     */
    constructor(credentials) {
        super();
        this._credentials = credentials;
        this._credentials.restrictPurposes([Credentials.PURPOSE_SECURE_EXPORT]);
        const { data: credentialData } = getCredentials(credentials.id);
        const { datasource: datasourceConfig = {} } = credentialData || {};
        const { content = "" } = datasourceConfig;
        this._content = content;
        this.type = "text";
        fireInstantiationHandlers("text", this);
    }

    /**
     * Datasource credentials
     * @type {Credentials}
     * @readonly
     */
    get credentials() {
        return this._credentials;
    }

    /**
     * Whether the datasource currently has content
     * Used to check if the datasource has encrypted content that can be
     * loaded. May be used when attempting to open a vault in offline mode.
     * @type {Boolean}
     * @memberof TextDatasource
     */
    get hasContent() {
{'fraction_non_alphanumeric': 0.10846812559467174, 'fraction_numerical': 0.0004757373929590866, 'mean_word_length': 3.3985355648535567, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3730094', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1117, 'n_words': 358}