Skip to content

Commit

Permalink
Remove fs-extra dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
alexparish committed Oct 19, 2024
1 parent 5555794 commit 21b4049
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 86 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-eagles-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'get-uri': patch
---

Removed `fs-extra` dependency and implemented its features using native Node.js functionality.
4 changes: 1 addition & 3 deletions packages/get-uri/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"license": "MIT",
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/fs-extra": "^8.1.2",
"@types/ftpd": "^0.2.35",
"@types/jest": "^29.5.1",
"@types/node": "^14.18.45",
Expand All @@ -51,8 +50,7 @@
"dependencies": {
"basic-ftp": "^5.0.2",
"data-uri-to-buffer": "^6.0.2",
"debug": "^4.3.4",
"fs-extra": "^11.2.0"
"debug": "^4.3.4"
},
"engines": {
"node": ">= 14"
Expand Down
15 changes: 7 additions & 8 deletions packages/get-uri/src/file.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Readable } from 'stream';
import createDebug from 'debug';
import { Stats, createReadStream } from 'fs';
import { fstat, open } from 'fs-extra';
import { Stats, createReadStream, promises as fsPromises } from 'fs';
import { GetUriProtocol } from './';
import NotFoundError from './notfound';
import NotModifiedError from './notmodified';
Expand Down Expand Up @@ -42,11 +41,12 @@ export const file: GetUriProtocol<FileOptions> = async (

// `open()` first to get a file descriptor and ensure that the file
// exists.
const fd = await open(filepath, flags, mode);
const fdHandle = await fsPromises.open(filepath, flags, mode);
// extract the numeric file descriptor
const fd = fdHandle.fd;

// Now `fstat()` to check the `mtime` and store the stat object for
// the cache.
const stat = await fstat(fd);
// store the stat object for the cache.
const stat = await fdHandle.stat();

// if a `cache` was provided, check if the file has not been modified
if (cache && cache.stat && stat && isNotModified(cache.stat, stat)) {
Expand All @@ -55,8 +55,7 @@ export const file: GetUriProtocol<FileOptions> = async (

// `fs.ReadStream` takes care of calling `fs.close()` on the
// fd after it's done reading
// @ts-expect-error `@types/node` doesn't allow `null` as file path :/
const rs = createReadStream(null, {
const rs = createReadStream(filepath, {
autoClose: true,
...opts,
fd,
Expand Down
2 changes: 1 addition & 1 deletion packages/get-uri/test/file.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path';
import { pathToFileURL } from 'url';
import { readFile } from 'fs-extra';
import { readFile } from 'fs/promises';
import { getUri } from '../src';
import { toBuffer } from './util';

Expand Down
2 changes: 1 addition & 1 deletion packages/get-uri/test/ftp.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { basename, join } from 'path';
import { FtpServer } from 'ftpd';
import { getUri } from '../src';
import { readFile } from 'fs-extra';
import { readFile } from 'fs/promises';
import { toBuffer } from './util';

describe('get-uri', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/get-uri/test/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import st from 'st';
import path from 'path';
import http from 'http';
import { listen } from 'async-listen';
import { readFile } from 'fs-extra';
import { readFile } from 'fs/promises';
import { getUri } from '../src';
import { toBuffer } from './util';

Expand Down
3 changes: 2 additions & 1 deletion packages/get-uri/test/https.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readFile, readFileSync } from 'fs-extra';
import { readFile } from 'fs/promises';
import { readFileSync } from 'fs';
// @ts-expect-error no `@types/st`
import st from 'st';
import path from 'path';
Expand Down
3 changes: 2 additions & 1 deletion packages/get-uri/test/redirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import path from 'path';
import http from 'http';
import https from 'https';
import { listen } from 'async-listen';
import { readFile, readFileSync } from 'fs-extra';
import { readFile } from 'fs/promises';
import { readFileSync } from 'fs';
import { getUri } from '../src';
import { toBuffer } from './util';

Expand Down
2 changes: 1 addition & 1 deletion packages/get-uri/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export async function toBuffer(stream: Readable): Promise<Buffer> {
for await (const chunk of stream) {
chunks.push(chunk);
}
return Buffer.concat(chunks);
return Buffer.concat(chunks.map((chunk) => new Uint8Array(chunk)));
}
Loading

0 comments on commit 21b4049

Please sign in to comment.