How to use the mime-types.charset function in mime-types

To help you get started, we’ve selected a few mime-types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / botbuilder-tools / packages / Chatdown / lib / index.js View on Github external
if (contentType) {
        contentType = contentType.toLowerCase();
        if (cardContentTypes[contentType])
            contentType = cardContentTypes[contentType];
    }
    else {
        contentType = mime.lookup(contentUrl) || cardContentTypes[path.extname(contentUrl)];

        if (!contentType && contentUrl && contentUrl.indexOf('http') == 0) {
            let options = { method: 'HEAD', uri: contentUrl };
            let response = await request(options);
            contentType = response['content-type'].split(';')[0];
        }
    }

    const charset = mime.charset(contentType);

    // if not a url
    if (contentUrl.indexOf('http') != 0) {
        // read the file
        let content = await readAttachmentFile(contentUrl, contentType);
        // if it is not a card
        if (!isCard(contentType) && charset !== 'UTF-8') {
            // send as base64
            contentUrl = `data:${contentType};base64,${new Buffer(content).toString('base64')}`;
            content = undefined;
        } else {
            contentUrl = undefined;
        }
        return (activity.attachments || (activity.attachments = [])).push(new Attachment({ contentType, contentUrl, content }));
    }
    // send as contentUrl
github aneilbaboo / replayer / src / cache.js View on Github external
if (resHeaders.error) {
          req.emit('error', resHeaders.error);
          return;
        }

        var res = new http.IncomingMessage(socket);
        res.headers = resHeaders.headers || {};
        res.statusCode = resHeaders.statusCode;

        if (callback) {
          callback(res);
        }

        if (!forceLive) {
          var isBinary = !mimeTypes.charset(resHeaders['content-encoding']);
          resBody = isBinary ? 
            fs.readFileSync(filename) : 
            replayerUtil.substituteWithRealValues(fs.readFileSync(filename).toString());
        }

        req.emit('response', res);

        if (res.push) {
          // node 0.10.x
          res.push(resBody);
          res.push(null);
        } else {
          // node 0.8.x
          res.emit('data', resBody);
          res.emit('end');
        }
github tivac / crucible / build / external / rollup-plugin-file-as-blob.js View on Github external
load: function load(id) {
			if(!filter(id)) {
                return null;
            }

			id = fs.realpathSync(id);
            
            var type = mime.lookup(id);
            var charset = mime.charset(type).toLowerCase();

            var readEncoding = 'base64';
            if (charset === 'utf-8') { readEncoding = 'utf8'; }
            if (charset.indexOf('ascii') !== -1) { readEncoding = 'ascii'; }

            var data = fs.readFileSync(id, readEncoding);

            var code;
            if (readEncoding === 'base64') {
                code = "export default __$strToBlobUri(atob(\"" + data + "\"), \"" + type + "\", true);";
            } else {
                // Unfortunately buble+rollup will create code that chokes
                // with newlines/quotes when the contents are read from
                // a file
                data = data.replace(/\n/g, '\\n')
                            .replace(/\r/g, '\\r')
github activescott / serverless-aws-static-file-handler / src / StaticFileHandler.js View on Github external
static isBinaryType(mimeType) {
    const mimeCharset = mimetypes.charset(mimeType)
    /* Using https://w3techs.com/technologies/overview/character_encoding/all
     * to be more comprehensive go through those at https://www.iana.org/assignments/character-sets/character-sets.xhtml
     */
    const textualCharSets = [
      "UTF-8",
      "ISO-8859-1",
      "Windows-1251",
      "Windows-1252",
      "Shift_JIS",
      "GB2312",
      "EUC-KR",
      "ISO-8859-2",
      "GBK",
      "Windows-1250",
      "EUC-JP",
      "Big5",
github egodigital / vscode-powertools / src / http.ts View on Github external
private getCharSet() {
        let charset: false | string = mimeTypes.charset(
            this.getContentType()
        );
        if (false === charset) {
            charset = 'ascii';
        }
        charset = charset.toLowerCase()
            .split('-')
            .join('')
            .trim();

        return charset;
    }
github TracerBench / tracerbench / packages / har-remix / src / index.ts View on Github external
entry: HAR.Entry,
    key: string
  ): Response | undefined {
    const { status, content } = entry.response;
    if (content && status >= 200 && status < 300) {
      let { text } = content;
      const { encoding, mimeType } = content as {
        encoding: 'base64';
        mimeType: string;
      };

      let body: Buffer | undefined;

      if (text === undefined) {
        body = undefined;
      } else if (mimeTypes.charset(mimeType) === 'UTF-8') {
        text = new Buffer(text, encoding).toString();

        if (this.delegate.textFor) {
          text = this.delegate.textFor(entry, key, text);
        }

        body = new Buffer(text);
      } else {
        body = new Buffer(text, encoding);
      }

      const compress =
        content.compression !== undefined && content.compression > 0;
      let response = this.buildResponse(status, mimeType, body, compress);
      if (this.delegate.finalizeResponse) {
        response = this.delegate.finalizeResponse(entry, key, response);
github Brightspace / frau-publisher / src / s3.js View on Github external
return promised(function s3Uploader(file) {
		if (!file.isBuffer()) {
			return Promise.resolve(file);
		}

		const base = replaceWindowsSlash(file.base);
		const path = replaceWindowsSlash(file.path);
		const uploadPath = path.replace(base, opt.uploadPath);

		const headers = JSON.parse(JSON.stringify(opt.headers));

		if (!headers['Content-Type']) {
			const contentType = opt.type || mime.lookup(uploadPath) || 'text/plain';
			const charset = opt.charset || mime.charset(contentType);

			headers['Content-Type'] = contentType;

			if (charset) {
				headers['Content-Type'] += `; charset=${charset.toLowerCase()}`;
			}
		}

		headers['Content-Length'] = file.contents.length;

		return new Promise((resolve, reject) => {
			client
				.put(uploadPath, headers)
				.on('response', res => {
					res.resume();