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

remove undici #223

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion lib/bacalhau.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import timers from 'node:timers/promises'
import { execa } from 'execa'
import { fetch } from 'undici'
import Sentry from '@sentry/node'
import { installBinaryModule, getBinaryModuleExecutable } from './modules.js'
import os from 'node:os'
Expand Down
8 changes: 4 additions & 4 deletions lib/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import os from 'node:os'
import assert from 'node:assert'
import { join } from 'node:path'
import { mkdir, chmod } from 'node:fs/promises'
import { fetch } from 'undici'
import { pipeline } from 'node:stream/promises'
import gunzip from 'gunzip-maybe'
import tar from 'tar-fs'
import unzip from 'unzip-stream'
import { createWriteStream } from 'node:fs'
import { moduleBinaries } from './paths.js'
import { streamAsyncIterable } from './util.js'

/** @typedef {import('unzip-stream').UnzipStreamEntry} UnzipStreamEntry */

Expand Down Expand Up @@ -74,7 +74,7 @@ export const installBinaryModule = async ({
}

if (target.asset.endsWith('tar.gz')) {
await pipeline(res.body, gunzip(), tar.extract(outFile))
await pipeline(streamAsyncIterable(res.body), gunzip(), tar.extract(outFile))
Copy link
Member Author

Choose a reason for hiding this comment

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

I feel like I'm doing something wrong here. However, MDN suggests this approach. I didn't find anything in node's stdlib for this 🤔

} else {
await mkdir(join(moduleBinaries, module), { recursive: true })
const parser = unzip.Parse()
Expand All @@ -86,7 +86,7 @@ export const installBinaryModule = async ({
await chmod(outPath, 0o755)
}
})
await pipeline(res.body, parser)
await pipeline(streamAsyncIterable(res.body), parser)
}
console.log(`[${module}] ✓ ${outFile}`)
}
Expand Down Expand Up @@ -119,6 +119,6 @@ export async function downloadSourceFiles ({ module, repo, distTag }) {
}

// `{ strip: 1}` tells tar to remove the top-level directory (e.g. `mod-peer-checker-v1.0.0`)
await pipeline(res.body, gunzip(), tar.extract(outDir, { strip: 1 }))
await pipeline(streamAsyncIterable(res.body), gunzip(), tar.extract(outDir, { strip: 1 }))
console.log(`[${module}] ✓ ${outDir}`)
}
13 changes: 13 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of
export async function * streamAsyncIterable (stream) {
const reader = stream.getReader()
try {
while (true) {
const { done, value } = await reader.read()
if (done) return
yield value
}
} finally {
reader.releaseLock()
}
}
52 changes: 0 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"gunzip-maybe": "^1.4.2",
"p-retry": "^6.0.0",
"tar-fs": "^3.0.3",
"undici": "^5.20.0",
"unzip-stream": "^0.3.1",
"yargs": "^17.7.1"
},
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"allowJs": true,
"checkJs": true,
"skipLibCheck": true,
"lib": ["es2021"],
"lib": ["es2021", "DOM"],
Copy link
Member Author

Choose a reason for hiding this comment

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

is there a better lib target to add? See https://www.typescriptlang.org/tsconfig#lib. DOM was suggested in stack overflow posts

Copy link
Member

Choose a reason for hiding this comment

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

Oh man, this is a rabbit hole. See the discussion in DefinitelyTyped/DefinitelyTyped#60924.

First of all, Station Core is currently supporting Node.js 16.x which does not provide a built-in Fetch API. So the first step is to upgrade our minimal supported version to 18.x - I think this is a breaking change.

Then, ideally, all we need to do is add @types/node@18 to our dev-dependencies.

However, that does not work because of the reasons described in DefinitelyTyped/DefinitelyTyped#60924.

Also, we should not be using DOM types, because Fetch API on server has slightly different API than in the browser. (This was mentioned in that DefinitelyTyped discussion too.)

I quite like the workaround described DefinitelyTyped/DefinitelyTyped#60924 (comment). The downside is that we still need to have undici in our dependencies (and probably keep it up to date via Dependabot?), but at least it becomes a dev-time only dependency. 🤷🏻‍♂️

// fetch.d.ts
import type * as undici from "undici";

declare global {
  // Re-export undici fetch function and various classes to global scope.
  // These are classes and functions expected to be at global scope according to
  // Node.js v18 API documentation.
  // See: https://nodejs.org/dist/latest-v18.x/docs/api/globals.html
  export const { 
    FormData,
    Headers,
    Request,
    Response,
    fetch,
  }: typeof undici;
}

Copy link
Member

Choose a reason for hiding this comment

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

Here is a diff of the changes I think we should make on our way to fix this properly. Unfortunately, it's not enough.

diff --git a/package.json b/package.json
index edc5522..abe92f3 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,7 @@
     "station": "./bin/station.js"
   },
   "engines": {
-    "node": ">=16"
+    "node": ">=18"
   },
   "scripts": {
     "build": "tsc -p .",
@@ -32,6 +32,7 @@
     ]
   },
   "devDependencies": {
+    "@types/node": "^18.18.1",
     "@types/yargs": "^17.0.24",
     "get-stream": "^8.0.1",
     "mocha": "^10.2.0",
diff --git a/tsconfig.json b/tsconfig.json
index 5d96c50..83db0fb 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -3,7 +3,7 @@
     "allowJs": true,
     "checkJs": true,
     "skipLibCheck": true,
-    "lib": ["es2021", "DOM"],
+    "lib": ["es2021"],
     "target": "es2021",
     "module": "Node16",
     "moduleResolution": "node16",

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for doing the research! I've applied the fixes you suggested. I don't think overall this improves our situation unfortunately, and I'd rather propose to continue using undici for now

Copy link
Member

Choose a reason for hiding this comment

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

I don't think overall this improves our situation unfortunately, and I'd rather propose to continue using undici for now

💯 agree

"target": "es2021",
"module": "Node16",
"moduleResolution": "node16",
Expand Down
Loading