/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
'use strict';

/* global Buffer: true */

const crypto = require('crypto');

const isUTF8 = encoding => /^utf-?8$/i.test(encoding);

const constantFor = encoding =>
  /^ascii$/i.test(encoding) ? 1 :
  isUTF8(encoding) ? 2 :
  /^(?:utf-?16(?:le)?|ucs-?2)$/.test(encoding) ? 3 : 0;

module.exports = function(code, encoding) {
  const hash = crypto.createHash('sha1');
  hash.update(code, encoding);
  const digest = hash.digest('binary');
  const signature = Buffer.alloc ? Buffer.alloc(digest.length + 1) : new Buffer(digest.length + 1); // remove the new Buffer call when RN drops support for Node 4
  signature.write(digest, 'binary');
  signature.writeUInt8(
    constantFor(tryAsciiPromotion(code, encoding)),
    signature.length - 1);
  return signature;
};

function tryAsciiPromotion(string, encoding) {
  if (!isUTF8(encoding)) { return encoding; }
  for (let i = 0, n = string.length; i < n; i++) {
    if (string.charCodeAt(i) > 0x7f) { return encoding; }
  }
  return 'ascii';
}

@@ -298,16 +298,28 @@
 ectory.%0A
+ *%0A * @flow%0A
  */%0A'use
@@ -625,96 +625,278 @@
 ion(
+%0A  
 code
-, encoding) %7B%0A  const hash = crypto.createHash('sha1');%0A  hash.update(code, encoding
+: string,%0A  encoding: 'ascii' %7C 'utf8' %7C 'utf16le' = 'utf8',%0A): Buffer %7B%0A  const hash = crypto.createHash('sha1');%0A  // remove %60new Buffer%60 calls when RN drops support for Node 4%0A  hash.update(Buffer.from ? Buffer.from(code, encoding) : new Buffer(code, encoding)
 );%0A 
@@ -923,24 +923,16 @@
 .digest(
-'binary'
 );%0A  con
@@ -1029,106 +1029,32 @@
  1);
- // remove the new Buffer call when RN drops support for Node 4%0A  signature.write(digest, 'binary'
+%0A  digest.copy(signature
 );%0A 

add flow