transferBufferToImmutable = (buffer, newLength = undefined) => {
  if (newLength !== undefined) {
    if (transfer) {
      buffer = apply(transfer, buffer, [newLength]);
    } else {
      buffer = arrayBufferTransfer(buffer);
      const oldLength = buffer.byteLength;
      // eslint-disable-next-line @endo/restrict-comparison-operands
      if (newLength <= oldLength) {
        buffer = arrayBufferSlice(buffer, 0, newLength);
      } else {
        const oldTA = new Uint8Array(buffer);
        const newTA = new Uint8Array(newLength);
        newTA.set(oldTA);
        buffer = newTA.buffer;
      }
    }
  }
  const result = new ImmutableArrayBufferInternal(buffer);
  return /** @type {ArrayBuffer} */ (/** @type {unknown} */ (result));
}
Transfer the contents to a new Immutable ArrayBuffer

@param {ArrayBuffer} buffer The original buffer.
@param {number} [newLength] The start index.
@returns {ArrayBuffer}