github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libmime/builder/index.js (about)

     1  // Copyright 2018 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  /*
     6   *
     7   * This file is adapted from github.com/jshttp/mime-db, with following LICENSE:
     8   * 
     9   * The MIT License (MIT)
    10   * 
    11   * Copyright (c) 2014 Jonathan Ong me@jongleberry.com
    12   * 
    13   * Permission is hereby granted, free of charge, to any person obtaining a copy
    14   * of this software and associated documentation files (the "Software"), to deal
    15   * in the Software without restriction, including without limitation the rights
    16   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17   * copies of the Software, and to permit persons to whom the Software is
    18   * furnished to do so, subject to the following conditions:
    19   * 
    20   * The above copyright notice and this permission notice shall be included in
    21   * all copies or substantial portions of the Software.
    22   * 
    23   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    26   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29   * THE SOFTWARE.
    30  */
    31  
    32  /**
    33   * Convert these text files to JSON for browser usage.
    34   */
    35  
    36  global.Promise = global.Promise || loadBluebird()
    37  
    38  var co = require('co')
    39  var cogent = require('cogent')
    40  
    41  /**
    42   * Mime types and associated extensions are stored in the form:
    43   *
    44   *   <type> <ext> <ext> <ext>;
    45   */
    46  var typeLineRegExp = /^\s*([\w-]+\/[\w+.-]+)((?:\s+[\w-]+)*);\s*$/gm
    47  
    48  co(function * () {
    49    var url = 'http://hg.nginx.org/nginx/raw-file/default/conf/mime.types'
    50    var res = yield * cogent(url, {
    51      string: true
    52    })
    53  
    54    if (res.statusCode !== 200) {
    55      throw new Error('got status code ' + res.statusCode + ' from ' + url)
    56    }
    57  
    58    var json = {}
    59    var match = null
    60  
    61    typeLineRegExp.index = 0
    62  
    63    while ((match = typeLineRegExp.exec(res.text))) {
    64      var mime = match[1]
    65  
    66      // parse the extensions
    67      var extensions = (match[2] || '')
    68        .split(/\s+/)
    69        .filter(Boolean)
    70      var data = json[mime] || (json[mime] = {})
    71  
    72      // append the extensions
    73      appendExtensions(data, extensions)
    74    }
    75  
    76    printGo(json)
    77  }).then()
    78  
    79  /**
    80   * Append an extension to an object.
    81   */
    82  function appendExtension (obj, extension) {
    83    if (!obj.extensions) {
    84      obj.extensions = []
    85    }
    86  
    87    if (obj.extensions.indexOf(extension) === -1) {
    88      obj.extensions.push(extension)
    89    }
    90  }
    91  
    92  /**
    93   * Append extensions to an object.
    94   */
    95  function appendExtensions (obj, extensions) {
    96    if (extensions.length === 0) {
    97      return
    98    }
    99  
   100    for (var i = 0; i < extensions.length; i++) {
   101      var extension = extensions[i]
   102  
   103      // add extension to the type entry
   104      appendExtension(obj, extension)
   105    }
   106  }
   107  
   108  /**
   109   * Load the Bluebird promise.
   110   */
   111  function loadBluebird () {
   112    var Promise = require('bluebird')
   113  
   114    // Silence all warnings
   115    Promise.config({
   116      warnings: false
   117    })
   118  
   119    return Promise
   120  }
   121  
   122  function getExtensionTypePairs(types) {
   123    return Object.keys(types).reduce( (accu, type) => {
   124      types[type].extensions.forEach( (ext) => accu.push({ext, type}) )
   125      return accu
   126    }, []).sort( (a, b) => a.ext < b.ext ? -1 : a.ext === b.ext ? 0 : 1)
   127  }
   128  
   129  function printGo(types) {
   130    process.stdout.write(`// Copyright 2018 Keybase Inc. All rights reserved.\n`)
   131    process.stdout.write(`// Use of this source code is governed by a BSD\n`)
   132    process.stdout.write(`// license that can be found in the LICENSE file.\n\n`)
   133    process.stdout.write(`// This file is auto-generated and should not be edited by hand.\n\n`)
   134    process.stdout.write(`package libmime\n\n`)
   135    process.stdout.write(`var mimeTypes = map[string]string{\n`)
   136    getExtensionTypePairs(types).forEach( ({ext, type}) => {
   137      process.stdout.write(`\t".${ext}": "${type}",\n`)
   138    })
   139    process.stdout.write(`}\n`)
   140  }