cloudfunctions/ClearingRedundantFolders/node_modules/bson/src/parser/utils.ts

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

import { Buffer } from 'buffer';
import { getGlobal } from '../utils/global';

type RandomBytesFunction = (size: number) => Uint8Array;

/**
 * Normalizes our expected stringified form of a function across versions of node
 * @param fn - The function to stringify
 */
export function normalizedFunctionString(fn: Function): string {
  return fn.toString().replace('function(', 'function (');
}

function isReactNative() {
  const g = getGlobal<{ navigator?: { product?: string } }>();
  return typeof g.navigator === 'object' && g.navigator.product === 'ReactNative';
}

const insecureRandomBytes: RandomBytesFunction = function insecureRandomBytes(size: number) {
  const insecureWarning = isReactNative()
    ? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
    : 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.';
  console.warn(insecureWarning);

  const result = Buffer.alloc(size);
  for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256);
  return result;
};

/* We do not want to have to include DOM types just for this check */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let window: any;
declare let require: Function;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let global: any;
declare const self: unknown;

const detectRandomBytes = (): RandomBytesFunction => {
  if (typeof window !== 'undefined') {
    // browser crypto implementation(s)
    const target = window.crypto || window.msCrypto; // allow for IE11
    if (target && target.getRandomValues) {
      return size => target.getRandomValues(Buffer.alloc(size));
    }
  }

  if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
    // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
    return size => global.crypto.getRandomValues(Buffer.alloc(size));
  }

  let requiredRandomBytes: RandomBytesFunction | null | undefined;
  try {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    requiredRandomBytes = require('crypto').randomBytes;
  } catch (e) {
    // keep the fallback
  }

  // NOTE: in transpiled cases the above require might return null/undefined
后面还有 63 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。