From 91b4abdd5623c1e26fc990936f649af3f9fb8b1a Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Thu, 15 Jun 2023 20:00:59 +0200 Subject: [PATCH] fix: new Request has consider second argument (#411) * 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 --- .changeset/large-dancers-stare.md | 7 +++++++ packages/primitives/src/primitives/fetch.js | 8 ++++---- packages/runtime/src/cli/repl.ts | 5 +++-- packages/vm/tests/integration/crypto.test.ts | 5 +++-- packages/vm/tests/integration/request.test.ts | 19 ++++++++++++++----- 5 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 .changeset/large-dancers-stare.md diff --git a/.changeset/large-dancers-stare.md b/.changeset/large-dancers-stare.md new file mode 100644 index 00000000..89fabd16 --- /dev/null +++ b/.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 diff --git a/packages/primitives/src/primitives/fetch.js b/packages/primitives/src/primitives/fetch.js index 9192c683..c0ee3b3f 100644 --- a/packages/primitives/src/primitives/fetch.js +++ b/packages/primitives/src/primitives/fetch.js @@ -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 } /** diff --git a/packages/runtime/src/cli/repl.ts b/packages/runtime/src/cli/repl.ts index 1c568d05..64b10270 100644 --- a/packages/runtime/src/cli/repl.ts +++ b/packages/runtime/src/cli/repl.ts @@ -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) => { @@ -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 ?? '' diff --git a/packages/vm/tests/integration/crypto.test.ts b/packages/vm/tests/integration/crypto.test.ts index ab0f9ad0..050907f7 100644 --- a/packages/vm/tests/integration/crypto.test.ts +++ b/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() @@ -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() diff --git a/packages/vm/tests/integration/request.test.ts b/packages/vm/tests/integration/request.test.ts index 963d38ed..e280a47f 100644 --- a/packages/vm/tests/integration/request.test.ts +++ b/packages/vm/tests/integration/request.test.ts @@ -1,19 +1,19 @@ /** * @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) { @@ -21,14 +21,23 @@ test('throws when the URL is malformed', async () => { } }) -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') +})