From 255320ccfcdf9a53560c669fac7b5a449e46a77e Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Wed, 4 Dec 2024 10:43:26 +0100 Subject: [PATCH 1/6] move Source from class to interface in its own module --- packages/components/demo/App.tsx | 3 +- .../components/src/components/Breadcrumb.tsx | 2 +- packages/components/src/components/Cell.tsx | 2 +- packages/components/src/components/File.tsx | 2 +- packages/components/src/components/Folder.tsx | 2 +- packages/components/src/components/Page.tsx | 7 +- .../src/components/viewers/ImageView.tsx | 2 +- .../src/components/viewers/MarkdownView.tsx | 2 +- .../src/components/viewers/ParquetView.tsx | 2 +- .../src/components/viewers/TextView.tsx | 2 +- .../src/components/viewers/Viewer.tsx | 2 +- packages/components/src/lib/filesystem.ts | 78 ++++--------------- packages/components/src/lib/index.ts | 4 +- packages/components/src/lib/source.ts | 34 ++++++++ 14 files changed, 61 insertions(+), 83 deletions(-) create mode 100644 packages/components/src/lib/source.ts diff --git a/packages/components/demo/App.tsx b/packages/components/demo/App.tsx index d1ae9fdb..77ef2728 100644 --- a/packages/components/demo/App.tsx +++ b/packages/components/demo/App.tsx @@ -1,8 +1,7 @@ import React from 'react' import { Page } from '../src/index.js' -import { HttpFileSystem } from '../src/index.ts' -import { Source } from '../src/lib/filesystem.js' +import { HttpFileSystem, Source } from '../src/index.ts' export interface Navigation { col?: number row?: number diff --git a/packages/components/src/components/Breadcrumb.tsx b/packages/components/src/components/Breadcrumb.tsx index fe52f6c1..a2f2579d 100644 --- a/packages/components/src/components/Breadcrumb.tsx +++ b/packages/components/src/components/Breadcrumb.tsx @@ -1,5 +1,5 @@ -import { Source } from '../lib/filesystem.js' import { RoutesConfig } from '../lib/routes.js' +import { Source } from '../lib/source.js' export type BreadcrumbConfig = RoutesConfig interface BreadcrumbProps { diff --git a/packages/components/src/components/Cell.tsx b/packages/components/src/components/Cell.tsx index d7389be0..680fd0e9 100644 --- a/packages/components/src/components/Cell.tsx +++ b/packages/components/src/components/Cell.tsx @@ -1,7 +1,7 @@ import { asyncRows } from 'hightable' import { asyncBufferFromUrl, parquetMetadataAsync } from 'hyparquet' import { useEffect, useState } from 'react' -import { FileSource } from '../lib/filesystem.js' +import { FileSource } from '../lib/source.js' import { parquetDataFrame } from '../lib/tableProvider.js' import Breadcrumb, { BreadcrumbConfig } from './Breadcrumb.js' import Layout from './Layout.js' diff --git a/packages/components/src/components/File.tsx b/packages/components/src/components/File.tsx index 7f90bfde..24248fd6 100644 --- a/packages/components/src/components/File.tsx +++ b/packages/components/src/components/File.tsx @@ -1,5 +1,5 @@ import { useState } from 'react' -import { FileSource } from '../lib/filesystem.js' +import { FileSource } from '../lib/source.js' import Breadcrumb, { BreadcrumbConfig } from './Breadcrumb.js' import Layout from './Layout.js' import Viewer, { ViewerConfig } from './viewers/Viewer.js' diff --git a/packages/components/src/components/Folder.tsx b/packages/components/src/components/Folder.tsx index 8596ad75..2ec2ceef 100644 --- a/packages/components/src/components/Folder.tsx +++ b/packages/components/src/components/Folder.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react' -import { DirSource, FileMetadata } from '../lib/filesystem.js' +import { DirSource, FileMetadata } from '../lib/source.js' import { cn, formatFileSize, getFileDate, getFileDateShort } from '../lib/utils.js' import Breadcrumb, { BreadcrumbConfig } from './Breadcrumb.js' import Layout, { Spinner } from './Layout.js' diff --git a/packages/components/src/components/Page.tsx b/packages/components/src/components/Page.tsx index 03114126..3c80de83 100644 --- a/packages/components/src/components/Page.tsx +++ b/packages/components/src/components/Page.tsx @@ -1,4 +1,4 @@ -import { DirSource, FileSource, Source } from '../lib/filesystem.js' +import { Source } from '../lib/source.js' import { BreadcrumbConfig } from './Breadcrumb.js' import Cell from './Cell.js' import File, { FileConfig } from './File.js' @@ -17,12 +17,9 @@ interface PageProps { } export default function Page({ source, navigation, config }: PageProps) { - if (source instanceof DirSource) { + if (source.kind === 'directory') { return } - if (!(source instanceof FileSource)) { - return
Invalid source
- } if (navigation?.row !== undefined && navigation.col !== undefined) { // cell view return diff --git a/packages/components/src/components/viewers/ImageView.tsx b/packages/components/src/components/viewers/ImageView.tsx index ac71c7c8..b2cc3125 100644 --- a/packages/components/src/components/viewers/ImageView.tsx +++ b/packages/components/src/components/viewers/ImageView.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react' -import { FileSource } from '../../lib/filesystem.js' +import { FileSource } from '../../lib/source.js' import { contentTypes, parseFileSize } from '../../lib/utils.js' import { Spinner } from '../Layout.js' import ContentHeader from './ContentHeader.js' diff --git a/packages/components/src/components/viewers/MarkdownView.tsx b/packages/components/src/components/viewers/MarkdownView.tsx index 65d10d03..32322879 100644 --- a/packages/components/src/components/viewers/MarkdownView.tsx +++ b/packages/components/src/components/viewers/MarkdownView.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react' -import { FileSource } from '../../lib/filesystem.js' +import { FileSource } from '../../lib/source.js' import { parseFileSize } from '../../lib/utils.js' import { Spinner } from '../Layout.js' import Markdown from '../Markdown.js' diff --git a/packages/components/src/components/viewers/ParquetView.tsx b/packages/components/src/components/viewers/ParquetView.tsx index 316eb98a..b90b045f 100644 --- a/packages/components/src/components/viewers/ParquetView.tsx +++ b/packages/components/src/components/viewers/ParquetView.tsx @@ -1,8 +1,8 @@ import HighTable, { DataFrame, rowCache } from 'hightable' import { asyncBufferFromUrl, parquetMetadataAsync } from 'hyparquet' import React, { useCallback, useEffect, useState } from 'react' -import { FileSource } from '../../lib/filesystem.js' import { RoutesConfig, appendSearchParams } from '../../lib/routes.js' +import { FileSource } from '../../lib/source.js' import { parquetDataFrame } from '../../lib/tableProvider.js' import { Spinner } from '../Layout.js' import CellPanel from './CellPanel.js' diff --git a/packages/components/src/components/viewers/TextView.tsx b/packages/components/src/components/viewers/TextView.tsx index b76117eb..c9f4d365 100644 --- a/packages/components/src/components/viewers/TextView.tsx +++ b/packages/components/src/components/viewers/TextView.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react' -import { FileSource } from '../../lib/filesystem.js' +import { FileSource } from '../../lib/source.js' import { parseFileSize } from '../../lib/utils.js' import { Spinner } from '../Layout.js' import ContentHeader, { TextContent } from './ContentHeader.js' diff --git a/packages/components/src/components/viewers/Viewer.tsx b/packages/components/src/components/viewers/Viewer.tsx index 505d14fd..9bf34abf 100644 --- a/packages/components/src/components/viewers/Viewer.tsx +++ b/packages/components/src/components/viewers/Viewer.tsx @@ -1,4 +1,4 @@ -import { FileSource } from '../../lib/filesystem.js' +import { FileSource } from '../../lib/source.js' import { imageTypes } from '../../lib/utils.js' import ImageView from './ImageView.js' import MarkdownView from './MarkdownView.js' diff --git a/packages/components/src/lib/filesystem.ts b/packages/components/src/lib/filesystem.ts index 8e57ca95..7133ba0e 100644 --- a/packages/components/src/lib/filesystem.ts +++ b/packages/components/src/lib/filesystem.ts @@ -1,19 +1,6 @@ +import type { DirSource, FileKind, FileMetadata, FileSource, SourcePart } from './source.js' import { getFileName } from './utils.js' -export type FileKind = 'file' | 'directory' -export interface FileMetadata { - name: string - eTag?: string - size?: number - lastModified?: string - source: string - kind: FileKind -} -export interface SourcePart { - name: string - source: string -} - export abstract class FileSystem { abstract fsId: string abstract canParse(source: string): boolean @@ -28,10 +15,18 @@ export abstract class FileSystem { if (!this.canParse(source)) { return } - if (this.getKind(source) === 'file') { - return new FileSource(source, this) - } else { - return new DirSource(source, this) + return { + source, + getSourceParts: () => this.getSourceParts(source), + ...this.getKind(source) === 'file' ? { + kind: 'file', + fileName: this.getFileName(source), + resolveUrl: this.getResolveUrl(source), + } : { + kind: 'directory', + prefix: this.getPrefix(source), + listFiles: () => this.listFiles(this.getPrefix(source)), + }, } } catch { console.debug('Failed to parse source', source) @@ -39,53 +34,6 @@ export abstract class FileSystem { } } -export abstract class Source { - abstract kind: 'file' | 'directory' - fs: FileSystem - source: string - constructor(source: string, fs: FileSystem) { - this.source = source - this.fs = fs - if (!this.fs.canParse(source)) { - throw new Error('Invalid source') - } - } - getSourceParts(): SourcePart[] { - return this.fs.getSourceParts(this.source) - } -} - -export class FileSource extends Source { - kind = 'file' as const - fileName: string - resolveUrl: string - - constructor(source: string, fs: FileSystem) { - super(source, fs) - if (this.fs.getKind(source) !== 'file') { - throw new Error('Invalid file source') - } - this.resolveUrl = this.fs.getResolveUrl(source) - this.fileName = this.fs.getFileName(source) - } -} - -export class DirSource extends Source { - kind = 'directory' as const - prefix: string - - constructor(source: string, fs: FileSystem) { - super(source, fs) - if (this.fs.getKind(source) !== 'directory') { - throw new Error('Invalid directory source') - } - this.prefix = this.fs.getPrefix(source) - } - - listFiles(): Promise { - return this.fs.listFiles(this.prefix) - } -} // Built-in implementations diff --git a/packages/components/src/lib/index.ts b/packages/components/src/lib/index.ts index 6d17c682..7c68de0b 100644 --- a/packages/components/src/lib/index.ts +++ b/packages/components/src/lib/index.ts @@ -1,7 +1,7 @@ -export { DirSource, FileSource, FileSystem, HttpFileSystem, HyperparamFileSystem, Source } from './filesystem.js' -export type { FileKind, FileMetadata, SourcePart } from './filesystem.js' +export { FileSystem, HttpFileSystem, HyperparamFileSystem } from './filesystem.js' export { appendSearchParams, replaceSearchParams } from './routes.js' export type { RoutesConfig } from './routes.js' +export type { DirSource, FileKind, FileMetadata, FileSource, Source, SourcePart } from './source.js' export { parquetDataFrame } from './tableProvider.js' export { asyncBufferFrom, cn, contentTypes, formatFileSize, getFileDate, getFileDateShort, getFileName, imageTypes, parseFileSize } from './utils.js' export { parquetQueryWorker } from './workers/parquetWorkerClient.js' diff --git a/packages/components/src/lib/source.ts b/packages/components/src/lib/source.ts new file mode 100644 index 00000000..7ed24dda --- /dev/null +++ b/packages/components/src/lib/source.ts @@ -0,0 +1,34 @@ +export type FileKind = 'file' | 'directory' + +export interface FileMetadata { + name: string + eTag?: string + size?: number + lastModified?: string + source: string + kind: FileKind +} + +export interface SourcePart { + name: string + source: string +} + +interface BaseSource { + source: string + getSourceParts: () => SourcePart[] +} + +export interface FileSource extends BaseSource { + kind: 'file' + fileName: string + resolveUrl: string +} + +export interface DirSource extends BaseSource { + kind: 'directory' + prefix: string, + listFiles: () => Promise +} + +export type Source = FileSource | DirSource From 6eda16185dfc945e99e7067d3f2fcab0a4d70d34 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Wed, 4 Dec 2024 11:45:14 +0100 Subject: [PATCH 2/6] replace FileSytem classes with interfaces --- packages/components/demo/App.tsx | 6 +- .../components/src/components/Breadcrumb.tsx | 3 +- packages/components/src/components/Folder.tsx | 3 +- packages/components/src/lib/filesystem.ts | 205 ++++++++---------- packages/components/src/lib/index.ts | 6 +- packages/components/src/lib/source.ts | 47 ++-- .../components/test/components/File.test.tsx | 13 +- .../test/components/Folder.test.tsx | 18 +- .../components/viewers/ImageView.test.tsx | 7 +- .../components/viewers/MarkdownView.test.tsx | 7 +- .../components/test/lib/filesystem.test.ts | 49 ++--- 11 files changed, 174 insertions(+), 190 deletions(-) diff --git a/packages/components/demo/App.tsx b/packages/components/demo/App.tsx index 77ef2728..bd723363 100644 --- a/packages/components/demo/App.tsx +++ b/packages/components/demo/App.tsx @@ -1,7 +1,7 @@ import React from 'react' import { Page } from '../src/index.js' -import { HttpFileSystem, Source } from '../src/index.ts' +import { Source, createHttpFileSystem, getSource } from '../src/index.ts' export interface Navigation { col?: number row?: number @@ -16,7 +16,7 @@ function getNumberParam(search: URLSearchParams, key: string): number | undefine } const fileSystems = [ - new HttpFileSystem(), + createHttpFileSystem(), ] export default function App() { @@ -33,7 +33,7 @@ export default function App() { let source: Source | undefined = undefined for (const fileSystem of fileSystems) { - const fsSource = fileSystem.getSource(url) + const fsSource = getSource(url, fileSystem) if (fsSource){ source = fsSource break diff --git a/packages/components/src/components/Breadcrumb.tsx b/packages/components/src/components/Breadcrumb.tsx index a2f2579d..b3846299 100644 --- a/packages/components/src/components/Breadcrumb.tsx +++ b/packages/components/src/components/Breadcrumb.tsx @@ -13,10 +13,9 @@ interface BreadcrumbProps { export default function Breadcrumb({ source, config }: BreadcrumbProps) { return - } diff --git a/packages/components/src/components/Folder.tsx b/packages/components/src/components/Folder.tsx index 2ec2ceef..975d64a7 100644 --- a/packages/components/src/components/Folder.tsx +++ b/packages/components/src/components/Folder.tsx @@ -1,5 +1,6 @@ import { useEffect, useState } from 'react' -import { DirSource, FileMetadata } from '../lib/source.js' +import type { FileMetadata } from '../lib/filesystem.js' +import type { DirSource } from '../lib/source.js' import { cn, formatFileSize, getFileDate, getFileDateShort } from '../lib/utils.js' import Breadcrumb, { BreadcrumbConfig } from './Breadcrumb.js' import Layout, { Spinner } from './Layout.js' diff --git a/packages/components/src/lib/filesystem.ts b/packages/components/src/lib/filesystem.ts index 7133ba0e..14b2c15f 100644 --- a/packages/components/src/lib/filesystem.ts +++ b/packages/components/src/lib/filesystem.ts @@ -1,65 +1,47 @@ -import type { DirSource, FileKind, FileMetadata, FileSource, SourcePart } from './source.js' import { getFileName } from './utils.js' -export abstract class FileSystem { - abstract fsId: string - abstract canParse(source: string): boolean - abstract getKind(source: string): FileKind - abstract getFileName(source: string): string - abstract getPrefix(source: string): string - abstract getResolveUrl(source: string): string - abstract getSourceParts(source: string): SourcePart[] - abstract listFiles(prefix: string): Promise - getSource(source: string): FileSource | DirSource | undefined { - try { - if (!this.canParse(source)) { - return - } - return { - source, - getSourceParts: () => this.getSourceParts(source), - ...this.getKind(source) === 'file' ? { - kind: 'file', - fileName: this.getFileName(source), - resolveUrl: this.getResolveUrl(source), - } : { - kind: 'directory', - prefix: this.getPrefix(source), - listFiles: () => this.listFiles(this.getPrefix(source)), - }, - } - } catch { - console.debug('Failed to parse source', source) - } - } +export type FileKind = 'file' | 'directory' + +export interface FileMetadata { + name: string + eTag?: string + size?: number + lastModified?: string + source: string + kind: FileKind } +export interface SourcePart { + name: string + source: string +} -// Built-in implementations +export interface FileSystem { + fsId: string + canParse: (source: string) => boolean + getKind: (source: string) => FileKind + getFileName: (source: string) => string + getPrefix: (source: string) => string + getResolveUrl: (source: string) => string + getSourceParts: (source: string) => SourcePart[] + listFiles: (prefix: string) => Promise +} -export class HttpFileSystem extends FileSystem { - fsId = 'http' as const - canParse(source: string): boolean { - return URL.canParse(source) - } - getKind(): FileKind { - /// all the URLs are considered files - return 'file' - } - getFileName(source: string): string { - return getFileName(source) - } - getPrefix(): string { - throw new Error('Not implemented') - } - getResolveUrl(source: string): string { - return source - } - getSourceParts(source: string): SourcePart[] { - return [{ name: source, source }] - } - listFiles(): Promise { - throw new Error('Not implemented') +function notImplemented(): never { + throw new Error('Not implemented') +} + +// Built-in implementations +export function createHttpFileSystem(): FileSystem { + return { + fsId: 'http' as const, + canParse: source => URL.canParse(source), + getKind: () => 'file', /// all the URLs are considered files + getFileName, + getPrefix: notImplemented, + getResolveUrl: source => source, + getSourceParts: source => [{ name: source, source }], + listFiles: notImplemented, } } @@ -69,68 +51,61 @@ export interface HyperparamFileMetadata { fileSize?: number lastModified: string } - -export class HyperparamFileSystem extends FileSystem { - fsId = 'hyparam' as const - endpoint: string - constructor({ endpoint }: {endpoint: string}) { - if (!URL.canParse(endpoint)) { - throw new Error('Invalid endpoint') - } - super() - this.endpoint = endpoint +async function fetchHyperparamFilesList(prefix: string, endpoint: string): Promise { + const url = new URL('/api/store/list', endpoint) + url.searchParams.append('prefix', prefix) + const res = await fetch(url) + if (res.ok) { + return await res.json() as HyperparamFileMetadata[] + } else { + throw new Error(await res.text()) } - canParse(source: string): boolean { - /// we expect relative paths, such as path/to/file or path/to/dir/ - /// let's just check that it is empty or starts with a "word" character - return source === '' || /^[\w]/.test(source) +} +export function createHyperparamFileSystem({ endpoint }: {endpoint: string}): FileSystem { + if (!URL.canParse(endpoint)) { + throw new Error('Invalid endpoint') } - getKind(source: string): FileKind { + function getKind(source: string): FileKind { return source === '' || source.endsWith('/') ? 'directory' : 'file' } - getFileName(source: string): string { - return getFileName(source) - } - getPrefix(source: string): string { - return source.replace(/\/$/, '') - } - getResolveUrl(source: string): string { - const url = new URL('/api/store/get', this.endpoint) - url.searchParams.append('key', source) - return url.toString() - } - getSourceParts(source: string): SourcePart[] { - const parts = source.split('/') - return [ - { 'name': '/', 'source': '' }, - ...parts.map((part, depth) => { - const slashSuffix = depth === parts.length - 1 ? '' : '/' - return { - name: part + slashSuffix, - source: parts.slice(0, depth + 1).join('/') + slashSuffix, - } - }), - ] - } - async _fetchFilesList(prefix: string): Promise { - const url = new URL('/api/store/list', this.endpoint) - url.searchParams.append('prefix', prefix) - const res = await fetch(url) - if (res.ok) { - return await res.json() as HyperparamFileMetadata[] - } else { - throw new Error(await res.text()) - } - } - async listFiles(prefix: string): Promise { - const files = await this._fetchFilesList(prefix) - return files.map(file => ({ - name: file.key, - eTag: file.eTag, - size: file.fileSize, - lastModified: file.lastModified, - source: (prefix === '' ? '' : prefix + '/') + file.key, - kind: this.getKind(file.key), - })) + return { + fsId: 'hyperparam' as const, + canParse: (source: string): boolean => { + /// we expect relative paths, such as path/to/file or path/to/dir/ + /// let's just check that it is empty or starts with a "word" character + return source === '' || /^[\w]/.test(source) + }, + getKind, + getFileName, + getPrefix: source => source.replace(/\/$/, ''), + getResolveUrl: (source: string): string => { + const url = new URL('/api/store/get', endpoint) + url.searchParams.append('key', source) + return url.toString() + }, + getSourceParts: (source: string): SourcePart[] => { + const parts = source.split('/') + return [ + { 'name': '/', 'source': '' }, + ...parts.map((part, depth) => { + const slashSuffix = depth === parts.length - 1 ? '' : '/' + return { + name: part + slashSuffix, + source: parts.slice(0, depth + 1).join('/') + slashSuffix, + } + }), + ] + }, + async listFiles(prefix: string): Promise { + const files = await fetchHyperparamFilesList(prefix, endpoint) + return files.map(file => ({ + name: file.key, + eTag: file.eTag, + size: file.fileSize, + lastModified: file.lastModified, + source: (prefix === '' ? '' : prefix + '/') + file.key, + kind: getKind(file.key), + })) + }, } } diff --git a/packages/components/src/lib/index.ts b/packages/components/src/lib/index.ts index 7c68de0b..f896348a 100644 --- a/packages/components/src/lib/index.ts +++ b/packages/components/src/lib/index.ts @@ -1,7 +1,9 @@ -export { FileSystem, HttpFileSystem, HyperparamFileSystem } from './filesystem.js' +export { createHttpFileSystem, createHyperparamFileSystem } from './filesystem.js' +export type { FileKind, FileMetadata, FileSystem, SourcePart } from './filesystem.js' export { appendSearchParams, replaceSearchParams } from './routes.js' export type { RoutesConfig } from './routes.js' -export type { DirSource, FileKind, FileMetadata, FileSource, Source, SourcePart } from './source.js' +export { getSource } from './source.js' +export type { DirSource, FileSource, Source } from './source.js' export { parquetDataFrame } from './tableProvider.js' export { asyncBufferFrom, cn, contentTypes, formatFileSize, getFileDate, getFileDateShort, getFileName, imageTypes, parseFileSize } from './utils.js' export { parquetQueryWorker } from './workers/parquetWorkerClient.js' diff --git a/packages/components/src/lib/source.ts b/packages/components/src/lib/source.ts index 7ed24dda..516e004c 100644 --- a/packages/components/src/lib/source.ts +++ b/packages/components/src/lib/source.ts @@ -1,22 +1,8 @@ -export type FileKind = 'file' | 'directory' - -export interface FileMetadata { - name: string - eTag?: string - size?: number - lastModified?: string - source: string - kind: FileKind -} - -export interface SourcePart { - name: string - source: string -} +import type { FileMetadata, FileSystem, SourcePart } from './filesystem.js' interface BaseSource { source: string - getSourceParts: () => SourcePart[] + sourceParts: SourcePart[] } export interface FileSource extends BaseSource { @@ -32,3 +18,32 @@ export interface DirSource extends BaseSource { } export type Source = FileSource | DirSource + +export function getSource(source: string, fs: FileSystem): FileSource | DirSource | undefined { + try { + if (!fs.canParse(source)) { + return + } + const sourceParts = fs.getSourceParts(source) + if (fs.getKind(source) === 'file') { + return { + kind: 'file', + source, + sourceParts, + fileName: fs.getFileName(source), + resolveUrl: fs.getResolveUrl(source), + } + } else { + const prefix = fs.getPrefix(source) + return { + kind: 'directory', + source, + sourceParts, + prefix, + listFiles: () => fs.listFiles(prefix), + } + } + } catch { + console.debug('Failed to get source', source) + } +} diff --git a/packages/components/test/components/File.test.tsx b/packages/components/test/components/File.test.tsx index 72c55e40..69d71662 100644 --- a/packages/components/test/components/File.test.tsx +++ b/packages/components/test/components/File.test.tsx @@ -3,11 +3,12 @@ import { strict as assert } from 'assert' import React, { act } from 'react' import { describe, expect, it, vi } from 'vitest' import File from '../../src/components/File.js' -import { HttpFileSystem, HyperparamFileSystem } from '../../src/lib/filesystem.js' +import { createHttpFileSystem, createHyperparamFileSystem } from '../../src/lib/filesystem.js' import { RoutesConfig } from '../../src/lib/routes.js' +import { getSource } from '../../src/lib/source.js' -const hyparamFileSystem = new HyperparamFileSystem({ endpoint: 'http://localhost:3000' }) -const httpFileSystem = new HttpFileSystem() +const hyparamFileSystem = createHyperparamFileSystem({ endpoint: 'http://localhost:3000' }) +const httpFileSystem = createHttpFileSystem() const config: RoutesConfig = { @@ -22,7 +23,7 @@ global.fetch = vi.fn(() => Promise.resolve({ text: vi.fn() } as unknown as Respo describe('File Component', () => { it('renders a local file path', async () => { - const source = hyparamFileSystem.getSource('folder/subfolder/test.txt') + const source = getSource('folder/subfolder/test.txt', hyparamFileSystem) assert(source?.kind === 'file') const { getByText } = await act(() => render( @@ -37,7 +38,7 @@ describe('File Component', () => { it('renders a URL', async () => { const url = 'https://example.com/test.txt' - const source = httpFileSystem.getSource(url) + const source = getSource(url, httpFileSystem) assert(source?.kind === 'file') const { getByText } = await act(() => render()) @@ -46,7 +47,7 @@ describe('File Component', () => { }) it('renders correct breadcrumbs for nested folders', async () => { - const source = hyparamFileSystem.getSource('folder1/folder2/folder3/test.txt') + const source = getSource('folder1/folder2/folder3/test.txt', hyparamFileSystem) assert(source?.kind === 'file') const { getAllByRole } = await act(() => render( diff --git a/packages/components/test/components/Folder.test.tsx b/packages/components/test/components/Folder.test.tsx index 2db30aca..1ff9cd78 100644 --- a/packages/components/test/components/Folder.test.tsx +++ b/packages/components/test/components/Folder.test.tsx @@ -3,15 +3,11 @@ import { strict as assert } from 'assert' import React from 'react' import { describe, expect, it, test, vi } from 'vitest' import Folder from '../../src/components/Folder.js' -import { HyperparamFileMetadata, HyperparamFileSystem } from '../../src/lib/filesystem.js' +import { HyperparamFileMetadata, createHyperparamFileSystem } from '../../src/lib/filesystem.js' import { RoutesConfig } from '../../src/lib/routes.js' +import { getSource } from '../../src/lib/source.js' -const hyparamFileSystem = new HyperparamFileSystem({ endpoint: 'http://localhost:3000' }) -// hyparamFileSystem._fetchFilesList = () => Promise.resolve([ -// { key: 'folder1/', lastModified: '2023-01-01T00:00:00Z' }, -// { key: 'file1.txt', fileSize: 8196, lastModified: '2023-01-01T00:00:00Z' }, -// ]) - +const hyparamFileSystem = createHyperparamFileSystem({ endpoint: 'http://localhost:3000' }) const mockFiles: HyperparamFileMetadata[] = [ { key: 'folder1/', lastModified: '2023-01-01T00:00:00Z' }, { key: 'file1.txt', fileSize: 8196, lastModified: '2023-01-01T00:00:00Z' }, @@ -35,7 +31,7 @@ describe('Folder Component', () => { ok: true, } as Response) - const source = hyparamFileSystem.getSource(path) + const source = getSource(path, hyparamFileSystem) assert(source?.kind === 'directory') const { findByText, getByText } = render() @@ -57,7 +53,7 @@ describe('Folder Component', () => { ok: true, } as Response) - const source = hyparamFileSystem.getSource('') + const source = getSource('', hyparamFileSystem) assert(source?.kind === 'directory') const { container } = render() @@ -71,7 +67,7 @@ describe('Folder Component', () => { ok: false, } as Response) - const source = hyparamFileSystem.getSource('test-prefix/') + const source = getSource('test-prefix/', hyparamFileSystem) assert(source?.kind === 'directory') const { findByText, queryByText } = render() @@ -89,7 +85,7 @@ describe('Folder Component', () => { ok: true, } as Response) - const source = hyparamFileSystem.getSource('subdir1/subdir2/') + const source = getSource('subdir1/subdir2/', hyparamFileSystem) assert(source?.kind === 'directory') const { findByText, getByText } = render() diff --git a/packages/components/test/components/viewers/ImageView.test.tsx b/packages/components/test/components/viewers/ImageView.test.tsx index 79cad4a8..23b3fb3d 100644 --- a/packages/components/test/components/viewers/ImageView.test.tsx +++ b/packages/components/test/components/viewers/ImageView.test.tsx @@ -3,9 +3,10 @@ import { strict as assert } from 'assert' import React from 'react' import { describe, expect, it, vi } from 'vitest' import ImageView from '../../../src/components/viewers/ImageView.js' -import { HyperparamFileSystem } from '../../../src/lib/filesystem.js' +import { createHyperparamFileSystem } from '../../../src/lib/filesystem.js' +import { getSource } from '../../../src/lib/source.js' -const hyparamFileSystem = new HyperparamFileSystem({ endpoint: 'http://localhost:3000' }) +const hyparamFileSystem = createHyperparamFileSystem({ endpoint: 'http://localhost:3000' }) global.fetch = vi.fn() @@ -17,7 +18,7 @@ describe('ImageView Component', () => { headers: new Map([['content-length', body.byteLength]]), } as unknown as Response) - const source = hyparamFileSystem.getSource('test.png') + const source = getSource('test.png', hyparamFileSystem) assert(source?.kind === 'file') const { findByRole, findByText } = render( diff --git a/packages/components/test/components/viewers/MarkdownView.test.tsx b/packages/components/test/components/viewers/MarkdownView.test.tsx index 70455054..78dc286a 100644 --- a/packages/components/test/components/viewers/MarkdownView.test.tsx +++ b/packages/components/test/components/viewers/MarkdownView.test.tsx @@ -3,9 +3,10 @@ import { strict as assert } from 'assert' import React from 'react' import { describe, expect, it, vi } from 'vitest' import MarkdownView from '../../../src/components/viewers/MarkdownView.js' -import { HyperparamFileSystem } from '../../../src/lib/filesystem.js' +import { createHyperparamFileSystem } from '../../../src/lib/filesystem.js' +import { getSource } from '../../../src/lib/source.js' -const hyparamFileSystem = new HyperparamFileSystem({ endpoint: 'http://localhost:3000' }) +const hyparamFileSystem = createHyperparamFileSystem({ endpoint: 'http://localhost:3000' }) global.fetch = vi.fn() @@ -17,7 +18,7 @@ describe('MarkdownView Component', () => { headers: new Map([['content-length', text.length]]), } as unknown as Response) - const source = hyparamFileSystem.getSource('test.md') + const source = getSource('test.md', hyparamFileSystem) assert(source?.kind === 'file') const { findByText } = render( diff --git a/packages/components/test/lib/filesystem.test.ts b/packages/components/test/lib/filesystem.test.ts index 71b83dc6..e2404a59 100644 --- a/packages/components/test/lib/filesystem.test.ts +++ b/packages/components/test/lib/filesystem.test.ts @@ -1,16 +1,19 @@ -import { assert, describe, expect, it, test, vi } from 'vitest' -import { HttpFileSystem, HyperparamFileMetadata, HyperparamFileSystem } from '../../src/lib/filesystem.js' +import { describe, expect, it, test, vi } from 'vitest' +import { HyperparamFileMetadata, createHttpFileSystem, createHyperparamFileSystem } from '../../src/lib/filesystem.js' global.fetch = vi.fn() -describe('HyperparamFileSystem', () => { +describe('createHyperparamFileSystem', () => { + const endpoint = 'http://localhost:3000' + const fs = createHyperparamFileSystem({ endpoint }) + test.for([ 'test.txt', 'no-extension', 'folder/subfolder/test.txt', ])('recognizes a local file path', (key: string) => { - const source = new HyperparamFileSystem({ endpoint: 'http://localhost:3000' }).getSource(key) - expect(source?.kind).toBe('file') + expect(fs.canParse(key)).toBe(true) + expect(fs.getKind(key)).toBe('file') }) test.for([ @@ -18,33 +21,25 @@ describe('HyperparamFileSystem', () => { 'folder1/', 'folder1/folder2/', ])('recognizes a folder', (key: string) => { - const source = new HyperparamFileSystem({ endpoint: 'http://localhost:3000' }).getSource(key) - expect(source?.kind).toBe('directory') + expect(fs.canParse(key)).toBe(true) + expect(fs.getKind(key)).toBe('directory') }) test.for([ '/', '////', ])('does not support a heading slash', (key: string) => { - const source = new HyperparamFileSystem({ endpoint: 'http://localhost:3000' }).getSource(key) - expect(source?.kind).toBeUndefined() + expect(fs.canParse(key)).toBe(false) }) -}) -describe('HyperparamFileSystem.getResolveUrl', () => { test.for([ 'test.txt', 'folder/subfolder/test.txt', - ])('encodes the parameters', (key: string) => { - const endpoint = 'http://localhost:3000' - const source = new HyperparamFileSystem({ endpoint }).getSource(key) - assert(source?.kind === 'file') - expect(source.resolveUrl).toBe(endpoint + '/api/store/get?key=' + encodeURIComponent(key)) + ])('encodes the parameters in resolveUrl', (key: string) => { + expect(fs.getResolveUrl(key)).toBe(endpoint + '/api/store/get?key=' + encodeURIComponent(key)) }) -}) -describe('HyperparamFileSystem.listFiles', () => { - it('creates a full source by concatenating the file with the prefix', async () => { + it('in listFiles, creates a full source by concatenating the file with the prefix', async () => { const mockFiles: HyperparamFileMetadata[] = [ { key: 'folder1/', lastModified: '2023-01-01T00:00:00Z' }, { key: 'file1.txt', fileSize: 8196, lastModified: '2023-01-01T00:00:00Z' }, @@ -53,11 +48,7 @@ describe('HyperparamFileSystem.listFiles', () => { json: () => Promise.resolve(mockFiles), ok: true, } as Response) - - const endpoint = 'http://localhost:3000' - const source = new HyperparamFileSystem({ endpoint }).getSource('folder0/') - assert(source?.kind === 'directory') - const files = await source.listFiles() + const files = await fs.listFiles('folder0') expect(files).to.be.an('array').and.have.length(2) expect(files[0].source).toBe('folder0/folder1/') @@ -65,16 +56,18 @@ describe('HyperparamFileSystem.listFiles', () => { }) }) -describe('HttpFileSystem', () => { +describe('createHttpFileSystem', () => { + const fs = createHttpFileSystem() + test.for([ 'http://example.com/test.txt', 'https://example.com/test.txt', 'http://weird', ])('recognizes a URL', (key: string) => { - const source = new HttpFileSystem().getSource(key) - expect(source?.kind).toBe('file') + expect(fs.canParse(key)).toBe(true) + expect(fs.getKind(key)).toBe('file') }) it('does not support encoded URLs', () => { - expect(new HttpFileSystem().getSource('https%3A%2F%2Fhyperparam-public.s3.amazonaws.com%2Fbunnies.parquet')).toBeUndefined() + expect(fs.canParse('https%3A%2F%2Fhyperparam-public.s3.amazonaws.com%2Fbunnies.parquet')).toBe(false) }) }) From db4437478746dfa09b6a72a87b4a7a37e3acef7e Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Wed, 4 Dec 2024 11:54:55 +0100 Subject: [PATCH 3/6] disambiguate source to source: Source vs sourceId: string --- .../components/src/components/Breadcrumb.tsx | 2 +- packages/components/src/components/Folder.tsx | 2 +- .../src/components/viewers/ImageView.tsx | 2 +- .../src/components/viewers/ParquetView.tsx | 12 ++--- packages/components/src/lib/filesystem.ts | 50 +++++++++---------- packages/components/src/lib/routes.ts | 4 +- packages/components/src/lib/source.ts | 22 ++++---- .../components/test/components/File.test.tsx | 4 +- .../test/components/Folder.test.tsx | 2 +- .../components/test/lib/filesystem.test.ts | 30 +++++------ 10 files changed, 65 insertions(+), 65 deletions(-) diff --git a/packages/components/src/components/Breadcrumb.tsx b/packages/components/src/components/Breadcrumb.tsx index b3846299..ee39ec13 100644 --- a/packages/components/src/components/Breadcrumb.tsx +++ b/packages/components/src/components/Breadcrumb.tsx @@ -14,7 +14,7 @@ export default function Breadcrumb({ source, config }: BreadcrumbProps) { return diff --git a/packages/components/src/components/Folder.tsx b/packages/components/src/components/Folder.tsx index 975d64a7..0330ae69 100644 --- a/packages/components/src/components/Folder.tsx +++ b/packages/components/src/components/Folder.tsx @@ -36,7 +36,7 @@ export default function Folder({ source, config }: FolderProps) { {files && files.length > 0 &&