Skip to content

Commit 21e0be4

Browse files
author
Thomas Krause
committed
Remove require calls #160
1 parent 7b431fa commit 21e0be4

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http11Server.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// The typings for module are incomplete and can't be augmented, so import as any.
2-
const Module = require('module');
1+
import ModuleTemp from 'module';
32
import * as http from 'http';
43
import { AddressInfo, Socket } from 'net';
54
import * as path from 'path';
@@ -8,6 +7,9 @@ import InvocationRequest from '../../../InvocationData/InvocationRequest';
87
import ModuleSourceType from '../../../InvocationData/ModuleSourceType';
98
import { getTempIdentifier, respondWithError, setup } from './Shared';
109

10+
// The typings for module are incomplete and can't be augmented, so import as any.
11+
const Module = ModuleTemp as any;
12+
1113
// Setup
1214
const [args, projectDir, moduleResolutionPaths] = setup();
1315

@@ -118,8 +120,8 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
118120
}
119121
} else if (invocationRequest.moduleSourceType === ModuleSourceType.File) {
120122
const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource);
121-
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
122-
} else {
123+
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
124+
} else {
123125
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
124126
return;
125127
}
@@ -133,7 +135,8 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
133135
if (invocationRequest.exportName != null) {
134136
functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName];
135137
if (functionToInvoke == null) {
136-
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`);
138+
let availableExports = Object.keys(exports).join(', ');
139+
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}. Available exports are: ${availableExports}`);
137140
return;
138141
}
139142
if (!(typeof functionToInvoke === 'function')) {

src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http20Server.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// The typings for module are incomplete and can't be augmented, so import as any.
2-
const Module = require('module');
1+
import ModuleTemp from 'module';
32
import * as http2 from 'http2';
43
import { AddressInfo } from 'net';
54
import * as path from 'path';
@@ -8,6 +7,9 @@ import InvocationRequest from '../../../InvocationData/InvocationRequest';
87
import ModuleSourceType from '../../../InvocationData/ModuleSourceType';
98
import { getTempIdentifier, respondWithError, setup } from './Shared';
109

10+
// The typings for module are incomplete and can't be augmented, so import as any.
11+
const Module = ModuleTemp as any;
12+
1113
// Setup
1214
const [args, projectDir, moduleResolutionPaths] = setup();
1315

@@ -104,8 +106,8 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
104106
}
105107
} else if (invocationRequest.moduleSourceType === ModuleSourceType.File) {
106108
const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource);
107-
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
108-
} else {
109+
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
110+
} else {
109111
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
110112
return;
111113
}
@@ -119,7 +121,8 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
119121
if (invocationRequest.exportName != null) {
120122
functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName];
121123
if (functionToInvoke == null) {
122-
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`);
124+
let availableExports = Object.keys(exports).join(', ');
125+
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}. Available exports are: ${availableExports}`);
123126
return;
124127
}
125128
if (!(typeof functionToInvoke === 'function')) {

src/NodeJS/Javascript/Servers/OutOfProcess/Http/Shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const path = require("path");
1+
import * as path from 'path';
22
import * as stream from 'stream';
33
import * as http from 'http';
44
import * as http2 from 'http2';

0 commit comments

Comments
 (0)