<commit_before>/*
 * Minimal Object Storage Library, (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var CombinedStream = require('combined-stream') // use MultiStream unless you need lazy append after stream created
var Concat = require('concat-stream')
var Crypto = require('crypto')
var Http = require('http')
var Moment = require('moment')
var ParseXml = require('xml-parser')
var Q = require('q')
var Stream = require('stream')
var Through = require('through')
var Xml = require('xml')
var ParseString = require('xml2js').parseString

class Client {
    constructor(params, transport) {
        "use strict"
        if (transport) {
            this.transport = transport
        } else {
            this.transport = Http
        }
        this.params = params
    }

    // SERIVCE LEVEL CALLS

    makeBucket(bucket, cb) {
        "use strict"

        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            method: 'PUT',
            path: `/${bucket}`
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var req = this.transport.request(requestParams, response => {
            if (response.statusCode !== 200) {
                return parseError(response, cb)
            }
            cb()
        })
        req.end()
    }

    listBuckets() {
        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            path: '/',
            method: 'GET'
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var stream = new Stream.Readable({objectMode: true})
        stream._read = () => {
        }

        var req = this.transport.request(requestParams, (response) => {
            if (response.statusCode !== 200) {
                // TODO work out how to handle errors with stream
                stream.push(parseError(response, (error) => {
                    "use strict";
                    stream.emit('error', error)
                }))
                stream.push(null)
            }
            response.pipe(Concat(errorXml => {
                "use strict";
                var parsedXml = ParseXml(errorXml.toString())
                parsedXml.root.children.forEach(element => {
                    "use strict";
                    if (element.name === 'Buckets') {
                        element.children.forEach(bucketListing => {
                            var bucket = {}
                            bucketListing.children.forEach(prop => {
                                switch (prop.name) {
                                    case "Name":
                                        bucket.name = prop.content
                                        break
                                    case "CreationDate":
                                        bucket.creationDate = prop.content
                                        break
                                }
                            })
                            stream.push(bucket)
                        })
                    }
                })
                stream.push(null)
            }))
        })
        req.end()
        return stream
    }

    bucketExists(bucket, cb) {
        "use strict";

        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            method: 'HEAD',
            path: `/${bucket}`
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var req = this.transport.request(requestParams, response => {
            if (response.statusCode !== 200) {
                return parseError(response, cb)
            }
            cb()
        })
        req.end()
    }

    removeBucket(bucket, cb) {
        "use strict";

        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            method: 'DELETE',
            path: `/${bucket}`
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var req = this.transport.request(requestParams, response => {
            if (response.statusCode !== 200) {
                return parseError(response, cb)
            }
            cb()
        })
        req.end()
    }

    getBucketACL(bucket, cb) {
        "use strict";
        cb('not implemented')
    }

    setBucketACL(bucket, acl, cb) {
        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            method: 'PUT',
            path: `/${bucket}`,
            headers: {
                acl: "",
                'x-amz-acl': acl
            }
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var req = this.transport.request(requestParams, response => {
            if (response.statusCode !== 200) {
                return parseError(response, cb)
            }
            cb()
        })
        req.end()
    }

    dropIncompleteUpload(bucket, key, cb) {<commit_msg>Add acl query string for setBucketACL()<commit_after>/*
 * Minimal Object Storage Library, (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var CombinedStream = require('combined-stream') // use MultiStream unless you need lazy append after stream created
var Concat = require('concat-stream')
var Crypto = require('crypto')
var Http = require('http')
var Moment = require('moment')
var ParseXml = require('xml-parser')
var Q = require('q')
var Stream = require('stream')
var Through = require('through')
var Xml = require('xml')
var ParseString = require('xml2js').parseString

class Client {
    constructor(params, transport) {
        "use strict"
        if (transport) {
            this.transport = transport
        } else {
            this.transport = Http
        }
        this.params = params
    }

    // SERIVCE LEVEL CALLS

    makeBucket(bucket, cb) {
        "use strict"

        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            method: 'PUT',
            path: `/${bucket}`
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var req = this.transport.request(requestParams, response => {
            if (response.statusCode !== 200) {
                return parseError(response, cb)
            }
            cb()
        })
        req.end()
    }

    listBuckets() {
        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            path: '/',
            method: 'GET'
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var stream = new Stream.Readable({objectMode: true})
        stream._read = () => {
        }

        var req = this.transport.request(requestParams, (response) => {
            if (response.statusCode !== 200) {
                // TODO work out how to handle errors with stream
                stream.push(parseError(response, (error) => {
                    "use strict";
                    stream.emit('error', error)
                }))
                stream.push(null)
            }
            response.pipe(Concat(errorXml => {
                "use strict";
                var parsedXml = ParseXml(errorXml.toString())
                parsedXml.root.children.forEach(element => {
                    "use strict";
                    if (element.name === 'Buckets') {
                        element.children.forEach(bucketListing => {
                            var bucket = {}
                            bucketListing.children.forEach(prop => {
                                switch (prop.name) {
                                    case "Name":
                                        bucket.name = prop.content
                                        break
                                    case "CreationDate":
                                        bucket.creationDate = prop.content
                                        break
                                }
                            })
                            stream.push(bucket)
                        })
                    }
                })
                stream.push(null)
            }))
        })
        req.end()
        return stream
    }

    bucketExists(bucket, cb) {
        "use strict";

        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            method: 'HEAD',
            path: `/${bucket}`
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var req = this.transport.request(requestParams, response => {
            if (response.statusCode !== 200) {
                return parseError(response, cb)
            }
            cb()
        })
        req.end()
    }

    removeBucket(bucket, cb) {
        "use strict";

        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            method: 'DELETE',
            path: `/${bucket}`
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var req = this.transport.request(requestParams, response => {
            if (response.statusCode !== 200) {
                return parseError(response, cb)
            }
            cb()
        })
        req.end()
    }

    getBucketACL(bucket, cb) {
        "use strict";
        cb('not implemented')
    }

    setBucketACL(bucket, acl, cb) {
        // we should make sure to set this query parameter, but the call apparently succeeds without it to s3
        // To differentiate this functionality from makeBucket() lets do it anyways.
        var query = `?acl`;
        var requestParams = {
            host: this.params.host,
            port: this.params.port,
            method: 'PUT',
            path: `/${bucket}${query}`,
            headers: {
                'x-amz-acl': acl
            }
        }

        signV4(requestParams, '', this.params.accessKey, this.params.secretKey)

        var req = this.transport.request(requestParams, response => {
            if (response.statusCode !== 200) {
                return parseError(response, cb)
            }
            cb()
        })
        req.end()
    }

    dropIncompleteUpload(bucket, key, cb) {
{'fraction_non_alphanumeric': 0.1068941504178273, 'fraction_numerical': 0.005135793871866295, 'mean_word_length': 1.4263991552270328, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4466534', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2824, 'n_words': 737}