signal-desktop/ts/util/arrayBufferToObjectURL.ts

23 lines
466 B
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import is from '@sindresorhus/is';
import type { MIMEType } from '../types/MIME';
2018-04-14 02:14:58 +00:00
export const arrayBufferToObjectURL = ({
data,
type,
}: {
data: ArrayBuffer;
type: MIMEType;
}): string => {
if (!is.arrayBuffer(data)) {
throw new TypeError('`data` must be an ArrayBuffer');
}
2018-04-14 02:14:58 +00:00
const blob = new Blob([data], { type });
2018-04-14 02:14:58 +00:00
return URL.createObjectURL(blob);
};