Skip to content

Commit

Permalink
Remove dependency on react-pdf internals in test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj committed Jul 27, 2023
1 parent 6fb4810 commit 2859170
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
3 changes: 2 additions & 1 deletion test/PassingOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { isDataURI } from 'react-pdf/src/shared/utils';

import { isDataURI } from './shared/utils';

import type { File, PassMethod } from './shared/types';

Expand Down
4 changes: 1 addition & 3 deletions test/Test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { pdfjs, Document, Outline, Page, Thumbnail } from 'react-pdf/src';
import 'react-pdf/src/Page/AnnotationLayer.css';
import 'react-pdf/src/Page/TextLayer.css';

import { isArrayBuffer, isBlob, isBrowser, loadFromFile } from 'react-pdf/src/shared/utils';

import './Test.css';

import AnnotationOptions from './AnnotationOptions';
Expand All @@ -15,7 +13,7 @@ import PassingOptions from './PassingOptions';
import ViewOptions from './ViewOptions';
import CustomRenderer from './CustomRenderer';

import { dataURItoBlob } from './shared/utils';
import { isArrayBuffer, isBlob, isBrowser, loadFromFile, dataURItoBlob } from './shared/utils';

import type { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist';
import type { ExternalLinkTarget, File, PassMethod, RenderMode } from './shared/types';
Expand Down
97 changes: 96 additions & 1 deletion test/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,60 @@
import { dataURItoByteString } from 'react-pdf/src/shared/utils';
import invariant from 'tiny-invariant';

/**
* Checks if we're running in a browser environment.
*/
export const isBrowser = typeof document !== 'undefined';

/**
* Checks whether a variable provided is a string.
*
* @param {*} variable Variable to check
*/
export function isString(variable: unknown): variable is string {
return typeof variable === 'string';
}

/**
* Checks whether a variable provided is an ArrayBuffer.
*
* @param {*} variable Variable to check
*/
export function isArrayBuffer(variable: unknown): variable is ArrayBuffer {
return variable instanceof ArrayBuffer;
}

/**
* Checks whether a variable provided is a Blob.
*
* @param {*} variable Variable to check
*/
export function isBlob(variable: unknown): variable is Blob {
invariant(isBrowser, 'isBlob can only be used in a browser environment');

return variable instanceof Blob;
}

/**
* Checks whether a variable provided is a data URI.
*
* @param {*} variable String to check
*/
export function isDataURI(variable: unknown): variable is `data:${string}` {
return isString(variable) && /^data:/.test(variable);
}

export function dataURItoByteString(dataURI: unknown): string {
invariant(isDataURI(dataURI), 'Invalid data URI.');

const [headersString = '', dataString = ''] = dataURI.split(',');
const headers = headersString.split(';');

if (headers.indexOf('base64') !== -1) {
return atob(dataString);
}

return unescape(dataString);
}

function dataURItoUint8Array(dataURI: string): Uint8Array {
const byteString = dataURItoByteString(dataURI);
Expand All @@ -22,3 +78,42 @@ export function dataURItoBlob(dataURI: string): Blob {
const mimeString = header.split(':')[1];
return new Blob([ia], { type: mimeString });
}

export function loadFromFile(file: Blob): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const reader = new FileReader();

reader.onload = () => {
if (!reader.result) {
return reject(new Error('Error while reading a file.'));
}

resolve(reader.result as ArrayBuffer);
};

reader.onerror = (event) => {
if (!event.target) {
return reject(new Error('Error while reading a file.'));
}

const { error } = event.target;

if (!error) {
return reject(new Error('Error while reading a file.'));
}

switch (error.code) {
case error.NOT_FOUND_ERR:
return reject(new Error('Error while reading a file: File not found.'));
case error.SECURITY_ERR:
return reject(new Error('Error while reading a file: Security error.'));
case error.ABORT_ERR:
return reject(new Error('Error while reading a file: Aborted.'));
default:
return reject(new Error('Error while reading a file.'));
}
};

reader.readAsArrayBuffer(file);
});
}

0 comments on commit 2859170

Please sign in to comment.