From 21e0be40d2abc059f51cee25536050a75d39dd70 Mon Sep 17 00:00:00 2001 From: Thomas Krause Date: Wed, 26 Jul 2023 16:36:53 +0200 Subject: [PATCH] Remove require calls #160 --- .../Servers/OutOfProcess/Http/Http11Server.ts | 13 ++++++++----- .../Servers/OutOfProcess/Http/Http20Server.ts | 13 ++++++++----- .../Javascript/Servers/OutOfProcess/Http/Shared.ts | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http11Server.ts b/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http11Server.ts index 6288178..d124e85 100644 --- a/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http11Server.ts +++ b/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http11Server.ts @@ -1,5 +1,4 @@ -// The typings for module are incomplete and can't be augmented, so import as any. -const Module = require('module'); +import ModuleTemp from 'module'; import * as http from 'http'; import { AddressInfo, Socket } from 'net'; import * as path from 'path'; @@ -8,6 +7,9 @@ import InvocationRequest from '../../../InvocationData/InvocationRequest'; import ModuleSourceType from '../../../InvocationData/ModuleSourceType'; import { getTempIdentifier, respondWithError, setup } from './Shared'; +// The typings for module are incomplete and can't be augmented, so import as any. +const Module = ModuleTemp as any; + // Setup const [args, projectDir, moduleResolutionPaths] = setup(); @@ -118,8 +120,8 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp } } else if (invocationRequest.moduleSourceType === ModuleSourceType.File) { const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource); - exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/')); - } else { + exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/')); + } else { respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`); return; } @@ -133,7 +135,8 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp if (invocationRequest.exportName != null) { functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName]; if (functionToInvoke == null) { - respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`); + let availableExports = Object.keys(exports).join(', '); + respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}. Available exports are: ${availableExports}`); return; } if (!(typeof functionToInvoke === 'function')) { diff --git a/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http20Server.ts b/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http20Server.ts index 18bbc49..dace52c 100644 --- a/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http20Server.ts +++ b/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http20Server.ts @@ -1,5 +1,4 @@ -// The typings for module are incomplete and can't be augmented, so import as any. -const Module = require('module'); +import ModuleTemp from 'module'; import * as http2 from 'http2'; import { AddressInfo } from 'net'; import * as path from 'path'; @@ -8,6 +7,9 @@ import InvocationRequest from '../../../InvocationData/InvocationRequest'; import ModuleSourceType from '../../../InvocationData/ModuleSourceType'; import { getTempIdentifier, respondWithError, setup } from './Shared'; +// The typings for module are incomplete and can't be augmented, so import as any. +const Module = ModuleTemp as any; + // Setup const [args, projectDir, moduleResolutionPaths] = setup(); @@ -104,8 +106,8 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2 } } else if (invocationRequest.moduleSourceType === ModuleSourceType.File) { const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource); - exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/')); - } else { + exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/')); + } else { respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`); return; } @@ -119,7 +121,8 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2 if (invocationRequest.exportName != null) { functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName]; if (functionToInvoke == null) { - respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`); + let availableExports = Object.keys(exports).join(', '); + respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}. Available exports are: ${availableExports}`); return; } if (!(typeof functionToInvoke === 'function')) { diff --git a/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Shared.ts b/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Shared.ts index bba3455..6038066 100644 --- a/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Shared.ts +++ b/src/NodeJS/Javascript/Servers/OutOfProcess/Http/Shared.ts @@ -1,4 +1,4 @@ -const path = require("path"); +import * as path from 'path'; import * as stream from 'stream'; import * as http from 'http'; import * as http2 from 'http2';