<commit_before>///<reference path="cbl.d.ts" />

import Emitter = require('emitter');

class cblDB {

    private serverUrl = '';
    private dbName = '';
    private dbUrl:uri.URI = null;
    replicate = null;

    constructor(dbName) {
        this.dbName = dbName.replace(/[^a-z0-9$_()+-/]/g, '');
        this.replicate = {
            from: this.replicateFrom,
            to: this.replicateTo
        }
    }

    initDB() {
        return new Promise((resolve, reject)=> {
            //get cbl server url
            cbl.getServerURL(
                (url)=> {
                    this.serverUrl = url;
                    this.dbUrl = new URI(this.serverUrl).directory(this.dbName);
                    this.processRequest('PUT', this.dbUrl.toString(), null, null,
                        (err, response)=> {
                            if (err) reject(cblDB.buildError('Error From DB PUT Request',err));
                            else if (response.ok) resolve(true);
                            else if (response.status = 412) resolve(true);
                            else reject( cblDB.buildError('Error From DB PUT Request',response));
                        });
                },
                (err)=> {throw new Error(err); });
        });
    }

    bulkDocs(docs:Array<cbl.IDoc>) {
        return new Promise((resolve, reject)=> {
            var headers:cbl.IHeaders = {'Content-Type': 'application/json'};
            var uri = this.dbUrl.segment('_bulk_docs');
            this.processRequest('POST', uri.toString(), docs, headers,
                (err, success)=> {
                    if (err) reject(cblDB.buildError('Error From bulkDocs Request',err));
                    else resolve(success);
                })
        });
    }

    changes() {
        var http = new XMLHttpRequest();
        var emitter = new Emitter();
        var uri = this.dbUrl.segment('_changes');
        http.onreadystatechange = () => {
            //if (http.readyState == 4 && http.status == 200) change(false, JSON.parse(http.responseText));
            //else error({status: http.status, response: http.responseText});
        };

        //http.open(verb, uri.toString(), true);
        //if (verb === 'GET' || verb === 'DELETE')http.send();
        //else if (verb === 'POST' || verb === 'PUT')http.send(JSON.stringify(data));
        return emitter;
    }

    destroy() {
        return new Promise((resolve, reject)=> {

        });
    }

    get(docId:string, params?:cbl.IGetDbDocParams) {
        return new Promise((resolve, reject)=> {
            var headers:cbl.IHeaders = {'Content-Type': 'application/json'};
            var uri = this.dbUrl.segment(docId);
            var requestParams:cbl.IGetDbDocParams = <cbl.IGetDbDocParams>{};
            if (params) requestParams = <cbl.IGetDbDocParams>_.assign(requestParams, params);
            this.processRequest('GET', uri.toString(), null, headers,
                (err, doc)=> {
                    if (err) reject(cblDB.buildError('Error From GET Request',err));
                    else resolve(doc);
                });
        });
    }

    info() {
        return new Promise((resolve, reject)=> {

        });
    }

    query(view:string, params:cbl.IGetPostDbDesignViewName) {
        return new Promise((resolve, reject)=> {
            var verb = 'GET';
            var headers:cbl.IHeaders = {'Content-Type': 'application/json'};
            var viewParts = view.split('/');
            var uri = this.dbUrl.segment('_design').segment(viewParts[0]).segment('_view').segment(viewParts[1]);
            var requestParams:cbl.IGetPostDbDesignViewName = <cbl.IGetPostDbDesignViewName>{};
            if (params.keys) {
                verb = 'POST';
                requestParams.keys = params.keys;
            }
            else requestParams = <cbl.IGetPostDbDesignViewName>_.assign(requestParams, params);
            uri.search(requestParams);

            this.processRequest(verb, uri.toString(), null, headers,
                (err, response)=> {
                    if (err) reject(cblDB.buildError('Error From Query Request',err));
                    else resolve(response);
                });
        });
    }

    replicateFrom() {

    }

    replicateTo() {

    }

    upsert(doc:cbl.IDoc, params?:cbl.IPutDbDocParams) {
        return new Promise((resolve, reject)=> {
            var put = (upsertDoc) => {
                this.processRequest('PUT', uri.toString(), upsertDoc, headers,
                    (err, success)=> {
                        if (err) reject(cblDB.buildError('Error From Upsert Request',err));
                        else resolve(success);
                    })
            };

            var headers:cbl.IHeaders = {'Content-Type': 'application/json'};
            var uri = this.dbUrl.segment(doc._id);
            var requestParams:cbl.IPutDbDocParams = <cbl.IPutDbDocParams>{};
            if (params) requestParams = <cbl.IPutDbDocParams>_.assign(requestParams, params);

            this.get(doc._id)
                .then((dbDoc:cbl.IDoc)=> {
                    requestParams.rev = dbDoc._rev;
                    doc._rev = dbDoc._rev;
                    uri.search(requestParams);
                    put(doc);<commit_msg>ensure db url is not mutated<commit_after>///<reference path="cbl.d.ts" />

import Emitter = require('emitter');

class cblDB {

    private serverUrl = '';
    private dbName = '';
    private dbUrl:string = '';
    replicate = null;

    constructor(dbName) {
        this.dbName = dbName.replace(/[^a-z0-9$_()+-/]/g, '');
        this.replicate = {
            from: this.replicateFrom,
            to: this.replicateTo
        }
    }

    initDB() {
        return new Promise((resolve, reject)=> {
            //get cbl server url
            cbl.getServerURL(
                (url)=> {
                    this.serverUrl = url;
                    this.dbUrl = new URI(this.serverUrl).directory(this.dbName).toString();
                    this.processRequest('PUT', this.dbUrl.toString(), null, null,
                        (err, response)=> {
                            if (err) reject(cblDB.buildError('Error From DB PUT Request',err));
                            else if (response.ok) resolve(true);
                            else if (response.status = 412) resolve(true);
                            else reject( cblDB.buildError('Error From DB PUT Request',response));
                        });
                },
                (err)=> {throw new Error(err); });
        });
    }

    bulkDocs(docs:Array<cbl.IDoc>) {
        return new Promise((resolve, reject)=> {
            var headers:cbl.IHeaders = {'Content-Type': 'application/json'};
            var uri = new URI(this.dbUrl);
            uri.segment('_bulk_docs');
            this.processRequest('POST', uri.toString(), docs, headers,
                (err, success)=> {
                    if (err) reject(cblDB.buildError('Error From bulkDocs Request',err));
                    else resolve(success);
                })
        });
    }

    changes() {
        var http = new XMLHttpRequest();
        var emitter = new Emitter();
        var uri = new URI(this.dbUrl);
        uri.segment('_changes');
        http.onreadystatechange = () => {
            //if (http.readyState == 4 && http.status == 200) change(false, JSON.parse(http.responseText));
            //else error({status: http.status, response: http.responseText});
        };

        //http.open(verb, uri.toString(), true);
        //if (verb === 'GET' || verb === 'DELETE')http.send();
        //else if (verb === 'POST' || verb === 'PUT')http.send(JSON.stringify(data));
        return emitter;
    }

    destroy() {
        return new Promise((resolve, reject)=> {

        });
    }

    get(docId:string, params?:cbl.IGetDbDocParams) {
        return new Promise((resolve, reject)=> {
            var headers:cbl.IHeaders = {'Content-Type': 'application/json'};
            var uri = new URI(this.dbUrl);
            uri.segment(docId);
            var requestParams:cbl.IGetDbDocParams = <cbl.IGetDbDocParams>{};
            if (params) requestParams = <cbl.IGetDbDocParams>_.assign(requestParams, params);
            this.processRequest('GET', uri.toString(), null, headers,
                (err, doc)=> {
                    if (err) reject(cblDB.buildError('Error From GET Request',err));
                    else resolve(doc);
                });
        });
    }

    info() {
        return new Promise((resolve, reject)=> {

        });
    }

    query(view:string, params:cbl.IGetPostDbDesignViewName) {
        return new Promise((resolve, reject)=> {
            var verb = 'GET';
            var headers:cbl.IHeaders = {'Content-Type': 'application/json'};
            var viewParts = view.split('/');
            var uri = new URI(this.dbUrl.toString());
            uri.segment('_design').segment(viewParts[0]).segment('_view').segment(viewParts[1]);
            var requestParams:cbl.IGetPostDbDesignViewName = <cbl.IGetPostDbDesignViewName>{};
            if (params.keys) {
                verb = 'POST';
                requestParams.keys = params.keys;
            }
            else requestParams = <cbl.IGetPostDbDesignViewName>_.assign(requestParams, params);
            uri.search(requestParams);

            this.processRequest(verb, uri.toString(), null, headers,
                (err, response)=> {
                    if (err) reject(cblDB.buildError('Error From Query Request',err));
                    else resolve(response);
                });
        });
    }

    replicateFrom() {

    }

    replicateTo() {

    }

    upsert(doc:cbl.IDoc, params?:cbl.IPutDbDocParams) {
        return new Promise((resolve, reject)=> {
            var put = (upsertDoc) => {
                this.processRequest('PUT', uri.toString(), upsertDoc, headers,
                    (err, success)=> {
                        if (err) reject(cblDB.buildError('Error From Upsert Request',err));
                        else resolve(success);
                    });
            };

            var headers:cbl.IHeaders = {'Content-Type': 'application/json'};
            var uri = new URI(this.dbUrl);
            uri.segment(doc._id);
            var requestParams:cbl.IPutDbDocParams = <cbl.IPutDbDocParams>{};
            if (params) requestParams = <cbl.IPutDbDocParams>_.assign(requestParams, params);

            this.get(doc._id)
                .then((dbDoc:cbl.IDoc)=> {
                    requestParams.rev = dbDoc._rev;
                    doc._rev = dbDoc._rev;
                    uri.search(requestParams);
                    put(doc);
{'fraction_non_alphanumeric': 0.14173596187984677, 'fraction_numerical': 0.0020554984583761563, 'mean_word_length': 1.732703599693643, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 53, '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': '6849211', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2931, 'n_words': 573}