Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitlab/scripts/publish_npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ if [ -d "./dist" ]; then
rm -rf ./dist
fi
yarn build
cp ./dist/handler.cjs ./dist/handler.js
npm publish
1 change: 0 additions & 1 deletion scripts/publish_prod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ else
rm -rf ./dist
fi
yarn build
cp ./dist/handler.cjs ./dist/handler.js
yarn publish --new-version "$NEW_VERSION"
fi

Expand Down
13 changes: 11 additions & 2 deletions scripts/update_dist_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ echo "$MAIN_CONSTANTS" |
echo "$TRACE_CONSTANTS" |
sed "s/\(ddtraceVersion =\) \"\(X\.X\.X\)\"/\1 \"$DD_TRACE_VERSION\"/" > ./dist/trace/constants.js

echo "Copying handler js files"
cp src/handler.* dist/
echo "Copying handler files"
# Only handler.mjs ships as a Lambda entry point. Lambda's bootstrap resolves
# `dist/handler.handler` to handler.mjs (it falls through `.js` -> `.mjs`),
# and handler.mjs's async `load()` handles both CJS and ESM user modules, so
# a separate `.js` / `.cjs` variant is no longer needed.
#
# Remove any stale `dist/handler.js` / `dist/handler.cjs` left over from prior
# builds — tsc doesn't clean dist between incremental compiles, and shipping
# either of those files would re-introduce the resolver bug this PR fixes.
rm -f dist/handler.js dist/handler.cjs
cp src/handler.mjs dist/
cp src/init.js dist/init.js
cp src/runtime/module_importer.js dist/runtime/
43 changes: 0 additions & 43 deletions src/handler.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { load, loadSync } from "./user-function";
export { load } from "./user-function";
export { subscribeToDC, getTraceTree, clearTraceTree, RequireNode } from "./require-tracer"
105 changes: 0 additions & 105 deletions src/runtime/user-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,45 +156,6 @@ async function _tryRequire(appRoot: string, moduleRoot: string, module: string):
return require(nodeStylePath);
}

/**
* Attempt to load the user's module.
* Attempts to directly resolve the module relative to the application root,
* then falls back to the more general require().
*/
function _tryRequireSync(appRoot: string, moduleRoot: string, module: string): Promise<any> {
const lambdaStylePath = path.resolve(appRoot, moduleRoot, module);
// Extensionless files are loaded via require.
const extensionless = _tryRequireFile(lambdaStylePath);
if (extensionless) {
return extensionless;
}
// If package.json type != module, .js files are loaded via require.
const pjHasModule = _hasPackageJsonTypeModule(lambdaStylePath);
if (!pjHasModule) {
const loaded = _tryRequireFile(lambdaStylePath, ".js");
if (loaded) {
return loaded;
}
}
// If still not loaded, try .js, .mjs, and .cjs in that order.
// Files ending with .js are loaded as ES modules when the nearest parent package.json
// file contains a top-level field "type" with a value of "module".
// https://nodejs.org/api/packages.html#packages_type
const loaded = _tryRequireFile(lambdaStylePath, ".cjs");
if (loaded) {
return loaded;
}
// Why not just require(module)?
// Because require() is relative to __dirname, not process.cwd(). And the
// runtime implementation is not located in /var/task
// This won't work (yet) for esModules as import.meta.resolve is still experimental
// See: https://nodejs.org/api/esm.html#esm_import_meta_resolve_specifier_parent
const nodeStylePath = require.resolve(module, {
paths: [appRoot, moduleRoot],
});
return require(nodeStylePath);
}

/**
* Load the user's application or throw a descriptive error.
* @throws Runtime errors in two cases
Expand All @@ -221,26 +182,6 @@ async function _loadUserApp(
}
}

function _loadUserAppSync(
appRoot: string,
moduleRoot: string,
module: string
): Promise<any> {
try {
return _tryRequireSync(appRoot, moduleRoot, module);
} catch (e) {
if (e instanceof SyntaxError) {
throw new UserCodeSyntaxError(<any>e);
// @ts-ignore
} else if (e.code !== undefined && e.code === "MODULE_NOT_FOUND") {
// @ts-ignore
throw new ImportModuleError(e);
} else {
throw e;
}
}
}

function _throwIfInvalidHandler(fullHandlerString: string): void {
if (fullHandlerString.includes(RELATIVE_PATH_SUBSTRING)) {
throw new MalformedHandlerName(
Expand Down Expand Up @@ -294,49 +235,3 @@ export const load = async function (

return handlerFunc;
};

/**
* Load the user's function with the approot and the handler string.
* @param appRoot {string}
* The path to the application root.
* @param handlerString {string}
* The user-provided handler function in the form 'module.function'.
* @return userFuction {function}
* The user's handler function. This function will be passed the event body,
* the context object, and the callback function.
* @throws In five cases:-
* 1 - if the handler string is incorrectly formatted an error is thrown
* 2 - if the module referenced by the handler cannot be loaded
* 3 - if the function in the handler does not exist in the module
* 4 - if a property with the same name, but isn't a function, exists on the
* module
* 5 - the handler includes illegal character sequences (like relative paths
* for traversing up the filesystem '..')
* Errors for scenarios known by the runtime, will be wrapped by Runtime.* errors.
*/
export const loadSync = function (
appRoot: string,
fullHandlerString: string
) {
_throwIfInvalidHandler(fullHandlerString);

const [moduleRoot, moduleAndHandler] = _moduleRootAndHandler(
fullHandlerString
);
const [module, handlerPath] = _splitHandlerString(moduleAndHandler);

const userApp = _loadUserAppSync(appRoot, moduleRoot, module);
const handlerFunc = _resolveHandler(userApp, handlerPath);

if (!handlerFunc) {
throw new HandlerNotFound(
`${fullHandlerString} is undefined or not exported`
);
}

if (typeof handlerFunc !== "function") {
throw new HandlerNotFound(`${fullHandlerString} is not a function`);
}

return handlerFunc;
};
Loading