Skip to content

convert to esm #265

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

Closed
wants to merge 18 commits into from
Closed
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
16 changes: 11 additions & 5 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
'use strict';
import EchoServer from 'aegir/echo-server'
import { format } from 'iso-url'
import { createRequire } from 'module'

const EchoServer = require('aegir/utils/echo-server')
const { format } =require('iso-url')
const require = createRequire(import.meta.url)

/** @type {import('aegir').Options["build"]["config"]} */
const esbuild = {
format: 'esm',
inject: ['./buffer-shim.js'],
plugins: [
{
name: 'node built ins',
setup (build) {
build.onResolve({ filter: /^stream$/ }, () => {
return { path: require.resolve('readable-stream') }
return { path: require.resolve('readable-stream') }
})
build.onResolve({filter: /^url$/}, () => {
return { path: require.resolve('url/') }
})
}
}
]
}

/** @type {import('aegir').PartialOptions} */
module.exports = {
export default {
build: {
config: esbuild
},
Expand Down
10 changes: 10 additions & 0 deletions .coverage/coverage-final.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .coverage/coverage-pw.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package-lock.json
yarn.lock
pnpm-lock.yaml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the npm package-lock.json description, the package manager manifest is meant to be included in source control.

It's how someone else building the project can guarantee they get the same versions of libraries that were used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware of that, though there were other lockfilels in gitignore so I decided not to touch it. Also because i'm using pnpm and someone else would use something else.

# Logs
logs
*.log
Expand Down
1 change: 1 addition & 0 deletions buffer-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Buffer } from 'buffer'
44 changes: 24 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
"url": "https://github.com/ipfs/js-ipfs-utils/issues"
},
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
"node": ">=20"
},
"type": "module",
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"typesVersions": {
"*": {
Expand All @@ -39,7 +44,11 @@
"eslintConfig": {
"extends": "ipfs",
"env": {
"worker": true
"worker": true,
"es6": true
},
"parserOptions": {
"sourceType": "module"
}
},
"release": {
Expand Down Expand Up @@ -143,34 +152,32 @@
"docs": "aegir docs"
},
"dependencies": {
"any-signal": "^3.0.0",
"browser-readablestream-to-it": "^1.0.0",
"any-signal": "^4.1.1",
"browser-readablestream-to-it": "^2.0.7",
"buffer": "^6.0.1",
"electron-fetch": "^1.7.2",
"electron-fetch": "^1.9.1",
"err-code": "^3.0.1",
"is-electron": "^2.2.0",
"iso-url": "^1.1.5",
"it-all": "^1.0.4",
"it-all": "^3.0.6",
"it-glob": "^1.0.1",
"it-to-stream": "^1.0.0",
"merge-options": "^3.0.4",
"nanoid": "^3.1.20",
"native-fetch": "^3.0.0",
"node-fetch": "^2.6.8",
"react-native-fetch-api": "^3.0.0",
"stream-to-it": "^0.2.2"
"nanoid": "^5.1.0"
},
"devDependencies": {
"aegir": "^36.1.1",
"delay": "^5.0.0",
"@types/mocha": "^10.0.10",
"aegir": "^45.0.9",
"delay": "^6.0.0",
"events": "^3.3.0",
"ipfs-unixfs": "^6.0.4",
"it-drain": "^1.0.3",
"it-last": "^1.0.4",
"it-to-buffer": "^2.0.0",
"mocha": "^11.1.0",
"react-native-polyfill-globals": "^3.0.0",
"readable-stream": "^4.3.0",
"uint8arrays": "^3.0.0",
"url": "^0.11.0",
"util": "^0.12.3"
},
"browser": {
Expand All @@ -184,8 +191,5 @@
"node-fetch": false,
"fs": false
},
"react-native": {
"./src/fetch.js": "./src/fetch.rn.js",
"./src/http/fetch.js": "./src/http/fetch.rn.js"
}
"packageManager": "pnpm@10.1.0+sha512.c89847b0667ddab50396bbbd008a2a43cf3b581efd59cf5d9aa8923ea1fb4b8106c041d540d08acb095037594d73ebc51e1ec89ee40c88b30b8a66c0fae0ac1b"
}
2 changes: 0 additions & 2 deletions rn-test.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

module.exports = {
require: require.resolve('./rn-test.require.js'),
runner: 'mocha',
Expand Down
4 changes: 1 addition & 3 deletions rn-test.require.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const { polyfill: polyfillEncoding } = require('react-native-polyfill-globals/src/encoding')
const { polyfill: polyfillReadableStream } = require('react-native-polyfill-globals/src/readable-stream')
const { polyfill: polyfillURL } = require('react-native-polyfill-globals/src/url')
const { polyfill: polyfillEncoding } = require('react-native-polyfill-globals/src/encoding')

polyfillURL()
polyfillReadableStream()
Expand Down
38 changes: 14 additions & 24 deletions src/env.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
'use strict'
const isElectron = require('is-electron')
import isElectronFn from 'is-electron'
const isEnvWithDom = typeof window === 'object' && typeof document === 'object' && document.nodeType === 9
const isElectron = isElectronFn()
const isBrowser = isEnvWithDom && !isElectron
const isElectronMain = isElectron && !isEnvWithDom
const isElectronRenderer = isElectron && isEnvWithDom
const isNode = typeof process !== 'undefined' && typeof process.release !== 'undefined' && process.release.name === 'node' && !isElectron

const IS_ENV_WITH_DOM = typeof window === 'object' && typeof document === 'object' && document.nodeType === 9
// @ts-ignore
const IS_ELECTRON = isElectron()
const IS_BROWSER = IS_ENV_WITH_DOM && !IS_ELECTRON
const IS_ELECTRON_MAIN = IS_ELECTRON && !IS_ENV_WITH_DOM
const IS_ELECTRON_RENDERER = IS_ELECTRON && IS_ENV_WITH_DOM
const IS_NODE = typeof require === 'function' && typeof process !== 'undefined' && typeof process.release !== 'undefined' && process.release.name === 'node' && !IS_ELECTRON
// @ts-ignore - we either ignore worker scope or dom scope
const IS_WEBWORKER = typeof importScripts === 'function' && typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope
const IS_TEST = typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.NODE_ENV === 'test'
const IS_REACT_NATIVE = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'

module.exports = {
isTest: IS_TEST,
isElectron: IS_ELECTRON,
isElectronMain: IS_ELECTRON_MAIN,
isElectronRenderer: IS_ELECTRON_RENDERER,
isNode: IS_NODE,
const isWebWorker = typeof importScripts === 'function' && typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope
const isTest = typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.NODE_ENV === 'test'
const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
export {
isTest, isElectron, isElectronMain, isElectronRenderer, isNode,
/**
* Detects browser main thread **NOT** web worker or service worker
*/
isBrowser: IS_BROWSER,
isWebWorker: IS_WEBWORKER,
isEnvWithDom: IS_ENV_WITH_DOM,
isReactNative: IS_REACT_NATIVE
isBrowser, isWebWorker, isEnvWithDom, isReactNative
}

12 changes: 3 additions & 9 deletions src/fetch.browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
'use strict'
const { fetch, Request, Response, Headers } = globalThis

/**
* @typedef {globalThis.Headers} Headers
* @typedef {globalThis.Request} Request
* @typedef {globalThis.Response} Response
*/

// use window.fetch if it is available, fall back to node-fetch if not
module.exports = require('native-fetch')
export default fetch
export { Request, Response, Headers }
25 changes: 16 additions & 9 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
'use strict'

/**
* @typedef {globalThis.Headers} Headers
* @typedef {globalThis.Request} Request
* @typedef {globalThis.Response} Response
*/

const { isElectronMain } = require('./env')

import { isElectronMain } from './env.js'
// use window.fetch if it is available, fall back to node-fetch if not
let impl = 'native-fetch'
const fetchImpl = isElectronMain
? await import('electron-fetch')
: {
default: globalThis.fetch,
Request: globalThis.Request,
Response: globalThis.Response,
Headers: globalThis.Headers
}

if (isElectronMain) {
impl = 'electron-fetch'
}
export const fetch = fetchImpl.default
export const Request = fetchImpl.Request
/** @type {typeof globalThis.Response} */
// @ts-expect-error
export const Response = fetchImpl.Response
export const Headers = fetchImpl.Headers

module.exports = require(impl)
export default fetch
18 changes: 0 additions & 18 deletions src/fetch.rn.js

This file was deleted.

14 changes: 6 additions & 8 deletions src/files/glob-source.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'

const fsp = require('fs').promises
const fs = require('fs')
const glob = require('it-glob')
const Path = require('path')
const errCode = require('err-code')
import fs from 'node:fs'
import fsp from 'node:fs/promises'
import Path from 'node:path'
import errCode from 'err-code'
import glob from 'it-glob'

/**
* Create an async iterator that yields paths that match requested glob pattern
Expand All @@ -14,7 +12,7 @@ const errCode = require('err-code')
* @param {import('../types').GlobSourceOptions} [options] - Optional options
* @returns {AsyncGenerator<import('../types').GlobSourceResult, void, unknown>} File objects that match glob
*/
module.exports = async function * globSource (cwd, pattern, options) {
export default async function * globSource (cwd, pattern, options) {
options = options || {}

if (typeof pattern !== 'string') {
Expand Down
6 changes: 2 additions & 4 deletions src/files/url-source.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const HTTP = require('../http')
import HTTP from '../http.js'

/**
*
Expand Down Expand Up @@ -28,4 +26,4 @@ async function * readURLContent (url, options) {
yield * response.iterator()
}

module.exports = urlSource
export default urlSource
28 changes: 17 additions & 11 deletions src/http.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/**
* DO NOT EDIT THIS FILE DIRECTLY.
* This file is generated following the conversion of
*
* @see [./src/http.js]{@link ./src/http.js}
**/
import { anySignal } from 'any-signal'
import browserReableStreamToIt from 'browser-readablestream-to-it'
import { URL, URLSearchParams } from 'iso-url'
import all from 'it-all'
import mergeOptions from 'merge-options'
import { isBrowser, isWebWorker } from './env.js'
import { TimeoutError, HTTPError } from './http/error.js'
import { fetch, Request, Headers } from './http/fetch.js'
/* eslint-disable no-undef */
'use strict'

const { fetch, Request, Headers } = require('./http/fetch')
const { TimeoutError, HTTPError } = require('./http/error')
const merge = require('merge-options').bind({ ignoreUndefined: true })
const { URL, URLSearchParams } = require('iso-url')
const anySignal = require('any-signal')
const browserReableStreamToIt = require('browser-readablestream-to-it')
const { isBrowser, isWebWorker } = require('./env')
const all = require('it-all')
const merge = mergeOptions.bind({ ignoreUndefined: true })

/**
* @typedef {import('stream').Readable} NodeReadableStream
Expand Down Expand Up @@ -94,7 +100,7 @@ class HTTP {
async fetch (resource, options = {}) {
/** @type {HTTPOptions} */
const opts = merge(this.opts, options)
// @ts-expect-error
// @ts-ignore
const headers = new Headers(opts.headers)

// validate resource type
Expand Down Expand Up @@ -372,4 +378,4 @@ HTTP.delete = (resource, options) => new HTTP(options).delete(resource, options)
*/
HTTP.options = (resource, options) => new HTTP(options).options(resource, options)

module.exports = HTTP
export default HTTP
11 changes: 3 additions & 8 deletions src/http/error.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
'use strict'

class TimeoutError extends Error {
export class TimeoutError extends Error {
constructor (message = 'Request timed out') {
super(message)
this.name = 'TimeoutError'
}
}
exports.TimeoutError = TimeoutError

class AbortError extends Error {
export class AbortError extends Error {
constructor (message = 'The operation was aborted.') {
super(message)
this.name = 'AbortError'
}
}
exports.AbortError = AbortError

class HTTPError extends Error {
export class HTTPError extends Error {
/**
* @param {Response} response
*/
Expand All @@ -26,4 +22,3 @@ class HTTPError extends Error {
this.response = response
}
}
exports.HTTPError = HTTPError
Loading