mirror of
https://github.com/status-im/react-native.git
synced 2025-01-16 12:34:17 +00:00
8b931ae09f
Summary: Thanks for submitting a pull request! Please provide enough information so that others can review your pull request: > **Unless you are a React Native release maintainer and cherry-picking an *existing* commit into a current release, ensure your pull request is targeting the `master` React Native branch.** Explain the **motivation** for making this change. What existing problem does the pull request solve? Warning about new Buffer when init Prefer **small pull requests**. These are much easier to review and more likely to get merged. Make sure the PR does only one thing, otherwise please split it. **Test plan (required)** Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes UI. Make sure tests pass on both Travis and Circle CI. **Code formatting** Look around. Match the style of the rest of the codebase. See also the simple [style guide](https://github.com/facebook/react-native/blob/master/CONTRIBUTING.md#s Closes https://github.com/facebook/react-native/pull/11153 Differential Revision: D4234591 fbshipit-source-id: a9373f3151de8a50c18342f45fb9684a5c7416a6
41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
/**
|
|
* 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';
|
|
}
|