Skip to content

Commit

Permalink
Avoid default exports in favour of named exports (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut authored Jan 14, 2025
1 parent d8b01d8 commit 50865a3
Show file tree
Hide file tree
Showing 23 changed files with 47 additions and 56 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"rules": {
"style": {
"noParameterAssign": "warn",
"noDefaultExport": "warn",
"noDefaultExport": "error",
"useCollapsedElseIf": "error",
"useNodejsImportProtocol": "off"
},
Expand Down
1 change: 1 addition & 0 deletions demos/reactnative/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const styles = StyleSheet.create({
},
})

// biome-ignore lint/style/noDefaultExport: React Native uses default exports
export default class App extends React.Component {
constructor() {
super()
Expand Down
8 changes: 4 additions & 4 deletions lib/browser/fileReader.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { isReactNativeFile, isReactNativePlatform } from './isReactNative.js'
import uriToBlob from './uriToBlob.js'
import { uriToBlob } from './uriToBlob.js'

import type { FileReader, FileSource, UploadInput } from '../options.js'
import BlobFileSource from './sources/BlobFileSource.js'
import StreamFileSource from './sources/StreamFileSource.js'
import { BlobFileSource } from './sources/BlobFileSource.js'
import { StreamFileSource } from './sources/StreamFileSource.js'

function isWebStream(input: UploadInput): input is Pick<ReadableStreamDefaultReader, 'read'> {
return 'read' in input && typeof input.read === 'function'
}

// TODO: Make sure that we support ArrayBuffers, TypedArrays, DataViews and Blobs
export default class BrowserFileReader implements FileReader {
export class BrowserFileReader implements FileReader {
async openFile(input: UploadInput, chunkSize: number): Promise<FileSource> {
// In React Native, when user selects a file, instead of a File or Blob,
// you usually get a file object {} with a uri property that contains
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/fileSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isReactNativeFile, isReactNativePlatform } from './isReactNative.js'
/**
* Generate a fingerprint for a file which will be used the store the endpoint
*/
export default function fingerprint(file: UploadInput, options: UploadOptions) {
export function fingerprint(file: UploadInput, options: UploadOptions) {
if (isReactNativePlatform() && isReactNativeFile(file)) {
return Promise.resolve(reactNativeFingerprint(file, options))
}
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/httpStack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HttpProgressHandler, HttpRequest, HttpResponse, HttpStack } from '../options.js'

export default class XHRHttpStack implements HttpStack {
export class XHRHttpStack implements HttpStack {
createRequest(method, url) {
return new XHRRequest(method, url)
}
Expand Down
12 changes: 6 additions & 6 deletions lib/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import DetailedError from '../error.js'
import { DetailedError } from '../error.js'
import { enableDebugLog } from '../logger.js'
import NoopUrlStorage from '../noopUrlStorage.js'
import { NoopUrlStorage } from '../noopUrlStorage.js'
import type { UploadInput, UploadOptions } from '../options.js'
import BaseUpload, { terminate, defaultOptions as baseDefaultOptions } from '../upload.js'
import { BaseUpload, defaultOptions as baseDefaultOptions, terminate } from '../upload.js'

import BrowserFileReader from './fileReader.js'
import fingerprint from './fileSignature.js'
import DefaultHttpStack from './httpStack.js'
import { BrowserFileReader } from './fileReader.js'
import { fingerprint } from './fileSignature.js'
import { XHRHttpStack as DefaultHttpStack } from './httpStack.js'
import { WebStorageUrlStorage, canStoreURLs } from './urlStorage.js'

const defaultOptions = {
Expand Down
6 changes: 3 additions & 3 deletions lib/browser/sources/BlobFileSource.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { FileSource, SliceResult } from '../../options.js'
import isCordova from './isCordova.js'
import readAsByteArray from './readAsByteArray.js'
import { isCordova } from './isCordova.js'
import { readAsByteArray } from './readAsByteArray.js'

export default class BlobFileSource implements FileSource {
export class BlobFileSource implements FileSource {
_file: Blob

size: number
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/sources/StreamFileSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function concat<T extends StreamFileSource['_buffer']>(a: T, b: T): T {
throw new Error('Unknown data type')
}

export default class StreamFileSource implements FileSource {
export class StreamFileSource implements FileSource {
_reader: Pick<ReadableStreamDefaultReader<StreamFileSource['_buffer']>, 'read'>

_buffer: Blob | Uint8Array | number[] | undefined
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/sources/isCordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ const isCordova = () =>
typeof window !== 'undefined' &&
('PhoneGap' in window || 'Cordova' in window || 'cordova' in window)

export default isCordova
export { isCordova }
2 changes: 1 addition & 1 deletion lib/browser/sources/readAsByteArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
// TODO: Reconsider whether this is a sensible approach or whether we cause
// high memory usage with `chunkSize` is unset.
export default function readAsByteArray(chunk: Blob): Promise<Uint8Array> {
export function readAsByteArray(chunk: Blob): Promise<Uint8Array> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/uriToBlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* React Native to retrieve a file (identified by a file://
* URI) as a blob.
*/
export default function uriToBlob(uri: string): Promise<Blob> {
export function uriToBlob(uri: string): Promise<Blob> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.responseType = 'blob'
Expand Down
4 changes: 1 addition & 3 deletions lib/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HttpRequest, HttpResponse } from './options.js'

class DetailedError extends Error {
export class DetailedError extends Error {
originalRequest?: HttpRequest

originalResponse?: HttpResponse
Expand Down Expand Up @@ -29,5 +29,3 @@ class DetailedError extends Error {
this.message = message
}
}

export default DetailedError
10 changes: 5 additions & 5 deletions lib/node/fileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { ReadStream } from 'fs'
import isStream from 'is-stream'

import type { FileReader, UploadInput } from '../options.js'
import BufferSource from './sources/BufferFileSource.js'
import getFileSource from './sources/NodeFileSource.js'
import StreamFileSource from './sources/StreamFileSource.js'
import { BufferFileSource } from './sources/BufferFileSource.js'
import { getFileSource } from './sources/NodeFileSource.js'
import { StreamFileSource } from './sources/StreamFileSource.js'

export default class NodeFileReader implements FileReader {
export class NodeFileReader implements FileReader {
// TODO: Use async here and less Promise.resolve
openFile(input: UploadInput, chunkSize: number) {
if (Buffer.isBuffer(input)) {
return Promise.resolve(new BufferSource(input))
return Promise.resolve(new BufferFileSource(input))
}

if (input instanceof ReadStream && input.path != null) {
Expand Down
11 changes: 1 addition & 10 deletions lib/node/fileSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ import * as fs from 'fs'
import * as path from 'path'
import type { UploadInput, UploadOptions } from '../options.js'

/**
* Generate a fingerprint for a file which will be used the store the endpoint
*
* @param {File} file
* @param {Object} options
*/
export default function fingerprint(
file: UploadInput,
options: UploadOptions,
): Promise<string | null> {
export function fingerprint(file: UploadInput, options: UploadOptions): Promise<string | null> {
if (Buffer.isBuffer(file)) {
// create MD5 hash for buffer type
const blockSize = 64 * 1024 // 64kb
Expand Down
2 changes: 1 addition & 1 deletion lib/node/httpStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import throttle from 'lodash.throttle'
import type { HttpProgressHandler, HttpRequest, HttpResponse, HttpStack } from '../options.js'
import type { FileSliceTypes } from './index.js'

export default class NodeHttpStack implements HttpStack {
export class NodeHttpStack implements HttpStack {
private _requestOptions: http.RequestOptions

constructor(requestOptions: http.RequestOptions = {}) {
Expand Down
15 changes: 8 additions & 7 deletions lib/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { ReadStream } from 'node:fs'
import type { Readable } from 'node:stream'
import DetailedError from '../error.js'
import { DetailedError } from '../error.js'
import { enableDebugLog } from '../logger.js'
import NoopUrlStorage from '../noopUrlStorage.js'
import { NoopUrlStorage } from '../noopUrlStorage.js'
import type { UploadInput, UploadOptions } from '../options.js'
import BaseUpload, { terminate, defaultOptions as baseDefaultOptions } from '../upload.js'
import { BaseUpload, defaultOptions as baseDefaultOptions, terminate } from '../upload.js'

import NodeFileReader from './fileReader.js'
import fingerprint from './fileSignature.js'
import DefaultHttpStack from './httpStack.js'
import StreamFileSource from './sources/StreamFileSource.js'
import { NodeFileReader } from './fileReader.js'
import { fingerprint } from './fileSignature.js'
import { NodeHttpStack as DefaultHttpStack } from './httpStack.js'
import { StreamFileSource } from './sources/StreamFileSource.js'
import { FileUrlStorage, canStoreURLs } from './urlStorage.js'

const defaultOptions = {
Expand Down Expand Up @@ -49,5 +49,6 @@ export {
enableDebugLog,
DefaultHttpStack,
DetailedError,
// TODO: Remove `as`
StreamFileSource as StreamSource,
}
2 changes: 1 addition & 1 deletion lib/node/sources/BufferFileSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FileSource } from '../../options.js'

export default class BufferFileSource implements FileSource {
export class BufferFileSource implements FileSource {
size: number

private _buffer: Buffer
Expand Down
4 changes: 2 additions & 2 deletions lib/node/sources/NodeFileSource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ReadStream, createReadStream, promises as fsPromises } from 'fs'
import type { FileSource } from '../../options.js'

export default async function getFileSource(stream: ReadStream): Promise<NodeFileSource> {
export async function getFileSource(stream: ReadStream): Promise<NodeFileSource> {
const path = stream.path.toString()
const { size } = await fsPromises.stat(path)

Expand All @@ -23,7 +23,7 @@ export default async function getFileSource(stream: ReadStream): Promise<NodeFil
return new NodeFileSource(stream, path, actualSize)
}

class NodeFileSource implements FileSource {
export class NodeFileSource implements FileSource {
size: number

_stream: ReadStream
Expand Down
2 changes: 1 addition & 1 deletion lib/node/sources/StreamFileSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function readChunk(stream: Readable, size: number) {
* Note that it is forbidden to call with startB < startA or startB > endA. In other words,
* the slice calls cannot seek back and must not skip data from the stream.
*/
export default class StreamFileSource implements FileSource {
export class StreamFileSource implements FileSource {
// Setting the size to null indicates that we have no calculation available
// for how much data this stream will emit requiring the user to specify
// it manually (see the `uploadSize` option).
Expand Down
2 changes: 1 addition & 1 deletion lib/noopUrlStorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PreviousUpload, UrlStorage } from './options.js'

export default class NoopUrlStorage implements UrlStorage {
export class NoopUrlStorage implements UrlStorage {
findAllUploads() {
return Promise.resolve([])
}
Expand Down
2 changes: 1 addition & 1 deletion lib/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReadStream } from 'node:fs'
import type { Readable } from 'node:stream'
import type DetailedError from './error.js'
import type { DetailedError } from './error.js'

export const PROTOCOL_TUS_V1 = 'tus-v1'
export const PROTOCOL_IETF_DRAFT_03 = 'ietf-draft-03'
Expand Down
6 changes: 3 additions & 3 deletions lib/upload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Base64 } from 'js-base64'
import URL from 'url-parse'
import DetailedError from './error.js'
import { DetailedError } from './error.js'
import { log } from './logger.js'
import {
type FileSource,
Expand All @@ -14,7 +14,7 @@ import {
type UploadOptions,
type UrlStorage,
} from './options.js'
import uuid from './uuid.js'
import { uuid } from './uuid.js'

export const defaultOptions = {
endpoint: null,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const defaultOptions = {
protocol: PROTOCOL_TUS_V1 as UploadOptions['protocol'],
}

export default class BaseUpload {
export class BaseUpload {
options: UploadOptions

// The storage module used to store URLs
Expand Down
2 changes: 1 addition & 1 deletion lib/uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @return {string} The generate UUID
*/
export default function uuid(): string {
export function uuid(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
Expand Down

0 comments on commit 50865a3

Please sign in to comment.