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

fix: new Request has consider second argument #411

Merged
merged 5 commits into from Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/large-dancers-stare.md
@@ -0,0 +1,7 @@
---
"@edge-runtime/primitives": patch
"edge-runtime": patch
"@edge-runtime/vm": patch
---

fix: new Request has consider second argument
8 changes: 4 additions & 4 deletions packages/primitives/src/primitives/fetch.js
Expand Up @@ -146,10 +146,10 @@ export function setGlobalDispatcher(agent) {
* Add `duplex: 'half'` by default to all requests
*/
function addDuplexToInit(init) {
if (typeof init === 'undefined' || typeof init === 'object') {
return { duplex: 'half', ...init }
}
return init
return typeof init === 'undefined' ||
(typeof init === 'object' && init.duplex === undefined)
? { duplex: 'half', ...init }
: init
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime/src/cli/repl.ts
Expand Up @@ -5,6 +5,8 @@ import { join } from 'path'

import { EdgeRuntime } from '../edge-runtime'

const [NODE_MAJOR] = process.versions.node.split('.').map((v) => Number(v))

const format = createFormat()

const writer: createRepl.REPLWriter = (output) => {
Expand Down Expand Up @@ -33,8 +35,7 @@ Object.defineProperty(repl.context, 'EdgeRuntime', {
value: runtime.context.EdgeRuntime,
})

const nodeMajorVersion = parseInt(process.versions.node.split('.')[0])
if (nodeMajorVersion < 16) {
if (NODE_MAJOR < 16) {
repl.context.util = {
inspect: (...args: any[]) => {
const stack = new Error().stack ?? ''
Expand Down
5 changes: 3 additions & 2 deletions packages/vm/tests/integration/crypto.test.ts
@@ -1,6 +1,8 @@
import { EdgeVM } from '../../src'
import { createHash } from 'crypto'

const [NODE_MAJOR] = process.versions.node.split('.').map((v) => Number(v))

test('crypto.subtle.digest returns an ArrayBuffer', async () => {
const vm = new EdgeVM()

Expand Down Expand Up @@ -59,8 +61,7 @@ test('crypto.generateKey works with a Uint8Array from the VM', async () => {
await vm.evaluate(`(${fn})()`)
})

const nodeMajorVersion = parseInt(process.versions.node.split('.')[0])
if (nodeMajorVersion >= 16) {
if (NODE_MAJOR >= 16) {
test('Ed25519', async () => {
const vm = new EdgeVM()

Expand Down
19 changes: 14 additions & 5 deletions packages/vm/tests/integration/request.test.ts
@@ -1,34 +1,43 @@
/**
* @jest-environment ../jest-environment/dist
*/
test('evaluate promise', async () => {
test('evaluate promise', () => {
const url = 'https://vercel.com/foo/bar?one=value'
const req = new Request(url)
expect(req.url).toEqual(url)
})

test('parses and reconstructs the URL alone', async () => {
test('parses and reconstructs the URL alone', () => {
const url = 'https://vercel.com/foo/bar?one=value'
const req = new Request(url)
expect(req.url).toEqual(url)
})

test('throws when the URL is malformed', async () => {
test('throws when the URL is malformed', () => {
try {
void new Request('meeeh')
} catch (error: any) {
expect(error.message).toEqual('Failed to parse URL from meeeh')
}
})

test('Request.referrer is `about:client` by default', async () => {
test('Request.referrer is `about:client` by default', () => {
const request = new Request('https://example.vercel.sh')
expect(request.referrer).toEqual('about:client')
})

test('Request.referrer can be customized', async () => {
test('Request.referrer can be customized', () => {
const request = new Request('https://example.vercel.sh', {
referrer: 'https://vercel.com/home',
})
expect(request.referrer).toEqual('https://vercel.com/home')
})

test('create a Request instance using second argument', () => {
expect(
new Request(
'https://example.vercel.sh',
new Request('https://example.vercel.sh', { method: 'POST' })
).method
).toBe('POST')
})