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

convert to esm #265

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
8 changes: 3 additions & 5 deletions .aegir.js
Copy link
Author

Choose a reason for hiding this comment

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

atm it fails bc of esbuild:

[15:19:06] esbuild [started]
[15:19:06] esbuild [completed]
test webworker
⠙ Setting up chromiumBEWARE: your OS is not officially supported by Playwright; downloading fallback build.
BEWARE: your OS is not officially supported by Playwright; downloading fallback build.
✔ chromium set up
✘ [ERROR] No matching export in "(disabled):src/files/glob-source.js" for import "default"

    test/files/glob-source.spec.js:4:7:
      4 │ import globSource from '../../src/files/glob-source.js'
        ╵        ~~~~~~~~~~

✖ Running tests failed.
Error: Build failed with 1 error:
test/files/glob-source.spec.js:4:7: ERROR: No matching export in "(disabled):src/files/glob-source.js" for import "default"
    at failureErrorWithLog (/home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/esbuild@0.16.10/node_modules/esbuild/lib/main.js:1596:15)
    at /home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/esbuild@0.16.10/node_modules/esbuild/lib/main.js:1052:28
    at runOnEndCallbacks (/home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/esbuild@0.16.10/node_modules/esbuild/lib/main.js:1468:61)
    at buildResponseToResult (/home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/esbuild@0.16.10/node_modules/esbuild/lib/main.js:1050:7)
    at /home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/esbuild@0.16.10/node_modules/esbuild/lib/main.js:1162:14
    at responseCallbacks.<computed> (/home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/esbuild@0.16.10/node_modules/esbuild/lib/main.js:697:9)
    at handleIncomingPacket (/home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/esbuild@0.16.10/node_modules/esbuild/lib/main.js:752:9)
    at Socket.readFromStdout (/home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/esbuild@0.16.10/node_modules/esbuild/lib/main.js:673:7)
    at Socket.emit (node:events:394:28)
    at addChunk (node:internal/streams/readable:315:12) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: [Object],
      notes: [],
      pluginName: '',
      text: 'No matching export in "(disabled):src/files/glob-source.js" for import "default"'
    }
  ],
  warnings: []
}
Command failed with exit code 1: /home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/aegir@38.1.7_eslint-plugin-n@15.6.1/node_modules/aegir/node_modules/.bin/pw-test test/**/*.spec.{js,cjs,mjs} test/browser.{js,cjs,mjs} dist/test/**/*.spec.{js,cjs,mjs} dist/test/browser.{js,cjs,mjs} --mode worker --config /home/v1rtl/Coding/forks/js-ipfs-utils/node_modules/.pnpm/aegir@38.1.7_eslint-plugin-n@15.6.1/node_modules/aegir/src/config/pw-test.js --timeout=60000 --bail
 ELIFECYCLE  Command failed with exit code 1.

Copy link
Author

@talentlessguy talentlessguy Mar 20, 2023

Choose a reason for hiding this comment

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

The reason might be this: evanw/esbuild#2780 or this: hugomrdias/playwright-test#530

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

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

/** @type {import('aegir').Options["build"]["config"]} */
const esbuild = {
Expand All @@ -18,7 +16,7 @@ const esbuild = {
}

/** @type {import('aegir').PartialOptions} */
module.exports = {
export default {
build: {
config: esbuild
},
Expand Down
23 changes: 17 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
"url": "https://github.com/ipfs/js-ipfs-utils/issues"
},
"engines": {
"node": ">=16.0.0",
"node": ">=16.8.0",
Copy link
Author

Choose a reason for hiding this comment

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

undici which is a dependency of native-fetch requires node.js 16.8+ to work: https://undici.nodejs.org/#/?id=undicifetchinput-init-promise

"npm": ">=7.0.0"
},
"main": "src/index.js",
"type": "module",
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"types": "dist/src/index.d.ts",
"typesVersions": {
"*": {
Expand All @@ -39,7 +45,11 @@
"eslintConfig": {
"extends": "ipfs",
"env": {
"worker": true
"worker": true,
"es6": true
},
"parserOptions": {
"sourceType": "module"
}
},
"release": {
Expand Down Expand Up @@ -155,13 +165,14 @@
"it-to-stream": "^1.0.0",
"merge-options": "^3.0.4",
"nanoid": "^3.1.20",
"native-fetch": "^3.0.0",
"native-fetch": "^4.0.2",
"node-fetch": "^2.6.8",
"react-native-fetch-api": "^3.0.0",
"stream-to-it": "^0.2.2"
"stream-to-it": "^0.2.2",
"undici": "^5.21.0"
},
"devDependencies": {
"aegir": "^36.1.1",
"aegir": "^38.1.7",
Copy link
Author

Choose a reason for hiding this comment

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

to support es module in testing

"delay": "^5.0.0",
"events": "^3.3.0",
"ipfs-unixfs": "^6.0.4",
Expand Down
1 change: 0 additions & 1 deletion rn-test.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict'

module.exports = {
require: require.resolve('./rn-test.require.js'),
Expand Down
1 change: 0 additions & 1 deletion rn-test.require.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict'

const { polyfill: polyfillReadableStream } = require('react-native-polyfill-globals/src/readable-stream')
const { polyfill: polyfillURL } = require('react-native-polyfill-globals/src/url')
Expand Down
41 changes: 17 additions & 24 deletions src/env.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
'use strict'
const isElectron = require('is-electron')

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
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
// @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
}

export default {
isTest, isElectron, isElectronMain, isElectronRenderer, isNode, isBrowser, isWebWorker, isEnvWithDom, isReactNative
}
5 changes: 3 additions & 2 deletions src/fetch.browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict'

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

import fetch from 'native-fetch'

// use window.fetch if it is available, fall back to node-fetch if not
module.exports = require('native-fetch')
export default fetch
14 changes: 9 additions & 5 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
'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'

if (isElectronMain) {
impl = 'electron-fetch'
}

module.exports = require(impl)
const fetchImpl = await import(impl)
Copy link
Author

Choose a reason for hiding this comment

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

replace dynamic require with dynamic import-s (top level await is available since node v14.18, dynamic import is supported by v13.2)


export const fetch = fetchImpl.fetch
export const Request = fetchImpl.Request
export const Response = fetchImpl.Response
export const Headers = fetchImpl.Headers

export default fetch
11 changes: 3 additions & 8 deletions src/fetch.rn.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-nocheck
'use strict'
// @ts-ignore
const { Headers, Request, Response, fetch } = require('react-native-fetch-api')
import { Headers, Request, Response, fetch } from 'react-native-fetch-api'

/** @type {import('electron-fetch').default} */
const rnFetch = fetch
Expand All @@ -11,8 +9,5 @@ const rnHeaders = Headers
const rnRequest = Request
/** @type {import('electron-fetch').Response} */
const rnResponse = Response
module.exports = rnFetch
module.exports.Headers = rnHeaders
module.exports.Request = rnRequest
module.exports.Response = rnResponse
module.exports.default = rnFetch
export default rnFetch
export { rnHeaders as Headers, rnRequest as Request, rnResponse as Response }
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 fsp from 'fs/promises'
import fs from 'fs'
import glob from 'it-glob'
import Path from 'path'
import errCode from 'err-code'

/**
* 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
29 changes: 18 additions & 11 deletions src/http.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
/**
* DO NOT EDIT THIS FILE DIRECTLY.
* This file is generated following the conversion of
*
* @see [./src/http.js]{@link ./src/http.js}
*
**/
import { fetch, Request, Headers } from './http/fetch.js'
import { TimeoutError, HTTPError } from './http/error.js'
import _toesmTemp1 from 'merge-options'
import { URL, URLSearchParams } from 'iso-url'
import anySignal from 'any-signal'
import browserReableStreamToIt from 'browser-readablestream-to-it'
import { isBrowser, isWebWorker } from './env.js'
import all from 'it-all'
/* 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 = _toesmTemp1.bind({ ignoreUndefined: true })

/**
* @typedef {import('stream').Readable} NodeReadableStream
Expand Down Expand Up @@ -94,7 +101,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 +379,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
16 changes: 6 additions & 10 deletions src/http/fetch.browser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict'

const { TimeoutError, AbortError } = require('./error')
// @ts-expect-error
const { Response, Request, Headers, default: fetch } = require('../fetch')
import { TimeoutError, AbortError } from './error.js'
import fetch, { Response, Request, Headers } from '../fetch.js'

/**
* @typedef {import('../types').FetchOptions} FetchOptions
Expand Down Expand Up @@ -102,9 +99,8 @@ const fetchWithStreaming = fetch
* @param {FetchOptions} options
*/
const fetchWith = (url, options = {}) =>
(options.onUploadProgress != null)
? fetchWithProgress(url, options)
: fetchWithStreaming(url, options)
// @ts-ignore native-fetch uses different types for Request and RequestInit
options.onUploadProgress != null ? fetchWithProgress(url, options) : fetchWithStreaming(url, options)

/**
* Parse Headers from a XMLHttpRequest
Expand Down Expand Up @@ -136,8 +132,8 @@ class ResponseWithURL extends Response {
}
}

module.exports = {
fetch: fetchWith,
export {
fetchWith as fetch,
Request,
Headers
}
15 changes: 9 additions & 6 deletions src/http/fetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* @typedef {object} fetchImpl
* @property {globalThis.fetch} fetchImpl.fetch
Expand All @@ -8,15 +6,20 @@
* @property {globalThis.Headers} fetchImpl.Headers
*/

let implName = './fetch.node'
let implName = './fetch.node.js'

if (typeof XMLHttpRequest === 'function') {
// Electron has `XMLHttpRequest` and should get the browser implementation
// instead of node.
implName = './fetch.browser'
implName = './fetch.browser.js'
}

/** @type {fetchImpl} */
const fetch = require(implName)
const fetchImpl = await import(implName).then(m => m.default)

export const fetch = fetchImpl.fetch
export const Request = fetchImpl.Request
export const Response = fetchImpl.Response
export const Headers = fetchImpl.Headers

module.exports = fetch
export default fetch
Loading