Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[get-uri] Remove fs-extra dependency #341

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
alexparish marked this conversation as resolved.
Show resolved Hide resolved
"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, {
TooTallNate marked this conversation as resolved.
Show resolved Hide resolved
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
Loading