Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fs-require

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-require - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

8

dist/fs-require.d.ts

@@ -1,6 +0,6 @@

import { FileSystem, fsRequire } from './types';
export type { FileSystem };
import { FileSystemLike, fsRequire } from './types';
export type { FileSystemLike as FileSystem };
declare type Options = {
fs?: boolean | FileSystem;
fs?: boolean | FileSystemLike;
};
export declare const createFsRequire: (mfs: FileSystem, options?: Options | undefined) => fsRequire;
export declare const createFsRequire: (mfs: FileSystemLike, options?: Options | undefined) => fsRequire;

@@ -19,3 +19,2 @@ "use strict";

* Extensionless JS files
* Takes priority over .js file
*/

@@ -35,4 +34,18 @@ loaders[''] = function (newModule, sourceCode, makeRequire, filename, id) {

};
function resolveImplicitExtension(fs, filePath) {
for (const extension of types_1.loaderTypes) {
function resolve(fs, filePath) {
var _a, _b;
// Exact match
if (fs.existsSync(filePath)) {
if ((0, utils_1.isDirectory)(fs, filePath)) {
return (resolve(fs, path_1.default.join(filePath, 'index.js'))
|| resolve(fs, path_1.default.join(filePath, 'index.json')));
}
const extension = ((_b = (_a = filePath.match(utils_1.hasValidExtensionPattern)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : '');
return {
extension,
filePath,
};
}
// Try extensions
for (const extension of types_1.implicitExtensions) {
const filePathWithExtension = filePath + extension;

@@ -58,37 +71,33 @@ if (fs.existsSync(filePathWithExtension)) {

if (!utils_1.isFilePathPattern.test(modulePath)) {
const [moduleName, moduleSubpath] = (_a = utils_1.getBareSpecifier(modulePath)) !== null && _a !== void 0 ? _a : [];
const [moduleName, moduleSubpath] = (_a = (0, utils_1.getBareSpecifier)(modulePath)) !== null && _a !== void 0 ? _a : [];
if (moduleName === 'fs') {
const { fs } = options !== null && options !== void 0 ? options : {};
if (!fs) {
if (moduleSubpath) {
throw new Error(`Cannot find module '${modulePath}'`);
// If true, use native fs (can still be truthy)
if (fs !== true) {
const shimFs = fs || mfs;
if (!moduleSubpath) {
return shimFs;
}
return mfs;
if (moduleSubpath === '/promises' && ('promises' in shimFs)) {
return shimFs.promises;
}
throw new Error(`Cannot find module '${modulePath}'`);
}
if (fs !== true) {
return fs;
}
}
return realRequire(modulePath);
}
let filename = path_1.default.resolve(path_1.default.dirname(parentModule.filename), modulePath);
let pathExtension = (_b = filename.match(utils_1.hasExtensionPattern)) === null || _b === void 0 ? void 0 : _b[0];
if (!pathExtension) {
const resolvedPath = utils_1.isDirectory(mfs, filename)
? resolveImplicitExtension(mfs, path_1.default.join(filename, 'index'))
: resolveImplicitExtension(mfs, filename);
if (!resolvedPath) {
throw new Error(`Cannot find module '${modulePath}'`);
}
filename = resolvedPath.filePath;
pathExtension = resolvedPath.extension;
let filePath = path_1.default.resolve(path_1.default.dirname(parentModule.filename), modulePath);
const resolvedPath = resolve(mfs, filePath);
if (!resolvedPath) {
throw new Error(`Cannot find module '${modulePath}'`);
}
if (moduleCache.has(filename)) {
return moduleCache.get(filename).exports;
filePath = resolvedPath.filePath;
if (moduleCache.has(filePath)) {
return moduleCache.get(filePath).exports;
}
const newModule = new module_1.default(filename, parentModule);
newModule.filename = filename;
const sourceCode = mfs.readFileSync(filename).toString();
loaders[pathExtension](newModule, sourceCode, makeRequireFunction, filename, fsRequireId);
moduleCache.set(filename, newModule);
const newModule = new module_1.default(filePath, parentModule);
newModule.filename = filePath;
const sourceCode = mfs.readFileSync(filePath).toString();
(_b = loaders[resolvedPath.extension]) === null || _b === void 0 ? void 0 : _b.call(loaders, newModule, sourceCode, makeRequireFunction, filePath, fsRequireId);
moduleCache.set(filePath, newModule);
return newModule.exports;

@@ -95,0 +104,0 @@ };

/// <reference types="node" />
import Module from 'module';
export interface FileSystem {
export interface FileSystemLike {
readFileSync: (path: string, options?: Record<string, unknown>) => string | Buffer;

@@ -9,2 +9,3 @@ existsSync: (path: string) => boolean;

};
promises?: unknown;
}

@@ -15,2 +16,3 @@ export declare type fsRequire = {

};
export declare const implicitExtensions: readonly [".js", ".json"];
export declare const loaderTypes: readonly ["", ".js", ".json"];

@@ -17,0 +19,0 @@ export declare type Loaders = {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loaderTypes = void 0;
exports.loaderTypes = ['', '.js', '.json'];
exports.loaderTypes = exports.implicitExtensions = void 0;
exports.implicitExtensions = ['.js', '.json'];
exports.loaderTypes = ['', ...exports.implicitExtensions];

@@ -1,5 +0,5 @@

import { FileSystem } from './types';
import { FileSystemLike } from './types';
export declare const isFilePathPattern: RegExp;
export declare const hasExtensionPattern: RegExp;
export declare const isDirectory: (fs: FileSystem, directoryPath: string) => boolean;
export declare const hasValidExtensionPattern: RegExp;
export declare const isDirectory: (fs: FileSystemLike, directoryPath: string) => boolean;
export declare const getBareSpecifier: (modulePath: string) => string[] | undefined;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBareSpecifier = exports.isDirectory = exports.hasExtensionPattern = exports.isFilePathPattern = void 0;
exports.getBareSpecifier = exports.isDirectory = exports.hasValidExtensionPattern = exports.isFilePathPattern = void 0;
const types_1 = require("./types");
exports.isFilePathPattern = /^[./]/;
exports.hasExtensionPattern = /\.\w+$/;
const isDirectory = (fs, directoryPath) => (fs.existsSync(directoryPath)
&& fs.lstatSync(directoryPath).isDirectory());
exports.hasValidExtensionPattern = new RegExp(`(${types_1.implicitExtensions
.map(extension => extension.replace(/\./g, '\\$&'))
.join('|')})$`);
const isDirectory = (fs, directoryPath) => (fs.lstatSync(directoryPath).isDirectory());
exports.isDirectory = isDirectory;
const specifierPattern = /^((?:@[\da-z][\w.-]+\/)?[\da-z][\w.-]+)(\/.+)?$/;
const specifierPattern = /^(?:node:)?((?:@[\da-z][\w.-]+\/)?[\da-z][\w.-]+)(\/.+)?$/;
const getBareSpecifier = (modulePath) => { var _a; return (_a = modulePath.match(specifierPattern)) === null || _a === void 0 ? void 0 : _a.slice(1, 3); };
exports.getBareSpecifier = getBareSpecifier;
{
"name": "fs-require",
"version": "1.1.0",
"version": "1.2.0",
"description": "Create a require() function from any file-system",

@@ -40,7 +40,7 @@ "keywords": [

"devDependencies": {
"@pvtnbr/eslint-config-typescript": "^0.1.15",
"@types/jest": "^26.0.23",
"@types/node": "^15.6.0",
"@pvtnbr/eslint-config": "^0.4.1",
"@types/jest": "^26.0.24",
"@types/node": "^15.14.9",
"es-jest": "^1.2.0",
"eslint": "^7.24.0",
"eslint": "^7.32.0",
"esno": "^0.5.0",

@@ -51,7 +51,7 @@ "husky": "^4.3.8",

"memfs": "^3.2.2",
"typescript": "^4.2.4"
"typescript": "^4.4.4"
},
"eslintConfig": {
"extends": "@pvtnbr/eslint-config-typescript"
"extends": "@pvtnbr/eslint-config"
}
}

@@ -1,2 +0,2 @@

# fs-require [![Latest version](https://badgen.net/npm/v/fs-require)](https://npm.im/fs-require) <!-- [![Monthly downloads](https://badgen.net/npm/dm/fs-require)](https://npm.im/fs-require) -->[![Install size](https://packagephobia.now.sh/badge?p=fs-require)](https://packagephobia.now.sh/result?p=fs-require) [![Bundle size](https://badgen.net/bundlephobia/minzip/fs-require)](https://bundlephobia.com/result?p=fs-require)
# fs-require [![Latest version](https://badgen.net/npm/v/fs-require)](https://npm.im/fs-require) [![Monthly downloads](https://badgen.net/npm/dm/fs-require)](https://npm.im/fs-require) [![Install size](https://packagephobia.now.sh/badge?p=fs-require)](https://packagephobia.now.sh/result?p=fs-require) [![Bundle size](https://badgen.net/bundlephobia/minzip/fs-require)](https://bundlephobia.com/result?p=fs-require)

@@ -68,3 +68,8 @@ Create a `require()` function from any file-system.

## 💁‍♂️ FAQ
### Can it resolve case insensitive paths?
Case sensitivity in paths is a file-system concern so it would depend on the `fs` passed in. For example, [macOS (native fs) is case insensitive](https://discussions.apple.com/thread/251191099#:~:text=No.,have%20two%20files%20named%20File.). [memfs is case sensitive](https://github.com/streamich/memfs/issues/533).
## 👨‍👩‍👧 Related
- [fs-monkey](https://github.com/streamich/fs-monkey) - By the same author of [memfs](https://github.com/streamich/memfs). Patches the global `require` to access a virtual fs.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc