Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jul 12, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/next/server/lib/squoosh/impl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import semver from 'next/dist/compiled/semver'
import { codecs as supportedFormats, preprocessors } from './codecs'
import ImageData from './image_data'

// Broken in 9.1.269 so hopefully fixed in 9.2.0 :fingerscrossed:
// See https://crbug.com/1224882
const V8_FIXED_VERSION = '9.2.0'
const DELAY_MS = 100
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> {
ijjk marked this conversation as resolved.
Show resolved Hide resolved
const isAppleM1 = process.arch === 'arm64' && process.platform === 'darwin'
const [major, minor, patch] = process.versions.v8.split('.')
const version = [major, minor, patch].join('.')
if (isAppleM1 && semver.lt(version, V8_FIXED_VERSION)) {
return delayOnce(DELAY_MS)
}
return Promise.resolve()
}

export async function decodeBuffer(
_buffer: Buffer | Uint8Array
): Promise<ImageData> {
Expand Down Expand Up @@ -39,6 +65,7 @@ export async function resize({ image, width, height }: ResizeOpts) {

const p = preprocessors['resize']
const m = await p.instantiate()
await maybeDelay()
return await m(image.data, image.width, image.height, {
...p.defaultOptions,
width,
Expand All @@ -54,6 +81,7 @@ export async function encodeJpeg(

const e = supportedFormats['mozjpeg']
const m = await e.enc()
await maybeDelay()
const r = await m.encode!(image.data, image.width, image.height, {
...e.defaultEncoderOptions,
quality,
Expand All @@ -69,6 +97,7 @@ export async function encodeWebp(

const e = supportedFormats['webp']
const m = await e.enc()
await maybeDelay()
const r = await m.encode!(image.data, image.width, image.height, {
...e.defaultEncoderOptions,
quality,
Expand All @@ -83,6 +112,7 @@ export async function encodePng(

const e = supportedFormats['oxipng']
const m = await e.enc()
await maybeDelay()
const r = await m.encode(image.data, image.width, image.height, {
...e.defaultEncoderOptions,
})
Expand Down