Skip to content

Commit

Permalink
fix: new Request has consider second argument (#411)
Browse files Browse the repository at this point in the history
* refactor: write as constant

* refactor(test): avoid async if no needed

* test: add second argument check

* fix: duplex half once

* Create large-dancers-stare.md
  • Loading branch information
Kikobeats committed Jun 15, 2023
1 parent 482f945 commit 91b4abd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
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')
})

1 comment on commit 91b4abd

@vercel
Copy link

@vercel vercel bot commented on 91b4abd Jun 15, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

edge-runtime – ./

edge-runtime.vercel.app
edge-runtime-git-main.vercel.sh
edge-runtime.vercel.sh

Please sign in to comment.