Skip to content

Commit

Permalink
[primitives] don't override Blob global (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schniz committed May 28, 2023
1 parent c88eb89 commit a6b6124
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-garlics-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@edge-runtime/primitives': patch
---

Don't remove Blob from global scope, and use global Blob if available
8 changes: 8 additions & 0 deletions packages/integration-tests/tests/blob.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const test18 = process.version.startsWith('v18.') ? test : test.skip

test18('Blob is available globally after importing the ponyfill', () => {
const blob = Blob
expect(blob).toBeDefined()
require('@edge-runtime/ponyfill')
expect(Blob).toBe(blob)
})
20 changes: 0 additions & 20 deletions packages/primitives/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,6 @@ async function bundlePackage() {
)
},
},
/**
* Modern Node.js versions include Blob globally which makes the Blob
* polyfill fail as if it was running on the browser so we attempt to
* remove if from `global` always.
*/
{
name: 'hide-builtin-blob',
setup: (build) => {
build.onLoad({ filter: /blob-polyfill/ }, async (args) => {
return {
contents: Buffer.concat([
Buffer.from(
`(() => { try { global.Blob = undefined; } catch {} })(); `
),
await fs.promises.readFile(args.path),
]),
}
})
},
},
],
})

Expand Down
33 changes: 27 additions & 6 deletions packages/primitives/src/primitives/load.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
/// <reference path="../injectSourceCode.d.ts" />

import Module from 'module'
import nodeCrypto from 'crypto'
Expand Down Expand Up @@ -152,12 +153,32 @@ export function load(scopedContext = {}) {
})

/** @type {import('../../type-definitions/blob')} */
const blobImpl = requireWithFakeGlobalScope({
context,
id: 'blob.js',
sourceCode: injectSourceCode('./blob.js'),
scopedContext: { ...streamsImpl, ...scopedContext },
})
const blobImpl = (() => {
if (typeof scopedContext.Blob === 'function') {
return { Blob: scopedContext.Blob }
}

if (typeof Blob === 'function') {
return { Blob }
}

/** @type {any} */
const global = {
...streamsImpl,
...scopedContext,
}

const globalGlobal = { ...global, Blob: undefined }
Object.setPrototypeOf(globalGlobal, globalThis)

global.global = globalGlobal
return requireWithFakeGlobalScope({
context,
id: 'blob.js',
sourceCode: injectSourceCode('./blob.js'),
scopedContext: global,
})
})()
assign(context, {
Blob: blobImpl.Blob,
})
Expand Down

1 comment on commit a6b6124

@vercel
Copy link

@vercel vercel bot commented on a6b6124 May 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

edge-runtime – ./

edge-runtime.vercel.app
edge-runtime.vercel.sh
edge-runtime-git-main.vercel.sh

Please sign in to comment.