Skip to content

Commit

Permalink
Remove require calls JeringTech#160
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Krause committed Jul 26, 2023
1 parent 7b431fa commit 21e0be4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http11Server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();

Expand Down Expand Up @@ -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;
}
Expand All @@ -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')) {
Expand Down
13 changes: 8 additions & 5 deletions src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http20Server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();

Expand Down Expand Up @@ -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;
}
Expand All @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion src/NodeJS/Javascript/Servers/OutOfProcess/Http/Shared.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit 21e0be4

Please sign in to comment.