Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate streaming writer chunk type in testing
- Loading branch information
Showing
3 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Use CJS format for represent next/dist/compiled/node-polyfill-web-streams.js | ||
|
||
const { | ||
WritableStreamDefaultWriter, | ||
ReadableStream, | ||
TransformStream, | ||
} = require('next/dist/compiled/web-streams-polyfill') | ||
|
||
const OriginWritableStreamWrite = WritableStreamDefaultWriter.prototype.write | ||
|
||
// Override writable stream write method to validate chunk type. | ||
// Currently CF workers only allow to write the encoded chunk in Uint8Array format. | ||
WritableStreamDefaultWriter.prototype.write = (chunk) => { | ||
if (!(chunk instanceof Uint8Array)) { | ||
throw new Error('Writing non Uint8Array chunk in streaming is not allowed') | ||
} | ||
OriginWritableStreamWrite(chunk) | ||
} | ||
|
||
global.ReadableStream = ReadableStream | ||
global.TransformStream = TransformStream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const mod = require('module') | ||
|
||
const hookPropertyMap = new Map([ | ||
[ | ||
/node-polyfill-web-streams/, | ||
require.resolve('../__mocks__/node-polyfill-web-streams.js'), | ||
], | ||
]) | ||
|
||
function matchModule(request) { | ||
for (const [key, value] of hookPropertyMap) { | ||
if (key.test(request)) { | ||
return value | ||
} | ||
} | ||
return null | ||
} | ||
|
||
const resolveFilename = mod._resolveFilename | ||
mod._resolveFilename = function (request, parent, isMain, options) { | ||
const hookResolved = matchModule(request) // hookPropertyMap.get(request) | ||
if (hookResolved) request = hookResolved | ||
return resolveFilename.call(mod, request, parent, isMain, options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters