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

feat(node-utils): introduces @edge-runtime/node-utils to run edge compliant code in Node.js environment #219

Merged
merged 26 commits into from
Dec 19, 2022

Conversation

feugy
Copy link
Member

@feugy feugy commented Nov 24, 2022

📖 What's in there?

Utility functions that we need for executing edge-related code on server side.

import { createServer } from 'node:http'
import { buildToNodeHandler } from '@edge-runtime/node-utils'

// build a transformer, using Node.js@18 globals, and a base url for URL constructor.
const transformToNode = buildToNodeHandler(global, { origin: 'http://example.com' })

const server = await createServer(transformToNode((req) => new Response(req.body)))
await server.listen()
const response = await fetch(`http://localhost:${server.address().port}`, {
  method: 'POST',
  body: 'hello world',
})
console.log(await response.text()) // is 'hello world'

🧪 How to try?

This will be used in Next.js and @vercel/node-bridge.
They are fully covered with unit tests, have a look a edge-to-node/handler.test.ts

Documentation is available here

@changeset-bot
Copy link

changeset-bot bot commented Nov 24, 2022

🦋 Changeset detected

Latest commit: cc38884

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@edge-runtime/node-utils Patch
@edge-runtime/primitives Patch
@edge-runtime/types Patch
@edge-runtime/vm Patch
@edge-runtime/jest-environment Patch
edge-runtime Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Nov 24, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
edge-runtime ✅ Ready (Inspect) Visit Preview Dec 19, 2022 at 0:03AM (UTC)

@codecov-commenter
Copy link

codecov-commenter commented Nov 25, 2022

Codecov Report

Merging #219 (1c7e52a) into main (fbf30d4) will increase coverage by 2.15%.
The diff coverage is 90.61%.

@@            Coverage Diff             @@
##             main     #219      +/-   ##
==========================================
+ Coverage   88.51%   90.67%   +2.15%     
==========================================
  Files          14       11       -3     
  Lines        1280      343     -937     
  Branches       67       34      -33     
==========================================
- Hits         1133      311     -822     
+ Misses        143       32     -111     
+ Partials        4        0       -4     
Impacted Files Coverage Δ
packages/node-utils/src/edge-to-node/stream.ts 61.84% <61.84%> (ø)
packages/node-utils/src/node-to-edge/request.ts 91.30% <91.30%> (ø)
packages/node-utils/src/node-to-edge/headers.ts 95.23% <95.23%> (ø)
packages/node-utils/src/edge-to-node/handler.ts 100.00% <100.00%> (ø)
packages/node-utils/src/edge-to-node/headers.ts 100.00% <100.00%> (ø)
packages/node-utils/src/edge-to-node/index.ts 100.00% <100.00%> (ø)
packages/node-utils/src/index.ts 100.00% <100.00%> (ø)
packages/node-utils/src/node-to-edge/index.ts 100.00% <100.00%> (ø)
packages/node-utils/src/node-to-edge/stream.ts 100.00% <100.00%> (ø)
packages/node-utils/src/types.ts 100.00% <100.00%> (ø)
... and 13 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link

@nuta nuta left a comment

Choose a reason for hiding this comment

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

Nice! Will take a look further so leaving some nitpicks for now

packages/node-utils/test/edge-to-node/headers.test.ts Outdated Show resolved Hide resolved
packages/node-utils/package.json Show resolved Hide resolved
@feugy feugy changed the title chore(node-utils): introduces @edge-runtime/node-utils to run edge compliant code in Node.js environment feat(node-utils): introduces @edge-runtime/node-utils to run edge compliant code in Node.js environment Nov 30, 2022
Copy link

@nuta nuta left a comment

Choose a reason for hiding this comment

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

LGTM! Left some nitpicks.

docs/pages/packages/node-utils.mdx Outdated Show resolved Hide resolved
packages/node-utils/src/edge-to-node/handler.ts Outdated Show resolved Hide resolved
@feugy feugy changed the base branch from main to docs December 19, 2022 11:38
@feugy feugy changed the base branch from docs to main December 19, 2022 11:38
@feugy feugy merged commit eef6b34 into main Dec 19, 2022
@github-actions github-actions bot mentioned this pull request Jan 9, 2023
@Kikobeats Kikobeats deleted the feat/edge-runtime-utils branch January 10, 2023 10:07
jridgewell pushed a commit to jridgewell/edge-runtime that referenced this pull request Jun 23, 2023
…pliant code in Node.js environment (vercel#219)

chore(node-utils): introduces edge-related utilities
chore(primitives): improves `fetch` type
chore: only considers src/ folder when running coverage with jest
chore(ponyfill): ignores types for fetch parameters
doc(node-utils): adds README and documentation page

Co-authored-by: Javi Velasco <javier.velasco86@gmail.com>
Co-authored-by: Kiko Beats <josefrancisco.verdu@gmail.com>
This was referenced Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants