createRequest.js 3.81 KB
Newer Older
huahua committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRequest = void 0;
const crypto = require('crypto');
const debug = require('debug')('ali-oss');
const mime = require('mime');
const dateFormat = require('dateformat');
const copy = require('copy-to');
const path = require('path');
const { encoder } = require('./encoder');
const { isIP } = require('./isIP');
const { setRegion } = require('./setRegion');
const { getReqUrl } = require('../client/getReqUrl');
function getHeader(headers, name) {
    return headers[name] || headers[name.toLowerCase()];
}
function delHeader(headers, name) {
    delete headers[name];
    delete headers[name.toLowerCase()];
}
function createRequest(params) {
    let date = new Date();
    if (this.options.amendTimeSkewed) {
        date = +new Date() + this.options.amendTimeSkewed;
    }
    const headers = {
        'x-oss-date': dateFormat(date, "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'")
    };
    if (typeof window !== 'undefined') {
        headers['x-oss-user-agent'] = this.userAgent;
    }
    if (this.userAgent.includes('nodejs')) {
        headers['User-Agent'] = this.userAgent;
    }
    if (this.options.isRequestPay) {
        Object.assign(headers, { 'x-oss-request-payer': 'requester' });
    }
    if (this.options.stsToken) {
        headers['x-oss-security-token'] = this.options.stsToken;
    }
    copy(params.headers).to(headers);
    if (!getHeader(headers, 'Content-Type')) {
        if (params.mime && params.mime.indexOf('/') > 0) {
            headers['Content-Type'] = params.mime;
        }
        else {
            headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || ''));
        }
    }
    if (!getHeader(headers, 'Content-Type')) {
        delHeader(headers, 'Content-Type');
    }
    if (params.content) {
        if (!params.disabledMD5) {
            if (!params.headers || !params.headers['Content-MD5']) {
                headers['Content-MD5'] = crypto.createHash('md5').update(Buffer.from(params.content, 'utf8')).digest('base64');
            }
            else {
                headers['Content-MD5'] = params.headers['Content-MD5'];
            }
        }
        if (!headers['Content-Length']) {
            headers['Content-Length'] = params.content.length;
        }
    }
    const { hasOwnProperty } = Object.prototype;
    for (const k in headers) {
        if (headers[k] && hasOwnProperty.call(headers, k)) {
            headers[k] = encoder(String(headers[k]), this.options.headerEncoding);
        }
    }
    const authResource = this._getResource(params);
    headers.authorization = this.authorization(params.method, authResource, params.subres, headers, this.options.headerEncoding);
    // const url = this._getReqUrl(params);
    if (isIP(this.options.endpoint.hostname)) {
        const { region, internal, secure } = this.options;
        const hostInfo = setRegion(region, internal, secure);
        headers.host = `${params.bucket}.${hostInfo.host}`;
    }
    const url = getReqUrl.bind(this)(params);
    debug('request %s %s, with headers %j, !!stream: %s', params.method, url, headers, !!params.stream);
    const timeout = params.timeout || this.options.timeout;
    const reqParams = {
        method: params.method,
        content: params.content,
        stream: params.stream,
        headers,
        timeout,
        writeStream: params.writeStream,
        customResponse: params.customResponse,
        ctx: params.ctx || this.ctx
    };
    if (this.agent) {
        reqParams.agent = this.agent;
    }
    if (this.httpsAgent) {
        reqParams.httpsAgent = this.httpsAgent;
    }
    reqParams.enableProxy = !!this.options.enableProxy;
    reqParams.proxy = this.options.proxy ? this.options.proxy : null;
    return {
        url,
        params: reqParams
    };
}
exports.createRequest = createRequest;