From 1b096079dacc01662c850e4e94e8e91be37ef262 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 5 Nov 2021 17:47:25 +0100 Subject: [PATCH 1/2] share collections in middleware vm context Resolves #30477 --- packages/next/server/web/sandbox/sandbox.ts | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/next/server/web/sandbox/sandbox.ts b/packages/next/server/web/sandbox/sandbox.ts index 94afaf0b501f..7317429467e7 100644 --- a/packages/next/server/web/sandbox/sandbox.ts +++ b/packages/next/server/web/sandbox/sandbox.ts @@ -86,6 +86,30 @@ export async function run(params: { TransformStream, URL, URLSearchParams, + + // Indexed collections + Array, + Int8Array, + Uint8Array, + Uint8ClampedArray, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + Float32Array, + Float64Array, + BigInt64Array, + BigUint64Array, + + // Keyed collections + Map, + Set, + WeakMap, + WeakSet, + + // Structured data + ArrayBuffer, + SharedArrayBuffer, } context.self = context From 9765a53fd72a80c291c4fd352f897b6b0ffc91ee Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 5 Nov 2021 19:39:35 +0100 Subject: [PATCH 2/2] add test --- .../core/pages/interface/_middleware.js | 24 ++++++++++++++++++- .../middleware/core/test/index.test.js | 6 +++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/test/integration/middleware/core/pages/interface/_middleware.js b/test/integration/middleware/core/pages/interface/_middleware.js index af15210b13f4..d20893a42782 100644 --- a/test/integration/middleware/core/pages/interface/_middleware.js +++ b/test/integration/middleware/core/pages/interface/_middleware.js @@ -2,7 +2,7 @@ import { NextResponse } from 'next/server' -export function middleware(request) { +export async function middleware(request) { const url = request.nextUrl if (url.pathname.endsWith('/globalthis')) { @@ -13,6 +13,28 @@ export function middleware(request) { }) } + if (url.pathname.endsWith('/webcrypto')) { + const response = {} + try { + const algorithm = { + name: 'RSA-PSS', + hash: 'SHA-256', + publicExponent: new Uint8Array([0x01, 0x00, 0x01]), + modulusLength: 2048, + } + const keyUsages = ['sign', 'verify'] + await crypto.subtle.generateKey(algorithm, false, keyUsages) + } catch (err) { + response.error = true + } finally { + return new NextResponse(JSON.stringify(response), { + headers: { + 'content-type': 'application/json; charset=utf-8', + }, + }) + } + } + return new Response(null, { headers: { 'req-url-basepath': request.nextUrl.basePath, diff --git a/test/integration/middleware/core/test/index.test.js b/test/integration/middleware/core/test/index.test.js index 2c386dfe9970..19fd085cc2cf 100644 --- a/test/integration/middleware/core/test/index.test.js +++ b/test/integration/middleware/core/test/index.test.js @@ -350,6 +350,12 @@ function interfaceTests(locale = '') { expect(globals.length > 0).toBe(true) }) + it(`${locale} collection constructors are shared`, async () => { + const res = await fetchViaHTTP(context.appPort, '/interface/webcrypto') + const response = await res.json() + expect('error' in response).toBe(false) + }) + it(`${locale} should validate request url parameters from a static route`, async () => { const res = await fetchViaHTTP( context.appPort,