Unverified Commit 52cf7fe5 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure setImmediate and punycode are polyfilled (#32768)

* Ensure setimmediate and punycode are polyfilled

* update compiled
parent 82adaee0
......@@ -615,6 +615,8 @@ export default async function getBaseWebpackConfig(
: 'react-dom/cjs/react-dom-server.browser.production.min',
}
: {}),
setimmediate: 'next/dist/compiled/setimmediate',
},
...(targetWeb
? {
......@@ -632,7 +634,7 @@ export default async function getBaseWebpackConfig(
https: require.resolve('next/dist/compiled/https-browserify'),
os: require.resolve('next/dist/compiled/os-browserify'),
path: require.resolve('next/dist/compiled/path-browserify'),
punycode: require.resolve('punycode'),
punycode: require.resolve('next/dist/compiled/punycode'),
process: require.resolve('next/dist/compiled/process'),
// Handled in separate alias
querystring: require.resolve('next/dist/compiled/querystring-es3'),
......@@ -650,6 +652,7 @@ export default async function getBaseWebpackConfig(
vm: require.resolve('next/dist/compiled/vm-browserify'),
zlib: require.resolve('next/dist/compiled/browserify-zlib'),
events: require.resolve('next/dist/compiled/events/'),
setImmediate: require.resolve('next/dist/compiled/setimmediate'),
},
}
: undefined),
......
{"name":"punycode","main":"punycode.js","author":{"name":"Mathias Bynens","url":"https://mathiasbynens.be/"},"license":"MIT"}
(function(){"use strict";var t={701:function(t){const e=2147483647;const o=36;const n=1;const r=26;const c=38;const s=700;const i=72;const f=128;const u="-";const l=/^xn--/;const a=/[^\0-\x7E]/;const p=/[\x2E\u3002\uFF0E\uFF61]/g;const d={overflow:"Overflow: input needs wider integers to process","not-basic":"Illegal input >= 0x80 (not a basic code point)","invalid-input":"Invalid input"};const _=o-n;const h=Math.floor;const w=String.fromCharCode;function error(t){throw new RangeError(d[t])}function map(t,e){const o=[];let n=t.length;while(n--){o[n]=e(t[n])}return o}function mapDomain(t,e){const o=t.split("@");let n="";if(o.length>1){n=o[0]+"@";t=o[1]}t=t.replace(p,".");const r=t.split(".");const c=map(r,e).join(".");return n+c}function ucs2decode(t){const e=[];let o=0;const n=t.length;while(o<n){const r=t.charCodeAt(o++);if(r>=55296&&r<=56319&&o<n){const n=t.charCodeAt(o++);if((n&64512)==56320){e.push(((r&1023)<<10)+(n&1023)+65536)}else{e.push(r);o--}}else{e.push(r)}}return e}const ucs2encode=t=>String.fromCodePoint(...t);const basicToDigit=function(t){if(t-48<10){return t-22}if(t-65<26){return t-65}if(t-97<26){return t-97}return o};const digitToBasic=function(t,e){return t+22+75*(t<26)-((e!=0)<<5)};const adapt=function(t,e,n){let i=0;t=n?h(t/s):t>>1;t+=h(t/e);for(;t>_*r>>1;i+=o){t=h(t/_)}return h(i+(_+1)*t/(t+c))};const decode=function(t){const c=[];const s=t.length;let l=0;let a=f;let p=i;let d=t.lastIndexOf(u);if(d<0){d=0}for(let e=0;e<d;++e){if(t.charCodeAt(e)>=128){error("not-basic")}c.push(t.charCodeAt(e))}for(let i=d>0?d+1:0;i<s;){let f=l;for(let c=1,f=o;;f+=o){if(i>=s){error("invalid-input")}const u=basicToDigit(t.charCodeAt(i++));if(u>=o||u>h((e-l)/c)){error("overflow")}l+=u*c;const a=f<=p?n:f>=p+r?r:f-p;if(u<a){break}const d=o-a;if(c>h(e/d)){error("overflow")}c*=d}const u=c.length+1;p=adapt(l-f,u,f==0);if(h(l/u)>e-a){error("overflow")}a+=h(l/u);l%=u;c.splice(l++,0,a)}return String.fromCodePoint(...c)};const encode=function(t){const c=[];t=ucs2decode(t);let s=t.length;let l=f;let a=0;let p=i;for(const e of t){if(e<128){c.push(w(e))}}let d=c.length;let _=d;if(d){c.push(u)}while(_<s){let s=e;for(const e of t){if(e>=l&&e<s){s=e}}const i=_+1;if(s-l>h((e-a)/i)){error("overflow")}a+=(s-l)*i;l=s;for(const s of t){if(s<l&&++a>e){error("overflow")}if(s==l){let t=a;for(let e=o;;e+=o){const s=e<=p?n:e>=p+r?r:e-p;if(t<s){break}const i=t-s;const f=o-s;c.push(w(digitToBasic(s+i%f,0)));t=h(i/f)}c.push(w(digitToBasic(t,0)));p=adapt(a,i,_==d);a=0;++_}}++a;++l}return c.join("")};const toUnicode=function(t){return mapDomain(t,(function(t){return l.test(t)?decode(t.slice(4).toLowerCase()):t}))};const toASCII=function(t){return mapDomain(t,(function(t){return a.test(t)?"xn--"+encode(t):t}))};const v={version:"2.1.0",ucs2:{decode:ucs2decode,encode:ucs2encode},decode:decode,encode:encode,toASCII:toASCII,toUnicode:toUnicode};t.exports=v}};var e={};function __nccwpck_require__(o){var n=e[o];if(n!==undefined){return n.exports}var r=e[o]={exports:{}};var c=true;try{t[o](r,r.exports,__nccwpck_require__);c=false}finally{if(c)delete e[o]}return r.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var o=__nccwpck_require__(701);module.exports=o})();
\ No newline at end of file
{"name":"setimmediate","main":"setImmediate.js","author":"YuzuJS","license":"MIT"}
(function(){var e={619:function(){(function(e,t){"use strict";if(e.setImmediate){return}var n=1;var a={};var s=false;var i=e.document;var r;function setImmediate(e){if(typeof e!=="function"){e=new Function(""+e)}var t=new Array(arguments.length-1);for(var s=0;s<t.length;s++){t[s]=arguments[s+1]}var i={callback:e,args:t};a[n]=i;r(n);return n++}function clearImmediate(e){delete a[e]}function run(e){var n=e.callback;var a=e.args;switch(a.length){case 0:n();break;case 1:n(a[0]);break;case 2:n(a[0],a[1]);break;case 3:n(a[0],a[1],a[2]);break;default:n.apply(t,a);break}}function runIfPresent(e){if(s){setTimeout(runIfPresent,0,e)}else{var t=a[e];if(t){s=true;try{run(t)}finally{clearImmediate(e);s=false}}}}function installNextTickImplementation(){r=function(e){process.nextTick((function(){runIfPresent(e)}))}}function canUsePostMessage(){if(e.postMessage&&!e.importScripts){var t=true;var n=e.onmessage;e.onmessage=function(){t=false};e.postMessage("","*");e.onmessage=n;return t}}function installPostMessageImplementation(){var t="setImmediate$"+Math.random()+"$";var onGlobalMessage=function(n){if(n.source===e&&typeof n.data==="string"&&n.data.indexOf(t)===0){runIfPresent(+n.data.slice(t.length))}};if(e.addEventListener){e.addEventListener("message",onGlobalMessage,false)}else{e.attachEvent("onmessage",onGlobalMessage)}r=function(n){e.postMessage(t+n,"*")}}function installMessageChannelImplementation(){var e=new MessageChannel;e.port1.onmessage=function(e){var t=e.data;runIfPresent(t)};r=function(t){e.port2.postMessage(t)}}function installReadyStateChangeImplementation(){var e=i.documentElement;r=function(t){var n=i.createElement("script");n.onreadystatechange=function(){runIfPresent(t);n.onreadystatechange=null;e.removeChild(n);n=null};e.appendChild(n)}}function installSetTimeoutImplementation(){r=function(e){setTimeout(runIfPresent,0,e)}}var o=Object.getPrototypeOf&&Object.getPrototypeOf(e);o=o&&o.setTimeout?o:e;if({}.toString.call(e.process)==="[object process]"){installNextTickImplementation()}else if(canUsePostMessage()){installPostMessageImplementation()}else if(e.MessageChannel){installMessageChannelImplementation()}else if(i&&"onreadystatechange"in i.createElement("script")){installReadyStateChangeImplementation()}else{installSetTimeoutImplementation()}o.setImmediate=setImmediate;o.clearImmediate=clearImmediate})(typeof self==="undefined"?typeof global==="undefined"?this:global:self)}};if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var t={};e[619]();module.exports=t})();
\ No newline at end of file
(function(){var e={619:function(){(function(e,t){"use strict";if(e.setImmediate){return}var n=1;var a={};var i=false;var o=e.document;var r;function setImmediate(e){if(typeof e!=="function"){e=new Function(""+e)}var t=new Array(arguments.length-1);for(var i=0;i<t.length;i++){t[i]=arguments[i+1]}var o={callback:e,args:t};a[n]=o;r(n);return n++}function clearImmediate(e){delete a[e]}function run(e){var n=e.callback;var a=e.args;switch(a.length){case 0:n();break;case 1:n(a[0]);break;case 2:n(a[0],a[1]);break;case 3:n(a[0],a[1],a[2]);break;default:n.apply(t,a);break}}function runIfPresent(e){if(i){setTimeout(runIfPresent,0,e)}else{var t=a[e];if(t){i=true;try{run(t)}finally{clearImmediate(e);i=false}}}}function installNextTickImplementation(){r=function(e){process.nextTick((function(){runIfPresent(e)}))}}function canUsePostMessage(){if(e.postMessage&&!e.importScripts){var t=true;var n=e.onmessage;e.onmessage=function(){t=false};e.postMessage("","*");e.onmessage=n;return t}}function installPostMessageImplementation(){var t="setImmediate$"+Math.random()+"$";var onGlobalMessage=function(n){if(n.source===e&&typeof n.data==="string"&&n.data.indexOf(t)===0){runIfPresent(+n.data.slice(t.length))}};if(e.addEventListener){e.addEventListener("message",onGlobalMessage,false)}else{e.attachEvent("onmessage",onGlobalMessage)}r=function(n){e.postMessage(t+n,"*")}}function installMessageChannelImplementation(){var e=new MessageChannel;e.port1.onmessage=function(e){var t=e.data;runIfPresent(t)};r=function(t){e.port2.postMessage(t)}}function installReadyStateChangeImplementation(){var e=o.documentElement;r=function(t){var n=o.createElement("script");n.onreadystatechange=function(){runIfPresent(t);n.onreadystatechange=null;e.removeChild(n);n=null};e.appendChild(n)}}function installSetTimeoutImplementation(){r=function(e){setTimeout(runIfPresent,0,e)}}var s=Object.getPrototypeOf&&Object.getPrototypeOf(e);s=s&&s.setTimeout?s:e;if({}.toString.call(e.process)==="[object process]"){installNextTickImplementation()}else if(canUsePostMessage()){installPostMessageImplementation()}else if(e.MessageChannel){installMessageChannelImplementation()}else if(o&&"onreadystatechange"in o.createElement("script")){installReadyStateChangeImplementation()}else{installSetTimeoutImplementation()}s.setImmediate=setImmediate;s.clearImmediate=clearImmediate})(typeof self==="undefined"?typeof global==="undefined"?this:global:self)},832:function(e,t,n){var a=typeof global!=="undefined"&&global||typeof self!=="undefined"&&self||window;var i=Function.prototype.apply;t.setTimeout=function(){return new Timeout(i.call(setTimeout,a,arguments),clearTimeout)};t.setInterval=function(){return new Timeout(i.call(setInterval,a,arguments),clearInterval)};t.clearTimeout=t.clearInterval=function(e){if(e){e.close()}};function Timeout(e,t){this._id=e;this._clearFn=t}Timeout.prototype.unref=Timeout.prototype.ref=function(){};Timeout.prototype.close=function(){this._clearFn.call(a,this._id)};t.enroll=function(e,t){clearTimeout(e._idleTimeoutId);e._idleTimeout=t};t.unenroll=function(e){clearTimeout(e._idleTimeoutId);e._idleTimeout=-1};t._unrefActive=t.active=function(e){clearTimeout(e._idleTimeoutId);var t=e._idleTimeout;if(t>=0){e._idleTimeoutId=setTimeout((function onTimeout(){if(e._onTimeout)e._onTimeout()}),t)}};n(619);t.setImmediate=typeof self!=="undefined"&&self.setImmediate||typeof global!=="undefined"&&global.setImmediate||this&&this.setImmediate;t.clearImmediate=typeof self!=="undefined"&&self.clearImmediate||typeof global!=="undefined"&&global.clearImmediate||this&&this.clearImmediate}};var t={};function __nccwpck_require__(n){var a=t[n];if(a!==undefined){return a.exports}var i=t[n]={exports:{}};var o=true;try{e[n].call(i.exports,i,i.exports,__nccwpck_require__);o=false}finally{if(o)delete t[n]}return i.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var n=__nccwpck_require__(832);module.exports=n})();
\ No newline at end of file
(function(){var e={832:function(e,t,i){var o=typeof global!=="undefined"&&global||typeof self!=="undefined"&&self||window;var n=Function.prototype.apply;t.setTimeout=function(){return new Timeout(n.call(setTimeout,o,arguments),clearTimeout)};t.setInterval=function(){return new Timeout(n.call(setInterval,o,arguments),clearInterval)};t.clearTimeout=t.clearInterval=function(e){if(e){e.close()}};function Timeout(e,t){this._id=e;this._clearFn=t}Timeout.prototype.unref=Timeout.prototype.ref=function(){};Timeout.prototype.close=function(){this._clearFn.call(o,this._id)};t.enroll=function(e,t){clearTimeout(e._idleTimeoutId);e._idleTimeout=t};t.unenroll=function(e){clearTimeout(e._idleTimeoutId);e._idleTimeout=-1};t._unrefActive=t.active=function(e){clearTimeout(e._idleTimeoutId);var t=e._idleTimeout;if(t>=0){e._idleTimeoutId=setTimeout((function onTimeout(){if(e._onTimeout)e._onTimeout()}),t)}};i(505);t.setImmediate=typeof self!=="undefined"&&self.setImmediate||typeof global!=="undefined"&&global.setImmediate||this&&this.setImmediate;t.clearImmediate=typeof self!=="undefined"&&self.clearImmediate||typeof global!=="undefined"&&global.clearImmediate||this&&this.clearImmediate},505:function(e){"use strict";e.exports=require("next/dist/compiled/setimmediate")}};var t={};function __nccwpck_require__(i){var o=t[i];if(o!==undefined){return o.exports}var n=t[i]={exports:{}};var r=true;try{e[i].call(n.exports,n,n.exports,__nccwpck_require__);r=false}finally{if(r)delete t[i]}return n.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var i=__nccwpck_require__(832);module.exports=i})();
\ No newline at end of file
......@@ -236,6 +236,7 @@
"postcss-safe-parser": "6.0.0",
"postcss-scss": "3.0.5",
"postcss-value-parser": "4.1.0",
"punycode": "2.1.1",
"process": "0.11.10",
"querystring-es3": "0.2.1",
"raw-body": "2.4.1",
......@@ -244,6 +245,7 @@
"sass-loader": "10.2.0",
"schema-utils2": "npm:schema-utils@2.7.1",
"schema-utils3": "npm:schema-utils@3.0.0",
"setimmediate": "1.0.5",
"semver": "7.3.2",
"send": "0.17.1",
"source-map": "0.6.1",
......
......@@ -510,6 +510,32 @@ export async function ncc_util(task, opts) {
.target('compiled/util')
}
// eslint-disable-next-line camelcase
export async function ncc_punycode(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('punycode/')))
.ncc({
packageName: 'punycode',
externals,
mainFields: ['browser', 'main'],
target: 'es5',
})
.target('compiled/punycode')
}
// eslint-disable-next-line camelcase
export async function ncc_set_immediate(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('setimmediate/')))
.ncc({
packageName: 'setimmediate',
externals,
mainFields: ['browser', 'main'],
target: 'es5',
})
.target('compiled/setimmediate')
}
// eslint-disable-next-line camelcase
export async function ncc_timers_browserify(task, opts) {
await task
......@@ -518,7 +544,10 @@ export async function ncc_timers_browserify(task, opts) {
)
.ncc({
packageName: 'timers-browserify',
externals,
externals: {
...externals,
setimmediate: 'next/dist/compiled/setimmediate',
},
mainFields: ['browser', 'main'],
target: 'es5',
})
......@@ -1444,6 +1473,8 @@ export async function ncc(task, opts) {
'ncc_querystring_es3',
'ncc_string_decoder',
'ncc_util',
'ncc_punycode',
'ncc_set_immediate',
'ncc_timers_browserify',
'ncc_tty_browserify',
'ncc_vm_browserify',
......
/* eslint-env jest */
import { join } from 'path'
import webdriver from 'next-webdriver'
import { findPort, launchApp, killApp } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
const appDir = join(__dirname, '../')
let appPort
let app
describe('theme-ui SWC option', () => {
let next: NextInstance
describe('Basic Features', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'node-browser-polyfills/pages')),
},
})
})
afterAll(() => killApp(app))
afterAll(() => next.destroy())
it('should polyfill Node.js modules', async () => {
const browser = await webdriver(appPort, '/node-browser-polyfills')
it('should have polyfilled correctly', async () => {
const browser = await webdriver(next.url, '/')
await browser.waitForCondition('window.didRender')
......
......@@ -19,6 +19,7 @@ import timers from 'timers'
import tty from 'tty'
import util from 'util'
import zlib from 'zlib'
import 'setimmediate'
export default function NodeBrowserPolyfillPage() {
const [state, setState] = useState({})
......@@ -50,26 +51,28 @@ export default function NodeBrowserPolyfillPage() {
assert.ok(!!util.inspect)
assert.ok(!!zlib.Gzip)
setState({
assert: true,
buffer: Buffer.from('hello world').toString('utf8'),
constants: constants.E2BIG,
hash: crypto.createHash('sha256').update('hello world').digest('hex'),
domain: true,
os: os.EOL,
path: path.join('/hello/world', 'test.txt'),
process: process.title,
querystring: querystring.stringify({ a: 'b' }),
stream: closedStream,
stringDecoder: true,
sys: true,
timers: true,
tty: true,
util: true,
http: true,
https: true,
vm: vm.runInNewContext('a + 5', { a: 100 }),
zlib: true,
setImmediate(() => {
setState({
assert: true,
buffer: Buffer.from('hello world').toString('utf8'),
constants: constants.E2BIG,
hash: crypto.createHash('sha256').update('hello world').digest('hex'),
domain: true,
os: os.EOL,
path: path.join('/hello/world', 'test.txt'),
process: process.title,
querystring: querystring.stringify({ a: 'b' }),
stream: closedStream,
stringDecoder: true,
sys: true,
timers: true,
tty: true,
util: true,
http: true,
https: true,
vm: vm.runInNewContext('a + 5', { a: 100 }),
zlib: true,
})
})
}, [])
......
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
}
......@@ -16277,14 +16277,14 @@ punycode@1.3.2:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
integrity sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=
punycode@2.1.1, punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
punycode@^1.2.4:
version "1.4.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"
punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
pupa@^2.0.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/pupa/-/pupa-2.1.1.tgz#f5e8fd4afc2c5d97828faa523549ed8744a20d62"
......@@ -17565,7 +17565,7 @@ set-value@^2.0.0, set-value@^2.0.1:
is-plain-object "^2.0.3"
split-string "^3.0.1"
setimmediate@^1.0.4:
setimmediate@1.0.5, setimmediate@^1.0.4:
version "1.0.5"
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285"
integrity sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment