From f47a251c71a552df9105c2bee0ae21dc6715a183 Mon Sep 17 00:00:00 2001 From: Rishabh Poddar Date: Tue, 21 Dec 2021 22:03:16 +0530 Subject: [PATCH 1/2] Updates with-supertokens example: Fixes init race condition (#32706) Updates with-supertokens example to fix a race condition for initialising the supertokens-node SDK. Earlier we used to do it in _app.js in an async manner, so if `getServerSideProps` is called before the `supertokens.init` is called, the page load would fail. Now, we call `supertokens.init` (for the node SDK) in the `getServerSideProps` function itself. Co-authored-by: kant01ne <5072452+kant01ne@users.noreply.github.com> Co-authored-by: Bhumil Sarvaiya <21988812+bhumilsarvaiya@users.noreply.github.com> Co-authored-by: Joel Coutinho <6310783+jscyo@users.noreply.github.com> --- examples/with-supertokens/pages/_app.js | 8 -------- examples/with-supertokens/pages/index.js | 4 ++++ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/examples/with-supertokens/pages/_app.js b/examples/with-supertokens/pages/_app.js index c44a9b2670e9e..d596135dc05c0 100644 --- a/examples/with-supertokens/pages/_app.js +++ b/examples/with-supertokens/pages/_app.js @@ -6,16 +6,8 @@ import * as SuperTokensConfig from '../config/frontendConfig' import Session from 'supertokens-auth-react/recipe/session' import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword' -async function initNode() { - const supertokensNode = await import('supertokens-node') - const { backendConfig } = await import('../config/backendConfig') - supertokensNode.init(backendConfig()) -} - if (typeof window !== 'undefined') { SuperTokensReact.init(SuperTokensConfig.frontendConfig()) -} else { - initNode().catch(console.error) } function MyApp({ Component, pageProps }) { diff --git a/examples/with-supertokens/pages/index.js b/examples/with-supertokens/pages/index.js index e1ce8fbd1a6fb..0629fa410da25 100644 --- a/examples/with-supertokens/pages/index.js +++ b/examples/with-supertokens/pages/index.js @@ -3,6 +3,8 @@ import Head from 'next/head' import styles from '../styles/Home.module.css' import ThirdPartyEmailPassword from 'supertokens-auth-react/recipe/thirdpartyemailpassword' import dynamic from 'next/dynamic' +import supertokensNode from 'supertokens-node' +import { backendConfig } from '../config/backendConfig' import Session from 'supertokens-node/recipe/session' const ThirdPartyEmailPasswordAuthNoSSR = dynamic( @@ -13,6 +15,8 @@ const ThirdPartyEmailPasswordAuthNoSSR = dynamic( ) export async function getServerSideProps(context) { + // this runs on the backend, so we must call init on supertokens-node SDK + supertokensNode.init(backendConfig()) let session try { session = await Session.getSession(context.req, context.res) From 0eba5b2558abc0a7a63006efcf74ae5f7571a690 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Tue, 21 Dec 2021 18:12:53 +0100 Subject: [PATCH 2/2] web runtime: add AbortController & AbortSignal (#32089) It adds AbortController and AbortSignal Web runtimes APIs to be used by the user at Edge Functions. For doing that it delegates into `abort-controller` dependency that has been frozen to prevent any modification. Co-authored-by: Zhang Zhi <20026577+fytriht@users.noreply.github.com> --- docs/api-reference/edge-runtime.md | 4 ---- .../next/compiled/abort-controller/LICENSE | 21 +++++++++++++++++ .../abort-controller/abort-controller.js | 1 + .../compiled/abort-controller/package.json | 1 + packages/next/server/web/sandbox/context.ts | 6 +++++ packages/next/taskfile.js | 11 +++++++++ packages/next/types/misc.d.ts | 6 +++++ .../core/pages/interface/_middleware.js | 23 +++++++++++++++++++ .../middleware/core/test/index.test.js | 12 ++++++++++ 9 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 packages/next/compiled/abort-controller/LICENSE create mode 100644 packages/next/compiled/abort-controller/abort-controller.js create mode 100644 packages/next/compiled/abort-controller/package.json diff --git a/docs/api-reference/edge-runtime.md b/docs/api-reference/edge-runtime.md index 95e3f914472a8..3c722513acac3 100644 --- a/docs/api-reference/edge-runtime.md +++ b/docs/api-reference/edge-runtime.md @@ -82,10 +82,6 @@ The following JavaScript language features are disabled, and **will not work:** - [`eval`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval): Evaluates JavaScript code represented as a string - [`new Function(evalString)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function): Creates a new function with the code provided as an argument -The following Web APIs are currently not supported, but will be in the future: - -- [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController): Abort one or more Web requests when desired - ## Related
diff --git a/packages/next/compiled/abort-controller/LICENSE b/packages/next/compiled/abort-controller/LICENSE new file mode 100644 index 0000000000000..c914149a6f845 --- /dev/null +++ b/packages/next/compiled/abort-controller/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Toru Nagashima + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/next/compiled/abort-controller/abort-controller.js b/packages/next/compiled/abort-controller/abort-controller.js new file mode 100644 index 0000000000000..98a59d22a0b88 --- /dev/null +++ b/packages/next/compiled/abort-controller/abort-controller.js @@ -0,0 +1 @@ +(()=>{"use strict";var e={27:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:true});var r=n(369);class AbortSignal extends r.EventTarget{constructor(){super();throw new TypeError("AbortSignal cannot be constructed directly")}get aborted(){const e=o.get(this);if(typeof e!=="boolean"){throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this===null?"null":typeof this}`)}return e}}r.defineEventAttribute(AbortSignal.prototype,"abort");function createAbortSignal(){const e=Object.create(AbortSignal.prototype);r.EventTarget.call(e);o.set(e,false);return e}function abortSignal(e){if(o.get(e)!==false){return}o.set(e,true);e.dispatchEvent({type:"abort"})}const o=new WeakMap;Object.defineProperties(AbortSignal.prototype,{aborted:{enumerable:true}});if(typeof Symbol==="function"&&typeof Symbol.toStringTag==="symbol"){Object.defineProperty(AbortSignal.prototype,Symbol.toStringTag,{configurable:true,value:"AbortSignal"})}class AbortController{constructor(){i.set(this,createAbortSignal())}get signal(){return getSignal(this)}abort(){abortSignal(getSignal(this))}}const i=new WeakMap;function getSignal(e){const t=i.get(e);if(t==null){throw new TypeError(`Expected 'this' to be an 'AbortController' object, but got ${e===null?"null":typeof e}`)}return t}Object.defineProperties(AbortController.prototype,{signal:{enumerable:true},abort:{enumerable:true}});if(typeof Symbol==="function"&&typeof Symbol.toStringTag==="symbol"){Object.defineProperty(AbortController.prototype,Symbol.toStringTag,{configurable:true,value:"AbortController"})}t.AbortController=AbortController;t.AbortSignal=AbortSignal;t["default"]=AbortController;e.exports=AbortController;e.exports.AbortController=e.exports["default"]=AbortController;e.exports.AbortSignal=AbortSignal},369:(e,t)=>{Object.defineProperty(t,"__esModule",{value:true});const n=new WeakMap;const r=new WeakMap;function pd(e){const t=n.get(e);console.assert(t!=null,"'this' is expected an Event object, but got",e);return t}function setCancelFlag(e){if(e.passiveListener!=null){if(typeof console!=="undefined"&&typeof console.error==="function"){console.error("Unable to preventDefault inside passive event listener invocation.",e.passiveListener)}return}if(!e.event.cancelable){return}e.canceled=true;if(typeof e.event.preventDefault==="function"){e.event.preventDefault()}}function Event(e,t){n.set(this,{eventTarget:e,event:t,eventPhase:2,currentTarget:e,canceled:false,stopped:false,immediateStopped:false,passiveListener:null,timeStamp:t.timeStamp||Date.now()});Object.defineProperty(this,"isTrusted",{value:false,enumerable:true});const r=Object.keys(t);for(let e=0;e0){const e=new Array(arguments.length);for(let t=0;t { + const res = await fetchViaHTTP( + context.appPort, + '/interface/abort-controller' + ) + const response = await res.json() + + expect('error' in response).toBe(true) + expect(response.error.name).toBe('AbortError') + expect(response.error.message).toBe('The user aborted a request.') + }) + it(`${locale} should validate request url parameters from a static route`, async () => { const res = await fetchViaHTTP( context.appPort,