@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
78 lines • 3.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.startTraceHandler = void 0;
const rpc_errors_1 = require("@metamask/rpc-errors");
const superstruct_1 = require("@metamask/superstruct");
const utils_1 = require("@metamask/utils");
const hookNames = {
startTrace: true,
};
const StartTraceParametersStruct = (0, superstruct_1.object)({
data: (0, superstruct_1.exactOptional)((0, superstruct_1.record)((0, superstruct_1.string)(), (0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)(), (0, superstruct_1.boolean)()]))),
id: (0, superstruct_1.exactOptional)((0, superstruct_1.string)()),
name: (0, superstruct_1.string)(),
parentContext: (0, superstruct_1.exactOptional)(utils_1.JsonStruct),
startTime: (0, superstruct_1.exactOptional)((0, superstruct_1.number)()),
tags: (0, superstruct_1.exactOptional)((0, superstruct_1.record)((0, superstruct_1.string)(), (0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)(), (0, superstruct_1.boolean)()]))),
});
/**
* Handler for the `snap_startTrace` method.
*
* @internal
*/
exports.startTraceHandler = {
implementation: getStartTraceImplementation,
hookNames,
actionNames: ['SnapController:getSnap'],
};
/**
* The `snap_startTrace` method implementation. This method is used to start a
* performance trace in Sentry. It is only available to preinstalled Snaps.
*
* @param request - The JSON-RPC request object.
* @param response - The JSON-RPC response object.
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
* function.
* @param end - The `json-rpc-engine` "end" callback.
* @param hooks - The RPC method hooks.
* @param hooks.startTrace - The hook function to start a performance trace.
* @param messenger - The messenger used to call controller actions.
* @returns Nothing.
*/
function getStartTraceImplementation(request, response, _next, end, { startTrace }, messenger) {
const snap = messenger.call('SnapController:getSnap', request.origin);
if (!snap?.preinstalled) {
return end(rpc_errors_1.rpcErrors.methodNotFound());
}
const { params } = request;
try {
const validatedParams = getValidatedParams(params);
response.result = startTrace(validatedParams);
}
catch (error) {
return end(error);
}
return end();
}
/**
* Validate the parameters for the `snap_startTrace` method.
*
* @param params - Parameters to validate.
* @returns Validated parameters.
* @throws Throws RPC error if validation fails.
*/
function getValidatedParams(params) {
try {
return (0, superstruct_1.create)(params, StartTraceParametersStruct);
}
catch (error) {
if (error instanceof superstruct_1.StructError) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: `Invalid params: ${error.message}.`,
});
}
/* istanbul ignore next */
throw rpc_errors_1.rpcErrors.internal();
}
}
//# sourceMappingURL=startTrace.cjs.map