@@ -11,6 +11,12 @@ const {
1111 StringPrototypeReplace,
1212} = primordials ;
1313
14+ let _TYPES = null ;
15+ function lazyTypes ( ) {
16+ if ( _TYPES !== null ) return _TYPES ;
17+ return _TYPES = require ( 'internal/util/types' ) ;
18+ }
19+
1420const {
1521 stripBOM,
1622 loadNativeModule
@@ -26,7 +32,10 @@ const createDynamicModule = require(
2632const { fileURLToPath, URL } = require ( 'url' ) ;
2733const { debuglog } = require ( 'internal/util/debuglog' ) ;
2834const { emitExperimentalWarning } = require ( 'internal/util' ) ;
29- const { ERR_UNKNOWN_BUILTIN_MODULE } = require ( 'internal/errors' ) . codes ;
35+ const {
36+ ERR_UNKNOWN_BUILTIN_MODULE ,
37+ ERR_INVALID_RETURN_PROPERTY_VALUE
38+ } = require ( 'internal/errors' ) . codes ;
3039const { maybeCacheSourceMap } = require ( 'internal/source_map/source_map_cache' ) ;
3140const moduleWrap = internalBinding ( 'module_wrap' ) ;
3241const { ModuleWrap } = moduleWrap ;
@@ -39,6 +48,30 @@ const debug = debuglog('esm');
3948const translators = new SafeMap ( ) ;
4049exports . translators = translators ;
4150
51+ let DECODER = null ;
52+ function assertBufferSource ( body , allowString , hookName ) {
53+ if ( allowString && typeof body === 'string' ) {
54+ return ;
55+ }
56+ const { isArrayBufferView, isAnyArrayBuffer } = lazyTypes ( ) ;
57+ if ( isArrayBufferView ( body ) || isAnyArrayBuffer ( body ) ) {
58+ return ;
59+ }
60+ throw new ERR_INVALID_RETURN_PROPERTY_VALUE (
61+ `${ allowString ? 'string, ' : '' } array buffer, or typed array` ,
62+ hookName ,
63+ 'source' ,
64+ body
65+ ) ;
66+ }
67+
68+ function stringify ( body ) {
69+ if ( typeof body === 'string' ) return body ;
70+ assertBufferSource ( body , false , 'transformSource' ) ;
71+ DECODER = DECODER === null ? new TextDecoder ( ) : DECODER ;
72+ return DECODER . decode ( body ) ;
73+ }
74+
4275function errPath ( url ) {
4376 const parsed = new URL ( url ) ;
4477 if ( parsed . protocol === 'file:' ) {
@@ -80,9 +113,10 @@ function initializeImportMeta(meta, { url }) {
80113translators . set ( 'module' , async function moduleStrategy ( url ) {
81114 let { source } = await this . _getSource (
82115 url , { format : 'module' } , defaultGetSource ) ;
83- source = ` ${ source } ` ;
116+ assertBufferSource ( source , true , 'getSource' ) ;
84117 ( { source } = await this . _transformSource (
85118 source , { url, format : 'module' } , defaultTransformSource ) ) ;
119+ source = stringify ( source ) ;
86120 maybeCacheSourceMap ( url , source ) ;
87121 debug ( `Translating StandardModule ${ url } ` ) ;
88122 const module = new ModuleWrap ( url , undefined , source , 0 , 0 ) ;
@@ -157,9 +191,10 @@ translators.set('json', async function jsonStrategy(url) {
157191 }
158192 let { source } = await this . _getSource (
159193 url , { format : 'json' } , defaultGetSource ) ;
160- source = ` ${ source } ` ;
194+ assertBufferSource ( source , true , 'getSource' ) ;
161195 ( { source } = await this . _transformSource (
162196 source , { url, format : 'json' } , defaultTransformSource ) ) ;
197+ source = stringify ( source ) ;
163198 if ( pathname ) {
164199 // A require call could have been called on the same file during loading and
165200 // that resolves synchronously. To make sure we always return the identical
@@ -200,8 +235,10 @@ translators.set('wasm', async function(url) {
200235 emitExperimentalWarning ( 'Importing Web Assembly modules' ) ;
201236 let { source } = await this . _getSource (
202237 url , { format : 'wasm' } , defaultGetSource ) ;
238+ assertBufferSource ( source , false , 'getSource' ) ;
203239 ( { source } = await this . _transformSource (
204240 source , { url, format : 'wasm' } , defaultTransformSource ) ) ;
241+ assertBufferSource ( source , false , 'transformSource' ) ;
205242 debug ( `Translating WASMModule ${ url } ` ) ;
206243 let compiled ;
207244 try {
0 commit comments