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

esm-utils

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esm-utils - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0

58

index.d.ts

@@ -10,27 +10,33 @@ // From https://github.com/sindresorhus/type-fest

export interface JsonUtils {
/**
Read and parse a JSON file.
/**
Read and parse a JSON file.
@example
```
import createEsmUtils from 'esm-utils'
const {json} = createEsmUtils(import.meta)
@example
```
import createEsmUtils from 'esm-utils'
const {readJson} = createEsmUtils(import.meta)
const data = await json.load('foo.json')
```
*/
load(file: string | URL): Promise<JsonValue>
/**
Read and parse a JSON file.
const data = await readJson('./foo.json')
```
*/
export type readJson = (file: string | URL) => Promise<JsonValue>
@example
```
import createEsmUtils from 'esm-utils'
const {json} = createEsmUtils(import.meta)
/**
Read and parse a JSON file.
const data = json.loadSync('foo.json')
```
*/
loadSync(file: string | URL): JsonValue
@example
```
import createEsmUtils from 'esm-utils'
const {readJsonSync} = createEsmUtils(import.meta)
const data = json.loadSync('foo.json')
```
*/
export type readJsonSync = (file: string | URL) => JsonValue
export interface JsonUtils {
read: readJson
load: readJson
readSync: readJsonSync
loadSync: readJsonSync
}

@@ -51,3 +57,3 @@

*/
type importFile = (file: string | URL) => Promise<unknown>
export type importFile = (file: string | URL) => Promise<unknown>

@@ -70,7 +76,13 @@ /**

readonly require: NodeRequire
readonly json: JsonUtils
readonly importFile: importFile
readonly readJson: readJson
readonly readJsonSync: readJsonSync
// Alias
readonly __filename: string
readonly __dirname: string
readonly import: importFile
readonly loadJson: readJson
readonly loadJsonSync: readJsonSync
readonly json: JsonUtils
}
import fs, {promises as fsAsync} from 'node:fs'
function create(base) {
return {
async load(file) {
const url = new URL(file, base)
const buffer = await fsAsync.readFile(url)
return JSON.parse(buffer)
},
loadSync(file) {
const url = new URL(file, base)
const buffer = fs.readFileSync(url)
return JSON.parse(buffer)
},
function createReadJson(base) {
return async function (file) {
const url = new URL(file, base)
const buffer = await fsAsync.readFile(url)
return JSON.parse(buffer)
}
}
export default create
function createReadJsonSync(base) {
return function (file) {
const url = new URL(file, base)
const buffer = fs.readFileSync(url)
return JSON.parse(buffer)
}
}
export {createReadJson, createReadJsonSync}
import {fileURLToPath} from 'node:url'
import path from 'node:path'
import {createRequire} from 'node:module'
import createJsonUtils from './json.js'
import {createReadJson, createReadJsonSync} from './json.js'
import createImport from './import.js'
function create(importMeta) {
const toDescriptor = (getter) => {
let value
return {
get filename() {
return fileURLToPath(importMeta.url)
get() {
if (!value) {
value = getter()
}
return value
},
get dirname() {
return path.dirname(this.filename)
},
get require() {
return createRequire(importMeta.url)
},
get json() {
return createJsonUtils(importMeta.url)
},
get importFile() {
return createImport(importMeta.url)
},
// Aliases
get __filename() {
return this.filename
},
get __dirname() {
return this.dirname
},
get import() {
return this.importFile
},
enumerable: true,
}
}
export default create
const createObject = (properties) => Object.create(null, properties)
function createEsmUtils({url: importMetaUrl}) {
const utils = createObject({
filename: toDescriptor(() => fileURLToPath(importMetaUrl)),
dirname: toDescriptor(() => path.dirname(utils.filename)),
require: toDescriptor(() => createRequire(importMetaUrl)),
importFile: toDescriptor(() => createImport(importMetaUrl)),
readJson: toDescriptor(() => createReadJson(importMetaUrl)),
readJsonSync: toDescriptor(() => createReadJsonSync(importMetaUrl)),
// Aliases
__filename: toDescriptor(() => utils.filename),
__dirname: toDescriptor(() => utils.dirname),
import: toDescriptor(() => utils.importFile),
loadJson: toDescriptor(() => utils.readJson),
loadJsonSync: toDescriptor(() => utils.readJsonSync),
json: toDescriptor(() =>
createObject({
read: toDescriptor(() => utils.readJson),
load: toDescriptor(() => utils.readJson),
readSync: toDescriptor(() => utils.readJsonSync),
loadSync: toDescriptor(() => utils.readJsonSync),
}),
),
})
return utils
}
export default createEsmUtils
{
"name": "esm-utils",
"version": "2.1.0",
"version": "2.2.0",
"description": "Utilities you'll need when migrating to ESModule.",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/fisker/esm-utils#readme",

@@ -35,3 +35,4 @@ # esm-utils

filename,
json,
readJson,
readJsonSync,
importFile,

@@ -50,15 +51,22 @@ } = createEsmUtils(import.meta)

- `filename` (alias `__filename`)
- `readJson` (alias `loadJson`)
- `readJsonSync` (alias `loadJsonSync`)
- `importFile` (alias `import`)
- `json`
- `importFile` (alias `import`)
**Please read [this note](#you-dont-need-dirname-and-filename) before you use `dirname` and `filename`**
### `json.load(string | URL)`
### `readJson(string | URL)`
Returns `Promise<jsonObject>`.
### `json.loadSync(string | URL)`
### `readJsonSync(string | URL)`
Sync version of `json.load`.
Sync version of `readJson`.
### `json`
- `json.{read,load}` alias of `readJson`
- `json.{read,load}Sync` alias of `readJsonSync`
### `importFile(string | URL)`

@@ -91,3 +99,3 @@

With `json.load` or `json.loadSync`
With `readJson` or `readJsonSync`

@@ -97,4 +105,4 @@ ```js

const {json} = createEsmUtils(import.meta)
const foo = await json.load('./foo.json')
const {readJson} = createEsmUtils(import.meta)
const foo = await readJson('./foo.json')
```

@@ -105,4 +113,4 @@

const {json} = createEsmUtils(import.meta)
const foo = json.loadSync('./foo.json')
const {readJsonSync} = createEsmUtils(import.meta)
const foo = readJsonSync('./foo.json')
```

@@ -109,0 +117,0 @@

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