Skip to content

Commit

Permalink
feat(fs): use "url" over "path" for url locations
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Aug 23, 2023
1 parent 57b692c commit 8b072bd
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/fs-aws/src/fs.s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class FsAwsS3 implements FileSystem {
}

async *list(loc: URL, opts?: ListOptions): AsyncGenerator<URL> {
for await (const obj of this.details(loc, opts)) yield new URL(obj.path);
for await (const obj of this.details(loc, opts)) yield obj.url;
}

async *details(loc: URL, opts?: ListOptions): AsyncGenerator<FileInfo> {
Expand All @@ -105,15 +105,15 @@ export class FsAwsS3 implements FileSystem {
if (res.CommonPrefixes != null) {
for (const prefix of res.CommonPrefixes) {
if (prefix.Prefix == null) continue;
yield { path: new URL(`s3://${Bucket}/${prefix.Prefix}`), isDirectory: true };
yield { url: new URL(`s3://${Bucket}/${prefix.Prefix}/`), isDirectory: true };
}
}

if (res.Contents != null) {
for (const obj of res.Contents) {
if (obj.Key == null) continue;
yield {
path: new URL(`s3://${Bucket}/${obj.Key}`),
url: new URL(`s3://${Bucket}/${obj.Key}`),
size: obj.Size,
eTag: obj.ETag,
lastModified: obj.LastModified?.toISOString(),
Expand Down Expand Up @@ -286,7 +286,7 @@ export class FsAwsS3 implements FileSystem {
}),
);

const info: FileInfo = { size: res.ContentLength, path: loc };
const info: FileInfo = { size: res.ContentLength, url: loc };
if (res.Metadata && Object.keys(res.Metadata).length > 0) info.metadata = res.Metadata;
if (res.ContentEncoding) info.contentEncoding = res.ContentEncoding;
if (res.ContentType) info.contentType = res.ContentType;
Expand Down
2 changes: 1 addition & 1 deletion packages/fs/src/file.system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type FileWriteTypes = Buffer | Readable | string;

export interface FileInfo {
/** file path */
path: URL;
url: URL;
/**
* Size of file in bytes
* undefined if no size found
Expand Down
6 changes: 3 additions & 3 deletions packages/fs/src/systems/__test__/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('LocalFileSystem', () => {

assert.equal(files.length > 3, true);
assert.notEqual(
files.find((f) => f.path.href.endsWith('__test__/file.test.js')),
files.find((f) => f.url.href.endsWith('__test__/file.test.js')),
undefined,
);
assert.deepEqual(
Expand All @@ -111,7 +111,7 @@ describe('LocalFileSystem', () => {
it('should list recursively', async () => {
const files = await toArray(fs.details(new URL('..', import.meta.url)));
assert.notEqual(
files.find((f) => f.path.href.endsWith('__test__/file.test.js')),
files.find((f) => f.url.href.endsWith('__test__/file.test.js')),
undefined,
);
assert.deepEqual(
Expand All @@ -124,7 +124,7 @@ describe('LocalFileSystem', () => {
const files = await toArray(fs.details(new URL('..', import.meta.url), { recursive: false }));
// In a sub folder shouldn't find it
assert.equal(
files.find((f) => f.path.href.endsWith('__test__/file.test.js')),
files.find((f) => f.url.href.endsWith('__test__/file.test.js')),
undefined,
);
assert.deepEqual(files.filter((f) => f.isDirectory).length, 1);
Expand Down
2 changes: 1 addition & 1 deletion packages/fs/src/systems/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class FsFile implements FileSystem {
async head(loc: URL): Promise<(FileInfo & { isDirectory: boolean }) | null> {
try {
const stat = await fs.promises.stat(loc);
return { path: loc, size: stat.size, isDirectory: stat.isDirectory() };
return { url: loc, size: stat.size, isDirectory: stat.isDirectory() };
} catch (e) {
if (isRecord(e) && e.code === 'ENOENT') return null;
throw new FsError(`Failed to stat ${loc}`, getCode(e), loc, 'head', this, e);
Expand Down
2 changes: 1 addition & 1 deletion packages/fs/src/systems/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class FsHttp implements FileSystem {
if (!res.ok) {
throw new FsError(`Failed to head: ${loc}`, res.status, loc, 'read', this, new Error(res.statusText));
}
return { path: loc, size: Number(res.headers.get('content-length')), isDirectory: false };
return { url: loc, size: Number(res.headers.get('content-length')), isDirectory: false };
} catch (e) {
if (FsError.is(e) && e.system === this) throw e;
throw new FsError(`Failed to head: ${loc}`, 500, loc, 'read', this, e);
Expand Down
4 changes: 2 additions & 2 deletions packages/fs/src/systems/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class FsMemory implements FileSystem {
for await (const file of this.list(loc, opt)) {
const data = await this.head(file);
if (data == null) {
yield { path: file, isDirectory: true };
yield { url: file, isDirectory: true };
} else {
yield data;
}
Expand All @@ -90,7 +90,7 @@ export class FsMemory implements FileSystem {
const obj = this.files.get(loc.href);
if (obj == null) return null;
return {
path: loc,
url: loc,
size: obj.buffer.length,
metadata: obj.opts?.metadata,
contentType: obj.opts?.contentType,
Expand Down

0 comments on commit 8b072bd

Please sign in to comment.