From 3aafe1941832444bee6875ddedf46b44908a2b49 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Tue, 11 May 2021 10:48:03 +0200 Subject: [PATCH] fix(gatsby): fix proxy creation on read-only properties (#31346) (cherry picked from commit c210f1dba38b16da851a72f9f864d0d21f9878f7) --- .../cache-dir/ssr-builtin-trackers/__tests__/fs.js | 7 +++++++ .../tracking-unsafe-module-wrapper.js | 12 ++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 packages/gatsby/cache-dir/ssr-builtin-trackers/__tests__/fs.js diff --git a/packages/gatsby/cache-dir/ssr-builtin-trackers/__tests__/fs.js b/packages/gatsby/cache-dir/ssr-builtin-trackers/__tests__/fs.js new file mode 100644 index 0000000000000..dca7213108605 --- /dev/null +++ b/packages/gatsby/cache-dir/ssr-builtin-trackers/__tests__/fs.js @@ -0,0 +1,7 @@ +const fs = require(`../fs`) + +describe(`tracking fs`, () => { + it(`doesn't crash on accessing fs.constants`, () => { + expect(() => fs.constants).not.toThrow() + }) +}) diff --git a/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js b/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js index c6393c5f27fc9..7b6ecba006593 100644 --- a/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js +++ b/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js @@ -11,6 +11,18 @@ function createProxyHandler(prefix, options) { return value } + const fieldDescriptor = Object.getOwnPropertyDescriptor(target, key) + if (fieldDescriptor && !fieldDescriptor.writable) { + // this is to prevent errors like: + // ``` + // TypeError: 'get' on proxy: property 'constants' is a read - only and + // non - configurable data property on the proxy target but the proxy + // did not return its actual value + // (expected '[object Object]' but got '[object Object]') + // ``` + return value + } + if (typeof value === `function`) { return function wrapper(...args) { const myErrorHolder = {