Unverified Commit ed19335d authored by Steven's avatar Steven Committed by GitHub

Workaround for Node.js 16+ on Apple Silicon M1 (#27031)

This PR is a workaround for #24421 by adding an artificial delay when Apple M1 + buggy Node.js is detected.

Node.js 14 is unaffected because it M1 still reports `arch=x64`. Starting in Node.js 16, M1 reports `arch=arm64`.

V8 Bug: https://crbug.com/1224882

Node.js Issue: https://github.com/nodejs/node/issues/39327
parent 849a79fb
import semver from 'next/dist/compiled/semver'
import { codecs as supportedFormats, preprocessors } from './codecs' import { codecs as supportedFormats, preprocessors } from './codecs'
import ImageData from './image_data' import ImageData from './image_data'
// Fixed in Node.js 16.5.0 and newer.
// See https://github.com/nodejs/node/pull/39337
// Eventually, remove this delay when engines is updated.
// See https://git.io/JCTr0
const FIXED_VERSION = '16.5.0'
const DELAY_MS = 1000
let _promise: Promise<void> | undefined
function delayOnce(ms: number): Promise<void> {
if (!_promise) {
_promise = new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
return _promise
}
function maybeDelay(): Promise<void> {
const isAppleM1 = process.arch === 'arm64' && process.platform === 'darwin'
if (isAppleM1 && semver.lt(process.version, FIXED_VERSION)) {
return delayOnce(DELAY_MS)
}
return Promise.resolve()
}
export async function decodeBuffer( export async function decodeBuffer(
_buffer: Buffer | Uint8Array _buffer: Buffer | Uint8Array
): Promise<ImageData> { ): Promise<ImageData> {
...@@ -39,6 +65,7 @@ export async function resize({ image, width, height }: ResizeOpts) { ...@@ -39,6 +65,7 @@ export async function resize({ image, width, height }: ResizeOpts) {
const p = preprocessors['resize'] const p = preprocessors['resize']
const m = await p.instantiate() const m = await p.instantiate()
await maybeDelay()
return await m(image.data, image.width, image.height, { return await m(image.data, image.width, image.height, {
...p.defaultOptions, ...p.defaultOptions,
width, width,
...@@ -54,6 +81,7 @@ export async function encodeJpeg( ...@@ -54,6 +81,7 @@ export async function encodeJpeg(
const e = supportedFormats['mozjpeg'] const e = supportedFormats['mozjpeg']
const m = await e.enc() const m = await e.enc()
await maybeDelay()
const r = await m.encode!(image.data, image.width, image.height, { const r = await m.encode!(image.data, image.width, image.height, {
...e.defaultEncoderOptions, ...e.defaultEncoderOptions,
quality, quality,
...@@ -69,6 +97,7 @@ export async function encodeWebp( ...@@ -69,6 +97,7 @@ export async function encodeWebp(
const e = supportedFormats['webp'] const e = supportedFormats['webp']
const m = await e.enc() const m = await e.enc()
await maybeDelay()
const r = await m.encode!(image.data, image.width, image.height, { const r = await m.encode!(image.data, image.width, image.height, {
...e.defaultEncoderOptions, ...e.defaultEncoderOptions,
quality, quality,
...@@ -83,6 +112,7 @@ export async function encodePng( ...@@ -83,6 +112,7 @@ export async function encodePng(
const e = supportedFormats['oxipng'] const e = supportedFormats['oxipng']
const m = await e.enc() const m = await e.enc()
await maybeDelay()
const r = await m.encode(image.data, image.width, image.height, { const r = await m.encode(image.data, image.width, image.height, {
...e.defaultEncoderOptions, ...e.defaultEncoderOptions,
}) })
......
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