All files / src/common index.ts

71.05% Statements 27/38
40% Branches 4/10
66.66% Functions 4/6
68.57% Lines 24/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 9168x   68x                                                                                           68x       13x 13x 13x 13x 13x 13x 1x           12x         12x 12x         68x 5507x 2022x 2022x 2022x 5507x 5507x   2022x     68x 1231x 1231x    
import { getGlobalScope } from '../storage/common';
 
export function arrayBufferToUint8(content: ArrayBuffer): Uint8Array {
  if (ArrayBuffer.isView(content)) {
    return new Uint8Array(content.buffer, content.byteOffset, content.byteLength);
  } else {
    throw new Error('Non array buffer passed to arrayBufferToUint8');
  }
}
 
function getAPIUsageErrorMessage(
  scopeObject: unknown,
  apiName: string,
  usageDesc?: string
): string {
  if (usageDesc) {
    return `Use of '${usageDesc}' requires \`${apiName}\` which is unavailable on the '${scopeObject}' object within the currently executing environment.`;
  } else {
    return `\`${apiName}\` is unavailable on the '${scopeObject}' object within the currently executing environment.`;
  }
}
 
interface GetGlobalObjectOptions {
  /**
   * Throw an error if the object is not found.
   * @default false
   */
  throwIfUnavailable?: boolean;
  /**
   * Additional information to include in an error if thrown.
   */
  usageDesc?: string;
  /**
   * If the object is not found, return an new empty object instead of undefined.
   * Requires [[throwIfUnavailable]] to be falsey.
   * @default false
   */
  returnEmptyObject?: boolean;
}
 
/**
 * Returns an object from the global scope (`Window` or `WorkerGlobalScope`) if it
 * is available within the currently executing environment.
 * When executing within the Node.js runtime these APIs are unavailable and will be
 * `undefined` unless the API is provided via polyfill.
 * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/self
 * @ignore
 */
export function getGlobalObject<K extends Extract<keyof Window, string>>(
  name: K,
  { throwIfUnavailable, usageDesc, returnEmptyObject }: GetGlobalObjectOptions = {}
): Window[K] | undefined {
  let globalScope: Window | undefined = undefined;
  try {
    globalScope = getGlobalScope();
    if (globalScope) {
      const obj = globalScope[name];
      if (obj) {
        return obj;
      }
    }
  } catch (error) {
    console.error(`Error getting object '${name}' from global scope '${globalScope}': ${error}`);
  }
  Iif (throwIfUnavailable) {
    const errMsg = getAPIUsageErrorMessage(globalScope, name.toString(), usageDesc);
    console.error(errMsg);
    throw new Error(errMsg);
  }
  if (returnEmptyObject) {
    return {} as any;
  }
  return undefined;
}
 
export function concatByteArrays(byteArrays: Uint8Array[]): Uint8Array {
  const totalSize = byteArrays.reduce((len, bytes) => len + bytes.length, 0);
  const resultArray = new Uint8Array(totalSize);
  let offset = 0;
  for (let i = 0; i < byteArrays.length; i++) {
    resultArray.set(byteArrays[i], offset);
    offset += byteArrays[i].length;
  }
  return resultArray;
}
 
export function ensureUint8Array(bytes: Uint8Array): Uint8Array {
  if (typeof bytes === 'object') bytes = Uint8Array.from(bytes);
  return bytes;
}