<commit_before>import * as url from 'url'
import { DesignDocument } from './couchify'
const NodeCouchDb = require('node-couchdb')

export function splitUrlIntoParts(remote: string): { host: string, port: number, protocol: string } {
    if (!remote.startsWith('http')) {
        remote = remote.replace(/^.+\:\/+/, '')
        remote = 'http://' + remote
    }

    const parsedUrl = url.parse(remote)
    const protocol = parsedUrl.protocol.replace(/\:+$/, '')

    return {
        host: parsedUrl.host.replace(/\:+\d+$/, ''),
        port: parsedUrl.port
            ? parseInt(parsedUrl.port, 10)
            : (protocol === 'https' ? 443 : 80),
        protocol: protocol
    }
}

export type DeployOptions = {
    remote?: string
    db?: string
    doc?: DesignDocument
    user?: string
    pass?: string
}

export function deploy({ remote, db, doc, user, pass }: DeployOptions) {
    const opts = splitUrlIntoParts(remote) as DeployOptions

    if (user && pass) {
        opts.user = user
        opts.pass = pass
    }

    const couch = new NodeCouchDb(opts)

    return couch
        .get(db, doc._id)
        .then(({ data: getData, headers: getHeaders, status: getStatus }) =>
            couch.update(db, { ...doc, ...{ _rev: getData._rev } }).then(({ data: updateData }) => updateData),
        (getErr) => {
            if (getErr.code === 'EDOCMISSING') {
                return couch
                    .insert(db, doc)
                    .then(({ data: insertData, headers: insertHeaders, status: insertStatus }) => {
                        return insertData
                    }, (insertErr) => {
                        throw new Error('could not insert data')
                    })
            } else {
                throw new Error('unexpected error')
            }
        })

}
<commit_msg>Set port to 5984 by default when not provided<commit_after>import * as url from 'url'
import { DesignDocument } from './couchify'
const NodeCouchDb = require('node-couchdb')

export function splitUrlIntoParts(remote: string): { host: string, port: number, protocol: string } {
    if (!remote.startsWith('http')) {
        remote = remote.replace(/^.+\:\/+/, '')
        remote = 'http://' + remote
    }

    const parsedUrl = url.parse(remote)
    const protocol = parsedUrl.protocol.replace(/\:+$/, '')

    let port = parsedUrl.port && parseInt(parsedUrl.port, 10)

    if (!port || isNaN(port)) {
        port = 5984
    }

    return {
        host: parsedUrl.host.replace(/\:+\d+$/, ''),
        port,
        protocol: protocol
    }
}

export type DeployOptions = {
    remote?: string
    db?: string
    doc?: DesignDocument
    user?: string
    pass?: string
}

export function deploy({ remote, db, doc, user, pass }: DeployOptions) {
    const opts = splitUrlIntoParts(remote) as DeployOptions

    if (user && pass) {
        opts.user = user
        opts.pass = pass
    }

    const couch = new NodeCouchDb(opts)

    return couch
        .get(db, doc._id)
        .then(({ data: getData, headers: getHeaders, status: getStatus }) =>
            couch.update(db, { ...doc, ...{ _rev: getData._rev } }).then(({ data: updateData }) => updateData),
        (getErr) => {
            if (getErr.code === 'EDOCMISSING') {
                return couch
                    .insert(db, doc)
                    .then(({ data: insertData, headers: insertHeaders, status: insertStatus }) => {
                        return insertData
                    }, (insertErr) => {
                        throw new Error('could not insert data')
                    })
            } else {
                throw new Error('unexpected error')
            }
        })

}

{'fraction_non_alphanumeric': 0.1415426251691475, 'fraction_numerical': 0.004600811907983762, 'mean_word_length': 1.9033778476040848, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5032285', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1035, 'n_words': 279}